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