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