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