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