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