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