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