The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/linux/linux_ioctl.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 4976c1bbd505551bdf1a0f35ebbafd47


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.