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