1 /* $NetBSD: usb_subr.c,v 1.98 2002/02/20 20:30:13 christos Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #if defined(__NetBSD__) || defined(__OpenBSD__)
46 #include <sys/device.h>
47 #include <sys/select.h>
48 #elif defined(__FreeBSD__)
49 #include <sys/module.h>
54 #include <machine/bus.h>
56 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbdivar.h>
61 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/usb_quirks.h>
64 #if defined(__FreeBSD__)
65 #include <machine/clock.h>
66 #define delay(d) DELAY(d)
70 #define DPRINTF(x) if (usbdebug) logprintf x
71 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
78 Static usbd_status usbd_set_config(usbd_device_handle, int);
79 Static void usbd_devinfo_vp(usbd_device_handle, char *, char *, int);
80 Static char *usbd_get_string(usbd_device_handle, int, char *);
81 Static int usbd_getnewaddr(usbd_bus_handle bus);
82 #if defined(__NetBSD__)
83 Static int usbd_print(void *aux, const char *pnp);
84 Static int usbd_submatch(device_ptr_t, struct cfdata *cf, void *);
85 #elif defined(__OpenBSD__)
86 Static int usbd_print(void *aux, const char *pnp);
87 Static int usbd_submatch(device_ptr_t, void *, void *);
89 Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
90 Static void usbd_kill_pipe(usbd_pipe_handle);
91 Static usbd_status usbd_probe_and_attach(device_ptr_t parent,
92 usbd_device_handle dev, int port, int addr);
94 Static u_int32_t usb_cookie_no = 0;
97 typedef u_int16_t usb_vendor_id_t;
98 typedef u_int16_t usb_product_id_t;
101 * Descriptions of of known vendors and devices ("products").
103 struct usb_knowndev {
104 usb_vendor_id_t vendor;
105 usb_product_id_t product;
107 char *vendorname, *productname;
109 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
111 #include <dev/usb/usbdevs_data.h>
112 #endif /* USBVERBOSE */
114 Static const char * const usbd_error_strs[] = {
138 usbd_errstr(usbd_status err)
140 static char buffer[5];
142 if (err < USBD_ERROR_MAX) {
143 return usbd_error_strs[err];
145 snprintf(buffer, sizeof buffer, "%d", err);
151 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
152 usb_string_descriptor_t *sdesc)
154 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, 1); /* only size byte first */
162 err = usbd_do_request(dev, &req, sdesc);
165 USETW(req.wLength, sdesc->bLength); /* the whole string */
166 return (usbd_do_request(dev, &req, sdesc));
170 usbd_get_string(usbd_device_handle dev, int si, char *buf)
172 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
173 usb_string_descriptor_t us;
181 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
183 if (dev->langid == USBD_NOLANG) {
184 /* Set up default language */
185 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us);
186 if (err || us.bLength < 4) {
187 dev->langid = 0; /* Well, just pick something then */
189 /* Pick the first language as the default. */
190 dev->langid = UGETW(us.bString[0]);
193 err = usbd_get_string_desc(dev, si, dev->langid, &us);
197 n = us.bLength / 2 - 1;
198 for (i = 0; i < n; i++) {
199 c = UGETW(us.bString[i]);
200 /* Convert from Unicode, handle buggy strings. */
201 if ((c & 0xff00) == 0)
203 else if ((c & 0x00ff) == 0 && swap)
213 usbd_trim_spaces(char *p)
220 while (*q == ' ') /* skip leading spaces */
222 while ((*p = *q++)) /* copy string */
223 if (*p++ != ' ') /* remember last non-space */
225 *e = 0; /* kill trailing spaces */
229 usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev)
231 usb_device_descriptor_t *udd = &dev->ddesc;
232 char *vendor = 0, *product = 0;
234 const struct usb_knowndev *kdp;
243 vendor = usbd_get_string(dev, udd->iManufacturer, v);
244 usbd_trim_spaces(vendor);
245 product = usbd_get_string(dev, udd->iProduct, p);
246 usbd_trim_spaces(product);
252 if (vendor == NULL || product == NULL) {
253 for(kdp = usb_knowndevs;
254 kdp->vendorname != NULL;
256 if (kdp->vendor == UGETW(udd->idVendor) &&
257 (kdp->product == UGETW(udd->idProduct) ||
258 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
261 if (kdp->vendorname != NULL) {
263 vendor = kdp->vendorname;
265 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
266 kdp->productname : NULL;
270 if (vendor != NULL && *vendor)
273 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
274 if (product != NULL && *product)
277 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
281 usbd_printBCD(char *cp, int bcd)
283 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
287 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp)
289 usb_device_descriptor_t *udd = &dev->ddesc;
290 char vendor[USB_MAX_STRING_LEN];
291 char product[USB_MAX_STRING_LEN];
292 int bcdDevice, bcdUSB;
294 usbd_devinfo_vp(dev, vendor, product, 1);
295 cp += sprintf(cp, "%s %s", vendor, product);
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);
309 /* Delay for a certain number of ms */
311 usb_delay_ms(usbd_bus_handle bus, u_int ms)
313 /* Wait at least two clock ticks so we know the time has passed. */
314 if (bus->use_polling || cold)
315 delay((ms+1) * 1000);
317 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
320 /* Delay given a device handle. */
322 usbd_delay_ms(usbd_device_handle dev, u_int ms)
324 usb_delay_ms(dev->bus, ms);
328 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
330 usb_device_request_t req;
334 req.bmRequestType = UT_WRITE_CLASS_OTHER;
335 req.bRequest = UR_SET_FEATURE;
336 USETW(req.wValue, UHF_PORT_RESET);
337 USETW(req.wIndex, port);
338 USETW(req.wLength, 0);
339 err = usbd_do_request(dev, &req, 0);
340 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
341 port, usbd_errstr(err)));
346 /* Wait for device to recover from reset. */
347 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
348 err = usbd_get_port_status(dev, port, ps);
350 DPRINTF(("usbd_reset_port: get status failed %d\n",
354 /* If the device disappeared, just give up. */
355 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
356 return (USBD_NORMAL_COMPLETION);
357 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
359 return (USBD_TIMEOUT);
360 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
363 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
367 /* Wait for the device to recover from reset. */
368 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
372 usb_interface_descriptor_t *
373 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
375 char *p = (char *)cd;
376 char *end = p + UGETW(cd->wTotalLength);
377 usb_interface_descriptor_t *d;
378 int curidx, lastidx, curaidx = 0;
380 for (curidx = lastidx = -1; p < end; ) {
381 d = (usb_interface_descriptor_t *)p;
382 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
384 ifaceidx, curidx, altidx, curaidx,
385 d->bLength, d->bDescriptorType));
386 if (d->bLength == 0) /* bad descriptor */
389 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
390 if (d->bInterfaceNumber != lastidx) {
391 lastidx = d->bInterfaceNumber;
396 if (ifaceidx == curidx && altidx == curaidx)
403 usb_endpoint_descriptor_t *
404 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
407 char *p = (char *)cd;
408 char *end = p + UGETW(cd->wTotalLength);
409 usb_interface_descriptor_t *d;
410 usb_endpoint_descriptor_t *e;
413 d = usbd_find_idesc(cd, ifaceidx, altidx);
416 if (endptidx >= d->bNumEndpoints) /* quick exit */
420 for (p = (char *)d + d->bLength; p < end; ) {
421 e = (usb_endpoint_descriptor_t *)p;
422 if (e->bLength == 0) /* bad descriptor */
425 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
427 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
429 if (curidx == endptidx)
437 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
439 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
440 usb_interface_descriptor_t *idesc;
444 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
446 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
451 ifc->index = ifaceidx;
452 ifc->altindex = altidx;
453 nendpt = ifc->idesc->bNumEndpoints;
454 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
456 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
458 if (ifc->endpoints == NULL)
461 ifc->endpoints = NULL;
463 p = (char *)ifc->idesc + ifc->idesc->bLength;
464 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
465 #define ed ((usb_endpoint_descriptor_t *)p)
466 for (endpt = 0; endpt < nendpt; endpt++) {
467 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
468 for (; p < end; p += ed->bLength) {
469 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
471 p, end, ed->bLength, ed->bDescriptorType));
472 if (p + ed->bLength <= end && ed->bLength != 0 &&
473 ed->bDescriptorType == UDESC_ENDPOINT)
475 if (ed->bLength == 0 ||
476 ed->bDescriptorType == UDESC_INTERFACE)
479 /* passed end, or bad desc */
480 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
481 ed->bLength == 0 ? "0 length" :
482 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
486 ifc->endpoints[endpt].edesc = ed;
487 if (dev->speed == USB_SPEED_HIGH) {
489 /* Control and bulk endpoints have max packet limits. */
490 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
492 mps = USB_2_MAX_CTRL_PACKET;
495 mps = USB_2_MAX_BULK_PACKET;
497 if (UGETW(ed->wMaxPacketSize) != mps) {
498 USETW(ed->wMaxPacketSize, mps);
500 printf("usbd_fill_iface_data: bad max "
509 ifc->endpoints[endpt].refcnt = 0;
513 LIST_INIT(&ifc->pipes);
514 return (USBD_NORMAL_COMPLETION);
517 if (ifc->endpoints != NULL) {
518 free(ifc->endpoints, M_USB);
519 ifc->endpoints = NULL;
525 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
527 usbd_interface_handle ifc = &dev->ifaces[ifcno];
529 free(ifc->endpoints, M_USB);
533 usbd_set_config(usbd_device_handle dev, int conf)
535 usb_device_request_t req;
537 req.bmRequestType = UT_WRITE_DEVICE;
538 req.bRequest = UR_SET_CONFIG;
539 USETW(req.wValue, conf);
540 USETW(req.wIndex, 0);
541 USETW(req.wLength, 0);
542 return (usbd_do_request(dev, &req, 0));
546 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
549 usb_config_descriptor_t cd;
552 if (no == USB_UNCONFIG_NO)
553 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
555 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
556 /* Figure out what config index to use. */
557 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
558 err = usbd_get_config_desc(dev, index, &cd);
561 if (cd.bConfigurationValue == no)
562 return (usbd_set_config_index(dev, index, msg));
568 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
571 usb_config_descriptor_t cd, *cdp;
573 int ifcidx, nifc, len, selfpowered, power;
575 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
577 /* XXX check that all interfaces are idle */
578 if (dev->config != USB_UNCONFIG_NO) {
579 DPRINTF(("usbd_set_config_index: free old config\n"));
580 /* Free all configuration data structures. */
581 nifc = dev->cdesc->bNumInterface;
582 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
583 usbd_free_iface_data(dev, ifcidx);
584 free(dev->ifaces, M_USB);
585 free(dev->cdesc, M_USB);
588 dev->config = USB_UNCONFIG_NO;
591 if (index == USB_UNCONFIG_INDEX) {
592 /* We are unconfiguring the device, so leave unallocated. */
593 DPRINTF(("usbd_set_config_index: set config 0\n"));
594 err = usbd_set_config(dev, USB_UNCONFIG_NO);
596 DPRINTF(("usbd_set_config_index: setting config=0 "
597 "failed, error=%s\n", usbd_errstr(err)));
601 /* Get the short descriptor. */
602 err = usbd_get_config_desc(dev, index, &cd);
605 len = UGETW(cd.wTotalLength);
606 cdp = malloc(len, M_USB, M_NOWAIT);
609 /* Get the full descriptor. */
610 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
613 if (cdp->bDescriptorType != UDESC_CONFIG) {
614 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
615 cdp->bDescriptorType));
620 /* Figure out if the device is self or bus powered. */
622 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
623 (cdp->bmAttributes & UC_SELF_POWERED)) {
624 /* May be self powered. */
625 if (cdp->bmAttributes & UC_BUS_POWERED) {
626 /* Must ask device. */
627 if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
629 * Hub claims to be self powered, but isn't.
630 * It seems that the power status can be
631 * determined by the hub characteristics.
633 usb_hub_descriptor_t hd;
634 usb_device_request_t req;
635 req.bmRequestType = UT_READ_CLASS_DEVICE;
636 req.bRequest = UR_GET_DESCRIPTOR;
637 USETW(req.wValue, 0);
638 USETW(req.wIndex, 0);
639 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
640 err = usbd_do_request(dev, &req, &hd);
642 (UGETW(hd.wHubCharacteristics) &
645 DPRINTF(("usbd_set_config_index: charac=0x%04x"
647 UGETW(hd.wHubCharacteristics),
650 err = usbd_get_device_status(dev, &ds);
652 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
654 DPRINTF(("usbd_set_config_index: status=0x%04x"
656 UGETW(ds.wStatus), usbd_errstr(err)));
661 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
662 "selfpowered=%d, power=%d\n",
663 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
664 selfpowered, cdp->bMaxPower * 2));
666 /* Check if we have enough power. */
668 if (dev->powersrc == NULL) {
669 DPRINTF(("usbd_set_config_index: No power source?\n"));
670 return (USBD_IOERROR);
673 power = cdp->bMaxPower * 2;
674 if (power > dev->powersrc->power) {
675 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
676 /* XXX print nicer message. */
678 printf("%s: device addr %d (config %d) exceeds power "
679 "budget, %d mA > %d mA\n",
680 USBDEVNAME(dev->bus->bdev), dev->address,
681 cdp->bConfigurationValue,
682 power, dev->powersrc->power);
687 dev->self_powered = selfpowered;
689 /* Set the actual configuration value. */
690 DPRINTF(("usbd_set_config_index: set config %d\n",
691 cdp->bConfigurationValue));
692 err = usbd_set_config(dev, cdp->bConfigurationValue);
694 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
696 cdp->bConfigurationValue, usbd_errstr(err)));
700 /* Allocate and fill interface data. */
701 nifc = cdp->bNumInterface;
702 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
704 if (dev->ifaces == NULL) {
708 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
710 dev->config = cdp->bConfigurationValue;
711 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
712 err = usbd_fill_iface_data(dev, ifcidx, 0);
714 while (--ifcidx >= 0)
715 usbd_free_iface_data(dev, ifcidx);
720 return (USBD_NORMAL_COMPLETION);
727 /* XXX add function for alternate settings */
730 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
731 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
736 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
737 dev, iface, ep, pipe));
738 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
751 SIMPLEQ_INIT(&p->queue);
752 err = dev->bus->methods->open_pipe(p);
754 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
756 ep->edesc->bEndpointAddress, usbd_errstr(err)));
760 /* Clear any stall and make sure DATA0 toggle will be used next. */
761 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT)
762 usbd_clear_endpoint_stall(p);
764 return (USBD_NORMAL_COMPLETION);
767 /* Abort the device control pipe. */
769 usbd_kill_pipe(usbd_pipe_handle pipe)
771 usbd_abort_pipe(pipe);
772 pipe->methods->close(pipe);
773 pipe->endpoint->refcnt--;
778 usbd_getnewaddr(usbd_bus_handle bus)
782 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
783 if (bus->devices[addr] == 0)
790 usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
793 struct usb_attach_arg uaa;
794 usb_device_descriptor_t *dd = &dev->ddesc;
795 int found, i, confi, nifaces;
798 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
800 #if defined(__FreeBSD__)
802 * XXX uaa is a static var. Not a problem as it _should_ be used only
803 * during probe and attach. Should be changed however.
806 bdev = device_add_child(parent, NULL, -1);
808 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
811 device_set_ivars(bdev, &uaa);
821 uaa.configno = UHUB_UNK_CONFIGURATION;
822 uaa.ifaceno = UHUB_UNK_INTERFACE;
823 uaa.vendor = UGETW(dd->idVendor);
824 uaa.product = UGETW(dd->idProduct);
825 uaa.release = UGETW(dd->bcdDevice);
827 /* First try with device specific drivers. */
828 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
829 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
831 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
832 if (dev->subdevs == NULL)
834 dev->subdevs[0] = dv;
836 return (USBD_NORMAL_COMPLETION);
839 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
841 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
842 dd->bNumConfigurations));
843 /* Next try with interface drivers. */
844 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
845 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
847 err = usbd_set_config_index(dev, confi, 1);
850 DPRINTF(("%s: port %d, set config at addr %d failed, "
851 "error=%s\n", USBDEVPTRNAME(parent), port,
852 addr, usbd_errstr(err)));
854 printf("%s: port %d, set config at addr %d failed\n",
855 USBDEVPTRNAME(parent), port, addr);
857 #if defined(__FreeBSD__)
858 device_delete_child(parent, bdev);
863 nifaces = dev->cdesc->bNumInterface;
864 uaa.configno = dev->cdesc->bConfigurationValue;
865 for (i = 0; i < nifaces; i++)
866 ifaces[i] = &dev->ifaces[i];
868 uaa.nifaces = nifaces;
869 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
870 if (dev->subdevs == NULL) {
871 #if defined(__FreeBSD__)
872 device_delete_child(parent, bdev);
878 for (i = 0; i < nifaces; i++) {
879 if (ifaces[i] == NULL)
880 continue; /* interface already claimed */
881 uaa.iface = ifaces[i];
882 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
883 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
886 dev->subdevs[found++] = dv;
887 dev->subdevs[found] = 0;
888 ifaces[i] = 0; /* consumed */
890 #if defined(__FreeBSD__)
891 /* create another child for the next iface */
892 bdev = device_add_child(parent, NULL, -1);
894 printf("%s: Device creation failed\n",
895 USBDEVNAME(dev->bus->bdev));
896 return (USBD_NORMAL_COMPLETION);
898 device_set_ivars(bdev, &uaa);
904 #if defined(__FreeBSD__)
905 /* remove the last created child again; it is unused */
906 device_delete_child(parent, bdev);
908 return (USBD_NORMAL_COMPLETION);
910 free(dev->subdevs, M_USB);
913 /* No interfaces were attached in any of the configurations. */
915 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
916 usbd_set_config_index(dev, 0, 0);
918 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
920 /* Finally try the generic driver. */
923 uaa.configno = UHUB_UNK_CONFIGURATION;
924 uaa.ifaceno = UHUB_UNK_INTERFACE;
925 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
927 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
928 if (dev->subdevs == 0)
930 dev->subdevs[0] = dv;
932 return (USBD_NORMAL_COMPLETION);
936 * The generic attach failed, but leave the device as it is.
937 * We just did not find any drivers, that's all. The device is
938 * fully operational and not harming anyone.
940 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
941 #if defined(__FreeBSD__)
942 device_delete_child(parent, bdev);
944 return (USBD_NORMAL_COMPLETION);
949 * Called when a new device has been put in the powered state,
950 * but not yet in the addressed state.
951 * Get initial descriptor, set the address, get full descriptor,
952 * and attach a driver.
955 usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
956 int speed, int port, struct usbd_port *up)
958 usbd_device_handle dev;
959 struct usbd_device *hub;
960 usb_device_descriptor_t *dd;
965 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
966 bus, port, depth, speed));
967 addr = usbd_getnewaddr(bus);
969 printf("%s: No free USB addresses, new device ignored.\n",
970 USBDEVNAME(bus->bdev));
971 return (USBD_NO_ADDR);
974 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
980 /* Set up default endpoint handle. */
981 dev->def_ep.edesc = &dev->def_ep_desc;
983 /* Set up default endpoint descriptor. */
984 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
985 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
986 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
987 dev->def_ep_desc.bmAttributes = UE_CONTROL;
988 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
989 dev->def_ep_desc.bInterval = 0;
991 dev->quirks = &usbd_no_quirk;
992 dev->address = USB_START_ADDR;
993 dev->ddesc.bMaxPacketSize = 0;
996 dev->myhub = up->parent;
997 for (hub = up->parent;
998 hub != NULL && hub->speed != USB_SPEED_HIGH;
1001 dev->myhighhub = hub;
1003 dev->langid = USBD_NOLANG;
1004 dev->cookie.cookie = ++usb_cookie_no;
1006 /* Establish the default pipe. */
1007 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1008 &dev->default_pipe);
1010 usbd_remove_device(dev, up);
1016 /* Try a few times in case the device is slow (i.e. outside specs.) */
1017 for (i = 0; i < 3; i++) {
1018 /* Get the first 8 bytes of the device descriptor. */
1019 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1022 usbd_delay_ms(dev, 200);
1025 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1027 usbd_remove_device(dev, up);
1031 if (speed == USB_SPEED_HIGH) {
1032 /* Max packet size must be 64 (sec 5.5.3). */
1033 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1035 printf("usbd_new_device: addr=%d bad max packet size\n",
1038 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1042 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1043 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1044 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1045 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1048 if (dd->bDescriptorType != UDESC_DEVICE) {
1049 /* Illegal device descriptor */
1050 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1051 dd->bDescriptorType));
1052 usbd_remove_device(dev, up);
1053 return (USBD_INVAL);
1056 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1057 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1058 usbd_remove_device(dev, up);
1059 return (USBD_INVAL);
1062 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1064 err = usbd_reload_device_desc(dev);
1066 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1068 usbd_remove_device(dev, up);
1072 /* Set the address */
1073 err = usbd_set_address(dev, addr);
1074 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1076 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1077 err = USBD_SET_ADDR_FAILED;
1078 usbd_remove_device(dev, up);
1081 /* Allow device time to set new address */
1082 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1084 dev->address = addr; /* New device address now */
1085 bus->devices[addr] = dev;
1087 /* Assume 100mA bus powered for now. Changed when configured. */
1088 dev->power = USB_MIN_POWER;
1089 dev->self_powered = 0;
1091 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1092 addr, dev, parent));
1094 err = usbd_probe_and_attach(parent, dev, port, addr);
1096 usbd_remove_device(dev, up);
1100 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1102 return (USBD_NORMAL_COMPLETION);
1106 usbd_reload_device_desc(usbd_device_handle dev)
1110 /* Get the full device descriptor. */
1111 err = usbd_get_device_desc(dev, &dev->ddesc);
1115 /* Figure out what's wrong with this device. */
1116 dev->quirks = usbd_find_quirk(&dev->ddesc);
1118 return (USBD_NORMAL_COMPLETION);
1122 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1124 DPRINTF(("usbd_remove_device: %p\n", dev));
1126 if (dev->default_pipe != NULL)
1127 usbd_kill_pipe(dev->default_pipe);
1129 dev->bus->devices[dev->address] = 0;
1134 #if defined(__NetBSD__) || defined(__OpenBSD__)
1136 usbd_print(void *aux, const char *pnp)
1138 struct usb_attach_arg *uaa = aux;
1141 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1143 if (!uaa->usegeneric)
1145 usbd_devinfo(uaa->device, 1, devinfo);
1146 printf("%s, %s", devinfo, pnp);
1149 printf(" port %d", uaa->port);
1150 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1151 printf(" configuration %d", uaa->configno);
1152 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1153 printf(" interface %d", uaa->ifaceno);
1156 * It gets very crowded with these locators on the attach line.
1157 * They are not really needed since they are printed in the clear
1160 if (uaa->vendor != UHUB_UNK_VENDOR)
1161 printf(" vendor 0x%04x", uaa->vendor);
1162 if (uaa->product != UHUB_UNK_PRODUCT)
1163 printf(" product 0x%04x", uaa->product);
1164 if (uaa->release != UHUB_UNK_RELEASE)
1165 printf(" release 0x%04x", uaa->release);
1170 #if defined(__NetBSD__)
1172 usbd_submatch(struct device *parent, struct cfdata *cf, void *aux)
1174 #elif defined(__OpenBSD__)
1176 usbd_submatch(struct device *parent, void *match, void *aux)
1178 struct cfdata *cf = match;
1180 struct usb_attach_arg *uaa = aux;
1182 DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1183 "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1184 uaa->port, cf->uhubcf_port,
1185 uaa->configno, cf->uhubcf_configuration,
1186 uaa->ifaceno, cf->uhubcf_interface,
1187 uaa->vendor, cf->uhubcf_vendor,
1188 uaa->product, cf->uhubcf_product,
1189 uaa->release, cf->uhubcf_release));
1190 if (uaa->port != 0 && /* root hub has port 0, it should match */
1192 cf->uhubcf_port != UHUB_UNK_PORT &&
1193 cf->uhubcf_port != uaa->port) ||
1194 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1195 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1196 cf->uhubcf_configuration != uaa->configno) ||
1197 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1198 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1199 cf->uhubcf_interface != uaa->ifaceno) ||
1200 (uaa->vendor != UHUB_UNK_VENDOR &&
1201 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1202 cf->uhubcf_vendor != uaa->vendor) ||
1203 (uaa->product != UHUB_UNK_PRODUCT &&
1204 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1205 cf->uhubcf_product != uaa->product) ||
1206 (uaa->release != UHUB_UNK_RELEASE &&
1207 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1208 cf->uhubcf_release != uaa->release)
1212 if (cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1213 cf->uhubcf_vendor == uaa->vendor &&
1214 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1215 cf->uhubcf_product == uaa->product) {
1216 /* We have a vendor&product locator match */
1217 if (cf->uhubcf_release != UHUB_UNK_RELEASE &&
1218 cf->uhubcf_release == uaa->release)
1219 uaa->matchlvl = UMATCH_VENDOR_PRODUCT_REV;
1221 uaa->matchlvl = UMATCH_VENDOR_PRODUCT;
1224 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1230 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1233 struct usbd_port *p;
1236 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1237 di->udi_addr = dev->address;
1238 di->udi_cookie = dev->cookie;
1239 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
1240 usbd_printBCD(di->udi_release, UGETW(dev->ddesc.bcdDevice));
1241 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1242 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1243 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1244 di->udi_class = dev->ddesc.bDeviceClass;
1245 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1246 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1247 di->udi_config = dev->config;
1248 di->udi_power = dev->self_powered ? 0 : dev->power;
1249 di->udi_speed = dev->speed;
1251 if (dev->subdevs != NULL) {
1252 for (i = 0; dev->subdevs[i] &&
1253 i < USB_MAX_DEVNAMES; i++) {
1254 strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1255 USB_MAX_DEVNAMELEN);
1256 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
1261 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1262 di->udi_devnames[i][0] = 0; /* empty */
1266 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1267 i < dev->hub->hubdesc.bNbrPorts;
1269 p = &dev->hub->ports[i];
1271 err = p->device->address;
1273 s = UGETW(p->status.wPortStatus);
1274 if (s & UPS_PORT_ENABLED)
1275 err = USB_PORT_ENABLED;
1276 else if (s & UPS_SUSPEND)
1277 err = USB_PORT_SUSPENDED;
1278 else if (s & UPS_PORT_POWER)
1279 err = USB_PORT_POWERED;
1281 err = USB_PORT_DISABLED;
1283 di->udi_ports[i] = err;
1285 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1291 usb_free_device(usbd_device_handle dev)
1295 if (dev->default_pipe != NULL)
1296 usbd_kill_pipe(dev->default_pipe);
1297 if (dev->ifaces != NULL) {
1298 nifc = dev->cdesc->bNumInterface;
1299 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1300 usbd_free_iface_data(dev, ifcidx);
1301 free(dev->ifaces, M_USB);
1303 if (dev->cdesc != NULL)
1304 free(dev->cdesc, M_USB);
1305 if (dev->subdevs != NULL)
1306 free(dev->subdevs, M_USB);
1311 * The general mechanism for detaching drivers works as follows: Each
1312 * driver is responsible for maintaining a reference count on the
1313 * number of outstanding references to its softc (e.g. from
1314 * processing hanging in a read or write). The detach method of the
1315 * driver decrements this counter and flags in the softc that the
1316 * driver is dying and then wakes any sleepers. It then sleeps on the
1317 * softc. Each place that can sleep must maintain the reference
1318 * count. When the reference count drops to -1 (0 is the normal value
1319 * of the reference count) the a wakeup on the softc is performed
1320 * signaling to the detach waiter that all references are gone.
1324 * Called from process context when we discover that a port has
1325 * been disconnected.
1328 usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1330 usbd_device_handle dev = up->device;
1331 const char *hubname = USBDEVPTRNAME(parent);
1334 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1335 up, dev, up->portno));
1339 printf("usb_disconnect_port: no device\n");
1344 if (dev->subdevs != NULL) {
1345 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1346 for (i = 0; dev->subdevs[i]; i++) {
1347 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1349 if (up->portno != 0)
1350 printf(" port %d", up->portno);
1351 printf(" (addr %d) disconnected\n", dev->address);
1352 config_detach(dev->subdevs[i], DETACH_FORCE);
1356 /*usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);*/
1357 dev->bus->devices[dev->address] = NULL;
1359 usb_free_device(dev);
1363 void *usb_realloc(void *p, u_int size, int pool, int flags)
1367 q = malloc(size, pool, flags);