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