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