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