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