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


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

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

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

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

Cache object: 67a7f86fd9b5d1abd16f4571d302ea25


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