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