usb_subr.c revision 1.259 1 /* $NetBSD: usb_subr.c,v 1.259 2021/06/12 15:41:22 riastradh 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.259 2021/06/12 15:41:22 riastradh 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 static void
407 usbd_iface_init(struct usbd_device *dev, int ifaceidx)
408 {
409 struct usbd_interface *ifc = &dev->ud_ifaces[ifaceidx];
410
411 memset(ifc, 0, sizeof(*ifc));
412
413 ifc->ui_dev = dev;
414 ifc->ui_idesc = NULL;
415 ifc->ui_index = 0;
416 ifc->ui_altindex = 0;
417 ifc->ui_endpoints = NULL;
418 ifc->ui_priv = NULL;
419 LIST_INIT(&ifc->ui_pipes);
420 mutex_init(&ifc->ui_pipelock, MUTEX_DEFAULT, IPL_NONE);
421 }
422
423 static void
424 usbd_iface_fini(struct usbd_device *dev, int ifaceidx)
425 {
426 struct usbd_interface *ifc = &dev->ud_ifaces[ifaceidx];
427
428 KASSERT(ifc->ui_dev == dev);
429 KASSERT(ifc->ui_idesc == NULL);
430 KASSERT(ifc->ui_index == 0);
431 KASSERT(ifc->ui_altindex == 0);
432 KASSERT(ifc->ui_endpoints == NULL);
433 KASSERT(ifc->ui_priv == NULL);
434 KASSERT(LIST_EMPTY(&ifc->ui_pipes));
435
436 mutex_destroy(&ifc->ui_pipelock);
437 }
438
439 usbd_status
440 usbd_fill_iface_data(struct usbd_device *dev, int ifaceidx, int altidx)
441 {
442 USBHIST_FUNC();
443 USBHIST_CALLARGS(usbdebug, "ifaceidx=%jd altidx=%jd",
444 ifaceidx, altidx, 0, 0);
445 struct usbd_interface *ifc = &dev->ud_ifaces[ifaceidx];
446 usb_interface_descriptor_t *idesc;
447 struct usbd_endpoint *endpoints;
448 char *p, *end;
449 int endpt, nendpt;
450
451 KASSERT(ifc->ui_dev == dev);
452 KASSERT(LIST_EMPTY(&ifc->ui_pipes));
453
454 idesc = usbd_find_idesc(dev->ud_cdesc, ifaceidx, altidx);
455 if (idesc == NULL)
456 return USBD_INVAL;
457
458 nendpt = idesc->bNumEndpoints;
459 DPRINTFN(4, "found idesc nendpt=%jd", nendpt, 0, 0, 0);
460 if (nendpt != 0) {
461 endpoints = kmem_alloc(nendpt * sizeof(struct usbd_endpoint),
462 KM_SLEEP);
463 } else
464 endpoints = NULL;
465
466 p = (char *)idesc + idesc->bLength;
467 end = (char *)dev->ud_cdesc + UGETW(dev->ud_cdesc->wTotalLength);
468 #define ed ((usb_endpoint_descriptor_t *)p)
469 for (endpt = 0; endpt < nendpt; endpt++) {
470 DPRINTFN(10, "endpt=%jd", endpt, 0, 0, 0);
471 for (; p < end; p += ed->bLength) {
472 DPRINTFN(10, "p=%#jx end=%#jx len=%jd type=%jd",
473 (uintptr_t)p, (uintptr_t)end, ed->bLength,
474 ed->bDescriptorType);
475 if (p + ed->bLength <= end &&
476 ed->bLength >= USB_ENDPOINT_DESCRIPTOR_SIZE &&
477 ed->bDescriptorType == UDESC_ENDPOINT)
478 goto found;
479 if (ed->bLength == 0 ||
480 ed->bDescriptorType == UDESC_INTERFACE)
481 break;
482 }
483 /* passed end, or bad desc */
484 if (p < end) {
485 if (ed->bLength == 0) {
486 printf("%s: bad descriptor: 0 length\n",
487 __func__);
488 } else {
489 printf("%s: bad descriptor: iface desc\n",
490 __func__);
491 }
492 } else {
493 printf("%s: no desc found\n", __func__);
494 }
495 goto bad;
496 found:
497 endpoints[endpt].ue_edesc = ed;
498 if (dev->ud_speed == USB_SPEED_HIGH) {
499 u_int mps;
500 /* Control and bulk endpoints have max packet limits. */
501 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
502 case UE_CONTROL:
503 mps = USB_2_MAX_CTRL_PACKET;
504 goto check;
505 case UE_BULK:
506 mps = USB_2_MAX_BULK_PACKET;
507 check:
508 if (UGETW(ed->wMaxPacketSize) != mps) {
509 USETW(ed->wMaxPacketSize, mps);
510 #ifdef DIAGNOSTIC
511 printf("usbd_fill_iface_data: bad max "
512 "packet size\n");
513 #endif
514 }
515 break;
516 default:
517 break;
518 }
519 }
520 endpoints[endpt].ue_refcnt = 0;
521 endpoints[endpt].ue_toggle = 0;
522 p += ed->bLength;
523 }
524 #undef ed
525
526 /* Success! Free the old endpoints and commit the changes. */
527 if (ifc->ui_endpoints) {
528 kmem_free(ifc->ui_endpoints, (sizeof(ifc->ui_endpoints[0]) *
529 ifc->ui_idesc->bNumEndpoints));
530 }
531
532 ifc->ui_idesc = idesc;
533 ifc->ui_index = ifaceidx;
534 ifc->ui_altindex = altidx;
535 ifc->ui_endpoints = endpoints;
536
537 return USBD_NORMAL_COMPLETION;
538
539 bad:
540 if (endpoints)
541 kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
542 return USBD_INVAL;
543 }
544
545 Static void
546 usbd_free_iface_data(struct usbd_device *dev, int ifcno)
547 {
548 struct usbd_interface *ifc = &dev->ud_ifaces[ifcno];
549
550 KASSERT(ifc->ui_dev == dev);
551 KASSERT(ifc->ui_idesc != NULL);
552 KASSERT(LIST_EMPTY(&ifc->ui_pipes));
553
554 if (ifc->ui_endpoints) {
555 int nendpt = ifc->ui_idesc->bNumEndpoints;
556 size_t sz = nendpt * sizeof(struct usbd_endpoint);
557 kmem_free(ifc->ui_endpoints, sz);
558 ifc->ui_endpoints = NULL;
559 }
560
561 ifc->ui_altindex = 0;
562 ifc->ui_index = 0;
563 ifc->ui_idesc = NULL;
564 }
565
566 usbd_status
567 usbd_set_config_no(struct usbd_device *dev, int no, int msg)
568 {
569 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "%jd", no, 0, 0, 0);
570 usb_config_descriptor_t cd;
571 usbd_status err;
572 int index;
573
574 if (no == USB_UNCONFIG_NO)
575 return usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg);
576
577 /* Figure out what config index to use. */
578 for (index = 0; index < dev->ud_ddesc.bNumConfigurations; index++) {
579 err = usbd_get_config_desc(dev, index, &cd);
580 if (err)
581 return err;
582 if (cd.bConfigurationValue == no)
583 return usbd_set_config_index(dev, index, msg);
584 }
585 return USBD_INVAL;
586 }
587
588 usbd_status
589 usbd_set_config_index(struct usbd_device *dev, int index, int msg)
590 {
591 USBHIST_FUNC();
592 USBHIST_CALLARGS(usbdebug, "dev=%#jx index=%jd",
593 (uintptr_t)dev, index, 0, 0);
594 usb_config_descriptor_t cd, *cdp;
595 usb_bos_descriptor_t *bdp = NULL;
596 usbd_status err;
597 int i, ifcidx, nifc, len, selfpowered, power;
598
599
600 if (index >= dev->ud_ddesc.bNumConfigurations &&
601 index != USB_UNCONFIG_INDEX) {
602 /* panic? */
603 printf("usbd_set_config_index: illegal index\n");
604 return USBD_INVAL;
605 }
606
607 /* XXX check that all interfaces are idle */
608 if (dev->ud_config != USB_UNCONFIG_NO) {
609 DPRINTF("free old config", 0, 0, 0, 0);
610 /* Free all configuration data structures. */
611 nifc = dev->ud_cdesc->bNumInterface;
612 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
613 usbd_free_iface_data(dev, ifcidx);
614 usbd_iface_fini(dev, ifcidx);
615 }
616 kmem_free(dev->ud_ifaces, nifc * sizeof(struct usbd_interface));
617 kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength));
618 if (dev->ud_bdesc != NULL)
619 kmem_free(dev->ud_bdesc,
620 UGETW(dev->ud_bdesc->wTotalLength));
621 dev->ud_ifaces = NULL;
622 dev->ud_cdesc = NULL;
623 dev->ud_bdesc = NULL;
624 dev->ud_config = USB_UNCONFIG_NO;
625 }
626
627 if (index == USB_UNCONFIG_INDEX) {
628 /* We are unconfiguring the device, so leave unallocated. */
629 DPRINTF("set config 0", 0, 0, 0, 0);
630 err = usbd_set_config(dev, USB_UNCONFIG_NO);
631 if (err) {
632 DPRINTF("setting config=0 failed, err = %jd", err,
633 0, 0, 0);
634 }
635 return err;
636 }
637
638 /* Get the short descriptor. */
639 err = usbd_get_config_desc(dev, index, &cd);
640 if (err) {
641 DPRINTF("get_config_desc=%jd", err, 0, 0, 0);
642 return err;
643 }
644 len = UGETW(cd.wTotalLength);
645 if (len < USB_CONFIG_DESCRIPTOR_SIZE) {
646 DPRINTF("empty short descriptor", 0, 0, 0, 0);
647 return USBD_INVAL;
648 }
649 cdp = kmem_alloc(len, KM_SLEEP);
650
651 /* Get the full descriptor. Try a few times for slow devices. */
652 for (i = 0; i < 3; i++) {
653 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
654 if (!err)
655 break;
656 usbd_delay_ms(dev, 200);
657 }
658 if (err) {
659 DPRINTF("get_desc=%jd", err, 0, 0, 0);
660 goto bad;
661 }
662 if (cdp->bDescriptorType != UDESC_CONFIG) {
663 DPRINTF("bad desc %jd", cdp->bDescriptorType, 0, 0, 0);
664 err = USBD_INVAL;
665 goto bad;
666 }
667 if (UGETW(cdp->wTotalLength) != UGETW(cd.wTotalLength)) {
668 DPRINTF("bad len %jd", UGETW(cdp->wTotalLength), 0, 0, 0);
669 err = USBD_INVAL;
670 goto bad;
671 }
672
673 if (USB_IS_SS(dev->ud_speed)) {
674 usb_bos_descriptor_t bd;
675
676 /* get short bos desc */
677 err = usbd_get_bos_desc(dev, index, &bd);
678 if (!err) {
679 int blen = UGETW(bd.wTotalLength);
680 if (blen < USB_BOS_DESCRIPTOR_SIZE) {
681 DPRINTF("empty bos descriptor", 0, 0, 0, 0);
682 err = USBD_INVAL;
683 goto bad;
684 }
685 bdp = kmem_alloc(blen, KM_SLEEP);
686
687 /* Get the full desc */
688 for (i = 0; i < 3; i++) {
689 err = usbd_get_desc(dev, UDESC_BOS, index, blen,
690 bdp);
691 if (!err)
692 break;
693 usbd_delay_ms(dev, 200);
694 }
695 if (err || bdp->bDescriptorType != UDESC_BOS ||
696 UGETW(bdp->wTotalLength) != UGETW(bd.wTotalLength)) {
697 DPRINTF("error %jd or bad desc %jd", err,
698 bdp->bDescriptorType, 0, 0);
699 kmem_free(bdp, blen);
700 bdp = NULL;
701 }
702 }
703 }
704 dev->ud_bdesc = bdp;
705
706 /*
707 * Figure out if the device is self or bus powered.
708 */
709 #if 0 /* XXX various devices don't report the power state correctly */
710 selfpowered = 0;
711 err = usbd_get_device_status(dev, &ds);
712 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
713 selfpowered = 1;
714 #endif
715 /*
716 * Use the power state in the configuration we are going
717 * to set. This doesn't necessarily reflect the actual
718 * power state of the device; the driver can control this
719 * by choosing the appropriate configuration.
720 */
721 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
722
723 DPRINTF("addr %jd cno=%jd attr=0x%02jx, selfpowered=%jd",
724 dev->ud_addr, cdp->bConfigurationValue, cdp->bmAttributes,
725 selfpowered);
726 DPRINTF("max power=%jd", cdp->bMaxPower * 2, 0, 0, 0);
727
728 /* Check if we have enough power. */
729 #if 0 /* this is a no-op, see above */
730 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
731 if (msg)
732 printf("%s: device addr %d (config %d): "
733 "can't set self powered configuration\n",
734 device_xname(dev->ud_bus->bdev), dev->ud_addr,
735 cdp->bConfigurationValue);
736 err = USBD_NO_POWER;
737 goto bad;
738 }
739 #endif
740 #ifdef USB_DEBUG
741 if (dev->ud_powersrc == NULL) {
742 DPRINTF("No power source?", 0, 0, 0, 0);
743 err = USBD_IOERROR;
744 goto bad;
745 }
746 #endif
747 power = cdp->bMaxPower * 2;
748 if (power > dev->ud_powersrc->up_power) {
749 DPRINTF("power exceeded %jd %jd", power,
750 dev->ud_powersrc->up_power, 0, 0);
751 /* XXX print nicer message. */
752 if (msg)
753 printf("%s: device addr %d (config %d) exceeds power "
754 "budget, %d mA > %d mA\n",
755 device_xname(dev->ud_bus->ub_usbctl), dev->ud_addr,
756 cdp->bConfigurationValue,
757 power, dev->ud_powersrc->up_power);
758 err = USBD_NO_POWER;
759 goto bad;
760 }
761 dev->ud_power = power;
762 dev->ud_selfpowered = selfpowered;
763
764 /* Set the actual configuration value. */
765 DPRINTF("set config %jd", cdp->bConfigurationValue, 0, 0, 0);
766 err = usbd_set_config(dev, cdp->bConfigurationValue);
767 if (err) {
768 DPRINTF("setting config=%jd failed, error=%jd",
769 cdp->bConfigurationValue, err, 0, 0);
770 goto bad;
771 }
772
773 /* Allocate and fill interface data. */
774 nifc = cdp->bNumInterface;
775 if (nifc == 0) {
776 DPRINTF("no interfaces", 0, 0, 0, 0);
777 err = USBD_INVAL;
778 goto bad;
779 }
780 dev->ud_ifaces = kmem_alloc(nifc * sizeof(struct usbd_interface),
781 KM_SLEEP);
782 DPRINTFN(5, "dev=%#jx cdesc=%#jx", (uintptr_t)dev, (uintptr_t)cdp,
783 0, 0);
784 dev->ud_cdesc = cdp;
785 dev->ud_config = cdp->bConfigurationValue;
786 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
787 usbd_iface_init(dev, ifcidx);
788 err = usbd_fill_iface_data(dev, ifcidx, 0);
789 if (err) {
790 while (--ifcidx >= 0) {
791 usbd_free_iface_data(dev, ifcidx);
792 usbd_iface_fini(dev, ifcidx);
793 }
794 kmem_free(dev->ud_ifaces,
795 nifc * sizeof(struct usbd_interface));
796 dev->ud_ifaces = NULL;
797 goto bad;
798 }
799 }
800
801 return USBD_NORMAL_COMPLETION;
802
803 bad:
804 /* XXX Use usbd_set_config() to reset the config? */
805 /* XXX Should we forbid USB_UNCONFIG_NO from bConfigurationValue? */
806 dev->ud_config = USB_UNCONFIG_NO;
807 kmem_free(cdp, len);
808 dev->ud_cdesc = NULL;
809 if (bdp != NULL) {
810 kmem_free(bdp, UGETW(bdp->wTotalLength));
811 dev->ud_bdesc = NULL;
812 }
813 return err;
814 }
815
816 /* XXX add function for alternate settings */
817
818 usbd_status
819 usbd_setup_pipe(struct usbd_device *dev, struct usbd_interface *iface,
820 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe)
821 {
822 return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0);
823 }
824
825 usbd_status
826 usbd_setup_pipe_flags(struct usbd_device *dev, struct usbd_interface *iface,
827 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe, uint8_t flags)
828 {
829 USBHIST_FUNC();
830 USBHIST_CALLARGS(usbdebug, "dev=%#jx addr=%jd iface=%#jx ep=%#jx",
831 (uintptr_t)dev, dev->ud_addr, (uintptr_t)iface, (uintptr_t)ep);
832 struct usbd_pipe *p;
833 usbd_status err;
834
835 err = usbd_endpoint_acquire(dev, ep, flags & USBD_EXCLUSIVE_USE);
836 if (err)
837 return err;
838
839 p = kmem_alloc(dev->ud_bus->ub_pipesize, KM_SLEEP);
840 DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0);
841 p->up_dev = dev;
842 p->up_iface = iface;
843 p->up_endpoint = ep;
844 p->up_intrxfer = NULL;
845 p->up_running = 0;
846 p->up_aborting = 0;
847 p->up_serialise = true;
848 p->up_repeat = 0;
849 p->up_interval = ival;
850 p->up_flags = flags;
851 SIMPLEQ_INIT(&p->up_queue);
852
853 if (iface) {
854 mutex_enter(&iface->ui_pipelock);
855 LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
856 mutex_exit(&iface->ui_pipelock);
857 }
858
859 err = dev->ud_bus->ub_methods->ubm_open(p);
860 if (err) {
861 DPRINTF("endpoint=%#jx failed, error=%jd",
862 (uintptr_t)ep->ue_edesc->bEndpointAddress, err, 0, 0);
863 if (iface) {
864 mutex_enter(&iface->ui_pipelock);
865 LIST_REMOVE(p, up_next);
866 mutex_exit(&iface->ui_pipelock);
867 }
868 kmem_free(p, dev->ud_bus->ub_pipesize);
869 usbd_endpoint_release(dev, ep);
870 return err;
871 }
872
873 KASSERT(p->up_methods->upm_start || p->up_serialise == false);
874
875 usb_init_task(&p->up_async_task, usbd_clear_endpoint_stall_task, p,
876 USB_TASKQ_MPSAFE);
877 DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0);
878 *pipe = p;
879 return USBD_NORMAL_COMPLETION;
880 }
881
882 usbd_status
883 usbd_endpoint_acquire(struct usbd_device *dev, struct usbd_endpoint *ep,
884 int flags)
885 {
886 usbd_status err;
887
888 mutex_enter(dev->ud_bus->ub_lock);
889 if (ep->ue_refcnt == INT_MAX) {
890 err = USBD_IN_USE; /* XXX rule out or switch to 64-bit */
891 } else if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt) {
892 err = USBD_IN_USE;
893 } else {
894 ep->ue_refcnt++;
895 err = 0;
896 }
897 mutex_exit(dev->ud_bus->ub_lock);
898
899 return err;
900 }
901
902 void
903 usbd_endpoint_release(struct usbd_device *dev, struct usbd_endpoint *ep)
904 {
905
906 mutex_enter(dev->ud_bus->ub_lock);
907 KASSERT(ep->ue_refcnt);
908 ep->ue_refcnt--;
909 mutex_exit(dev->ud_bus->ub_lock);
910 }
911
912 /* Abort and close the device control pipe. */
913 void
914 usbd_kill_pipe(struct usbd_pipe *pipe)
915 {
916
917 usbd_abort_pipe(pipe);
918 usbd_close_pipe(pipe);
919 }
920
921 int
922 usbd_getnewaddr(struct usbd_bus *bus)
923 {
924 int addr;
925
926 for (addr = 1; addr < USB_MAX_DEVICES; addr++) {
927 size_t dindex = usb_addr2dindex(addr);
928 if (bus->ub_devices[dindex] == NULL)
929 return addr;
930 }
931 return -1;
932 }
933
934 usbd_status
935 usbd_attach_roothub(device_t parent, struct usbd_device *dev)
936 {
937 struct usb_attach_arg uaa;
938 usb_device_descriptor_t *dd = &dev->ud_ddesc;
939 device_t dv;
940
941 uaa.uaa_device = dev;
942 uaa.uaa_usegeneric = 0;
943 uaa.uaa_port = 0;
944 uaa.uaa_vendor = UGETW(dd->idVendor);
945 uaa.uaa_product = UGETW(dd->idProduct);
946 uaa.uaa_release = UGETW(dd->bcdDevice);
947 uaa.uaa_class = dd->bDeviceClass;
948 uaa.uaa_subclass = dd->bDeviceSubClass;
949 uaa.uaa_proto = dd->bDeviceProtocol;
950
951 KERNEL_LOCK(1, curlwp);
952 dv = config_found(parent, &uaa, NULL,
953 CFARG_IATTR, "usbroothubif",
954 CFARG_EOL);
955 KERNEL_UNLOCK_ONE(curlwp);
956 if (dv) {
957 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP);
958 dev->ud_subdevs[0] = dv;
959 dev->ud_subdevlen = 1;
960 }
961 return USBD_NORMAL_COMPLETION;
962 }
963
964 static void
965 usbd_properties(device_t dv, struct usbd_device *dev)
966 {
967 usb_device_descriptor_t *dd = &dev->ud_ddesc;
968 prop_dictionary_t dict = device_properties(dv);
969 int class, subclass, release, proto, vendor, product;
970
971 class = dd->bDeviceClass;
972 subclass = dd->bDeviceSubClass;
973 release = UGETW(dd->bcdDevice);
974 proto = dd->bDeviceProtocol;
975 vendor = UGETW(dd->idVendor);
976 product = UGETW(dd->idProduct);
977
978 prop_dictionary_set_uint16(dict, "class", class);
979 prop_dictionary_set_uint16(dict, "subclass", subclass);
980 prop_dictionary_set_uint16(dict, "release", release);
981 prop_dictionary_set_uint16(dict, "proto", proto);
982 prop_dictionary_set_uint16(dict, "vendor", vendor);
983 prop_dictionary_set_uint16(dict, "product", product);
984
985 if (dev->ud_vendor) {
986 prop_dictionary_set_string(dict,
987 "vendor-string", dev->ud_vendor);
988 }
989 if (dev->ud_product) {
990 prop_dictionary_set_string(dict,
991 "product-string", dev->ud_product);
992 }
993 if (dev->ud_serial) {
994 prop_dictionary_set_string(dict,
995 "serialnumber", dev->ud_serial);
996 }
997 }
998
999 static usbd_status
1000 usbd_attachwholedevice(device_t parent, struct usbd_device *dev, int port,
1001 int usegeneric)
1002 {
1003 struct usb_attach_arg uaa;
1004 usb_device_descriptor_t *dd = &dev->ud_ddesc;
1005 device_t dv;
1006 int dlocs[USBDEVIFCF_NLOCS];
1007
1008 uaa.uaa_device = dev;
1009 uaa.uaa_usegeneric = usegeneric;
1010 uaa.uaa_port = port;
1011 uaa.uaa_vendor = UGETW(dd->idVendor);
1012 uaa.uaa_product = UGETW(dd->idProduct);
1013 uaa.uaa_release = UGETW(dd->bcdDevice);
1014 uaa.uaa_class = dd->bDeviceClass;
1015 uaa.uaa_subclass = dd->bDeviceSubClass;
1016 uaa.uaa_proto = dd->bDeviceProtocol;
1017
1018 dlocs[USBDEVIFCF_PORT] = uaa.uaa_port;
1019 dlocs[USBDEVIFCF_VENDOR] = uaa.uaa_vendor;
1020 dlocs[USBDEVIFCF_PRODUCT] = uaa.uaa_product;
1021 dlocs[USBDEVIFCF_RELEASE] = uaa.uaa_release;
1022 /* the rest is historical ballast */
1023 dlocs[USBDEVIFCF_CONFIGURATION] = -1;
1024 dlocs[USBDEVIFCF_INTERFACE] = -1;
1025
1026 KERNEL_LOCK(1, curlwp);
1027 config_pending_incr(parent);
1028 dv = config_found(parent, &uaa, usbd_print,
1029 CFARG_SUBMATCH, config_stdsubmatch,
1030 CFARG_IATTR, "usbdevif",
1031 CFARG_LOCATORS, dlocs,
1032 CFARG_EOL);
1033 KERNEL_UNLOCK_ONE(curlwp);
1034 if (dv) {
1035 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP);
1036 dev->ud_subdevs[0] = dv;
1037 dev->ud_subdevlen = 1;
1038 dev->ud_nifaces_claimed = 1; /* XXX */
1039 usbd_properties(dv, dev);
1040 }
1041 config_pending_decr(parent);
1042 return USBD_NORMAL_COMPLETION;
1043 }
1044
1045 static usbd_status
1046 usbd_attachinterfaces(device_t parent, struct usbd_device *dev,
1047 int port, const int *locators)
1048 {
1049 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1050 struct usbif_attach_arg uiaa;
1051 int ilocs[USBIFIFCF_NLOCS];
1052 usb_device_descriptor_t *dd = &dev->ud_ddesc;
1053 int nifaces;
1054 struct usbd_interface **ifaces;
1055 int i, j, loc;
1056 device_t dv;
1057
1058 /* Needed for access to dev->ud_subdevs. */
1059 KASSERT(KERNEL_LOCKED_P());
1060
1061 nifaces = dev->ud_cdesc->bNumInterface;
1062 ifaces = kmem_zalloc(nifaces * sizeof(*ifaces), KM_SLEEP);
1063 for (i = 0; i < nifaces; i++) {
1064 if (!dev->ud_subdevs[i]) {
1065 ifaces[i] = &dev->ud_ifaces[i];
1066 }
1067 DPRINTF("interface %jd %#jx", i, (uintptr_t)ifaces[i], 0, 0);
1068 }
1069
1070
1071 uiaa.uiaa_device = dev;
1072 uiaa.uiaa_port = port;
1073 uiaa.uiaa_vendor = UGETW(dd->idVendor);
1074 uiaa.uiaa_product = UGETW(dd->idProduct);
1075 uiaa.uiaa_release = UGETW(dd->bcdDevice);
1076 uiaa.uiaa_configno = dev->ud_cdesc->bConfigurationValue;
1077 uiaa.uiaa_ifaces = ifaces;
1078 uiaa.uiaa_nifaces = nifaces;
1079 ilocs[USBIFIFCF_PORT] = uiaa.uiaa_port;
1080 ilocs[USBIFIFCF_VENDOR] = uiaa.uiaa_vendor;
1081 ilocs[USBIFIFCF_PRODUCT] = uiaa.uiaa_product;
1082 ilocs[USBIFIFCF_RELEASE] = uiaa.uiaa_release;
1083 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.uiaa_configno;
1084
1085 for (i = 0; i < nifaces; i++) {
1086 if (!ifaces[i]) {
1087 DPRINTF("interface %jd claimed", i, 0, 0, 0);
1088 continue; /* interface already claimed */
1089 }
1090 uiaa.uiaa_iface = ifaces[i];
1091 uiaa.uiaa_class = ifaces[i]->ui_idesc->bInterfaceClass;
1092 uiaa.uiaa_subclass = ifaces[i]->ui_idesc->bInterfaceSubClass;
1093 uiaa.uiaa_proto = ifaces[i]->ui_idesc->bInterfaceProtocol;
1094 uiaa.uiaa_ifaceno = ifaces[i]->ui_idesc->bInterfaceNumber;
1095
1096 DPRINTF("searching for interface %jd...", i, 0, 0, 0);
1097 DPRINTF("class %jx subclass %jx proto %jx ifaceno %jd",
1098 uiaa.uiaa_class, uiaa.uiaa_subclass, uiaa.uiaa_proto,
1099 uiaa.uiaa_ifaceno);
1100 ilocs[USBIFIFCF_INTERFACE] = uiaa.uiaa_ifaceno;
1101 if (locators != NULL) {
1102 loc = locators[USBIFIFCF_CONFIGURATION];
1103 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT &&
1104 loc != uiaa.uiaa_configno)
1105 continue;
1106 loc = locators[USBIFIFCF_INTERFACE];
1107 if (loc != USBIFIFCF_INTERFACE_DEFAULT &&
1108 loc != uiaa.uiaa_ifaceno)
1109 continue;
1110 }
1111 KERNEL_LOCK(1, curlwp);
1112 dv = config_found(parent, &uiaa, usbd_ifprint,
1113 CFARG_SUBMATCH, config_stdsubmatch,
1114 CFARG_IATTR, "usbifif",
1115 CFARG_LOCATORS, ilocs,
1116 CFARG_EOL);
1117 KERNEL_UNLOCK_ONE(curlwp);
1118 if (!dv)
1119 continue;
1120
1121 usbd_properties(dv, dev);
1122
1123 /* claim */
1124 ifaces[i] = NULL;
1125 /* account for ifaces claimed by the driver behind our back */
1126 for (j = 0; j < nifaces; j++) {
1127
1128 if (!ifaces[j] && !dev->ud_subdevs[j]) {
1129 DPRINTF("interface %jd claimed behind our back",
1130 j, 0, 0, 0);
1131 dev->ud_subdevs[j] = dv;
1132 dev->ud_nifaces_claimed++;
1133 }
1134 }
1135 }
1136
1137 kmem_free(ifaces, nifaces * sizeof(*ifaces));
1138 return USBD_NORMAL_COMPLETION;
1139 }
1140
1141 usbd_status
1142 usbd_probe_and_attach(device_t parent, struct usbd_device *dev,
1143 int port, int addr)
1144 {
1145 USBHIST_FUNC();
1146 USBHIST_CALLARGS(usbdebug, "trying device specific drivers", 0, 0, 0, 0);
1147 usb_device_descriptor_t *dd = &dev->ud_ddesc;
1148 int confi, nifaces;
1149 usbd_status err;
1150
1151 KASSERT(KERNEL_LOCKED_P());
1152
1153 /* First try with device specific drivers. */
1154 err = usbd_attachwholedevice(parent, dev, port, 0);
1155 if (dev->ud_nifaces_claimed || err)
1156 return err;
1157 DPRINTF("no device specific driver found", 0, 0, 0, 0);
1158
1159 DPRINTF("looping over %jd configurations", dd->bNumConfigurations,
1160 0, 0, 0);
1161 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
1162 DPRINTFN(1, "trying config idx=%jd", confi, 0, 0, 0);
1163 err = usbd_set_config_index(dev, confi, 1);
1164 if (err) {
1165 DPRINTF("port %jd, set config at addr %jd failed, "
1166 "error=%jd", port, addr, err, 0);
1167 printf("%s: port %d, set config at addr %d failed\n",
1168 device_xname(parent), port, addr);
1169 return err;
1170 }
1171 nifaces = dev->ud_cdesc->bNumInterface;
1172 dev->ud_subdevs = kmem_zalloc(nifaces * sizeof(device_t),
1173 KM_SLEEP);
1174 dev->ud_subdevlen = nifaces;
1175
1176 err = usbd_attachinterfaces(parent, dev, port, NULL);
1177
1178 if (dev->ud_subdevs && dev->ud_nifaces_claimed == 0) {
1179 kmem_free(dev->ud_subdevs,
1180 dev->ud_subdevlen * sizeof(device_t));
1181 dev->ud_subdevs = 0;
1182 dev->ud_subdevlen = 0;
1183 }
1184 if (dev->ud_nifaces_claimed || err)
1185 return err;
1186 }
1187 /* No interfaces were attached in any of the configurations. */
1188
1189 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
1190 usbd_set_config_index(dev, 0, 0);
1191
1192 DPRINTF("no interface drivers found", 0, 0, 0, 0);
1193
1194 /* Finally try the generic driver. */
1195 err = usbd_attachwholedevice(parent, dev, port, 1);
1196
1197 /*
1198 * The generic attach failed, but leave the device as it is.
1199 * We just did not find any drivers, that's all. The device is
1200 * fully operational and not harming anyone.
1201 */
1202 DPRINTF("generic attach failed", 0, 0, 0, 0);
1203
1204 return USBD_NORMAL_COMPLETION;
1205 }
1206
1207 /**
1208 * Called from uhub_rescan(). usbd_new_device() for the target dev must be
1209 * called before calling this.
1210 */
1211 usbd_status
1212 usbd_reattach_device(device_t parent, struct usbd_device *dev,
1213 int port, const int *locators)
1214 {
1215 int i, loc;
1216
1217 USBHIST_FUNC();
1218 USBHIST_CALLARGS(usbdebug, "uhub%jd port=%jd",
1219 device_unit(parent), port, 0, 0);
1220
1221 KASSERT(KERNEL_LOCKED_P());
1222
1223 if (locators != NULL) {
1224 loc = locators[USBIFIFCF_PORT];
1225 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port)
1226 return USBD_NORMAL_COMPLETION;
1227 loc = locators[USBIFIFCF_VENDOR];
1228 if (loc != USBIFIFCF_VENDOR_DEFAULT &&
1229 loc != UGETW(dev->ud_ddesc.idVendor))
1230 return USBD_NORMAL_COMPLETION;
1231 loc = locators[USBIFIFCF_PRODUCT];
1232 if (loc != USBIFIFCF_PRODUCT_DEFAULT &&
1233 loc != UGETW(dev->ud_ddesc.idProduct))
1234 return USBD_NORMAL_COMPLETION;
1235 loc = locators[USBIFIFCF_RELEASE];
1236 if (loc != USBIFIFCF_RELEASE_DEFAULT &&
1237 loc != UGETW(dev->ud_ddesc.bcdDevice))
1238 return USBD_NORMAL_COMPLETION;
1239 }
1240 if (dev->ud_subdevlen == 0) {
1241 /* XXX: check USBIFIFCF_CONFIGURATION and
1242 * USBIFIFCF_INTERFACE too */
1243 return usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
1244 } else if (dev->ud_subdevlen != dev->ud_cdesc->bNumInterface) {
1245 /* device-specific or generic driver is already attached. */
1246 return USBD_NORMAL_COMPLETION;
1247 }
1248 /* Does the device have unconfigured interfaces? */
1249 for (i = 0; i < dev->ud_subdevlen; i++) {
1250 if (dev->ud_subdevs[i] == NULL) {
1251 break;
1252 }
1253 }
1254 if (i >= dev->ud_subdevlen)
1255 return USBD_NORMAL_COMPLETION;
1256 return usbd_attachinterfaces(parent, dev, port, locators);
1257 }
1258
1259 /*
1260 * Called when a new device has been put in the powered state,
1261 * but not yet in the addressed state.
1262 * Get initial descriptor, set the address, get full descriptor,
1263 * and attach a driver.
1264 */
1265 usbd_status
1266 usbd_new_device(device_t parent, struct usbd_bus *bus, int depth, int speed,
1267 int port, struct usbd_port *up)
1268 {
1269 USBHIST_FUNC();
1270 USBHIST_CALLARGS(usbdebug, "bus=%#jx port=%jd depth=%jd speed=%jd",
1271 (uintptr_t)bus, port, depth, speed);
1272 struct usbd_device *dev, *adev;
1273 struct usbd_device *hub;
1274 usb_device_descriptor_t *dd;
1275 usb_port_status_t ps;
1276 usbd_status err;
1277 int addr;
1278 int i;
1279 int p;
1280
1281 KASSERT(KERNEL_LOCKED_P());
1282
1283 if (bus->ub_methods->ubm_newdev != NULL)
1284 return (bus->ub_methods->ubm_newdev)(parent, bus, depth, speed,
1285 port, up);
1286
1287 addr = usbd_getnewaddr(bus);
1288 if (addr < 0) {
1289 printf("%s: No free USB addresses, new device ignored.\n",
1290 device_xname(bus->ub_usbctl));
1291 return USBD_NO_ADDR;
1292 }
1293
1294 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
1295 dev->ud_bus = bus;
1296
1297 /* Set up default endpoint handle. */
1298 dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
1299
1300 /* Set up default endpoint descriptor. */
1301 dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1302 dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
1303 dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1304 dev->ud_ep0desc.bmAttributes = UE_CONTROL;
1305 /*
1306 * temporary, will be fixed after first descriptor fetch
1307 * (which uses 64 bytes so it shouldn't be less),
1308 * highspeed devices must support 64 byte packets anyway
1309 */
1310 if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL)
1311 USETW(dev->ud_ep0desc.wMaxPacketSize, 64);
1312 else
1313 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
1314
1315 dev->ud_ep0desc.bInterval = 0;
1316
1317 /* doesn't matter, just don't leave it uninitialized */
1318 dev->ud_ep0.ue_toggle = 0;
1319
1320 dev->ud_quirks = &usbd_no_quirk;
1321 dev->ud_addr = USB_START_ADDR;
1322 dev->ud_ddesc.bMaxPacketSize = 0;
1323 dev->ud_depth = depth;
1324 dev->ud_powersrc = up;
1325 dev->ud_myhub = up->up_parent;
1326
1327 up->up_dev = dev;
1328
1329 /* Locate port on upstream high speed hub */
1330 for (adev = dev, hub = up->up_parent;
1331 hub != NULL && hub->ud_speed != USB_SPEED_HIGH;
1332 adev = hub, hub = hub->ud_myhub)
1333 ;
1334 if (hub) {
1335 for (p = 1; p <= hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
1336 if (hub->ud_hub->uh_ports[p - 1].up_dev == adev) {
1337 dev->ud_myhsport =
1338 &hub->ud_hub->uh_ports[p - 1];
1339 goto found;
1340 }
1341 }
1342 panic("usbd_new_device: cannot find HS port");
1343 found:
1344 DPRINTFN(1, "high speed port %jd", p, 0, 0, 0);
1345 } else {
1346 dev->ud_myhsport = NULL;
1347 }
1348 dev->ud_speed = speed;
1349 dev->ud_langid = USBD_NOLANG;
1350 dev->ud_cookie.cookie = ++usb_cookie_no;
1351
1352 /* Establish the default pipe. */
1353 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1354 &dev->ud_pipe0, USBD_MPSAFE);
1355 if (err) {
1356 usbd_remove_device(dev, up);
1357 return err;
1358 }
1359
1360 dd = &dev->ud_ddesc;
1361 /* Try a few times in case the device is slow (i.e. outside specs.) */
1362 for (i = 0; i < 10; i++) {
1363 /* Get the first 8 bytes of the device descriptor. */
1364 err = usbd_get_initial_ddesc(dev, dd);
1365 if (!err)
1366 break;
1367 /*
1368 * The root hub can never fail to give the initial descriptor,
1369 * but assert it just in case.
1370 */
1371 KASSERT(up->up_parent);
1372 usbd_delay_ms(dev, 200);
1373 if ((i & 3) == 3)
1374 usbd_reset_port(up->up_parent, port, &ps);
1375 }
1376 if (err) {
1377 DPRINTF("addr=%jd, getting first desc failed: %jd", addr, err,
1378 0, 0);
1379 usbd_remove_device(dev, up);
1380 return err;
1381 }
1382
1383 /* Windows resets the port here, do likewise */
1384 if (up->up_parent)
1385 usbd_reset_port(up->up_parent, port, &ps);
1386
1387 if (speed == USB_SPEED_HIGH) {
1388 /* Max packet size must be 64 (sec 5.5.3). */
1389 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1390 #ifdef DIAGNOSTIC
1391 printf("usbd_new_device: addr=%d bad max packet "
1392 "size=%d. adjusting to %d.\n",
1393 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET);
1394 #endif
1395 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1396 }
1397 }
1398
1399 DPRINTF("adding unit addr=%jd, rev=%02jx, class=%jd, subclass=%jd",
1400 addr, UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass);
1401 DPRINTF("protocol=%jd, maxpacket=%jd, len=%jd, speed=%jd",
1402 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, dev->ud_speed);
1403
1404 if (dd->bDescriptorType != UDESC_DEVICE) {
1405 /* Illegal device descriptor */
1406 DPRINTF("illegal descriptor %jd", dd->bDescriptorType, 0, 0, 0);
1407 usbd_remove_device(dev, up);
1408 return USBD_INVAL;
1409 }
1410
1411 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1412 DPRINTF("bad length %jd", dd->bLength, 0, 0, 0);
1413 usbd_remove_device(dev, up);
1414 return USBD_INVAL;
1415 }
1416
1417 USETW(dev->ud_ep0desc.wMaxPacketSize, dd->bMaxPacketSize);
1418
1419 /* Re-establish the default pipe with the new MPS. */
1420 usbd_kill_pipe(dev->ud_pipe0);
1421 dev->ud_pipe0 = NULL;
1422 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1423 &dev->ud_pipe0, USBD_MPSAFE);
1424 if (err) {
1425 DPRINTF("setup default pipe failed err %jd", err, 0, 0, 0);
1426 usbd_remove_device(dev, up);
1427 return err;
1428 }
1429
1430 /* Set the address */
1431 DPRINTFN(5, "setting device address=%jd", addr, 0, 0, 0);
1432 err = usbd_set_address(dev, addr);
1433 if (err) {
1434 DPRINTF("set address %jd failed, err = %jd", addr, err, 0, 0);
1435 err = USBD_SET_ADDR_FAILED;
1436 usbd_remove_device(dev, up);
1437 return err;
1438 }
1439
1440 /* Allow device time to set new address */
1441 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1442 dev->ud_addr = addr; /* new device address now */
1443 bus->ub_devices[usb_addr2dindex(addr)] = dev;
1444
1445 /* Re-establish the default pipe with the new address. */
1446 usbd_kill_pipe(dev->ud_pipe0);
1447 dev->ud_pipe0 = NULL;
1448 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1449 &dev->ud_pipe0, USBD_MPSAFE);
1450 if (err) {
1451 DPRINTF("setup default pipe failed, err = %jd", err, 0, 0, 0);
1452 usbd_remove_device(dev, up);
1453 return err;
1454 }
1455
1456 err = usbd_reload_device_desc(dev);
1457 if (err) {
1458 DPRINTF("addr=%jd, getting full desc failed, err = %jd", addr,
1459 err, 0, 0);
1460 usbd_remove_device(dev, up);
1461 return err;
1462 }
1463
1464 /* Assume 100mA bus powered for now. Changed when configured. */
1465 dev->ud_power = USB_MIN_POWER;
1466 dev->ud_selfpowered = 0;
1467
1468 DPRINTF("new dev (addr %jd), dev=%#jx, parent=%#jx",
1469 addr, (uintptr_t)dev, (uintptr_t)parent, 0);
1470
1471 usbd_get_device_strings(dev);
1472
1473 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1474
1475 if (port == 0) { /* root hub */
1476 KASSERT(addr == 1);
1477 usbd_attach_roothub(parent, dev);
1478 return USBD_NORMAL_COMPLETION;
1479 }
1480
1481 err = usbd_probe_and_attach(parent, dev, port, addr);
1482 if (err) {
1483 usbd_remove_device(dev, up);
1484 return err;
1485 }
1486
1487 return USBD_NORMAL_COMPLETION;
1488 }
1489
1490 usbd_status
1491 usbd_reload_device_desc(struct usbd_device *dev)
1492 {
1493 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1494 usb_device_descriptor_t *udd = &dev->ud_ddesc;
1495 usbd_status err;
1496
1497 /* Get the full device descriptor. */
1498 err = usbd_get_device_desc(dev, udd);
1499 if (err)
1500 return err;
1501 if (udd->bDescriptorType != UDESC_DEVICE)
1502 return USBD_INVAL;
1503 if (udd->bLength < USB_DEVICE_DESCRIPTOR_SIZE)
1504 return USBD_INVAL;
1505
1506 DPRINTFN(15, "bLength %5ju", udd->bLength, 0, 0, 0);
1507 DPRINTFN(15, "bDescriptorType %5ju", udd->bDescriptorType, 0, 0, 0);
1508 DPRINTFN(15, "bcdUSB %2jx.%02jx", udd->bcdUSB[1],
1509 udd->bcdUSB[0], 0, 0);
1510 DPRINTFN(15, "bDeviceClass %5ju", udd->bDeviceClass, 0, 0, 0);
1511 DPRINTFN(15, "bDeviceSubClass %5ju", udd->bDeviceSubClass, 0, 0, 0);
1512 DPRINTFN(15, "bDeviceProtocol %5ju", udd->bDeviceProtocol, 0, 0, 0);
1513 DPRINTFN(15, "bMaxPacketSize0 %5ju", udd->bMaxPacketSize, 0, 0, 0);
1514 DPRINTFN(15, "idVendor 0x%02jx 0x%02jx",
1515 udd->idVendor[0],
1516 udd->idVendor[1], 0, 0);
1517 DPRINTFN(15, "idProduct 0x%02jx 0x%02jx",
1518 udd->idProduct[0],
1519 udd->idProduct[1], 0, 0);
1520 DPRINTFN(15, "bcdDevice %2jx.%02jx", udd->bcdDevice[1],
1521 udd->bcdDevice[0], 0, 0);
1522 DPRINTFN(15, "iManufacturer %5ju", udd->iManufacturer, 0, 0, 0);
1523 DPRINTFN(15, "iProduct %5ju", udd->iProduct, 0, 0, 0);
1524 DPRINTFN(15, "iSerial %5ju", udd->iSerialNumber, 0, 0, 0);
1525 DPRINTFN(15, "bNumConfigurations %5ju", udd->bNumConfigurations, 0, 0,
1526 0);
1527
1528 /* Figure out what's wrong with this device. */
1529 dev->ud_quirks = usbd_find_quirk(udd);
1530
1531 return USBD_NORMAL_COMPLETION;
1532 }
1533
1534 void
1535 usbd_remove_device(struct usbd_device *dev, struct usbd_port *up)
1536 {
1537
1538 USBHIST_FUNC();
1539 USBHIST_CALLARGS(usbdebug, "dev %#jx up %#jx",
1540 (uintptr_t)dev, (uintptr_t)up, 0, 0);
1541
1542 if (dev->ud_pipe0 != NULL)
1543 usbd_kill_pipe(dev->ud_pipe0);
1544 up->up_dev = NULL;
1545 dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL;
1546
1547 if (dev->ud_vendor != NULL) {
1548 kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN);
1549 }
1550 if (dev->ud_product != NULL) {
1551 kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN);
1552 }
1553 if (dev->ud_serial != NULL) {
1554 kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN);
1555 }
1556 kmem_free(dev, sizeof(*dev));
1557 }
1558
1559 int
1560 usbd_print(void *aux, const char *pnp)
1561 {
1562 struct usb_attach_arg *uaa = aux;
1563
1564 if (pnp) {
1565 #define USB_DEVINFO 1024
1566 char *devinfo;
1567 if (!uaa->uaa_usegeneric)
1568 return QUIET;
1569 devinfo = kmem_alloc(USB_DEVINFO, KM_SLEEP);
1570 usbd_devinfo(uaa->uaa_device, 1, devinfo, USB_DEVINFO);
1571 aprint_normal("%s, %s", devinfo, pnp);
1572 kmem_free(devinfo, USB_DEVINFO);
1573 }
1574 aprint_normal(" port %d", uaa->uaa_port);
1575 #if 0
1576 /*
1577 * It gets very crowded with these locators on the attach line.
1578 * They are not really needed since they are printed in the clear
1579 * by each driver.
1580 */
1581 if (uaa->uaa_vendor != UHUB_UNK_VENDOR)
1582 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor);
1583 if (uaa->uaa_product != UHUB_UNK_PRODUCT)
1584 aprint_normal(" product 0x%04x", uaa->uaa_product);
1585 if (uaa->uaa_release != UHUB_UNK_RELEASE)
1586 aprint_normal(" release 0x%04x", uaa->uaa_release);
1587 #endif
1588 return UNCONF;
1589 }
1590
1591 int
1592 usbd_ifprint(void *aux, const char *pnp)
1593 {
1594 struct usbif_attach_arg *uiaa = aux;
1595
1596 if (pnp)
1597 return QUIET;
1598 aprint_normal(" port %d", uiaa->uiaa_port);
1599 aprint_normal(" configuration %d", uiaa->uiaa_configno);
1600 aprint_normal(" interface %d", uiaa->uiaa_ifaceno);
1601 #if 0
1602 /*
1603 * It gets very crowded with these locators on the attach line.
1604 * They are not really needed since they are printed in the clear
1605 * by each driver.
1606 */
1607 if (uaa->uaa_vendor != UHUB_UNK_VENDOR)
1608 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor);
1609 if (uaa->uaa_product != UHUB_UNK_PRODUCT)
1610 aprint_normal(" product 0x%04x", uaa->uaa_product);
1611 if (uaa->uaa_release != UHUB_UNK_RELEASE)
1612 aprint_normal(" release 0x%04x", uaa->uaa_release);
1613 #endif
1614 return UNCONF;
1615 }
1616
1617 void
1618 usbd_fill_deviceinfo(struct usbd_device *dev, struct usb_device_info *di,
1619 int usedev)
1620 {
1621 struct usbd_port *p;
1622 int i, j, err;
1623
1624 di->udi_bus = device_unit(dev->ud_bus->ub_usbctl);
1625 di->udi_addr = dev->ud_addr;
1626 di->udi_cookie = dev->ud_cookie;
1627 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1628 di->udi_product, sizeof(di->udi_product), usedev, 1);
1629 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1630 UGETW(dev->ud_ddesc.bcdDevice));
1631 if (usedev) {
1632 usbd_status uerr = usbd_get_string(dev,
1633 dev->ud_ddesc.iSerialNumber, di->udi_serial);
1634 if (uerr != USBD_NORMAL_COMPLETION) {
1635 di->udi_serial[0] = '\0';
1636 } else {
1637 usbd_trim_spaces(di->udi_serial);
1638 }
1639 } else {
1640 di->udi_serial[0] = '\0';
1641 if (dev->ud_serial) {
1642 strlcpy(di->udi_serial, dev->ud_serial,
1643 sizeof(di->udi_serial));
1644 }
1645 }
1646
1647 di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor);
1648 di->udi_productNo = UGETW(dev->ud_ddesc.idProduct);
1649 di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice);
1650 di->udi_class = dev->ud_ddesc.bDeviceClass;
1651 di->udi_subclass = dev->ud_ddesc.bDeviceSubClass;
1652 di->udi_protocol = dev->ud_ddesc.bDeviceProtocol;
1653 di->udi_config = dev->ud_config;
1654 di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power;
1655 di->udi_speed = dev->ud_speed;
1656
1657 if (dev->ud_subdevlen > 0) {
1658 for (i = 0, j = 0; i < dev->ud_subdevlen &&
1659 j < USB_MAX_DEVNAMES; i++) {
1660 if (!dev->ud_subdevs[i])
1661 continue;
1662 strncpy(di->udi_devnames[j],
1663 device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN);
1664 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1665 j++;
1666 }
1667 } else {
1668 j = 0;
1669 }
1670 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1671 di->udi_devnames[j][0] = 0; /* empty */
1672
1673 if (!dev->ud_hub) {
1674 di->udi_nports = 0;
1675 return;
1676 }
1677
1678 const int nports = dev->ud_hub->uh_hubdesc.bNbrPorts;
1679 for (i = 1; i <= __arraycount(di->udi_ports) && i <= nports; i++) {
1680 p = &dev->ud_hub->uh_ports[i - 1];
1681 if (p->up_dev)
1682 err = p->up_dev->ud_addr;
1683 else {
1684 const int s = UGETW(p->up_status.wPortStatus);
1685 const bool sshub_p = USB_IS_SS(dev->ud_speed);
1686 if (s & UPS_PORT_ENABLED)
1687 err = USB_PORT_ENABLED;
1688 else if (s & UPS_SUSPEND)
1689 err = USB_PORT_SUSPENDED;
1690 /*
1691 * Note: UPS_PORT_POWER_SS is available only
1692 * on 3.x, and UPS_PORT_POWER is available
1693 * only on 2.0 or 1.1.
1694 */
1695 else if (sshub_p && (s & UPS_PORT_POWER_SS))
1696 err = USB_PORT_POWERED;
1697 else if (!sshub_p && (s & UPS_PORT_POWER))
1698 err = USB_PORT_POWERED;
1699 else
1700 err = USB_PORT_DISABLED;
1701 }
1702 di->udi_ports[i - 1] = err;
1703 }
1704 di->udi_nports = nports;
1705 }
1706
1707 void
1708 usb_free_device(struct usbd_device *dev)
1709 {
1710 int ifcidx, nifc;
1711
1712 if (dev->ud_pipe0 != NULL)
1713 usbd_kill_pipe(dev->ud_pipe0);
1714 if (dev->ud_ifaces != NULL) {
1715 nifc = dev->ud_cdesc->bNumInterface;
1716 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
1717 usbd_free_iface_data(dev, ifcidx);
1718 usbd_iface_fini(dev, ifcidx);
1719 }
1720 kmem_free(dev->ud_ifaces,
1721 nifc * sizeof(struct usbd_interface));
1722 }
1723 if (dev->ud_cdesc != NULL)
1724 kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength));
1725 if (dev->ud_bdesc != NULL)
1726 kmem_free(dev->ud_bdesc, UGETW(dev->ud_bdesc->wTotalLength));
1727 if (dev->ud_subdevlen > 0) {
1728 kmem_free(dev->ud_subdevs,
1729 dev->ud_subdevlen * sizeof(device_t));
1730 dev->ud_subdevlen = 0;
1731 }
1732 if (dev->ud_vendor) {
1733 kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN);
1734 }
1735 if (dev->ud_product) {
1736 kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN);
1737 }
1738 if (dev->ud_serial) {
1739 kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN);
1740 }
1741 kmem_free(dev, sizeof(*dev));
1742 }
1743
1744 /*
1745 * The general mechanism for detaching drivers works as follows: Each
1746 * driver is responsible for maintaining a reference count on the
1747 * number of outstanding references to its softc (e.g. from
1748 * processing hanging in a read or write). The detach method of the
1749 * driver decrements this counter and flags in the softc that the
1750 * driver is dying and then wakes any sleepers. It then sleeps on the
1751 * softc. Each place that can sleep must maintain the reference
1752 * count. When the reference count drops to -1 (0 is the normal value
1753 * of the reference count) then a wakeup on the softc is performed
1754 * signaling to the detach waiter that all references are gone.
1755 */
1756
1757 /*
1758 * Called from process context when we discover that a port has
1759 * been disconnected.
1760 */
1761 int
1762 usb_disconnect_port(struct usbd_port *up, device_t parent, int flags)
1763 {
1764 struct usbd_device *dev = up->up_dev;
1765 device_t subdev;
1766 char subdevname[16];
1767 const char *hubname = device_xname(parent);
1768 int i, rc;
1769
1770 USBHIST_FUNC();
1771 USBHIST_CALLARGS(usbdebug, "up=%#jx dev=%#jx port=%jd",
1772 (uintptr_t)up, (uintptr_t)dev, up->up_portno, 0);
1773
1774 if (dev == NULL) {
1775 return 0;
1776 }
1777
1778 if (dev->ud_subdevlen > 0) {
1779 DPRINTFN(3, "disconnect subdevs", 0, 0, 0, 0);
1780 for (i = 0; i < dev->ud_subdevlen; i++) {
1781 if ((subdev = dev->ud_subdevs[i]) == NULL)
1782 continue;
1783 strlcpy(subdevname, device_xname(subdev),
1784 sizeof(subdevname));
1785 KERNEL_LOCK(1, curlwp);
1786 rc = config_detach(subdev, flags);
1787 KERNEL_UNLOCK_ONE(curlwp);
1788 if (rc != 0)
1789 return rc;
1790 printf("%s: at %s", subdevname, hubname);
1791 if (up->up_portno != 0)
1792 printf(" port %d", up->up_portno);
1793 printf(" (addr %d) disconnected\n", dev->ud_addr);
1794 }
1795 KASSERT(!dev->ud_nifaces_claimed);
1796 }
1797
1798 mutex_enter(dev->ud_bus->ub_lock);
1799 dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL;
1800 up->up_dev = NULL;
1801 mutex_exit(dev->ud_bus->ub_lock);
1802
1803 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1804
1805 usb_free_device(dev);
1806
1807 return 0;
1808 }
1809