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