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