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