usb_subr.c revision 1.133 1 /* $NetBSD: usb_subr.c,v 1.133 2006/06/04 19:38:32 christos 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.133 2006/06/04 19:38:32 christos 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 free(dev->subdevs, M_USB);
905 return (USBD_NORMAL_COMPLETION);
906 }
907 device_quiet(bdev);
908 #endif
909 }
910 }
911 if (found != 0) {
912 #if defined(__FreeBSD__)
913 /* remove the last created child again; it is unused */
914 device_delete_child(parent, bdev);
915 #endif
916 free(ifaces, M_USB);
917 free(dev->subdevs, M_USB);
918 return (USBD_NORMAL_COMPLETION);
919 }
920 free(ifaces, M_USB);
921 free(dev->subdevs, M_USB);
922 dev->subdevs = 0;
923 }
924 /* No interfaces were attached in any of the configurations. */
925
926 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
927 usbd_set_config_index(dev, 0, 0);
928
929 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
930
931 /* Finally try the generic driver. */
932 uaa.iface = NULL;
933 uaa.usegeneric = 1;
934 uaa.configno = UHUB_UNK_CONFIGURATION;
935 uaa.ifaceno = UHUB_UNK_INTERFACE;
936 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
937 if (dv != NULL) {
938 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
939 if (dev->subdevs == 0)
940 return (USBD_NOMEM);
941 dev->subdevs[0] = dv;
942 dev->subdevs[1] = 0;
943 return (USBD_NORMAL_COMPLETION);
944 }
945
946 /*
947 * The generic attach failed, but leave the device as it is.
948 * We just did not find any drivers, that's all. The device is
949 * fully operational and not harming anyone.
950 */
951 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
952 #if defined(__FreeBSD__)
953 device_delete_child(parent, bdev);
954 #endif
955 return (USBD_NORMAL_COMPLETION);
956 }
957
958
959 /*
960 * Called when a new device has been put in the powered state,
961 * but not yet in the addressed state.
962 * Get initial descriptor, set the address, get full descriptor,
963 * and attach a driver.
964 */
965 usbd_status
966 usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
967 int speed, int port, struct usbd_port *up)
968 {
969 usbd_device_handle dev, adev;
970 struct usbd_device *hub;
971 usb_device_descriptor_t *dd;
972 usb_port_status_t ps;
973 usbd_status err;
974 int addr;
975 int i;
976 int p;
977
978 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
979 bus, port, depth, speed));
980 addr = usbd_getnewaddr(bus);
981 if (addr < 0) {
982 printf("%s: No free USB addresses, new device ignored.\n",
983 USBDEVNAME(bus->bdev));
984 return (USBD_NO_ADDR);
985 }
986
987 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
988 if (dev == NULL)
989 return (USBD_NOMEM);
990
991 dev->bus = bus;
992
993 /* Set up default endpoint handle. */
994 dev->def_ep.edesc = &dev->def_ep_desc;
995
996 /* Set up default endpoint descriptor. */
997 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
998 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
999 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1000 dev->def_ep_desc.bmAttributes = UE_CONTROL;
1001 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1002 dev->def_ep_desc.bInterval = 0;
1003
1004 dev->quirks = &usbd_no_quirk;
1005 dev->address = USB_START_ADDR;
1006 dev->ddesc.bMaxPacketSize = 0;
1007 dev->depth = depth;
1008 dev->powersrc = up;
1009 dev->myhub = up->parent;
1010
1011 up->device = dev;
1012
1013 /* Locate port on upstream high speed hub */
1014 for (adev = dev, hub = up->parent;
1015 hub != NULL && hub->speed != USB_SPEED_HIGH;
1016 adev = hub, hub = hub->myhub)
1017 ;
1018 if (hub) {
1019 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1020 if (hub->hub->ports[p].device == adev) {
1021 dev->myhsport = &hub->hub->ports[p];
1022 goto found;
1023 }
1024 }
1025 panic("usbd_new_device: cannot find HS port\n");
1026 found:
1027 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1028 } else {
1029 dev->myhsport = NULL;
1030 }
1031 dev->speed = speed;
1032 dev->langid = USBD_NOLANG;
1033 dev->cookie.cookie = ++usb_cookie_no;
1034
1035 /* Establish the default pipe. */
1036 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1037 &dev->default_pipe);
1038 if (err) {
1039 usbd_remove_device(dev, up);
1040 return (err);
1041 }
1042
1043 /* Set the address. Do this early; some devices need that. */
1044 err = usbd_set_address(dev, addr);
1045 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1046 if (err) {
1047 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1048 err = USBD_SET_ADDR_FAILED;
1049 usbd_remove_device(dev, up);
1050 return (err);
1051 }
1052 /* Allow device time to set new address */
1053 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1054 dev->address = addr; /* New device address now */
1055 bus->devices[addr] = dev;
1056
1057 dd = &dev->ddesc;
1058 /* Try a few times in case the device is slow (i.e. outside specs.) */
1059 for (i = 0; i < 10; i++) {
1060 /* Get the first 8 bytes of the device descriptor. */
1061 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1062 if (!err)
1063 break;
1064 usbd_delay_ms(dev, 200);
1065 if ((i & 3) == 3)
1066 usbd_reset_port(up->parent, port, &ps);
1067 }
1068 if (err) {
1069 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1070 "failed\n", addr));
1071 usbd_remove_device(dev, up);
1072 return (err);
1073 }
1074
1075 if (speed == USB_SPEED_HIGH) {
1076 /* Max packet size must be 64 (sec 5.5.3). */
1077 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1078 #ifdef DIAGNOSTIC
1079 printf("usbd_new_device: addr=%d bad max packet size\n",
1080 addr);
1081 #endif
1082 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1083 }
1084 }
1085
1086 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1087 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1088 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1089 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1090 dev->speed));
1091
1092 if (dd->bDescriptorType != UDESC_DEVICE) {
1093 /* Illegal device descriptor */
1094 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1095 dd->bDescriptorType));
1096 usbd_remove_device(dev, up);
1097 return (USBD_INVAL);
1098 }
1099
1100 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1101 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1102 usbd_remove_device(dev, up);
1103 return (USBD_INVAL);
1104 }
1105
1106 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1107
1108 err = usbd_reload_device_desc(dev);
1109 if (err) {
1110 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1111 "failed\n", addr));
1112 usbd_remove_device(dev, up);
1113 return (err);
1114 }
1115
1116 /* Assume 100mA bus powered for now. Changed when configured. */
1117 dev->power = USB_MIN_POWER;
1118 dev->self_powered = 0;
1119
1120 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1121 addr, dev, parent));
1122
1123 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1124
1125 err = usbd_probe_and_attach(parent, dev, port, addr);
1126 if (err) {
1127 usbd_remove_device(dev, up);
1128 return (err);
1129 }
1130
1131 return (USBD_NORMAL_COMPLETION);
1132 }
1133
1134 usbd_status
1135 usbd_reload_device_desc(usbd_device_handle dev)
1136 {
1137 usbd_status err;
1138
1139 /* Get the full device descriptor. */
1140 err = usbd_get_device_desc(dev, &dev->ddesc);
1141 if (err)
1142 return (err);
1143
1144 /* Figure out what's wrong with this device. */
1145 dev->quirks = usbd_find_quirk(&dev->ddesc);
1146
1147 return (USBD_NORMAL_COMPLETION);
1148 }
1149
1150 void
1151 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1152 {
1153 DPRINTF(("usbd_remove_device: %p\n", dev));
1154
1155 if (dev->default_pipe != NULL)
1156 usbd_kill_pipe(dev->default_pipe);
1157 up->device = NULL;
1158 dev->bus->devices[dev->address] = NULL;
1159
1160 free(dev, M_USB);
1161 }
1162
1163 #if defined(__NetBSD__) || defined(__OpenBSD__)
1164 int
1165 usbd_print(void *aux, const char *pnp)
1166 {
1167 struct usb_attach_arg *uaa = aux;
1168 char devinfo[1024];
1169
1170 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1171 if (pnp) {
1172 if (!uaa->usegeneric)
1173 return (QUIET);
1174 usbd_devinfo(uaa->device, 1, devinfo, sizeof(devinfo));
1175 aprint_normal("%s, %s", devinfo, pnp);
1176 }
1177 if (uaa->port != 0)
1178 aprint_normal(" port %d", uaa->port);
1179 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1180 aprint_normal(" configuration %d", uaa->configno);
1181 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1182 aprint_normal(" interface %d", uaa->ifaceno);
1183 #if 0
1184 /*
1185 * It gets very crowded with these locators on the attach line.
1186 * They are not really needed since they are printed in the clear
1187 * by each driver.
1188 */
1189 if (uaa->vendor != UHUB_UNK_VENDOR)
1190 aprint_normal(" vendor 0x%04x", uaa->vendor);
1191 if (uaa->product != UHUB_UNK_PRODUCT)
1192 aprint_normal(" product 0x%04x", uaa->product);
1193 if (uaa->release != UHUB_UNK_RELEASE)
1194 aprint_normal(" release 0x%04x", uaa->release);
1195 #endif
1196 return (UNCONF);
1197 }
1198
1199 #if defined(__NetBSD__)
1200 int
1201 usbd_submatch(struct device *parent, struct cfdata *cf,
1202 const int *ldesc, void *aux)
1203 {
1204 #elif defined(__OpenBSD__)
1205 int
1206 usbd_submatch(struct device *parent, void *match, void *aux)
1207 {
1208 struct cfdata *cf = match;
1209 #endif
1210 struct usb_attach_arg *uaa = aux;
1211
1212 DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1213 "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1214 uaa->port, cf->uhubcf_port,
1215 uaa->configno, cf->uhubcf_configuration,
1216 uaa->ifaceno, cf->uhubcf_interface,
1217 uaa->vendor, cf->uhubcf_vendor,
1218 uaa->product, cf->uhubcf_product,
1219 uaa->release, cf->uhubcf_release));
1220 if (uaa->port != 0 && /* root hub has port 0, it should match */
1221 ((cf->uhubcf_port != UHUB_UNK_PORT &&
1222 cf->uhubcf_port != uaa->port) ||
1223 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1224 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1225 cf->uhubcf_configuration != uaa->configno) ||
1226 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1227 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1228 cf->uhubcf_interface != uaa->ifaceno) ||
1229 (uaa->vendor != UHUB_UNK_VENDOR &&
1230 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1231 cf->uhubcf_vendor != uaa->vendor) ||
1232 (uaa->product != UHUB_UNK_PRODUCT &&
1233 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1234 cf->uhubcf_product != uaa->product) ||
1235 (uaa->release != UHUB_UNK_RELEASE &&
1236 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1237 cf->uhubcf_release != uaa->release)
1238 )
1239 )
1240 return 0;
1241 return (config_match(parent, cf, aux));
1242 }
1243
1244 #endif
1245
1246 void
1247 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1248 int usedev)
1249 {
1250 struct usbd_port *p;
1251 int i, err, s;
1252
1253 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1254 di->udi_addr = dev->address;
1255 di->udi_cookie = dev->cookie;
1256 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev);
1257 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1258 UGETW(dev->ddesc.bcdDevice));
1259 di->udi_serial[0] = 0;
1260 if (usedev)
1261 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
1262 di->udi_serial);
1263 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1264 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1265 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1266 di->udi_class = dev->ddesc.bDeviceClass;
1267 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1268 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1269 di->udi_config = dev->config;
1270 di->udi_power = dev->self_powered ? 0 : dev->power;
1271 di->udi_speed = dev->speed;
1272
1273 if (dev->subdevs != NULL) {
1274 for (i = 0; dev->subdevs[i] &&
1275 i < USB_MAX_DEVNAMES; i++) {
1276 strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1277 USB_MAX_DEVNAMELEN);
1278 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
1279 }
1280 } else {
1281 i = 0;
1282 }
1283 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1284 di->udi_devnames[i][0] = 0; /* empty */
1285
1286 if (dev->hub) {
1287 for (i = 0;
1288 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1289 i < dev->hub->hubdesc.bNbrPorts;
1290 i++) {
1291 p = &dev->hub->ports[i];
1292 if (p->device)
1293 err = p->device->address;
1294 else {
1295 s = UGETW(p->status.wPortStatus);
1296 if (s & UPS_PORT_ENABLED)
1297 err = USB_PORT_ENABLED;
1298 else if (s & UPS_SUSPEND)
1299 err = USB_PORT_SUSPENDED;
1300 else if (s & UPS_PORT_POWER)
1301 err = USB_PORT_POWERED;
1302 else
1303 err = USB_PORT_DISABLED;
1304 }
1305 di->udi_ports[i] = err;
1306 }
1307 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1308 } else
1309 di->udi_nports = 0;
1310 }
1311
1312 void
1313 usb_free_device(usbd_device_handle dev)
1314 {
1315 int ifcidx, nifc;
1316
1317 if (dev->default_pipe != NULL)
1318 usbd_kill_pipe(dev->default_pipe);
1319 if (dev->ifaces != NULL) {
1320 nifc = dev->cdesc->bNumInterface;
1321 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1322 usbd_free_iface_data(dev, ifcidx);
1323 free(dev->ifaces, M_USB);
1324 }
1325 if (dev->cdesc != NULL)
1326 free(dev->cdesc, M_USB);
1327 if (dev->subdevs != NULL)
1328 free(dev->subdevs, M_USB);
1329 free(dev, M_USB);
1330 }
1331
1332 /*
1333 * The general mechanism for detaching drivers works as follows: Each
1334 * driver is responsible for maintaining a reference count on the
1335 * number of outstanding references to its softc (e.g. from
1336 * processing hanging in a read or write). The detach method of the
1337 * driver decrements this counter and flags in the softc that the
1338 * driver is dying and then wakes any sleepers. It then sleeps on the
1339 * softc. Each place that can sleep must maintain the reference
1340 * count. When the reference count drops to -1 (0 is the normal value
1341 * of the reference count) the a wakeup on the softc is performed
1342 * signaling to the detach waiter that all references are gone.
1343 */
1344
1345 /*
1346 * Called from process context when we discover that a port has
1347 * been disconnected.
1348 */
1349 void
1350 usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1351 {
1352 usbd_device_handle dev = up->device;
1353 char *hubname = USBDEVPTRNAME(parent);
1354 int i;
1355
1356 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1357 up, dev, up->portno));
1358
1359 #ifdef DIAGNOSTIC
1360 if (dev == NULL) {
1361 printf("usb_disconnect_port: no device\n");
1362 return;
1363 }
1364 #endif
1365
1366 if (dev->subdevs != NULL) {
1367 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1368 for (i = 0; dev->subdevs[i]; i++) {
1369 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1370 hubname);
1371 if (up->portno != 0)
1372 printf(" port %d", up->portno);
1373 printf(" (addr %d) disconnected\n", dev->address);
1374 config_detach(dev->subdevs[i], DETACH_FORCE);
1375 dev->subdevs[i] = 0;
1376 }
1377 }
1378
1379 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1380 dev->bus->devices[dev->address] = NULL;
1381 up->device = NULL;
1382 usb_free_device(dev);
1383 }
1384
1385 #ifdef __OpenBSD__
1386 void *usb_realloc(void *p, u_int size, int pool, int flags)
1387 {
1388 void *q;
1389
1390 q = malloc(size, pool, flags);
1391 if (q == NULL)
1392 return (NULL);
1393 bcopy(p, q, size);
1394 free(p, pool);
1395 return (q);
1396 }
1397 #endif
1398