]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linux/linux_ioctl.c
Update tcsh to 6.21.00.
[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                 error = copyin((void *)args->arg, &sc, sizeof(sc));
1493                 if (error)
1494                         break;
1495
1496                 /*
1497                  * Invoke the native ioctl and bounce the returned data through
1498                  * the userspace buffer.  This works because the Linux structure
1499                  * is the same size as our structures for the subchannel header
1500                  * and position data.
1501                  */
1502                 bsdsc.address_format = CD_LBA_FORMAT;
1503                 bsdsc.data_format = CD_CURRENT_POSITION;
1504                 bsdsc.track = 0;
1505                 bsdsc.data_len = sizeof(sc);
1506                 bsdsc.data = (void *)args->arg;
1507                 error = fo_ioctl(fp, CDIOCREADSUBCHANNEL, (caddr_t)&bsdsc,
1508                     td->td_ucred, td);
1509                 if (error)
1510                         break;
1511                 error = copyin((void *)args->arg, &bsdinfo, sizeof(bsdinfo));
1512                 if (error)
1513                         break;
1514                 sc.cdsc_audiostatus = bsdinfo.header.audio_status;
1515                 sc.cdsc_adr = bsdinfo.what.position.addr_type;
1516                 sc.cdsc_ctrl = bsdinfo.what.position.control;
1517                 sc.cdsc_trk = bsdinfo.what.position.track_number;
1518                 sc.cdsc_ind = bsdinfo.what.position.index_number;
1519                 set_linux_cdrom_addr(&sc.cdsc_absaddr, sc.cdsc_format,
1520                     bsdinfo.what.position.absaddr.lba);
1521                 set_linux_cdrom_addr(&sc.cdsc_reladdr, sc.cdsc_format,
1522                     bsdinfo.what.position.reladdr.lba);
1523                 error = copyout(&sc, (void *)args->arg, sizeof(sc));
1524                 break;
1525         }
1526
1527         /* LINUX_CDROMREADMODE2 */
1528         /* LINUX_CDROMREADMODE1 */
1529         /* LINUX_CDROMREADAUDIO */
1530         /* LINUX_CDROMEJECT_SW */
1531         /* LINUX_CDROMMULTISESSION */
1532         /* LINUX_CDROM_GET_UPC */
1533
1534         case LINUX_CDROMRESET:
1535                 args->cmd = CDIOCRESET;
1536                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1537                 break;
1538
1539         /* LINUX_CDROMVOLREAD */
1540         /* LINUX_CDROMREADRAW */
1541         /* LINUX_CDROMREADCOOKED */
1542         /* LINUX_CDROMSEEK */
1543         /* LINUX_CDROMPLAYBLK */
1544         /* LINUX_CDROMREADALL */
1545         /* LINUX_CDROMCLOSETRAY */
1546         /* LINUX_CDROMLOADFROMSLOT */
1547         /* LINUX_CDROMGETSPINDOWN */
1548         /* LINUX_CDROMSETSPINDOWN */
1549         /* LINUX_CDROM_SET_OPTIONS */
1550         /* LINUX_CDROM_CLEAR_OPTIONS */
1551         /* LINUX_CDROM_SELECT_SPEED */
1552         /* LINUX_CDROM_SELECT_DISC */
1553         /* LINUX_CDROM_MEDIA_CHANGED */
1554         /* LINUX_CDROM_DRIVE_STATUS */
1555         /* LINUX_CDROM_DISC_STATUS */
1556         /* LINUX_CDROM_CHANGER_NSLOTS */
1557         /* LINUX_CDROM_LOCKDOOR */
1558         /* LINUX_CDROM_DEBUG */
1559         /* LINUX_CDROM_GET_CAPABILITY */
1560         /* LINUX_CDROMAUDIOBUFSIZ */
1561
1562         case LINUX_DVD_READ_STRUCT: {
1563                 l_dvd_struct *lds;
1564                 struct dvd_struct *bds;
1565
1566                 lds = malloc(sizeof(*lds), M_LINUX, M_WAITOK);
1567                 bds = malloc(sizeof(*bds), M_LINUX, M_WAITOK);
1568                 error = copyin((void *)args->arg, lds, sizeof(*lds));
1569                 if (error)
1570                         goto out;
1571                 error = linux_to_bsd_dvd_struct(lds, bds);
1572                 if (error)
1573                         goto out;
1574                 error = fo_ioctl(fp, DVDIOCREADSTRUCTURE, (caddr_t)bds,
1575                     td->td_ucred, td);
1576                 if (error)
1577                         goto out;
1578                 error = bsd_to_linux_dvd_struct(bds, lds);
1579                 if (error)
1580                         goto out;
1581                 error = copyout(lds, (void *)args->arg, sizeof(*lds));
1582         out:
1583                 free(bds, M_LINUX);
1584                 free(lds, M_LINUX);
1585                 break;
1586         }
1587
1588         /* LINUX_DVD_WRITE_STRUCT */
1589
1590         case LINUX_DVD_AUTH: {
1591                 l_dvd_authinfo lda;
1592                 struct dvd_authinfo bda;
1593                 int bcode;
1594
1595                 error = copyin((void *)args->arg, &lda, sizeof(lda));
1596                 if (error)
1597                         break;
1598                 error = linux_to_bsd_dvd_authinfo(&lda, &bcode, &bda);
1599                 if (error)
1600                         break;
1601                 error = fo_ioctl(fp, bcode, (caddr_t)&bda, td->td_ucred,
1602                     td);
1603                 if (error) {
1604                         if (lda.type == LINUX_DVD_HOST_SEND_KEY2) {
1605                                 lda.type = LINUX_DVD_AUTH_FAILURE;
1606                                 copyout(&lda, (void *)args->arg, sizeof(lda));
1607                         }
1608                         break;
1609                 }
1610                 error = bsd_to_linux_dvd_authinfo(&bda, &lda);
1611                 if (error)
1612                         break;
1613                 error = copyout(&lda, (void *)args->arg, sizeof(lda));
1614                 break;
1615         }
1616
1617         case LINUX_SCSI_GET_BUS_NUMBER:
1618         {
1619                 struct sg_scsi_id id;
1620
1621                 error = fo_ioctl(fp, SG_GET_SCSI_ID, (caddr_t)&id,
1622                     td->td_ucred, td);
1623                 if (error)
1624                         break;
1625                 error = copyout(&id.channel, (void *)args->arg, sizeof(int));
1626                 break;
1627         }
1628
1629         case LINUX_SCSI_GET_IDLUN:
1630         {
1631                 struct sg_scsi_id id;
1632                 struct scsi_idlun idl;
1633
1634                 error = fo_ioctl(fp, SG_GET_SCSI_ID, (caddr_t)&id,
1635                     td->td_ucred, td);
1636                 if (error)
1637                         break;
1638                 idl.dev_id = (id.scsi_id & 0xff) + ((id.lun & 0xff) << 8) +
1639                     ((id.channel & 0xff) << 16) + ((id.host_no & 0xff) << 24);
1640                 idl.host_unique_id = id.host_no;
1641                 error = copyout(&idl, (void *)args->arg, sizeof(idl));
1642                 break;
1643         }
1644
1645         /* LINUX_CDROM_SEND_PACKET */
1646         /* LINUX_CDROM_NEXT_WRITABLE */
1647         /* LINUX_CDROM_LAST_WRITTEN */
1648
1649         default:
1650                 error = ENOIOCTL;
1651                 break;
1652         }
1653
1654         fdrop(fp, td);
1655         return (error);
1656 }
1657
1658 static int
1659 linux_ioctl_vfat(struct thread *td, struct linux_ioctl_args *args)
1660 {
1661
1662         return (ENOTTY);
1663 }
1664
1665 /*
1666  * Sound related ioctls
1667  */
1668
1669 struct linux_old_mixer_info {
1670         char    id[16];
1671         char    name[32];
1672 };
1673
1674 static u_int32_t dirbits[4] = { IOC_VOID, IOC_IN, IOC_OUT, IOC_INOUT };
1675
1676 #define SETDIR(c)       (((c) & ~IOC_DIRMASK) | dirbits[args->cmd >> 30])
1677
1678 static int
1679 linux_ioctl_sound(struct thread *td, struct linux_ioctl_args *args)
1680 {
1681
1682         switch (args->cmd & 0xffff) {
1683
1684         case LINUX_SOUND_MIXER_WRITE_VOLUME:
1685                 args->cmd = SETDIR(SOUND_MIXER_WRITE_VOLUME);
1686                 return (sys_ioctl(td, (struct ioctl_args *)args));
1687
1688         case LINUX_SOUND_MIXER_WRITE_BASS:
1689                 args->cmd = SETDIR(SOUND_MIXER_WRITE_BASS);
1690                 return (sys_ioctl(td, (struct ioctl_args *)args));
1691
1692         case LINUX_SOUND_MIXER_WRITE_TREBLE:
1693                 args->cmd = SETDIR(SOUND_MIXER_WRITE_TREBLE);
1694                 return (sys_ioctl(td, (struct ioctl_args *)args));
1695
1696         case LINUX_SOUND_MIXER_WRITE_SYNTH:
1697                 args->cmd = SETDIR(SOUND_MIXER_WRITE_SYNTH);
1698                 return (sys_ioctl(td, (struct ioctl_args *)args));
1699
1700         case LINUX_SOUND_MIXER_WRITE_PCM:
1701                 args->cmd = SETDIR(SOUND_MIXER_WRITE_PCM);
1702                 return (sys_ioctl(td, (struct ioctl_args *)args));
1703
1704         case LINUX_SOUND_MIXER_WRITE_SPEAKER:
1705                 args->cmd = SETDIR(SOUND_MIXER_WRITE_SPEAKER);
1706                 return (sys_ioctl(td, (struct ioctl_args *)args));
1707
1708         case LINUX_SOUND_MIXER_WRITE_LINE:
1709                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE);
1710                 return (sys_ioctl(td, (struct ioctl_args *)args));
1711
1712         case LINUX_SOUND_MIXER_WRITE_MIC:
1713                 args->cmd = SETDIR(SOUND_MIXER_WRITE_MIC);
1714                 return (sys_ioctl(td, (struct ioctl_args *)args));
1715
1716         case LINUX_SOUND_MIXER_WRITE_CD:
1717                 args->cmd = SETDIR(SOUND_MIXER_WRITE_CD);
1718                 return (sys_ioctl(td, (struct ioctl_args *)args));
1719
1720         case LINUX_SOUND_MIXER_WRITE_IMIX:
1721                 args->cmd = SETDIR(SOUND_MIXER_WRITE_IMIX);
1722                 return (sys_ioctl(td, (struct ioctl_args *)args));
1723
1724         case LINUX_SOUND_MIXER_WRITE_ALTPCM:
1725                 args->cmd = SETDIR(SOUND_MIXER_WRITE_ALTPCM);
1726                 return (sys_ioctl(td, (struct ioctl_args *)args));
1727
1728         case LINUX_SOUND_MIXER_WRITE_RECLEV:
1729                 args->cmd = SETDIR(SOUND_MIXER_WRITE_RECLEV);
1730                 return (sys_ioctl(td, (struct ioctl_args *)args));
1731
1732         case LINUX_SOUND_MIXER_WRITE_IGAIN:
1733                 args->cmd = SETDIR(SOUND_MIXER_WRITE_IGAIN);
1734                 return (sys_ioctl(td, (struct ioctl_args *)args));
1735
1736         case LINUX_SOUND_MIXER_WRITE_OGAIN:
1737                 args->cmd = SETDIR(SOUND_MIXER_WRITE_OGAIN);
1738                 return (sys_ioctl(td, (struct ioctl_args *)args));
1739
1740         case LINUX_SOUND_MIXER_WRITE_LINE1:
1741                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE1);
1742                 return (sys_ioctl(td, (struct ioctl_args *)args));
1743
1744         case LINUX_SOUND_MIXER_WRITE_LINE2:
1745                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE2);
1746                 return (sys_ioctl(td, (struct ioctl_args *)args));
1747
1748         case LINUX_SOUND_MIXER_WRITE_LINE3:
1749                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE3);
1750                 return (sys_ioctl(td, (struct ioctl_args *)args));
1751
1752         case LINUX_SOUND_MIXER_INFO: {
1753                 /* Key on encoded length */
1754                 switch ((args->cmd >> 16) & 0x1fff) {
1755                 case 0x005c: {  /* SOUND_MIXER_INFO */
1756                         args->cmd = SOUND_MIXER_INFO;
1757                         return (sys_ioctl(td, (struct ioctl_args *)args));
1758                 }
1759                 case 0x0030: {  /* SOUND_OLD_MIXER_INFO */
1760                         struct linux_old_mixer_info info;
1761                         bzero(&info, sizeof(info));
1762                         strncpy(info.id, "OSS", sizeof(info.id) - 1);
1763                         strncpy(info.name, "FreeBSD OSS Mixer", sizeof(info.name) - 1);
1764                         copyout(&info, (void *)args->arg, sizeof(info));
1765                         return (0);
1766                 }
1767                 default:
1768                         return (ENOIOCTL);
1769                 }
1770                 break;
1771         }
1772
1773         case LINUX_OSS_GETVERSION: {
1774                 int version = linux_get_oss_version(td);
1775                 return (copyout(&version, (void *)args->arg, sizeof(int)));
1776         }
1777
1778         case LINUX_SOUND_MIXER_READ_STEREODEVS:
1779                 args->cmd = SOUND_MIXER_READ_STEREODEVS;
1780                 return (sys_ioctl(td, (struct ioctl_args *)args));
1781
1782         case LINUX_SOUND_MIXER_READ_CAPS:
1783                 args->cmd = SOUND_MIXER_READ_CAPS;
1784                 return (sys_ioctl(td, (struct ioctl_args *)args));
1785
1786         case LINUX_SOUND_MIXER_READ_RECMASK:
1787                 args->cmd = SOUND_MIXER_READ_RECMASK;
1788                 return (sys_ioctl(td, (struct ioctl_args *)args));
1789
1790         case LINUX_SOUND_MIXER_READ_DEVMASK:
1791                 args->cmd = SOUND_MIXER_READ_DEVMASK;
1792                 return (sys_ioctl(td, (struct ioctl_args *)args));
1793
1794         case LINUX_SOUND_MIXER_WRITE_RECSRC:
1795                 args->cmd = SETDIR(SOUND_MIXER_WRITE_RECSRC);
1796                 return (sys_ioctl(td, (struct ioctl_args *)args));
1797
1798         case LINUX_SNDCTL_DSP_RESET:
1799                 args->cmd = SNDCTL_DSP_RESET;
1800                 return (sys_ioctl(td, (struct ioctl_args *)args));
1801
1802         case LINUX_SNDCTL_DSP_SYNC:
1803                 args->cmd = SNDCTL_DSP_SYNC;
1804                 return (sys_ioctl(td, (struct ioctl_args *)args));
1805
1806         case LINUX_SNDCTL_DSP_SPEED:
1807                 args->cmd = SNDCTL_DSP_SPEED;
1808                 return (sys_ioctl(td, (struct ioctl_args *)args));
1809
1810         case LINUX_SNDCTL_DSP_STEREO:
1811                 args->cmd = SNDCTL_DSP_STEREO;
1812                 return (sys_ioctl(td, (struct ioctl_args *)args));
1813
1814         case LINUX_SNDCTL_DSP_GETBLKSIZE: /* LINUX_SNDCTL_DSP_SETBLKSIZE */
1815                 args->cmd = SNDCTL_DSP_GETBLKSIZE;
1816                 return (sys_ioctl(td, (struct ioctl_args *)args));
1817
1818         case LINUX_SNDCTL_DSP_SETFMT:
1819                 args->cmd = SNDCTL_DSP_SETFMT;
1820                 return (sys_ioctl(td, (struct ioctl_args *)args));
1821
1822         case LINUX_SOUND_PCM_WRITE_CHANNELS:
1823                 args->cmd = SOUND_PCM_WRITE_CHANNELS;
1824                 return (sys_ioctl(td, (struct ioctl_args *)args));
1825
1826         case LINUX_SOUND_PCM_WRITE_FILTER:
1827                 args->cmd = SOUND_PCM_WRITE_FILTER;
1828                 return (sys_ioctl(td, (struct ioctl_args *)args));
1829
1830         case LINUX_SNDCTL_DSP_POST:
1831                 args->cmd = SNDCTL_DSP_POST;
1832                 return (sys_ioctl(td, (struct ioctl_args *)args));
1833
1834         case LINUX_SNDCTL_DSP_SUBDIVIDE:
1835                 args->cmd = SNDCTL_DSP_SUBDIVIDE;
1836                 return (sys_ioctl(td, (struct ioctl_args *)args));
1837
1838         case LINUX_SNDCTL_DSP_SETFRAGMENT:
1839                 args->cmd = SNDCTL_DSP_SETFRAGMENT;
1840                 return (sys_ioctl(td, (struct ioctl_args *)args));
1841
1842         case LINUX_SNDCTL_DSP_GETFMTS:
1843                 args->cmd = SNDCTL_DSP_GETFMTS;
1844                 return (sys_ioctl(td, (struct ioctl_args *)args));
1845
1846         case LINUX_SNDCTL_DSP_GETOSPACE:
1847                 args->cmd = SNDCTL_DSP_GETOSPACE;
1848                 return (sys_ioctl(td, (struct ioctl_args *)args));
1849
1850         case LINUX_SNDCTL_DSP_GETISPACE:
1851                 args->cmd = SNDCTL_DSP_GETISPACE;
1852                 return (sys_ioctl(td, (struct ioctl_args *)args));
1853
1854         case LINUX_SNDCTL_DSP_NONBLOCK:
1855                 args->cmd = SNDCTL_DSP_NONBLOCK;
1856                 return (sys_ioctl(td, (struct ioctl_args *)args));
1857
1858         case LINUX_SNDCTL_DSP_GETCAPS:
1859                 args->cmd = SNDCTL_DSP_GETCAPS;
1860                 return (sys_ioctl(td, (struct ioctl_args *)args));
1861
1862         case LINUX_SNDCTL_DSP_SETTRIGGER: /* LINUX_SNDCTL_GETTRIGGER */
1863                 args->cmd = SNDCTL_DSP_SETTRIGGER;
1864                 return (sys_ioctl(td, (struct ioctl_args *)args));
1865
1866         case LINUX_SNDCTL_DSP_GETIPTR:
1867                 args->cmd = SNDCTL_DSP_GETIPTR;
1868                 return (sys_ioctl(td, (struct ioctl_args *)args));
1869
1870         case LINUX_SNDCTL_DSP_GETOPTR:
1871                 args->cmd = SNDCTL_DSP_GETOPTR;
1872                 return (sys_ioctl(td, (struct ioctl_args *)args));
1873
1874         case LINUX_SNDCTL_DSP_SETDUPLEX:
1875                 args->cmd = SNDCTL_DSP_SETDUPLEX;
1876                 return (sys_ioctl(td, (struct ioctl_args *)args));
1877
1878         case LINUX_SNDCTL_DSP_GETODELAY:
1879                 args->cmd = SNDCTL_DSP_GETODELAY;
1880                 return (sys_ioctl(td, (struct ioctl_args *)args));
1881
1882         case LINUX_SNDCTL_SEQ_RESET:
1883                 args->cmd = SNDCTL_SEQ_RESET;
1884                 return (sys_ioctl(td, (struct ioctl_args *)args));
1885
1886         case LINUX_SNDCTL_SEQ_SYNC:
1887                 args->cmd = SNDCTL_SEQ_SYNC;
1888                 return (sys_ioctl(td, (struct ioctl_args *)args));
1889
1890         case LINUX_SNDCTL_SYNTH_INFO:
1891                 args->cmd = SNDCTL_SYNTH_INFO;
1892                 return (sys_ioctl(td, (struct ioctl_args *)args));
1893
1894         case LINUX_SNDCTL_SEQ_CTRLRATE:
1895                 args->cmd = SNDCTL_SEQ_CTRLRATE;
1896                 return (sys_ioctl(td, (struct ioctl_args *)args));
1897
1898         case LINUX_SNDCTL_SEQ_GETOUTCOUNT:
1899                 args->cmd = SNDCTL_SEQ_GETOUTCOUNT;
1900                 return (sys_ioctl(td, (struct ioctl_args *)args));
1901
1902         case LINUX_SNDCTL_SEQ_GETINCOUNT:
1903                 args->cmd = SNDCTL_SEQ_GETINCOUNT;
1904                 return (sys_ioctl(td, (struct ioctl_args *)args));
1905
1906         case LINUX_SNDCTL_SEQ_PERCMODE:
1907                 args->cmd = SNDCTL_SEQ_PERCMODE;
1908                 return (sys_ioctl(td, (struct ioctl_args *)args));
1909
1910         case LINUX_SNDCTL_FM_LOAD_INSTR:
1911                 args->cmd = SNDCTL_FM_LOAD_INSTR;
1912                 return (sys_ioctl(td, (struct ioctl_args *)args));
1913
1914         case LINUX_SNDCTL_SEQ_TESTMIDI:
1915                 args->cmd = SNDCTL_SEQ_TESTMIDI;
1916                 return (sys_ioctl(td, (struct ioctl_args *)args));
1917
1918         case LINUX_SNDCTL_SEQ_RESETSAMPLES:
1919                 args->cmd = SNDCTL_SEQ_RESETSAMPLES;
1920                 return (sys_ioctl(td, (struct ioctl_args *)args));
1921
1922         case LINUX_SNDCTL_SEQ_NRSYNTHS:
1923                 args->cmd = SNDCTL_SEQ_NRSYNTHS;
1924                 return (sys_ioctl(td, (struct ioctl_args *)args));
1925
1926         case LINUX_SNDCTL_SEQ_NRMIDIS:
1927                 args->cmd = SNDCTL_SEQ_NRMIDIS;
1928                 return (sys_ioctl(td, (struct ioctl_args *)args));
1929
1930         case LINUX_SNDCTL_MIDI_INFO:
1931                 args->cmd = SNDCTL_MIDI_INFO;
1932                 return (sys_ioctl(td, (struct ioctl_args *)args));
1933
1934         case LINUX_SNDCTL_SEQ_TRESHOLD:
1935                 args->cmd = SNDCTL_SEQ_TRESHOLD;
1936                 return (sys_ioctl(td, (struct ioctl_args *)args));
1937
1938         case LINUX_SNDCTL_SYNTH_MEMAVL:
1939                 args->cmd = SNDCTL_SYNTH_MEMAVL;
1940                 return (sys_ioctl(td, (struct ioctl_args *)args));
1941
1942         }
1943
1944         return (ENOIOCTL);
1945 }
1946
1947 /*
1948  * Console related ioctls
1949  */
1950
1951 static int
1952 linux_ioctl_console(struct thread *td, struct linux_ioctl_args *args)
1953 {
1954         struct file *fp;
1955         int error;
1956
1957         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
1958         if (error != 0)
1959                 return (error);
1960         switch (args->cmd & 0xffff) {
1961
1962         case LINUX_KIOCSOUND:
1963                 args->cmd = KIOCSOUND;
1964                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1965                 break;
1966
1967         case LINUX_KDMKTONE:
1968                 args->cmd = KDMKTONE;
1969                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1970                 break;
1971
1972         case LINUX_KDGETLED:
1973                 args->cmd = KDGETLED;
1974                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1975                 break;
1976
1977         case LINUX_KDSETLED:
1978                 args->cmd = KDSETLED;
1979                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1980                 break;
1981
1982         case LINUX_KDSETMODE:
1983                 args->cmd = KDSETMODE;
1984                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1985                 break;
1986
1987         case LINUX_KDGETMODE:
1988                 args->cmd = KDGETMODE;
1989                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1990                 break;
1991
1992         case LINUX_KDGKBMODE:
1993                 args->cmd = KDGKBMODE;
1994                 error = (sys_ioctl(td, (struct ioctl_args *)args));
1995                 break;
1996
1997         case LINUX_KDSKBMODE: {
1998                 int kbdmode;
1999                 switch (args->arg) {
2000                 case LINUX_KBD_RAW:
2001                         kbdmode = K_RAW;
2002                         break;
2003                 case LINUX_KBD_XLATE:
2004                         kbdmode = K_XLATE;
2005                         break;
2006                 case LINUX_KBD_MEDIUMRAW:
2007                         kbdmode = K_RAW;
2008                         break;
2009                 default:
2010                         fdrop(fp, td);
2011                         return (EINVAL);
2012                 }
2013                 error = (fo_ioctl(fp, KDSKBMODE, (caddr_t)&kbdmode,
2014                     td->td_ucred, td));
2015                 break;
2016         }
2017
2018         case LINUX_VT_OPENQRY:
2019                 args->cmd = VT_OPENQRY;
2020                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2021                 break;
2022
2023         case LINUX_VT_GETMODE:
2024                 args->cmd = VT_GETMODE;
2025                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2026                 break;
2027
2028         case LINUX_VT_SETMODE: {
2029                 struct vt_mode mode;
2030                 if ((error = copyin((void *)args->arg, &mode, sizeof(mode))))
2031                         break;
2032                 if (LINUX_SIG_VALID(mode.relsig))
2033                         mode.relsig = linux_to_bsd_signal(mode.relsig);
2034                 else
2035                         mode.relsig = 0;
2036                 if (LINUX_SIG_VALID(mode.acqsig))
2037                         mode.acqsig = linux_to_bsd_signal(mode.acqsig);
2038                 else
2039                         mode.acqsig = 0;
2040                 /* XXX. Linux ignores frsig and set it to 0. */
2041                 mode.frsig = 0;
2042                 if ((error = copyout(&mode, (void *)args->arg, sizeof(mode))))
2043                         break;
2044                 args->cmd = VT_SETMODE;
2045                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2046                 break;
2047         }
2048
2049         case LINUX_VT_GETSTATE:
2050                 args->cmd = VT_GETACTIVE;
2051                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2052                 break;
2053
2054         case LINUX_VT_RELDISP:
2055                 args->cmd = VT_RELDISP;
2056                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2057                 break;
2058
2059         case LINUX_VT_ACTIVATE:
2060                 args->cmd = VT_ACTIVATE;
2061                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2062                 break;
2063
2064         case LINUX_VT_WAITACTIVE:
2065                 args->cmd = VT_WAITACTIVE;
2066                 error = (sys_ioctl(td, (struct ioctl_args *)args));
2067                 break;
2068
2069         default:
2070                 error = ENOIOCTL;
2071                 break;
2072         }
2073
2074         fdrop(fp, td);
2075         return (error);
2076 }
2077
2078 /*
2079  * Implement the SIOCGIFNAME ioctl
2080  */
2081
2082 static int
2083 linux_ioctl_ifname(struct thread *td, struct l_ifreq *uifr)
2084 {
2085         struct l_ifreq ifr;
2086         struct ifnet *ifp;
2087         int error, ethno, index;
2088
2089         error = copyin(uifr, &ifr, sizeof(ifr));
2090         if (error != 0)
2091                 return (error);
2092
2093         CURVNET_SET(TD_TO_VNET(curthread));
2094         IFNET_RLOCK();
2095         index = 1;      /* ifr.ifr_ifindex starts from 1 */
2096         ethno = 0;
2097         error = ENODEV;
2098         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2099                 if (ifr.ifr_ifindex == index) {
2100                         if (IFP_IS_ETH(ifp))
2101                                 snprintf(ifr.ifr_name, LINUX_IFNAMSIZ,
2102                                     "eth%d", ethno);
2103                         else
2104                                 strlcpy(ifr.ifr_name, ifp->if_xname,
2105                                     LINUX_IFNAMSIZ);
2106                         error = 0;
2107                         break;
2108                 }
2109                 if (IFP_IS_ETH(ifp))
2110                         ethno++;
2111                 index++;
2112         }
2113         IFNET_RUNLOCK();
2114         if (error == 0)
2115                 error = copyout(&ifr, uifr, sizeof(ifr));
2116         CURVNET_RESTORE();
2117
2118         return (error);
2119 }
2120
2121 /*
2122  * Implement the SIOCGIFCONF ioctl
2123  */
2124
2125 static int
2126 linux_ifconf(struct thread *td, struct ifconf *uifc)
2127 {
2128 #ifdef COMPAT_LINUX32
2129         struct l_ifconf ifc;
2130 #else
2131         struct ifconf ifc;
2132 #endif
2133         struct l_ifreq ifr;
2134         struct ifnet *ifp;
2135         struct ifaddr *ifa;
2136         struct sbuf *sb;
2137         int error, ethno, full = 0, valid_len, max_len;
2138
2139         error = copyin(uifc, &ifc, sizeof(ifc));
2140         if (error != 0)
2141                 return (error);
2142
2143         max_len = MAXPHYS - 1;
2144
2145         CURVNET_SET(TD_TO_VNET(td));
2146         /* handle the 'request buffer size' case */
2147         if ((l_uintptr_t)ifc.ifc_buf == PTROUT(NULL)) {
2148                 ifc.ifc_len = 0;
2149                 IFNET_RLOCK();
2150                 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2151                         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2152                                 struct sockaddr *sa = ifa->ifa_addr;
2153                                 if (sa->sa_family == AF_INET)
2154                                         ifc.ifc_len += sizeof(ifr);
2155                         }
2156                 }
2157                 IFNET_RUNLOCK();
2158                 error = copyout(&ifc, uifc, sizeof(ifc));
2159                 CURVNET_RESTORE();
2160                 return (error);
2161         }
2162
2163         if (ifc.ifc_len <= 0) {
2164                 CURVNET_RESTORE();
2165                 return (EINVAL);
2166         }
2167
2168 again:
2169         /* Keep track of eth interfaces */
2170         ethno = 0;
2171         if (ifc.ifc_len <= max_len) {
2172                 max_len = ifc.ifc_len;
2173                 full = 1;
2174         }
2175         sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2176         max_len = 0;
2177         valid_len = 0;
2178
2179         /* Return all AF_INET addresses of all interfaces */
2180         IFNET_RLOCK();
2181         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2182                 int addrs = 0;
2183
2184                 bzero(&ifr, sizeof(ifr));
2185                 if (IFP_IS_ETH(ifp))
2186                         snprintf(ifr.ifr_name, LINUX_IFNAMSIZ, "eth%d",
2187                             ethno++);
2188                 else
2189                         strlcpy(ifr.ifr_name, ifp->if_xname, LINUX_IFNAMSIZ);
2190
2191                 /* Walk the address list */
2192                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2193                         struct sockaddr *sa = ifa->ifa_addr;
2194
2195                         if (sa->sa_family == AF_INET) {
2196                                 ifr.ifr_addr.sa_family = LINUX_AF_INET;
2197                                 memcpy(ifr.ifr_addr.sa_data, sa->sa_data,
2198                                     sizeof(ifr.ifr_addr.sa_data));
2199                                 sbuf_bcat(sb, &ifr, sizeof(ifr));
2200                                 max_len += sizeof(ifr);
2201                                 addrs++;
2202                         }
2203
2204                         if (sbuf_error(sb) == 0)
2205                                 valid_len = sbuf_len(sb);
2206                 }
2207                 if (addrs == 0) {
2208                         bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2209                         sbuf_bcat(sb, &ifr, sizeof(ifr));
2210                         max_len += sizeof(ifr);
2211
2212                         if (sbuf_error(sb) == 0)
2213                                 valid_len = sbuf_len(sb);
2214                 }
2215         }
2216         IFNET_RUNLOCK();
2217
2218         if (valid_len != max_len && !full) {
2219                 sbuf_delete(sb);
2220                 goto again;
2221         }
2222
2223         ifc.ifc_len = valid_len;
2224         sbuf_finish(sb);
2225         error = copyout(sbuf_data(sb), PTRIN(ifc.ifc_buf), ifc.ifc_len);
2226         if (error == 0)
2227                 error = copyout(&ifc, uifc, sizeof(ifc));
2228         sbuf_delete(sb);
2229         CURVNET_RESTORE();
2230
2231         return (error);
2232 }
2233
2234 static int
2235 linux_gifflags(struct thread *td, struct ifnet *ifp, struct l_ifreq *ifr)
2236 {
2237         l_short flags;
2238
2239         linux_ifflags(ifp, &flags);
2240
2241         return (copyout(&flags, &ifr->ifr_flags, sizeof(flags)));
2242 }
2243
2244 static int
2245 linux_gifhwaddr(struct ifnet *ifp, struct l_ifreq *ifr)
2246 {
2247         struct l_sockaddr lsa;
2248
2249         if (linux_ifhwaddr(ifp, &lsa) != 0)
2250                 return (ENOENT);
2251
2252         return (copyout(&lsa, &ifr->ifr_hwaddr, sizeof(lsa)));
2253 }
2254
2255
2256  /*
2257 * If we fault in bsd_to_linux_ifreq() then we will fault when we call
2258 * the native ioctl().  Thus, we don't really need to check the return
2259 * value of this function.
2260 */
2261 static int
2262 bsd_to_linux_ifreq(struct ifreq *arg)
2263 {
2264         struct ifreq ifr;
2265         size_t ifr_len = sizeof(struct ifreq);
2266         int error;
2267
2268         if ((error = copyin(arg, &ifr, ifr_len)))
2269                 return (error);
2270
2271         *(u_short *)&ifr.ifr_addr = ifr.ifr_addr.sa_family;
2272
2273         error = copyout(&ifr, arg, ifr_len);
2274
2275         return (error);
2276 }
2277
2278 /*
2279  * Socket related ioctls
2280  */
2281
2282 static int
2283 linux_ioctl_socket(struct thread *td, struct linux_ioctl_args *args)
2284 {
2285         char lifname[LINUX_IFNAMSIZ], ifname[IFNAMSIZ];
2286         struct ifnet *ifp;
2287         struct file *fp;
2288         int error, type;
2289
2290         ifp = NULL;
2291         error = 0;
2292
2293         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
2294         if (error != 0)
2295                 return (error);
2296         type = fp->f_type;
2297         fdrop(fp, td);
2298         if (type != DTYPE_SOCKET) {
2299                 /* not a socket - probably a tap / vmnet device */
2300                 switch (args->cmd) {
2301                 case LINUX_SIOCGIFADDR:
2302                 case LINUX_SIOCSIFADDR:
2303                 case LINUX_SIOCGIFFLAGS:
2304                         return (linux_ioctl_special(td, args));
2305                 default:
2306                         return (ENOIOCTL);
2307                 }
2308         }
2309
2310         switch (args->cmd & 0xffff) {
2311
2312         case LINUX_FIOGETOWN:
2313         case LINUX_FIOSETOWN:
2314         case LINUX_SIOCADDMULTI:
2315         case LINUX_SIOCATMARK:
2316         case LINUX_SIOCDELMULTI:
2317         case LINUX_SIOCGIFNAME:
2318         case LINUX_SIOCGIFCONF:
2319         case LINUX_SIOCGPGRP:
2320         case LINUX_SIOCSPGRP:
2321         case LINUX_SIOCGIFCOUNT:
2322                 /* these ioctls don't take an interface name */
2323                 break;
2324
2325         case LINUX_SIOCGIFFLAGS:
2326         case LINUX_SIOCGIFADDR:
2327         case LINUX_SIOCSIFADDR:
2328         case LINUX_SIOCGIFDSTADDR:
2329         case LINUX_SIOCGIFBRDADDR:
2330         case LINUX_SIOCGIFNETMASK:
2331         case LINUX_SIOCSIFNETMASK:
2332         case LINUX_SIOCGIFMTU:
2333         case LINUX_SIOCSIFMTU:
2334         case LINUX_SIOCSIFNAME:
2335         case LINUX_SIOCGIFHWADDR:
2336         case LINUX_SIOCSIFHWADDR:
2337         case LINUX_SIOCDEVPRIVATE:
2338         case LINUX_SIOCDEVPRIVATE+1:
2339         case LINUX_SIOCGIFINDEX:
2340                 /* copy in the interface name and translate it. */
2341                 error = copyin((void *)args->arg, lifname, LINUX_IFNAMSIZ);
2342                 if (error != 0)
2343                         return (error);
2344                 memset(ifname, 0, sizeof(ifname));
2345                 ifp = ifname_linux_to_bsd(td, lifname, ifname);
2346                 if (ifp == NULL)
2347                         return (EINVAL);
2348                 /*
2349                  * We need to copy it back out in case we pass the
2350                  * request on to our native ioctl(), which will expect
2351                  * the ifreq to be in user space and have the correct
2352                  * interface name.
2353                  */
2354                 error = copyout(ifname, (void *)args->arg, IFNAMSIZ);
2355                 if (error != 0)
2356                         return (error);
2357                 break;
2358
2359         default:
2360                 return (ENOIOCTL);
2361         }
2362
2363         switch (args->cmd & 0xffff) {
2364
2365         case LINUX_FIOSETOWN:
2366                 args->cmd = FIOSETOWN;
2367                 error = sys_ioctl(td, (struct ioctl_args *)args);
2368                 break;
2369
2370         case LINUX_SIOCSPGRP:
2371                 args->cmd = SIOCSPGRP;
2372                 error = sys_ioctl(td, (struct ioctl_args *)args);
2373                 break;
2374
2375         case LINUX_FIOGETOWN:
2376                 args->cmd = FIOGETOWN;
2377                 error = sys_ioctl(td, (struct ioctl_args *)args);
2378                 break;
2379
2380         case LINUX_SIOCGPGRP:
2381                 args->cmd = SIOCGPGRP;
2382                 error = sys_ioctl(td, (struct ioctl_args *)args);
2383                 break;
2384
2385         case LINUX_SIOCATMARK:
2386                 args->cmd = SIOCATMARK;
2387                 error = sys_ioctl(td, (struct ioctl_args *)args);
2388                 break;
2389
2390         /* LINUX_SIOCGSTAMP */
2391
2392         case LINUX_SIOCGIFNAME:
2393                 error = linux_ioctl_ifname(td, (struct l_ifreq *)args->arg);
2394                 break;
2395
2396         case LINUX_SIOCGIFCONF:
2397                 error = linux_ifconf(td, (struct ifconf *)args->arg);
2398                 break;
2399
2400         case LINUX_SIOCGIFFLAGS:
2401                 args->cmd = SIOCGIFFLAGS;
2402                 error = linux_gifflags(td, ifp, (struct l_ifreq *)args->arg);
2403                 break;
2404
2405         case LINUX_SIOCGIFADDR:
2406                 args->cmd = SIOCGIFADDR;
2407                 error = sys_ioctl(td, (struct ioctl_args *)args);
2408                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
2409                 break;
2410
2411         case LINUX_SIOCSIFADDR:
2412                 /* XXX probably doesn't work, included for completeness */
2413                 args->cmd = SIOCSIFADDR;
2414                 error = sys_ioctl(td, (struct ioctl_args *)args);
2415                 break;
2416
2417         case LINUX_SIOCGIFDSTADDR:
2418                 args->cmd = SIOCGIFDSTADDR;
2419                 error = sys_ioctl(td, (struct ioctl_args *)args);
2420                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
2421                 break;
2422
2423         case LINUX_SIOCGIFBRDADDR:
2424                 args->cmd = SIOCGIFBRDADDR;
2425                 error = sys_ioctl(td, (struct ioctl_args *)args);
2426                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
2427                 break;
2428
2429         case LINUX_SIOCGIFNETMASK:
2430                 args->cmd = SIOCGIFNETMASK;
2431                 error = sys_ioctl(td, (struct ioctl_args *)args);
2432                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
2433                 break;
2434
2435         case LINUX_SIOCSIFNETMASK:
2436                 error = ENOIOCTL;
2437                 break;
2438
2439         case LINUX_SIOCGIFMTU:
2440                 args->cmd = SIOCGIFMTU;
2441                 error = sys_ioctl(td, (struct ioctl_args *)args);
2442                 break;
2443
2444         case LINUX_SIOCSIFMTU:
2445                 args->cmd = SIOCSIFMTU;
2446                 error = sys_ioctl(td, (struct ioctl_args *)args);
2447                 break;
2448
2449         case LINUX_SIOCSIFNAME:
2450                 error = ENOIOCTL;
2451                 break;
2452
2453         case LINUX_SIOCGIFHWADDR:
2454                 error = linux_gifhwaddr(ifp, (struct l_ifreq *)args->arg);
2455                 break;
2456
2457         case LINUX_SIOCSIFHWADDR:
2458                 error = ENOIOCTL;
2459                 break;
2460
2461         case LINUX_SIOCADDMULTI:
2462                 args->cmd = SIOCADDMULTI;
2463                 error = sys_ioctl(td, (struct ioctl_args *)args);
2464                 break;
2465
2466         case LINUX_SIOCDELMULTI:
2467                 args->cmd = SIOCDELMULTI;
2468                 error = sys_ioctl(td, (struct ioctl_args *)args);
2469                 break;
2470
2471         case LINUX_SIOCGIFINDEX:
2472                 args->cmd = SIOCGIFINDEX;
2473                 error = sys_ioctl(td, (struct ioctl_args *)args);
2474                 break;
2475
2476         case LINUX_SIOCGIFCOUNT:
2477                 error = 0;
2478                 break;
2479
2480         /*
2481          * XXX This is slightly bogus, but these ioctls are currently
2482          * XXX only used by the aironet (if_an) network driver.
2483          */
2484         case LINUX_SIOCDEVPRIVATE:
2485                 args->cmd = SIOCGPRIVATE_0;
2486                 error = sys_ioctl(td, (struct ioctl_args *)args);
2487                 break;
2488
2489         case LINUX_SIOCDEVPRIVATE+1:
2490                 args->cmd = SIOCGPRIVATE_1;
2491                 error = sys_ioctl(td, (struct ioctl_args *)args);
2492                 break;
2493         }
2494
2495         if (ifp != NULL)
2496                 /* restore the original interface name */
2497                 copyout(lifname, (void *)args->arg, LINUX_IFNAMSIZ);
2498
2499         return (error);
2500 }
2501
2502 /*
2503  * Device private ioctl handler
2504  */
2505 static int
2506 linux_ioctl_private(struct thread *td, struct linux_ioctl_args *args)
2507 {
2508         struct file *fp;
2509         int error, type;
2510
2511         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
2512         if (error != 0)
2513                 return (error);
2514         type = fp->f_type;
2515         fdrop(fp, td);
2516         if (type == DTYPE_SOCKET)
2517                 return (linux_ioctl_socket(td, args));
2518         return (ENOIOCTL);
2519 }
2520
2521 /*
2522  * DRM ioctl handler (sys/dev/drm)
2523  */
2524 static int
2525 linux_ioctl_drm(struct thread *td, struct linux_ioctl_args *args)
2526 {
2527         args->cmd = SETDIR(args->cmd);
2528         return (sys_ioctl(td, (struct ioctl_args *)args));
2529 }
2530
2531 #ifdef COMPAT_LINUX32
2532 #define CP(src,dst,fld) do { (dst).fld = (src).fld; } while (0)
2533 #define PTRIN_CP(src,dst,fld) \
2534         do { (dst).fld = PTRIN((src).fld); } while (0)
2535 #define PTROUT_CP(src,dst,fld) \
2536         do { (dst).fld = PTROUT((src).fld); } while (0)
2537
2538 static int
2539 linux_ioctl_sg_io(struct thread *td, struct linux_ioctl_args *args)
2540 {
2541         struct sg_io_hdr io;
2542         struct sg_io_hdr32 io32;
2543         struct file *fp;
2544         int error;
2545
2546         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
2547         if (error != 0) {
2548                 printf("sg_linux_ioctl: fget returned %d\n", error);
2549                 return (error);
2550         }
2551
2552         if ((error = copyin((void *)args->arg, &io32, sizeof(io32))) != 0)
2553                 goto out;
2554
2555         CP(io32, io, interface_id);
2556         CP(io32, io, dxfer_direction);
2557         CP(io32, io, cmd_len);
2558         CP(io32, io, mx_sb_len);
2559         CP(io32, io, iovec_count);
2560         CP(io32, io, dxfer_len);
2561         PTRIN_CP(io32, io, dxferp);
2562         PTRIN_CP(io32, io, cmdp);
2563         PTRIN_CP(io32, io, sbp);
2564         CP(io32, io, timeout);
2565         CP(io32, io, flags);
2566         CP(io32, io, pack_id);
2567         PTRIN_CP(io32, io, usr_ptr);
2568         CP(io32, io, status);
2569         CP(io32, io, masked_status);
2570         CP(io32, io, msg_status);
2571         CP(io32, io, sb_len_wr);
2572         CP(io32, io, host_status);
2573         CP(io32, io, driver_status);
2574         CP(io32, io, resid);
2575         CP(io32, io, duration);
2576         CP(io32, io, info);
2577
2578         if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
2579                 goto out;
2580
2581         CP(io, io32, interface_id);
2582         CP(io, io32, dxfer_direction);
2583         CP(io, io32, cmd_len);
2584         CP(io, io32, mx_sb_len);
2585         CP(io, io32, iovec_count);
2586         CP(io, io32, dxfer_len);
2587         PTROUT_CP(io, io32, dxferp);
2588         PTROUT_CP(io, io32, cmdp);
2589         PTROUT_CP(io, io32, sbp);
2590         CP(io, io32, timeout);
2591         CP(io, io32, flags);
2592         CP(io, io32, pack_id);
2593         PTROUT_CP(io, io32, usr_ptr);
2594         CP(io, io32, status);
2595         CP(io, io32, masked_status);
2596         CP(io, io32, msg_status);
2597         CP(io, io32, sb_len_wr);
2598         CP(io, io32, host_status);
2599         CP(io, io32, driver_status);
2600         CP(io, io32, resid);
2601         CP(io, io32, duration);
2602         CP(io, io32, info);
2603
2604         error = copyout(&io32, (void *)args->arg, sizeof(io32));
2605
2606 out:
2607         fdrop(fp, td);
2608         return (error);
2609 }
2610 #endif
2611
2612 static int
2613 linux_ioctl_sg(struct thread *td, struct linux_ioctl_args *args)
2614 {
2615
2616         switch (args->cmd) {
2617         case LINUX_SG_GET_VERSION_NUM:
2618                 args->cmd = SG_GET_VERSION_NUM;
2619                 break;
2620         case LINUX_SG_SET_TIMEOUT:
2621                 args->cmd = SG_SET_TIMEOUT;
2622                 break;
2623         case LINUX_SG_GET_TIMEOUT:
2624                 args->cmd = SG_GET_TIMEOUT;
2625                 break;
2626         case LINUX_SG_IO:
2627                 args->cmd = SG_IO;
2628 #ifdef COMPAT_LINUX32
2629                 return (linux_ioctl_sg_io(td, args));
2630 #endif
2631                 break;
2632         case LINUX_SG_GET_RESERVED_SIZE:
2633                 args->cmd = SG_GET_RESERVED_SIZE;
2634                 break;
2635         case LINUX_SG_GET_SCSI_ID:
2636                 args->cmd = SG_GET_SCSI_ID;
2637                 break;
2638         case LINUX_SG_GET_SG_TABLESIZE:
2639                 args->cmd = SG_GET_SG_TABLESIZE;
2640                 break;
2641         default:
2642                 return (ENODEV);
2643         }
2644         return (sys_ioctl(td, (struct ioctl_args *)args));
2645 }
2646
2647 /*
2648  * Video4Linux (V4L) ioctl handler
2649  */
2650 static int
2651 linux_to_bsd_v4l_tuner(struct l_video_tuner *lvt, struct video_tuner *vt)
2652 {
2653         vt->tuner = lvt->tuner;
2654         strlcpy(vt->name, lvt->name, LINUX_VIDEO_TUNER_NAME_SIZE);
2655         vt->rangelow = lvt->rangelow;   /* possible long size conversion */
2656         vt->rangehigh = lvt->rangehigh; /* possible long size conversion */
2657         vt->flags = lvt->flags;
2658         vt->mode = lvt->mode;
2659         vt->signal = lvt->signal;
2660         return (0);
2661 }
2662
2663 static int
2664 bsd_to_linux_v4l_tuner(struct video_tuner *vt, struct l_video_tuner *lvt)
2665 {
2666         lvt->tuner = vt->tuner;
2667         strlcpy(lvt->name, vt->name, LINUX_VIDEO_TUNER_NAME_SIZE);
2668         lvt->rangelow = vt->rangelow;   /* possible long size conversion */
2669         lvt->rangehigh = vt->rangehigh; /* possible long size conversion */
2670         lvt->flags = vt->flags;
2671         lvt->mode = vt->mode;
2672         lvt->signal = vt->signal;
2673         return (0);
2674 }
2675
2676 #ifdef COMPAT_LINUX_V4L_CLIPLIST
2677 static int
2678 linux_to_bsd_v4l_clip(struct l_video_clip *lvc, struct video_clip *vc)
2679 {
2680         vc->x = lvc->x;
2681         vc->y = lvc->y;
2682         vc->width = lvc->width;
2683         vc->height = lvc->height;
2684         vc->next = PTRIN(lvc->next);    /* possible pointer size conversion */
2685         return (0);
2686 }
2687 #endif
2688
2689 static int
2690 linux_to_bsd_v4l_window(struct l_video_window *lvw, struct video_window *vw)
2691 {
2692         vw->x = lvw->x;
2693         vw->y = lvw->y;
2694         vw->width = lvw->width;
2695         vw->height = lvw->height;
2696         vw->chromakey = lvw->chromakey;
2697         vw->flags = lvw->flags;
2698         vw->clips = PTRIN(lvw->clips);  /* possible pointer size conversion */
2699         vw->clipcount = lvw->clipcount;
2700         return (0);
2701 }
2702
2703 static int
2704 bsd_to_linux_v4l_window(struct video_window *vw, struct l_video_window *lvw)
2705 {
2706         memset(lvw, 0, sizeof(*lvw));
2707
2708         lvw->x = vw->x;
2709         lvw->y = vw->y;
2710         lvw->width = vw->width;
2711         lvw->height = vw->height;
2712         lvw->chromakey = vw->chromakey;
2713         lvw->flags = vw->flags;
2714         lvw->clips = PTROUT(vw->clips); /* possible pointer size conversion */
2715         lvw->clipcount = vw->clipcount;
2716         return (0);
2717 }
2718
2719 static int
2720 linux_to_bsd_v4l_buffer(struct l_video_buffer *lvb, struct video_buffer *vb)
2721 {
2722         vb->base = PTRIN(lvb->base);    /* possible pointer size conversion */
2723         vb->height = lvb->height;
2724         vb->width = lvb->width;
2725         vb->depth = lvb->depth;
2726         vb->bytesperline = lvb->bytesperline;
2727         return (0);
2728 }
2729
2730 static int
2731 bsd_to_linux_v4l_buffer(struct video_buffer *vb, struct l_video_buffer *lvb)
2732 {
2733         lvb->base = PTROUT(vb->base);   /* possible pointer size conversion */
2734         lvb->height = vb->height;
2735         lvb->width = vb->width;
2736         lvb->depth = vb->depth;
2737         lvb->bytesperline = vb->bytesperline;
2738         return (0);
2739 }
2740
2741 static int
2742 linux_to_bsd_v4l_code(struct l_video_code *lvc, struct video_code *vc)
2743 {
2744         strlcpy(vc->loadwhat, lvc->loadwhat, LINUX_VIDEO_CODE_LOADWHAT_SIZE);
2745         vc->datasize = lvc->datasize;
2746         vc->data = PTRIN(lvc->data);    /* possible pointer size conversion */
2747         return (0);
2748 }
2749
2750 #ifdef COMPAT_LINUX_V4L_CLIPLIST
2751 static int
2752 linux_v4l_clip_copy(void *lvc, struct video_clip **ppvc)
2753 {
2754         int error;
2755         struct video_clip vclip;
2756         struct l_video_clip l_vclip;
2757
2758         error = copyin(lvc, &l_vclip, sizeof(l_vclip));
2759         if (error) return (error);
2760         linux_to_bsd_v4l_clip(&l_vclip, &vclip);
2761         /* XXX: If there can be no concurrency: s/M_NOWAIT/M_WAITOK/ */
2762         if ((*ppvc = malloc(sizeof(**ppvc), M_LINUX, M_NOWAIT)) == NULL)
2763                 return (ENOMEM);    /* XXX: Linux has no ENOMEM here. */
2764         memcpy(*ppvc, &vclip, sizeof(vclip));
2765         (*ppvc)->next = NULL;
2766         return (0);
2767 }
2768
2769 static int
2770 linux_v4l_cliplist_free(struct video_window *vw)
2771 {
2772         struct video_clip **ppvc;
2773         struct video_clip **ppvc_next;
2774
2775         for (ppvc = &(vw->clips); *ppvc != NULL; ppvc = ppvc_next) {
2776                 ppvc_next = &((*ppvc)->next);
2777                 free(*ppvc, M_LINUX);
2778         }
2779         vw->clips = NULL;
2780
2781         return (0);
2782 }
2783
2784 static int
2785 linux_v4l_cliplist_copy(struct l_video_window *lvw, struct video_window *vw)
2786 {
2787         int error;
2788         int clipcount;
2789         void *plvc;
2790         struct video_clip **ppvc;
2791
2792         /*
2793          * XXX: The cliplist is used to pass in a list of clipping
2794          *      rectangles or, if clipcount == VIDEO_CLIP_BITMAP, a
2795          *      clipping bitmap.  Some Linux apps, however, appear to
2796          *      leave cliplist and clips uninitialized.  In any case,
2797          *      the cliplist is not used by pwc(4), at the time of
2798          *      writing, FreeBSD's only V4L driver.  When a driver
2799          *      that uses the cliplist is developed, this code may
2800          *      need re-examiniation.
2801          */
2802         error = 0;
2803         clipcount = vw->clipcount;
2804         if (clipcount == VIDEO_CLIP_BITMAP) {
2805                 /*
2806                  * In this case, the pointer (clips) is overloaded
2807                  * to be a "void *" to a bitmap, therefore there
2808                  * is no struct video_clip to copy now.
2809                  */
2810         } else if (clipcount > 0 && clipcount <= 16384) {
2811                 /*
2812                  * Clips points to list of clip rectangles, so
2813                  * copy the list.
2814                  *
2815                  * XXX: Upper limit of 16384 was used here to try to
2816                  *      avoid cases when clipcount and clips pointer
2817                  *      are uninitialized and therefore have high random
2818                  *      values, as is the case in the Linux Skype
2819                  *      application.  The value 16384 was chosen as that
2820                  *      is what is used in the Linux stradis(4) MPEG
2821                  *      decoder driver, the only place we found an
2822                  *      example of cliplist use.
2823                  */
2824                 plvc = PTRIN(lvw->clips);
2825                 vw->clips = NULL;
2826                 ppvc = &(vw->clips);
2827                 while (clipcount-- > 0) {
2828                         if (plvc == NULL) {
2829                                 error = EFAULT;
2830                                 break;
2831                         } else {
2832                                 error = linux_v4l_clip_copy(plvc, ppvc);
2833                                 if (error) {
2834                                         linux_v4l_cliplist_free(vw);
2835                                         break;
2836                                 }
2837                         }
2838                         ppvc = &((*ppvc)->next);
2839                         plvc = PTRIN(((struct l_video_clip *) plvc)->next);
2840                 }
2841         } else {
2842                 /*
2843                  * clipcount == 0 or negative (but not VIDEO_CLIP_BITMAP)
2844                  * Force cliplist to null.
2845                  */
2846                 vw->clipcount = 0;
2847                 vw->clips = NULL;
2848         }
2849         return (error);
2850 }
2851 #endif
2852
2853 static int
2854 linux_ioctl_v4l(struct thread *td, struct linux_ioctl_args *args)
2855 {
2856         struct file *fp;
2857         int error;
2858         struct video_tuner vtun;
2859         struct video_window vwin;
2860         struct video_buffer vbuf;
2861         struct video_code vcode;
2862         struct l_video_tuner l_vtun;
2863         struct l_video_window l_vwin;
2864         struct l_video_buffer l_vbuf;
2865         struct l_video_code l_vcode;
2866
2867         switch (args->cmd & 0xffff) {
2868         case LINUX_VIDIOCGCAP:          args->cmd = VIDIOCGCAP; break;
2869         case LINUX_VIDIOCGCHAN:         args->cmd = VIDIOCGCHAN; break;
2870         case LINUX_VIDIOCSCHAN:         args->cmd = VIDIOCSCHAN; break;
2871
2872         case LINUX_VIDIOCGTUNER:
2873                 error = fget(td, args->fd,
2874                     &cap_ioctl_rights, &fp);
2875                 if (error != 0)
2876                         return (error);
2877                 error = copyin((void *) args->arg, &l_vtun, sizeof(l_vtun));
2878                 if (error) {
2879                         fdrop(fp, td);
2880                         return (error);
2881                 }
2882                 linux_to_bsd_v4l_tuner(&l_vtun, &vtun);
2883                 error = fo_ioctl(fp, VIDIOCGTUNER, &vtun, td->td_ucred, td);
2884                 if (!error) {
2885                         bsd_to_linux_v4l_tuner(&vtun, &l_vtun);
2886                         error = copyout(&l_vtun, (void *) args->arg,
2887                             sizeof(l_vtun));
2888                 }
2889                 fdrop(fp, td);
2890                 return (error);
2891
2892         case LINUX_VIDIOCSTUNER:
2893                 error = fget(td, args->fd,
2894                     &cap_ioctl_rights, &fp);
2895                 if (error != 0)
2896                         return (error);
2897                 error = copyin((void *) args->arg, &l_vtun, sizeof(l_vtun));
2898                 if (error) {
2899                         fdrop(fp, td);
2900                         return (error);
2901                 }
2902                 linux_to_bsd_v4l_tuner(&l_vtun, &vtun);
2903                 error = fo_ioctl(fp, VIDIOCSTUNER, &vtun, td->td_ucred, td);
2904                 fdrop(fp, td);
2905                 return (error);
2906
2907         case LINUX_VIDIOCGPICT:         args->cmd = VIDIOCGPICT; break;
2908         case LINUX_VIDIOCSPICT:         args->cmd = VIDIOCSPICT; break;
2909         case LINUX_VIDIOCCAPTURE:       args->cmd = VIDIOCCAPTURE; break;
2910
2911         case LINUX_VIDIOCGWIN:
2912                 error = fget(td, args->fd,
2913                     &cap_ioctl_rights, &fp);
2914                 if (error != 0)
2915                         return (error);
2916                 error = fo_ioctl(fp, VIDIOCGWIN, &vwin, td->td_ucred, td);
2917                 if (!error) {
2918                         bsd_to_linux_v4l_window(&vwin, &l_vwin);
2919                         error = copyout(&l_vwin, (void *) args->arg,
2920                             sizeof(l_vwin));
2921                 }
2922                 fdrop(fp, td);
2923                 return (error);
2924
2925         case LINUX_VIDIOCSWIN:
2926                 error = fget(td, args->fd,
2927                     &cap_ioctl_rights, &fp);
2928                 if (error != 0)
2929                         return (error);
2930                 error = copyin((void *) args->arg, &l_vwin, sizeof(l_vwin));
2931                 if (error) {
2932                         fdrop(fp, td);
2933                         return (error);
2934                 }
2935                 linux_to_bsd_v4l_window(&l_vwin, &vwin);
2936 #ifdef COMPAT_LINUX_V4L_CLIPLIST
2937                 error = linux_v4l_cliplist_copy(&l_vwin, &vwin);
2938                 if (error) {
2939                         fdrop(fp, td);
2940                         return (error);
2941                 }
2942 #endif
2943                 error = fo_ioctl(fp, VIDIOCSWIN, &vwin, td->td_ucred, td);
2944                 fdrop(fp, td);
2945 #ifdef COMPAT_LINUX_V4L_CLIPLIST
2946                 linux_v4l_cliplist_free(&vwin);
2947 #endif
2948                 return (error);
2949
2950         case LINUX_VIDIOCGFBUF:
2951                 error = fget(td, args->fd,
2952                     &cap_ioctl_rights, &fp);
2953                 if (error != 0)
2954                         return (error);
2955                 error = fo_ioctl(fp, VIDIOCGFBUF, &vbuf, td->td_ucred, td);
2956                 if (!error) {
2957                         bsd_to_linux_v4l_buffer(&vbuf, &l_vbuf);
2958                         error = copyout(&l_vbuf, (void *) args->arg,
2959                             sizeof(l_vbuf));
2960                 }
2961                 fdrop(fp, td);
2962                 return (error);
2963
2964         case LINUX_VIDIOCSFBUF:
2965                 error = fget(td, args->fd,
2966                     &cap_ioctl_rights, &fp);
2967                 if (error != 0)
2968                         return (error);
2969                 error = copyin((void *) args->arg, &l_vbuf, sizeof(l_vbuf));
2970                 if (error) {
2971                         fdrop(fp, td);
2972                         return (error);
2973                 }
2974                 linux_to_bsd_v4l_buffer(&l_vbuf, &vbuf);
2975                 error = fo_ioctl(fp, VIDIOCSFBUF, &vbuf, td->td_ucred, td);
2976                 fdrop(fp, td);
2977                 return (error);
2978
2979         case LINUX_VIDIOCKEY:           args->cmd = VIDIOCKEY; break;
2980         case LINUX_VIDIOCGFREQ:         args->cmd = VIDIOCGFREQ; break;
2981         case LINUX_VIDIOCSFREQ:         args->cmd = VIDIOCSFREQ; break;
2982         case LINUX_VIDIOCGAUDIO:        args->cmd = VIDIOCGAUDIO; break;
2983         case LINUX_VIDIOCSAUDIO:        args->cmd = VIDIOCSAUDIO; break;
2984         case LINUX_VIDIOCSYNC:          args->cmd = VIDIOCSYNC; break;
2985         case LINUX_VIDIOCMCAPTURE:      args->cmd = VIDIOCMCAPTURE; break;
2986         case LINUX_VIDIOCGMBUF:         args->cmd = VIDIOCGMBUF; break;
2987         case LINUX_VIDIOCGUNIT:         args->cmd = VIDIOCGUNIT; break;
2988         case LINUX_VIDIOCGCAPTURE:      args->cmd = VIDIOCGCAPTURE; break;
2989         case LINUX_VIDIOCSCAPTURE:      args->cmd = VIDIOCSCAPTURE; break;
2990         case LINUX_VIDIOCSPLAYMODE:     args->cmd = VIDIOCSPLAYMODE; break;
2991         case LINUX_VIDIOCSWRITEMODE:    args->cmd = VIDIOCSWRITEMODE; break;
2992         case LINUX_VIDIOCGPLAYINFO:     args->cmd = VIDIOCGPLAYINFO; break;
2993
2994         case LINUX_VIDIOCSMICROCODE:
2995                 error = fget(td, args->fd,
2996                     &cap_ioctl_rights, &fp);
2997                 if (error != 0)
2998                         return (error);
2999                 error = copyin((void *) args->arg, &l_vcode, sizeof(l_vcode));
3000                 if (error) {
3001                         fdrop(fp, td);
3002                         return (error);
3003                 }
3004                 linux_to_bsd_v4l_code(&l_vcode, &vcode);
3005                 error = fo_ioctl(fp, VIDIOCSMICROCODE, &vcode, td->td_ucred, td);
3006                 fdrop(fp, td);
3007                 return (error);
3008
3009         case LINUX_VIDIOCGVBIFMT:       args->cmd = VIDIOCGVBIFMT; break;
3010         case LINUX_VIDIOCSVBIFMT:       args->cmd = VIDIOCSVBIFMT; break;
3011         default:                        return (ENOIOCTL);
3012         }
3013
3014         error = sys_ioctl(td, (struct ioctl_args *)args);
3015         return (error);
3016 }
3017
3018 /*
3019  * Special ioctl handler
3020  */
3021 static int
3022 linux_ioctl_special(struct thread *td, struct linux_ioctl_args *args)
3023 {
3024         int error;
3025
3026         switch (args->cmd) {
3027         case LINUX_SIOCGIFADDR:
3028                 args->cmd = SIOCGIFADDR;
3029                 error = sys_ioctl(td, (struct ioctl_args *)args);
3030                 break;
3031         case LINUX_SIOCSIFADDR:
3032                 args->cmd = SIOCSIFADDR;
3033                 error = sys_ioctl(td, (struct ioctl_args *)args);
3034                 break;
3035         case LINUX_SIOCGIFFLAGS:
3036                 args->cmd = SIOCGIFFLAGS;
3037                 error = sys_ioctl(td, (struct ioctl_args *)args);
3038                 break;
3039         default:
3040                 error = ENOIOCTL;
3041         }
3042
3043         return (error);
3044 }
3045
3046 static int
3047 linux_to_bsd_v4l2_standard(struct l_v4l2_standard *lvstd, struct v4l2_standard *vstd)
3048 {
3049         vstd->index = lvstd->index;
3050         vstd->id = lvstd->id;
3051         CTASSERT(sizeof(vstd->name) == sizeof(lvstd->name));
3052         memcpy(vstd->name, lvstd->name, sizeof(vstd->name));
3053         vstd->frameperiod = lvstd->frameperiod;
3054         vstd->framelines = lvstd->framelines;
3055         CTASSERT(sizeof(vstd->reserved) == sizeof(lvstd->reserved));
3056         memcpy(vstd->reserved, lvstd->reserved, sizeof(vstd->reserved));
3057         return (0);
3058 }
3059
3060 static int
3061 bsd_to_linux_v4l2_standard(struct v4l2_standard *vstd, struct l_v4l2_standard *lvstd)
3062 {
3063         lvstd->index = vstd->index;
3064         lvstd->id = vstd->id;
3065         CTASSERT(sizeof(vstd->name) == sizeof(lvstd->name));
3066         memcpy(lvstd->name, vstd->name, sizeof(lvstd->name));
3067         lvstd->frameperiod = vstd->frameperiod;
3068         lvstd->framelines = vstd->framelines;
3069         CTASSERT(sizeof(vstd->reserved) == sizeof(lvstd->reserved));
3070         memcpy(lvstd->reserved, vstd->reserved, sizeof(lvstd->reserved));
3071         return (0);
3072 }
3073
3074 static int
3075 linux_to_bsd_v4l2_buffer(struct l_v4l2_buffer *lvb, struct v4l2_buffer *vb)
3076 {
3077         vb->index = lvb->index;
3078         vb->type = lvb->type;
3079         vb->bytesused = lvb->bytesused;
3080         vb->flags = lvb->flags;
3081         vb->field = lvb->field;
3082         vb->timestamp.tv_sec = lvb->timestamp.tv_sec;
3083         vb->timestamp.tv_usec = lvb->timestamp.tv_usec;
3084         memcpy(&vb->timecode, &lvb->timecode, sizeof (lvb->timecode));
3085         vb->sequence = lvb->sequence;
3086         vb->memory = lvb->memory;
3087         if (lvb->memory == V4L2_MEMORY_USERPTR)
3088                 /* possible pointer size conversion */
3089                 vb->m.userptr = (unsigned long)PTRIN(lvb->m.userptr);
3090         else
3091                 vb->m.offset = lvb->m.offset;
3092         vb->length = lvb->length;
3093         vb->input = lvb->input;
3094         vb->reserved = lvb->reserved;
3095         return (0);
3096 }
3097
3098 static int
3099 bsd_to_linux_v4l2_buffer(struct v4l2_buffer *vb, struct l_v4l2_buffer *lvb)
3100 {
3101         lvb->index = vb->index;
3102         lvb->type = vb->type;
3103         lvb->bytesused = vb->bytesused;
3104         lvb->flags = vb->flags;
3105         lvb->field = vb->field;
3106         lvb->timestamp.tv_sec = vb->timestamp.tv_sec;
3107         lvb->timestamp.tv_usec = vb->timestamp.tv_usec;
3108         memcpy(&lvb->timecode, &vb->timecode, sizeof (vb->timecode));
3109         lvb->sequence = vb->sequence;
3110         lvb->memory = vb->memory;
3111         if (vb->memory == V4L2_MEMORY_USERPTR)
3112                 /* possible pointer size conversion */
3113                 lvb->m.userptr = PTROUT(vb->m.userptr);
3114         else
3115                 lvb->m.offset = vb->m.offset;
3116         lvb->length = vb->length;
3117         lvb->input = vb->input;
3118         lvb->reserved = vb->reserved;
3119         return (0);
3120 }
3121
3122 static int
3123 linux_to_bsd_v4l2_format(struct l_v4l2_format *lvf, struct v4l2_format *vf)
3124 {
3125         vf->type = lvf->type;
3126         if (lvf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY
3127 #ifdef V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
3128             || lvf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
3129 #endif
3130             )
3131                 /*
3132                  * XXX TODO - needs 32 -> 64 bit conversion:
3133                  * (unused by webcams?)
3134                  */
3135                 return (EINVAL);
3136         memcpy(&vf->fmt, &lvf->fmt, sizeof(vf->fmt));
3137         return (0);
3138 }
3139
3140 static int
3141 bsd_to_linux_v4l2_format(struct v4l2_format *vf, struct l_v4l2_format *lvf)
3142 {
3143         lvf->type = vf->type;
3144         if (vf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY
3145 #ifdef V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
3146             || vf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
3147 #endif
3148             )
3149                 /*
3150                  * XXX TODO - needs 32 -> 64 bit conversion:
3151                  * (unused by webcams?)
3152                  */
3153                 return (EINVAL);
3154         memcpy(&lvf->fmt, &vf->fmt, sizeof(vf->fmt));
3155         return (0);
3156 }
3157 static int
3158 linux_ioctl_v4l2(struct thread *td, struct linux_ioctl_args *args)
3159 {
3160         struct file *fp;
3161         int error;
3162         struct v4l2_format vformat;
3163         struct l_v4l2_format l_vformat;
3164         struct v4l2_standard vstd;
3165         struct l_v4l2_standard l_vstd;
3166         struct l_v4l2_buffer l_vbuf;
3167         struct v4l2_buffer vbuf;
3168         struct v4l2_input vinp;
3169
3170         switch (args->cmd & 0xffff) {
3171         case LINUX_VIDIOC_RESERVED:
3172         case LINUX_VIDIOC_LOG_STATUS:
3173                 if ((args->cmd & IOC_DIRMASK) != LINUX_IOC_VOID)
3174                         return (ENOIOCTL);
3175                 args->cmd = (args->cmd & 0xffff) | IOC_VOID;
3176                 break;
3177
3178         case LINUX_VIDIOC_OVERLAY:
3179         case LINUX_VIDIOC_STREAMON:
3180         case LINUX_VIDIOC_STREAMOFF:
3181         case LINUX_VIDIOC_S_STD:
3182         case LINUX_VIDIOC_S_TUNER:
3183         case LINUX_VIDIOC_S_AUDIO:
3184         case LINUX_VIDIOC_S_AUDOUT:
3185         case LINUX_VIDIOC_S_MODULATOR:
3186         case LINUX_VIDIOC_S_FREQUENCY:
3187         case LINUX_VIDIOC_S_CROP:
3188         case LINUX_VIDIOC_S_JPEGCOMP:
3189         case LINUX_VIDIOC_S_PRIORITY:
3190         case LINUX_VIDIOC_DBG_S_REGISTER:
3191         case LINUX_VIDIOC_S_HW_FREQ_SEEK:
3192         case LINUX_VIDIOC_SUBSCRIBE_EVENT:
3193         case LINUX_VIDIOC_UNSUBSCRIBE_EVENT:
3194                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_IN;
3195                 break;
3196
3197         case LINUX_VIDIOC_QUERYCAP:
3198         case LINUX_VIDIOC_G_STD:
3199         case LINUX_VIDIOC_G_AUDIO:
3200         case LINUX_VIDIOC_G_INPUT:
3201         case LINUX_VIDIOC_G_OUTPUT:
3202         case LINUX_VIDIOC_G_AUDOUT:
3203         case LINUX_VIDIOC_G_JPEGCOMP:
3204         case LINUX_VIDIOC_QUERYSTD:
3205         case LINUX_VIDIOC_G_PRIORITY:
3206         case LINUX_VIDIOC_QUERY_DV_PRESET:
3207                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_OUT;
3208                 break;
3209
3210         case LINUX_VIDIOC_ENUM_FMT:
3211         case LINUX_VIDIOC_REQBUFS:
3212         case LINUX_VIDIOC_G_PARM:
3213         case LINUX_VIDIOC_S_PARM:
3214         case LINUX_VIDIOC_G_CTRL:
3215         case LINUX_VIDIOC_S_CTRL:
3216         case LINUX_VIDIOC_G_TUNER:
3217         case LINUX_VIDIOC_QUERYCTRL:
3218         case LINUX_VIDIOC_QUERYMENU:
3219         case LINUX_VIDIOC_S_INPUT:
3220         case LINUX_VIDIOC_S_OUTPUT:
3221         case LINUX_VIDIOC_ENUMOUTPUT:
3222         case LINUX_VIDIOC_G_MODULATOR:
3223         case LINUX_VIDIOC_G_FREQUENCY:
3224         case LINUX_VIDIOC_CROPCAP:
3225         case LINUX_VIDIOC_G_CROP:
3226         case LINUX_VIDIOC_ENUMAUDIO:
3227         case LINUX_VIDIOC_ENUMAUDOUT:
3228         case LINUX_VIDIOC_G_SLICED_VBI_CAP:
3229 #ifdef VIDIOC_ENUM_FRAMESIZES
3230         case LINUX_VIDIOC_ENUM_FRAMESIZES:
3231         case LINUX_VIDIOC_ENUM_FRAMEINTERVALS:
3232         case LINUX_VIDIOC_ENCODER_CMD:
3233         case LINUX_VIDIOC_TRY_ENCODER_CMD:
3234 #endif
3235         case LINUX_VIDIOC_DBG_G_REGISTER:
3236         case LINUX_VIDIOC_DBG_G_CHIP_IDENT:
3237         case LINUX_VIDIOC_ENUM_DV_PRESETS:
3238         case LINUX_VIDIOC_S_DV_PRESET:
3239         case LINUX_VIDIOC_G_DV_PRESET:
3240         case LINUX_VIDIOC_S_DV_TIMINGS:
3241         case LINUX_VIDIOC_G_DV_TIMINGS:
3242                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_INOUT;
3243                 break;
3244
3245         case LINUX_VIDIOC_G_FMT:
3246         case LINUX_VIDIOC_S_FMT:
3247         case LINUX_VIDIOC_TRY_FMT:
3248                 error = copyin((void *)args->arg, &l_vformat, sizeof(l_vformat));
3249                 if (error)
3250                         return (error);
3251                 error = fget(td, args->fd,
3252                     &cap_ioctl_rights, &fp);
3253                 if (error)
3254                         return (error);
3255                 if (linux_to_bsd_v4l2_format(&l_vformat, &vformat) != 0)
3256                         error = EINVAL;
3257                 else if ((args->cmd & 0xffff) == LINUX_VIDIOC_G_FMT)
3258                         error = fo_ioctl(fp, VIDIOC_G_FMT, &vformat,
3259                             td->td_ucred, td);
3260                 else if ((args->cmd & 0xffff) == LINUX_VIDIOC_S_FMT)
3261                         error = fo_ioctl(fp, VIDIOC_S_FMT, &vformat,
3262                             td->td_ucred, td);
3263                 else
3264                         error = fo_ioctl(fp, VIDIOC_TRY_FMT, &vformat,
3265                             td->td_ucred, td);
3266                 bsd_to_linux_v4l2_format(&vformat, &l_vformat);
3267                 copyout(&l_vformat, (void *)args->arg, sizeof(l_vformat));
3268                 fdrop(fp, td);
3269                 return (error);
3270
3271         case LINUX_VIDIOC_ENUMSTD:
3272                 error = copyin((void *)args->arg, &l_vstd, sizeof(l_vstd));
3273                 if (error)
3274                         return (error);
3275                 linux_to_bsd_v4l2_standard(&l_vstd, &vstd);
3276                 error = fget(td, args->fd,
3277                     &cap_ioctl_rights, &fp);
3278                 if (error)
3279                         return (error);
3280                 error = fo_ioctl(fp, VIDIOC_ENUMSTD, (caddr_t)&vstd,
3281                     td->td_ucred, td);
3282                 if (error) {
3283                         fdrop(fp, td);
3284                         return (error);
3285                 }
3286                 bsd_to_linux_v4l2_standard(&vstd, &l_vstd);
3287                 error = copyout(&l_vstd, (void *)args->arg, sizeof(l_vstd));
3288                 fdrop(fp, td);
3289                 return (error);
3290
3291         case LINUX_VIDIOC_ENUMINPUT:
3292                 /*
3293                  * The Linux struct l_v4l2_input differs only in size,
3294                  * it has no padding at the end.
3295                  */
3296                 error = copyin((void *)args->arg, &vinp,
3297                                 sizeof(struct l_v4l2_input));
3298                 if (error != 0)
3299                         return (error);
3300                 error = fget(td, args->fd,
3301                     &cap_ioctl_rights, &fp);
3302                 if (error != 0)
3303                         return (error);
3304                 error = fo_ioctl(fp, VIDIOC_ENUMINPUT, (caddr_t)&vinp,
3305                     td->td_ucred, td);
3306                 if (error) {
3307                         fdrop(fp, td);
3308                         return (error);
3309                 }
3310                 error = copyout(&vinp, (void *)args->arg,
3311                                 sizeof(struct l_v4l2_input));
3312                 fdrop(fp, td);
3313                 return (error);
3314
3315         case LINUX_VIDIOC_QUERYBUF:
3316         case LINUX_VIDIOC_QBUF:
3317         case LINUX_VIDIOC_DQBUF:
3318                 error = copyin((void *)args->arg, &l_vbuf, sizeof(l_vbuf));
3319                 if (error)
3320                         return (error);
3321                 error = fget(td, args->fd,
3322                     &cap_ioctl_rights, &fp);
3323                 if (error)
3324                         return (error);
3325                 linux_to_bsd_v4l2_buffer(&l_vbuf, &vbuf);
3326                 if ((args->cmd & 0xffff) == LINUX_VIDIOC_QUERYBUF)
3327                         error = fo_ioctl(fp, VIDIOC_QUERYBUF, &vbuf,
3328                             td->td_ucred, td);
3329                 else if ((args->cmd & 0xffff) == LINUX_VIDIOC_QBUF)
3330                         error = fo_ioctl(fp, VIDIOC_QBUF, &vbuf,
3331                             td->td_ucred, td);
3332                 else
3333                         error = fo_ioctl(fp, VIDIOC_DQBUF, &vbuf,
3334                             td->td_ucred, td);
3335                 bsd_to_linux_v4l2_buffer(&vbuf, &l_vbuf);
3336                 copyout(&l_vbuf, (void *)args->arg, sizeof(l_vbuf));
3337                 fdrop(fp, td);
3338                 return (error);
3339
3340         /*
3341          * XXX TODO - these need 32 -> 64 bit conversion:
3342          * (are any of them needed for webcams?)
3343          */
3344         case LINUX_VIDIOC_G_FBUF:
3345         case LINUX_VIDIOC_S_FBUF:
3346
3347         case LINUX_VIDIOC_G_EXT_CTRLS:
3348         case LINUX_VIDIOC_S_EXT_CTRLS:
3349         case LINUX_VIDIOC_TRY_EXT_CTRLS:
3350
3351         case LINUX_VIDIOC_DQEVENT:
3352
3353         default:                        return (ENOIOCTL);
3354         }
3355
3356         error = sys_ioctl(td, (struct ioctl_args *)args);
3357         return (error);
3358 }
3359
3360 /*
3361  * Support for emulators/linux-libusb. This port uses FBSD_LUSB* macros
3362  * instead of USB* ones. This lets us to provide correct values for cmd.
3363  * 0xffffffe0 -- 0xffffffff range seemed to be the least collision-prone.
3364  */
3365 static int
3366 linux_ioctl_fbsd_usb(struct thread *td, struct linux_ioctl_args *args)
3367 {
3368         int error;
3369
3370         error = 0;
3371         switch (args->cmd) {
3372         case FBSD_LUSB_DEVICEENUMERATE:
3373                 args->cmd = USB_DEVICEENUMERATE;
3374                 break;
3375         case FBSD_LUSB_DEV_QUIRK_ADD:
3376                 args->cmd = USB_DEV_QUIRK_ADD;
3377                 break;
3378         case FBSD_LUSB_DEV_QUIRK_GET:
3379                 args->cmd = USB_DEV_QUIRK_GET;
3380                 break;
3381         case FBSD_LUSB_DEV_QUIRK_REMOVE:
3382                 args->cmd = USB_DEV_QUIRK_REMOVE;
3383                 break;
3384         case FBSD_LUSB_DO_REQUEST:
3385                 args->cmd = USB_DO_REQUEST;
3386                 break;
3387         case FBSD_LUSB_FS_CLEAR_STALL_SYNC:
3388                 args->cmd = USB_FS_CLEAR_STALL_SYNC;
3389                 break;
3390         case FBSD_LUSB_FS_CLOSE:
3391                 args->cmd = USB_FS_CLOSE;
3392                 break;
3393         case FBSD_LUSB_FS_COMPLETE:
3394                 args->cmd = USB_FS_COMPLETE;
3395                 break;
3396         case FBSD_LUSB_FS_INIT:
3397                 args->cmd = USB_FS_INIT;
3398                 break;
3399         case FBSD_LUSB_FS_OPEN:
3400                 args->cmd = USB_FS_OPEN;
3401                 break;
3402         case FBSD_LUSB_FS_START:
3403                 args->cmd = USB_FS_START;
3404                 break;
3405         case FBSD_LUSB_FS_STOP:
3406                 args->cmd = USB_FS_STOP;
3407                 break;
3408         case FBSD_LUSB_FS_UNINIT:
3409                 args->cmd = USB_FS_UNINIT;
3410                 break;
3411         case FBSD_LUSB_GET_CONFIG:
3412                 args->cmd = USB_GET_CONFIG;
3413                 break;
3414         case FBSD_LUSB_GET_DEVICEINFO:
3415                 args->cmd = USB_GET_DEVICEINFO;
3416                 break;
3417         case FBSD_LUSB_GET_DEVICE_DESC:
3418                 args->cmd = USB_GET_DEVICE_DESC;
3419                 break;
3420         case FBSD_LUSB_GET_FULL_DESC:
3421                 args->cmd = USB_GET_FULL_DESC;
3422                 break;
3423         case FBSD_LUSB_GET_IFACE_DRIVER:
3424                 args->cmd = USB_GET_IFACE_DRIVER;
3425                 break;
3426         case FBSD_LUSB_GET_PLUGTIME:
3427                 args->cmd = USB_GET_PLUGTIME;
3428                 break;
3429         case FBSD_LUSB_GET_POWER_MODE:
3430                 args->cmd = USB_GET_POWER_MODE;
3431                 break;
3432         case FBSD_LUSB_GET_REPORT_DESC:
3433                 args->cmd = USB_GET_REPORT_DESC;
3434                 break;
3435         case FBSD_LUSB_GET_REPORT_ID:
3436                 args->cmd = USB_GET_REPORT_ID;
3437                 break;
3438         case FBSD_LUSB_GET_TEMPLATE:
3439                 args->cmd = USB_GET_TEMPLATE;
3440                 break;
3441         case FBSD_LUSB_IFACE_DRIVER_ACTIVE:
3442                 args->cmd = USB_IFACE_DRIVER_ACTIVE;
3443                 break;
3444         case FBSD_LUSB_IFACE_DRIVER_DETACH:
3445                 args->cmd = USB_IFACE_DRIVER_DETACH;
3446                 break;
3447         case FBSD_LUSB_QUIRK_NAME_GET:
3448                 args->cmd = USB_QUIRK_NAME_GET;
3449                 break;
3450         case FBSD_LUSB_READ_DIR:
3451                 args->cmd = USB_READ_DIR;
3452                 break;
3453         case FBSD_LUSB_SET_ALTINTERFACE:
3454                 args->cmd = USB_SET_ALTINTERFACE;
3455                 break;
3456         case FBSD_LUSB_SET_CONFIG:
3457                 args->cmd = USB_SET_CONFIG;
3458                 break;
3459         case FBSD_LUSB_SET_IMMED:
3460                 args->cmd = USB_SET_IMMED;
3461                 break;
3462         case FBSD_LUSB_SET_POWER_MODE:
3463                 args->cmd = USB_SET_POWER_MODE;
3464                 break;
3465         case FBSD_LUSB_SET_TEMPLATE:
3466                 args->cmd = USB_SET_TEMPLATE;
3467                 break;
3468         case FBSD_LUSB_FS_OPEN_STREAM:
3469                 args->cmd = USB_FS_OPEN_STREAM;
3470                 break;
3471         case FBSD_LUSB_GET_DEV_PORT_PATH:
3472                 args->cmd = USB_GET_DEV_PORT_PATH;
3473                 break;
3474         case FBSD_LUSB_GET_POWER_USAGE:
3475                 args->cmd = USB_GET_POWER_USAGE;
3476                 break;
3477         default:
3478                 error = ENOIOCTL;
3479         }
3480         if (error != ENOIOCTL)
3481                 error = sys_ioctl(td, (struct ioctl_args *)args);
3482         return (error);
3483 }
3484
3485 /*
3486  * Some evdev ioctls must be translated.
3487  *  - EVIOCGMTSLOTS is a IOC_READ ioctl on Linux although it has input data
3488  *    (must be IOC_INOUT on FreeBSD).
3489  *  - On Linux, EVIOCGRAB, EVIOCREVOKE and EVIOCRMFF are defined as _IOW with
3490  *    an int argument. You don't pass an int pointer to the ioctl(), however,
3491  *    but just the int directly. On FreeBSD, they are defined as _IOWINT for
3492  *    this to work.
3493  */
3494 static int
3495 linux_ioctl_evdev(struct thread *td, struct linux_ioctl_args *args)
3496 {
3497         struct file *fp;
3498         clockid_t clock;
3499         int error;
3500
3501         args->cmd = SETDIR(args->cmd);
3502
3503         switch (args->cmd) {
3504         case (EVIOCGRAB & ~IOC_DIRMASK) | IOC_IN:
3505                 args->cmd = EVIOCGRAB;
3506                 break;
3507         case (EVIOCREVOKE & ~IOC_DIRMASK) | IOC_IN:
3508                 args->cmd = EVIOCREVOKE;
3509                 break;
3510         case (EVIOCRMFF & ~IOC_DIRMASK) | IOC_IN:
3511                 args->cmd = EVIOCRMFF;
3512                 break;
3513         case EVIOCSCLOCKID: {
3514                 error = copyin(PTRIN(args->arg), &clock, sizeof(clock));
3515                 if (error != 0)
3516                         return (error);
3517                 if (clock & ~(LINUX_IOCTL_EVDEV_CLK))
3518                         return (EINVAL);
3519                 error = linux_to_native_clockid(&clock, clock);
3520                 if (error != 0)
3521                         return (error);
3522
3523                 error = fget(td, args->fd,
3524                     &cap_ioctl_rights, &fp);
3525                 if (error != 0)
3526                         return (error);
3527
3528                 error = fo_ioctl(fp, EVIOCSCLOCKID, &clock, td->td_ucred, td);
3529                 fdrop(fp, td);
3530                 return (error);
3531         }
3532         default:
3533                 break;
3534         }
3535
3536         if (IOCBASECMD(args->cmd) ==
3537             ((EVIOCGMTSLOTS(0) & ~IOC_DIRMASK) | IOC_OUT))
3538                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_INOUT;
3539
3540         return (sys_ioctl(td, (struct ioctl_args *)args));
3541 }
3542
3543 /*
3544  * main ioctl syscall function
3545  */
3546
3547 int
3548 linux_ioctl(struct thread *td, struct linux_ioctl_args *args)
3549 {
3550         struct file *fp;
3551         struct linux_ioctl_handler_element *he;
3552         int error, cmd;
3553
3554         error = fget(td, args->fd, &cap_ioctl_rights, &fp);
3555         if (error != 0)
3556                 return (error);
3557         if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
3558                 fdrop(fp, td);
3559                 return (EBADF);
3560         }
3561
3562         /* Iterate over the ioctl handlers */
3563         cmd = args->cmd & 0xffff;
3564         sx_slock(&linux_ioctl_sx);
3565         mtx_lock(&Giant);
3566 #ifdef COMPAT_LINUX32
3567         TAILQ_FOREACH(he, &linux32_ioctl_handlers, list) {
3568                 if (cmd >= he->low && cmd <= he->high) {
3569                         error = (*he->func)(td, args);
3570                         if (error != ENOIOCTL) {
3571                                 mtx_unlock(&Giant);
3572                                 sx_sunlock(&linux_ioctl_sx);
3573                                 fdrop(fp, td);
3574                                 return (error);
3575                         }
3576                 }
3577         }
3578 #endif
3579         TAILQ_FOREACH(he, &linux_ioctl_handlers, list) {
3580                 if (cmd >= he->low && cmd <= he->high) {
3581                         error = (*he->func)(td, args);
3582                         if (error != ENOIOCTL) {
3583                                 mtx_unlock(&Giant);
3584                                 sx_sunlock(&linux_ioctl_sx);
3585                                 fdrop(fp, td);
3586                                 return (error);
3587                         }
3588                 }
3589         }
3590         mtx_unlock(&Giant);
3591         sx_sunlock(&linux_ioctl_sx);
3592         fdrop(fp, td);
3593
3594         switch (args->cmd & 0xffff) {
3595         case LINUX_BTRFS_IOC_CLONE:
3596         case LINUX_FS_IOC_FIEMAP:
3597                 return (ENOTSUP);
3598
3599         default:
3600                 linux_msg(td, "ioctl fd=%d, cmd=0x%x ('%c',%d) is not implemented",
3601                     args->fd, (int)(args->cmd & 0xffff),
3602                     (int)(args->cmd & 0xff00) >> 8, (int)(args->cmd & 0xff));
3603                 break;
3604         }
3605
3606         return (EINVAL);
3607 }
3608
3609 int
3610 linux_ioctl_register_handler(struct linux_ioctl_handler *h)
3611 {
3612         struct linux_ioctl_handler_element *he, *cur;
3613
3614         if (h == NULL || h->func == NULL)
3615                 return (EINVAL);
3616
3617         /*
3618          * Reuse the element if the handler is already on the list, otherwise
3619          * create a new element.
3620          */
3621         sx_xlock(&linux_ioctl_sx);
3622         TAILQ_FOREACH(he, &linux_ioctl_handlers, list) {
3623                 if (he->func == h->func)
3624                         break;
3625         }
3626         if (he == NULL) {
3627                 he = malloc(sizeof(*he),
3628                     M_LINUX, M_WAITOK);
3629                 he->func = h->func;
3630         } else
3631                 TAILQ_REMOVE(&linux_ioctl_handlers, he, list);
3632
3633         /* Initialize range information. */
3634         he->low = h->low;
3635         he->high = h->high;
3636         he->span = h->high - h->low + 1;
3637
3638         /* Add the element to the list, sorted on span. */
3639         TAILQ_FOREACH(cur, &linux_ioctl_handlers, list) {
3640                 if (cur->span > he->span) {
3641                         TAILQ_INSERT_BEFORE(cur, he, list);
3642                         sx_xunlock(&linux_ioctl_sx);
3643                         return (0);
3644                 }
3645         }
3646         TAILQ_INSERT_TAIL(&linux_ioctl_handlers, he, list);
3647         sx_xunlock(&linux_ioctl_sx);
3648
3649         return (0);
3650 }
3651
3652 int
3653 linux_ioctl_unregister_handler(struct linux_ioctl_handler *h)
3654 {
3655         struct linux_ioctl_handler_element *he;
3656
3657         if (h == NULL || h->func == NULL)
3658                 return (EINVAL);
3659
3660         sx_xlock(&linux_ioctl_sx);
3661         TAILQ_FOREACH(he, &linux_ioctl_handlers, list) {
3662                 if (he->func == h->func) {
3663                         TAILQ_REMOVE(&linux_ioctl_handlers, he, list);
3664                         sx_xunlock(&linux_ioctl_sx);
3665                         free(he, M_LINUX);
3666                         return (0);
3667                 }
3668         }
3669         sx_xunlock(&linux_ioctl_sx);
3670
3671         return (EINVAL);
3672 }
3673
3674 #ifdef COMPAT_LINUX32
3675 int
3676 linux32_ioctl_register_handler(struct linux_ioctl_handler *h)
3677 {
3678         struct linux_ioctl_handler_element *he, *cur;
3679
3680         if (h == NULL || h->func == NULL)
3681                 return (EINVAL);
3682
3683         /*
3684          * Reuse the element if the handler is already on the list, otherwise
3685          * create a new element.
3686          */
3687         sx_xlock(&linux_ioctl_sx);
3688         TAILQ_FOREACH(he, &linux32_ioctl_handlers, list) {
3689                 if (he->func == h->func)
3690                         break;
3691         }
3692         if (he == NULL) {
3693                 he = malloc(sizeof(*he), M_LINUX, M_WAITOK);
3694                 he->func = h->func;
3695         } else
3696                 TAILQ_REMOVE(&linux32_ioctl_handlers, he, list);
3697
3698         /* Initialize range information. */
3699         he->low = h->low;
3700         he->high = h->high;
3701         he->span = h->high - h->low + 1;
3702
3703         /* Add the element to the list, sorted on span. */
3704         TAILQ_FOREACH(cur, &linux32_ioctl_handlers, list) {
3705                 if (cur->span > he->span) {
3706                         TAILQ_INSERT_BEFORE(cur, he, list);
3707                         sx_xunlock(&linux_ioctl_sx);
3708                         return (0);
3709                 }
3710         }
3711         TAILQ_INSERT_TAIL(&linux32_ioctl_handlers, he, list);
3712         sx_xunlock(&linux_ioctl_sx);
3713
3714         return (0);
3715 }
3716
3717 int
3718 linux32_ioctl_unregister_handler(struct linux_ioctl_handler *h)
3719 {
3720         struct linux_ioctl_handler_element *he;
3721
3722         if (h == NULL || h->func == NULL)
3723                 return (EINVAL);
3724
3725         sx_xlock(&linux_ioctl_sx);
3726         TAILQ_FOREACH(he, &linux32_ioctl_handlers, list) {
3727                 if (he->func == h->func) {
3728                         TAILQ_REMOVE(&linux32_ioctl_handlers, he, list);
3729                         sx_xunlock(&linux_ioctl_sx);
3730                         free(he, M_LINUX);
3731                         return (0);
3732                 }
3733         }
3734         sx_xunlock(&linux_ioctl_sx);
3735
3736         return (EINVAL);
3737 }
3738 #endif