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