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