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