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