1 /* $NetBSD: usb_subr.c,v 1.99 2002/07/11 21:14:34 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.76.2.3 2006/03/01 01:59:05 iedowse Exp $ */
3 /* $DragonFly: src/sys/bus/usb/usb_subr.c,v 1.28 2008/05/25 16:38:36 mneumann Exp $ */
5 /* Also already have from NetBSD:
6 * $NetBSD: usb_subr.c,v 1.102 2003/01/01 16:21:50 augustss Exp $
7 * $NetBSD: usb_subr.c,v 1.103 2003/01/10 11:19:13 augustss Exp $
8 * $NetBSD: usb_subr.c,v 1.111 2004/03/15 10:35:04 augustss Exp $
9 * $NetBSD: usb_subr.c,v 1.114 2004/06/23 02:30:52 mycroft Exp $
10 * $NetBSD: usb_subr.c,v 1.115 2004/06/23 05:23:19 mycroft Exp $
11 * $NetBSD: usb_subr.c,v 1.116 2004/06/23 06:27:54 mycroft Exp $
12 * $NetBSD: usb_subr.c,v 1.119 2004/10/23 13:26:33 augustss Exp $
16 * Copyright (c) 1998 The NetBSD Foundation, Inc.
17 * All rights reserved.
19 * This code is derived from software contributed to The NetBSD Foundation
20 * by Lennart Augustsson (lennart@augustsson.net) at
21 * Carlstedt Research & Technology.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * This product includes software developed by the NetBSD
34 * Foundation, Inc. and its contributors.
35 * 4. Neither the name of The NetBSD Foundation nor the names of its
36 * contributors may be used to endorse or promote products derived
37 * from this software without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/module.h>
62 #include <bus/usb/usb.h>
64 #include <bus/usb/usbdi.h>
65 #include <bus/usb/usbdi_util.h>
66 #include <bus/usb/usbdivar.h>
67 #include <bus/usb/usb_quirks.h>
69 #define delay(d) DELAY(d)
72 #define DPRINTF(x) if (usbdebug) kprintf x
73 #define DPRINTFN(n,x) if (usbdebug>(n)) kprintf x
80 static usbd_status usbd_set_config(usbd_device_handle, int);
81 static void usbd_devinfo_vp(usbd_device_handle, char *, char *, int);
82 static int usbd_getnewaddr(usbd_bus_handle bus);
83 static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
84 static void usbd_kill_pipe(usbd_pipe_handle);
85 static usbd_status usbd_probe_and_attach(device_t parent,
86 usbd_device_handle dev, int port, int addr);
88 static u_int32_t usb_cookie_no = 0;
90 static const char * const usbd_error_strs[] = {
114 usbd_errstr(usbd_status err)
116 static char buffer[5];
118 if (err < USBD_ERROR_MAX) {
119 return usbd_error_strs[err];
121 ksnprintf(buffer, sizeof buffer, "%d", err);
127 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
128 usb_string_descriptor_t *sdesc, int *sizep)
130 usb_device_request_t req;
134 req.bmRequestType = UT_READ_DEVICE;
135 req.bRequest = UR_GET_DESCRIPTOR;
136 USETW2(req.wValue, UDESC_STRING, sindex);
137 USETW(req.wIndex, langid);
138 USETW(req.wLength, 2); /* only size byte first */
139 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
140 &actlen, USBD_DEFAULT_TIMEOUT);
145 return (USBD_SHORT_XFER);
147 USETW(req.wLength, sdesc->bLength); /* the whole string */
148 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
149 &actlen, USBD_DEFAULT_TIMEOUT);
153 if (actlen != sdesc->bLength) {
154 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
155 sdesc->bLength, actlen));
159 return (USBD_NORMAL_COMPLETION);
163 usbd_trim_spaces(char *p)
170 while (*q == ' ') /* skip leading spaces */
172 while ((*p = *q++)) /* copy string */
173 if (*p++ != ' ') /* remember last non-space */
175 *e = 0; /* kill trailing spaces */
179 usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev)
181 usb_device_descriptor_t *udd = &dev->ddesc;
182 char *vendor = 0, *product = 0;
190 if (usbd_get_string(dev, udd->iManufacturer, v))
194 usbd_trim_spaces(vendor);
195 if (usbd_get_string(dev, udd->iProduct, p))
199 usbd_trim_spaces(product);
200 if (vendor && !*vendor)
202 if (product && !*product)
209 if (vendor != NULL && *vendor)
212 ksprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
213 if (product != NULL && *product)
216 ksprintf(p, "product 0x%04x", UGETW(udd->idProduct));
220 usbd_printBCD(char *cp, int bcd)
222 return (ksprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
226 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp)
228 usb_device_descriptor_t *udd = &dev->ddesc;
229 usbd_interface_handle iface;
230 char vendor[USB_MAX_STRING_LEN];
231 char product[USB_MAX_STRING_LEN];
232 int bcdDevice, bcdUSB;
233 usb_interface_descriptor_t *id;
235 usbd_devinfo_vp(dev, vendor, product, 1);
236 cp += ksprintf(cp, "%s %s", vendor, product);
237 if (showclass & USBD_SHOW_DEVICE_CLASS)
238 cp += ksprintf(cp, ", class %d/%d",
239 udd->bDeviceClass, udd->bDeviceSubClass);
240 bcdUSB = UGETW(udd->bcdUSB);
241 bcdDevice = UGETW(udd->bcdDevice);
242 cp += ksprintf(cp, ", rev ");
243 cp += usbd_printBCD(cp, bcdUSB);
245 cp += usbd_printBCD(cp, bcdDevice);
246 cp += ksprintf(cp, ", addr %d", dev->address);
247 if (showclass & USBD_SHOW_INTERFACE_CLASS)
249 /* fetch the interface handle for the first interface */
250 (void)usbd_device2interface_handle(dev, 0, &iface);
251 id = usbd_get_interface_descriptor(iface);
252 cp += ksprintf(cp, ", iclass %d/%d",
253 id->bInterfaceClass, id->bInterfaceSubClass);
258 /* Delay for a certain number of ms */
260 usb_delay_ms(usbd_bus_handle bus, u_int ms)
262 /* Wait at least two clock ticks so we know the time has passed. */
263 if (bus->use_polling || cold)
264 delay((ms+1) * 1000);
266 tsleep(&ms, 0, "usbdly", (ms*hz+999)/1000 + 1);
269 /* Delay given a device handle. */
271 usbd_delay_ms(usbd_device_handle dev, u_int ms)
273 usb_delay_ms(dev->bus, ms);
277 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
279 usb_device_request_t req;
283 req.bmRequestType = UT_WRITE_CLASS_OTHER;
284 req.bRequest = UR_SET_FEATURE;
285 USETW(req.wValue, UHF_PORT_RESET);
286 USETW(req.wIndex, port);
287 USETW(req.wLength, 0);
288 err = usbd_do_request(dev, &req, 0);
289 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
290 port, usbd_errstr(err)));
295 /* Wait for device to recover from reset. */
296 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
297 err = usbd_get_port_status(dev, port, ps);
299 DPRINTF(("usbd_reset_port: get status failed %d\n",
303 /* If the device disappeared, just give up. */
304 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
305 return (USBD_NORMAL_COMPLETION);
306 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
308 return (USBD_TIMEOUT);
309 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
312 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
316 /* Wait for the device to recover from reset. */
317 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
321 usb_interface_descriptor_t *
322 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
324 char *p = (char *)cd;
325 char *end = p + UGETW(cd->wTotalLength);
326 usb_interface_descriptor_t *d;
327 int curidx, lastidx, curaidx = 0;
329 for (curidx = lastidx = -1; p < end; ) {
330 d = (usb_interface_descriptor_t *)p;
331 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
333 ifaceidx, curidx, altidx, curaidx,
334 d->bLength, d->bDescriptorType));
335 if (d->bLength == 0) /* bad descriptor */
338 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
339 if (d->bInterfaceNumber != lastidx) {
340 lastidx = d->bInterfaceNumber;
345 if (ifaceidx == curidx && altidx == curaidx)
352 usb_endpoint_descriptor_t *
353 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
356 char *p = (char *)cd;
357 char *end = p + UGETW(cd->wTotalLength);
358 usb_interface_descriptor_t *d;
359 usb_endpoint_descriptor_t *e;
362 d = usbd_find_idesc(cd, ifaceidx, altidx);
365 if (endptidx >= d->bNumEndpoints) /* quick exit */
369 for (p = (char *)d + d->bLength; p < end; ) {
370 e = (usb_endpoint_descriptor_t *)p;
371 if (e->bLength == 0) /* bad descriptor */
374 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
376 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
378 if (curidx == endptidx)
386 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
388 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
389 usb_interface_descriptor_t *idesc;
393 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
395 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
400 ifc->index = ifaceidx;
401 ifc->altindex = altidx;
402 nendpt = ifc->idesc->bNumEndpoints;
403 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
405 ifc->endpoints = kmalloc(nendpt * sizeof(struct usbd_endpoint),
408 ifc->endpoints = NULL;
411 p = (char *)ifc->idesc + ifc->idesc->bLength;
412 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
413 #define ed ((usb_endpoint_descriptor_t *)p)
414 for (endpt = 0; endpt < nendpt; endpt++) {
415 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
416 for (; p < end; p += ed->bLength) {
417 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
419 p, end, ed->bLength, ed->bDescriptorType));
420 if (p + ed->bLength <= end && ed->bLength != 0 &&
421 ed->bDescriptorType == UDESC_ENDPOINT)
423 if (ed->bLength == 0 ||
424 ed->bDescriptorType == UDESC_INTERFACE)
427 /* passed end, or bad desc */
428 kprintf("usbd_fill_iface_data: bad descriptor(s): %s\n",
429 ed->bLength == 0 ? "0 length" :
430 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
434 ifc->endpoints[endpt].edesc = ed;
435 if (dev->speed == USB_SPEED_HIGH) {
437 /* Control and bulk endpoints have max packet limits. */
438 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
440 mps = USB_2_MAX_CTRL_PACKET;
443 mps = USB_2_MAX_BULK_PACKET;
445 if (UGETW(ed->wMaxPacketSize) != mps) {
446 USETW(ed->wMaxPacketSize, mps);
448 kprintf("usbd_fill_iface_data: bad max "
457 ifc->endpoints[endpt].refcnt = 0;
458 ifc->endpoints[endpt].savedtoggle = 0;
462 LIST_INIT(&ifc->pipes);
463 return (USBD_NORMAL_COMPLETION);
466 if (ifc->endpoints != NULL) {
467 kfree(ifc->endpoints, M_USB);
468 ifc->endpoints = NULL;
474 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
476 usbd_interface_handle ifc = &dev->ifaces[ifcno];
478 kfree(ifc->endpoints, M_USB);
482 usbd_set_config(usbd_device_handle dev, int conf)
484 usb_device_request_t req;
486 req.bmRequestType = UT_WRITE_DEVICE;
487 req.bRequest = UR_SET_CONFIG;
488 USETW(req.wValue, conf);
489 USETW(req.wIndex, 0);
490 USETW(req.wLength, 0);
491 return (usbd_do_request(dev, &req, 0));
495 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
498 usb_config_descriptor_t cd;
501 if (no == USB_UNCONFIG_NO)
502 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
504 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
505 /* Figure out what config index to use. */
506 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
507 err = usbd_get_config_desc(dev, index, &cd);
510 if (cd.bConfigurationValue == no)
511 return (usbd_set_config_index(dev, index, msg));
517 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
520 usb_config_descriptor_t cd, *cdp;
522 int i, ifcidx, nifc, len, selfpowered, power;
524 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
526 if (dev->config != USB_UNCONFIG_NO) {
527 nifc = dev->cdesc->bNumInterface;
529 /* Check that all interfaces are idle */
530 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
531 if (LIST_EMPTY(&dev->ifaces[ifcidx].pipes))
533 DPRINTF(("usbd_set_config_index: open pipes exist\n"));
534 return (USBD_IN_USE);
537 DPRINTF(("usbd_set_config_index: free old config\n"));
538 /* Free all configuration data structures. */
539 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
540 usbd_free_iface_data(dev, ifcidx);
541 kfree(dev->ifaces, M_USB);
542 kfree(dev->cdesc, M_USB);
545 dev->config = USB_UNCONFIG_NO;
548 if (index == USB_UNCONFIG_INDEX) {
549 /* We are unconfiguring the device, so leave unallocated. */
550 DPRINTF(("usbd_set_config_index: set config 0\n"));
551 err = usbd_set_config(dev, USB_UNCONFIG_NO);
553 DPRINTF(("usbd_set_config_index: setting config=0 "
554 "failed, error=%s\n", usbd_errstr(err)));
558 /* Get the short descriptor. */
559 err = usbd_get_config_desc(dev, index, &cd);
562 len = UGETW(cd.wTotalLength);
563 cdp = kmalloc(len, M_USB, M_INTWAIT);
564 /* Get the full descriptor. Try a few times for slow devices. */
565 for (i = 0; i < 3; i++) {
566 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
569 usbd_delay_ms(dev, 200);
573 if (cdp->bDescriptorType != UDESC_CONFIG) {
574 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
575 cdp->bDescriptorType));
580 /* Figure out if the device is self or bus powered. */
582 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
583 (cdp->bmAttributes & UC_SELF_POWERED)) {
584 /* May be self powered. */
585 if (cdp->bmAttributes & UC_BUS_POWERED) {
586 /* Must ask device. */
587 if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
589 * Hub claims to be self powered, but isn't.
590 * It seems that the power status can be
591 * determined by the hub characteristics.
593 usb_hub_descriptor_t hd;
594 usb_device_request_t req;
595 req.bmRequestType = UT_READ_CLASS_DEVICE;
596 req.bRequest = UR_GET_DESCRIPTOR;
597 USETW(req.wValue, 0);
598 USETW(req.wIndex, 0);
599 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
600 err = usbd_do_request(dev, &req, &hd);
602 (UGETW(hd.wHubCharacteristics) &
605 DPRINTF(("usbd_set_config_index: charac=0x%04x"
607 UGETW(hd.wHubCharacteristics),
610 err = usbd_get_device_status(dev, &ds);
612 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
614 DPRINTF(("usbd_set_config_index: status=0x%04x"
616 UGETW(ds.wStatus), usbd_errstr(err)));
621 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
622 "selfpowered=%d, power=%d\n",
623 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
624 selfpowered, cdp->bMaxPower * 2));
626 /* Check if we have enough power. */
628 if (dev->powersrc == NULL) {
629 DPRINTF(("usbd_set_config_index: No power source?\n"));
630 return (USBD_IOERROR);
633 power = cdp->bMaxPower * 2;
634 if (power > dev->powersrc->power) {
635 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
636 /* XXX print nicer message. */
638 kprintf("%s: device addr %d (config %d) exceeds power "
639 "budget, %d mA > %d mA\n",
640 device_get_nameunit(dev->bus->bdev), dev->address,
641 cdp->bConfigurationValue,
642 power, dev->powersrc->power);
647 dev->self_powered = selfpowered;
649 /* Set the actual configuration value. */
650 DPRINTF(("usbd_set_config_index: set config %d\n",
651 cdp->bConfigurationValue));
652 err = usbd_set_config(dev, cdp->bConfigurationValue);
654 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
656 cdp->bConfigurationValue, usbd_errstr(err)));
660 /* Allocate and fill interface data. */
661 nifc = cdp->bNumInterface;
662 dev->ifaces = kmalloc(nifc * sizeof(struct usbd_interface),
664 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
666 dev->config = cdp->bConfigurationValue;
667 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
668 err = usbd_fill_iface_data(dev, ifcidx, 0);
670 while (--ifcidx >= 0)
671 usbd_free_iface_data(dev, ifcidx);
676 return (USBD_NORMAL_COMPLETION);
683 /* XXX add function for alternate settings */
686 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
687 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
692 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
693 dev, iface, ep, pipe));
694 p = kmalloc(dev->bus->pipe_size, M_USB, M_INTWAIT);
705 STAILQ_INIT(&p->queue);
706 err = dev->bus->methods->open_pipe(p);
708 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
710 ep->edesc->bEndpointAddress, usbd_errstr(err)));
716 return (USBD_NORMAL_COMPLETION);
719 /* Abort the device control pipe. */
721 usbd_kill_pipe(usbd_pipe_handle pipe)
723 usbd_abort_pipe(pipe);
724 pipe->methods->close(pipe);
725 pipe->endpoint->refcnt--;
730 usbd_getnewaddr(usbd_bus_handle bus)
734 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
735 if (bus->devices[addr] == 0)
742 usbd_probe_and_attach(device_t parent, usbd_device_handle dev,
745 struct usb_attach_arg uaa;
746 usb_device_descriptor_t *dd = &dev->ddesc;
747 int found, i, confi, nifaces;
750 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
753 /* XXX FreeBSD may leak resources on failure cases -- fixme */
755 struct usb_attach_arg *uaap;
757 devinfo = kmalloc(1024, M_USB, M_NOWAIT);
758 if (devinfo == NULL) {
759 device_printf(parent,
760 "Can't allocate memory for probe string\n");
763 bdev = device_add_child(parent, NULL, -1);
765 kfree(devinfo, M_USB);
766 device_printf(parent, "Device creation failed\n");
769 uaap = kmalloc(sizeof(uaa), M_USB, M_WAITOK);
770 device_set_ivars(bdev, uaap);
777 uaa.configno = UHUB_UNK_CONFIGURATION;
778 uaa.ifaceno = UHUB_UNK_INTERFACE;
779 uaa.vendor = UGETW(dd->idVendor);
780 uaa.product = UGETW(dd->idProduct);
781 uaa.release = UGETW(dd->bcdDevice);
784 /* First try with device specific drivers. */
785 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
787 dev->ifacenums = NULL;
788 dev->subdevs = kmalloc(2 * sizeof(device_t), M_USB, M_WAITOK);
789 dev->subdevs[0] = bdev;
792 usbd_devinfo(dev, 1, devinfo);
793 device_set_desc_copy(bdev, devinfo);
794 if (device_probe_and_attach(bdev) == 0) {
795 kfree(devinfo, M_USB);
796 return (USBD_NORMAL_COMPLETION);
800 * Free subdevs so we can reallocate it larger for the number of
803 tmpdv = dev->subdevs;
806 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
808 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
809 dd->bNumConfigurations));
810 /* Next try with interface drivers. */
811 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
812 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
814 err = usbd_set_config_index(dev, confi, 1);
817 DPRINTF(("%s: port %d, set config at addr %d failed, "
818 "error=%s\n", device_get_nameunit(parent), port,
819 addr, usbd_errstr(err)));
821 kprintf("%s: port %d, set config at addr %d failed\n",
822 device_get_nameunit(parent), port, addr);
824 kfree(devinfo, M_USB);
827 nifaces = dev->cdesc->bNumInterface;
828 uaa.configno = dev->cdesc->bConfigurationValue;
829 for (i = 0; i < nifaces; i++)
830 ifaces[i] = &dev->ifaces[i];
832 uaa.nifaces = nifaces;
833 dev->subdevs = kmalloc((nifaces+1) * sizeof(device_t), M_USB,
835 dev->ifacenums = kmalloc((nifaces) * sizeof(*dev->ifacenums),
839 for (i = 0; i < nifaces; i++) {
840 if (ifaces[i] == NULL)
841 continue; /* interface already claimed */
842 uaa.iface = ifaces[i];
843 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
844 dev->subdevs[found] = bdev;
845 dev->subdevs[found + 1] = 0;
846 dev->ifacenums[found] = i;
848 usbd_devinfo(dev, 1, devinfo);
849 device_set_desc_copy(bdev, devinfo);
850 if (device_probe_and_attach(bdev) == 0) {
851 ifaces[i] = 0; /* consumed */
854 /* create another child for the next iface */
855 bdev = device_add_child(parent, NULL, -1);
857 device_printf(parent,
858 "Device add failed\n");
859 kfree(devinfo, M_USB);
860 return (USBD_NORMAL_COMPLETION);
862 uaap = kmalloc(sizeof(uaa), M_USB, M_WAITOK);
863 device_set_ivars(bdev, uaap);
865 dev->subdevs[found] = 0;
869 /* remove the last created child. It is unused */
871 kfree(devinfo, M_USB);
872 device_delete_child(parent, bdev);
873 /* kfree(uaap, M_USB); */ /* May be needed? xxx */
874 return (USBD_NORMAL_COMPLETION);
876 tmpdv = dev->subdevs;
879 kfree(dev->ifacenums, M_USB);
880 dev->ifacenums = NULL;
882 /* No interfaces were attached in any of the configurations. */
884 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
885 usbd_set_config_index(dev, 0, 0);
887 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
889 /* Finally try the generic driver. */
892 uaa.configno = UHUB_UNK_CONFIGURATION;
893 uaa.ifaceno = UHUB_UNK_INTERFACE;
894 dev->subdevs = kmalloc(2 * sizeof(device_t), M_USB, M_WAITOK);
895 dev->subdevs[0] = bdev;
898 usbd_devinfo(dev, 1, devinfo);
899 device_set_desc_copy(bdev, devinfo);
900 kfree(devinfo, M_USB);
901 if (device_probe_and_attach(bdev) == 0)
902 return (USBD_NORMAL_COMPLETION);
905 * The generic attach failed, but leave the device as it is.
906 * We just did not find any drivers, that's all. The device is
907 * fully operational and not harming anyone.
909 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
910 return (USBD_NORMAL_COMPLETION);
915 * Called when a new device has been put in the powered state,
916 * but not yet in the addressed state.
917 * Get initial descriptor, set the address, get full descriptor,
918 * and attach a driver.
921 usbd_new_device(device_t parent, usbd_bus_handle bus, int depth,
922 int speed, int port, struct usbd_port *up)
924 usbd_device_handle dev, adev;
925 struct usbd_device *hub;
926 usb_device_descriptor_t *dd;
927 usb_port_status_t ps;
933 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
934 bus, port, depth, speed));
935 addr = usbd_getnewaddr(bus);
937 kprintf("%s: No free USB addresses, new device ignored.\n",
938 device_get_nameunit(bus->bdev));
939 return (USBD_NO_ADDR);
942 dev = kmalloc(sizeof *dev, M_USB, M_INTWAIT | M_ZERO);
945 /* Set up default endpoint handle. */
946 dev->def_ep.edesc = &dev->def_ep_desc;
948 /* Set up default endpoint descriptor. */
949 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
950 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
951 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
952 dev->def_ep_desc.bmAttributes = UE_CONTROL;
953 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
954 dev->def_ep_desc.bInterval = 0;
956 dev->quirks = &usbd_no_quirk;
957 dev->address = USB_START_ADDR;
958 dev->ddesc.bMaxPacketSize = 0;
961 dev->myhub = up->parent;
965 /* Locate port on upstream high speed hub */
966 for (adev = dev, hub = up->parent;
967 hub != NULL && hub->speed != USB_SPEED_HIGH;
968 adev = hub, hub = hub->myhub)
971 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
972 if (hub->hub->ports[p].device == adev) {
973 dev->myhsport = &hub->hub->ports[p];
977 panic("usbd_new_device: cannot find HS port\n");
979 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
981 dev->myhsport = NULL;
984 dev->langid = USBD_NOLANG;
985 dev->cookie.cookie = ++usb_cookie_no;
987 /* Establish the default pipe. */
988 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
991 usbd_remove_device(dev, up);
995 /* Set the address. Do this early; some devices need that. */
996 /* Try a few times in case the device is slow (i.e. outside specs.) */
997 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
998 for (i = 0; i < 15; i++) {
999 err = usbd_set_address(dev, addr);
1002 usbd_delay_ms(dev, 200);
1004 DPRINTFN(-1,("usb_new_device: set address %d "
1005 "failed - trying a port reset\n", addr));
1006 usbd_reset_port(up->parent, port, &ps);
1010 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1011 err = USBD_SET_ADDR_FAILED;
1012 usbd_remove_device(dev, up);
1015 /* Allow device time to set new address */
1016 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1017 dev->address = addr; /* New device address now */
1018 bus->devices[addr] = dev;
1021 /* Get the first 8 bytes of the device descriptor. */
1022 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1024 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1026 usbd_remove_device(dev, up);
1030 if (speed == USB_SPEED_HIGH) {
1031 /* Max packet size must be 64 (sec 5.5.3). */
1032 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1034 kprintf("usbd_new_device: addr=%d bad max packet size\n",
1037 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1041 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1042 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1043 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1044 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1047 if (dd->bDescriptorType != UDESC_DEVICE) {
1048 /* Illegal device descriptor */
1049 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1050 dd->bDescriptorType));
1051 usbd_remove_device(dev, up);
1052 return (USBD_INVAL);
1055 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1056 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1057 usbd_remove_device(dev, up);
1058 return (USBD_INVAL);
1061 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1063 err = usbd_reload_device_desc(dev);
1065 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1067 usbd_remove_device(dev, up);
1071 /* Assume 100mA bus powered for now. Changed when configured. */
1072 dev->power = USB_MIN_POWER;
1073 dev->self_powered = 0;
1075 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1076 addr, dev, parent));
1078 err = usbd_probe_and_attach(parent, dev, port, addr);
1080 usbd_remove_device(dev, up);
1084 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1086 return (USBD_NORMAL_COMPLETION);
1090 usbd_reload_device_desc(usbd_device_handle dev)
1095 /* Get the full device descriptor. */
1096 for (i = 0; i < 3; ++i) {
1097 err = usbd_get_device_desc(dev, &dev->ddesc);
1100 usbd_delay_ms(dev, 200);
1105 /* Figure out what's wrong with this device. */
1106 dev->quirks = usbd_find_quirk(&dev->ddesc);
1108 return (USBD_NORMAL_COMPLETION);
1112 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1114 DPRINTF(("usbd_remove_device: %p\n", dev));
1116 if (dev->default_pipe != NULL)
1117 usbd_kill_pipe(dev->default_pipe);
1119 dev->bus->devices[dev->address] = NULL;
1125 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1128 struct usbd_port *p;
1131 di->udi_bus = device_get_unit(dev->bus->bdev);
1132 di->udi_addr = dev->address;
1133 di->udi_cookie = dev->cookie;
1134 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
1135 usbd_printBCD(di->udi_release, UGETW(dev->ddesc.bcdDevice));
1136 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1137 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1138 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1139 di->udi_class = dev->ddesc.bDeviceClass;
1140 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1141 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1142 di->udi_config = dev->config;
1143 di->udi_power = dev->self_powered ? 0 : dev->power;
1144 di->udi_speed = dev->speed;
1146 if (dev->subdevs != NULL) {
1147 for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) {
1148 if (device_is_attached(dev->subdevs[i]))
1149 strlcpy(di->udi_devnames[i],
1150 device_get_nameunit(dev->subdevs[i]),
1151 USB_MAX_DEVNAMELEN);
1153 di->udi_devnames[i][0] = 0;
1158 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1159 di->udi_devnames[i][0] = 0; /* empty */
1163 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1164 i < dev->hub->hubdesc.bNbrPorts;
1166 p = &dev->hub->ports[i];
1168 err = p->device->address;
1170 s = UGETW(p->status.wPortStatus);
1171 if (s & UPS_PORT_ENABLED)
1172 err = USB_PORT_ENABLED;
1173 else if (s & UPS_SUSPEND)
1174 err = USB_PORT_SUSPENDED;
1175 else if (s & UPS_PORT_POWER)
1176 err = USB_PORT_POWERED;
1178 err = USB_PORT_DISABLED;
1180 di->udi_ports[i] = err;
1182 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1188 usb_free_device(usbd_device_handle dev)
1192 if (dev->default_pipe != NULL)
1193 usbd_kill_pipe(dev->default_pipe);
1194 if (dev->ifaces != NULL) {
1195 nifc = dev->cdesc->bNumInterface;
1196 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1197 usbd_free_iface_data(dev, ifcidx);
1198 kfree(dev->ifaces, M_USB);
1200 if (dev->cdesc != NULL)
1201 kfree(dev->cdesc, M_USB);
1202 if (dev->subdevs != NULL)
1203 kfree(dev->subdevs, M_USB);
1204 if (dev->ifacenums != NULL)
1205 kfree(dev->ifacenums, M_USB);
1210 * The general mechanism for detaching drivers works as follows: Each
1211 * driver is responsible for maintaining a reference count on the
1212 * number of outstanding references to its softc (e.g. from
1213 * processing hanging in a read or write). The detach method of the
1214 * driver decrements this counter and flags in the softc that the
1215 * driver is dying and then wakes any sleepers. It then sleeps on the
1216 * softc. Each place that can sleep must maintain the reference
1217 * count. When the reference count drops to -1 (0 is the normal value
1218 * of the reference count) the a wakeup on the softc is performed
1219 * signaling to the detach waiter that all references are gone.
1223 * Called from process context when we discover that a port has
1224 * been disconnected.
1227 usb_disconnect_port(struct usbd_port *up, device_t parent)
1229 usbd_device_handle dev = up->device;
1230 const char *hubname = device_get_nameunit(parent);
1233 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1234 up, dev, up->portno));
1238 kprintf("usb_disconnect_port: no device\n");
1243 if (dev->subdevs != NULL) {
1244 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1245 for (i = 0; dev->subdevs[i]; i++) {
1246 kprintf("%s: at %s", device_get_nameunit(dev->subdevs[i]),
1248 if (up->portno != 0)
1249 kprintf(" port %d", up->portno);
1250 kprintf(" (addr %d) disconnected\n", dev->address);
1251 device_delete_child(device_get_parent(dev->subdevs[i]), dev->subdevs[i]);
1252 dev->subdevs[i] = NULL;
1256 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1257 dev->bus->devices[dev->address] = NULL;
1259 usb_free_device(dev);