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_subr.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 /*      $NetBSD: usb_subr.c,v 1.99 2002/07/11 21:14:34 augustss Exp $   */
    2 
    3 /* Also already have from NetBSD:
    4  *      $NetBSD: usb_subr.c,v 1.102 2003/01/01 16:21:50 augustss Exp $
    5  *      $NetBSD: usb_subr.c,v 1.103 2003/01/10 11:19:13 augustss Exp $
    6  *      $NetBSD: usb_subr.c,v 1.111 2004/03/15 10:35:04 augustss Exp $
    7  *      $NetBSD: usb_subr.c,v 1.114 2004/06/23 02:30:52 mycroft Exp $
    8  *      $NetBSD: usb_subr.c,v 1.115 2004/06/23 05:23:19 mycroft Exp $
    9  *      $NetBSD: usb_subr.c,v 1.116 2004/06/23 06:27:54 mycroft Exp $
   10  *      $NetBSD: usb_subr.c,v 1.119 2004/10/23 13:26:33 augustss Exp $
   11  */
   12 
   13 #include <sys/cdefs.h>
   14 __FBSDID("$FreeBSD: releng/7.3/sys/dev/usb/usb_subr.c 190251 2009-03-22 06:37:14Z n_hibma $");
   15 
   16 /*-
   17  * Copyright (c) 1998 The NetBSD Foundation, Inc.
   18  * All rights reserved.
   19  *
   20  * This code is derived from software contributed to The NetBSD Foundation
   21  * by Lennart Augustsson (lennart@augustsson.net) at
   22  * Carlstedt Research & Technology.
   23  *
   24  * Redistribution and use in source and binary forms, with or without
   25  * modification, are permitted provided that the following conditions
   26  * are met:
   27  * 1. Redistributions of source code must retain the above copyright
   28  *    notice, this list of conditions and the following disclaimer.
   29  * 2. Redistributions in binary form must reproduce the above copyright
   30  *    notice, this list of conditions and the following disclaimer in the
   31  *    documentation and/or other materials provided with the distribution.
   32  * 3. All advertising materials mentioning features or use of this software
   33  *    must display the following acknowledgement:
   34  *        This product includes software developed by the NetBSD
   35  *        Foundation, Inc. and its contributors.
   36  * 4. Neither the name of The NetBSD Foundation nor the names of its
   37  *    contributors may be used to endorse or promote products derived
   38  *    from this software without specific prior written permission.
   39  *
   40  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   41  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   42  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   45  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   46  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   47  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   48  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   50  * POSSIBILITY OF SUCH DAMAGE.
   51  */
   52 
   53 #include "opt_usb.h"
   54 
   55 #include <sys/param.h>
   56 #include <sys/systm.h>
   57 #include <sys/kernel.h>
   58 #include <sys/malloc.h>
   59 #include <sys/module.h>
   60 #include <sys/bus.h>
   61 #include <sys/proc.h>
   62 #include <sys/sysctl.h>
   63 
   64 #include <machine/bus.h>
   65 
   66 #include <dev/usb/usb.h>
   67 
   68 #include <dev/usb/usbdi.h>
   69 #include <dev/usb/usbdi_util.h>
   70 #include <dev/usb/usbdivar.h>
   71 #include "usbdevs.h"
   72 #include <dev/usb/usb_quirks.h>
   73 
   74 #define delay(d)         DELAY(d)
   75 
   76 #ifdef USB_DEBUG
   77 #define DPRINTF(x)      if (usbdebug) printf x
   78 #define DPRINTFN(n,x)   if (usbdebug>(n)) printf x
   79 extern int usbdebug;
   80 #else
   81 #define DPRINTF(x)
   82 #define DPRINTFN(n,x)
   83 #endif
   84 
   85 static usbd_status usbd_set_config(usbd_device_handle, int);
   86 static void usbd_devinfo_vp(usbd_device_handle, char *, char *, int);
   87 static int usbd_getnewaddr(usbd_bus_handle bus);
   88 static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
   89 static void usbd_kill_pipe(usbd_pipe_handle);
   90 static usbd_status usbd_probe_and_attach(device_t parent,
   91                                  usbd_device_handle dev, int port, int addr);
   92 
   93 static u_int32_t usb_cookie_no = 0;
   94 
   95 #ifdef USBVERBOSE
   96 typedef u_int16_t usb_vendor_id_t;
   97 typedef u_int16_t usb_product_id_t;
   98 
   99 /*
  100  * Descriptions of of known vendors and devices ("products").
  101  */
  102 struct usb_knowndev {
  103         usb_vendor_id_t         vendor;
  104         usb_product_id_t        product;
  105         int                     flags;
  106         char                    *vendorname, *productname;
  107 };
  108 #define USB_KNOWNDEV_NOPROD     0x01            /* match on vendor only */
  109 
  110 #include "usbdevs_data.h"
  111 #endif /* USBVERBOSE */
  112 
  113 static const char * const usbd_error_strs[] = {
  114         "NORMAL_COMPLETION",
  115         "IN_PROGRESS",
  116         "PENDING_REQUESTS",
  117         "NOT_STARTED",
  118         "INVAL",
  119         "NOMEM",
  120         "CANCELLED",
  121         "BAD_ADDRESS",
  122         "IN_USE",
  123         "NO_ADDR",
  124         "SET_ADDR_FAILED",
  125         "NO_POWER",
  126         "TOO_DEEP",
  127         "IOERROR",
  128         "NOT_CONFIGURED",
  129         "TIMEOUT",
  130         "SHORT_XFER",
  131         "STALLED",
  132         "INTERRUPTED",
  133         "XXX",
  134 };
  135 
  136 const char *
  137 usbd_errstr(usbd_status err)
  138 {
  139         static char buffer[5];
  140 
  141         if (err < USBD_ERROR_MAX) {
  142                 return usbd_error_strs[err];
  143         } else {
  144                 snprintf(buffer, sizeof buffer, "%d", err);
  145                 return buffer;
  146         }
  147 }
  148 
  149 usbd_status
  150 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
  151                      usb_string_descriptor_t *sdesc, int *sizep)
  152 {
  153         usb_device_request_t req;
  154         usbd_status err;
  155         int actlen;
  156 
  157         req.bmRequestType = UT_READ_DEVICE;
  158         req.bRequest = UR_GET_DESCRIPTOR;
  159         USETW2(req.wValue, UDESC_STRING, sindex);
  160         USETW(req.wIndex, langid);
  161         USETW(req.wLength, 2);  /* only size byte first */
  162         err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
  163                 &actlen, USBD_DEFAULT_TIMEOUT);
  164         if (err)
  165                 return (err);
  166 
  167         if (actlen < 2)
  168                 return (USBD_SHORT_XFER);
  169 
  170         USETW(req.wLength, sdesc->bLength);     /* the whole string */
  171         err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
  172                 &actlen, USBD_DEFAULT_TIMEOUT);
  173         if (err)
  174                 return (err);
  175 
  176         if (actlen != sdesc->bLength) {
  177                 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
  178                     sdesc->bLength, actlen));
  179         }
  180 
  181         *sizep = actlen;
  182         return (USBD_NORMAL_COMPLETION);
  183 }
  184 
  185 static void
  186 usbd_trim_spaces(char *p)
  187 {
  188         char *q, *e;
  189 
  190         if (p == NULL)
  191                 return;
  192         q = e = p;
  193         while (*q == ' ')       /* skip leading spaces */
  194                 q++;
  195         while ((*p = *q++))     /* copy string */
  196                 if (*p++ != ' ') /* remember last non-space */
  197                         e = p;
  198         *e = 0;                 /* kill trailing spaces */
  199 }
  200 
  201 static void
  202 usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev)
  203 {
  204         usb_device_descriptor_t *udd = &dev->ddesc;
  205         char *vendor = 0, *product = 0;
  206 #ifdef USBVERBOSE
  207         const struct usb_knowndev *kdp;
  208 #endif
  209 
  210         if (dev == NULL) {
  211                 v[0] = p[0] = '\0';
  212                 return;
  213         }
  214 
  215         if (usedev) {
  216                 if (usbd_get_string(dev, udd->iManufacturer, v,
  217                     USB_MAX_STRING_LEN))
  218                         vendor = NULL;
  219                 else
  220                         vendor = v;
  221                 usbd_trim_spaces(vendor);
  222                 if (usbd_get_string(dev, udd->iProduct, p,
  223                     USB_MAX_STRING_LEN))
  224                         product = NULL;
  225                 else
  226                         product = p;
  227                 usbd_trim_spaces(product);
  228                 if (vendor && !*vendor)
  229                         vendor = NULL;
  230                 if (product && !*product)
  231                         product = NULL;
  232         } else {
  233                 vendor = NULL;
  234                 product = NULL;
  235         }
  236 #ifdef USBVERBOSE
  237         if (vendor == NULL || product == NULL) {
  238                 for(kdp = usb_knowndevs;
  239                     kdp->vendorname != NULL;
  240                     kdp++) {
  241                         if (kdp->vendor == UGETW(udd->idVendor) &&
  242                             (kdp->product == UGETW(udd->idProduct) ||
  243                              (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
  244                                 break;
  245                 }
  246                 if (kdp->vendorname != NULL) {
  247                         if (vendor == NULL)
  248                             vendor = kdp->vendorname;
  249                         if (product == NULL)
  250                             product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
  251                                 kdp->productname : NULL;
  252                 }
  253         }
  254 #endif
  255         if (vendor != NULL && *vendor)
  256                 strcpy(v, vendor);
  257         else
  258                 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
  259         if (product != NULL && *product)
  260                 strcpy(p, product);
  261         else
  262                 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
  263 }
  264 
  265 int
  266 usbd_printBCD(char *cp, int bcd)
  267 {
  268         return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
  269 }
  270 
  271 void
  272 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp)
  273 {
  274         usb_device_descriptor_t *udd = &dev->ddesc;
  275         usbd_interface_handle iface;
  276         char vendor[USB_MAX_STRING_LEN];
  277         char product[USB_MAX_STRING_LEN];
  278         int bcdDevice, bcdUSB;
  279         usb_interface_descriptor_t *id;
  280 
  281         usbd_devinfo_vp(dev, vendor, product, 1);
  282         cp += sprintf(cp, "%s %s", vendor, product);
  283         if (showclass & USBD_SHOW_DEVICE_CLASS)
  284                 cp += sprintf(cp, ", class %d/%d",
  285                     udd->bDeviceClass, udd->bDeviceSubClass);
  286         bcdUSB = UGETW(udd->bcdUSB);
  287         bcdDevice = UGETW(udd->bcdDevice);
  288         cp += sprintf(cp, ", rev ");
  289         cp += usbd_printBCD(cp, bcdUSB);
  290         *cp++ = '/';
  291         cp += usbd_printBCD(cp, bcdDevice);
  292         cp += sprintf(cp, ", addr %d", dev->address);
  293         if (showclass & USBD_SHOW_INTERFACE_CLASS)
  294         {
  295                 /* fetch the interface handle for the first interface */
  296                 (void)usbd_device2interface_handle(dev, 0, &iface);
  297                 id = usbd_get_interface_descriptor(iface);
  298                 cp += sprintf(cp, ", iclass %d/%d",
  299                     id->bInterfaceClass, id->bInterfaceSubClass);
  300         }
  301         *cp = 0;
  302 }
  303 
  304 /* Delay for a certain number of ms */
  305 void
  306 usb_delay_ms(usbd_bus_handle bus, u_int ms)
  307 {
  308         /* Wait at least two clock ticks so we know the time has passed. */
  309         if (bus->use_polling || cold)
  310                 delay((ms+1) * 1000);
  311         else
  312                 pause("usbdly", (ms*hz+999)/1000 + 1);
  313 }
  314 
  315 /* Delay given a device handle. */
  316 void
  317 usbd_delay_ms(usbd_device_handle dev, u_int ms)
  318 {
  319         usb_delay_ms(dev->bus, ms);
  320 }
  321 
  322 usbd_status
  323 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
  324 {
  325         usbd_status err;
  326         int n;
  327 
  328         err = usbd_set_port_feature(dev, port, UHF_PORT_RESET);
  329         DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
  330                     port, usbd_errstr(err)));
  331         if (err)
  332                 return (err);
  333         n = 10;
  334         do {
  335                 /* Wait for device to recover from reset. */
  336                 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
  337                 err = usbd_get_port_status(dev, port, ps);
  338                 if (err) {
  339                         DPRINTF(("usbd_reset_port: get status failed %d\n",
  340                                  err));
  341                         return (err);
  342                 }
  343                 /* If the device disappeared, just give up. */
  344                 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
  345                         return (USBD_NORMAL_COMPLETION);
  346         } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
  347         if (n == 0)
  348                 return (USBD_TIMEOUT);
  349         err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
  350 #ifdef USB_DEBUG
  351         if (err)
  352                 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
  353                          err));
  354 #endif
  355 
  356         /* Wait for the device to recover from reset. */
  357         usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
  358         return (err);
  359 }
  360 
  361 usb_interface_descriptor_t *
  362 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
  363 {
  364         char *p = (char *)cd;
  365         char *end = p + UGETW(cd->wTotalLength);
  366         usb_interface_descriptor_t *d;
  367         int curidx, lastidx, curaidx = 0;
  368 
  369         for (curidx = lastidx = -1; p < end; ) {
  370                 d = (usb_interface_descriptor_t *)p;
  371                 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
  372                             "type=%d\n",
  373                             ifaceidx, curidx, altidx, curaidx,
  374                             d->bLength, d->bDescriptorType));
  375                 if (d->bLength == 0) /* bad descriptor */
  376                         break;
  377                 p += d->bLength;
  378                 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
  379                         if (d->bInterfaceNumber != lastidx) {
  380                                 lastidx = d->bInterfaceNumber;
  381                                 curidx++;
  382                                 curaidx = 0;
  383                         } else
  384                                 curaidx++;
  385                         if (ifaceidx == curidx && altidx == curaidx)
  386                                 return (d);
  387                 }
  388         }
  389         return (NULL);
  390 }
  391 
  392 usb_endpoint_descriptor_t *
  393 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
  394                 int endptidx)
  395 {
  396         char *p = (char *)cd;
  397         char *end = p + UGETW(cd->wTotalLength);
  398         usb_interface_descriptor_t *d;
  399         usb_endpoint_descriptor_t *e;
  400         int curidx;
  401 
  402         d = usbd_find_idesc(cd, ifaceidx, altidx);
  403         if (d == NULL)
  404                 return (NULL);
  405         if (endptidx >= d->bNumEndpoints) /* quick exit */
  406                 return (NULL);
  407 
  408         curidx = -1;
  409         for (p = (char *)d + d->bLength; p < end; ) {
  410                 e = (usb_endpoint_descriptor_t *)p;
  411                 if (e->bLength == 0) /* bad descriptor */
  412                         break;
  413                 p += e->bLength;
  414                 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
  415                         return (NULL);
  416                 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
  417                         curidx++;
  418                         if (curidx == endptidx)
  419                                 return (e);
  420                 }
  421         }
  422         return (NULL);
  423 }
  424 
  425 usbd_status
  426 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
  427 {
  428         usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
  429         usb_interface_descriptor_t *idesc;
  430         char *p, *end;
  431         int endpt, nendpt;
  432 
  433         DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
  434                     ifaceidx, altidx));
  435         idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
  436         if (idesc == NULL)
  437                 return (USBD_INVAL);
  438         ifc->device = dev;
  439         ifc->idesc = idesc;
  440         ifc->index = ifaceidx;
  441         ifc->altindex = altidx;
  442         nendpt = ifc->idesc->bNumEndpoints;
  443         DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
  444         if (nendpt != 0) {
  445                 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
  446                                         M_USB, M_NOWAIT);
  447                 if (ifc->endpoints == NULL)
  448                         return (USBD_NOMEM);
  449         } else
  450                 ifc->endpoints = NULL;
  451         ifc->priv = NULL;
  452         p = (char *)ifc->idesc + ifc->idesc->bLength;
  453         end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
  454 #define ed ((usb_endpoint_descriptor_t *)p)
  455         for (endpt = 0; endpt < nendpt; endpt++) {
  456                 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
  457                 for (; p < end; p += ed->bLength) {
  458                         DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
  459                                      "len=%d type=%d\n",
  460                                  p, end, ed->bLength, ed->bDescriptorType));
  461                         if (p + ed->bLength <= end && ed->bLength != 0 &&
  462                             ed->bDescriptorType == UDESC_ENDPOINT)
  463                                 goto found;
  464                         if (ed->bLength == 0 ||
  465                             ed->bDescriptorType == UDESC_INTERFACE)
  466                                 break;
  467                 }
  468                 /* passed end, or bad desc */
  469                 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
  470                        ed->bLength == 0 ? "0 length" :
  471                        ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
  472                        "out of data");
  473                 goto bad;
  474         found:
  475                 ifc->endpoints[endpt].edesc = ed;
  476                 if (dev->speed == USB_SPEED_HIGH) {
  477                         u_int mps;
  478                         /* Control and bulk endpoints have max packet limits. */
  479                         switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
  480                         case UE_CONTROL:
  481                                 mps = USB_2_MAX_CTRL_PACKET;
  482                                 goto check;
  483                         case UE_BULK:
  484                                 mps = USB_2_MAX_BULK_PACKET;
  485                         check:
  486                                 if (UGETW(ed->wMaxPacketSize) != mps) {
  487                                         USETW(ed->wMaxPacketSize, mps);
  488 #ifdef DIAGNOSTIC
  489                                         printf("usbd_fill_iface_data: bad max "
  490                                                "packet size\n");
  491 #endif
  492                                 }
  493                                 break;
  494                         default:
  495                                 break;
  496                         }
  497                 }
  498                 ifc->endpoints[endpt].refcnt = 0;
  499                 ifc->endpoints[endpt].savedtoggle = 0;
  500                 p += ed->bLength;
  501         }
  502 #undef ed
  503         LIST_INIT(&ifc->pipes);
  504         return (USBD_NORMAL_COMPLETION);
  505 
  506  bad:
  507         if (ifc->endpoints != NULL) {
  508                 free(ifc->endpoints, M_USB);
  509                 ifc->endpoints = NULL;
  510         }
  511         return (USBD_INVAL);
  512 }
  513 
  514 void
  515 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
  516 {
  517         usbd_interface_handle ifc = &dev->ifaces[ifcno];
  518         if (ifc->endpoints)
  519                 free(ifc->endpoints, M_USB);
  520 }
  521 
  522 static usbd_status
  523 usbd_set_config(usbd_device_handle dev, int conf)
  524 {
  525         usb_device_request_t req;
  526 
  527         req.bmRequestType = UT_WRITE_DEVICE;
  528         req.bRequest = UR_SET_CONFIG;
  529         USETW(req.wValue, conf);
  530         USETW(req.wIndex, 0);
  531         USETW(req.wLength, 0);
  532         return (usbd_do_request(dev, &req, 0));
  533 }
  534 
  535 usbd_status
  536 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
  537 {
  538         int index;
  539         usb_config_descriptor_t cd;
  540         usbd_status err;
  541 
  542         if (no == USB_UNCONFIG_NO)
  543                 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
  544 
  545         DPRINTFN(5,("usbd_set_config_no: %d\n", no));
  546         /* Figure out what config index to use. */
  547         for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
  548                 err = usbd_get_config_desc(dev, index, &cd);
  549                 if (err)
  550                         return (err);
  551                 if (cd.bConfigurationValue == no)
  552                         return (usbd_set_config_index(dev, index, msg));
  553         }
  554         return (USBD_INVAL);
  555 }
  556 
  557 usbd_status
  558 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
  559 {
  560         usb_status_t ds;
  561         usb_config_descriptor_t cd, *cdp;
  562         usbd_status err;
  563         int i, ifcidx, nifc, len, selfpowered, power;
  564 
  565         DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
  566 
  567         if (dev->config != USB_UNCONFIG_NO) {
  568                 nifc = dev->cdesc->bNumInterface;
  569 
  570                 /* Check that all interfaces are idle */
  571                 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
  572                         if (LIST_EMPTY(&dev->ifaces[ifcidx].pipes))
  573                                 continue;
  574                         DPRINTF(("usbd_set_config_index: open pipes exist\n"));
  575                         return (USBD_IN_USE);
  576                 }
  577 
  578                 DPRINTF(("usbd_set_config_index: free old config\n"));
  579                 /* Free all configuration data structures. */
  580                 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
  581                         usbd_free_iface_data(dev, ifcidx);
  582                 free(dev->ifaces, M_USB);
  583                 free(dev->cdesc, M_USB);
  584                 dev->ifaces = NULL;
  585                 dev->cdesc = NULL;
  586                 dev->config = USB_UNCONFIG_NO;
  587         }
  588 
  589         if (index == USB_UNCONFIG_INDEX) {
  590                 /* We are unconfiguring the device, so leave unallocated. */
  591                 DPRINTF(("usbd_set_config_index: set config 0\n"));
  592                 err = usbd_set_config(dev, USB_UNCONFIG_NO);
  593                 if (err)
  594                         DPRINTF(("usbd_set_config_index: setting config=0 "
  595                                  "failed, error=%s\n", usbd_errstr(err)));
  596                 return (err);
  597         }
  598 
  599         /* Get the short descriptor. */
  600         err = usbd_get_config_desc(dev, index, &cd);
  601         if (err)
  602                 return (err);
  603         len = UGETW(cd.wTotalLength);
  604         cdp = malloc(len, M_USB, M_NOWAIT);
  605         if (cdp == NULL)
  606                 return (USBD_NOMEM);
  607 
  608         /* Get the full descriptor.  Try a few times for slow devices. */
  609         for (i = 0; i < 3; i++) {
  610                 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
  611                 if (!err)
  612                         break;
  613                 usbd_delay_ms(dev, 200);
  614         }
  615         if (err)
  616                 goto bad;
  617         if (cdp->bDescriptorType != UDESC_CONFIG) {
  618                 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
  619                              cdp->bDescriptorType));
  620                 err = USBD_INVAL;
  621                 goto bad;
  622         }
  623 
  624         /* Figure out if the device is self or bus powered. */
  625         selfpowered = 0;
  626         if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
  627             (cdp->bmAttributes & UC_SELF_POWERED)) {
  628                 /* May be self powered. */
  629                 if (cdp->bmAttributes & UC_BUS_POWERED) {
  630                         /* Must ask device. */
  631                         if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
  632                                 /*
  633                                  * Hub claims to be self powered, but isn't.
  634                                  * It seems that the power status can be
  635                                  * determined by the hub characteristics.
  636                                  */
  637                                 usb_hub_descriptor_t hd;
  638                                 usb_device_request_t req;
  639                                 req.bmRequestType = UT_READ_CLASS_DEVICE;
  640                                 req.bRequest = UR_GET_DESCRIPTOR;
  641                                 USETW(req.wValue, 0);
  642                                 USETW(req.wIndex, 0);
  643                                 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
  644                                 err = usbd_do_request(dev, &req, &hd);
  645                                 if (!err &&
  646                                     (UGETW(hd.wHubCharacteristics) &
  647                                      UHD_PWR_INDIVIDUAL))
  648                                         selfpowered = 1;
  649                                 DPRINTF(("usbd_set_config_index: charac=0x%04x"
  650                                     ", error=%s\n",
  651                                     UGETW(hd.wHubCharacteristics),
  652                                     usbd_errstr(err)));
  653                         } else {
  654                                 err = usbd_get_device_status(dev, &ds);
  655                                 if (!err &&
  656                                     (UGETW(ds.wStatus) & UDS_SELF_POWERED))
  657                                         selfpowered = 1;
  658                                 DPRINTF(("usbd_set_config_index: status=0x%04x"
  659                                     ", error=%s\n",
  660                                     UGETW(ds.wStatus), usbd_errstr(err)));
  661                         }
  662                 } else
  663                         selfpowered = 1;
  664         }
  665         DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
  666                  "selfpowered=%d, power=%d\n",
  667                  cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
  668                  selfpowered, cdp->bMaxPower * 2));
  669 
  670         /* Check if we have enough power. */
  671 #ifdef USB_DEBUG
  672         if (dev->powersrc == NULL) {
  673                 DPRINTF(("usbd_set_config_index: No power source?\n"));
  674                 return (USBD_IOERROR);
  675         }
  676 #endif
  677         power = cdp->bMaxPower * 2;
  678         if (power > dev->powersrc->power) {
  679                 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
  680                 /* XXX print nicer message. */
  681                 if (msg)
  682                         device_printf(dev->bus->bdev,
  683                                       "device addr %d (config %d) exceeds "
  684                                       "power budget, %d mA > %d mA\n",
  685                                       dev->address, cdp->bConfigurationValue,
  686                                       power, dev->powersrc->power);
  687                 err = USBD_NO_POWER;
  688                 goto bad;
  689         }
  690         dev->power = power;
  691         dev->self_powered = selfpowered;
  692 
  693         /* Set the actual configuration value. */
  694         DPRINTF(("usbd_set_config_index: set config %d\n",
  695                  cdp->bConfigurationValue));
  696         err = usbd_set_config(dev, cdp->bConfigurationValue);
  697         if (err) {
  698                 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
  699                          "error=%s\n",
  700                          cdp->bConfigurationValue, usbd_errstr(err)));
  701                 goto bad;
  702         }
  703 
  704         /* Allocate and fill interface data. */
  705         nifc = cdp->bNumInterface;
  706         dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
  707                              M_USB, M_NOWAIT);
  708         if (dev->ifaces == NULL) {
  709                 err = USBD_NOMEM;
  710                 goto bad;
  711         }
  712         DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
  713         dev->cdesc = cdp;
  714         dev->config = cdp->bConfigurationValue;
  715         for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
  716                 err = usbd_fill_iface_data(dev, ifcidx, 0);
  717                 if (err) {
  718                         while (--ifcidx >= 0)
  719                                 usbd_free_iface_data(dev, ifcidx);
  720                         goto bad;
  721                 }
  722         }
  723 
  724         return (USBD_NORMAL_COMPLETION);
  725 
  726  bad:
  727         free(cdp, M_USB);
  728         return (err);
  729 }
  730 
  731 /* XXX add function for alternate settings */
  732 
  733 usbd_status
  734 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
  735                 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
  736 {
  737         usbd_pipe_handle p;
  738         usbd_status err;
  739 
  740         DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
  741                     dev, iface, ep, pipe));
  742         p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
  743         if (p == NULL)
  744                 return (USBD_NOMEM);
  745         p->device = dev;
  746         p->iface = iface;
  747         p->endpoint = ep;
  748         ep->refcnt++;
  749         p->refcnt = 1;
  750         p->intrxfer = 0;
  751         p->running = 0;
  752         p->aborting = 0;
  753         p->repeat = 0;
  754         p->interval = ival;
  755         STAILQ_INIT(&p->queue);
  756         err = dev->bus->methods->open_pipe(p);
  757         if (err) {
  758                 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
  759                          "%s\n",
  760                          ep->edesc->bEndpointAddress, usbd_errstr(err)));
  761                 free(p, M_USB);
  762                 return (err);
  763         }
  764 
  765         if (dev->quirks->uq_flags & UQ_OPEN_CLEARSTALL) {
  766                 /* Clear any stall and make sure DATA0 toggle will be used next. */
  767                 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT) {
  768                         err = usbd_clear_endpoint_stall(p);
  769                         if (err && err != USBD_STALLED && err != USBD_TIMEOUT) {
  770                                 printf("usbd_setup_pipe: failed to start "
  771                                     "endpoint, %s\n", usbd_errstr(err));
  772                                 return (err);
  773                         }
  774                 }
  775         }
  776 
  777         *pipe = p;
  778         return (USBD_NORMAL_COMPLETION);
  779 }
  780 
  781 /* Abort the device control pipe. */
  782 void
  783 usbd_kill_pipe(usbd_pipe_handle pipe)
  784 {
  785         usbd_abort_pipe(pipe);
  786         pipe->methods->close(pipe);
  787         pipe->endpoint->refcnt--;
  788         free(pipe, M_USB);
  789 }
  790 
  791 int
  792 usbd_getnewaddr(usbd_bus_handle bus)
  793 {
  794         int addr;
  795 
  796         for (addr = 1; addr < USB_MAX_DEVICES; addr++)
  797                 if (bus->devices[addr] == 0)
  798                         return (addr);
  799         return (-1);
  800 }
  801 
  802 
  803 usbd_status
  804 usbd_probe_and_attach(device_t parent, usbd_device_handle dev,
  805                       int port, int addr)
  806 {
  807         struct usb_attach_arg uaa;
  808         usb_device_descriptor_t *dd = &dev->ddesc;
  809         int found, i, confi, nifaces;
  810         usbd_status err;
  811         device_t *tmpdv;
  812         usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
  813         char *devinfo;
  814 
  815         /* XXX FreeBSD may leak resources on failure cases -- fixme */
  816         device_t bdev;
  817         struct usb_attach_arg *uaap;
  818 
  819         devinfo = malloc(1024, M_USB, M_NOWAIT);
  820         if (devinfo == NULL) {
  821                 device_printf(parent, "Can't allocate memory for probe string\n");
  822                 return (USBD_NOMEM);
  823         }
  824         bdev = device_add_child(parent, NULL, -1);
  825         if (!bdev) {
  826                 free(devinfo, M_USB);
  827                 device_printf(parent, "Device creation failed\n");
  828                 return (USBD_INVAL);
  829         }
  830         uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
  831         if (uaap == NULL) {
  832                 free(devinfo, M_USB);
  833                 return (USBD_INVAL);
  834         }
  835         device_set_ivars(bdev, uaap);
  836         uaa.device = dev;
  837         uaa.iface = NULL;
  838         uaa.ifaces = NULL;
  839         uaa.nifaces = 0;
  840         uaa.usegeneric = 0;
  841         uaa.port = port;
  842         uaa.configno = UHUB_UNK_CONFIGURATION;
  843         uaa.ifaceno = UHUB_UNK_INTERFACE;
  844         uaa.vendor = UGETW(dd->idVendor);
  845         uaa.product = UGETW(dd->idProduct);
  846         uaa.release = UGETW(dd->bcdDevice);
  847         uaa.matchlvl = 0;
  848 
  849         /* First try with device specific drivers. */
  850         DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
  851 
  852         dev->ifacenums = NULL;
  853         dev->subdevs = malloc(2 * sizeof(device_t), M_USB, M_NOWAIT);
  854         if (dev->subdevs == NULL) {
  855                 free(devinfo, M_USB);
  856                 return (USBD_NOMEM);
  857         }
  858         dev->subdevs[0] = bdev;
  859         dev->subdevs[1] = 0;
  860         *uaap = uaa;
  861         usbd_devinfo(dev, 1, devinfo);
  862         device_set_desc_copy(bdev, devinfo);
  863         if (device_probe_and_attach(bdev) == 0) {
  864                 free(devinfo, M_USB);
  865                 return (USBD_NORMAL_COMPLETION);
  866         }
  867 
  868         /*
  869          * Free subdevs so we can reallocate it larger for the number of
  870          * interfaces 
  871          */
  872         tmpdv = dev->subdevs;
  873         dev->subdevs = NULL;
  874         free(tmpdv, M_USB);
  875         DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
  876 
  877         DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
  878                  dd->bNumConfigurations));
  879         /* Next try with interface drivers. */
  880         for (confi = 0; confi < dd->bNumConfigurations; confi++) {
  881                 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
  882                             confi));
  883                 err = usbd_set_config_index(dev, confi, 1);
  884                 if (err) {
  885 #ifdef USB_DEBUG
  886                         DPRINTF(("%s: port %d, set config at addr %d failed, "
  887                                  "error=%s\n", device_get_nameunit(parent), port,
  888                                  addr, usbd_errstr(err)));
  889 #else
  890                         printf("%s: port %d, set config at addr %d failed\n",
  891                                device_get_nameunit(parent), port, addr);
  892 #endif
  893                         free(devinfo, M_USB);
  894                         return (err);
  895                 }
  896                 nifaces = dev->cdesc->bNumInterface;
  897                 uaa.configno = dev->cdesc->bConfigurationValue;
  898                 for (i = 0; i < nifaces; i++)
  899                         ifaces[i] = &dev->ifaces[i];
  900                 uaa.ifaces = ifaces;
  901                 uaa.nifaces = nifaces;
  902                 dev->subdevs = malloc((nifaces+1) * sizeof(device_t), M_USB,M_NOWAIT);
  903                 if (dev->subdevs == NULL) {
  904                         free(devinfo, M_USB);
  905                         return (USBD_NOMEM);
  906                 }
  907                 dev->ifacenums = malloc((nifaces) * sizeof(*dev->ifacenums),
  908                     M_USB,M_NOWAIT);
  909                 if (dev->ifacenums == NULL) {
  910                         free(devinfo, M_USB);
  911                         return (USBD_NOMEM);
  912                 }
  913 
  914                 found = 0;
  915                 for (i = 0; i < nifaces; i++) {
  916                         if (ifaces[i] == NULL)
  917                                 continue; /* interface already claimed */
  918                         uaa.iface = ifaces[i];
  919                         uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
  920                         dev->subdevs[found] = bdev;
  921                         dev->subdevs[found + 1] = 0;
  922                         dev->ifacenums[found] = i;
  923                         *uaap = uaa;
  924                         usbd_devinfo(dev, 1, devinfo);
  925                         device_set_desc_copy(bdev, devinfo);
  926                         if (device_probe_and_attach(bdev) == 0) {
  927                                 ifaces[i] = 0; /* consumed */
  928                                 found++;
  929                                 /* create another child for the next iface */
  930                                 bdev = device_add_child(parent, NULL, -1);
  931                                 if (!bdev) {
  932                                         device_printf(parent,
  933                                             "Device add failed\n");
  934                                         free(devinfo, M_USB);
  935                                         return (USBD_NORMAL_COMPLETION);
  936                                 }
  937                                 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
  938                                 if (uaap == NULL) {
  939                                         free(devinfo, M_USB);
  940                                         return (USBD_NOMEM);
  941                                 }
  942                                 device_set_ivars(bdev, uaap);
  943                         } else {
  944                                 dev->subdevs[found] = 0;
  945                         }
  946                 }
  947                 if (found != 0) {
  948                         /* remove the last created child.  It is unused */
  949                         free(uaap, M_USB);
  950                         free(devinfo, M_USB);
  951                         device_delete_child(parent, bdev);
  952                         /* free(uaap, M_USB); */ /* May be needed? xxx */
  953                         return (USBD_NORMAL_COMPLETION);
  954                 }
  955                 tmpdv = dev->subdevs;
  956                 dev->subdevs = NULL;
  957                 free(tmpdv, M_USB);
  958                 free(dev->ifacenums, M_USB);
  959                 dev->ifacenums = NULL;
  960         }
  961         /* No interfaces were attached in any of the configurations. */
  962 
  963         if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
  964                 usbd_set_config_index(dev, 0, 0);
  965 
  966         DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
  967 
  968         /* Finally try the generic driver. */
  969         uaa.iface = NULL;
  970         uaa.usegeneric = 1;
  971         uaa.configno = UHUB_UNK_CONFIGURATION;
  972         uaa.ifaceno = UHUB_UNK_INTERFACE;
  973         dev->subdevs = malloc(2 * sizeof(device_t), M_USB, M_NOWAIT);
  974         if (dev->subdevs == 0) {
  975                 free(devinfo, M_USB);
  976                 return (USBD_NOMEM);
  977         }
  978         dev->subdevs[0] = bdev;
  979         dev->subdevs[1] = 0;
  980         *uaap = uaa;
  981         usbd_devinfo(dev, 1, devinfo);
  982         device_set_desc_copy(bdev, devinfo);
  983         free(devinfo, M_USB);
  984         if (device_probe_and_attach(bdev) == 0)
  985                 return (USBD_NORMAL_COMPLETION);
  986 
  987         /*
  988          * The generic attach failed, but leave the device as it is.
  989          * We just did not find any drivers, that's all.  The device is
  990          * fully operational and not harming anyone.
  991          */
  992         DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
  993         return (USBD_NORMAL_COMPLETION);
  994 }
  995 
  996 
  997 /*
  998  * Called when a new device has been put in the powered state,
  999  * but not yet in the addressed state.
 1000  * Get initial descriptor, set the address, get full descriptor,
 1001  * and attach a driver.
 1002  */
 1003 usbd_status
 1004 usbd_new_device(device_t parent, usbd_bus_handle bus, int depth,
 1005                 int speed, int port, struct usbd_port *up)
 1006 {
 1007         usbd_device_handle dev, adev;
 1008         struct usbd_device *hub;
 1009         usb_device_descriptor_t *dd;
 1010         usb_port_status_t ps;
 1011         usbd_status err;
 1012         int addr;
 1013         int i;
 1014         int p;
 1015 
 1016         DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
 1017                  bus, port, depth, speed));
 1018         addr = usbd_getnewaddr(bus);
 1019         if (addr < 0) {
 1020                 device_printf(bus->bdev, "No free USB addresses\n");
 1021                 return (USBD_NO_ADDR);
 1022         }
 1023 
 1024         dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
 1025         if (dev == NULL)
 1026                 return (USBD_NOMEM);
 1027 
 1028         dev->bus = bus;
 1029 
 1030         /* Set up default endpoint handle. */
 1031         dev->def_ep.edesc = &dev->def_ep_desc;
 1032 
 1033         /* Set up default endpoint descriptor. */
 1034         dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
 1035         dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
 1036         dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
 1037         dev->def_ep_desc.bmAttributes = UE_CONTROL;
 1038         USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
 1039         dev->def_ep_desc.bInterval = 0;
 1040 
 1041         dev->quirks = &usbd_no_quirk;
 1042         dev->address = USB_START_ADDR;
 1043         dev->ddesc.bMaxPacketSize = 0;
 1044         dev->depth = depth;
 1045         dev->powersrc = up;
 1046         dev->myhub = up->parent;
 1047 
 1048         up->device = dev;
 1049 
 1050         if (up->parent && speed > up->parent->speed) {
 1051 #ifdef USB_DEBUG
 1052                 printf("%s: maxium speed of attached "
 1053                     "device, %d, is higher than speed "
 1054                     "of parent HUB, %d.\n",
 1055                     __FUNCTION__, speed, up->parent->speed);
 1056 #endif
 1057                 /*
 1058                  * Reduce the speed, otherwise we won't setup the
 1059                  * proper transfer methods.
 1060                  */
 1061                 speed = up->parent->speed;
 1062         }
 1063         
 1064         /* Locate port on upstream high speed hub */
 1065         for (adev = dev, hub = up->parent;
 1066              hub != NULL && hub->speed != USB_SPEED_HIGH;
 1067              adev = hub, hub = hub->myhub)
 1068                 ;
 1069         if (hub) {
 1070                 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
 1071                         if (hub->hub->ports[p].device == adev) {
 1072                                 dev->myhsport = &hub->hub->ports[p];
 1073                                 goto found;
 1074                         }
 1075                 }
 1076                 panic("usbd_new_device: cannot find HS port\n");
 1077         found:
 1078                 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
 1079         } else {
 1080                 dev->myhsport = NULL;
 1081         }
 1082         dev->speed = speed;
 1083         dev->langid = USBD_NOLANG;
 1084         dev->cookie.cookie = ++usb_cookie_no;
 1085 
 1086         /* Establish the default pipe. */
 1087         err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
 1088                               &dev->default_pipe);
 1089         if (err) {
 1090                 usbd_remove_device(dev, up);
 1091                 return (err);
 1092         }
 1093 
 1094         dd = &dev->ddesc;
 1095         /* Try a few times in case the device is slow (i.e. outside specs.) */
 1096         DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
 1097         for (i = 0; i < 15; i++) {
 1098                 /* Get the first 8 bytes of the device descriptor. */
 1099                 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
 1100                 if (!err)
 1101                         break;
 1102                 usbd_delay_ms(dev, 200);
 1103                 if ((i & 3) == 3) {
 1104                         DPRINTFN(-1,("usb_new_device: set address %d "
 1105                             "failed - trying a port reset\n", addr));
 1106                         usbd_reset_port(up->parent, port, &ps);
 1107                 }
 1108 
 1109         }
 1110         if (err) {
 1111                 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
 1112                               "failed\n", addr));
 1113                 usbd_remove_device(dev, up);
 1114                 return (err);
 1115         }
 1116 
 1117         if (speed == USB_SPEED_HIGH) {
 1118                 /* Max packet size must be 64 (sec 5.5.3). */
 1119                 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
 1120 #ifdef DIAGNOSTIC
 1121                         printf("usbd_new_device: addr=%d bad max packet size\n",
 1122                                addr);
 1123 #endif
 1124                         dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
 1125                 }
 1126         }
 1127 
 1128         DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
 1129                  "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
 1130                  addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
 1131                  dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
 1132                  dev->speed));
 1133 
 1134         if (dd->bDescriptorType != UDESC_DEVICE) {
 1135                 /* Illegal device descriptor */
 1136                 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
 1137                              dd->bDescriptorType));
 1138                 usbd_remove_device(dev, up);
 1139                 return (USBD_INVAL);
 1140         }
 1141 
 1142         if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
 1143                 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
 1144                 usbd_remove_device(dev, up);
 1145                 return (USBD_INVAL);
 1146         }
 1147 
 1148         USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
 1149 
 1150         /* Re-establish the default pipe with the new max packet size. */
 1151         usbd_kill_pipe(dev->default_pipe);
 1152         err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
 1153             &dev->default_pipe);
 1154         if (err) {
 1155                 usbd_remove_device(dev, up);
 1156                 return (err);
 1157         }
 1158 
 1159         err = usbd_reload_device_desc(dev);
 1160         if (err) {
 1161                 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
 1162                               "failed\n", addr));
 1163                 usbd_remove_device(dev, up);
 1164                 return (err);
 1165         }
 1166 
 1167         /* Set the address */
 1168         DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
 1169         err = usbd_set_address(dev, addr);
 1170         if (err) {
 1171                 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
 1172                 err = USBD_SET_ADDR_FAILED;
 1173                 usbd_remove_device(dev, up);
 1174                 return (err);
 1175         }
 1176 
 1177         /* Allow device time to set new address */
 1178         usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
 1179         dev->address = addr;    /* New device address now */
 1180         bus->devices[addr] = dev;
 1181 
 1182         /* Re-establish the default pipe with the new address. */
 1183         usbd_kill_pipe(dev->default_pipe);
 1184         err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
 1185             &dev->default_pipe);
 1186         if (err) {
 1187                 usbd_remove_device(dev, up);
 1188                 return (err);
 1189         }
 1190 
 1191         /* Assume 100mA bus powered for now. Changed when configured. */
 1192         dev->power = USB_MIN_POWER;
 1193         dev->self_powered = 0;
 1194 
 1195         DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
 1196                  addr, dev, parent));
 1197 
 1198         err = usbd_probe_and_attach(parent, dev, port, addr);
 1199         if (err) {
 1200                 usbd_remove_device(dev, up);
 1201                 return (err);
 1202         }
 1203 
 1204         usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
 1205 
 1206         return (USBD_NORMAL_COMPLETION);
 1207 }
 1208 
 1209 usbd_status
 1210 usbd_reload_device_desc(usbd_device_handle dev)
 1211 {
 1212         usbd_status err;
 1213         int i;
 1214 
 1215         /* Get the full device descriptor. */
 1216         for (i = 0; i < 3; ++i) {
 1217                 err = usbd_get_device_desc(dev, &dev->ddesc);
 1218                 if (!err)
 1219                         break;
 1220                 usbd_delay_ms(dev, 200);
 1221         }
 1222         if (err)
 1223                 return (err);
 1224 
 1225         /* Figure out what's wrong with this device. */
 1226         dev->quirks = usbd_find_quirk(&dev->ddesc);
 1227 
 1228         return (USBD_NORMAL_COMPLETION);
 1229 }
 1230 
 1231 void
 1232 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
 1233 {
 1234         DPRINTF(("usbd_remove_device: %p\n", dev));
 1235 
 1236         if (dev->default_pipe != NULL)
 1237                 usbd_kill_pipe(dev->default_pipe);
 1238         up->device = NULL;
 1239         dev->bus->devices[dev->address] = NULL;
 1240 
 1241         free(dev, M_USB);
 1242 }
 1243 
 1244 void
 1245 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
 1246                      int usedev)
 1247 {
 1248         struct usbd_port *p;
 1249         int i, err, s;
 1250 
 1251         di->udi_bus = device_get_unit(dev->bus->bdev);
 1252         di->udi_addr = dev->address;
 1253         di->udi_cookie = dev->cookie;
 1254         usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
 1255         usbd_printBCD(di->udi_release, UGETW(dev->ddesc.bcdDevice));
 1256         di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
 1257         di->udi_productNo = UGETW(dev->ddesc.idProduct);
 1258         di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
 1259         di->udi_class = dev->ddesc.bDeviceClass;
 1260         di->udi_subclass = dev->ddesc.bDeviceSubClass;
 1261         di->udi_protocol = dev->ddesc.bDeviceProtocol;
 1262         di->udi_config = dev->config;
 1263         di->udi_power = dev->self_powered ? 0 : dev->power;
 1264         di->udi_speed = dev->speed;
 1265 
 1266         if (dev->subdevs != NULL) {
 1267                 for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) {
 1268                         if (device_is_attached(dev->subdevs[i]))
 1269                                 strlcpy(di->udi_devnames[i],
 1270                                     device_get_nameunit(dev->subdevs[i]),
 1271                                     USB_MAX_DEVNAMELEN);
 1272                         else
 1273                                 di->udi_devnames[i][0] = 0;
 1274                 }
 1275         } else {
 1276                 i = 0;
 1277         }
 1278         for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
 1279                 di->udi_devnames[i][0] = 0;                 /* empty */
 1280 
 1281         if (dev->hub) {
 1282                 for (i = 0;
 1283                      i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
 1284                              i < dev->hub->hubdesc.bNbrPorts;
 1285                      i++) {
 1286                         p = &dev->hub->ports[i];
 1287                         if (p->device)
 1288                                 err = p->device->address;
 1289                         else {
 1290                                 s = UGETW(p->status.wPortStatus);
 1291                                 if (s & UPS_PORT_ENABLED)
 1292                                         err = USB_PORT_ENABLED;
 1293                                 else if (s & UPS_SUSPEND)
 1294                                         err = USB_PORT_SUSPENDED;
 1295                                 else if (s & UPS_PORT_POWER)
 1296                                         err = USB_PORT_POWERED;
 1297                                 else
 1298                                         err = USB_PORT_DISABLED;
 1299                         }
 1300                         di->udi_ports[i] = err;
 1301                 }
 1302                 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
 1303         } else
 1304                 di->udi_nports = 0;
 1305 }
 1306 
 1307 void
 1308 usb_free_device(usbd_device_handle dev)
 1309 {
 1310         int ifcidx, nifc;
 1311 
 1312         if (dev->default_pipe != NULL)
 1313                 usbd_kill_pipe(dev->default_pipe);
 1314         if (dev->ifaces != NULL) {
 1315                 nifc = dev->cdesc->bNumInterface;
 1316                 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
 1317                         usbd_free_iface_data(dev, ifcidx);
 1318                 free(dev->ifaces, M_USB);
 1319         }
 1320         if (dev->cdesc != NULL)
 1321                 free(dev->cdesc, M_USB);
 1322         if (dev->subdevs != NULL)
 1323                 free(dev->subdevs, M_USB);
 1324         if (dev->ifacenums != NULL)
 1325                 free(dev->ifacenums, M_USB);
 1326         free(dev, M_USB);
 1327 }
 1328 
 1329 /*
 1330  * The general mechanism for detaching drivers works as follows: Each
 1331  * driver is responsible for maintaining a reference count on the
 1332  * number of outstanding references to its softc (e.g.  from
 1333  * processing hanging in a read or write).  The detach method of the
 1334  * driver decrements this counter and flags in the softc that the
 1335  * driver is dying and then wakes any sleepers.  It then sleeps on the
 1336  * softc.  Each place that can sleep must maintain the reference
 1337  * count.  When the reference count drops to -1 (0 is the normal value
 1338  * of the reference count) the a wakeup on the softc is performed
 1339  * signaling to the detach waiter that all references are gone.
 1340  */
 1341 
 1342 /*
 1343  * Called from process context when we discover that a port has
 1344  * been disconnected.
 1345  */
 1346 void
 1347 usb_disconnect_port(struct usbd_port *up, device_t parent)
 1348 {
 1349         usbd_device_handle dev = up->device;
 1350         const char *hubname = device_get_nameunit(parent);
 1351         int i;
 1352 
 1353         DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
 1354                     up, dev, up->portno));
 1355 
 1356 #ifdef DIAGNOSTIC
 1357         if (dev == NULL) {
 1358                 printf("usb_disconnect_port: no device\n");
 1359                 return;
 1360         }
 1361 #endif
 1362 
 1363         if (dev->subdevs != NULL) {
 1364                 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
 1365                 for (i = 0; dev->subdevs[i]; i++) {
 1366                         if (!device_is_quiet(dev->subdevs[i])) {
 1367                                 device_printf(dev->subdevs[i],
 1368                                                   "at %s", hubname);
 1369                                 if (up->portno != 0)
 1370                                         printf(" port %d", up->portno);
 1371                                 printf(" (addr %d) disconnected\n", dev->address);
 1372                         }
 1373 
 1374                         struct usb_attach_arg *uaap =
 1375                             device_get_ivars(dev->subdevs[i]);
 1376                         device_detach(dev->subdevs[i]);
 1377                         free(uaap, M_USB);
 1378                         device_delete_child(device_get_parent(dev->subdevs[i]),
 1379                             dev->subdevs[i]);
 1380                         dev->subdevs[i] = NULL;
 1381                 }
 1382         }
 1383 
 1384         usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
 1385         dev->bus->devices[dev->address] = NULL;
 1386         up->device = NULL;
 1387         usb_free_device(dev);
 1388 }

Cache object: 9a0c61de21dd53b862edd62774456c90


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