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