usb_subr.c revision 1.5 1 /* $NetBSD: usb_subr.c,v 1.5 1998/08/01 18:16:20 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 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(bus, ms)
221 usbd_bus_handle bus;
222 int ms;
223 {
224 /* Wait at least two clock ticks so we know the time has passed. */
225 if (bus->use_polling)
226 delay((ms+1) * 1000);
227 else
228 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
229 }
230
231 usbd_status
232 usbd_reset_port(dev, port, ps)
233 usbd_device_handle dev;
234 int port;
235 usb_port_status_t *ps;
236 {
237 usb_device_request_t req;
238 usbd_status r;
239 int n;
240
241 req.bmRequestType = UT_WRITE_CLASS_OTHER;
242 req.bRequest = UR_SET_FEATURE;
243 USETW(req.wValue, UHF_PORT_RESET);
244 USETW(req.wIndex, port);
245 USETW(req.wLength, 0);
246 r = usbd_do_request(dev, &req, 0);
247 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%d\n",
248 port, r));
249 if (r != USBD_NORMAL_COMPLETION)
250 return (r);
251 n = 10;
252 do {
253 /* Wait for device to recover from reset. */
254 usbd_delay_ms(dev->bus, USB_PORT_RESET_DELAY);
255 r = usbd_get_port_status(dev, port, ps);
256 if (r != USBD_NORMAL_COMPLETION) {
257 DPRINTF(("usbd_reset_port: get status failed %d\n",r));
258 return (r);
259 }
260 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
261 if (n == 0) {
262 printf("usbd_reset_port: timeout\n");
263 return (USBD_IOERROR);
264 }
265 r = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
266 #ifdef USB_DEBUG
267 if (r != USBD_NORMAL_COMPLETION)
268 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",r));
269 #endif
270 return (r);
271 }
272
273 usb_interface_descriptor_t *
274 usbd_find_idesc(cd, ino, ano)
275 usb_config_descriptor_t *cd;
276 int ino;
277 int ano;
278 {
279 char *p = (char *)cd;
280 char *end = p + UGETW(cd->wTotalLength);
281 usb_interface_descriptor_t *d;
282
283 for (; p < end; p += d->bLength) {
284 d = (usb_interface_descriptor_t *)p;
285 if (p + d->bLength <= end &&
286 d->bDescriptorType == UDESC_INTERFACE &&
287 d->bInterfaceNumber == ino && d->bAlternateSetting == ano)
288 return (d);
289 }
290 return (0);
291 }
292
293 usbd_status
294 usbd_fill_iface_data(dev, ino, ano)
295 usbd_device_handle dev;
296 int ino;
297 int ano;
298 {
299 usbd_interface_handle ifc = &dev->ifaces[ino];
300 usb_endpoint_descriptor_t *ed;
301 char *p, *end;
302 int endpt, nendpt;
303 usbd_status r;
304
305 DPRINTFN(5,("usbd_fill_iface_data: ino=%d ano=%d\n", ino, ano));
306 ifc->device = dev;
307 ifc->state = USBD_INTERFACE_ACTIVE;
308 ifc->idesc = usbd_find_idesc(dev->cdesc, ino, ano);
309 if (ifc->idesc == 0)
310 return (USBD_INVAL);
311 nendpt = ifc->idesc->bNumEndpoints;
312 DPRINTFN(10,("usbd_fill_iface_data: found idesc n=%d\n", nendpt));
313 if (nendpt != 0) {
314 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
315 M_USB, M_NOWAIT);
316 if (ifc->endpoints == 0)
317 return (USBD_NOMEM);
318 } else
319 ifc->endpoints = 0;
320 ifc->priv = 0;
321 p = (char *)ifc->idesc + ifc->idesc->bLength;
322 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
323 for (endpt = 0; endpt < nendpt; endpt++) {
324 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
325 for (; p < end; p += ed->bLength) {
326 ed = (usb_endpoint_descriptor_t *)p;
327 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p len=%d type=%d\n",
328 p, end, ed->bLength, ed->bDescriptorType));
329 if (p + ed->bLength <= end &&
330 ed->bDescriptorType == UDESC_ENDPOINT)
331 goto found;
332 if (ed->bDescriptorType == UDESC_INTERFACE)
333 break;
334 }
335 r = USBD_INVAL;
336 goto bad;
337 found:
338 ifc->endpoints[endpt].edesc = ed;
339 ifc->endpoints[endpt].state = USBD_ENDPOINT_ACTIVE;
340 ifc->endpoints[endpt].refcnt = 0;
341 ifc->endpoints[endpt].toggle = 0;
342 }
343 LIST_INIT(&ifc->pipes);
344 return (USBD_NORMAL_COMPLETION);
345 bad:
346 free(ifc->endpoints, M_USB);
347 return (r);
348 }
349
350 void
351 usbd_free_iface_data(dev, ifcno)
352 usbd_device_handle dev;
353 int ifcno;
354 {
355 usbd_interface_handle ifc = &dev->ifaces[ifcno];
356 if (ifc->endpoints)
357 free(ifc->endpoints, M_USB);
358 }
359
360 usbd_status
361 usbd_set_config_no(dev, no)
362 usbd_device_handle dev;
363 int no;
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 r = usbd_set_config(dev, cdp->bConfigurationValue);
444 if (r != USBD_NORMAL_COMPLETION) {
445 DPRINTF(("usbd_set_config_no: setting config=%d failed, error=%d\n",
446 cdp->bConfigurationValue, r));
447 goto bad;
448 }
449 DPRINTF(("usbd_set_config_no: setting new config %d\n",
450 cdp->bConfigurationValue));
451 nifc = cdp->bNumInterface;
452 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
453 M_USB, M_NOWAIT);
454 if (dev->ifaces == 0) {
455 r = USBD_NOMEM;
456 goto bad;
457 }
458 dev->cdesc = cdp;
459 dev->config = cdp->bConfigurationValue;
460 dev->state = USBD_DEVICE_CONFIGURED;
461 for (ifcno = 0; ifcno < nifc; ifcno++) {
462 r = usbd_fill_iface_data(dev, ifcno, 0);
463 if (r != USBD_NORMAL_COMPLETION) {
464 while (--ifcno >= 0)
465 usbd_free_iface_data(dev, ifcno);
466 goto bad;
467 }
468 }
469
470 return (USBD_NORMAL_COMPLETION);
471
472 bad:
473 free(cdp, M_USB);
474 return (r);
475 }
476
477 /* XXX add function for alternate settings */
478
479 usbd_status
480 usbd_setup_pipe(dev, iface, ep, pipe)
481 usbd_device_handle dev;
482 usbd_interface_handle iface;
483 struct usbd_endpoint *ep;
484 usbd_pipe_handle *pipe;
485 {
486 usbd_pipe_handle p;
487 usbd_status r;
488
489 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
490 dev, iface, ep, pipe));
491 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
492 if (p == 0)
493 return (USBD_NOMEM);
494 p->device = dev;
495 p->iface = iface;
496 p->state = USBD_PIPE_ACTIVE;
497 p->endpoint = ep;
498 ep->refcnt++;
499 p->refcnt = 1;
500 p->intrreqh = 0;
501 p->running = 0;
502 SIMPLEQ_INIT(&p->queue);
503 r = dev->bus->open_pipe(p);
504 if (r != USBD_NORMAL_COMPLETION) {
505 DPRINTF(("usbd_setup_pipe: endpoint=%d failed, error=%d\n",
506 ep->edesc->bEndpointAddress, r));
507 free(p, M_USB);
508 return (r);
509 }
510 *pipe = p;
511 return (USBD_NORMAL_COMPLETION);
512 }
513
514 /* Abort the device control pipe. */
515 void
516 usbd_kill_pipe(pipe)
517 usbd_pipe_handle pipe;
518 {
519 pipe->methods->close(pipe);
520 pipe->endpoint->refcnt--;
521 free(pipe, M_USB);
522 }
523
524 int
525 usbd_getnewaddr(bus)
526 usbd_bus_handle bus;
527 {
528 int i;
529
530 for (i = 1; i < USB_MAX_DEVICES; i++)
531 if (bus->devices[i] == 0)
532 return (i);
533 return (-1);
534 }
535
536 /*
537 * Called when a new device has been put in the powered state,
538 * but not yet in the addressed state.
539 * Get initial descriptor, set the address, get full descriptor,
540 * and attach a driver.
541 */
542 usbd_status
543 usbd_new_device(parent, bus, depth, lowspeed, port, up)
544 struct device *parent;
545 usbd_bus_handle bus;
546 int depth;
547 int lowspeed;
548 int port;
549 struct usbd_port *up;
550 {
551 usbd_device_handle dev;
552 usb_device_descriptor_t *d;
553 usbd_status r;
554 struct usb_attach_arg uaa;
555 int addr;
556 int found, i, confi;
557
558 DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
559 bus, depth, lowspeed));
560 addr = usbd_getnewaddr(bus);
561 if (addr < 0) {
562 printf("%s: No free USB addresses, new device ignored.\n",
563 bus->bdev.dv_xname);
564 return (USBD_NO_ADDR);
565 }
566
567 dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
568 if (dev == 0)
569 return (USBD_NOMEM);
570 memset(dev, 0, sizeof(*dev));
571
572 dev->bus = bus;
573
574 /* Set up default endpoint handle. */
575 dev->def_ep.edesc = &dev->def_ep_desc;
576 dev->def_ep.state = USBD_ENDPOINT_ACTIVE;
577 dev->def_ep.refcnt = 0;
578 dev->def_ep.toggle = 0; /* XXX */
579
580 /* Set up default endpoint descriptor. */
581 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
582 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
583 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
584 dev->def_ep_desc.bmAttributes = UE_CONTROL;
585 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
586 dev->def_ep_desc.bInterval = 0;
587
588 dev->state = USBD_DEVICE_DEFAULT;
589 dev->quirks = &usbd_no_quirk;
590 dev->address = USB_START_ADDR;
591 dev->ddesc.bMaxPacketSize = 0;
592 dev->lowspeed = lowspeed != 0;
593 dev->depth = depth;
594 dev->powersrc = up;
595
596 /* Establish the the default pipe. */
597 r = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
598 if (r != USBD_NORMAL_COMPLETION)
599 goto bad1;
600
601 up->device = dev;
602 d = &dev->ddesc;
603 /* Get the first 8 bytes of the device descriptor. */
604 r = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, d);
605 if (r != USBD_NORMAL_COMPLETION) {
606 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc failed\n", addr));
607 goto bad;
608 }
609
610 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, subclass=%d, protocol=%d, maxpacket=%d, ls=%d\n",
611 addr, UGETW(d->bcdUSB), d->bDeviceClass, d->bDeviceSubClass,
612 d->bDeviceProtocol, d->bMaxPacketSize, dev->lowspeed));
613
614 USETW(dev->def_ep_desc.wMaxPacketSize, d->bMaxPacketSize);
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
656 for (confi = 0; confi < d->bNumConfigurations; confi++) {
657 r = usbd_set_config_no(dev, confi);
658 if (r != USBD_NORMAL_COMPLETION) {
659 printf("%s: set config at addr %d failed, error=%d\n",
660 parent->dv_xname, addr, r);
661 goto bad;
662 }
663 for (found = i = 0; i < dev->cdesc->bNumInterface; i++) {
664 uaa.iface = &dev->ifaces[i];
665 if (config_found_sm(parent, &uaa, usbd_print,
666 usbd_submatch))
667 found++;
668 }
669 if (found != 0)
670 return (USBD_NORMAL_COMPLETION);
671 }
672 /* No interfaces were attach in any of the configurations. */
673 if (d->bNumConfigurations > 0)
674 usbd_set_config_no(dev, 0);
675
676 DPRINTF(("usbd_new_device: no interface drivers found\n"));
677
678 /* Finally try the generic driver. */
679 uaa.iface = 0;
680 uaa.usegeneric = 1;
681 if (config_found_sm(parent, &uaa, usbd_print, usbd_submatch) != 0)
682 return (USBD_NORMAL_COMPLETION);
683
684 DPRINTF(("usbd_new_device: generic attach failed\n"));
685
686 return (USBD_NORMAL_COMPLETION);
687
688 bad:
689 usbd_kill_pipe(dev->default_pipe);
690 bad1:
691 up->device = 0;
692 bus->devices[addr] = 0;
693 free(dev, M_USB);
694 return (r);
695 }
696
697 int
698 usbd_print(aux, pnp)
699 void *aux;
700 const char *pnp;
701 {
702 struct usb_attach_arg *uaa = aux;
703 char devinfo[1024];
704
705 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
706 if (pnp) {
707 if (!uaa->usegeneric)
708 return (QUIET);
709 usbd_devinfo(uaa->device, 1, devinfo);
710 printf("%s at %s", devinfo, pnp);
711 }
712 if (uaa->port != 0)
713 printf(" port %d", uaa->port);
714 return (UNCONF);
715 }
716
717 int
718 usbd_submatch(parent, cf, aux)
719 struct device *parent;
720 struct cfdata *cf;
721 void *aux;
722 {
723 struct usb_attach_arg *uaa = aux;
724
725 if (uaa->port != 0 &&
726 cf->uhubcf_port != UHUB_UNK_PORT &&
727 cf->uhubcf_port != uaa->port)
728 return 0;
729 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
730 }
731