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

Cache object: aca9bbc6645ce4444c9adbb858e36ddd


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