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