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