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