usb_subr.c revision 1.198.2.20 1 /* $NetBSD: usb_subr.c,v 1.198.2.20 2015/12/19 09:20:35 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.20 2015/12/19 09:20:35 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(struct usbd_device *, int);
70 Static void usbd_devinfo(struct usbd_device *, int, char *, size_t);
71 Static void usbd_devinfo_vp(struct usbd_device *, char *, size_t, char *, size_t,
72 int, int);
73 Static int usbd_getnewaddr(struct usbd_bus *);
74 Static int usbd_print(void *, const char *);
75 Static int usbd_ifprint(void *, const char *);
76 Static void usbd_free_iface_data(struct usbd_device *, 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(struct usbd_device *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(struct usbd_device *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(struct usbd_device *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(struct usbd_device *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(struct usbd_bus *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(struct usbd_bus *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(struct usbd_device *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(struct usbd_device *dev, u_int ms)
277 {
278 usb_delay_ms_locked(dev->ud_bus, ms, NULL);
279 }
280
281 usbd_status
282 usbd_reset_port(struct usbd_device *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(struct usbd_device *dev, int ifaceidx, int altidx)
393 {
394 struct usbd_interface *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", 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(struct usbd_device *dev, int ifcno)
482 {
483 struct usbd_interface *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(struct usbd_device *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(struct usbd_device *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(struct usbd_device *dev, int index, int msg)
530 {
531 usb_config_descriptor_t cd, *cdp;
532 usb_bos_descriptor_t bd, *bdp = NULL;
533 usbd_status err;
534 int i, ifcidx, nifc, len, selfpowered, power;
535
536 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
537
538 DPRINTFN(5, "dev=%p index=%d", dev, index, 0, 0);
539
540 if (index >= dev->ud_ddesc.bNumConfigurations &&
541 index != USB_UNCONFIG_INDEX) {
542 /* panic? */
543 printf("usbd_set_config_index: illegal index\n");
544 return USBD_INVAL;
545 }
546
547 /* XXX check that all interfaces are idle */
548 if (dev->ud_config != USB_UNCONFIG_NO) {
549 DPRINTF("free old config", 0, 0, 0, 0);
550 /* Free all configuration data structures. */
551 nifc = dev->ud_cdesc->bNumInterface;
552 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
553 usbd_free_iface_data(dev, ifcidx);
554 kmem_free(dev->ud_ifaces, nifc * sizeof(struct usbd_interface));
555 kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength));
556 if (dev->ud_bdesc != NULL)
557 kmem_free(dev->ud_bdesc,
558 UGETW(dev->ud_bdesc->wTotalLength));
559 dev->ud_ifaces = NULL;
560 dev->ud_cdesc = NULL;
561 dev->ud_bdesc = NULL;
562 dev->ud_config = USB_UNCONFIG_NO;
563 }
564
565 if (index == USB_UNCONFIG_INDEX) {
566 /* We are unconfiguring the device, so leave unallocated. */
567 DPRINTF("set config 0", 0, 0, 0, 0);
568 err = usbd_set_config(dev, USB_UNCONFIG_NO);
569 if (err) {
570 DPRINTF("setting config=0 failed, err = %d", err,
571 0, 0, 0);
572 }
573 return err;
574 }
575
576 /* Get the short descriptor. */
577 err = usbd_get_config_desc(dev, index, &cd);
578 if (err) {
579 DPRINTF("get_config_desc=%d", err, 0, 0, 0);
580 return err;
581 }
582 len = UGETW(cd.wTotalLength);
583 cdp = kmem_alloc(len, KM_SLEEP);
584 if (cdp == NULL)
585 return USBD_NOMEM;
586
587 /* Get the full descriptor. Try a few times for slow devices. */
588 for (i = 0; i < 3; i++) {
589 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
590 if (!err)
591 break;
592 usbd_delay_ms(dev, 200);
593 }
594 if (err) {
595 DPRINTF("get_desc=%d", err, 0, 0, 0);
596 goto bad;
597 }
598 if (cdp->bDescriptorType != UDESC_CONFIG) {
599 DPRINTF("bad desc %d", cdp->bDescriptorType, 0, 0, 0);
600 err = USBD_INVAL;
601 goto bad;
602 }
603
604 if (USB_IS_SS(dev->ud_speed)) {
605 /* get short bos desc */
606 err = usbd_get_bos_desc(dev, index, &bd);
607 if (!err) {
608 int blen = UGETW(bd.wTotalLength);
609 bdp = kmem_alloc(blen, KM_SLEEP);
610 if (bdp == NULL) {
611 err = USBD_NOMEM;
612 goto bad;
613 }
614
615 /* Get the full desc */
616 for (i = 0; i < 3; i++) {
617 err = usbd_get_desc(dev, UDESC_BOS, index, blen,
618 bdp);
619 if (!err)
620 break;
621 usbd_delay_ms(dev, 200);
622 }
623 if (err || bdp->bDescriptorType != UDESC_BOS) {
624 DPRINTF("error %d or bad desc %d", err,
625 bdp->bDescriptorType, 0, 0);
626 kmem_free(bdp, blen);
627 bdp = NULL;
628 }
629 }
630 }
631 dev->ud_bdesc = bdp;
632
633 /*
634 * Figure out if the device is self or bus powered.
635 */
636 #if 0 /* XXX various devices don't report the power state correctly */
637 selfpowered = 0;
638 err = usbd_get_device_status(dev, &ds);
639 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
640 selfpowered = 1;
641 #endif
642 /*
643 * Use the power state in the configuration we are going
644 * to set. This doesn't necessarily reflect the actual
645 * power state of the device; the driver can control this
646 * by choosing the appropriate configuration.
647 */
648 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
649
650 DPRINTF("addr %d cno=%d attr=0x%02x, selfpowered=%d",
651 dev->ud_addr, cdp->bConfigurationValue, cdp->bmAttributes,
652 selfpowered);
653 DPRINTF("max power=%d", cdp->bMaxPower * 2, 0, 0, 0);
654
655 /* Check if we have enough power. */
656 #if 0 /* this is a no-op, see above */
657 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
658 if (msg)
659 printf("%s: device addr %d (config %d): "
660 "can't set self powered configuration\n",
661 device_xname(dev->ud_bus->bdev), dev->ud_addr,
662 cdp->bConfigurationValue);
663 err = USBD_NO_POWER;
664 goto bad;
665 }
666 #endif
667 #ifdef USB_DEBUG
668 if (dev->ud_powersrc == NULL) {
669 DPRINTF("No power source?", 0, 0, 0, 0);
670 err = USBD_IOERROR;
671 goto bad;
672 }
673 #endif
674 power = cdp->bMaxPower * 2;
675 if (power > dev->ud_powersrc->up_power) {
676 DPRINTF("power exceeded %d %d", power, dev->ud_powersrc->up_power,
677 0, 0);
678 /* XXX print nicer message. */
679 if (msg)
680 printf("%s: device addr %d (config %d) exceeds power "
681 "budget, %d mA > %d mA\n",
682 device_xname(dev->ud_bus->ub_usbctl), dev->ud_addr,
683 cdp->bConfigurationValue,
684 power, dev->ud_powersrc->up_power);
685 err = USBD_NO_POWER;
686 goto bad;
687 }
688 dev->ud_power = power;
689 dev->ud_selfpowered = selfpowered;
690
691 /* Set the actual configuration value. */
692 DPRINTF("set config %d", cdp->bConfigurationValue, 0, 0, 0);
693 err = usbd_set_config(dev, cdp->bConfigurationValue);
694 if (err) {
695 DPRINTF("setting config=%d failed, error=%d",
696 cdp->bConfigurationValue, err, 0, 0);
697 goto bad;
698 }
699
700 /* Allocate and fill interface data. */
701 nifc = cdp->bNumInterface;
702 dev->ud_ifaces = kmem_alloc(nifc * sizeof(struct usbd_interface),
703 KM_SLEEP);
704 if (dev->ud_ifaces == NULL) {
705 err = USBD_NOMEM;
706 goto bad;
707 }
708 DPRINTFN(5, "dev=%p cdesc=%p", dev, cdp, 0, 0);
709 dev->ud_cdesc = cdp;
710 dev->ud_config = cdp->bConfigurationValue;
711 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
712 err = usbd_fill_iface_data(dev, ifcidx, 0);
713 if (err) {
714 while (--ifcidx >= 0)
715 usbd_free_iface_data(dev, ifcidx);
716 goto bad;
717 }
718 }
719
720 return USBD_NORMAL_COMPLETION;
721
722 bad:
723 kmem_free(cdp, len);
724 if (bdp != NULL) {
725 kmem_free(bdp, UGETW(bdp->wTotalLength));
726 dev->ud_bdesc = NULL;
727 }
728 return err;
729 }
730
731 /* XXX add function for alternate settings */
732
733 usbd_status
734 usbd_setup_pipe(struct usbd_device *dev, struct usbd_interface *iface,
735 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe)
736 {
737 return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0);
738 }
739
740 usbd_status
741 usbd_setup_pipe_flags(struct usbd_device *dev, struct usbd_interface *iface,
742 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe, uint8_t flags)
743 {
744 struct usbd_pipe *p;
745 usbd_status err;
746
747 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
748
749 p = kmem_alloc(dev->ud_bus->ub_pipesize, KM_SLEEP);
750 DPRINTFN(1, "dev=%p addr=%d iface=%p ep=%p pipe=%p", dev, dev->ud_addr, iface, ep);
751 if (p == NULL) {
752 DPRINTFN(1, "(nomem)", 0, 0, 0, 0);
753 return USBD_NOMEM;
754 }
755 p->up_dev = dev;
756 p->up_iface = iface;
757 p->up_endpoint = ep;
758 ep->ue_refcnt++;
759 p->up_refcnt = 1;
760 p->up_intrxfer = NULL;
761 p->up_running = 0;
762 p->up_aborting = 0;
763 p->up_repeat = 0;
764 p->up_interval = ival;
765 p->up_flags = flags;
766 SIMPLEQ_INIT(&p->up_queue);
767 err = dev->ud_bus->ub_methods->ubm_open(p);
768 if (err) {
769 DPRINTF("endpoint=0x%x failed, error=%d",
770 ep->ue_edesc->bEndpointAddress, err, 0, 0);
771 kmem_intr_free(p, dev->ud_bus->ub_pipesize);
772 return err;
773 }
774 usb_init_task(&p->up_async_task, usbd_clear_endpoint_stall_task, p,
775 USB_TASKQ_MPSAFE);
776 DPRINTFN(1, "pipe=%p", p, 0, 0, 0);
777 *pipe = p;
778 return USBD_NORMAL_COMPLETION;
779 }
780
781 /* Abort the device control pipe. */
782 void
783 usbd_kill_pipe(struct usbd_pipe *pipe)
784 {
785 usbd_abort_pipe(pipe);
786 usbd_lock_pipe(pipe);
787 pipe->up_methods->upm_close(pipe);
788 usbd_unlock_pipe(pipe);
789 usb_rem_task(pipe->up_dev, &pipe->up_async_task);
790 pipe->up_endpoint->ue_refcnt--;
791 kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
792 }
793
794 int
795 usbd_getnewaddr(struct usbd_bus *bus)
796 {
797 int addr;
798
799 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
800 if (bus->ub_devices[addr] == NULL)
801 return addr;
802 return -1;
803 }
804
805 usbd_status
806 usbd_attach_roothub(device_t parent, struct usbd_device *dev)
807 {
808 struct usb_attach_arg uaa;
809 usb_device_descriptor_t *dd = &dev->ud_ddesc;
810 device_t dv;
811
812 uaa.uaa_device = dev;
813 uaa.uaa_usegeneric = 0;
814 uaa.uaa_port = 0;
815 uaa.uaa_vendor = UGETW(dd->idVendor);
816 uaa.uaa_product = UGETW(dd->idProduct);
817 uaa.uaa_release = UGETW(dd->bcdDevice);
818 uaa.uaa_class = dd->bDeviceClass;
819 uaa.uaa_subclass = dd->bDeviceSubClass;
820 uaa.uaa_proto = dd->bDeviceProtocol;
821
822 dv = config_found_ia(parent, "usbroothubif", &uaa, 0);
823 if (dv) {
824 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP);
825 if (dev->ud_subdevs == NULL)
826 return USBD_NOMEM;
827 dev->ud_subdevs[0] = dv;
828 dev->ud_subdevlen = 1;
829 }
830 return USBD_NORMAL_COMPLETION;
831 }
832
833 static usbd_status
834 usbd_attachwholedevice(device_t parent, struct usbd_device *dev, int port,
835 int usegeneric)
836 {
837 struct usb_attach_arg uaa;
838 usb_device_descriptor_t *dd = &dev->ud_ddesc;
839 device_t dv;
840 int dlocs[USBDEVIFCF_NLOCS];
841
842 uaa.uaa_device = dev;
843 uaa.uaa_usegeneric = usegeneric;
844 uaa.uaa_port = port;
845 uaa.uaa_vendor = UGETW(dd->idVendor);
846 uaa.uaa_product = UGETW(dd->idProduct);
847 uaa.uaa_release = UGETW(dd->bcdDevice);
848 uaa.uaa_class = dd->bDeviceClass;
849 uaa.uaa_subclass = dd->bDeviceSubClass;
850 uaa.uaa_proto = dd->bDeviceProtocol;
851
852 dlocs[USBDEVIFCF_PORT] = uaa.uaa_port;
853 dlocs[USBDEVIFCF_VENDOR] = uaa.uaa_vendor;
854 dlocs[USBDEVIFCF_PRODUCT] = uaa.uaa_product;
855 dlocs[USBDEVIFCF_RELEASE] = uaa.uaa_release;
856 /* the rest is historical ballast */
857 dlocs[USBDEVIFCF_CONFIGURATION] = -1;
858 dlocs[USBDEVIFCF_INTERFACE] = -1;
859
860 dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print,
861 config_stdsubmatch);
862 if (dv) {
863 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP);
864 if (dev->ud_subdevs == NULL)
865 return USBD_NOMEM;
866 dev->ud_subdevs[0] = dv;
867 dev->ud_subdevlen = 1;
868 dev->ud_nifaces_claimed = 1; /* XXX */
869 }
870 return USBD_NORMAL_COMPLETION;
871 }
872
873 static usbd_status
874 usbd_attachinterfaces(device_t parent, struct usbd_device *dev,
875 int port, const int *locators)
876 {
877 struct usbif_attach_arg uiaa;
878 int ilocs[USBIFIFCF_NLOCS];
879 usb_device_descriptor_t *dd = &dev->ud_ddesc;
880 int nifaces;
881 struct usbd_interface **ifaces;
882 int i, j, loc;
883 device_t dv;
884
885 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
886
887 nifaces = dev->ud_cdesc->bNumInterface;
888 ifaces = kmem_zalloc(nifaces * sizeof(*ifaces), KM_SLEEP);
889 if (!ifaces)
890 return USBD_NOMEM;
891 for (i = 0; i < nifaces; i++) {
892 if (!dev->ud_subdevs[i]) {
893 ifaces[i] = &dev->ud_ifaces[i];
894 }
895 DPRINTF("interface %d %p\n", i, ifaces[i], 0, 0);
896 }
897
898
899 uiaa.uiaa_device = dev;
900 uiaa.uiaa_port = port;
901 uiaa.uiaa_vendor = UGETW(dd->idVendor);
902 uiaa.uiaa_product = UGETW(dd->idProduct);
903 uiaa.uiaa_release = UGETW(dd->bcdDevice);
904 uiaa.uiaa_configno = dev->ud_cdesc->bConfigurationValue;
905 uiaa.uiaa_ifaces = ifaces;
906 uiaa.uiaa_nifaces = nifaces;
907 ilocs[USBIFIFCF_PORT] = uiaa.uiaa_port;
908 ilocs[USBIFIFCF_VENDOR] = uiaa.uiaa_vendor;
909 ilocs[USBIFIFCF_PRODUCT] = uiaa.uiaa_product;
910 ilocs[USBIFIFCF_RELEASE] = uiaa.uiaa_release;
911 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.uiaa_configno;
912
913 for (i = 0; i < nifaces; i++) {
914 if (!ifaces[i]) {
915 DPRINTF("interface %d claimed", i, 0, 0, 0);
916 continue; /* interface already claimed */
917 }
918 uiaa.uiaa_iface = ifaces[i];
919 uiaa.uiaa_class = ifaces[i]->ui_idesc->bInterfaceClass;
920 uiaa.uiaa_subclass = ifaces[i]->ui_idesc->bInterfaceSubClass;
921 uiaa.uiaa_proto = ifaces[i]->ui_idesc->bInterfaceProtocol;
922 uiaa.uiaa_ifaceno = ifaces[i]->ui_idesc->bInterfaceNumber;
923
924 DPRINTF("searching for interface %d...", i, 0, 0, 0);
925 DPRINTF("class %x subclass %x proto %x ifaceno %d\n",
926 uiaa.uiaa_class, uiaa.uiaa_subclass, uiaa.uiaa_proto,
927 uiaa.uiaa_ifaceno);
928 ilocs[USBIFIFCF_INTERFACE] = uiaa.uiaa_ifaceno;
929 if (locators != NULL) {
930 loc = locators[USBIFIFCF_CONFIGURATION];
931 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT &&
932 loc != uiaa.uiaa_configno)
933 continue;
934 loc = locators[USBIFIFCF_INTERFACE];
935 if (loc != USBIFIFCF_INTERFACE_DEFAULT &&
936 loc != uiaa.uiaa_ifaceno)
937 continue;
938 }
939 dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa,
940 usbd_ifprint, config_stdsubmatch);
941 if (!dv)
942 continue;
943 /* claim */
944 ifaces[i] = NULL;
945 /* account for ifaces claimed by the driver behind our back */
946 for (j = 0; j < nifaces; j++) {
947
948 if (!ifaces[j] && !dev->ud_subdevs[j]) {
949 DPRINTF("interface %d claimed behind our back",
950 j, 0, 0, 0);
951 dev->ud_subdevs[j] = dv;
952 dev->ud_nifaces_claimed++;
953 }
954 }
955 }
956
957 kmem_free(ifaces, nifaces * sizeof(*ifaces));
958 return USBD_NORMAL_COMPLETION;
959 }
960
961 usbd_status
962 usbd_probe_and_attach(device_t parent, struct usbd_device *dev,
963 int port, int addr)
964 {
965 usb_device_descriptor_t *dd = &dev->ud_ddesc;
966 int confi, nifaces;
967 usbd_status err;
968
969 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
970
971 /* First try with device specific drivers. */
972 DPRINTF("trying device specific drivers", 0, 0, 0, 0);
973 err = usbd_attachwholedevice(parent, dev, port, 0);
974 if (dev->ud_nifaces_claimed || err)
975 return err;
976 DPRINTF("no device specific driver found", 0, 0, 0, 0);
977
978 DPRINTF("looping over %d configurations", dd->bNumConfigurations,
979 0, 0, 0);
980 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
981 DPRINTFN(1, "trying config idx=%d", confi, 0, 0, 0);
982 err = usbd_set_config_index(dev, confi, 1);
983 if (err) {
984 DPRINTF("port %d, set config at addr %d failed, "
985 "error=%d", port, addr, err, 0);
986 printf("%s: port %d, set config at addr %d failed\n",
987 device_xname(parent), port, addr);
988 return err;
989 }
990 nifaces = dev->ud_cdesc->bNumInterface;
991 dev->ud_subdevs = kmem_zalloc(nifaces * sizeof(device_t),
992 KM_SLEEP);
993 if (dev->ud_subdevs == NULL)
994 return USBD_NOMEM;
995 dev->ud_subdevlen = nifaces;
996
997 err = usbd_attachinterfaces(parent, dev, port, NULL);
998
999 if (!dev->ud_nifaces_claimed) {
1000 kmem_free(dev->ud_subdevs,
1001 dev->ud_subdevlen * sizeof(device_t));
1002 dev->ud_subdevs = 0;
1003 dev->ud_subdevlen = 0;
1004 }
1005 if (dev->ud_nifaces_claimed || err)
1006 return err;
1007 }
1008 /* No interfaces were attached in any of the configurations. */
1009
1010 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
1011 usbd_set_config_index(dev, 0, 0);
1012
1013 DPRINTF("no interface drivers found", 0, 0, 0, 0);
1014
1015 /* Finally try the generic driver. */
1016 err = usbd_attachwholedevice(parent, dev, port, 1);
1017
1018 /*
1019 * The generic attach failed, but leave the device as it is.
1020 * We just did not find any drivers, that's all. The device is
1021 * fully operational and not harming anyone.
1022 */
1023 DPRINTF("generic attach failed", 0, 0, 0, 0);
1024
1025 return USBD_NORMAL_COMPLETION;
1026 }
1027
1028 /**
1029 * Called from uhub_rescan(). usbd_new_device() for the target dev must be
1030 * called before calling this.
1031 */
1032 usbd_status
1033 usbd_reattach_device(device_t parent, struct usbd_device *dev,
1034 int port, const int *locators)
1035 {
1036 int i, loc;
1037
1038 if (locators != NULL) {
1039 loc = locators[USBIFIFCF_PORT];
1040 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port)
1041 return USBD_NORMAL_COMPLETION;
1042 loc = locators[USBIFIFCF_VENDOR];
1043 if (loc != USBIFIFCF_VENDOR_DEFAULT &&
1044 loc != UGETW(dev->ud_ddesc.idVendor))
1045 return USBD_NORMAL_COMPLETION;
1046 loc = locators[USBIFIFCF_PRODUCT];
1047 if (loc != USBIFIFCF_PRODUCT_DEFAULT &&
1048 loc != UGETW(dev->ud_ddesc.idProduct))
1049 return USBD_NORMAL_COMPLETION;
1050 loc = locators[USBIFIFCF_RELEASE];
1051 if (loc != USBIFIFCF_RELEASE_DEFAULT &&
1052 loc != UGETW(dev->ud_ddesc.bcdDevice))
1053 return USBD_NORMAL_COMPLETION;
1054 }
1055 if (dev->ud_subdevlen == 0) {
1056 /* XXX: check USBIFIFCF_CONFIGURATION and
1057 * USBIFIFCF_INTERFACE too */
1058 return usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
1059 } else if (dev->ud_subdevlen != dev->ud_cdesc->bNumInterface) {
1060 /* device-specific or generic driver is already attached. */
1061 return USBD_NORMAL_COMPLETION;
1062 }
1063 /* Does the device have unconfigured interfaces? */
1064 for (i = 0; i < dev->ud_subdevlen; i++) {
1065 if (dev->ud_subdevs[i] == NULL) {
1066 break;
1067 }
1068 }
1069 if (i >= dev->ud_subdevlen)
1070 return USBD_NORMAL_COMPLETION;
1071 return usbd_attachinterfaces(parent, dev, port, locators);
1072 }
1073
1074 /*
1075 * Get the first 8 bytes of the device descriptor.
1076 * Do as Windows does: try to read 64 bytes -- there are devices which
1077 * recognize the initial descriptor fetch (before the control endpoint's
1078 * MaxPacketSize is known by the host) by exactly this length.
1079 */
1080 usbd_status
1081 usbd_get_initial_ddesc(struct usbd_device *dev, usb_device_descriptor_t *desc)
1082 {
1083 usb_device_request_t req;
1084 char buf[64];
1085 int res, actlen;
1086
1087 req.bmRequestType = UT_READ_DEVICE;
1088 req.bRequest = UR_GET_DESCRIPTOR;
1089 USETW2(req.wValue, UDESC_DEVICE, 0);
1090 USETW(req.wIndex, 0);
1091 USETW(req.wLength, 64);
1092 res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK,
1093 &actlen, USBD_DEFAULT_TIMEOUT);
1094 if (res)
1095 return res;
1096 if (actlen < 8)
1097 return USBD_SHORT_XFER;
1098 memcpy(desc, buf, 8);
1099 return USBD_NORMAL_COMPLETION;
1100 }
1101
1102 /*
1103 * Called when a new device has been put in the powered state,
1104 * but not yet in the addressed state.
1105 * Get initial descriptor, set the address, get full descriptor,
1106 * and attach a driver.
1107 */
1108 usbd_status
1109 usbd_new_device(device_t parent, struct usbd_bus* bus, int depth,
1110 int speed, int port, struct usbd_port *up)
1111 {
1112 struct usbd_device *dev, *adev;
1113 struct usbd_device *hub;
1114 usb_device_descriptor_t *dd;
1115 usb_port_status_t ps;
1116 usbd_status err;
1117 int addr;
1118 int i;
1119 int p;
1120
1121 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1122
1123 DPRINTF("bus=%p port=%d depth=%d speed=%d", bus, port, depth, speed);
1124
1125 if (bus->ub_methods->ubm_newdev != NULL)
1126 return (bus->ub_methods->ubm_newdev)(parent, bus, depth, speed,
1127 port, up);
1128
1129 addr = usbd_getnewaddr(bus);
1130 if (addr < 0) {
1131 printf("%s: No free USB addresses, new device ignored.\n",
1132 device_xname(bus->ub_usbctl));
1133 return USBD_NO_ADDR;
1134 }
1135
1136 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
1137 if (dev == NULL)
1138 return USBD_NOMEM;
1139
1140 dev->ud_bus = bus;
1141
1142 /* Set up default endpoint handle. */
1143 dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
1144
1145 /* Set up default endpoint descriptor. */
1146 dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1147 dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
1148 dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1149 dev->ud_ep0desc.bmAttributes = UE_CONTROL;
1150 /*
1151 * temporary, will be fixed after first descriptor fetch
1152 * (which uses 64 bytes so it shouldn't be less),
1153 * highspeed devices must support 64 byte packets anyway
1154 */
1155 if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL)
1156 USETW(dev->ud_ep0desc.wMaxPacketSize, 64);
1157 else
1158 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
1159
1160 dev->ud_ep0desc.bInterval = 0;
1161
1162 /* doesn't matter, just don't let it uninitialized */
1163 dev->ud_ep0.ue_toggle = 0;
1164
1165 dev->ud_quirks = &usbd_no_quirk;
1166 dev->ud_addr = USB_START_ADDR;
1167 dev->ud_ddesc.bMaxPacketSize = 0;
1168 dev->ud_depth = depth;
1169 dev->ud_powersrc = up;
1170 dev->ud_myhub = up->up_parent;
1171
1172 up->up_dev = dev;
1173
1174 /* Locate port on upstream high speed hub */
1175 for (adev = dev, hub = up->up_parent;
1176 hub != NULL && hub->ud_speed != USB_SPEED_HIGH;
1177 adev = hub, hub = hub->ud_myhub)
1178 ;
1179 if (hub) {
1180 for (p = 0; p < hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
1181 if (hub->ud_hub->uh_ports[p].up_dev == adev) {
1182 dev->ud_myhsport = &hub->ud_hub->uh_ports[p];
1183 goto found;
1184 }
1185 }
1186 panic("usbd_new_device: cannot find HS port");
1187 found:
1188 DPRINTFN(1, "high speed port %d", p, 0, 0, 0);
1189 } else {
1190 dev->ud_myhsport = NULL;
1191 }
1192 dev->ud_speed = speed;
1193 dev->ud_langid = USBD_NOLANG;
1194 dev->ud_cookie.cookie = ++usb_cookie_no;
1195
1196 /* Establish the default pipe. */
1197 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1198 &dev->ud_pipe0, USBD_MPSAFE);
1199 if (err) {
1200 usbd_remove_device(dev, up);
1201 return err;
1202 }
1203
1204 dd = &dev->ud_ddesc;
1205 /* Try a few times in case the device is slow (i.e. outside specs.) */
1206 for (i = 0; i < 10; i++) {
1207 /* Get the first 8 bytes of the device descriptor. */
1208 err = usbd_get_initial_ddesc(dev, dd);
1209 if (!err)
1210 break;
1211 usbd_delay_ms(dev, 200);
1212 if ((i & 3) == 3)
1213 usbd_reset_port(up->up_parent, port, &ps);
1214 }
1215 if (err) {
1216 DPRINTF("addr=%d, getting first desc failed: %d", addr, err,
1217 0, 0);
1218 usbd_remove_device(dev, up);
1219 return err;
1220 }
1221
1222 /* Windows resets the port here, do likewise */
1223 if (up->up_parent)
1224 usbd_reset_port(up->up_parent, port, &ps);
1225
1226 if (speed == USB_SPEED_HIGH) {
1227 /* Max packet size must be 64 (sec 5.5.3). */
1228 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1229 #ifdef DIAGNOSTIC
1230 printf("usbd_new_device: addr=%d bad max packet "
1231 "size=%d. adjusting to %d.\n",
1232 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET);
1233 #endif
1234 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1235 }
1236 }
1237
1238 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, subclass=%d", addr,
1239 UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass);
1240 DPRINTF("protocol=%d, maxpacket=%d, len=%d, speed=%d",
1241 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, dev->ud_speed);
1242
1243 if (dd->bDescriptorType != UDESC_DEVICE) {
1244 /* Illegal device descriptor */
1245 DPRINTF("illegal descriptor %d", dd->bDescriptorType, 0, 0, 0);
1246 usbd_remove_device(dev, up);
1247 return USBD_INVAL;
1248 }
1249
1250 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1251 DPRINTF("bad length %d", dd->bLength, 0, 0, 0);
1252 usbd_remove_device(dev, up);
1253 return USBD_INVAL;
1254 }
1255
1256 USETW(dev->ud_ep0desc.wMaxPacketSize, dd->bMaxPacketSize);
1257
1258 /* Re-establish the default pipe with the new MPS. */
1259 usbd_kill_pipe(dev->ud_pipe0);
1260 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1261 &dev->ud_pipe0, USBD_MPSAFE);
1262 if (err) {
1263 DPRINTF("setup default pipe failed err %d", err, 0, 0, 0);
1264 usbd_remove_device(dev, up);
1265 return err;
1266 }
1267
1268 /* Set the address */
1269 DPRINTFN(5, "setting device address=%d", addr, 0, 0, 0);
1270 err = usbd_set_address(dev, addr);
1271 if (err) {
1272 DPRINTF("set address %d failed, err = %d", addr, err, 0, 0);
1273 err = USBD_SET_ADDR_FAILED;
1274 usbd_remove_device(dev, up);
1275 return err;
1276 }
1277
1278 /* Allow device time to set new address */
1279 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1280 dev->ud_addr = addr; /* new device address now */
1281 bus->ub_devices[addr] = dev;
1282
1283 /* Re-establish the default pipe with the new address. */
1284 usbd_kill_pipe(dev->ud_pipe0);
1285 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1286 &dev->ud_pipe0, USBD_MPSAFE);
1287 if (err) {
1288 DPRINTF("setup default pipe failed, err = %d", err, 0, 0, 0);
1289 usbd_remove_device(dev, up);
1290 return err;
1291 }
1292
1293 err = usbd_reload_device_desc(dev);
1294 if (err) {
1295 DPRINTF("addr=%d, getting full desc failed, err = %d", addr,
1296 err, 0, 0);
1297 usbd_remove_device(dev, up);
1298 return err;
1299 }
1300
1301 /* Assume 100mA bus powered for now. Changed when configured. */
1302 dev->ud_power = USB_MIN_POWER;
1303 dev->ud_selfpowered = 0;
1304
1305 DPRINTF("new dev (addr %d), dev=%p, parent=%p", addr, dev, parent, 0);
1306
1307 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1308
1309 if (port == 0) { /* root hub */
1310 KASSERT(addr == 1);
1311 usbd_attach_roothub(parent, dev);
1312 return USBD_NORMAL_COMPLETION;
1313 }
1314
1315 err = usbd_probe_and_attach(parent, dev, port, addr);
1316 if (err) {
1317 usbd_remove_device(dev, up);
1318 return err;
1319 }
1320
1321 return USBD_NORMAL_COMPLETION;
1322 }
1323
1324 usbd_status
1325 usbd_reload_device_desc(struct usbd_device *dev)
1326 {
1327 usbd_status err;
1328
1329 /* Get the full device descriptor. */
1330 err = usbd_get_device_desc(dev, &dev->ud_ddesc);
1331 if (err)
1332 return err;
1333
1334 /* Figure out what's wrong with this device. */
1335 dev->ud_quirks = usbd_find_quirk(&dev->ud_ddesc);
1336
1337 return USBD_NORMAL_COMPLETION;
1338 }
1339
1340 void
1341 usbd_remove_device(struct usbd_device *dev, struct usbd_port *up)
1342 {
1343
1344 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1345
1346 DPRINTF("dev %p", dev, 0, 0, 0);
1347
1348 if (dev->ud_pipe0 != NULL)
1349 usbd_kill_pipe(dev->ud_pipe0);
1350 up->up_dev = NULL;
1351 dev->ud_bus->ub_devices[dev->ud_addr] = NULL;
1352
1353 kmem_free(dev, sizeof(*dev));
1354 }
1355
1356 int
1357 usbd_print(void *aux, const char *pnp)
1358 {
1359 struct usb_attach_arg *uaa = aux;
1360
1361 if (pnp) {
1362 #define USB_DEVINFO 1024
1363 char *devinfo;
1364 if (!uaa->uaa_usegeneric)
1365 return QUIET;
1366 devinfo = kmem_alloc(USB_DEVINFO, KM_SLEEP);
1367 usbd_devinfo(uaa->uaa_device, 1, devinfo, USB_DEVINFO);
1368 aprint_normal("%s, %s", devinfo, pnp);
1369 kmem_free(devinfo, USB_DEVINFO);
1370 }
1371 aprint_normal(" port %d", uaa->uaa_port);
1372 #if 0
1373 /*
1374 * It gets very crowded with these locators on the attach line.
1375 * They are not really needed since they are printed in the clear
1376 * by each driver.
1377 */
1378 if (uaa->uaa_vendor != UHUB_UNK_VENDOR)
1379 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor);
1380 if (uaa->uaa_product != UHUB_UNK_PRODUCT)
1381 aprint_normal(" product 0x%04x", uaa->uaa_product);
1382 if (uaa->uaa_release != UHUB_UNK_RELEASE)
1383 aprint_normal(" release 0x%04x", uaa->uaa_release);
1384 #endif
1385 return UNCONF;
1386 }
1387
1388 int
1389 usbd_ifprint(void *aux, const char *pnp)
1390 {
1391 struct usbif_attach_arg *uiaa = aux;
1392
1393 if (pnp)
1394 return QUIET;
1395 aprint_normal(" port %d", uiaa->uiaa_port);
1396 aprint_normal(" configuration %d", uiaa->uiaa_configno);
1397 aprint_normal(" interface %d", uiaa->uiaa_ifaceno);
1398 #if 0
1399 /*
1400 * It gets very crowded with these locators on the attach line.
1401 * They are not really needed since they are printed in the clear
1402 * by each driver.
1403 */
1404 if (uaa->uaa_vendor != UHUB_UNK_VENDOR)
1405 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor);
1406 if (uaa->uaa_product != UHUB_UNK_PRODUCT)
1407 aprint_normal(" product 0x%04x", uaa->uaa_product);
1408 if (uaa->uaa_release != UHUB_UNK_RELEASE)
1409 aprint_normal(" release 0x%04x", uaa->uaa_release);
1410 #endif
1411 return UNCONF;
1412 }
1413
1414 void
1415 usbd_fill_deviceinfo(struct usbd_device *dev, struct usb_device_info *di,
1416 int usedev)
1417 {
1418 struct usbd_port *p;
1419 int i, j, err, s;
1420
1421 di->udi_bus = device_unit(dev->ud_bus->ub_usbctl);
1422 di->udi_addr = dev->ud_addr;
1423 di->udi_cookie = dev->ud_cookie;
1424 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1425 di->udi_product, sizeof(di->udi_product), usedev, 1);
1426 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1427 UGETW(dev->ud_ddesc.bcdDevice));
1428 di->udi_serial[0] = 0;
1429 if (usedev)
1430 (void)usbd_get_string(dev, dev->ud_ddesc.iSerialNumber,
1431 di->udi_serial);
1432 di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor);
1433 di->udi_productNo = UGETW(dev->ud_ddesc.idProduct);
1434 di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice);
1435 di->udi_class = dev->ud_ddesc.bDeviceClass;
1436 di->udi_subclass = dev->ud_ddesc.bDeviceSubClass;
1437 di->udi_protocol = dev->ud_ddesc.bDeviceProtocol;
1438 di->udi_config = dev->ud_config;
1439 di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power;
1440 di->udi_speed = dev->ud_speed;
1441
1442 if (dev->ud_subdevlen > 0) {
1443 for (i = 0, j = 0; i < dev->ud_subdevlen &&
1444 j < USB_MAX_DEVNAMES; i++) {
1445 if (!dev->ud_subdevs[i])
1446 continue;
1447 strncpy(di->udi_devnames[j],
1448 device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN);
1449 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1450 j++;
1451 }
1452 } else {
1453 j = 0;
1454 }
1455 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1456 di->udi_devnames[j][0] = 0; /* empty */
1457
1458 if (dev->ud_hub) {
1459 for (i = 0;
1460 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1461 i < dev->ud_hub->uh_hubdesc.bNbrPorts;
1462 i++) {
1463 p = &dev->ud_hub->uh_ports[i];
1464 if (p->up_dev)
1465 err = p->up_dev->ud_addr;
1466 else {
1467 s = UGETW(p->up_status.wPortStatus);
1468 if (s & UPS_PORT_ENABLED)
1469 err = USB_PORT_ENABLED;
1470 else if (s & UPS_SUSPEND)
1471 err = USB_PORT_SUSPENDED;
1472 /*
1473 * Note: UPS_PORT_POWER_SS is available only
1474 * on 3.x, and UPS_PORT_POWER is available
1475 * only on 2.0 or 1.1.
1476 */
1477 else if (USB_IS_SS(dev->ud_speed) &&
1478 (s & UPS_PORT_POWER_SS))
1479 err = USB_PORT_POWERED;
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
1492 #ifdef COMPAT_30
1493 void
1494 usbd_fill_deviceinfo_old(struct usbd_device *dev, struct usb_device_info_old *di,
1495 int usedev)
1496 {
1497 struct usbd_port *p;
1498 int i, j, err, s;
1499
1500 di->udi_bus = device_unit(dev->ud_bus->ub_usbctl);
1501 di->udi_addr = dev->ud_addr;
1502 di->udi_cookie = dev->ud_cookie;
1503 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1504 di->udi_product, sizeof(di->udi_product), usedev, 0);
1505 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1506 UGETW(dev->ud_ddesc.bcdDevice));
1507 di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor);
1508 di->udi_productNo = UGETW(dev->ud_ddesc.idProduct);
1509 di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice);
1510 di->udi_class = dev->ud_ddesc.bDeviceClass;
1511 di->udi_subclass = dev->ud_ddesc.bDeviceSubClass;
1512 di->udi_protocol = dev->ud_ddesc.bDeviceProtocol;
1513 di->udi_config = dev->ud_config;
1514 di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power;
1515 di->udi_speed = dev->ud_speed;
1516
1517 if (dev->ud_subdevlen > 0) {
1518 for (i = 0, j = 0; i < dev->ud_subdevlen &&
1519 j < USB_MAX_DEVNAMES; i++) {
1520 if (!dev->ud_subdevs[i])
1521 continue;
1522 strncpy(di->udi_devnames[j],
1523 device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN);
1524 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1525 j++;
1526 }
1527 } else {
1528 j = 0;
1529 }
1530 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1531 di->udi_devnames[j][0] = 0; /* empty */
1532
1533 if (dev->ud_hub) {
1534 for (i = 0;
1535 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1536 i < dev->ud_hub->uh_hubdesc.bNbrPorts;
1537 i++) {
1538 p = &dev->ud_hub->uh_ports[i];
1539 if (p->up_dev)
1540 err = p->up_dev->ud_addr;
1541 else {
1542 s = UGETW(p->up_status.wPortStatus);
1543 if (s & UPS_PORT_ENABLED)
1544 err = USB_PORT_ENABLED;
1545 else if (s & UPS_SUSPEND)
1546 err = USB_PORT_SUSPENDED;
1547 else if (s & UPS_PORT_POWER)
1548 err = USB_PORT_POWERED;
1549 else
1550 err = USB_PORT_DISABLED;
1551 }
1552 di->udi_ports[i] = err;
1553 }
1554 di->udi_nports = dev->ud_hub->uh_hubdesc.bNbrPorts;
1555 } else
1556 di->udi_nports = 0;
1557 }
1558 #endif
1559
1560
1561 void
1562 usb_free_device(struct usbd_device *dev)
1563 {
1564 int ifcidx, nifc;
1565
1566 if (dev->ud_pipe0 != NULL)
1567 usbd_kill_pipe(dev->ud_pipe0);
1568 if (dev->ud_ifaces != NULL) {
1569 nifc = dev->ud_cdesc->bNumInterface;
1570 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1571 usbd_free_iface_data(dev, ifcidx);
1572 kmem_free(dev->ud_ifaces,
1573 nifc * sizeof(struct usbd_interface));
1574 }
1575 if (dev->ud_cdesc != NULL)
1576 kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength));
1577 if (dev->ud_bdesc != NULL)
1578 kmem_free(dev->ud_bdesc, UGETW(dev->ud_bdesc->wTotalLength));
1579 if (dev->ud_subdevlen > 0) {
1580 kmem_free(dev->ud_subdevs,
1581 dev->ud_subdevlen * sizeof(device_t));
1582 dev->ud_subdevlen = 0;
1583 }
1584 kmem_free(dev, sizeof(*dev));
1585 }
1586
1587 /*
1588 * The general mechanism for detaching drivers works as follows: Each
1589 * driver is responsible for maintaining a reference count on the
1590 * number of outstanding references to its softc (e.g. from
1591 * processing hanging in a read or write). The detach method of the
1592 * driver decrements this counter and flags in the softc that the
1593 * driver is dying and then wakes any sleepers. It then sleeps on the
1594 * softc. Each place that can sleep must maintain the reference
1595 * count. When the reference count drops to -1 (0 is the normal value
1596 * of the reference count) the a wakeup on the softc is performed
1597 * signaling to the detach waiter that all references are gone.
1598 */
1599
1600 /*
1601 * Called from process context when we discover that a port has
1602 * been disconnected.
1603 */
1604 int
1605 usb_disconnect_port(struct usbd_port *up, device_t parent, int flags)
1606 {
1607 struct usbd_device *dev = up->up_dev;
1608 device_t subdev;
1609 char subdevname[16];
1610 const char *hubname = device_xname(parent);
1611 int i, rc;
1612
1613 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1614 DPRINTFN(3, "up=%p dev=%p port=%d", up, dev, up->up_portno, 0);
1615
1616 if (dev == NULL) {
1617 #ifdef DIAGNOSTIC
1618 printf("usb_disconnect_port: no device\n");
1619 #endif
1620 return 0;
1621 }
1622
1623 if (dev->ud_subdevlen > 0) {
1624 DPRINTFN(3, "disconnect subdevs", 0, 0, 0, 0);
1625 for (i = 0; i < dev->ud_subdevlen; i++) {
1626 if ((subdev = dev->ud_subdevs[i]) == NULL)
1627 continue;
1628 strlcpy(subdevname, device_xname(subdev),
1629 sizeof(subdevname));
1630 if ((rc = config_detach(subdev, flags)) != 0)
1631 return rc;
1632 printf("%s: at %s", subdevname, hubname);
1633 if (up->up_portno != 0)
1634 printf(" port %d", up->up_portno);
1635 printf(" (addr %d) disconnected\n", dev->ud_addr);
1636 }
1637 KASSERT(!dev->ud_nifaces_claimed);
1638 }
1639
1640 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1641 dev->ud_bus->ub_devices[dev->ud_addr] = NULL;
1642 up->up_dev = NULL;
1643 usb_free_device(dev);
1644 return 0;
1645 }
1646