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