1 /* $NetBSD: usb_subr.c,v 1.99 2002/07/11 21:14:34 augustss Exp $ */
3 /* Also already have from NetBSD:
4 * $NetBSD: usb_subr.c,v 1.102 2003/01/01 16:21:50 augustss Exp $
5 * $NetBSD: usb_subr.c,v 1.103 2003/01/10 11:19:13 augustss Exp $
6 * $NetBSD: usb_subr.c,v 1.111 2004/03/15 10:35:04 augustss Exp $
7 * $NetBSD: usb_subr.c,v 1.114 2004/06/23 02:30:52 mycroft Exp $
8 * $NetBSD: usb_subr.c,v 1.115 2004/06/23 05:23:19 mycroft Exp $
9 * $NetBSD: usb_subr.c,v 1.116 2004/06/23 06:27:54 mycroft Exp $
10 * $NetBSD: usb_subr.c,v 1.119 2004/10/23 13:26:33 augustss Exp $
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
17 * Copyright (c) 1998 The NetBSD Foundation, Inc.
18 * All rights reserved.
20 * This code is derived from software contributed to The NetBSD Foundation
21 * by Lennart Augustsson (lennart@augustsson.net) at
22 * Carlstedt Research & Technology.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the NetBSD
35 * Foundation, Inc. and its contributors.
36 * 4. Neither the name of The NetBSD Foundation nor the names of its
37 * contributors may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
41 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
44 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50 * POSSIBILITY OF SUCH DAMAGE.
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/module.h>
62 #include <sys/sysctl.h>
64 #include <machine/bus.h>
66 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdi_util.h>
70 #include <dev/usb/usbdivar.h>
72 #include <dev/usb/usb_quirks.h>
74 #define delay(d) DELAY(d)
77 #define DPRINTF(x) if (usbdebug) logprintf x
78 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
85 static usbd_status usbd_set_config(usbd_device_handle, int);
86 static void usbd_devinfo_vp(usbd_device_handle, char *, char *, int);
87 static int usbd_getnewaddr(usbd_bus_handle bus);
88 static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
89 static void usbd_kill_pipe(usbd_pipe_handle);
90 static usbd_status usbd_probe_and_attach(device_t parent,
91 usbd_device_handle dev, int port, int addr);
93 static u_int32_t usb_cookie_no = 0;
96 typedef u_int16_t usb_vendor_id_t;
97 typedef u_int16_t usb_product_id_t;
100 * Descriptions of of known vendors and devices ("products").
102 struct usb_knowndev {
103 usb_vendor_id_t vendor;
104 usb_product_id_t product;
106 char *vendorname, *productname;
108 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
110 #include "usbdevs_data.h"
111 #endif /* USBVERBOSE */
113 static const char * const usbd_error_strs[] = {
137 usbd_errstr(usbd_status err)
139 static char buffer[5];
141 if (err < USBD_ERROR_MAX) {
142 return usbd_error_strs[err];
144 snprintf(buffer, sizeof buffer, "%d", err);
150 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
151 usb_string_descriptor_t *sdesc, int *sizep)
153 usb_device_request_t req;
157 req.bmRequestType = UT_READ_DEVICE;
158 req.bRequest = UR_GET_DESCRIPTOR;
159 USETW2(req.wValue, UDESC_STRING, sindex);
160 USETW(req.wIndex, langid);
161 USETW(req.wLength, 2); /* only size byte first */
162 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
163 &actlen, USBD_DEFAULT_TIMEOUT);
168 return (USBD_SHORT_XFER);
170 USETW(req.wLength, sdesc->bLength); /* the whole string */
171 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
172 &actlen, USBD_DEFAULT_TIMEOUT);
176 if (actlen != sdesc->bLength) {
177 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
178 sdesc->bLength, actlen));
182 return (USBD_NORMAL_COMPLETION);
186 usbd_trim_spaces(char *p)
193 while (*q == ' ') /* skip leading spaces */
195 while ((*p = *q++)) /* copy string */
196 if (*p++ != ' ') /* remember last non-space */
198 *e = 0; /* kill trailing spaces */
202 usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev)
204 usb_device_descriptor_t *udd = &dev->ddesc;
205 char *vendor = 0, *product = 0;
207 const struct usb_knowndev *kdp;
216 if (usbd_get_string(dev, udd->iManufacturer, v))
220 usbd_trim_spaces(vendor);
221 if (usbd_get_string(dev, udd->iProduct, p))
225 usbd_trim_spaces(product);
226 if (vendor && !*vendor)
228 if (product && !*product)
235 if (vendor == NULL || product == NULL) {
236 for(kdp = usb_knowndevs;
237 kdp->vendorname != NULL;
239 if (kdp->vendor == UGETW(udd->idVendor) &&
240 (kdp->product == UGETW(udd->idProduct) ||
241 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
244 if (kdp->vendorname != NULL) {
246 vendor = kdp->vendorname;
248 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
249 kdp->productname : NULL;
253 if (vendor != NULL && *vendor)
256 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
257 if (product != NULL && *product)
260 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
264 usbd_printBCD(char *cp, int bcd)
266 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
270 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp)
272 usb_device_descriptor_t *udd = &dev->ddesc;
273 usbd_interface_handle iface;
274 char vendor[USB_MAX_STRING_LEN];
275 char product[USB_MAX_STRING_LEN];
276 int bcdDevice, bcdUSB;
277 usb_interface_descriptor_t *id;
279 usbd_devinfo_vp(dev, vendor, product, 1);
280 cp += sprintf(cp, "%s %s", vendor, product);
281 if (showclass & USBD_SHOW_DEVICE_CLASS)
282 cp += sprintf(cp, ", class %d/%d",
283 udd->bDeviceClass, udd->bDeviceSubClass);
284 bcdUSB = UGETW(udd->bcdUSB);
285 bcdDevice = UGETW(udd->bcdDevice);
286 cp += sprintf(cp, ", rev ");
287 cp += usbd_printBCD(cp, bcdUSB);
289 cp += usbd_printBCD(cp, bcdDevice);
290 cp += sprintf(cp, ", addr %d", dev->address);
291 if (showclass & USBD_SHOW_INTERFACE_CLASS)
293 /* fetch the interface handle for the first interface */
294 (void)usbd_device2interface_handle(dev, 0, &iface);
295 id = usbd_get_interface_descriptor(iface);
296 cp += sprintf(cp, ", iclass %d/%d",
297 id->bInterfaceClass, id->bInterfaceSubClass);
302 /* Delay for a certain number of ms */
304 usb_delay_ms(usbd_bus_handle bus, u_int ms)
306 /* Wait at least two clock ticks so we know the time has passed. */
307 if (bus->use_polling || cold)
308 delay((ms+1) * 1000);
310 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
313 /* Delay given a device handle. */
315 usbd_delay_ms(usbd_device_handle dev, u_int ms)
317 usb_delay_ms(dev->bus, ms);
321 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
323 usb_device_request_t req;
327 req.bmRequestType = UT_WRITE_CLASS_OTHER;
328 req.bRequest = UR_SET_FEATURE;
329 USETW(req.wValue, UHF_PORT_RESET);
330 USETW(req.wIndex, port);
331 USETW(req.wLength, 0);
332 err = usbd_do_request(dev, &req, 0);
333 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
334 port, usbd_errstr(err)));
339 /* Wait for device to recover from reset. */
340 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
341 err = usbd_get_port_status(dev, port, ps);
343 DPRINTF(("usbd_reset_port: get status failed %d\n",
347 /* If the device disappeared, just give up. */
348 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
349 return (USBD_NORMAL_COMPLETION);
350 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
352 return (USBD_TIMEOUT);
353 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
356 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
360 /* Wait for the device to recover from reset. */
361 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
365 usb_interface_descriptor_t *
366 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
368 char *p = (char *)cd;
369 char *end = p + UGETW(cd->wTotalLength);
370 usb_interface_descriptor_t *d;
371 int curidx, lastidx, curaidx = 0;
373 for (curidx = lastidx = -1; p < end; ) {
374 d = (usb_interface_descriptor_t *)p;
375 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
377 ifaceidx, curidx, altidx, curaidx,
378 d->bLength, d->bDescriptorType));
379 if (d->bLength == 0) /* bad descriptor */
382 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
383 if (d->bInterfaceNumber != lastidx) {
384 lastidx = d->bInterfaceNumber;
389 if (ifaceidx == curidx && altidx == curaidx)
396 usb_endpoint_descriptor_t *
397 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
400 char *p = (char *)cd;
401 char *end = p + UGETW(cd->wTotalLength);
402 usb_interface_descriptor_t *d;
403 usb_endpoint_descriptor_t *e;
406 d = usbd_find_idesc(cd, ifaceidx, altidx);
409 if (endptidx >= d->bNumEndpoints) /* quick exit */
413 for (p = (char *)d + d->bLength; p < end; ) {
414 e = (usb_endpoint_descriptor_t *)p;
415 if (e->bLength == 0) /* bad descriptor */
418 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
420 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
422 if (curidx == endptidx)
430 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
432 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
433 usb_interface_descriptor_t *idesc;
437 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
439 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
444 ifc->index = ifaceidx;
445 ifc->altindex = altidx;
446 nendpt = ifc->idesc->bNumEndpoints;
447 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
449 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
451 if (ifc->endpoints == NULL)
454 ifc->endpoints = NULL;
456 p = (char *)ifc->idesc + ifc->idesc->bLength;
457 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
458 #define ed ((usb_endpoint_descriptor_t *)p)
459 for (endpt = 0; endpt < nendpt; endpt++) {
460 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
461 for (; p < end; p += ed->bLength) {
462 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
464 p, end, ed->bLength, ed->bDescriptorType));
465 if (p + ed->bLength <= end && ed->bLength != 0 &&
466 ed->bDescriptorType == UDESC_ENDPOINT)
468 if (ed->bLength == 0 ||
469 ed->bDescriptorType == UDESC_INTERFACE)
472 /* passed end, or bad desc */
473 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
474 ed->bLength == 0 ? "0 length" :
475 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
479 ifc->endpoints[endpt].edesc = ed;
480 if (dev->speed == USB_SPEED_HIGH) {
482 /* Control and bulk endpoints have max packet limits. */
483 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
485 mps = USB_2_MAX_CTRL_PACKET;
488 mps = USB_2_MAX_BULK_PACKET;
490 if (UGETW(ed->wMaxPacketSize) != mps) {
491 USETW(ed->wMaxPacketSize, mps);
493 printf("usbd_fill_iface_data: bad max "
502 ifc->endpoints[endpt].refcnt = 0;
503 ifc->endpoints[endpt].savedtoggle = 0;
507 LIST_INIT(&ifc->pipes);
508 return (USBD_NORMAL_COMPLETION);
511 if (ifc->endpoints != NULL) {
512 free(ifc->endpoints, M_USB);
513 ifc->endpoints = NULL;
519 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
521 usbd_interface_handle ifc = &dev->ifaces[ifcno];
523 free(ifc->endpoints, M_USB);
527 usbd_set_config(usbd_device_handle dev, int conf)
529 usb_device_request_t req;
531 req.bmRequestType = UT_WRITE_DEVICE;
532 req.bRequest = UR_SET_CONFIG;
533 USETW(req.wValue, conf);
534 USETW(req.wIndex, 0);
535 USETW(req.wLength, 0);
536 return (usbd_do_request(dev, &req, 0));
540 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
543 usb_config_descriptor_t cd;
546 if (no == USB_UNCONFIG_NO)
547 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
549 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
550 /* Figure out what config index to use. */
551 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
552 err = usbd_get_config_desc(dev, index, &cd);
555 if (cd.bConfigurationValue == no)
556 return (usbd_set_config_index(dev, index, msg));
562 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
565 usb_config_descriptor_t cd, *cdp;
567 int i, ifcidx, nifc, len, selfpowered, power;
569 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
571 if (dev->config != USB_UNCONFIG_NO) {
572 nifc = dev->cdesc->bNumInterface;
574 /* Check that all interfaces are idle */
575 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
576 if (LIST_EMPTY(&dev->ifaces[ifcidx].pipes))
578 DPRINTF(("usbd_set_config_index: open pipes exist\n"));
579 return (USBD_IN_USE);
582 DPRINTF(("usbd_set_config_index: free old config\n"));
583 /* Free all configuration data structures. */
584 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
585 usbd_free_iface_data(dev, ifcidx);
586 free(dev->ifaces, M_USB);
587 free(dev->cdesc, M_USB);
590 dev->config = USB_UNCONFIG_NO;
593 if (index == USB_UNCONFIG_INDEX) {
594 /* We are unconfiguring the device, so leave unallocated. */
595 DPRINTF(("usbd_set_config_index: set config 0\n"));
596 err = usbd_set_config(dev, USB_UNCONFIG_NO);
598 DPRINTF(("usbd_set_config_index: setting config=0 "
599 "failed, error=%s\n", usbd_errstr(err)));
603 /* Get the short descriptor. */
604 err = usbd_get_config_desc(dev, index, &cd);
607 len = UGETW(cd.wTotalLength);
608 cdp = malloc(len, M_USB, M_NOWAIT);
612 /* Get the full descriptor. Try a few times for slow devices. */
613 for (i = 0; i < 3; i++) {
614 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
617 usbd_delay_ms(dev, 200);
621 if (cdp->bDescriptorType != UDESC_CONFIG) {
622 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
623 cdp->bDescriptorType));
628 /* Figure out if the device is self or bus powered. */
630 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
631 (cdp->bmAttributes & UC_SELF_POWERED)) {
632 /* May be self powered. */
633 if (cdp->bmAttributes & UC_BUS_POWERED) {
634 /* Must ask device. */
635 if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
637 * Hub claims to be self powered, but isn't.
638 * It seems that the power status can be
639 * determined by the hub characteristics.
641 usb_hub_descriptor_t hd;
642 usb_device_request_t req;
643 req.bmRequestType = UT_READ_CLASS_DEVICE;
644 req.bRequest = UR_GET_DESCRIPTOR;
645 USETW(req.wValue, 0);
646 USETW(req.wIndex, 0);
647 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
648 err = usbd_do_request(dev, &req, &hd);
650 (UGETW(hd.wHubCharacteristics) &
653 DPRINTF(("usbd_set_config_index: charac=0x%04x"
655 UGETW(hd.wHubCharacteristics),
658 err = usbd_get_device_status(dev, &ds);
660 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
662 DPRINTF(("usbd_set_config_index: status=0x%04x"
664 UGETW(ds.wStatus), usbd_errstr(err)));
669 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
670 "selfpowered=%d, power=%d\n",
671 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
672 selfpowered, cdp->bMaxPower * 2));
674 /* Check if we have enough power. */
676 if (dev->powersrc == NULL) {
677 DPRINTF(("usbd_set_config_index: No power source?\n"));
678 return (USBD_IOERROR);
681 power = cdp->bMaxPower * 2;
682 if (power > dev->powersrc->power) {
683 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
684 /* XXX print nicer message. */
686 printf("%s: device addr %d (config %d) exceeds power "
687 "budget, %d mA > %d mA\n",
688 device_get_nameunit(dev->bus->bdev), dev->address,
689 cdp->bConfigurationValue,
690 power, dev->powersrc->power);
695 dev->self_powered = selfpowered;
697 /* Set the actual configuration value. */
698 DPRINTF(("usbd_set_config_index: set config %d\n",
699 cdp->bConfigurationValue));
700 err = usbd_set_config(dev, cdp->bConfigurationValue);
702 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
704 cdp->bConfigurationValue, usbd_errstr(err)));
708 /* Allocate and fill interface data. */
709 nifc = cdp->bNumInterface;
710 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
712 if (dev->ifaces == NULL) {
716 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
718 dev->config = cdp->bConfigurationValue;
719 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
720 err = usbd_fill_iface_data(dev, ifcidx, 0);
722 while (--ifcidx >= 0)
723 usbd_free_iface_data(dev, ifcidx);
728 return (USBD_NORMAL_COMPLETION);
735 /* XXX add function for alternate settings */
738 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
739 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
744 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
745 dev, iface, ep, pipe));
746 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
759 STAILQ_INIT(&p->queue);
760 err = dev->bus->methods->open_pipe(p);
762 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
764 ep->edesc->bEndpointAddress, usbd_errstr(err)));
769 if (dev->quirks->uq_flags & UQ_OPEN_CLEARSTALL) {
770 /* Clear any stall and make sure DATA0 toggle will be used next. */
771 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT) {
772 err = usbd_clear_endpoint_stall(p);
773 if (err && err != USBD_STALLED && err != USBD_TIMEOUT) {
774 printf("usbd_setup_pipe: failed to start "
775 "endpoint, %s\n", usbd_errstr(err));
782 return (USBD_NORMAL_COMPLETION);
785 /* Abort the device control pipe. */
787 usbd_kill_pipe(usbd_pipe_handle pipe)
789 usbd_abort_pipe(pipe);
790 pipe->methods->close(pipe);
791 pipe->endpoint->refcnt--;
796 usbd_getnewaddr(usbd_bus_handle bus)
800 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
801 if (bus->devices[addr] == 0)
808 usbd_probe_and_attach(device_t parent, usbd_device_handle dev,
811 struct usb_attach_arg uaa;
812 usb_device_descriptor_t *dd = &dev->ddesc;
813 int found, i, confi, nifaces;
817 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
820 #if defined(__FreeBSD__)
821 /* XXX FreeBSD may leak resources on failure cases -- fixme */
823 struct usb_attach_arg *uaap;
825 devinfo = malloc(1024, M_USB, M_NOWAIT);
826 if (devinfo == NULL) {
827 device_printf(parent, "Can't allocate memory for probe string\n");
830 bdev = device_add_child(parent, NULL, -1);
832 free(devinfo, M_USB);
833 device_printf(parent, "Device creation failed\n");
836 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
838 free(devinfo, M_USB);
841 device_set_ivars(bdev, uaap);
849 uaa.configno = UHUB_UNK_CONFIGURATION;
850 uaa.ifaceno = UHUB_UNK_INTERFACE;
851 uaa.vendor = UGETW(dd->idVendor);
852 uaa.product = UGETW(dd->idProduct);
853 uaa.release = UGETW(dd->bcdDevice);
855 /* First try with device specific drivers. */
856 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
858 dev->ifacenums = NULL;
859 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
860 if (dev->subdevs == NULL) {
861 free(devinfo, M_USB);
864 dev->subdevs[0] = bdev;
867 usbd_devinfo(dev, 1, devinfo);
868 device_set_desc_copy(bdev, devinfo);
869 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
871 free(devinfo, M_USB);
872 return (USBD_NORMAL_COMPLETION);
875 * Free subdevs so we can reallocate it larger for the number of
878 tmpdv = dev->subdevs;
881 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
883 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
884 dd->bNumConfigurations));
885 /* Next try with interface drivers. */
886 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
887 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
889 err = usbd_set_config_index(dev, confi, 1);
892 DPRINTF(("%s: port %d, set config at addr %d failed, "
893 "error=%s\n", device_get_nameunit(parent), port,
894 addr, usbd_errstr(err)));
896 printf("%s: port %d, set config at addr %d failed\n",
897 device_get_nameunit(parent), port, addr);
899 free(devinfo, M_USB);
902 nifaces = dev->cdesc->bNumInterface;
903 uaa.configno = dev->cdesc->bConfigurationValue;
904 for (i = 0; i < nifaces; i++)
905 ifaces[i] = &dev->ifaces[i];
907 uaa.nifaces = nifaces;
908 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
909 if (dev->subdevs == NULL) {
910 free(devinfo, M_USB);
913 dev->ifacenums = malloc((nifaces) * sizeof(*dev->ifacenums),
915 if (dev->ifacenums == NULL) {
916 free(devinfo, M_USB);
921 for (i = 0; i < nifaces; i++) {
922 if (ifaces[i] == NULL)
923 continue; /* interface already claimed */
924 uaa.iface = ifaces[i];
925 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
926 dev->subdevs[found] = bdev;
927 dev->subdevs[found + 1] = 0;
928 dev->ifacenums[found] = i;
930 usbd_devinfo(dev, 1, devinfo);
931 device_set_desc_copy(bdev, devinfo);
932 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
935 ifaces[i] = 0; /* consumed */
937 /* create another child for the next iface */
938 bdev = device_add_child(parent, NULL, -1);
940 device_printf(parent,
941 "Device add failed\n");
942 free(devinfo, M_USB);
943 return (USBD_NORMAL_COMPLETION);
945 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
949 device_set_ivars(bdev, uaap);
951 dev->subdevs[found] = 0;
955 /* remove the last created child. It is unused */
957 free(devinfo, M_USB);
958 device_delete_child(parent, bdev);
959 /* free(uaap, M_USB); */ /* May be needed? xxx */
960 return (USBD_NORMAL_COMPLETION);
962 tmpdv = dev->subdevs;
965 free(dev->ifacenums, M_USB);
966 dev->ifacenums = NULL;
968 /* No interfaces were attached in any of the configurations. */
970 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
971 usbd_set_config_index(dev, 0, 0);
973 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
975 /* Finally try the generic driver. */
978 uaa.configno = UHUB_UNK_CONFIGURATION;
979 uaa.ifaceno = UHUB_UNK_INTERFACE;
980 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
981 if (dev->subdevs == 0) {
982 free(devinfo, M_USB);
985 dev->subdevs[0] = bdev;
988 usbd_devinfo(dev, 1, devinfo);
989 device_set_desc_copy(bdev, devinfo);
990 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
992 return (USBD_NORMAL_COMPLETION);
995 * The generic attach failed, but leave the device as it is.
996 * We just did not find any drivers, that's all. The device is
997 * fully operational and not harming anyone.
999 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
1000 return (USBD_NORMAL_COMPLETION);
1005 * Called when a new device has been put in the powered state,
1006 * but not yet in the addressed state.
1007 * Get initial descriptor, set the address, get full descriptor,
1008 * and attach a driver.
1011 usbd_new_device(device_t parent, usbd_bus_handle bus, int depth,
1012 int speed, int port, struct usbd_port *up)
1014 usbd_device_handle dev, adev;
1015 struct usbd_device *hub;
1016 usb_device_descriptor_t *dd;
1017 usb_port_status_t ps;
1023 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
1024 bus, port, depth, speed));
1025 addr = usbd_getnewaddr(bus);
1027 printf("%s: No free USB addresses, new device ignored.\n",
1028 device_get_nameunit(bus->bdev));
1029 return (USBD_NO_ADDR);
1032 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1034 return (USBD_NOMEM);
1038 /* Set up default endpoint handle. */
1039 dev->def_ep.edesc = &dev->def_ep_desc;
1041 /* Set up default endpoint descriptor. */
1042 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1043 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1044 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1045 dev->def_ep_desc.bmAttributes = UE_CONTROL;
1046 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1047 dev->def_ep_desc.bInterval = 0;
1049 dev->quirks = &usbd_no_quirk;
1050 dev->address = USB_START_ADDR;
1051 dev->ddesc.bMaxPacketSize = 0;
1054 dev->myhub = up->parent;
1058 /* Locate port on upstream high speed hub */
1059 for (adev = dev, hub = up->parent;
1060 hub != NULL && hub->speed != USB_SPEED_HIGH;
1061 adev = hub, hub = hub->myhub)
1064 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1065 if (hub->hub->ports[p].device == adev) {
1066 dev->myhsport = &hub->hub->ports[p];
1070 panic("usbd_new_device: cannot find HS port\n");
1072 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1074 dev->myhsport = NULL;
1077 dev->langid = USBD_NOLANG;
1078 dev->cookie.cookie = ++usb_cookie_no;
1080 /* Establish the default pipe. */
1081 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1082 &dev->default_pipe);
1084 usbd_remove_device(dev, up);
1088 /* Set the address. Do this early; some devices need that. */
1089 /* Try a few times in case the device is slow (i.e. outside specs.) */
1090 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1091 for (i = 0; i < 15; i++) {
1092 err = usbd_set_address(dev, addr);
1095 usbd_delay_ms(dev, 200);
1097 DPRINTFN(-1,("usb_new_device: set address %d "
1098 "failed - trying a port reset\n", addr));
1099 usbd_reset_port(up->parent, port, &ps);
1103 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1104 err = USBD_SET_ADDR_FAILED;
1105 usbd_remove_device(dev, up);
1108 /* Allow device time to set new address */
1109 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1110 dev->address = addr; /* New device address now */
1111 bus->devices[addr] = dev;
1113 /* Re-establish the default pipe with the new address. */
1114 usbd_kill_pipe(dev->default_pipe);
1115 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1116 &dev->default_pipe);
1118 usbd_remove_device(dev, up);
1123 /* Get the first 8 bytes of the device descriptor. */
1124 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1126 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1128 usbd_remove_device(dev, up);
1132 if (speed == USB_SPEED_HIGH) {
1133 /* Max packet size must be 64 (sec 5.5.3). */
1134 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1136 printf("usbd_new_device: addr=%d bad max packet size\n",
1139 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1143 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1144 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1145 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1146 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1149 if (dd->bDescriptorType != UDESC_DEVICE) {
1150 /* Illegal device descriptor */
1151 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1152 dd->bDescriptorType));
1153 usbd_remove_device(dev, up);
1154 return (USBD_INVAL);
1157 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1158 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1159 usbd_remove_device(dev, up);
1160 return (USBD_INVAL);
1163 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1165 /* Re-establish the default pipe with the new max packet size. */
1166 usbd_kill_pipe(dev->default_pipe);
1167 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1168 &dev->default_pipe);
1170 usbd_remove_device(dev, up);
1174 err = usbd_reload_device_desc(dev);
1176 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1178 usbd_remove_device(dev, up);
1182 /* Assume 100mA bus powered for now. Changed when configured. */
1183 dev->power = USB_MIN_POWER;
1184 dev->self_powered = 0;
1186 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1187 addr, dev, parent));
1189 err = usbd_probe_and_attach(parent, dev, port, addr);
1191 usbd_remove_device(dev, up);
1195 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1197 return (USBD_NORMAL_COMPLETION);
1201 usbd_reload_device_desc(usbd_device_handle dev)
1206 /* Get the full device descriptor. */
1207 for (i = 0; i < 3; ++i) {
1208 err = usbd_get_device_desc(dev, &dev->ddesc);
1211 usbd_delay_ms(dev, 200);
1216 /* Figure out what's wrong with this device. */
1217 dev->quirks = usbd_find_quirk(&dev->ddesc);
1219 return (USBD_NORMAL_COMPLETION);
1223 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1225 DPRINTF(("usbd_remove_device: %p\n", dev));
1227 if (dev->default_pipe != NULL)
1228 usbd_kill_pipe(dev->default_pipe);
1230 dev->bus->devices[dev->address] = NULL;
1236 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1239 struct usbd_port *p;
1242 di->udi_bus = device_get_unit(dev->bus->bdev);
1243 di->udi_addr = dev->address;
1244 di->udi_cookie = dev->cookie;
1245 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
1246 usbd_printBCD(di->udi_release, UGETW(dev->ddesc.bcdDevice));
1247 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1248 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1249 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1250 di->udi_class = dev->ddesc.bDeviceClass;
1251 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1252 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1253 di->udi_config = dev->config;
1254 di->udi_power = dev->self_powered ? 0 : dev->power;
1255 di->udi_speed = dev->speed;
1257 if (dev->subdevs != NULL) {
1258 for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) {
1259 if (device_is_attached(dev->subdevs[i]))
1260 strlcpy(di->udi_devnames[i],
1261 device_get_nameunit(dev->subdevs[i]),
1262 USB_MAX_DEVNAMELEN);
1264 di->udi_devnames[i][0] = 0;
1269 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1270 di->udi_devnames[i][0] = 0; /* empty */
1274 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1275 i < dev->hub->hubdesc.bNbrPorts;
1277 p = &dev->hub->ports[i];
1279 err = p->device->address;
1281 s = UGETW(p->status.wPortStatus);
1282 if (s & UPS_PORT_ENABLED)
1283 err = USB_PORT_ENABLED;
1284 else if (s & UPS_SUSPEND)
1285 err = USB_PORT_SUSPENDED;
1286 else if (s & UPS_PORT_POWER)
1287 err = USB_PORT_POWERED;
1289 err = USB_PORT_DISABLED;
1291 di->udi_ports[i] = err;
1293 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1299 usb_free_device(usbd_device_handle dev)
1303 if (dev->default_pipe != NULL)
1304 usbd_kill_pipe(dev->default_pipe);
1305 if (dev->ifaces != NULL) {
1306 nifc = dev->cdesc->bNumInterface;
1307 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1308 usbd_free_iface_data(dev, ifcidx);
1309 free(dev->ifaces, M_USB);
1311 if (dev->cdesc != NULL)
1312 free(dev->cdesc, M_USB);
1313 if (dev->subdevs != NULL)
1314 free(dev->subdevs, M_USB);
1315 if (dev->ifacenums != NULL)
1316 free(dev->ifacenums, M_USB);
1321 * The general mechanism for detaching drivers works as follows: Each
1322 * driver is responsible for maintaining a reference count on the
1323 * number of outstanding references to its softc (e.g. from
1324 * processing hanging in a read or write). The detach method of the
1325 * driver decrements this counter and flags in the softc that the
1326 * driver is dying and then wakes any sleepers. It then sleeps on the
1327 * softc. Each place that can sleep must maintain the reference
1328 * count. When the reference count drops to -1 (0 is the normal value
1329 * of the reference count) the a wakeup on the softc is performed
1330 * signaling to the detach waiter that all references are gone.
1334 * Called from process context when we discover that a port has
1335 * been disconnected.
1338 usb_disconnect_port(struct usbd_port *up, device_t parent)
1340 usbd_device_handle dev = up->device;
1341 const char *hubname = device_get_nameunit(parent);
1344 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1345 up, dev, up->portno));
1349 printf("usb_disconnect_port: no device\n");
1354 if (dev->subdevs != NULL) {
1355 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1356 for (i = 0; dev->subdevs[i]; i++) {
1357 printf("%s: at %s", device_get_nameunit(dev->subdevs[i]),
1359 if (up->portno != 0)
1360 printf(" port %d", up->portno);
1361 printf(" (addr %d) disconnected\n", dev->address);
1362 config_detach(dev->subdevs[i], DETACH_FORCE);
1363 dev->subdevs[i] = NULL;
1367 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1368 dev->bus->devices[dev->address] = NULL;
1370 usb_free_device(dev);