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