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