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

Cache object: 2974f1c8945e5469a39b1b2a2cfeb301


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