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