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/dev/usb/usb_dev.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 /* $FreeBSD$ */
    2 /*-
    3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    4  *
    5  * Copyright (c) 2006-2008 Hans Petter Selasky. 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  * usb_dev.c - An abstraction layer for creating devices under /dev/...
   30  */
   31 
   32 #ifdef USB_GLOBAL_INCLUDE_FILE
   33 #include USB_GLOBAL_INCLUDE_FILE
   34 #else
   35 #ifdef COMPAT_FREEBSD32
   36 #include <sys/abi_compat.h>
   37 #endif
   38 #include <sys/stdint.h>
   39 #include <sys/stddef.h>
   40 #include <sys/param.h>
   41 #include <sys/queue.h>
   42 #include <sys/types.h>
   43 #include <sys/systm.h>
   44 #include <sys/kernel.h>
   45 #include <sys/bus.h>
   46 #include <sys/module.h>
   47 #include <sys/lock.h>
   48 #include <sys/mutex.h>
   49 #include <sys/condvar.h>
   50 #include <sys/sysctl.h>
   51 #include <sys/sx.h>
   52 #include <sys/unistd.h>
   53 #include <sys/callout.h>
   54 #include <sys/malloc.h>
   55 #include <sys/priv.h>
   56 #include <sys/vnode.h>
   57 #include <sys/conf.h>
   58 #include <sys/fcntl.h>
   59 
   60 #include <dev/usb/usb.h>
   61 #include <dev/usb/usb_ioctl.h>
   62 #include <dev/usb/usbdi.h>
   63 #include <dev/usb/usbdi_util.h>
   64 
   65 #define USB_DEBUG_VAR usb_fifo_debug
   66 
   67 #include <dev/usb/usb_core.h>
   68 #include <dev/usb/usb_dev.h>
   69 #include <dev/usb/usb_mbuf.h>
   70 #include <dev/usb/usb_process.h>
   71 #include <dev/usb/usb_device.h>
   72 #include <dev/usb/usb_debug.h>
   73 #include <dev/usb/usb_busdma.h>
   74 #include <dev/usb/usb_generic.h>
   75 #include <dev/usb/usb_dynamic.h>
   76 #include <dev/usb/usb_util.h>
   77 
   78 #include <dev/usb/usb_controller.h>
   79 #include <dev/usb/usb_bus.h>
   80 
   81 #include <sys/filio.h>
   82 #include <sys/ttycom.h>
   83 #include <sys/syscallsubr.h>
   84 
   85 #include <machine/stdarg.h>
   86 #endif                  /* USB_GLOBAL_INCLUDE_FILE */
   87 
   88 #if USB_HAVE_UGEN
   89 
   90 #ifdef USB_DEBUG
   91 static int usb_fifo_debug = 0;
   92 
   93 static SYSCTL_NODE(_hw_usb, OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
   94     "USB device");
   95 SYSCTL_INT(_hw_usb_dev, OID_AUTO, debug, CTLFLAG_RWTUN,
   96     &usb_fifo_debug, 0, "Debug Level");
   97 #endif
   98 
   99 #if ((__FreeBSD_version >= 700001) || (__FreeBSD_version == 0) || \
  100      ((__FreeBSD_version >= 600034) && (__FreeBSD_version < 700000)))
  101 #define USB_UCRED struct ucred *ucred,
  102 #else
  103 #define USB_UCRED
  104 #endif
  105 
  106 /* prototypes */
  107 
  108 static int      usb_fifo_open(struct usb_cdev_privdata *, 
  109                     struct usb_fifo *, int);
  110 static void     usb_fifo_close(struct usb_fifo *, int);
  111 static void     usb_dev_init(void *);
  112 static void     usb_dev_init_post(void *);
  113 static void     usb_dev_uninit(void *);
  114 static int      usb_fifo_uiomove(struct usb_fifo *, void *, int,
  115                     struct uio *);
  116 static void     usb_fifo_check_methods(struct usb_fifo_methods *);
  117 static struct   usb_fifo *usb_fifo_alloc(struct mtx *);
  118 static struct   usb_endpoint *usb_dev_get_ep(struct usb_device *, uint8_t,
  119                     uint8_t);
  120 static void     usb_loc_fill(struct usb_fs_privdata *,
  121                     struct usb_cdev_privdata *);
  122 static void     usb_close(void *);
  123 static usb_error_t usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *, int);
  124 static usb_error_t usb_usb_ref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
  125 static void     usb_unref_device(struct usb_cdev_privdata *, struct usb_cdev_refdata *);
  126 
  127 static d_open_t usb_open;
  128 static d_ioctl_t usb_ioctl;
  129 static d_read_t usb_read;
  130 static d_write_t usb_write;
  131 static d_poll_t usb_poll;
  132 static d_kqfilter_t usb_kqfilter;
  133 
  134 static d_ioctl_t usb_static_ioctl;
  135 
  136 static usb_fifo_open_t usb_fifo_dummy_open;
  137 static usb_fifo_close_t usb_fifo_dummy_close;
  138 static usb_fifo_ioctl_t usb_fifo_dummy_ioctl;
  139 static usb_fifo_cmd_t usb_fifo_dummy_cmd;
  140 
  141 /* character device structure used for devices (/dev/ugenX.Y and /dev/uXXX) */
  142 struct cdevsw usb_devsw = {
  143         .d_version = D_VERSION,
  144         .d_open = usb_open,
  145         .d_ioctl = usb_ioctl,
  146         .d_name = "usbdev",
  147         .d_flags = D_TRACKCLOSE,
  148         .d_read = usb_read,
  149         .d_write = usb_write,
  150         .d_poll = usb_poll,
  151         .d_kqfilter = usb_kqfilter,
  152 };
  153 
  154 static struct cdev* usb_dev = NULL;
  155 
  156 /* character device structure used for /dev/usb */
  157 static struct cdevsw usb_static_devsw = {
  158         .d_version = D_VERSION,
  159         .d_ioctl = usb_static_ioctl,
  160         .d_name = "usb"
  161 };
  162 
  163 static TAILQ_HEAD(, usb_symlink) usb_sym_head;
  164 static struct sx usb_sym_lock;
  165 
  166 struct mtx usb_ref_lock;
  167 
  168 /*------------------------------------------------------------------------*
  169  *      usb_loc_fill
  170  *
  171  * This is used to fill out a usb_cdev_privdata structure based on the
  172  * device's address as contained in usb_fs_privdata.
  173  *------------------------------------------------------------------------*/
  174 static void
  175 usb_loc_fill(struct usb_fs_privdata* pd, struct usb_cdev_privdata *cpd)
  176 {
  177         cpd->bus_index = pd->bus_index;
  178         cpd->dev_index = pd->dev_index;
  179         cpd->ep_addr = pd->ep_addr;
  180         cpd->fifo_index = pd->fifo_index;
  181 }
  182 
  183 /*------------------------------------------------------------------------*
  184  *      usb_ref_device
  185  *
  186  * This function is used to atomically refer an USB device by its
  187  * device location. If this function returns success the USB device
  188  * will not disappear until the USB device is unreferenced.
  189  *
  190  * Return values:
  191  *  0: Success, refcount incremented on the given USB device.
  192  *  Else: Failure.
  193  *------------------------------------------------------------------------*/
  194 static usb_error_t
  195 usb_ref_device(struct usb_cdev_privdata *cpd, 
  196     struct usb_cdev_refdata *crd, int need_uref)
  197 {
  198         struct usb_fifo **ppf;
  199         struct usb_fifo *f;
  200 
  201         DPRINTFN(2, "cpd=%p need uref=%d\n", cpd, need_uref);
  202 
  203         /* clear all refs */
  204         memset(crd, 0, sizeof(*crd));
  205 
  206         mtx_lock(&usb_ref_lock);
  207         cpd->bus = devclass_get_softc(usb_devclass_ptr, cpd->bus_index);
  208         if (cpd->bus == NULL) {
  209                 DPRINTFN(2, "no bus at %u\n", cpd->bus_index);
  210                 goto error;
  211         }
  212         cpd->udev = cpd->bus->devices[cpd->dev_index];
  213         if (cpd->udev == NULL) {
  214                 DPRINTFN(2, "no device at %u\n", cpd->dev_index);
  215                 goto error;
  216         }
  217         if (cpd->udev->state == USB_STATE_DETACHED &&
  218             (need_uref != 2)) {
  219                 DPRINTFN(2, "device is detached\n");
  220                 goto error;
  221         }
  222         if (need_uref) {
  223                 DPRINTFN(2, "ref udev - needed\n");
  224 
  225                 if (cpd->udev->refcount == USB_DEV_REF_MAX) {
  226                         DPRINTFN(2, "no dev ref\n");
  227                         goto error;
  228                 }
  229                 cpd->udev->refcount++;
  230 
  231                 mtx_unlock(&usb_ref_lock);
  232 
  233                 /*
  234                  * We need to grab the enumeration SX-lock before
  235                  * grabbing the FIFO refs to avoid deadlock at detach!
  236                  */
  237                 crd->do_unlock = usbd_enum_lock_sig(cpd->udev);
  238 
  239                 mtx_lock(&usb_ref_lock);
  240 
  241                 /* 
  242                  * Set "is_uref" after grabbing the default SX lock
  243                  */
  244                 crd->is_uref = 1;
  245 
  246                 /* check for signal */
  247                 if (crd->do_unlock > 1) {
  248                         crd->do_unlock = 0;
  249                         goto error;
  250                 }
  251         }
  252 
  253         /* check if we are doing an open */
  254         if (cpd->fflags == 0) {
  255                 /* use zero defaults */
  256         } else {
  257                 /* check for write */
  258                 if (cpd->fflags & FWRITE) {
  259                         ppf = cpd->udev->fifo;
  260                         f = ppf[cpd->fifo_index + USB_FIFO_TX];
  261                         crd->txfifo = f;
  262                         crd->is_write = 1;      /* ref */
  263                         if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
  264                                 goto error;
  265                         if (f->curr_cpd != cpd)
  266                                 goto error;
  267                         /* check if USB-FS is active */
  268                         if (f->fs_ep_max != 0) {
  269                                 crd->is_usbfs = 1;
  270                         }
  271                 }
  272 
  273                 /* check for read */
  274                 if (cpd->fflags & FREAD) {
  275                         ppf = cpd->udev->fifo;
  276                         f = ppf[cpd->fifo_index + USB_FIFO_RX];
  277                         crd->rxfifo = f;
  278                         crd->is_read = 1;       /* ref */
  279                         if (f == NULL || f->refcount == USB_FIFO_REF_MAX)
  280                                 goto error;
  281                         if (f->curr_cpd != cpd)
  282                                 goto error;
  283                         /* check if USB-FS is active */
  284                         if (f->fs_ep_max != 0) {
  285                                 crd->is_usbfs = 1;
  286                         }
  287                 }
  288         }
  289 
  290         /* when everything is OK we increment the refcounts */
  291         if (crd->is_write) {
  292                 DPRINTFN(2, "ref write\n");
  293                 crd->txfifo->refcount++;
  294         }
  295         if (crd->is_read) {
  296                 DPRINTFN(2, "ref read\n");
  297                 crd->rxfifo->refcount++;
  298         }
  299         mtx_unlock(&usb_ref_lock);
  300 
  301         return (0);
  302 
  303 error:
  304         if (crd->do_unlock)
  305                 usbd_enum_unlock(cpd->udev);
  306 
  307         if (crd->is_uref) {
  308                 if (--(cpd->udev->refcount) == 0)
  309                         cv_broadcast(&cpd->udev->ref_cv);
  310         }
  311         mtx_unlock(&usb_ref_lock);
  312         DPRINTFN(2, "fail\n");
  313 
  314         /* clear all refs */
  315         memset(crd, 0, sizeof(*crd));
  316 
  317         return (USB_ERR_INVAL);
  318 }
  319 
  320 /*------------------------------------------------------------------------*
  321  *      usb_usb_ref_device
  322  *
  323  * This function is used to upgrade an USB reference to include the
  324  * USB device reference on a USB location.
  325  *
  326  * Return values:
  327  *  0: Success, refcount incremented on the given USB device.
  328  *  Else: Failure.
  329  *------------------------------------------------------------------------*/
  330 static usb_error_t
  331 usb_usb_ref_device(struct usb_cdev_privdata *cpd,
  332     struct usb_cdev_refdata *crd)
  333 {
  334         /*
  335          * Check if we already got an USB reference on this location:
  336          */
  337         if (crd->is_uref)
  338                 return (0);             /* success */
  339 
  340         /*
  341          * To avoid deadlock at detach we need to drop the FIFO ref
  342          * and re-acquire a new ref!
  343          */
  344         usb_unref_device(cpd, crd);
  345 
  346         return (usb_ref_device(cpd, crd, 1 /* need uref */));
  347 }
  348 
  349 /*------------------------------------------------------------------------*
  350  *      usb_unref_device
  351  *
  352  * This function will release the reference count by one unit for the
  353  * given USB device.
  354  *------------------------------------------------------------------------*/
  355 static void
  356 usb_unref_device(struct usb_cdev_privdata *cpd,
  357     struct usb_cdev_refdata *crd)
  358 {
  359 
  360         DPRINTFN(2, "cpd=%p is_uref=%d\n", cpd, crd->is_uref);
  361 
  362         if (crd->do_unlock)
  363                 usbd_enum_unlock(cpd->udev);
  364 
  365         mtx_lock(&usb_ref_lock);
  366         if (crd->is_read) {
  367                 if (--(crd->rxfifo->refcount) == 0) {
  368                         cv_signal(&crd->rxfifo->cv_drain);
  369                 }
  370                 crd->is_read = 0;
  371         }
  372         if (crd->is_write) {
  373                 if (--(crd->txfifo->refcount) == 0) {
  374                         cv_signal(&crd->txfifo->cv_drain);
  375                 }
  376                 crd->is_write = 0;
  377         }
  378         if (crd->is_uref) {
  379                 crd->is_uref = 0;
  380                 if (--(cpd->udev->refcount) == 0)
  381                         cv_broadcast(&cpd->udev->ref_cv);
  382         }
  383         mtx_unlock(&usb_ref_lock);
  384 }
  385 
  386 static struct usb_fifo *
  387 usb_fifo_alloc(struct mtx *mtx)
  388 {
  389         struct usb_fifo *f;
  390 
  391         f = malloc(sizeof(*f), M_USBDEV, M_WAITOK | M_ZERO);
  392         cv_init(&f->cv_io, "FIFO-IO");
  393         cv_init(&f->cv_drain, "FIFO-DRAIN");
  394         f->priv_mtx = mtx;
  395         f->refcount = 1;
  396         knlist_init_mtx(&f->selinfo.si_note, mtx);
  397         return (f);
  398 }
  399 
  400 /*------------------------------------------------------------------------*
  401  *      usb_fifo_create
  402  *------------------------------------------------------------------------*/
  403 static int
  404 usb_fifo_create(struct usb_cdev_privdata *cpd,
  405     struct usb_cdev_refdata *crd)
  406 {
  407         struct usb_device *udev = cpd->udev;
  408         struct usb_fifo *f;
  409         struct usb_endpoint *ep;
  410         uint8_t n;
  411         uint8_t is_tx;
  412         uint8_t is_rx;
  413         uint8_t no_null;
  414         uint8_t is_busy;
  415         int e = cpd->ep_addr;
  416 
  417         is_tx = (cpd->fflags & FWRITE) ? 1 : 0;
  418         is_rx = (cpd->fflags & FREAD) ? 1 : 0;
  419         no_null = 1;
  420         is_busy = 0;
  421 
  422         /* Preallocated FIFO */
  423         if (e < 0) {
  424                 DPRINTFN(5, "Preallocated FIFO\n");
  425                 if (is_tx) {
  426                         f = udev->fifo[cpd->fifo_index + USB_FIFO_TX];
  427                         if (f == NULL)
  428                                 return (EINVAL);
  429                         crd->txfifo = f;
  430                 }
  431                 if (is_rx) {
  432                         f = udev->fifo[cpd->fifo_index + USB_FIFO_RX];
  433                         if (f == NULL)
  434                                 return (EINVAL);
  435                         crd->rxfifo = f;
  436                 }
  437                 return (0);
  438         }
  439 
  440         KASSERT(e >= 0 && e <= 15, ("endpoint %d out of range", e));
  441 
  442         /* search for a free FIFO slot */
  443         DPRINTFN(5, "Endpoint device, searching for 0x%02x\n", e);
  444         for (n = 0;; n += 2) {
  445                 if (n == USB_FIFO_MAX) {
  446                         if (no_null) {
  447                                 no_null = 0;
  448                                 n = 0;
  449                         } else {
  450                                 /* end of FIFOs reached */
  451                                 DPRINTFN(5, "out of FIFOs\n");
  452                                 return (ENOMEM);
  453                         }
  454                 }
  455                 /* Check for TX FIFO */
  456                 if (is_tx) {
  457                         f = udev->fifo[n + USB_FIFO_TX];
  458                         if (f != NULL) {
  459                                 if (f->dev_ep_index != e) {
  460                                         /* wrong endpoint index */
  461                                         continue;
  462                                 }
  463                                 if (f->curr_cpd != NULL) {
  464                                         /* FIFO is opened */
  465                                         is_busy = 1;
  466                                         continue;
  467                                 }
  468                         } else if (no_null) {
  469                                 continue;
  470                         }
  471                 }
  472                 /* Check for RX FIFO */
  473                 if (is_rx) {
  474                         f = udev->fifo[n + USB_FIFO_RX];
  475                         if (f != NULL) {
  476                                 if (f->dev_ep_index != e) {
  477                                         /* wrong endpoint index */
  478                                         continue;
  479                                 }
  480                                 if (f->curr_cpd != NULL) {
  481                                         /* FIFO is opened */
  482                                         is_busy = 1;
  483                                         continue;
  484                                 }
  485                         } else if (no_null) {
  486                                 continue;
  487                         }
  488                 }
  489                 break;
  490         }
  491 
  492         if (no_null == 0) {
  493                 if (e >= (USB_EP_MAX / 2)) {
  494                         /* we don't create any endpoints in this range */
  495                         DPRINTFN(5, "ep out of range\n");
  496                         return (is_busy ? EBUSY : EINVAL);
  497                 }
  498         }
  499 
  500         if ((e != 0) && is_busy) {
  501                 /*
  502                  * Only the default control endpoint is allowed to be
  503                  * opened multiple times!
  504                  */
  505                 DPRINTFN(5, "busy\n");
  506                 return (EBUSY);
  507         }
  508 
  509         /* Check TX FIFO */
  510         if (is_tx &&
  511             (udev->fifo[n + USB_FIFO_TX] == NULL)) {
  512                 ep = usb_dev_get_ep(udev, e, USB_FIFO_TX);
  513                 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_TX);
  514                 if (ep == NULL) {
  515                         DPRINTFN(5, "dev_get_endpoint returned NULL\n");
  516                         return (EINVAL);
  517                 }
  518                 f = usb_fifo_alloc(&udev->device_mtx);
  519                 if (f == NULL) {
  520                         DPRINTFN(5, "could not alloc tx fifo\n");
  521                         return (ENOMEM);
  522                 }
  523                 /* update some fields */
  524                 f->fifo_index = n + USB_FIFO_TX;
  525                 f->dev_ep_index = e;
  526                 f->priv_sc0 = ep;
  527                 f->methods = &usb_ugen_methods;
  528                 f->iface_index = ep->iface_index;
  529                 f->udev = udev;
  530                 mtx_lock(&usb_ref_lock);
  531                 udev->fifo[n + USB_FIFO_TX] = f;
  532                 mtx_unlock(&usb_ref_lock);
  533         }
  534         /* Check RX FIFO */
  535         if (is_rx &&
  536             (udev->fifo[n + USB_FIFO_RX] == NULL)) {
  537                 ep = usb_dev_get_ep(udev, e, USB_FIFO_RX);
  538                 DPRINTFN(5, "dev_get_endpoint(%d, 0x%x)\n", e, USB_FIFO_RX);
  539                 if (ep == NULL) {
  540                         DPRINTFN(5, "dev_get_endpoint returned NULL\n");
  541                         return (EINVAL);
  542                 }
  543                 f = usb_fifo_alloc(&udev->device_mtx);
  544                 if (f == NULL) {
  545                         DPRINTFN(5, "could not alloc rx fifo\n");
  546                         return (ENOMEM);
  547                 }
  548                 /* update some fields */
  549                 f->fifo_index = n + USB_FIFO_RX;
  550                 f->dev_ep_index = e;
  551                 f->priv_sc0 = ep;
  552                 f->methods = &usb_ugen_methods;
  553                 f->iface_index = ep->iface_index;
  554                 f->udev = udev;
  555                 mtx_lock(&usb_ref_lock);
  556                 udev->fifo[n + USB_FIFO_RX] = f;
  557                 mtx_unlock(&usb_ref_lock);
  558         }
  559         if (is_tx) {
  560                 crd->txfifo = udev->fifo[n + USB_FIFO_TX];
  561         }
  562         if (is_rx) {
  563                 crd->rxfifo = udev->fifo[n + USB_FIFO_RX];
  564         }
  565         /* fill out fifo index */
  566         DPRINTFN(5, "fifo index = %d\n", n);
  567         cpd->fifo_index = n;
  568 
  569         /* complete */
  570 
  571         return (0);
  572 }
  573 
  574 void
  575 usb_fifo_free(struct usb_fifo *f)
  576 {
  577         uint8_t n;
  578 
  579         if (f == NULL) {
  580                 /* be NULL safe */
  581                 return;
  582         }
  583         /* destroy symlink devices, if any */
  584         for (n = 0; n != 2; n++) {
  585                 if (f->symlink[n]) {
  586                         usb_free_symlink(f->symlink[n]);
  587                         f->symlink[n] = NULL;
  588                 }
  589         }
  590         mtx_lock(&usb_ref_lock);
  591 
  592         /* delink ourselves to stop calls from userland */
  593         if ((f->fifo_index < USB_FIFO_MAX) &&
  594             (f->udev != NULL) &&
  595             (f->udev->fifo[f->fifo_index] == f)) {
  596                 f->udev->fifo[f->fifo_index] = NULL;
  597         } else {
  598                 DPRINTFN(0, "USB FIFO %p has not been linked\n", f);
  599         }
  600 
  601         /* decrease refcount */
  602         f->refcount--;
  603         /* need to wait until all callers have exited */
  604         while (f->refcount != 0) {
  605                 mtx_unlock(&usb_ref_lock);      /* avoid LOR */
  606                 mtx_lock(f->priv_mtx);
  607                 /* prevent write flush, if any */
  608                 f->flag_iserror = 1;
  609                 /* get I/O thread out of any sleep state */
  610                 if (f->flag_sleeping) {
  611                         f->flag_sleeping = 0;
  612                         cv_broadcast(&f->cv_io);
  613                 }
  614                 mtx_unlock(f->priv_mtx);
  615                 mtx_lock(&usb_ref_lock);
  616 
  617                 /*
  618                  * Check if the "f->refcount" variable reached zero
  619                  * during the unlocked time before entering wait:
  620                  */
  621                 if (f->refcount == 0)
  622                         break;
  623 
  624                 /* wait for sync */
  625                 cv_wait(&f->cv_drain, &usb_ref_lock);
  626         }
  627         mtx_unlock(&usb_ref_lock);
  628 
  629         /* take care of closing the device here, if any */
  630         usb_fifo_close(f, 0);
  631 
  632         cv_destroy(&f->cv_io);
  633         cv_destroy(&f->cv_drain);
  634 
  635         knlist_clear(&f->selinfo.si_note, 0);
  636         seldrain(&f->selinfo);
  637         knlist_destroy(&f->selinfo.si_note);
  638 
  639         free(f, M_USBDEV);
  640 }
  641 
  642 static struct usb_endpoint *
  643 usb_dev_get_ep(struct usb_device *udev, uint8_t ep_index, uint8_t dir)
  644 {
  645         struct usb_endpoint *ep;
  646         uint8_t ep_dir;
  647 
  648         if (ep_index == 0) {
  649                 ep = &udev->ctrl_ep;
  650         } else {
  651                 if (dir == USB_FIFO_RX) {
  652                         if (udev->flags.usb_mode == USB_MODE_HOST) {
  653                                 ep_dir = UE_DIR_IN;
  654                         } else {
  655                                 ep_dir = UE_DIR_OUT;
  656                         }
  657                 } else {
  658                         if (udev->flags.usb_mode == USB_MODE_HOST) {
  659                                 ep_dir = UE_DIR_OUT;
  660                         } else {
  661                                 ep_dir = UE_DIR_IN;
  662                         }
  663                 }
  664                 ep = usbd_get_ep_by_addr(udev, ep_index | ep_dir);
  665         }
  666 
  667         if (ep == NULL) {
  668                 /* if the endpoint does not exist then return */
  669                 return (NULL);
  670         }
  671         if (ep->edesc == NULL) {
  672                 /* invalid endpoint */
  673                 return (NULL);
  674         }
  675         return (ep);                    /* success */
  676 }
  677 
  678 /*------------------------------------------------------------------------*
  679  *      usb_fifo_open
  680  *
  681  * Returns:
  682  * 0: Success
  683  * Else: Failure
  684  *------------------------------------------------------------------------*/
  685 static int
  686 usb_fifo_open(struct usb_cdev_privdata *cpd, 
  687     struct usb_fifo *f, int fflags)
  688 {
  689         int err;
  690 
  691         if (f == NULL) {
  692                 /* no FIFO there */
  693                 DPRINTFN(2, "no FIFO\n");
  694                 return (ENXIO);
  695         }
  696         /* remove FWRITE and FREAD flags */
  697         fflags &= ~(FWRITE | FREAD);
  698 
  699         /* set correct file flags */
  700         if ((f->fifo_index & 1) == USB_FIFO_TX) {
  701                 fflags |= FWRITE;
  702         } else {
  703                 fflags |= FREAD;
  704         }
  705 
  706         /* check if we are already opened */
  707         /* we don't need any locks when checking this variable */
  708         if (f->curr_cpd != NULL) {
  709                 err = EBUSY;
  710                 goto done;
  711         }
  712 
  713         /* reset short flag before open */
  714         f->flag_short = 0;
  715 
  716         /* call open method */
  717         err = (f->methods->f_open) (f, fflags);
  718         if (err) {
  719                 goto done;
  720         }
  721         mtx_lock(f->priv_mtx);
  722 
  723         /* reset sleep flag */
  724         f->flag_sleeping = 0;
  725 
  726         /* reset error flag */
  727         f->flag_iserror = 0;
  728 
  729         /* reset complete flag */
  730         f->flag_iscomplete = 0;
  731 
  732         /* reset select flag */
  733         f->flag_isselect = 0;
  734 
  735         /* reset flushing flag */
  736         f->flag_flushing = 0;
  737 
  738         /* reset ASYNC proc flag */
  739         f->async_p = NULL;
  740 
  741         mtx_lock(&usb_ref_lock);
  742         /* flag the fifo as opened to prevent others */
  743         f->curr_cpd = cpd;
  744         mtx_unlock(&usb_ref_lock);
  745 
  746         /* reset queue */
  747         usb_fifo_reset(f);
  748 
  749         mtx_unlock(f->priv_mtx);
  750 done:
  751         return (err);
  752 }
  753 
  754 /*------------------------------------------------------------------------*
  755  *      usb_fifo_reset
  756  *------------------------------------------------------------------------*/
  757 void
  758 usb_fifo_reset(struct usb_fifo *f)
  759 {
  760         struct usb_mbuf *m;
  761 
  762         if (f == NULL) {
  763                 return;
  764         }
  765         while (1) {
  766                 USB_IF_DEQUEUE(&f->used_q, m);
  767                 if (m) {
  768                         USB_IF_ENQUEUE(&f->free_q, m);
  769                 } else {
  770                         break;
  771                 }
  772         }
  773         /* reset have fragment flag */
  774         f->flag_have_fragment = 0;
  775 }
  776 
  777 /*------------------------------------------------------------------------*
  778  *      usb_fifo_close
  779  *------------------------------------------------------------------------*/
  780 static void
  781 usb_fifo_close(struct usb_fifo *f, int fflags)
  782 {
  783         int err;
  784 
  785         /* check if we are not opened */
  786         if (f->curr_cpd == NULL) {
  787                 /* nothing to do - already closed */
  788                 return;
  789         }
  790         mtx_lock(f->priv_mtx);
  791 
  792         /* clear current cdev private data pointer */
  793         mtx_lock(&usb_ref_lock);
  794         f->curr_cpd = NULL;
  795         mtx_unlock(&usb_ref_lock);
  796 
  797         /* check if we are watched by kevent */
  798         KNOTE_LOCKED(&f->selinfo.si_note, 0);
  799 
  800         /* check if we are selected */
  801         if (f->flag_isselect) {
  802                 selwakeup(&f->selinfo);
  803                 f->flag_isselect = 0;
  804         }
  805         /* check if a thread wants SIGIO */
  806         if (f->async_p != NULL) {
  807                 PROC_LOCK(f->async_p);
  808                 kern_psignal(f->async_p, SIGIO);
  809                 PROC_UNLOCK(f->async_p);
  810                 f->async_p = NULL;
  811         }
  812         /* remove FWRITE and FREAD flags */
  813         fflags &= ~(FWRITE | FREAD);
  814 
  815         /* flush written data, if any */
  816         if ((f->fifo_index & 1) == USB_FIFO_TX) {
  817                 if (!f->flag_iserror) {
  818                         /* set flushing flag */
  819                         f->flag_flushing = 1;
  820 
  821                         /* get the last packet in */
  822                         if (f->flag_have_fragment) {
  823                                 struct usb_mbuf *m;
  824                                 f->flag_have_fragment = 0;
  825                                 USB_IF_DEQUEUE(&f->free_q, m);
  826                                 if (m) {
  827                                         USB_IF_ENQUEUE(&f->used_q, m);
  828                                 }
  829                         }
  830 
  831                         /* start write transfer, if not already started */
  832                         (f->methods->f_start_write) (f);
  833 
  834                         /* check if flushed already */
  835                         while (f->flag_flushing &&
  836                             (!f->flag_iserror)) {
  837                                 /* wait until all data has been written */
  838                                 f->flag_sleeping = 1;
  839                                 err = cv_timedwait_sig(&f->cv_io, f->priv_mtx,
  840                                     USB_MS_TO_TICKS(USB_DEFAULT_TIMEOUT));
  841                                 if (err) {
  842                                         DPRINTF("signal received\n");
  843                                         break;
  844                                 }
  845                         }
  846                 }
  847                 fflags |= FWRITE;
  848 
  849                 /* stop write transfer, if not already stopped */
  850                 (f->methods->f_stop_write) (f);
  851         } else {
  852                 fflags |= FREAD;
  853 
  854                 /* stop write transfer, if not already stopped */
  855                 (f->methods->f_stop_read) (f);
  856         }
  857 
  858         /* check if we are sleeping */
  859         if (f->flag_sleeping) {
  860                 DPRINTFN(2, "Sleeping at close!\n");
  861         }
  862         mtx_unlock(f->priv_mtx);
  863 
  864         /* call close method */
  865         (f->methods->f_close) (f, fflags);
  866 
  867         DPRINTF("closed\n");
  868 }
  869 
  870 /*------------------------------------------------------------------------*
  871  *      usb_open - cdev callback
  872  *------------------------------------------------------------------------*/
  873 static int
  874 usb_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
  875 {
  876         struct usb_fs_privdata* pd = (struct usb_fs_privdata*)dev->si_drv1;
  877         struct usb_cdev_refdata refs;
  878         struct usb_cdev_privdata *cpd;
  879         int err;
  880 
  881         DPRINTFN(2, "%s fflags=0x%08x\n", devtoname(dev), fflags);
  882 
  883         KASSERT(fflags & (FREAD|FWRITE), ("invalid open flags"));
  884         if (((fflags & FREAD) && !(pd->mode & FREAD)) ||
  885             ((fflags & FWRITE) && !(pd->mode & FWRITE))) {
  886                 DPRINTFN(2, "access mode not supported\n");
  887                 return (EPERM);
  888         }
  889 
  890         cpd = malloc(sizeof(*cpd), M_USBDEV, M_WAITOK | M_ZERO);
  891 
  892         usb_loc_fill(pd, cpd);
  893         err = usb_ref_device(cpd, &refs, 1);
  894         if (err) {
  895                 DPRINTFN(2, "cannot ref device\n");
  896                 free(cpd, M_USBDEV);
  897                 return (ENXIO);
  898         }
  899         cpd->fflags = fflags;   /* access mode for open lifetime */
  900 
  901         /* create FIFOs, if any */
  902         err = usb_fifo_create(cpd, &refs);
  903         /* check for error */
  904         if (err) {
  905                 DPRINTFN(2, "cannot create fifo\n");
  906                 usb_unref_device(cpd, &refs);
  907                 free(cpd, M_USBDEV);
  908                 return (err);
  909         }
  910         if (fflags & FREAD) {
  911                 err = usb_fifo_open(cpd, refs.rxfifo, fflags);
  912                 if (err) {
  913                         DPRINTFN(2, "read open failed\n");
  914                         usb_unref_device(cpd, &refs);
  915                         free(cpd, M_USBDEV);
  916                         return (err);
  917                 }
  918         }
  919         if (fflags & FWRITE) {
  920                 err = usb_fifo_open(cpd, refs.txfifo, fflags);
  921                 if (err) {
  922                         DPRINTFN(2, "write open failed\n");
  923                         if (fflags & FREAD) {
  924                                 usb_fifo_close(refs.rxfifo, fflags);
  925                         }
  926                         usb_unref_device(cpd, &refs);
  927                         free(cpd, M_USBDEV);
  928                         return (err);
  929                 }
  930         }
  931         usb_unref_device(cpd, &refs);
  932         devfs_set_cdevpriv(cpd, usb_close);
  933 
  934         return (0);
  935 }
  936 
  937 /*------------------------------------------------------------------------*
  938  *      usb_close - cdev callback
  939  *------------------------------------------------------------------------*/
  940 static void
  941 usb_close(void *arg)
  942 {
  943         struct usb_cdev_refdata refs;
  944         struct usb_cdev_privdata *cpd = arg;
  945         int err;
  946 
  947         DPRINTFN(2, "cpd=%p\n", cpd);
  948 
  949         err = usb_ref_device(cpd, &refs,
  950             2 /* uref and allow detached state */);
  951         if (err) {
  952                 DPRINTFN(2, "Cannot grab USB reference when "
  953                     "closing USB file handle\n");
  954                 goto done;
  955         }
  956         if (cpd->fflags & FREAD) {
  957                 usb_fifo_close(refs.rxfifo, cpd->fflags);
  958         }
  959         if (cpd->fflags & FWRITE) {
  960                 usb_fifo_close(refs.txfifo, cpd->fflags);
  961         }
  962         usb_unref_device(cpd, &refs);
  963 done:
  964         free(cpd, M_USBDEV);
  965 }
  966 
  967 static void
  968 usb_dev_init(void *arg)
  969 {
  970         mtx_init(&usb_ref_lock, "USB ref mutex", NULL, MTX_DEF);
  971         sx_init(&usb_sym_lock, "USB sym mutex");
  972         TAILQ_INIT(&usb_sym_head);
  973 
  974         /* check the UGEN methods */
  975         usb_fifo_check_methods(&usb_ugen_methods);
  976 }
  977 
  978 SYSINIT(usb_dev_init, SI_SUB_KLD, SI_ORDER_FIRST, usb_dev_init, NULL);
  979 
  980 static void
  981 usb_dev_init_post(void *arg)
  982 {
  983         /*
  984          * Create /dev/usb - this is needed for usbconfig(8), which
  985          * needs a well-known device name to access.
  986          */
  987         usb_dev = make_dev(&usb_static_devsw, 0, UID_ROOT, GID_OPERATOR,
  988             0644, USB_DEVICE_NAME);
  989         if (usb_dev == NULL) {
  990                 DPRINTFN(0, "Could not create usb bus device\n");
  991         }
  992 }
  993 
  994 SYSINIT(usb_dev_init_post, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, usb_dev_init_post, NULL);
  995 
  996 static void
  997 usb_dev_uninit(void *arg)
  998 {
  999         if (usb_dev != NULL) {
 1000                 destroy_dev(usb_dev);
 1001                 usb_dev = NULL;
 1002         }
 1003         mtx_destroy(&usb_ref_lock);
 1004         sx_destroy(&usb_sym_lock);
 1005 }
 1006 
 1007 SYSUNINIT(usb_dev_uninit, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_dev_uninit, NULL);
 1008 
 1009 static int
 1010 usb_ioctl_f_sub(struct usb_fifo *f, u_long cmd, void *addr,
 1011     struct thread *td)
 1012 {
 1013         int error = 0;
 1014 
 1015         switch (cmd) {
 1016         case FIODTYPE:
 1017                 *(int *)addr = 0;       /* character device */
 1018                 break;
 1019 
 1020         case FIONBIO:
 1021                 /* handled by upper FS layer */
 1022                 break;
 1023 
 1024         case FIOASYNC:
 1025                 if (*(int *)addr) {
 1026                         if (f->async_p != NULL) {
 1027                                 error = EBUSY;
 1028                                 break;
 1029                         }
 1030                         f->async_p = USB_TD_GET_PROC(td);
 1031                 } else {
 1032                         f->async_p = NULL;
 1033                 }
 1034                 break;
 1035 
 1036                 /* XXX this is not the most general solution */
 1037         case TIOCSPGRP:
 1038                 if (f->async_p == NULL) {
 1039                         error = EINVAL;
 1040                         break;
 1041                 }
 1042                 if (*(int *)addr != USB_PROC_GET_GID(f->async_p)) {
 1043                         error = EPERM;
 1044                         break;
 1045                 }
 1046                 break;
 1047         default:
 1048                 return (ENOIOCTL);
 1049         }
 1050         DPRINTFN(3, "cmd 0x%lx = %d\n", cmd, error);
 1051         return (error);
 1052 }
 1053 
 1054 /*------------------------------------------------------------------------*
 1055  *      usb_ioctl - cdev callback
 1056  *------------------------------------------------------------------------*/
 1057 static int
 1058 usb_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int fflag, struct thread* td)
 1059 {
 1060         struct usb_cdev_refdata refs;
 1061         struct usb_cdev_privdata* cpd;
 1062         struct usb_fifo *f;
 1063         int fflags;
 1064         int err;
 1065 
 1066         DPRINTFN(2, "cmd=0x%lx\n", cmd);
 1067 
 1068         err = devfs_get_cdevpriv((void **)&cpd);
 1069         if (err != 0)
 1070                 return (err);
 1071 
 1072         /* 
 1073          * Performance optimisation: We try to check for IOCTL's that
 1074          * don't need the USB reference first. Then we grab the USB
 1075          * reference if we need it!
 1076          */
 1077         err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
 1078         if (err)
 1079                 return (ENXIO);
 1080 
 1081         fflags = cpd->fflags;
 1082 
 1083         f = NULL;                       /* set default value */
 1084         err = ENOIOCTL;                 /* set default value */
 1085 
 1086         if (fflags & FWRITE) {
 1087                 f = refs.txfifo;
 1088                 err = usb_ioctl_f_sub(f, cmd, addr, td);
 1089         }
 1090         if (fflags & FREAD) {
 1091                 f = refs.rxfifo;
 1092                 err = usb_ioctl_f_sub(f, cmd, addr, td);
 1093         }
 1094         KASSERT(f != NULL, ("fifo not found"));
 1095         if (err != ENOIOCTL)
 1096                 goto done;
 1097 
 1098         err = (f->methods->f_ioctl) (f, cmd, addr, fflags);
 1099 
 1100         DPRINTFN(2, "f_ioctl cmd 0x%lx = %d\n", cmd, err);
 1101 
 1102         if (err != ENOIOCTL)
 1103                 goto done;
 1104 
 1105         if (usb_usb_ref_device(cpd, &refs)) {
 1106                 /* we lost the reference */
 1107                 return (ENXIO);
 1108         }
 1109 
 1110         err = (f->methods->f_ioctl_post) (f, cmd, addr, fflags);
 1111 
 1112         DPRINTFN(2, "f_ioctl_post cmd 0x%lx = %d\n", cmd, err);
 1113 
 1114         if (err == ENOIOCTL)
 1115                 err = ENOTTY;
 1116 
 1117         if (err)
 1118                 goto done;
 1119 
 1120         /* Wait for re-enumeration, if any */
 1121 
 1122         while (f->udev->re_enumerate_wait != USB_RE_ENUM_DONE) {
 1123                 usb_unref_device(cpd, &refs);
 1124 
 1125                 usb_pause_mtx(NULL, hz / 128);
 1126 
 1127                 while (usb_ref_device(cpd, &refs, 1 /* need uref */)) {
 1128                         if (usb_ref_device(cpd, &refs, 0)) {
 1129                                 /* device no longer exists */
 1130                                 return (ENXIO);
 1131                         }
 1132                         usb_unref_device(cpd, &refs);
 1133                         usb_pause_mtx(NULL, hz / 128);
 1134                 }
 1135         }
 1136 
 1137 done:
 1138         usb_unref_device(cpd, &refs);
 1139         return (err);
 1140 }
 1141 
 1142 static void
 1143 usb_filter_detach(struct knote *kn)
 1144 {
 1145         struct usb_fifo *f = kn->kn_hook;
 1146         knlist_remove(&f->selinfo.si_note, kn, 0);
 1147 }
 1148 
 1149 static int
 1150 usb_filter_write(struct knote *kn, long hint)
 1151 {
 1152         struct usb_cdev_privdata* cpd;
 1153         struct usb_fifo *f;
 1154         struct usb_mbuf *m;
 1155 
 1156         DPRINTFN(2, "\n");
 1157 
 1158         f = kn->kn_hook;
 1159 
 1160         USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
 1161 
 1162         cpd = f->curr_cpd;
 1163         if (cpd == NULL) {
 1164                 m = (void *)1;
 1165         } else if (f->fs_ep_max == 0) {
 1166                 if (f->flag_iserror) {
 1167                         /* we got an error */
 1168                         m = (void *)1;
 1169                 } else {
 1170                         if (f->queue_data == NULL) {
 1171                                 /*
 1172                                  * start write transfer, if not
 1173                                  * already started
 1174                                  */
 1175                                 (f->methods->f_start_write) (f);
 1176                         }
 1177                         /* check if any packets are available */
 1178                         USB_IF_POLL(&f->free_q, m);
 1179                 }
 1180         } else {
 1181                 if (f->flag_iscomplete) {
 1182                         m = (void *)1;
 1183                 } else {
 1184                         m = NULL;
 1185                 }
 1186         }
 1187         return (m ? 1 : 0);
 1188 }
 1189 
 1190 static int
 1191 usb_filter_read(struct knote *kn, long hint)
 1192 {
 1193         struct usb_cdev_privdata* cpd;
 1194         struct usb_fifo *f;
 1195         struct usb_mbuf *m;
 1196 
 1197         DPRINTFN(2, "\n");
 1198 
 1199         f = kn->kn_hook;
 1200 
 1201         USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
 1202 
 1203         cpd = f->curr_cpd;
 1204         if (cpd == NULL) {
 1205                 m = (void *)1;
 1206         } else if (f->fs_ep_max == 0) {
 1207                 if (f->flag_iserror) {
 1208                         /* we have an error */
 1209                         m = (void *)1;
 1210                 } else {
 1211                         if (f->queue_data == NULL) {
 1212                                 /*
 1213                                  * start read transfer, if not
 1214                                  * already started
 1215                                  */
 1216                                 (f->methods->f_start_read) (f);
 1217                         }
 1218                         /* check if any packets are available */
 1219                         USB_IF_POLL(&f->used_q, m);
 1220 
 1221                         /* start reading data, if any */
 1222                         if (m == NULL)
 1223                                 (f->methods->f_start_read) (f);
 1224                 }
 1225         } else {
 1226                 if (f->flag_iscomplete) {
 1227                         m = (void *)1;
 1228                 } else {
 1229                         m = NULL;
 1230                 }
 1231         }
 1232         return (m ? 1 : 0);
 1233 }
 1234 
 1235 static struct filterops usb_filtops_write = {
 1236         .f_isfd = 1,
 1237         .f_detach = usb_filter_detach,
 1238         .f_event = usb_filter_write,
 1239 };
 1240 
 1241 static struct filterops usb_filtops_read = {
 1242         .f_isfd = 1,
 1243         .f_detach = usb_filter_detach,
 1244         .f_event = usb_filter_read,
 1245 };
 1246 
 1247 /* ARGSUSED */
 1248 static int
 1249 usb_kqfilter(struct cdev* dev, struct knote *kn)
 1250 {
 1251         struct usb_cdev_refdata refs;
 1252         struct usb_cdev_privdata* cpd;
 1253         struct usb_fifo *f;
 1254         int fflags;
 1255         int err = EINVAL;
 1256 
 1257         DPRINTFN(2, "\n");
 1258 
 1259         if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
 1260             usb_ref_device(cpd, &refs, 0) != 0)
 1261                 return (ENXIO);
 1262 
 1263         fflags = cpd->fflags;
 1264 
 1265         /* Figure out who needs service */
 1266         switch (kn->kn_filter) {
 1267         case EVFILT_WRITE:
 1268                 if (fflags & FWRITE) {
 1269                         f = refs.txfifo;
 1270                         kn->kn_fop = &usb_filtops_write;
 1271                         err = 0;
 1272                 }
 1273                 break;
 1274         case EVFILT_READ:
 1275                 if (fflags & FREAD) {
 1276                         f = refs.rxfifo;
 1277                         kn->kn_fop = &usb_filtops_read;
 1278                         err = 0;
 1279                 }
 1280                 break;
 1281         default:
 1282                 err = EOPNOTSUPP;
 1283                 break;
 1284         }
 1285 
 1286         if (err == 0) {
 1287                 kn->kn_hook = f;
 1288                 mtx_lock(f->priv_mtx);
 1289                 knlist_add(&f->selinfo.si_note, kn, 1);
 1290                 mtx_unlock(f->priv_mtx);
 1291         }
 1292 
 1293         usb_unref_device(cpd, &refs);
 1294         return (err);
 1295 }
 1296 
 1297 /* ARGSUSED */
 1298 static int
 1299 usb_poll(struct cdev* dev, int events, struct thread* td)
 1300 {
 1301         struct usb_cdev_refdata refs;
 1302         struct usb_cdev_privdata* cpd;
 1303         struct usb_fifo *f;
 1304         struct usb_mbuf *m;
 1305         int fflags, revents;
 1306 
 1307         if (devfs_get_cdevpriv((void **)&cpd) != 0 ||
 1308             usb_ref_device(cpd, &refs, 0) != 0)
 1309                 return (events &
 1310                     (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
 1311 
 1312         fflags = cpd->fflags;
 1313 
 1314         /* Figure out who needs service */
 1315         revents = 0;
 1316         if ((events & (POLLOUT | POLLWRNORM)) &&
 1317             (fflags & FWRITE)) {
 1318                 f = refs.txfifo;
 1319 
 1320                 mtx_lock(f->priv_mtx);
 1321 
 1322                 if (!refs.is_usbfs) {
 1323                         if (f->flag_iserror) {
 1324                                 /* we got an error */
 1325                                 m = (void *)1;
 1326                         } else {
 1327                                 if (f->queue_data == NULL) {
 1328                                         /*
 1329                                          * start write transfer, if not
 1330                                          * already started
 1331                                          */
 1332                                         (f->methods->f_start_write) (f);
 1333                                 }
 1334                                 /* check if any packets are available */
 1335                                 USB_IF_POLL(&f->free_q, m);
 1336                         }
 1337                 } else {
 1338                         if (f->flag_iscomplete) {
 1339                                 m = (void *)1;
 1340                         } else {
 1341                                 m = NULL;
 1342                         }
 1343                 }
 1344 
 1345                 if (m) {
 1346                         revents |= events & (POLLOUT | POLLWRNORM);
 1347                 } else {
 1348                         f->flag_isselect = 1;
 1349                         selrecord(td, &f->selinfo);
 1350                 }
 1351 
 1352                 mtx_unlock(f->priv_mtx);
 1353         }
 1354         if ((events & (POLLIN | POLLRDNORM)) &&
 1355             (fflags & FREAD)) {
 1356                 f = refs.rxfifo;
 1357 
 1358                 mtx_lock(f->priv_mtx);
 1359 
 1360                 if (!refs.is_usbfs) {
 1361                         if (f->flag_iserror) {
 1362                                 /* we have an error */
 1363                                 m = (void *)1;
 1364                         } else {
 1365                                 if (f->queue_data == NULL) {
 1366                                         /*
 1367                                          * start read transfer, if not
 1368                                          * already started
 1369                                          */
 1370                                         (f->methods->f_start_read) (f);
 1371                                 }
 1372                                 /* check if any packets are available */
 1373                                 USB_IF_POLL(&f->used_q, m);
 1374                         }
 1375                 } else {
 1376                         if (f->flag_iscomplete) {
 1377                                 m = (void *)1;
 1378                         } else {
 1379                                 m = NULL;
 1380                         }
 1381                 }
 1382 
 1383                 if (m) {
 1384                         revents |= events & (POLLIN | POLLRDNORM);
 1385                 } else {
 1386                         f->flag_isselect = 1;
 1387                         selrecord(td, &f->selinfo);
 1388 
 1389                         if (!refs.is_usbfs) {
 1390                                 /* start reading data */
 1391                                 (f->methods->f_start_read) (f);
 1392                         }
 1393                 }
 1394 
 1395                 mtx_unlock(f->priv_mtx);
 1396         }
 1397         usb_unref_device(cpd, &refs);
 1398         return (revents);
 1399 }
 1400 
 1401 static int
 1402 usb_read(struct cdev *dev, struct uio *uio, int ioflag)
 1403 {
 1404         struct usb_cdev_refdata refs;
 1405         struct usb_cdev_privdata* cpd;
 1406         struct usb_fifo *f;
 1407         struct usb_mbuf *m;
 1408         int io_len;
 1409         int err;
 1410         uint8_t tr_data = 0;
 1411 
 1412         err = devfs_get_cdevpriv((void **)&cpd);
 1413         if (err != 0)
 1414                 return (err);
 1415 
 1416         err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
 1417         if (err)
 1418                 return (ENXIO);
 1419 
 1420         f = refs.rxfifo;
 1421         if (f == NULL) {
 1422                 /* should not happen */
 1423                 usb_unref_device(cpd, &refs);
 1424                 return (EPERM);
 1425         }
 1426 
 1427         mtx_lock(f->priv_mtx);
 1428 
 1429         /* check for permanent read error */
 1430         if (f->flag_iserror) {
 1431                 err = EIO;
 1432                 goto done;
 1433         }
 1434         /* check if USB-FS interface is active */
 1435         if (refs.is_usbfs) {
 1436                 /*
 1437                  * The queue is used for events that should be
 1438                  * retrieved using the "USB_FS_COMPLETE" ioctl.
 1439                  */
 1440                 err = EINVAL;
 1441                 goto done;
 1442         }
 1443         while (uio->uio_resid > 0) {
 1444                 USB_IF_DEQUEUE(&f->used_q, m);
 1445 
 1446                 if (m == NULL) {
 1447                         /* start read transfer, if not already started */
 1448 
 1449                         (f->methods->f_start_read) (f);
 1450 
 1451                         if (ioflag & IO_NDELAY) {
 1452                                 if (tr_data) {
 1453                                         /* return length before error */
 1454                                         break;
 1455                                 }
 1456                                 err = EWOULDBLOCK;
 1457                                 break;
 1458                         }
 1459                         DPRINTF("sleeping\n");
 1460 
 1461                         err = usb_fifo_wait(f);
 1462                         if (err) {
 1463                                 break;
 1464                         }
 1465                         continue;
 1466                 }
 1467                 if (f->methods->f_filter_read) {
 1468                         /*
 1469                          * Sometimes it is convenient to process data at the
 1470                          * expense of a userland process instead of a kernel
 1471                          * process.
 1472                          */
 1473                         (f->methods->f_filter_read) (f, m);
 1474                 }
 1475                 tr_data = 1;
 1476 
 1477                 io_len = MIN(m->cur_data_len, uio->uio_resid);
 1478 
 1479                 DPRINTFN(2, "transfer %d bytes from %p\n",
 1480                     io_len, m->cur_data_ptr);
 1481 
 1482                 err = usb_fifo_uiomove(f,
 1483                     m->cur_data_ptr, io_len, uio);
 1484 
 1485                 m->cur_data_len -= io_len;
 1486                 m->cur_data_ptr += io_len;
 1487 
 1488                 if (m->cur_data_len == 0) {
 1489                         uint8_t last_packet;
 1490 
 1491                         last_packet = m->last_packet;
 1492 
 1493                         USB_IF_ENQUEUE(&f->free_q, m);
 1494 
 1495                         if (last_packet) {
 1496                                 /* keep framing */
 1497                                 break;
 1498                         }
 1499                 } else {
 1500                         USB_IF_PREPEND(&f->used_q, m);
 1501                 }
 1502 
 1503                 if (err) {
 1504                         break;
 1505                 }
 1506         }
 1507 done:
 1508         mtx_unlock(f->priv_mtx);
 1509 
 1510         usb_unref_device(cpd, &refs);
 1511 
 1512         return (err);
 1513 }
 1514 
 1515 static int
 1516 usb_write(struct cdev *dev, struct uio *uio, int ioflag)
 1517 {
 1518         struct usb_cdev_refdata refs;
 1519         struct usb_cdev_privdata* cpd;
 1520         struct usb_fifo *f;
 1521         struct usb_mbuf *m;
 1522         uint8_t *pdata;
 1523         int io_len;
 1524         int err;
 1525         uint8_t tr_data = 0;
 1526 
 1527         DPRINTFN(2, "\n");
 1528 
 1529         err = devfs_get_cdevpriv((void **)&cpd);
 1530         if (err != 0)
 1531                 return (err);
 1532 
 1533         err = usb_ref_device(cpd, &refs, 0 /* no uref */ );
 1534         if (err)
 1535                 return (ENXIO);
 1536 
 1537         f = refs.txfifo;
 1538         if (f == NULL) {
 1539                 /* should not happen */
 1540                 usb_unref_device(cpd, &refs);
 1541                 return (EPERM);
 1542         }
 1543 
 1544         mtx_lock(f->priv_mtx);
 1545 
 1546         /* check for permanent write error */
 1547         if (f->flag_iserror) {
 1548                 err = EIO;
 1549                 goto done;
 1550         }
 1551         /* check if USB-FS interface is active */
 1552         if (refs.is_usbfs) {
 1553                 /*
 1554                  * The queue is used for events that should be
 1555                  * retrieved using the "USB_FS_COMPLETE" ioctl.
 1556                  */
 1557                 err = EINVAL;
 1558                 goto done;
 1559         }
 1560         if (f->queue_data == NULL) {
 1561                 /* start write transfer, if not already started */
 1562                 (f->methods->f_start_write) (f);
 1563         }
 1564         /* we allow writing zero length data */
 1565         do {
 1566                 USB_IF_DEQUEUE(&f->free_q, m);
 1567 
 1568                 if (m == NULL) {
 1569                         if (ioflag & IO_NDELAY) {
 1570                                 if (tr_data) {
 1571                                         /* return length before error */
 1572                                         break;
 1573                                 }
 1574                                 err = EWOULDBLOCK;
 1575                                 break;
 1576                         }
 1577                         DPRINTF("sleeping\n");
 1578 
 1579                         err = usb_fifo_wait(f);
 1580                         if (err) {
 1581                                 break;
 1582                         }
 1583                         continue;
 1584                 }
 1585                 tr_data = 1;
 1586 
 1587                 if (f->flag_have_fragment == 0) {
 1588                         USB_MBUF_RESET(m);
 1589                         io_len = m->cur_data_len;
 1590                         pdata = m->cur_data_ptr;
 1591                         if (io_len > uio->uio_resid)
 1592                                 io_len = uio->uio_resid;
 1593                         m->cur_data_len = io_len;
 1594                 } else {
 1595                         io_len = m->max_data_len - m->cur_data_len;
 1596                         pdata = m->cur_data_ptr + m->cur_data_len;
 1597                         if (io_len > uio->uio_resid)
 1598                                 io_len = uio->uio_resid;
 1599                         m->cur_data_len += io_len;
 1600                 }
 1601 
 1602                 DPRINTFN(2, "transfer %d bytes to %p\n",
 1603                     io_len, pdata);
 1604 
 1605                 err = usb_fifo_uiomove(f, pdata, io_len, uio);
 1606 
 1607                 if (err) {
 1608                         f->flag_have_fragment = 0;
 1609                         USB_IF_ENQUEUE(&f->free_q, m);
 1610                         break;
 1611                 }
 1612 
 1613                 /* check if the buffer is ready to be transmitted */
 1614 
 1615                 if ((f->flag_write_defrag == 0) ||
 1616                     (m->cur_data_len == m->max_data_len)) {
 1617                         f->flag_have_fragment = 0;
 1618 
 1619                         /*
 1620                          * Check for write filter:
 1621                          *
 1622                          * Sometimes it is convenient to process data
 1623                          * at the expense of a userland process
 1624                          * instead of a kernel process.
 1625                          */
 1626                         if (f->methods->f_filter_write) {
 1627                                 (f->methods->f_filter_write) (f, m);
 1628                         }
 1629 
 1630                         /* Put USB mbuf in the used queue */
 1631                         USB_IF_ENQUEUE(&f->used_q, m);
 1632 
 1633                         /* Start writing data, if not already started */
 1634                         (f->methods->f_start_write) (f);
 1635                 } else {
 1636                         /* Wait for more data or close */
 1637                         f->flag_have_fragment = 1;
 1638                         USB_IF_PREPEND(&f->free_q, m);
 1639                 }
 1640 
 1641         } while (uio->uio_resid > 0);
 1642 done:
 1643         mtx_unlock(f->priv_mtx);
 1644 
 1645         usb_unref_device(cpd, &refs);
 1646 
 1647         return (err);
 1648 }
 1649 
 1650 int
 1651 usb_static_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
 1652     struct thread *td)
 1653 {
 1654         union {
 1655                 struct usb_read_dir *urd;
 1656 #ifdef COMPAT_FREEBSD32
 1657                 struct usb_read_dir32 *urd32;
 1658 #endif
 1659                 void* data;
 1660         } u;
 1661         int err;
 1662 
 1663         u.data = data;
 1664         switch (cmd) {
 1665                 case USB_READ_DIR:
 1666                         err = usb_read_symlink(u.urd->urd_data,
 1667                             u.urd->urd_startentry, u.urd->urd_maxlen);
 1668                         break;
 1669 #ifdef COMPAT_FREEBSD32
 1670                 case USB_READ_DIR32:
 1671                         err = usb_read_symlink(PTRIN(u.urd32->urd_data),
 1672                             u.urd32->urd_startentry, u.urd32->urd_maxlen);
 1673                         break;
 1674 #endif
 1675                 case USB_DEV_QUIRK_GET:
 1676                 case USB_QUIRK_NAME_GET:
 1677                 case USB_DEV_QUIRK_ADD:
 1678                 case USB_DEV_QUIRK_REMOVE:
 1679                         err = usb_quirk_ioctl_p(cmd, data, fflag, td);
 1680                         break;
 1681                 case USB_GET_TEMPLATE:
 1682                         *(int *)data = usb_template;
 1683                         err = 0;
 1684                         break;
 1685                 case USB_SET_TEMPLATE:
 1686                         err = priv_check(curthread, PRIV_DRIVER);
 1687                         if (err)
 1688                                 break;
 1689                         usb_template = *(int *)data;
 1690                         break;
 1691                 default:
 1692                         err = ENOTTY;
 1693                         break;
 1694         }
 1695         return (err);
 1696 }
 1697 
 1698 static int
 1699 usb_fifo_uiomove(struct usb_fifo *f, void *cp,
 1700     int n, struct uio *uio)
 1701 {
 1702         int error;
 1703 
 1704         mtx_unlock(f->priv_mtx);
 1705 
 1706         /*
 1707          * "uiomove()" can sleep so one needs to make a wrapper,
 1708          * exiting the mutex and checking things:
 1709          */
 1710         error = uiomove(cp, n, uio);
 1711 
 1712         mtx_lock(f->priv_mtx);
 1713 
 1714         return (error);
 1715 }
 1716 
 1717 int
 1718 usb_fifo_wait(struct usb_fifo *f)
 1719 {
 1720         int err;
 1721 
 1722         USB_MTX_ASSERT(f->priv_mtx, MA_OWNED);
 1723 
 1724         if (f->flag_iserror) {
 1725                 /* we are gone */
 1726                 return (EIO);
 1727         }
 1728         f->flag_sleeping = 1;
 1729 
 1730         err = cv_wait_sig(&f->cv_io, f->priv_mtx);
 1731 
 1732         if (f->flag_iserror) {
 1733                 /* we are gone */
 1734                 err = EIO;
 1735         }
 1736         return (err);
 1737 }
 1738 
 1739 void
 1740 usb_fifo_signal(struct usb_fifo *f)
 1741 {
 1742         if (f->flag_sleeping) {
 1743                 f->flag_sleeping = 0;
 1744                 cv_broadcast(&f->cv_io);
 1745         }
 1746 }
 1747 
 1748 void
 1749 usb_fifo_wakeup(struct usb_fifo *f)
 1750 {
 1751         usb_fifo_signal(f);
 1752 
 1753         KNOTE_LOCKED(&f->selinfo.si_note, 0);
 1754 
 1755         if (f->flag_isselect) {
 1756                 selwakeup(&f->selinfo);
 1757                 f->flag_isselect = 0;
 1758         }
 1759         if (f->async_p != NULL) {
 1760                 PROC_LOCK(f->async_p);
 1761                 kern_psignal(f->async_p, SIGIO);
 1762                 PROC_UNLOCK(f->async_p);
 1763         }
 1764 }
 1765 
 1766 static int
 1767 usb_fifo_dummy_open(struct usb_fifo *fifo, int fflags)
 1768 {
 1769         return (0);
 1770 }
 1771 
 1772 static void
 1773 usb_fifo_dummy_close(struct usb_fifo *fifo, int fflags)
 1774 {
 1775         return;
 1776 }
 1777 
 1778 static int
 1779 usb_fifo_dummy_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
 1780 {
 1781         return (ENOIOCTL);
 1782 }
 1783 
 1784 static void
 1785 usb_fifo_dummy_cmd(struct usb_fifo *fifo)
 1786 {
 1787         fifo->flag_flushing = 0;        /* not flushing */
 1788 }
 1789 
 1790 static void
 1791 usb_fifo_check_methods(struct usb_fifo_methods *pm)
 1792 {
 1793         /* check that all callback functions are OK */
 1794 
 1795         if (pm->f_open == NULL)
 1796                 pm->f_open = &usb_fifo_dummy_open;
 1797 
 1798         if (pm->f_close == NULL)
 1799                 pm->f_close = &usb_fifo_dummy_close;
 1800 
 1801         if (pm->f_ioctl == NULL)
 1802                 pm->f_ioctl = &usb_fifo_dummy_ioctl;
 1803 
 1804         if (pm->f_ioctl_post == NULL)
 1805                 pm->f_ioctl_post = &usb_fifo_dummy_ioctl;
 1806 
 1807         if (pm->f_start_read == NULL)
 1808                 pm->f_start_read = &usb_fifo_dummy_cmd;
 1809 
 1810         if (pm->f_stop_read == NULL)
 1811                 pm->f_stop_read = &usb_fifo_dummy_cmd;
 1812 
 1813         if (pm->f_start_write == NULL)
 1814                 pm->f_start_write = &usb_fifo_dummy_cmd;
 1815 
 1816         if (pm->f_stop_write == NULL)
 1817                 pm->f_stop_write = &usb_fifo_dummy_cmd;
 1818 }
 1819 
 1820 /*------------------------------------------------------------------------*
 1821  *      usb_fifo_attach
 1822  *
 1823  * The following function will create a duplex FIFO.
 1824  *
 1825  * Return values:
 1826  * 0: Success.
 1827  * Else: Failure.
 1828  *------------------------------------------------------------------------*/
 1829 int
 1830 usb_fifo_attach(struct usb_device *udev, void *priv_sc,
 1831     struct mtx *priv_mtx, struct usb_fifo_methods *pm,
 1832     struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
 1833     uint8_t iface_index, uid_t uid, gid_t gid, int mode)
 1834 {
 1835         struct usb_fifo *f_tx;
 1836         struct usb_fifo *f_rx;
 1837         char devname[32];
 1838         uint8_t n;
 1839 
 1840         f_sc->fp[USB_FIFO_TX] = NULL;
 1841         f_sc->fp[USB_FIFO_RX] = NULL;
 1842 
 1843         if (pm == NULL)
 1844                 return (EINVAL);
 1845 
 1846         /* check the methods */
 1847         usb_fifo_check_methods(pm);
 1848 
 1849         if (priv_mtx == NULL)
 1850                 priv_mtx = &Giant;
 1851 
 1852         /* search for a free FIFO slot */
 1853         for (n = 0;; n += 2) {
 1854                 if (n == USB_FIFO_MAX) {
 1855                         /* end of FIFOs reached */
 1856                         return (ENOMEM);
 1857                 }
 1858                 /* Check for TX FIFO */
 1859                 if (udev->fifo[n + USB_FIFO_TX] != NULL) {
 1860                         continue;
 1861                 }
 1862                 /* Check for RX FIFO */
 1863                 if (udev->fifo[n + USB_FIFO_RX] != NULL) {
 1864                         continue;
 1865                 }
 1866                 break;
 1867         }
 1868 
 1869         f_tx = usb_fifo_alloc(priv_mtx);
 1870         f_rx = usb_fifo_alloc(priv_mtx);
 1871 
 1872         if ((f_tx == NULL) || (f_rx == NULL)) {
 1873                 usb_fifo_free(f_tx);
 1874                 usb_fifo_free(f_rx);
 1875                 return (ENOMEM);
 1876         }
 1877         /* initialise FIFO structures */
 1878 
 1879         f_tx->fifo_index = n + USB_FIFO_TX;
 1880         f_tx->dev_ep_index = -1;
 1881         f_tx->priv_sc0 = priv_sc;
 1882         f_tx->methods = pm;
 1883         f_tx->iface_index = iface_index;
 1884         f_tx->udev = udev;
 1885 
 1886         f_rx->fifo_index = n + USB_FIFO_RX;
 1887         f_rx->dev_ep_index = -1;
 1888         f_rx->priv_sc0 = priv_sc;
 1889         f_rx->methods = pm;
 1890         f_rx->iface_index = iface_index;
 1891         f_rx->udev = udev;
 1892 
 1893         f_sc->fp[USB_FIFO_TX] = f_tx;
 1894         f_sc->fp[USB_FIFO_RX] = f_rx;
 1895 
 1896         mtx_lock(&usb_ref_lock);
 1897         udev->fifo[f_tx->fifo_index] = f_tx;
 1898         udev->fifo[f_rx->fifo_index] = f_rx;
 1899         mtx_unlock(&usb_ref_lock);
 1900 
 1901         for (n = 0; n != 4; n++) {
 1902                 if (pm->basename[n] == NULL) {
 1903                         continue;
 1904                 }
 1905                 if (subunit < 0) {
 1906                         if (snprintf(devname, sizeof(devname),
 1907                             "%s%u%s", pm->basename[n],
 1908                             unit, pm->postfix[n] ?
 1909                             pm->postfix[n] : "")) {
 1910                                 /* ignore */
 1911                         }
 1912                 } else {
 1913                         if (snprintf(devname, sizeof(devname),
 1914                             "%s%u.%d%s", pm->basename[n],
 1915                             unit, subunit, pm->postfix[n] ?
 1916                             pm->postfix[n] : "")) {
 1917                                 /* ignore */
 1918                         }
 1919                 }
 1920 
 1921                 /*
 1922                  * Distribute the symbolic links into two FIFO structures:
 1923                  */
 1924                 if (n & 1) {
 1925                         f_rx->symlink[n / 2] =
 1926                             usb_alloc_symlink(devname);
 1927                 } else {
 1928                         f_tx->symlink[n / 2] =
 1929                             usb_alloc_symlink(devname);
 1930                 }
 1931 
 1932                 /* Create the device */
 1933                 f_sc->dev = usb_make_dev(udev, devname, -1,
 1934                     f_tx->fifo_index & f_rx->fifo_index,
 1935                     FREAD|FWRITE, uid, gid, mode);
 1936         }
 1937 
 1938         DPRINTFN(2, "attached %p/%p\n", f_tx, f_rx);
 1939         return (0);
 1940 }
 1941 
 1942 /*------------------------------------------------------------------------*
 1943  *      usb_fifo_alloc_buffer
 1944  *
 1945  * Return values:
 1946  * 0: Success
 1947  * Else failure
 1948  *------------------------------------------------------------------------*/
 1949 int
 1950 usb_fifo_alloc_buffer(struct usb_fifo *f, usb_size_t bufsize,
 1951     uint16_t nbuf)
 1952 {
 1953         struct usb_ifqueue temp_q = {};
 1954         void *queue_data;
 1955 
 1956         usb_fifo_free_buffer(f);
 1957 
 1958         temp_q.ifq_maxlen = nbuf;
 1959 
 1960         queue_data = usb_alloc_mbufs(
 1961             M_USBDEV, &temp_q, bufsize, nbuf);
 1962 
 1963         if (queue_data == NULL && bufsize != 0 && nbuf != 0)
 1964                 return (ENOMEM);
 1965 
 1966         mtx_lock(f->priv_mtx);
 1967 
 1968         /*
 1969          * Setup queues and sizes under lock to avoid early use by
 1970          * concurrent FIFO access:
 1971          */
 1972         f->free_q = temp_q;
 1973         f->used_q.ifq_maxlen = nbuf;
 1974         f->queue_data = queue_data;
 1975         mtx_unlock(f->priv_mtx);
 1976 
 1977         return (0);                     /* success */
 1978 }
 1979 
 1980 /*------------------------------------------------------------------------*
 1981  *      usb_fifo_free_buffer
 1982  *
 1983  * This function will free the buffers associated with a FIFO. This
 1984  * function can be called multiple times in a row.
 1985  *------------------------------------------------------------------------*/
 1986 void
 1987 usb_fifo_free_buffer(struct usb_fifo *f)
 1988 {
 1989         void *queue_data;
 1990 
 1991         mtx_lock(f->priv_mtx);
 1992 
 1993         /* Get and clear pointer to free, if any. */
 1994         queue_data = f->queue_data;
 1995         f->queue_data = NULL;
 1996 
 1997         /*
 1998          * Reset queues under lock to avoid use of freed buffers by
 1999          * concurrent FIFO activity:
 2000          */
 2001         memset(&f->free_q, 0, sizeof(f->free_q));
 2002         memset(&f->used_q, 0, sizeof(f->used_q));
 2003         mtx_unlock(f->priv_mtx);
 2004 
 2005         /* Free old buffer, if any. */
 2006         free(queue_data, M_USBDEV);
 2007 }
 2008 
 2009 void
 2010 usb_fifo_detach(struct usb_fifo_sc *f_sc)
 2011 {
 2012         if (f_sc == NULL) {
 2013                 return;
 2014         }
 2015         usb_fifo_free(f_sc->fp[USB_FIFO_TX]);
 2016         usb_fifo_free(f_sc->fp[USB_FIFO_RX]);
 2017 
 2018         f_sc->fp[USB_FIFO_TX] = NULL;
 2019         f_sc->fp[USB_FIFO_RX] = NULL;
 2020 
 2021         usb_destroy_dev(f_sc->dev);
 2022 
 2023         f_sc->dev = NULL;
 2024 
 2025         DPRINTFN(2, "detached %p\n", f_sc);
 2026 }
 2027 
 2028 usb_size_t
 2029 usb_fifo_put_bytes_max(struct usb_fifo *f)
 2030 {
 2031         struct usb_mbuf *m;
 2032         usb_size_t len;
 2033 
 2034         USB_IF_POLL(&f->free_q, m);
 2035 
 2036         if (m) {
 2037                 len = m->max_data_len;
 2038         } else {
 2039                 len = 0;
 2040         }
 2041         return (len);
 2042 }
 2043 
 2044 /*------------------------------------------------------------------------*
 2045  *      usb_fifo_put_data
 2046  *
 2047  * what:
 2048  *  0 - normal operation
 2049  *  1 - set last packet flag to enforce framing
 2050  *------------------------------------------------------------------------*/
 2051 void
 2052 usb_fifo_put_data(struct usb_fifo *f, struct usb_page_cache *pc,
 2053     usb_frlength_t offset, usb_frlength_t len, uint8_t what)
 2054 {
 2055         struct usb_mbuf *m;
 2056         usb_frlength_t io_len;
 2057 
 2058         while (len || (what == 1)) {
 2059                 USB_IF_DEQUEUE(&f->free_q, m);
 2060 
 2061                 if (m) {
 2062                         USB_MBUF_RESET(m);
 2063 
 2064                         io_len = MIN(len, m->cur_data_len);
 2065 
 2066                         usbd_copy_out(pc, offset, m->cur_data_ptr, io_len);
 2067 
 2068                         m->cur_data_len = io_len;
 2069                         offset += io_len;
 2070                         len -= io_len;
 2071 
 2072                         if ((len == 0) && (what == 1)) {
 2073                                 m->last_packet = 1;
 2074                         }
 2075                         USB_IF_ENQUEUE(&f->used_q, m);
 2076 
 2077                         usb_fifo_wakeup(f);
 2078 
 2079                         if ((len == 0) || (what == 1)) {
 2080                                 break;
 2081                         }
 2082                 } else {
 2083                         break;
 2084                 }
 2085         }
 2086 }
 2087 
 2088 void
 2089 usb_fifo_put_data_linear(struct usb_fifo *f, void *ptr,
 2090     usb_size_t len, uint8_t what)
 2091 {
 2092         struct usb_mbuf *m;
 2093         usb_size_t io_len;
 2094 
 2095         while (len || (what == 1)) {
 2096                 USB_IF_DEQUEUE(&f->free_q, m);
 2097 
 2098                 if (m) {
 2099                         USB_MBUF_RESET(m);
 2100 
 2101                         io_len = MIN(len, m->cur_data_len);
 2102 
 2103                         memcpy(m->cur_data_ptr, ptr, io_len);
 2104 
 2105                         m->cur_data_len = io_len;
 2106                         ptr = USB_ADD_BYTES(ptr, io_len);
 2107                         len -= io_len;
 2108 
 2109                         if ((len == 0) && (what == 1)) {
 2110                                 m->last_packet = 1;
 2111                         }
 2112                         USB_IF_ENQUEUE(&f->used_q, m);
 2113 
 2114                         usb_fifo_wakeup(f);
 2115 
 2116                         if ((len == 0) || (what == 1)) {
 2117                                 break;
 2118                         }
 2119                 } else {
 2120                         break;
 2121                 }
 2122         }
 2123 }
 2124 
 2125 uint8_t
 2126 usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len)
 2127 {
 2128         struct usb_mbuf *m;
 2129 
 2130         USB_IF_DEQUEUE(&f->free_q, m);
 2131 
 2132         if (m) {
 2133                 m->cur_data_len = len;
 2134                 m->cur_data_ptr = ptr;
 2135                 USB_IF_ENQUEUE(&f->used_q, m);
 2136                 usb_fifo_wakeup(f);
 2137                 return (1);
 2138         }
 2139         return (0);
 2140 }
 2141 
 2142 void
 2143 usb_fifo_put_data_error(struct usb_fifo *f)
 2144 {
 2145         f->flag_iserror = 1;
 2146         usb_fifo_wakeup(f);
 2147 }
 2148 
 2149 /*------------------------------------------------------------------------*
 2150  *      usb_fifo_get_data
 2151  *
 2152  * what:
 2153  *  0 - normal operation
 2154  *  1 - only get one "usb_mbuf"
 2155  *
 2156  * returns:
 2157  *  0 - no more data
 2158  *  1 - data in buffer
 2159  *------------------------------------------------------------------------*/
 2160 uint8_t
 2161 usb_fifo_get_data(struct usb_fifo *f, struct usb_page_cache *pc,
 2162     usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
 2163     uint8_t what)
 2164 {
 2165         struct usb_mbuf *m;
 2166         usb_frlength_t io_len;
 2167         uint8_t tr_data = 0;
 2168 
 2169         actlen[0] = 0;
 2170 
 2171         while (1) {
 2172                 USB_IF_DEQUEUE(&f->used_q, m);
 2173 
 2174                 if (m) {
 2175                         tr_data = 1;
 2176 
 2177                         io_len = MIN(len, m->cur_data_len);
 2178 
 2179                         usbd_copy_in(pc, offset, m->cur_data_ptr, io_len);
 2180 
 2181                         len -= io_len;
 2182                         offset += io_len;
 2183                         actlen[0] += io_len;
 2184                         m->cur_data_ptr += io_len;
 2185                         m->cur_data_len -= io_len;
 2186 
 2187                         if ((m->cur_data_len == 0) || (what == 1)) {
 2188                                 USB_IF_ENQUEUE(&f->free_q, m);
 2189 
 2190                                 usb_fifo_wakeup(f);
 2191 
 2192                                 if (what == 1) {
 2193                                         break;
 2194                                 }
 2195                         } else {
 2196                                 USB_IF_PREPEND(&f->used_q, m);
 2197                         }
 2198                 } else {
 2199                         if (tr_data) {
 2200                                 /* wait for data to be written out */
 2201                                 break;
 2202                         }
 2203                         if (f->flag_flushing) {
 2204                                 /* check if we should send a short packet */
 2205                                 if (f->flag_short != 0) {
 2206                                         f->flag_short = 0;
 2207                                         tr_data = 1;
 2208                                         break;
 2209                                 }
 2210                                 /* flushing complete */
 2211                                 f->flag_flushing = 0;
 2212                                 usb_fifo_wakeup(f);
 2213                         }
 2214                         break;
 2215                 }
 2216                 if (len == 0) {
 2217                         break;
 2218                 }
 2219         }
 2220         return (tr_data);
 2221 }
 2222 
 2223 uint8_t
 2224 usb_fifo_get_data_linear(struct usb_fifo *f, void *ptr,
 2225     usb_size_t len, usb_size_t *actlen, uint8_t what)
 2226 {
 2227         struct usb_mbuf *m;
 2228         usb_size_t io_len;
 2229         uint8_t tr_data = 0;
 2230 
 2231         actlen[0] = 0;
 2232 
 2233         while (1) {
 2234                 USB_IF_DEQUEUE(&f->used_q, m);
 2235 
 2236                 if (m) {
 2237                         tr_data = 1;
 2238 
 2239                         io_len = MIN(len, m->cur_data_len);
 2240 
 2241                         memcpy(ptr, m->cur_data_ptr, io_len);
 2242 
 2243                         len -= io_len;
 2244                         ptr = USB_ADD_BYTES(ptr, io_len);
 2245                         actlen[0] += io_len;
 2246                         m->cur_data_ptr += io_len;
 2247                         m->cur_data_len -= io_len;
 2248 
 2249                         if ((m->cur_data_len == 0) || (what == 1)) {
 2250                                 USB_IF_ENQUEUE(&f->free_q, m);
 2251 
 2252                                 usb_fifo_wakeup(f);
 2253 
 2254                                 if (what == 1) {
 2255                                         break;
 2256                                 }
 2257                         } else {
 2258                                 USB_IF_PREPEND(&f->used_q, m);
 2259                         }
 2260                 } else {
 2261                         if (tr_data) {
 2262                                 /* wait for data to be written out */
 2263                                 break;
 2264                         }
 2265                         if (f->flag_flushing) {
 2266                                 /* check if we should send a short packet */
 2267                                 if (f->flag_short != 0) {
 2268                                         f->flag_short = 0;
 2269                                         tr_data = 1;
 2270                                         break;
 2271                                 }
 2272                                 /* flushing complete */
 2273                                 f->flag_flushing = 0;
 2274                                 usb_fifo_wakeup(f);
 2275                         }
 2276                         break;
 2277                 }
 2278                 if (len == 0) {
 2279                         break;
 2280                 }
 2281         }
 2282         return (tr_data);
 2283 }
 2284 
 2285 uint8_t
 2286 usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr, usb_size_t *plen)
 2287 {
 2288         struct usb_mbuf *m;
 2289 
 2290         USB_IF_POLL(&f->used_q, m);
 2291 
 2292         if (m) {
 2293                 *plen = m->cur_data_len;
 2294                 *pptr = m->cur_data_ptr;
 2295 
 2296                 return (1);
 2297         }
 2298         return (0);
 2299 }
 2300 
 2301 void
 2302 usb_fifo_get_data_error(struct usb_fifo *f)
 2303 {
 2304         f->flag_iserror = 1;
 2305         usb_fifo_wakeup(f);
 2306 }
 2307 
 2308 /*------------------------------------------------------------------------*
 2309  *      usb_alloc_symlink
 2310  *
 2311  * Return values:
 2312  * NULL: Failure
 2313  * Else: Pointer to symlink entry
 2314  *------------------------------------------------------------------------*/
 2315 struct usb_symlink *
 2316 usb_alloc_symlink(const char *target)
 2317 {
 2318         struct usb_symlink *ps;
 2319 
 2320         ps = malloc(sizeof(*ps), M_USBDEV, M_WAITOK);
 2321         /* XXX no longer needed */
 2322         strlcpy(ps->src_path, target, sizeof(ps->src_path));
 2323         ps->src_len = strlen(ps->src_path);
 2324         strlcpy(ps->dst_path, target, sizeof(ps->dst_path));
 2325         ps->dst_len = strlen(ps->dst_path);
 2326 
 2327         sx_xlock(&usb_sym_lock);
 2328         TAILQ_INSERT_TAIL(&usb_sym_head, ps, sym_entry);
 2329         sx_unlock(&usb_sym_lock);
 2330         return (ps);
 2331 }
 2332 
 2333 /*------------------------------------------------------------------------*
 2334  *      usb_free_symlink
 2335  *------------------------------------------------------------------------*/
 2336 void
 2337 usb_free_symlink(struct usb_symlink *ps)
 2338 {
 2339         if (ps == NULL) {
 2340                 return;
 2341         }
 2342         sx_xlock(&usb_sym_lock);
 2343         TAILQ_REMOVE(&usb_sym_head, ps, sym_entry);
 2344         sx_unlock(&usb_sym_lock);
 2345 
 2346         free(ps, M_USBDEV);
 2347 }
 2348 
 2349 /*------------------------------------------------------------------------*
 2350  *      usb_read_symlink
 2351  *
 2352  * Return value:
 2353  * 0: Success
 2354  * Else: Failure
 2355  *------------------------------------------------------------------------*/
 2356 int
 2357 usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, uint32_t user_len)
 2358 {
 2359         struct usb_symlink *ps;
 2360         uint32_t temp;
 2361         uint32_t delta = 0;
 2362         uint8_t len;
 2363         int error = 0;
 2364 
 2365         sx_xlock(&usb_sym_lock);
 2366 
 2367         TAILQ_FOREACH(ps, &usb_sym_head, sym_entry) {
 2368                 /*
 2369                  * Compute total length of source and destination symlink
 2370                  * strings pluss one length byte and two NUL bytes:
 2371                  */
 2372                 temp = ps->src_len + ps->dst_len + 3;
 2373 
 2374                 if (temp > 255) {
 2375                         /*
 2376                          * Skip entry because this length cannot fit
 2377                          * into one byte:
 2378                          */
 2379                         continue;
 2380                 }
 2381                 if (startentry != 0) {
 2382                         /* decrement read offset */
 2383                         startentry--;
 2384                         continue;
 2385                 }
 2386                 if (temp > user_len) {
 2387                         /* out of buffer space */
 2388                         break;
 2389                 }
 2390                 len = temp;
 2391 
 2392                 /* copy out total length */
 2393 
 2394                 error = copyout(&len,
 2395                     USB_ADD_BYTES(user_ptr, delta), 1);
 2396                 if (error) {
 2397                         break;
 2398                 }
 2399                 delta += 1;
 2400 
 2401                 /* copy out source string */
 2402 
 2403                 error = copyout(ps->src_path,
 2404                     USB_ADD_BYTES(user_ptr, delta), ps->src_len);
 2405                 if (error) {
 2406                         break;
 2407                 }
 2408                 len = 0;
 2409                 delta += ps->src_len;
 2410                 error = copyout(&len,
 2411                     USB_ADD_BYTES(user_ptr, delta), 1);
 2412                 if (error) {
 2413                         break;
 2414                 }
 2415                 delta += 1;
 2416 
 2417                 /* copy out destination string */
 2418 
 2419                 error = copyout(ps->dst_path,
 2420                     USB_ADD_BYTES(user_ptr, delta), ps->dst_len);
 2421                 if (error) {
 2422                         break;
 2423                 }
 2424                 len = 0;
 2425                 delta += ps->dst_len;
 2426                 error = copyout(&len,
 2427                     USB_ADD_BYTES(user_ptr, delta), 1);
 2428                 if (error) {
 2429                         break;
 2430                 }
 2431                 delta += 1;
 2432 
 2433                 user_len -= temp;
 2434         }
 2435 
 2436         /* a zero length entry indicates the end */
 2437 
 2438         if ((user_len != 0) && (error == 0)) {
 2439                 len = 0;
 2440 
 2441                 error = copyout(&len,
 2442                     USB_ADD_BYTES(user_ptr, delta), 1);
 2443         }
 2444         sx_unlock(&usb_sym_lock);
 2445         return (error);
 2446 }
 2447 
 2448 void
 2449 usb_fifo_set_close_zlp(struct usb_fifo *f, uint8_t onoff)
 2450 {
 2451         if (f == NULL)
 2452                 return;
 2453 
 2454         /* send a Zero Length Packet, ZLP, before close */
 2455         f->flag_short = onoff;
 2456 }
 2457 
 2458 void
 2459 usb_fifo_set_write_defrag(struct usb_fifo *f, uint8_t onoff)
 2460 {
 2461         if (f == NULL)
 2462                 return;
 2463 
 2464         /* defrag written data */
 2465         f->flag_write_defrag = onoff;
 2466         /* reset defrag state */
 2467         f->flag_have_fragment = 0;
 2468 }
 2469 
 2470 void *
 2471 usb_fifo_softc(struct usb_fifo *f)
 2472 {
 2473         return (f->priv_sc0);
 2474 }
 2475 #endif  /* USB_HAVE_UGEN */

Cache object: 645af83a5ff91f4e6da434068b4eebc6


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