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