]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linux/linux_ioctl.c
Merge OpenSSL 1.1.1a.
[FreeBSD/FreeBSD.git] / sys / compat / linux / linux_ioctl.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1994-1995 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include "opt_compat.h"
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sysproto.h>
37 #include <sys/capsicum.h>
38 #include <sys/cdio.h>
39 #include <sys/dvdio.h>
40 #include <sys/conf.h>
41 #include <sys/disk.h>
42 #include <sys/consio.h>
43 #include <sys/ctype.h>
44 #include <sys/fcntl.h>
45 #include <sys/file.h>
46 #include <sys/filedesc.h>
47 #include <sys/filio.h>
48 #include <sys/jail.h>
49 #include <sys/kbio.h>
50 #include <sys/kernel.h>
51 #include <sys/linker_set.h>
52 #include <sys/lock.h>
53 #include <sys/malloc.h>
54 #include <sys/proc.h>
55 #include <sys/sbuf.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
58 #include <sys/soundcard.h>
59 #include <sys/stdint.h>
60 #include <sys/sx.h>
61 #include <sys/sysctl.h>
62 #include <sys/tty.h>
63 #include <sys/uio.h>
64 #include <sys/types.h>
65 #include <sys/mman.h>
66 #include <sys/resourcevar.h>
67
68 #include <net/if.h>
69 #include <net/if_var.h>
70 #include <net/if_dl.h>
71 #include <net/if_types.h>
72
73 #include <dev/evdev/input.h>
74 #include <dev/usb/usb_ioctl.h>
75
76 #ifdef COMPAT_LINUX32
77 #include <machine/../linux32/linux.h>
78 #include <machine/../linux32/linux32_proto.h>
79 #else
80 #include <machine/../linux/linux.h>
81 #include <machine/../linux/linux_proto.h>
82 #endif
83
84 #include <compat/linux/linux_ioctl.h>
85 #include <compat/linux/linux_mib.h>
86 #include <compat/linux/linux_socket.h>
87 #include <compat/linux/linux_timer.h>
88 #include <compat/linux/linux_util.h>
89
90 #include <contrib/v4l/videodev.h>
91 #include <compat/linux/linux_videodev_compat.h>
92
93 #include <contrib/v4l/videodev2.h>
94 #include <compat/linux/linux_videodev2_compat.h>
95
96 #include <cam/scsi/scsi_sg.h>
97
98 CTASSERT(LINUX_IFNAMSIZ == IFNAMSIZ);
99
100 static linux_ioctl_function_t linux_ioctl_cdrom;
101 static linux_ioctl_function_t linux_ioctl_vfat;
102 static linux_ioctl_function_t linux_ioctl_console;
103 static linux_ioctl_function_t linux_ioctl_hdio;
104 static linux_ioctl_function_t linux_ioctl_disk;
105 static linux_ioctl_function_t linux_ioctl_socket;
106 static linux_ioctl_function_t linux_ioctl_sound;
107 static linux_ioctl_function_t linux_ioctl_termio;
108 static linux_ioctl_function_t linux_ioctl_private;
109 static linux_ioctl_function_t linux_ioctl_drm;
110 static linux_ioctl_function_t linux_ioctl_sg;
111 static linux_ioctl_function_t linux_ioctl_v4l;
112 static linux_ioctl_function_t linux_ioctl_v4l2;
113 static linux_ioctl_function_t linux_ioctl_special;
114 static linux_ioctl_function_t linux_ioctl_fbsd_usb;
115 static linux_ioctl_function_t linux_ioctl_evdev;
116
117 static struct linux_ioctl_handler cdrom_handler =
118 { linux_ioctl_cdrom, LINUX_IOCTL_CDROM_MIN, LINUX_IOCTL_CDROM_MAX };
119 static struct linux_ioctl_handler vfat_handler =
120 { linux_ioctl_vfat, LINUX_IOCTL_VFAT_MIN, LINUX_IOCTL_VFAT_MAX };
121 static struct linux_ioctl_handler console_handler =
122 { linux_ioctl_console, LINUX_IOCTL_CONSOLE_MIN, LINUX_IOCTL_CONSOLE_MAX };
123 static struct linux_ioctl_handler hdio_handler =
124 { linux_ioctl_hdio, LINUX_IOCTL_HDIO_MIN, LINUX_IOCTL_HDIO_MAX };
125 static struct linux_ioctl_handler disk_handler =
126 { linux_ioctl_disk, LINUX_IOCTL_DISK_MIN, LINUX_IOCTL_DISK_MAX };
127 static struct linux_ioctl_handler socket_handler =
128 { linux_ioctl_socket, LINUX_IOCTL_SOCKET_MIN, LINUX_IOCTL_SOCKET_MAX };
129 static struct linux_ioctl_handler sound_handler =
130 { linux_ioctl_sound, LINUX_IOCTL_SOUND_MIN, LINUX_IOCTL_SOUND_MAX };
131 static struct linux_ioctl_handler termio_handler =
132 { linux_ioctl_termio, LINUX_IOCTL_TERMIO_MIN, LINUX_IOCTL_TERMIO_MAX };
133 static struct linux_ioctl_handler private_handler =
134 { linux_ioctl_private, LINUX_IOCTL_PRIVATE_MIN, LINUX_IOCTL_PRIVATE_MAX };
135 static struct linux_ioctl_handler drm_handler =
136 { linux_ioctl_drm, LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX };
137 static struct linux_ioctl_handler sg_handler =
138 { linux_ioctl_sg, LINUX_IOCTL_SG_MIN, LINUX_IOCTL_SG_MAX };
139 static struct linux_ioctl_handler video_handler =
140 { linux_ioctl_v4l, LINUX_IOCTL_VIDEO_MIN, LINUX_IOCTL_VIDEO_MAX };
141 static struct linux_ioctl_handler video2_handler =
142 { linux_ioctl_v4l2, LINUX_IOCTL_VIDEO2_MIN, LINUX_IOCTL_VIDEO2_MAX };
143 static struct linux_ioctl_handler fbsd_usb =
144 { linux_ioctl_fbsd_usb, FBSD_LUSB_MIN, FBSD_LUSB_MAX };
145 static struct linux_ioctl_handler evdev_handler =
146 { linux_ioctl_evdev, LINUX_IOCTL_EVDEV_MIN, LINUX_IOCTL_EVDEV_MAX };
147
148 DATA_SET(linux_ioctl_handler_set, cdrom_handler);
149 DATA_SET(linux_ioctl_handler_set, vfat_handler);
150 DATA_SET(linux_ioctl_handler_set, console_handler);
151 DATA_SET(linux_ioctl_handler_set, hdio_handler);
152 DATA_SET(linux_ioctl_handler_set, disk_handler);
153 DATA_SET(linux_ioctl_handler_set, socket_handler);
154 DATA_SET(linux_ioctl_handler_set, sound_handler);
155 DATA_SET(linux_ioctl_handler_set, termio_handler);
156 DATA_SET(linux_ioctl_handler_set, private_handler);
157 DATA_SET(linux_ioctl_handler_set, drm_handler);
158 DATA_SET(linux_ioctl_handler_set, sg_handler);
159 DATA_SET(linux_ioctl_handler_set, video_handler);
160 DATA_SET(linux_ioctl_handler_set, video2_handler);
161 DATA_SET(linux_ioctl_handler_set, fbsd_usb);
162 DATA_SET(linux_ioctl_handler_set, evdev_handler);
163
164 #ifdef __i386__
165 static TAILQ_HEAD(, linux_ioctl_handler_element) linux_ioctl_handlers =
166     TAILQ_HEAD_INITIALIZER(linux_ioctl_handlers);
167 static struct sx linux_ioctl_sx;
168 SX_SYSINIT(linux_ioctl, &linux_ioctl_sx, "Linux ioctl handlers");
169 #else
170 extern TAILQ_HEAD(, linux_ioctl_handler_element) linux_ioctl_handlers;
171 extern struct sx linux_ioctl_sx;
172 #endif
173 #ifdef COMPAT_LINUX32
174 static TAILQ_HEAD(, linux_ioctl_handler_element) linux32_ioctl_handlers =
175     TAILQ_HEAD_INITIALIZER(linux32_ioctl_handlers);
176 #endif
177
178 /*
179  * hdio related ioctls for VMWare support
180  */
181
182 struct linux_hd_geometry {
183         u_int8_t        heads;
184         u_int8_t        sectors;
185         u_int16_t       cylinders;
186         u_int32_t       start;
187 };
188
189 struct linux_hd_big_geometry {
190         u_int8_t        heads;
191         u_int8_t        sectors;
192         u_int32_t       cylinders;
193         u_int32_t       start;
194 };
195
196 static int
197 linux_ioctl_hdio(struct thread *td, struct linux_ioctl_args *args)
198 {
199         struct file *fp;
200         int error;
201         u_int sectorsize, fwcylinders, fwheads, fwsectors;
202         off_t mediasize, bytespercyl;
203
204         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
205         if (error != 0)
206                 return (error);
207         switch (args->cmd & 0xffff) {
208         case LINUX_HDIO_GET_GEO:
209         case LINUX_HDIO_GET_GEO_BIG:
210                 error = fo_ioctl(fp, DIOCGMEDIASIZE,
211                         (caddr_t)&mediasize, td->td_ucred, td);
212                 if (!error)
213                         error = fo_ioctl(fp, DIOCGSECTORSIZE,
214                                 (caddr_t)&sectorsize, td->td_ucred, td);
215                 if (!error)
216                         error = fo_ioctl(fp, DIOCGFWHEADS,
217                                 (caddr_t)&fwheads, td->td_ucred, td);
218                 if (!error)
219                         error = fo_ioctl(fp, DIOCGFWSECTORS,
220                                 (caddr_t)&fwsectors, td->td_ucred, td);
221                 /*
222                  * XXX: DIOCGFIRSTOFFSET is not yet implemented, so
223                  * so pretend that GEOM always says 0. This is NOT VALID
224                  * for slices or partitions, only the per-disk raw devices.
225                  */
226
227                 fdrop(fp, td);
228                 if (error)
229                         return (error);
230                 /*
231                  * 1. Calculate the number of bytes in a cylinder,
232                  *    given the firmware's notion of heads and sectors
233                  *    per cylinder.
234                  * 2. Calculate the number of cylinders, given the total
235                  *    size of the media.
236                  * All internal calculations should have 64-bit precision.
237                  */
238                 bytespercyl = (off_t) sectorsize * fwheads * fwsectors;
239                 fwcylinders = mediasize / bytespercyl;
240 #if defined(DEBUG)
241                 linux_msg(td, "HDIO_GET_GEO: mediasize %jd, c/h/s %d/%d/%d, "
242                           "bpc %jd",
243                           (intmax_t)mediasize, fwcylinders, fwheads, fwsectors,
244                           (intmax_t)bytespercyl);
245 #endif
246                 if ((args->cmd & 0xffff) == LINUX_HDIO_GET_GEO) {
247                         struct linux_hd_geometry hdg;
248
249                         hdg.cylinders = fwcylinders;
250                         hdg.heads = fwheads;
251                         hdg.sectors = fwsectors;
252                         hdg.start = 0;
253                         error = copyout(&hdg, (void *)args->arg, sizeof(hdg));
254                 } else if ((args->cmd & 0xffff) == LINUX_HDIO_GET_GEO_BIG) {
255                         struct linux_hd_big_geometry hdbg;
256
257                         memset(&hdbg, 0, sizeof(hdbg));
258                         hdbg.cylinders = fwcylinders;
259                         hdbg.heads = fwheads;
260                         hdbg.sectors = fwsectors;
261                         hdbg.start = 0;
262                         error = copyout(&hdbg, (void *)args->arg, sizeof(hdbg));
263                 }
264                 return (error);
265                 break;
266         default:
267                 /* XXX */
268                 linux_msg(td,
269                         "ioctl fd=%d, cmd=0x%x ('%c',%d) is not implemented",
270                         args->fd, (int)(args->cmd & 0xffff),
271                         (int)(args->cmd & 0xff00) >> 8,
272                         (int)(args->cmd & 0xff));
273                 break;
274         }
275         fdrop(fp, td);
276         return (ENOIOCTL);
277 }
278
279 static int
280 linux_ioctl_disk(struct thread *td, struct linux_ioctl_args *args)
281 {
282         struct file *fp;
283         int error;
284         u_int sectorsize;
285         off_t mediasize;
286
287         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
288         if (error != 0)
289                 return (error);
290         switch (args->cmd & 0xffff) {
291         case LINUX_BLKGETSIZE:
292                 error = fo_ioctl(fp, DIOCGSECTORSIZE,
293                     (caddr_t)&sectorsize, td->td_ucred, td);
294                 if (!error)
295                         error = fo_ioctl(fp, DIOCGMEDIASIZE,
296                             (caddr_t)&mediasize, td->td_ucred, td);
297                 fdrop(fp, td);
298                 if (error)
299                         return (error);
300                 sectorsize = mediasize / sectorsize;
301                 /*
302                  * XXX: How do we know we return the right size of integer ?
303                  */
304                 return (copyout(&sectorsize, (void *)args->arg,
305                     sizeof(sectorsize)));
306                 break;
307         case LINUX_BLKSSZGET:
308                 error = fo_ioctl(fp, DIOCGSECTORSIZE,
309                     (caddr_t)&sectorsize, td->td_ucred, td);
310                 fdrop(fp, td);
311                 if (error)
312                         return (error);
313                 return (copyout(&sectorsize, (void *)args->arg,
314                     sizeof(sectorsize)));
315                 break;
316         }
317         fdrop(fp, td);
318         return (ENOIOCTL);
319 }
320
321 /*
322  * termio related ioctls
323  */
324
325 struct linux_termio {
326         unsigned short c_iflag;
327         unsigned short c_oflag;
328         unsigned short c_cflag;
329         unsigned short c_lflag;
330         unsigned char c_line;
331         unsigned char c_cc[LINUX_NCC];
332 };
333
334 struct linux_termios {
335         unsigned int c_iflag;
336         unsigned int c_oflag;
337         unsigned int c_cflag;
338         unsigned int c_lflag;
339         unsigned char c_line;
340         unsigned char c_cc[LINUX_NCCS];
341 };
342
343 struct linux_winsize {
344         unsigned short ws_row, ws_col;
345         unsigned short ws_xpixel, ws_ypixel;
346 };
347
348 struct speedtab {
349         int sp_speed;                   /* Speed. */
350         int sp_code;                    /* Code. */
351 };
352
353 static struct speedtab sptab[] = {
354         { B0, LINUX_B0 }, { B50, LINUX_B50 },
355         { B75, LINUX_B75 }, { B110, LINUX_B110 },
356         { B134, LINUX_B134 }, { B150, LINUX_B150 },
357         { B200, LINUX_B200 }, { B300, LINUX_B300 },
358         { B600, LINUX_B600 }, { B1200, LINUX_B1200 },
359         { B1800, LINUX_B1800 }, { B2400, LINUX_B2400 },
360         { B4800, LINUX_B4800 }, { B9600, LINUX_B9600 },
361         { B19200, LINUX_B19200 }, { B38400, LINUX_B38400 },
362         { B57600, LINUX_B57600 }, { B115200, LINUX_B115200 },
363         {-1, -1 }
364 };
365
366 struct linux_serial_struct {
367         int     type;
368         int     line;
369         int     port;
370         int     irq;
371         int     flags;
372         int     xmit_fifo_size;
373         int     custom_divisor;
374         int     baud_base;
375         unsigned short close_delay;
376         char    reserved_char[2];
377         int     hub6;
378         unsigned short closing_wait;
379         unsigned short closing_wait2;
380         int     reserved[4];
381 };
382
383 static int
384 linux_to_bsd_speed(int code, struct speedtab *table)
385 {
386         for ( ; table->sp_code != -1; table++)
387                 if (table->sp_code == code)
388                         return (table->sp_speed);
389         return (-1);
390 }
391
392 static int
393 bsd_to_linux_speed(int speed, struct speedtab *table)
394 {
395         for ( ; table->sp_speed != -1; table++)
396                 if (table->sp_speed == speed)
397                         return (table->sp_code);
398         return (-1);
399 }
400
401 static void
402 bsd_to_linux_termios(struct termios *bios, struct linux_termios *lios)
403 {
404         int i;
405
406 #ifdef DEBUG
407         if (ldebug(ioctl)) {
408                 printf("LINUX: BSD termios structure (input):\n");
409                 printf("i=%08x o=%08x c=%08x l=%08x ispeed=%d ospeed=%d\n",
410                     bios->c_iflag, bios->c_oflag, bios->c_cflag, bios->c_lflag,
411                     bios->c_ispeed, bios->c_ospeed);
412                 printf("c_cc ");
413                 for (i=0; i<NCCS; i++)
414                         printf("%02x ", bios->c_cc[i]);
415                 printf("\n");
416         }
417 #endif
418
419         lios->c_iflag = 0;
420         if (bios->c_iflag & IGNBRK)
421                 lios->c_iflag |= LINUX_IGNBRK;
422         if (bios->c_iflag & BRKINT)
423                 lios->c_iflag |= LINUX_BRKINT;
424         if (bios->c_iflag & IGNPAR)
425                 lios->c_iflag |= LINUX_IGNPAR;
426         if (bios->c_iflag & PARMRK)
427                 lios->c_iflag |= LINUX_PARMRK;
428         if (bios->c_iflag & INPCK)
429                 lios->c_iflag |= LINUX_INPCK;
430         if (bios->c_iflag & ISTRIP)
431                 lios->c_iflag |= LINUX_ISTRIP;
432         if (bios->c_iflag & INLCR)
433                 lios->c_iflag |= LINUX_INLCR;
434         if (bios->c_iflag & IGNCR)
435                 lios->c_iflag |= LINUX_IGNCR;
436         if (bios->c_iflag & ICRNL)
437                 lios->c_iflag |= LINUX_ICRNL;
438         if (bios->c_iflag & IXON)
439                 lios->c_iflag |= LINUX_IXON;
440         if (bios->c_iflag & IXANY)
441                 lios->c_iflag |= LINUX_IXANY;
442         if (bios->c_iflag & IXOFF)
443                 lios->c_iflag |= LINUX_IXOFF;
444         if (bios->c_iflag & IMAXBEL)
445                 lios->c_iflag |= LINUX_IMAXBEL;
446
447         lios->c_oflag = 0;
448         if (bios->c_oflag & OPOST)
449                 lios->c_oflag |= LINUX_OPOST;
450         if (bios->c_oflag & ONLCR)
451                 lios->c_oflag |= LINUX_ONLCR;
452         if (bios->c_oflag & TAB3)
453                 lios->c_oflag |= LINUX_XTABS;
454
455         lios->c_cflag = bsd_to_linux_speed(bios->c_ispeed, sptab);
456         lios->c_cflag |= (bios->c_cflag & CSIZE) >> 4;
457         if (bios->c_cflag & CSTOPB)
458                 lios->c_cflag |= LINUX_CSTOPB;
459         if (bios->c_cflag & CREAD)
460                 lios->c_cflag |= LINUX_CREAD;
461         if (bios->c_cflag & PARENB)
462                 lios->c_cflag |= LINUX_PARENB;
463         if (bios->c_cflag & PARODD)
464                 lios->c_cflag |= LINUX_PARODD;
465         if (bios->c_cflag & HUPCL)
466                 lios->c_cflag |= LINUX_HUPCL;
467         if (bios->c_cflag & CLOCAL)
468                 lios->c_cflag |= LINUX_CLOCAL;
469         if (bios->c_cflag & CRTSCTS)
470                 lios->c_cflag |= LINUX_CRTSCTS;
471
472         lios->c_lflag = 0;
473         if (bios->c_lflag & ISIG)
474                 lios->c_lflag |= LINUX_ISIG;
475         if (bios->c_lflag & ICANON)
476                 lios->c_lflag |= LINUX_ICANON;
477         if (bios->c_lflag & ECHO)
478                 lios->c_lflag |= LINUX_ECHO;
479         if (bios->c_lflag & ECHOE)
480                 lios->c_lflag |= LINUX_ECHOE;
481         if (bios->c_lflag & ECHOK)
482                 lios->c_lflag |= LINUX_ECHOK;
483         if (bios->c_lflag & ECHONL)
484                 lios->c_lflag |= LINUX_ECHONL;
485         if (bios->c_lflag & NOFLSH)
486                 lios->c_lflag |= LINUX_NOFLSH;
487         if (bios->c_lflag & TOSTOP)
488                 lios->c_lflag |= LINUX_TOSTOP;
489         if (bios->c_lflag & ECHOCTL)
490                 lios->c_lflag |= LINUX_ECHOCTL;
491         if (bios->c_lflag & ECHOPRT)
492                 lios->c_lflag |= LINUX_ECHOPRT;
493         if (bios->c_lflag & ECHOKE)
494                 lios->c_lflag |= LINUX_ECHOKE;
495         if (bios->c_lflag & FLUSHO)
496                 lios->c_lflag |= LINUX_FLUSHO;
497         if (bios->c_lflag & PENDIN)
498                 lios->c_lflag |= LINUX_PENDIN;
499         if (bios->c_lflag & IEXTEN)
500                 lios->c_lflag |= LINUX_IEXTEN;
501
502         for (i=0; i<LINUX_NCCS; i++)
503                 lios->c_cc[i] = LINUX_POSIX_VDISABLE;
504         lios->c_cc[LINUX_VINTR] = bios->c_cc[VINTR];
505         lios->c_cc[LINUX_VQUIT] = bios->c_cc[VQUIT];
506         lios->c_cc[LINUX_VERASE] = bios->c_cc[VERASE];
507         lios->c_cc[LINUX_VKILL] = bios->c_cc[VKILL];
508         lios->c_cc[LINUX_VEOF] = bios->c_cc[VEOF];
509         lios->c_cc[LINUX_VEOL] = bios->c_cc[VEOL];
510         lios->c_cc[LINUX_VMIN] = bios->c_cc[VMIN];
511         lios->c_cc[LINUX_VTIME] = bios->c_cc[VTIME];
512         lios->c_cc[LINUX_VEOL2] = bios->c_cc[VEOL2];
513         lios->c_cc[LINUX_VSUSP] = bios->c_cc[VSUSP];
514         lios->c_cc[LINUX_VSTART] = bios->c_cc[VSTART];
515         lios->c_cc[LINUX_VSTOP] = bios->c_cc[VSTOP];
516         lios->c_cc[LINUX_VREPRINT] = bios->c_cc[VREPRINT];
517         lios->c_cc[LINUX_VDISCARD] = bios->c_cc[VDISCARD];
518         lios->c_cc[LINUX_VWERASE] = bios->c_cc[VWERASE];
519         lios->c_cc[LINUX_VLNEXT] = bios->c_cc[VLNEXT];
520
521         for (i=0; i<LINUX_NCCS; i++) {
522                 if (i != LINUX_VMIN && i != LINUX_VTIME &&
523                     lios->c_cc[i] == _POSIX_VDISABLE)
524                         lios->c_cc[i] = LINUX_POSIX_VDISABLE;
525         }
526         lios->c_line = 0;
527
528 #ifdef DEBUG
529         if (ldebug(ioctl)) {
530                 printf("LINUX: LINUX termios structure (output):\n");
531                 printf("i=%08x o=%08x c=%08x l=%08x line=%d\n",
532                     lios->c_iflag, lios->c_oflag, lios->c_cflag,
533                     lios->c_lflag, (int)lios->c_line);
534                 printf("c_cc ");
535                 for (i=0; i<LINUX_NCCS; i++)
536                         printf("%02x ", lios->c_cc[i]);
537                 printf("\n");
538         }
539 #endif
540 }
541
542 static void
543 linux_to_bsd_termios(struct linux_termios *lios, struct termios *bios)
544 {
545         int i;
546
547 #ifdef DEBUG
548         if (ldebug(ioctl)) {
549                 printf("LINUX: LINUX termios structure (input):\n");
550                 printf("i=%08x o=%08x c=%08x l=%08x line=%d\n",
551                     lios->c_iflag, lios->c_oflag, lios->c_cflag,
552                     lios->c_lflag, (int)lios->c_line);
553                 printf("c_cc ");
554                 for (i=0; i<LINUX_NCCS; i++)
555                         printf("%02x ", lios->c_cc[i]);
556                 printf("\n");
557         }
558 #endif
559
560         bios->c_iflag = 0;
561         if (lios->c_iflag & LINUX_IGNBRK)
562                 bios->c_iflag |= IGNBRK;
563         if (lios->c_iflag & LINUX_BRKINT)
564                 bios->c_iflag |= BRKINT;
565         if (lios->c_iflag & LINUX_IGNPAR)
566                 bios->c_iflag |= IGNPAR;
567         if (lios->c_iflag & LINUX_PARMRK)
568                 bios->c_iflag |= PARMRK;
569         if (lios->c_iflag & LINUX_INPCK)
570                 bios->c_iflag |= INPCK;
571         if (lios->c_iflag & LINUX_ISTRIP)
572                 bios->c_iflag |= ISTRIP;
573         if (lios->c_iflag & LINUX_INLCR)
574                 bios->c_iflag |= INLCR;
575         if (lios->c_iflag & LINUX_IGNCR)
576                 bios->c_iflag |= IGNCR;
577         if (lios->c_iflag & LINUX_ICRNL)
578                 bios->c_iflag |= ICRNL;
579         if (lios->c_iflag & LINUX_IXON)
580                 bios->c_iflag |= IXON;
581         if (lios->c_iflag & LINUX_IXANY)
582                 bios->c_iflag |= IXANY;
583         if (lios->c_iflag & LINUX_IXOFF)
584                 bios->c_iflag |= IXOFF;
585         if (lios->c_iflag & LINUX_IMAXBEL)
586                 bios->c_iflag |= IMAXBEL;
587
588         bios->c_oflag = 0;
589         if (lios->c_oflag & LINUX_OPOST)
590                 bios->c_oflag |= OPOST;
591         if (lios->c_oflag & LINUX_ONLCR)
592                 bios->c_oflag |= ONLCR;
593         if (lios->c_oflag & LINUX_XTABS)
594                 bios->c_oflag |= TAB3;
595
596         bios->c_cflag = (lios->c_cflag & LINUX_CSIZE) << 4;
597         if (lios->c_cflag & LINUX_CSTOPB)
598                 bios->c_cflag |= CSTOPB;
599         if (lios->c_cflag & LINUX_CREAD)
600                 bios->c_cflag |= CREAD;
601         if (lios->c_cflag & LINUX_PARENB)
602                 bios->c_cflag |= PARENB;
603         if (lios->c_cflag & LINUX_PARODD)
604                 bios->c_cflag |= PARODD;
605         if (lios->c_cflag & LINUX_HUPCL)
606                 bios->c_cflag |= HUPCL;
607         if (lios->c_cflag & LINUX_CLOCAL)
608                 bios->c_cflag |= CLOCAL;
609         if (lios->c_cflag & LINUX_CRTSCTS)
610                 bios->c_cflag |= CRTSCTS;
611
612         bios->c_lflag = 0;
613         if (lios->c_lflag & LINUX_ISIG)
614                 bios->c_lflag |= ISIG;
615         if (lios->c_lflag & LINUX_ICANON)
616                 bios->c_lflag |= ICANON;
617         if (lios->c_lflag & LINUX_ECHO)
618                 bios->c_lflag |= ECHO;
619         if (lios->c_lflag & LINUX_ECHOE)
620                 bios->c_lflag |= ECHOE;
621         if (lios->c_lflag & LINUX_ECHOK)
622                 bios->c_lflag |= ECHOK;
623         if (lios->c_lflag & LINUX_ECHONL)
624                 bios->c_lflag |= ECHONL;
625         if (lios->c_lflag & LINUX_NOFLSH)
626                 bios->c_lflag |= NOFLSH;
627         if (lios->c_lflag & LINUX_TOSTOP)
628                 bios->c_lflag |= TOSTOP;
629         if (lios->c_lflag & LINUX_ECHOCTL)
630                 bios->c_lflag |= ECHOCTL;
631         if (lios->c_lflag & LINUX_ECHOPRT)
632                 bios->c_lflag |= ECHOPRT;
633         if (lios->c_lflag & LINUX_ECHOKE)
634                 bios->c_lflag |= ECHOKE;
635         if (lios->c_lflag & LINUX_FLUSHO)
636                 bios->c_lflag |= FLUSHO;
637         if (lios->c_lflag & LINUX_PENDIN)
638                 bios->c_lflag |= PENDIN;
639         if (lios->c_lflag & LINUX_IEXTEN)
640                 bios->c_lflag |= IEXTEN;
641
642         for (i=0; i<NCCS; i++)
643                 bios->c_cc[i] = _POSIX_VDISABLE;
644         bios->c_cc[VINTR] = lios->c_cc[LINUX_VINTR];
645         bios->c_cc[VQUIT] = lios->c_cc[LINUX_VQUIT];
646         bios->c_cc[VERASE] = lios->c_cc[LINUX_VERASE];
647         bios->c_cc[VKILL] = lios->c_cc[LINUX_VKILL];
648         bios->c_cc[VEOF] = lios->c_cc[LINUX_VEOF];
649         bios->c_cc[VEOL] = lios->c_cc[LINUX_VEOL];
650         bios->c_cc[VMIN] = lios->c_cc[LINUX_VMIN];
651         bios->c_cc[VTIME] = lios->c_cc[LINUX_VTIME];
652         bios->c_cc[VEOL2] = lios->c_cc[LINUX_VEOL2];
653         bios->c_cc[VSUSP] = lios->c_cc[LINUX_VSUSP];
654         bios->c_cc[VSTART] = lios->c_cc[LINUX_VSTART];
655         bios->c_cc[VSTOP] = lios->c_cc[LINUX_VSTOP];
656         bios->c_cc[VREPRINT] = lios->c_cc[LINUX_VREPRINT];
657         bios->c_cc[VDISCARD] = lios->c_cc[LINUX_VDISCARD];
658         bios->c_cc[VWERASE] = lios->c_cc[LINUX_VWERASE];
659         bios->c_cc[VLNEXT] = lios->c_cc[LINUX_VLNEXT];
660
661         for (i=0; i<NCCS; i++) {
662                 if (i != VMIN && i != VTIME &&
663                     bios->c_cc[i] == LINUX_POSIX_VDISABLE)
664                         bios->c_cc[i] = _POSIX_VDISABLE;
665         }
666
667         bios->c_ispeed = bios->c_ospeed =
668             linux_to_bsd_speed(lios->c_cflag & LINUX_CBAUD, sptab);
669
670 #ifdef DEBUG
671         if (ldebug(ioctl)) {
672                 printf("LINUX: BSD termios structure (output):\n");
673                 printf("i=%08x o=%08x c=%08x l=%08x ispeed=%d ospeed=%d\n",
674                     bios->c_iflag, bios->c_oflag, bios->c_cflag, bios->c_lflag,
675                     bios->c_ispeed, bios->c_ospeed);
676                 printf("c_cc ");
677                 for (i=0; i<NCCS; i++)
678                         printf("%02x ", bios->c_cc[i]);
679                 printf("\n");
680         }
681 #endif
682 }
683
684 static void
685 bsd_to_linux_termio(struct termios *bios, struct linux_termio *lio)
686 {
687         struct linux_termios lios;
688
689         bsd_to_linux_termios(bios, &lios);
690         lio->c_iflag = lios.c_iflag;
691         lio->c_oflag = lios.c_oflag;
692         lio->c_cflag = lios.c_cflag;
693         lio->c_lflag = lios.c_lflag;
694         lio->c_line  = lios.c_line;
695         memcpy(lio->c_cc, lios.c_cc, LINUX_NCC);
696 }
697
698 static void
699 linux_to_bsd_termio(struct linux_termio *lio, struct termios *bios)
700 {
701         struct linux_termios lios;
702         int i;
703
704         lios.c_iflag = lio->c_iflag;
705         lios.c_oflag = lio->c_oflag;
706         lios.c_cflag = lio->c_cflag;
707         lios.c_lflag = lio->c_lflag;
708         for (i=LINUX_NCC; i<LINUX_NCCS; i++)
709                 lios.c_cc[i] = LINUX_POSIX_VDISABLE;
710         memcpy(lios.c_cc, lio->c_cc, LINUX_NCC);
711         linux_to_bsd_termios(&lios, bios);
712 }
713
714 static int
715 linux_ioctl_termio(struct thread *td, struct linux_ioctl_args *args)
716 {
717         struct termios bios;
718         struct linux_termios lios;
719         struct linux_termio lio;
720         struct file *fp;
721         int error;
722
723         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
724         if (error != 0)
725                 return (error);
726
727         switch (args->cmd & 0xffff) {
728
729         case LINUX_TCGETS:
730                 error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios, td->td_ucred,
731                     td);
732                 if (error)
733                         break;
734                 bsd_to_linux_termios(&bios, &lios);
735                 error = copyout(&lios, (void *)args->arg, sizeof(lios));
736                 break;
737
738         case LINUX_TCSETS:
739                 error = copyin((void *)args->arg, &lios, sizeof(lios));
740                 if (error)
741                         break;
742                 linux_to_bsd_termios(&lios, &bios);
743                 error = (fo_ioctl(fp, TIOCSETA, (caddr_t)&bios, td->td_ucred,
744                     td));
745                 break;
746
747         case LINUX_TCSETSW:
748                 error = copyin((void *)args->arg, &lios, sizeof(lios));
749                 if (error)
750                         break;
751                 linux_to_bsd_termios(&lios, &bios);
752                 error = (fo_ioctl(fp, TIOCSETAW, (caddr_t)&bios, td->td_ucred,
753                     td));
754                 break;
755
756         case LINUX_TCSETSF:
757                 error = copyin((void *)args->arg, &lios, sizeof(lios));
758                 if (error)
759                         break;
760                 linux_to_bsd_termios(&lios, &bios);
761                 error = (fo_ioctl(fp, TIOCSETAF, (caddr_t)&bios, td->td_ucred,
762                     td));
763                 break;
764
765         case LINUX_TCGETA:
766                 error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios, td->td_ucred,
767                     td);
768                 if (error)
769                         break;
770                 bsd_to_linux_termio(&bios, &lio);
771                 error = (copyout(&lio, (void *)args->arg, sizeof(lio)));
772                 break;
773
774         case LINUX_TCSETA:
775                 error = copyin((void *)args->arg, &lio, sizeof(lio));
776                 if (error)
777                         break;
778                 linux_to_bsd_termio(&lio, &bios);
779                 error = (fo_ioctl(fp, TIOCSETA, (caddr_t)&bios, td->td_ucred,
780                     td));
781                 break;
782
783         case LINUX_TCSETAW:
784                 error = copyin((void *)args->arg, &lio, sizeof(lio));
785                 if (error)
786                         break;
787                 linux_to_bsd_termio(&lio, &bios);
788                 error = (fo_ioctl(fp, TIOCSETAW, (caddr_t)&bios, td->td_ucred,
789                     td));
790                 break;
791
792         case LINUX_TCSETAF:
793                 error = copyin((void *)args->arg, &lio, sizeof(lio));
794                 if (error)
795                         break;
796                 linux_to_bsd_termio(&lio, &bios);
797                 error = (fo_ioctl(fp, TIOCSETAF, (caddr_t)&bios, td->td_ucred,
798                     td));
799                 break;
800
801         /* LINUX_TCSBRK */
802
803         case LINUX_TCXONC: {
804                 switch (args->arg) {
805                 case LINUX_TCOOFF:
806                         args->cmd = TIOCSTOP;
807                         break;
808                 case LINUX_TCOON:
809                         args->cmd = TIOCSTART;
810                         break;
811                 case LINUX_TCIOFF:
812                 case LINUX_TCION: {
813                         int c;
814                         struct write_args wr;
815                         error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios,
816                             td->td_ucred, td);
817                         if (error)
818                                 break;
819                         fdrop(fp, td);
820                         c = (args->arg == LINUX_TCIOFF) ? VSTOP : VSTART;
821                         c = bios.c_cc[c];
822                         if (c != _POSIX_VDISABLE) {
823                                 wr.fd = args->fd;
824                                 wr.buf = &c;
825                                 wr.nbyte = sizeof(c);
826                                 return (sys_write(td, &wr));
827                         } else
828                                 return (0);
829                 }
830                 default:
831                         fdrop(fp, td);
832                         return (EINVAL);
833                 }
834                 args->arg = 0;
835                 error = (sys_ioctl(td, (struct ioctl_args *)args));
836                 break;
837         }
838
839         case LINUX_TCFLSH: {
840                 int val;
841                 switch (args->arg) {
842                 case LINUX_TCIFLUSH:
843                         val = FREAD;
844                         break;
845                 case LINUX_TCOFLUSH:
846                         val = FWRITE;
847                         break;
848                 case LINUX_TCIOFLUSH:
849                         val = FREAD | FWRITE;
850                         break;
851                 default:
852                         fdrop(fp, td);
853                         return (EINVAL);
854                 }
855                 error = (fo_ioctl(fp,TIOCFLUSH,(caddr_t)&val,td->td_ucred,td));
856                 break;
857         }
858
859         case LINUX_TIOCEXCL:
860                 args->cmd = TIOCEXCL;
861                 error = (sys_ioctl(td, (struct ioctl_args *)args));
862                 break;
863
864         case LINUX_TIOCNXCL:
865                 args->cmd = TIOCNXCL;
866                 error = (sys_ioctl(td, (struct ioctl_args *)args));
867                 break;
868
869         case LINUX_TIOCSCTTY:
870                 args->cmd = TIOCSCTTY;
871                 error = (sys_ioctl(td, (struct ioctl_args *)args));
872                 break;
873
874         case LINUX_TIOCGPGRP:
875                 args->cmd = TIOCGPGRP;
876                 error = (sys_ioctl(td, (struct ioctl_args *)args));
877                 break;
878
879         case LINUX_TIOCSPGRP:
880                 args->cmd = TIOCSPGRP;
881                 error = (sys_ioctl(td, (struct ioctl_args *)args));
882                 break;
883
884         /* LINUX_TIOCOUTQ */
885         /* LINUX_TIOCSTI */
886
887         case LINUX_TIOCGWINSZ:
888                 args->cmd = TIOCGWINSZ;
889                 error = (sys_ioctl(td, (struct ioctl_args *)args));
890                 break;
891
892         case LINUX_TIOCSWINSZ:
893                 args->cmd = TIOCSWINSZ;
894                 error = (sys_ioctl(td, (struct ioctl_args *)args));
895                 break;
896
897         case LINUX_TIOCMGET:
898                 args->cmd = TIOCMGET;
899                 error = (sys_ioctl(td, (struct ioctl_args *)args));
900                 break;
901
902         case LINUX_TIOCMBIS:
903                 args->cmd = TIOCMBIS;
904                 error = (sys_ioctl(td, (struct ioctl_args *)args));
905                 break;
906
907         case LINUX_TIOCMBIC:
908                 args->cmd = TIOCMBIC;
909                 error = (sys_ioctl(td, (struct ioctl_args *)args));
910                 break;
911
912         case LINUX_TIOCMSET:
913                 args->cmd = TIOCMSET;
914                 error = (sys_ioctl(td, (struct ioctl_args *)args));
915                 break;
916
917         /* TIOCGSOFTCAR */
918         /* TIOCSSOFTCAR */
919
920         case LINUX_FIONREAD: /* LINUX_TIOCINQ */
921                 args->cmd = FIONREAD;
922                 error = (sys_ioctl(td, (struct ioctl_args *)args));
923                 break;
924
925         /* LINUX_TIOCLINUX */
926
927         case LINUX_TIOCCONS:
928                 args->cmd = TIOCCONS;
929                 error = (sys_ioctl(td, (struct ioctl_args *)args));
930                 break;
931
932         case LINUX_TIOCGSERIAL: {
933                 struct linux_serial_struct lss;
934
935                 bzero(&lss, sizeof(lss));
936                 lss.type = LINUX_PORT_16550A;
937                 lss.flags = 0;
938                 lss.close_delay = 0;
939                 error = copyout(&lss, (void *)args->arg, sizeof(lss));
940                 break;
941         }
942
943         case LINUX_TIOCSSERIAL: {
944                 struct linux_serial_struct lss;
945                 error = copyin((void *)args->arg, &lss, sizeof(lss));
946                 if (error)
947                         break;
948                 /* XXX - It really helps to have an implementation that
949                  * does nothing. NOT!
950                  */
951                 error = 0;
952                 break;
953         }
954
955         case LINUX_TIOCPKT:
956                 args->cmd = TIOCPKT;
957                 error = (sys_ioctl(td, (struct ioctl_args *)args));
958                 break;
959
960         case LINUX_FIONBIO:
961                 args->cmd = FIONBIO;
962                 error = (sys_ioctl(td, (struct ioctl_args *)args));
963                 break;
964
965         case LINUX_TIOCNOTTY:
966                 args->cmd = TIOCNOTTY;
967                 error = (sys_ioctl(td, (struct ioctl_args *)args));
968                 break;
969
970         case LINUX_TIOCSETD: {
971                 int line;
972                 switch (args->arg) {
973                 case LINUX_N_TTY:
974                         line = TTYDISC;
975                         break;
976                 case LINUX_N_SLIP:
977                         line = SLIPDISC;
978                         break;
979                 case LINUX_N_PPP:
980                         line = PPPDISC;
981                         break;
982                 default:
983                         fdrop(fp, td);
984                         return (EINVAL);
985                 }
986                 error = (fo_ioctl(fp, TIOCSETD, (caddr_t)&line, td->td_ucred,
987                     td));
988                 break;
989         }
990
991         case LINUX_TIOCGETD: {
992                 int linux_line;
993                 int bsd_line = TTYDISC;
994                 error = fo_ioctl(fp, TIOCGETD, (caddr_t)&bsd_line,
995                     td->td_ucred, td);
996                 if (error)
997                         break;
998                 switch (bsd_line) {
999                 case TTYDISC:
1000                         linux_line = LINUX_N_TTY;
1001                         break;
1002                 case SLIPDISC:
1003                         linux_line = LINUX_N_SLIP;
1004                         break;
1005                 case PPPDISC:
1006                         linux_line = LINUX_N_PPP;
1007                         break;
1008                 default:
1009                         fdrop(fp, td);
1010                         return (EINVAL);
1011                 }
1012                 error = (copyout(&linux_line, (void *)args->arg, sizeof(int)));
1013                 break;
1014         }
1015
1016         /* LINUX_TCSBRKP */
1017         /* LINUX_TIOCTTYGSTRUCT */
1018
1019         case LINUX_FIONCLEX:
1020                 args->cmd = FIONCLEX;
1021                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1022                 break;
1023
1024         case LINUX_FIOCLEX:
1025                 args->cmd = FIOCLEX;
1026                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1027                 break;
1028
1029         case LINUX_FIOASYNC:
1030                 args->cmd = FIOASYNC;
1031                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1032                 break;
1033
1034         /* LINUX_TIOCSERCONFIG */
1035         /* LINUX_TIOCSERGWILD */
1036         /* LINUX_TIOCSERSWILD */
1037         /* LINUX_TIOCGLCKTRMIOS */
1038         /* LINUX_TIOCSLCKTRMIOS */
1039
1040         case LINUX_TIOCSBRK:
1041                 args->cmd = TIOCSBRK;
1042                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1043                 break;
1044
1045         case LINUX_TIOCCBRK:
1046                 args->cmd = TIOCCBRK;
1047                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1048                 break;
1049         case LINUX_TIOCGPTN: {
1050                 int nb;
1051
1052                 error = fo_ioctl(fp, TIOCGPTN, (caddr_t)&nb, td->td_ucred, td);
1053                 if (!error)
1054                         error = copyout(&nb, (void *)args->arg,
1055                             sizeof(int));
1056                 break;
1057         }
1058         case LINUX_TIOCSPTLCK:
1059                 /* Our unlockpt() does nothing. */
1060                 error = 0;
1061                 break;
1062         default:
1063                 error = ENOIOCTL;
1064                 break;
1065         }
1066
1067         fdrop(fp, td);
1068         return (error);
1069 }
1070
1071 /*
1072  * CDROM related ioctls
1073  */
1074
1075 struct linux_cdrom_msf
1076 {
1077         u_char  cdmsf_min0;
1078         u_char  cdmsf_sec0;
1079         u_char  cdmsf_frame0;
1080         u_char  cdmsf_min1;
1081         u_char  cdmsf_sec1;
1082         u_char  cdmsf_frame1;
1083 };
1084
1085 struct linux_cdrom_tochdr
1086 {
1087         u_char  cdth_trk0;
1088         u_char  cdth_trk1;
1089 };
1090
1091 union linux_cdrom_addr
1092 {
1093         struct {
1094                 u_char  minute;
1095                 u_char  second;
1096                 u_char  frame;
1097         } msf;
1098         int     lba;
1099 };
1100
1101 struct linux_cdrom_tocentry
1102 {
1103         u_char  cdte_track;
1104         u_char  cdte_adr:4;
1105         u_char  cdte_ctrl:4;
1106         u_char  cdte_format;
1107         union linux_cdrom_addr cdte_addr;
1108         u_char  cdte_datamode;
1109 };
1110
1111 struct linux_cdrom_subchnl
1112 {
1113         u_char  cdsc_format;
1114         u_char  cdsc_audiostatus;
1115         u_char  cdsc_adr:4;
1116         u_char  cdsc_ctrl:4;
1117         u_char  cdsc_trk;
1118         u_char  cdsc_ind;
1119         union linux_cdrom_addr cdsc_absaddr;
1120         union linux_cdrom_addr cdsc_reladdr;
1121 };
1122
1123 struct l_cdrom_read_audio {
1124         union linux_cdrom_addr addr;
1125         u_char          addr_format;
1126         l_int           nframes;
1127         u_char          *buf;
1128 };
1129
1130 struct l_dvd_layer {
1131         u_char          book_version:4;
1132         u_char          book_type:4;
1133         u_char          min_rate:4;
1134         u_char          disc_size:4;
1135         u_char          layer_type:4;
1136         u_char          track_path:1;
1137         u_char          nlayers:2;
1138         u_char          track_density:4;
1139         u_char          linear_density:4;
1140         u_char          bca:1;
1141         u_int32_t       start_sector;
1142         u_int32_t       end_sector;
1143         u_int32_t       end_sector_l0;
1144 };
1145
1146 struct l_dvd_physical {
1147         u_char          type;
1148         u_char          layer_num;
1149         struct l_dvd_layer layer[4];
1150 };
1151
1152 struct l_dvd_copyright {
1153         u_char          type;
1154         u_char          layer_num;
1155         u_char          cpst;
1156         u_char          rmi;
1157 };
1158
1159 struct l_dvd_disckey {
1160         u_char          type;
1161         l_uint          agid:2;
1162         u_char          value[2048];
1163 };
1164
1165 struct l_dvd_bca {
1166         u_char          type;
1167         l_int           len;
1168         u_char          value[188];
1169 };
1170
1171 struct l_dvd_manufact {
1172         u_char          type;
1173         u_char          layer_num;
1174         l_int           len;
1175         u_char          value[2048];
1176 };
1177
1178 typedef union {
1179         u_char                  type;
1180         struct l_dvd_physical   physical;
1181         struct l_dvd_copyright  copyright;
1182         struct l_dvd_disckey    disckey;
1183         struct l_dvd_bca        bca;
1184         struct l_dvd_manufact   manufact;
1185 } l_dvd_struct;
1186
1187 typedef u_char l_dvd_key[5];
1188 typedef u_char l_dvd_challenge[10];
1189
1190 struct l_dvd_lu_send_agid {
1191         u_char          type;
1192         l_uint          agid:2;
1193 };
1194
1195 struct l_dvd_host_send_challenge {
1196         u_char          type;
1197         l_uint          agid:2;
1198         l_dvd_challenge chal;
1199 };
1200
1201 struct l_dvd_send_key {
1202         u_char          type;
1203         l_uint          agid:2;
1204         l_dvd_key       key;
1205 };
1206
1207 struct l_dvd_lu_send_challenge {
1208         u_char          type;
1209         l_uint          agid:2;
1210         l_dvd_challenge chal;
1211 };
1212
1213 struct l_dvd_lu_send_title_key {
1214         u_char          type;
1215         l_uint          agid:2;
1216         l_dvd_key       title_key;
1217         l_int           lba;
1218         l_uint          cpm:1;
1219         l_uint          cp_sec:1;
1220         l_uint          cgms:2;
1221 };
1222
1223 struct l_dvd_lu_send_asf {
1224         u_char          type;
1225         l_uint          agid:2;
1226         l_uint          asf:1;
1227 };
1228
1229 struct l_dvd_host_send_rpcstate {
1230         u_char          type;
1231         u_char          pdrc;
1232 };
1233
1234 struct l_dvd_lu_send_rpcstate {
1235         u_char          type:2;
1236         u_char          vra:3;
1237         u_char          ucca:3;
1238         u_char          region_mask;
1239         u_char          rpc_scheme;
1240 };
1241
1242 typedef union {
1243         u_char                          type;
1244         struct l_dvd_lu_send_agid       lsa;
1245         struct l_dvd_host_send_challenge hsc;
1246         struct l_dvd_send_key           lsk;
1247         struct l_dvd_lu_send_challenge  lsc;
1248         struct l_dvd_send_key           hsk;
1249         struct l_dvd_lu_send_title_key  lstk;
1250         struct l_dvd_lu_send_asf        lsasf;
1251         struct l_dvd_host_send_rpcstate hrpcs;
1252         struct l_dvd_lu_send_rpcstate   lrpcs;
1253 } l_dvd_authinfo;
1254
1255 static void
1256 bsd_to_linux_msf_lba(u_char af, union msf_lba *bp, union linux_cdrom_addr *lp)
1257 {
1258         if (af == CD_LBA_FORMAT)
1259                 lp->lba = bp->lba;
1260         else {
1261                 lp->msf.minute = bp->msf.minute;
1262                 lp->msf.second = bp->msf.second;
1263                 lp->msf.frame = bp->msf.frame;
1264         }
1265 }
1266
1267 static void
1268 set_linux_cdrom_addr(union linux_cdrom_addr *addr, int format, int lba)
1269 {
1270         if (format == LINUX_CDROM_MSF) {
1271                 addr->msf.frame = lba % 75;
1272                 lba /= 75;
1273                 lba += 2;
1274                 addr->msf.second = lba % 60;
1275                 addr->msf.minute = lba / 60;
1276         } else
1277                 addr->lba = lba;
1278 }
1279
1280 static int
1281 linux_to_bsd_dvd_struct(l_dvd_struct *lp, struct dvd_struct *bp)
1282 {
1283         bp->format = lp->type;
1284         switch (bp->format) {
1285         case DVD_STRUCT_PHYSICAL:
1286                 if (bp->layer_num >= 4)
1287                         return (EINVAL);
1288                 bp->layer_num = lp->physical.layer_num;
1289                 break;
1290         case DVD_STRUCT_COPYRIGHT:
1291                 bp->layer_num = lp->copyright.layer_num;
1292                 break;
1293         case DVD_STRUCT_DISCKEY:
1294                 bp->agid = lp->disckey.agid;
1295                 break;
1296         case DVD_STRUCT_BCA:
1297         case DVD_STRUCT_MANUFACT:
1298                 break;
1299         default:
1300                 return (EINVAL);
1301         }
1302         return (0);
1303 }
1304
1305 static int
1306 bsd_to_linux_dvd_struct(struct dvd_struct *bp, l_dvd_struct *lp)
1307 {
1308         switch (bp->format) {
1309         case DVD_STRUCT_PHYSICAL: {
1310                 struct dvd_layer *blp = (struct dvd_layer *)bp->data;
1311                 struct l_dvd_layer *llp = &lp->physical.layer[bp->layer_num];
1312                 memset(llp, 0, sizeof(*llp));
1313                 llp->book_version = blp->book_version;
1314                 llp->book_type = blp->book_type;
1315                 llp->min_rate = blp->max_rate;
1316                 llp->disc_size = blp->disc_size;
1317                 llp->layer_type = blp->layer_type;
1318                 llp->track_path = blp->track_path;
1319                 llp->nlayers = blp->nlayers;
1320                 llp->track_density = blp->track_density;
1321                 llp->linear_density = blp->linear_density;
1322                 llp->bca = blp->bca;
1323                 llp->start_sector = blp->start_sector;
1324                 llp->end_sector = blp->end_sector;
1325                 llp->end_sector_l0 = blp->end_sector_l0;
1326                 break;
1327         }
1328         case DVD_STRUCT_COPYRIGHT:
1329                 lp->copyright.cpst = bp->cpst;
1330                 lp->copyright.rmi = bp->rmi;
1331                 break;
1332         case DVD_STRUCT_DISCKEY:
1333                 memcpy(lp->disckey.value, bp->data, sizeof(lp->disckey.value));
1334                 break;
1335         case DVD_STRUCT_BCA:
1336                 lp->bca.len = bp->length;
1337                 memcpy(lp->bca.value, bp->data, sizeof(lp->bca.value));
1338                 break;
1339         case DVD_STRUCT_MANUFACT:
1340                 lp->manufact.len = bp->length;
1341                 memcpy(lp->manufact.value, bp->data,
1342                     sizeof(lp->manufact.value));
1343                 /* lp->manufact.layer_num is unused in Linux (redhat 7.0). */
1344                 break;
1345         default:
1346                 return (EINVAL);
1347         }
1348         return (0);
1349 }
1350
1351 static int
1352 linux_to_bsd_dvd_authinfo(l_dvd_authinfo *lp, int *bcode,
1353     struct dvd_authinfo *bp)
1354 {
1355         switch (lp->type) {
1356         case LINUX_DVD_LU_SEND_AGID:
1357                 *bcode = DVDIOCREPORTKEY;
1358                 bp->format = DVD_REPORT_AGID;
1359                 bp->agid = lp->lsa.agid;
1360                 break;
1361         case LINUX_DVD_HOST_SEND_CHALLENGE:
1362                 *bcode = DVDIOCSENDKEY;
1363                 bp->format = DVD_SEND_CHALLENGE;
1364                 bp->agid = lp->hsc.agid;
1365                 memcpy(bp->keychal, lp->hsc.chal, 10);
1366                 break;
1367         case LINUX_DVD_LU_SEND_KEY1:
1368                 *bcode = DVDIOCREPORTKEY;
1369                 bp->format = DVD_REPORT_KEY1;
1370                 bp->agid = lp->lsk.agid;
1371                 break;
1372         case LINUX_DVD_LU_SEND_CHALLENGE:
1373                 *bcode = DVDIOCREPORTKEY;
1374                 bp->format = DVD_REPORT_CHALLENGE;
1375                 bp->agid = lp->lsc.agid;
1376                 break;
1377         case LINUX_DVD_HOST_SEND_KEY2:
1378                 *bcode = DVDIOCSENDKEY;
1379                 bp->format = DVD_SEND_KEY2;
1380                 bp->agid = lp->hsk.agid;
1381                 memcpy(bp->keychal, lp->hsk.key, 5);
1382                 break;
1383         case LINUX_DVD_LU_SEND_TITLE_KEY:
1384                 *bcode = DVDIOCREPORTKEY;
1385                 bp->format = DVD_REPORT_TITLE_KEY;
1386                 bp->agid = lp->lstk.agid;
1387                 bp->lba = lp->lstk.lba;
1388                 break;
1389         case LINUX_DVD_LU_SEND_ASF:
1390                 *bcode = DVDIOCREPORTKEY;
1391                 bp->format = DVD_REPORT_ASF;
1392                 bp->agid = lp->lsasf.agid;
1393                 break;
1394         case LINUX_DVD_INVALIDATE_AGID:
1395                 *bcode = DVDIOCREPORTKEY;
1396                 bp->format = DVD_INVALIDATE_AGID;
1397                 bp->agid = lp->lsa.agid;
1398                 break;
1399         case LINUX_DVD_LU_SEND_RPC_STATE:
1400                 *bcode = DVDIOCREPORTKEY;
1401                 bp->format = DVD_REPORT_RPC;
1402                 break;
1403         case LINUX_DVD_HOST_SEND_RPC_STATE:
1404                 *bcode = DVDIOCSENDKEY;
1405                 bp->format = DVD_SEND_RPC;
1406                 bp->region = lp->hrpcs.pdrc;
1407                 break;
1408         default:
1409                 return (EINVAL);
1410         }
1411         return (0);
1412 }
1413
1414 static int
1415 bsd_to_linux_dvd_authinfo(struct dvd_authinfo *bp, l_dvd_authinfo *lp)
1416 {
1417         switch (lp->type) {
1418         case LINUX_DVD_LU_SEND_AGID:
1419                 lp->lsa.agid = bp->agid;
1420                 break;
1421         case LINUX_DVD_HOST_SEND_CHALLENGE:
1422                 lp->type = LINUX_DVD_LU_SEND_KEY1;
1423                 break;
1424         case LINUX_DVD_LU_SEND_KEY1:
1425                 memcpy(lp->lsk.key, bp->keychal, sizeof(lp->lsk.key));
1426                 break;
1427         case LINUX_DVD_LU_SEND_CHALLENGE:
1428                 memcpy(lp->lsc.chal, bp->keychal, sizeof(lp->lsc.chal));
1429                 break;
1430         case LINUX_DVD_HOST_SEND_KEY2:
1431                 lp->type = LINUX_DVD_AUTH_ESTABLISHED;
1432                 break;
1433         case LINUX_DVD_LU_SEND_TITLE_KEY:
1434                 memcpy(lp->lstk.title_key, bp->keychal,
1435                     sizeof(lp->lstk.title_key));
1436                 lp->lstk.cpm = bp->cpm;
1437                 lp->lstk.cp_sec = bp->cp_sec;
1438                 lp->lstk.cgms = bp->cgms;
1439                 break;
1440         case LINUX_DVD_LU_SEND_ASF:
1441                 lp->lsasf.asf = bp->asf;
1442                 break;
1443         case LINUX_DVD_INVALIDATE_AGID:
1444                 break;
1445         case LINUX_DVD_LU_SEND_RPC_STATE:
1446                 lp->lrpcs.type = bp->reg_type;
1447                 lp->lrpcs.vra = bp->vend_rsts;
1448                 lp->lrpcs.ucca = bp->user_rsts;
1449                 lp->lrpcs.region_mask = bp->region;
1450                 lp->lrpcs.rpc_scheme = bp->rpc_scheme;
1451                 break;
1452         case LINUX_DVD_HOST_SEND_RPC_STATE:
1453                 break;
1454         default:
1455                 return (EINVAL);
1456         }
1457         return (0);
1458 }
1459
1460 static int
1461 linux_ioctl_cdrom(struct thread *td, struct linux_ioctl_args *args)
1462 {
1463         struct file *fp;
1464         int error;
1465
1466         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
1467         if (error != 0)
1468                 return (error);
1469         switch (args->cmd & 0xffff) {
1470
1471         case LINUX_CDROMPAUSE:
1472                 args->cmd = CDIOCPAUSE;
1473                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1474                 break;
1475
1476         case LINUX_CDROMRESUME:
1477                 args->cmd = CDIOCRESUME;
1478                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1479                 break;
1480
1481         case LINUX_CDROMPLAYMSF:
1482                 args->cmd = CDIOCPLAYMSF;
1483                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1484                 break;
1485
1486         case LINUX_CDROMPLAYTRKIND:
1487                 args->cmd = CDIOCPLAYTRACKS;
1488                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1489                 break;
1490
1491         case LINUX_CDROMREADTOCHDR: {
1492                 struct ioc_toc_header th;
1493                 struct linux_cdrom_tochdr lth;
1494                 error = fo_ioctl(fp, CDIOREADTOCHEADER, (caddr_t)&th,
1495                     td->td_ucred, td);
1496                 if (!error) {
1497                         lth.cdth_trk0 = th.starting_track;
1498                         lth.cdth_trk1 = th.ending_track;
1499                         copyout(&lth, (void *)args->arg, sizeof(lth));
1500                 }
1501                 break;
1502         }
1503
1504         case LINUX_CDROMREADTOCENTRY: {
1505                 struct linux_cdrom_tocentry lte;
1506                 struct ioc_read_toc_single_entry irtse;
1507
1508                 error = copyin((void *)args->arg, &lte, sizeof(lte));
1509                 if (error)
1510                         break;
1511                 irtse.address_format = lte.cdte_format;
1512                 irtse.track = lte.cdte_track;
1513                 error = fo_ioctl(fp, CDIOREADTOCENTRY, (caddr_t)&irtse,
1514                     td->td_ucred, td);
1515                 if (!error) {
1516                         lte.cdte_ctrl = irtse.entry.control;
1517                         lte.cdte_adr = irtse.entry.addr_type;
1518                         bsd_to_linux_msf_lba(irtse.address_format,
1519                             &irtse.entry.addr, &lte.cdte_addr);
1520                         error = copyout(&lte, (void *)args->arg, sizeof(lte));
1521                 }
1522                 break;
1523         }
1524
1525         case LINUX_CDROMSTOP:
1526                 args->cmd = CDIOCSTOP;
1527                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1528                 break;
1529
1530         case LINUX_CDROMSTART:
1531                 args->cmd = CDIOCSTART;
1532                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1533                 break;
1534
1535         case LINUX_CDROMEJECT:
1536                 args->cmd = CDIOCEJECT;
1537                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1538                 break;
1539
1540         /* LINUX_CDROMVOLCTRL */
1541
1542         case LINUX_CDROMSUBCHNL: {
1543                 struct linux_cdrom_subchnl sc;
1544                 struct ioc_read_subchannel bsdsc;
1545                 struct cd_sub_channel_info bsdinfo;
1546
1547                 bsdsc.address_format = CD_LBA_FORMAT;
1548                 bsdsc.data_format = CD_CURRENT_POSITION;
1549                 bsdsc.track = 0;
1550                 bsdsc.data_len = sizeof(bsdinfo);
1551                 bsdsc.data = &bsdinfo;
1552                 error = fo_ioctl(fp, CDIOCREADSUBCHANNEL_SYSSPACE,
1553                     (caddr_t)&bsdsc, td->td_ucred, td);
1554                 if (error)
1555                         break;
1556                 error = copyin((void *)args->arg, &sc, sizeof(sc));
1557                 if (error)
1558                         break;
1559                 sc.cdsc_audiostatus = bsdinfo.header.audio_status;
1560                 sc.cdsc_adr = bsdinfo.what.position.addr_type;
1561                 sc.cdsc_ctrl = bsdinfo.what.position.control;
1562                 sc.cdsc_trk = bsdinfo.what.position.track_number;
1563                 sc.cdsc_ind = bsdinfo.what.position.index_number;
1564                 set_linux_cdrom_addr(&sc.cdsc_absaddr, sc.cdsc_format,
1565                     bsdinfo.what.position.absaddr.lba);
1566                 set_linux_cdrom_addr(&sc.cdsc_reladdr, sc.cdsc_format,
1567                     bsdinfo.what.position.reladdr.lba);
1568                 error = copyout(&sc, (void *)args->arg, sizeof(sc));
1569                 break;
1570         }
1571
1572         /* LINUX_CDROMREADMODE2 */
1573         /* LINUX_CDROMREADMODE1 */
1574         /* LINUX_CDROMREADAUDIO */
1575         /* LINUX_CDROMEJECT_SW */
1576         /* LINUX_CDROMMULTISESSION */
1577         /* LINUX_CDROM_GET_UPC */
1578
1579         case LINUX_CDROMRESET:
1580                 args->cmd = CDIOCRESET;
1581                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1582                 break;
1583
1584         /* LINUX_CDROMVOLREAD */
1585         /* LINUX_CDROMREADRAW */
1586         /* LINUX_CDROMREADCOOKED */
1587         /* LINUX_CDROMSEEK */
1588         /* LINUX_CDROMPLAYBLK */
1589         /* LINUX_CDROMREADALL */
1590         /* LINUX_CDROMCLOSETRAY */
1591         /* LINUX_CDROMLOADFROMSLOT */
1592         /* LINUX_CDROMGETSPINDOWN */
1593         /* LINUX_CDROMSETSPINDOWN */
1594         /* LINUX_CDROM_SET_OPTIONS */
1595         /* LINUX_CDROM_CLEAR_OPTIONS */
1596         /* LINUX_CDROM_SELECT_SPEED */
1597         /* LINUX_CDROM_SELECT_DISC */
1598         /* LINUX_CDROM_MEDIA_CHANGED */
1599         /* LINUX_CDROM_DRIVE_STATUS */
1600         /* LINUX_CDROM_DISC_STATUS */
1601         /* LINUX_CDROM_CHANGER_NSLOTS */
1602         /* LINUX_CDROM_LOCKDOOR */
1603         /* LINUX_CDROM_DEBUG */
1604         /* LINUX_CDROM_GET_CAPABILITY */
1605         /* LINUX_CDROMAUDIOBUFSIZ */
1606
1607         case LINUX_DVD_READ_STRUCT: {
1608                 l_dvd_struct *lds;
1609                 struct dvd_struct *bds;
1610
1611                 lds = malloc(sizeof(*lds), M_LINUX, M_WAITOK);
1612                 bds = malloc(sizeof(*bds), M_LINUX, M_WAITOK);
1613                 error = copyin((void *)args->arg, lds, sizeof(*lds));
1614                 if (error)
1615                         goto out;
1616                 error = linux_to_bsd_dvd_struct(lds, bds);
1617                 if (error)
1618                         goto out;
1619                 error = fo_ioctl(fp, DVDIOCREADSTRUCTURE, (caddr_t)bds,
1620                     td->td_ucred, td);
1621                 if (error)
1622                         goto out;
1623                 error = bsd_to_linux_dvd_struct(bds, lds);
1624                 if (error)
1625                         goto out;
1626                 error = copyout(lds, (void *)args->arg, sizeof(*lds));
1627         out:
1628                 free(bds, M_LINUX);
1629                 free(lds, M_LINUX);
1630                 break;
1631         }
1632
1633         /* LINUX_DVD_WRITE_STRUCT */
1634
1635         case LINUX_DVD_AUTH: {
1636                 l_dvd_authinfo lda;
1637                 struct dvd_authinfo bda;
1638                 int bcode;
1639
1640                 error = copyin((void *)args->arg, &lda, sizeof(lda));
1641                 if (error)
1642                         break;
1643                 error = linux_to_bsd_dvd_authinfo(&lda, &bcode, &bda);
1644                 if (error)
1645                         break;
1646                 error = fo_ioctl(fp, bcode, (caddr_t)&bda, td->td_ucred,
1647                     td);
1648                 if (error) {
1649                         if (lda.type == LINUX_DVD_HOST_SEND_KEY2) {
1650                                 lda.type = LINUX_DVD_AUTH_FAILURE;
1651                                 copyout(&lda, (void *)args->arg, sizeof(lda));
1652                         }
1653                         break;
1654                 }
1655                 error = bsd_to_linux_dvd_authinfo(&bda, &lda);
1656                 if (error)
1657                         break;
1658                 error = copyout(&lda, (void *)args->arg, sizeof(lda));
1659                 break;
1660         }
1661
1662         case LINUX_SCSI_GET_BUS_NUMBER:
1663         {
1664                 struct sg_scsi_id id;
1665
1666                 error = fo_ioctl(fp, SG_GET_SCSI_ID, (caddr_t)&id,
1667                     td->td_ucred, td);
1668                 if (error)
1669                         break;
1670                 error = copyout(&id.channel, (void *)args->arg, sizeof(int));
1671                 break;
1672         }
1673
1674         case LINUX_SCSI_GET_IDLUN:
1675         {
1676                 struct sg_scsi_id id;
1677                 struct scsi_idlun idl;
1678
1679                 error = fo_ioctl(fp, SG_GET_SCSI_ID, (caddr_t)&id,
1680                     td->td_ucred, td);
1681                 if (error)
1682                         break;
1683                 idl.dev_id = (id.scsi_id & 0xff) + ((id.lun & 0xff) << 8) +
1684                     ((id.channel & 0xff) << 16) + ((id.host_no & 0xff) << 24);
1685                 idl.host_unique_id = id.host_no;
1686                 error = copyout(&idl, (void *)args->arg, sizeof(idl));
1687                 break;
1688         }
1689
1690         /* LINUX_CDROM_SEND_PACKET */
1691         /* LINUX_CDROM_NEXT_WRITABLE */
1692         /* LINUX_CDROM_LAST_WRITTEN */
1693
1694         default:
1695                 error = ENOIOCTL;
1696                 break;
1697         }
1698
1699         fdrop(fp, td);
1700         return (error);
1701 }
1702
1703 static int
1704 linux_ioctl_vfat(struct thread *td, struct linux_ioctl_args *args)
1705 {
1706
1707         return (ENOTTY);
1708 }
1709
1710 /*
1711  * Sound related ioctls
1712  */
1713
1714 struct linux_old_mixer_info {
1715         char    id[16];
1716         char    name[32];
1717 };
1718
1719 static u_int32_t dirbits[4] = { IOC_VOID, IOC_IN, IOC_OUT, IOC_INOUT };
1720
1721 #define SETDIR(c)       (((c) & ~IOC_DIRMASK) | dirbits[args->cmd >> 30])
1722
1723 static int
1724 linux_ioctl_sound(struct thread *td, struct linux_ioctl_args *args)
1725 {
1726
1727         switch (args->cmd & 0xffff) {
1728
1729         case LINUX_SOUND_MIXER_WRITE_VOLUME:
1730                 args->cmd = SETDIR(SOUND_MIXER_WRITE_VOLUME);
1731                 return (sys_ioctl(td, (struct ioctl_args *)args));
1732
1733         case LINUX_SOUND_MIXER_WRITE_BASS:
1734                 args->cmd = SETDIR(SOUND_MIXER_WRITE_BASS);
1735                 return (sys_ioctl(td, (struct ioctl_args *)args));
1736
1737         case LINUX_SOUND_MIXER_WRITE_TREBLE:
1738                 args->cmd = SETDIR(SOUND_MIXER_WRITE_TREBLE);
1739                 return (sys_ioctl(td, (struct ioctl_args *)args));
1740
1741         case LINUX_SOUND_MIXER_WRITE_SYNTH:
1742                 args->cmd = SETDIR(SOUND_MIXER_WRITE_SYNTH);
1743                 return (sys_ioctl(td, (struct ioctl_args *)args));
1744
1745         case LINUX_SOUND_MIXER_WRITE_PCM:
1746                 args->cmd = SETDIR(SOUND_MIXER_WRITE_PCM);
1747                 return (sys_ioctl(td, (struct ioctl_args *)args));
1748
1749         case LINUX_SOUND_MIXER_WRITE_SPEAKER:
1750                 args->cmd = SETDIR(SOUND_MIXER_WRITE_SPEAKER);
1751                 return (sys_ioctl(td, (struct ioctl_args *)args));
1752
1753         case LINUX_SOUND_MIXER_WRITE_LINE:
1754                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE);
1755                 return (sys_ioctl(td, (struct ioctl_args *)args));
1756
1757         case LINUX_SOUND_MIXER_WRITE_MIC:
1758                 args->cmd = SETDIR(SOUND_MIXER_WRITE_MIC);
1759                 return (sys_ioctl(td, (struct ioctl_args *)args));
1760
1761         case LINUX_SOUND_MIXER_WRITE_CD:
1762                 args->cmd = SETDIR(SOUND_MIXER_WRITE_CD);
1763                 return (sys_ioctl(td, (struct ioctl_args *)args));
1764
1765         case LINUX_SOUND_MIXER_WRITE_IMIX:
1766                 args->cmd = SETDIR(SOUND_MIXER_WRITE_IMIX);
1767                 return (sys_ioctl(td, (struct ioctl_args *)args));
1768
1769         case LINUX_SOUND_MIXER_WRITE_ALTPCM:
1770                 args->cmd = SETDIR(SOUND_MIXER_WRITE_ALTPCM);
1771                 return (sys_ioctl(td, (struct ioctl_args *)args));
1772
1773         case LINUX_SOUND_MIXER_WRITE_RECLEV:
1774                 args->cmd = SETDIR(SOUND_MIXER_WRITE_RECLEV);
1775                 return (sys_ioctl(td, (struct ioctl_args *)args));
1776
1777         case LINUX_SOUND_MIXER_WRITE_IGAIN:
1778                 args->cmd = SETDIR(SOUND_MIXER_WRITE_IGAIN);
1779                 return (sys_ioctl(td, (struct ioctl_args *)args));
1780
1781         case LINUX_SOUND_MIXER_WRITE_OGAIN:
1782                 args->cmd = SETDIR(SOUND_MIXER_WRITE_OGAIN);
1783                 return (sys_ioctl(td, (struct ioctl_args *)args));
1784
1785         case LINUX_SOUND_MIXER_WRITE_LINE1:
1786                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE1);
1787                 return (sys_ioctl(td, (struct ioctl_args *)args));
1788
1789         case LINUX_SOUND_MIXER_WRITE_LINE2:
1790                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE2);
1791                 return (sys_ioctl(td, (struct ioctl_args *)args));
1792
1793         case LINUX_SOUND_MIXER_WRITE_LINE3:
1794                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE3);
1795                 return (sys_ioctl(td, (struct ioctl_args *)args));
1796
1797         case LINUX_SOUND_MIXER_INFO: {
1798                 /* Key on encoded length */
1799                 switch ((args->cmd >> 16) & 0x1fff) {
1800                 case 0x005c: {  /* SOUND_MIXER_INFO */
1801                         args->cmd = SOUND_MIXER_INFO;
1802                         return (sys_ioctl(td, (struct ioctl_args *)args));
1803                 }
1804                 case 0x0030: {  /* SOUND_OLD_MIXER_INFO */
1805                         struct linux_old_mixer_info info;
1806                         bzero(&info, sizeof(info));
1807                         strncpy(info.id, "OSS", sizeof(info.id) - 1);
1808                         strncpy(info.name, "FreeBSD OSS Mixer", sizeof(info.name) - 1);
1809                         copyout(&info, (void *)args->arg, sizeof(info));
1810                         return (0);
1811                 }
1812                 default:
1813                         return (ENOIOCTL);
1814                 }
1815                 break;
1816         }
1817
1818         case LINUX_OSS_GETVERSION: {
1819                 int version = linux_get_oss_version(td);
1820                 return (copyout(&version, (void *)args->arg, sizeof(int)));
1821         }
1822
1823         case LINUX_SOUND_MIXER_READ_STEREODEVS:
1824                 args->cmd = SOUND_MIXER_READ_STEREODEVS;
1825                 return (sys_ioctl(td, (struct ioctl_args *)args));
1826
1827         case LINUX_SOUND_MIXER_READ_CAPS:
1828                 args->cmd = SOUND_MIXER_READ_CAPS;
1829                 return (sys_ioctl(td, (struct ioctl_args *)args));
1830
1831         case LINUX_SOUND_MIXER_READ_RECMASK:
1832                 args->cmd = SOUND_MIXER_READ_RECMASK;
1833                 return (sys_ioctl(td, (struct ioctl_args *)args));
1834
1835         case LINUX_SOUND_MIXER_READ_DEVMASK:
1836                 args->cmd = SOUND_MIXER_READ_DEVMASK;
1837                 return (sys_ioctl(td, (struct ioctl_args *)args));
1838
1839         case LINUX_SOUND_MIXER_WRITE_RECSRC:
1840                 args->cmd = SETDIR(SOUND_MIXER_WRITE_RECSRC);
1841                 return (sys_ioctl(td, (struct ioctl_args *)args));
1842
1843         case LINUX_SNDCTL_DSP_RESET:
1844                 args->cmd = SNDCTL_DSP_RESET;
1845                 return (sys_ioctl(td, (struct ioctl_args *)args));
1846
1847         case LINUX_SNDCTL_DSP_SYNC:
1848                 args->cmd = SNDCTL_DSP_SYNC;
1849                 return (sys_ioctl(td, (struct ioctl_args *)args));
1850
1851         case LINUX_SNDCTL_DSP_SPEED:
1852                 args->cmd = SNDCTL_DSP_SPEED;
1853                 return (sys_ioctl(td, (struct ioctl_args *)args));
1854
1855         case LINUX_SNDCTL_DSP_STEREO:
1856                 args->cmd = SNDCTL_DSP_STEREO;
1857                 return (sys_ioctl(td, (struct ioctl_args *)args));
1858
1859         case LINUX_SNDCTL_DSP_GETBLKSIZE: /* LINUX_SNDCTL_DSP_SETBLKSIZE */
1860                 args->cmd = SNDCTL_DSP_GETBLKSIZE;
1861                 return (sys_ioctl(td, (struct ioctl_args *)args));
1862
1863         case LINUX_SNDCTL_DSP_SETFMT:
1864                 args->cmd = SNDCTL_DSP_SETFMT;
1865                 return (sys_ioctl(td, (struct ioctl_args *)args));
1866
1867         case LINUX_SOUND_PCM_WRITE_CHANNELS:
1868                 args->cmd = SOUND_PCM_WRITE_CHANNELS;
1869                 return (sys_ioctl(td, (struct ioctl_args *)args));
1870
1871         case LINUX_SOUND_PCM_WRITE_FILTER:
1872                 args->cmd = SOUND_PCM_WRITE_FILTER;
1873                 return (sys_ioctl(td, (struct ioctl_args *)args));
1874
1875         case LINUX_SNDCTL_DSP_POST:
1876                 args->cmd = SNDCTL_DSP_POST;
1877                 return (sys_ioctl(td, (struct ioctl_args *)args));
1878
1879         case LINUX_SNDCTL_DSP_SUBDIVIDE:
1880                 args->cmd = SNDCTL_DSP_SUBDIVIDE;
1881                 return (sys_ioctl(td, (struct ioctl_args *)args));
1882
1883         case LINUX_SNDCTL_DSP_SETFRAGMENT:
1884                 args->cmd = SNDCTL_DSP_SETFRAGMENT;
1885                 return (sys_ioctl(td, (struct ioctl_args *)args));
1886
1887         case LINUX_SNDCTL_DSP_GETFMTS:
1888                 args->cmd = SNDCTL_DSP_GETFMTS;
1889                 return (sys_ioctl(td, (struct ioctl_args *)args));
1890
1891         case LINUX_SNDCTL_DSP_GETOSPACE:
1892                 args->cmd = SNDCTL_DSP_GETOSPACE;
1893                 return (sys_ioctl(td, (struct ioctl_args *)args));
1894
1895         case LINUX_SNDCTL_DSP_GETISPACE:
1896                 args->cmd = SNDCTL_DSP_GETISPACE;
1897                 return (sys_ioctl(td, (struct ioctl_args *)args));
1898
1899         case LINUX_SNDCTL_DSP_NONBLOCK:
1900                 args->cmd = SNDCTL_DSP_NONBLOCK;
1901                 return (sys_ioctl(td, (struct ioctl_args *)args));
1902
1903         case LINUX_SNDCTL_DSP_GETCAPS:
1904                 args->cmd = SNDCTL_DSP_GETCAPS;
1905                 return (sys_ioctl(td, (struct ioctl_args *)args));
1906
1907         case LINUX_SNDCTL_DSP_SETTRIGGER: /* LINUX_SNDCTL_GETTRIGGER */
1908                 args->cmd = SNDCTL_DSP_SETTRIGGER;
1909                 return (sys_ioctl(td, (struct ioctl_args *)args));
1910
1911         case LINUX_SNDCTL_DSP_GETIPTR:
1912                 args->cmd = SNDCTL_DSP_GETIPTR;
1913                 return (sys_ioctl(td, (struct ioctl_args *)args));
1914
1915         case LINUX_SNDCTL_DSP_GETOPTR:
1916                 args->cmd = SNDCTL_DSP_GETOPTR;
1917                 return (sys_ioctl(td, (struct ioctl_args *)args));
1918
1919         case LINUX_SNDCTL_DSP_SETDUPLEX:
1920                 args->cmd = SNDCTL_DSP_SETDUPLEX;
1921                 return (sys_ioctl(td, (struct ioctl_args *)args));
1922
1923         case LINUX_SNDCTL_DSP_GETODELAY:
1924                 args->cmd = SNDCTL_DSP_GETODELAY;
1925                 return (sys_ioctl(td, (struct ioctl_args *)args));
1926
1927         case LINUX_SNDCTL_SEQ_RESET:
1928                 args->cmd = SNDCTL_SEQ_RESET;
1929                 return (sys_ioctl(td, (struct ioctl_args *)args));
1930
1931         case LINUX_SNDCTL_SEQ_SYNC:
1932                 args->cmd = SNDCTL_SEQ_SYNC;
1933                 return (sys_ioctl(td, (struct ioctl_args *)args));
1934
1935         case LINUX_SNDCTL_SYNTH_INFO:
1936                 args->cmd = SNDCTL_SYNTH_INFO;
1937                 return (sys_ioctl(td, (struct ioctl_args *)args));
1938
1939         case LINUX_SNDCTL_SEQ_CTRLRATE:
1940                 args->cmd = SNDCTL_SEQ_CTRLRATE;
1941                 return (sys_ioctl(td, (struct ioctl_args *)args));
1942
1943         case LINUX_SNDCTL_SEQ_GETOUTCOUNT:
1944                 args->cmd = SNDCTL_SEQ_GETOUTCOUNT;
1945                 return (sys_ioctl(td, (struct ioctl_args *)args));
1946
1947         case LINUX_SNDCTL_SEQ_GETINCOUNT:
1948                 args->cmd = SNDCTL_SEQ_GETINCOUNT;
1949                 return (sys_ioctl(td, (struct ioctl_args *)args));
1950
1951         case LINUX_SNDCTL_SEQ_PERCMODE:
1952                 args->cmd = SNDCTL_SEQ_PERCMODE;
1953                 return (sys_ioctl(td, (struct ioctl_args *)args));
1954
1955         case LINUX_SNDCTL_FM_LOAD_INSTR:
1956                 args->cmd = SNDCTL_FM_LOAD_INSTR;
1957                 return (sys_ioctl(td, (struct ioctl_args *)args));
1958
1959         case LINUX_SNDCTL_SEQ_TESTMIDI:
1960                 args->cmd = SNDCTL_SEQ_TESTMIDI;
1961                 return (sys_ioctl(td, (struct ioctl_args *)args));
1962
1963         case LINUX_SNDCTL_SEQ_RESETSAMPLES:
1964                 args->cmd = SNDCTL_SEQ_RESETSAMPLES;
1965                 return (sys_ioctl(td, (struct ioctl_args *)args));
1966
1967         case LINUX_SNDCTL_SEQ_NRSYNTHS:
1968                 args->cmd = SNDCTL_SEQ_NRSYNTHS;
1969                 return (sys_ioctl(td, (struct ioctl_args *)args));
1970
1971         case LINUX_SNDCTL_SEQ_NRMIDIS:
1972                 args->cmd = SNDCTL_SEQ_NRMIDIS;
1973                 return (sys_ioctl(td, (struct ioctl_args *)args));
1974
1975         case LINUX_SNDCTL_MIDI_INFO:
1976                 args->cmd = SNDCTL_MIDI_INFO;
1977                 return (sys_ioctl(td, (struct ioctl_args *)args));
1978
1979         case LINUX_SNDCTL_SEQ_TRESHOLD:
1980                 args->cmd = SNDCTL_SEQ_TRESHOLD;
1981                 return (sys_ioctl(td, (struct ioctl_args *)args));
1982
1983         case LINUX_SNDCTL_SYNTH_MEMAVL:
1984                 args->cmd = SNDCTL_SYNTH_MEMAVL;
1985                 return (sys_ioctl(td, (struct ioctl_args *)args));
1986
1987         }
1988
1989         return (ENOIOCTL);
1990 }
1991
1992 /*
1993  * Console related ioctls
1994  */
1995
1996 static int
1997 linux_ioctl_console(struct thread *td, struct linux_ioctl_args *args)
1998 {
1999         struct file *fp;
2000         int error;
2001
2002         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
2003         if (error != 0)
2004                 return (error);
2005         switch (args->cmd & 0xffff) {
2006
2007         case LINUX_KIOCSOUND:
2008                 args->cmd = KIOCSOUND;
2009                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2010                 break;
2011
2012         case LINUX_KDMKTONE:
2013                 args->cmd = KDMKTONE;
2014                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2015                 break;
2016
2017         case LINUX_KDGETLED:
2018                 args->cmd = KDGETLED;
2019                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2020                 break;
2021
2022         case LINUX_KDSETLED:
2023                 args->cmd = KDSETLED;
2024                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2025                 break;
2026
2027         case LINUX_KDSETMODE:
2028                 args->cmd = KDSETMODE;
2029                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2030                 break;
2031
2032         case LINUX_KDGETMODE:
2033                 args->cmd = KDGETMODE;
2034                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2035                 break;
2036
2037         case LINUX_KDGKBMODE:
2038                 args->cmd = KDGKBMODE;
2039                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2040                 break;
2041
2042         case LINUX_KDSKBMODE: {
2043                 int kbdmode;
2044                 switch (args->arg) {
2045                 case LINUX_KBD_RAW:
2046                         kbdmode = K_RAW;
2047                         break;
2048                 case LINUX_KBD_XLATE:
2049                         kbdmode = K_XLATE;
2050                         break;
2051                 case LINUX_KBD_MEDIUMRAW:
2052                         kbdmode = K_RAW;
2053                         break;
2054                 default:
2055                         fdrop(fp, td);
2056                         return (EINVAL);
2057                 }
2058                 error = (fo_ioctl(fp, KDSKBMODE, (caddr_t)&kbdmode,
2059                     td->td_ucred, td));
2060                 break;
2061         }
2062
2063         case LINUX_VT_OPENQRY:
2064                 args->cmd = VT_OPENQRY;
2065                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2066                 break;
2067
2068         case LINUX_VT_GETMODE:
2069                 args->cmd = VT_GETMODE;
2070                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2071                 break;
2072
2073         case LINUX_VT_SETMODE: {
2074                 struct vt_mode mode;
2075                 if ((error = copyin((void *)args->arg, &mode, sizeof(mode))))
2076                         break;
2077                 if (LINUX_SIG_VALID(mode.relsig))
2078                         mode.relsig = linux_to_bsd_signal(mode.relsig);
2079                 else
2080                         mode.relsig = 0;
2081                 if (LINUX_SIG_VALID(mode.acqsig))
2082                         mode.acqsig = linux_to_bsd_signal(mode.acqsig);
2083                 else
2084                         mode.acqsig = 0;
2085                 /* XXX. Linux ignores frsig and set it to 0. */
2086                 mode.frsig = 0;
2087                 if ((error = copyout(&mode, (void *)args->arg, sizeof(mode))))
2088                         break;
2089                 args->cmd = VT_SETMODE;
2090                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2091                 break;
2092         }
2093
2094         case LINUX_VT_GETSTATE:
2095                 args->cmd = VT_GETACTIVE;
2096                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2097                 break;
2098
2099         case LINUX_VT_RELDISP:
2100                 args->cmd = VT_RELDISP;
2101                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2102                 break;
2103
2104         case LINUX_VT_ACTIVATE:
2105                 args->cmd = VT_ACTIVATE;
2106                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2107                 break;
2108
2109         case LINUX_VT_WAITACTIVE:
2110                 args->cmd = VT_WAITACTIVE;
2111                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2112                 break;
2113
2114         default:
2115                 error = ENOIOCTL;
2116                 break;
2117         }
2118
2119         fdrop(fp, td);
2120         return (error);
2121 }
2122
2123 /*
2124  * Criteria for interface name translation
2125  */
2126 #define IFP_IS_ETH(ifp) (ifp->if_type == IFT_ETHER)
2127
2128 /*
2129  * Translate a Linux interface name to a FreeBSD interface name,
2130  * and return the associated ifnet structure
2131  * bsdname and lxname need to be least IFNAMSIZ bytes long, but
2132  * can point to the same buffer.
2133  */
2134
2135 static struct ifnet *
2136 ifname_linux_to_bsd(struct thread *td, const char *lxname, char *bsdname)
2137 {
2138         struct ifnet *ifp;
2139         int len, unit;
2140         char *ep;
2141         int is_eth, index;
2142
2143         for (len = 0; len < LINUX_IFNAMSIZ; ++len)
2144                 if (!isalpha(lxname[len]))
2145                         break;
2146         if (len == 0 || len == LINUX_IFNAMSIZ)
2147                 return (NULL);
2148         unit = (int)strtoul(lxname + len, &ep, 10);
2149         if (ep == NULL || ep == lxname + len || ep >= lxname + LINUX_IFNAMSIZ)
2150                 return (NULL);
2151         index = 0;
2152         is_eth = (len == 3 && !strncmp(lxname, "eth", len)) ? 1 : 0;
2153         CURVNET_SET(TD_TO_VNET(td));
2154         IFNET_RLOCK();
2155         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2156                 /*
2157                  * Allow Linux programs to use FreeBSD names. Don't presume
2158                  * we never have an interface named "eth", so don't make
2159                  * the test optional based on is_eth.
2160                  */
2161                 if (strncmp(ifp->if_xname, lxname, LINUX_IFNAMSIZ) == 0)
2162                         break;
2163                 if (is_eth && IFP_IS_ETH(ifp) && unit == index++)
2164                         break;
2165         }
2166         IFNET_RUNLOCK();
2167         CURVNET_RESTORE();
2168         if (ifp != NULL)
2169                 strlcpy(bsdname, ifp->if_xname, IFNAMSIZ);
2170         return (ifp);
2171 }
2172
2173 /*
2174  * Implement the SIOCGIFNAME ioctl
2175  */
2176
2177 static int
2178 linux_ioctl_ifname(struct thread *td, struct l_ifreq *uifr)
2179 {
2180         struct l_ifreq ifr;
2181         struct ifnet *ifp;
2182         int error, ethno, index;
2183
2184         error = copyin(uifr, &ifr, sizeof(ifr));
2185         if (error != 0)
2186                 return (error);
2187
2188         CURVNET_SET(TD_TO_VNET(curthread));
2189         IFNET_RLOCK();
2190         index = 1;      /* ifr.ifr_ifindex starts from 1 */
2191         ethno = 0;
2192         error = ENODEV;
2193         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2194                 if (ifr.ifr_ifindex == index) {
2195                         if (IFP_IS_ETH(ifp))
2196                                 snprintf(ifr.ifr_name, LINUX_IFNAMSIZ,
2197                                     "eth%d", ethno);
2198                         else
2199                                 strlcpy(ifr.ifr_name, ifp->if_xname,
2200                                     LINUX_IFNAMSIZ);
2201                         error = 0;
2202                         break;
2203                 }
2204                 if (IFP_IS_ETH(ifp))
2205                         ethno++;
2206                 index++;
2207         }
2208         IFNET_RUNLOCK();
2209         if (error == 0)
2210                 error = copyout(&ifr, uifr, sizeof(ifr));
2211         CURVNET_RESTORE();
2212
2213         return (error);
2214 }
2215
2216 /*
2217  * Implement the SIOCGIFCONF ioctl
2218  */
2219
2220 static int
2221 linux_ifconf(struct thread *td, struct ifconf *uifc)
2222 {
2223 #ifdef COMPAT_LINUX32
2224         struct l_ifconf ifc;
2225 #else
2226         struct ifconf ifc;
2227 #endif
2228         struct l_ifreq ifr;
2229         struct ifnet *ifp;
2230         struct ifaddr *ifa;
2231         struct sbuf *sb;
2232         int error, ethno, full = 0, valid_len, max_len;
2233
2234         error = copyin(uifc, &ifc, sizeof(ifc));
2235         if (error != 0)
2236                 return (error);
2237
2238         max_len = MAXPHYS - 1;
2239
2240         CURVNET_SET(TD_TO_VNET(td));
2241         /* handle the 'request buffer size' case */
2242         if ((l_uintptr_t)ifc.ifc_buf == PTROUT(NULL)) {
2243                 ifc.ifc_len = 0;
2244                 IFNET_RLOCK();
2245                 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2246                         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2247                                 struct sockaddr *sa = ifa->ifa_addr;
2248                                 if (sa->sa_family == AF_INET)
2249                                         ifc.ifc_len += sizeof(ifr);
2250                         }
2251                 }
2252                 IFNET_RUNLOCK();
2253                 error = copyout(&ifc, uifc, sizeof(ifc));
2254                 CURVNET_RESTORE();
2255                 return (error);
2256         }
2257
2258         if (ifc.ifc_len <= 0) {
2259                 CURVNET_RESTORE();
2260                 return (EINVAL);
2261         }
2262
2263 again:
2264         /* Keep track of eth interfaces */
2265         ethno = 0;
2266         if (ifc.ifc_len <= max_len) {
2267                 max_len = ifc.ifc_len;
2268                 full = 1;
2269         }
2270         sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2271         max_len = 0;
2272         valid_len = 0;
2273
2274         /* Return all AF_INET addresses of all interfaces */
2275         IFNET_RLOCK();
2276         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2277                 int addrs = 0;
2278
2279                 bzero(&ifr, sizeof(ifr));
2280                 if (IFP_IS_ETH(ifp))
2281                         snprintf(ifr.ifr_name, LINUX_IFNAMSIZ, "eth%d",
2282                             ethno++);
2283                 else
2284                         strlcpy(ifr.ifr_name, ifp->if_xname, LINUX_IFNAMSIZ);
2285
2286                 /* Walk the address list */
2287                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2288                         struct sockaddr *sa = ifa->ifa_addr;
2289
2290                         if (sa->sa_family == AF_INET) {
2291                                 ifr.ifr_addr.sa_family = LINUX_AF_INET;
2292                                 memcpy(ifr.ifr_addr.sa_data, sa->sa_data,
2293                                     sizeof(ifr.ifr_addr.sa_data));
2294                                 sbuf_bcat(sb, &ifr, sizeof(ifr));
2295                                 max_len += sizeof(ifr);
2296                                 addrs++;
2297                         }
2298
2299                         if (sbuf_error(sb) == 0)
2300                                 valid_len = sbuf_len(sb);
2301                 }
2302                 if (addrs == 0) {
2303                         bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2304                         sbuf_bcat(sb, &ifr, sizeof(ifr));
2305                         max_len += sizeof(ifr);
2306
2307                         if (sbuf_error(sb) == 0)
2308                                 valid_len = sbuf_len(sb);
2309                 }
2310         }
2311         IFNET_RUNLOCK();
2312
2313         if (valid_len != max_len && !full) {
2314                 sbuf_delete(sb);
2315                 goto again;
2316         }
2317
2318         ifc.ifc_len = valid_len;
2319         sbuf_finish(sb);
2320         error = copyout(sbuf_data(sb), PTRIN(ifc.ifc_buf), ifc.ifc_len);
2321         if (error == 0)
2322                 error = copyout(&ifc, uifc, sizeof(ifc));
2323         sbuf_delete(sb);
2324         CURVNET_RESTORE();
2325
2326         return (error);
2327 }
2328
2329 static int
2330 linux_gifflags(struct thread *td, struct ifnet *ifp, struct l_ifreq *ifr)
2331 {
2332         l_short flags;
2333
2334         flags = (ifp->if_flags | ifp->if_drv_flags) & 0xffff;
2335         /* these flags have no Linux equivalent */
2336         flags &= ~(IFF_DRV_OACTIVE|IFF_SIMPLEX|
2337             IFF_LINK0|IFF_LINK1|IFF_LINK2);
2338         /* Linux' multicast flag is in a different bit */
2339         if (flags & IFF_MULTICAST) {
2340                 flags &= ~IFF_MULTICAST;
2341                 flags |= 0x1000;
2342         }
2343
2344         return (copyout(&flags, &ifr->ifr_flags, sizeof(flags)));
2345 }
2346
2347 #define ARPHRD_ETHER    1
2348 #define ARPHRD_LOOPBACK 772
2349
2350 static int
2351 linux_gifhwaddr(struct ifnet *ifp, struct l_ifreq *ifr)
2352 {
2353         struct ifaddr *ifa;
2354         struct sockaddr_dl *sdl;
2355         struct l_sockaddr lsa;
2356
2357         if (ifp->if_type == IFT_LOOP) {
2358                 bzero(&lsa, sizeof(lsa));
2359                 lsa.sa_family = ARPHRD_LOOPBACK;
2360                 return (copyout(&lsa, &ifr->ifr_hwaddr, sizeof(lsa)));
2361         }
2362
2363         if (ifp->if_type != IFT_ETHER)
2364                 return (ENOENT);
2365
2366         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2367                 sdl = (struct sockaddr_dl*)ifa->ifa_addr;
2368                 if (sdl != NULL && (sdl->sdl_family == AF_LINK) &&
2369                     (sdl->sdl_type == IFT_ETHER)) {
2370                         bzero(&lsa, sizeof(lsa));
2371                         lsa.sa_family = ARPHRD_ETHER;
2372                         bcopy(LLADDR(sdl), lsa.sa_data, LINUX_IFHWADDRLEN);
2373                         return (copyout(&lsa, &ifr->ifr_hwaddr, sizeof(lsa)));
2374                 }
2375         }
2376
2377         return (ENOENT);
2378 }
2379
2380
2381  /*
2382 * If we fault in bsd_to_linux_ifreq() then we will fault when we call
2383 * the native ioctl().  Thus, we don't really need to check the return
2384 * value of this function.
2385 */
2386 static int
2387 bsd_to_linux_ifreq(struct ifreq *arg)
2388 {
2389         struct ifreq ifr;
2390         size_t ifr_len = sizeof(struct ifreq);
2391         int error;
2392
2393         if ((error = copyin(arg, &ifr, ifr_len)))
2394                 return (error);
2395
2396         *(u_short *)&ifr.ifr_addr = ifr.ifr_addr.sa_family;
2397
2398         error = copyout(&ifr, arg, ifr_len);
2399
2400         return (error);
2401 }
2402
2403 /*
2404  * Socket related ioctls
2405  */
2406
2407 static int
2408 linux_ioctl_socket(struct thread *td, struct linux_ioctl_args *args)
2409 {
2410         char lifname[LINUX_IFNAMSIZ], ifname[IFNAMSIZ];
2411         struct ifnet *ifp;
2412         struct file *fp;
2413         int error, type;
2414
2415         ifp = NULL;
2416         error = 0;
2417
2418         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
2419         if (error != 0)
2420                 return (error);
2421         type = fp->f_type;
2422         fdrop(fp, td);
2423         if (type != DTYPE_SOCKET) {
2424                 /* not a socket - probably a tap / vmnet device */
2425                 switch (args->cmd) {
2426                 case LINUX_SIOCGIFADDR:
2427                 case LINUX_SIOCSIFADDR:
2428                 case LINUX_SIOCGIFFLAGS:
2429                         return (linux_ioctl_special(td, args));
2430                 default:
2431                         return (ENOIOCTL);
2432                 }
2433         }
2434
2435         switch (args->cmd & 0xffff) {
2436
2437         case LINUX_FIOGETOWN:
2438         case LINUX_FIOSETOWN:
2439         case LINUX_SIOCADDMULTI:
2440         case LINUX_SIOCATMARK:
2441         case LINUX_SIOCDELMULTI:
2442         case LINUX_SIOCGIFNAME:
2443         case LINUX_SIOCGIFCONF:
2444         case LINUX_SIOCGPGRP:
2445         case LINUX_SIOCSPGRP:
2446         case LINUX_SIOCGIFCOUNT:
2447                 /* these ioctls don't take an interface name */
2448 #ifdef DEBUG
2449                 printf("%s(): ioctl %d\n", __func__,
2450                     args->cmd & 0xffff);
2451 #endif
2452                 break;
2453
2454         case LINUX_SIOCGIFFLAGS:
2455         case LINUX_SIOCGIFADDR:
2456         case LINUX_SIOCSIFADDR:
2457         case LINUX_SIOCGIFDSTADDR:
2458         case LINUX_SIOCGIFBRDADDR:
2459         case LINUX_SIOCGIFNETMASK:
2460         case LINUX_SIOCSIFNETMASK:
2461         case LINUX_SIOCGIFMTU:
2462         case LINUX_SIOCSIFMTU:
2463         case LINUX_SIOCSIFNAME:
2464         case LINUX_SIOCGIFHWADDR:
2465         case LINUX_SIOCSIFHWADDR:
2466         case LINUX_SIOCDEVPRIVATE:
2467         case LINUX_SIOCDEVPRIVATE+1:
2468         case LINUX_SIOCGIFINDEX:
2469                 /* copy in the interface name and translate it. */
2470                 error = copyin((void *)args->arg, lifname, LINUX_IFNAMSIZ);
2471                 if (error != 0)
2472                         return (error);
2473 #ifdef DEBUG
2474                 printf("%s(): ioctl %d on %.*s\n", __func__,
2475                     args->cmd & 0xffff, LINUX_IFNAMSIZ, lifname);
2476 #endif
2477                 memset(ifname, 0, sizeof(ifname));
2478                 ifp = ifname_linux_to_bsd(td, lifname, ifname);
2479                 if (ifp == NULL)
2480                         return (EINVAL);
2481                 /*
2482                  * We need to copy it back out in case we pass the
2483                  * request on to our native ioctl(), which will expect
2484                  * the ifreq to be in user space and have the correct
2485                  * interface name.
2486                  */
2487                 error = copyout(ifname, (void *)args->arg, IFNAMSIZ);
2488                 if (error != 0)
2489                         return (error);
2490 #ifdef DEBUG
2491                 printf("%s(): %s translated to %s\n", __func__,
2492                     lifname, ifname);
2493 #endif
2494                 break;
2495
2496         default:
2497                 return (ENOIOCTL);
2498         }
2499
2500         switch (args->cmd & 0xffff) {
2501
2502         case LINUX_FIOSETOWN:
2503                 args->cmd = FIOSETOWN;
2504                 error = sys_ioctl(td, (struct ioctl_args *)args);
2505                 break;
2506
2507         case LINUX_SIOCSPGRP:
2508                 args->cmd = SIOCSPGRP;
2509                 error = sys_ioctl(td, (struct ioctl_args *)args);
2510                 break;
2511
2512         case LINUX_FIOGETOWN:
2513                 args->cmd = FIOGETOWN;
2514                 error = sys_ioctl(td, (struct ioctl_args *)args);
2515                 break;
2516
2517         case LINUX_SIOCGPGRP:
2518                 args->cmd = SIOCGPGRP;
2519                 error = sys_ioctl(td, (struct ioctl_args *)args);
2520                 break;
2521
2522         case LINUX_SIOCATMARK:
2523                 args->cmd = SIOCATMARK;
2524                 error = sys_ioctl(td, (struct ioctl_args *)args);
2525                 break;
2526
2527         /* LINUX_SIOCGSTAMP */
2528
2529         case LINUX_SIOCGIFNAME:
2530                 error = linux_ioctl_ifname(td, (struct l_ifreq *)args->arg);
2531                 break;
2532
2533         case LINUX_SIOCGIFCONF:
2534                 error = linux_ifconf(td, (struct ifconf *)args->arg);
2535                 break;
2536
2537         case LINUX_SIOCGIFFLAGS:
2538                 args->cmd = SIOCGIFFLAGS;
2539                 error = linux_gifflags(td, ifp, (struct l_ifreq *)args->arg);
2540                 break;
2541
2542         case LINUX_SIOCGIFADDR:
2543                 args->cmd = SIOCGIFADDR;
2544                 error = sys_ioctl(td, (struct ioctl_args *)args);
2545                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
2546                 break;
2547
2548         case LINUX_SIOCSIFADDR:
2549                 /* XXX probably doesn't work, included for completeness */
2550                 args->cmd = SIOCSIFADDR;
2551                 error = sys_ioctl(td, (struct ioctl_args *)args);
2552                 break;
2553
2554         case LINUX_SIOCGIFDSTADDR:
2555                 args->cmd = SIOCGIFDSTADDR;
2556                 error = sys_ioctl(td, (struct ioctl_args *)args);
2557                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
2558                 break;
2559
2560         case LINUX_SIOCGIFBRDADDR:
2561                 args->cmd = SIOCGIFBRDADDR;
2562                 error = sys_ioctl(td, (struct ioctl_args *)args);
2563                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
2564                 break;
2565
2566         case LINUX_SIOCGIFNETMASK:
2567                 args->cmd = SIOCGIFNETMASK;
2568                 error = sys_ioctl(td, (struct ioctl_args *)args);
2569                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
2570                 break;
2571
2572         case LINUX_SIOCSIFNETMASK:
2573                 error = ENOIOCTL;
2574                 break;
2575
2576         case LINUX_SIOCGIFMTU:
2577                 args->cmd = SIOCGIFMTU;
2578                 error = sys_ioctl(td, (struct ioctl_args *)args);
2579                 break;
2580
2581         case LINUX_SIOCSIFMTU:
2582                 args->cmd = SIOCSIFMTU;
2583                 error = sys_ioctl(td, (struct ioctl_args *)args);
2584                 break;
2585
2586         case LINUX_SIOCSIFNAME:
2587                 error = ENOIOCTL;
2588                 break;
2589
2590         case LINUX_SIOCGIFHWADDR:
2591                 error = linux_gifhwaddr(ifp, (struct l_ifreq *)args->arg);
2592                 break;
2593
2594         case LINUX_SIOCSIFHWADDR:
2595                 error = ENOIOCTL;
2596                 break;
2597
2598         case LINUX_SIOCADDMULTI:
2599                 args->cmd = SIOCADDMULTI;
2600                 error = sys_ioctl(td, (struct ioctl_args *)args);
2601                 break;
2602
2603         case LINUX_SIOCDELMULTI:
2604                 args->cmd = SIOCDELMULTI;
2605                 error = sys_ioctl(td, (struct ioctl_args *)args);
2606                 break;
2607
2608         case LINUX_SIOCGIFINDEX:
2609                 args->cmd = SIOCGIFINDEX;
2610                 error = sys_ioctl(td, (struct ioctl_args *)args);
2611                 break;
2612
2613         case LINUX_SIOCGIFCOUNT:
2614                 error = 0;
2615                 break;
2616
2617         /*
2618          * XXX This is slightly bogus, but these ioctls are currently
2619          * XXX only used by the aironet (if_an) network driver.
2620          */
2621         case LINUX_SIOCDEVPRIVATE:
2622                 args->cmd = SIOCGPRIVATE_0;
2623                 error = sys_ioctl(td, (struct ioctl_args *)args);
2624                 break;
2625
2626         case LINUX_SIOCDEVPRIVATE+1:
2627                 args->cmd = SIOCGPRIVATE_1;
2628                 error = sys_ioctl(td, (struct ioctl_args *)args);
2629                 break;
2630         }
2631
2632         if (ifp != NULL)
2633                 /* restore the original interface name */
2634                 copyout(lifname, (void *)args->arg, LINUX_IFNAMSIZ);
2635
2636 #ifdef DEBUG
2637         printf("%s(): returning %d\n", __func__, error);
2638 #endif
2639         return (error);
2640 }
2641
2642 /*
2643  * Device private ioctl handler
2644  */
2645 static int
2646 linux_ioctl_private(struct thread *td, struct linux_ioctl_args *args)
2647 {
2648         struct file *fp;
2649         int error, type;
2650
2651         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
2652         if (error != 0)
2653                 return (error);
2654         type = fp->f_type;
2655         fdrop(fp, td);
2656         if (type == DTYPE_SOCKET)
2657                 return (linux_ioctl_socket(td, args));
2658         return (ENOIOCTL);
2659 }
2660
2661 /*
2662  * DRM ioctl handler (sys/dev/drm)
2663  */
2664 static int
2665 linux_ioctl_drm(struct thread *td, struct linux_ioctl_args *args)
2666 {
2667         args->cmd = SETDIR(args->cmd);
2668         return (sys_ioctl(td, (struct ioctl_args *)args));
2669 }
2670
2671 #ifdef COMPAT_LINUX32
2672 #define CP(src,dst,fld) do { (dst).fld = (src).fld; } while (0)
2673 #define PTRIN_CP(src,dst,fld) \
2674         do { (dst).fld = PTRIN((src).fld); } while (0)
2675 #define PTROUT_CP(src,dst,fld) \
2676         do { (dst).fld = PTROUT((src).fld); } while (0)
2677
2678 static int
2679 linux_ioctl_sg_io(struct thread *td, struct linux_ioctl_args *args)
2680 {
2681         struct sg_io_hdr io;
2682         struct sg_io_hdr32 io32;
2683         struct file *fp;
2684         int error;
2685
2686         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
2687         if (error != 0) {
2688                 printf("sg_linux_ioctl: fget returned %d\n", error);
2689                 return (error);
2690         }
2691
2692         if ((error = copyin((void *)args->arg, &io32, sizeof(io32))) != 0)
2693                 goto out;
2694
2695         CP(io32, io, interface_id);
2696         CP(io32, io, dxfer_direction);
2697         CP(io32, io, cmd_len);
2698         CP(io32, io, mx_sb_len);
2699         CP(io32, io, iovec_count);
2700         CP(io32, io, dxfer_len);
2701         PTRIN_CP(io32, io, dxferp);
2702         PTRIN_CP(io32, io, cmdp);
2703         PTRIN_CP(io32, io, sbp);
2704         CP(io32, io, timeout);
2705         CP(io32, io, flags);
2706         CP(io32, io, pack_id);
2707         PTRIN_CP(io32, io, usr_ptr);
2708         CP(io32, io, status);
2709         CP(io32, io, masked_status);
2710         CP(io32, io, msg_status);
2711         CP(io32, io, sb_len_wr);
2712         CP(io32, io, host_status);
2713         CP(io32, io, driver_status);
2714         CP(io32, io, resid);
2715         CP(io32, io, duration);
2716         CP(io32, io, info);
2717
2718         if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
2719                 goto out;
2720
2721         CP(io, io32, interface_id);
2722         CP(io, io32, dxfer_direction);
2723         CP(io, io32, cmd_len);
2724         CP(io, io32, mx_sb_len);
2725         CP(io, io32, iovec_count);
2726         CP(io, io32, dxfer_len);
2727         PTROUT_CP(io, io32, dxferp);
2728         PTROUT_CP(io, io32, cmdp);
2729         PTROUT_CP(io, io32, sbp);
2730         CP(io, io32, timeout);
2731         CP(io, io32, flags);
2732         CP(io, io32, pack_id);
2733         PTROUT_CP(io, io32, usr_ptr);
2734         CP(io, io32, status);
2735         CP(io, io32, masked_status);
2736         CP(io, io32, msg_status);
2737         CP(io, io32, sb_len_wr);
2738         CP(io, io32, host_status);
2739         CP(io, io32, driver_status);
2740         CP(io, io32, resid);
2741         CP(io, io32, duration);
2742         CP(io, io32, info);
2743
2744         error = copyout(&io32, (void *)args->arg, sizeof(io32));
2745
2746 out:
2747         fdrop(fp, td);
2748         return (error);
2749 }
2750 #endif
2751
2752 static int
2753 linux_ioctl_sg(struct thread *td, struct linux_ioctl_args *args)
2754 {
2755
2756         switch (args->cmd) {
2757         case LINUX_SG_GET_VERSION_NUM:
2758                 args->cmd = SG_GET_VERSION_NUM;
2759                 break;
2760         case LINUX_SG_SET_TIMEOUT:
2761                 args->cmd = SG_SET_TIMEOUT;
2762                 break;
2763         case LINUX_SG_GET_TIMEOUT:
2764                 args->cmd = SG_GET_TIMEOUT;
2765                 break;
2766         case LINUX_SG_IO:
2767                 args->cmd = SG_IO;
2768 #ifdef COMPAT_LINUX32
2769                 return (linux_ioctl_sg_io(td, args));
2770 #endif
2771                 break;
2772         case LINUX_SG_GET_RESERVED_SIZE:
2773                 args->cmd = SG_GET_RESERVED_SIZE;
2774                 break;
2775         case LINUX_SG_GET_SCSI_ID:
2776                 args->cmd = SG_GET_SCSI_ID;
2777                 break;
2778         case LINUX_SG_GET_SG_TABLESIZE:
2779                 args->cmd = SG_GET_SG_TABLESIZE;
2780                 break;
2781         default:
2782                 return (ENODEV);
2783         }
2784         return (sys_ioctl(td, (struct ioctl_args *)args));
2785 }
2786
2787 /*
2788  * Video4Linux (V4L) ioctl handler
2789  */
2790 static int
2791 linux_to_bsd_v4l_tuner(struct l_video_tuner *lvt, struct video_tuner *vt)
2792 {
2793         vt->tuner = lvt->tuner;
2794         strlcpy(vt->name, lvt->name, LINUX_VIDEO_TUNER_NAME_SIZE);
2795         vt->rangelow = lvt->rangelow;   /* possible long size conversion */
2796         vt->rangehigh = lvt->rangehigh; /* possible long size conversion */
2797         vt->flags = lvt->flags;
2798         vt->mode = lvt->mode;
2799         vt->signal = lvt->signal;
2800         return (0);
2801 }
2802
2803 static int
2804 bsd_to_linux_v4l_tuner(struct video_tuner *vt, struct l_video_tuner *lvt)
2805 {
2806         lvt->tuner = vt->tuner;
2807         strlcpy(lvt->name, vt->name, LINUX_VIDEO_TUNER_NAME_SIZE);
2808         lvt->rangelow = vt->rangelow;   /* possible long size conversion */
2809         lvt->rangehigh = vt->rangehigh; /* possible long size conversion */
2810         lvt->flags = vt->flags;
2811         lvt->mode = vt->mode;
2812         lvt->signal = vt->signal;
2813         return (0);
2814 }
2815
2816 #ifdef COMPAT_LINUX_V4L_CLIPLIST
2817 static int
2818 linux_to_bsd_v4l_clip(struct l_video_clip *lvc, struct video_clip *vc)
2819 {
2820         vc->x = lvc->x;
2821         vc->y = lvc->y;
2822         vc->width = lvc->width;
2823         vc->height = lvc->height;
2824         vc->next = PTRIN(lvc->next);    /* possible pointer size conversion */
2825         return (0);
2826 }
2827 #endif
2828
2829 static int
2830 linux_to_bsd_v4l_window(struct l_video_window *lvw, struct video_window *vw)
2831 {
2832         vw->x = lvw->x;
2833         vw->y = lvw->y;
2834         vw->width = lvw->width;
2835         vw->height = lvw->height;
2836         vw->chromakey = lvw->chromakey;
2837         vw->flags = lvw->flags;
2838         vw->clips = PTRIN(lvw->clips);  /* possible pointer size conversion */
2839         vw->clipcount = lvw->clipcount;
2840         return (0);
2841 }
2842
2843 static int
2844 bsd_to_linux_v4l_window(struct video_window *vw, struct l_video_window *lvw)
2845 {
2846         lvw->x = vw->x;
2847         lvw->y = vw->y;
2848         lvw->width = vw->width;
2849         lvw->height = vw->height;
2850         lvw->chromakey = vw->chromakey;
2851         lvw->flags = vw->flags;
2852         lvw->clips = PTROUT(vw->clips); /* possible pointer size conversion */
2853         lvw->clipcount = vw->clipcount;
2854         return (0);
2855 }
2856
2857 static int
2858 linux_to_bsd_v4l_buffer(struct l_video_buffer *lvb, struct video_buffer *vb)
2859 {
2860         vb->base = PTRIN(lvb->base);    /* possible pointer size conversion */
2861         vb->height = lvb->height;
2862         vb->width = lvb->width;
2863         vb->depth = lvb->depth;
2864         vb->bytesperline = lvb->bytesperline;
2865         return (0);
2866 }
2867
2868 static int
2869 bsd_to_linux_v4l_buffer(struct video_buffer *vb, struct l_video_buffer *lvb)
2870 {
2871         lvb->base = PTROUT(vb->base);   /* possible pointer size conversion */
2872         lvb->height = vb->height;
2873         lvb->width = vb->width;
2874         lvb->depth = vb->depth;
2875         lvb->bytesperline = vb->bytesperline;
2876         return (0);
2877 }
2878
2879 static int
2880 linux_to_bsd_v4l_code(struct l_video_code *lvc, struct video_code *vc)
2881 {
2882         strlcpy(vc->loadwhat, lvc->loadwhat, LINUX_VIDEO_CODE_LOADWHAT_SIZE);
2883         vc->datasize = lvc->datasize;
2884         vc->data = PTRIN(lvc->data);    /* possible pointer size conversion */
2885         return (0);
2886 }
2887
2888 #ifdef COMPAT_LINUX_V4L_CLIPLIST
2889 static int
2890 linux_v4l_clip_copy(void *lvc, struct video_clip **ppvc)
2891 {
2892         int error;
2893         struct video_clip vclip;
2894         struct l_video_clip l_vclip;
2895
2896         error = copyin(lvc, &l_vclip, sizeof(l_vclip));
2897         if (error) return (error);
2898         linux_to_bsd_v4l_clip(&l_vclip, &vclip);
2899         /* XXX: If there can be no concurrency: s/M_NOWAIT/M_WAITOK/ */
2900         if ((*ppvc = malloc(sizeof(**ppvc), M_LINUX, M_NOWAIT)) == NULL)
2901                 return (ENOMEM);    /* XXX: Linux has no ENOMEM here. */
2902         memcpy(*ppvc, &vclip, sizeof(vclip));
2903         (*ppvc)->next = NULL;
2904         return (0);
2905 }
2906
2907 static int
2908 linux_v4l_cliplist_free(struct video_window *vw)
2909 {
2910         struct video_clip **ppvc;
2911         struct video_clip **ppvc_next;
2912
2913         for (ppvc = &(vw->clips); *ppvc != NULL; ppvc = ppvc_next) {
2914                 ppvc_next = &((*ppvc)->next);
2915                 free(*ppvc, M_LINUX);
2916         }
2917         vw->clips = NULL;
2918
2919         return (0);
2920 }
2921
2922 static int
2923 linux_v4l_cliplist_copy(struct l_video_window *lvw, struct video_window *vw)
2924 {
2925         int error;
2926         int clipcount;
2927         void *plvc;
2928         struct video_clip **ppvc;
2929
2930         /*
2931          * XXX: The cliplist is used to pass in a list of clipping
2932          *      rectangles or, if clipcount == VIDEO_CLIP_BITMAP, a
2933          *      clipping bitmap.  Some Linux apps, however, appear to
2934          *      leave cliplist and clips uninitialized.  In any case,
2935          *      the cliplist is not used by pwc(4), at the time of
2936          *      writing, FreeBSD's only V4L driver.  When a driver
2937          *      that uses the cliplist is developed, this code may
2938          *      need re-examiniation.
2939          */
2940         error = 0;
2941         clipcount = vw->clipcount;
2942         if (clipcount == VIDEO_CLIP_BITMAP) {
2943                 /*
2944                  * In this case, the pointer (clips) is overloaded
2945                  * to be a "void *" to a bitmap, therefore there
2946                  * is no struct video_clip to copy now.
2947                  */
2948         } else if (clipcount > 0 && clipcount <= 16384) {
2949                 /*
2950                  * Clips points to list of clip rectangles, so
2951                  * copy the list.
2952                  *
2953                  * XXX: Upper limit of 16384 was used here to try to
2954                  *      avoid cases when clipcount and clips pointer
2955                  *      are uninitialized and therefore have high random
2956                  *      values, as is the case in the Linux Skype
2957                  *      application.  The value 16384 was chosen as that
2958                  *      is what is used in the Linux stradis(4) MPEG
2959                  *      decoder driver, the only place we found an
2960                  *      example of cliplist use.
2961                  */
2962                 plvc = PTRIN(lvw->clips);
2963                 vw->clips = NULL;
2964                 ppvc = &(vw->clips);
2965                 while (clipcount-- > 0) {
2966                         if (plvc == NULL) {
2967                                 error = EFAULT;
2968                                 break;
2969                         } else {
2970                                 error = linux_v4l_clip_copy(plvc, ppvc);
2971                                 if (error) {
2972                                         linux_v4l_cliplist_free(vw);
2973                                         break;
2974                                 }
2975                         }
2976                         ppvc = &((*ppvc)->next);
2977                         plvc = PTRIN(((struct l_video_clip *) plvc)->next);
2978                 }
2979         } else {
2980                 /*
2981                  * clipcount == 0 or negative (but not VIDEO_CLIP_BITMAP)
2982                  * Force cliplist to null.
2983                  */
2984                 vw->clipcount = 0;
2985                 vw->clips = NULL;
2986         }
2987         return (error);
2988 }
2989 #endif
2990
2991 static int
2992 linux_ioctl_v4l(struct thread *td, struct linux_ioctl_args *args)
2993 {
2994         struct file *fp;
2995         int error;
2996         struct video_tuner vtun;
2997         struct video_window vwin;
2998         struct video_buffer vbuf;
2999         struct video_code vcode;
3000         struct l_video_tuner l_vtun;
3001         struct l_video_window l_vwin;
3002         struct l_video_buffer l_vbuf;
3003         struct l_video_code l_vcode;
3004
3005         switch (args->cmd & 0xffff) {
3006         case LINUX_VIDIOCGCAP:          args->cmd = VIDIOCGCAP; break;
3007         case LINUX_VIDIOCGCHAN:         args->cmd = VIDIOCGCHAN; break;
3008         case LINUX_VIDIOCSCHAN:         args->cmd = VIDIOCSCHAN; break;
3009
3010         case LINUX_VIDIOCGTUNER:
3011                 error = fget(td, args->fd,
3012                     &cap_ioctl_rights, &fp);
3013                 if (error != 0)
3014                         return (error);
3015                 error = copyin((void *) args->arg, &l_vtun, sizeof(l_vtun));
3016                 if (error) {
3017                         fdrop(fp, td);
3018                         return (error);
3019                 }
3020                 linux_to_bsd_v4l_tuner(&l_vtun, &vtun);
3021                 error = fo_ioctl(fp, VIDIOCGTUNER, &vtun, td->td_ucred, td);
3022                 if (!error) {
3023                         bsd_to_linux_v4l_tuner(&vtun, &l_vtun);
3024                         error = copyout(&l_vtun, (void *) args->arg,
3025                             sizeof(l_vtun));
3026                 }
3027                 fdrop(fp, td);
3028                 return (error);
3029
3030         case LINUX_VIDIOCSTUNER:
3031                 error = fget(td, args->fd,
3032                     &cap_ioctl_rights, &fp);
3033                 if (error != 0)
3034                         return (error);
3035                 error = copyin((void *) args->arg, &l_vtun, sizeof(l_vtun));
3036                 if (error) {
3037                         fdrop(fp, td);
3038                         return (error);
3039                 }
3040                 linux_to_bsd_v4l_tuner(&l_vtun, &vtun);
3041                 error = fo_ioctl(fp, VIDIOCSTUNER, &vtun, td->td_ucred, td);
3042                 fdrop(fp, td);
3043                 return (error);
3044
3045         case LINUX_VIDIOCGPICT:         args->cmd = VIDIOCGPICT; break;
3046         case LINUX_VIDIOCSPICT:         args->cmd = VIDIOCSPICT; break;
3047         case LINUX_VIDIOCCAPTURE:       args->cmd = VIDIOCCAPTURE; break;
3048
3049         case LINUX_VIDIOCGWIN:
3050                 error = fget(td, args->fd,
3051                     &cap_ioctl_rights, &fp);
3052                 if (error != 0)
3053                         return (error);
3054                 error = fo_ioctl(fp, VIDIOCGWIN, &vwin, td->td_ucred, td);
3055                 if (!error) {
3056                         bsd_to_linux_v4l_window(&vwin, &l_vwin);
3057                         error = copyout(&l_vwin, (void *) args->arg,
3058                             sizeof(l_vwin));
3059                 }
3060                 fdrop(fp, td);
3061                 return (error);
3062
3063         case LINUX_VIDIOCSWIN:
3064                 error = fget(td, args->fd,
3065                     &cap_ioctl_rights, &fp);
3066                 if (error != 0)
3067                         return (error);
3068                 error = copyin((void *) args->arg, &l_vwin, sizeof(l_vwin));
3069                 if (error) {
3070                         fdrop(fp, td);
3071                         return (error);
3072                 }
3073                 linux_to_bsd_v4l_window(&l_vwin, &vwin);
3074 #ifdef COMPAT_LINUX_V4L_CLIPLIST
3075                 error = linux_v4l_cliplist_copy(&l_vwin, &vwin);
3076                 if (error) {
3077                         fdrop(fp, td);
3078                         return (error);
3079                 }
3080 #endif
3081                 error = fo_ioctl(fp, VIDIOCSWIN, &vwin, td->td_ucred, td);
3082                 fdrop(fp, td);
3083 #ifdef COMPAT_LINUX_V4L_CLIPLIST
3084                 linux_v4l_cliplist_free(&vwin);
3085 #endif
3086                 return (error);
3087
3088         case LINUX_VIDIOCGFBUF:
3089                 error = fget(td, args->fd,
3090                     &cap_ioctl_rights, &fp);
3091                 if (error != 0)
3092                         return (error);
3093                 error = fo_ioctl(fp, VIDIOCGFBUF, &vbuf, td->td_ucred, td);
3094                 if (!error) {
3095                         bsd_to_linux_v4l_buffer(&vbuf, &l_vbuf);
3096                         error = copyout(&l_vbuf, (void *) args->arg,
3097                             sizeof(l_vbuf));
3098                 }
3099                 fdrop(fp, td);
3100                 return (error);
3101
3102         case LINUX_VIDIOCSFBUF:
3103                 error = fget(td, args->fd,
3104                     &cap_ioctl_rights, &fp);
3105                 if (error != 0)
3106                         return (error);
3107                 error = copyin((void *) args->arg, &l_vbuf, sizeof(l_vbuf));
3108                 if (error) {
3109                         fdrop(fp, td);
3110                         return (error);
3111                 }
3112                 linux_to_bsd_v4l_buffer(&l_vbuf, &vbuf);
3113                 error = fo_ioctl(fp, VIDIOCSFBUF, &vbuf, td->td_ucred, td);
3114                 fdrop(fp, td);
3115                 return (error);
3116
3117         case LINUX_VIDIOCKEY:           args->cmd = VIDIOCKEY; break;
3118         case LINUX_VIDIOCGFREQ:         args->cmd = VIDIOCGFREQ; break;
3119         case LINUX_VIDIOCSFREQ:         args->cmd = VIDIOCSFREQ; break;
3120         case LINUX_VIDIOCGAUDIO:        args->cmd = VIDIOCGAUDIO; break;
3121         case LINUX_VIDIOCSAUDIO:        args->cmd = VIDIOCSAUDIO; break;
3122         case LINUX_VIDIOCSYNC:          args->cmd = VIDIOCSYNC; break;
3123         case LINUX_VIDIOCMCAPTURE:      args->cmd = VIDIOCMCAPTURE; break;
3124         case LINUX_VIDIOCGMBUF:         args->cmd = VIDIOCGMBUF; break;
3125         case LINUX_VIDIOCGUNIT:         args->cmd = VIDIOCGUNIT; break;
3126         case LINUX_VIDIOCGCAPTURE:      args->cmd = VIDIOCGCAPTURE; break;
3127         case LINUX_VIDIOCSCAPTURE:      args->cmd = VIDIOCSCAPTURE; break;
3128         case LINUX_VIDIOCSPLAYMODE:     args->cmd = VIDIOCSPLAYMODE; break;
3129         case LINUX_VIDIOCSWRITEMODE:    args->cmd = VIDIOCSWRITEMODE; break;
3130         case LINUX_VIDIOCGPLAYINFO:     args->cmd = VIDIOCGPLAYINFO; break;
3131
3132         case LINUX_VIDIOCSMICROCODE:
3133                 error = fget(td, args->fd,
3134                     &cap_ioctl_rights, &fp);
3135                 if (error != 0)
3136                         return (error);
3137                 error = copyin((void *) args->arg, &l_vcode, sizeof(l_vcode));
3138                 if (error) {
3139                         fdrop(fp, td);
3140                         return (error);
3141                 }
3142                 linux_to_bsd_v4l_code(&l_vcode, &vcode);
3143                 error = fo_ioctl(fp, VIDIOCSMICROCODE, &vcode, td->td_ucred, td);
3144                 fdrop(fp, td);
3145                 return (error);
3146
3147         case LINUX_VIDIOCGVBIFMT:       args->cmd = VIDIOCGVBIFMT; break;
3148         case LINUX_VIDIOCSVBIFMT:       args->cmd = VIDIOCSVBIFMT; break;
3149         default:                        return (ENOIOCTL);
3150         }
3151
3152         error = sys_ioctl(td, (struct ioctl_args *)args);
3153         return (error);
3154 }
3155
3156 /*
3157  * Special ioctl handler
3158  */
3159 static int
3160 linux_ioctl_special(struct thread *td, struct linux_ioctl_args *args)
3161 {
3162         int error;
3163
3164         switch (args->cmd) {
3165         case LINUX_SIOCGIFADDR:
3166                 args->cmd = SIOCGIFADDR;
3167                 error = sys_ioctl(td, (struct ioctl_args *)args);
3168                 break;
3169         case LINUX_SIOCSIFADDR:
3170                 args->cmd = SIOCSIFADDR;
3171                 error = sys_ioctl(td, (struct ioctl_args *)args);
3172                 break;
3173         case LINUX_SIOCGIFFLAGS:
3174                 args->cmd = SIOCGIFFLAGS;
3175                 error = sys_ioctl(td, (struct ioctl_args *)args);
3176                 break;
3177         default:
3178                 error = ENOIOCTL;
3179         }
3180
3181         return (error);
3182 }
3183
3184 static int
3185 linux_to_bsd_v4l2_standard(struct l_v4l2_standard *lvstd, struct v4l2_standard *vstd)
3186 {
3187         vstd->index = lvstd->index;
3188         vstd->id = lvstd->id;
3189         CTASSERT(sizeof(vstd->name) == sizeof(lvstd->name));
3190         memcpy(vstd->name, lvstd->name, sizeof(vstd->name));
3191         vstd->frameperiod = lvstd->frameperiod;
3192         vstd->framelines = lvstd->framelines;
3193         CTASSERT(sizeof(vstd->reserved) == sizeof(lvstd->reserved));
3194         memcpy(vstd->reserved, lvstd->reserved, sizeof(vstd->reserved));
3195         return (0);
3196 }
3197
3198 static int
3199 bsd_to_linux_v4l2_standard(struct v4l2_standard *vstd, struct l_v4l2_standard *lvstd)
3200 {
3201         lvstd->index = vstd->index;
3202         lvstd->id = vstd->id;
3203         CTASSERT(sizeof(vstd->name) == sizeof(lvstd->name));
3204         memcpy(lvstd->name, vstd->name, sizeof(lvstd->name));
3205         lvstd->frameperiod = vstd->frameperiod;
3206         lvstd->framelines = vstd->framelines;
3207         CTASSERT(sizeof(vstd->reserved) == sizeof(lvstd->reserved));
3208         memcpy(lvstd->reserved, vstd->reserved, sizeof(lvstd->reserved));
3209         return (0);
3210 }
3211
3212 static int
3213 linux_to_bsd_v4l2_buffer(struct l_v4l2_buffer *lvb, struct v4l2_buffer *vb)
3214 {
3215         vb->index = lvb->index;
3216         vb->type = lvb->type;
3217         vb->bytesused = lvb->bytesused;
3218         vb->flags = lvb->flags;
3219         vb->field = lvb->field;
3220         vb->timestamp.tv_sec = lvb->timestamp.tv_sec;
3221         vb->timestamp.tv_usec = lvb->timestamp.tv_usec;
3222         memcpy(&vb->timecode, &lvb->timecode, sizeof (lvb->timecode));
3223         vb->sequence = lvb->sequence;
3224         vb->memory = lvb->memory;
3225         if (lvb->memory == V4L2_MEMORY_USERPTR)
3226                 /* possible pointer size conversion */
3227                 vb->m.userptr = (unsigned long)PTRIN(lvb->m.userptr);
3228         else
3229                 vb->m.offset = lvb->m.offset;
3230         vb->length = lvb->length;
3231         vb->input = lvb->input;
3232         vb->reserved = lvb->reserved;
3233         return (0);
3234 }
3235
3236 static int
3237 bsd_to_linux_v4l2_buffer(struct v4l2_buffer *vb, struct l_v4l2_buffer *lvb)
3238 {
3239         lvb->index = vb->index;
3240         lvb->type = vb->type;
3241         lvb->bytesused = vb->bytesused;
3242         lvb->flags = vb->flags;
3243         lvb->field = vb->field;
3244         lvb->timestamp.tv_sec = vb->timestamp.tv_sec;
3245         lvb->timestamp.tv_usec = vb->timestamp.tv_usec;
3246         memcpy(&lvb->timecode, &vb->timecode, sizeof (vb->timecode));
3247         lvb->sequence = vb->sequence;
3248         lvb->memory = vb->memory;
3249         if (vb->memory == V4L2_MEMORY_USERPTR)
3250                 /* possible pointer size conversion */
3251                 lvb->m.userptr = PTROUT(vb->m.userptr);
3252         else
3253                 lvb->m.offset = vb->m.offset;
3254         lvb->length = vb->length;
3255         lvb->input = vb->input;
3256         lvb->reserved = vb->reserved;
3257         return (0);
3258 }
3259
3260 static int
3261 linux_to_bsd_v4l2_format(struct l_v4l2_format *lvf, struct v4l2_format *vf)
3262 {
3263         vf->type = lvf->type;
3264         if (lvf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY
3265 #ifdef V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
3266             || lvf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
3267 #endif
3268             )
3269                 /*
3270                  * XXX TODO - needs 32 -> 64 bit conversion:
3271                  * (unused by webcams?)
3272                  */
3273                 return (EINVAL);
3274         memcpy(&vf->fmt, &lvf->fmt, sizeof(vf->fmt));
3275         return (0);
3276 }
3277
3278 static int
3279 bsd_to_linux_v4l2_format(struct v4l2_format *vf, struct l_v4l2_format *lvf)
3280 {
3281         lvf->type = vf->type;
3282         if (vf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY
3283 #ifdef V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
3284             || vf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
3285 #endif
3286             )
3287                 /*
3288                  * XXX TODO - needs 32 -> 64 bit conversion:
3289                  * (unused by webcams?)
3290                  */
3291                 return (EINVAL);
3292         memcpy(&lvf->fmt, &vf->fmt, sizeof(vf->fmt));
3293         return (0);
3294 }
3295 static int
3296 linux_ioctl_v4l2(struct thread *td, struct linux_ioctl_args *args)
3297 {
3298         struct file *fp;
3299         int error;
3300         struct v4l2_format vformat;
3301         struct l_v4l2_format l_vformat;
3302         struct v4l2_standard vstd;
3303         struct l_v4l2_standard l_vstd;
3304         struct l_v4l2_buffer l_vbuf;
3305         struct v4l2_buffer vbuf;
3306         struct v4l2_input vinp;
3307
3308         switch (args->cmd & 0xffff) {
3309         case LINUX_VIDIOC_RESERVED:
3310         case LINUX_VIDIOC_LOG_STATUS:
3311                 if ((args->cmd & IOC_DIRMASK) != LINUX_IOC_VOID)
3312                         return (ENOIOCTL);
3313                 args->cmd = (args->cmd & 0xffff) | IOC_VOID;
3314                 break;
3315
3316         case LINUX_VIDIOC_OVERLAY:
3317         case LINUX_VIDIOC_STREAMON:
3318         case LINUX_VIDIOC_STREAMOFF:
3319         case LINUX_VIDIOC_S_STD:
3320         case LINUX_VIDIOC_S_TUNER:
3321         case LINUX_VIDIOC_S_AUDIO:
3322         case LINUX_VIDIOC_S_AUDOUT:
3323         case LINUX_VIDIOC_S_MODULATOR:
3324         case LINUX_VIDIOC_S_FREQUENCY:
3325         case LINUX_VIDIOC_S_CROP:
3326         case LINUX_VIDIOC_S_JPEGCOMP:
3327         case LINUX_VIDIOC_S_PRIORITY:
3328         case LINUX_VIDIOC_DBG_S_REGISTER:
3329         case LINUX_VIDIOC_S_HW_FREQ_SEEK:
3330         case LINUX_VIDIOC_SUBSCRIBE_EVENT:
3331         case LINUX_VIDIOC_UNSUBSCRIBE_EVENT:
3332                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_IN;
3333                 break;
3334
3335         case LINUX_VIDIOC_QUERYCAP:
3336         case LINUX_VIDIOC_G_STD:
3337         case LINUX_VIDIOC_G_AUDIO:
3338         case LINUX_VIDIOC_G_INPUT:
3339         case LINUX_VIDIOC_G_OUTPUT:
3340         case LINUX_VIDIOC_G_AUDOUT:
3341         case LINUX_VIDIOC_G_JPEGCOMP:
3342         case LINUX_VIDIOC_QUERYSTD:
3343         case LINUX_VIDIOC_G_PRIORITY:
3344         case LINUX_VIDIOC_QUERY_DV_PRESET:
3345                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_OUT;
3346                 break;
3347
3348         case LINUX_VIDIOC_ENUM_FMT:
3349         case LINUX_VIDIOC_REQBUFS:
3350         case LINUX_VIDIOC_G_PARM:
3351         case LINUX_VIDIOC_S_PARM:
3352         case LINUX_VIDIOC_G_CTRL:
3353         case LINUX_VIDIOC_S_CTRL:
3354         case LINUX_VIDIOC_G_TUNER:
3355         case LINUX_VIDIOC_QUERYCTRL:
3356         case LINUX_VIDIOC_QUERYMENU:
3357         case LINUX_VIDIOC_S_INPUT:
3358         case LINUX_VIDIOC_S_OUTPUT:
3359         case LINUX_VIDIOC_ENUMOUTPUT:
3360         case LINUX_VIDIOC_G_MODULATOR:
3361         case LINUX_VIDIOC_G_FREQUENCY:
3362         case LINUX_VIDIOC_CROPCAP:
3363         case LINUX_VIDIOC_G_CROP:
3364         case LINUX_VIDIOC_ENUMAUDIO:
3365         case LINUX_VIDIOC_ENUMAUDOUT:
3366         case LINUX_VIDIOC_G_SLICED_VBI_CAP:
3367 #ifdef VIDIOC_ENUM_FRAMESIZES
3368         case LINUX_VIDIOC_ENUM_FRAMESIZES:
3369         case LINUX_VIDIOC_ENUM_FRAMEINTERVALS:
3370         case LINUX_VIDIOC_ENCODER_CMD:
3371         case LINUX_VIDIOC_TRY_ENCODER_CMD:
3372 #endif
3373         case LINUX_VIDIOC_DBG_G_REGISTER:
3374         case LINUX_VIDIOC_DBG_G_CHIP_IDENT:
3375         case LINUX_VIDIOC_ENUM_DV_PRESETS:
3376         case LINUX_VIDIOC_S_DV_PRESET:
3377         case LINUX_VIDIOC_G_DV_PRESET:
3378         case LINUX_VIDIOC_S_DV_TIMINGS:
3379         case LINUX_VIDIOC_G_DV_TIMINGS:
3380                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_INOUT;
3381                 break;
3382
3383         case LINUX_VIDIOC_G_FMT:
3384         case LINUX_VIDIOC_S_FMT:
3385         case LINUX_VIDIOC_TRY_FMT:
3386                 error = copyin((void *)args->arg, &l_vformat, sizeof(l_vformat));
3387                 if (error)
3388                         return (error);
3389                 error = fget(td, args->fd,
3390                     &cap_ioctl_rights, &fp);
3391                 if (error)
3392                         return (error);
3393                 if (linux_to_bsd_v4l2_format(&l_vformat, &vformat) != 0)
3394                         error = EINVAL;
3395                 else if ((args->cmd & 0xffff) == LINUX_VIDIOC_G_FMT)
3396                         error = fo_ioctl(fp, VIDIOC_G_FMT, &vformat,
3397                             td->td_ucred, td);
3398                 else if ((args->cmd & 0xffff) == LINUX_VIDIOC_S_FMT)
3399                         error = fo_ioctl(fp, VIDIOC_S_FMT, &vformat,
3400                             td->td_ucred, td);
3401                 else
3402                         error = fo_ioctl(fp, VIDIOC_TRY_FMT, &vformat,
3403                             td->td_ucred, td);
3404                 bsd_to_linux_v4l2_format(&vformat, &l_vformat);
3405                 copyout(&l_vformat, (void *)args->arg, sizeof(l_vformat));
3406                 fdrop(fp, td);
3407                 return (error);
3408
3409         case LINUX_VIDIOC_ENUMSTD:
3410                 error = copyin((void *)args->arg, &l_vstd, sizeof(l_vstd));
3411                 if (error)
3412                         return (error);
3413                 linux_to_bsd_v4l2_standard(&l_vstd, &vstd);
3414                 error = fget(td, args->fd,
3415                     &cap_ioctl_rights, &fp);
3416                 if (error)
3417                         return (error);
3418                 error = fo_ioctl(fp, VIDIOC_ENUMSTD, (caddr_t)&vstd,
3419                     td->td_ucred, td);
3420                 if (error) {
3421                         fdrop(fp, td);
3422                         return (error);
3423                 }
3424                 bsd_to_linux_v4l2_standard(&vstd, &l_vstd);
3425                 error = copyout(&l_vstd, (void *)args->arg, sizeof(l_vstd));
3426                 fdrop(fp, td);
3427                 return (error);
3428
3429         case LINUX_VIDIOC_ENUMINPUT:
3430                 /*
3431                  * The Linux struct l_v4l2_input differs only in size,
3432                  * it has no padding at the end.
3433                  */
3434                 error = copyin((void *)args->arg, &vinp,
3435                                 sizeof(struct l_v4l2_input));
3436                 if (error != 0)
3437                         return (error);
3438                 error = fget(td, args->fd,
3439                     &cap_ioctl_rights, &fp);
3440                 if (error != 0)
3441                         return (error);
3442                 error = fo_ioctl(fp, VIDIOC_ENUMINPUT, (caddr_t)&vinp,
3443                     td->td_ucred, td);
3444                 if (error) {
3445                         fdrop(fp, td);
3446                         return (error);
3447                 }
3448                 error = copyout(&vinp, (void *)args->arg,
3449                                 sizeof(struct l_v4l2_input));
3450                 fdrop(fp, td);
3451                 return (error);
3452
3453         case LINUX_VIDIOC_QUERYBUF:
3454         case LINUX_VIDIOC_QBUF:
3455         case LINUX_VIDIOC_DQBUF:
3456                 error = copyin((void *)args->arg, &l_vbuf, sizeof(l_vbuf));
3457                 if (error)
3458                         return (error);
3459                 error = fget(td, args->fd,
3460                     &cap_ioctl_rights, &fp);
3461                 if (error)
3462                         return (error);
3463                 linux_to_bsd_v4l2_buffer(&l_vbuf, &vbuf);
3464                 if ((args->cmd & 0xffff) == LINUX_VIDIOC_QUERYBUF)
3465                         error = fo_ioctl(fp, VIDIOC_QUERYBUF, &vbuf,
3466                             td->td_ucred, td);
3467                 else if ((args->cmd & 0xffff) == LINUX_VIDIOC_QBUF)
3468                         error = fo_ioctl(fp, VIDIOC_QBUF, &vbuf,
3469                             td->td_ucred, td);
3470                 else
3471                         error = fo_ioctl(fp, VIDIOC_DQBUF, &vbuf,
3472                             td->td_ucred, td);
3473                 bsd_to_linux_v4l2_buffer(&vbuf, &l_vbuf);
3474                 copyout(&l_vbuf, (void *)args->arg, sizeof(l_vbuf));
3475                 fdrop(fp, td);
3476                 return (error);
3477
3478         /*
3479          * XXX TODO - these need 32 -> 64 bit conversion:
3480          * (are any of them needed for webcams?)
3481          */
3482         case LINUX_VIDIOC_G_FBUF:
3483         case LINUX_VIDIOC_S_FBUF:
3484
3485         case LINUX_VIDIOC_G_EXT_CTRLS:
3486         case LINUX_VIDIOC_S_EXT_CTRLS:
3487         case LINUX_VIDIOC_TRY_EXT_CTRLS:
3488
3489         case LINUX_VIDIOC_DQEVENT:
3490
3491         default:                        return (ENOIOCTL);
3492         }
3493
3494         error = sys_ioctl(td, (struct ioctl_args *)args);
3495         return (error);
3496 }
3497
3498 /*
3499  * Support for emulators/linux-libusb. This port uses FBSD_LUSB* macros
3500  * instead of USB* ones. This lets us to provide correct values for cmd.
3501  * 0xffffffe0 -- 0xffffffff range seemed to be the least collision-prone.
3502  */
3503 static int
3504 linux_ioctl_fbsd_usb(struct thread *td, struct linux_ioctl_args *args)
3505 {
3506         int error;
3507
3508         error = 0;
3509         switch (args->cmd) {
3510         case FBSD_LUSB_DEVICEENUMERATE:
3511                 args->cmd = USB_DEVICEENUMERATE;
3512                 break;
3513         case FBSD_LUSB_DEV_QUIRK_ADD:
3514                 args->cmd = USB_DEV_QUIRK_ADD;
3515                 break;
3516         case FBSD_LUSB_DEV_QUIRK_GET:
3517                 args->cmd = USB_DEV_QUIRK_GET;
3518                 break;
3519         case FBSD_LUSB_DEV_QUIRK_REMOVE:
3520                 args->cmd = USB_DEV_QUIRK_REMOVE;
3521                 break;
3522         case FBSD_LUSB_DO_REQUEST:
3523                 args->cmd = USB_DO_REQUEST;
3524                 break;
3525         case FBSD_LUSB_FS_CLEAR_STALL_SYNC:
3526                 args->cmd = USB_FS_CLEAR_STALL_SYNC;
3527                 break;
3528         case FBSD_LUSB_FS_CLOSE:
3529                 args->cmd = USB_FS_CLOSE;
3530                 break;
3531         case FBSD_LUSB_FS_COMPLETE:
3532                 args->cmd = USB_FS_COMPLETE;
3533                 break;
3534         case FBSD_LUSB_FS_INIT:
3535                 args->cmd = USB_FS_INIT;
3536                 break;
3537         case FBSD_LUSB_FS_OPEN:
3538                 args->cmd = USB_FS_OPEN;
3539                 break;
3540         case FBSD_LUSB_FS_START:
3541                 args->cmd = USB_FS_START;
3542                 break;
3543         case FBSD_LUSB_FS_STOP:
3544                 args->cmd = USB_FS_STOP;
3545                 break;
3546         case FBSD_LUSB_FS_UNINIT:
3547                 args->cmd = USB_FS_UNINIT;
3548                 break;
3549         case FBSD_LUSB_GET_CONFIG:
3550                 args->cmd = USB_GET_CONFIG;
3551                 break;
3552         case FBSD_LUSB_GET_DEVICEINFO:
3553                 args->cmd = USB_GET_DEVICEINFO;
3554                 break;
3555         case FBSD_LUSB_GET_DEVICE_DESC:
3556                 args->cmd = USB_GET_DEVICE_DESC;
3557                 break;
3558         case FBSD_LUSB_GET_FULL_DESC:
3559                 args->cmd = USB_GET_FULL_DESC;
3560                 break;
3561         case FBSD_LUSB_GET_IFACE_DRIVER:
3562                 args->cmd = USB_GET_IFACE_DRIVER;
3563                 break;
3564         case FBSD_LUSB_GET_PLUGTIME:
3565                 args->cmd = USB_GET_PLUGTIME;
3566                 break;
3567         case FBSD_LUSB_GET_POWER_MODE:
3568                 args->cmd = USB_GET_POWER_MODE;
3569                 break;
3570         case FBSD_LUSB_GET_REPORT_DESC:
3571                 args->cmd = USB_GET_REPORT_DESC;
3572                 break;
3573         case FBSD_LUSB_GET_REPORT_ID:
3574                 args->cmd = USB_GET_REPORT_ID;
3575                 break;
3576         case FBSD_LUSB_GET_TEMPLATE:
3577                 args->cmd = USB_GET_TEMPLATE;
3578                 break;
3579         case FBSD_LUSB_IFACE_DRIVER_ACTIVE:
3580                 args->cmd = USB_IFACE_DRIVER_ACTIVE;
3581                 break;
3582         case FBSD_LUSB_IFACE_DRIVER_DETACH:
3583                 args->cmd = USB_IFACE_DRIVER_DETACH;
3584                 break;
3585         case FBSD_LUSB_QUIRK_NAME_GET:
3586                 args->cmd = USB_QUIRK_NAME_GET;
3587                 break;
3588         case FBSD_LUSB_READ_DIR:
3589                 args->cmd = USB_READ_DIR;
3590                 break;
3591         case FBSD_LUSB_SET_ALTINTERFACE:
3592                 args->cmd = USB_SET_ALTINTERFACE;
3593                 break;
3594         case FBSD_LUSB_SET_CONFIG:
3595                 args->cmd = USB_SET_CONFIG;
3596                 break;
3597         case FBSD_LUSB_SET_IMMED:
3598                 args->cmd = USB_SET_IMMED;
3599                 break;
3600         case FBSD_LUSB_SET_POWER_MODE:
3601                 args->cmd = USB_SET_POWER_MODE;
3602                 break;
3603         case FBSD_LUSB_SET_TEMPLATE:
3604                 args->cmd = USB_SET_TEMPLATE;
3605                 break;
3606         case FBSD_LUSB_FS_OPEN_STREAM:
3607                 args->cmd = USB_FS_OPEN_STREAM;
3608                 break;
3609         case FBSD_LUSB_GET_DEV_PORT_PATH:
3610                 args->cmd = USB_GET_DEV_PORT_PATH;
3611                 break;
3612         case FBSD_LUSB_GET_POWER_USAGE:
3613                 args->cmd = USB_GET_POWER_USAGE;
3614                 break;
3615         default:
3616                 error = ENOIOCTL;
3617         }
3618         if (error != ENOIOCTL)
3619                 error = sys_ioctl(td, (struct ioctl_args *)args);
3620         return (error);
3621 }
3622
3623 /*
3624  * Some evdev ioctls must be translated.
3625  *  - EVIOCGMTSLOTS is a IOC_READ ioctl on Linux although it has input data
3626  *    (must be IOC_INOUT on FreeBSD).
3627  *  - On Linux, EVIOCGRAB, EVIOCREVOKE and EVIOCRMFF are defined as _IOW with
3628  *    an int argument. You don't pass an int pointer to the ioctl(), however,
3629  *    but just the int directly. On FreeBSD, they are defined as _IOWINT for
3630  *    this to work.
3631  */
3632 static int
3633 linux_ioctl_evdev(struct thread *td, struct linux_ioctl_args *args)
3634 {
3635         struct file *fp;
3636         clockid_t clock;
3637         int error;
3638
3639         args->cmd = SETDIR(args->cmd);
3640
3641         switch (args->cmd) {
3642         case (EVIOCGRAB & ~IOC_DIRMASK) | IOC_IN:
3643                 args->cmd = EVIOCGRAB;
3644                 break;
3645         case (EVIOCREVOKE & ~IOC_DIRMASK) | IOC_IN:
3646                 args->cmd = EVIOCREVOKE;
3647                 break;
3648         case (EVIOCRMFF & ~IOC_DIRMASK) | IOC_IN:
3649                 args->cmd = EVIOCRMFF;
3650                 break;
3651         case EVIOCSCLOCKID: {
3652                 error = copyin(PTRIN(args->arg), &clock, sizeof(clock));
3653                 if (error != 0)
3654                         return (error);
3655                 if (clock & ~(LINUX_IOCTL_EVDEV_CLK))
3656                         return (EINVAL);
3657                 error = linux_to_native_clockid(&clock, clock);
3658                 if (error != 0)
3659                         return (error);
3660
3661                 error = fget(td, args->fd,
3662                     &cap_ioctl_rights, &fp);
3663                 if (error != 0)
3664                         return (error);
3665
3666                 error = fo_ioctl(fp, EVIOCSCLOCKID, &clock, td->td_ucred, td);
3667                 fdrop(fp, td);
3668                 return (error);
3669         }
3670         default:
3671                 break;
3672         }
3673
3674         if (IOCBASECMD(args->cmd) ==
3675             ((EVIOCGMTSLOTS(0) & ~IOC_DIRMASK) | IOC_OUT))
3676                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_INOUT;
3677
3678         return (sys_ioctl(td, (struct ioctl_args *)args));
3679 }
3680
3681 /*
3682  * main ioctl syscall function
3683  */
3684
3685 int
3686 linux_ioctl(struct thread *td, struct linux_ioctl_args *args)
3687 {
3688         struct file *fp;
3689         struct linux_ioctl_handler_element *he;
3690         int error, cmd;
3691
3692 #ifdef DEBUG
3693         if (ldebug(ioctl))
3694                 printf(ARGS(ioctl, "%d, %04lx, *"), args->fd,
3695                     (unsigned long)args->cmd);
3696 #endif
3697
3698         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
3699         if (error != 0)
3700                 return (error);
3701         if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
3702                 fdrop(fp, td);
3703                 return (EBADF);
3704         }
3705
3706         /* Iterate over the ioctl handlers */
3707         cmd = args->cmd & 0xffff;
3708         sx_slock(&linux_ioctl_sx);
3709         mtx_lock(&Giant);
3710 #ifdef COMPAT_LINUX32
3711         TAILQ_FOREACH(he, &linux32_ioctl_handlers, list) {
3712                 if (cmd >= he->low && cmd <= he->high) {
3713                         error = (*he->func)(td, args);
3714                         if (error != ENOIOCTL) {
3715                                 mtx_unlock(&Giant);
3716                                 sx_sunlock(&linux_ioctl_sx);
3717                                 fdrop(fp, td);
3718                                 return (error);
3719                         }
3720                 }
3721         }
3722 #endif
3723         TAILQ_FOREACH(he, &linux_ioctl_handlers, list) {
3724                 if (cmd >= he->low && cmd <= he->high) {
3725                         error = (*he->func)(td, args);
3726                         if (error != ENOIOCTL) {
3727                                 mtx_unlock(&Giant);
3728                                 sx_sunlock(&linux_ioctl_sx);
3729                                 fdrop(fp, td);
3730                                 return (error);
3731                         }
3732                 }
3733         }
3734         mtx_unlock(&Giant);
3735         sx_sunlock(&linux_ioctl_sx);
3736         fdrop(fp, td);
3737
3738         switch (args->cmd & 0xffff) {
3739         case LINUX_BTRFS_IOC_CLONE:
3740                 return (ENOTSUP);
3741
3742         default:
3743                 linux_msg(td, "ioctl fd=%d, cmd=0x%x ('%c',%d) is not implemented",
3744                     args->fd, (int)(args->cmd & 0xffff),
3745                     (int)(args->cmd & 0xff00) >> 8, (int)(args->cmd & 0xff));
3746                 break;
3747         }
3748
3749         return (EINVAL);
3750 }
3751
3752 int
3753 linux_ioctl_register_handler(struct linux_ioctl_handler *h)
3754 {
3755         struct linux_ioctl_handler_element *he, *cur;
3756
3757         if (h == NULL || h->func == NULL)
3758                 return (EINVAL);
3759
3760         /*
3761          * Reuse the element if the handler is already on the list, otherwise
3762          * create a new element.
3763          */
3764         sx_xlock(&linux_ioctl_sx);
3765         TAILQ_FOREACH(he, &linux_ioctl_handlers, list) {
3766                 if (he->func == h->func)
3767                         break;
3768         }
3769         if (he == NULL) {
3770                 he = malloc(sizeof(*he),
3771                     M_LINUX, M_WAITOK);
3772                 he->func = h->func;
3773         } else
3774                 TAILQ_REMOVE(&linux_ioctl_handlers, he, list);
3775
3776         /* Initialize range information. */
3777         he->low = h->low;
3778         he->high = h->high;
3779         he->span = h->high - h->low + 1;
3780
3781         /* Add the element to the list, sorted on span. */
3782         TAILQ_FOREACH(cur, &linux_ioctl_handlers, list) {
3783                 if (cur->span > he->span) {
3784                         TAILQ_INSERT_BEFORE(cur, he, list);
3785                         sx_xunlock(&linux_ioctl_sx);
3786                         return (0);
3787                 }
3788         }
3789         TAILQ_INSERT_TAIL(&linux_ioctl_handlers, he, list);
3790         sx_xunlock(&linux_ioctl_sx);
3791
3792         return (0);
3793 }
3794
3795 int
3796 linux_ioctl_unregister_handler(struct linux_ioctl_handler *h)
3797 {
3798         struct linux_ioctl_handler_element *he;
3799
3800         if (h == NULL || h->func == NULL)
3801                 return (EINVAL);
3802
3803         sx_xlock(&linux_ioctl_sx);
3804         TAILQ_FOREACH(he, &linux_ioctl_handlers, list) {
3805                 if (he->func == h->func) {
3806                         TAILQ_REMOVE(&linux_ioctl_handlers, he, list);
3807                         sx_xunlock(&linux_ioctl_sx);
3808                         free(he, M_LINUX);
3809                         return (0);
3810                 }
3811         }
3812         sx_xunlock(&linux_ioctl_sx);
3813
3814         return (EINVAL);
3815 }
3816
3817 #ifdef COMPAT_LINUX32
3818 int
3819 linux32_ioctl_register_handler(struct linux_ioctl_handler *h)
3820 {
3821         struct linux_ioctl_handler_element *he, *cur;
3822
3823         if (h == NULL || h->func == NULL)
3824                 return (EINVAL);
3825
3826         /*
3827          * Reuse the element if the handler is already on the list, otherwise
3828          * create a new element.
3829          */
3830         sx_xlock(&linux_ioctl_sx);
3831         TAILQ_FOREACH(he, &linux32_ioctl_handlers, list) {
3832                 if (he->func == h->func)
3833                         break;
3834         }
3835         if (he == NULL) {
3836                 he = malloc(sizeof(*he), M_LINUX, M_WAITOK);
3837                 he->func = h->func;
3838         } else
3839                 TAILQ_REMOVE(&linux32_ioctl_handlers, he, list);
3840
3841         /* Initialize range information. */
3842         he->low = h->low;
3843         he->high = h->high;
3844         he->span = h->high - h->low + 1;
3845
3846         /* Add the element to the list, sorted on span. */
3847         TAILQ_FOREACH(cur, &linux32_ioctl_handlers, list) {
3848                 if (cur->span > he->span) {
3849                         TAILQ_INSERT_BEFORE(cur, he, list);
3850                         sx_xunlock(&linux_ioctl_sx);
3851                         return (0);
3852                 }
3853         }
3854         TAILQ_INSERT_TAIL(&linux32_ioctl_handlers, he, list);
3855         sx_xunlock(&linux_ioctl_sx);
3856
3857         return (0);
3858 }
3859
3860 int
3861 linux32_ioctl_unregister_handler(struct linux_ioctl_handler *h)
3862 {
3863         struct linux_ioctl_handler_element *he;
3864
3865         if (h == NULL || h->func == NULL)
3866                 return (EINVAL);
3867
3868         sx_xlock(&linux_ioctl_sx);
3869         TAILQ_FOREACH(he, &linux32_ioctl_handlers, list) {
3870                 if (he->func == h->func) {
3871                         TAILQ_REMOVE(&linux32_ioctl_handlers, he, list);
3872                         sx_xunlock(&linux_ioctl_sx);
3873                         free(he, M_LINUX);
3874                         return (0);
3875                 }
3876         }
3877         sx_xunlock(&linux_ioctl_sx);
3878
3879         return (EINVAL);
3880 }
3881 #endif