usb_subr.c revision 1.243 1 /* $NetBSD: usb_subr.c,v 1.243 2020/03/14 02:35:33 christos Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart (at) augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.243 2020/03/14 02:35:33 christos Exp $");
36
37 #ifdef _KERNEL_OPT
38 #include "opt_compat_netbsd.h"
39 #include "opt_usb.h"
40 #include "opt_usbverbose.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/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 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
451 ed->bLength == 0 ? "0 length" :
452 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
453 "out of data");
454 goto bad;
455 found:
456 ifc->ui_endpoints[endpt].ue_edesc = ed;
457 if (dev->ud_speed == USB_SPEED_HIGH) {
458 u_int mps;
459 /* Control and bulk endpoints have max packet limits. */
460 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
461 case UE_CONTROL:
462 mps = USB_2_MAX_CTRL_PACKET;
463 goto check;
464 case UE_BULK:
465 mps = USB_2_MAX_BULK_PACKET;
466 check:
467 if (UGETW(ed->wMaxPacketSize) != mps) {
468 USETW(ed->wMaxPacketSize, mps);
469 #ifdef DIAGNOSTIC
470 printf("usbd_fill_iface_data: bad max "
471 "packet size\n");
472 #endif
473 }
474 break;
475 default:
476 break;
477 }
478 }
479 ifc->ui_endpoints[endpt].ue_refcnt = 0;
480 ifc->ui_endpoints[endpt].ue_toggle = 0;
481 p += ed->bLength;
482 }
483 #undef ed
484 LIST_INIT(&ifc->ui_pipes);
485 return USBD_NORMAL_COMPLETION;
486
487 bad:
488 if (ifc->ui_endpoints != NULL) {
489 kmem_free(ifc->ui_endpoints, nendpt * sizeof(struct usbd_endpoint));
490 ifc->ui_endpoints = NULL;
491 }
492 return USBD_INVAL;
493 }
494
495 void
496 usbd_free_iface_data(struct usbd_device *dev, int ifcno)
497 {
498 struct usbd_interface *ifc = &dev->ud_ifaces[ifcno];
499 if (ifc->ui_endpoints) {
500 int nendpt = ifc->ui_idesc->bNumEndpoints;
501 size_t sz = nendpt * sizeof(struct usbd_endpoint);
502 kmem_free(ifc->ui_endpoints, sz);
503 }
504 }
505
506 usbd_status
507 usbd_set_config_no(struct usbd_device *dev, int no, int msg)
508 {
509 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "%jd", no, 0, 0, 0);
510 usb_config_descriptor_t cd;
511 usbd_status err;
512 int index;
513
514 if (no == USB_UNCONFIG_NO)
515 return usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg);
516
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 USBHIST_FUNC();
532 USBHIST_CALLARGS(usbdebug, "dev=%#jx index=%jd",
533 (uintptr_t)dev, index, 0, 0);
534 usb_config_descriptor_t cd, *cdp;
535 usb_bos_descriptor_t *bdp = NULL;
536 usbd_status err;
537 int i, ifcidx, nifc, len, selfpowered, power;
538
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 = %jd", 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=%jd", err, 0, 0, 0);
580 return err;
581 }
582 len = UGETW(cd.wTotalLength);
583 if (len < USB_CONFIG_DESCRIPTOR_SIZE) {
584 DPRINTF("empty short descriptor", 0, 0, 0, 0);
585 return USBD_INVAL;
586 }
587 cdp = kmem_alloc(len, KM_SLEEP);
588
589 /* Get the full descriptor. Try a few times for slow devices. */
590 for (i = 0; i < 3; i++) {
591 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
592 if (!err)
593 break;
594 usbd_delay_ms(dev, 200);
595 }
596 if (err) {
597 DPRINTF("get_desc=%jd", err, 0, 0, 0);
598 goto bad;
599 }
600 if (cdp->bDescriptorType != UDESC_CONFIG) {
601 DPRINTF("bad desc %jd", cdp->bDescriptorType, 0, 0, 0);
602 err = USBD_INVAL;
603 goto bad;
604 }
605 if (UGETW(cdp->wTotalLength) != UGETW(cd.wTotalLength)) {
606 DPRINTF("bad len %jd", UGETW(cdp->wTotalLength), 0, 0, 0);
607 err = USBD_INVAL;
608 goto bad;
609 }
610
611 if (USB_IS_SS(dev->ud_speed)) {
612 usb_bos_descriptor_t bd;
613
614 /* get short bos desc */
615 err = usbd_get_bos_desc(dev, index, &bd);
616 if (!err) {
617 int blen = UGETW(bd.wTotalLength);
618 if (blen < USB_BOS_DESCRIPTOR_SIZE) {
619 DPRINTF("empty bos descriptor", 0, 0, 0, 0);
620 err = USBD_INVAL;
621 goto bad;
622 }
623 bdp = kmem_alloc(blen, KM_SLEEP);
624
625 /* Get the full desc */
626 for (i = 0; i < 3; i++) {
627 err = usbd_get_desc(dev, UDESC_BOS, index, blen,
628 bdp);
629 if (!err)
630 break;
631 usbd_delay_ms(dev, 200);
632 }
633 if (err || bdp->bDescriptorType != UDESC_BOS ||
634 UGETW(bdp->wTotalLength) != UGETW(bd.wTotalLength)) {
635 DPRINTF("error %jd or bad desc %jd", err,
636 bdp->bDescriptorType, 0, 0);
637 kmem_free(bdp, blen);
638 bdp = NULL;
639 }
640 }
641 }
642 dev->ud_bdesc = bdp;
643
644 /*
645 * Figure out if the device is self or bus powered.
646 */
647 #if 0 /* XXX various devices don't report the power state correctly */
648 selfpowered = 0;
649 err = usbd_get_device_status(dev, &ds);
650 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
651 selfpowered = 1;
652 #endif
653 /*
654 * Use the power state in the configuration we are going
655 * to set. This doesn't necessarily reflect the actual
656 * power state of the device; the driver can control this
657 * by choosing the appropriate configuration.
658 */
659 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
660
661 DPRINTF("addr %jd cno=%jd attr=0x%02jx, selfpowered=%jd",
662 dev->ud_addr, cdp->bConfigurationValue, cdp->bmAttributes,
663 selfpowered);
664 DPRINTF("max power=%jd", cdp->bMaxPower * 2, 0, 0, 0);
665
666 /* Check if we have enough power. */
667 #if 0 /* this is a no-op, see above */
668 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
669 if (msg)
670 printf("%s: device addr %d (config %d): "
671 "can't set self powered configuration\n",
672 device_xname(dev->ud_bus->bdev), dev->ud_addr,
673 cdp->bConfigurationValue);
674 err = USBD_NO_POWER;
675 goto bad;
676 }
677 #endif
678 #ifdef USB_DEBUG
679 if (dev->ud_powersrc == NULL) {
680 DPRINTF("No power source?", 0, 0, 0, 0);
681 err = USBD_IOERROR;
682 goto bad;
683 }
684 #endif
685 power = cdp->bMaxPower * 2;
686 if (power > dev->ud_powersrc->up_power) {
687 DPRINTF("power exceeded %jd %jd", power,
688 dev->ud_powersrc->up_power, 0, 0);
689 /* XXX print nicer message. */
690 if (msg)
691 printf("%s: device addr %d (config %d) exceeds power "
692 "budget, %d mA > %d mA\n",
693 device_xname(dev->ud_bus->ub_usbctl), dev->ud_addr,
694 cdp->bConfigurationValue,
695 power, dev->ud_powersrc->up_power);
696 err = USBD_NO_POWER;
697 goto bad;
698 }
699 dev->ud_power = power;
700 dev->ud_selfpowered = selfpowered;
701
702 /* Set the actual configuration value. */
703 DPRINTF("set config %jd", cdp->bConfigurationValue, 0, 0, 0);
704 err = usbd_set_config(dev, cdp->bConfigurationValue);
705 if (err) {
706 DPRINTF("setting config=%jd failed, error=%jd",
707 cdp->bConfigurationValue, err, 0, 0);
708 goto bad;
709 }
710
711 /* Allocate and fill interface data. */
712 nifc = cdp->bNumInterface;
713 if (nifc == 0) {
714 DPRINTF("no interfaces", 0, 0, 0, 0);
715 err = USBD_INVAL;
716 goto bad;
717 }
718 dev->ud_ifaces = kmem_alloc(nifc * sizeof(struct usbd_interface),
719 KM_SLEEP);
720 DPRINTFN(5, "dev=%#jx cdesc=%#jx", (uintptr_t)dev, (uintptr_t)cdp,
721 0, 0);
722 dev->ud_cdesc = cdp;
723 dev->ud_config = cdp->bConfigurationValue;
724 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
725 err = usbd_fill_iface_data(dev, ifcidx, 0);
726 if (err) {
727 while (--ifcidx >= 0)
728 usbd_free_iface_data(dev, ifcidx);
729 goto bad;
730 }
731 }
732
733 return USBD_NORMAL_COMPLETION;
734
735 bad:
736 kmem_free(cdp, len);
737 if (bdp != NULL) {
738 kmem_free(bdp, UGETW(bdp->wTotalLength));
739 dev->ud_bdesc = NULL;
740 }
741 return err;
742 }
743
744 /* XXX add function for alternate settings */
745
746 usbd_status
747 usbd_setup_pipe(struct usbd_device *dev, struct usbd_interface *iface,
748 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe)
749 {
750 return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0);
751 }
752
753 usbd_status
754 usbd_setup_pipe_flags(struct usbd_device *dev, struct usbd_interface *iface,
755 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe, uint8_t flags)
756 {
757 USBHIST_FUNC();
758 USBHIST_CALLARGS(usbdebug, "dev=%#jx addr=%jd iface=%#jx ep=%#jx",
759 (uintptr_t)dev, dev->ud_addr, (uintptr_t)iface, (uintptr_t)ep);
760 struct usbd_pipe *p;
761 usbd_status err;
762
763 p = kmem_alloc(dev->ud_bus->ub_pipesize, KM_SLEEP);
764 DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0);
765 p->up_dev = dev;
766 p->up_iface = iface;
767 p->up_endpoint = ep;
768 ep->ue_refcnt++;
769 p->up_intrxfer = NULL;
770 p->up_running = 0;
771 p->up_aborting = 0;
772 p->up_serialise = true;
773 p->up_repeat = 0;
774 p->up_interval = ival;
775 p->up_flags = flags;
776 SIMPLEQ_INIT(&p->up_queue);
777 err = dev->ud_bus->ub_methods->ubm_open(p);
778 if (err) {
779 DPRINTF("endpoint=%#jx failed, error=%jd",
780 (uintptr_t)ep->ue_edesc->bEndpointAddress, err, 0, 0);
781 kmem_free(p, dev->ud_bus->ub_pipesize);
782 return err;
783 }
784
785 KASSERT(p->up_methods->upm_start || p->up_serialise == false);
786
787 usb_init_task(&p->up_async_task, usbd_clear_endpoint_stall_task, p,
788 USB_TASKQ_MPSAFE);
789 DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0);
790 *pipe = p;
791 return USBD_NORMAL_COMPLETION;
792 }
793
794 /* Abort the device control pipe. */
795 void
796 usbd_kill_pipe(struct usbd_pipe *pipe)
797 {
798 usbd_abort_pipe(pipe);
799 usbd_lock_pipe(pipe);
800 pipe->up_methods->upm_close(pipe);
801 usbd_unlock_pipe(pipe);
802 usb_rem_task_wait(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER,
803 NULL);
804 pipe->up_endpoint->ue_refcnt--;
805 kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
806 }
807
808 int
809 usbd_getnewaddr(struct usbd_bus *bus)
810 {
811 int addr;
812
813 for (addr = 1; addr < USB_MAX_DEVICES; addr++) {
814 size_t dindex = usb_addr2dindex(addr);
815 if (bus->ub_devices[dindex] == NULL)
816 return addr;
817 }
818 return -1;
819 }
820
821 usbd_status
822 usbd_attach_roothub(device_t parent, struct usbd_device *dev)
823 {
824 struct usb_attach_arg uaa;
825 usb_device_descriptor_t *dd = &dev->ud_ddesc;
826 device_t dv;
827
828 uaa.uaa_device = dev;
829 uaa.uaa_usegeneric = 0;
830 uaa.uaa_port = 0;
831 uaa.uaa_vendor = UGETW(dd->idVendor);
832 uaa.uaa_product = UGETW(dd->idProduct);
833 uaa.uaa_release = UGETW(dd->bcdDevice);
834 uaa.uaa_class = dd->bDeviceClass;
835 uaa.uaa_subclass = dd->bDeviceSubClass;
836 uaa.uaa_proto = dd->bDeviceProtocol;
837
838 KERNEL_LOCK(1, curlwp);
839 dv = config_found_ia(parent, "usbroothubif", &uaa, 0);
840 KERNEL_UNLOCK_ONE(curlwp);
841 if (dv) {
842 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP);
843 dev->ud_subdevs[0] = dv;
844 dev->ud_subdevlen = 1;
845 }
846 return USBD_NORMAL_COMPLETION;
847 }
848
849 static void
850 usbd_serialnumber(device_t dv, struct usbd_device *dev)
851 {
852 if (dev->ud_serial) {
853 prop_dictionary_set_cstring(device_properties(dv),
854 "serialnumber", dev->ud_serial);
855 }
856 }
857
858 static usbd_status
859 usbd_attachwholedevice(device_t parent, struct usbd_device *dev, int port,
860 int usegeneric)
861 {
862 struct usb_attach_arg uaa;
863 usb_device_descriptor_t *dd = &dev->ud_ddesc;
864 device_t dv;
865 int dlocs[USBDEVIFCF_NLOCS];
866
867 uaa.uaa_device = dev;
868 uaa.uaa_usegeneric = usegeneric;
869 uaa.uaa_port = port;
870 uaa.uaa_vendor = UGETW(dd->idVendor);
871 uaa.uaa_product = UGETW(dd->idProduct);
872 uaa.uaa_release = UGETW(dd->bcdDevice);
873 uaa.uaa_class = dd->bDeviceClass;
874 uaa.uaa_subclass = dd->bDeviceSubClass;
875 uaa.uaa_proto = dd->bDeviceProtocol;
876
877 dlocs[USBDEVIFCF_PORT] = uaa.uaa_port;
878 dlocs[USBDEVIFCF_VENDOR] = uaa.uaa_vendor;
879 dlocs[USBDEVIFCF_PRODUCT] = uaa.uaa_product;
880 dlocs[USBDEVIFCF_RELEASE] = uaa.uaa_release;
881 /* the rest is historical ballast */
882 dlocs[USBDEVIFCF_CONFIGURATION] = -1;
883 dlocs[USBDEVIFCF_INTERFACE] = -1;
884
885 KERNEL_LOCK(1, curlwp);
886 config_pending_incr(parent);
887 dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print,
888 config_stdsubmatch);
889 KERNEL_UNLOCK_ONE(curlwp);
890 if (dv) {
891 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP);
892 dev->ud_subdevs[0] = dv;
893 dev->ud_subdevlen = 1;
894 dev->ud_nifaces_claimed = 1; /* XXX */
895 usbd_serialnumber(dv, dev);
896 }
897 config_pending_decr(parent);
898 return USBD_NORMAL_COMPLETION;
899 }
900
901 static usbd_status
902 usbd_attachinterfaces(device_t parent, struct usbd_device *dev,
903 int port, const int *locators)
904 {
905 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
906 struct usbif_attach_arg uiaa;
907 int ilocs[USBIFIFCF_NLOCS];
908 usb_device_descriptor_t *dd = &dev->ud_ddesc;
909 int nifaces;
910 struct usbd_interface **ifaces;
911 int i, j, loc;
912 device_t dv;
913
914 nifaces = dev->ud_cdesc->bNumInterface;
915 ifaces = kmem_zalloc(nifaces * sizeof(*ifaces), KM_SLEEP);
916 for (i = 0; i < nifaces; i++) {
917 if (!dev->ud_subdevs[i]) {
918 ifaces[i] = &dev->ud_ifaces[i];
919 }
920 DPRINTF("interface %jd %#jx", i, (uintptr_t)ifaces[i], 0, 0);
921 }
922
923
924 uiaa.uiaa_device = dev;
925 uiaa.uiaa_port = port;
926 uiaa.uiaa_vendor = UGETW(dd->idVendor);
927 uiaa.uiaa_product = UGETW(dd->idProduct);
928 uiaa.uiaa_release = UGETW(dd->bcdDevice);
929 uiaa.uiaa_configno = dev->ud_cdesc->bConfigurationValue;
930 uiaa.uiaa_ifaces = ifaces;
931 uiaa.uiaa_nifaces = nifaces;
932 ilocs[USBIFIFCF_PORT] = uiaa.uiaa_port;
933 ilocs[USBIFIFCF_VENDOR] = uiaa.uiaa_vendor;
934 ilocs[USBIFIFCF_PRODUCT] = uiaa.uiaa_product;
935 ilocs[USBIFIFCF_RELEASE] = uiaa.uiaa_release;
936 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.uiaa_configno;
937
938 for (i = 0; i < nifaces; i++) {
939 if (!ifaces[i]) {
940 DPRINTF("interface %jd claimed", i, 0, 0, 0);
941 continue; /* interface already claimed */
942 }
943 uiaa.uiaa_iface = ifaces[i];
944 uiaa.uiaa_class = ifaces[i]->ui_idesc->bInterfaceClass;
945 uiaa.uiaa_subclass = ifaces[i]->ui_idesc->bInterfaceSubClass;
946 uiaa.uiaa_proto = ifaces[i]->ui_idesc->bInterfaceProtocol;
947 uiaa.uiaa_ifaceno = ifaces[i]->ui_idesc->bInterfaceNumber;
948
949 DPRINTF("searching for interface %jd...", i, 0, 0, 0);
950 DPRINTF("class %jx subclass %jx proto %jx ifaceno %jd",
951 uiaa.uiaa_class, uiaa.uiaa_subclass, uiaa.uiaa_proto,
952 uiaa.uiaa_ifaceno);
953 ilocs[USBIFIFCF_INTERFACE] = uiaa.uiaa_ifaceno;
954 if (locators != NULL) {
955 loc = locators[USBIFIFCF_CONFIGURATION];
956 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT &&
957 loc != uiaa.uiaa_configno)
958 continue;
959 loc = locators[USBIFIFCF_INTERFACE];
960 if (loc != USBIFIFCF_INTERFACE_DEFAULT &&
961 loc != uiaa.uiaa_ifaceno)
962 continue;
963 }
964 KERNEL_LOCK(1, curlwp);
965 dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa,
966 usbd_ifprint, config_stdsubmatch);
967 KERNEL_UNLOCK_ONE(curlwp);
968 if (!dv)
969 continue;
970
971 usbd_serialnumber(dv, dev);
972
973 /* claim */
974 ifaces[i] = NULL;
975 /* account for ifaces claimed by the driver behind our back */
976 for (j = 0; j < nifaces; j++) {
977
978 if (!ifaces[j] && !dev->ud_subdevs[j]) {
979 DPRINTF("interface %jd claimed behind our back",
980 j, 0, 0, 0);
981 dev->ud_subdevs[j] = dv;
982 dev->ud_nifaces_claimed++;
983 }
984 }
985 }
986
987 kmem_free(ifaces, nifaces * sizeof(*ifaces));
988 return USBD_NORMAL_COMPLETION;
989 }
990
991 usbd_status
992 usbd_probe_and_attach(device_t parent, struct usbd_device *dev,
993 int port, int addr)
994 {
995 USBHIST_FUNC();
996 USBHIST_CALLARGS(usbdebug, "trying device specific drivers", 0, 0, 0, 0);
997 usb_device_descriptor_t *dd = &dev->ud_ddesc;
998 int confi, nifaces;
999 usbd_status err;
1000
1001 /* First try with device specific drivers. */
1002 err = usbd_attachwholedevice(parent, dev, port, 0);
1003 if (dev->ud_nifaces_claimed || err)
1004 return err;
1005 DPRINTF("no device specific driver found", 0, 0, 0, 0);
1006
1007 DPRINTF("looping over %jd configurations", dd->bNumConfigurations,
1008 0, 0, 0);
1009 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
1010 DPRINTFN(1, "trying config idx=%jd", confi, 0, 0, 0);
1011 err = usbd_set_config_index(dev, confi, 1);
1012 if (err) {
1013 DPRINTF("port %jd, set config at addr %jd failed, "
1014 "error=%d", port, addr, err, 0);
1015 printf("%s: port %d, set config at addr %d failed\n",
1016 device_xname(parent), port, addr);
1017 return err;
1018 }
1019 nifaces = dev->ud_cdesc->bNumInterface;
1020 dev->ud_subdevs = kmem_zalloc(nifaces * sizeof(device_t),
1021 KM_SLEEP);
1022 if (dev->ud_subdevs == NULL)
1023 return USBD_NOMEM;
1024 dev->ud_subdevlen = nifaces;
1025
1026 err = usbd_attachinterfaces(parent, dev, port, NULL);
1027
1028 if (!dev->ud_nifaces_claimed) {
1029 kmem_free(dev->ud_subdevs,
1030 dev->ud_subdevlen * sizeof(device_t));
1031 dev->ud_subdevs = 0;
1032 dev->ud_subdevlen = 0;
1033 }
1034 if (dev->ud_nifaces_claimed || err)
1035 return err;
1036 }
1037 /* No interfaces were attached in any of the configurations. */
1038
1039 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
1040 usbd_set_config_index(dev, 0, 0);
1041
1042 DPRINTF("no interface drivers found", 0, 0, 0, 0);
1043
1044 /* Finally try the generic driver. */
1045 err = usbd_attachwholedevice(parent, dev, port, 1);
1046
1047 /*
1048 * The generic attach failed, but leave the device as it is.
1049 * We just did not find any drivers, that's all. The device is
1050 * fully operational and not harming anyone.
1051 */
1052 DPRINTF("generic attach failed", 0, 0, 0, 0);
1053
1054 return USBD_NORMAL_COMPLETION;
1055 }
1056
1057 /**
1058 * Called from uhub_rescan(). usbd_new_device() for the target dev must be
1059 * called before calling this.
1060 */
1061 usbd_status
1062 usbd_reattach_device(device_t parent, struct usbd_device *dev,
1063 int port, const int *locators)
1064 {
1065 int i, loc;
1066
1067 if (locators != NULL) {
1068 loc = locators[USBIFIFCF_PORT];
1069 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port)
1070 return USBD_NORMAL_COMPLETION;
1071 loc = locators[USBIFIFCF_VENDOR];
1072 if (loc != USBIFIFCF_VENDOR_DEFAULT &&
1073 loc != UGETW(dev->ud_ddesc.idVendor))
1074 return USBD_NORMAL_COMPLETION;
1075 loc = locators[USBIFIFCF_PRODUCT];
1076 if (loc != USBIFIFCF_PRODUCT_DEFAULT &&
1077 loc != UGETW(dev->ud_ddesc.idProduct))
1078 return USBD_NORMAL_COMPLETION;
1079 loc = locators[USBIFIFCF_RELEASE];
1080 if (loc != USBIFIFCF_RELEASE_DEFAULT &&
1081 loc != UGETW(dev->ud_ddesc.bcdDevice))
1082 return USBD_NORMAL_COMPLETION;
1083 }
1084 if (dev->ud_subdevlen == 0) {
1085 /* XXX: check USBIFIFCF_CONFIGURATION and
1086 * USBIFIFCF_INTERFACE too */
1087 return usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
1088 } else if (dev->ud_subdevlen != dev->ud_cdesc->bNumInterface) {
1089 /* device-specific or generic driver is already attached. */
1090 return USBD_NORMAL_COMPLETION;
1091 }
1092 /* Does the device have unconfigured interfaces? */
1093 for (i = 0; i < dev->ud_subdevlen; i++) {
1094 if (dev->ud_subdevs[i] == NULL) {
1095 break;
1096 }
1097 }
1098 if (i >= dev->ud_subdevlen)
1099 return USBD_NORMAL_COMPLETION;
1100 return usbd_attachinterfaces(parent, dev, port, locators);
1101 }
1102
1103 /*
1104 * Called when a new device has been put in the powered state,
1105 * but not yet in the addressed state.
1106 * Get initial descriptor, set the address, get full descriptor,
1107 * and attach a driver.
1108 */
1109 usbd_status
1110 usbd_new_device(device_t parent, struct usbd_bus *bus, int depth, int speed,
1111 int port, struct usbd_port *up)
1112 {
1113 USBHIST_FUNC();
1114 USBHIST_CALLARGS(usbdebug, "bus=%#jx port=%jd depth=%jd speed=%jd",
1115 (uintptr_t)bus, port, depth, speed);
1116 struct usbd_device *dev, *adev;
1117 struct usbd_device *hub;
1118 usb_device_descriptor_t *dd;
1119 usb_port_status_t ps;
1120 usbd_status err;
1121 int addr;
1122 int i;
1123 int p;
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 dev->ud_bus = bus;
1138
1139 /* Set up default endpoint handle. */
1140 dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
1141
1142 /* Set up default endpoint descriptor. */
1143 dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1144 dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
1145 dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1146 dev->ud_ep0desc.bmAttributes = UE_CONTROL;
1147 /*
1148 * temporary, will be fixed after first descriptor fetch
1149 * (which uses 64 bytes so it shouldn't be less),
1150 * highspeed devices must support 64 byte packets anyway
1151 */
1152 if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL)
1153 USETW(dev->ud_ep0desc.wMaxPacketSize, 64);
1154 else
1155 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
1156
1157 dev->ud_ep0desc.bInterval = 0;
1158
1159 /* doesn't matter, just don't leave it uninitialized */
1160 dev->ud_ep0.ue_toggle = 0;
1161
1162 dev->ud_quirks = &usbd_no_quirk;
1163 dev->ud_addr = USB_START_ADDR;
1164 dev->ud_ddesc.bMaxPacketSize = 0;
1165 dev->ud_depth = depth;
1166 dev->ud_powersrc = up;
1167 dev->ud_myhub = up->up_parent;
1168
1169 up->up_dev = dev;
1170
1171 /* Locate port on upstream high speed hub */
1172 for (adev = dev, hub = up->up_parent;
1173 hub != NULL && hub->ud_speed != USB_SPEED_HIGH;
1174 adev = hub, hub = hub->ud_myhub)
1175 ;
1176 if (hub) {
1177 for (p = 1; p <= hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
1178 if (hub->ud_hub->uh_ports[p - 1].up_dev == adev) {
1179 dev->ud_myhsport =
1180 &hub->ud_hub->uh_ports[p - 1];
1181 goto found;
1182 }
1183 }
1184 panic("usbd_new_device: cannot find HS port");
1185 found:
1186 DPRINTFN(1, "high speed port %jd", p, 0, 0, 0);
1187 } else {
1188 dev->ud_myhsport = NULL;
1189 }
1190 dev->ud_speed = speed;
1191 dev->ud_langid = USBD_NOLANG;
1192 dev->ud_cookie.cookie = ++usb_cookie_no;
1193
1194 /* Establish the default pipe. */
1195 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1196 &dev->ud_pipe0, USBD_MPSAFE);
1197 if (err) {
1198 usbd_remove_device(dev, up);
1199 return err;
1200 }
1201
1202 dd = &dev->ud_ddesc;
1203 /* Try a few times in case the device is slow (i.e. outside specs.) */
1204 for (i = 0; i < 10; i++) {
1205 /* Get the first 8 bytes of the device descriptor. */
1206 err = usbd_get_initial_ddesc(dev, dd);
1207 if (!err)
1208 break;
1209 /*
1210 * The root hub can never fail to give the initial descriptor,
1211 * but assert it just in case.
1212 */
1213 KASSERT(up->up_parent);
1214 usbd_delay_ms(dev, 200);
1215 if ((i & 3) == 3)
1216 usbd_reset_port(up->up_parent, port, &ps);
1217 }
1218 if (err) {
1219 DPRINTF("addr=%jd, getting first desc failed: %jd", addr, err,
1220 0, 0);
1221 usbd_remove_device(dev, up);
1222 return err;
1223 }
1224
1225 /* Windows resets the port here, do likewise */
1226 if (up->up_parent)
1227 usbd_reset_port(up->up_parent, port, &ps);
1228
1229 if (speed == USB_SPEED_HIGH) {
1230 /* Max packet size must be 64 (sec 5.5.3). */
1231 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1232 #ifdef DIAGNOSTIC
1233 printf("usbd_new_device: addr=%d bad max packet "
1234 "size=%d. adjusting to %d.\n",
1235 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET);
1236 #endif
1237 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1238 }
1239 }
1240
1241 DPRINTF("adding unit addr=%jd, rev=%02jx, class=%jd, subclass=%jd",
1242 addr, UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass);
1243 DPRINTF("protocol=%jd, maxpacket=%jd, len=%jd, speed=%jd",
1244 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, dev->ud_speed);
1245
1246 if (dd->bDescriptorType != UDESC_DEVICE) {
1247 /* Illegal device descriptor */
1248 DPRINTF("illegal descriptor %jd", dd->bDescriptorType, 0, 0, 0);
1249 usbd_remove_device(dev, up);
1250 return USBD_INVAL;
1251 }
1252
1253 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1254 DPRINTF("bad length %jd", dd->bLength, 0, 0, 0);
1255 usbd_remove_device(dev, up);
1256 return USBD_INVAL;
1257 }
1258
1259 USETW(dev->ud_ep0desc.wMaxPacketSize, dd->bMaxPacketSize);
1260
1261 /* Re-establish the default pipe with the new MPS. */
1262 usbd_kill_pipe(dev->ud_pipe0);
1263 dev->ud_pipe0 = NULL;
1264 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1265 &dev->ud_pipe0, USBD_MPSAFE);
1266 if (err) {
1267 DPRINTF("setup default pipe failed err %jd", err, 0, 0, 0);
1268 usbd_remove_device(dev, up);
1269 return err;
1270 }
1271
1272 /* Set the address */
1273 DPRINTFN(5, "setting device address=%jd", addr, 0, 0, 0);
1274 err = usbd_set_address(dev, addr);
1275 if (err) {
1276 DPRINTF("set address %jd failed, err = %jd", addr, err, 0, 0);
1277 err = USBD_SET_ADDR_FAILED;
1278 usbd_remove_device(dev, up);
1279 return err;
1280 }
1281
1282 /* Allow device time to set new address */
1283 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1284 dev->ud_addr = addr; /* new device address now */
1285 bus->ub_devices[usb_addr2dindex(addr)] = dev;
1286
1287 /* Re-establish the default pipe with the new address. */
1288 usbd_kill_pipe(dev->ud_pipe0);
1289 dev->ud_pipe0 = NULL;
1290 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1291 &dev->ud_pipe0, USBD_MPSAFE);
1292 if (err) {
1293 DPRINTF("setup default pipe failed, err = %jd", err, 0, 0, 0);
1294 usbd_remove_device(dev, up);
1295 return err;
1296 }
1297
1298 err = usbd_reload_device_desc(dev);
1299 if (err) {
1300 DPRINTF("addr=%jd, getting full desc failed, err = %jd", addr,
1301 err, 0, 0);
1302 usbd_remove_device(dev, up);
1303 return err;
1304 }
1305
1306 /* Assume 100mA bus powered for now. Changed when configured. */
1307 dev->ud_power = USB_MIN_POWER;
1308 dev->ud_selfpowered = 0;
1309
1310 DPRINTF("new dev (addr %jd), dev=%#jx, parent=%#jx",
1311 addr, (uintptr_t)dev, (uintptr_t)parent, 0);
1312
1313 usbd_get_device_strings(dev);
1314
1315 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1316
1317 if (port == 0) { /* root hub */
1318 KASSERT(addr == 1);
1319 usbd_attach_roothub(parent, dev);
1320 return USBD_NORMAL_COMPLETION;
1321 }
1322
1323 err = usbd_probe_and_attach(parent, dev, port, addr);
1324 if (err) {
1325 usbd_remove_device(dev, up);
1326 return err;
1327 }
1328
1329 return USBD_NORMAL_COMPLETION;
1330 }
1331
1332 usbd_status
1333 usbd_reload_device_desc(struct usbd_device *dev)
1334 {
1335 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1336 usb_device_descriptor_t *udd = &dev->ud_ddesc;
1337 usbd_status err;
1338
1339 /* Get the full device descriptor. */
1340 err = usbd_get_device_desc(dev, udd);
1341 if (err)
1342 return err;
1343 if (udd->bDescriptorType != UDESC_DEVICE)
1344 return USBD_INVAL;
1345 if (udd->bLength < USB_DEVICE_DESCRIPTOR_SIZE)
1346 return USBD_INVAL;
1347
1348 DPRINTFN(15, "bLength %5ju", udd->bLength, 0, 0, 0);
1349 DPRINTFN(15, "bDescriptorType %5ju", udd->bDescriptorType, 0, 0, 0);
1350 DPRINTFN(15, "bcdUSB %2jx.%02jx", udd->bcdUSB[1],
1351 udd->bcdUSB[0], 0, 0);
1352 DPRINTFN(15, "bDeviceClass %5ju", udd->bDeviceClass, 0, 0, 0);
1353 DPRINTFN(15, "bDeviceSubClass %5ju", udd->bDeviceSubClass, 0, 0, 0);
1354 DPRINTFN(15, "bDeviceProtocol %5ju", udd->bDeviceProtocol, 0, 0, 0);
1355 DPRINTFN(15, "bMaxPacketSize0 %5ju", udd->bMaxPacketSize, 0, 0, 0);
1356 DPRINTFN(15, "idVendor 0x%02jx 0x%02jx",
1357 udd->idVendor[0],
1358 udd->idVendor[1], 0, 0);
1359 DPRINTFN(15, "idProduct 0x%02jx 0x%02jx",
1360 udd->idProduct[0],
1361 udd->idProduct[1], 0, 0);
1362 DPRINTFN(15, "bcdDevice %2jx.%02jx", udd->bcdDevice[1],
1363 udd->bcdDevice[0], 0, 0);
1364 DPRINTFN(15, "iManufacturer %5ju", udd->iManufacturer, 0, 0, 0);
1365 DPRINTFN(15, "iProduct %5ju", udd->iProduct, 0, 0, 0);
1366 DPRINTFN(15, "iSerial %5ju", udd->iSerialNumber, 0, 0, 0);
1367 DPRINTFN(15, "bNumConfigurations %5ju", udd->bNumConfigurations, 0, 0,
1368 0);
1369
1370 /* Figure out what's wrong with this device. */
1371 dev->ud_quirks = usbd_find_quirk(udd);
1372
1373 return USBD_NORMAL_COMPLETION;
1374 }
1375
1376 void
1377 usbd_remove_device(struct usbd_device *dev, struct usbd_port *up)
1378 {
1379
1380 USBHIST_FUNC();
1381 USBHIST_CALLARGS(usbdebug, "dev %#jx up %#jx",
1382 (uintptr_t)dev, (uintptr_t)up, 0, 0);
1383
1384 if (dev->ud_pipe0 != NULL)
1385 usbd_kill_pipe(dev->ud_pipe0);
1386 up->up_dev = NULL;
1387 dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL;
1388
1389 if (dev->ud_vendor != NULL) {
1390 kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN);
1391 }
1392 if (dev->ud_product != NULL) {
1393 kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN);
1394 }
1395 if (dev->ud_serial != NULL) {
1396 kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN);
1397 }
1398 kmem_free(dev, sizeof(*dev));
1399 }
1400
1401 int
1402 usbd_print(void *aux, const char *pnp)
1403 {
1404 struct usb_attach_arg *uaa = aux;
1405
1406 if (pnp) {
1407 #define USB_DEVINFO 1024
1408 char *devinfo;
1409 if (!uaa->uaa_usegeneric)
1410 return QUIET;
1411 devinfo = kmem_alloc(USB_DEVINFO, KM_SLEEP);
1412 usbd_devinfo(uaa->uaa_device, 1, devinfo, USB_DEVINFO);
1413 aprint_normal("%s, %s", devinfo, pnp);
1414 kmem_free(devinfo, USB_DEVINFO);
1415 }
1416 aprint_normal(" port %d", uaa->uaa_port);
1417 #if 0
1418 /*
1419 * It gets very crowded with these locators on the attach line.
1420 * They are not really needed since they are printed in the clear
1421 * by each driver.
1422 */
1423 if (uaa->uaa_vendor != UHUB_UNK_VENDOR)
1424 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor);
1425 if (uaa->uaa_product != UHUB_UNK_PRODUCT)
1426 aprint_normal(" product 0x%04x", uaa->uaa_product);
1427 if (uaa->uaa_release != UHUB_UNK_RELEASE)
1428 aprint_normal(" release 0x%04x", uaa->uaa_release);
1429 #endif
1430 return UNCONF;
1431 }
1432
1433 int
1434 usbd_ifprint(void *aux, const char *pnp)
1435 {
1436 struct usbif_attach_arg *uiaa = aux;
1437
1438 if (pnp)
1439 return QUIET;
1440 aprint_normal(" port %d", uiaa->uiaa_port);
1441 aprint_normal(" configuration %d", uiaa->uiaa_configno);
1442 aprint_normal(" interface %d", uiaa->uiaa_ifaceno);
1443 #if 0
1444 /*
1445 * It gets very crowded with these locators on the attach line.
1446 * They are not really needed since they are printed in the clear
1447 * by each driver.
1448 */
1449 if (uaa->uaa_vendor != UHUB_UNK_VENDOR)
1450 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor);
1451 if (uaa->uaa_product != UHUB_UNK_PRODUCT)
1452 aprint_normal(" product 0x%04x", uaa->uaa_product);
1453 if (uaa->uaa_release != UHUB_UNK_RELEASE)
1454 aprint_normal(" release 0x%04x", uaa->uaa_release);
1455 #endif
1456 return UNCONF;
1457 }
1458
1459 void
1460 usbd_fill_deviceinfo(struct usbd_device *dev, struct usb_device_info *di,
1461 int usedev)
1462 {
1463 struct usbd_port *p;
1464 int i, j, err;
1465
1466 di->udi_bus = device_unit(dev->ud_bus->ub_usbctl);
1467 di->udi_addr = dev->ud_addr;
1468 di->udi_cookie = dev->ud_cookie;
1469 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1470 di->udi_product, sizeof(di->udi_product), usedev, 1);
1471 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1472 UGETW(dev->ud_ddesc.bcdDevice));
1473 if (usedev) {
1474 usbd_status uerr = usbd_get_string(dev,
1475 dev->ud_ddesc.iSerialNumber, di->udi_serial);
1476 if (uerr != USBD_NORMAL_COMPLETION) {
1477 di->udi_serial[0] = '\0';
1478 } else {
1479 usbd_trim_spaces(di->udi_serial);
1480 }
1481 } else {
1482 di->udi_serial[0] = '\0';
1483 if (dev->ud_serial) {
1484 strlcpy(di->udi_serial, dev->ud_serial,
1485 sizeof(di->udi_serial));
1486 }
1487 }
1488
1489 di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor);
1490 di->udi_productNo = UGETW(dev->ud_ddesc.idProduct);
1491 di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice);
1492 di->udi_class = dev->ud_ddesc.bDeviceClass;
1493 di->udi_subclass = dev->ud_ddesc.bDeviceSubClass;
1494 di->udi_protocol = dev->ud_ddesc.bDeviceProtocol;
1495 di->udi_config = dev->ud_config;
1496 di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power;
1497 di->udi_speed = dev->ud_speed;
1498
1499 if (dev->ud_subdevlen > 0) {
1500 for (i = 0, j = 0; i < dev->ud_subdevlen &&
1501 j < USB_MAX_DEVNAMES; i++) {
1502 if (!dev->ud_subdevs[i])
1503 continue;
1504 strncpy(di->udi_devnames[j],
1505 device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN);
1506 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1507 j++;
1508 }
1509 } else {
1510 j = 0;
1511 }
1512 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1513 di->udi_devnames[j][0] = 0; /* empty */
1514
1515 if (!dev->ud_hub) {
1516 di->udi_nports = 0;
1517 return;
1518 }
1519
1520 const int nports = dev->ud_hub->uh_hubdesc.bNbrPorts;
1521 for (i = 1; i <= __arraycount(di->udi_ports) && i <= nports; i++) {
1522 p = &dev->ud_hub->uh_ports[i - 1];
1523 if (p->up_dev)
1524 err = p->up_dev->ud_addr;
1525 else {
1526 const int s = UGETW(p->up_status.wPortStatus);
1527 const bool sshub_p = USB_IS_SS(dev->ud_speed);
1528 if (s & UPS_PORT_ENABLED)
1529 err = USB_PORT_ENABLED;
1530 else if (s & UPS_SUSPEND)
1531 err = USB_PORT_SUSPENDED;
1532 /*
1533 * Note: UPS_PORT_POWER_SS is available only
1534 * on 3.x, and UPS_PORT_POWER is available
1535 * only on 2.0 or 1.1.
1536 */
1537 else if (sshub_p && (s & UPS_PORT_POWER_SS))
1538 err = USB_PORT_POWERED;
1539 else if (!sshub_p && (s & UPS_PORT_POWER))
1540 err = USB_PORT_POWERED;
1541 else
1542 err = USB_PORT_DISABLED;
1543 }
1544 di->udi_ports[i - 1] = err;
1545 }
1546 di->udi_nports = nports;
1547 }
1548
1549 void
1550 usb_free_device(struct usbd_device *dev)
1551 {
1552 int ifcidx, nifc;
1553
1554 if (dev->ud_pipe0 != NULL)
1555 usbd_kill_pipe(dev->ud_pipe0);
1556 if (dev->ud_ifaces != NULL) {
1557 nifc = dev->ud_cdesc->bNumInterface;
1558 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1559 usbd_free_iface_data(dev, ifcidx);
1560 kmem_free(dev->ud_ifaces,
1561 nifc * sizeof(struct usbd_interface));
1562 }
1563 if (dev->ud_cdesc != NULL)
1564 kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength));
1565 if (dev->ud_bdesc != NULL)
1566 kmem_free(dev->ud_bdesc, UGETW(dev->ud_bdesc->wTotalLength));
1567 if (dev->ud_subdevlen > 0) {
1568 kmem_free(dev->ud_subdevs,
1569 dev->ud_subdevlen * sizeof(device_t));
1570 dev->ud_subdevlen = 0;
1571 }
1572 if (dev->ud_vendor) {
1573 kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN);
1574 }
1575 if (dev->ud_product) {
1576 kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN);
1577 }
1578 if (dev->ud_serial) {
1579 kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN);
1580 }
1581 kmem_free(dev, sizeof(*dev));
1582 }
1583
1584 /*
1585 * The general mechanism for detaching drivers works as follows: Each
1586 * driver is responsible for maintaining a reference count on the
1587 * number of outstanding references to its softc (e.g. from
1588 * processing hanging in a read or write). The detach method of the
1589 * driver decrements this counter and flags in the softc that the
1590 * driver is dying and then wakes any sleepers. It then sleeps on the
1591 * softc. Each place that can sleep must maintain the reference
1592 * count. When the reference count drops to -1 (0 is the normal value
1593 * of the reference count) then a wakeup on the softc is performed
1594 * signaling to the detach waiter that all references are gone.
1595 */
1596
1597 /*
1598 * Called from process context when we discover that a port has
1599 * been disconnected.
1600 */
1601 int
1602 usb_disconnect_port(struct usbd_port *up, device_t parent, int flags)
1603 {
1604 struct usbd_device *dev = up->up_dev;
1605 device_t subdev;
1606 char subdevname[16];
1607 const char *hubname = device_xname(parent);
1608 int i, rc;
1609
1610 USBHIST_FUNC();
1611 USBHIST_CALLARGS(usbdebug, "up=%#jx dev=%#jx port=%jd",
1612 (uintptr_t)up, (uintptr_t)dev, up->up_portno, 0);
1613
1614 if (dev == NULL) {
1615 return 0;
1616 }
1617
1618 if (dev->ud_subdevlen > 0) {
1619 DPRINTFN(3, "disconnect subdevs", 0, 0, 0, 0);
1620 for (i = 0; i < dev->ud_subdevlen; i++) {
1621 if ((subdev = dev->ud_subdevs[i]) == NULL)
1622 continue;
1623 strlcpy(subdevname, device_xname(subdev),
1624 sizeof(subdevname));
1625 KERNEL_LOCK(1, curlwp);
1626 rc = config_detach(subdev, flags);
1627 KERNEL_UNLOCK_ONE(curlwp);
1628 if (rc != 0)
1629 return rc;
1630 printf("%s: at %s", subdevname, hubname);
1631 if (up->up_portno != 0)
1632 printf(" port %d", up->up_portno);
1633 printf(" (addr %d) disconnected\n", dev->ud_addr);
1634 }
1635 KASSERT(!dev->ud_nifaces_claimed);
1636 }
1637
1638 mutex_enter(dev->ud_bus->ub_lock);
1639 dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL;
1640 up->up_dev = NULL;
1641 mutex_exit(dev->ud_bus->ub_lock);
1642
1643 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1644
1645 usb_free_device(dev);
1646
1647 return 0;
1648 }
1649