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