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