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