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 pause("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 /* XXX FreeBSD may leak resources on failure cases -- fixme */
822 struct usb_attach_arg *uaap;
824 devinfo = malloc(1024, M_USB, M_NOWAIT);
825 if (devinfo == NULL) {
826 device_printf(parent, "Can't allocate memory for probe string\n");
829 bdev = device_add_child(parent, NULL, -1);
831 free(devinfo, M_USB);
832 device_printf(parent, "Device creation failed\n");
835 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
837 free(devinfo, M_USB);
840 device_set_ivars(bdev, uaap);
847 uaa.configno = UHUB_UNK_CONFIGURATION;
848 uaa.ifaceno = UHUB_UNK_INTERFACE;
849 uaa.vendor = UGETW(dd->idVendor);
850 uaa.product = UGETW(dd->idProduct);
851 uaa.release = UGETW(dd->bcdDevice);
854 /* First try with device specific drivers. */
855 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
857 dev->ifacenums = NULL;
858 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
859 if (dev->subdevs == NULL) {
860 free(devinfo, M_USB);
863 dev->subdevs[0] = bdev;
866 usbd_devinfo(dev, 1, devinfo);
867 device_set_desc_copy(bdev, devinfo);
868 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
870 free(devinfo, M_USB);
871 return (USBD_NORMAL_COMPLETION);
874 * Free subdevs so we can reallocate it larger for the number of
877 tmpdv = dev->subdevs;
880 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
882 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
883 dd->bNumConfigurations));
884 /* Next try with interface drivers. */
885 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
886 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
888 err = usbd_set_config_index(dev, confi, 1);
891 DPRINTF(("%s: port %d, set config at addr %d failed, "
892 "error=%s\n", device_get_nameunit(parent), port,
893 addr, usbd_errstr(err)));
895 printf("%s: port %d, set config at addr %d failed\n",
896 device_get_nameunit(parent), port, addr);
898 free(devinfo, M_USB);
901 nifaces = dev->cdesc->bNumInterface;
902 uaa.configno = dev->cdesc->bConfigurationValue;
903 for (i = 0; i < nifaces; i++)
904 ifaces[i] = &dev->ifaces[i];
906 uaa.nifaces = nifaces;
907 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
908 if (dev->subdevs == NULL) {
909 free(devinfo, M_USB);
912 dev->ifacenums = malloc((nifaces) * sizeof(*dev->ifacenums),
914 if (dev->ifacenums == NULL) {
915 free(devinfo, M_USB);
920 for (i = 0; i < nifaces; i++) {
921 if (ifaces[i] == NULL)
922 continue; /* interface already claimed */
923 uaa.iface = ifaces[i];
924 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
925 dev->subdevs[found] = bdev;
926 dev->subdevs[found + 1] = 0;
927 dev->ifacenums[found] = i;
929 usbd_devinfo(dev, 1, devinfo);
930 device_set_desc_copy(bdev, devinfo);
931 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
934 ifaces[i] = 0; /* consumed */
936 /* create another child for the next iface */
937 bdev = device_add_child(parent, NULL, -1);
939 device_printf(parent,
940 "Device add failed\n");
941 free(devinfo, M_USB);
942 return (USBD_NORMAL_COMPLETION);
944 uaap = malloc(sizeof(uaa), M_USB, M_NOWAIT);
946 free(devinfo, M_USB);
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 free(devinfo, M_USB);
991 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
993 return (USBD_NORMAL_COMPLETION);
996 * The generic attach failed, but leave the device as it is.
997 * We just did not find any drivers, that's all. The device is
998 * fully operational and not harming anyone.
1000 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
1001 return (USBD_NORMAL_COMPLETION);
1006 * Called when a new device has been put in the powered state,
1007 * but not yet in the addressed state.
1008 * Get initial descriptor, set the address, get full descriptor,
1009 * and attach a driver.
1012 usbd_new_device(device_t parent, usbd_bus_handle bus, int depth,
1013 int speed, int port, struct usbd_port *up)
1015 usbd_device_handle dev, adev;
1016 struct usbd_device *hub;
1017 usb_device_descriptor_t *dd;
1018 usb_port_status_t ps;
1024 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
1025 bus, port, depth, speed));
1026 addr = usbd_getnewaddr(bus);
1028 printf("%s: No free USB addresses, new device ignored.\n",
1029 device_get_nameunit(bus->bdev));
1030 return (USBD_NO_ADDR);
1033 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1035 return (USBD_NOMEM);
1039 /* Set up default endpoint handle. */
1040 dev->def_ep.edesc = &dev->def_ep_desc;
1042 /* Set up default endpoint descriptor. */
1043 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1044 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1045 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1046 dev->def_ep_desc.bmAttributes = UE_CONTROL;
1047 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1048 dev->def_ep_desc.bInterval = 0;
1050 dev->quirks = &usbd_no_quirk;
1051 dev->address = USB_START_ADDR;
1052 dev->ddesc.bMaxPacketSize = 0;
1055 dev->myhub = up->parent;
1059 /* Locate port on upstream high speed hub */
1060 for (adev = dev, hub = up->parent;
1061 hub != NULL && hub->speed != USB_SPEED_HIGH;
1062 adev = hub, hub = hub->myhub)
1065 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1066 if (hub->hub->ports[p].device == adev) {
1067 dev->myhsport = &hub->hub->ports[p];
1071 panic("usbd_new_device: cannot find HS port\n");
1073 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1075 dev->myhsport = NULL;
1078 dev->langid = USBD_NOLANG;
1079 dev->cookie.cookie = ++usb_cookie_no;
1081 /* Establish the default pipe. */
1082 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1083 &dev->default_pipe);
1085 usbd_remove_device(dev, up);
1090 /* Try a few times in case the device is slow (i.e. outside specs.) */
1091 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1092 for (i = 0; i < 15; i++) {
1093 /* Get the first 8 bytes of the device descriptor. */
1094 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1097 usbd_delay_ms(dev, 200);
1099 DPRINTFN(-1,("usb_new_device: set address %d "
1100 "failed - trying a port reset\n", addr));
1101 usbd_reset_port(up->parent, port, &ps);
1106 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1108 usbd_remove_device(dev, up);
1112 if (speed == USB_SPEED_HIGH) {
1113 /* Max packet size must be 64 (sec 5.5.3). */
1114 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1116 printf("usbd_new_device: addr=%d bad max packet size\n",
1119 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1123 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1124 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1125 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1126 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1129 if (dd->bDescriptorType != UDESC_DEVICE) {
1130 /* Illegal device descriptor */
1131 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1132 dd->bDescriptorType));
1133 usbd_remove_device(dev, up);
1134 return (USBD_INVAL);
1137 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1138 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1139 usbd_remove_device(dev, up);
1140 return (USBD_INVAL);
1143 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1145 /* Re-establish the default pipe with the new max packet size. */
1146 usbd_kill_pipe(dev->default_pipe);
1147 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1148 &dev->default_pipe);
1150 usbd_remove_device(dev, up);
1154 err = usbd_reload_device_desc(dev);
1156 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1158 usbd_remove_device(dev, up);
1162 /* Set the address */
1163 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1164 err = usbd_set_address(dev, addr);
1166 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1167 err = USBD_SET_ADDR_FAILED;
1168 usbd_remove_device(dev, up);
1172 /* Allow device time to set new address */
1173 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1174 dev->address = addr; /* New device address now */
1175 bus->devices[addr] = dev;
1177 /* Re-establish the default pipe with the new address. */
1178 usbd_kill_pipe(dev->default_pipe);
1179 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1180 &dev->default_pipe);
1182 usbd_remove_device(dev, up);
1186 /* Assume 100mA bus powered for now. Changed when configured. */
1187 dev->power = USB_MIN_POWER;
1188 dev->self_powered = 0;
1190 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1191 addr, dev, parent));
1193 err = usbd_probe_and_attach(parent, dev, port, addr);
1195 usbd_remove_device(dev, up);
1199 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1201 return (USBD_NORMAL_COMPLETION);
1205 usbd_reload_device_desc(usbd_device_handle dev)
1210 /* Get the full device descriptor. */
1211 for (i = 0; i < 3; ++i) {
1212 err = usbd_get_device_desc(dev, &dev->ddesc);
1215 usbd_delay_ms(dev, 200);
1220 /* Figure out what's wrong with this device. */
1221 dev->quirks = usbd_find_quirk(&dev->ddesc);
1223 return (USBD_NORMAL_COMPLETION);
1227 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1229 DPRINTF(("usbd_remove_device: %p\n", dev));
1231 if (dev->default_pipe != NULL)
1232 usbd_kill_pipe(dev->default_pipe);
1234 dev->bus->devices[dev->address] = NULL;
1240 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1243 struct usbd_port *p;
1246 di->udi_bus = device_get_unit(dev->bus->bdev);
1247 di->udi_addr = dev->address;
1248 di->udi_cookie = dev->cookie;
1249 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
1250 usbd_printBCD(di->udi_release, UGETW(dev->ddesc.bcdDevice));
1251 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1252 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1253 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1254 di->udi_class = dev->ddesc.bDeviceClass;
1255 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1256 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1257 di->udi_config = dev->config;
1258 di->udi_power = dev->self_powered ? 0 : dev->power;
1259 di->udi_speed = dev->speed;
1261 if (dev->subdevs != NULL) {
1262 for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) {
1263 if (device_is_attached(dev->subdevs[i]))
1264 strlcpy(di->udi_devnames[i],
1265 device_get_nameunit(dev->subdevs[i]),
1266 USB_MAX_DEVNAMELEN);
1268 di->udi_devnames[i][0] = 0;
1273 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1274 di->udi_devnames[i][0] = 0; /* empty */
1278 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1279 i < dev->hub->hubdesc.bNbrPorts;
1281 p = &dev->hub->ports[i];
1283 err = p->device->address;
1285 s = UGETW(p->status.wPortStatus);
1286 if (s & UPS_PORT_ENABLED)
1287 err = USB_PORT_ENABLED;
1288 else if (s & UPS_SUSPEND)
1289 err = USB_PORT_SUSPENDED;
1290 else if (s & UPS_PORT_POWER)
1291 err = USB_PORT_POWERED;
1293 err = USB_PORT_DISABLED;
1295 di->udi_ports[i] = err;
1297 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1303 usb_free_device(usbd_device_handle dev)
1307 if (dev->default_pipe != NULL)
1308 usbd_kill_pipe(dev->default_pipe);
1309 if (dev->ifaces != NULL) {
1310 nifc = dev->cdesc->bNumInterface;
1311 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1312 usbd_free_iface_data(dev, ifcidx);
1313 free(dev->ifaces, M_USB);
1315 if (dev->cdesc != NULL)
1316 free(dev->cdesc, M_USB);
1317 if (dev->subdevs != NULL)
1318 free(dev->subdevs, M_USB);
1319 if (dev->ifacenums != NULL)
1320 free(dev->ifacenums, M_USB);
1325 * The general mechanism for detaching drivers works as follows: Each
1326 * driver is responsible for maintaining a reference count on the
1327 * number of outstanding references to its softc (e.g. from
1328 * processing hanging in a read or write). The detach method of the
1329 * driver decrements this counter and flags in the softc that the
1330 * driver is dying and then wakes any sleepers. It then sleeps on the
1331 * softc. Each place that can sleep must maintain the reference
1332 * count. When the reference count drops to -1 (0 is the normal value
1333 * of the reference count) the a wakeup on the softc is performed
1334 * signaling to the detach waiter that all references are gone.
1338 * Called from process context when we discover that a port has
1339 * been disconnected.
1342 usb_disconnect_port(struct usbd_port *up, device_t parent)
1344 usbd_device_handle dev = up->device;
1345 const char *hubname = device_get_nameunit(parent);
1348 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1349 up, dev, up->portno));
1353 printf("usb_disconnect_port: no device\n");
1358 if (dev->subdevs != NULL) {
1359 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1360 for (i = 0; dev->subdevs[i]; i++) {
1361 printf("%s: at %s", device_get_nameunit(dev->subdevs[i]),
1363 if (up->portno != 0)
1364 printf(" port %d", up->portno);
1365 printf(" (addr %d) disconnected\n", dev->address);
1366 config_detach(dev->subdevs[i], DETACH_FORCE);
1367 dev->subdevs[i] = NULL;
1371 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1372 dev->bus->devices[dev->address] = NULL;
1374 usb_free_device(dev);