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

Cache object: b73d912679e92b0a74955e771a17decd


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