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