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

Cache object: c7d0933d1de016a25ff9edc44948893c


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