1 /* $NetBSD: usb_subr.c,v 1.76 2000/04/27 15:26:50 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.23.2.8 2003/01/17 17:46:24 joe Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #if defined(__NetBSD__) || defined(__OpenBSD__)
46 #include <sys/device.h>
47 #include <sys/select.h>
48 #elif defined(__FreeBSD__)
49 #include <sys/module.h>
54 #include <machine/bus.h>
56 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbdivar.h>
61 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/usb_quirks.h>
64 #if defined(__FreeBSD__)
65 #include <machine/clock.h>
66 #define delay(d) DELAY(d)
70 #define DPRINTF(x) if (usbdebug) logprintf x
71 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
78 Static usbd_status usbd_set_config(usbd_device_handle, int);
79 Static char *usbd_get_string(usbd_device_handle, int, char *);
80 Static int usbd_getnewaddr(usbd_bus_handle bus);
81 #if defined(__NetBSD__)
82 Static int usbd_print(void *aux, const char *pnp);
83 Static int usbd_submatch(device_ptr_t, struct cfdata *cf, void *);
84 #elif defined(__OpenBSD__)
85 Static int usbd_submatch(device_ptr_t, void *, void *);
87 Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
88 Static void usbd_kill_pipe(usbd_pipe_handle);
89 Static usbd_status usbd_probe_and_attach
90 (device_ptr_t parent, usbd_device_handle dev, int port, int addr);
92 Static u_int32_t usb_cookie_no = 0;
95 typedef u_int16_t usb_vendor_id_t;
96 typedef u_int16_t usb_product_id_t;
99 * Descriptions of of known vendors and devices ("products").
101 struct usb_knowndev {
102 usb_vendor_id_t vendor;
103 usb_product_id_t product;
105 char *vendorname, *productname;
107 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
109 #include <dev/usb/usbdevs_data.h>
110 #endif /* USBVERBOSE */
112 Static const char *usbd_error_strs[] = {
136 usbd_errstr(usbd_status err)
138 static char buffer[5];
140 if (err < USBD_ERROR_MAX) {
141 return usbd_error_strs[err];
143 snprintf(buffer, sizeof buffer, "%d", err);
149 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
150 usb_string_descriptor_t *sdesc)
152 usb_device_request_t req;
156 req.bmRequestType = UT_READ_DEVICE;
157 req.bRequest = UR_GET_DESCRIPTOR;
158 USETW2(req.wValue, UDESC_STRING, sindex);
159 USETW(req.wIndex, langid);
160 USETW(req.wLength, 2); /* only size byte first */
161 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
167 return (USBD_SHORT_XFER);
169 USETW(req.wLength, sdesc->bLength); /* the whole string */
170 return (usbd_do_request(dev, &req, sdesc));
174 usbd_get_string(usbd_device_handle dev, int si, char *buf)
176 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
177 usb_string_descriptor_t us;
185 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
187 if (dev->langid == USBD_NOLANG) {
188 /* Set up default language */
189 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us);
190 if (err || us.bLength < 4) {
191 dev->langid = 0; /* Well, just pick English then */
193 /* Pick the first language as the default. */
194 dev->langid = UGETW(us.bString[0]);
197 err = usbd_get_string_desc(dev, si, dev->langid, &us);
201 n = us.bLength / 2 - 1;
202 for (i = 0; i < n; i++) {
203 c = UGETW(us.bString[i]);
204 /* Convert from Unicode, handle buggy strings. */
205 if ((c & 0xff00) == 0)
207 else if ((c & 0x00ff) == 0 && swap)
217 usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p)
219 usb_device_descriptor_t *udd = &dev->ddesc;
220 char *vendor = 0, *product = 0;
222 struct usb_knowndev *kdp;
230 vendor = usbd_get_string(dev, udd->iManufacturer, v);
231 product = usbd_get_string(dev, udd->iProduct, p);
233 if (vendor == NULL || product == NULL) {
234 for(kdp = usb_knowndevs;
235 kdp->vendorname != NULL;
237 if (kdp->vendor == UGETW(udd->idVendor) &&
238 (kdp->product == UGETW(udd->idProduct) ||
239 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
242 if (kdp->vendorname != NULL) {
244 vendor = kdp->vendorname;
246 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
247 kdp->productname : NULL;
254 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
258 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
262 usbd_printBCD(char *cp, int bcd)
264 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
268 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp)
270 usb_device_descriptor_t *udd = &dev->ddesc;
271 char vendor[USB_MAX_STRING_LEN];
272 char product[USB_MAX_STRING_LEN];
273 int bcdDevice, bcdUSB;
275 usbd_devinfo_vp(dev, vendor, product);
276 cp += sprintf(cp, "%s %s", vendor, product);
278 cp += sprintf(cp, ", class %d/%d",
279 udd->bDeviceClass, udd->bDeviceSubClass);
280 bcdUSB = UGETW(udd->bcdUSB);
281 bcdDevice = UGETW(udd->bcdDevice);
282 cp += sprintf(cp, ", rev ");
283 cp += usbd_printBCD(cp, bcdUSB);
285 cp += usbd_printBCD(cp, bcdDevice);
286 cp += sprintf(cp, ", addr %d", dev->address);
290 /* Delay for a certain number of ms */
292 usb_delay_ms(usbd_bus_handle bus, u_int ms)
294 /* Wait at least two clock ticks so we know the time has passed. */
295 if (bus->use_polling || cold)
296 delay((ms+1) * 1000);
298 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
301 /* Delay given a device handle. */
303 usbd_delay_ms(usbd_device_handle dev, u_int ms)
305 usb_delay_ms(dev->bus, ms);
309 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
311 usb_device_request_t req;
315 req.bmRequestType = UT_WRITE_CLASS_OTHER;
316 req.bRequest = UR_SET_FEATURE;
317 USETW(req.wValue, UHF_PORT_RESET);
318 USETW(req.wIndex, port);
319 USETW(req.wLength, 0);
320 err = usbd_do_request(dev, &req, 0);
321 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
322 port, usbd_errstr(err)));
327 /* Wait for device to recover from reset. */
328 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
329 err = usbd_get_port_status(dev, port, ps);
331 DPRINTF(("usbd_reset_port: get status failed %d\n",
335 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
337 return (USBD_TIMEOUT);
338 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
341 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
345 /* Wait for the device to recover from reset. */
346 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
350 usb_interface_descriptor_t *
351 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
353 char *p = (char *)cd;
354 char *end = p + UGETW(cd->wTotalLength);
355 usb_interface_descriptor_t *d;
356 int curidx, lastidx, curaidx = 0;
358 for (curidx = lastidx = -1; p < end; ) {
359 d = (usb_interface_descriptor_t *)p;
360 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
362 ifaceidx, curidx, altidx, curaidx,
363 d->bLength, d->bDescriptorType));
364 if (d->bLength == 0) /* bad descriptor */
367 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
368 if (d->bInterfaceNumber != lastidx) {
369 lastidx = d->bInterfaceNumber;
374 if (ifaceidx == curidx && altidx == curaidx)
381 usb_endpoint_descriptor_t *
382 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
385 char *p = (char *)cd;
386 char *end = p + UGETW(cd->wTotalLength);
387 usb_interface_descriptor_t *d;
388 usb_endpoint_descriptor_t *e;
391 d = usbd_find_idesc(cd, ifaceidx, altidx);
394 if (endptidx >= d->bNumEndpoints) /* quick exit */
398 for (p = (char *)d + d->bLength; p < end; ) {
399 e = (usb_endpoint_descriptor_t *)p;
400 if (e->bLength == 0) /* bad descriptor */
403 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
405 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
407 if (curidx == endptidx)
415 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
417 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
421 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
424 ifc->idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
427 ifc->index = ifaceidx;
428 ifc->altindex = altidx;
429 nendpt = ifc->idesc->bNumEndpoints;
430 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
432 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
434 if (ifc->endpoints == NULL)
437 ifc->endpoints = NULL;
439 p = (char *)ifc->idesc + ifc->idesc->bLength;
440 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
441 #define ed ((usb_endpoint_descriptor_t *)p)
442 for (endpt = 0; endpt < nendpt; endpt++) {
443 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
444 for (; p < end; p += ed->bLength) {
445 ed = (usb_endpoint_descriptor_t *)p;
446 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
448 p, end, ed->bLength, ed->bDescriptorType));
449 if (p + ed->bLength <= end && ed->bLength != 0 &&
450 ed->bDescriptorType == UDESC_ENDPOINT)
452 if (ed->bLength == 0 ||
453 ed->bDescriptorType == UDESC_INTERFACE)
456 /* passed end, or bad desc */
457 DPRINTF(("usbd_fill_iface_data: bad descriptor(s): %s\n",
458 ed->bLength == 0 ? "0 length" :
459 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
463 ifc->endpoints[endpt].edesc = ed;
464 ifc->endpoints[endpt].refcnt = 0;
468 LIST_INIT(&ifc->pipes);
469 return (USBD_NORMAL_COMPLETION);
472 if (ifc->endpoints != NULL)
473 free(ifc->endpoints, M_USB);
478 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
480 usbd_interface_handle ifc = &dev->ifaces[ifcno];
482 free(ifc->endpoints, M_USB);
486 usbd_set_config(usbd_device_handle dev, int conf)
488 usb_device_request_t req;
490 req.bmRequestType = UT_WRITE_DEVICE;
491 req.bRequest = UR_SET_CONFIG;
492 USETW(req.wValue, conf);
493 USETW(req.wIndex, 0);
494 USETW(req.wLength, 0);
495 return (usbd_do_request(dev, &req, 0));
499 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
502 usb_config_descriptor_t cd;
505 if (no == USB_UNCONFIG_NO)
506 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
508 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
509 /* Figure out what config index to use. */
510 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
511 err = usbd_get_config_desc(dev, index, &cd);
514 if (cd.bConfigurationValue == no)
515 return (usbd_set_config_index(dev, index, msg));
521 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
524 usb_config_descriptor_t cd, *cdp;
526 int ifcidx, nifc, len, selfpowered, power;
528 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
530 /* XXX check that all interfaces are idle */
531 if (dev->config != USB_UNCONFIG_NO) {
532 DPRINTF(("usbd_set_config_index: free old config\n"));
533 /* Free all configuration data structures. */
534 nifc = dev->cdesc->bNumInterface;
535 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
536 usbd_free_iface_data(dev, ifcidx);
537 free(dev->ifaces, M_USB);
538 free(dev->cdesc, M_USB);
541 dev->config = USB_UNCONFIG_NO;
544 if (index == USB_UNCONFIG_INDEX) {
545 /* We are unconfiguring the device, so leave unallocated. */
546 DPRINTF(("usbd_set_config_index: set config 0\n"));
547 err = usbd_set_config(dev, USB_UNCONFIG_NO);
549 DPRINTF(("usbd_set_config_index: setting config=0 "
550 "failed, error=%s\n", usbd_errstr(err)));
554 /* Get the short descriptor. */
555 err = usbd_get_config_desc(dev, index, &cd);
558 len = UGETW(cd.wTotalLength);
559 cdp = malloc(len, M_USB, M_NOWAIT);
562 /* Get the full descriptor. */
563 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
566 if (cdp->bDescriptorType != UDESC_CONFIG) {
567 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
568 cdp->bDescriptorType));
573 /* Figure out if the device is self or bus powered. */
575 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
576 (cdp->bmAttributes & UC_SELF_POWERED)) {
577 /* May be self powered. */
578 if (cdp->bmAttributes & UC_BUS_POWERED) {
579 /* Must ask device. */
580 err = usbd_get_device_status(dev, &ds);
581 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
583 DPRINTF(("usbd_set_config_index: status=0x%04x, "
585 UGETW(ds.wStatus), usbd_errstr(err)));
589 DPRINTF(("usbd_set_config_index: (addr %d) attr=0x%02x, "
590 "selfpowered=%d, power=%d\n",
591 dev->address, cdp->bmAttributes,
592 selfpowered, cdp->bMaxPower * 2));
594 /* Check if we have enough power. */
596 if (dev->powersrc == NULL) {
597 DPRINTF(("usbd_set_config_index: No power source?\n"));
598 return (USBD_IOERROR);
601 power = cdp->bMaxPower * 2;
602 if (power > dev->powersrc->power) {
603 /* XXX print nicer message. */
605 printf("%s: device addr %d (config %d) exceeds power "
606 "budget, %d mA > %d mA\n",
607 USBDEVNAME(dev->bus->bdev), dev->address,
608 cdp->bConfigurationValue,
609 power, dev->powersrc->power);
614 dev->self_powered = selfpowered;
616 /* Set the actual configuration value. */
617 DPRINTF(("usbd_set_config_index: set config %d\n",
618 cdp->bConfigurationValue));
619 err = usbd_set_config(dev, cdp->bConfigurationValue);
621 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
623 cdp->bConfigurationValue, usbd_errstr(err)));
627 /* Allocate and fill interface data. */
628 nifc = cdp->bNumInterface;
629 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
631 if (dev->ifaces == NULL) {
635 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
637 dev->config = cdp->bConfigurationValue;
638 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
639 err = usbd_fill_iface_data(dev, ifcidx, 0);
641 while (--ifcidx >= 0)
642 usbd_free_iface_data(dev, ifcidx);
647 return (USBD_NORMAL_COMPLETION);
654 /* XXX add function for alternate settings */
657 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
658 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
663 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
664 dev, iface, ep, pipe));
665 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
677 SIMPLEQ_INIT(&p->queue);
678 err = dev->bus->methods->open_pipe(p);
680 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
682 ep->edesc->bEndpointAddress, usbd_errstr(err)));
686 /* Clear any stall and make sure DATA0 toggle will be used next. */
687 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT)
688 usbd_clear_endpoint_stall(p);
690 return (USBD_NORMAL_COMPLETION);
693 /* Abort the device control pipe. */
695 usbd_kill_pipe(usbd_pipe_handle pipe)
697 pipe->methods->close(pipe);
698 pipe->endpoint->refcnt--;
703 usbd_getnewaddr(usbd_bus_handle bus)
707 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
708 if (bus->devices[addr] == 0)
715 usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
718 struct usb_attach_arg uaa;
719 usb_device_descriptor_t *dd = &dev->ddesc;
720 int found, i, confi, nifaces;
723 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
725 #if defined(__FreeBSD__)
727 * XXX uaa is a static var. Not a problem as it _should_ be used only
728 * during probe and attach. Should be changed however.
731 bdev = device_add_child(parent, NULL, -1);
732 device_set_ivars(bdev, &uaa);
734 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
746 uaa.configno = UHUB_UNK_CONFIGURATION;
747 uaa.ifaceno = UHUB_UNK_INTERFACE;
748 uaa.vendor = UGETW(dd->idVendor);
749 uaa.product = UGETW(dd->idProduct);
750 uaa.release = UGETW(dd->bcdDevice);
752 /* First try with device specific drivers. */
753 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
754 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
756 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
757 if (dev->subdevs == NULL)
759 dev->subdevs[0] = dv;
761 return (USBD_NORMAL_COMPLETION);
764 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
766 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
767 dd->bNumConfigurations));
768 /* Next try with interface drivers. */
769 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
770 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
772 err = usbd_set_config_index(dev, confi, 1);
775 DPRINTF(("%s: port %d, set config at addr %d failed, "
776 "error=%s\n", USBDEVPTRNAME(parent), port,
777 addr, usbd_errstr(err)));
779 printf("%s: port %d, set config at addr %d failed\n",
780 USBDEVPTRNAME(parent), port, addr);
782 #if defined(__FreeBSD__)
783 device_delete_child(parent, bdev);
788 nifaces = dev->cdesc->bNumInterface;
789 uaa.configno = dev->cdesc->bConfigurationValue;
790 for (i = 0; i < nifaces; i++)
791 ifaces[i] = &dev->ifaces[i];
793 uaa.nifaces = nifaces;
794 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
795 if (dev->subdevs == NULL) {
796 #if defined(__FreeBSD__)
797 device_delete_child(parent, bdev);
803 for (i = 0; i < nifaces; i++) {
804 if (ifaces[i] == NULL)
805 continue; /* interface already claimed */
806 uaa.iface = ifaces[i];
807 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
808 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
811 dev->subdevs[found++] = dv;
812 dev->subdevs[found] = 0;
813 ifaces[i] = 0; /* consumed */
815 #if defined(__FreeBSD__)
816 /* create another child for the next iface */
817 bdev = device_add_child(parent, NULL, -1);
818 device_set_ivars(bdev, &uaa);
820 printf("%s: Device creation failed\n",
821 USBDEVNAME(dev->bus->bdev));
822 return (USBD_NORMAL_COMPLETION);
829 #if defined(__FreeBSD__)
830 /* remove the last created child again; it is unused */
831 device_delete_child(parent, bdev);
833 return (USBD_NORMAL_COMPLETION);
835 free(dev->subdevs, M_USB);
838 /* No interfaces were attached in any of the configurations. */
840 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
841 usbd_set_config_index(dev, 0, 0);
843 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
845 /* Finally try the generic driver. */
848 uaa.configno = UHUB_UNK_CONFIGURATION;
849 uaa.ifaceno = UHUB_UNK_INTERFACE;
850 uaa.vendor = UHUB_UNK_VENDOR;
851 uaa.product = UHUB_UNK_PRODUCT;
852 uaa.release = UHUB_UNK_RELEASE;
853 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
855 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
856 if (dev->subdevs == 0)
858 dev->subdevs[0] = dv;
860 return (USBD_NORMAL_COMPLETION);
864 * The generic attach failed, but leave the device as it is.
865 * We just did not find any drivers, that's all. The device is
866 * fully operational and not harming anyone.
868 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
869 #if defined(__FreeBSD__)
870 device_delete_child(parent, bdev);
872 return (USBD_NORMAL_COMPLETION);
877 * Called when a new device has been put in the powered state,
878 * but not yet in the addressed state.
879 * Get initial descriptor, set the address, get full descriptor,
880 * and attach a driver.
883 usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
884 int lowspeed, int port, struct usbd_port *up)
886 usbd_device_handle dev;
887 usb_device_descriptor_t *dd;
892 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d lowspeed=%d\n",
893 bus, port, depth, lowspeed));
894 addr = usbd_getnewaddr(bus);
896 printf("%s: No free USB addresses, new device ignored.\n",
897 USBDEVNAME(bus->bdev));
898 return (USBD_NO_ADDR);
901 dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
904 memset(dev, 0, sizeof(*dev));
908 /* Set up default endpoint handle. */
909 dev->def_ep.edesc = &dev->def_ep_desc;
911 /* Set up default endpoint descriptor. */
912 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
913 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
914 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
915 dev->def_ep_desc.bmAttributes = UE_CONTROL;
916 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
917 dev->def_ep_desc.bInterval = 0;
919 dev->quirks = &usbd_no_quirk;
920 dev->address = USB_START_ADDR;
921 dev->ddesc.bMaxPacketSize = 0;
922 dev->lowspeed = lowspeed != 0;
925 dev->langid = USBD_NOLANG;
926 dev->cookie.cookie = ++usb_cookie_no;
928 /* Establish the default pipe. */
929 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
932 usbd_remove_device(dev, up);
938 /* Try a few times in case the device is slow (i.e. outside specs.) */
939 for (i = 0; i < 3; i++) {
940 /* Get the first 8 bytes of the device descriptor. */
941 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
944 usbd_delay_ms(dev, 200);
947 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
949 usbd_remove_device(dev, up);
953 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
954 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, ls=%d\n",
955 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
956 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
959 if (dd->bDescriptorType != UDESC_DEVICE) {
960 /* Illegal device descriptor */
961 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
962 dd->bDescriptorType));
963 usbd_remove_device(dev, up);
967 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
968 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
969 usbd_remove_device(dev, up);
973 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
975 err = usbd_reload_device_desc(dev);
977 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
979 usbd_remove_device(dev, up);
983 /* Set the address */
984 err = usbd_set_address(dev, addr);
985 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
987 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
988 err = USBD_SET_ADDR_FAILED;
989 usbd_remove_device(dev, up);
992 /* Allow device time to set new address */
993 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
995 dev->address = addr; /* New device address now */
996 bus->devices[addr] = dev;
998 /* Assume 100mA bus powered for now. Changed when configured. */
999 dev->power = USB_MIN_POWER;
1000 dev->self_powered = 0;
1002 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1003 addr, dev, parent));
1005 err = usbd_probe_and_attach(parent, dev, port, addr);
1007 usbd_remove_device(dev, up);
1011 usbd_add_event(USB_EVENT_DEVICE_ATTACH, dev);
1012 return (USBD_NORMAL_COMPLETION);
1016 usbd_reload_device_desc(usbd_device_handle dev)
1021 /* Get the full device descriptor. */
1022 for (i = 0; i < 3; ++i) {
1023 err = usbd_get_device_desc(dev, &dev->ddesc);
1026 usbd_delay_ms(dev, 200);
1031 /* Figure out what's wrong with this device. */
1032 dev->quirks = usbd_find_quirk(&dev->ddesc);
1034 return (USBD_NORMAL_COMPLETION);
1038 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1040 DPRINTF(("usbd_remove_device: %p\n", dev));
1042 if (dev->default_pipe != NULL)
1043 usbd_kill_pipe(dev->default_pipe);
1045 dev->bus->devices[dev->address] = 0;
1050 #if defined(__NetBSD__) || defined(__OpenBSD__)
1052 usbd_print(void *aux, const char *pnp)
1054 struct usb_attach_arg *uaa = aux;
1057 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1059 if (!uaa->usegeneric)
1061 usbd_devinfo(uaa->device, 1, devinfo);
1062 printf("%s, %s", devinfo, pnp);
1065 printf(" port %d", uaa->port);
1066 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1067 printf(" configuration %d", uaa->configno);
1068 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1069 printf(" interface %d", uaa->ifaceno);
1072 * It gets very crowded with these locators on the attach line.
1073 * They are not really needed since they are printed in the clear
1076 if (uaa->vendor != UHUB_UNK_VENDOR)
1077 printf(" vendor 0x%04x", uaa->vendor);
1078 if (uaa->product != UHUB_UNK_PRODUCT)
1079 printf(" product 0x%04x", uaa->product);
1080 if (uaa->release != UHUB_UNK_RELEASE)
1081 printf(" release 0x%04x", uaa->release);
1086 #if defined(__NetBSD__)
1088 usbd_submatch(struct device *parent, struct cfdata *cf, void *aux)
1090 #elif defined(__OpenBSD__)
1092 usbd_submatch(struct device *parent, void *match, void *aux)
1094 struct cfdata *cf = match;
1096 struct usb_attach_arg *uaa = aux;
1098 DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1099 "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1100 uaa->port, cf->uhubcf_port,
1101 uaa->configno, cf->uhubcf_configuration,
1102 uaa->ifaceno, cf->uhubcf_interface,
1103 uaa->vendor, cf->uhubcf_vendor,
1104 uaa->product, cf->uhubcf_product,
1105 uaa->release, cf->uhubcf_release));
1106 if (uaa->port != 0 && /* root hub has port 0, it should match */
1108 cf->uhubcf_port != UHUB_UNK_PORT &&
1109 cf->uhubcf_port != uaa->port) ||
1110 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1111 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1112 cf->uhubcf_configuration != uaa->configno) ||
1113 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1114 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1115 cf->uhubcf_interface != uaa->ifaceno) ||
1116 (uaa->vendor != UHUB_UNK_VENDOR &&
1117 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1118 cf->uhubcf_vendor != uaa->vendor) ||
1119 (uaa->product != UHUB_UNK_PRODUCT &&
1120 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1121 cf->uhubcf_product != uaa->product) ||
1122 (uaa->release != UHUB_UNK_RELEASE &&
1123 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1124 cf->uhubcf_release != uaa->release)
1128 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1134 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di)
1136 struct usbd_port *p;
1139 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1140 di->udi_addr = dev->address;
1143 for (i = 0; dev->subdevs[i] &&
1144 i < USB_MAX_DEVNAMES; i++) {
1145 strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1146 USB_MAX_DEVNAMELEN);
1147 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0'; /* terminate */
1152 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1153 di->udi_devnames[i][0] = 0; /* empty */
1155 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product);
1156 usbd_printBCD(di->udi_release, UGETW(dev->ddesc.bcdDevice));
1157 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1158 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1159 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1160 di->udi_class = dev->ddesc.bDeviceClass;
1161 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1162 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1163 di->udi_config = dev->config;
1164 di->udi_power = dev->self_powered ? 0 : dev->power;
1165 di->udi_lowspeed = dev->lowspeed;
1169 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1170 i < dev->hub->hubdesc.bNbrPorts;
1172 p = &dev->hub->ports[i];
1174 err = p->device->address;
1176 s = UGETW(p->status.wPortStatus);
1177 if (s & UPS_PORT_ENABLED)
1178 err = USB_PORT_ENABLED;
1179 else if (s & UPS_SUSPEND)
1180 err = USB_PORT_SUSPENDED;
1181 else if (s & UPS_PORT_POWER)
1182 err = USB_PORT_POWERED;
1184 err = USB_PORT_DISABLED;
1186 di->udi_ports[i] = err;
1188 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1194 usb_free_device(usbd_device_handle dev)
1198 if (dev->default_pipe != NULL)
1199 usbd_kill_pipe(dev->default_pipe);
1200 if (dev->ifaces != NULL) {
1201 nifc = dev->cdesc->bNumInterface;
1202 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1203 usbd_free_iface_data(dev, ifcidx);
1204 free(dev->ifaces, M_USB);
1206 if (dev->cdesc != NULL)
1207 free(dev->cdesc, M_USB);
1208 if (dev->subdevs != NULL)
1209 free(dev->subdevs, M_USB);
1214 * The general mechanism for detaching drivers works as follows: Each
1215 * driver is responsible for maintaining a reference count on the
1216 * number of outstanding references to its softc (e.g. from
1217 * processing hanging in a read or write). The detach method of the
1218 * driver decrements this counter and flags in the softc that the
1219 * driver is dying and then wakes any sleepers. It then sleeps on the
1220 * softc. Each place that can sleep must maintain the reference
1221 * count. When the reference count drops to -1 (0 is the normal value
1222 * of the reference count) the a wakeup on the softc is performed
1223 * signaling to the detach waiter that all references are gone.
1227 * Called from process context when we discover that a port has
1228 * been disconnected.
1231 usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1233 usbd_device_handle dev = up->device;
1234 const char *hubname = USBDEVPTRNAME(parent);
1237 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1238 up, dev, up->portno));
1242 printf("usb_disconnect_port: no device\n");
1247 if (dev->cdesc == NULL) {
1248 /* Partially attached device, just drop it. */
1249 dev->bus->devices[dev->address] = 0;
1254 usbd_add_event(USB_EVENT_DEVICE_DETACH, dev);
1256 if (dev->subdevs != NULL) {
1257 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1258 for (i = 0; dev->subdevs[i]; i++) {
1259 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1261 if (up->portno != 0)
1262 printf(" port %d", up->portno);
1263 printf(" (addr %d) disconnected\n", dev->address);
1264 #if defined(__NetBSD__) || defined(__OpenBSD__)
1265 config_detach(dev->subdevs[i], DETACH_FORCE);
1266 #elif defined(__FreeBSD__)
1267 device_delete_child(device_get_parent(dev->subdevs[i]),
1274 /*usbd_add_event(USB_EVENT_DEVICE_DETACH, dev);*/
1275 dev->bus->devices[dev->address] = NULL;
1277 usb_free_device(dev);
1281 void *usb_realloc(void *p, u_int size, int pool, int flags)
1285 q = malloc(size, pool, flags);