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