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>
67 #include <sys/sysctl.h>
69 #include <machine/bus.h>
71 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/usbdivar.h>
77 #include <dev/usb/usb_quirks.h>
79 #if defined(__FreeBSD__)
80 #include <machine/clock.h>
81 #define delay(d) DELAY(d)
85 #define DPRINTF(x) if (usbdebug) logprintf x
86 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
93 Static usbd_status usbd_set_config(usbd_device_handle, int);
94 Static void usbd_devinfo_vp(usbd_device_handle, char *, char *, int);
95 Static int usbd_getnewaddr(usbd_bus_handle bus);
96 #if defined(__NetBSD__)
97 Static int usbd_print(void *aux, const char *pnp);
98 Static int usbd_submatch(device_ptr_t, struct cfdata *cf, void *);
99 #elif defined(__OpenBSD__)
100 Static int usbd_print(void *aux, const char *pnp);
101 Static int usbd_submatch(device_ptr_t, void *, void *);
103 Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
104 Static void usbd_kill_pipe(usbd_pipe_handle);
105 Static usbd_status usbd_probe_and_attach(device_ptr_t parent,
106 usbd_device_handle dev, int port, int addr);
108 Static u_int32_t usb_cookie_no = 0;
111 typedef u_int16_t usb_vendor_id_t;
112 typedef u_int16_t usb_product_id_t;
115 * Descriptions of of known vendors and devices ("products").
117 struct usb_knowndev {
118 usb_vendor_id_t vendor;
119 usb_product_id_t product;
121 char *vendorname, *productname;
123 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
125 #include "usbdevs_data.h"
126 #endif /* USBVERBOSE */
128 Static const char * const usbd_error_strs[] = {
152 usbd_errstr(usbd_status err)
154 static char buffer[5];
156 if (err < USBD_ERROR_MAX) {
157 return usbd_error_strs[err];
159 snprintf(buffer, sizeof buffer, "%d", err);
165 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
166 usb_string_descriptor_t *sdesc, int *sizep)
168 usb_device_request_t req;
172 req.bmRequestType = UT_READ_DEVICE;
173 req.bRequest = UR_GET_DESCRIPTOR;
174 USETW2(req.wValue, UDESC_STRING, sindex);
175 USETW(req.wIndex, langid);
176 USETW(req.wLength, 2); /* only size byte first */
177 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
178 &actlen, USBD_DEFAULT_TIMEOUT);
183 return (USBD_SHORT_XFER);
185 USETW(req.wLength, sdesc->bLength); /* the whole string */
186 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
187 &actlen, USBD_DEFAULT_TIMEOUT);
191 if (actlen != sdesc->bLength) {
192 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
193 sdesc->bLength, actlen));
197 return (USBD_NORMAL_COMPLETION);
201 usbd_trim_spaces(char *p)
208 while (*q == ' ') /* skip leading spaces */
210 while ((*p = *q++)) /* copy string */
211 if (*p++ != ' ') /* remember last non-space */
213 *e = 0; /* kill trailing spaces */
217 usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev)
219 usb_device_descriptor_t *udd = &dev->ddesc;
220 char *vendor = 0, *product = 0;
222 const struct usb_knowndev *kdp;
231 if (usbd_get_string(dev, udd->iManufacturer, v))
235 usbd_trim_spaces(vendor);
236 if (usbd_get_string(dev, udd->iProduct, p))
240 usbd_trim_spaces(product);
241 if (vendor && !*vendor)
243 if (product && !*product)
250 if (vendor == NULL || product == NULL) {
251 for(kdp = usb_knowndevs;
252 kdp->vendorname != NULL;
254 if (kdp->vendor == UGETW(udd->idVendor) &&
255 (kdp->product == UGETW(udd->idProduct) ||
256 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
259 if (kdp->vendorname != NULL) {
261 vendor = kdp->vendorname;
263 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
264 kdp->productname : NULL;
268 if (vendor != NULL && *vendor)
271 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
272 if (product != NULL && *product)
275 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
279 usbd_printBCD(char *cp, int bcd)
281 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
285 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp)
287 usb_device_descriptor_t *udd = &dev->ddesc;
288 usbd_interface_handle iface;
289 char vendor[USB_MAX_STRING_LEN];
290 char product[USB_MAX_STRING_LEN];
291 int bcdDevice, bcdUSB;
292 usb_interface_descriptor_t *id;
294 usbd_devinfo_vp(dev, vendor, product, 1);
295 cp += sprintf(cp, "%s %s", vendor, product);
296 if (showclass & USBD_SHOW_DEVICE_CLASS)
297 cp += sprintf(cp, ", class %d/%d",
298 udd->bDeviceClass, udd->bDeviceSubClass);
299 bcdUSB = UGETW(udd->bcdUSB);
300 bcdDevice = UGETW(udd->bcdDevice);
301 cp += sprintf(cp, ", rev ");
302 cp += usbd_printBCD(cp, bcdUSB);
304 cp += usbd_printBCD(cp, bcdDevice);
305 cp += sprintf(cp, ", addr %d", dev->address);
306 if (showclass & USBD_SHOW_INTERFACE_CLASS)
308 /* fetch the interface handle for the first interface */
309 (void)usbd_device2interface_handle(dev, 0, &iface);
310 id = usbd_get_interface_descriptor(iface);
311 cp += sprintf(cp, ", iclass %d/%d",
312 id->bInterfaceClass, id->bInterfaceSubClass);
317 /* Delay for a certain number of ms */
319 usb_delay_ms(usbd_bus_handle bus, u_int ms)
321 /* Wait at least two clock ticks so we know the time has passed. */
322 if (bus->use_polling || cold)
323 delay((ms+1) * 1000);
325 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
328 /* Delay given a device handle. */
330 usbd_delay_ms(usbd_device_handle dev, u_int ms)
332 usb_delay_ms(dev->bus, ms);
336 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
338 usb_device_request_t req;
342 req.bmRequestType = UT_WRITE_CLASS_OTHER;
343 req.bRequest = UR_SET_FEATURE;
344 USETW(req.wValue, UHF_PORT_RESET);
345 USETW(req.wIndex, port);
346 USETW(req.wLength, 0);
347 err = usbd_do_request(dev, &req, 0);
348 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
349 port, usbd_errstr(err)));
354 /* Wait for device to recover from reset. */
355 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
356 err = usbd_get_port_status(dev, port, ps);
358 DPRINTF(("usbd_reset_port: get status failed %d\n",
362 /* If the device disappeared, just give up. */
363 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
364 return (USBD_NORMAL_COMPLETION);
365 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
367 return (USBD_TIMEOUT);
368 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
371 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
375 /* Wait for the device to recover from reset. */
376 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
380 usb_interface_descriptor_t *
381 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
383 char *p = (char *)cd;
384 char *end = p + UGETW(cd->wTotalLength);
385 usb_interface_descriptor_t *d;
386 int curidx, lastidx, curaidx = 0;
388 for (curidx = lastidx = -1; p < end; ) {
389 d = (usb_interface_descriptor_t *)p;
390 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
392 ifaceidx, curidx, altidx, curaidx,
393 d->bLength, d->bDescriptorType));
394 if (d->bLength == 0) /* bad descriptor */
397 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
398 if (d->bInterfaceNumber != lastidx) {
399 lastidx = d->bInterfaceNumber;
404 if (ifaceidx == curidx && altidx == curaidx)
411 usb_endpoint_descriptor_t *
412 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
415 char *p = (char *)cd;
416 char *end = p + UGETW(cd->wTotalLength);
417 usb_interface_descriptor_t *d;
418 usb_endpoint_descriptor_t *e;
421 d = usbd_find_idesc(cd, ifaceidx, altidx);
424 if (endptidx >= d->bNumEndpoints) /* quick exit */
428 for (p = (char *)d + d->bLength; p < end; ) {
429 e = (usb_endpoint_descriptor_t *)p;
430 if (e->bLength == 0) /* bad descriptor */
433 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
435 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
437 if (curidx == endptidx)
445 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
447 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
448 usb_interface_descriptor_t *idesc;
452 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
454 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
459 ifc->index = ifaceidx;
460 ifc->altindex = altidx;
461 nendpt = ifc->idesc->bNumEndpoints;
462 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
464 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
466 if (ifc->endpoints == NULL)
469 ifc->endpoints = NULL;
471 p = (char *)ifc->idesc + ifc->idesc->bLength;
472 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
473 #define ed ((usb_endpoint_descriptor_t *)p)
474 for (endpt = 0; endpt < nendpt; endpt++) {
475 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
476 for (; p < end; p += ed->bLength) {
477 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
479 p, end, ed->bLength, ed->bDescriptorType));
480 if (p + ed->bLength <= end && ed->bLength != 0 &&
481 ed->bDescriptorType == UDESC_ENDPOINT)
483 if (ed->bLength == 0 ||
484 ed->bDescriptorType == UDESC_INTERFACE)
487 /* passed end, or bad desc */
488 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
489 ed->bLength == 0 ? "0 length" :
490 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
494 ifc->endpoints[endpt].edesc = ed;
495 if (dev->speed == USB_SPEED_HIGH) {
497 /* Control and bulk endpoints have max packet limits. */
498 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
500 mps = USB_2_MAX_CTRL_PACKET;
503 mps = USB_2_MAX_BULK_PACKET;
505 if (UGETW(ed->wMaxPacketSize) != mps) {
506 USETW(ed->wMaxPacketSize, mps);
508 printf("usbd_fill_iface_data: bad max "
517 ifc->endpoints[endpt].refcnt = 0;
518 ifc->endpoints[endpt].savedtoggle = 0;
522 LIST_INIT(&ifc->pipes);
523 return (USBD_NORMAL_COMPLETION);
526 if (ifc->endpoints != NULL) {
527 free(ifc->endpoints, M_USB);
528 ifc->endpoints = NULL;
534 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
536 usbd_interface_handle ifc = &dev->ifaces[ifcno];
538 free(ifc->endpoints, M_USB);
542 usbd_set_config(usbd_device_handle dev, int conf)
544 usb_device_request_t req;
546 req.bmRequestType = UT_WRITE_DEVICE;
547 req.bRequest = UR_SET_CONFIG;
548 USETW(req.wValue, conf);
549 USETW(req.wIndex, 0);
550 USETW(req.wLength, 0);
551 return (usbd_do_request(dev, &req, 0));
555 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
558 usb_config_descriptor_t cd;
561 if (no == USB_UNCONFIG_NO)
562 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
564 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
565 /* Figure out what config index to use. */
566 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
567 err = usbd_get_config_desc(dev, index, &cd);
570 if (cd.bConfigurationValue == no)
571 return (usbd_set_config_index(dev, index, msg));
577 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
580 usb_config_descriptor_t cd, *cdp;
582 int i, ifcidx, nifc, len, selfpowered, power;
584 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
586 if (dev->config != USB_UNCONFIG_NO) {
587 nifc = dev->cdesc->bNumInterface;
589 /* Check that all interfaces are idle */
590 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
591 if (LIST_EMPTY(&dev->ifaces[ifcidx].pipes))
593 DPRINTF(("usbd_set_config_index: open pipes exist\n"));
594 return (USBD_IN_USE);
597 DPRINTF(("usbd_set_config_index: free old config\n"));
598 /* Free all configuration data structures. */
599 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
600 usbd_free_iface_data(dev, ifcidx);
601 free(dev->ifaces, M_USB);
602 free(dev->cdesc, M_USB);
605 dev->config = USB_UNCONFIG_NO;
608 if (index == USB_UNCONFIG_INDEX) {
609 /* We are unconfiguring the device, so leave unallocated. */
610 DPRINTF(("usbd_set_config_index: set config 0\n"));
611 err = usbd_set_config(dev, USB_UNCONFIG_NO);
613 DPRINTF(("usbd_set_config_index: setting config=0 "
614 "failed, error=%s\n", usbd_errstr(err)));
618 /* Get the short descriptor. */
619 err = usbd_get_config_desc(dev, index, &cd);
622 len = UGETW(cd.wTotalLength);
623 cdp = malloc(len, M_USB, M_NOWAIT);
627 /* Get the full descriptor. Try a few times for slow devices. */
628 for (i = 0; i < 3; i++) {
629 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
632 usbd_delay_ms(dev, 200);
636 if (cdp->bDescriptorType != UDESC_CONFIG) {
637 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
638 cdp->bDescriptorType));
643 /* Figure out if the device is self or bus powered. */
645 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
646 (cdp->bmAttributes & UC_SELF_POWERED)) {
647 /* May be self powered. */
648 if (cdp->bmAttributes & UC_BUS_POWERED) {
649 /* Must ask device. */
650 if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
652 * Hub claims to be self powered, but isn't.
653 * It seems that the power status can be
654 * determined by the hub characteristics.
656 usb_hub_descriptor_t hd;
657 usb_device_request_t req;
658 req.bmRequestType = UT_READ_CLASS_DEVICE;
659 req.bRequest = UR_GET_DESCRIPTOR;
660 USETW(req.wValue, 0);
661 USETW(req.wIndex, 0);
662 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
663 err = usbd_do_request(dev, &req, &hd);
665 (UGETW(hd.wHubCharacteristics) &
668 DPRINTF(("usbd_set_config_index: charac=0x%04x"
670 UGETW(hd.wHubCharacteristics),
673 err = usbd_get_device_status(dev, &ds);
675 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
677 DPRINTF(("usbd_set_config_index: status=0x%04x"
679 UGETW(ds.wStatus), usbd_errstr(err)));
684 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
685 "selfpowered=%d, power=%d\n",
686 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
687 selfpowered, cdp->bMaxPower * 2));
689 /* Check if we have enough power. */
691 if (dev->powersrc == NULL) {
692 DPRINTF(("usbd_set_config_index: No power source?\n"));
693 return (USBD_IOERROR);
696 power = cdp->bMaxPower * 2;
697 if (power > dev->powersrc->power) {
698 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
699 /* XXX print nicer message. */
701 printf("%s: device addr %d (config %d) exceeds power "
702 "budget, %d mA > %d mA\n",
703 USBDEVNAME(dev->bus->bdev), dev->address,
704 cdp->bConfigurationValue,
705 power, dev->powersrc->power);
710 dev->self_powered = selfpowered;
712 /* Set the actual configuration value. */
713 DPRINTF(("usbd_set_config_index: set config %d\n",
714 cdp->bConfigurationValue));
715 err = usbd_set_config(dev, cdp->bConfigurationValue);
717 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
719 cdp->bConfigurationValue, usbd_errstr(err)));
723 /* Allocate and fill interface data. */
724 nifc = cdp->bNumInterface;
725 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
727 if (dev->ifaces == NULL) {
731 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
733 dev->config = cdp->bConfigurationValue;
734 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
735 err = usbd_fill_iface_data(dev, ifcidx, 0);
737 while (--ifcidx >= 0)
738 usbd_free_iface_data(dev, ifcidx);
743 return (USBD_NORMAL_COMPLETION);
750 /* XXX add function for alternate settings */
753 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
754 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
759 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
760 dev, iface, ep, pipe));
761 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
774 SIMPLEQ_INIT(&p->queue);
775 err = dev->bus->methods->open_pipe(p);
777 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
779 ep->edesc->bEndpointAddress, usbd_errstr(err)));
784 if (dev->quirks->uq_flags & UQ_OPEN_CLEARSTALL) {
785 /* Clear any stall and make sure DATA0 toggle will be used next. */
786 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT) {
787 err = usbd_clear_endpoint_stall(p);
788 if (err && err != USBD_STALLED && err != USBD_TIMEOUT) {
789 printf("usbd_setup_pipe: failed to start "
790 "endpoint, %s\n", usbd_errstr(err));
797 return (USBD_NORMAL_COMPLETION);
800 /* Abort the device control pipe. */
802 usbd_kill_pipe(usbd_pipe_handle pipe)
804 usbd_abort_pipe(pipe);
805 pipe->methods->close(pipe);
806 pipe->endpoint->refcnt--;
811 usbd_getnewaddr(usbd_bus_handle bus)
815 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
816 if (bus->devices[addr] == 0)
823 usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
826 struct usb_attach_arg uaa;
827 usb_device_descriptor_t *dd = &dev->ddesc;
828 int found, i, confi, nifaces;
832 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
835 #if defined(__FreeBSD__)
836 /* XXX FreeBSD may leak resources on failure cases -- fixme */
838 struct usb_attach_arg *uaap;
840 devinfo = malloc(1024, M_USB, M_NOWAIT);
841 if (devinfo == NULL) {
842 device_printf(parent, "Can't allocate memory for probe string\n");
845 bdev = device_add_child(parent, NULL, -1);
847 free(devinfo, M_USB);
848 device_printf(parent, "Device creation failed\n");
851 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
853 free(devinfo, M_USB);
856 device_set_ivars(bdev, uaap);
864 uaa.configno = UHUB_UNK_CONFIGURATION;
865 uaa.ifaceno = UHUB_UNK_INTERFACE;
866 uaa.vendor = UGETW(dd->idVendor);
867 uaa.product = UGETW(dd->idProduct);
868 uaa.release = UGETW(dd->bcdDevice);
870 /* First try with device specific drivers. */
871 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
873 dev->ifacenums = NULL;
874 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
875 if (dev->subdevs == NULL) {
876 free(devinfo, M_USB);
879 dev->subdevs[0] = bdev;
882 usbd_devinfo(dev, 1, devinfo);
883 device_set_desc_copy(bdev, devinfo);
884 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
886 free(devinfo, M_USB);
887 return (USBD_NORMAL_COMPLETION);
890 * Free subdevs so we can reallocate it larger for the number of
893 tmpdv = dev->subdevs;
896 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
898 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
899 dd->bNumConfigurations));
900 /* Next try with interface drivers. */
901 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
902 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
904 err = usbd_set_config_index(dev, confi, 1);
907 DPRINTF(("%s: port %d, set config at addr %d failed, "
908 "error=%s\n", USBDEVPTRNAME(parent), port,
909 addr, usbd_errstr(err)));
911 printf("%s: port %d, set config at addr %d failed\n",
912 USBDEVPTRNAME(parent), port, addr);
914 free(devinfo, M_USB);
917 nifaces = dev->cdesc->bNumInterface;
918 uaa.configno = dev->cdesc->bConfigurationValue;
919 for (i = 0; i < nifaces; i++)
920 ifaces[i] = &dev->ifaces[i];
922 uaa.nifaces = nifaces;
923 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
924 if (dev->subdevs == NULL) {
925 free(devinfo, M_USB);
928 dev->ifacenums = malloc((nifaces) * sizeof(*dev->ifacenums),
930 if (dev->ifacenums == NULL) {
931 free(devinfo, M_USB);
936 for (i = 0; i < nifaces; i++) {
937 if (ifaces[i] == NULL)
938 continue; /* interface already claimed */
939 uaa.iface = ifaces[i];
940 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
941 dev->subdevs[found] = bdev;
942 dev->subdevs[found + 1] = 0;
943 dev->ifacenums[found] = i;
945 usbd_devinfo(dev, 1, devinfo);
946 device_set_desc_copy(bdev, devinfo);
947 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
950 ifaces[i] = 0; /* consumed */
953 /* create another child for the next iface */
954 bdev = device_add_child(parent, NULL, -1);
956 device_printf(parent,
957 "Device add failed\n");
958 free(devinfo, M_USB);
959 return (USBD_NORMAL_COMPLETION);
961 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
965 device_set_ivars(bdev, uaap);
968 dev->subdevs[found] = 0;
973 /* remove the last created child. It is unused */
975 free(devinfo, M_USB);
976 device_delete_child(parent, bdev);
977 /* free(uaap, M_USB); */ /* May be needed? xxx */
979 return (USBD_NORMAL_COMPLETION);
981 tmpdv = dev->subdevs;
984 free(dev->ifacenums, M_USB);
985 dev->ifacenums = NULL;
987 /* No interfaces were attached in any of the configurations. */
989 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
990 usbd_set_config_index(dev, 0, 0);
992 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
994 /* Finally try the generic driver. */
997 uaa.configno = UHUB_UNK_CONFIGURATION;
998 uaa.ifaceno = UHUB_UNK_INTERFACE;
999 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
1000 if (dev->subdevs == 0) {
1001 free(devinfo, M_USB);
1002 return (USBD_NOMEM);
1004 dev->subdevs[0] = bdev;
1005 dev->subdevs[1] = 0;
1007 usbd_devinfo(dev, 1, devinfo);
1008 device_set_desc_copy(bdev, devinfo);
1009 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
1011 return (USBD_NORMAL_COMPLETION);
1014 * The generic attach failed, but leave the device as it is.
1015 * We just did not find any drivers, that's all. The device is
1016 * fully operational and not harming anyone.
1018 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
1019 return (USBD_NORMAL_COMPLETION);
1024 * Called when a new device has been put in the powered state,
1025 * but not yet in the addressed state.
1026 * Get initial descriptor, set the address, get full descriptor,
1027 * and attach a driver.
1030 usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
1031 int speed, int port, struct usbd_port *up)
1033 usbd_device_handle dev, adev;
1034 struct usbd_device *hub;
1035 usb_device_descriptor_t *dd;
1036 usb_port_status_t ps;
1042 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
1043 bus, port, depth, speed));
1044 addr = usbd_getnewaddr(bus);
1046 printf("%s: No free USB addresses, new device ignored.\n",
1047 USBDEVNAME(bus->bdev));
1048 return (USBD_NO_ADDR);
1051 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1053 return (USBD_NOMEM);
1057 /* Set up default endpoint handle. */
1058 dev->def_ep.edesc = &dev->def_ep_desc;
1060 /* Set up default endpoint descriptor. */
1061 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1062 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1063 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1064 dev->def_ep_desc.bmAttributes = UE_CONTROL;
1065 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1066 dev->def_ep_desc.bInterval = 0;
1068 dev->quirks = &usbd_no_quirk;
1069 dev->address = USB_START_ADDR;
1070 dev->ddesc.bMaxPacketSize = 0;
1073 dev->myhub = up->parent;
1077 /* Locate port on upstream high speed hub */
1078 for (adev = dev, hub = up->parent;
1079 hub != NULL && hub->speed != USB_SPEED_HIGH;
1080 adev = hub, hub = hub->myhub)
1083 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1084 if (hub->hub->ports[p].device == adev) {
1085 dev->myhsport = &hub->hub->ports[p];
1089 panic("usbd_new_device: cannot find HS port\n");
1091 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1093 dev->myhsport = NULL;
1096 dev->langid = USBD_NOLANG;
1097 dev->cookie.cookie = ++usb_cookie_no;
1099 /* Establish the default pipe. */
1100 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1101 &dev->default_pipe);
1103 usbd_remove_device(dev, up);
1107 /* Set the address. Do this early; some devices need that. */
1108 /* Try a few times in case the device is slow (i.e. outside specs.) */
1109 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1110 for (i = 0; i < 15; i++) {
1111 err = usbd_set_address(dev, addr);
1114 usbd_delay_ms(dev, 200);
1116 DPRINTFN(-1,("usb_new_device: set address %d "
1117 "failed - trying a port reset\n", addr));
1118 usbd_reset_port(up->parent, port, &ps);
1122 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1123 err = USBD_SET_ADDR_FAILED;
1124 usbd_remove_device(dev, up);
1127 /* Allow device time to set new address */
1128 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1129 dev->address = addr; /* New device address now */
1130 bus->devices[addr] = dev;
1133 /* Get the first 8 bytes of the device descriptor. */
1134 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1136 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1138 usbd_remove_device(dev, up);
1142 if (speed == USB_SPEED_HIGH) {
1143 /* Max packet size must be 64 (sec 5.5.3). */
1144 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1146 printf("usbd_new_device: addr=%d bad max packet size\n",
1149 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1153 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1154 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1155 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1156 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1159 if (dd->bDescriptorType != UDESC_DEVICE) {
1160 /* Illegal device descriptor */
1161 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1162 dd->bDescriptorType));
1163 usbd_remove_device(dev, up);
1164 return (USBD_INVAL);
1167 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1168 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1169 usbd_remove_device(dev, up);
1170 return (USBD_INVAL);
1173 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1175 err = usbd_reload_device_desc(dev);
1177 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1179 usbd_remove_device(dev, up);
1183 /* Assume 100mA bus powered for now. Changed when configured. */
1184 dev->power = USB_MIN_POWER;
1185 dev->self_powered = 0;
1187 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1188 addr, dev, parent));
1190 err = usbd_probe_and_attach(parent, dev, port, addr);
1192 usbd_remove_device(dev, up);
1196 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1198 return (USBD_NORMAL_COMPLETION);
1202 usbd_reload_device_desc(usbd_device_handle dev)
1207 /* Get the full device descriptor. */
1208 for (i = 0; i < 3; ++i) {
1209 err = usbd_get_device_desc(dev, &dev->ddesc);
1212 usbd_delay_ms(dev, 200);
1217 /* Figure out what's wrong with this device. */
1218 dev->quirks = usbd_find_quirk(&dev->ddesc);
1220 return (USBD_NORMAL_COMPLETION);
1224 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1226 DPRINTF(("usbd_remove_device: %p\n", dev));
1228 if (dev->default_pipe != NULL)
1229 usbd_kill_pipe(dev->default_pipe);
1231 dev->bus->devices[dev->address] = NULL;
1236 #if defined(__NetBSD__) || defined(__OpenBSD__)
1238 usbd_print(void *aux, const char *pnp)
1240 struct usb_attach_arg *uaa = aux;
1243 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1245 if (!uaa->usegeneric)
1247 usbd_devinfo(uaa->device, 1, devinfo);
1248 printf("%s, %s", devinfo, pnp);
1251 printf(" port %d", uaa->port);
1252 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1253 printf(" configuration %d", uaa->configno);
1254 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1255 printf(" interface %d", uaa->ifaceno);
1258 * It gets very crowded with these locators on the attach line.
1259 * They are not really needed since they are printed in the clear
1262 if (uaa->vendor != UHUB_UNK_VENDOR)
1263 printf(" vendor 0x%04x", uaa->vendor);
1264 if (uaa->product != UHUB_UNK_PRODUCT)
1265 printf(" product 0x%04x", uaa->product);
1266 if (uaa->release != UHUB_UNK_RELEASE)
1267 printf(" release 0x%04x", uaa->release);
1272 #if defined(__NetBSD__)
1274 usbd_submatch(struct device *parent, struct cfdata *cf, void *aux)
1276 #elif defined(__OpenBSD__)
1278 usbd_submatch(struct device *parent, void *match, void *aux)
1280 struct cfdata *cf = match;
1282 struct usb_attach_arg *uaa = aux;
1284 DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1285 "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1286 uaa->port, cf->uhubcf_port,
1287 uaa->configno, cf->uhubcf_configuration,
1288 uaa->ifaceno, cf->uhubcf_interface,
1289 uaa->vendor, cf->uhubcf_vendor,
1290 uaa->product, cf->uhubcf_product,
1291 uaa->release, cf->uhubcf_release));
1292 if (uaa->port != 0 && /* root hub has port 0, it should match */
1294 cf->uhubcf_port != UHUB_UNK_PORT &&
1295 cf->uhubcf_port != uaa->port) ||
1296 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1297 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1298 cf->uhubcf_configuration != uaa->configno) ||
1299 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1300 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1301 cf->uhubcf_interface != uaa->ifaceno) ||
1302 (uaa->vendor != UHUB_UNK_VENDOR &&
1303 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1304 cf->uhubcf_vendor != uaa->vendor) ||
1305 (uaa->product != UHUB_UNK_PRODUCT &&
1306 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1307 cf->uhubcf_product != uaa->product) ||
1308 (uaa->release != UHUB_UNK_RELEASE &&
1309 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1310 cf->uhubcf_release != uaa->release)
1314 if (cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1315 cf->uhubcf_vendor == uaa->vendor &&
1316 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1317 cf->uhubcf_product == uaa->product) {
1318 /* We have a vendor&product locator match */
1319 if (cf->uhubcf_release != UHUB_UNK_RELEASE &&
1320 cf->uhubcf_release == uaa->release)
1321 uaa->matchlvl = UMATCH_VENDOR_PRODUCT_REV;
1323 uaa->matchlvl = UMATCH_VENDOR_PRODUCT;
1326 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1332 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1335 struct usbd_port *p;
1338 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1339 di->udi_addr = dev->address;
1340 di->udi_cookie = dev->cookie;
1341 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
1342 usbd_printBCD(di->udi_release, UGETW(dev->ddesc.bcdDevice));
1343 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1344 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1345 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1346 di->udi_class = dev->ddesc.bDeviceClass;
1347 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1348 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1349 di->udi_config = dev->config;
1350 di->udi_power = dev->self_powered ? 0 : dev->power;
1351 di->udi_speed = dev->speed;
1353 if (dev->subdevs != NULL) {
1354 for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) {
1355 if (device_is_attached(dev->subdevs[i]))
1356 strlcpy(di->udi_devnames[i],
1357 USBDEVPTRNAME(dev->subdevs[i]),
1358 USB_MAX_DEVNAMELEN);
1360 di->udi_devnames[i][0] = 0;
1365 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1366 di->udi_devnames[i][0] = 0; /* empty */
1370 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1371 i < dev->hub->hubdesc.bNbrPorts;
1373 p = &dev->hub->ports[i];
1375 err = p->device->address;
1377 s = UGETW(p->status.wPortStatus);
1378 if (s & UPS_PORT_ENABLED)
1379 err = USB_PORT_ENABLED;
1380 else if (s & UPS_SUSPEND)
1381 err = USB_PORT_SUSPENDED;
1382 else if (s & UPS_PORT_POWER)
1383 err = USB_PORT_POWERED;
1385 err = USB_PORT_DISABLED;
1387 di->udi_ports[i] = err;
1389 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1395 usb_free_device(usbd_device_handle dev)
1399 if (dev->default_pipe != NULL)
1400 usbd_kill_pipe(dev->default_pipe);
1401 if (dev->ifaces != NULL) {
1402 nifc = dev->cdesc->bNumInterface;
1403 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1404 usbd_free_iface_data(dev, ifcidx);
1405 free(dev->ifaces, M_USB);
1407 if (dev->cdesc != NULL)
1408 free(dev->cdesc, M_USB);
1409 if (dev->subdevs != NULL)
1410 free(dev->subdevs, M_USB);
1411 if (dev->ifacenums != NULL)
1412 free(dev->ifacenums, M_USB);
1417 * The general mechanism for detaching drivers works as follows: Each
1418 * driver is responsible for maintaining a reference count on the
1419 * number of outstanding references to its softc (e.g. from
1420 * processing hanging in a read or write). The detach method of the
1421 * driver decrements this counter and flags in the softc that the
1422 * driver is dying and then wakes any sleepers. It then sleeps on the
1423 * softc. Each place that can sleep must maintain the reference
1424 * count. When the reference count drops to -1 (0 is the normal value
1425 * of the reference count) the a wakeup on the softc is performed
1426 * signaling to the detach waiter that all references are gone.
1430 * Called from process context when we discover that a port has
1431 * been disconnected.
1434 usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1436 usbd_device_handle dev = up->device;
1437 const char *hubname = USBDEVPTRNAME(parent);
1440 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1441 up, dev, up->portno));
1445 printf("usb_disconnect_port: no device\n");
1450 if (dev->subdevs != NULL) {
1451 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1452 for (i = 0; dev->subdevs[i]; i++) {
1453 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1455 if (up->portno != 0)
1456 printf(" port %d", up->portno);
1457 printf(" (addr %d) disconnected\n", dev->address);
1458 config_detach(dev->subdevs[i], DETACH_FORCE);
1459 dev->subdevs[i] = NULL;
1463 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1464 dev->bus->devices[dev->address] = NULL;
1466 usb_free_device(dev);
1470 void *usb_realloc(void *p, u_int size, int pool, int flags)
1474 q = malloc(size, pool, flags);