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