1 /* $NetBSD: usb_subr.c,v 1.99 2002/07/11 21:14:34 augustss Exp $ */
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 $
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
17 * Copyright (c) 1998 The NetBSD Foundation, Inc.
18 * All rights reserved.
20 * This code is derived from software contributed to The NetBSD Foundation
21 * by Lennart Augustsson (lennart@augustsson.net) at
22 * Carlstedt Research & Technology.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
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.
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.
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>
62 #include <sys/sysctl.h>
64 #include <machine/bus.h>
66 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdi_util.h>
70 #include <dev/usb/usbdivar.h>
72 #include <dev/usb/usb_quirks.h>
74 #define delay(d) DELAY(d)
77 #define DPRINTF(x) if (usbdebug) printf x
78 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x
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);
93 static u_int32_t usb_cookie_no = 0;
96 typedef u_int16_t usb_vendor_id_t;
97 typedef u_int16_t usb_product_id_t;
100 * Descriptions of of known vendors and devices ("products").
102 struct usb_knowndev {
103 usb_vendor_id_t vendor;
104 usb_product_id_t product;
106 char *vendorname, *productname;
108 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
110 #include "usbdevs_data.h"
111 #endif /* USBVERBOSE */
113 static const char * const usbd_error_strs[] = {
137 usbd_errstr(usbd_status err)
139 static char buffer[5];
141 if (err < USBD_ERROR_MAX) {
142 return usbd_error_strs[err];
144 snprintf(buffer, sizeof buffer, "%d", err);
150 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
151 usb_string_descriptor_t *sdesc, int *sizep)
153 usb_device_request_t req;
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);
168 return (USBD_SHORT_XFER);
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);
176 if (actlen != sdesc->bLength) {
177 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
178 sdesc->bLength, actlen));
182 return (USBD_NORMAL_COMPLETION);
186 usbd_trim_spaces(char *p)
193 while (*q == ' ') /* skip leading spaces */
195 while ((*p = *q++)) /* copy string */
196 if (*p++ != ' ') /* remember last non-space */
198 *e = 0; /* kill trailing spaces */
202 usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev)
204 usb_device_descriptor_t *udd = &dev->ddesc;
205 char *vendor = 0, *product = 0;
207 const struct usb_knowndev *kdp;
216 if (usbd_get_string(dev, udd->iManufacturer, v,
221 usbd_trim_spaces(vendor);
222 if (usbd_get_string(dev, udd->iProduct, p,
227 usbd_trim_spaces(product);
228 if (vendor && !*vendor)
230 if (product && !*product)
237 if (vendor == NULL || product == NULL) {
238 for(kdp = usb_knowndevs;
239 kdp->vendorname != NULL;
241 if (kdp->vendor == UGETW(udd->idVendor) &&
242 (kdp->product == UGETW(udd->idProduct) ||
243 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
246 if (kdp->vendorname != NULL) {
248 vendor = kdp->vendorname;
250 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
251 kdp->productname : NULL;
255 if (vendor != NULL && *vendor)
258 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
259 if (product != NULL && *product)
262 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
266 usbd_printBCD(char *cp, int bcd)
268 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
272 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp)
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;
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);
291 cp += usbd_printBCD(cp, bcdDevice);
292 cp += sprintf(cp, ", addr %d", dev->address);
293 if (showclass & USBD_SHOW_INTERFACE_CLASS)
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);
304 /* Delay for a certain number of ms */
306 usb_delay_ms(usbd_bus_handle bus, u_int ms)
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);
312 pause("usbdly", (ms*hz+999)/1000 + 1);
315 /* Delay given a device handle. */
317 usbd_delay_ms(usbd_device_handle dev, u_int ms)
319 usb_delay_ms(dev->bus, ms);
323 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
325 usb_device_request_t req;
329 req.bmRequestType = UT_WRITE_CLASS_OTHER;
330 req.bRequest = UR_SET_FEATURE;
331 USETW(req.wValue, UHF_PORT_RESET);
332 USETW(req.wIndex, port);
333 USETW(req.wLength, 0);
334 err = usbd_do_request(dev, &req, 0);
335 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
336 port, usbd_errstr(err)));
341 /* Wait for device to recover from reset. */
342 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
343 err = usbd_get_port_status(dev, port, ps);
345 DPRINTF(("usbd_reset_port: get status failed %d\n",
349 /* If the device disappeared, just give up. */
350 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
351 return (USBD_NORMAL_COMPLETION);
352 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
354 return (USBD_TIMEOUT);
355 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
358 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
362 /* Wait for the device to recover from reset. */
363 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
367 usb_interface_descriptor_t *
368 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
370 char *p = (char *)cd;
371 char *end = p + UGETW(cd->wTotalLength);
372 usb_interface_descriptor_t *d;
373 int curidx, lastidx, curaidx = 0;
375 for (curidx = lastidx = -1; p < end; ) {
376 d = (usb_interface_descriptor_t *)p;
377 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
379 ifaceidx, curidx, altidx, curaidx,
380 d->bLength, d->bDescriptorType));
381 if (d->bLength == 0) /* bad descriptor */
384 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
385 if (d->bInterfaceNumber != lastidx) {
386 lastidx = d->bInterfaceNumber;
391 if (ifaceidx == curidx && altidx == curaidx)
398 usb_endpoint_descriptor_t *
399 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
402 char *p = (char *)cd;
403 char *end = p + UGETW(cd->wTotalLength);
404 usb_interface_descriptor_t *d;
405 usb_endpoint_descriptor_t *e;
408 d = usbd_find_idesc(cd, ifaceidx, altidx);
411 if (endptidx >= d->bNumEndpoints) /* quick exit */
415 for (p = (char *)d + d->bLength; p < end; ) {
416 e = (usb_endpoint_descriptor_t *)p;
417 if (e->bLength == 0) /* bad descriptor */
420 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
422 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
424 if (curidx == endptidx)
432 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
434 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
435 usb_interface_descriptor_t *idesc;
439 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
441 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
446 ifc->index = ifaceidx;
447 ifc->altindex = altidx;
448 nendpt = ifc->idesc->bNumEndpoints;
449 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
451 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
453 if (ifc->endpoints == NULL)
456 ifc->endpoints = NULL;
458 p = (char *)ifc->idesc + ifc->idesc->bLength;
459 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
460 #define ed ((usb_endpoint_descriptor_t *)p)
461 for (endpt = 0; endpt < nendpt; endpt++) {
462 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
463 for (; p < end; p += ed->bLength) {
464 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
466 p, end, ed->bLength, ed->bDescriptorType));
467 if (p + ed->bLength <= end && ed->bLength != 0 &&
468 ed->bDescriptorType == UDESC_ENDPOINT)
470 if (ed->bLength == 0 ||
471 ed->bDescriptorType == UDESC_INTERFACE)
474 /* passed end, or bad desc */
475 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
476 ed->bLength == 0 ? "0 length" :
477 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
481 ifc->endpoints[endpt].edesc = ed;
482 if (dev->speed == USB_SPEED_HIGH) {
484 /* Control and bulk endpoints have max packet limits. */
485 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
487 mps = USB_2_MAX_CTRL_PACKET;
490 mps = USB_2_MAX_BULK_PACKET;
492 if (UGETW(ed->wMaxPacketSize) != mps) {
493 USETW(ed->wMaxPacketSize, mps);
495 printf("usbd_fill_iface_data: bad max "
504 ifc->endpoints[endpt].refcnt = 0;
505 ifc->endpoints[endpt].savedtoggle = 0;
509 LIST_INIT(&ifc->pipes);
510 return (USBD_NORMAL_COMPLETION);
513 if (ifc->endpoints != NULL) {
514 free(ifc->endpoints, M_USB);
515 ifc->endpoints = NULL;
521 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
523 usbd_interface_handle ifc = &dev->ifaces[ifcno];
525 free(ifc->endpoints, M_USB);
529 usbd_set_config(usbd_device_handle dev, int conf)
531 usb_device_request_t req;
533 req.bmRequestType = UT_WRITE_DEVICE;
534 req.bRequest = UR_SET_CONFIG;
535 USETW(req.wValue, conf);
536 USETW(req.wIndex, 0);
537 USETW(req.wLength, 0);
538 return (usbd_do_request(dev, &req, 0));
542 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
545 usb_config_descriptor_t cd;
548 if (no == USB_UNCONFIG_NO)
549 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
551 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
552 /* Figure out what config index to use. */
553 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
554 err = usbd_get_config_desc(dev, index, &cd);
557 if (cd.bConfigurationValue == no)
558 return (usbd_set_config_index(dev, index, msg));
564 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
567 usb_config_descriptor_t cd, *cdp;
569 int i, ifcidx, nifc, len, selfpowered, power;
571 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
573 if (dev->config != USB_UNCONFIG_NO) {
574 nifc = dev->cdesc->bNumInterface;
576 /* Check that all interfaces are idle */
577 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
578 if (LIST_EMPTY(&dev->ifaces[ifcidx].pipes))
580 DPRINTF(("usbd_set_config_index: open pipes exist\n"));
581 return (USBD_IN_USE);
584 DPRINTF(("usbd_set_config_index: free old config\n"));
585 /* Free all configuration data structures. */
586 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
587 usbd_free_iface_data(dev, ifcidx);
588 free(dev->ifaces, M_USB);
589 free(dev->cdesc, M_USB);
592 dev->config = USB_UNCONFIG_NO;
595 if (index == USB_UNCONFIG_INDEX) {
596 /* We are unconfiguring the device, so leave unallocated. */
597 DPRINTF(("usbd_set_config_index: set config 0\n"));
598 err = usbd_set_config(dev, USB_UNCONFIG_NO);
600 DPRINTF(("usbd_set_config_index: setting config=0 "
601 "failed, error=%s\n", usbd_errstr(err)));
605 /* Get the short descriptor. */
606 err = usbd_get_config_desc(dev, index, &cd);
609 len = UGETW(cd.wTotalLength);
610 cdp = malloc(len, M_USB, M_NOWAIT);
614 /* Get the full descriptor. Try a few times for slow devices. */
615 for (i = 0; i < 3; i++) {
616 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
619 usbd_delay_ms(dev, 200);
623 if (cdp->bDescriptorType != UDESC_CONFIG) {
624 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
625 cdp->bDescriptorType));
630 /* Figure out if the device is self or bus powered. */
632 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
633 (cdp->bmAttributes & UC_SELF_POWERED)) {
634 /* May be self powered. */
635 if (cdp->bmAttributes & UC_BUS_POWERED) {
636 /* Must ask device. */
637 if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
639 * Hub claims to be self powered, but isn't.
640 * It seems that the power status can be
641 * determined by the hub characteristics.
643 usb_hub_descriptor_t hd;
644 usb_device_request_t req;
645 req.bmRequestType = UT_READ_CLASS_DEVICE;
646 req.bRequest = UR_GET_DESCRIPTOR;
647 USETW(req.wValue, 0);
648 USETW(req.wIndex, 0);
649 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
650 err = usbd_do_request(dev, &req, &hd);
652 (UGETW(hd.wHubCharacteristics) &
655 DPRINTF(("usbd_set_config_index: charac=0x%04x"
657 UGETW(hd.wHubCharacteristics),
660 err = usbd_get_device_status(dev, &ds);
662 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
664 DPRINTF(("usbd_set_config_index: status=0x%04x"
666 UGETW(ds.wStatus), usbd_errstr(err)));
671 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
672 "selfpowered=%d, power=%d\n",
673 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
674 selfpowered, cdp->bMaxPower * 2));
676 /* Check if we have enough power. */
678 if (dev->powersrc == NULL) {
679 DPRINTF(("usbd_set_config_index: No power source?\n"));
680 return (USBD_IOERROR);
683 power = cdp->bMaxPower * 2;
684 if (power > dev->powersrc->power) {
685 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
686 /* XXX print nicer message. */
688 printf("%s: device addr %d (config %d) exceeds power "
689 "budget, %d mA > %d mA\n",
690 device_get_nameunit(dev->bus->bdev), dev->address,
691 cdp->bConfigurationValue,
692 power, dev->powersrc->power);
697 dev->self_powered = selfpowered;
699 /* Set the actual configuration value. */
700 DPRINTF(("usbd_set_config_index: set config %d\n",
701 cdp->bConfigurationValue));
702 err = usbd_set_config(dev, cdp->bConfigurationValue);
704 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
706 cdp->bConfigurationValue, usbd_errstr(err)));
710 /* Allocate and fill interface data. */
711 nifc = cdp->bNumInterface;
712 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
714 if (dev->ifaces == NULL) {
718 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
720 dev->config = cdp->bConfigurationValue;
721 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
722 err = usbd_fill_iface_data(dev, ifcidx, 0);
724 while (--ifcidx >= 0)
725 usbd_free_iface_data(dev, ifcidx);
730 return (USBD_NORMAL_COMPLETION);
737 /* XXX add function for alternate settings */
740 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
741 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
746 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
747 dev, iface, ep, pipe));
748 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
761 STAILQ_INIT(&p->queue);
762 err = dev->bus->methods->open_pipe(p);
764 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
766 ep->edesc->bEndpointAddress, usbd_errstr(err)));
771 if (dev->quirks->uq_flags & UQ_OPEN_CLEARSTALL) {
772 /* Clear any stall and make sure DATA0 toggle will be used next. */
773 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT) {
774 err = usbd_clear_endpoint_stall(p);
775 if (err && err != USBD_STALLED && err != USBD_TIMEOUT) {
776 printf("usbd_setup_pipe: failed to start "
777 "endpoint, %s\n", usbd_errstr(err));
784 return (USBD_NORMAL_COMPLETION);
787 /* Abort the device control pipe. */
789 usbd_kill_pipe(usbd_pipe_handle pipe)
791 usbd_abort_pipe(pipe);
792 pipe->methods->close(pipe);
793 pipe->endpoint->refcnt--;
798 usbd_getnewaddr(usbd_bus_handle bus)
802 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
803 if (bus->devices[addr] == 0)
810 usbd_probe_and_attach(device_t parent, usbd_device_handle dev,
813 struct usb_attach_arg uaa;
814 usb_device_descriptor_t *dd = &dev->ddesc;
815 int found, i, confi, nifaces;
818 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
821 /* XXX FreeBSD may leak resources on failure cases -- fixme */
823 struct usb_attach_arg *uaap;
825 devinfo = malloc(1024, M_USB, M_NOWAIT);
826 if (devinfo == NULL) {
827 device_printf(parent, "Can't allocate memory for probe string\n");
830 bdev = device_add_child(parent, NULL, -1);
832 free(devinfo, M_USB);
833 device_printf(parent, "Device creation failed\n");
836 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
838 free(devinfo, M_USB);
841 device_set_ivars(bdev, uaap);
848 uaa.configno = UHUB_UNK_CONFIGURATION;
849 uaa.ifaceno = UHUB_UNK_INTERFACE;
850 uaa.vendor = UGETW(dd->idVendor);
851 uaa.product = UGETW(dd->idProduct);
852 uaa.release = UGETW(dd->bcdDevice);
855 /* First try with device specific drivers. */
856 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
858 dev->ifacenums = NULL;
859 dev->subdevs = malloc(2 * sizeof(device_t), M_USB, M_NOWAIT);
860 if (dev->subdevs == NULL) {
861 free(devinfo, M_USB);
864 dev->subdevs[0] = bdev;
867 usbd_devinfo(dev, 1, devinfo);
868 device_set_desc_copy(bdev, devinfo);
869 if (device_probe_and_attach(bdev) == 0) {
870 free(devinfo, M_USB);
871 return (USBD_NORMAL_COMPLETION);
875 * Free subdevs so we can reallocate it larger for the number of
878 tmpdv = dev->subdevs;
881 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
883 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
884 dd->bNumConfigurations));
885 /* Next try with interface drivers. */
886 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
887 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
889 err = usbd_set_config_index(dev, confi, 1);
892 DPRINTF(("%s: port %d, set config at addr %d failed, "
893 "error=%s\n", device_get_nameunit(parent), port,
894 addr, usbd_errstr(err)));
896 printf("%s: port %d, set config at addr %d failed\n",
897 device_get_nameunit(parent), port, addr);
899 free(devinfo, M_USB);
902 nifaces = dev->cdesc->bNumInterface;
903 uaa.configno = dev->cdesc->bConfigurationValue;
904 for (i = 0; i < nifaces; i++)
905 ifaces[i] = &dev->ifaces[i];
907 uaa.nifaces = nifaces;
908 dev->subdevs = malloc((nifaces+1) * sizeof(device_t), M_USB,M_NOWAIT);
909 if (dev->subdevs == NULL) {
910 free(devinfo, M_USB);
913 dev->ifacenums = malloc((nifaces) * sizeof(*dev->ifacenums),
915 if (dev->ifacenums == NULL) {
916 free(devinfo, M_USB);
921 for (i = 0; i < nifaces; i++) {
922 if (ifaces[i] == NULL)
923 continue; /* interface already claimed */
924 uaa.iface = ifaces[i];
925 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
926 dev->subdevs[found] = bdev;
927 dev->subdevs[found + 1] = 0;
928 dev->ifacenums[found] = i;
930 usbd_devinfo(dev, 1, devinfo);
931 device_set_desc_copy(bdev, devinfo);
932 if (device_probe_and_attach(bdev) == 0) {
933 ifaces[i] = 0; /* consumed */
935 /* create another child for the next iface */
936 bdev = device_add_child(parent, NULL, -1);
938 device_printf(parent,
939 "Device add failed\n");
940 free(devinfo, M_USB);
941 return (USBD_NORMAL_COMPLETION);
943 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
945 free(devinfo, M_USB);
948 device_set_ivars(bdev, uaap);
950 dev->subdevs[found] = 0;
954 /* remove the last created child. It is unused */
956 free(devinfo, M_USB);
957 device_delete_child(parent, bdev);
958 /* free(uaap, M_USB); */ /* May be needed? xxx */
959 return (USBD_NORMAL_COMPLETION);
961 tmpdv = dev->subdevs;
964 free(dev->ifacenums, M_USB);
965 dev->ifacenums = NULL;
967 /* No interfaces were attached in any of the configurations. */
969 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
970 usbd_set_config_index(dev, 0, 0);
972 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
974 /* Finally try the generic driver. */
977 uaa.configno = UHUB_UNK_CONFIGURATION;
978 uaa.ifaceno = UHUB_UNK_INTERFACE;
979 dev->subdevs = malloc(2 * sizeof(device_t), M_USB, M_NOWAIT);
980 if (dev->subdevs == 0) {
981 free(devinfo, M_USB);
984 dev->subdevs[0] = bdev;
987 usbd_devinfo(dev, 1, devinfo);
988 device_set_desc_copy(bdev, devinfo);
989 free(devinfo, M_USB);
990 if (device_probe_and_attach(bdev) == 0)
991 return (USBD_NORMAL_COMPLETION);
994 * The generic attach failed, but leave the device as it is.
995 * We just did not find any drivers, that's all. The device is
996 * fully operational and not harming anyone.
998 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
999 return (USBD_NORMAL_COMPLETION);
1004 * Called when a new device has been put in the powered state,
1005 * but not yet in the addressed state.
1006 * Get initial descriptor, set the address, get full descriptor,
1007 * and attach a driver.
1010 usbd_new_device(device_t parent, usbd_bus_handle bus, int depth,
1011 int speed, int port, struct usbd_port *up)
1013 usbd_device_handle dev, adev;
1014 struct usbd_device *hub;
1015 usb_device_descriptor_t *dd;
1016 usb_port_status_t ps;
1022 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
1023 bus, port, depth, speed));
1024 addr = usbd_getnewaddr(bus);
1026 printf("%s: No free USB addresses, new device ignored.\n",
1027 device_get_nameunit(bus->bdev));
1028 return (USBD_NO_ADDR);
1031 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1033 return (USBD_NOMEM);
1037 /* Set up default endpoint handle. */
1038 dev->def_ep.edesc = &dev->def_ep_desc;
1040 /* Set up default endpoint descriptor. */
1041 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1042 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1043 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1044 dev->def_ep_desc.bmAttributes = UE_CONTROL;
1045 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1046 dev->def_ep_desc.bInterval = 0;
1048 dev->quirks = &usbd_no_quirk;
1049 dev->address = USB_START_ADDR;
1050 dev->ddesc.bMaxPacketSize = 0;
1053 dev->myhub = up->parent;
1057 if (up->parent && speed > up->parent->speed) {
1059 printf("%s: maxium speed of attached "
1060 "device, %d, is higher than speed "
1061 "of parent HUB, %d.\n",
1062 __FUNCTION__, speed, up->parent->speed);
1065 * Reduce the speed, otherwise we won't setup the
1066 * proper transfer methods.
1068 speed = up->parent->speed;
1071 /* Locate port on upstream high speed hub */
1072 for (adev = dev, hub = up->parent;
1073 hub != NULL && hub->speed != USB_SPEED_HIGH;
1074 adev = hub, hub = hub->myhub)
1077 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1078 if (hub->hub->ports[p].device == adev) {
1079 dev->myhsport = &hub->hub->ports[p];
1083 panic("usbd_new_device: cannot find HS port\n");
1085 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1087 dev->myhsport = NULL;
1090 dev->langid = USBD_NOLANG;
1091 dev->cookie.cookie = ++usb_cookie_no;
1093 /* Establish the default pipe. */
1094 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1095 &dev->default_pipe);
1097 usbd_remove_device(dev, up);
1102 /* Try a few times in case the device is slow (i.e. outside specs.) */
1103 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1104 for (i = 0; i < 15; i++) {
1105 /* Get the first 8 bytes of the device descriptor. */
1106 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1109 usbd_delay_ms(dev, 200);
1111 DPRINTFN(-1,("usb_new_device: set address %d "
1112 "failed - trying a port reset\n", addr));
1113 usbd_reset_port(up->parent, port, &ps);
1118 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1120 usbd_remove_device(dev, up);
1124 if (speed == USB_SPEED_HIGH) {
1125 /* Max packet size must be 64 (sec 5.5.3). */
1126 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1128 printf("usbd_new_device: addr=%d bad max packet size\n",
1131 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1135 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1136 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1137 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1138 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1141 if (dd->bDescriptorType != UDESC_DEVICE) {
1142 /* Illegal device descriptor */
1143 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1144 dd->bDescriptorType));
1145 usbd_remove_device(dev, up);
1146 return (USBD_INVAL);
1149 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1150 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1151 usbd_remove_device(dev, up);
1152 return (USBD_INVAL);
1155 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1157 /* Re-establish the default pipe with the new max packet size. */
1158 usbd_kill_pipe(dev->default_pipe);
1159 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1160 &dev->default_pipe);
1162 usbd_remove_device(dev, up);
1166 err = usbd_reload_device_desc(dev);
1168 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1170 usbd_remove_device(dev, up);
1174 /* Set the address */
1175 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1176 err = usbd_set_address(dev, addr);
1178 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1179 err = USBD_SET_ADDR_FAILED;
1180 usbd_remove_device(dev, up);
1184 /* Allow device time to set new address */
1185 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1186 dev->address = addr; /* New device address now */
1187 bus->devices[addr] = dev;
1189 /* Re-establish the default pipe with the new address. */
1190 usbd_kill_pipe(dev->default_pipe);
1191 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1192 &dev->default_pipe);
1194 usbd_remove_device(dev, up);
1198 /* Assume 100mA bus powered for now. Changed when configured. */
1199 dev->power = USB_MIN_POWER;
1200 dev->self_powered = 0;
1202 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1203 addr, dev, parent));
1205 err = usbd_probe_and_attach(parent, dev, port, addr);
1207 usbd_remove_device(dev, up);
1211 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1213 return (USBD_NORMAL_COMPLETION);
1217 usbd_reload_device_desc(usbd_device_handle dev)
1222 /* Get the full device descriptor. */
1223 for (i = 0; i < 3; ++i) {
1224 err = usbd_get_device_desc(dev, &dev->ddesc);
1227 usbd_delay_ms(dev, 200);
1232 /* Figure out what's wrong with this device. */
1233 dev->quirks = usbd_find_quirk(&dev->ddesc);
1235 return (USBD_NORMAL_COMPLETION);
1239 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1241 DPRINTF(("usbd_remove_device: %p\n", dev));
1243 if (dev->default_pipe != NULL)
1244 usbd_kill_pipe(dev->default_pipe);
1246 dev->bus->devices[dev->address] = NULL;
1252 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1255 struct usbd_port *p;
1258 di->udi_bus = device_get_unit(dev->bus->bdev);
1259 di->udi_addr = dev->address;
1260 di->udi_cookie = dev->cookie;
1261 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
1262 usbd_printBCD(di->udi_release, UGETW(dev->ddesc.bcdDevice));
1263 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1264 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1265 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1266 di->udi_class = dev->ddesc.bDeviceClass;
1267 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1268 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1269 di->udi_config = dev->config;
1270 di->udi_power = dev->self_powered ? 0 : dev->power;
1271 di->udi_speed = dev->speed;
1273 if (dev->subdevs != NULL) {
1274 for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) {
1275 if (device_is_attached(dev->subdevs[i]))
1276 strlcpy(di->udi_devnames[i],
1277 device_get_nameunit(dev->subdevs[i]),
1278 USB_MAX_DEVNAMELEN);
1280 di->udi_devnames[i][0] = 0;
1285 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1286 di->udi_devnames[i][0] = 0; /* empty */
1290 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1291 i < dev->hub->hubdesc.bNbrPorts;
1293 p = &dev->hub->ports[i];
1295 err = p->device->address;
1297 s = UGETW(p->status.wPortStatus);
1298 if (s & UPS_PORT_ENABLED)
1299 err = USB_PORT_ENABLED;
1300 else if (s & UPS_SUSPEND)
1301 err = USB_PORT_SUSPENDED;
1302 else if (s & UPS_PORT_POWER)
1303 err = USB_PORT_POWERED;
1305 err = USB_PORT_DISABLED;
1307 di->udi_ports[i] = err;
1309 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1315 usb_free_device(usbd_device_handle dev)
1319 if (dev->default_pipe != NULL)
1320 usbd_kill_pipe(dev->default_pipe);
1321 if (dev->ifaces != NULL) {
1322 nifc = dev->cdesc->bNumInterface;
1323 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1324 usbd_free_iface_data(dev, ifcidx);
1325 free(dev->ifaces, M_USB);
1327 if (dev->cdesc != NULL)
1328 free(dev->cdesc, M_USB);
1329 if (dev->subdevs != NULL)
1330 free(dev->subdevs, M_USB);
1331 if (dev->ifacenums != NULL)
1332 free(dev->ifacenums, M_USB);
1337 * The general mechanism for detaching drivers works as follows: Each
1338 * driver is responsible for maintaining a reference count on the
1339 * number of outstanding references to its softc (e.g. from
1340 * processing hanging in a read or write). The detach method of the
1341 * driver decrements this counter and flags in the softc that the
1342 * driver is dying and then wakes any sleepers. It then sleeps on the
1343 * softc. Each place that can sleep must maintain the reference
1344 * count. When the reference count drops to -1 (0 is the normal value
1345 * of the reference count) the a wakeup on the softc is performed
1346 * signaling to the detach waiter that all references are gone.
1350 * Called from process context when we discover that a port has
1351 * been disconnected.
1354 usb_disconnect_port(struct usbd_port *up, device_t parent)
1356 usbd_device_handle dev = up->device;
1357 const char *hubname = device_get_nameunit(parent);
1360 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1361 up, dev, up->portno));
1365 printf("usb_disconnect_port: no device\n");
1370 if (dev->subdevs != NULL) {
1371 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1372 for (i = 0; dev->subdevs[i]; i++) {
1373 printf("%s: at %s", device_get_nameunit(dev->subdevs[i]),
1375 if (up->portno != 0)
1376 printf(" port %d", up->portno);
1377 printf(" (addr %d) disconnected\n", dev->address);
1378 struct usb_attach_arg *uaap =
1379 device_get_ivars(dev->subdevs[i]);
1380 device_detach(dev->subdevs[i]);
1382 device_delete_child(device_get_parent(dev->subdevs[i]),
1384 dev->subdevs[i] = NULL;
1388 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1389 dev->bus->devices[dev->address] = NULL;
1391 usb_free_device(dev);