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