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