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