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