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