usb_subr.c revision 1.198 1 /* $NetBSD: usb_subr.c,v 1.198 2014/09/21 14:30:22 christos Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart (at) augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.198 2014/09/21 14:30:22 christos 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 uiaa.device = dev;
855 uiaa.port = port;
856 uiaa.vendor = UGETW(dd->idVendor);
857 uiaa.product = UGETW(dd->idProduct);
858 uiaa.release = UGETW(dd->bcdDevice);
859 uiaa.configno = dev->cdesc->bConfigurationValue;
860 uiaa.ifaces = ifaces;
861 uiaa.nifaces = nifaces;
862 ilocs[USBIFIFCF_PORT] = uiaa.port;
863 ilocs[USBIFIFCF_VENDOR] = uiaa.vendor;
864 ilocs[USBIFIFCF_PRODUCT] = uiaa.product;
865 ilocs[USBIFIFCF_RELEASE] = uiaa.release;
866 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.configno;
867
868 for (i = 0; i < nifaces; i++) {
869 if (!ifaces[i])
870 continue; /* interface already claimed */
871 uiaa.iface = ifaces[i];
872 uiaa.class = ifaces[i]->idesc->bInterfaceClass;
873 uiaa.subclass = ifaces[i]->idesc->bInterfaceSubClass;
874 uiaa.proto = ifaces[i]->idesc->bInterfaceProtocol;
875 uiaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
876 ilocs[USBIFIFCF_INTERFACE] = uiaa.ifaceno;
877 if (locators != NULL) {
878 loc = locators[USBIFIFCF_CONFIGURATION];
879 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT &&
880 loc != uiaa.configno)
881 continue;
882 loc = locators[USBIFIFCF_INTERFACE];
883 if (loc != USBIFIFCF_INTERFACE && loc != uiaa.ifaceno)
884 continue;
885 }
886 dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa,
887 usbd_ifprint, config_stdsubmatch);
888 if (!dv)
889 continue;
890 ifaces[i] = 0; /* claim */
891 /* account for ifaces claimed by the driver behind our back */
892 for (j = 0; j < nifaces; j++) {
893 if (!ifaces[j] && !dev->subdevs[j]) {
894 dev->subdevs[j] = dv;
895 dev->nifaces_claimed++;
896 }
897 }
898 }
899
900 free(ifaces, M_USB);
901 return (USBD_NORMAL_COMPLETION);
902 }
903
904 usbd_status
905 usbd_probe_and_attach(device_t parent, usbd_device_handle dev,
906 int port, int addr)
907 {
908 usb_device_descriptor_t *dd = &dev->ddesc;
909 int confi, nifaces;
910 usbd_status err;
911
912 /* First try with device specific drivers. */
913 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
914 err = usbd_attachwholedevice(parent, dev, port, 0);
915 if (dev->nifaces_claimed || err)
916 return (err);
917 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
918
919 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
920 dd->bNumConfigurations));
921 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
922 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
923 confi));
924 err = usbd_set_config_index(dev, confi, 1);
925 if (err) {
926 #ifdef USB_DEBUG
927 DPRINTF(("%s: port %d, set config at addr %d failed, "
928 "error=%s\n", device_xname(parent), port,
929 addr, usbd_errstr(err)));
930 #else
931 printf("%s: port %d, set config at addr %d failed\n",
932 device_xname(parent), port, addr);
933 #endif
934 return (err);
935 }
936 nifaces = dev->cdesc->bNumInterface;
937 dev->subdevs = malloc(nifaces * sizeof(device_t), M_USB,
938 M_NOWAIT|M_ZERO);
939 if (dev->subdevs == NULL)
940 return (USBD_NOMEM);
941 dev->subdevlen = nifaces;
942
943 err = usbd_attachinterfaces(parent, dev, port, NULL);
944
945 if (!dev->nifaces_claimed) {
946 free(dev->subdevs, M_USB);
947 dev->subdevs = 0;
948 dev->subdevlen = 0;
949 }
950 if (dev->nifaces_claimed || err)
951 return (err);
952 }
953 /* No interfaces were attached in any of the configurations. */
954
955 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
956 usbd_set_config_index(dev, 0, 0);
957
958 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
959
960 /* Finally try the generic driver. */
961 err = usbd_attachwholedevice(parent, dev, port, 1);
962
963 /*
964 * The generic attach failed, but leave the device as it is.
965 * We just did not find any drivers, that's all. The device is
966 * fully operational and not harming anyone.
967 */
968 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
969 return (USBD_NORMAL_COMPLETION);
970 }
971
972 /**
973 * Called from uhub_rescan(). usbd_new_device() for the target dev must be
974 * called before calling this.
975 */
976 usbd_status
977 usbd_reattach_device(device_t parent, usbd_device_handle dev,
978 int port, const int *locators)
979 {
980 int i, loc;
981
982 if (locators != NULL) {
983 loc = locators[USBIFIFCF_PORT];
984 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port)
985 return USBD_NORMAL_COMPLETION;
986 loc = locators[USBIFIFCF_VENDOR];
987 if (loc != USBIFIFCF_VENDOR_DEFAULT &&
988 loc != UGETW(dev->ddesc.idVendor))
989 return USBD_NORMAL_COMPLETION;
990 loc = locators[USBIFIFCF_PRODUCT];
991 if (loc != USBIFIFCF_PRODUCT_DEFAULT &&
992 loc != UGETW(dev->ddesc.idProduct))
993 return USBD_NORMAL_COMPLETION;
994 loc = locators[USBIFIFCF_RELEASE];
995 if (loc != USBIFIFCF_RELEASE_DEFAULT &&
996 loc != UGETW(dev->ddesc.bcdDevice))
997 return USBD_NORMAL_COMPLETION;
998 }
999 if (dev->subdevlen == 0) {
1000 /* XXX: check USBIFIFCF_CONFIGURATION and
1001 * USBIFIFCF_INTERFACE too */
1002 return usbd_probe_and_attach(parent, dev, port, dev->address);
1003 } else if (dev->subdevlen != dev->cdesc->bNumInterface) {
1004 /* device-specific or generic driver is already attached. */
1005 return USBD_NORMAL_COMPLETION;
1006 }
1007 /* Does the device have unconfigured interfaces? */
1008 for (i = 0; i < dev->subdevlen; i++) {
1009 if (dev->subdevs[i] == NULL) {
1010 break;
1011 }
1012 }
1013 if (i >= dev->subdevlen)
1014 return USBD_NORMAL_COMPLETION;
1015 return usbd_attachinterfaces(parent, dev, port, locators);
1016 }
1017
1018 /*
1019 * Get the first 8 bytes of the device descriptor.
1020 * Do as Windows does: try to read 64 bytes -- there are devices which
1021 * recognize the initial descriptor fetch (before the control endpoint's
1022 * MaxPacketSize is known by the host) by exactly this length.
1023 */
1024 usbd_status
1025 usbd_get_initial_ddesc(usbd_device_handle dev, usb_device_descriptor_t *desc)
1026 {
1027 usb_device_request_t req;
1028 char buf[64];
1029 int res, actlen;
1030
1031 req.bmRequestType = UT_READ_DEVICE;
1032 req.bRequest = UR_GET_DESCRIPTOR;
1033 USETW2(req.wValue, UDESC_DEVICE, 0);
1034 USETW(req.wIndex, 0);
1035 USETW(req.wLength, 64);
1036 res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK,
1037 &actlen, USBD_DEFAULT_TIMEOUT);
1038 if (res)
1039 return res;
1040 if (actlen < 8)
1041 return USBD_SHORT_XFER;
1042 memcpy(desc, buf, 8);
1043 return USBD_NORMAL_COMPLETION;
1044 }
1045
1046 /*
1047 * Called when a new device has been put in the powered state,
1048 * but not yet in the addressed state.
1049 * Get initial descriptor, set the address, get full descriptor,
1050 * and attach a driver.
1051 */
1052 usbd_status
1053 usbd_new_device(device_t parent, usbd_bus_handle bus, int depth,
1054 int speed, int port, struct usbd_port *up)
1055 {
1056 usbd_device_handle dev, adev;
1057 struct usbd_device *hub;
1058 usb_device_descriptor_t *dd;
1059 usb_port_status_t ps;
1060 usbd_status err;
1061 int addr;
1062 int i;
1063 int p;
1064
1065 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
1066 bus, port, depth, speed));
1067
1068 if (bus->methods->new_device != NULL)
1069 return (bus->methods->new_device)(parent, bus, depth, speed,
1070 port, up);
1071
1072 addr = usbd_getnewaddr(bus);
1073 if (addr < 0) {
1074 printf("%s: No free USB addresses, new device ignored.\n",
1075 device_xname(bus->usbctl));
1076 return (USBD_NO_ADDR);
1077 }
1078
1079 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1080 if (dev == NULL)
1081 return (USBD_NOMEM);
1082
1083 dev->bus = bus;
1084
1085 /* Set up default endpoint handle. */
1086 dev->def_ep.edesc = &dev->def_ep_desc;
1087
1088 /* Set up default endpoint descriptor. */
1089 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1090 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1091 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1092 dev->def_ep_desc.bmAttributes = UE_CONTROL;
1093 /*
1094 * temporary, will be fixed after first descriptor fetch
1095 * (which uses 64 bytes so it shouldn't be less),
1096 * highspeed devices must support 64 byte packets anyway
1097 */
1098 if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL)
1099 USETW(dev->def_ep_desc.wMaxPacketSize, 64);
1100 else
1101 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1102
1103 dev->def_ep_desc.bInterval = 0;
1104
1105 /* doesn't matter, just don't let it uninitialized */
1106 dev->def_ep.datatoggle = 0;
1107
1108 dev->quirks = &usbd_no_quirk;
1109 dev->address = USB_START_ADDR;
1110 dev->ddesc.bMaxPacketSize = 0;
1111 dev->depth = depth;
1112 dev->powersrc = up;
1113 dev->myhub = up->parent;
1114
1115 up->device = dev;
1116
1117 /* Locate port on upstream high speed hub */
1118 for (adev = dev, hub = up->parent;
1119 hub != NULL && hub->speed != USB_SPEED_HIGH;
1120 adev = hub, hub = hub->myhub)
1121 ;
1122 if (hub) {
1123 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1124 if (hub->hub->ports[p].device == adev) {
1125 dev->myhsport = &hub->hub->ports[p];
1126 goto found;
1127 }
1128 }
1129 panic("usbd_new_device: cannot find HS port\n");
1130 found:
1131 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1132 } else {
1133 dev->myhsport = NULL;
1134 }
1135 dev->speed = speed;
1136 dev->langid = USBD_NOLANG;
1137 dev->cookie.cookie = ++usb_cookie_no;
1138
1139 /* Establish the default pipe. */
1140 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1141 &dev->default_pipe, USBD_MPSAFE);
1142 if (err) {
1143 usbd_remove_device(dev, up);
1144 return (err);
1145 }
1146
1147 dd = &dev->ddesc;
1148 /* Try a few times in case the device is slow (i.e. outside specs.) */
1149 for (i = 0; i < 10; i++) {
1150 /* Get the first 8 bytes of the device descriptor. */
1151 err = usbd_get_initial_ddesc(dev, dd);
1152 if (!err)
1153 break;
1154 usbd_delay_ms(dev, 200);
1155 if ((i & 3) == 3)
1156 usbd_reset_port(up->parent, port, &ps);
1157 }
1158 if (err) {
1159 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1160 "failed: %d\n", addr, err));
1161 usbd_remove_device(dev, up);
1162 return (err);
1163 }
1164
1165 /* Windows resets the port here, do likewise */
1166 if (up->parent)
1167 usbd_reset_port(up->parent, port, &ps);
1168
1169 if (speed == USB_SPEED_HIGH) {
1170 /* Max packet size must be 64 (sec 5.5.3). */
1171 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1172 #ifdef DIAGNOSTIC
1173 printf("usbd_new_device: addr=%d bad max packet "
1174 "size=%d. adjusting to %d.\n",
1175 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET);
1176 #endif
1177 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1178 }
1179 }
1180
1181 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1182 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1183 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1184 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1185 dev->speed));
1186
1187 if (dd->bDescriptorType != UDESC_DEVICE) {
1188 /* Illegal device descriptor */
1189 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1190 dd->bDescriptorType));
1191 usbd_remove_device(dev, up);
1192 return (USBD_INVAL);
1193 }
1194
1195 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1196 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1197 usbd_remove_device(dev, up);
1198 return (USBD_INVAL);
1199 }
1200
1201 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1202
1203 /* Re-establish the default pipe with the new MPS. */
1204 usbd_kill_pipe(dev->default_pipe);
1205 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1206 &dev->default_pipe, USBD_MPSAFE);
1207 if (err) {
1208 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n"));
1209 usbd_remove_device(dev, up);
1210 return err;
1211 }
1212
1213 /* Set the address */
1214 DPRINTFN(5, ("usbd_new_device: setting device address=%d\n", addr));
1215 err = usbd_set_address(dev, addr);
1216 if (err) {
1217 DPRINTFN(-1, ("usbd_new_device: set address %d failed\n", addr));
1218 err = USBD_SET_ADDR_FAILED;
1219 usbd_remove_device(dev, up);
1220 return err;
1221 }
1222
1223 /* Allow device time to set new address */
1224 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1225 dev->address = addr; /* new device address now */
1226 bus->devices[addr] = dev;
1227
1228 /* Re-establish the default pipe with the new address. */
1229 usbd_kill_pipe(dev->default_pipe);
1230 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1231 &dev->default_pipe, USBD_MPSAFE);
1232 if (err) {
1233 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n"));
1234 usbd_remove_device(dev, up);
1235 return err;
1236 }
1237
1238 err = usbd_reload_device_desc(dev);
1239 if (err) {
1240 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1241 "failed\n", addr));
1242 usbd_remove_device(dev, up);
1243 return (err);
1244 }
1245
1246 /* Assume 100mA bus powered for now. Changed when configured. */
1247 dev->power = USB_MIN_POWER;
1248 dev->self_powered = 0;
1249
1250 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1251 addr, dev, parent));
1252
1253 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1254
1255 if (port == 0) { /* root hub */
1256 KASSERT(addr == 1);
1257 usbd_attach_roothub(parent, dev);
1258 return (USBD_NORMAL_COMPLETION);
1259 }
1260
1261 err = usbd_probe_and_attach(parent, dev, port, addr);
1262 if (err) {
1263 usbd_remove_device(dev, up);
1264 return (err);
1265 }
1266
1267 return (USBD_NORMAL_COMPLETION);
1268 }
1269
1270 usbd_status
1271 usbd_reload_device_desc(usbd_device_handle dev)
1272 {
1273 usbd_status err;
1274
1275 /* Get the full device descriptor. */
1276 err = usbd_get_device_desc(dev, &dev->ddesc);
1277 if (err)
1278 return (err);
1279
1280 /* Figure out what's wrong with this device. */
1281 dev->quirks = usbd_find_quirk(&dev->ddesc);
1282
1283 return (USBD_NORMAL_COMPLETION);
1284 }
1285
1286 void
1287 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1288 {
1289
1290 DPRINTF(("usbd_remove_device: %p\n", dev));
1291
1292 if (dev->default_pipe != NULL)
1293 usbd_kill_pipe(dev->default_pipe);
1294 up->device = NULL;
1295 dev->bus->devices[dev->address] = NULL;
1296
1297 free(dev, M_USB);
1298 }
1299
1300 int
1301 usbd_print(void *aux, const char *pnp)
1302 {
1303 struct usb_attach_arg *uaa = aux;
1304
1305 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1306 if (pnp) {
1307 #define USB_DEVINFO 1024
1308 char *devinfo;
1309 if (!uaa->usegeneric)
1310 return (QUIET);
1311 devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK);
1312 usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO);
1313 aprint_normal("%s, %s", devinfo, pnp);
1314 free(devinfo, M_TEMP);
1315 }
1316 aprint_normal(" port %d", uaa->port);
1317 #if 0
1318 /*
1319 * It gets very crowded with these locators on the attach line.
1320 * They are not really needed since they are printed in the clear
1321 * by each driver.
1322 */
1323 if (uaa->vendor != UHUB_UNK_VENDOR)
1324 aprint_normal(" vendor 0x%04x", uaa->vendor);
1325 if (uaa->product != UHUB_UNK_PRODUCT)
1326 aprint_normal(" product 0x%04x", uaa->product);
1327 if (uaa->release != UHUB_UNK_RELEASE)
1328 aprint_normal(" release 0x%04x", uaa->release);
1329 #endif
1330 return (UNCONF);
1331 }
1332
1333 int
1334 usbd_ifprint(void *aux, const char *pnp)
1335 {
1336 struct usbif_attach_arg *uaa = aux;
1337
1338 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1339 if (pnp)
1340 return (QUIET);
1341 aprint_normal(" port %d", uaa->port);
1342 aprint_normal(" configuration %d", uaa->configno);
1343 aprint_normal(" interface %d", uaa->ifaceno);
1344 #if 0
1345 /*
1346 * It gets very crowded with these locators on the attach line.
1347 * They are not really needed since they are printed in the clear
1348 * by each driver.
1349 */
1350 if (uaa->vendor != UHUB_UNK_VENDOR)
1351 aprint_normal(" vendor 0x%04x", uaa->vendor);
1352 if (uaa->product != UHUB_UNK_PRODUCT)
1353 aprint_normal(" product 0x%04x", uaa->product);
1354 if (uaa->release != UHUB_UNK_RELEASE)
1355 aprint_normal(" release 0x%04x", uaa->release);
1356 #endif
1357 return (UNCONF);
1358 }
1359
1360 void
1361 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1362 int usedev)
1363 {
1364 struct usbd_port *p;
1365 int i, j, err, s;
1366
1367 di->udi_bus = device_unit(dev->bus->usbctl);
1368 di->udi_addr = dev->address;
1369 di->udi_cookie = dev->cookie;
1370 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1371 di->udi_product, sizeof(di->udi_product), usedev, 1);
1372 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1373 UGETW(dev->ddesc.bcdDevice));
1374 di->udi_serial[0] = 0;
1375 if (usedev)
1376 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
1377 di->udi_serial);
1378 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1379 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1380 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1381 di->udi_class = dev->ddesc.bDeviceClass;
1382 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1383 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1384 di->udi_config = dev->config;
1385 di->udi_power = dev->self_powered ? 0 : dev->power;
1386 di->udi_speed = dev->speed;
1387
1388 if (dev->subdevlen > 0) {
1389 for (i = 0, j = 0; i < dev->subdevlen &&
1390 j < USB_MAX_DEVNAMES; i++) {
1391 if (!dev->subdevs[i])
1392 continue;
1393 strncpy(di->udi_devnames[j],
1394 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN);
1395 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1396 j++;
1397 }
1398 } else {
1399 j = 0;
1400 }
1401 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1402 di->udi_devnames[j][0] = 0; /* empty */
1403
1404 if (dev->hub) {
1405 for (i = 0;
1406 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1407 i < dev->hub->hubdesc.bNbrPorts;
1408 i++) {
1409 p = &dev->hub->ports[i];
1410 if (p->device)
1411 err = p->device->address;
1412 else {
1413 s = UGETW(p->status.wPortStatus);
1414 if (s & UPS_PORT_ENABLED)
1415 err = USB_PORT_ENABLED;
1416 else if (s & UPS_SUSPEND)
1417 err = USB_PORT_SUSPENDED;
1418 else if (s & UPS_PORT_POWER)
1419 err = USB_PORT_POWERED;
1420 else
1421 err = USB_PORT_DISABLED;
1422 }
1423 di->udi_ports[i] = err;
1424 }
1425 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1426 } else
1427 di->udi_nports = 0;
1428 }
1429
1430 #ifdef COMPAT_30
1431 void
1432 usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di,
1433 int usedev)
1434 {
1435 struct usbd_port *p;
1436 int i, j, err, s;
1437
1438 di->udi_bus = device_unit(dev->bus->usbctl);
1439 di->udi_addr = dev->address;
1440 di->udi_cookie = dev->cookie;
1441 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1442 di->udi_product, sizeof(di->udi_product), usedev, 0);
1443 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1444 UGETW(dev->ddesc.bcdDevice));
1445 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1446 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1447 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1448 di->udi_class = dev->ddesc.bDeviceClass;
1449 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1450 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1451 di->udi_config = dev->config;
1452 di->udi_power = dev->self_powered ? 0 : dev->power;
1453 di->udi_speed = dev->speed;
1454
1455 if (dev->subdevlen > 0) {
1456 for (i = 0, j = 0; i < dev->subdevlen &&
1457 j < USB_MAX_DEVNAMES; i++) {
1458 if (!dev->subdevs[i])
1459 continue;
1460 strncpy(di->udi_devnames[j],
1461 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN);
1462 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1463 j++;
1464 }
1465 } else {
1466 j = 0;
1467 }
1468 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1469 di->udi_devnames[j][0] = 0; /* empty */
1470
1471 if (dev->hub) {
1472 for (i = 0;
1473 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1474 i < dev->hub->hubdesc.bNbrPorts;
1475 i++) {
1476 p = &dev->hub->ports[i];
1477 if (p->device)
1478 err = p->device->address;
1479 else {
1480 s = UGETW(p->status.wPortStatus);
1481 if (s & UPS_PORT_ENABLED)
1482 err = USB_PORT_ENABLED;
1483 else if (s & UPS_SUSPEND)
1484 err = USB_PORT_SUSPENDED;
1485 else if (s & UPS_PORT_POWER)
1486 err = USB_PORT_POWERED;
1487 else
1488 err = USB_PORT_DISABLED;
1489 }
1490 di->udi_ports[i] = err;
1491 }
1492 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1493 } else
1494 di->udi_nports = 0;
1495 }
1496 #endif
1497
1498
1499 void
1500 usb_free_device(usbd_device_handle dev)
1501 {
1502 int ifcidx, nifc;
1503
1504 if (dev->default_pipe != NULL)
1505 usbd_kill_pipe(dev->default_pipe);
1506 if (dev->ifaces != NULL) {
1507 nifc = dev->cdesc->bNumInterface;
1508 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1509 usbd_free_iface_data(dev, ifcidx);
1510 free(dev->ifaces, M_USB);
1511 }
1512 if (dev->cdesc != NULL)
1513 free(dev->cdesc, M_USB);
1514 if (dev->subdevlen > 0) {
1515 free(dev->subdevs, M_USB);
1516 dev->subdevlen = 0;
1517 }
1518 free(dev, M_USB);
1519 }
1520
1521 /*
1522 * The general mechanism for detaching drivers works as follows: Each
1523 * driver is responsible for maintaining a reference count on the
1524 * number of outstanding references to its softc (e.g. from
1525 * processing hanging in a read or write). The detach method of the
1526 * driver decrements this counter and flags in the softc that the
1527 * driver is dying and then wakes any sleepers. It then sleeps on the
1528 * softc. Each place that can sleep must maintain the reference
1529 * count. When the reference count drops to -1 (0 is the normal value
1530 * of the reference count) the a wakeup on the softc is performed
1531 * signaling to the detach waiter that all references are gone.
1532 */
1533
1534 /*
1535 * Called from process context when we discover that a port has
1536 * been disconnected.
1537 */
1538 int
1539 usb_disconnect_port(struct usbd_port *up, device_t parent, int flags)
1540 {
1541 usbd_device_handle dev = up->device;
1542 device_t subdev;
1543 char subdevname[16];
1544 const char *hubname = device_xname(parent);
1545 int i, rc;
1546
1547 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1548 up, dev, up->portno));
1549
1550 if (dev == NULL) {
1551 #ifdef DIAGNOSTIC
1552 printf("usb_disconnect_port: no device\n");
1553 #endif
1554 return 0;
1555 }
1556
1557 if (dev->subdevlen > 0) {
1558 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1559 for (i = 0; i < dev->subdevlen; i++) {
1560 if ((subdev = dev->subdevs[i]) == NULL)
1561 continue;
1562 strlcpy(subdevname, device_xname(subdev),
1563 sizeof(subdevname));
1564 if ((rc = config_detach(subdev, flags)) != 0)
1565 return rc;
1566 printf("%s: at %s", subdevname, hubname);
1567 if (up->portno != 0)
1568 printf(" port %d", up->portno);
1569 printf(" (addr %d) disconnected\n", dev->address);
1570 }
1571 KASSERT(!dev->nifaces_claimed);
1572 }
1573
1574 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1575 dev->bus->devices[dev->address] = NULL;
1576 up->device = NULL;
1577 usb_free_device(dev);
1578 return 0;
1579 }
1580