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