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