usb_subr.c revision 1.196.4.2.2.1 1 /* $NetBSD: usb_subr.c,v 1.196.4.2.2.1 2016/09/06 20:33:09 skrll Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart (at) augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.196.4.2.2.1 2016/09/06 20:33:09 skrll Exp $");
36
37 #ifdef _KERNEL_OPT
38 #include "opt_compat_netbsd.h"
39 #include "opt_usb.h"
40 #include "opt_usbverbose.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/kmem.h>
47 #include <sys/device.h>
48 #include <sys/select.h>
49 #include <sys/proc.h>
50
51 #include <sys/bus.h>
52 #include <sys/module.h>
53
54 #include <dev/usb/usb.h>
55
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
58 #include <dev/usb/usbdivar.h>
59 #include <dev/usb/usbdevs.h>
60 #include <dev/usb/usb_quirks.h>
61 #include <dev/usb/usb_verbose.h>
62 #include <dev/usb/usbhist.h>
63
64 #include "locators.h"
65
66 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOG(usbdebug,FMT,A,B,C,D)
67 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
68
69 Static usbd_status usbd_set_config(struct usbd_device *, int);
70 Static void usbd_devinfo(struct usbd_device *, int, char *, size_t);
71 Static void usbd_devinfo_vp(struct usbd_device *, char *, size_t, char *, size_t,
72 int, int);
73 Static int usbd_getnewaddr(struct usbd_bus *);
74 Static int usbd_print(void *, const char *);
75 Static int usbd_ifprint(void *, const char *);
76 Static void usbd_free_iface_data(struct usbd_device *, int);
77
78 uint32_t usb_cookie_no = 0;
79
80 Static const char * const usbd_error_strs[] = {
81 "NORMAL_COMPLETION",
82 "IN_PROGRESS",
83 "PENDING_REQUESTS",
84 "NOT_STARTED",
85 "INVAL",
86 "NOMEM",
87 "CANCELLED",
88 "BAD_ADDRESS",
89 "IN_USE",
90 "NO_ADDR",
91 "SET_ADDR_FAILED",
92 "NO_POWER",
93 "TOO_DEEP",
94 "IOERROR",
95 "NOT_CONFIGURED",
96 "TIMEOUT",
97 "SHORT_XFER",
98 "STALLED",
99 "INTERRUPTED",
100 "XXX",
101 };
102
103 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 bd, *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 cdp = kmem_alloc(len, KM_SLEEP);
648 if (cdp == NULL)
649 return USBD_NOMEM;
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=%d", err, 0, 0, 0);
660 goto bad;
661 }
662 if (cdp->bDescriptorType != UDESC_CONFIG) {
663 DPRINTF("bad desc %d", cdp->bDescriptorType, 0, 0, 0);
664 err = USBD_INVAL;
665 goto bad;
666 }
667
668 if (USB_IS_SS(dev->ud_speed)) {
669 /* get short bos desc */
670 err = usbd_get_bos_desc(dev, index, &bd);
671 if (!err) {
672 int blen = UGETW(bd.wTotalLength);
673 bdp = kmem_alloc(blen, KM_SLEEP);
674 if (bdp == NULL) {
675 err = USBD_NOMEM;
676 goto bad;
677 }
678
679 /* Get the full desc */
680 for (i = 0; i < 3; i++) {
681 err = usbd_get_desc(dev, UDESC_BOS, index, blen,
682 bdp);
683 if (!err)
684 break;
685 usbd_delay_ms(dev, 200);
686 }
687 if (err || bdp->bDescriptorType != UDESC_BOS) {
688 DPRINTF("error %d or bad desc %d", err,
689 bdp->bDescriptorType, 0, 0);
690 kmem_free(bdp, blen);
691 bdp = NULL;
692 }
693 }
694 }
695 dev->ud_bdesc = bdp;
696
697 /*
698 * Figure out if the device is self or bus powered.
699 */
700 #if 0 /* XXX various devices don't report the power state correctly */
701 selfpowered = 0;
702 err = usbd_get_device_status(dev, &ds);
703 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
704 selfpowered = 1;
705 #endif
706 /*
707 * Use the power state in the configuration we are going
708 * to set. This doesn't necessarily reflect the actual
709 * power state of the device; the driver can control this
710 * by choosing the appropriate configuration.
711 */
712 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
713
714 DPRINTF("addr %d cno=%d attr=0x%02x, selfpowered=%d",
715 dev->ud_addr, cdp->bConfigurationValue, cdp->bmAttributes,
716 selfpowered);
717 DPRINTF("max power=%d", cdp->bMaxPower * 2, 0, 0, 0);
718
719 /* Check if we have enough power. */
720 #if 0 /* this is a no-op, see above */
721 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
722 if (msg)
723 printf("%s: device addr %d (config %d): "
724 "can't set self powered configuration\n",
725 device_xname(dev->ud_bus->bdev), dev->ud_addr,
726 cdp->bConfigurationValue);
727 err = USBD_NO_POWER;
728 goto bad;
729 }
730 #endif
731 #ifdef USB_DEBUG
732 if (dev->ud_powersrc == NULL) {
733 DPRINTF("No power source?", 0, 0, 0, 0);
734 err = USBD_IOERROR;
735 goto bad;
736 }
737 #endif
738 power = cdp->bMaxPower * 2;
739 if (power > dev->ud_powersrc->up_power) {
740 DPRINTF("power exceeded %d %d", power, dev->ud_powersrc->up_power,
741 0, 0);
742 /* XXX print nicer message. */
743 if (msg)
744 printf("%s: device addr %d (config %d) exceeds power "
745 "budget, %d mA > %d mA\n",
746 device_xname(dev->ud_bus->ub_usbctl), dev->ud_addr,
747 cdp->bConfigurationValue,
748 power, dev->ud_powersrc->up_power);
749 err = USBD_NO_POWER;
750 goto bad;
751 }
752 dev->ud_power = power;
753 dev->ud_selfpowered = selfpowered;
754
755 /* Set the actual configuration value. */
756 DPRINTF("set config %d", cdp->bConfigurationValue, 0, 0, 0);
757 err = usbd_set_config(dev, cdp->bConfigurationValue);
758 if (err) {
759 DPRINTF("setting config=%d failed, error=%d",
760 cdp->bConfigurationValue, err, 0, 0);
761 goto bad;
762 }
763
764 /* Allocate and fill interface data. */
765 nifc = cdp->bNumInterface;
766 dev->ud_ifaces = kmem_alloc(nifc * sizeof(struct usbd_interface),
767 KM_SLEEP);
768 if (dev->ud_ifaces == NULL) {
769 err = USBD_NOMEM;
770 goto bad;
771 }
772 DPRINTFN(5, "dev=%p cdesc=%p", dev, cdp, 0, 0);
773 dev->ud_cdesc = cdp;
774 dev->ud_config = cdp->bConfigurationValue;
775 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
776 err = usbd_fill_iface_data(dev, ifcidx, 0);
777 if (err) {
778 while (--ifcidx >= 0)
779 usbd_free_iface_data(dev, ifcidx);
780 goto bad;
781 }
782 }
783
784 return USBD_NORMAL_COMPLETION;
785
786 bad:
787 kmem_free(cdp, len);
788 if (bdp != NULL) {
789 kmem_free(bdp, UGETW(bdp->wTotalLength));
790 dev->ud_bdesc = NULL;
791 }
792 return err;
793 }
794
795 /* XXX add function for alternate settings */
796
797 usbd_status
798 usbd_setup_pipe(struct usbd_device *dev, struct usbd_interface *iface,
799 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe)
800 {
801 return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0);
802 }
803
804 usbd_status
805 usbd_setup_pipe_flags(struct usbd_device *dev, struct usbd_interface *iface,
806 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe, uint8_t flags)
807 {
808 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
809 struct usbd_pipe *p;
810 usbd_status err;
811
812 p = kmem_alloc(dev->ud_bus->ub_pipesize, KM_SLEEP);
813 DPRINTFN(1, "dev=%p addr=%d iface=%p ep=%p pipe=%p", dev, dev->ud_addr, iface, ep);
814 if (p == NULL) {
815 DPRINTFN(1, "(nomem)", 0, 0, 0, 0);
816 return USBD_NOMEM;
817 }
818 p->up_dev = dev;
819 p->up_iface = iface;
820 p->up_endpoint = ep;
821 ep->ue_refcnt++;
822 p->up_intrxfer = NULL;
823 p->up_running = 0;
824 p->up_aborting = 0;
825 p->up_serialise = true;
826 p->up_repeat = 0;
827 p->up_interval = ival;
828 p->up_flags = flags;
829 p->up_serialise = true;
830 SIMPLEQ_INIT(&p->up_queue);
831 err = dev->ud_bus->ub_methods->ubm_open(p);
832 if (err) {
833 DPRINTF("endpoint=0x%x failed, error=%d",
834 ep->ue_edesc->bEndpointAddress, err, 0, 0);
835 kmem_intr_free(p, dev->ud_bus->ub_pipesize);
836 return err;
837 }
838
839 KASSERT(p->up_methods->upm_start || p->up_serialise == false);
840
841 usb_init_task(&p->up_async_task, usbd_clear_endpoint_stall_task, p,
842 USB_TASKQ_MPSAFE);
843 DPRINTFN(1, "pipe=%p", p, 0, 0, 0);
844 *pipe = p;
845 return USBD_NORMAL_COMPLETION;
846 }
847
848 /* Abort the device control pipe. */
849 void
850 usbd_kill_pipe(struct usbd_pipe *pipe)
851 {
852 usbd_abort_pipe(pipe);
853 usbd_lock_pipe(pipe);
854 pipe->up_methods->upm_close(pipe);
855 usbd_unlock_pipe(pipe);
856 usb_rem_task(pipe->up_dev, &pipe->up_async_task);
857 pipe->up_endpoint->ue_refcnt--;
858 kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
859 }
860
861 int
862 usbd_getnewaddr(struct usbd_bus *bus)
863 {
864 int addr;
865
866 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
867 if (bus->ub_devices[addr] == NULL)
868 return addr;
869 return -1;
870 }
871
872 usbd_status
873 usbd_attach_roothub(device_t parent, struct usbd_device *dev)
874 {
875 struct usb_attach_arg uaa;
876 usb_device_descriptor_t *dd = &dev->ud_ddesc;
877 device_t dv;
878
879 uaa.uaa_device = dev;
880 uaa.uaa_usegeneric = 0;
881 uaa.uaa_port = 0;
882 uaa.uaa_vendor = UGETW(dd->idVendor);
883 uaa.uaa_product = UGETW(dd->idProduct);
884 uaa.uaa_release = UGETW(dd->bcdDevice);
885 uaa.uaa_class = dd->bDeviceClass;
886 uaa.uaa_subclass = dd->bDeviceSubClass;
887 uaa.uaa_proto = dd->bDeviceProtocol;
888
889 dv = config_found_ia(parent, "usbroothubif", &uaa, 0);
890 if (dv) {
891 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP);
892 if (dev->ud_subdevs == NULL)
893 return USBD_NOMEM;
894 dev->ud_subdevs[0] = dv;
895 dev->ud_subdevlen = 1;
896 }
897 return USBD_NORMAL_COMPLETION;
898 }
899
900 static void
901 usbd_serialnumber(device_t dv, struct usbd_device *dev)
902 {
903 if (dev->ud_serial) {
904 prop_dictionary_set_cstring(device_properties(dv),
905 "serialnumber", dev->ud_serial);
906 }
907 }
908
909 static usbd_status
910 usbd_attachwholedevice(device_t parent, struct usbd_device *dev, int port,
911 int usegeneric)
912 {
913 struct usb_attach_arg uaa;
914 usb_device_descriptor_t *dd = &dev->ud_ddesc;
915 device_t dv;
916 int dlocs[USBDEVIFCF_NLOCS];
917
918 uaa.uaa_device = dev;
919 uaa.uaa_usegeneric = usegeneric;
920 uaa.uaa_port = port;
921 uaa.uaa_vendor = UGETW(dd->idVendor);
922 uaa.uaa_product = UGETW(dd->idProduct);
923 uaa.uaa_release = UGETW(dd->bcdDevice);
924 uaa.uaa_class = dd->bDeviceClass;
925 uaa.uaa_subclass = dd->bDeviceSubClass;
926 uaa.uaa_proto = dd->bDeviceProtocol;
927
928 dlocs[USBDEVIFCF_PORT] = uaa.uaa_port;
929 dlocs[USBDEVIFCF_VENDOR] = uaa.uaa_vendor;
930 dlocs[USBDEVIFCF_PRODUCT] = uaa.uaa_product;
931 dlocs[USBDEVIFCF_RELEASE] = uaa.uaa_release;
932 /* the rest is historical ballast */
933 dlocs[USBDEVIFCF_CONFIGURATION] = -1;
934 dlocs[USBDEVIFCF_INTERFACE] = -1;
935
936 KERNEL_LOCK(1, NULL);
937 dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print,
938 config_stdsubmatch);
939 KERNEL_UNLOCK_ONE(NULL);
940 if (dv) {
941 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP);
942 if (dev->ud_subdevs == NULL)
943 return USBD_NOMEM;
944 dev->ud_subdevs[0] = dv;
945 dev->ud_subdevlen = 1;
946 dev->ud_nifaces_claimed = 1; /* XXX */
947 usbd_serialnumber(dv, dev);
948 }
949 return USBD_NORMAL_COMPLETION;
950 }
951
952 static usbd_status
953 usbd_attachinterfaces(device_t parent, struct usbd_device *dev,
954 int port, const int *locators)
955 {
956 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
957 struct usbif_attach_arg uiaa;
958 int ilocs[USBIFIFCF_NLOCS];
959 usb_device_descriptor_t *dd = &dev->ud_ddesc;
960 int nifaces;
961 struct usbd_interface **ifaces;
962 int i, j, loc;
963 device_t dv;
964
965 nifaces = dev->ud_cdesc->bNumInterface;
966 ifaces = kmem_zalloc(nifaces * sizeof(*ifaces), KM_SLEEP);
967 if (!ifaces)
968 return USBD_NOMEM;
969 for (i = 0; i < nifaces; i++) {
970 if (!dev->ud_subdevs[i]) {
971 ifaces[i] = &dev->ud_ifaces[i];
972 }
973 DPRINTF("interface %d %p", i, ifaces[i], 0, 0);
974 }
975
976
977 uiaa.uiaa_device = dev;
978 uiaa.uiaa_port = port;
979 uiaa.uiaa_vendor = UGETW(dd->idVendor);
980 uiaa.uiaa_product = UGETW(dd->idProduct);
981 uiaa.uiaa_release = UGETW(dd->bcdDevice);
982 uiaa.uiaa_configno = dev->ud_cdesc->bConfigurationValue;
983 uiaa.uiaa_ifaces = ifaces;
984 uiaa.uiaa_nifaces = nifaces;
985 ilocs[USBIFIFCF_PORT] = uiaa.uiaa_port;
986 ilocs[USBIFIFCF_VENDOR] = uiaa.uiaa_vendor;
987 ilocs[USBIFIFCF_PRODUCT] = uiaa.uiaa_product;
988 ilocs[USBIFIFCF_RELEASE] = uiaa.uiaa_release;
989 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.uiaa_configno;
990
991 for (i = 0; i < nifaces; i++) {
992 if (!ifaces[i]) {
993 DPRINTF("interface %d claimed", i, 0, 0, 0);
994 continue; /* interface already claimed */
995 }
996 uiaa.uiaa_iface = ifaces[i];
997 uiaa.uiaa_class = ifaces[i]->ui_idesc->bInterfaceClass;
998 uiaa.uiaa_subclass = ifaces[i]->ui_idesc->bInterfaceSubClass;
999 uiaa.uiaa_proto = ifaces[i]->ui_idesc->bInterfaceProtocol;
1000 uiaa.uiaa_ifaceno = ifaces[i]->ui_idesc->bInterfaceNumber;
1001
1002 DPRINTF("searching for interface %d...", i, 0, 0, 0);
1003 DPRINTF("class %x subclass %x proto %x ifaceno %d",
1004 uiaa.uiaa_class, uiaa.uiaa_subclass, uiaa.uiaa_proto,
1005 uiaa.uiaa_ifaceno);
1006 ilocs[USBIFIFCF_INTERFACE] = uiaa.uiaa_ifaceno;
1007 if (locators != NULL) {
1008 loc = locators[USBIFIFCF_CONFIGURATION];
1009 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT &&
1010 loc != uiaa.uiaa_configno)
1011 continue;
1012 loc = locators[USBIFIFCF_INTERFACE];
1013 if (loc != USBIFIFCF_INTERFACE_DEFAULT &&
1014 loc != uiaa.uiaa_ifaceno)
1015 continue;
1016 }
1017 KERNEL_LOCK(1, NULL);
1018 dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa,
1019 usbd_ifprint, config_stdsubmatch);
1020 KERNEL_UNLOCK_ONE(NULL);
1021 if (!dv)
1022 continue;
1023
1024 usbd_serialnumber(dv, dev);
1025
1026 /* claim */
1027 ifaces[i] = NULL;
1028 /* account for ifaces claimed by the driver behind our back */
1029 for (j = 0; j < nifaces; j++) {
1030
1031 if (!ifaces[j] && !dev->ud_subdevs[j]) {
1032 DPRINTF("interface %d claimed behind our back",
1033 j, 0, 0, 0);
1034 dev->ud_subdevs[j] = dv;
1035 dev->ud_nifaces_claimed++;
1036 }
1037 }
1038 }
1039
1040 kmem_free(ifaces, nifaces * sizeof(*ifaces));
1041 return USBD_NORMAL_COMPLETION;
1042 }
1043
1044 usbd_status
1045 usbd_probe_and_attach(device_t parent, struct usbd_device *dev,
1046 int port, int addr)
1047 {
1048 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1049 usb_device_descriptor_t *dd = &dev->ud_ddesc;
1050 int confi, nifaces;
1051 usbd_status err;
1052
1053 /* First try with device specific drivers. */
1054 DPRINTF("trying device specific drivers", 0, 0, 0, 0);
1055 err = usbd_attachwholedevice(parent, dev, port, 0);
1056 if (dev->ud_nifaces_claimed || err)
1057 return err;
1058 DPRINTF("no device specific driver found", 0, 0, 0, 0);
1059
1060 DPRINTF("looping over %d configurations", dd->bNumConfigurations,
1061 0, 0, 0);
1062 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
1063 DPRINTFN(1, "trying config idx=%d", confi, 0, 0, 0);
1064 err = usbd_set_config_index(dev, confi, 1);
1065 if (err) {
1066 DPRINTF("port %d, set config at addr %d failed, "
1067 "error=%d", port, addr, err, 0);
1068 printf("%s: port %d, set config at addr %d failed\n",
1069 device_xname(parent), port, addr);
1070 return err;
1071 }
1072 nifaces = dev->ud_cdesc->bNumInterface;
1073 dev->ud_subdevs = kmem_zalloc(nifaces * sizeof(device_t),
1074 KM_SLEEP);
1075 if (dev->ud_subdevs == NULL)
1076 return USBD_NOMEM;
1077 dev->ud_subdevlen = nifaces;
1078
1079 err = usbd_attachinterfaces(parent, dev, port, NULL);
1080
1081 if (!dev->ud_nifaces_claimed) {
1082 kmem_free(dev->ud_subdevs,
1083 dev->ud_subdevlen * sizeof(device_t));
1084 dev->ud_subdevs = 0;
1085 dev->ud_subdevlen = 0;
1086 }
1087 if (dev->ud_nifaces_claimed || err)
1088 return err;
1089 }
1090 /* No interfaces were attached in any of the configurations. */
1091
1092 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
1093 usbd_set_config_index(dev, 0, 0);
1094
1095 DPRINTF("no interface drivers found", 0, 0, 0, 0);
1096
1097 /* Finally try the generic driver. */
1098 err = usbd_attachwholedevice(parent, dev, port, 1);
1099
1100 /*
1101 * The generic attach failed, but leave the device as it is.
1102 * We just did not find any drivers, that's all. The device is
1103 * fully operational and not harming anyone.
1104 */
1105 DPRINTF("generic attach failed", 0, 0, 0, 0);
1106
1107 return USBD_NORMAL_COMPLETION;
1108 }
1109
1110 /**
1111 * Called from uhub_rescan(). usbd_new_device() for the target dev must be
1112 * called before calling this.
1113 */
1114 usbd_status
1115 usbd_reattach_device(device_t parent, struct usbd_device *dev,
1116 int port, const int *locators)
1117 {
1118 int i, loc;
1119
1120 if (locators != NULL) {
1121 loc = locators[USBIFIFCF_PORT];
1122 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port)
1123 return USBD_NORMAL_COMPLETION;
1124 loc = locators[USBIFIFCF_VENDOR];
1125 if (loc != USBIFIFCF_VENDOR_DEFAULT &&
1126 loc != UGETW(dev->ud_ddesc.idVendor))
1127 return USBD_NORMAL_COMPLETION;
1128 loc = locators[USBIFIFCF_PRODUCT];
1129 if (loc != USBIFIFCF_PRODUCT_DEFAULT &&
1130 loc != UGETW(dev->ud_ddesc.idProduct))
1131 return USBD_NORMAL_COMPLETION;
1132 loc = locators[USBIFIFCF_RELEASE];
1133 if (loc != USBIFIFCF_RELEASE_DEFAULT &&
1134 loc != UGETW(dev->ud_ddesc.bcdDevice))
1135 return USBD_NORMAL_COMPLETION;
1136 }
1137 if (dev->ud_subdevlen == 0) {
1138 /* XXX: check USBIFIFCF_CONFIGURATION and
1139 * USBIFIFCF_INTERFACE too */
1140 return usbd_probe_and_attach(parent, dev, port, dev->ud_addr);
1141 } else if (dev->ud_subdevlen != dev->ud_cdesc->bNumInterface) {
1142 /* device-specific or generic driver is already attached. */
1143 return USBD_NORMAL_COMPLETION;
1144 }
1145 /* Does the device have unconfigured interfaces? */
1146 for (i = 0; i < dev->ud_subdevlen; i++) {
1147 if (dev->ud_subdevs[i] == NULL) {
1148 break;
1149 }
1150 }
1151 if (i >= dev->ud_subdevlen)
1152 return USBD_NORMAL_COMPLETION;
1153 return usbd_attachinterfaces(parent, dev, port, locators);
1154 }
1155
1156 /*
1157 * Get the first 8 bytes of the device descriptor.
1158 * Do as Windows does: try to read 64 bytes -- there are devices which
1159 * recognize the initial descriptor fetch (before the control endpoint's
1160 * MaxPacketSize is known by the host) by exactly this length.
1161 */
1162 usbd_status
1163 usbd_get_initial_ddesc(struct usbd_device *dev, usb_device_descriptor_t *desc)
1164 {
1165 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1166 usb_device_request_t req;
1167 char buf[64];
1168 int res, actlen;
1169
1170 DPRINTFN(5, "dev %p", dev, 0, 0, 0);
1171
1172 req.bmRequestType = UT_READ_DEVICE;
1173 req.bRequest = UR_GET_DESCRIPTOR;
1174 USETW2(req.wValue, UDESC_DEVICE, 0);
1175 USETW(req.wIndex, 0);
1176 USETW(req.wLength, 64);
1177 res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK,
1178 &actlen, USBD_DEFAULT_TIMEOUT);
1179 if (res)
1180 return res;
1181 if (actlen < 8)
1182 return USBD_SHORT_XFER;
1183 memcpy(desc, buf, 8);
1184 return USBD_NORMAL_COMPLETION;
1185 }
1186
1187 /*
1188 * Called when a new device has been put in the powered state,
1189 * but not yet in the addressed state.
1190 * Get initial descriptor, set the address, get full descriptor,
1191 * and attach a driver.
1192 */
1193 usbd_status
1194 usbd_new_device(device_t parent, struct usbd_bus* bus, int depth,
1195 int speed, int port, struct usbd_port *up)
1196 {
1197 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1198 struct usbd_device *dev, *adev;
1199 struct usbd_device *hub;
1200 usb_device_descriptor_t *dd;
1201 usb_port_status_t ps;
1202 usbd_status err;
1203 int addr;
1204 int i;
1205 int p;
1206
1207 DPRINTF("bus=%p port=%d depth=%d speed=%d", bus, port, depth, speed);
1208
1209 if (bus->ub_methods->ubm_newdev != NULL)
1210 return (bus->ub_methods->ubm_newdev)(parent, bus, depth, speed,
1211 port, up);
1212
1213 addr = usbd_getnewaddr(bus);
1214 if (addr < 0) {
1215 printf("%s: No free USB addresses, new device ignored.\n",
1216 device_xname(bus->ub_usbctl));
1217 return USBD_NO_ADDR;
1218 }
1219
1220 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
1221 if (dev == NULL)
1222 return USBD_NOMEM;
1223
1224 dev->ud_bus = bus;
1225
1226 /* Set up default endpoint handle. */
1227 dev->ud_ep0.ue_edesc = &dev->ud_ep0desc;
1228
1229 /* Set up default endpoint descriptor. */
1230 dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1231 dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT;
1232 dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1233 dev->ud_ep0desc.bmAttributes = UE_CONTROL;
1234 /*
1235 * temporary, will be fixed after first descriptor fetch
1236 * (which uses 64 bytes so it shouldn't be less),
1237 * highspeed devices must support 64 byte packets anyway
1238 */
1239 if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL)
1240 USETW(dev->ud_ep0desc.wMaxPacketSize, 64);
1241 else
1242 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET);
1243
1244 dev->ud_ep0desc.bInterval = 0;
1245
1246 /* doesn't matter, just don't leave it uninitialized */
1247 dev->ud_ep0.ue_toggle = 0;
1248
1249 dev->ud_quirks = &usbd_no_quirk;
1250 dev->ud_addr = USB_START_ADDR;
1251 dev->ud_ddesc.bMaxPacketSize = 0;
1252 dev->ud_depth = depth;
1253 dev->ud_powersrc = up;
1254 dev->ud_myhub = up->up_parent;
1255
1256 up->up_dev = dev;
1257
1258 /* Locate port on upstream high speed hub */
1259 for (adev = dev, hub = up->up_parent;
1260 hub != NULL && hub->ud_speed != USB_SPEED_HIGH;
1261 adev = hub, hub = hub->ud_myhub)
1262 ;
1263 if (hub) {
1264 for (p = 0; p < hub->ud_hub->uh_hubdesc.bNbrPorts; p++) {
1265 if (hub->ud_hub->uh_ports[p].up_dev == adev) {
1266 dev->ud_myhsport = &hub->ud_hub->uh_ports[p];
1267 goto found;
1268 }
1269 }
1270 panic("usbd_new_device: cannot find HS port");
1271 found:
1272 DPRINTFN(1, "high speed port %d", p, 0, 0, 0);
1273 } else {
1274 dev->ud_myhsport = NULL;
1275 }
1276 dev->ud_speed = speed;
1277 dev->ud_langid = USBD_NOLANG;
1278 dev->ud_cookie.cookie = ++usb_cookie_no;
1279
1280 /* Establish the default pipe. */
1281 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1282 &dev->ud_pipe0, USBD_MPSAFE);
1283 if (err) {
1284 usbd_remove_device(dev, up);
1285 return err;
1286 }
1287
1288 dd = &dev->ud_ddesc;
1289 /* Try a few times in case the device is slow (i.e. outside specs.) */
1290 for (i = 0; i < 10; i++) {
1291 /* Get the first 8 bytes of the device descriptor. */
1292 err = usbd_get_initial_ddesc(dev, dd);
1293 if (!err)
1294 break;
1295 usbd_delay_ms(dev, 200);
1296 if ((i & 3) == 3)
1297 usbd_reset_port(up->up_parent, port, &ps);
1298 }
1299 if (err) {
1300 DPRINTF("addr=%d, getting first desc failed: %d", addr, err,
1301 0, 0);
1302 usbd_remove_device(dev, up);
1303 return err;
1304 }
1305
1306 /* Windows resets the port here, do likewise */
1307 if (up->up_parent)
1308 usbd_reset_port(up->up_parent, port, &ps);
1309
1310 if (speed == USB_SPEED_HIGH) {
1311 /* Max packet size must be 64 (sec 5.5.3). */
1312 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1313 #ifdef DIAGNOSTIC
1314 printf("usbd_new_device: addr=%d bad max packet "
1315 "size=%d. adjusting to %d.\n",
1316 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET);
1317 #endif
1318 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1319 }
1320 }
1321
1322 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, subclass=%d", addr,
1323 UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass);
1324 DPRINTF("protocol=%d, maxpacket=%d, len=%d, speed=%d",
1325 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, dev->ud_speed);
1326
1327 if (dd->bDescriptorType != UDESC_DEVICE) {
1328 /* Illegal device descriptor */
1329 DPRINTF("illegal descriptor %d", dd->bDescriptorType, 0, 0, 0);
1330 usbd_remove_device(dev, up);
1331 return USBD_INVAL;
1332 }
1333
1334 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1335 DPRINTF("bad length %d", dd->bLength, 0, 0, 0);
1336 usbd_remove_device(dev, up);
1337 return USBD_INVAL;
1338 }
1339
1340 USETW(dev->ud_ep0desc.wMaxPacketSize, dd->bMaxPacketSize);
1341
1342 /* Re-establish the default pipe with the new MPS. */
1343 usbd_kill_pipe(dev->ud_pipe0);
1344 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1345 &dev->ud_pipe0, USBD_MPSAFE);
1346 if (err) {
1347 DPRINTF("setup default pipe failed err %d", err, 0, 0, 0);
1348 usbd_remove_device(dev, up);
1349 return err;
1350 }
1351
1352 /* Set the address */
1353 DPRINTFN(5, "setting device address=%d", addr, 0, 0, 0);
1354 err = usbd_set_address(dev, addr);
1355 if (err) {
1356 DPRINTF("set address %d failed, err = %d", addr, err, 0, 0);
1357 err = USBD_SET_ADDR_FAILED;
1358 usbd_remove_device(dev, up);
1359 return err;
1360 }
1361
1362 /* Allow device time to set new address */
1363 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1364 dev->ud_addr = addr; /* new device address now */
1365 bus->ub_devices[addr] = dev;
1366
1367 /* Re-establish the default pipe with the new address. */
1368 usbd_kill_pipe(dev->ud_pipe0);
1369 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL,
1370 &dev->ud_pipe0, USBD_MPSAFE);
1371 if (err) {
1372 DPRINTF("setup default pipe failed, err = %d", err, 0, 0, 0);
1373 usbd_remove_device(dev, up);
1374 return err;
1375 }
1376
1377 err = usbd_reload_device_desc(dev);
1378 if (err) {
1379 DPRINTF("addr=%d, getting full desc failed, err = %d", addr,
1380 err, 0, 0);
1381 usbd_remove_device(dev, up);
1382 return err;
1383 }
1384
1385 /* Assume 100mA bus powered for now. Changed when configured. */
1386 dev->ud_power = USB_MIN_POWER;
1387 dev->ud_selfpowered = 0;
1388
1389 DPRINTF("new dev (addr %d), dev=%p, parent=%p", addr, dev, parent, 0);
1390
1391 usbd_get_device_strings(dev);
1392
1393 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1394
1395 if (port == 0) { /* root hub */
1396 KASSERT(addr == 1);
1397 usbd_attach_roothub(parent, dev);
1398 return USBD_NORMAL_COMPLETION;
1399 }
1400
1401 err = usbd_probe_and_attach(parent, dev, port, addr);
1402 if (err) {
1403 usbd_remove_device(dev, up);
1404 return err;
1405 }
1406
1407 return USBD_NORMAL_COMPLETION;
1408 }
1409
1410 usbd_status
1411 usbd_reload_device_desc(struct usbd_device *dev)
1412 {
1413 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1414 usb_device_descriptor_t *udd = &dev->ud_ddesc;
1415 usbd_status err;
1416
1417 /* Get the full device descriptor. */
1418 err = usbd_get_device_desc(dev, udd);
1419 if (err)
1420 return err;
1421
1422 DPRINTFN(15, "bLength %5u", udd->bLength, 0, 0, 0);
1423 DPRINTFN(15, "bDescriptorType %5u", udd->bDescriptorType, 0, 0, 0);
1424 DPRINTFN(15, "bcdUSB %2x.%02x", udd->bcdUSB[1],
1425 udd->bcdUSB[0], 0, 0);
1426 DPRINTFN(15, "bDeviceClass %5u", udd->bDeviceClass, 0, 0, 0);
1427 DPRINTFN(15, "bDeviceSubClass %5u", udd->bDeviceSubClass, 0, 0, 0);
1428 DPRINTFN(15, "bDeviceProtocol %5u", udd->bDeviceProtocol, 0, 0, 0);
1429 DPRINTFN(15, "bMaxPacketSize0 %5u", udd->bMaxPacketSize, 0, 0, 0);
1430 DPRINTFN(15, "idVendor 0x%04x", udd->idVendor, 0, 0, 0);
1431 DPRINTFN(15, "idProduct 0x%04x", udd->idProduct, 0, 0, 0);
1432 DPRINTFN(15, "bcdDevice %2x.%02x", udd->bcdDevice[1],
1433 udd->bcdDevice[0], 0, 0);
1434 DPRINTFN(15, "iManufacturer %5u", udd->iManufacturer, 0, 0, 0);
1435 DPRINTFN(15, "iProduct %5u", udd->iProduct, 0, 0, 0);
1436 DPRINTFN(15, "iSerial %5u", udd->iSerialNumber, 0, 0, 0);
1437 DPRINTFN(15, "bNumConfigurations %5u", udd->bNumConfigurations, 0, 0,
1438 0);
1439
1440 /* Figure out what's wrong with this device. */
1441 dev->ud_quirks = usbd_find_quirk(udd);
1442
1443 return USBD_NORMAL_COMPLETION;
1444 }
1445
1446 void
1447 usbd_remove_device(struct usbd_device *dev, struct usbd_port *up)
1448 {
1449
1450 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1451
1452 DPRINTF("dev %p up %p", dev, up, 0, 0);
1453
1454 if (dev->ud_pipe0 != NULL)
1455 usbd_kill_pipe(dev->ud_pipe0);
1456 up->up_dev = NULL;
1457 dev->ud_bus->ub_devices[dev->ud_addr] = NULL;
1458
1459 kmem_free(dev, sizeof(*dev));
1460 }
1461
1462 int
1463 usbd_print(void *aux, const char *pnp)
1464 {
1465 struct usb_attach_arg *uaa = aux;
1466
1467 if (pnp) {
1468 #define USB_DEVINFO 1024
1469 char *devinfo;
1470 if (!uaa->uaa_usegeneric)
1471 return QUIET;
1472 devinfo = kmem_alloc(USB_DEVINFO, KM_SLEEP);
1473 usbd_devinfo(uaa->uaa_device, 1, devinfo, USB_DEVINFO);
1474 aprint_normal("%s, %s", devinfo, pnp);
1475 kmem_free(devinfo, USB_DEVINFO);
1476 }
1477 aprint_normal(" port %d", uaa->uaa_port);
1478 #if 0
1479 /*
1480 * It gets very crowded with these locators on the attach line.
1481 * They are not really needed since they are printed in the clear
1482 * by each driver.
1483 */
1484 if (uaa->uaa_vendor != UHUB_UNK_VENDOR)
1485 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor);
1486 if (uaa->uaa_product != UHUB_UNK_PRODUCT)
1487 aprint_normal(" product 0x%04x", uaa->uaa_product);
1488 if (uaa->uaa_release != UHUB_UNK_RELEASE)
1489 aprint_normal(" release 0x%04x", uaa->uaa_release);
1490 #endif
1491 return UNCONF;
1492 }
1493
1494 int
1495 usbd_ifprint(void *aux, const char *pnp)
1496 {
1497 struct usbif_attach_arg *uiaa = aux;
1498
1499 if (pnp)
1500 return QUIET;
1501 aprint_normal(" port %d", uiaa->uiaa_port);
1502 aprint_normal(" configuration %d", uiaa->uiaa_configno);
1503 aprint_normal(" interface %d", uiaa->uiaa_ifaceno);
1504 #if 0
1505 /*
1506 * It gets very crowded with these locators on the attach line.
1507 * They are not really needed since they are printed in the clear
1508 * by each driver.
1509 */
1510 if (uaa->uaa_vendor != UHUB_UNK_VENDOR)
1511 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor);
1512 if (uaa->uaa_product != UHUB_UNK_PRODUCT)
1513 aprint_normal(" product 0x%04x", uaa->uaa_product);
1514 if (uaa->uaa_release != UHUB_UNK_RELEASE)
1515 aprint_normal(" release 0x%04x", uaa->uaa_release);
1516 #endif
1517 return UNCONF;
1518 }
1519
1520 void
1521 usbd_fill_deviceinfo(struct usbd_device *dev, struct usb_device_info *di,
1522 int usedev)
1523 {
1524 struct usbd_port *p;
1525 int i, j, err, s;
1526
1527 di->udi_bus = device_unit(dev->ud_bus->ub_usbctl);
1528 di->udi_addr = dev->ud_addr;
1529 di->udi_cookie = dev->ud_cookie;
1530 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1531 di->udi_product, sizeof(di->udi_product), usedev, 1);
1532 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1533 UGETW(dev->ud_ddesc.bcdDevice));
1534 if (usedev) {
1535 usbd_status uerr = usbd_get_string(dev,
1536 dev->ud_ddesc.iSerialNumber, di->udi_serial);
1537 if (uerr != USBD_NORMAL_COMPLETION) {
1538 di->udi_serial[0] = '\0';
1539 } else {
1540 usbd_trim_spaces(di->udi_serial);
1541 }
1542 } else {
1543 di->udi_serial[0] = '\0';
1544 if (dev->ud_serial) {
1545 strlcpy(di->udi_serial, dev->ud_serial,
1546 sizeof(di->udi_serial));
1547 }
1548 }
1549
1550 di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor);
1551 di->udi_productNo = UGETW(dev->ud_ddesc.idProduct);
1552 di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice);
1553 di->udi_class = dev->ud_ddesc.bDeviceClass;
1554 di->udi_subclass = dev->ud_ddesc.bDeviceSubClass;
1555 di->udi_protocol = dev->ud_ddesc.bDeviceProtocol;
1556 di->udi_config = dev->ud_config;
1557 di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power;
1558 di->udi_speed = dev->ud_speed;
1559
1560 if (dev->ud_subdevlen > 0) {
1561 for (i = 0, j = 0; i < dev->ud_subdevlen &&
1562 j < USB_MAX_DEVNAMES; i++) {
1563 if (!dev->ud_subdevs[i])
1564 continue;
1565 strncpy(di->udi_devnames[j],
1566 device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN);
1567 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1568 j++;
1569 }
1570 } else {
1571 j = 0;
1572 }
1573 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1574 di->udi_devnames[j][0] = 0; /* empty */
1575
1576 if (dev->ud_hub) {
1577 for (i = 0;
1578 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1579 i < dev->ud_hub->uh_hubdesc.bNbrPorts;
1580 i++) {
1581 p = &dev->ud_hub->uh_ports[i];
1582 if (p->up_dev)
1583 err = p->up_dev->ud_addr;
1584 else {
1585 s = UGETW(p->up_status.wPortStatus);
1586 if (s & UPS_PORT_ENABLED)
1587 err = USB_PORT_ENABLED;
1588 else if (s & UPS_SUSPEND)
1589 err = USB_PORT_SUSPENDED;
1590 /*
1591 * Note: UPS_PORT_POWER_SS is available only
1592 * on 3.x, and UPS_PORT_POWER is available
1593 * only on 2.0 or 1.1.
1594 */
1595 else if (USB_IS_SS(dev->ud_speed) &&
1596 (s & UPS_PORT_POWER_SS))
1597 err = USB_PORT_POWERED;
1598 else if (s & UPS_PORT_POWER)
1599 err = USB_PORT_POWERED;
1600 else
1601 err = USB_PORT_DISABLED;
1602 }
1603 di->udi_ports[i] = err;
1604 }
1605 di->udi_nports = dev->ud_hub->uh_hubdesc.bNbrPorts;
1606 } else
1607 di->udi_nports = 0;
1608 }
1609
1610 #ifdef COMPAT_30
1611 void
1612 usbd_fill_deviceinfo_old(struct usbd_device *dev, struct usb_device_info_old *di,
1613 int usedev)
1614 {
1615 struct usbd_port *p;
1616 int i, j, err, s;
1617
1618 di->udi_bus = device_unit(dev->ud_bus->ub_usbctl);
1619 di->udi_addr = dev->ud_addr;
1620 di->udi_cookie = dev->ud_cookie;
1621 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor),
1622 di->udi_product, sizeof(di->udi_product), usedev, 0);
1623 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1624 UGETW(dev->ud_ddesc.bcdDevice));
1625 di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor);
1626 di->udi_productNo = UGETW(dev->ud_ddesc.idProduct);
1627 di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice);
1628 di->udi_class = dev->ud_ddesc.bDeviceClass;
1629 di->udi_subclass = dev->ud_ddesc.bDeviceSubClass;
1630 di->udi_protocol = dev->ud_ddesc.bDeviceProtocol;
1631 di->udi_config = dev->ud_config;
1632 di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power;
1633 di->udi_speed = dev->ud_speed;
1634
1635 if (dev->ud_subdevlen > 0) {
1636 for (i = 0, j = 0; i < dev->ud_subdevlen &&
1637 j < USB_MAX_DEVNAMES; i++) {
1638 if (!dev->ud_subdevs[i])
1639 continue;
1640 strncpy(di->udi_devnames[j],
1641 device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN);
1642 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1643 j++;
1644 }
1645 } else {
1646 j = 0;
1647 }
1648 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1649 di->udi_devnames[j][0] = 0; /* empty */
1650
1651 if (dev->ud_hub) {
1652 for (i = 0;
1653 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1654 i < dev->ud_hub->uh_hubdesc.bNbrPorts;
1655 i++) {
1656 p = &dev->ud_hub->uh_ports[i];
1657 if (p->up_dev)
1658 err = p->up_dev->ud_addr;
1659 else {
1660 s = UGETW(p->up_status.wPortStatus);
1661 if (s & UPS_PORT_ENABLED)
1662 err = USB_PORT_ENABLED;
1663 else if (s & UPS_SUSPEND)
1664 err = USB_PORT_SUSPENDED;
1665 else if (s & UPS_PORT_POWER)
1666 err = USB_PORT_POWERED;
1667 else
1668 err = USB_PORT_DISABLED;
1669 }
1670 di->udi_ports[i] = err;
1671 }
1672 di->udi_nports = dev->ud_hub->uh_hubdesc.bNbrPorts;
1673 } else
1674 di->udi_nports = 0;
1675 }
1676 #endif
1677
1678
1679 void
1680 usb_free_device(struct usbd_device *dev)
1681 {
1682 int ifcidx, nifc;
1683
1684 if (dev->ud_pipe0 != NULL)
1685 usbd_kill_pipe(dev->ud_pipe0);
1686 if (dev->ud_ifaces != NULL) {
1687 nifc = dev->ud_cdesc->bNumInterface;
1688 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1689 usbd_free_iface_data(dev, ifcidx);
1690 kmem_free(dev->ud_ifaces,
1691 nifc * sizeof(struct usbd_interface));
1692 }
1693 if (dev->ud_cdesc != NULL)
1694 kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength));
1695 if (dev->ud_bdesc != NULL)
1696 kmem_free(dev->ud_bdesc, UGETW(dev->ud_bdesc->wTotalLength));
1697 if (dev->ud_subdevlen > 0) {
1698 kmem_free(dev->ud_subdevs,
1699 dev->ud_subdevlen * sizeof(device_t));
1700 dev->ud_subdevlen = 0;
1701 }
1702 if (dev->ud_vendor) {
1703 kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN);
1704 }
1705 if (dev->ud_product) {
1706 kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN);
1707 }
1708 if (dev->ud_serial) {
1709 kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN);
1710 }
1711 kmem_free(dev, sizeof(*dev));
1712 }
1713
1714 /*
1715 * The general mechanism for detaching drivers works as follows: Each
1716 * driver is responsible for maintaining a reference count on the
1717 * number of outstanding references to its softc (e.g. from
1718 * processing hanging in a read or write). The detach method of the
1719 * driver decrements this counter and flags in the softc that the
1720 * driver is dying and then wakes any sleepers. It then sleeps on the
1721 * softc. Each place that can sleep must maintain the reference
1722 * count. When the reference count drops to -1 (0 is the normal value
1723 * of the reference count) the a wakeup on the softc is performed
1724 * signaling to the detach waiter that all references are gone.
1725 */
1726
1727 /*
1728 * Called from process context when we discover that a port has
1729 * been disconnected.
1730 */
1731 int
1732 usb_disconnect_port(struct usbd_port *up, device_t parent, int flags)
1733 {
1734 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1735 struct usbd_device *dev = up->up_dev;
1736 device_t subdev;
1737 char subdevname[16];
1738 const char *hubname = device_xname(parent);
1739 int i, rc;
1740
1741 DPRINTFN(3, "up=%p dev=%p port=%d", up, dev, up->up_portno, 0);
1742
1743 if (dev == NULL) {
1744 return 0;
1745 }
1746
1747 if (dev->ud_subdevlen > 0) {
1748 DPRINTFN(3, "disconnect subdevs", 0, 0, 0, 0);
1749 for (i = 0; i < dev->ud_subdevlen; i++) {
1750 if ((subdev = dev->ud_subdevs[i]) == NULL)
1751 continue;
1752 strlcpy(subdevname, device_xname(subdev),
1753 sizeof(subdevname));
1754 if ((rc = config_detach(subdev, flags)) != 0)
1755 return rc;
1756 printf("%s: at %s", subdevname, hubname);
1757 if (up->up_portno != 0)
1758 printf(" port %d", up->up_portno);
1759 printf(" (addr %d) disconnected\n", dev->ud_addr);
1760 }
1761 KASSERT(!dev->ud_nifaces_claimed);
1762 }
1763
1764 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1765 dev->ud_bus->ub_devices[dev->ud_addr] = NULL;
1766 up->up_dev = NULL;
1767 usb_free_device(dev);
1768 return 0;
1769 }
1770