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