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