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

Cache object: 5e7415de884cc4aaaee1f611f6b69616


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