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


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

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

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

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

Cache object: e71361ae8f44d30a9453e571e0dbdd1c


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