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

Cache object: 7dd5abc63355c31b6e6ed29e37bba864


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