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

Cache object: b6799fde8c15593e2ae95eb5bcb311ff


[ 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.