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 #if defined(__NetBSD__) || defined(__OpenBSD__)
60 #include <sys/device.h>
61 #include <sys/select.h>
62 #elif defined(__FreeBSD__)
63 #include <sys/module.h>
68 #include <machine/bus.h>
70 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
74 #include <dev/usb/usbdivar.h>
76 #include <dev/usb/usb_quirks.h>
78 #if defined(__FreeBSD__)
79 #include <machine/clock.h>
80 #define delay(d) DELAY(d)
84 #define DPRINTF(x) if (usbdebug) logprintf x
85 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
92 Static usbd_status usbd_set_config(usbd_device_handle, int);
93 Static void usbd_devinfo_vp(usbd_device_handle, char *, char *, int);
94 Static int usbd_getnewaddr(usbd_bus_handle bus);
95 #if defined(__NetBSD__)
96 Static int usbd_print(void *aux, const char *pnp);
97 Static int usbd_submatch(device_ptr_t, struct cfdata *cf, void *);
98 #elif defined(__OpenBSD__)
99 Static int usbd_print(void *aux, const char *pnp);
100 Static int usbd_submatch(device_ptr_t, void *, void *);
102 Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
103 Static void usbd_kill_pipe(usbd_pipe_handle);
104 Static usbd_status usbd_probe_and_attach(device_ptr_t parent,
105 usbd_device_handle dev, int port, int addr);
107 Static u_int32_t usb_cookie_no = 0;
110 typedef u_int16_t usb_vendor_id_t;
111 typedef u_int16_t usb_product_id_t;
114 * Descriptions of of known vendors and devices ("products").
116 struct usb_knowndev {
117 usb_vendor_id_t vendor;
118 usb_product_id_t product;
120 char *vendorname, *productname;
122 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
124 #include "usbdevs_data.h"
125 #endif /* USBVERBOSE */
127 Static const char * const usbd_error_strs[] = {
151 usbd_errstr(usbd_status err)
153 static char buffer[5];
155 if (err < USBD_ERROR_MAX) {
156 return usbd_error_strs[err];
158 snprintf(buffer, sizeof buffer, "%d", err);
164 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
165 usb_string_descriptor_t *sdesc, int *sizep)
167 usb_device_request_t req;
171 req.bmRequestType = UT_READ_DEVICE;
172 req.bRequest = UR_GET_DESCRIPTOR;
173 USETW2(req.wValue, UDESC_STRING, sindex);
174 USETW(req.wIndex, langid);
175 USETW(req.wLength, 2); /* only size byte first */
176 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
177 &actlen, USBD_DEFAULT_TIMEOUT);
182 return (USBD_SHORT_XFER);
184 USETW(req.wLength, sdesc->bLength); /* the whole string */
185 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
186 &actlen, USBD_DEFAULT_TIMEOUT);
190 if (actlen != sdesc->bLength) {
191 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
192 sdesc->bLength, actlen));
196 return (USBD_NORMAL_COMPLETION);
200 usbd_trim_spaces(char *p)
207 while (*q == ' ') /* skip leading spaces */
209 while ((*p = *q++)) /* copy string */
210 if (*p++ != ' ') /* remember last non-space */
212 *e = 0; /* kill trailing spaces */
216 usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev)
218 usb_device_descriptor_t *udd = &dev->ddesc;
219 char *vendor = 0, *product = 0;
221 const struct usb_knowndev *kdp;
230 if (usbd_get_string(dev, udd->iManufacturer, v))
234 usbd_trim_spaces(vendor);
235 if (usbd_get_string(dev, udd->iProduct, p))
239 usbd_trim_spaces(product);
240 if (vendor && !*vendor)
242 if (product && !*product)
249 if (vendor == NULL || product == NULL) {
250 for(kdp = usb_knowndevs;
251 kdp->vendorname != NULL;
253 if (kdp->vendor == UGETW(udd->idVendor) &&
254 (kdp->product == UGETW(udd->idProduct) ||
255 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
258 if (kdp->vendorname != NULL) {
260 vendor = kdp->vendorname;
262 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
263 kdp->productname : NULL;
267 if (vendor != NULL && *vendor)
270 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
271 if (product != NULL && *product)
274 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
278 usbd_printBCD(char *cp, int bcd)
280 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
284 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp)
286 usb_device_descriptor_t *udd = &dev->ddesc;
287 usbd_interface_handle iface;
288 char vendor[USB_MAX_STRING_LEN];
289 char product[USB_MAX_STRING_LEN];
290 int bcdDevice, bcdUSB;
291 usb_interface_descriptor_t *id;
293 usbd_devinfo_vp(dev, vendor, product, 1);
294 cp += sprintf(cp, "%s %s", vendor, product);
295 if (showclass & USBD_SHOW_DEVICE_CLASS)
296 cp += sprintf(cp, ", class %d/%d",
297 udd->bDeviceClass, udd->bDeviceSubClass);
298 bcdUSB = UGETW(udd->bcdUSB);
299 bcdDevice = UGETW(udd->bcdDevice);
300 cp += sprintf(cp, ", rev ");
301 cp += usbd_printBCD(cp, bcdUSB);
303 cp += usbd_printBCD(cp, bcdDevice);
304 cp += sprintf(cp, ", addr %d", dev->address);
305 if (showclass & USBD_SHOW_INTERFACE_CLASS)
307 /* fetch the interface handle for the first interface */
308 (void)usbd_device2interface_handle(dev, 0, &iface);
309 id = usbd_get_interface_descriptor(iface);
310 cp += sprintf(cp, ", iclass %d/%d",
311 id->bInterfaceClass, id->bInterfaceSubClass);
316 /* Delay for a certain number of ms */
318 usb_delay_ms(usbd_bus_handle bus, u_int ms)
320 /* Wait at least two clock ticks so we know the time has passed. */
321 if (bus->use_polling || cold)
322 delay((ms+1) * 1000);
324 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
327 /* Delay given a device handle. */
329 usbd_delay_ms(usbd_device_handle dev, u_int ms)
331 usb_delay_ms(dev->bus, ms);
335 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
337 usb_device_request_t req;
341 req.bmRequestType = UT_WRITE_CLASS_OTHER;
342 req.bRequest = UR_SET_FEATURE;
343 USETW(req.wValue, UHF_PORT_RESET);
344 USETW(req.wIndex, port);
345 USETW(req.wLength, 0);
346 err = usbd_do_request(dev, &req, 0);
347 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
348 port, usbd_errstr(err)));
353 /* Wait for device to recover from reset. */
354 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
355 err = usbd_get_port_status(dev, port, ps);
357 DPRINTF(("usbd_reset_port: get status failed %d\n",
361 /* If the device disappeared, just give up. */
362 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
363 return (USBD_NORMAL_COMPLETION);
364 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
366 return (USBD_TIMEOUT);
367 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
370 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
374 /* Wait for the device to recover from reset. */
375 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
379 usb_interface_descriptor_t *
380 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
382 char *p = (char *)cd;
383 char *end = p + UGETW(cd->wTotalLength);
384 usb_interface_descriptor_t *d;
385 int curidx, lastidx, curaidx = 0;
387 for (curidx = lastidx = -1; p < end; ) {
388 d = (usb_interface_descriptor_t *)p;
389 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
391 ifaceidx, curidx, altidx, curaidx,
392 d->bLength, d->bDescriptorType));
393 if (d->bLength == 0) /* bad descriptor */
396 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
397 if (d->bInterfaceNumber != lastidx) {
398 lastidx = d->bInterfaceNumber;
403 if (ifaceidx == curidx && altidx == curaidx)
410 usb_endpoint_descriptor_t *
411 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
414 char *p = (char *)cd;
415 char *end = p + UGETW(cd->wTotalLength);
416 usb_interface_descriptor_t *d;
417 usb_endpoint_descriptor_t *e;
420 d = usbd_find_idesc(cd, ifaceidx, altidx);
423 if (endptidx >= d->bNumEndpoints) /* quick exit */
427 for (p = (char *)d + d->bLength; p < end; ) {
428 e = (usb_endpoint_descriptor_t *)p;
429 if (e->bLength == 0) /* bad descriptor */
432 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
434 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
436 if (curidx == endptidx)
444 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
446 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
447 usb_interface_descriptor_t *idesc;
451 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
453 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
458 ifc->index = ifaceidx;
459 ifc->altindex = altidx;
460 nendpt = ifc->idesc->bNumEndpoints;
461 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
463 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
465 if (ifc->endpoints == NULL)
468 ifc->endpoints = NULL;
470 p = (char *)ifc->idesc + ifc->idesc->bLength;
471 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
472 #define ed ((usb_endpoint_descriptor_t *)p)
473 for (endpt = 0; endpt < nendpt; endpt++) {
474 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
475 for (; p < end; p += ed->bLength) {
476 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
478 p, end, ed->bLength, ed->bDescriptorType));
479 if (p + ed->bLength <= end && ed->bLength != 0 &&
480 ed->bDescriptorType == UDESC_ENDPOINT)
482 if (ed->bLength == 0 ||
483 ed->bDescriptorType == UDESC_INTERFACE)
486 /* passed end, or bad desc */
487 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
488 ed->bLength == 0 ? "0 length" :
489 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
493 ifc->endpoints[endpt].edesc = ed;
494 if (dev->speed == USB_SPEED_HIGH) {
496 /* Control and bulk endpoints have max packet limits. */
497 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
499 mps = USB_2_MAX_CTRL_PACKET;
502 mps = USB_2_MAX_BULK_PACKET;
504 if (UGETW(ed->wMaxPacketSize) != mps) {
505 USETW(ed->wMaxPacketSize, mps);
507 printf("usbd_fill_iface_data: bad max "
516 ifc->endpoints[endpt].refcnt = 0;
520 LIST_INIT(&ifc->pipes);
521 return (USBD_NORMAL_COMPLETION);
524 if (ifc->endpoints != NULL) {
525 free(ifc->endpoints, M_USB);
526 ifc->endpoints = NULL;
532 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
534 usbd_interface_handle ifc = &dev->ifaces[ifcno];
536 free(ifc->endpoints, M_USB);
540 usbd_set_config(usbd_device_handle dev, int conf)
542 usb_device_request_t req;
544 req.bmRequestType = UT_WRITE_DEVICE;
545 req.bRequest = UR_SET_CONFIG;
546 USETW(req.wValue, conf);
547 USETW(req.wIndex, 0);
548 USETW(req.wLength, 0);
549 return (usbd_do_request(dev, &req, 0));
553 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
556 usb_config_descriptor_t cd;
559 if (no == USB_UNCONFIG_NO)
560 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
562 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
563 /* Figure out what config index to use. */
564 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
565 err = usbd_get_config_desc(dev, index, &cd);
568 if (cd.bConfigurationValue == no)
569 return (usbd_set_config_index(dev, index, msg));
575 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
578 usb_config_descriptor_t cd, *cdp;
580 int i, ifcidx, nifc, len, selfpowered, power;
582 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
584 if (dev->config != USB_UNCONFIG_NO) {
585 nifc = dev->cdesc->bNumInterface;
587 /* Check that all interfaces are idle */
588 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
589 if (LIST_EMPTY(&dev->ifaces[ifcidx].pipes))
591 DPRINTF(("usbd_set_config_index: open pipes exist\n"));
592 return (USBD_IN_USE);
595 DPRINTF(("usbd_set_config_index: free old config\n"));
596 /* Free all configuration data structures. */
597 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
598 usbd_free_iface_data(dev, ifcidx);
599 free(dev->ifaces, M_USB);
600 free(dev->cdesc, M_USB);
603 dev->config = USB_UNCONFIG_NO;
606 if (index == USB_UNCONFIG_INDEX) {
607 /* We are unconfiguring the device, so leave unallocated. */
608 DPRINTF(("usbd_set_config_index: set config 0\n"));
609 err = usbd_set_config(dev, USB_UNCONFIG_NO);
611 DPRINTF(("usbd_set_config_index: setting config=0 "
612 "failed, error=%s\n", usbd_errstr(err)));
616 /* Get the short descriptor. */
617 err = usbd_get_config_desc(dev, index, &cd);
620 len = UGETW(cd.wTotalLength);
621 cdp = malloc(len, M_USB, M_NOWAIT);
625 /* Get the full descriptor. Try a few times for slow devices. */
626 for (i = 0; i < 3; i++) {
627 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
630 usbd_delay_ms(dev, 200);
634 if (cdp->bDescriptorType != UDESC_CONFIG) {
635 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
636 cdp->bDescriptorType));
641 /* Figure out if the device is self or bus powered. */
643 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
644 (cdp->bmAttributes & UC_SELF_POWERED)) {
645 /* May be self powered. */
646 if (cdp->bmAttributes & UC_BUS_POWERED) {
647 /* Must ask device. */
648 if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
650 * Hub claims to be self powered, but isn't.
651 * It seems that the power status can be
652 * determined by the hub characteristics.
654 usb_hub_descriptor_t hd;
655 usb_device_request_t req;
656 req.bmRequestType = UT_READ_CLASS_DEVICE;
657 req.bRequest = UR_GET_DESCRIPTOR;
658 USETW(req.wValue, 0);
659 USETW(req.wIndex, 0);
660 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
661 err = usbd_do_request(dev, &req, &hd);
663 (UGETW(hd.wHubCharacteristics) &
666 DPRINTF(("usbd_set_config_index: charac=0x%04x"
668 UGETW(hd.wHubCharacteristics),
671 err = usbd_get_device_status(dev, &ds);
673 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
675 DPRINTF(("usbd_set_config_index: status=0x%04x"
677 UGETW(ds.wStatus), usbd_errstr(err)));
682 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
683 "selfpowered=%d, power=%d\n",
684 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
685 selfpowered, cdp->bMaxPower * 2));
687 /* Check if we have enough power. */
689 if (dev->powersrc == NULL) {
690 DPRINTF(("usbd_set_config_index: No power source?\n"));
691 return (USBD_IOERROR);
694 power = cdp->bMaxPower * 2;
695 if (power > dev->powersrc->power) {
696 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
697 /* XXX print nicer message. */
699 printf("%s: device addr %d (config %d) exceeds power "
700 "budget, %d mA > %d mA\n",
701 USBDEVNAME(dev->bus->bdev), dev->address,
702 cdp->bConfigurationValue,
703 power, dev->powersrc->power);
708 dev->self_powered = selfpowered;
710 /* Set the actual configuration value. */
711 DPRINTF(("usbd_set_config_index: set config %d\n",
712 cdp->bConfigurationValue));
713 err = usbd_set_config(dev, cdp->bConfigurationValue);
715 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
717 cdp->bConfigurationValue, usbd_errstr(err)));
721 /* Allocate and fill interface data. */
722 nifc = cdp->bNumInterface;
723 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
725 if (dev->ifaces == NULL) {
729 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
731 dev->config = cdp->bConfigurationValue;
732 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
733 err = usbd_fill_iface_data(dev, ifcidx, 0);
735 while (--ifcidx >= 0)
736 usbd_free_iface_data(dev, ifcidx);
741 return (USBD_NORMAL_COMPLETION);
748 /* XXX add function for alternate settings */
751 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
752 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
757 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
758 dev, iface, ep, pipe));
759 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
772 SIMPLEQ_INIT(&p->queue);
773 err = dev->bus->methods->open_pipe(p);
775 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
777 ep->edesc->bEndpointAddress, usbd_errstr(err)));
781 /* Clear any stall and make sure DATA0 toggle will be used next. */
782 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT) {
783 err = usbd_clear_endpoint_stall(p);
785 * Some devices reject this command, so ignore a STALL.
786 * Some device just time out on this command, so ignore
789 if (err && err != USBD_STALLED && err != USBD_TIMEOUT) {
790 printf("usbd_setup_pipe: failed to start "
791 "endpoint, %s\n", usbd_errstr(err));
796 return (USBD_NORMAL_COMPLETION);
799 /* Abort the device control pipe. */
801 usbd_kill_pipe(usbd_pipe_handle pipe)
803 usbd_abort_pipe(pipe);
804 pipe->methods->close(pipe);
805 pipe->endpoint->refcnt--;
810 usbd_getnewaddr(usbd_bus_handle bus)
814 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
815 if (bus->devices[addr] == 0)
822 usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
825 struct usb_attach_arg uaa;
826 usb_device_descriptor_t *dd = &dev->ddesc;
827 int found, i, confi, nifaces;
831 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
833 #if defined(__FreeBSD__)
834 /* XXX FreeBSD may leak resources on failure cases -- fixme */
836 struct usb_attach_arg *uaap;
838 bdev = device_add_child(parent, NULL, -1);
840 device_printf(parent, "Device creation failed\n");
844 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
848 device_set_ivars(bdev, uaap);
856 uaa.configno = UHUB_UNK_CONFIGURATION;
857 uaa.ifaceno = UHUB_UNK_INTERFACE;
858 uaa.vendor = UGETW(dd->idVendor);
859 uaa.product = UGETW(dd->idProduct);
860 uaa.release = UGETW(dd->bcdDevice);
862 /* First try with device specific drivers. */
863 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
865 dev->ifacenums = NULL;
866 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
867 if (dev->subdevs == NULL)
869 dev->subdevs[0] = bdev;
872 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
874 return (USBD_NORMAL_COMPLETION);
877 * Free subdevs so we can reallocate it larger for the number of
880 tmpdv = dev->subdevs;
883 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
885 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
886 dd->bNumConfigurations));
887 /* Next try with interface drivers. */
888 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
889 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
891 err = usbd_set_config_index(dev, confi, 1);
894 DPRINTF(("%s: port %d, set config at addr %d failed, "
895 "error=%s\n", USBDEVPTRNAME(parent), port,
896 addr, usbd_errstr(err)));
898 printf("%s: port %d, set config at addr %d failed\n",
899 USBDEVPTRNAME(parent), port, addr);
903 nifaces = dev->cdesc->bNumInterface;
904 uaa.configno = dev->cdesc->bConfigurationValue;
905 for (i = 0; i < nifaces; i++)
906 ifaces[i] = &dev->ifaces[i];
908 uaa.nifaces = nifaces;
909 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
910 if (dev->subdevs == NULL) {
913 dev->ifacenums = malloc((nifaces) * sizeof(*dev->ifacenums),
915 if (dev->ifacenums == NULL) {
920 for (i = 0; i < nifaces; i++) {
921 if (ifaces[i] == NULL)
922 continue; /* interface already claimed */
923 uaa.iface = ifaces[i];
924 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
925 dev->subdevs[found] = bdev;
926 dev->subdevs[found + 1] = 0;
927 dev->ifacenums[found] = i;
929 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
932 ifaces[i] = 0; /* consumed */
935 #if defined(__FreeBSD__)
936 /* create another child for the next iface */
937 bdev = device_add_child(parent, NULL, -1);
939 device_printf(parent,
940 "Device add failed\n");
941 return (USBD_NORMAL_COMPLETION);
943 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
947 device_set_ivars(bdev, uaap);
951 dev->subdevs[found] = 0;
955 #if defined(__FreeBSD__)
956 /* remove the last created child. It is unused */
957 device_delete_child(parent, bdev);
958 /* free(uaap, M_USB); */ /* May be needed? xxx */
960 return (USBD_NORMAL_COMPLETION);
962 tmpdv = dev->subdevs;
965 free(dev->ifacenums, M_USB);
966 dev->ifacenums = NULL;
968 /* No interfaces were attached in any of the configurations. */
970 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
971 usbd_set_config_index(dev, 0, 0);
973 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
975 /* Finally try the generic driver. */
978 uaa.configno = UHUB_UNK_CONFIGURATION;
979 uaa.ifaceno = UHUB_UNK_INTERFACE;
980 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
981 if (dev->subdevs == 0) {
984 dev->subdevs[0] = bdev;
987 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
989 return (USBD_NORMAL_COMPLETION);
993 * The generic attach failed, but leave the device as it is.
994 * We just did not find any drivers, that's all. The device is
995 * fully operational and not harming anyone.
997 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
998 return (USBD_NORMAL_COMPLETION);
1003 * Called when a new device has been put in the powered state,
1004 * but not yet in the addressed state.
1005 * Get initial descriptor, set the address, get full descriptor,
1006 * and attach a driver.
1009 usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
1010 int speed, int port, struct usbd_port *up)
1012 usbd_device_handle dev, adev;
1013 struct usbd_device *hub;
1014 usb_device_descriptor_t *dd;
1015 usb_port_status_t ps;
1021 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
1022 bus, port, depth, speed));
1023 addr = usbd_getnewaddr(bus);
1025 printf("%s: No free USB addresses, new device ignored.\n",
1026 USBDEVNAME(bus->bdev));
1027 return (USBD_NO_ADDR);
1030 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1032 return (USBD_NOMEM);
1036 /* Set up default endpoint handle. */
1037 dev->def_ep.edesc = &dev->def_ep_desc;
1039 /* Set up default endpoint descriptor. */
1040 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1041 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1042 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1043 dev->def_ep_desc.bmAttributes = UE_CONTROL;
1044 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1045 dev->def_ep_desc.bInterval = 0;
1047 dev->quirks = &usbd_no_quirk;
1048 dev->address = USB_START_ADDR;
1049 dev->ddesc.bMaxPacketSize = 0;
1052 dev->myhub = up->parent;
1056 /* Locate port on upstream high speed hub */
1057 for (adev = dev, hub = up->parent;
1058 hub != NULL && hub->speed != USB_SPEED_HIGH;
1059 adev = hub, hub = hub->myhub)
1062 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1063 if (hub->hub->ports[p].device == adev) {
1064 dev->myhsport = &hub->hub->ports[p];
1068 panic("usbd_new_device: cannot find HS port\n");
1070 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1072 dev->myhsport = NULL;
1075 dev->langid = USBD_NOLANG;
1076 dev->cookie.cookie = ++usb_cookie_no;
1078 /* Establish the default pipe. */
1079 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1080 &dev->default_pipe);
1082 usbd_remove_device(dev, up);
1086 /* Set the address. Do this early; some devices need that. */
1087 /* Try a few times in case the device is slow (i.e. outside specs.) */
1088 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1089 for (i = 0; i < 15; i++) {
1090 err = usbd_set_address(dev, addr);
1093 usbd_delay_ms(dev, 200);
1095 DPRINTFN(-1,("usb_new_device: set address %d "
1096 "failed - trying a port reset\n", addr));
1097 usbd_reset_port(up->parent, port, &ps);
1101 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1102 err = USBD_SET_ADDR_FAILED;
1103 usbd_remove_device(dev, up);
1106 /* Allow device time to set new address */
1107 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1108 dev->address = addr; /* New device address now */
1109 bus->devices[addr] = dev;
1112 /* Get the first 8 bytes of the device descriptor. */
1113 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1115 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1117 usbd_remove_device(dev, up);
1121 if (speed == USB_SPEED_HIGH) {
1122 /* Max packet size must be 64 (sec 5.5.3). */
1123 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1125 printf("usbd_new_device: addr=%d bad max packet size\n",
1128 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1132 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1133 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1134 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1135 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1138 if (dd->bDescriptorType != UDESC_DEVICE) {
1139 /* Illegal device descriptor */
1140 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1141 dd->bDescriptorType));
1142 usbd_remove_device(dev, up);
1143 return (USBD_INVAL);
1146 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1147 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1148 usbd_remove_device(dev, up);
1149 return (USBD_INVAL);
1152 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1154 err = usbd_reload_device_desc(dev);
1156 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1158 usbd_remove_device(dev, up);
1162 /* Assume 100mA bus powered for now. Changed when configured. */
1163 dev->power = USB_MIN_POWER;
1164 dev->self_powered = 0;
1166 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1167 addr, dev, parent));
1169 err = usbd_probe_and_attach(parent, dev, port, addr);
1171 usbd_remove_device(dev, up);
1175 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1177 return (USBD_NORMAL_COMPLETION);
1181 usbd_reload_device_desc(usbd_device_handle dev)
1186 /* Get the full device descriptor. */
1187 for (i = 0; i < 3; ++i) {
1188 err = usbd_get_device_desc(dev, &dev->ddesc);
1191 usbd_delay_ms(dev, 200);
1196 /* Figure out what's wrong with this device. */
1197 dev->quirks = usbd_find_quirk(&dev->ddesc);
1199 return (USBD_NORMAL_COMPLETION);
1203 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1205 DPRINTF(("usbd_remove_device: %p\n", dev));
1207 if (dev->default_pipe != NULL)
1208 usbd_kill_pipe(dev->default_pipe);
1210 dev->bus->devices[dev->address] = NULL;
1215 #if defined(__NetBSD__) || defined(__OpenBSD__)
1217 usbd_print(void *aux, const char *pnp)
1219 struct usb_attach_arg *uaa = aux;
1222 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1224 if (!uaa->usegeneric)
1226 usbd_devinfo(uaa->device, 1, devinfo);
1227 printf("%s, %s", devinfo, pnp);
1230 printf(" port %d", uaa->port);
1231 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1232 printf(" configuration %d", uaa->configno);
1233 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1234 printf(" interface %d", uaa->ifaceno);
1237 * It gets very crowded with these locators on the attach line.
1238 * They are not really needed since they are printed in the clear
1241 if (uaa->vendor != UHUB_UNK_VENDOR)
1242 printf(" vendor 0x%04x", uaa->vendor);
1243 if (uaa->product != UHUB_UNK_PRODUCT)
1244 printf(" product 0x%04x", uaa->product);
1245 if (uaa->release != UHUB_UNK_RELEASE)
1246 printf(" release 0x%04x", uaa->release);
1251 #if defined(__NetBSD__)
1253 usbd_submatch(struct device *parent, struct cfdata *cf, void *aux)
1255 #elif defined(__OpenBSD__)
1257 usbd_submatch(struct device *parent, void *match, void *aux)
1259 struct cfdata *cf = match;
1261 struct usb_attach_arg *uaa = aux;
1263 DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1264 "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1265 uaa->port, cf->uhubcf_port,
1266 uaa->configno, cf->uhubcf_configuration,
1267 uaa->ifaceno, cf->uhubcf_interface,
1268 uaa->vendor, cf->uhubcf_vendor,
1269 uaa->product, cf->uhubcf_product,
1270 uaa->release, cf->uhubcf_release));
1271 if (uaa->port != 0 && /* root hub has port 0, it should match */
1273 cf->uhubcf_port != UHUB_UNK_PORT &&
1274 cf->uhubcf_port != uaa->port) ||
1275 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1276 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1277 cf->uhubcf_configuration != uaa->configno) ||
1278 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1279 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1280 cf->uhubcf_interface != uaa->ifaceno) ||
1281 (uaa->vendor != UHUB_UNK_VENDOR &&
1282 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1283 cf->uhubcf_vendor != uaa->vendor) ||
1284 (uaa->product != UHUB_UNK_PRODUCT &&
1285 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1286 cf->uhubcf_product != uaa->product) ||
1287 (uaa->release != UHUB_UNK_RELEASE &&
1288 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1289 cf->uhubcf_release != uaa->release)
1293 if (cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1294 cf->uhubcf_vendor == uaa->vendor &&
1295 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1296 cf->uhubcf_product == uaa->product) {
1297 /* We have a vendor&product locator match */
1298 if (cf->uhubcf_release != UHUB_UNK_RELEASE &&
1299 cf->uhubcf_release == uaa->release)
1300 uaa->matchlvl = UMATCH_VENDOR_PRODUCT_REV;
1302 uaa->matchlvl = UMATCH_VENDOR_PRODUCT;
1305 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1311 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1314 struct usbd_port *p;
1317 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1318 di->udi_addr = dev->address;
1319 di->udi_cookie = dev->cookie;
1320 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
1321 usbd_printBCD(di->udi_release, UGETW(dev->ddesc.bcdDevice));
1322 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1323 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1324 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1325 di->udi_class = dev->ddesc.bDeviceClass;
1326 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1327 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1328 di->udi_config = dev->config;
1329 di->udi_power = dev->self_powered ? 0 : dev->power;
1330 di->udi_speed = dev->speed;
1332 if (dev->subdevs != NULL) {
1333 for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) {
1334 if (device_is_attached(dev->subdevs[i]))
1335 strlcpy(di->udi_devnames[i],
1336 USBDEVPTRNAME(dev->subdevs[i]),
1337 USB_MAX_DEVNAMELEN);
1339 di->udi_devnames[i][0] = 0;
1344 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1345 di->udi_devnames[i][0] = 0; /* empty */
1349 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1350 i < dev->hub->hubdesc.bNbrPorts;
1352 p = &dev->hub->ports[i];
1354 err = p->device->address;
1356 s = UGETW(p->status.wPortStatus);
1357 if (s & UPS_PORT_ENABLED)
1358 err = USB_PORT_ENABLED;
1359 else if (s & UPS_SUSPEND)
1360 err = USB_PORT_SUSPENDED;
1361 else if (s & UPS_PORT_POWER)
1362 err = USB_PORT_POWERED;
1364 err = USB_PORT_DISABLED;
1366 di->udi_ports[i] = err;
1368 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1374 usb_free_device(usbd_device_handle dev)
1378 if (dev->default_pipe != NULL)
1379 usbd_kill_pipe(dev->default_pipe);
1380 if (dev->ifaces != NULL) {
1381 nifc = dev->cdesc->bNumInterface;
1382 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1383 usbd_free_iface_data(dev, ifcidx);
1384 free(dev->ifaces, M_USB);
1386 if (dev->cdesc != NULL)
1387 free(dev->cdesc, M_USB);
1388 if (dev->subdevs != NULL)
1389 free(dev->subdevs, M_USB);
1390 if (dev->ifacenums != NULL)
1391 free(dev->ifacenums, M_USB);
1396 * The general mechanism for detaching drivers works as follows: Each
1397 * driver is responsible for maintaining a reference count on the
1398 * number of outstanding references to its softc (e.g. from
1399 * processing hanging in a read or write). The detach method of the
1400 * driver decrements this counter and flags in the softc that the
1401 * driver is dying and then wakes any sleepers. It then sleeps on the
1402 * softc. Each place that can sleep must maintain the reference
1403 * count. When the reference count drops to -1 (0 is the normal value
1404 * of the reference count) the a wakeup on the softc is performed
1405 * signaling to the detach waiter that all references are gone.
1409 * Called from process context when we discover that a port has
1410 * been disconnected.
1413 usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1415 usbd_device_handle dev = up->device;
1416 const char *hubname = USBDEVPTRNAME(parent);
1419 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1420 up, dev, up->portno));
1424 printf("usb_disconnect_port: no device\n");
1429 if (dev->subdevs != NULL) {
1430 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1431 for (i = 0; dev->subdevs[i]; i++) {
1432 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1434 if (up->portno != 0)
1435 printf(" port %d", up->portno);
1436 printf(" (addr %d) disconnected\n", dev->address);
1437 config_detach(dev->subdevs[i], DETACH_FORCE);
1438 dev->subdevs[i] = NULL;
1442 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1443 dev->bus->devices[dev->address] = NULL;
1445 usb_free_device(dev);
1449 void *usb_realloc(void *p, u_int size, int pool, int flags)
1453 q = malloc(size, pool, flags);