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