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