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