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