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