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