Annotation of src/sys/dev/usb/usb_subr.c, Revision 1.158
1.158 ! drochner 1: /* $NetBSD: usb_subr.c,v 1.157 2008/05/27 20:46:16 drochner Exp $ */
1.56 augustss 2: /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */
1.1 augustss 3:
4: /*
1.121 mycroft 5: * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
1.1 augustss 6: * All rights reserved.
7: *
1.9 augustss 8: * This code is derived from software contributed to The NetBSD Foundation
1.76 augustss 9: * by Lennart Augustsson (lennart@augustsson.net) at
1.9 augustss 10: * Carlstedt Research & Technology.
1.1 augustss 11: *
12: * Redistribution and use in source and binary forms, with or without
13: * modification, are permitted provided that the following conditions
14: * are met:
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: *
21: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31: * POSSIBILITY OF SUCH DAMAGE.
32: */
1.91 lukem 33:
34: #include <sys/cdefs.h>
1.158 ! drochner 35: __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.157 2008/05/27 20:46:16 drochner Exp $");
1.104 martin 36:
1.140 pavel 37: #include "opt_compat_netbsd.h"
1.104 martin 38: #include "opt_usbverbose.h"
1.1 augustss 39:
40: #include <sys/param.h>
41: #include <sys/systm.h>
42: #include <sys/kernel.h>
43: #include <sys/malloc.h>
44: #include <sys/device.h>
1.56 augustss 45: #include <sys/select.h>
1.1 augustss 46: #include <sys/proc.h>
1.46 augustss 47:
1.149 ad 48: #include <sys/bus.h>
1.1 augustss 49:
50: #include <dev/usb/usb.h>
51:
52: #include <dev/usb/usbdi.h>
53: #include <dev/usb/usbdi_util.h>
54: #include <dev/usb/usbdivar.h>
55: #include <dev/usb/usbdevs.h>
56: #include <dev/usb/usb_quirks.h>
57:
1.158 ! drochner 58: #include "locators.h"
! 59:
1.1 augustss 60: #ifdef USB_DEBUG
1.35 augustss 61: #define DPRINTF(x) if (usbdebug) logprintf x
62: #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
1.1 augustss 63: extern int usbdebug;
64: #else
65: #define DPRINTF(x)
66: #define DPRINTFN(n,x)
67: #endif
68:
1.82 augustss 69: Static usbd_status usbd_set_config(usbd_device_handle, int);
1.124 augustss 70: Static void usbd_devinfo(usbd_device_handle, int, char *, size_t);
1.123 augustss 71: Static void usbd_devinfo_vp(usbd_device_handle dev,
1.139 pavel 72: char *v,
73: char *p, int usedev,
74: int useencoded );
1.78 augustss 75: Static int usbd_getnewaddr(usbd_bus_handle bus);
1.118 drochner 76: Static int usbd_print(void *, const char *);
1.144 drochner 77: Static int usbd_ifprint(void *, const char *);
1.78 augustss 78: Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
79: Static void usbd_kill_pipe(usbd_pipe_handle);
1.155 drochner 80: usbd_status usbd_attach_roothub(device_t, usbd_device_handle);
1.78 augustss 81: Static usbd_status usbd_probe_and_attach(device_ptr_t parent,
82: usbd_device_handle dev, int port, int addr);
1.1 augustss 83:
1.69 augustss 84: Static u_int32_t usb_cookie_no = 0;
1.27 augustss 85:
1.1 augustss 86: #ifdef USBVERBOSE
87: typedef u_int16_t usb_vendor_id_t;
88: typedef u_int16_t usb_product_id_t;
89:
90: /*
91: * Descriptions of of known vendors and devices ("products").
92: */
1.122 mycroft 93: struct usb_vendor {
94: usb_vendor_id_t vendor;
1.126 christos 95: const char *vendorname;
1.122 mycroft 96: };
97: struct usb_product {
1.1 augustss 98: usb_vendor_id_t vendor;
99: usb_product_id_t product;
1.126 christos 100: const char *productname;
1.1 augustss 101: };
102:
103: #include <dev/usb/usbdevs_data.h>
104: #endif /* USBVERBOSE */
105:
1.84 jdolecek 106: Static const char * const usbd_error_strs[] = {
1.12 augustss 107: "NORMAL_COMPLETION",
108: "IN_PROGRESS",
109: "PENDING_REQUESTS",
110: "NOT_STARTED",
111: "INVAL",
112: "NOMEM",
113: "CANCELLED",
114: "BAD_ADDRESS",
115: "IN_USE",
116: "NO_ADDR",
117: "SET_ADDR_FAILED",
118: "NO_POWER",
119: "TOO_DEEP",
120: "IOERROR",
121: "NOT_CONFIGURED",
122: "TIMEOUT",
123: "SHORT_XFER",
124: "STALLED",
1.33 augustss 125: "INTERRUPTED",
1.12 augustss 126: "XXX",
127: };
1.1 augustss 128:
1.42 thorpej 129: const char *
1.78 augustss 130: usbd_errstr(usbd_status err)
1.39 augustss 131: {
132: static char buffer[5];
133:
1.42 thorpej 134: if (err < USBD_ERROR_MAX) {
1.39 augustss 135: return usbd_error_strs[err];
136: } else {
137: snprintf(buffer, sizeof buffer, "%d", err);
138: return buffer;
139: }
140: }
141:
1.13 augustss 142: usbd_status
1.78 augustss 143: usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
1.114 mycroft 144: usb_string_descriptor_t *sdesc, int *sizep)
1.13 augustss 145: {
146: usb_device_request_t req;
1.116 mycroft 147: usbd_status err;
148: int actlen;
1.13 augustss 149:
150: req.bmRequestType = UT_READ_DEVICE;
151: req.bRequest = UR_GET_DESCRIPTOR;
152: USETW2(req.wValue, UDESC_STRING, sindex);
153: USETW(req.wIndex, langid);
1.146 christos 154: USETW(req.wLength, 2); /* only size byte first */
1.116 mycroft 155: err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
156: &actlen, USBD_DEFAULT_TIMEOUT);
157: if (err)
158: return (err);
159:
1.146 christos 160: if (actlen < 2)
161: return (USBD_SHORT_XFER);
162:
163: USETW(req.wLength, sdesc->bLength); /* the whole string */
164: err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
165: &actlen, USBD_DEFAULT_TIMEOUT);
166: if (err)
167: return (err);
168:
1.116 mycroft 169: if (actlen != sdesc->bLength) {
170: DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
171: sdesc->bLength, actlen));
172: }
173:
174: *sizep = actlen;
175: return (USBD_NORMAL_COMPLETION);
1.13 augustss 176: }
177:
1.128 enami 178: static void
179: usbd_trim_spaces(char *p)
1.83 augustss 180: {
1.128 enami 181: char *q, *e;
1.83 augustss 182:
1.128 enami 183: q = e = p;
184: while (*q == ' ') /* skip leading spaces */
1.85 augustss 185: q++;
1.128 enami 186: while ((*p = *q++)) /* copy string */
187: if (*p++ != ' ') /* remember last non-space */
188: e = p;
189: *e = '\0'; /* kill trailing spaces */
1.83 augustss 190: }
191:
1.123 augustss 192: Static void
1.139 pavel 193: usbd_devinfo_vp(usbd_device_handle dev, char *v,
194: char *p, int usedev, int useencoded)
1.1 augustss 195: {
196: usb_device_descriptor_t *udd = &dev->ddesc;
1.2 is 197: #ifdef USBVERBOSE
1.122 mycroft 198: int n;
1.2 is 199: #endif
1.1 augustss 200:
1.128 enami 201: v[0] = p[0] = '\0';
202: if (dev == NULL)
1.52 augustss 203: return;
204:
1.82 augustss 205: if (usedev) {
1.139 pavel 206: if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) ==
1.128 enami 207: USBD_NORMAL_COMPLETION)
208: usbd_trim_spaces(v);
1.139 pavel 209: if (usbd_get_string0(dev, udd->iProduct, p, useencoded) ==
1.128 enami 210: USBD_NORMAL_COMPLETION)
211: usbd_trim_spaces(p);
1.82 augustss 212: }
1.128 enami 213: /* There is no need for strlcpy & snprintf below. */
1.1 augustss 214: #ifdef USBVERBOSE
1.128 enami 215: if (v[0] == '\0')
1.122 mycroft 216: for (n = 0; n < usb_nvendors; n++)
1.128 enami 217: if (usb_vendors[n].vendor == UGETW(udd->idVendor)) {
218: strcpy(v, usb_vendors[n].vendorname);
219: break;
220: }
221: if (p[0] == '\0')
1.122 mycroft 222: for (n = 0; n < usb_nproducts; n++)
223: if (usb_products[n].vendor == UGETW(udd->idVendor) &&
1.128 enami 224: usb_products[n].product == UGETW(udd->idProduct)) {
225: strcpy(p, usb_products[n].productname);
226: break;
227: }
1.1 augustss 228: #endif
1.128 enami 229: if (v[0] == '\0')
1.123 augustss 230: sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
1.128 enami 231: if (p[0] == '\0')
1.123 augustss 232: sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
1.1 augustss 233: }
234:
235: int
1.113 itojun 236: usbd_printBCD(char *cp, size_t l, int bcd)
1.1 augustss 237: {
1.113 itojun 238: return (snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff));
1.1 augustss 239: }
240:
1.124 augustss 241: Static void
1.113 itojun 242: usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l)
1.1 augustss 243: {
244: usb_device_descriptor_t *udd = &dev->ddesc;
1.133 christos 245: char *vendor, *product;
1.1 augustss 246: int bcdDevice, bcdUSB;
1.113 itojun 247: char *ep;
248:
1.133 christos 249: vendor = malloc(USB_MAX_ENCODED_STRING_LEN * 2, M_USB, M_NOWAIT);
250: if (vendor == NULL) {
251: *cp = '\0';
252: return;
253: }
254: product = &vendor[USB_MAX_ENCODED_STRING_LEN];
255:
1.113 itojun 256: ep = cp + l;
1.1 augustss 257:
1.139 pavel 258: usbd_devinfo_vp(dev, vendor, product, 1, 1);
1.113 itojun 259: cp += snprintf(cp, ep - cp, "%s %s", vendor, product);
1.1 augustss 260: if (showclass)
1.113 itojun 261: cp += snprintf(cp, ep - cp, ", class %d/%d",
262: udd->bDeviceClass, udd->bDeviceSubClass);
1.1 augustss 263: bcdUSB = UGETW(udd->bcdUSB);
264: bcdDevice = UGETW(udd->bcdDevice);
1.113 itojun 265: cp += snprintf(cp, ep - cp, ", rev ");
266: cp += usbd_printBCD(cp, ep - cp, bcdUSB);
1.1 augustss 267: *cp++ = '/';
1.113 itojun 268: cp += usbd_printBCD(cp, ep - cp, bcdDevice);
269: cp += snprintf(cp, ep - cp, ", addr %d", dev->address);
1.10 augustss 270: *cp = 0;
1.133 christos 271: free(vendor, M_USB);
1.1 augustss 272: }
273:
1.124 augustss 274: char *
275: usbd_devinfo_alloc(usbd_device_handle dev, int showclass)
276: {
277: char *devinfop;
278:
279: devinfop = malloc(DEVINFOSIZE, M_TEMP, M_WAITOK);
280: usbd_devinfo(dev, showclass, devinfop, DEVINFOSIZE);
281: return devinfop;
282: }
283:
284: void
285: usbd_devinfo_free(char *devinfop)
286: {
287: free(devinfop, M_TEMP);
288: }
289:
1.1 augustss 290: /* Delay for a certain number of ms */
291: void
1.78 augustss 292: usb_delay_ms(usbd_bus_handle bus, u_int ms)
1.1 augustss 293: {
294: /* Wait at least two clock ticks so we know the time has passed. */
1.64 augustss 295: if (bus->use_polling || cold)
1.1 augustss 296: delay((ms+1) * 1000);
297: else
298: tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
299: }
300:
1.23 augustss 301: /* Delay given a device handle. */
302: void
1.78 augustss 303: usbd_delay_ms(usbd_device_handle dev, u_int ms)
1.23 augustss 304: {
305: usb_delay_ms(dev->bus, ms);
306: }
307:
1.1 augustss 308: usbd_status
1.78 augustss 309: usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
1.1 augustss 310: {
311: usb_device_request_t req;
1.53 augustss 312: usbd_status err;
1.1 augustss 313: int n;
1.99 augustss 314:
1.1 augustss 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);
1.53 augustss 320: err = usbd_do_request(dev, &req, 0);
1.39 augustss 321: DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
1.53 augustss 322: port, usbd_errstr(err)));
323: if (err)
324: return (err);
1.1 augustss 325: n = 10;
326: do {
327: /* Wait for device to recover from reset. */
1.23 augustss 328: usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
1.53 augustss 329: err = usbd_get_port_status(dev, port, ps);
330: if (err) {
331: DPRINTF(("usbd_reset_port: get status failed %d\n",
332: err));
333: return (err);
1.1 augustss 334: }
1.92 augustss 335: /* If the device disappeared, just give up. */
336: if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
337: return (USBD_NORMAL_COMPLETION);
1.1 augustss 338: } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
1.73 augustss 339: if (n == 0)
340: return (USBD_TIMEOUT);
1.53 augustss 341: err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
1.1 augustss 342: #ifdef USB_DEBUG
1.53 augustss 343: if (err)
344: DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
345: err));
1.1 augustss 346: #endif
1.18 augustss 347:
348: /* Wait for the device to recover from reset. */
1.23 augustss 349: usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
1.53 augustss 350: return (err);
1.1 augustss 351: }
352:
1.12 augustss 353: usb_interface_descriptor_t *
1.78 augustss 354: usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
1.12 augustss 355: {
356: char *p = (char *)cd;
357: char *end = p + UGETW(cd->wTotalLength);
358: usb_interface_descriptor_t *d;
1.19 augustss 359: int curidx, lastidx, curaidx = 0;
1.12 augustss 360:
1.19 augustss 361: for (curidx = lastidx = -1; p < end; ) {
1.12 augustss 362: d = (usb_interface_descriptor_t *)p;
1.18 augustss 363: DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
1.99 augustss 364: "type=%d\n",
1.18 augustss 365: ifaceidx, curidx, altidx, curaidx,
366: d->bLength, d->bDescriptorType));
1.12 augustss 367: if (d->bLength == 0) /* bad descriptor */
368: break;
369: p += d->bLength;
370: if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
1.19 augustss 371: if (d->bInterfaceNumber != lastidx) {
372: lastidx = d->bInterfaceNumber;
1.12 augustss 373: curidx++;
374: curaidx = 0;
375: } else
376: curaidx++;
377: if (ifaceidx == curidx && altidx == curaidx)
378: return (d);
379: }
380: }
1.58 augustss 381: return (NULL);
1.12 augustss 382: }
383:
384: usb_endpoint_descriptor_t *
1.99 augustss 385: usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
1.78 augustss 386: int endptidx)
1.12 augustss 387: {
388: char *p = (char *)cd;
389: char *end = p + UGETW(cd->wTotalLength);
390: usb_interface_descriptor_t *d;
391: usb_endpoint_descriptor_t *e;
392: int curidx;
393:
394: d = usbd_find_idesc(cd, ifaceidx, altidx);
1.53 augustss 395: if (d == NULL)
1.58 augustss 396: return (NULL);
1.12 augustss 397: if (endptidx >= d->bNumEndpoints) /* quick exit */
1.58 augustss 398: return (NULL);
1.12 augustss 399:
400: curidx = -1;
401: for (p = (char *)d + d->bLength; p < end; ) {
402: e = (usb_endpoint_descriptor_t *)p;
403: if (e->bLength == 0) /* bad descriptor */
404: break;
405: p += e->bLength;
406: if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
1.58 augustss 407: return (NULL);
1.12 augustss 408: if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
409: curidx++;
410: if (curidx == endptidx)
411: return (e);
412: }
1.1 augustss 413: }
1.58 augustss 414: return (NULL);
1.1 augustss 415: }
416:
417: usbd_status
1.78 augustss 418: usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
1.1 augustss 419: {
1.12 augustss 420: usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
1.77 augustss 421: usb_interface_descriptor_t *idesc;
1.1 augustss 422: char *p, *end;
423: int endpt, nendpt;
424:
1.18 augustss 425: DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
1.14 drochner 426: ifaceidx, altidx));
1.77 augustss 427: idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
428: if (idesc == NULL)
429: return (USBD_INVAL);
1.1 augustss 430: ifc->device = dev;
1.77 augustss 431: ifc->idesc = idesc;
1.13 augustss 432: ifc->index = ifaceidx;
433: ifc->altindex = altidx;
1.1 augustss 434: nendpt = ifc->idesc->bNumEndpoints;
1.59 augustss 435: DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
1.1 augustss 436: if (nendpt != 0) {
437: ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
438: M_USB, M_NOWAIT);
1.58 augustss 439: if (ifc->endpoints == NULL)
1.1 augustss 440: return (USBD_NOMEM);
441: } else
1.58 augustss 442: ifc->endpoints = NULL;
443: ifc->priv = NULL;
1.1 augustss 444: p = (char *)ifc->idesc + ifc->idesc->bLength;
445: end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
1.19 augustss 446: #define ed ((usb_endpoint_descriptor_t *)p)
1.1 augustss 447: for (endpt = 0; endpt < nendpt; endpt++) {
448: DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
449: for (; p < end; p += ed->bLength) {
1.14 drochner 450: DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
451: "len=%d type=%d\n",
1.1 augustss 452: p, end, ed->bLength, ed->bDescriptorType));
1.24 augustss 453: if (p + ed->bLength <= end && ed->bLength != 0 &&
1.1 augustss 454: ed->bDescriptorType == UDESC_ENDPOINT)
1.24 augustss 455: goto found;
1.59 augustss 456: if (ed->bLength == 0 ||
457: ed->bDescriptorType == UDESC_INTERFACE)
1.1 augustss 458: break;
459: }
1.24 augustss 460: /* passed end, or bad desc */
1.95 augustss 461: printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
462: ed->bLength == 0 ? "0 length" :
463: ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
464: "out of data");
1.24 augustss 465: goto bad;
466: found:
1.1 augustss 467: ifc->endpoints[endpt].edesc = ed;
1.95 augustss 468: if (dev->speed == USB_SPEED_HIGH) {
469: u_int mps;
470: /* Control and bulk endpoints have max packet limits. */
471: switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
472: case UE_CONTROL:
473: mps = USB_2_MAX_CTRL_PACKET;
474: goto check;
475: case UE_BULK:
476: mps = USB_2_MAX_BULK_PACKET;
477: check:
478: if (UGETW(ed->wMaxPacketSize) != mps) {
479: USETW(ed->wMaxPacketSize, mps);
480: #ifdef DIAGNOSTIC
481: printf("usbd_fill_iface_data: bad max "
482: "packet size\n");
483: #endif
484: }
485: break;
486: default:
487: break;
488: }
489: }
1.1 augustss 490: ifc->endpoints[endpt].refcnt = 0;
1.24 augustss 491: p += ed->bLength;
1.1 augustss 492: }
1.19 augustss 493: #undef ed
1.1 augustss 494: LIST_INIT(&ifc->pipes);
495: return (USBD_NORMAL_COMPLETION);
1.24 augustss 496:
1.1 augustss 497: bad:
1.77 augustss 498: if (ifc->endpoints != NULL) {
1.59 augustss 499: free(ifc->endpoints, M_USB);
1.77 augustss 500: ifc->endpoints = NULL;
501: }
1.24 augustss 502: return (USBD_INVAL);
1.1 augustss 503: }
504:
505: void
1.78 augustss 506: usbd_free_iface_data(usbd_device_handle dev, int ifcno)
1.1 augustss 507: {
508: usbd_interface_handle ifc = &dev->ifaces[ifcno];
509: if (ifc->endpoints)
510: free(ifc->endpoints, M_USB);
511: }
512:
1.69 augustss 513: Static usbd_status
1.78 augustss 514: usbd_set_config(usbd_device_handle dev, int conf)
1.7 augustss 515: {
516: usb_device_request_t req;
517:
518: req.bmRequestType = UT_WRITE_DEVICE;
519: req.bRequest = UR_SET_CONFIG;
520: USETW(req.wValue, conf);
521: USETW(req.wIndex, 0);
522: USETW(req.wLength, 0);
523: return (usbd_do_request(dev, &req, 0));
524: }
525:
1.1 augustss 526: usbd_status
1.78 augustss 527: usbd_set_config_no(usbd_device_handle dev, int no, int msg)
1.1 augustss 528: {
1.12 augustss 529: int index;
530: usb_config_descriptor_t cd;
1.53 augustss 531: usbd_status err;
1.12 augustss 532:
1.75 augustss 533: if (no == USB_UNCONFIG_NO)
534: return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
535:
1.12 augustss 536: DPRINTFN(5,("usbd_set_config_no: %d\n", no));
537: /* Figure out what config index to use. */
538: for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
1.53 augustss 539: err = usbd_get_config_desc(dev, index, &cd);
540: if (err)
541: return (err);
1.12 augustss 542: if (cd.bConfigurationValue == no)
543: return (usbd_set_config_index(dev, index, msg));
544: }
545: return (USBD_INVAL);
546: }
547:
548: usbd_status
1.78 augustss 549: usbd_set_config_index(usbd_device_handle dev, int index, int msg)
1.12 augustss 550: {
1.1 augustss 551: usb_config_descriptor_t cd, *cdp;
1.53 augustss 552: usbd_status err;
1.103 augustss 553: int i, ifcidx, nifc, len, selfpowered, power;
1.1 augustss 554:
1.12 augustss 555: DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
1.1 augustss 556:
1.145 drochner 557: if (index >= dev->ddesc.bNumConfigurations &&
558: index != USB_UNCONFIG_NO) {
559: /* panic? */
560: printf("usbd_set_config_index: illegal index\n");
561: return (USBD_INVAL);
562: }
563:
1.1 augustss 564: /* XXX check that all interfaces are idle */
1.75 augustss 565: if (dev->config != USB_UNCONFIG_NO) {
1.12 augustss 566: DPRINTF(("usbd_set_config_index: free old config\n"));
1.1 augustss 567: /* Free all configuration data structures. */
568: nifc = dev->cdesc->bNumInterface;
1.12 augustss 569: for (ifcidx = 0; ifcidx < nifc; ifcidx++)
570: usbd_free_iface_data(dev, ifcidx);
1.1 augustss 571: free(dev->ifaces, M_USB);
572: free(dev->cdesc, M_USB);
1.57 augustss 573: dev->ifaces = NULL;
574: dev->cdesc = NULL;
1.75 augustss 575: dev->config = USB_UNCONFIG_NO;
576: }
577:
578: if (index == USB_UNCONFIG_INDEX) {
579: /* We are unconfiguring the device, so leave unallocated. */
580: DPRINTF(("usbd_set_config_index: set config 0\n"));
581: err = usbd_set_config(dev, USB_UNCONFIG_NO);
1.136 christos 582: if (err) {
1.75 augustss 583: DPRINTF(("usbd_set_config_index: setting config=0 "
584: "failed, error=%s\n", usbd_errstr(err)));
1.136 christos 585: }
1.75 augustss 586: return (err);
1.1 augustss 587: }
588:
1.74 augustss 589: /* Get the short descriptor. */
1.53 augustss 590: err = usbd_get_config_desc(dev, index, &cd);
591: if (err)
592: return (err);
1.1 augustss 593: len = UGETW(cd.wTotalLength);
594: cdp = malloc(len, M_USB, M_NOWAIT);
1.53 augustss 595: if (cdp == NULL)
1.1 augustss 596: return (USBD_NOMEM);
1.103 augustss 597:
598: /* Get the full descriptor. Try a few times for slow devices. */
599: for (i = 0; i < 3; i++) {
600: err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
601: if (!err)
602: break;
603: usbd_delay_ms(dev, 200);
604: }
1.53 augustss 605: if (err)
1.1 augustss 606: goto bad;
1.103 augustss 607:
1.18 augustss 608: if (cdp->bDescriptorType != UDESC_CONFIG) {
609: DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
610: cdp->bDescriptorType));
1.53 augustss 611: err = USBD_INVAL;
1.18 augustss 612: goto bad;
613: }
1.74 augustss 614:
1.142 drochner 615: /*
616: * Figure out if the device is self or bus powered.
617: */
618: #if 0 /* XXX various devices don't report the power state correctly */
1.1 augustss 619: selfpowered = 0;
1.141 drochner 620: err = usbd_get_device_status(dev, &ds);
621: if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
622: selfpowered = 1;
1.142 drochner 623: #endif
624: /*
625: * Use the power state in the configuration we are going
626: * to set. This doesn't necessarily reflect the actual
627: * power state of the device; the driver can control this
628: * by choosing the appropriate configuration.
629: */
630: selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
1.141 drochner 631:
1.81 augustss 632: DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
633: "selfpowered=%d, power=%d\n",
1.99 augustss 634: cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
1.33 augustss 635: selfpowered, cdp->bMaxPower * 2));
1.74 augustss 636:
637: /* Check if we have enough power. */
1.142 drochner 638: #if 0 /* this is a no-op, see above */
1.141 drochner 639: if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
640: if (msg)
641: printf("%s: device addr %d (config %d): "
642: "can't set self powered configuration\n",
643: USBDEVNAME(dev->bus->bdev), dev->address,
644: cdp->bConfigurationValue);
645: err = USBD_NO_POWER;
646: goto bad;
647: }
1.142 drochner 648: #endif
1.1 augustss 649: #ifdef USB_DEBUG
1.53 augustss 650: if (dev->powersrc == NULL) {
1.44 augustss 651: DPRINTF(("usbd_set_config_index: No power source?\n"));
1.141 drochner 652: err = USBD_IOERROR;
653: goto bad;
1.1 augustss 654: }
655: #endif
656: power = cdp->bMaxPower * 2;
657: if (power > dev->powersrc->power) {
1.81 augustss 658: DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
1.1 augustss 659: /* XXX print nicer message. */
1.7 augustss 660: if (msg)
1.18 augustss 661: printf("%s: device addr %d (config %d) exceeds power "
662: "budget, %d mA > %d mA\n",
1.151 drochner 663: device_xname(dev->bus->usbctl), dev->address,
1.99 augustss 664: cdp->bConfigurationValue,
1.7 augustss 665: power, dev->powersrc->power);
1.53 augustss 666: err = USBD_NO_POWER;
1.1 augustss 667: goto bad;
668: }
669: dev->power = power;
670: dev->self_powered = selfpowered;
671:
1.74 augustss 672: /* Set the actual configuration value. */
1.18 augustss 673: DPRINTF(("usbd_set_config_index: set config %d\n",
674: cdp->bConfigurationValue));
1.53 augustss 675: err = usbd_set_config(dev, cdp->bConfigurationValue);
676: if (err) {
1.14 drochner 677: DPRINTF(("usbd_set_config_index: setting config=%d failed, "
1.39 augustss 678: "error=%s\n",
1.53 augustss 679: cdp->bConfigurationValue, usbd_errstr(err)));
1.1 augustss 680: goto bad;
681: }
1.74 augustss 682:
683: /* Allocate and fill interface data. */
1.1 augustss 684: nifc = cdp->bNumInterface;
1.99 augustss 685: dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
1.1 augustss 686: M_USB, M_NOWAIT);
1.58 augustss 687: if (dev->ifaces == NULL) {
1.53 augustss 688: err = USBD_NOMEM;
1.1 augustss 689: goto bad;
690: }
1.12 augustss 691: DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
1.1 augustss 692: dev->cdesc = cdp;
693: dev->config = cdp->bConfigurationValue;
1.12 augustss 694: for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
1.53 augustss 695: err = usbd_fill_iface_data(dev, ifcidx, 0);
696: if (err) {
1.12 augustss 697: while (--ifcidx >= 0)
698: usbd_free_iface_data(dev, ifcidx);
1.1 augustss 699: goto bad;
700: }
701: }
702:
703: return (USBD_NORMAL_COMPLETION);
704:
705: bad:
706: free(cdp, M_USB);
1.53 augustss 707: return (err);
1.1 augustss 708: }
709:
710: /* XXX add function for alternate settings */
711:
712: usbd_status
1.78 augustss 713: usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
714: struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
1.1 augustss 715: {
716: usbd_pipe_handle p;
1.53 augustss 717: usbd_status err;
1.1 augustss 718:
719: DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
720: dev, iface, ep, pipe));
721: p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
1.53 augustss 722: if (p == NULL)
1.1 augustss 723: return (USBD_NOMEM);
724: p->device = dev;
725: p->iface = iface;
726: p->endpoint = ep;
727: ep->refcnt++;
728: p->refcnt = 1;
1.53 augustss 729: p->intrxfer = 0;
1.1 augustss 730: p->running = 0;
1.70 augustss 731: p->aborting = 0;
1.34 augustss 732: p->repeat = 0;
1.63 augustss 733: p->interval = ival;
1.1 augustss 734: SIMPLEQ_INIT(&p->queue);
1.53 augustss 735: err = dev->bus->methods->open_pipe(p);
736: if (err) {
1.39 augustss 737: DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
738: "%s\n",
1.53 augustss 739: ep->edesc->bEndpointAddress, usbd_errstr(err)));
1.1 augustss 740: free(p, M_USB);
1.53 augustss 741: return (err);
1.1 augustss 742: }
743: *pipe = p;
744: return (USBD_NORMAL_COMPLETION);
745: }
746:
747: /* Abort the device control pipe. */
748: void
1.78 augustss 749: usbd_kill_pipe(usbd_pipe_handle pipe)
1.1 augustss 750: {
1.89 augustss 751: usbd_abort_pipe(pipe);
1.1 augustss 752: pipe->methods->close(pipe);
753: pipe->endpoint->refcnt--;
754: free(pipe, M_USB);
755: }
756:
757: int
1.78 augustss 758: usbd_getnewaddr(usbd_bus_handle bus)
1.1 augustss 759: {
1.18 augustss 760: int addr;
1.1 augustss 761:
1.18 augustss 762: for (addr = 1; addr < USB_MAX_DEVICES; addr++)
763: if (bus->devices[addr] == 0)
764: return (addr);
1.1 augustss 765: return (-1);
766: }
767:
1.155 drochner 768: usbd_status
769: usbd_attach_roothub(device_t parent, usbd_device_handle dev)
770: {
771: struct usb_attach_arg uaa;
772: usb_device_descriptor_t *dd = &dev->ddesc;
773: device_t dv;
774:
775: uaa.device = dev;
776: uaa.usegeneric = 0;
777: uaa.port = 0;
778: uaa.vendor = UGETW(dd->idVendor);
779: uaa.product = UGETW(dd->idProduct);
780: uaa.release = UGETW(dd->bcdDevice);
781: uaa.class = dd->bDeviceClass;
782: uaa.subclass = dd->bDeviceSubClass;
783: uaa.proto = dd->bDeviceProtocol;
784:
1.156 drochner 785: dv = config_found_ia(parent, "usbroothubif", &uaa, 0);
1.155 drochner 786: if (dv) {
787: dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT);
788: if (dev->subdevs == NULL)
789: return (USBD_NOMEM);
790: dev->subdevs[0] = dv;
791: dev->subdevlen = 1;
792: }
793: return (USBD_NORMAL_COMPLETION);
794: }
1.18 augustss 795:
796: usbd_status
1.78 augustss 797: usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
798: int port, int addr)
1.18 augustss 799: {
800: struct usb_attach_arg uaa;
1.144 drochner 801: struct usbif_attach_arg uiaa;
1.18 augustss 802: usb_device_descriptor_t *dd = &dev->ddesc;
1.53 augustss 803: int found, i, confi, nifaces;
804: usbd_status err;
1.43 augustss 805: device_ptr_t dv;
1.132 christos 806: usbd_interface_handle *ifaces;
1.158 ! drochner 807: int dlocs[USBDEVIFCF_NLOCS];
! 808: int ilocs[USBIFIFCF_NLOCS];
1.18 augustss 809:
810: uaa.device = dev;
811: uaa.usegeneric = 0;
812: uaa.port = port;
1.32 augustss 813: uaa.vendor = UGETW(dd->idVendor);
814: uaa.product = UGETW(dd->idProduct);
815: uaa.release = UGETW(dd->bcdDevice);
1.144 drochner 816: uaa.class = dd->bDeviceClass;
817: uaa.subclass = dd->bDeviceSubClass;
818: uaa.proto = dd->bDeviceProtocol;
1.18 augustss 819:
1.158 ! drochner 820: dlocs[USBDEVIFCF_PORT] = uaa.port;
! 821: dlocs[USBDEVIFCF_VENDOR] = uaa.vendor;
! 822: dlocs[USBDEVIFCF_PRODUCT] = uaa.product;
! 823: dlocs[USBDEVIFCF_RELEASE] = uaa.release;
! 824: /* the rest is historical ballast */
! 825: dlocs[USBDEVIFCF_CONFIGURATION] = -1;
! 826: dlocs[USBDEVIFCF_INTERFACE] = -1;
! 827:
1.18 augustss 828: /* First try with device specific drivers. */
1.60 augustss 829: DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
1.158 ! drochner 830: dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print,
1.157 drochner 831: config_stdsubmatch);
1.34 augustss 832: if (dv) {
1.155 drochner 833: dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT);
1.57 augustss 834: if (dev->subdevs == NULL)
1.34 augustss 835: return (USBD_NOMEM);
836: dev->subdevs[0] = dv;
1.155 drochner 837: dev->subdevlen = 1;
1.18 augustss 838: return (USBD_NORMAL_COMPLETION);
1.34 augustss 839: }
1.18 augustss 840:
841: DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
842:
1.144 drochner 843: uiaa.device = dev;
844: uiaa.port = port;
845: uiaa.vendor = UGETW(dd->idVendor);
846: uiaa.product = UGETW(dd->idProduct);
847: uiaa.release = UGETW(dd->bcdDevice);
848:
1.158 ! drochner 849: ilocs[USBIFIFCF_PORT] = uiaa.port;
! 850: ilocs[USBIFIFCF_VENDOR] = uiaa.vendor;
! 851: ilocs[USBIFIFCF_PRODUCT] = uiaa.product;
! 852: ilocs[USBIFIFCF_RELEASE] = uiaa.release;
! 853:
1.60 augustss 854: DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
855: dd->bNumConfigurations));
1.18 augustss 856: /* Next try with interface drivers. */
857: for (confi = 0; confi < dd->bNumConfigurations; confi++) {
858: DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
859: confi));
1.53 augustss 860: err = usbd_set_config_index(dev, confi, 1);
861: if (err) {
1.18 augustss 862: #ifdef USB_DEBUG
863: DPRINTF(("%s: port %d, set config at addr %d failed, "
1.43 augustss 864: "error=%s\n", USBDEVPTRNAME(parent), port,
1.53 augustss 865: addr, usbd_errstr(err)));
1.18 augustss 866: #else
867: printf("%s: port %d, set config at addr %d failed\n",
1.43 augustss 868: USBDEVPTRNAME(parent), port, addr);
1.18 augustss 869: #endif
1.53 augustss 870: return (err);
1.18 augustss 871: }
1.20 augustss 872: nifaces = dev->cdesc->bNumInterface;
1.144 drochner 873: uiaa.configno = dev->cdesc->bConfigurationValue;
1.158 ! drochner 874: ilocs[USBIFIFCF_CONFIGURATION] = uiaa.configno;
! 875:
1.132 christos 876: ifaces = malloc(nifaces * sizeof(*ifaces), M_USB, M_NOWAIT);
877: if (ifaces == NULL)
878: goto nomem;
1.20 augustss 879: for (i = 0; i < nifaces; i++)
880: ifaces[i] = &dev->ifaces[i];
1.144 drochner 881: uiaa.ifaces = ifaces;
882: uiaa.nifaces = nifaces;
1.155 drochner 883: dev->subdevs = malloc(nifaces * sizeof dv, M_USB,
884: M_NOWAIT|M_ZERO);
1.57 augustss 885: if (dev->subdevs == NULL) {
1.132 christos 886: free(ifaces, M_USB);
887: nomem:
1.34 augustss 888: return (USBD_NOMEM);
1.52 augustss 889: }
1.155 drochner 890: dev->subdevlen = nifaces;
1.52 augustss 891:
892: found = 0;
893: for (i = 0; i < nifaces; i++) {
1.53 augustss 894: if (ifaces[i] == NULL)
1.20 augustss 895: continue; /* interface already claimed */
1.144 drochner 896: uiaa.iface = ifaces[i];
897: uiaa.class = ifaces[i]->idesc->bInterfaceClass;
898: uiaa.subclass = ifaces[i]->idesc->bInterfaceSubClass;
899: uiaa.proto = ifaces[i]->idesc->bInterfaceProtocol;
900: uiaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
1.158 ! drochner 901: ilocs[USBIFIFCF_INTERFACE] = uiaa.ifaceno;
! 902: dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa,
1.157 drochner 903: usbd_ifprint, config_stdsubmatch);
1.53 augustss 904: if (dv != NULL) {
1.155 drochner 905: found++;
906: dev->subdevs[i] = dv;
1.20 augustss 907: ifaces[i] = 0; /* consumed */
908: }
1.18 augustss 909: }
1.52 augustss 910: if (found != 0) {
1.132 christos 911: free(ifaces, M_USB);
1.18 augustss 912: return (USBD_NORMAL_COMPLETION);
1.52 augustss 913: }
1.132 christos 914: free(ifaces, M_USB);
1.34 augustss 915: free(dev->subdevs, M_USB);
916: dev->subdevs = 0;
1.155 drochner 917: dev->subdevlen = 0;
1.18 augustss 918: }
1.21 augustss 919: /* No interfaces were attached in any of the configurations. */
1.34 augustss 920:
921: if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
1.18 augustss 922: usbd_set_config_index(dev, 0, 0);
923:
924: DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
925:
926: /* Finally try the generic driver. */
927: uaa.usegeneric = 1;
1.158 ! drochner 928: dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print,
1.157 drochner 929: config_stdsubmatch);
1.53 augustss 930: if (dv != NULL) {
1.155 drochner 931: dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT);
1.34 augustss 932: if (dev->subdevs == 0)
933: return (USBD_NOMEM);
934: dev->subdevs[0] = dv;
1.155 drochner 935: dev->subdevlen = 1;
1.18 augustss 936: return (USBD_NORMAL_COMPLETION);
1.34 augustss 937: }
1.18 augustss 938:
1.99 augustss 939: /*
1.18 augustss 940: * The generic attach failed, but leave the device as it is.
941: * We just did not find any drivers, that's all. The device is
942: * fully operational and not harming anyone.
943: */
944: DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
1.27 augustss 945: return (USBD_NORMAL_COMPLETION);
1.18 augustss 946: }
947:
948:
1.1 augustss 949: /*
950: * Called when a new device has been put in the powered state,
951: * but not yet in the addressed state.
952: * Get initial descriptor, set the address, get full descriptor,
953: * and attach a driver.
954: */
955: usbd_status
1.78 augustss 956: usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
1.94 augustss 957: int speed, int port, struct usbd_port *up)
1.1 augustss 958: {
1.120 augustss 959: usbd_device_handle dev, adev;
1.94 augustss 960: struct usbd_device *hub;
1.18 augustss 961: usb_device_descriptor_t *dd;
1.107 augustss 962: usb_port_status_t ps;
1.53 augustss 963: usbd_status err;
1.1 augustss 964: int addr;
1.18 augustss 965: int i;
1.120 augustss 966: int p;
1.1 augustss 967:
1.94 augustss 968: DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
969: bus, port, depth, speed));
1.1 augustss 970: addr = usbd_getnewaddr(bus);
971: if (addr < 0) {
1.99 augustss 972: printf("%s: No free USB addresses, new device ignored.\n",
1.151 drochner 973: device_xname(bus->usbctl));
1.1 augustss 974: return (USBD_NO_ADDR);
975: }
976:
1.97 tsutsui 977: dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1.53 augustss 978: if (dev == NULL)
1.1 augustss 979: return (USBD_NOMEM);
980:
981: dev->bus = bus;
982:
983: /* Set up default endpoint handle. */
984: dev->def_ep.edesc = &dev->def_ep_desc;
985:
986: /* Set up default endpoint descriptor. */
987: dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
988: dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
989: dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
990: dev->def_ep_desc.bmAttributes = UE_CONTROL;
991: USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
992: dev->def_ep_desc.bInterval = 0;
993:
994: dev->quirks = &usbd_no_quirk;
995: dev->address = USB_START_ADDR;
1.4 augustss 996: dev->ddesc.bMaxPacketSize = 0;
1.1 augustss 997: dev->depth = depth;
998: dev->powersrc = up;
1.94 augustss 999: dev->myhub = up->parent;
1.120 augustss 1000:
1001: up->device = dev;
1002:
1003: /* Locate port on upstream high speed hub */
1004: for (adev = dev, hub = up->parent;
1.94 augustss 1005: hub != NULL && hub->speed != USB_SPEED_HIGH;
1.120 augustss 1006: adev = hub, hub = hub->myhub)
1.94 augustss 1007: ;
1.120 augustss 1008: if (hub) {
1009: for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1010: if (hub->hub->ports[p].device == adev) {
1011: dev->myhsport = &hub->hub->ports[p];
1012: goto found;
1013: }
1014: }
1015: panic("usbd_new_device: cannot find HS port\n");
1016: found:
1017: DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1018: } else {
1019: dev->myhsport = NULL;
1020: }
1.94 augustss 1021: dev->speed = speed;
1.22 augustss 1022: dev->langid = USBD_NOLANG;
1.50 augustss 1023: dev->cookie.cookie = ++usb_cookie_no;
1.1 augustss 1024:
1.67 soren 1025: /* Establish the default pipe. */
1.63 augustss 1026: err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1027: &dev->default_pipe);
1.53 augustss 1028: if (err) {
1.18 augustss 1029: usbd_remove_device(dev, up);
1.53 augustss 1030: return (err);
1.18 augustss 1031: }
1.1 augustss 1032:
1.111 augustss 1033: /* Set the address. Do this early; some devices need that. */
1.147 mlelstv 1034: /* Try a few times in case the device is slow (i.e. outside specs) */
1.111 augustss 1035: DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1.147 mlelstv 1036: for (i = 0; i < 15; i++) {
1037: err = usbd_set_address(dev, addr);
1038: if (!err)
1039: break;
1040: usbd_delay_ms(dev, 200);
1.148 mlelstv 1041: if ((i & 3) == 3) {
1.147 mlelstv 1042: DPRINTFN(-1,("usbd_new_device: set address %d "
1043: "failed - trying a port reset\n", addr));
1044: usbd_reset_port(up->parent, port, &ps);
1045: }
1046: }
1.111 augustss 1047: if (err) {
1048: DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1049: err = USBD_SET_ADDR_FAILED;
1050: usbd_remove_device(dev, up);
1051: return (err);
1052: }
1053: /* Allow device time to set new address */
1054: usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1055: dev->address = addr; /* New device address now */
1056: bus->devices[addr] = dev;
1057:
1.18 augustss 1058: dd = &dev->ddesc;
1.6 augustss 1059: /* Try a few times in case the device is slow (i.e. outside specs.) */
1.107 augustss 1060: for (i = 0; i < 10; i++) {
1.6 augustss 1061: /* Get the first 8 bytes of the device descriptor. */
1.53 augustss 1062: err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1063: if (!err)
1.6 augustss 1064: break;
1.23 augustss 1065: usbd_delay_ms(dev, 200);
1.107 augustss 1066: if ((i & 3) == 3)
1067: usbd_reset_port(up->parent, port, &ps);
1.6 augustss 1068: }
1.53 augustss 1069: if (err) {
1.14 drochner 1070: DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1.53 augustss 1071: "failed\n", addr));
1.18 augustss 1072: usbd_remove_device(dev, up);
1.53 augustss 1073: return (err);
1.95 augustss 1074: }
1075:
1076: if (speed == USB_SPEED_HIGH) {
1077: /* Max packet size must be 64 (sec 5.5.3). */
1078: if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1079: #ifdef DIAGNOSTIC
1080: printf("usbd_new_device: addr=%d bad max packet size\n",
1081: addr);
1082: #endif
1083: dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1084: }
1.18 augustss 1085: }
1086:
1.39 augustss 1087: DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1.99 augustss 1088: "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1.39 augustss 1089: addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1.99 augustss 1090: dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1.94 augustss 1091: dev->speed));
1.39 augustss 1092:
1.18 augustss 1093: if (dd->bDescriptorType != UDESC_DEVICE) {
1094: /* Illegal device descriptor */
1095: DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1096: dd->bDescriptorType));
1097: usbd_remove_device(dev, up);
1098: return (USBD_INVAL);
1.1 augustss 1099: }
1100:
1.39 augustss 1101: if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1102: DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1103: usbd_remove_device(dev, up);
1104: return (USBD_INVAL);
1105: }
1.4 augustss 1106:
1.18 augustss 1107: USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1.1 augustss 1108:
1.62 augustss 1109: err = usbd_reload_device_desc(dev);
1.53 augustss 1110: if (err) {
1.14 drochner 1111: DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1112: "failed\n", addr));
1.18 augustss 1113: usbd_remove_device(dev, up);
1.53 augustss 1114: return (err);
1.1 augustss 1115: }
1116:
1117: /* Assume 100mA bus powered for now. Changed when configured. */
1118: dev->power = USB_MIN_POWER;
1119: dev->self_powered = 0;
1120:
1.99 augustss 1121: DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1.1 augustss 1122: addr, dev, parent));
1.99 augustss 1123:
1.65 augustss 1124: usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1.1 augustss 1125:
1.155 drochner 1126: if (port == 0) { /* root hub */
1127: KASSERT(addr == 1);
1128: usbd_attach_roothub(parent, dev);
1129: return (USBD_NORMAL_COMPLETION);
1130: }
1131:
1.53 augustss 1132: err = usbd_probe_and_attach(parent, dev, port, addr);
1133: if (err) {
1.18 augustss 1134: usbd_remove_device(dev, up);
1.53 augustss 1135: return (err);
1.18 augustss 1136: }
1.65 augustss 1137:
1.18 augustss 1138: return (USBD_NORMAL_COMPLETION);
1.62 augustss 1139: }
1140:
1141: usbd_status
1.78 augustss 1142: usbd_reload_device_desc(usbd_device_handle dev)
1.62 augustss 1143: {
1144: usbd_status err;
1145:
1146: /* Get the full device descriptor. */
1147: err = usbd_get_device_desc(dev, &dev->ddesc);
1148: if (err)
1149: return (err);
1150:
1151: /* Figure out what's wrong with this device. */
1152: dev->quirks = usbd_find_quirk(&dev->ddesc);
1153:
1154: return (USBD_NORMAL_COMPLETION);
1.18 augustss 1155: }
1.1 augustss 1156:
1.18 augustss 1157: void
1.78 augustss 1158: usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1.18 augustss 1159: {
1160: DPRINTF(("usbd_remove_device: %p\n", dev));
1.99 augustss 1161:
1.53 augustss 1162: if (dev->default_pipe != NULL)
1.18 augustss 1163: usbd_kill_pipe(dev->default_pipe);
1.120 augustss 1164: up->device = NULL;
1165: dev->bus->devices[dev->address] = NULL;
1.1 augustss 1166:
1167: free(dev, M_USB);
1168: }
1169:
1170: int
1.78 augustss 1171: usbd_print(void *aux, const char *pnp)
1.1 augustss 1172: {
1173: struct usb_attach_arg *uaa = aux;
1174:
1175: DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1176: if (pnp) {
1.134 christos 1177: #define USB_DEVINFO 1024
1178: char *devinfo;
1.1 augustss 1179: if (!uaa->usegeneric)
1180: return (QUIET);
1.134 christos 1181: devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK);
1182: usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO);
1.101 thorpej 1183: aprint_normal("%s, %s", devinfo, pnp);
1.134 christos 1184: free(devinfo, M_TEMP);
1.1 augustss 1185: }
1.155 drochner 1186: aprint_normal(" port %d", uaa->port);
1.144 drochner 1187: #if 0
1188: /*
1189: * It gets very crowded with these locators on the attach line.
1190: * They are not really needed since they are printed in the clear
1191: * by each driver.
1192: */
1193: if (uaa->vendor != UHUB_UNK_VENDOR)
1194: aprint_normal(" vendor 0x%04x", uaa->vendor);
1195: if (uaa->product != UHUB_UNK_PRODUCT)
1196: aprint_normal(" product 0x%04x", uaa->product);
1197: if (uaa->release != UHUB_UNK_RELEASE)
1198: aprint_normal(" release 0x%04x", uaa->release);
1199: #endif
1200: return (UNCONF);
1201: }
1202:
1203: int
1204: usbd_ifprint(void *aux, const char *pnp)
1205: {
1206: struct usbif_attach_arg *uaa = aux;
1207:
1208: DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1209: if (pnp)
1210: return (QUIET);
1.155 drochner 1211: aprint_normal(" port %d", uaa->port);
1.156 drochner 1212: aprint_normal(" configuration %d", uaa->configno);
1213: aprint_normal(" interface %d", uaa->ifaceno);
1.32 augustss 1214: #if 0
1.99 augustss 1215: /*
1.32 augustss 1216: * It gets very crowded with these locators on the attach line.
1217: * They are not really needed since they are printed in the clear
1218: * by each driver.
1219: */
1220: if (uaa->vendor != UHUB_UNK_VENDOR)
1.101 thorpej 1221: aprint_normal(" vendor 0x%04x", uaa->vendor);
1.32 augustss 1222: if (uaa->product != UHUB_UNK_PRODUCT)
1.101 thorpej 1223: aprint_normal(" product 0x%04x", uaa->product);
1.32 augustss 1224: if (uaa->release != UHUB_UNK_RELEASE)
1.101 thorpej 1225: aprint_normal(" release 0x%04x", uaa->release);
1.32 augustss 1226: #endif
1.1 augustss 1227: return (UNCONF);
1228: }
1229:
1.13 augustss 1230: void
1.82 augustss 1231: usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1232: int usedev)
1.13 augustss 1233: {
1234: struct usbd_port *p;
1.155 drochner 1235: int i, j, err, s;
1.13 augustss 1236:
1.151 drochner 1237: di->udi_bus = device_unit(dev->bus->usbctl);
1.98 christos 1238: di->udi_addr = dev->address;
1239: di->udi_cookie = dev->cookie;
1.139 pavel 1240: usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 1);
1.113 itojun 1241: usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1242: UGETW(dev->ddesc.bcdDevice));
1.123 augustss 1243: di->udi_serial[0] = 0;
1244: if (usedev)
1245: (void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
1246: di->udi_serial);
1.98 christos 1247: di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1248: di->udi_productNo = UGETW(dev->ddesc.idProduct);
1249: di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1250: di->udi_class = dev->ddesc.bDeviceClass;
1251: di->udi_subclass = dev->ddesc.bDeviceSubClass;
1252: di->udi_protocol = dev->ddesc.bDeviceProtocol;
1253: di->udi_config = dev->config;
1254: di->udi_power = dev->self_powered ? 0 : dev->power;
1255: di->udi_speed = dev->speed;
1.65 augustss 1256:
1.155 drochner 1257: if (dev->subdevlen > 0) {
1258: for (i = 0, j = 0; i < dev->subdevlen &&
1259: j < USB_MAX_DEVNAMES; i++) {
1260: if (!dev->subdevs[i])
1261: continue;
1262: strncpy(di->udi_devnames[j], USBDEVPTRNAME(dev->subdevs[i]),
1.65 augustss 1263: USB_MAX_DEVNAMELEN);
1.155 drochner 1264: di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1265: j++;
1.65 augustss 1266: }
1267: } else {
1.155 drochner 1268: j = 0;
1.65 augustss 1269: }
1.155 drochner 1270: for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1271: di->udi_devnames[j][0] = 0; /* empty */
1.65 augustss 1272:
1.13 augustss 1273: if (dev->hub) {
1.99 augustss 1274: for (i = 0;
1.98 christos 1275: i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1.13 augustss 1276: i < dev->hub->hubdesc.bNbrPorts;
1277: i++) {
1278: p = &dev->hub->ports[i];
1279: if (p->device)
1.56 augustss 1280: err = p->device->address;
1.13 augustss 1281: else {
1282: s = UGETW(p->status.wPortStatus);
1283: if (s & UPS_PORT_ENABLED)
1.56 augustss 1284: err = USB_PORT_ENABLED;
1.13 augustss 1285: else if (s & UPS_SUSPEND)
1.56 augustss 1286: err = USB_PORT_SUSPENDED;
1.13 augustss 1287: else if (s & UPS_PORT_POWER)
1.56 augustss 1288: err = USB_PORT_POWERED;
1.13 augustss 1289: else
1.56 augustss 1290: err = USB_PORT_DISABLED;
1.13 augustss 1291: }
1.98 christos 1292: di->udi_ports[i] = err;
1.13 augustss 1293: }
1.98 christos 1294: di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1.13 augustss 1295: } else
1.98 christos 1296: di->udi_nports = 0;
1.34 augustss 1297: }
1298:
1.139 pavel 1299: #ifdef COMPAT_30
1300: void
1301: usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di,
1302: int usedev)
1303: {
1304: struct usbd_port *p;
1.155 drochner 1305: int i, j, err, s;
1.139 pavel 1306:
1.151 drochner 1307: di->udi_bus = device_unit(dev->bus->usbctl);
1.139 pavel 1308: di->udi_addr = dev->address;
1309: di->udi_cookie = dev->cookie;
1310: usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 0);
1311: usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1312: UGETW(dev->ddesc.bcdDevice));
1313: di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1314: di->udi_productNo = UGETW(dev->ddesc.idProduct);
1315: di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1316: di->udi_class = dev->ddesc.bDeviceClass;
1317: di->udi_subclass = dev->ddesc.bDeviceSubClass;
1318: di->udi_protocol = dev->ddesc.bDeviceProtocol;
1319: di->udi_config = dev->config;
1320: di->udi_power = dev->self_powered ? 0 : dev->power;
1321: di->udi_speed = dev->speed;
1322:
1.155 drochner 1323: if (dev->subdevlen > 0) {
1324: for (i = 0, j = 0; i < dev->subdevlen &&
1325: j < USB_MAX_DEVNAMES; i++) {
1326: if (!dev->subdevs[i])
1327: continue;
1328: strncpy(di->udi_devnames[j], USBDEVPTRNAME(dev->subdevs[i]),
1.139 pavel 1329: USB_MAX_DEVNAMELEN);
1.155 drochner 1330: di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1331: j++;
1.139 pavel 1332: }
1333: } else {
1.155 drochner 1334: j = 0;
1.139 pavel 1335: }
1.155 drochner 1336: for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1337: di->udi_devnames[j][0] = 0; /* empty */
1.139 pavel 1338:
1339: if (dev->hub) {
1340: for (i = 0;
1341: i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1342: i < dev->hub->hubdesc.bNbrPorts;
1343: i++) {
1344: p = &dev->hub->ports[i];
1345: if (p->device)
1346: err = p->device->address;
1347: else {
1348: s = UGETW(p->status.wPortStatus);
1349: if (s & UPS_PORT_ENABLED)
1350: err = USB_PORT_ENABLED;
1351: else if (s & UPS_SUSPEND)
1352: err = USB_PORT_SUSPENDED;
1353: else if (s & UPS_PORT_POWER)
1354: err = USB_PORT_POWERED;
1355: else
1356: err = USB_PORT_DISABLED;
1357: }
1358: di->udi_ports[i] = err;
1359: }
1360: di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1361: } else
1362: di->udi_nports = 0;
1363: }
1364: #endif
1365:
1366:
1.34 augustss 1367: void
1.78 augustss 1368: usb_free_device(usbd_device_handle dev)
1.34 augustss 1369: {
1370: int ifcidx, nifc;
1371:
1.53 augustss 1372: if (dev->default_pipe != NULL)
1.34 augustss 1373: usbd_kill_pipe(dev->default_pipe);
1.53 augustss 1374: if (dev->ifaces != NULL) {
1.34 augustss 1375: nifc = dev->cdesc->bNumInterface;
1376: for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1377: usbd_free_iface_data(dev, ifcidx);
1378: free(dev->ifaces, M_USB);
1379: }
1.53 augustss 1380: if (dev->cdesc != NULL)
1.34 augustss 1381: free(dev->cdesc, M_USB);
1.155 drochner 1382: if (dev->subdevlen > 0) {
1.34 augustss 1383: free(dev->subdevs, M_USB);
1.155 drochner 1384: dev->subdevlen = 0;
1385: }
1.34 augustss 1386: free(dev, M_USB);
1.1 augustss 1387: }
1.47 augustss 1388:
1389: /*
1390: * The general mechanism for detaching drivers works as follows: Each
1391: * driver is responsible for maintaining a reference count on the
1392: * number of outstanding references to its softc (e.g. from
1393: * processing hanging in a read or write). The detach method of the
1394: * driver decrements this counter and flags in the softc that the
1395: * driver is dying and then wakes any sleepers. It then sleeps on the
1396: * softc. Each place that can sleep must maintain the reference
1397: * count. When the reference count drops to -1 (0 is the normal value
1398: * of the reference count) the a wakeup on the softc is performed
1399: * signaling to the detach waiter that all references are gone.
1400: */
1401:
1402: /*
1403: * Called from process context when we discover that a port has
1404: * been disconnected.
1405: */
1406: void
1.78 augustss 1407: usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1.47 augustss 1408: {
1409: usbd_device_handle dev = up->device;
1.152 cegger 1410: const char *hubname = USBDEVPTRNAME(parent);
1.47 augustss 1411: int i;
1412:
1.99 augustss 1413: DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1.47 augustss 1414: up, dev, up->portno));
1.48 augustss 1415:
1416: #ifdef DIAGNOSTIC
1.57 augustss 1417: if (dev == NULL) {
1.48 augustss 1418: printf("usb_disconnect_port: no device\n");
1419: return;
1420: }
1421: #endif
1.47 augustss 1422:
1.155 drochner 1423: if (dev->subdevlen > 0) {
1.57 augustss 1424: DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1.155 drochner 1425: for (i = 0; i < dev->subdevlen; i++) {
1426: if (!dev->subdevs[i])
1427: continue;
1.99 augustss 1428: printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1.51 augustss 1429: hubname);
1430: if (up->portno != 0)
1431: printf(" port %d", up->portno);
1432: printf(" (addr %d) disconnected\n", dev->address);
1.47 augustss 1433: config_detach(dev->subdevs[i], DETACH_FORCE);
1434: }
1435: }
1436:
1.65 augustss 1437: usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1.57 augustss 1438: dev->bus->devices[dev->address] = NULL;
1439: up->device = NULL;
1.47 augustss 1440: usb_free_device(dev);
1441: }
CVSweb <webmaster@jp.NetBSD.org>