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