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