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