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