usb_subr.c revision 1.7 1 /* $NetBSD: usb_subr.c,v 1.7 1998/08/02 22:30:53 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Author: Lennart Augustsson <augustss (at) carlstedt.se>
8 * Carlstedt Research & Technology
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/device.h>
44 #include <sys/proc.h>
45 #include <sys/select.h>
46
47 #include <dev/usb/usb.h>
48
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
51 #include <dev/usb/usbdivar.h>
52 #include <dev/usb/usbdevs.h>
53 #include <dev/usb/usb_quirks.h>
54
55 #include "opt_usbverbose.h"
56
57 #ifdef USB_DEBUG
58 #define DPRINTF(x) if (usbdebug) printf x
59 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x
60 extern int usbdebug;
61 #else
62 #define DPRINTF(x)
63 #define DPRINTFN(n,x)
64 #endif
65
66 static usbd_status usbd_set_config __P((usbd_device_handle, int));
67 char *usbd_get_string __P((usbd_device_handle, int, char *));
68 usbd_status usbd_get_desc __P((usbd_device_handle dev, int type,
69 int index, int len, void *desc));
70 int usbd_getnewaddr __P((usbd_bus_handle bus));
71 int usbd_print __P((void *aux, const char *pnp));
72 int usbd_submatch __P((struct device *, struct cfdata *cf, void *));
73 usb_interface_descriptor_t *usbd_find_idesc __P((usb_config_descriptor_t *cd,
74 int ino, int ano));
75 usbd_status usbd_fill_iface_data __P((usbd_device_handle dev, int i, int a));
76 void usbd_free_iface_data __P((usbd_device_handle dev, int ifcno));
77 void usbd_kill_pipe __P((usbd_pipe_handle));
78
79 #ifdef USBVERBOSE
80 typedef u_int16_t usb_vendor_id_t;
81 typedef u_int16_t usb_product_id_t;
82
83 /*
84 * Descriptions of of known vendors and devices ("products").
85 */
86 struct usb_knowndev {
87 usb_vendor_id_t vendor;
88 usb_product_id_t product;
89 int flags;
90 char *vendorname, *productname;
91 };
92 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
93
94 #include <dev/usb/usbdevs_data.h>
95 #endif /* USBVERBOSE */
96
97
98 char *
99 usbd_get_string(dev, si, buf)
100 usbd_device_handle dev;
101 int si;
102 char *buf;
103 {
104 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
105 usb_device_request_t req;
106 usb_string_descriptor_t us;
107 char *s;
108 int i, n;
109 u_int16_t c;
110 usbd_status r;
111
112 if (si == 0)
113 return 0;
114 req.bmRequestType = UT_READ_DEVICE;
115 req.bRequest = UR_GET_DESCRIPTOR;
116 USETW2(req.wValue, UDESC_STRING, si);
117 USETW(req.wIndex, 0);
118 USETW(req.wLength, 1); /* only size byte first */
119 r = usbd_do_request(dev, &req, &us);
120 if (r != USBD_NORMAL_COMPLETION)
121 return 0;
122 USETW(req.wLength, us.bLength); /* the whole string */
123 r = usbd_do_request(dev, &req, &us);
124 if (r != USBD_NORMAL_COMPLETION)
125 return 0;
126 s = buf;
127 n = us.bLength / 2 - 1;
128 for (i = 0; i < n; i++) {
129 c = UGETW(us.bString[i]);
130 /* Convert from Unicode, handle buggy strings. */
131 if ((c & 0xff00) == 0)
132 *s++ = c;
133 else if ((c & 0x00ff) == 0 && swap)
134 *s++ = c >> 8;
135 else
136 *s++ = '?';
137 }
138 *s++ = 0;
139 return buf;
140 }
141
142 void
143 usbd_devinfo_vp(dev, v, p)
144 usbd_device_handle dev;
145 char *v, *p;
146 {
147 usb_device_descriptor_t *udd = &dev->ddesc;
148 char *vendor = 0, *product = 0;
149 #ifdef USBVERBOSE
150 struct usb_knowndev *kdp;
151 #endif
152
153 vendor = usbd_get_string(dev, udd->iManufacturer, v);
154 product = usbd_get_string(dev, udd->iProduct, p);
155 #ifdef USBVERBOSE
156 if (!vendor) {
157 for(kdp = usb_knowndevs;
158 kdp->vendorname != NULL;
159 kdp++) {
160 if (kdp->vendor == UGETW(udd->idVendor) &&
161 (kdp->product == UGETW(udd->idProduct) ||
162 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
163 break;
164 }
165 if (kdp->vendorname == NULL)
166 vendor = product = NULL;
167 else {
168 vendor = kdp->vendorname;
169 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
170 kdp->productname : NULL;
171 }
172 }
173 #endif
174 if (vendor)
175 strcpy(v, vendor);
176 else
177 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
178 if (product)
179 strcpy(p, product);
180 else
181 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
182 }
183
184 int
185 usbd_printBCD(cp, bcd)
186 char *cp;
187 int bcd;
188 {
189 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
190 }
191
192 void
193 usbd_devinfo(dev, showclass, cp)
194 usbd_device_handle dev;
195 int showclass;
196 char *cp;
197 {
198 usb_device_descriptor_t *udd = &dev->ddesc;
199 char vendor[USB_MAX_STRING_LEN];
200 char product[USB_MAX_STRING_LEN];
201 int bcdDevice, bcdUSB;
202
203 usbd_devinfo_vp(dev, vendor, product);
204 cp += sprintf(cp, "%s", vendor);
205 cp += sprintf(cp, " %s", product);
206 if (showclass)
207 cp += sprintf(cp, " (class %d/%d)",
208 udd->bDeviceClass, udd->bDeviceSubClass);
209 bcdUSB = UGETW(udd->bcdUSB);
210 bcdDevice = UGETW(udd->bcdDevice);
211 cp += sprintf(cp, " (rev ");
212 cp += usbd_printBCD(cp, bcdUSB);
213 *cp++ = '/';
214 cp += usbd_printBCD(cp, bcdDevice);
215 *cp++ = ')';
216 cp += sprintf(cp, " (addr %d)", dev->address);
217 }
218
219 /* Delay for a certain number of ms */
220 void
221 usbd_delay_ms(bus, ms)
222 usbd_bus_handle bus;
223 int ms;
224 {
225 /* Wait at least two clock ticks so we know the time has passed. */
226 if (bus->use_polling)
227 delay((ms+1) * 1000);
228 else
229 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
230 }
231
232 usbd_status
233 usbd_reset_port(dev, port, ps)
234 usbd_device_handle dev;
235 int port;
236 usb_port_status_t *ps;
237 {
238 usb_device_request_t req;
239 usbd_status r;
240 int n;
241
242 req.bmRequestType = UT_WRITE_CLASS_OTHER;
243 req.bRequest = UR_SET_FEATURE;
244 USETW(req.wValue, UHF_PORT_RESET);
245 USETW(req.wIndex, port);
246 USETW(req.wLength, 0);
247 r = usbd_do_request(dev, &req, 0);
248 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%d\n",
249 port, r));
250 if (r != USBD_NORMAL_COMPLETION)
251 return (r);
252 n = 10;
253 do {
254 /* Wait for device to recover from reset. */
255 usbd_delay_ms(dev->bus, USB_PORT_RESET_DELAY);
256 r = usbd_get_port_status(dev, port, ps);
257 if (r != USBD_NORMAL_COMPLETION) {
258 DPRINTF(("usbd_reset_port: get status failed %d\n",r));
259 return (r);
260 }
261 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
262 if (n == 0) {
263 printf("usbd_reset_port: timeout\n");
264 return (USBD_IOERROR);
265 }
266 r = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
267 #ifdef USB_DEBUG
268 if (r != USBD_NORMAL_COMPLETION)
269 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",r));
270 #endif
271 return (r);
272 }
273
274 usb_interface_descriptor_t *
275 usbd_find_idesc(cd, ino, ano)
276 usb_config_descriptor_t *cd;
277 int ino;
278 int ano;
279 {
280 char *p = (char *)cd;
281 char *end = p + UGETW(cd->wTotalLength);
282 usb_interface_descriptor_t *d;
283
284 for (; p < end; p += d->bLength) {
285 d = (usb_interface_descriptor_t *)p;
286 if (p + d->bLength <= end &&
287 d->bDescriptorType == UDESC_INTERFACE &&
288 d->bInterfaceNumber == ino && d->bAlternateSetting == ano)
289 return (d);
290 }
291 return (0);
292 }
293
294 usbd_status
295 usbd_fill_iface_data(dev, ino, ano)
296 usbd_device_handle dev;
297 int ino;
298 int ano;
299 {
300 usbd_interface_handle ifc = &dev->ifaces[ino];
301 usb_endpoint_descriptor_t *ed;
302 char *p, *end;
303 int endpt, nendpt;
304 usbd_status r;
305
306 DPRINTFN(5,("usbd_fill_iface_data: ino=%d ano=%d\n", ino, ano));
307 ifc->device = dev;
308 ifc->state = USBD_INTERFACE_ACTIVE;
309 ifc->idesc = usbd_find_idesc(dev->cdesc, ino, ano);
310 if (ifc->idesc == 0)
311 return (USBD_INVAL);
312 nendpt = ifc->idesc->bNumEndpoints;
313 DPRINTFN(10,("usbd_fill_iface_data: found idesc n=%d\n", nendpt));
314 if (nendpt != 0) {
315 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
316 M_USB, M_NOWAIT);
317 if (ifc->endpoints == 0)
318 return (USBD_NOMEM);
319 } else
320 ifc->endpoints = 0;
321 ifc->priv = 0;
322 p = (char *)ifc->idesc + ifc->idesc->bLength;
323 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
324 for (endpt = 0; endpt < nendpt; endpt++) {
325 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
326 for (; p < end; p += ed->bLength) {
327 ed = (usb_endpoint_descriptor_t *)p;
328 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p len=%d type=%d\n",
329 p, end, ed->bLength, ed->bDescriptorType));
330 if (p + ed->bLength <= end &&
331 ed->bDescriptorType == UDESC_ENDPOINT)
332 goto found;
333 if (ed->bDescriptorType == UDESC_INTERFACE)
334 break;
335 }
336 r = USBD_INVAL;
337 goto bad;
338 found:
339 ifc->endpoints[endpt].edesc = ed;
340 ifc->endpoints[endpt].state = USBD_ENDPOINT_ACTIVE;
341 ifc->endpoints[endpt].refcnt = 0;
342 ifc->endpoints[endpt].toggle = 0;
343 }
344 LIST_INIT(&ifc->pipes);
345 return (USBD_NORMAL_COMPLETION);
346 bad:
347 free(ifc->endpoints, M_USB);
348 return (r);
349 }
350
351 void
352 usbd_free_iface_data(dev, ifcno)
353 usbd_device_handle dev;
354 int ifcno;
355 {
356 usbd_interface_handle ifc = &dev->ifaces[ifcno];
357 if (ifc->endpoints)
358 free(ifc->endpoints, M_USB);
359 }
360
361 static usbd_status
362 usbd_set_config(dev, conf)
363 usbd_device_handle dev;
364 int conf;
365 {
366 usb_device_request_t req;
367
368 req.bmRequestType = UT_WRITE_DEVICE;
369 req.bRequest = UR_SET_CONFIG;
370 USETW(req.wValue, conf);
371 USETW(req.wIndex, 0);
372 USETW(req.wLength, 0);
373 return (usbd_do_request(dev, &req, 0));
374 }
375
376 usbd_status
377 usbd_set_config_no(dev, no, msg)
378 usbd_device_handle dev;
379 int no;
380 int msg;
381 {
382 usb_status_t ds;
383 usb_hub_status_t hs;
384 usb_config_descriptor_t cd, *cdp;
385 usbd_status r;
386 int ifcno, nifc, len, selfpowered, power;
387
388 DPRINTFN(5, ("usbd_set_config_no: dev=%p no=%d\n", dev, no));
389
390 /* XXX check that all interfaces are idle */
391 if (dev->config != 0) {
392 DPRINTF(("usbd_set_config_no: free old config\n"));
393 /* Free all configuration data structures. */
394 nifc = dev->cdesc->bNumInterface;
395 for (ifcno = 0; ifcno < nifc; ifcno++)
396 usbd_free_iface_data(dev, ifcno);
397 free(dev->ifaces, M_USB);
398 free(dev->cdesc, M_USB);
399 dev->ifaces = 0;
400 dev->cdesc = 0;
401 dev->config = 0;
402 dev->state = USBD_DEVICE_ADDRESSED;
403 }
404
405 /* Figure out what config number to use. */
406 r = usbd_get_config_desc(dev, no, &cd);
407 if (r != USBD_NORMAL_COMPLETION)
408 return (r);
409 len = UGETW(cd.wTotalLength);
410 cdp = malloc(len, M_USB, M_NOWAIT);
411 if (cdp == 0)
412 return (USBD_NOMEM);
413 r = usbd_get_desc(dev, UDESC_CONFIG, no, len, cdp);
414 if (r != USBD_NORMAL_COMPLETION)
415 goto bad;
416 selfpowered = 0;
417 if (cdp->bmAttributes & UC_SELF_POWERED) {
418 /* May be self powered. */
419 if (cdp->bmAttributes & UC_BUS_POWERED) {
420 /* Must ask device. */
421 if (dev->quirks->uq_flags & UQ_HUB_POWER) {
422 /* Buggy hub, use hub descriptor. */
423 r = usbd_get_hub_status(dev, &hs);
424 if (r == USBD_NORMAL_COMPLETION &&
425 !(UGETW(hs.wHubStatus) & UHS_LOCAL_POWER))
426 selfpowered = 1;
427 } else {
428 r = usbd_get_device_status(dev, &ds);
429 if (r == USBD_NORMAL_COMPLETION &&
430 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
431 selfpowered = 1;
432 }
433 DPRINTF(("usbd_set_config_no: status=0x%04x, error=%d\n",
434 UGETW(ds.wStatus), r));
435 } else
436 selfpowered = 1;
437 }
438 DPRINTF(("usbd_set_config_no: (addr %d) attr=0x%02x, selfpowered=%d, power=%d, powerquirk=%x\n",
439 dev->address, cdp->bmAttributes,
440 selfpowered, cdp->bMaxPower * 2,
441 dev->quirks->uq_flags & UQ_HUB_POWER));
442 #ifdef USB_DEBUG
443 if (!dev->powersrc) {
444 printf("usbd_set_config_no: No power source?\n");
445 return (EIO);
446 }
447 #endif
448 power = cdp->bMaxPower * 2;
449 if (power > dev->powersrc->power) {
450 /* XXX print nicer message. */
451 if (msg)
452 printf("%s: device addr %d (config %d) exceeds power budget, %d mA > %d mA\n",
453 dev->bus->bdev.dv_xname, dev->address,
454 cdp->bConfigurationValue,
455 power, dev->powersrc->power);
456 r = USBD_NO_POWER;
457 goto bad;
458 }
459 dev->power = power;
460 dev->self_powered = selfpowered;
461
462 r = usbd_set_config(dev, cdp->bConfigurationValue);
463 if (r != USBD_NORMAL_COMPLETION) {
464 DPRINTF(("usbd_set_config_no: setting config=%d failed, error=%d\n",
465 cdp->bConfigurationValue, r));
466 goto bad;
467 }
468 DPRINTF(("usbd_set_config_no: setting new config %d\n",
469 cdp->bConfigurationValue));
470 nifc = cdp->bNumInterface;
471 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
472 M_USB, M_NOWAIT);
473 if (dev->ifaces == 0) {
474 r = USBD_NOMEM;
475 goto bad;
476 }
477 DPRINTFN(5,("usbd_set_config_no: dev=%p cdesc=%p\n", dev, cdp));
478 dev->cdesc = cdp;
479 dev->config = cdp->bConfigurationValue;
480 dev->state = USBD_DEVICE_CONFIGURED;
481 for (ifcno = 0; ifcno < nifc; ifcno++) {
482 r = usbd_fill_iface_data(dev, ifcno, 0);
483 if (r != USBD_NORMAL_COMPLETION) {
484 while (--ifcno >= 0)
485 usbd_free_iface_data(dev, ifcno);
486 goto bad;
487 }
488 }
489
490 return (USBD_NORMAL_COMPLETION);
491
492 bad:
493 free(cdp, M_USB);
494 return (r);
495 }
496
497 /* XXX add function for alternate settings */
498
499 usbd_status
500 usbd_setup_pipe(dev, iface, ep, pipe)
501 usbd_device_handle dev;
502 usbd_interface_handle iface;
503 struct usbd_endpoint *ep;
504 usbd_pipe_handle *pipe;
505 {
506 usbd_pipe_handle p;
507 usbd_status r;
508
509 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
510 dev, iface, ep, pipe));
511 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
512 if (p == 0)
513 return (USBD_NOMEM);
514 p->device = dev;
515 p->iface = iface;
516 p->state = USBD_PIPE_ACTIVE;
517 p->endpoint = ep;
518 ep->refcnt++;
519 p->refcnt = 1;
520 p->intrreqh = 0;
521 p->running = 0;
522 SIMPLEQ_INIT(&p->queue);
523 r = dev->bus->open_pipe(p);
524 if (r != USBD_NORMAL_COMPLETION) {
525 DPRINTF(("usbd_setup_pipe: endpoint=%d failed, error=%d\n",
526 ep->edesc->bEndpointAddress, r));
527 free(p, M_USB);
528 return (r);
529 }
530 *pipe = p;
531 return (USBD_NORMAL_COMPLETION);
532 }
533
534 /* Abort the device control pipe. */
535 void
536 usbd_kill_pipe(pipe)
537 usbd_pipe_handle pipe;
538 {
539 pipe->methods->close(pipe);
540 pipe->endpoint->refcnt--;
541 free(pipe, M_USB);
542 }
543
544 int
545 usbd_getnewaddr(bus)
546 usbd_bus_handle bus;
547 {
548 int i;
549
550 for (i = 1; i < USB_MAX_DEVICES; i++)
551 if (bus->devices[i] == 0)
552 return (i);
553 return (-1);
554 }
555
556 /*
557 * Called when a new device has been put in the powered state,
558 * but not yet in the addressed state.
559 * Get initial descriptor, set the address, get full descriptor,
560 * and attach a driver.
561 */
562 usbd_status
563 usbd_new_device(parent, bus, depth, lowspeed, port, up)
564 struct device *parent;
565 usbd_bus_handle bus;
566 int depth;
567 int lowspeed;
568 int port;
569 struct usbd_port *up;
570 {
571 usbd_device_handle dev;
572 usb_device_descriptor_t *d;
573 usbd_status r;
574 struct usb_attach_arg uaa;
575 int addr;
576 int found, i, confi;
577
578 DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
579 bus, depth, lowspeed));
580 addr = usbd_getnewaddr(bus);
581 if (addr < 0) {
582 printf("%s: No free USB addresses, new device ignored.\n",
583 bus->bdev.dv_xname);
584 return (USBD_NO_ADDR);
585 }
586
587 dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
588 if (dev == 0)
589 return (USBD_NOMEM);
590 memset(dev, 0, sizeof(*dev));
591
592 dev->bus = bus;
593
594 /* Set up default endpoint handle. */
595 dev->def_ep.edesc = &dev->def_ep_desc;
596 dev->def_ep.state = USBD_ENDPOINT_ACTIVE;
597 dev->def_ep.refcnt = 0;
598 dev->def_ep.toggle = 0; /* XXX */
599
600 /* Set up default endpoint descriptor. */
601 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
602 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
603 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
604 dev->def_ep_desc.bmAttributes = UE_CONTROL;
605 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
606 dev->def_ep_desc.bInterval = 0;
607
608 dev->state = USBD_DEVICE_DEFAULT;
609 dev->quirks = &usbd_no_quirk;
610 dev->address = USB_START_ADDR;
611 dev->ddesc.bMaxPacketSize = 0;
612 dev->lowspeed = lowspeed != 0;
613 dev->depth = depth;
614 dev->powersrc = up;
615
616 /* Establish the the default pipe. */
617 r = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
618 if (r != USBD_NORMAL_COMPLETION)
619 goto bad1;
620
621 up->device = dev;
622 d = &dev->ddesc;
623 /* Try a few times in case the device is slow (i.e. outside specs.) */
624 for (i = 0; i < 5; i++) {
625 /* Get the first 8 bytes of the device descriptor. */
626 r = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, d);
627 if (r == USBD_NORMAL_COMPLETION)
628 break;
629 usbd_delay_ms(dev->bus, 200);
630 }
631 if (r != USBD_NORMAL_COMPLETION) {
632 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc failed\n",
633 addr));
634 goto bad;
635 }
636
637 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, subclass=%d, protocol=%d, maxpacket=%d, ls=%d\n",
638 addr, UGETW(d->bcdUSB), d->bDeviceClass, d->bDeviceSubClass,
639 d->bDeviceProtocol, d->bMaxPacketSize, dev->lowspeed));
640
641 USETW(dev->def_ep_desc.wMaxPacketSize, d->bMaxPacketSize);
642
643 /* Get the full device descriptor. */
644 r = usbd_get_device_desc(dev, d);
645 if (r != USBD_NORMAL_COMPLETION) {
646 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc failed\n", addr));
647 goto bad;
648 }
649
650 /* Figure out what's wrong with this device. */
651 dev->quirks = usbd_find_quirk(d);
652
653 /* Set the address */
654 r = usbd_set_address(dev, addr);
655 if (r != USBD_NORMAL_COMPLETION) {
656 DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
657 r = USBD_SET_ADDR_FAILED;
658 goto bad;
659 }
660 dev->address = addr; /* New device address now */
661 dev->state = USBD_DEVICE_ADDRESSED;
662 bus->devices[addr] = dev;
663
664 /* Assume 100mA bus powered for now. Changed when configured. */
665 dev->power = USB_MIN_POWER;
666 dev->self_powered = 0;
667
668 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
669 addr, dev, parent));
670
671 uaa.device = dev;
672 uaa.iface = 0;
673 uaa.usegeneric = 0;
674 uaa.port = port;
675 /* First try with device specific drivers. */
676 if (config_found_sm(parent, &uaa, usbd_print, usbd_submatch) != 0)
677 return (USBD_NORMAL_COMPLETION);
678
679 DPRINTF(("usbd_new_device: no device driver found\n"));
680
681 /* Next try with interface drivers. */
682 for (confi = 0; confi < d->bNumConfigurations; confi++) {
683 r = usbd_set_config_no(dev, confi, 1);
684 if (r != USBD_NORMAL_COMPLETION) {
685 printf("%s: set config at addr %d failed, error=%d\n",
686 parent->dv_xname, addr, r);
687 goto bad;
688 }
689 for (found = i = 0; i < dev->cdesc->bNumInterface; i++) {
690 uaa.iface = &dev->ifaces[i];
691 if (config_found_sm(parent, &uaa, usbd_print,
692 usbd_submatch))
693 found++;
694 }
695 if (found != 0)
696 return (USBD_NORMAL_COMPLETION);
697 }
698 /* No interfaces were attach in any of the configurations. */
699 if (d->bNumConfigurations > 0)
700 usbd_set_config_no(dev, 0, 0);
701
702 DPRINTF(("usbd_new_device: no interface drivers found\n"));
703
704 /* Finally try the generic driver. */
705 uaa.iface = 0;
706 uaa.usegeneric = 1;
707 if (config_found_sm(parent, &uaa, usbd_print, usbd_submatch) != 0)
708 return (USBD_NORMAL_COMPLETION);
709
710 DPRINTF(("usbd_new_device: generic attach failed\n"));
711
712 return (USBD_NORMAL_COMPLETION);
713
714 bad:
715 usbd_kill_pipe(dev->default_pipe);
716 bad1:
717 up->device = 0;
718 bus->devices[addr] = 0;
719 free(dev, M_USB);
720 return (r);
721 }
722
723 int
724 usbd_print(aux, pnp)
725 void *aux;
726 const char *pnp;
727 {
728 struct usb_attach_arg *uaa = aux;
729 char devinfo[1024];
730
731 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
732 if (pnp) {
733 if (!uaa->usegeneric)
734 return (QUIET);
735 usbd_devinfo(uaa->device, 1, devinfo);
736 printf("%s at %s", devinfo, pnp);
737 }
738 if (uaa->port != 0)
739 printf(" port %d", uaa->port);
740 return (UNCONF);
741 }
742
743 int
744 usbd_submatch(parent, cf, aux)
745 struct device *parent;
746 struct cfdata *cf;
747 void *aux;
748 {
749 struct usb_attach_arg *uaa = aux;
750
751 if (uaa->port != 0 &&
752 cf->uhubcf_port != UHUB_UNK_PORT &&
753 cf->uhubcf_port != uaa->port)
754 return 0;
755 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
756 }
757