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