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