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