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


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

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

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

    1 /*-
    2  * Copyright (c) 1994-1995 Søren Schmidt
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer
   10  *    in this position and unchanged.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include "opt_compat.h"
   30 
   31 #include <sys/cdefs.h>
   32 __FBSDID("$FreeBSD: releng/8.3/sys/compat/linux/linux_ioctl.c 243417 2012-11-22 22:52:15Z simon $");
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/sysproto.h>
   37 #include <sys/cdio.h>
   38 #include <sys/dvdio.h>
   39 #include <sys/conf.h>
   40 #include <sys/disk.h>
   41 #include <sys/consio.h>
   42 #include <sys/ctype.h>
   43 #include <sys/fcntl.h>
   44 #include <sys/file.h>
   45 #include <sys/filedesc.h>
   46 #include <sys/filio.h>
   47 #include <sys/jail.h>
   48 #include <sys/kbio.h>
   49 #include <sys/kernel.h>
   50 #include <sys/linker_set.h>
   51 #include <sys/lock.h>
   52 #include <sys/malloc.h>
   53 #include <sys/proc.h>
   54 #include <sys/sbuf.h>
   55 #include <sys/socket.h>
   56 #include <sys/sockio.h>
   57 #include <sys/soundcard.h>
   58 #include <sys/stdint.h>
   59 #include <sys/sx.h>
   60 #include <sys/sysctl.h>
   61 #include <sys/tty.h>
   62 #include <sys/uio.h>
   63 #include <sys/types.h>
   64 #include <sys/mman.h>
   65 #include <sys/resourcevar.h>
   66 
   67 #include <net/if.h>
   68 #include <net/if_dl.h>
   69 #include <net/if_types.h>
   70 #include <net/vnet.h>
   71 
   72 #ifdef COMPAT_LINUX32
   73 #include <machine/../linux32/linux.h>
   74 #include <machine/../linux32/linux32_proto.h>
   75 #else
   76 #include <machine/../linux/linux.h>
   77 #include <machine/../linux/linux_proto.h>
   78 #endif
   79 
   80 #include <compat/linux/linux_ioctl.h>
   81 #include <compat/linux/linux_mib.h>
   82 #include <compat/linux/linux_socket.h>
   83 #include <compat/linux/linux_util.h>
   84 
   85 #include <compat/linux/linux_videodev.h>
   86 #include <compat/linux/linux_videodev_compat.h>
   87 
   88 #include <compat/linux/linux_videodev2.h>
   89 #include <compat/linux/linux_videodev2_compat.h>
   90 
   91 CTASSERT(LINUX_IFNAMSIZ == IFNAMSIZ);
   92 
   93 FEATURE(linuxulator_v4l, "V4L ioctl wrapper support in the linuxulator");
   94 FEATURE(linuxulator_v4l2, "V4L2 ioctl wrapper support in the linuxulator");
   95 
   96 static linux_ioctl_function_t linux_ioctl_cdrom;
   97 static linux_ioctl_function_t linux_ioctl_vfat;
   98 static linux_ioctl_function_t linux_ioctl_console;
   99 static linux_ioctl_function_t linux_ioctl_hdio;
  100 static linux_ioctl_function_t linux_ioctl_disk;
  101 static linux_ioctl_function_t linux_ioctl_socket;
  102 static linux_ioctl_function_t linux_ioctl_sound;
  103 static linux_ioctl_function_t linux_ioctl_termio;
  104 static linux_ioctl_function_t linux_ioctl_private;
  105 static linux_ioctl_function_t linux_ioctl_drm;
  106 static linux_ioctl_function_t linux_ioctl_sg;
  107 static linux_ioctl_function_t linux_ioctl_v4l;
  108 static linux_ioctl_function_t linux_ioctl_v4l2;
  109 static linux_ioctl_function_t linux_ioctl_special;
  110 
  111 static struct linux_ioctl_handler cdrom_handler =
  112 { linux_ioctl_cdrom, LINUX_IOCTL_CDROM_MIN, LINUX_IOCTL_CDROM_MAX };
  113 static struct linux_ioctl_handler vfat_handler =
  114 { linux_ioctl_vfat, LINUX_IOCTL_VFAT_MIN, LINUX_IOCTL_VFAT_MAX };
  115 static struct linux_ioctl_handler console_handler =
  116 { linux_ioctl_console, LINUX_IOCTL_CONSOLE_MIN, LINUX_IOCTL_CONSOLE_MAX };
  117 static struct linux_ioctl_handler hdio_handler =
  118 { linux_ioctl_hdio, LINUX_IOCTL_HDIO_MIN, LINUX_IOCTL_HDIO_MAX };
  119 static struct linux_ioctl_handler disk_handler =
  120 { linux_ioctl_disk, LINUX_IOCTL_DISK_MIN, LINUX_IOCTL_DISK_MAX };
  121 static struct linux_ioctl_handler socket_handler =
  122 { linux_ioctl_socket, LINUX_IOCTL_SOCKET_MIN, LINUX_IOCTL_SOCKET_MAX };
  123 static struct linux_ioctl_handler sound_handler =
  124 { linux_ioctl_sound, LINUX_IOCTL_SOUND_MIN, LINUX_IOCTL_SOUND_MAX };
  125 static struct linux_ioctl_handler termio_handler =
  126 { linux_ioctl_termio, LINUX_IOCTL_TERMIO_MIN, LINUX_IOCTL_TERMIO_MAX };
  127 static struct linux_ioctl_handler private_handler =
  128 { linux_ioctl_private, LINUX_IOCTL_PRIVATE_MIN, LINUX_IOCTL_PRIVATE_MAX };
  129 static struct linux_ioctl_handler drm_handler =
  130 { linux_ioctl_drm, LINUX_IOCTL_DRM_MIN, LINUX_IOCTL_DRM_MAX };
  131 static struct linux_ioctl_handler sg_handler =
  132 { linux_ioctl_sg, LINUX_IOCTL_SG_MIN, LINUX_IOCTL_SG_MAX };
  133 static struct linux_ioctl_handler video_handler =
  134 { linux_ioctl_v4l, LINUX_IOCTL_VIDEO_MIN, LINUX_IOCTL_VIDEO_MAX };
  135 static struct linux_ioctl_handler video2_handler =
  136 { linux_ioctl_v4l2, LINUX_IOCTL_VIDEO2_MIN, LINUX_IOCTL_VIDEO2_MAX };
  137 
  138 DATA_SET(linux_ioctl_handler_set, cdrom_handler);
  139 DATA_SET(linux_ioctl_handler_set, vfat_handler);
  140 DATA_SET(linux_ioctl_handler_set, console_handler);
  141 DATA_SET(linux_ioctl_handler_set, hdio_handler);
  142 DATA_SET(linux_ioctl_handler_set, disk_handler);
  143 DATA_SET(linux_ioctl_handler_set, socket_handler);
  144 DATA_SET(linux_ioctl_handler_set, sound_handler);
  145 DATA_SET(linux_ioctl_handler_set, termio_handler);
  146 DATA_SET(linux_ioctl_handler_set, private_handler);
  147 DATA_SET(linux_ioctl_handler_set, drm_handler);
  148 DATA_SET(linux_ioctl_handler_set, sg_handler);
  149 DATA_SET(linux_ioctl_handler_set, video_handler);
  150 DATA_SET(linux_ioctl_handler_set, video2_handler);
  151 
  152 struct handler_element
  153 {
  154         TAILQ_ENTRY(handler_element) list;
  155         int     (*func)(struct thread *, struct linux_ioctl_args *);
  156         int     low, high, span;
  157 };
  158 
  159 static TAILQ_HEAD(, handler_element) handlers =
  160     TAILQ_HEAD_INITIALIZER(handlers);
  161 static struct sx linux_ioctl_sx;
  162 SX_SYSINIT(linux_ioctl, &linux_ioctl_sx, "linux ioctl handlers");
  163 
  164 /*
  165  * hdio related ioctls for VMWare support
  166  */
  167 
  168 struct linux_hd_geometry {
  169         u_int8_t        heads;
  170         u_int8_t        sectors;
  171         u_int16_t       cylinders;
  172         u_int32_t       start;
  173 };
  174 
  175 struct linux_hd_big_geometry {
  176         u_int8_t        heads;
  177         u_int8_t        sectors;
  178         u_int32_t       cylinders;
  179         u_int32_t       start;
  180 };
  181 
  182 static int
  183 linux_ioctl_hdio(struct thread *td, struct linux_ioctl_args *args)
  184 {
  185         struct file *fp;
  186         int error;
  187         u_int sectorsize, fwcylinders, fwheads, fwsectors;
  188         off_t mediasize, bytespercyl;
  189 
  190         if ((error = fget(td, args->fd, &fp)) != 0)
  191                 return (error);
  192         switch (args->cmd & 0xffff) {
  193         case LINUX_HDIO_GET_GEO:
  194         case LINUX_HDIO_GET_GEO_BIG:
  195                 error = fo_ioctl(fp, DIOCGMEDIASIZE,
  196                         (caddr_t)&mediasize, td->td_ucred, td);
  197                 if (!error)
  198                         error = fo_ioctl(fp, DIOCGSECTORSIZE,
  199                                 (caddr_t)&sectorsize, td->td_ucred, td);
  200                 if (!error)
  201                         error = fo_ioctl(fp, DIOCGFWHEADS,
  202                                 (caddr_t)&fwheads, td->td_ucred, td);
  203                 if (!error)
  204                         error = fo_ioctl(fp, DIOCGFWSECTORS,
  205                                 (caddr_t)&fwsectors, td->td_ucred, td);
  206                 /*
  207                  * XXX: DIOCGFIRSTOFFSET is not yet implemented, so
  208                  * so pretend that GEOM always says 0. This is NOT VALID
  209                  * for slices or partitions, only the per-disk raw devices.
  210                  */
  211 
  212                 fdrop(fp, td);
  213                 if (error)
  214                         return (error);
  215                 /*
  216                  * 1. Calculate the number of bytes in a cylinder,
  217                  *    given the firmware's notion of heads and sectors
  218                  *    per cylinder.
  219                  * 2. Calculate the number of cylinders, given the total
  220                  *    size of the media.
  221                  * All internal calculations should have 64-bit precision.
  222                  */
  223                 bytespercyl = (off_t) sectorsize * fwheads * fwsectors;
  224                 fwcylinders = mediasize / bytespercyl;
  225 #if defined(DEBUG)
  226                 linux_msg(td, "HDIO_GET_GEO: mediasize %jd, c/h/s %d/%d/%d, "
  227                           "bpc %jd",
  228                           (intmax_t)mediasize, fwcylinders, fwheads, fwsectors, 
  229                           (intmax_t)bytespercyl);
  230 #endif
  231                 if ((args->cmd & 0xffff) == LINUX_HDIO_GET_GEO) {
  232                         struct linux_hd_geometry hdg;
  233 
  234                         hdg.cylinders = fwcylinders;
  235                         hdg.heads = fwheads;
  236                         hdg.sectors = fwsectors;
  237                         hdg.start = 0;
  238                         error = copyout(&hdg, (void *)args->arg, sizeof(hdg));
  239                 } else if ((args->cmd & 0xffff) == LINUX_HDIO_GET_GEO_BIG) {
  240                         struct linux_hd_big_geometry hdbg;
  241 
  242                         hdbg.cylinders = fwcylinders;
  243                         hdbg.heads = fwheads;
  244                         hdbg.sectors = fwsectors;
  245                         hdbg.start = 0;
  246                         error = copyout(&hdbg, (void *)args->arg, sizeof(hdbg));
  247                 }
  248                 return (error);
  249                 break;
  250         default:
  251                 /* XXX */
  252                 linux_msg(td,
  253                         "ioctl fd=%d, cmd=0x%x ('%c',%d) is not implemented",
  254                         args->fd, (int)(args->cmd & 0xffff),
  255                         (int)(args->cmd & 0xff00) >> 8,
  256                         (int)(args->cmd & 0xff));
  257                 break;
  258         }
  259         fdrop(fp, td);
  260         return (ENOIOCTL);
  261 }
  262 
  263 static int
  264 linux_ioctl_disk(struct thread *td, struct linux_ioctl_args *args)
  265 {
  266         struct file *fp;
  267         int error;
  268         u_int sectorsize;
  269         off_t mediasize;
  270 
  271         if ((error = fget(td, args->fd, &fp)) != 0)
  272                 return (error);
  273         switch (args->cmd & 0xffff) {
  274         case LINUX_BLKGETSIZE:
  275                 error = fo_ioctl(fp, DIOCGSECTORSIZE,
  276                     (caddr_t)&sectorsize, td->td_ucred, td);
  277                 if (!error)
  278                         error = fo_ioctl(fp, DIOCGMEDIASIZE,
  279                             (caddr_t)&mediasize, td->td_ucred, td);
  280                 fdrop(fp, td);
  281                 if (error)
  282                         return (error);
  283                 sectorsize = mediasize / sectorsize;
  284                 /*
  285                  * XXX: How do we know we return the right size of integer ?
  286                  */
  287                 return (copyout(&sectorsize, (void *)args->arg,
  288                     sizeof(sectorsize)));
  289                 break;
  290         }
  291         fdrop(fp, td);
  292         return (ENOIOCTL);
  293 }
  294 
  295 /*
  296  * termio related ioctls
  297  */
  298 
  299 struct linux_termio {
  300         unsigned short c_iflag;
  301         unsigned short c_oflag;
  302         unsigned short c_cflag;
  303         unsigned short c_lflag;
  304         unsigned char c_line;
  305         unsigned char c_cc[LINUX_NCC];
  306 };
  307 
  308 struct linux_termios {
  309         unsigned int c_iflag;
  310         unsigned int c_oflag;
  311         unsigned int c_cflag;
  312         unsigned int c_lflag;
  313         unsigned char c_line;
  314         unsigned char c_cc[LINUX_NCCS];
  315 };
  316 
  317 struct linux_winsize {
  318         unsigned short ws_row, ws_col;
  319         unsigned short ws_xpixel, ws_ypixel;
  320 };
  321 
  322 struct speedtab {
  323         int sp_speed;                   /* Speed. */
  324         int sp_code;                    /* Code. */
  325 };
  326 
  327 static struct speedtab sptab[] = {
  328         { B0, LINUX_B0 }, { B50, LINUX_B50 },
  329         { B75, LINUX_B75 }, { B110, LINUX_B110 },
  330         { B134, LINUX_B134 }, { B150, LINUX_B150 },
  331         { B200, LINUX_B200 }, { B300, LINUX_B300 },
  332         { B600, LINUX_B600 }, { B1200, LINUX_B1200 },
  333         { B1800, LINUX_B1800 }, { B2400, LINUX_B2400 },
  334         { B4800, LINUX_B4800 }, { B9600, LINUX_B9600 },
  335         { B19200, LINUX_B19200 }, { B38400, LINUX_B38400 },
  336         { B57600, LINUX_B57600 }, { B115200, LINUX_B115200 },
  337         {-1, -1 }
  338 };
  339 
  340 struct linux_serial_struct {
  341         int     type;
  342         int     line;
  343         int     port;
  344         int     irq;
  345         int     flags;
  346         int     xmit_fifo_size;
  347         int     custom_divisor;
  348         int     baud_base;
  349         unsigned short close_delay;
  350         char    reserved_char[2];
  351         int     hub6;
  352         unsigned short closing_wait;
  353         unsigned short closing_wait2;
  354         int     reserved[4];
  355 };
  356 
  357 static int
  358 linux_to_bsd_speed(int code, struct speedtab *table)
  359 {
  360         for ( ; table->sp_code != -1; table++)
  361                 if (table->sp_code == code)
  362                         return (table->sp_speed);
  363         return -1;
  364 }
  365 
  366 static int
  367 bsd_to_linux_speed(int speed, struct speedtab *table)
  368 {
  369         for ( ; table->sp_speed != -1; table++)
  370                 if (table->sp_speed == speed)
  371                         return (table->sp_code);
  372         return -1;
  373 }
  374 
  375 static void
  376 bsd_to_linux_termios(struct termios *bios, struct linux_termios *lios)
  377 {
  378         int i;
  379 
  380 #ifdef DEBUG
  381         if (ldebug(ioctl)) {
  382                 printf("LINUX: BSD termios structure (input):\n");
  383                 printf("i=%08x o=%08x c=%08x l=%08x ispeed=%d ospeed=%d\n",
  384                     bios->c_iflag, bios->c_oflag, bios->c_cflag, bios->c_lflag,
  385                     bios->c_ispeed, bios->c_ospeed);
  386                 printf("c_cc ");
  387                 for (i=0; i<NCCS; i++)
  388                         printf("%02x ", bios->c_cc[i]);
  389                 printf("\n");
  390         }
  391 #endif
  392 
  393         lios->c_iflag = 0;
  394         if (bios->c_iflag & IGNBRK)
  395                 lios->c_iflag |= LINUX_IGNBRK;
  396         if (bios->c_iflag & BRKINT)
  397                 lios->c_iflag |= LINUX_BRKINT;
  398         if (bios->c_iflag & IGNPAR)
  399                 lios->c_iflag |= LINUX_IGNPAR;
  400         if (bios->c_iflag & PARMRK)
  401                 lios->c_iflag |= LINUX_PARMRK;
  402         if (bios->c_iflag & INPCK)
  403                 lios->c_iflag |= LINUX_INPCK;
  404         if (bios->c_iflag & ISTRIP)
  405                 lios->c_iflag |= LINUX_ISTRIP;
  406         if (bios->c_iflag & INLCR)
  407                 lios->c_iflag |= LINUX_INLCR;
  408         if (bios->c_iflag & IGNCR)
  409                 lios->c_iflag |= LINUX_IGNCR;
  410         if (bios->c_iflag & ICRNL)
  411                 lios->c_iflag |= LINUX_ICRNL;
  412         if (bios->c_iflag & IXON)
  413                 lios->c_iflag |= LINUX_IXON;
  414         if (bios->c_iflag & IXANY)
  415                 lios->c_iflag |= LINUX_IXANY;
  416         if (bios->c_iflag & IXOFF)
  417                 lios->c_iflag |= LINUX_IXOFF;
  418         if (bios->c_iflag & IMAXBEL)
  419                 lios->c_iflag |= LINUX_IMAXBEL;
  420 
  421         lios->c_oflag = 0;
  422         if (bios->c_oflag & OPOST)
  423                 lios->c_oflag |= LINUX_OPOST;
  424         if (bios->c_oflag & ONLCR)
  425                 lios->c_oflag |= LINUX_ONLCR;
  426         if (bios->c_oflag & TAB3)
  427                 lios->c_oflag |= LINUX_XTABS;
  428 
  429         lios->c_cflag = bsd_to_linux_speed(bios->c_ispeed, sptab);
  430         lios->c_cflag |= (bios->c_cflag & CSIZE) >> 4;
  431         if (bios->c_cflag & CSTOPB)
  432                 lios->c_cflag |= LINUX_CSTOPB;
  433         if (bios->c_cflag & CREAD)
  434                 lios->c_cflag |= LINUX_CREAD;
  435         if (bios->c_cflag & PARENB)
  436                 lios->c_cflag |= LINUX_PARENB;
  437         if (bios->c_cflag & PARODD)
  438                 lios->c_cflag |= LINUX_PARODD;
  439         if (bios->c_cflag & HUPCL)
  440                 lios->c_cflag |= LINUX_HUPCL;
  441         if (bios->c_cflag & CLOCAL)
  442                 lios->c_cflag |= LINUX_CLOCAL;
  443         if (bios->c_cflag & CRTSCTS)
  444                 lios->c_cflag |= LINUX_CRTSCTS;
  445 
  446         lios->c_lflag = 0;
  447         if (bios->c_lflag & ISIG)
  448                 lios->c_lflag |= LINUX_ISIG;
  449         if (bios->c_lflag & ICANON)
  450                 lios->c_lflag |= LINUX_ICANON;
  451         if (bios->c_lflag & ECHO)
  452                 lios->c_lflag |= LINUX_ECHO;
  453         if (bios->c_lflag & ECHOE)
  454                 lios->c_lflag |= LINUX_ECHOE;
  455         if (bios->c_lflag & ECHOK)
  456                 lios->c_lflag |= LINUX_ECHOK;
  457         if (bios->c_lflag & ECHONL)
  458                 lios->c_lflag |= LINUX_ECHONL;
  459         if (bios->c_lflag & NOFLSH)
  460                 lios->c_lflag |= LINUX_NOFLSH;
  461         if (bios->c_lflag & TOSTOP)
  462                 lios->c_lflag |= LINUX_TOSTOP;
  463         if (bios->c_lflag & ECHOCTL)
  464                 lios->c_lflag |= LINUX_ECHOCTL;
  465         if (bios->c_lflag & ECHOPRT)
  466                 lios->c_lflag |= LINUX_ECHOPRT;
  467         if (bios->c_lflag & ECHOKE)
  468                 lios->c_lflag |= LINUX_ECHOKE;
  469         if (bios->c_lflag & FLUSHO)
  470                 lios->c_lflag |= LINUX_FLUSHO;
  471         if (bios->c_lflag & PENDIN)
  472                 lios->c_lflag |= LINUX_PENDIN;
  473         if (bios->c_lflag & IEXTEN)
  474                 lios->c_lflag |= LINUX_IEXTEN;
  475 
  476         for (i=0; i<LINUX_NCCS; i++)
  477                 lios->c_cc[i] = LINUX_POSIX_VDISABLE;
  478         lios->c_cc[LINUX_VINTR] = bios->c_cc[VINTR];
  479         lios->c_cc[LINUX_VQUIT] = bios->c_cc[VQUIT];
  480         lios->c_cc[LINUX_VERASE] = bios->c_cc[VERASE];
  481         lios->c_cc[LINUX_VKILL] = bios->c_cc[VKILL];
  482         lios->c_cc[LINUX_VEOF] = bios->c_cc[VEOF];
  483         lios->c_cc[LINUX_VEOL] = bios->c_cc[VEOL];
  484         lios->c_cc[LINUX_VMIN] = bios->c_cc[VMIN];
  485         lios->c_cc[LINUX_VTIME] = bios->c_cc[VTIME];
  486         lios->c_cc[LINUX_VEOL2] = bios->c_cc[VEOL2];
  487         lios->c_cc[LINUX_VSUSP] = bios->c_cc[VSUSP];
  488         lios->c_cc[LINUX_VSTART] = bios->c_cc[VSTART];
  489         lios->c_cc[LINUX_VSTOP] = bios->c_cc[VSTOP];
  490         lios->c_cc[LINUX_VREPRINT] = bios->c_cc[VREPRINT];
  491         lios->c_cc[LINUX_VDISCARD] = bios->c_cc[VDISCARD];
  492         lios->c_cc[LINUX_VWERASE] = bios->c_cc[VWERASE];
  493         lios->c_cc[LINUX_VLNEXT] = bios->c_cc[VLNEXT];
  494 
  495         for (i=0; i<LINUX_NCCS; i++) {
  496                 if (i != LINUX_VMIN && i != LINUX_VTIME &&
  497                     lios->c_cc[i] == _POSIX_VDISABLE)
  498                         lios->c_cc[i] = LINUX_POSIX_VDISABLE;
  499         }
  500         lios->c_line = 0;
  501 
  502 #ifdef DEBUG
  503         if (ldebug(ioctl)) {
  504                 printf("LINUX: LINUX termios structure (output):\n");
  505                 printf("i=%08x o=%08x c=%08x l=%08x line=%d\n",
  506                     lios->c_iflag, lios->c_oflag, lios->c_cflag,
  507                     lios->c_lflag, (int)lios->c_line);
  508                 printf("c_cc ");
  509                 for (i=0; i<LINUX_NCCS; i++)
  510                         printf("%02x ", lios->c_cc[i]);
  511                 printf("\n");
  512         }
  513 #endif
  514 }
  515 
  516 static void
  517 linux_to_bsd_termios(struct linux_termios *lios, struct termios *bios)
  518 {
  519         int i;
  520 
  521 #ifdef DEBUG
  522         if (ldebug(ioctl)) {
  523                 printf("LINUX: LINUX termios structure (input):\n");
  524                 printf("i=%08x o=%08x c=%08x l=%08x line=%d\n",
  525                     lios->c_iflag, lios->c_oflag, lios->c_cflag,
  526                     lios->c_lflag, (int)lios->c_line);
  527                 printf("c_cc ");
  528                 for (i=0; i<LINUX_NCCS; i++)
  529                         printf("%02x ", lios->c_cc[i]);
  530                 printf("\n");
  531         }
  532 #endif
  533 
  534         bios->c_iflag = 0;
  535         if (lios->c_iflag & LINUX_IGNBRK)
  536                 bios->c_iflag |= IGNBRK;
  537         if (lios->c_iflag & LINUX_BRKINT)
  538                 bios->c_iflag |= BRKINT;
  539         if (lios->c_iflag & LINUX_IGNPAR)
  540                 bios->c_iflag |= IGNPAR;
  541         if (lios->c_iflag & LINUX_PARMRK)
  542                 bios->c_iflag |= PARMRK;
  543         if (lios->c_iflag & LINUX_INPCK)
  544                 bios->c_iflag |= INPCK;
  545         if (lios->c_iflag & LINUX_ISTRIP)
  546                 bios->c_iflag |= ISTRIP;
  547         if (lios->c_iflag & LINUX_INLCR)
  548                 bios->c_iflag |= INLCR;
  549         if (lios->c_iflag & LINUX_IGNCR)
  550                 bios->c_iflag |= IGNCR;
  551         if (lios->c_iflag & LINUX_ICRNL)
  552                 bios->c_iflag |= ICRNL;
  553         if (lios->c_iflag & LINUX_IXON)
  554                 bios->c_iflag |= IXON;
  555         if (lios->c_iflag & LINUX_IXANY)
  556                 bios->c_iflag |= IXANY;
  557         if (lios->c_iflag & LINUX_IXOFF)
  558                 bios->c_iflag |= IXOFF;
  559         if (lios->c_iflag & LINUX_IMAXBEL)
  560                 bios->c_iflag |= IMAXBEL;
  561 
  562         bios->c_oflag = 0;
  563         if (lios->c_oflag & LINUX_OPOST)
  564                 bios->c_oflag |= OPOST;
  565         if (lios->c_oflag & LINUX_ONLCR)
  566                 bios->c_oflag |= ONLCR;
  567         if (lios->c_oflag & LINUX_XTABS)
  568                 bios->c_oflag |= TAB3;
  569 
  570         bios->c_cflag = (lios->c_cflag & LINUX_CSIZE) << 4;
  571         if (lios->c_cflag & LINUX_CSTOPB)
  572                 bios->c_cflag |= CSTOPB;
  573         if (lios->c_cflag & LINUX_CREAD)
  574                 bios->c_cflag |= CREAD;
  575         if (lios->c_cflag & LINUX_PARENB)
  576                 bios->c_cflag |= PARENB;
  577         if (lios->c_cflag & LINUX_PARODD)
  578                 bios->c_cflag |= PARODD;
  579         if (lios->c_cflag & LINUX_HUPCL)
  580                 bios->c_cflag |= HUPCL;
  581         if (lios->c_cflag & LINUX_CLOCAL)
  582                 bios->c_cflag |= CLOCAL;
  583         if (lios->c_cflag & LINUX_CRTSCTS)
  584                 bios->c_cflag |= CRTSCTS;
  585 
  586         bios->c_lflag = 0;
  587         if (lios->c_lflag & LINUX_ISIG)
  588                 bios->c_lflag |= ISIG;
  589         if (lios->c_lflag & LINUX_ICANON)
  590                 bios->c_lflag |= ICANON;
  591         if (lios->c_lflag & LINUX_ECHO)
  592                 bios->c_lflag |= ECHO;
  593         if (lios->c_lflag & LINUX_ECHOE)
  594                 bios->c_lflag |= ECHOE;
  595         if (lios->c_lflag & LINUX_ECHOK)
  596                 bios->c_lflag |= ECHOK;
  597         if (lios->c_lflag & LINUX_ECHONL)
  598                 bios->c_lflag |= ECHONL;
  599         if (lios->c_lflag & LINUX_NOFLSH)
  600                 bios->c_lflag |= NOFLSH;
  601         if (lios->c_lflag & LINUX_TOSTOP)
  602                 bios->c_lflag |= TOSTOP;
  603         if (lios->c_lflag & LINUX_ECHOCTL)
  604                 bios->c_lflag |= ECHOCTL;
  605         if (lios->c_lflag & LINUX_ECHOPRT)
  606                 bios->c_lflag |= ECHOPRT;
  607         if (lios->c_lflag & LINUX_ECHOKE)
  608                 bios->c_lflag |= ECHOKE;
  609         if (lios->c_lflag & LINUX_FLUSHO)
  610                 bios->c_lflag |= FLUSHO;
  611         if (lios->c_lflag & LINUX_PENDIN)
  612                 bios->c_lflag |= PENDIN;
  613         if (lios->c_lflag & LINUX_IEXTEN)
  614                 bios->c_lflag |= IEXTEN;
  615 
  616         for (i=0; i<NCCS; i++)
  617                 bios->c_cc[i] = _POSIX_VDISABLE;
  618         bios->c_cc[VINTR] = lios->c_cc[LINUX_VINTR];
  619         bios->c_cc[VQUIT] = lios->c_cc[LINUX_VQUIT];
  620         bios->c_cc[VERASE] = lios->c_cc[LINUX_VERASE];
  621         bios->c_cc[VKILL] = lios->c_cc[LINUX_VKILL];
  622         bios->c_cc[VEOF] = lios->c_cc[LINUX_VEOF];
  623         bios->c_cc[VEOL] = lios->c_cc[LINUX_VEOL];
  624         bios->c_cc[VMIN] = lios->c_cc[LINUX_VMIN];
  625         bios->c_cc[VTIME] = lios->c_cc[LINUX_VTIME];
  626         bios->c_cc[VEOL2] = lios->c_cc[LINUX_VEOL2];
  627         bios->c_cc[VSUSP] = lios->c_cc[LINUX_VSUSP];
  628         bios->c_cc[VSTART] = lios->c_cc[LINUX_VSTART];
  629         bios->c_cc[VSTOP] = lios->c_cc[LINUX_VSTOP];
  630         bios->c_cc[VREPRINT] = lios->c_cc[LINUX_VREPRINT];
  631         bios->c_cc[VDISCARD] = lios->c_cc[LINUX_VDISCARD];
  632         bios->c_cc[VWERASE] = lios->c_cc[LINUX_VWERASE];
  633         bios->c_cc[VLNEXT] = lios->c_cc[LINUX_VLNEXT];
  634 
  635         for (i=0; i<NCCS; i++) {
  636                 if (i != VMIN && i != VTIME &&
  637                     bios->c_cc[i] == LINUX_POSIX_VDISABLE)
  638                         bios->c_cc[i] = _POSIX_VDISABLE;
  639         }
  640 
  641         bios->c_ispeed = bios->c_ospeed =
  642             linux_to_bsd_speed(lios->c_cflag & LINUX_CBAUD, sptab);
  643 
  644 #ifdef DEBUG
  645         if (ldebug(ioctl)) {
  646                 printf("LINUX: BSD termios structure (output):\n");
  647                 printf("i=%08x o=%08x c=%08x l=%08x ispeed=%d ospeed=%d\n",
  648                     bios->c_iflag, bios->c_oflag, bios->c_cflag, bios->c_lflag,
  649                     bios->c_ispeed, bios->c_ospeed);
  650                 printf("c_cc ");
  651                 for (i=0; i<NCCS; i++)
  652                         printf("%02x ", bios->c_cc[i]);
  653                 printf("\n");
  654         }
  655 #endif
  656 }
  657 
  658 static void
  659 bsd_to_linux_termio(struct termios *bios, struct linux_termio *lio)
  660 {
  661         struct linux_termios lios;
  662 
  663         bsd_to_linux_termios(bios, &lios);
  664         lio->c_iflag = lios.c_iflag;
  665         lio->c_oflag = lios.c_oflag;
  666         lio->c_cflag = lios.c_cflag;
  667         lio->c_lflag = lios.c_lflag;
  668         lio->c_line  = lios.c_line;
  669         memcpy(lio->c_cc, lios.c_cc, LINUX_NCC);
  670 }
  671 
  672 static void
  673 linux_to_bsd_termio(struct linux_termio *lio, struct termios *bios)
  674 {
  675         struct linux_termios lios;
  676         int i;
  677 
  678         lios.c_iflag = lio->c_iflag;
  679         lios.c_oflag = lio->c_oflag;
  680         lios.c_cflag = lio->c_cflag;
  681         lios.c_lflag = lio->c_lflag;
  682         for (i=LINUX_NCC; i<LINUX_NCCS; i++)
  683                 lios.c_cc[i] = LINUX_POSIX_VDISABLE;
  684         memcpy(lios.c_cc, lio->c_cc, LINUX_NCC);
  685         linux_to_bsd_termios(&lios, bios);
  686 }
  687 
  688 static int
  689 linux_ioctl_termio(struct thread *td, struct linux_ioctl_args *args)
  690 {
  691         struct termios bios;
  692         struct linux_termios lios;
  693         struct linux_termio lio;
  694         struct file *fp;
  695         int error;
  696 
  697         if ((error = fget(td, args->fd, &fp)) != 0)
  698                 return (error);
  699 
  700         switch (args->cmd & 0xffff) {
  701 
  702         case LINUX_TCGETS:
  703                 error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios, td->td_ucred,
  704                     td);
  705                 if (error)
  706                         break;
  707                 bsd_to_linux_termios(&bios, &lios);
  708                 error = copyout(&lios, (void *)args->arg, sizeof(lios));
  709                 break;
  710 
  711         case LINUX_TCSETS:
  712                 error = copyin((void *)args->arg, &lios, sizeof(lios));
  713                 if (error)
  714                         break;
  715                 linux_to_bsd_termios(&lios, &bios);
  716                 error = (fo_ioctl(fp, TIOCSETA, (caddr_t)&bios, td->td_ucred,
  717                     td));
  718                 break;
  719 
  720         case LINUX_TCSETSW:
  721                 error = copyin((void *)args->arg, &lios, sizeof(lios));
  722                 if (error)
  723                         break;
  724                 linux_to_bsd_termios(&lios, &bios);
  725                 error = (fo_ioctl(fp, TIOCSETAW, (caddr_t)&bios, td->td_ucred,
  726                     td));
  727                 break;
  728 
  729         case LINUX_TCSETSF:
  730                 error = copyin((void *)args->arg, &lios, sizeof(lios));
  731                 if (error)
  732                         break;
  733                 linux_to_bsd_termios(&lios, &bios);
  734                 error = (fo_ioctl(fp, TIOCSETAF, (caddr_t)&bios, td->td_ucred,
  735                     td));
  736                 break;
  737 
  738         case LINUX_TCGETA:
  739                 error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios, td->td_ucred,
  740                     td);
  741                 if (error)
  742                         break;
  743                 bsd_to_linux_termio(&bios, &lio);
  744                 error = (copyout(&lio, (void *)args->arg, sizeof(lio)));
  745                 break;
  746 
  747         case LINUX_TCSETA:
  748                 error = copyin((void *)args->arg, &lio, sizeof(lio));
  749                 if (error)
  750                         break;
  751                 linux_to_bsd_termio(&lio, &bios);
  752                 error = (fo_ioctl(fp, TIOCSETA, (caddr_t)&bios, td->td_ucred,
  753                     td));
  754                 break;
  755 
  756         case LINUX_TCSETAW:
  757                 error = copyin((void *)args->arg, &lio, sizeof(lio));
  758                 if (error)
  759                         break;
  760                 linux_to_bsd_termio(&lio, &bios);
  761                 error = (fo_ioctl(fp, TIOCSETAW, (caddr_t)&bios, td->td_ucred,
  762                     td));
  763                 break;
  764 
  765         case LINUX_TCSETAF:
  766                 error = copyin((void *)args->arg, &lio, sizeof(lio));
  767                 if (error)
  768                         break;
  769                 linux_to_bsd_termio(&lio, &bios);
  770                 error = (fo_ioctl(fp, TIOCSETAF, (caddr_t)&bios, td->td_ucred,
  771                     td));
  772                 break;
  773 
  774         /* LINUX_TCSBRK */
  775 
  776         case LINUX_TCXONC: {
  777                 switch (args->arg) {
  778                 case LINUX_TCOOFF:
  779                         args->cmd = TIOCSTOP;
  780                         break;
  781                 case LINUX_TCOON:
  782                         args->cmd = TIOCSTART;
  783                         break;
  784                 case LINUX_TCIOFF:
  785                 case LINUX_TCION: {
  786                         int c;
  787                         struct write_args wr;
  788                         error = fo_ioctl(fp, TIOCGETA, (caddr_t)&bios,
  789                             td->td_ucred, td);
  790                         if (error)
  791                                 break;
  792                         fdrop(fp, td);
  793                         c = (args->arg == LINUX_TCIOFF) ? VSTOP : VSTART;
  794                         c = bios.c_cc[c];
  795                         if (c != _POSIX_VDISABLE) {
  796                                 wr.fd = args->fd;
  797                                 wr.buf = &c;
  798                                 wr.nbyte = sizeof(c);
  799                                 return (write(td, &wr));
  800                         } else
  801                                 return (0);
  802                 }
  803                 default:
  804                         fdrop(fp, td);
  805                         return (EINVAL);
  806                 }
  807                 args->arg = 0;
  808                 error = (ioctl(td, (struct ioctl_args *)args));
  809                 break;
  810         }
  811 
  812         case LINUX_TCFLSH: {
  813                 int val;
  814                 switch (args->arg) {
  815                 case LINUX_TCIFLUSH:
  816                         val = FREAD;
  817                         break;
  818                 case LINUX_TCOFLUSH:
  819                         val = FWRITE;
  820                         break;
  821                 case LINUX_TCIOFLUSH:
  822                         val = FREAD | FWRITE;
  823                         break;
  824                 default:
  825                         fdrop(fp, td);
  826                         return (EINVAL);
  827                 }
  828                 error = (fo_ioctl(fp,TIOCFLUSH,(caddr_t)&val,td->td_ucred,td));
  829                 break;
  830         }
  831 
  832         case LINUX_TIOCEXCL:
  833                 args->cmd = TIOCEXCL;
  834                 error = (ioctl(td, (struct ioctl_args *)args));
  835                 break;
  836 
  837         case LINUX_TIOCNXCL:
  838                 args->cmd = TIOCNXCL;
  839                 error = (ioctl(td, (struct ioctl_args *)args));
  840                 break;
  841 
  842         case LINUX_TIOCSCTTY:
  843                 args->cmd = TIOCSCTTY;
  844                 error = (ioctl(td, (struct ioctl_args *)args));
  845                 break;
  846 
  847         case LINUX_TIOCGPGRP:
  848                 args->cmd = TIOCGPGRP;
  849                 error = (ioctl(td, (struct ioctl_args *)args));
  850                 break;
  851 
  852         case LINUX_TIOCSPGRP:
  853                 args->cmd = TIOCSPGRP;
  854                 error = (ioctl(td, (struct ioctl_args *)args));
  855                 break;
  856 
  857         /* LINUX_TIOCOUTQ */
  858         /* LINUX_TIOCSTI */
  859 
  860         case LINUX_TIOCGWINSZ:
  861                 args->cmd = TIOCGWINSZ;
  862                 error = (ioctl(td, (struct ioctl_args *)args));
  863                 break;
  864 
  865         case LINUX_TIOCSWINSZ:
  866                 args->cmd = TIOCSWINSZ;
  867                 error = (ioctl(td, (struct ioctl_args *)args));
  868                 break;
  869 
  870         case LINUX_TIOCMGET:
  871                 args->cmd = TIOCMGET;
  872                 error = (ioctl(td, (struct ioctl_args *)args));
  873                 break;
  874 
  875         case LINUX_TIOCMBIS:
  876                 args->cmd = TIOCMBIS;
  877                 error = (ioctl(td, (struct ioctl_args *)args));
  878                 break;
  879 
  880         case LINUX_TIOCMBIC:
  881                 args->cmd = TIOCMBIC;
  882                 error = (ioctl(td, (struct ioctl_args *)args));
  883                 break;
  884 
  885         case LINUX_TIOCMSET:
  886                 args->cmd = TIOCMSET;
  887                 error = (ioctl(td, (struct ioctl_args *)args));
  888                 break;
  889 
  890         /* TIOCGSOFTCAR */
  891         /* TIOCSSOFTCAR */
  892 
  893         case LINUX_FIONREAD: /* LINUX_TIOCINQ */
  894                 args->cmd = FIONREAD;
  895                 error = (ioctl(td, (struct ioctl_args *)args));
  896                 break;
  897 
  898         /* LINUX_TIOCLINUX */
  899 
  900         case LINUX_TIOCCONS:
  901                 args->cmd = TIOCCONS;
  902                 error = (ioctl(td, (struct ioctl_args *)args));
  903                 break;
  904 
  905         case LINUX_TIOCGSERIAL: {
  906                 struct linux_serial_struct lss;
  907                 lss.type = LINUX_PORT_16550A;
  908                 lss.flags = 0;
  909                 lss.close_delay = 0;
  910                 error = copyout(&lss, (void *)args->arg, sizeof(lss));
  911                 break;
  912         }
  913 
  914         case LINUX_TIOCSSERIAL: {
  915                 struct linux_serial_struct lss;
  916                 error = copyin((void *)args->arg, &lss, sizeof(lss));
  917                 if (error)
  918                         break;
  919                 /* XXX - It really helps to have an implementation that
  920                  * does nothing. NOT!
  921                  */
  922                 error = 0;
  923                 break;
  924         }
  925 
  926         case LINUX_TIOCPKT:
  927                 args->cmd = TIOCPKT;
  928                 error = (ioctl(td, (struct ioctl_args *)args));
  929                 break;
  930 
  931         case LINUX_FIONBIO:
  932                 args->cmd = FIONBIO;
  933                 error = (ioctl(td, (struct ioctl_args *)args));
  934                 break;
  935 
  936         case LINUX_TIOCNOTTY:
  937                 args->cmd = TIOCNOTTY;
  938                 error = (ioctl(td, (struct ioctl_args *)args));
  939                 break;
  940 
  941         case LINUX_TIOCSETD: {
  942                 int line;
  943                 switch (args->arg) {
  944                 case LINUX_N_TTY:
  945                         line = TTYDISC;
  946                         break;
  947                 case LINUX_N_SLIP:
  948                         line = SLIPDISC;
  949                         break;
  950                 case LINUX_N_PPP:
  951                         line = PPPDISC;
  952                         break;
  953                 default:
  954                         fdrop(fp, td);
  955                         return (EINVAL);
  956                 }
  957                 error = (fo_ioctl(fp, TIOCSETD, (caddr_t)&line, td->td_ucred,
  958                     td));
  959                 break;
  960         }
  961 
  962         case LINUX_TIOCGETD: {
  963                 int linux_line;
  964                 int bsd_line = TTYDISC;
  965                 error = fo_ioctl(fp, TIOCGETD, (caddr_t)&bsd_line,
  966                     td->td_ucred, td);
  967                 if (error)
  968                         return (error);
  969                 switch (bsd_line) {
  970                 case TTYDISC:
  971                         linux_line = LINUX_N_TTY;
  972                         break;
  973                 case SLIPDISC:
  974                         linux_line = LINUX_N_SLIP;
  975                         break;
  976                 case PPPDISC:
  977                         linux_line = LINUX_N_PPP;
  978                         break;
  979                 default:
  980                         fdrop(fp, td);
  981                         return (EINVAL);
  982                 }
  983                 error = (copyout(&linux_line, (void *)args->arg, sizeof(int)));
  984                 break;
  985         }
  986 
  987         /* LINUX_TCSBRKP */
  988         /* LINUX_TIOCTTYGSTRUCT */
  989 
  990         case LINUX_FIONCLEX:
  991                 args->cmd = FIONCLEX;
  992                 error = (ioctl(td, (struct ioctl_args *)args));
  993                 break;
  994 
  995         case LINUX_FIOCLEX:
  996                 args->cmd = FIOCLEX;
  997                 error = (ioctl(td, (struct ioctl_args *)args));
  998                 break;
  999 
 1000         case LINUX_FIOASYNC:
 1001                 args->cmd = FIOASYNC;
 1002                 error = (ioctl(td, (struct ioctl_args *)args));
 1003                 break;
 1004 
 1005         /* LINUX_TIOCSERCONFIG */
 1006         /* LINUX_TIOCSERGWILD */
 1007         /* LINUX_TIOCSERSWILD */
 1008         /* LINUX_TIOCGLCKTRMIOS */
 1009         /* LINUX_TIOCSLCKTRMIOS */
 1010 
 1011         case LINUX_TIOCSBRK:
 1012                 args->cmd = TIOCSBRK;
 1013                 error = (ioctl(td, (struct ioctl_args *)args));
 1014                 break;
 1015 
 1016         case LINUX_TIOCCBRK:
 1017                 args->cmd = TIOCCBRK;
 1018                 error = (ioctl(td, (struct ioctl_args *)args));
 1019                 break;
 1020         case LINUX_TIOCGPTN: {
 1021                 int nb;
 1022                 
 1023                 error = fo_ioctl(fp, TIOCGPTN, (caddr_t)&nb, td->td_ucred, td);
 1024                 if (!error)
 1025                         error = copyout(&nb, (void *)args->arg,
 1026                             sizeof(int));
 1027                 break;
 1028         }
 1029         case LINUX_TIOCSPTLCK:
 1030                 /* Our unlockpt() does nothing. */
 1031                 error = 0;
 1032                 break;
 1033         default:
 1034                 error = ENOIOCTL;
 1035                 break;
 1036         }
 1037 
 1038         fdrop(fp, td);
 1039         return (error);
 1040 }
 1041 
 1042 /*
 1043  * CDROM related ioctls
 1044  */
 1045 
 1046 struct linux_cdrom_msf
 1047 {
 1048         u_char  cdmsf_min0;
 1049         u_char  cdmsf_sec0;
 1050         u_char  cdmsf_frame0;
 1051         u_char  cdmsf_min1;
 1052         u_char  cdmsf_sec1;
 1053         u_char  cdmsf_frame1;
 1054 };
 1055 
 1056 struct linux_cdrom_tochdr
 1057 {
 1058         u_char  cdth_trk0;
 1059         u_char  cdth_trk1;
 1060 };
 1061 
 1062 union linux_cdrom_addr
 1063 {
 1064         struct {
 1065                 u_char  minute;
 1066                 u_char  second;
 1067                 u_char  frame;
 1068         } msf;
 1069         int     lba;
 1070 };
 1071 
 1072 struct linux_cdrom_tocentry
 1073 {
 1074         u_char  cdte_track;
 1075         u_char  cdte_adr:4;
 1076         u_char  cdte_ctrl:4;
 1077         u_char  cdte_format;
 1078         union linux_cdrom_addr cdte_addr;
 1079         u_char  cdte_datamode;
 1080 };
 1081 
 1082 struct linux_cdrom_subchnl
 1083 {
 1084         u_char  cdsc_format;
 1085         u_char  cdsc_audiostatus;
 1086         u_char  cdsc_adr:4;
 1087         u_char  cdsc_ctrl:4;
 1088         u_char  cdsc_trk;
 1089         u_char  cdsc_ind;
 1090         union linux_cdrom_addr cdsc_absaddr;
 1091         union linux_cdrom_addr cdsc_reladdr;
 1092 };
 1093 
 1094 struct l_cdrom_read_audio {
 1095         union linux_cdrom_addr addr;
 1096         u_char          addr_format;
 1097         l_int           nframes;
 1098         u_char          *buf;
 1099 };
 1100 
 1101 struct l_dvd_layer {
 1102         u_char          book_version:4;
 1103         u_char          book_type:4;
 1104         u_char          min_rate:4;
 1105         u_char          disc_size:4;
 1106         u_char          layer_type:4;
 1107         u_char          track_path:1;
 1108         u_char          nlayers:2;
 1109         u_char          track_density:4;
 1110         u_char          linear_density:4;
 1111         u_char          bca:1;
 1112         u_int32_t       start_sector;
 1113         u_int32_t       end_sector;
 1114         u_int32_t       end_sector_l0;
 1115 };
 1116 
 1117 struct l_dvd_physical {
 1118         u_char          type;
 1119         u_char          layer_num;
 1120         struct l_dvd_layer layer[4];
 1121 };
 1122 
 1123 struct l_dvd_copyright {
 1124         u_char          type;
 1125         u_char          layer_num;
 1126         u_char          cpst;
 1127         u_char          rmi;
 1128 };
 1129 
 1130 struct l_dvd_disckey {
 1131         u_char          type;
 1132         l_uint          agid:2;
 1133         u_char          value[2048];
 1134 };
 1135 
 1136 struct l_dvd_bca {
 1137         u_char          type;
 1138         l_int           len;
 1139         u_char          value[188];
 1140 };
 1141 
 1142 struct l_dvd_manufact {
 1143         u_char          type;
 1144         u_char          layer_num;
 1145         l_int           len;
 1146         u_char          value[2048];
 1147 };
 1148 
 1149 typedef union {
 1150         u_char                  type;
 1151         struct l_dvd_physical   physical;
 1152         struct l_dvd_copyright  copyright;
 1153         struct l_dvd_disckey    disckey;
 1154         struct l_dvd_bca        bca;
 1155         struct l_dvd_manufact   manufact;
 1156 } l_dvd_struct;
 1157 
 1158 typedef u_char l_dvd_key[5];
 1159 typedef u_char l_dvd_challenge[10];
 1160 
 1161 struct l_dvd_lu_send_agid {
 1162         u_char          type;
 1163         l_uint          agid:2;
 1164 };
 1165 
 1166 struct l_dvd_host_send_challenge {
 1167         u_char          type;
 1168         l_uint          agid:2;
 1169         l_dvd_challenge chal;
 1170 };
 1171 
 1172 struct l_dvd_send_key {
 1173         u_char          type;
 1174         l_uint          agid:2;
 1175         l_dvd_key       key;
 1176 };
 1177 
 1178 struct l_dvd_lu_send_challenge {
 1179         u_char          type;
 1180         l_uint          agid:2;
 1181         l_dvd_challenge chal;
 1182 };
 1183 
 1184 struct l_dvd_lu_send_title_key {
 1185         u_char          type;
 1186         l_uint          agid:2;
 1187         l_dvd_key       title_key;
 1188         l_int           lba;
 1189         l_uint          cpm:1;
 1190         l_uint          cp_sec:1;
 1191         l_uint          cgms:2;
 1192 };
 1193 
 1194 struct l_dvd_lu_send_asf {
 1195         u_char          type;
 1196         l_uint          agid:2;
 1197         l_uint          asf:1;
 1198 };
 1199 
 1200 struct l_dvd_host_send_rpcstate {
 1201         u_char          type;
 1202         u_char          pdrc;
 1203 };
 1204 
 1205 struct l_dvd_lu_send_rpcstate {
 1206         u_char          type:2;
 1207         u_char          vra:3;
 1208         u_char          ucca:3;
 1209         u_char          region_mask;
 1210         u_char          rpc_scheme;
 1211 };
 1212 
 1213 typedef union {
 1214         u_char                          type;
 1215         struct l_dvd_lu_send_agid       lsa;
 1216         struct l_dvd_host_send_challenge hsc;
 1217         struct l_dvd_send_key           lsk;
 1218         struct l_dvd_lu_send_challenge  lsc;
 1219         struct l_dvd_send_key           hsk;
 1220         struct l_dvd_lu_send_title_key  lstk;
 1221         struct l_dvd_lu_send_asf        lsasf;
 1222         struct l_dvd_host_send_rpcstate hrpcs;
 1223         struct l_dvd_lu_send_rpcstate   lrpcs;
 1224 } l_dvd_authinfo;
 1225 
 1226 static void
 1227 bsd_to_linux_msf_lba(u_char af, union msf_lba *bp, union linux_cdrom_addr *lp)
 1228 {
 1229         if (af == CD_LBA_FORMAT)
 1230                 lp->lba = bp->lba;
 1231         else {
 1232                 lp->msf.minute = bp->msf.minute;
 1233                 lp->msf.second = bp->msf.second;
 1234                 lp->msf.frame = bp->msf.frame;
 1235         }
 1236 }
 1237 
 1238 static void
 1239 set_linux_cdrom_addr(union linux_cdrom_addr *addr, int format, int lba)
 1240 {
 1241         if (format == LINUX_CDROM_MSF) {
 1242                 addr->msf.frame = lba % 75;
 1243                 lba /= 75;
 1244                 lba += 2;
 1245                 addr->msf.second = lba % 60;
 1246                 addr->msf.minute = lba / 60;
 1247         } else
 1248                 addr->lba = lba;
 1249 }
 1250 
 1251 static int
 1252 linux_to_bsd_dvd_struct(l_dvd_struct *lp, struct dvd_struct *bp)
 1253 {
 1254         bp->format = lp->type;
 1255         switch (bp->format) {
 1256         case DVD_STRUCT_PHYSICAL:
 1257                 if (bp->layer_num >= 4)
 1258                         return (EINVAL);
 1259                 bp->layer_num = lp->physical.layer_num;
 1260                 break;
 1261         case DVD_STRUCT_COPYRIGHT:
 1262                 bp->layer_num = lp->copyright.layer_num;
 1263                 break;
 1264         case DVD_STRUCT_DISCKEY:
 1265                 bp->agid = lp->disckey.agid;
 1266                 break;
 1267         case DVD_STRUCT_BCA:
 1268         case DVD_STRUCT_MANUFACT:
 1269                 break;
 1270         default:
 1271                 return (EINVAL);
 1272         }
 1273         return (0);
 1274 }
 1275 
 1276 static int
 1277 bsd_to_linux_dvd_struct(struct dvd_struct *bp, l_dvd_struct *lp)
 1278 {
 1279         switch (bp->format) {
 1280         case DVD_STRUCT_PHYSICAL: {
 1281                 struct dvd_layer *blp = (struct dvd_layer *)bp->data;
 1282                 struct l_dvd_layer *llp = &lp->physical.layer[bp->layer_num];
 1283                 memset(llp, 0, sizeof(*llp));
 1284                 llp->book_version = blp->book_version;
 1285                 llp->book_type = blp->book_type;
 1286                 llp->min_rate = blp->max_rate;
 1287                 llp->disc_size = blp->disc_size;
 1288                 llp->layer_type = blp->layer_type;
 1289                 llp->track_path = blp->track_path;
 1290                 llp->nlayers = blp->nlayers;
 1291                 llp->track_density = blp->track_density;
 1292                 llp->linear_density = blp->linear_density;
 1293                 llp->bca = blp->bca;
 1294                 llp->start_sector = blp->start_sector;
 1295                 llp->end_sector = blp->end_sector;
 1296                 llp->end_sector_l0 = blp->end_sector_l0;
 1297                 break;
 1298         }
 1299         case DVD_STRUCT_COPYRIGHT:
 1300                 lp->copyright.cpst = bp->cpst;
 1301                 lp->copyright.rmi = bp->rmi;
 1302                 break;
 1303         case DVD_STRUCT_DISCKEY:
 1304                 memcpy(lp->disckey.value, bp->data, sizeof(lp->disckey.value));
 1305                 break;
 1306         case DVD_STRUCT_BCA:
 1307                 lp->bca.len = bp->length;
 1308                 memcpy(lp->bca.value, bp->data, sizeof(lp->bca.value));
 1309                 break;
 1310         case DVD_STRUCT_MANUFACT:
 1311                 lp->manufact.len = bp->length;
 1312                 memcpy(lp->manufact.value, bp->data,
 1313                     sizeof(lp->manufact.value));
 1314                 /* lp->manufact.layer_num is unused in linux (redhat 7.0) */
 1315                 break;
 1316         default:
 1317                 return (EINVAL);
 1318         }
 1319         return (0);
 1320 }
 1321 
 1322 static int
 1323 linux_to_bsd_dvd_authinfo(l_dvd_authinfo *lp, int *bcode,
 1324     struct dvd_authinfo *bp)
 1325 {
 1326         switch (lp->type) {
 1327         case LINUX_DVD_LU_SEND_AGID:
 1328                 *bcode = DVDIOCREPORTKEY;
 1329                 bp->format = DVD_REPORT_AGID;
 1330                 bp->agid = lp->lsa.agid;
 1331                 break;
 1332         case LINUX_DVD_HOST_SEND_CHALLENGE:
 1333                 *bcode = DVDIOCSENDKEY;
 1334                 bp->format = DVD_SEND_CHALLENGE;
 1335                 bp->agid = lp->hsc.agid;
 1336                 memcpy(bp->keychal, lp->hsc.chal, 10);
 1337                 break;
 1338         case LINUX_DVD_LU_SEND_KEY1:
 1339                 *bcode = DVDIOCREPORTKEY;
 1340                 bp->format = DVD_REPORT_KEY1;
 1341                 bp->agid = lp->lsk.agid;
 1342                 break;
 1343         case LINUX_DVD_LU_SEND_CHALLENGE:
 1344                 *bcode = DVDIOCREPORTKEY;
 1345                 bp->format = DVD_REPORT_CHALLENGE;
 1346                 bp->agid = lp->lsc.agid;
 1347                 break;
 1348         case LINUX_DVD_HOST_SEND_KEY2:
 1349                 *bcode = DVDIOCSENDKEY;
 1350                 bp->format = DVD_SEND_KEY2;
 1351                 bp->agid = lp->hsk.agid;
 1352                 memcpy(bp->keychal, lp->hsk.key, 5);
 1353                 break;
 1354         case LINUX_DVD_LU_SEND_TITLE_KEY:
 1355                 *bcode = DVDIOCREPORTKEY;
 1356                 bp->format = DVD_REPORT_TITLE_KEY;
 1357                 bp->agid = lp->lstk.agid;
 1358                 bp->lba = lp->lstk.lba;
 1359                 break;
 1360         case LINUX_DVD_LU_SEND_ASF:
 1361                 *bcode = DVDIOCREPORTKEY;
 1362                 bp->format = DVD_REPORT_ASF;
 1363                 bp->agid = lp->lsasf.agid;
 1364                 break;
 1365         case LINUX_DVD_INVALIDATE_AGID:
 1366                 *bcode = DVDIOCREPORTKEY;
 1367                 bp->format = DVD_INVALIDATE_AGID;
 1368                 bp->agid = lp->lsa.agid;
 1369                 break;
 1370         case LINUX_DVD_LU_SEND_RPC_STATE:
 1371                 *bcode = DVDIOCREPORTKEY;
 1372                 bp->format = DVD_REPORT_RPC;
 1373                 break;
 1374         case LINUX_DVD_HOST_SEND_RPC_STATE:
 1375                 *bcode = DVDIOCSENDKEY;
 1376                 bp->format = DVD_SEND_RPC;
 1377                 bp->region = lp->hrpcs.pdrc;
 1378                 break;
 1379         default:
 1380                 return (EINVAL);
 1381         }
 1382         return (0);
 1383 }
 1384 
 1385 static int
 1386 bsd_to_linux_dvd_authinfo(struct dvd_authinfo *bp, l_dvd_authinfo *lp)
 1387 {
 1388         switch (lp->type) {
 1389         case LINUX_DVD_LU_SEND_AGID:
 1390                 lp->lsa.agid = bp->agid;
 1391                 break;
 1392         case LINUX_DVD_HOST_SEND_CHALLENGE:
 1393                 lp->type = LINUX_DVD_LU_SEND_KEY1;
 1394                 break;
 1395         case LINUX_DVD_LU_SEND_KEY1:
 1396                 memcpy(lp->lsk.key, bp->keychal, sizeof(lp->lsk.key));
 1397                 break;
 1398         case LINUX_DVD_LU_SEND_CHALLENGE:
 1399                 memcpy(lp->lsc.chal, bp->keychal, sizeof(lp->lsc.chal));
 1400                 break;
 1401         case LINUX_DVD_HOST_SEND_KEY2:
 1402                 lp->type = LINUX_DVD_AUTH_ESTABLISHED;
 1403                 break;
 1404         case LINUX_DVD_LU_SEND_TITLE_KEY:
 1405                 memcpy(lp->lstk.title_key, bp->keychal,
 1406                     sizeof(lp->lstk.title_key));
 1407                 lp->lstk.cpm = bp->cpm;
 1408                 lp->lstk.cp_sec = bp->cp_sec;
 1409                 lp->lstk.cgms = bp->cgms;
 1410                 break;
 1411         case LINUX_DVD_LU_SEND_ASF:
 1412                 lp->lsasf.asf = bp->asf;
 1413                 break;
 1414         case LINUX_DVD_INVALIDATE_AGID:
 1415                 break;
 1416         case LINUX_DVD_LU_SEND_RPC_STATE:
 1417                 lp->lrpcs.type = bp->reg_type;
 1418                 lp->lrpcs.vra = bp->vend_rsts;
 1419                 lp->lrpcs.ucca = bp->user_rsts;
 1420                 lp->lrpcs.region_mask = bp->region;
 1421                 lp->lrpcs.rpc_scheme = bp->rpc_scheme;
 1422                 break;
 1423         case LINUX_DVD_HOST_SEND_RPC_STATE:
 1424                 break;
 1425         default:
 1426                 return (EINVAL);
 1427         }
 1428         return (0);
 1429 }
 1430 
 1431 static int
 1432 linux_ioctl_cdrom(struct thread *td, struct linux_ioctl_args *args)
 1433 {
 1434         struct file *fp;
 1435         int error;
 1436 
 1437         if ((error = fget(td, args->fd, &fp)) != 0)
 1438                 return (error);
 1439         switch (args->cmd & 0xffff) {
 1440 
 1441         case LINUX_CDROMPAUSE:
 1442                 args->cmd = CDIOCPAUSE;
 1443                 error = (ioctl(td, (struct ioctl_args *)args));
 1444                 break;
 1445 
 1446         case LINUX_CDROMRESUME:
 1447                 args->cmd = CDIOCRESUME;
 1448                 error = (ioctl(td, (struct ioctl_args *)args));
 1449                 break;
 1450 
 1451         case LINUX_CDROMPLAYMSF:
 1452                 args->cmd = CDIOCPLAYMSF;
 1453                 error = (ioctl(td, (struct ioctl_args *)args));
 1454                 break;
 1455 
 1456         case LINUX_CDROMPLAYTRKIND:
 1457                 args->cmd = CDIOCPLAYTRACKS;
 1458                 error = (ioctl(td, (struct ioctl_args *)args));
 1459                 break;
 1460 
 1461         case LINUX_CDROMREADTOCHDR: {
 1462                 struct ioc_toc_header th;
 1463                 struct linux_cdrom_tochdr lth;
 1464                 error = fo_ioctl(fp, CDIOREADTOCHEADER, (caddr_t)&th,
 1465                     td->td_ucred, td);
 1466                 if (!error) {
 1467                         lth.cdth_trk0 = th.starting_track;
 1468                         lth.cdth_trk1 = th.ending_track;
 1469                         copyout(&lth, (void *)args->arg, sizeof(lth));
 1470                 }
 1471                 break;
 1472         }
 1473 
 1474         case LINUX_CDROMREADTOCENTRY: {
 1475                 struct linux_cdrom_tocentry lte;
 1476                 struct ioc_read_toc_single_entry irtse;
 1477 
 1478                 error = copyin((void *)args->arg, &lte, sizeof(lte));
 1479                 if (error)
 1480                         break;
 1481                 irtse.address_format = lte.cdte_format;
 1482                 irtse.track = lte.cdte_track;
 1483                 error = fo_ioctl(fp, CDIOREADTOCENTRY, (caddr_t)&irtse,
 1484                     td->td_ucred, td);
 1485                 if (!error) {
 1486                         lte.cdte_ctrl = irtse.entry.control;
 1487                         lte.cdte_adr = irtse.entry.addr_type;
 1488                         bsd_to_linux_msf_lba(irtse.address_format,
 1489                             &irtse.entry.addr, &lte.cdte_addr);
 1490                         error = copyout(&lte, (void *)args->arg, sizeof(lte));
 1491                 }
 1492                 break;
 1493         }
 1494 
 1495         case LINUX_CDROMSTOP:
 1496                 args->cmd = CDIOCSTOP;
 1497                 error = (ioctl(td, (struct ioctl_args *)args));
 1498                 break;
 1499 
 1500         case LINUX_CDROMSTART:
 1501                 args->cmd = CDIOCSTART;
 1502                 error = (ioctl(td, (struct ioctl_args *)args));
 1503                 break;
 1504 
 1505         case LINUX_CDROMEJECT:
 1506                 args->cmd = CDIOCEJECT;
 1507                 error = (ioctl(td, (struct ioctl_args *)args));
 1508                 break;
 1509 
 1510         /* LINUX_CDROMVOLCTRL */
 1511 
 1512         case LINUX_CDROMSUBCHNL: {
 1513                 struct linux_cdrom_subchnl sc;
 1514                 struct ioc_read_subchannel bsdsc;
 1515                 struct cd_sub_channel_info bsdinfo;
 1516 
 1517                 bsdsc.address_format = CD_LBA_FORMAT;
 1518                 bsdsc.data_format = CD_CURRENT_POSITION;
 1519                 bsdsc.track = 0;
 1520                 bsdsc.data_len = sizeof(bsdinfo);
 1521                 bsdsc.data = &bsdinfo;
 1522                 error = fo_ioctl(fp, CDIOCREADSUBCHANNEL_SYSSPACE,
 1523                     (caddr_t)&bsdsc, td->td_ucred, td);
 1524                 if (error)
 1525                         break;
 1526                 error = copyin((void *)args->arg, &sc, sizeof(sc));
 1527                 if (error)
 1528                         break;
 1529                 sc.cdsc_audiostatus = bsdinfo.header.audio_status;
 1530                 sc.cdsc_adr = bsdinfo.what.position.addr_type;
 1531                 sc.cdsc_ctrl = bsdinfo.what.position.control;
 1532                 sc.cdsc_trk = bsdinfo.what.position.track_number;
 1533                 sc.cdsc_ind = bsdinfo.what.position.index_number;
 1534                 set_linux_cdrom_addr(&sc.cdsc_absaddr, sc.cdsc_format,
 1535                     bsdinfo.what.position.absaddr.lba);
 1536                 set_linux_cdrom_addr(&sc.cdsc_reladdr, sc.cdsc_format,
 1537                     bsdinfo.what.position.reladdr.lba);
 1538                 error = copyout(&sc, (void *)args->arg, sizeof(sc));
 1539                 break;
 1540         }
 1541 
 1542         /* LINUX_CDROMREADMODE2 */
 1543         /* LINUX_CDROMREADMODE1 */
 1544         /* LINUX_CDROMREADAUDIO */
 1545         /* LINUX_CDROMEJECT_SW */
 1546         /* LINUX_CDROMMULTISESSION */
 1547         /* LINUX_CDROM_GET_UPC */
 1548 
 1549         case LINUX_CDROMRESET:
 1550                 args->cmd = CDIOCRESET;
 1551                 error = (ioctl(td, (struct ioctl_args *)args));
 1552                 break;
 1553 
 1554         /* LINUX_CDROMVOLREAD */
 1555         /* LINUX_CDROMREADRAW */
 1556         /* LINUX_CDROMREADCOOKED */
 1557         /* LINUX_CDROMSEEK */
 1558         /* LINUX_CDROMPLAYBLK */
 1559         /* LINUX_CDROMREADALL */
 1560         /* LINUX_CDROMCLOSETRAY */
 1561         /* LINUX_CDROMLOADFROMSLOT */
 1562         /* LINUX_CDROMGETSPINDOWN */
 1563         /* LINUX_CDROMSETSPINDOWN */
 1564         /* LINUX_CDROM_SET_OPTIONS */
 1565         /* LINUX_CDROM_CLEAR_OPTIONS */
 1566         /* LINUX_CDROM_SELECT_SPEED */
 1567         /* LINUX_CDROM_SELECT_DISC */
 1568         /* LINUX_CDROM_MEDIA_CHANGED */
 1569         /* LINUX_CDROM_DRIVE_STATUS */
 1570         /* LINUX_CDROM_DISC_STATUS */
 1571         /* LINUX_CDROM_CHANGER_NSLOTS */
 1572         /* LINUX_CDROM_LOCKDOOR */
 1573         /* LINUX_CDROM_DEBUG */
 1574         /* LINUX_CDROM_GET_CAPABILITY */
 1575         /* LINUX_CDROMAUDIOBUFSIZ */
 1576 
 1577         case LINUX_DVD_READ_STRUCT: {
 1578                 l_dvd_struct *lds;
 1579                 struct dvd_struct *bds;
 1580 
 1581                 lds = malloc(sizeof(*lds), M_LINUX, M_WAITOK);
 1582                 bds = malloc(sizeof(*bds), M_LINUX, M_WAITOK);
 1583                 error = copyin((void *)args->arg, lds, sizeof(*lds));
 1584                 if (error)
 1585                         goto out;
 1586                 error = linux_to_bsd_dvd_struct(lds, bds);
 1587                 if (error)
 1588                         goto out;
 1589                 error = fo_ioctl(fp, DVDIOCREADSTRUCTURE, (caddr_t)bds,
 1590                     td->td_ucred, td);
 1591                 if (error)
 1592                         goto out;
 1593                 error = bsd_to_linux_dvd_struct(bds, lds);
 1594                 if (error)
 1595                         goto out;
 1596                 error = copyout(lds, (void *)args->arg, sizeof(*lds));
 1597         out:
 1598                 free(bds, M_LINUX);
 1599                 free(lds, M_LINUX);
 1600                 break;
 1601         }
 1602 
 1603         /* LINUX_DVD_WRITE_STRUCT */
 1604 
 1605         case LINUX_DVD_AUTH: {
 1606                 l_dvd_authinfo lda;
 1607                 struct dvd_authinfo bda;
 1608                 int bcode;
 1609 
 1610                 error = copyin((void *)args->arg, &lda, sizeof(lda));
 1611                 if (error)
 1612                         break;
 1613                 error = linux_to_bsd_dvd_authinfo(&lda, &bcode, &bda);
 1614                 if (error)
 1615                         break;
 1616                 error = fo_ioctl(fp, bcode, (caddr_t)&bda, td->td_ucred,
 1617                     td);
 1618                 if (error) {
 1619                         if (lda.type == LINUX_DVD_HOST_SEND_KEY2) {
 1620                                 lda.type = LINUX_DVD_AUTH_FAILURE;
 1621                                 copyout(&lda, (void *)args->arg, sizeof(lda));
 1622                         }
 1623                         break;
 1624                 }
 1625                 error = bsd_to_linux_dvd_authinfo(&bda, &lda);
 1626                 if (error)
 1627                         break;
 1628                 error = copyout(&lda, (void *)args->arg, sizeof(lda));
 1629                 break;
 1630         }
 1631 
 1632         case LINUX_SCSI_GET_BUS_NUMBER:
 1633         case LINUX_SCSI_GET_IDLUN:
 1634                 error = linux_ioctl_sg(td, args);
 1635                 break;
 1636 
 1637         /* LINUX_CDROM_SEND_PACKET */
 1638         /* LINUX_CDROM_NEXT_WRITABLE */
 1639         /* LINUX_CDROM_LAST_WRITTEN */
 1640 
 1641         default:
 1642                 error = ENOIOCTL;
 1643                 break;
 1644         }
 1645 
 1646         fdrop(fp, td);
 1647         return (error);
 1648 }
 1649 
 1650 static int
 1651 linux_ioctl_vfat(struct thread *td, struct linux_ioctl_args *args)
 1652 {
 1653 
 1654         return (ENOTTY);
 1655 }
 1656 
 1657 /*
 1658  * Sound related ioctls
 1659  */
 1660 
 1661 struct linux_mixer_info {
 1662         char    id[16];
 1663         char    name[32];
 1664         int     modify_counter;
 1665         int     fillers[10];
 1666 };
 1667 
 1668 struct linux_old_mixer_info {
 1669         char    id[16];
 1670         char    name[32];
 1671 };
 1672 
 1673 static u_int32_t dirbits[4] = { IOC_VOID, IOC_IN, IOC_OUT, IOC_INOUT };
 1674 
 1675 #define SETDIR(c)       (((c) & ~IOC_DIRMASK) | dirbits[args->cmd >> 30])
 1676 
 1677 static int
 1678 linux_ioctl_sound(struct thread *td, struct linux_ioctl_args *args)
 1679 {
 1680 
 1681         switch (args->cmd & 0xffff) {
 1682 
 1683         case LINUX_SOUND_MIXER_WRITE_VOLUME:
 1684                 args->cmd = SETDIR(SOUND_MIXER_WRITE_VOLUME);
 1685                 return (ioctl(td, (struct ioctl_args *)args));
 1686 
 1687         case LINUX_SOUND_MIXER_WRITE_BASS:
 1688                 args->cmd = SETDIR(SOUND_MIXER_WRITE_BASS);
 1689                 return (ioctl(td, (struct ioctl_args *)args));
 1690 
 1691         case LINUX_SOUND_MIXER_WRITE_TREBLE:
 1692                 args->cmd = SETDIR(SOUND_MIXER_WRITE_TREBLE);
 1693                 return (ioctl(td, (struct ioctl_args *)args));
 1694 
 1695         case LINUX_SOUND_MIXER_WRITE_SYNTH:
 1696                 args->cmd = SETDIR(SOUND_MIXER_WRITE_SYNTH);
 1697                 return (ioctl(td, (struct ioctl_args *)args));
 1698 
 1699         case LINUX_SOUND_MIXER_WRITE_PCM:
 1700                 args->cmd = SETDIR(SOUND_MIXER_WRITE_PCM);
 1701                 return (ioctl(td, (struct ioctl_args *)args));
 1702 
 1703         case LINUX_SOUND_MIXER_WRITE_SPEAKER:
 1704                 args->cmd = SETDIR(SOUND_MIXER_WRITE_SPEAKER);
 1705                 return (ioctl(td, (struct ioctl_args *)args));
 1706 
 1707         case LINUX_SOUND_MIXER_WRITE_LINE:
 1708                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE);
 1709                 return (ioctl(td, (struct ioctl_args *)args));
 1710 
 1711         case LINUX_SOUND_MIXER_WRITE_MIC:
 1712                 args->cmd = SETDIR(SOUND_MIXER_WRITE_MIC);
 1713                 return (ioctl(td, (struct ioctl_args *)args));
 1714 
 1715         case LINUX_SOUND_MIXER_WRITE_CD:
 1716                 args->cmd = SETDIR(SOUND_MIXER_WRITE_CD);
 1717                 return (ioctl(td, (struct ioctl_args *)args));
 1718 
 1719         case LINUX_SOUND_MIXER_WRITE_IMIX:
 1720                 args->cmd = SETDIR(SOUND_MIXER_WRITE_IMIX);
 1721                 return (ioctl(td, (struct ioctl_args *)args));
 1722 
 1723         case LINUX_SOUND_MIXER_WRITE_ALTPCM:
 1724                 args->cmd = SETDIR(SOUND_MIXER_WRITE_ALTPCM);
 1725                 return (ioctl(td, (struct ioctl_args *)args));
 1726 
 1727         case LINUX_SOUND_MIXER_WRITE_RECLEV:
 1728                 args->cmd = SETDIR(SOUND_MIXER_WRITE_RECLEV);
 1729                 return (ioctl(td, (struct ioctl_args *)args));
 1730 
 1731         case LINUX_SOUND_MIXER_WRITE_IGAIN:
 1732                 args->cmd = SETDIR(SOUND_MIXER_WRITE_IGAIN);
 1733                 return (ioctl(td, (struct ioctl_args *)args));
 1734 
 1735         case LINUX_SOUND_MIXER_WRITE_OGAIN:
 1736                 args->cmd = SETDIR(SOUND_MIXER_WRITE_OGAIN);
 1737                 return (ioctl(td, (struct ioctl_args *)args));
 1738 
 1739         case LINUX_SOUND_MIXER_WRITE_LINE1:
 1740                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE1);
 1741                 return (ioctl(td, (struct ioctl_args *)args));
 1742 
 1743         case LINUX_SOUND_MIXER_WRITE_LINE2:
 1744                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE2);
 1745                 return (ioctl(td, (struct ioctl_args *)args));
 1746 
 1747         case LINUX_SOUND_MIXER_WRITE_LINE3:
 1748                 args->cmd = SETDIR(SOUND_MIXER_WRITE_LINE3);
 1749                 return (ioctl(td, (struct ioctl_args *)args));
 1750 
 1751         case LINUX_SOUND_MIXER_INFO: {
 1752                 /* Key on encoded length */
 1753                 switch ((args->cmd >> 16) & 0x1fff) {
 1754                 case 0x005c: {  /* SOUND_MIXER_INFO */
 1755                         struct linux_mixer_info info;
 1756                         bzero(&info, sizeof(info));
 1757                         strncpy(info.id, "OSS", sizeof(info.id) - 1);
 1758                         strncpy(info.name, "FreeBSD OSS Mixer", sizeof(info.name) - 1);
 1759                         copyout(&info, (void *)args->arg, sizeof(info));
 1760                         return (0);
 1761                 }
 1762                 case 0x0030: {  /* SOUND_OLD_MIXER_INFO */
 1763                         struct linux_old_mixer_info info;
 1764                         bzero(&info, sizeof(info));
 1765                         strncpy(info.id, "OSS", sizeof(info.id) - 1);
 1766                         strncpy(info.name, "FreeBSD OSS Mixer", sizeof(info.name) - 1);
 1767                         copyout(&info, (void *)args->arg, sizeof(info));
 1768                         return (0);
 1769                 }
 1770                 default:
 1771                         return (ENOIOCTL);
 1772                 }
 1773                 break;
 1774         }
 1775 
 1776         case LINUX_OSS_GETVERSION: {
 1777                 int version = linux_get_oss_version(td);
 1778                 return (copyout(&version, (void *)args->arg, sizeof(int)));
 1779         }
 1780 
 1781         case LINUX_SOUND_MIXER_READ_STEREODEVS:
 1782                 args->cmd = SOUND_MIXER_READ_STEREODEVS;
 1783                 return (ioctl(td, (struct ioctl_args *)args));
 1784 
 1785         case LINUX_SOUND_MIXER_READ_CAPS:
 1786                 args->cmd = SOUND_MIXER_READ_CAPS;
 1787                 return (ioctl(td, (struct ioctl_args *)args));
 1788 
 1789         case LINUX_SOUND_MIXER_READ_RECMASK:
 1790                 args->cmd = SOUND_MIXER_READ_RECMASK;
 1791                 return (ioctl(td, (struct ioctl_args *)args));
 1792 
 1793         case LINUX_SOUND_MIXER_READ_DEVMASK:
 1794                 args->cmd = SOUND_MIXER_READ_DEVMASK;
 1795                 return (ioctl(td, (struct ioctl_args *)args));
 1796 
 1797         case LINUX_SOUND_MIXER_WRITE_RECSRC:
 1798                 args->cmd = SETDIR(SOUND_MIXER_WRITE_RECSRC);
 1799                 return (ioctl(td, (struct ioctl_args *)args));
 1800 
 1801         case LINUX_SNDCTL_DSP_RESET:
 1802                 args->cmd = SNDCTL_DSP_RESET;
 1803                 return (ioctl(td, (struct ioctl_args *)args));
 1804 
 1805         case LINUX_SNDCTL_DSP_SYNC:
 1806                 args->cmd = SNDCTL_DSP_SYNC;
 1807                 return (ioctl(td, (struct ioctl_args *)args));
 1808 
 1809         case LINUX_SNDCTL_DSP_SPEED:
 1810                 args->cmd = SNDCTL_DSP_SPEED;
 1811                 return (ioctl(td, (struct ioctl_args *)args));
 1812 
 1813         case LINUX_SNDCTL_DSP_STEREO:
 1814                 args->cmd = SNDCTL_DSP_STEREO;
 1815                 return (ioctl(td, (struct ioctl_args *)args));
 1816 
 1817         case LINUX_SNDCTL_DSP_GETBLKSIZE: /* LINUX_SNDCTL_DSP_SETBLKSIZE */
 1818                 args->cmd = SNDCTL_DSP_GETBLKSIZE;
 1819                 return (ioctl(td, (struct ioctl_args *)args));
 1820 
 1821         case LINUX_SNDCTL_DSP_SETFMT:
 1822                 args->cmd = SNDCTL_DSP_SETFMT;
 1823                 return (ioctl(td, (struct ioctl_args *)args));
 1824 
 1825         case LINUX_SOUND_PCM_WRITE_CHANNELS:
 1826                 args->cmd = SOUND_PCM_WRITE_CHANNELS;
 1827                 return (ioctl(td, (struct ioctl_args *)args));
 1828 
 1829         case LINUX_SOUND_PCM_WRITE_FILTER:
 1830                 args->cmd = SOUND_PCM_WRITE_FILTER;
 1831                 return (ioctl(td, (struct ioctl_args *)args));
 1832 
 1833         case LINUX_SNDCTL_DSP_POST:
 1834                 args->cmd = SNDCTL_DSP_POST;
 1835                 return (ioctl(td, (struct ioctl_args *)args));
 1836 
 1837         case LINUX_SNDCTL_DSP_SUBDIVIDE:
 1838                 args->cmd = SNDCTL_DSP_SUBDIVIDE;
 1839                 return (ioctl(td, (struct ioctl_args *)args));
 1840 
 1841         case LINUX_SNDCTL_DSP_SETFRAGMENT:
 1842                 args->cmd = SNDCTL_DSP_SETFRAGMENT;
 1843                 return (ioctl(td, (struct ioctl_args *)args));
 1844 
 1845         case LINUX_SNDCTL_DSP_GETFMTS:
 1846                 args->cmd = SNDCTL_DSP_GETFMTS;
 1847                 return (ioctl(td, (struct ioctl_args *)args));
 1848 
 1849         case LINUX_SNDCTL_DSP_GETOSPACE:
 1850                 args->cmd = SNDCTL_DSP_GETOSPACE;
 1851                 return (ioctl(td, (struct ioctl_args *)args));
 1852 
 1853         case LINUX_SNDCTL_DSP_GETISPACE:
 1854                 args->cmd = SNDCTL_DSP_GETISPACE;
 1855                 return (ioctl(td, (struct ioctl_args *)args));
 1856 
 1857         case LINUX_SNDCTL_DSP_NONBLOCK:
 1858                 args->cmd = SNDCTL_DSP_NONBLOCK;
 1859                 return (ioctl(td, (struct ioctl_args *)args));
 1860 
 1861         case LINUX_SNDCTL_DSP_GETCAPS:
 1862                 args->cmd = SNDCTL_DSP_GETCAPS;
 1863                 return (ioctl(td, (struct ioctl_args *)args));
 1864 
 1865         case LINUX_SNDCTL_DSP_SETTRIGGER: /* LINUX_SNDCTL_GETTRIGGER */
 1866                 args->cmd = SNDCTL_DSP_SETTRIGGER;
 1867                 return (ioctl(td, (struct ioctl_args *)args));
 1868 
 1869         case LINUX_SNDCTL_DSP_GETIPTR:
 1870                 args->cmd = SNDCTL_DSP_GETIPTR;
 1871                 return (ioctl(td, (struct ioctl_args *)args));
 1872 
 1873         case LINUX_SNDCTL_DSP_GETOPTR:
 1874                 args->cmd = SNDCTL_DSP_GETOPTR;
 1875                 return (ioctl(td, (struct ioctl_args *)args));
 1876 
 1877         case LINUX_SNDCTL_DSP_SETDUPLEX:
 1878                 args->cmd = SNDCTL_DSP_SETDUPLEX;
 1879                 return (ioctl(td, (struct ioctl_args *)args));
 1880 
 1881         case LINUX_SNDCTL_DSP_GETODELAY:
 1882                 args->cmd = SNDCTL_DSP_GETODELAY;
 1883                 return (ioctl(td, (struct ioctl_args *)args));
 1884 
 1885         case LINUX_SNDCTL_SEQ_RESET:
 1886                 args->cmd = SNDCTL_SEQ_RESET;
 1887                 return (ioctl(td, (struct ioctl_args *)args));
 1888 
 1889         case LINUX_SNDCTL_SEQ_SYNC:
 1890                 args->cmd = SNDCTL_SEQ_SYNC;
 1891                 return (ioctl(td, (struct ioctl_args *)args));
 1892 
 1893         case LINUX_SNDCTL_SYNTH_INFO:
 1894                 args->cmd = SNDCTL_SYNTH_INFO;
 1895                 return (ioctl(td, (struct ioctl_args *)args));
 1896 
 1897         case LINUX_SNDCTL_SEQ_CTRLRATE:
 1898                 args->cmd = SNDCTL_SEQ_CTRLRATE;
 1899                 return (ioctl(td, (struct ioctl_args *)args));
 1900 
 1901         case LINUX_SNDCTL_SEQ_GETOUTCOUNT:
 1902                 args->cmd = SNDCTL_SEQ_GETOUTCOUNT;
 1903                 return (ioctl(td, (struct ioctl_args *)args));
 1904 
 1905         case LINUX_SNDCTL_SEQ_GETINCOUNT:
 1906                 args->cmd = SNDCTL_SEQ_GETINCOUNT;
 1907                 return (ioctl(td, (struct ioctl_args *)args));
 1908 
 1909         case LINUX_SNDCTL_SEQ_PERCMODE:
 1910                 args->cmd = SNDCTL_SEQ_PERCMODE;
 1911                 return (ioctl(td, (struct ioctl_args *)args));
 1912 
 1913         case LINUX_SNDCTL_FM_LOAD_INSTR:
 1914                 args->cmd = SNDCTL_FM_LOAD_INSTR;
 1915                 return (ioctl(td, (struct ioctl_args *)args));
 1916 
 1917         case LINUX_SNDCTL_SEQ_TESTMIDI:
 1918                 args->cmd = SNDCTL_SEQ_TESTMIDI;
 1919                 return (ioctl(td, (struct ioctl_args *)args));
 1920 
 1921         case LINUX_SNDCTL_SEQ_RESETSAMPLES:
 1922                 args->cmd = SNDCTL_SEQ_RESETSAMPLES;
 1923                 return (ioctl(td, (struct ioctl_args *)args));
 1924 
 1925         case LINUX_SNDCTL_SEQ_NRSYNTHS:
 1926                 args->cmd = SNDCTL_SEQ_NRSYNTHS;
 1927                 return (ioctl(td, (struct ioctl_args *)args));
 1928 
 1929         case LINUX_SNDCTL_SEQ_NRMIDIS:
 1930                 args->cmd = SNDCTL_SEQ_NRMIDIS;
 1931                 return (ioctl(td, (struct ioctl_args *)args));
 1932 
 1933         case LINUX_SNDCTL_MIDI_INFO:
 1934                 args->cmd = SNDCTL_MIDI_INFO;
 1935                 return (ioctl(td, (struct ioctl_args *)args));
 1936 
 1937         case LINUX_SNDCTL_SEQ_TRESHOLD:
 1938                 args->cmd = SNDCTL_SEQ_TRESHOLD;
 1939                 return (ioctl(td, (struct ioctl_args *)args));
 1940 
 1941         case LINUX_SNDCTL_SYNTH_MEMAVL:
 1942                 args->cmd = SNDCTL_SYNTH_MEMAVL;
 1943                 return (ioctl(td, (struct ioctl_args *)args));
 1944 
 1945         }
 1946 
 1947         return (ENOIOCTL);
 1948 }
 1949 
 1950 /*
 1951  * Console related ioctls
 1952  */
 1953 
 1954 #define ISSIGVALID(sig)         ((sig) > 0 && (sig) < NSIG)
 1955 
 1956 static int
 1957 linux_ioctl_console(struct thread *td, struct linux_ioctl_args *args)
 1958 {
 1959         struct file *fp;
 1960         int error;
 1961 
 1962         if ((error = fget(td, args->fd, &fp)) != 0)
 1963                 return (error);
 1964         switch (args->cmd & 0xffff) {
 1965 
 1966         case LINUX_KIOCSOUND:
 1967                 args->cmd = KIOCSOUND;
 1968                 error = (ioctl(td, (struct ioctl_args *)args));
 1969                 break;
 1970 
 1971         case LINUX_KDMKTONE:
 1972                 args->cmd = KDMKTONE;
 1973                 error = (ioctl(td, (struct ioctl_args *)args));
 1974                 break;
 1975 
 1976         case LINUX_KDGETLED:
 1977                 args->cmd = KDGETLED;
 1978                 error = (ioctl(td, (struct ioctl_args *)args));
 1979                 break;
 1980 
 1981         case LINUX_KDSETLED:
 1982                 args->cmd = KDSETLED;
 1983                 error = (ioctl(td, (struct ioctl_args *)args));
 1984                 break;
 1985 
 1986         case LINUX_KDSETMODE:
 1987                 args->cmd = KDSETMODE;
 1988                 error = (ioctl(td, (struct ioctl_args *)args));
 1989                 break;
 1990 
 1991         case LINUX_KDGETMODE:
 1992                 args->cmd = KDGETMODE;
 1993                 error = (ioctl(td, (struct ioctl_args *)args));
 1994                 break;
 1995 
 1996         case LINUX_KDGKBMODE:
 1997                 args->cmd = KDGKBMODE;
 1998                 error = (ioctl(td, (struct ioctl_args *)args));
 1999                 break;
 2000 
 2001         case LINUX_KDSKBMODE: {
 2002                 int kbdmode;
 2003                 switch (args->arg) {
 2004                 case LINUX_KBD_RAW:
 2005                         kbdmode = K_RAW;
 2006                         break;
 2007                 case LINUX_KBD_XLATE:
 2008                         kbdmode = K_XLATE;
 2009                         break;
 2010                 case LINUX_KBD_MEDIUMRAW:
 2011                         kbdmode = K_RAW;
 2012                         break;
 2013                 default:
 2014                         fdrop(fp, td);
 2015                         return (EINVAL);
 2016                 }
 2017                 error = (fo_ioctl(fp, KDSKBMODE, (caddr_t)&kbdmode,
 2018                     td->td_ucred, td));
 2019                 break;
 2020         }
 2021 
 2022         case LINUX_VT_OPENQRY:
 2023                 args->cmd = VT_OPENQRY;
 2024                 error = (ioctl(td, (struct ioctl_args *)args));
 2025                 break;
 2026 
 2027         case LINUX_VT_GETMODE:
 2028                 args->cmd = VT_GETMODE;
 2029                 error = (ioctl(td, (struct ioctl_args *)args));
 2030                 break;
 2031 
 2032         case LINUX_VT_SETMODE: {
 2033                 struct vt_mode mode;
 2034                 if ((error = copyin((void *)args->arg, &mode, sizeof(mode))))
 2035                         break;
 2036                 if (!ISSIGVALID(mode.frsig) && ISSIGVALID(mode.acqsig))
 2037                         mode.frsig = mode.acqsig;
 2038                 if ((error = copyout(&mode, (void *)args->arg, sizeof(mode))))
 2039                         break;
 2040                 args->cmd = VT_SETMODE;
 2041                 error = (ioctl(td, (struct ioctl_args *)args));
 2042                 break;
 2043         }
 2044 
 2045         case LINUX_VT_GETSTATE:
 2046                 args->cmd = VT_GETACTIVE;
 2047                 error = (ioctl(td, (struct ioctl_args *)args));
 2048                 break;
 2049 
 2050         case LINUX_VT_RELDISP:
 2051                 args->cmd = VT_RELDISP;
 2052                 error = (ioctl(td, (struct ioctl_args *)args));
 2053                 break;
 2054 
 2055         case LINUX_VT_ACTIVATE:
 2056                 args->cmd = VT_ACTIVATE;
 2057                 error = (ioctl(td, (struct ioctl_args *)args));
 2058                 break;
 2059 
 2060         case LINUX_VT_WAITACTIVE:
 2061                 args->cmd = VT_WAITACTIVE;
 2062                 error = (ioctl(td, (struct ioctl_args *)args));
 2063                 break;
 2064 
 2065         default:
 2066                 error = ENOIOCTL;
 2067                 break;
 2068         }
 2069 
 2070         fdrop(fp, td);
 2071         return (error);
 2072 }
 2073 
 2074 /*
 2075  * Criteria for interface name translation
 2076  */
 2077 #define IFP_IS_ETH(ifp) (ifp->if_type == IFT_ETHER)
 2078 
 2079 /*
 2080  * Interface function used by linprocfs (at the time of writing). It's not
 2081  * used by the Linuxulator itself.
 2082  */
 2083 int
 2084 linux_ifname(struct ifnet *ifp, char *buffer, size_t buflen)
 2085 {
 2086         struct ifnet *ifscan;
 2087         int ethno;
 2088 
 2089         IFNET_RLOCK_ASSERT();
 2090 
 2091         /* Short-circuit non ethernet interfaces */
 2092         if (!IFP_IS_ETH(ifp))
 2093                 return (strlcpy(buffer, ifp->if_xname, buflen));
 2094 
 2095         /* Determine the (relative) unit number for ethernet interfaces */
 2096         ethno = 0;
 2097         TAILQ_FOREACH(ifscan, &V_ifnet, if_link) {
 2098                 if (ifscan == ifp)
 2099                         return (snprintf(buffer, buflen, "eth%d", ethno));
 2100                 if (IFP_IS_ETH(ifscan))
 2101                         ethno++;
 2102         }
 2103 
 2104         return (0);
 2105 }
 2106 
 2107 /*
 2108  * Translate a Linux interface name to a FreeBSD interface name,
 2109  * and return the associated ifnet structure
 2110  * bsdname and lxname need to be least IFNAMSIZ bytes long, but
 2111  * can point to the same buffer.
 2112  */
 2113 
 2114 static struct ifnet *
 2115 ifname_linux_to_bsd(struct thread *td, const char *lxname, char *bsdname)
 2116 {
 2117         struct ifnet *ifp;
 2118         int len, unit;
 2119         char *ep;
 2120         int is_eth, index;
 2121 
 2122         for (len = 0; len < LINUX_IFNAMSIZ; ++len)
 2123                 if (!isalpha(lxname[len]))
 2124                         break;
 2125         if (len == 0 || len == LINUX_IFNAMSIZ)
 2126                 return (NULL);
 2127         unit = (int)strtoul(lxname + len, &ep, 10);
 2128         if (ep == NULL || ep == lxname + len || ep >= lxname + LINUX_IFNAMSIZ)
 2129                 return (NULL);
 2130         index = 0;
 2131         is_eth = (len == 3 && !strncmp(lxname, "eth", len)) ? 1 : 0;
 2132         CURVNET_SET(TD_TO_VNET(td));
 2133         IFNET_RLOCK();
 2134         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 2135                 /*
 2136                  * Allow Linux programs to use FreeBSD names. Don't presume
 2137                  * we never have an interface named "eth", so don't make
 2138                  * the test optional based on is_eth.
 2139                  */
 2140                 if (strncmp(ifp->if_xname, lxname, LINUX_IFNAMSIZ) == 0)
 2141                         break;
 2142                 if (is_eth && IFP_IS_ETH(ifp) && unit == index++)
 2143                         break;
 2144         }
 2145         IFNET_RUNLOCK();
 2146         CURVNET_RESTORE();
 2147         if (ifp != NULL)
 2148                 strlcpy(bsdname, ifp->if_xname, IFNAMSIZ);
 2149         return (ifp);
 2150 }
 2151 
 2152 /*
 2153  * Implement the SIOCGIFCONF ioctl
 2154  */
 2155 
 2156 static int
 2157 linux_ifconf(struct thread *td, struct ifconf *uifc)
 2158 {
 2159 #ifdef COMPAT_LINUX32
 2160         struct l_ifconf ifc;
 2161 #else
 2162         struct ifconf ifc;
 2163 #endif
 2164         struct l_ifreq ifr;
 2165         struct ifnet *ifp;
 2166         struct ifaddr *ifa;
 2167         struct sbuf *sb;
 2168         int error, ethno, full = 0, valid_len, max_len;
 2169 
 2170         error = copyin(uifc, &ifc, sizeof(ifc));
 2171         if (error != 0)
 2172                 return (error);
 2173 
 2174         max_len = MAXPHYS - 1;
 2175 
 2176         CURVNET_SET(TD_TO_VNET(td));
 2177         /* handle the 'request buffer size' case */
 2178         if (ifc.ifc_buf == PTROUT(NULL)) {
 2179                 ifc.ifc_len = 0;
 2180                 IFNET_RLOCK();
 2181                 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 2182                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 2183                                 struct sockaddr *sa = ifa->ifa_addr;
 2184                                 if (sa->sa_family == AF_INET)
 2185                                         ifc.ifc_len += sizeof(ifr);
 2186                         }
 2187                 }
 2188                 IFNET_RUNLOCK();
 2189                 error = copyout(&ifc, uifc, sizeof(ifc));
 2190                 CURVNET_RESTORE();
 2191                 return (error);
 2192         }
 2193 
 2194         if (ifc.ifc_len <= 0) {
 2195                 CURVNET_RESTORE();
 2196                 return (EINVAL);
 2197         }
 2198 
 2199 again:
 2200         /* Keep track of eth interfaces */
 2201         ethno = 0;
 2202         if (ifc.ifc_len <= max_len) {
 2203                 max_len = ifc.ifc_len;
 2204                 full = 1;
 2205         }
 2206         sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
 2207         max_len = 0;
 2208         valid_len = 0;
 2209 
 2210         /* Return all AF_INET addresses of all interfaces */
 2211         IFNET_RLOCK();
 2212         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 2213                 int addrs = 0;
 2214 
 2215                 bzero(&ifr, sizeof(ifr));
 2216                 if (IFP_IS_ETH(ifp))
 2217                         snprintf(ifr.ifr_name, LINUX_IFNAMSIZ, "eth%d",
 2218                             ethno++);
 2219                 else
 2220                         strlcpy(ifr.ifr_name, ifp->if_xname, LINUX_IFNAMSIZ);
 2221 
 2222                 /* Walk the address list */
 2223                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 2224                         struct sockaddr *sa = ifa->ifa_addr;
 2225 
 2226                         if (sa->sa_family == AF_INET) {
 2227                                 ifr.ifr_addr.sa_family = LINUX_AF_INET;
 2228                                 memcpy(ifr.ifr_addr.sa_data, sa->sa_data,
 2229                                     sizeof(ifr.ifr_addr.sa_data));
 2230                                 sbuf_bcat(sb, &ifr, sizeof(ifr));
 2231                                 max_len += sizeof(ifr);
 2232                                 addrs++;
 2233                         }
 2234 
 2235                         if (!sbuf_overflowed(sb))
 2236                                 valid_len = sbuf_len(sb);
 2237                 }
 2238                 if (addrs == 0) {
 2239                         bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
 2240                         sbuf_bcat(sb, &ifr, sizeof(ifr));
 2241                         max_len += sizeof(ifr);
 2242 
 2243                         if (!sbuf_overflowed(sb))
 2244                                 valid_len = sbuf_len(sb);
 2245                 }
 2246         }
 2247         IFNET_RUNLOCK();
 2248 
 2249         if (valid_len != max_len && !full) {
 2250                 sbuf_delete(sb);
 2251                 goto again;
 2252         }
 2253 
 2254         ifc.ifc_len = valid_len; 
 2255         sbuf_finish(sb);
 2256         error = copyout(sbuf_data(sb), PTRIN(ifc.ifc_buf), ifc.ifc_len);
 2257         if (error == 0)
 2258                 error = copyout(&ifc, uifc, sizeof(ifc));
 2259         sbuf_delete(sb);
 2260         CURVNET_RESTORE();
 2261 
 2262         return (error);
 2263 }
 2264 
 2265 static int
 2266 linux_gifflags(struct thread *td, struct ifnet *ifp, struct l_ifreq *ifr)
 2267 {
 2268         l_short flags;
 2269 
 2270         flags = (ifp->if_flags | ifp->if_drv_flags) & 0xffff;
 2271         /* these flags have no Linux equivalent */
 2272         flags &= ~(IFF_SMART|IFF_DRV_OACTIVE|IFF_SIMPLEX|
 2273             IFF_LINK0|IFF_LINK1|IFF_LINK2);
 2274         /* Linux' multicast flag is in a different bit */
 2275         if (flags & IFF_MULTICAST) {
 2276                 flags &= ~IFF_MULTICAST;
 2277                 flags |= 0x1000;
 2278         }
 2279 
 2280         return (copyout(&flags, &ifr->ifr_flags, sizeof(flags)));
 2281 }
 2282 
 2283 #define ARPHRD_ETHER    1
 2284 #define ARPHRD_LOOPBACK 772
 2285 
 2286 static int
 2287 linux_gifhwaddr(struct ifnet *ifp, struct l_ifreq *ifr)
 2288 {
 2289         struct ifaddr *ifa;
 2290         struct sockaddr_dl *sdl;
 2291         struct l_sockaddr lsa;
 2292 
 2293         if (ifp->if_type == IFT_LOOP) {
 2294                 bzero(&lsa, sizeof(lsa));
 2295                 lsa.sa_family = ARPHRD_LOOPBACK;
 2296                 return (copyout(&lsa, &ifr->ifr_hwaddr, sizeof(lsa)));
 2297         }
 2298 
 2299         if (ifp->if_type != IFT_ETHER)
 2300                 return (ENOENT);
 2301 
 2302         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 2303                 sdl = (struct sockaddr_dl*)ifa->ifa_addr;
 2304                 if (sdl != NULL && (sdl->sdl_family == AF_LINK) &&
 2305                     (sdl->sdl_type == IFT_ETHER)) {
 2306                         bzero(&lsa, sizeof(lsa));
 2307                         lsa.sa_family = ARPHRD_ETHER;
 2308                         bcopy(LLADDR(sdl), lsa.sa_data, LINUX_IFHWADDRLEN);
 2309                         return (copyout(&lsa, &ifr->ifr_hwaddr, sizeof(lsa)));
 2310                 }
 2311         }
 2312 
 2313         return (ENOENT);
 2314 }
 2315 
 2316 
 2317  /*
 2318 * If we fault in bsd_to_linux_ifreq() then we will fault when we call
 2319 * the native ioctl().  Thus, we don't really need to check the return
 2320 * value of this function.
 2321 */
 2322 static int
 2323 bsd_to_linux_ifreq(struct ifreq *arg)
 2324 {
 2325         struct ifreq ifr;
 2326         size_t ifr_len = sizeof(struct ifreq);
 2327         int error;
 2328         
 2329         if ((error = copyin(arg, &ifr, ifr_len)))
 2330                 return (error);
 2331         
 2332         *(u_short *)&ifr.ifr_addr = ifr.ifr_addr.sa_family;
 2333         
 2334         error = copyout(&ifr, arg, ifr_len);
 2335 
 2336         return (error);
 2337 }
 2338 
 2339 /*
 2340  * Socket related ioctls
 2341  */
 2342 
 2343 static int
 2344 linux_ioctl_socket(struct thread *td, struct linux_ioctl_args *args)
 2345 {
 2346         char lifname[LINUX_IFNAMSIZ], ifname[IFNAMSIZ];
 2347         struct ifnet *ifp;
 2348         struct file *fp;
 2349         int error, type;
 2350 
 2351         ifp = NULL;
 2352         error = 0;
 2353 
 2354         if ((error = fget(td, args->fd, &fp)) != 0)
 2355                 return (error);
 2356         type = fp->f_type;
 2357         fdrop(fp, td);
 2358         if (type != DTYPE_SOCKET) {
 2359                 /* not a socket - probably a tap / vmnet device */
 2360                 switch (args->cmd) {
 2361                 case LINUX_SIOCGIFADDR:
 2362                 case LINUX_SIOCSIFADDR:
 2363                 case LINUX_SIOCGIFFLAGS:
 2364                         return (linux_ioctl_special(td, args));
 2365                 default:
 2366                         return (ENOIOCTL);
 2367                 }
 2368         }
 2369 
 2370         switch (args->cmd & 0xffff) {
 2371 
 2372         case LINUX_FIOGETOWN:
 2373         case LINUX_FIOSETOWN:
 2374         case LINUX_SIOCADDMULTI:
 2375         case LINUX_SIOCATMARK:
 2376         case LINUX_SIOCDELMULTI:
 2377         case LINUX_SIOCGIFCONF:
 2378         case LINUX_SIOCGPGRP:
 2379         case LINUX_SIOCSPGRP:
 2380         case LINUX_SIOCGIFCOUNT:
 2381                 /* these ioctls don't take an interface name */
 2382 #ifdef DEBUG
 2383                 printf("%s(): ioctl %d\n", __func__,
 2384                     args->cmd & 0xffff);
 2385 #endif
 2386                 break;
 2387 
 2388         case LINUX_SIOCGIFFLAGS:
 2389         case LINUX_SIOCGIFADDR:
 2390         case LINUX_SIOCSIFADDR:
 2391         case LINUX_SIOCGIFDSTADDR:
 2392         case LINUX_SIOCGIFBRDADDR:
 2393         case LINUX_SIOCGIFNETMASK:
 2394         case LINUX_SIOCSIFNETMASK:
 2395         case LINUX_SIOCGIFMTU:
 2396         case LINUX_SIOCSIFMTU:
 2397         case LINUX_SIOCSIFNAME:
 2398         case LINUX_SIOCGIFHWADDR:
 2399         case LINUX_SIOCSIFHWADDR:
 2400         case LINUX_SIOCDEVPRIVATE:
 2401         case LINUX_SIOCDEVPRIVATE+1:
 2402         case LINUX_SIOCGIFINDEX:
 2403                 /* copy in the interface name and translate it. */
 2404                 error = copyin((void *)args->arg, lifname, LINUX_IFNAMSIZ);
 2405                 if (error != 0)
 2406                         return (error);
 2407 #ifdef DEBUG
 2408                 printf("%s(): ioctl %d on %.*s\n", __func__,
 2409                     args->cmd & 0xffff, LINUX_IFNAMSIZ, lifname);
 2410 #endif
 2411                 ifp = ifname_linux_to_bsd(td, lifname, ifname);
 2412                 if (ifp == NULL)
 2413                         return (EINVAL);
 2414                 /*
 2415                  * We need to copy it back out in case we pass the
 2416                  * request on to our native ioctl(), which will expect
 2417                  * the ifreq to be in user space and have the correct
 2418                  * interface name.
 2419                  */
 2420                 error = copyout(ifname, (void *)args->arg, IFNAMSIZ);
 2421                 if (error != 0)
 2422                         return (error);
 2423 #ifdef DEBUG
 2424                 printf("%s(): %s translated to %s\n", __func__,
 2425                     lifname, ifname);
 2426 #endif
 2427                 break;
 2428 
 2429         default:
 2430                 return (ENOIOCTL);
 2431         }
 2432 
 2433         switch (args->cmd & 0xffff) {
 2434 
 2435         case LINUX_FIOSETOWN:
 2436                 args->cmd = FIOSETOWN;
 2437                 error = ioctl(td, (struct ioctl_args *)args);
 2438                 break;
 2439 
 2440         case LINUX_SIOCSPGRP:
 2441                 args->cmd = SIOCSPGRP;
 2442                 error = ioctl(td, (struct ioctl_args *)args);
 2443                 break;
 2444 
 2445         case LINUX_FIOGETOWN:
 2446                 args->cmd = FIOGETOWN;
 2447                 error = ioctl(td, (struct ioctl_args *)args);
 2448                 break;
 2449 
 2450         case LINUX_SIOCGPGRP:
 2451                 args->cmd = SIOCGPGRP;
 2452                 error = ioctl(td, (struct ioctl_args *)args);
 2453                 break;
 2454 
 2455         case LINUX_SIOCATMARK:
 2456                 args->cmd = SIOCATMARK;
 2457                 error = ioctl(td, (struct ioctl_args *)args);
 2458                 break;
 2459 
 2460         /* LINUX_SIOCGSTAMP */
 2461 
 2462         case LINUX_SIOCGIFCONF:
 2463                 error = linux_ifconf(td, (struct ifconf *)args->arg);
 2464                 break;
 2465 
 2466         case LINUX_SIOCGIFFLAGS:
 2467                 args->cmd = SIOCGIFFLAGS;
 2468                 error = linux_gifflags(td, ifp, (struct l_ifreq *)args->arg);
 2469                 break;
 2470 
 2471         case LINUX_SIOCGIFADDR:
 2472                 args->cmd = SIOCGIFADDR;
 2473                 error = ioctl(td, (struct ioctl_args *)args);
 2474                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
 2475                 break;
 2476 
 2477         case LINUX_SIOCSIFADDR:
 2478                 /* XXX probably doesn't work, included for completeness */
 2479                 args->cmd = SIOCSIFADDR;
 2480                 error = ioctl(td, (struct ioctl_args *)args);
 2481                 break;
 2482 
 2483         case LINUX_SIOCGIFDSTADDR:
 2484                 args->cmd = SIOCGIFDSTADDR;
 2485                 error = ioctl(td, (struct ioctl_args *)args);
 2486                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
 2487                 break;
 2488 
 2489         case LINUX_SIOCGIFBRDADDR:
 2490                 args->cmd = SIOCGIFBRDADDR;
 2491                 error = ioctl(td, (struct ioctl_args *)args);
 2492                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
 2493                 break;
 2494 
 2495         case LINUX_SIOCGIFNETMASK:
 2496                 args->cmd = SIOCGIFNETMASK;
 2497                 error = ioctl(td, (struct ioctl_args *)args);
 2498                 bsd_to_linux_ifreq((struct ifreq *)args->arg);
 2499                 break;
 2500 
 2501         case LINUX_SIOCSIFNETMASK:
 2502                 error = ENOIOCTL;
 2503                 break;
 2504 
 2505         case LINUX_SIOCGIFMTU:
 2506                 args->cmd = SIOCGIFMTU;
 2507                 error = ioctl(td, (struct ioctl_args *)args);
 2508                 break;
 2509 
 2510         case LINUX_SIOCSIFMTU:
 2511                 args->cmd = SIOCSIFMTU;
 2512                 error = ioctl(td, (struct ioctl_args *)args);
 2513                 break;
 2514 
 2515         case LINUX_SIOCSIFNAME:
 2516                 error = ENOIOCTL;
 2517                 break;
 2518 
 2519         case LINUX_SIOCGIFHWADDR:
 2520                 error = linux_gifhwaddr(ifp, (struct l_ifreq *)args->arg);
 2521                 break;
 2522 
 2523         case LINUX_SIOCSIFHWADDR:
 2524                 error = ENOIOCTL;
 2525                 break;
 2526 
 2527         case LINUX_SIOCADDMULTI:
 2528                 args->cmd = SIOCADDMULTI;
 2529                 error = ioctl(td, (struct ioctl_args *)args);
 2530                 break;
 2531 
 2532         case LINUX_SIOCDELMULTI:
 2533                 args->cmd = SIOCDELMULTI;
 2534                 error = ioctl(td, (struct ioctl_args *)args);
 2535                 break;
 2536 
 2537         case LINUX_SIOCGIFINDEX:
 2538                 args->cmd = SIOCGIFINDEX;
 2539                 error = ioctl(td, (struct ioctl_args *)args);
 2540                 break;
 2541 
 2542         case LINUX_SIOCGIFCOUNT:
 2543                 error = 0;
 2544                 break;
 2545 
 2546         /*
 2547          * XXX This is slightly bogus, but these ioctls are currently
 2548          * XXX only used by the aironet (if_an) network driver.
 2549          */
 2550         case LINUX_SIOCDEVPRIVATE:
 2551                 args->cmd = SIOCGPRIVATE_0;
 2552                 error = ioctl(td, (struct ioctl_args *)args);
 2553                 break;
 2554 
 2555         case LINUX_SIOCDEVPRIVATE+1:
 2556                 args->cmd = SIOCGPRIVATE_1;
 2557                 error = ioctl(td, (struct ioctl_args *)args);
 2558                 break;
 2559         }
 2560 
 2561         if (ifp != NULL)
 2562                 /* restore the original interface name */
 2563                 copyout(lifname, (void *)args->arg, LINUX_IFNAMSIZ);
 2564 
 2565 #ifdef DEBUG
 2566         printf("%s(): returning %d\n", __func__, error);
 2567 #endif
 2568         return (error);
 2569 }
 2570 
 2571 /*
 2572  * Device private ioctl handler
 2573  */
 2574 static int
 2575 linux_ioctl_private(struct thread *td, struct linux_ioctl_args *args)
 2576 {
 2577         struct file *fp;
 2578         int error, type;
 2579 
 2580         if ((error = fget(td, args->fd, &fp)) != 0)
 2581                 return (error);
 2582         type = fp->f_type;
 2583         fdrop(fp, td);
 2584         if (type == DTYPE_SOCKET)
 2585                 return (linux_ioctl_socket(td, args));
 2586         return (ENOIOCTL);
 2587 }
 2588 
 2589 /*
 2590  * DRM ioctl handler (sys/dev/drm)
 2591  */
 2592 static int
 2593 linux_ioctl_drm(struct thread *td, struct linux_ioctl_args *args)
 2594 {
 2595         args->cmd = SETDIR(args->cmd);
 2596         return ioctl(td, (struct ioctl_args *)args);
 2597 }
 2598 
 2599 static int
 2600 linux_ioctl_sg(struct thread *td, struct linux_ioctl_args *args)
 2601 {
 2602         struct file *fp;
 2603         u_long cmd;
 2604         int error;
 2605 
 2606         if ((error = fget(td, args->fd, &fp)) != 0) {
 2607                 printf("sg_linux_ioctl: fget returned %d\n", error);
 2608                 return (error);
 2609         }
 2610         cmd = args->cmd;
 2611 
 2612         error = (fo_ioctl(fp, cmd, (caddr_t)args->arg, td->td_ucred, td));
 2613         fdrop(fp, td);
 2614         return (error);
 2615 }
 2616 
 2617 /*
 2618  * Video4Linux (V4L) ioctl handler
 2619  */
 2620 static int
 2621 linux_to_bsd_v4l_tuner(struct l_video_tuner *lvt, struct video_tuner *vt)
 2622 {
 2623         vt->tuner = lvt->tuner;
 2624         strlcpy(vt->name, lvt->name, LINUX_VIDEO_TUNER_NAME_SIZE);
 2625         vt->rangelow = lvt->rangelow;   /* possible long size conversion */
 2626         vt->rangehigh = lvt->rangehigh; /* possible long size conversion */
 2627         vt->flags = lvt->flags;
 2628         vt->mode = lvt->mode;
 2629         vt->signal = lvt->signal;
 2630         return (0);
 2631 }
 2632 
 2633 static int
 2634 bsd_to_linux_v4l_tuner(struct video_tuner *vt, struct l_video_tuner *lvt)
 2635 {
 2636         lvt->tuner = vt->tuner;
 2637         strlcpy(lvt->name, vt->name, LINUX_VIDEO_TUNER_NAME_SIZE);
 2638         lvt->rangelow = vt->rangelow;   /* possible long size conversion */
 2639         lvt->rangehigh = vt->rangehigh; /* possible long size conversion */
 2640         lvt->flags = vt->flags;
 2641         lvt->mode = vt->mode;
 2642         lvt->signal = vt->signal;
 2643         return (0);
 2644 }
 2645 
 2646 #ifdef COMPAT_LINUX_V4L_CLIPLIST
 2647 static int
 2648 linux_to_bsd_v4l_clip(struct l_video_clip *lvc, struct video_clip *vc)
 2649 {
 2650         vc->x = lvc->x;
 2651         vc->y = lvc->y;
 2652         vc->width = lvc->width;
 2653         vc->height = lvc->height;
 2654         vc->next = PTRIN(lvc->next);    /* possible pointer size conversion */
 2655         return (0);
 2656 }
 2657 #endif
 2658 
 2659 static int
 2660 linux_to_bsd_v4l_window(struct l_video_window *lvw, struct video_window *vw)
 2661 {
 2662         vw->x = lvw->x;
 2663         vw->y = lvw->y;
 2664         vw->width = lvw->width;
 2665         vw->height = lvw->height;
 2666         vw->chromakey = lvw->chromakey;
 2667         vw->flags = lvw->flags;
 2668         vw->clips = PTRIN(lvw->clips);  /* possible pointer size conversion */
 2669         vw->clipcount = lvw->clipcount;
 2670         return (0);
 2671 }
 2672 
 2673 static int
 2674 bsd_to_linux_v4l_window(struct video_window *vw, struct l_video_window *lvw)
 2675 {
 2676         lvw->x = vw->x;
 2677         lvw->y = vw->y;
 2678         lvw->width = vw->width;
 2679         lvw->height = vw->height;
 2680         lvw->chromakey = vw->chromakey;
 2681         lvw->flags = vw->flags;
 2682         lvw->clips = PTROUT(vw->clips); /* possible pointer size conversion */
 2683         lvw->clipcount = vw->clipcount;
 2684         return (0);
 2685 }
 2686 
 2687 static int
 2688 linux_to_bsd_v4l_buffer(struct l_video_buffer *lvb, struct video_buffer *vb)
 2689 {
 2690         vb->base = PTRIN(lvb->base);    /* possible pointer size conversion */
 2691         vb->height = lvb->height;
 2692         vb->width = lvb->width;
 2693         vb->depth = lvb->depth;
 2694         vb->bytesperline = lvb->bytesperline;
 2695         return (0);
 2696 }
 2697 
 2698 static int
 2699 bsd_to_linux_v4l_buffer(struct video_buffer *vb, struct l_video_buffer *lvb)
 2700 {
 2701         lvb->base = PTROUT(vb->base);   /* possible pointer size conversion */
 2702         lvb->height = vb->height;
 2703         lvb->width = vb->width;
 2704         lvb->depth = vb->depth;
 2705         lvb->bytesperline = vb->bytesperline;
 2706         return (0);
 2707 }
 2708 
 2709 static int
 2710 linux_to_bsd_v4l_code(struct l_video_code *lvc, struct video_code *vc)
 2711 {
 2712         strlcpy(vc->loadwhat, lvc->loadwhat, LINUX_VIDEO_CODE_LOADWHAT_SIZE);
 2713         vc->datasize = lvc->datasize;
 2714         vc->data = PTRIN(lvc->data);    /* possible pointer size conversion */
 2715         return (0);
 2716 }
 2717 
 2718 #ifdef COMPAT_LINUX_V4L_CLIPLIST
 2719 static int
 2720 linux_v4l_clip_copy(void *lvc, struct video_clip **ppvc)
 2721 {
 2722         int error;
 2723         struct video_clip vclip;
 2724         struct l_video_clip l_vclip;
 2725 
 2726         error = copyin(lvc, &l_vclip, sizeof(l_vclip));
 2727         if (error) return (error);
 2728         linux_to_bsd_v4l_clip(&l_vclip, &vclip);
 2729         /* XXX: If there can be no concurrency: s/M_NOWAIT/M_WAITOK/ */
 2730         if ((*ppvc = malloc(sizeof(**ppvc), M_LINUX, M_NOWAIT)) == NULL)
 2731                 return (ENOMEM);    /* XXX: linux has no ENOMEM here */
 2732         memcpy(*ppvc, &vclip, sizeof(vclip));
 2733         (*ppvc)->next = NULL;
 2734         return (0);
 2735 }
 2736 
 2737 static int
 2738 linux_v4l_cliplist_free(struct video_window *vw)
 2739 {
 2740         struct video_clip **ppvc;
 2741         struct video_clip **ppvc_next;
 2742 
 2743         for (ppvc = &(vw->clips); *ppvc != NULL; ppvc = ppvc_next) {
 2744                 ppvc_next = &((*ppvc)->next);
 2745                 free(*ppvc, M_LINUX);
 2746         }
 2747         vw->clips = NULL;
 2748 
 2749         return (0);
 2750 }
 2751 
 2752 static int
 2753 linux_v4l_cliplist_copy(struct l_video_window *lvw, struct video_window *vw)
 2754 {
 2755         int error;
 2756         int clipcount;
 2757         void *plvc;
 2758         struct video_clip **ppvc;
 2759 
 2760         /*
 2761          * XXX: The cliplist is used to pass in a list of clipping
 2762          *      rectangles or, if clipcount == VIDEO_CLIP_BITMAP, a
 2763          *      clipping bitmap.  Some Linux apps, however, appear to
 2764          *      leave cliplist and clips uninitialized.  In any case,
 2765          *      the cliplist is not used by pwc(4), at the time of
 2766          *      writing, FreeBSD's only V4L driver.  When a driver
 2767          *      that uses the cliplist is developed, this code may
 2768          *      need re-examiniation.
 2769          */
 2770         error = 0;
 2771         clipcount = vw->clipcount;
 2772         if (clipcount == VIDEO_CLIP_BITMAP) {
 2773                 /*
 2774                  * In this case, the pointer (clips) is overloaded
 2775                  * to be a "void *" to a bitmap, therefore there
 2776                  * is no struct video_clip to copy now.
 2777                  */
 2778         } else if (clipcount > 0 && clipcount <= 16384) {
 2779                 /*
 2780                  * Clips points to list of clip rectangles, so
 2781                  * copy the list.
 2782                  *
 2783                  * XXX: Upper limit of 16384 was used here to try to
 2784                  *      avoid cases when clipcount and clips pointer
 2785                  *      are uninitialized and therefore have high random
 2786                  *      values, as is the case in the Linux Skype
 2787                  *      application.  The value 16384 was chosen as that
 2788                  *      is what is used in the Linux stradis(4) MPEG
 2789                  *      decoder driver, the only place we found an
 2790                  *      example of cliplist use.
 2791                  */
 2792                 plvc = PTRIN(lvw->clips);
 2793                 vw->clips = NULL;
 2794                 ppvc = &(vw->clips);
 2795                 while (clipcount-- > 0) {
 2796                         if (plvc == 0) {
 2797                                 error = EFAULT;
 2798                                 break;
 2799                         } else {
 2800                                 error = linux_v4l_clip_copy(plvc, ppvc);
 2801                                 if (error) {
 2802                                         linux_v4l_cliplist_free(vw);
 2803                                         break;
 2804                                 }
 2805                         }
 2806                         ppvc = &((*ppvc)->next);
 2807                         plvc = PTRIN(((struct l_video_clip *) plvc)->next);
 2808                 }
 2809         } else {
 2810                 /*
 2811                  * clipcount == 0 or negative (but not VIDEO_CLIP_BITMAP)
 2812                  * Force cliplist to null.
 2813                  */
 2814                 vw->clipcount = 0;
 2815                 vw->clips = NULL;
 2816         }
 2817         return (error);
 2818 }
 2819 #endif
 2820 
 2821 static int
 2822 linux_ioctl_v4l(struct thread *td, struct linux_ioctl_args *args)
 2823 {
 2824         struct file *fp;
 2825         int error;
 2826         struct video_tuner vtun;
 2827         struct video_window vwin;
 2828         struct video_buffer vbuf;
 2829         struct video_code vcode;
 2830         struct l_video_tuner l_vtun;
 2831         struct l_video_window l_vwin;
 2832         struct l_video_buffer l_vbuf;
 2833         struct l_video_code l_vcode;
 2834 
 2835         switch (args->cmd & 0xffff) {
 2836         case LINUX_VIDIOCGCAP:          args->cmd = VIDIOCGCAP; break;
 2837         case LINUX_VIDIOCGCHAN:         args->cmd = VIDIOCGCHAN; break;
 2838         case LINUX_VIDIOCSCHAN:         args->cmd = VIDIOCSCHAN; break;
 2839 
 2840         case LINUX_VIDIOCGTUNER:
 2841                 if ((error = fget(td, args->fd, &fp)) != 0)
 2842                         return (error);
 2843                 error = copyin((void *) args->arg, &l_vtun, sizeof(l_vtun));
 2844                 if (error) {
 2845                         fdrop(fp, td);
 2846                         return (error);
 2847                 }
 2848                 linux_to_bsd_v4l_tuner(&l_vtun, &vtun);
 2849                 error = fo_ioctl(fp, VIDIOCGTUNER, &vtun, td->td_ucred, td);
 2850                 if (!error) {
 2851                         bsd_to_linux_v4l_tuner(&vtun, &l_vtun);
 2852                         error = copyout(&l_vtun, (void *) args->arg,
 2853                             sizeof(l_vtun));
 2854                 }
 2855                 fdrop(fp, td);
 2856                 return (error);
 2857 
 2858         case LINUX_VIDIOCSTUNER:
 2859                 if ((error = fget(td, args->fd, &fp)) != 0)
 2860                         return (error);
 2861                 error = copyin((void *) args->arg, &l_vtun, sizeof(l_vtun));
 2862                 if (error) {
 2863                         fdrop(fp, td);
 2864                         return (error);
 2865                 }
 2866                 linux_to_bsd_v4l_tuner(&l_vtun, &vtun);
 2867                 error = fo_ioctl(fp, VIDIOCSTUNER, &vtun, td->td_ucred, td);
 2868                 fdrop(fp, td);
 2869                 return (error);
 2870 
 2871         case LINUX_VIDIOCGPICT:         args->cmd = VIDIOCGPICT; break;
 2872         case LINUX_VIDIOCSPICT:         args->cmd = VIDIOCSPICT; break;
 2873         case LINUX_VIDIOCCAPTURE:       args->cmd = VIDIOCCAPTURE; break;
 2874 
 2875         case LINUX_VIDIOCGWIN:
 2876                 if ((error = fget(td, args->fd, &fp)) != 0)
 2877                         return (error);
 2878                 error = fo_ioctl(fp, VIDIOCGWIN, &vwin, td->td_ucred, td);
 2879                 if (!error) {
 2880                         bsd_to_linux_v4l_window(&vwin, &l_vwin);
 2881                         error = copyout(&l_vwin, (void *) args->arg,
 2882                             sizeof(l_vwin));
 2883                 }
 2884                 fdrop(fp, td);
 2885                 return (error);
 2886 
 2887         case LINUX_VIDIOCSWIN:
 2888                 if ((error = fget(td, args->fd, &fp)) != 0)
 2889                         return (error);
 2890                 error = copyin((void *) args->arg, &l_vwin, sizeof(l_vwin));
 2891                 if (error) {
 2892                         fdrop(fp, td);
 2893                         return (error);
 2894                 }
 2895                 linux_to_bsd_v4l_window(&l_vwin, &vwin);
 2896 #ifdef COMPAT_LINUX_V4L_CLIPLIST
 2897                 error = linux_v4l_cliplist_copy(&l_vwin, &vwin);
 2898                 if (error) {
 2899                         fdrop(fp, td);
 2900                         return (error);
 2901                 }
 2902 #endif
 2903                 error = fo_ioctl(fp, VIDIOCSWIN, &vwin, td->td_ucred, td);
 2904                 fdrop(fp, td);
 2905 #ifdef COMPAT_LINUX_V4L_CLIPLIST
 2906                 linux_v4l_cliplist_free(&vwin);
 2907 #endif
 2908                 return (error);
 2909 
 2910         case LINUX_VIDIOCGFBUF:
 2911                 if ((error = fget(td, args->fd, &fp)) != 0)
 2912                         return (error);
 2913                 error = fo_ioctl(fp, VIDIOCGFBUF, &vbuf, td->td_ucred, td);
 2914                 if (!error) {
 2915                         bsd_to_linux_v4l_buffer(&vbuf, &l_vbuf);
 2916                         error = copyout(&l_vbuf, (void *) args->arg,
 2917                             sizeof(l_vbuf));
 2918                 }
 2919                 fdrop(fp, td);
 2920                 return (error);
 2921 
 2922         case LINUX_VIDIOCSFBUF:
 2923                 if ((error = fget(td, args->fd, &fp)) != 0)
 2924                         return (error);
 2925                 error = copyin((void *) args->arg, &l_vbuf, sizeof(l_vbuf));
 2926                 if (error) {
 2927                         fdrop(fp, td);
 2928                         return (error);
 2929                 }
 2930                 linux_to_bsd_v4l_buffer(&l_vbuf, &vbuf);
 2931                 error = fo_ioctl(fp, VIDIOCSFBUF, &vbuf, td->td_ucred, td);
 2932                 fdrop(fp, td);
 2933                 return (error);
 2934 
 2935         case LINUX_VIDIOCKEY:           args->cmd = VIDIOCKEY; break;
 2936         case LINUX_VIDIOCGFREQ:         args->cmd = VIDIOCGFREQ; break;
 2937         case LINUX_VIDIOCSFREQ:         args->cmd = VIDIOCSFREQ; break;
 2938         case LINUX_VIDIOCGAUDIO:        args->cmd = VIDIOCGAUDIO; break;
 2939         case LINUX_VIDIOCSAUDIO:        args->cmd = VIDIOCSAUDIO; break;
 2940         case LINUX_VIDIOCSYNC:          args->cmd = VIDIOCSYNC; break;
 2941         case LINUX_VIDIOCMCAPTURE:      args->cmd = VIDIOCMCAPTURE; break;
 2942         case LINUX_VIDIOCGMBUF:         args->cmd = VIDIOCGMBUF; break;
 2943         case LINUX_VIDIOCGUNIT:         args->cmd = VIDIOCGUNIT; break;
 2944         case LINUX_VIDIOCGCAPTURE:      args->cmd = VIDIOCGCAPTURE; break;
 2945         case LINUX_VIDIOCSCAPTURE:      args->cmd = VIDIOCSCAPTURE; break;
 2946         case LINUX_VIDIOCSPLAYMODE:     args->cmd = VIDIOCSPLAYMODE; break;
 2947         case LINUX_VIDIOCSWRITEMODE:    args->cmd = VIDIOCSWRITEMODE; break;
 2948         case LINUX_VIDIOCGPLAYINFO:     args->cmd = VIDIOCGPLAYINFO; break;
 2949 
 2950         case LINUX_VIDIOCSMICROCODE:
 2951                 if ((error = fget(td, args->fd, &fp)) != 0)
 2952                         return (error);
 2953                 error = copyin((void *) args->arg, &l_vcode, sizeof(l_vcode));
 2954                 if (error) {
 2955                         fdrop(fp, td);
 2956                         return (error);
 2957                 }
 2958                 linux_to_bsd_v4l_code(&l_vcode, &vcode);
 2959                 error = fo_ioctl(fp, VIDIOCSMICROCODE, &vcode, td->td_ucred, td);
 2960                 fdrop(fp, td);
 2961                 return (error);
 2962 
 2963         case LINUX_VIDIOCGVBIFMT:       args->cmd = VIDIOCGVBIFMT; break;
 2964         case LINUX_VIDIOCSVBIFMT:       args->cmd = VIDIOCSVBIFMT; break;
 2965         default:                        return (ENOIOCTL);
 2966         }
 2967 
 2968         error = ioctl(td, (struct ioctl_args *)args);
 2969         return (error);
 2970 }
 2971 
 2972 /*
 2973  * Special ioctl handler
 2974  */
 2975 static int
 2976 linux_ioctl_special(struct thread *td, struct linux_ioctl_args *args)
 2977 {
 2978         int error;
 2979 
 2980         switch (args->cmd) {
 2981         case LINUX_SIOCGIFADDR:
 2982                 args->cmd = SIOCGIFADDR;
 2983                 error = ioctl(td, (struct ioctl_args *)args);
 2984                 break;
 2985         case LINUX_SIOCSIFADDR:
 2986                 args->cmd = SIOCSIFADDR;
 2987                 error = ioctl(td, (struct ioctl_args *)args);
 2988                 break;
 2989         case LINUX_SIOCGIFFLAGS:
 2990                 args->cmd = SIOCGIFFLAGS;
 2991                 error = ioctl(td, (struct ioctl_args *)args);
 2992                 break;
 2993         default:
 2994                 error = ENOIOCTL;
 2995         }
 2996 
 2997         return (error);
 2998 }
 2999 
 3000 static int
 3001 linux_to_bsd_v4l2_standard(struct l_v4l2_standard *lvstd, struct v4l2_standard *vstd)
 3002 {
 3003         vstd->index = lvstd->index;
 3004         vstd->id = lvstd->id;
 3005         memcpy(&vstd->name, &lvstd->name, sizeof(*lvstd) - offsetof(struct l_v4l2_standard, name));
 3006         return (0);
 3007 }
 3008 
 3009 static int
 3010 bsd_to_linux_v4l2_standard(struct v4l2_standard *vstd, struct l_v4l2_standard *lvstd)
 3011 {
 3012         lvstd->index = vstd->index;
 3013         lvstd->id = vstd->id;
 3014         memcpy(&lvstd->name, &vstd->name, sizeof(*lvstd) - offsetof(struct l_v4l2_standard, name));
 3015         return (0);
 3016 }
 3017 
 3018 static int
 3019 linux_to_bsd_v4l2_buffer(struct l_v4l2_buffer *lvb, struct v4l2_buffer *vb)
 3020 {
 3021         vb->index = lvb->index;
 3022         vb->type = lvb->type;
 3023         vb->bytesused = lvb->bytesused;
 3024         vb->flags = lvb->flags;
 3025         vb->field = lvb->field;
 3026         vb->timestamp.tv_sec = lvb->timestamp.tv_sec;
 3027         vb->timestamp.tv_usec = lvb->timestamp.tv_usec;
 3028         memcpy(&vb->timecode, &lvb->timecode, sizeof (lvb->timecode));
 3029         vb->sequence = lvb->sequence;
 3030         vb->memory = lvb->memory;
 3031         if (lvb->memory == V4L2_MEMORY_USERPTR)
 3032                 /* possible pointer size conversion */
 3033                 vb->m.userptr = (unsigned long)PTRIN(lvb->m.userptr);
 3034         else
 3035                 vb->m.offset = lvb->m.offset;
 3036         vb->length = lvb->length;
 3037         vb->input = lvb->input;
 3038         vb->reserved = lvb->reserved;
 3039         return (0);
 3040 }
 3041 
 3042 static int
 3043 bsd_to_linux_v4l2_buffer(struct v4l2_buffer *vb, struct l_v4l2_buffer *lvb)
 3044 {
 3045         lvb->index = vb->index;
 3046         lvb->type = vb->type;
 3047         lvb->bytesused = vb->bytesused;
 3048         lvb->flags = vb->flags;
 3049         lvb->field = vb->field;
 3050         lvb->timestamp.tv_sec = vb->timestamp.tv_sec;
 3051         lvb->timestamp.tv_usec = vb->timestamp.tv_usec;
 3052         memcpy(&lvb->timecode, &vb->timecode, sizeof (vb->timecode));
 3053         lvb->sequence = vb->sequence;
 3054         lvb->memory = vb->memory;
 3055         if (vb->memory == V4L2_MEMORY_USERPTR)
 3056                 /* possible pointer size conversion */
 3057                 lvb->m.userptr = PTROUT(vb->m.userptr);
 3058         else
 3059                 lvb->m.offset = vb->m.offset;
 3060         lvb->length = vb->length;
 3061         lvb->input = vb->input;
 3062         lvb->reserved = vb->reserved;
 3063         return (0);
 3064 }
 3065 
 3066 static int
 3067 linux_to_bsd_v4l2_format(struct l_v4l2_format *lvf, struct v4l2_format *vf)
 3068 {
 3069         vf->type = lvf->type;
 3070         if (lvf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY
 3071 #ifdef V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
 3072             || lvf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
 3073 #endif
 3074             )
 3075                 /*
 3076                  * XXX TODO - needs 32 -> 64 bit conversion:
 3077                  * (unused by webcams?)
 3078                  */
 3079                 return EINVAL;
 3080         memcpy(&vf->fmt, &lvf->fmt, sizeof(vf->fmt));
 3081         return 0;
 3082 }
 3083 
 3084 static int
 3085 bsd_to_linux_v4l2_format(struct v4l2_format *vf, struct l_v4l2_format *lvf)
 3086 {
 3087         lvf->type = vf->type;
 3088         if (vf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY
 3089 #ifdef V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
 3090             || vf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
 3091 #endif
 3092             )
 3093                 /*
 3094                  * XXX TODO - needs 32 -> 64 bit conversion:
 3095                  * (unused by webcams?)
 3096                  */
 3097                 return EINVAL;
 3098         memcpy(&lvf->fmt, &vf->fmt, sizeof(vf->fmt));
 3099         return 0;
 3100 }
 3101 static int
 3102 linux_ioctl_v4l2(struct thread *td, struct linux_ioctl_args *args)
 3103 {
 3104         struct file *fp;
 3105         int error;
 3106         struct v4l2_format vformat;
 3107         struct l_v4l2_format l_vformat;
 3108         struct v4l2_standard vstd;
 3109         struct l_v4l2_standard l_vstd;
 3110         struct l_v4l2_buffer l_vbuf;
 3111         struct v4l2_buffer vbuf;
 3112         struct v4l2_input vinp;
 3113 
 3114         switch (args->cmd & 0xffff) {
 3115         case LINUX_VIDIOC_RESERVED:
 3116         case LINUX_VIDIOC_LOG_STATUS:
 3117                 if ((args->cmd & IOC_DIRMASK) != LINUX_IOC_VOID)
 3118                         return ENOIOCTL;
 3119                 args->cmd = (args->cmd & 0xffff) | IOC_VOID;
 3120                 break;
 3121 
 3122         case LINUX_VIDIOC_OVERLAY:
 3123         case LINUX_VIDIOC_STREAMON:
 3124         case LINUX_VIDIOC_STREAMOFF:
 3125         case LINUX_VIDIOC_S_STD:
 3126         case LINUX_VIDIOC_S_TUNER:
 3127         case LINUX_VIDIOC_S_AUDIO:
 3128         case LINUX_VIDIOC_S_AUDOUT:
 3129         case LINUX_VIDIOC_S_MODULATOR:
 3130         case LINUX_VIDIOC_S_FREQUENCY:
 3131         case LINUX_VIDIOC_S_CROP:
 3132         case LINUX_VIDIOC_S_JPEGCOMP:
 3133         case LINUX_VIDIOC_S_PRIORITY:
 3134         case LINUX_VIDIOC_DBG_S_REGISTER:
 3135         case LINUX_VIDIOC_S_HW_FREQ_SEEK:
 3136         case LINUX_VIDIOC_SUBSCRIBE_EVENT:
 3137         case LINUX_VIDIOC_UNSUBSCRIBE_EVENT:
 3138                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_IN;
 3139                 break;
 3140 
 3141         case LINUX_VIDIOC_QUERYCAP:
 3142         case LINUX_VIDIOC_G_STD:
 3143         case LINUX_VIDIOC_G_AUDIO:
 3144         case LINUX_VIDIOC_G_INPUT:
 3145         case LINUX_VIDIOC_G_OUTPUT:
 3146         case LINUX_VIDIOC_G_AUDOUT:
 3147         case LINUX_VIDIOC_G_JPEGCOMP:
 3148         case LINUX_VIDIOC_QUERYSTD:
 3149         case LINUX_VIDIOC_G_PRIORITY:
 3150         case LINUX_VIDIOC_QUERY_DV_PRESET:
 3151                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_OUT;
 3152                 break;
 3153 
 3154         case LINUX_VIDIOC_ENUM_FMT:
 3155         case LINUX_VIDIOC_REQBUFS:
 3156         case LINUX_VIDIOC_G_PARM:
 3157         case LINUX_VIDIOC_S_PARM:
 3158         case LINUX_VIDIOC_G_CTRL:
 3159         case LINUX_VIDIOC_S_CTRL:
 3160         case LINUX_VIDIOC_G_TUNER:
 3161         case LINUX_VIDIOC_QUERYCTRL:
 3162         case LINUX_VIDIOC_QUERYMENU:
 3163         case LINUX_VIDIOC_S_INPUT:
 3164         case LINUX_VIDIOC_S_OUTPUT:
 3165         case LINUX_VIDIOC_ENUMOUTPUT:
 3166         case LINUX_VIDIOC_G_MODULATOR:
 3167         case LINUX_VIDIOC_G_FREQUENCY:
 3168         case LINUX_VIDIOC_CROPCAP:
 3169         case LINUX_VIDIOC_G_CROP:
 3170         case LINUX_VIDIOC_ENUMAUDIO:
 3171         case LINUX_VIDIOC_ENUMAUDOUT:
 3172         case LINUX_VIDIOC_G_SLICED_VBI_CAP:
 3173 #ifdef VIDIOC_ENUM_FRAMESIZES
 3174         case LINUX_VIDIOC_ENUM_FRAMESIZES:
 3175         case LINUX_VIDIOC_ENUM_FRAMEINTERVALS:
 3176         case LINUX_VIDIOC_ENCODER_CMD:
 3177         case LINUX_VIDIOC_TRY_ENCODER_CMD:
 3178 #endif
 3179         case LINUX_VIDIOC_DBG_G_REGISTER:
 3180         case LINUX_VIDIOC_DBG_G_CHIP_IDENT:
 3181         case LINUX_VIDIOC_ENUM_DV_PRESETS:
 3182         case LINUX_VIDIOC_S_DV_PRESET:
 3183         case LINUX_VIDIOC_G_DV_PRESET:
 3184         case LINUX_VIDIOC_S_DV_TIMINGS:
 3185         case LINUX_VIDIOC_G_DV_TIMINGS:
 3186                 args->cmd = (args->cmd & ~IOC_DIRMASK) | IOC_INOUT;
 3187                 break;
 3188 
 3189         case LINUX_VIDIOC_G_FMT:
 3190         case LINUX_VIDIOC_S_FMT:
 3191         case LINUX_VIDIOC_TRY_FMT:
 3192                 error = copyin((void *)args->arg, &l_vformat, sizeof(l_vformat));
 3193                 if (error)
 3194                         return (error);
 3195                 if ((error = fget(td, args->fd, &fp)) != 0)
 3196                         return (error);
 3197                 if (linux_to_bsd_v4l2_format(&l_vformat, &vformat) != 0)
 3198                         error = EINVAL;
 3199                 else if ((args->cmd & 0xffff) == LINUX_VIDIOC_G_FMT)
 3200                         error = fo_ioctl(fp, VIDIOC_G_FMT, &vformat,
 3201                             td->td_ucred, td);
 3202                 else if ((args->cmd & 0xffff) == LINUX_VIDIOC_S_FMT)
 3203                         error = fo_ioctl(fp, VIDIOC_S_FMT, &vformat,
 3204                             td->td_ucred, td);
 3205                 else
 3206                         error = fo_ioctl(fp, VIDIOC_TRY_FMT, &vformat,
 3207                             td->td_ucred, td);
 3208                 bsd_to_linux_v4l2_format(&vformat, &l_vformat);
 3209                 copyout(&l_vformat, (void *)args->arg, sizeof(l_vformat));
 3210                 fdrop(fp, td);
 3211                 return (error);
 3212 
 3213         case LINUX_VIDIOC_ENUMSTD:
 3214                 error = copyin((void *)args->arg, &l_vstd, sizeof(l_vstd));
 3215                 if (error)
 3216                         return (error);
 3217                 linux_to_bsd_v4l2_standard(&l_vstd, &vstd);
 3218                 if ((error = fget(td, args->fd, &fp)) != 0)
 3219                         return (error);
 3220                 error = fo_ioctl(fp, VIDIOC_ENUMSTD, (caddr_t)&vstd,
 3221                     td->td_ucred, td);
 3222                 if (error) {
 3223                         fdrop(fp, td);
 3224                         return (error);
 3225                 }
 3226                 bsd_to_linux_v4l2_standard(&vstd, &l_vstd);
 3227                 error = copyout(&l_vstd, (void *)args->arg, sizeof(l_vstd));
 3228                 fdrop(fp, td);
 3229                 return (error);
 3230 
 3231         case LINUX_VIDIOC_ENUMINPUT:
 3232                 /*
 3233                  * The Linux struct l_v4l2_input differs only in size,
 3234                  * it has no padding at the end.
 3235                  */
 3236                 error = copyin((void *)args->arg, &vinp,
 3237                                 sizeof(struct l_v4l2_input));
 3238                 if (error != 0)
 3239                         return (error);
 3240                 if ((error = fget(td, args->fd, &fp)) != 0)
 3241                         return (error);
 3242                 error = fo_ioctl(fp, VIDIOC_ENUMINPUT, (caddr_t)&vinp,
 3243                     td->td_ucred, td);
 3244                 if (error) {
 3245                         fdrop(fp, td);
 3246                         return (error);
 3247                 }
 3248                 error = copyout(&vinp, (void *)args->arg,
 3249                                 sizeof(struct l_v4l2_input));
 3250                 fdrop(fp, td);
 3251                 return (error);
 3252 
 3253         case LINUX_VIDIOC_QUERYBUF:
 3254         case LINUX_VIDIOC_QBUF:
 3255         case LINUX_VIDIOC_DQBUF:
 3256                 error = copyin((void *)args->arg, &l_vbuf, sizeof(l_vbuf));
 3257                 if (error)
 3258                         return (error);
 3259                 if ((error = fget(td, args->fd, &fp)) != 0)
 3260                         return (error);
 3261                 linux_to_bsd_v4l2_buffer(&l_vbuf, &vbuf);
 3262                 if ((args->cmd & 0xffff) == LINUX_VIDIOC_QUERYBUF)
 3263                         error = fo_ioctl(fp, VIDIOC_QUERYBUF, &vbuf,
 3264                             td->td_ucred, td);
 3265                 else if ((args->cmd & 0xffff) == LINUX_VIDIOC_QBUF)
 3266                         error = fo_ioctl(fp, VIDIOC_QBUF, &vbuf,
 3267                             td->td_ucred, td);
 3268                 else
 3269                         error = fo_ioctl(fp, VIDIOC_DQBUF, &vbuf,
 3270                             td->td_ucred, td);
 3271                 bsd_to_linux_v4l2_buffer(&vbuf, &l_vbuf);
 3272                 copyout(&l_vbuf, (void *)args->arg, sizeof(l_vbuf));
 3273                 fdrop(fp, td);
 3274                 return (error);
 3275 
 3276         /*
 3277          * XXX TODO - these need 32 -> 64 bit conversion:
 3278          * (are any of them needed for webcams?)
 3279          */
 3280         case LINUX_VIDIOC_G_FBUF:
 3281         case LINUX_VIDIOC_S_FBUF:
 3282 
 3283         case LINUX_VIDIOC_G_EXT_CTRLS:
 3284         case LINUX_VIDIOC_S_EXT_CTRLS:
 3285         case LINUX_VIDIOC_TRY_EXT_CTRLS:
 3286 
 3287         case LINUX_VIDIOC_DQEVENT:
 3288 
 3289         default:                        return (ENOIOCTL);
 3290         }
 3291 
 3292         error = ioctl(td, (struct ioctl_args *)args);
 3293         return (error);
 3294 }
 3295 
 3296 /*
 3297  * main ioctl syscall function
 3298  */
 3299 
 3300 int
 3301 linux_ioctl(struct thread *td, struct linux_ioctl_args *args)
 3302 {
 3303         struct file *fp;
 3304         struct handler_element *he;
 3305         int error, cmd;
 3306 
 3307 #ifdef DEBUG
 3308         if (ldebug(ioctl))
 3309                 printf(ARGS(ioctl, "%d, %04lx, *"), args->fd,
 3310                     (unsigned long)args->cmd);
 3311 #endif
 3312 
 3313         if ((error = fget(td, args->fd, &fp)) != 0)
 3314                 return (error);
 3315         if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
 3316                 fdrop(fp, td);
 3317                 return (EBADF);
 3318         }
 3319 
 3320         /* Iterate over the ioctl handlers */
 3321         cmd = args->cmd & 0xffff;
 3322         sx_slock(&linux_ioctl_sx);
 3323         mtx_lock(&Giant);
 3324         TAILQ_FOREACH(he, &handlers, list) {
 3325                 if (cmd >= he->low && cmd <= he->high) {
 3326                         error = (*he->func)(td, args);
 3327                         if (error != ENOIOCTL) {
 3328                                 mtx_unlock(&Giant);
 3329                                 sx_sunlock(&linux_ioctl_sx);
 3330                                 fdrop(fp, td);
 3331                                 return (error);
 3332                         }
 3333                 }
 3334         }
 3335         mtx_unlock(&Giant);
 3336         sx_sunlock(&linux_ioctl_sx);
 3337         fdrop(fp, td);
 3338 
 3339         linux_msg(td, "ioctl fd=%d, cmd=0x%x ('%c',%d) is not implemented",
 3340             args->fd, (int)(args->cmd & 0xffff),
 3341             (int)(args->cmd & 0xff00) >> 8, (int)(args->cmd & 0xff));
 3342 
 3343         return (EINVAL);
 3344 }
 3345 
 3346 int
 3347 linux_ioctl_register_handler(struct linux_ioctl_handler *h)
 3348 {
 3349         struct handler_element *he, *cur;
 3350 
 3351         if (h == NULL || h->func == NULL)
 3352                 return (EINVAL);
 3353 
 3354         /*
 3355          * Reuse the element if the handler is already on the list, otherwise
 3356          * create a new element.
 3357          */
 3358         sx_xlock(&linux_ioctl_sx);
 3359         TAILQ_FOREACH(he, &handlers, list) {
 3360                 if (he->func == h->func)
 3361                         break;
 3362         }
 3363         if (he == NULL) {
 3364                 he = malloc(sizeof(*he),
 3365                     M_LINUX, M_WAITOK);
 3366                 he->func = h->func;
 3367         } else
 3368                 TAILQ_REMOVE(&handlers, he, list);
 3369 
 3370         /* Initialize range information. */
 3371         he->low = h->low;
 3372         he->high = h->high;
 3373         he->span = h->high - h->low + 1;
 3374 
 3375         /* Add the element to the list, sorted on span. */
 3376         TAILQ_FOREACH(cur, &handlers, list) {
 3377                 if (cur->span > he->span) {
 3378                         TAILQ_INSERT_BEFORE(cur, he, list);
 3379                         sx_xunlock(&linux_ioctl_sx);
 3380                         return (0);
 3381                 }
 3382         }
 3383         TAILQ_INSERT_TAIL(&handlers, he, list);
 3384         sx_xunlock(&linux_ioctl_sx);
 3385 
 3386         return (0);
 3387 }
 3388 
 3389 int
 3390 linux_ioctl_unregister_handler(struct linux_ioctl_handler *h)
 3391 {
 3392         struct handler_element *he;
 3393 
 3394         if (h == NULL || h->func == NULL)
 3395                 return (EINVAL);
 3396 
 3397         sx_xlock(&linux_ioctl_sx);
 3398         TAILQ_FOREACH(he, &handlers, list) {
 3399                 if (he->func == h->func) {
 3400                         TAILQ_REMOVE(&handlers, he, list);
 3401                         sx_xunlock(&linux_ioctl_sx);
 3402                         free(he, M_LINUX);
 3403                         return (0);
 3404                 }
 3405         }
 3406         sx_xunlock(&linux_ioctl_sx);
 3407 
 3408         return (EINVAL);
 3409 }

Cache object: 7481405f1bcedcd73265efd670741337


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