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