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