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