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