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