2 * $NetBSD: usb_subr.c,v 1.99 2002/07/11 21:14:34 augustss Exp $
3 * $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.58 2003/09/01 07:47:42 ticso Exp $
4 * $DragonFly: src/sys/bus/usb/usb_subr.c,v 1.10 2004/03/19 00:35:57 dillon Exp $
7 /* Also already have from NetBSD:
8 * $NetBSD: usb_subr.c,v 1.102 2003/01/01 16:21:50 augustss Exp $
9 * $NetBSD: usb_subr.c,v 1.103 2003/01/10 11:19:13 augustss Exp $
13 * Copyright (c) 1998 The NetBSD Foundation, Inc.
14 * All rights reserved.
16 * This code is derived from software contributed to The NetBSD Foundation
17 * by Lennart Augustsson (lennart@augustsson.net) at
18 * Carlstedt Research & Technology.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by the NetBSD
31 * Foundation, Inc. and its contributors.
32 * 4. Neither the name of The NetBSD Foundation nor the names of its
33 * contributors may be used to endorse or promote products derived
34 * from this software without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
37 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
38 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
40 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46 * POSSIBILITY OF SUCH DAMAGE.
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #if defined(__NetBSD__) || defined(__OpenBSD__)
54 #include <sys/device.h>
55 #include <sys/select.h>
56 #elif defined(__FreeBSD__) || defined(__DragonFly__)
57 #include <sys/module.h>
62 #include <machine/bus.h>
67 #include "usbdi_util.h"
70 #include "usb_quirks.h"
72 #if defined(__FreeBSD__) || defined(__DragonFly__)
73 #include <machine/clock.h>
74 #define delay(d) DELAY(d)
78 #define DPRINTF(x) if (usbdebug) logprintf x
79 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
86 Static usbd_status usbd_set_config(usbd_device_handle, int);
87 Static void usbd_devinfo_vp(usbd_device_handle, char *, char *, int);
88 Static char *usbd_get_string(usbd_device_handle, int, char *);
89 Static int usbd_getnewaddr(usbd_bus_handle bus);
90 #if defined(__NetBSD__)
91 Static int usbd_print(void *aux, const char *pnp);
92 Static int usbd_submatch(device_ptr_t, struct cfdata *cf, void *);
93 #elif defined(__OpenBSD__)
94 Static int usbd_print(void *aux, const char *pnp);
95 Static int usbd_submatch(device_ptr_t, void *, void *);
97 Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
98 Static void usbd_kill_pipe(usbd_pipe_handle);
99 Static usbd_status usbd_probe_and_attach(device_ptr_t parent,
100 usbd_device_handle dev, int port, int addr);
102 Static u_int32_t usb_cookie_no = 0;
105 typedef u_int16_t usb_vendor_id_t;
106 typedef u_int16_t usb_product_id_t;
109 * Descriptions of of known vendors and devices ("products").
111 struct usb_knowndev {
112 usb_vendor_id_t vendor;
113 usb_product_id_t product;
115 char *vendorname, *productname;
117 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
119 #include "usbdevs_data.h"
120 #endif /* USBVERBOSE */
122 Static const char * const usbd_error_strs[] = {
146 usbd_errstr(usbd_status err)
148 static char buffer[5];
150 if (err < USBD_ERROR_MAX) {
151 return usbd_error_strs[err];
153 snprintf(buffer, sizeof buffer, "%d", err);
159 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
160 usb_string_descriptor_t *sdesc)
162 usb_device_request_t req;
166 req.bmRequestType = UT_READ_DEVICE;
167 req.bRequest = UR_GET_DESCRIPTOR;
168 USETW2(req.wValue, UDESC_STRING, sindex);
169 USETW(req.wIndex, langid);
170 USETW(req.wLength, 2); /* only size byte first */
171 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
172 &actlen, USBD_DEFAULT_TIMEOUT);
177 return (USBD_SHORT_XFER);
179 USETW(req.wLength, sdesc->bLength); /* the whole string */
180 return (usbd_do_request(dev, &req, sdesc));
184 usbd_get_string(usbd_device_handle dev, int si, char *buf)
186 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
187 usb_string_descriptor_t us;
195 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
197 if (dev->langid == USBD_NOLANG) {
198 /* Set up default language */
199 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us);
200 if (err || us.bLength < 4) {
201 dev->langid = 0; /* Well, just pick something then */
203 /* Pick the first language as the default. */
204 dev->langid = UGETW(us.bString[0]);
207 err = usbd_get_string_desc(dev, si, dev->langid, &us);
211 n = us.bLength / 2 - 1;
212 for (i = 0; i < n; i++) {
213 c = UGETW(us.bString[i]);
214 /* Convert from Unicode, handle buggy strings. */
215 if ((c & 0xff00) == 0)
217 else if ((c & 0x00ff) == 0 && swap)
227 usbd_trim_spaces(char *p)
234 while (*q == ' ') /* skip leading spaces */
236 while ((*p = *q++)) /* copy string */
237 if (*p++ != ' ') /* remember last non-space */
239 *e = 0; /* kill trailing spaces */
243 usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev)
245 usb_device_descriptor_t *udd = &dev->ddesc;
246 char *vendor = 0, *product = 0;
248 const struct usb_knowndev *kdp;
257 vendor = usbd_get_string(dev, udd->iManufacturer, v);
258 usbd_trim_spaces(vendor);
259 product = usbd_get_string(dev, udd->iProduct, p);
260 usbd_trim_spaces(product);
266 if (vendor == NULL || product == NULL) {
267 for(kdp = usb_knowndevs;
268 kdp->vendorname != NULL;
270 if (kdp->vendor == UGETW(udd->idVendor) &&
271 (kdp->product == UGETW(udd->idProduct) ||
272 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
275 if (kdp->vendorname != NULL) {
277 vendor = kdp->vendorname;
279 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
280 kdp->productname : NULL;
284 if (vendor != NULL && *vendor)
287 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
288 if (product != NULL && *product)
291 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
295 usbd_printBCD(char *cp, int bcd)
297 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
301 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp)
303 usb_device_descriptor_t *udd = &dev->ddesc;
304 char vendor[USB_MAX_STRING_LEN];
305 char product[USB_MAX_STRING_LEN];
306 int bcdDevice, bcdUSB;
308 usbd_devinfo_vp(dev, vendor, product, 1);
309 cp += sprintf(cp, "%s %s", vendor, product);
311 cp += sprintf(cp, ", class %d/%d",
312 udd->bDeviceClass, udd->bDeviceSubClass);
313 bcdUSB = UGETW(udd->bcdUSB);
314 bcdDevice = UGETW(udd->bcdDevice);
315 cp += sprintf(cp, ", rev ");
316 cp += usbd_printBCD(cp, bcdUSB);
318 cp += usbd_printBCD(cp, bcdDevice);
319 cp += sprintf(cp, ", addr %d", dev->address);
323 /* Delay for a certain number of ms */
325 usb_delay_ms(usbd_bus_handle bus, u_int ms)
327 /* Wait at least two clock ticks so we know the time has passed. */
328 if (bus->use_polling || cold)
329 delay((ms+1) * 1000);
331 tsleep(&ms, 0, "usbdly", (ms*hz+999)/1000 + 1);
334 /* Delay given a device handle. */
336 usbd_delay_ms(usbd_device_handle dev, u_int ms)
338 usb_delay_ms(dev->bus, ms);
342 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
344 usb_device_request_t req;
348 req.bmRequestType = UT_WRITE_CLASS_OTHER;
349 req.bRequest = UR_SET_FEATURE;
350 USETW(req.wValue, UHF_PORT_RESET);
351 USETW(req.wIndex, port);
352 USETW(req.wLength, 0);
353 err = usbd_do_request(dev, &req, 0);
354 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
355 port, usbd_errstr(err)));
360 /* Wait for device to recover from reset. */
361 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
362 err = usbd_get_port_status(dev, port, ps);
364 DPRINTF(("usbd_reset_port: get status failed %d\n",
368 /* If the device disappeared, just give up. */
369 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
370 return (USBD_NORMAL_COMPLETION);
371 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
373 return (USBD_TIMEOUT);
374 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
377 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
381 /* Wait for the device to recover from reset. */
382 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
386 usb_interface_descriptor_t *
387 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
389 char *p = (char *)cd;
390 char *end = p + UGETW(cd->wTotalLength);
391 usb_interface_descriptor_t *d;
392 int curidx, lastidx, curaidx = 0;
394 for (curidx = lastidx = -1; p < end; ) {
395 d = (usb_interface_descriptor_t *)p;
396 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
398 ifaceidx, curidx, altidx, curaidx,
399 d->bLength, d->bDescriptorType));
400 if (d->bLength == 0) /* bad descriptor */
403 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
404 if (d->bInterfaceNumber != lastidx) {
405 lastidx = d->bInterfaceNumber;
410 if (ifaceidx == curidx && altidx == curaidx)
417 usb_endpoint_descriptor_t *
418 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
421 char *p = (char *)cd;
422 char *end = p + UGETW(cd->wTotalLength);
423 usb_interface_descriptor_t *d;
424 usb_endpoint_descriptor_t *e;
427 d = usbd_find_idesc(cd, ifaceidx, altidx);
430 if (endptidx >= d->bNumEndpoints) /* quick exit */
434 for (p = (char *)d + d->bLength; p < end; ) {
435 e = (usb_endpoint_descriptor_t *)p;
436 if (e->bLength == 0) /* bad descriptor */
439 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
441 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
443 if (curidx == endptidx)
451 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
453 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
454 usb_interface_descriptor_t *idesc;
458 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
460 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
465 ifc->index = ifaceidx;
466 ifc->altindex = altidx;
467 nendpt = ifc->idesc->bNumEndpoints;
468 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
470 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
473 ifc->endpoints = NULL;
476 p = (char *)ifc->idesc + ifc->idesc->bLength;
477 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
478 #define ed ((usb_endpoint_descriptor_t *)p)
479 for (endpt = 0; endpt < nendpt; endpt++) {
480 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
481 for (; p < end; p += ed->bLength) {
482 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
484 p, end, ed->bLength, ed->bDescriptorType));
485 if (p + ed->bLength <= end && ed->bLength != 0 &&
486 ed->bDescriptorType == UDESC_ENDPOINT)
488 if (ed->bLength == 0 ||
489 ed->bDescriptorType == UDESC_INTERFACE)
492 /* passed end, or bad desc */
493 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
494 ed->bLength == 0 ? "0 length" :
495 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
499 ifc->endpoints[endpt].edesc = ed;
500 if (dev->speed == USB_SPEED_HIGH) {
502 /* Control and bulk endpoints have max packet limits. */
503 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
505 mps = USB_2_MAX_CTRL_PACKET;
508 mps = USB_2_MAX_BULK_PACKET;
510 if (UGETW(ed->wMaxPacketSize) != mps) {
511 USETW(ed->wMaxPacketSize, mps);
513 printf("usbd_fill_iface_data: bad max "
522 ifc->endpoints[endpt].refcnt = 0;
526 LIST_INIT(&ifc->pipes);
527 return (USBD_NORMAL_COMPLETION);
530 if (ifc->endpoints != NULL) {
531 free(ifc->endpoints, M_USB);
532 ifc->endpoints = NULL;
538 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
540 usbd_interface_handle ifc = &dev->ifaces[ifcno];
542 free(ifc->endpoints, M_USB);
546 usbd_set_config(usbd_device_handle dev, int conf)
548 usb_device_request_t req;
550 req.bmRequestType = UT_WRITE_DEVICE;
551 req.bRequest = UR_SET_CONFIG;
552 USETW(req.wValue, conf);
553 USETW(req.wIndex, 0);
554 USETW(req.wLength, 0);
555 return (usbd_do_request(dev, &req, 0));
559 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
562 usb_config_descriptor_t cd;
565 if (no == USB_UNCONFIG_NO)
566 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
568 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
569 /* Figure out what config index to use. */
570 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
571 err = usbd_get_config_desc(dev, index, &cd);
574 if (cd.bConfigurationValue == no)
575 return (usbd_set_config_index(dev, index, msg));
581 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
584 usb_config_descriptor_t cd, *cdp;
586 int ifcidx, nifc, len, selfpowered, power;
588 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
590 /* XXX check that all interfaces are idle */
591 if (dev->config != USB_UNCONFIG_NO) {
592 DPRINTF(("usbd_set_config_index: free old config\n"));
593 /* Free all configuration data structures. */
594 nifc = dev->cdesc->bNumInterface;
595 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
596 usbd_free_iface_data(dev, ifcidx);
597 free(dev->ifaces, M_USB);
598 free(dev->cdesc, M_USB);
601 dev->config = USB_UNCONFIG_NO;
604 if (index == USB_UNCONFIG_INDEX) {
605 /* We are unconfiguring the device, so leave unallocated. */
606 DPRINTF(("usbd_set_config_index: set config 0\n"));
607 err = usbd_set_config(dev, USB_UNCONFIG_NO);
609 DPRINTF(("usbd_set_config_index: setting config=0 "
610 "failed, error=%s\n", usbd_errstr(err)));
614 /* Get the short descriptor. */
615 err = usbd_get_config_desc(dev, index, &cd);
618 len = UGETW(cd.wTotalLength);
619 cdp = malloc(len, M_USB, M_INTWAIT);
620 /* Get the full descriptor. */
621 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
624 if (cdp->bDescriptorType != UDESC_CONFIG) {
625 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
626 cdp->bDescriptorType));
631 /* Figure out if the device is self or bus powered. */
633 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
634 (cdp->bmAttributes & UC_SELF_POWERED)) {
635 /* May be self powered. */
636 if (cdp->bmAttributes & UC_BUS_POWERED) {
637 /* Must ask device. */
638 if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
640 * Hub claims to be self powered, but isn't.
641 * It seems that the power status can be
642 * determined by the hub characteristics.
644 usb_hub_descriptor_t hd;
645 usb_device_request_t req;
646 req.bmRequestType = UT_READ_CLASS_DEVICE;
647 req.bRequest = UR_GET_DESCRIPTOR;
648 USETW(req.wValue, 0);
649 USETW(req.wIndex, 0);
650 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
651 err = usbd_do_request(dev, &req, &hd);
653 (UGETW(hd.wHubCharacteristics) &
656 DPRINTF(("usbd_set_config_index: charac=0x%04x"
658 UGETW(hd.wHubCharacteristics),
661 err = usbd_get_device_status(dev, &ds);
663 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
665 DPRINTF(("usbd_set_config_index: status=0x%04x"
667 UGETW(ds.wStatus), usbd_errstr(err)));
672 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
673 "selfpowered=%d, power=%d\n",
674 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
675 selfpowered, cdp->bMaxPower * 2));
677 /* Check if we have enough power. */
679 if (dev->powersrc == NULL) {
680 DPRINTF(("usbd_set_config_index: No power source?\n"));
681 return (USBD_IOERROR);
684 power = cdp->bMaxPower * 2;
685 if (power > dev->powersrc->power) {
686 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
687 /* XXX print nicer message. */
689 printf("%s: device addr %d (config %d) exceeds power "
690 "budget, %d mA > %d mA\n",
691 USBDEVNAME(dev->bus->bdev), dev->address,
692 cdp->bConfigurationValue,
693 power, dev->powersrc->power);
698 dev->self_powered = selfpowered;
700 /* Set the actual configuration value. */
701 DPRINTF(("usbd_set_config_index: set config %d\n",
702 cdp->bConfigurationValue));
703 err = usbd_set_config(dev, cdp->bConfigurationValue);
705 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
707 cdp->bConfigurationValue, usbd_errstr(err)));
711 /* Allocate and fill interface data. */
712 nifc = cdp->bNumInterface;
713 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
715 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
717 dev->config = cdp->bConfigurationValue;
718 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
719 err = usbd_fill_iface_data(dev, ifcidx, 0);
721 while (--ifcidx >= 0)
722 usbd_free_iface_data(dev, ifcidx);
727 return (USBD_NORMAL_COMPLETION);
734 /* XXX add function for alternate settings */
737 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
738 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
743 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
744 dev, iface, ep, pipe));
745 p = malloc(dev->bus->pipe_size, M_USB, M_INTWAIT);
756 SIMPLEQ_INIT(&p->queue);
757 err = dev->bus->methods->open_pipe(p);
759 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
761 ep->edesc->bEndpointAddress, usbd_errstr(err)));
765 /* Clear any stall and make sure DATA0 toggle will be used next. */
766 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT)
767 usbd_clear_endpoint_stall(p);
769 return (USBD_NORMAL_COMPLETION);
772 /* Abort the device control pipe. */
774 usbd_kill_pipe(usbd_pipe_handle pipe)
776 usbd_abort_pipe(pipe);
777 pipe->methods->close(pipe);
778 pipe->endpoint->refcnt--;
783 usbd_getnewaddr(usbd_bus_handle bus)
787 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
788 if (bus->devices[addr] == 0)
795 usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
798 struct usb_attach_arg uaa;
799 usb_device_descriptor_t *dd = &dev->ddesc;
800 int found, i, confi, nifaces;
803 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
805 #if defined(__FreeBSD__) || defined(__DragonFly__)
807 * XXX uaa is a static var. Not a problem as it _should_ be used only
808 * during probe and attach. Should be changed however.
811 bdev = device_add_child(parent, NULL, -1);
813 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
816 device_set_ivars(bdev, &uaa);
826 uaa.configno = UHUB_UNK_CONFIGURATION;
827 uaa.ifaceno = UHUB_UNK_INTERFACE;
828 uaa.vendor = UGETW(dd->idVendor);
829 uaa.product = UGETW(dd->idProduct);
830 uaa.release = UGETW(dd->bcdDevice);
832 /* First try with device specific drivers. */
833 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
834 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
836 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_INTWAIT);
837 dev->subdevs[0] = dv;
839 return (USBD_NORMAL_COMPLETION);
842 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
844 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
845 dd->bNumConfigurations));
846 /* Next try with interface drivers. */
847 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
848 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
850 err = usbd_set_config_index(dev, confi, 1);
853 DPRINTF(("%s: port %d, set config at addr %d failed, "
854 "error=%s\n", USBDEVPTRNAME(parent), port,
855 addr, usbd_errstr(err)));
857 printf("%s: port %d, set config at addr %d failed\n",
858 USBDEVPTRNAME(parent), port, addr);
860 #if defined(__FreeBSD__) || defined(__DragonFly__)
861 device_delete_child(parent, bdev);
866 nifaces = dev->cdesc->bNumInterface;
867 uaa.configno = dev->cdesc->bConfigurationValue;
868 for (i = 0; i < nifaces; i++)
869 ifaces[i] = &dev->ifaces[i];
871 uaa.nifaces = nifaces;
872 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB, M_INTWAIT);
875 for (i = 0; i < nifaces; i++) {
876 if (ifaces[i] == NULL)
877 continue; /* interface already claimed */
878 uaa.iface = ifaces[i];
879 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
880 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
883 dev->subdevs[found++] = dv;
884 dev->subdevs[found] = 0;
885 ifaces[i] = 0; /* consumed */
887 #if defined(__FreeBSD__) || defined(__DragonFly__)
888 /* create another child for the next iface */
889 bdev = device_add_child(parent, NULL, -1);
891 printf("%s: Device creation failed\n",
892 USBDEVNAME(dev->bus->bdev));
893 return (USBD_NORMAL_COMPLETION);
895 device_set_ivars(bdev, &uaa);
901 #if defined(__FreeBSD__) || defined(__DragonFly__)
902 /* remove the last created child again; it is unused */
903 device_delete_child(parent, bdev);
905 return (USBD_NORMAL_COMPLETION);
907 free(dev->subdevs, M_USB);
910 /* No interfaces were attached in any of the configurations. */
912 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
913 usbd_set_config_index(dev, 0, 0);
915 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
917 /* Finally try the generic driver. */
920 uaa.configno = UHUB_UNK_CONFIGURATION;
921 uaa.ifaceno = UHUB_UNK_INTERFACE;
922 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
924 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_INTWAIT);
925 dev->subdevs[0] = dv;
927 return (USBD_NORMAL_COMPLETION);
931 * The generic attach failed, but leave the device as it is.
932 * We just did not find any drivers, that's all. The device is
933 * fully operational and not harming anyone.
935 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
936 #if defined(__FreeBSD__) || defined(__DragonFly__)
937 device_delete_child(parent, bdev);
939 return (USBD_NORMAL_COMPLETION);
944 * Called when a new device has been put in the powered state,
945 * but not yet in the addressed state.
946 * Get initial descriptor, set the address, get full descriptor,
947 * and attach a driver.
950 usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
951 int speed, int port, struct usbd_port *up)
953 usbd_device_handle dev;
954 struct usbd_device *hub;
955 usb_device_descriptor_t *dd;
956 usb_port_status_t ps;
961 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
962 bus, port, depth, speed));
963 addr = usbd_getnewaddr(bus);
965 printf("%s: No free USB addresses, new device ignored.\n",
966 USBDEVNAME(bus->bdev));
967 return (USBD_NO_ADDR);
970 dev = malloc(sizeof *dev, M_USB, M_INTWAIT | M_ZERO);
973 /* Set up default endpoint handle. */
974 dev->def_ep.edesc = &dev->def_ep_desc;
976 /* Set up default endpoint descriptor. */
977 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
978 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
979 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
980 dev->def_ep_desc.bmAttributes = UE_CONTROL;
981 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
982 dev->def_ep_desc.bInterval = 0;
984 dev->quirks = &usbd_no_quirk;
985 dev->address = USB_START_ADDR;
986 dev->ddesc.bMaxPacketSize = 0;
989 dev->myhub = up->parent;
990 for (hub = up->parent;
991 hub != NULL && hub->speed != USB_SPEED_HIGH;
994 dev->myhighhub = hub;
996 dev->langid = USBD_NOLANG;
997 dev->cookie.cookie = ++usb_cookie_no;
999 /* Establish the default pipe. */
1000 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1001 &dev->default_pipe);
1003 usbd_remove_device(dev, up);
1009 /* Set the address. Do this early; some devices need that. */
1010 err = usbd_set_address(dev, addr);
1011 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1013 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1014 err = USBD_SET_ADDR_FAILED;
1015 usbd_remove_device(dev, up);
1018 /* Allow device time to set new address */
1019 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1020 dev->address = addr; /* New device address now */
1021 bus->devices[addr] = dev;
1024 /* Try a few times in case the device is slow (i.e. outside specs.) */
1025 for (i = 0; i < 15; i++) {
1026 /* Get the first 8 bytes of the device descriptor. */
1027 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1030 usbd_delay_ms(dev, 200);
1032 usbd_reset_port(up->parent, port, &ps);
1035 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1037 usbd_remove_device(dev, up);
1041 if (speed == USB_SPEED_HIGH) {
1042 /* Max packet size must be 64 (sec 5.5.3). */
1043 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1045 printf("usbd_new_device: addr=%d bad max packet size\n",
1048 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1052 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1053 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1054 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1055 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1058 if (dd->bDescriptorType != UDESC_DEVICE) {
1059 /* Illegal device descriptor */
1060 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1061 dd->bDescriptorType));
1062 usbd_remove_device(dev, up);
1063 return (USBD_INVAL);
1066 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1067 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1068 usbd_remove_device(dev, up);
1069 return (USBD_INVAL);
1072 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1074 err = usbd_reload_device_desc(dev);
1076 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1078 usbd_remove_device(dev, up);
1082 /* Assume 100mA bus powered for now. Changed when configured. */
1083 dev->power = USB_MIN_POWER;
1084 dev->self_powered = 0;
1086 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1087 addr, dev, parent));
1089 err = usbd_probe_and_attach(parent, dev, port, addr);
1091 usbd_remove_device(dev, up);
1095 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1097 return (USBD_NORMAL_COMPLETION);
1101 usbd_reload_device_desc(usbd_device_handle dev)
1103 usbd_status err = 0;
1106 /* Get the full device descriptor. */
1107 for (i = 0; i < 3; ++i) {
1108 err = usbd_get_device_desc(dev, &dev->ddesc);
1111 usbd_delay_ms(dev, 200);
1116 /* Figure out what's wrong with this device. */
1117 dev->quirks = usbd_find_quirk(&dev->ddesc);
1119 return (USBD_NORMAL_COMPLETION);
1123 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1125 DPRINTF(("usbd_remove_device: %p\n", dev));
1127 if (dev->default_pipe != NULL)
1128 usbd_kill_pipe(dev->default_pipe);
1130 dev->bus->devices[dev->address] = 0;
1135 #if defined(__NetBSD__) || defined(__OpenBSD__)
1137 usbd_print(void *aux, const char *pnp)
1139 struct usb_attach_arg *uaa = aux;
1142 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1144 if (!uaa->usegeneric)
1146 usbd_devinfo(uaa->device, 1, devinfo);
1147 printf("%s, %s", devinfo, pnp);
1150 printf(" port %d", uaa->port);
1151 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1152 printf(" configuration %d", uaa->configno);
1153 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1154 printf(" interface %d", uaa->ifaceno);
1157 * It gets very crowded with these locators on the attach line.
1158 * They are not really needed since they are printed in the clear
1161 if (uaa->vendor != UHUB_UNK_VENDOR)
1162 printf(" vendor 0x%04x", uaa->vendor);
1163 if (uaa->product != UHUB_UNK_PRODUCT)
1164 printf(" product 0x%04x", uaa->product);
1165 if (uaa->release != UHUB_UNK_RELEASE)
1166 printf(" release 0x%04x", uaa->release);
1171 #if defined(__NetBSD__)
1173 usbd_submatch(struct device *parent, struct cfdata *cf, void *aux)
1175 #elif defined(__OpenBSD__)
1177 usbd_submatch(struct device *parent, void *match, void *aux)
1179 struct cfdata *cf = match;
1181 struct usb_attach_arg *uaa = aux;
1183 DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1184 "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1185 uaa->port, cf->uhubcf_port,
1186 uaa->configno, cf->uhubcf_configuration,
1187 uaa->ifaceno, cf->uhubcf_interface,
1188 uaa->vendor, cf->uhubcf_vendor,
1189 uaa->product, cf->uhubcf_product,
1190 uaa->release, cf->uhubcf_release));
1191 if (uaa->port != 0 && /* root hub has port 0, it should match */
1193 cf->uhubcf_port != UHUB_UNK_PORT &&
1194 cf->uhubcf_port != uaa->port) ||
1195 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1196 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1197 cf->uhubcf_configuration != uaa->configno) ||
1198 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1199 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1200 cf->uhubcf_interface != uaa->ifaceno) ||
1201 (uaa->vendor != UHUB_UNK_VENDOR &&
1202 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1203 cf->uhubcf_vendor != uaa->vendor) ||
1204 (uaa->product != UHUB_UNK_PRODUCT &&
1205 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1206 cf->uhubcf_product != uaa->product) ||
1207 (uaa->release != UHUB_UNK_RELEASE &&
1208 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1209 cf->uhubcf_release != uaa->release)
1213 if (cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1214 cf->uhubcf_vendor == uaa->vendor &&
1215 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1216 cf->uhubcf_product == uaa->product) {
1217 /* We have a vendor&product locator match */
1218 if (cf->uhubcf_release != UHUB_UNK_RELEASE &&
1219 cf->uhubcf_release == uaa->release)
1220 uaa->matchlvl = UMATCH_VENDOR_PRODUCT_REV;
1222 uaa->matchlvl = UMATCH_VENDOR_PRODUCT;
1225 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1231 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1234 struct usbd_port *p;
1237 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1238 di->udi_addr = dev->address;
1239 di->udi_cookie = dev->cookie;
1240 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
1241 usbd_printBCD(di->udi_release, UGETW(dev->ddesc.bcdDevice));
1242 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1243 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1244 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1245 di->udi_class = dev->ddesc.bDeviceClass;
1246 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1247 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1248 di->udi_config = dev->config;
1249 di->udi_power = dev->self_powered ? 0 : dev->power;
1250 di->udi_speed = dev->speed;
1252 if (dev->subdevs != NULL) {
1253 for (i = 0; dev->subdevs[i] &&
1254 i < USB_MAX_DEVNAMES; i++) {
1255 strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1256 USB_MAX_DEVNAMELEN);
1257 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
1262 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1263 di->udi_devnames[i][0] = 0; /* empty */
1267 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1268 i < dev->hub->hubdesc.bNbrPorts;
1270 p = &dev->hub->ports[i];
1272 err = p->device->address;
1274 s = UGETW(p->status.wPortStatus);
1275 if (s & UPS_PORT_ENABLED)
1276 err = USB_PORT_ENABLED;
1277 else if (s & UPS_SUSPEND)
1278 err = USB_PORT_SUSPENDED;
1279 else if (s & UPS_PORT_POWER)
1280 err = USB_PORT_POWERED;
1282 err = USB_PORT_DISABLED;
1284 di->udi_ports[i] = err;
1286 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1292 usb_free_device(usbd_device_handle dev)
1296 if (dev->default_pipe != NULL)
1297 usbd_kill_pipe(dev->default_pipe);
1298 if (dev->ifaces != NULL) {
1299 nifc = dev->cdesc->bNumInterface;
1300 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1301 usbd_free_iface_data(dev, ifcidx);
1302 free(dev->ifaces, M_USB);
1304 if (dev->cdesc != NULL)
1305 free(dev->cdesc, M_USB);
1306 if (dev->subdevs != NULL)
1307 free(dev->subdevs, M_USB);
1312 * The general mechanism for detaching drivers works as follows: Each
1313 * driver is responsible for maintaining a reference count on the
1314 * number of outstanding references to its softc (e.g. from
1315 * processing hanging in a read or write). The detach method of the
1316 * driver decrements this counter and flags in the softc that the
1317 * driver is dying and then wakes any sleepers. It then sleeps on the
1318 * softc. Each place that can sleep must maintain the reference
1319 * count. When the reference count drops to -1 (0 is the normal value
1320 * of the reference count) the a wakeup on the softc is performed
1321 * signaling to the detach waiter that all references are gone.
1325 * Called from process context when we discover that a port has
1326 * been disconnected.
1329 usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1331 usbd_device_handle dev = up->device;
1332 const char *hubname = USBDEVPTRNAME(parent);
1335 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1336 up, dev, up->portno));
1340 printf("usb_disconnect_port: no device\n");
1345 if (dev->subdevs != NULL) {
1346 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1347 for (i = 0; dev->subdevs[i]; i++) {
1348 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1350 if (up->portno != 0)
1351 printf(" port %d", up->portno);
1352 printf(" (addr %d) disconnected\n", dev->address);
1353 config_detach(dev->subdevs[i], DETACH_FORCE);
1354 dev->subdevs[i] = NULL;
1358 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1359 dev->bus->devices[dev->address] = NULL;
1361 usb_free_device(dev);
1365 void *usb_realloc(void *p, u_int size, int pool, int flags)
1369 q = malloc(size, pool, flags);