usb_subr.c revision 1.114 1 /* $NetBSD: usb_subr.c,v 1.114 2004/06/23 02:30:52 mycroft Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (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.114 2004/06/23 02:30:52 mycroft 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_vp(usbd_device_handle, char *, size_t, char *,
85 size_t, int);
86 Static char *usbd_get_string(usbd_device_handle, int, char *);
87 Static int usbd_getnewaddr(usbd_bus_handle bus);
88 #if defined(__NetBSD__)
89 Static int usbd_print(void *aux, const char *pnp);
90 Static int usbd_submatch(device_ptr_t, struct cfdata *cf, void *);
91 #elif defined(__OpenBSD__)
92 Static int usbd_print(void *aux, const char *pnp);
93 Static int usbd_submatch(device_ptr_t, void *, void *);
94 #endif
95 Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
96 Static void usbd_kill_pipe(usbd_pipe_handle);
97 Static usbd_status usbd_probe_and_attach(device_ptr_t parent,
98 usbd_device_handle dev, int port, int addr);
99
100 Static u_int32_t usb_cookie_no = 0;
101
102 #ifdef USBVERBOSE
103 typedef u_int16_t usb_vendor_id_t;
104 typedef u_int16_t usb_product_id_t;
105
106 /*
107 * Descriptions of of known vendors and devices ("products").
108 */
109 struct usb_knowndev {
110 usb_vendor_id_t vendor;
111 usb_product_id_t product;
112 int flags;
113 char *vendorname, *productname;
114 };
115 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
116
117 #include <dev/usb/usbdevs_data.h>
118 #endif /* USBVERBOSE */
119
120 Static const char * const usbd_error_strs[] = {
121 "NORMAL_COMPLETION",
122 "IN_PROGRESS",
123 "PENDING_REQUESTS",
124 "NOT_STARTED",
125 "INVAL",
126 "NOMEM",
127 "CANCELLED",
128 "BAD_ADDRESS",
129 "IN_USE",
130 "NO_ADDR",
131 "SET_ADDR_FAILED",
132 "NO_POWER",
133 "TOO_DEEP",
134 "IOERROR",
135 "NOT_CONFIGURED",
136 "TIMEOUT",
137 "SHORT_XFER",
138 "STALLED",
139 "INTERRUPTED",
140 "XXX",
141 };
142
143 const char *
144 usbd_errstr(usbd_status err)
145 {
146 static char buffer[5];
147
148 if (err < USBD_ERROR_MAX) {
149 return usbd_error_strs[err];
150 } else {
151 snprintf(buffer, sizeof buffer, "%d", err);
152 return buffer;
153 }
154 }
155
156 usbd_status
157 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
158 usb_string_descriptor_t *sdesc, int *sizep)
159 {
160 usb_device_request_t req;
161
162 req.bmRequestType = UT_READ_DEVICE;
163 req.bRequest = UR_GET_DESCRIPTOR;
164 USETW2(req.wValue, UDESC_STRING, sindex);
165 USETW(req.wIndex, langid);
166 USETW(req.wLength, USB_MAX_STRING_LEN);
167 return (usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
168 sizep, USBD_DEFAULT_TIMEOUT));
169 }
170
171 char *
172 usbd_get_string(usbd_device_handle dev, int si, char *buf)
173 {
174 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
175 usb_string_descriptor_t us;
176 char *s;
177 int i, n;
178 u_int16_t c;
179 usbd_status err;
180 int size;
181
182 if (si == 0)
183 return (0);
184 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
185 return (0);
186 if (dev->langid == USBD_NOLANG) {
187 /* Set up default language */
188 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
189 &size);
190 if (err || size < 4) {
191 dev->langid = 0; /* Well, just pick something then */
192 } else {
193 /* Pick the first language as the default. */
194 dev->langid = UGETW(us.bString[0]);
195 }
196 }
197 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
198 if (err)
199 return (0);
200 s = buf;
201 n = size / 2 - 1;
202 for (i = 0; i < n; i++) {
203 c = UGETW(us.bString[i]);
204 /* Convert from Unicode, handle buggy strings. */
205 if ((c & 0xff00) == 0)
206 *s++ = c;
207 else if ((c & 0x00ff) == 0 && swap)
208 *s++ = c >> 8;
209 else
210 *s++ = '?';
211 }
212 *s++ = 0;
213 return (buf);
214 }
215
216 static void
217 usbd_trim_spaces(char *p)
218 {
219 char *q, *e;
220
221 if (p == NULL)
222 return;
223 q = e = p;
224 while (*q == ' ') /* skip leading spaces */
225 q++;
226 while ((*p = *q++)) /* copy string */
227 if (*p++ != ' ') /* remember last non-space */
228 e = p;
229 *e = 0; /* kill trailing spaces */
230 }
231
232 void
233 usbd_devinfo_vp(usbd_device_handle dev, char *v, size_t lv, char *p, size_t lp,
234 int usedev)
235 {
236 usb_device_descriptor_t *udd = &dev->ddesc;
237 char *vendor = NULL, *product = NULL;
238 #ifdef USBVERBOSE
239 const struct usb_knowndev *kdp;
240 #endif
241
242 if (dev == NULL) {
243 v[0] = p[0] = '\0';
244 return;
245 }
246
247 if (usedev) {
248 vendor = usbd_get_string(dev, udd->iManufacturer, v);
249 usbd_trim_spaces(vendor);
250 product = usbd_get_string(dev, udd->iProduct, p);
251 usbd_trim_spaces(product);
252 if (vendor && !*vendor)
253 vendor = NULL;
254 if (product && !*product)
255 product = NULL;
256 } else {
257 vendor = NULL;
258 product = NULL;
259 }
260 #ifdef USBVERBOSE
261 if (vendor == NULL || product == NULL) {
262 for(kdp = usb_knowndevs;
263 kdp->vendorname != NULL;
264 kdp++) {
265 if (kdp->vendor == UGETW(udd->idVendor) &&
266 (kdp->product == UGETW(udd->idProduct) ||
267 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
268 break;
269 }
270 if (kdp->vendorname != NULL) {
271 if (vendor == NULL)
272 vendor = kdp->vendorname;
273 if (product == NULL)
274 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
275 kdp->productname : NULL;
276 }
277 }
278 #endif
279 if (vendor != NULL && *vendor)
280 strlcpy(v, vendor, lv);
281 else
282 snprintf(v, lv, "vendor 0x%04x", UGETW(udd->idVendor));
283 if (product != NULL && *product)
284 strlcpy(p, product, lp);
285 else
286 snprintf(p, lp, "product 0x%04x", UGETW(udd->idProduct));
287 }
288
289 int
290 usbd_printBCD(char *cp, size_t l, int bcd)
291 {
292 return (snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff));
293 }
294
295 void
296 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l)
297 {
298 usb_device_descriptor_t *udd = &dev->ddesc;
299 char vendor[USB_MAX_STRING_LEN];
300 char product[USB_MAX_STRING_LEN];
301 int bcdDevice, bcdUSB;
302 char *ep;
303
304 ep = cp + l;
305
306 usbd_devinfo_vp(dev, vendor, sizeof(vendor), product,
307 sizeof(product), 1);
308 cp += snprintf(cp, ep - cp, "%s %s", vendor, product);
309 if (showclass)
310 cp += snprintf(cp, ep - cp, ", class %d/%d",
311 udd->bDeviceClass, udd->bDeviceSubClass);
312 bcdUSB = UGETW(udd->bcdUSB);
313 bcdDevice = UGETW(udd->bcdDevice);
314 cp += snprintf(cp, ep - cp, ", rev ");
315 cp += usbd_printBCD(cp, ep - cp, bcdUSB);
316 *cp++ = '/';
317 cp += usbd_printBCD(cp, ep - cp, bcdDevice);
318 cp += snprintf(cp, ep - cp, ", addr %d", dev->address);
319 *cp = 0;
320 }
321
322 /* Delay for a certain number of ms */
323 void
324 usb_delay_ms(usbd_bus_handle bus, u_int ms)
325 {
326 /* Wait at least two clock ticks so we know the time has passed. */
327 if (bus->use_polling || cold)
328 delay((ms+1) * 1000);
329 else
330 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
331 }
332
333 /* Delay given a device handle. */
334 void
335 usbd_delay_ms(usbd_device_handle dev, u_int ms)
336 {
337 usb_delay_ms(dev->bus, ms);
338 }
339
340 usbd_status
341 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
342 {
343 usb_device_request_t req;
344 usbd_status err;
345 int n;
346
347 req.bmRequestType = UT_WRITE_CLASS_OTHER;
348 req.bRequest = UR_SET_FEATURE;
349 USETW(req.wValue, UHF_PORT_RESET);
350 USETW(req.wIndex, port);
351 USETW(req.wLength, 0);
352 err = usbd_do_request(dev, &req, 0);
353 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
354 port, usbd_errstr(err)));
355 if (err)
356 return (err);
357 n = 10;
358 do {
359 /* Wait for device to recover from reset. */
360 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
361 err = usbd_get_port_status(dev, port, ps);
362 if (err) {
363 DPRINTF(("usbd_reset_port: get status failed %d\n",
364 err));
365 return (err);
366 }
367 /* If the device disappeared, just give up. */
368 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
369 return (USBD_NORMAL_COMPLETION);
370 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
371 if (n == 0)
372 return (USBD_TIMEOUT);
373 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
374 #ifdef USB_DEBUG
375 if (err)
376 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
377 err));
378 #endif
379
380 /* Wait for the device to recover from reset. */
381 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
382 return (err);
383 }
384
385 usb_interface_descriptor_t *
386 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
387 {
388 char *p = (char *)cd;
389 char *end = p + UGETW(cd->wTotalLength);
390 usb_interface_descriptor_t *d;
391 int curidx, lastidx, curaidx = 0;
392
393 for (curidx = lastidx = -1; p < end; ) {
394 d = (usb_interface_descriptor_t *)p;
395 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
396 "type=%d\n",
397 ifaceidx, curidx, altidx, curaidx,
398 d->bLength, d->bDescriptorType));
399 if (d->bLength == 0) /* bad descriptor */
400 break;
401 p += d->bLength;
402 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
403 if (d->bInterfaceNumber != lastidx) {
404 lastidx = d->bInterfaceNumber;
405 curidx++;
406 curaidx = 0;
407 } else
408 curaidx++;
409 if (ifaceidx == curidx && altidx == curaidx)
410 return (d);
411 }
412 }
413 return (NULL);
414 }
415
416 usb_endpoint_descriptor_t *
417 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
418 int endptidx)
419 {
420 char *p = (char *)cd;
421 char *end = p + UGETW(cd->wTotalLength);
422 usb_interface_descriptor_t *d;
423 usb_endpoint_descriptor_t *e;
424 int curidx;
425
426 d = usbd_find_idesc(cd, ifaceidx, altidx);
427 if (d == NULL)
428 return (NULL);
429 if (endptidx >= d->bNumEndpoints) /* quick exit */
430 return (NULL);
431
432 curidx = -1;
433 for (p = (char *)d + d->bLength; p < end; ) {
434 e = (usb_endpoint_descriptor_t *)p;
435 if (e->bLength == 0) /* bad descriptor */
436 break;
437 p += e->bLength;
438 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
439 return (NULL);
440 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
441 curidx++;
442 if (curidx == endptidx)
443 return (e);
444 }
445 }
446 return (NULL);
447 }
448
449 usbd_status
450 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
451 {
452 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
453 usb_interface_descriptor_t *idesc;
454 char *p, *end;
455 int endpt, nendpt;
456
457 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
458 ifaceidx, altidx));
459 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
460 if (idesc == NULL)
461 return (USBD_INVAL);
462 ifc->device = dev;
463 ifc->idesc = idesc;
464 ifc->index = ifaceidx;
465 ifc->altindex = altidx;
466 nendpt = ifc->idesc->bNumEndpoints;
467 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
468 if (nendpt != 0) {
469 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
470 M_USB, M_NOWAIT);
471 if (ifc->endpoints == NULL)
472 return (USBD_NOMEM);
473 } else
474 ifc->endpoints = NULL;
475 ifc->priv = NULL;
476 p = (char *)ifc->idesc + ifc->idesc->bLength;
477 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
478 #define ed ((usb_endpoint_descriptor_t *)p)
479 for (endpt = 0; endpt < nendpt; endpt++) {
480 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
481 for (; p < end; p += ed->bLength) {
482 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
483 "len=%d type=%d\n",
484 p, end, ed->bLength, ed->bDescriptorType));
485 if (p + ed->bLength <= end && ed->bLength != 0 &&
486 ed->bDescriptorType == UDESC_ENDPOINT)
487 goto found;
488 if (ed->bLength == 0 ||
489 ed->bDescriptorType == UDESC_INTERFACE)
490 break;
491 }
492 /* passed end, or bad desc */
493 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
494 ed->bLength == 0 ? "0 length" :
495 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
496 "out of data");
497 goto bad;
498 found:
499 ifc->endpoints[endpt].edesc = ed;
500 if (dev->speed == USB_SPEED_HIGH) {
501 u_int mps;
502 /* Control and bulk endpoints have max packet limits. */
503 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
504 case UE_CONTROL:
505 mps = USB_2_MAX_CTRL_PACKET;
506 goto check;
507 case UE_BULK:
508 mps = USB_2_MAX_BULK_PACKET;
509 check:
510 if (UGETW(ed->wMaxPacketSize) != mps) {
511 USETW(ed->wMaxPacketSize, mps);
512 #ifdef DIAGNOSTIC
513 printf("usbd_fill_iface_data: bad max "
514 "packet size\n");
515 #endif
516 }
517 break;
518 default:
519 break;
520 }
521 }
522 ifc->endpoints[endpt].refcnt = 0;
523 p += ed->bLength;
524 }
525 #undef ed
526 LIST_INIT(&ifc->pipes);
527 return (USBD_NORMAL_COMPLETION);
528
529 bad:
530 if (ifc->endpoints != NULL) {
531 free(ifc->endpoints, M_USB);
532 ifc->endpoints = NULL;
533 }
534 return (USBD_INVAL);
535 }
536
537 void
538 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
539 {
540 usbd_interface_handle ifc = &dev->ifaces[ifcno];
541 if (ifc->endpoints)
542 free(ifc->endpoints, M_USB);
543 }
544
545 Static usbd_status
546 usbd_set_config(usbd_device_handle dev, int conf)
547 {
548 usb_device_request_t req;
549
550 req.bmRequestType = UT_WRITE_DEVICE;
551 req.bRequest = UR_SET_CONFIG;
552 USETW(req.wValue, conf);
553 USETW(req.wIndex, 0);
554 USETW(req.wLength, 0);
555 return (usbd_do_request(dev, &req, 0));
556 }
557
558 usbd_status
559 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
560 {
561 int index;
562 usb_config_descriptor_t cd;
563 usbd_status err;
564
565 if (no == USB_UNCONFIG_NO)
566 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
567
568 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
569 /* Figure out what config index to use. */
570 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
571 err = usbd_get_config_desc(dev, index, &cd);
572 if (err)
573 return (err);
574 if (cd.bConfigurationValue == no)
575 return (usbd_set_config_index(dev, index, msg));
576 }
577 return (USBD_INVAL);
578 }
579
580 usbd_status
581 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
582 {
583 usb_status_t ds;
584 usb_config_descriptor_t cd, *cdp;
585 usbd_status err;
586 int i, ifcidx, nifc, len, selfpowered, power;
587
588 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
589
590 /* XXX check that all interfaces are idle */
591 if (dev->config != USB_UNCONFIG_NO) {
592 DPRINTF(("usbd_set_config_index: free old config\n"));
593 /* Free all configuration data structures. */
594 nifc = dev->cdesc->bNumInterface;
595 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
596 usbd_free_iface_data(dev, ifcidx);
597 free(dev->ifaces, M_USB);
598 free(dev->cdesc, M_USB);
599 dev->ifaces = NULL;
600 dev->cdesc = NULL;
601 dev->config = USB_UNCONFIG_NO;
602 }
603
604 if (index == USB_UNCONFIG_INDEX) {
605 /* We are unconfiguring the device, so leave unallocated. */
606 DPRINTF(("usbd_set_config_index: set config 0\n"));
607 err = usbd_set_config(dev, USB_UNCONFIG_NO);
608 if (err)
609 DPRINTF(("usbd_set_config_index: setting config=0 "
610 "failed, error=%s\n", usbd_errstr(err)));
611 return (err);
612 }
613
614 /* Get the short descriptor. */
615 err = usbd_get_config_desc(dev, index, &cd);
616 if (err)
617 return (err);
618 len = UGETW(cd.wTotalLength);
619 cdp = malloc(len, M_USB, M_NOWAIT);
620 if (cdp == NULL)
621 return (USBD_NOMEM);
622
623 /* Get the full descriptor. Try a few times for slow devices. */
624 for (i = 0; i < 3; i++) {
625 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
626 if (!err)
627 break;
628 usbd_delay_ms(dev, 200);
629 }
630 if (err)
631 goto bad;
632
633 if (cdp->bDescriptorType != UDESC_CONFIG) {
634 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
635 cdp->bDescriptorType));
636 err = USBD_INVAL;
637 goto bad;
638 }
639
640 /* Figure out if the device is self or bus powered. */
641 selfpowered = 0;
642 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
643 (cdp->bmAttributes & UC_SELF_POWERED)) {
644 /* May be self powered. */
645 if (cdp->bmAttributes & UC_BUS_POWERED) {
646 /* Must ask device. */
647 if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
648 /*
649 * Hub claims to be self powered, but isn't.
650 * It seems that the power status can be
651 * determined by the hub characteristics.
652 */
653 usb_hub_descriptor_t hd;
654 usb_device_request_t req;
655 req.bmRequestType = UT_READ_CLASS_DEVICE;
656 req.bRequest = UR_GET_DESCRIPTOR;
657 USETW(req.wValue, 0);
658 USETW(req.wIndex, 0);
659 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
660 err = usbd_do_request(dev, &req, &hd);
661 if (!err &&
662 (UGETW(hd.wHubCharacteristics) &
663 UHD_PWR_INDIVIDUAL))
664 selfpowered = 1;
665 DPRINTF(("usbd_set_config_index: charac=0x%04x"
666 ", error=%s\n",
667 UGETW(hd.wHubCharacteristics),
668 usbd_errstr(err)));
669 } else {
670 err = usbd_get_device_status(dev, &ds);
671 if (!err &&
672 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
673 selfpowered = 1;
674 DPRINTF(("usbd_set_config_index: status=0x%04x"
675 ", error=%s\n",
676 UGETW(ds.wStatus), usbd_errstr(err)));
677 }
678 } else
679 selfpowered = 1;
680 }
681 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
682 "selfpowered=%d, power=%d\n",
683 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
684 selfpowered, cdp->bMaxPower * 2));
685
686 /* Check if we have enough power. */
687 #ifdef USB_DEBUG
688 if (dev->powersrc == NULL) {
689 DPRINTF(("usbd_set_config_index: No power source?\n"));
690 return (USBD_IOERROR);
691 }
692 #endif
693 power = cdp->bMaxPower * 2;
694 if (power > dev->powersrc->power) {
695 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
696 /* XXX print nicer message. */
697 if (msg)
698 printf("%s: device addr %d (config %d) exceeds power "
699 "budget, %d mA > %d mA\n",
700 USBDEVNAME(dev->bus->bdev), dev->address,
701 cdp->bConfigurationValue,
702 power, dev->powersrc->power);
703 err = USBD_NO_POWER;
704 goto bad;
705 }
706 dev->power = power;
707 dev->self_powered = selfpowered;
708
709 /* Set the actual configuration value. */
710 DPRINTF(("usbd_set_config_index: set config %d\n",
711 cdp->bConfigurationValue));
712 err = usbd_set_config(dev, cdp->bConfigurationValue);
713 if (err) {
714 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
715 "error=%s\n",
716 cdp->bConfigurationValue, usbd_errstr(err)));
717 goto bad;
718 }
719
720 /* Allocate and fill interface data. */
721 nifc = cdp->bNumInterface;
722 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
723 M_USB, M_NOWAIT);
724 if (dev->ifaces == NULL) {
725 err = USBD_NOMEM;
726 goto bad;
727 }
728 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
729 dev->cdesc = cdp;
730 dev->config = cdp->bConfigurationValue;
731 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
732 err = usbd_fill_iface_data(dev, ifcidx, 0);
733 if (err) {
734 while (--ifcidx >= 0)
735 usbd_free_iface_data(dev, ifcidx);
736 goto bad;
737 }
738 }
739
740 return (USBD_NORMAL_COMPLETION);
741
742 bad:
743 free(cdp, M_USB);
744 return (err);
745 }
746
747 /* XXX add function for alternate settings */
748
749 usbd_status
750 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
751 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
752 {
753 usbd_pipe_handle p;
754 usbd_status err;
755
756 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
757 dev, iface, ep, pipe));
758 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
759 if (p == NULL)
760 return (USBD_NOMEM);
761 p->device = dev;
762 p->iface = iface;
763 p->endpoint = ep;
764 ep->refcnt++;
765 p->refcnt = 1;
766 p->intrxfer = 0;
767 p->running = 0;
768 p->aborting = 0;
769 p->repeat = 0;
770 p->interval = ival;
771 SIMPLEQ_INIT(&p->queue);
772 err = dev->bus->methods->open_pipe(p);
773 if (err) {
774 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
775 "%s\n",
776 ep->edesc->bEndpointAddress, usbd_errstr(err)));
777 free(p, M_USB);
778 return (err);
779 }
780 /* Clear any stall and make sure DATA0 toggle will be used next. */
781 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT) {
782 err = usbd_clear_endpoint_stall(p);
783 /* Some devices reject this command, so ignore a STALL. */
784 if (err && err != USBD_STALLED) {
785 printf("usbd_setup_pipe: failed to start endpoint, %s\n", usbd_errstr(err));
786 return (err);
787 }
788 }
789 *pipe = p;
790 return (USBD_NORMAL_COMPLETION);
791 }
792
793 /* Abort the device control pipe. */
794 void
795 usbd_kill_pipe(usbd_pipe_handle pipe)
796 {
797 usbd_abort_pipe(pipe);
798 pipe->methods->close(pipe);
799 pipe->endpoint->refcnt--;
800 free(pipe, M_USB);
801 }
802
803 int
804 usbd_getnewaddr(usbd_bus_handle bus)
805 {
806 int addr;
807
808 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
809 if (bus->devices[addr] == 0)
810 return (addr);
811 return (-1);
812 }
813
814
815 usbd_status
816 usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
817 int port, int addr)
818 {
819 struct usb_attach_arg uaa;
820 usb_device_descriptor_t *dd = &dev->ddesc;
821 int found, i, confi, nifaces;
822 usbd_status err;
823 device_ptr_t dv;
824 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
825
826 #if defined(__FreeBSD__)
827 /*
828 * XXX uaa is a static var. Not a problem as it _should_ be used only
829 * during probe and attach. Should be changed however.
830 */
831 device_t bdev;
832 bdev = device_add_child(parent, NULL, -1, &uaa);
833 if (!bdev) {
834 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
835 return (USBD_INVAL);
836 }
837 device_quiet(bdev);
838 #endif
839
840 uaa.device = dev;
841 uaa.iface = NULL;
842 uaa.ifaces = NULL;
843 uaa.nifaces = 0;
844 uaa.usegeneric = 0;
845 uaa.port = port;
846 uaa.configno = UHUB_UNK_CONFIGURATION;
847 uaa.ifaceno = UHUB_UNK_INTERFACE;
848 uaa.vendor = UGETW(dd->idVendor);
849 uaa.product = UGETW(dd->idProduct);
850 uaa.release = UGETW(dd->bcdDevice);
851
852 /* First try with device specific drivers. */
853 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
854 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
855 if (dv) {
856 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
857 if (dev->subdevs == NULL)
858 return (USBD_NOMEM);
859 dev->subdevs[0] = dv;
860 dev->subdevs[1] = 0;
861 return (USBD_NORMAL_COMPLETION);
862 }
863
864 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
865
866 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
867 dd->bNumConfigurations));
868 /* Next try with interface drivers. */
869 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
870 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
871 confi));
872 err = usbd_set_config_index(dev, confi, 1);
873 if (err) {
874 #ifdef USB_DEBUG
875 DPRINTF(("%s: port %d, set config at addr %d failed, "
876 "error=%s\n", USBDEVPTRNAME(parent), port,
877 addr, usbd_errstr(err)));
878 #else
879 printf("%s: port %d, set config at addr %d failed\n",
880 USBDEVPTRNAME(parent), port, addr);
881 #endif
882 #if defined(__FreeBSD__)
883 device_delete_child(parent, bdev);
884 #endif
885
886 return (err);
887 }
888 nifaces = dev->cdesc->bNumInterface;
889 uaa.configno = dev->cdesc->bConfigurationValue;
890 for (i = 0; i < nifaces; i++)
891 ifaces[i] = &dev->ifaces[i];
892 uaa.ifaces = ifaces;
893 uaa.nifaces = nifaces;
894 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
895 if (dev->subdevs == NULL) {
896 #if defined(__FreeBSD__)
897 device_delete_child(parent, bdev);
898 #endif
899 return (USBD_NOMEM);
900 }
901
902 found = 0;
903 for (i = 0; i < nifaces; i++) {
904 if (ifaces[i] == NULL)
905 continue; /* interface already claimed */
906 uaa.iface = ifaces[i];
907 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
908 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
909 usbd_submatch);
910 if (dv != NULL) {
911 dev->subdevs[found++] = dv;
912 dev->subdevs[found] = 0;
913 ifaces[i] = 0; /* consumed */
914
915 #if defined(__FreeBSD__)
916 /* create another child for the next iface */
917 bdev = device_add_child(parent, NULL, -1,&uaa);
918 if (!bdev) {
919 printf("%s: Device creation failed\n",
920 USBDEVNAME(dev->bus->bdev));
921 return (USBD_NORMAL_COMPLETION);
922 }
923 device_quiet(bdev);
924 #endif
925 }
926 }
927 if (found != 0) {
928 #if defined(__FreeBSD__)
929 /* remove the last created child again; it is unused */
930 device_delete_child(parent, bdev);
931 #endif
932 return (USBD_NORMAL_COMPLETION);
933 }
934 free(dev->subdevs, M_USB);
935 dev->subdevs = 0;
936 }
937 /* No interfaces were attached in any of the configurations. */
938
939 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
940 usbd_set_config_index(dev, 0, 0);
941
942 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
943
944 /* Finally try the generic driver. */
945 uaa.iface = NULL;
946 uaa.usegeneric = 1;
947 uaa.configno = UHUB_UNK_CONFIGURATION;
948 uaa.ifaceno = UHUB_UNK_INTERFACE;
949 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
950 if (dv != NULL) {
951 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
952 if (dev->subdevs == 0)
953 return (USBD_NOMEM);
954 dev->subdevs[0] = dv;
955 dev->subdevs[1] = 0;
956 return (USBD_NORMAL_COMPLETION);
957 }
958
959 /*
960 * The generic attach failed, but leave the device as it is.
961 * We just did not find any drivers, that's all. The device is
962 * fully operational and not harming anyone.
963 */
964 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
965 #if defined(__FreeBSD__)
966 device_delete_child(parent, bdev);
967 #endif
968 return (USBD_NORMAL_COMPLETION);
969 }
970
971
972 /*
973 * Called when a new device has been put in the powered state,
974 * but not yet in the addressed state.
975 * Get initial descriptor, set the address, get full descriptor,
976 * and attach a driver.
977 */
978 usbd_status
979 usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
980 int speed, int port, struct usbd_port *up)
981 {
982 usbd_device_handle dev;
983 struct usbd_device *hub;
984 usb_device_descriptor_t *dd;
985 usb_port_status_t ps;
986 usbd_status err;
987 int addr;
988 int i;
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 for (hub = up->parent;
1023 hub != NULL && hub->speed != USB_SPEED_HIGH;
1024 hub = hub->myhub)
1025 ;
1026 dev->myhighhub = hub;
1027 dev->speed = speed;
1028 dev->langid = USBD_NOLANG;
1029 dev->cookie.cookie = ++usb_cookie_no;
1030
1031 /* Establish the default pipe. */
1032 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1033 &dev->default_pipe);
1034 if (err) {
1035 usbd_remove_device(dev, up);
1036 return (err);
1037 }
1038
1039 up->device = dev;
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 = 0;
1156 dev->bus->devices[dev->address] = 0;
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 char devinfo[1024];
1167
1168 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1169 if (pnp) {
1170 if (!uaa->usegeneric)
1171 return (QUIET);
1172 usbd_devinfo(uaa->device, 1, devinfo, sizeof(devinfo));
1173 aprint_normal("%s, %s", devinfo, pnp);
1174 }
1175 if (uaa->port != 0)
1176 aprint_normal(" port %d", uaa->port);
1177 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1178 aprint_normal(" configuration %d", uaa->configno);
1179 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1180 aprint_normal(" interface %d", uaa->ifaceno);
1181 #if 0
1182 /*
1183 * It gets very crowded with these locators on the attach line.
1184 * They are not really needed since they are printed in the clear
1185 * by each driver.
1186 */
1187 if (uaa->vendor != UHUB_UNK_VENDOR)
1188 aprint_normal(" vendor 0x%04x", uaa->vendor);
1189 if (uaa->product != UHUB_UNK_PRODUCT)
1190 aprint_normal(" product 0x%04x", uaa->product);
1191 if (uaa->release != UHUB_UNK_RELEASE)
1192 aprint_normal(" release 0x%04x", uaa->release);
1193 #endif
1194 return (UNCONF);
1195 }
1196
1197 #if defined(__NetBSD__)
1198 int
1199 usbd_submatch(struct device *parent, struct cfdata *cf, void *aux)
1200 {
1201 #elif defined(__OpenBSD__)
1202 int
1203 usbd_submatch(struct device *parent, void *match, void *aux)
1204 {
1205 struct cfdata *cf = match;
1206 #endif
1207 struct usb_attach_arg *uaa = aux;
1208
1209 DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1210 "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1211 uaa->port, cf->uhubcf_port,
1212 uaa->configno, cf->uhubcf_configuration,
1213 uaa->ifaceno, cf->uhubcf_interface,
1214 uaa->vendor, cf->uhubcf_vendor,
1215 uaa->product, cf->uhubcf_product,
1216 uaa->release, cf->uhubcf_release));
1217 if (uaa->port != 0 && /* root hub has port 0, it should match */
1218 ((uaa->port != 0 &&
1219 cf->uhubcf_port != UHUB_UNK_PORT &&
1220 cf->uhubcf_port != uaa->port) ||
1221 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1222 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1223 cf->uhubcf_configuration != uaa->configno) ||
1224 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1225 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1226 cf->uhubcf_interface != uaa->ifaceno) ||
1227 (uaa->vendor != UHUB_UNK_VENDOR &&
1228 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1229 cf->uhubcf_vendor != uaa->vendor) ||
1230 (uaa->product != UHUB_UNK_PRODUCT &&
1231 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1232 cf->uhubcf_product != uaa->product) ||
1233 (uaa->release != UHUB_UNK_RELEASE &&
1234 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1235 cf->uhubcf_release != uaa->release)
1236 )
1237 )
1238 return 0;
1239 if (cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1240 cf->uhubcf_vendor == uaa->vendor &&
1241 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1242 cf->uhubcf_product == uaa->product) {
1243 /* We have a vendor&product locator match */
1244 if (cf->uhubcf_release != UHUB_UNK_RELEASE &&
1245 cf->uhubcf_release == uaa->release)
1246 uaa->matchlvl = UMATCH_VENDOR_PRODUCT_REV;
1247 else
1248 uaa->matchlvl = UMATCH_VENDOR_PRODUCT;
1249 } else
1250 uaa->matchlvl = 0;
1251 return (config_match(parent, cf, aux));
1252 }
1253
1254 #endif
1255
1256 void
1257 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1258 int usedev)
1259 {
1260 struct usbd_port *p;
1261 int i, err, s;
1262
1263 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1264 di->udi_addr = dev->address;
1265 di->udi_cookie = dev->cookie;
1266 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1267 di->udi_product, sizeof(di->udi_product), usedev);
1268 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1269 UGETW(dev->ddesc.bcdDevice));
1270 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1271 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1272 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1273 di->udi_class = dev->ddesc.bDeviceClass;
1274 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1275 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1276 di->udi_config = dev->config;
1277 di->udi_power = dev->self_powered ? 0 : dev->power;
1278 di->udi_speed = dev->speed;
1279
1280 if (dev->subdevs != NULL) {
1281 for (i = 0; dev->subdevs[i] &&
1282 i < USB_MAX_DEVNAMES; i++) {
1283 strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1284 USB_MAX_DEVNAMELEN);
1285 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
1286 }
1287 } else {
1288 i = 0;
1289 }
1290 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1291 di->udi_devnames[i][0] = 0; /* empty */
1292
1293 if (dev->hub) {
1294 for (i = 0;
1295 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1296 i < dev->hub->hubdesc.bNbrPorts;
1297 i++) {
1298 p = &dev->hub->ports[i];
1299 if (p->device)
1300 err = p->device->address;
1301 else {
1302 s = UGETW(p->status.wPortStatus);
1303 if (s & UPS_PORT_ENABLED)
1304 err = USB_PORT_ENABLED;
1305 else if (s & UPS_SUSPEND)
1306 err = USB_PORT_SUSPENDED;
1307 else if (s & UPS_PORT_POWER)
1308 err = USB_PORT_POWERED;
1309 else
1310 err = USB_PORT_DISABLED;
1311 }
1312 di->udi_ports[i] = err;
1313 }
1314 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1315 } else
1316 di->udi_nports = 0;
1317 }
1318
1319 void
1320 usb_free_device(usbd_device_handle dev)
1321 {
1322 int ifcidx, nifc;
1323
1324 if (dev->default_pipe != NULL)
1325 usbd_kill_pipe(dev->default_pipe);
1326 if (dev->ifaces != NULL) {
1327 nifc = dev->cdesc->bNumInterface;
1328 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1329 usbd_free_iface_data(dev, ifcidx);
1330 free(dev->ifaces, M_USB);
1331 }
1332 if (dev->cdesc != NULL)
1333 free(dev->cdesc, M_USB);
1334 if (dev->subdevs != NULL)
1335 free(dev->subdevs, M_USB);
1336 free(dev, M_USB);
1337 }
1338
1339 /*
1340 * The general mechanism for detaching drivers works as follows: Each
1341 * driver is responsible for maintaining a reference count on the
1342 * number of outstanding references to its softc (e.g. from
1343 * processing hanging in a read or write). The detach method of the
1344 * driver decrements this counter and flags in the softc that the
1345 * driver is dying and then wakes any sleepers. It then sleeps on the
1346 * softc. Each place that can sleep must maintain the reference
1347 * count. When the reference count drops to -1 (0 is the normal value
1348 * of the reference count) the a wakeup on the softc is performed
1349 * signaling to the detach waiter that all references are gone.
1350 */
1351
1352 /*
1353 * Called from process context when we discover that a port has
1354 * been disconnected.
1355 */
1356 void
1357 usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1358 {
1359 usbd_device_handle dev = up->device;
1360 char *hubname = USBDEVPTRNAME(parent);
1361 int i;
1362
1363 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1364 up, dev, up->portno));
1365
1366 #ifdef DIAGNOSTIC
1367 if (dev == NULL) {
1368 printf("usb_disconnect_port: no device\n");
1369 return;
1370 }
1371 #endif
1372
1373 if (dev->subdevs != NULL) {
1374 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1375 for (i = 0; dev->subdevs[i]; i++) {
1376 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1377 hubname);
1378 if (up->portno != 0)
1379 printf(" port %d", up->portno);
1380 printf(" (addr %d) disconnected\n", dev->address);
1381 config_detach(dev->subdevs[i], DETACH_FORCE);
1382 dev->subdevs[i] = 0;
1383 }
1384 }
1385
1386 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1387 dev->bus->devices[dev->address] = NULL;
1388 up->device = NULL;
1389 usb_free_device(dev);
1390 }
1391
1392 #ifdef __OpenBSD__
1393 void *usb_realloc(void *p, u_int size, int pool, int flags)
1394 {
1395 void *q;
1396
1397 q = malloc(size, pool, flags);
1398 if (q == NULL)
1399 return (NULL);
1400 bcopy(p, q, size);
1401 free(p, pool);
1402 return (q);
1403 }
1404 #endif
1405