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