usb_subr.c revision 1.142 1 /* $NetBSD: usb_subr.c,v 1.142 2007/01/24 12:36:56 drochner 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.142 2007/01/24 12:36:56 drochner 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_config_descriptor_t cd, *cdp;
572 usbd_status err;
573 int i, ifcidx, nifc, len, selfpowered, power;
574
575 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
576
577 /* XXX check that all interfaces are idle */
578 if (dev->config != USB_UNCONFIG_NO) {
579 DPRINTF(("usbd_set_config_index: free old config\n"));
580 /* Free all configuration data structures. */
581 nifc = dev->cdesc->bNumInterface;
582 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
583 usbd_free_iface_data(dev, ifcidx);
584 free(dev->ifaces, M_USB);
585 free(dev->cdesc, M_USB);
586 dev->ifaces = NULL;
587 dev->cdesc = NULL;
588 dev->config = USB_UNCONFIG_NO;
589 }
590
591 if (index == USB_UNCONFIG_INDEX) {
592 /* We are unconfiguring the device, so leave unallocated. */
593 DPRINTF(("usbd_set_config_index: set config 0\n"));
594 err = usbd_set_config(dev, USB_UNCONFIG_NO);
595 if (err) {
596 DPRINTF(("usbd_set_config_index: setting config=0 "
597 "failed, error=%s\n", usbd_errstr(err)));
598 }
599 return (err);
600 }
601
602 /* Get the short descriptor. */
603 err = usbd_get_config_desc(dev, index, &cd);
604 if (err)
605 return (err);
606 len = UGETW(cd.wTotalLength);
607 cdp = malloc(len, M_USB, M_NOWAIT);
608 if (cdp == NULL)
609 return (USBD_NOMEM);
610
611 /* Get the full descriptor. Try a few times for slow devices. */
612 for (i = 0; i < 3; i++) {
613 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
614 if (!err)
615 break;
616 usbd_delay_ms(dev, 200);
617 }
618 if (err)
619 goto bad;
620
621 if (cdp->bDescriptorType != UDESC_CONFIG) {
622 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
623 cdp->bDescriptorType));
624 err = USBD_INVAL;
625 goto bad;
626 }
627
628 /*
629 * Figure out if the device is self or bus powered.
630 */
631 #if 0 /* XXX various devices don't report the power state correctly */
632 selfpowered = 0;
633 err = usbd_get_device_status(dev, &ds);
634 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
635 selfpowered = 1;
636 #endif
637 /*
638 * Use the power state in the configuration we are going
639 * to set. This doesn't necessarily reflect the actual
640 * power state of the device; the driver can control this
641 * by choosing the appropriate configuration.
642 */
643 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
644
645 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
646 "selfpowered=%d, power=%d\n",
647 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
648 selfpowered, cdp->bMaxPower * 2));
649
650 /* Check if we have enough power. */
651 #if 0 /* this is a no-op, see above */
652 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
653 if (msg)
654 printf("%s: device addr %d (config %d): "
655 "can't set self powered configuration\n",
656 USBDEVNAME(dev->bus->bdev), dev->address,
657 cdp->bConfigurationValue);
658 err = USBD_NO_POWER;
659 goto bad;
660 }
661 #endif
662 #ifdef USB_DEBUG
663 if (dev->powersrc == NULL) {
664 DPRINTF(("usbd_set_config_index: No power source?\n"));
665 err = USBD_IOERROR;
666 goto bad;
667 }
668 #endif
669 power = cdp->bMaxPower * 2;
670 if (power > dev->powersrc->power) {
671 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
672 /* XXX print nicer message. */
673 if (msg)
674 printf("%s: device addr %d (config %d) exceeds power "
675 "budget, %d mA > %d mA\n",
676 USBDEVNAME(dev->bus->bdev), dev->address,
677 cdp->bConfigurationValue,
678 power, dev->powersrc->power);
679 err = USBD_NO_POWER;
680 goto bad;
681 }
682 dev->power = power;
683 dev->self_powered = selfpowered;
684
685 /* Set the actual configuration value. */
686 DPRINTF(("usbd_set_config_index: set config %d\n",
687 cdp->bConfigurationValue));
688 err = usbd_set_config(dev, cdp->bConfigurationValue);
689 if (err) {
690 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
691 "error=%s\n",
692 cdp->bConfigurationValue, usbd_errstr(err)));
693 goto bad;
694 }
695
696 /* Allocate and fill interface data. */
697 nifc = cdp->bNumInterface;
698 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
699 M_USB, M_NOWAIT);
700 if (dev->ifaces == NULL) {
701 err = USBD_NOMEM;
702 goto bad;
703 }
704 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
705 dev->cdesc = cdp;
706 dev->config = cdp->bConfigurationValue;
707 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
708 err = usbd_fill_iface_data(dev, ifcidx, 0);
709 if (err) {
710 while (--ifcidx >= 0)
711 usbd_free_iface_data(dev, ifcidx);
712 goto bad;
713 }
714 }
715
716 return (USBD_NORMAL_COMPLETION);
717
718 bad:
719 free(cdp, M_USB);
720 return (err);
721 }
722
723 /* XXX add function for alternate settings */
724
725 usbd_status
726 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
727 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
728 {
729 usbd_pipe_handle p;
730 usbd_status err;
731
732 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
733 dev, iface, ep, pipe));
734 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
735 if (p == NULL)
736 return (USBD_NOMEM);
737 p->device = dev;
738 p->iface = iface;
739 p->endpoint = ep;
740 ep->refcnt++;
741 p->refcnt = 1;
742 p->intrxfer = 0;
743 p->running = 0;
744 p->aborting = 0;
745 p->repeat = 0;
746 p->interval = ival;
747 SIMPLEQ_INIT(&p->queue);
748 err = dev->bus->methods->open_pipe(p);
749 if (err) {
750 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
751 "%s\n",
752 ep->edesc->bEndpointAddress, usbd_errstr(err)));
753 free(p, M_USB);
754 return (err);
755 }
756 *pipe = p;
757 return (USBD_NORMAL_COMPLETION);
758 }
759
760 /* Abort the device control pipe. */
761 void
762 usbd_kill_pipe(usbd_pipe_handle pipe)
763 {
764 usbd_abort_pipe(pipe);
765 pipe->methods->close(pipe);
766 pipe->endpoint->refcnt--;
767 free(pipe, M_USB);
768 }
769
770 int
771 usbd_getnewaddr(usbd_bus_handle bus)
772 {
773 int addr;
774
775 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
776 if (bus->devices[addr] == 0)
777 return (addr);
778 return (-1);
779 }
780
781
782 usbd_status
783 usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
784 int port, int addr)
785 {
786 struct usb_attach_arg uaa;
787 usb_device_descriptor_t *dd = &dev->ddesc;
788 int found, i, confi, nifaces;
789 usbd_status err;
790 device_ptr_t dv;
791 usbd_interface_handle *ifaces;
792
793 #if defined(__FreeBSD__)
794 /*
795 * XXX uaa is a static var. Not a problem as it _should_ be used only
796 * during probe and attach. Should be changed however.
797 */
798 device_t bdev;
799 bdev = device_add_child(parent, NULL, -1, &uaa);
800 if (!bdev) {
801 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
802 return (USBD_INVAL);
803 }
804 device_quiet(bdev);
805 #endif
806
807 uaa.device = dev;
808 uaa.iface = NULL;
809 uaa.ifaces = NULL;
810 uaa.nifaces = 0;
811 uaa.usegeneric = 0;
812 uaa.port = port;
813 uaa.configno = UHUB_UNK_CONFIGURATION;
814 uaa.ifaceno = UHUB_UNK_INTERFACE;
815 uaa.vendor = UGETW(dd->idVendor);
816 uaa.product = UGETW(dd->idProduct);
817 uaa.release = UGETW(dd->bcdDevice);
818
819 /* First try with device specific drivers. */
820 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
821 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
822 if (dv) {
823 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
824 if (dev->subdevs == NULL)
825 return (USBD_NOMEM);
826 dev->subdevs[0] = dv;
827 dev->subdevs[1] = 0;
828 return (USBD_NORMAL_COMPLETION);
829 }
830
831 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
832
833 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
834 dd->bNumConfigurations));
835 /* Next try with interface drivers. */
836 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
837 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
838 confi));
839 err = usbd_set_config_index(dev, confi, 1);
840 if (err) {
841 #ifdef USB_DEBUG
842 DPRINTF(("%s: port %d, set config at addr %d failed, "
843 "error=%s\n", USBDEVPTRNAME(parent), port,
844 addr, usbd_errstr(err)));
845 #else
846 printf("%s: port %d, set config at addr %d failed\n",
847 USBDEVPTRNAME(parent), port, addr);
848 #endif
849 #if defined(__FreeBSD__)
850 device_delete_child(parent, bdev);
851 #endif
852
853 return (err);
854 }
855 nifaces = dev->cdesc->bNumInterface;
856 uaa.configno = dev->cdesc->bConfigurationValue;
857 ifaces = malloc(nifaces * sizeof(*ifaces), M_USB, M_NOWAIT);
858 if (ifaces == NULL)
859 goto nomem;
860 for (i = 0; i < nifaces; i++)
861 ifaces[i] = &dev->ifaces[i];
862 uaa.ifaces = ifaces;
863 uaa.nifaces = nifaces;
864 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
865 if (dev->subdevs == NULL) {
866 free(ifaces, M_USB);
867 nomem:
868 #if defined(__FreeBSD__)
869 device_delete_child(parent, bdev);
870 #endif
871 return (USBD_NOMEM);
872 }
873
874 found = 0;
875 for (i = 0; i < nifaces; i++) {
876 if (ifaces[i] == NULL)
877 continue; /* interface already claimed */
878 uaa.iface = ifaces[i];
879 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
880 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
881 usbd_submatch);
882 if (dv != NULL) {
883 dev->subdevs[found++] = dv;
884 dev->subdevs[found] = 0;
885 ifaces[i] = 0; /* consumed */
886
887 #if defined(__FreeBSD__)
888 /* create another child for the next iface */
889 bdev = device_add_child(parent, NULL, -1,&uaa);
890 if (!bdev) {
891 printf("%s: Device creation failed\n",
892 USBDEVNAME(dev->bus->bdev));
893 free(ifaces, M_USB);
894 return (USBD_NORMAL_COMPLETION);
895 }
896 device_quiet(bdev);
897 #endif
898 }
899 }
900 if (found != 0) {
901 #if defined(__FreeBSD__)
902 /* remove the last created child again; it is unused */
903 device_delete_child(parent, bdev);
904 #endif
905 free(ifaces, M_USB);
906 return (USBD_NORMAL_COMPLETION);
907 }
908 free(ifaces, M_USB);
909 free(dev->subdevs, M_USB);
910 dev->subdevs = 0;
911 }
912 /* No interfaces were attached in any of the configurations. */
913
914 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
915 usbd_set_config_index(dev, 0, 0);
916
917 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
918
919 /* Finally try the generic driver. */
920 uaa.iface = NULL;
921 uaa.usegeneric = 1;
922 uaa.configno = UHUB_UNK_CONFIGURATION;
923 uaa.ifaceno = UHUB_UNK_INTERFACE;
924 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
925 if (dv != NULL) {
926 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
927 if (dev->subdevs == 0)
928 return (USBD_NOMEM);
929 dev->subdevs[0] = dv;
930 dev->subdevs[1] = 0;
931 return (USBD_NORMAL_COMPLETION);
932 }
933
934 /*
935 * The generic attach failed, but leave the device as it is.
936 * We just did not find any drivers, that's all. The device is
937 * fully operational and not harming anyone.
938 */
939 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
940 #if defined(__FreeBSD__)
941 device_delete_child(parent, bdev);
942 #endif
943 return (USBD_NORMAL_COMPLETION);
944 }
945
946
947 /*
948 * Called when a new device has been put in the powered state,
949 * but not yet in the addressed state.
950 * Get initial descriptor, set the address, get full descriptor,
951 * and attach a driver.
952 */
953 usbd_status
954 usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
955 int speed, int port, struct usbd_port *up)
956 {
957 usbd_device_handle dev, adev;
958 struct usbd_device *hub;
959 usb_device_descriptor_t *dd;
960 usb_port_status_t ps;
961 usbd_status err;
962 int addr;
963 int i;
964 int p;
965
966 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
967 bus, port, depth, speed));
968 addr = usbd_getnewaddr(bus);
969 if (addr < 0) {
970 printf("%s: No free USB addresses, new device ignored.\n",
971 USBDEVNAME(bus->bdev));
972 return (USBD_NO_ADDR);
973 }
974
975 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
976 if (dev == NULL)
977 return (USBD_NOMEM);
978
979 dev->bus = bus;
980
981 /* Set up default endpoint handle. */
982 dev->def_ep.edesc = &dev->def_ep_desc;
983
984 /* Set up default endpoint descriptor. */
985 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
986 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
987 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
988 dev->def_ep_desc.bmAttributes = UE_CONTROL;
989 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
990 dev->def_ep_desc.bInterval = 0;
991
992 dev->quirks = &usbd_no_quirk;
993 dev->address = USB_START_ADDR;
994 dev->ddesc.bMaxPacketSize = 0;
995 dev->depth = depth;
996 dev->powersrc = up;
997 dev->myhub = up->parent;
998
999 up->device = dev;
1000
1001 /* Locate port on upstream high speed hub */
1002 for (adev = dev, hub = up->parent;
1003 hub != NULL && hub->speed != USB_SPEED_HIGH;
1004 adev = hub, hub = hub->myhub)
1005 ;
1006 if (hub) {
1007 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1008 if (hub->hub->ports[p].device == adev) {
1009 dev->myhsport = &hub->hub->ports[p];
1010 goto found;
1011 }
1012 }
1013 panic("usbd_new_device: cannot find HS port\n");
1014 found:
1015 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1016 } else {
1017 dev->myhsport = NULL;
1018 }
1019 dev->speed = speed;
1020 dev->langid = USBD_NOLANG;
1021 dev->cookie.cookie = ++usb_cookie_no;
1022
1023 /* Establish the default pipe. */
1024 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1025 &dev->default_pipe);
1026 if (err) {
1027 usbd_remove_device(dev, up);
1028 return (err);
1029 }
1030
1031 /* Set the address. Do this early; some devices need that. */
1032 err = usbd_set_address(dev, addr);
1033 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1034 if (err) {
1035 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1036 err = USBD_SET_ADDR_FAILED;
1037 usbd_remove_device(dev, up);
1038 return (err);
1039 }
1040 /* Allow device time to set new address */
1041 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1042 dev->address = addr; /* New device address now */
1043 bus->devices[addr] = dev;
1044
1045 dd = &dev->ddesc;
1046 /* Try a few times in case the device is slow (i.e. outside specs.) */
1047 for (i = 0; i < 10; i++) {
1048 /* Get the first 8 bytes of the device descriptor. */
1049 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1050 if (!err)
1051 break;
1052 usbd_delay_ms(dev, 200);
1053 if ((i & 3) == 3)
1054 usbd_reset_port(up->parent, port, &ps);
1055 }
1056 if (err) {
1057 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1058 "failed\n", addr));
1059 usbd_remove_device(dev, up);
1060 return (err);
1061 }
1062
1063 if (speed == USB_SPEED_HIGH) {
1064 /* Max packet size must be 64 (sec 5.5.3). */
1065 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1066 #ifdef DIAGNOSTIC
1067 printf("usbd_new_device: addr=%d bad max packet size\n",
1068 addr);
1069 #endif
1070 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1071 }
1072 }
1073
1074 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1075 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1076 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1077 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1078 dev->speed));
1079
1080 if (dd->bDescriptorType != UDESC_DEVICE) {
1081 /* Illegal device descriptor */
1082 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1083 dd->bDescriptorType));
1084 usbd_remove_device(dev, up);
1085 return (USBD_INVAL);
1086 }
1087
1088 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1089 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1090 usbd_remove_device(dev, up);
1091 return (USBD_INVAL);
1092 }
1093
1094 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1095
1096 err = usbd_reload_device_desc(dev);
1097 if (err) {
1098 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1099 "failed\n", addr));
1100 usbd_remove_device(dev, up);
1101 return (err);
1102 }
1103
1104 /* Assume 100mA bus powered for now. Changed when configured. */
1105 dev->power = USB_MIN_POWER;
1106 dev->self_powered = 0;
1107
1108 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1109 addr, dev, parent));
1110
1111 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1112
1113 err = usbd_probe_and_attach(parent, dev, port, addr);
1114 if (err) {
1115 usbd_remove_device(dev, up);
1116 return (err);
1117 }
1118
1119 return (USBD_NORMAL_COMPLETION);
1120 }
1121
1122 usbd_status
1123 usbd_reload_device_desc(usbd_device_handle dev)
1124 {
1125 usbd_status err;
1126
1127 /* Get the full device descriptor. */
1128 err = usbd_get_device_desc(dev, &dev->ddesc);
1129 if (err)
1130 return (err);
1131
1132 /* Figure out what's wrong with this device. */
1133 dev->quirks = usbd_find_quirk(&dev->ddesc);
1134
1135 return (USBD_NORMAL_COMPLETION);
1136 }
1137
1138 void
1139 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1140 {
1141 DPRINTF(("usbd_remove_device: %p\n", dev));
1142
1143 if (dev->default_pipe != NULL)
1144 usbd_kill_pipe(dev->default_pipe);
1145 up->device = NULL;
1146 dev->bus->devices[dev->address] = NULL;
1147
1148 free(dev, M_USB);
1149 }
1150
1151 #if defined(__NetBSD__) || defined(__OpenBSD__)
1152 int
1153 usbd_print(void *aux, const char *pnp)
1154 {
1155 struct usb_attach_arg *uaa = aux;
1156
1157 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1158 if (pnp) {
1159 #define USB_DEVINFO 1024
1160 char *devinfo;
1161 if (!uaa->usegeneric)
1162 return (QUIET);
1163 devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK);
1164 usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO);
1165 aprint_normal("%s, %s", devinfo, pnp);
1166 free(devinfo, M_TEMP);
1167 }
1168 if (uaa->port != 0)
1169 aprint_normal(" port %d", uaa->port);
1170 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1171 aprint_normal(" configuration %d", uaa->configno);
1172 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1173 aprint_normal(" interface %d", uaa->ifaceno);
1174 #if 0
1175 /*
1176 * It gets very crowded with these locators on the attach line.
1177 * They are not really needed since they are printed in the clear
1178 * by each driver.
1179 */
1180 if (uaa->vendor != UHUB_UNK_VENDOR)
1181 aprint_normal(" vendor 0x%04x", uaa->vendor);
1182 if (uaa->product != UHUB_UNK_PRODUCT)
1183 aprint_normal(" product 0x%04x", uaa->product);
1184 if (uaa->release != UHUB_UNK_RELEASE)
1185 aprint_normal(" release 0x%04x", uaa->release);
1186 #endif
1187 return (UNCONF);
1188 }
1189
1190 #if defined(__NetBSD__)
1191 int
1192 usbd_submatch(struct device *parent, struct cfdata *cf,
1193 const int *ldesc, void *aux)
1194 {
1195 #elif defined(__OpenBSD__)
1196 int
1197 usbd_submatch(struct device *parent, void *match, void *aux)
1198 {
1199 struct cfdata *cf = match;
1200 #endif
1201 struct usb_attach_arg *uaa = aux;
1202
1203 DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1204 "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1205 uaa->port, cf->uhubcf_port,
1206 uaa->configno, cf->uhubcf_configuration,
1207 uaa->ifaceno, cf->uhubcf_interface,
1208 uaa->vendor, cf->uhubcf_vendor,
1209 uaa->product, cf->uhubcf_product,
1210 uaa->release, cf->uhubcf_release));
1211 if (uaa->port != 0 && /* root hub has port 0, it should match */
1212 ((cf->uhubcf_port != UHUB_UNK_PORT &&
1213 cf->uhubcf_port != uaa->port) ||
1214 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1215 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1216 cf->uhubcf_configuration != uaa->configno) ||
1217 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1218 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1219 cf->uhubcf_interface != uaa->ifaceno) ||
1220 (uaa->vendor != UHUB_UNK_VENDOR &&
1221 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1222 cf->uhubcf_vendor != uaa->vendor) ||
1223 (uaa->product != UHUB_UNK_PRODUCT &&
1224 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1225 cf->uhubcf_product != uaa->product) ||
1226 (uaa->release != UHUB_UNK_RELEASE &&
1227 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1228 cf->uhubcf_release != uaa->release)
1229 )
1230 )
1231 return 0;
1232 return (config_match(parent, cf, aux));
1233 }
1234
1235 #endif
1236
1237 void
1238 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1239 int usedev)
1240 {
1241 struct usbd_port *p;
1242 int i, err, s;
1243
1244 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1245 di->udi_addr = dev->address;
1246 di->udi_cookie = dev->cookie;
1247 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 1);
1248 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1249 UGETW(dev->ddesc.bcdDevice));
1250 di->udi_serial[0] = 0;
1251 if (usedev)
1252 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
1253 di->udi_serial);
1254 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1255 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1256 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1257 di->udi_class = dev->ddesc.bDeviceClass;
1258 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1259 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1260 di->udi_config = dev->config;
1261 di->udi_power = dev->self_powered ? 0 : dev->power;
1262 di->udi_speed = dev->speed;
1263
1264 if (dev->subdevs != NULL) {
1265 for (i = 0; dev->subdevs[i] &&
1266 i < USB_MAX_DEVNAMES; i++) {
1267 strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1268 USB_MAX_DEVNAMELEN);
1269 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
1270 }
1271 } else {
1272 i = 0;
1273 }
1274 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1275 di->udi_devnames[i][0] = 0; /* empty */
1276
1277 if (dev->hub) {
1278 for (i = 0;
1279 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1280 i < dev->hub->hubdesc.bNbrPorts;
1281 i++) {
1282 p = &dev->hub->ports[i];
1283 if (p->device)
1284 err = p->device->address;
1285 else {
1286 s = UGETW(p->status.wPortStatus);
1287 if (s & UPS_PORT_ENABLED)
1288 err = USB_PORT_ENABLED;
1289 else if (s & UPS_SUSPEND)
1290 err = USB_PORT_SUSPENDED;
1291 else if (s & UPS_PORT_POWER)
1292 err = USB_PORT_POWERED;
1293 else
1294 err = USB_PORT_DISABLED;
1295 }
1296 di->udi_ports[i] = err;
1297 }
1298 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1299 } else
1300 di->udi_nports = 0;
1301 }
1302
1303 #ifdef COMPAT_30
1304 void
1305 usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di,
1306 int usedev)
1307 {
1308 struct usbd_port *p;
1309 int i, err, s;
1310
1311 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1312 di->udi_addr = dev->address;
1313 di->udi_cookie = dev->cookie;
1314 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 0);
1315 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1316 UGETW(dev->ddesc.bcdDevice));
1317 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1318 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1319 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1320 di->udi_class = dev->ddesc.bDeviceClass;
1321 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1322 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1323 di->udi_config = dev->config;
1324 di->udi_power = dev->self_powered ? 0 : dev->power;
1325 di->udi_speed = dev->speed;
1326
1327 if (dev->subdevs != NULL) {
1328 for (i = 0; dev->subdevs[i] &&
1329 i < USB_MAX_DEVNAMES; i++) {
1330 strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1331 USB_MAX_DEVNAMELEN);
1332 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
1333 }
1334 } else {
1335 i = 0;
1336 }
1337 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1338 di->udi_devnames[i][0] = 0; /* empty */
1339
1340 if (dev->hub) {
1341 for (i = 0;
1342 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1343 i < dev->hub->hubdesc.bNbrPorts;
1344 i++) {
1345 p = &dev->hub->ports[i];
1346 if (p->device)
1347 err = p->device->address;
1348 else {
1349 s = UGETW(p->status.wPortStatus);
1350 if (s & UPS_PORT_ENABLED)
1351 err = USB_PORT_ENABLED;
1352 else if (s & UPS_SUSPEND)
1353 err = USB_PORT_SUSPENDED;
1354 else if (s & UPS_PORT_POWER)
1355 err = USB_PORT_POWERED;
1356 else
1357 err = USB_PORT_DISABLED;
1358 }
1359 di->udi_ports[i] = err;
1360 }
1361 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1362 } else
1363 di->udi_nports = 0;
1364 }
1365 #endif
1366
1367
1368 void
1369 usb_free_device(usbd_device_handle dev)
1370 {
1371 int ifcidx, nifc;
1372
1373 if (dev->default_pipe != NULL)
1374 usbd_kill_pipe(dev->default_pipe);
1375 if (dev->ifaces != NULL) {
1376 nifc = dev->cdesc->bNumInterface;
1377 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1378 usbd_free_iface_data(dev, ifcidx);
1379 free(dev->ifaces, M_USB);
1380 }
1381 if (dev->cdesc != NULL)
1382 free(dev->cdesc, M_USB);
1383 if (dev->subdevs != NULL)
1384 free(dev->subdevs, M_USB);
1385 free(dev, M_USB);
1386 }
1387
1388 /*
1389 * The general mechanism for detaching drivers works as follows: Each
1390 * driver is responsible for maintaining a reference count on the
1391 * number of outstanding references to its softc (e.g. from
1392 * processing hanging in a read or write). The detach method of the
1393 * driver decrements this counter and flags in the softc that the
1394 * driver is dying and then wakes any sleepers. It then sleeps on the
1395 * softc. Each place that can sleep must maintain the reference
1396 * count. When the reference count drops to -1 (0 is the normal value
1397 * of the reference count) the a wakeup on the softc is performed
1398 * signaling to the detach waiter that all references are gone.
1399 */
1400
1401 /*
1402 * Called from process context when we discover that a port has
1403 * been disconnected.
1404 */
1405 void
1406 usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1407 {
1408 usbd_device_handle dev = up->device;
1409 char *hubname = USBDEVPTRNAME(parent);
1410 int i;
1411
1412 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1413 up, dev, up->portno));
1414
1415 #ifdef DIAGNOSTIC
1416 if (dev == NULL) {
1417 printf("usb_disconnect_port: no device\n");
1418 return;
1419 }
1420 #endif
1421
1422 if (dev->subdevs != NULL) {
1423 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1424 for (i = 0; dev->subdevs[i]; i++) {
1425 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1426 hubname);
1427 if (up->portno != 0)
1428 printf(" port %d", up->portno);
1429 printf(" (addr %d) disconnected\n", dev->address);
1430 config_detach(dev->subdevs[i], DETACH_FORCE);
1431 dev->subdevs[i] = 0;
1432 }
1433 }
1434
1435 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1436 dev->bus->devices[dev->address] = NULL;
1437 up->device = NULL;
1438 usb_free_device(dev);
1439 }
1440
1441 #ifdef __OpenBSD__
1442 void *usb_realloc(void *p, u_int size, int pool, int flags)
1443 {
1444 void *q;
1445
1446 q = malloc(size, pool, flags);
1447 if (q == NULL)
1448 return (NULL);
1449 bcopy(p, q, size);
1450 free(p, pool);
1451 return (q);
1452 }
1453 #endif
1454