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