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