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