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