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


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

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

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

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

Cache object: bcc63f0b8486c12b17bcdae8efe47ba8


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