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