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