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