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