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