usb_subr.c revision 1.12 1 /* $NetBSD: usb_subr.c,v 1.12 1998/12/08 15:18:45 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 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 (augustss (at) carlstedt.se) 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/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/device.h>
45 #include <sys/proc.h>
46 #include <sys/select.h>
47
48 #include <dev/usb/usb.h>
49
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include <dev/usb/usbdivar.h>
53 #include <dev/usb/usbdevs.h>
54 #include <dev/usb/usb_quirks.h>
55
56 #include "opt_usbverbose.h"
57
58 #ifdef USB_DEBUG
59 #define DPRINTF(x) if (usbdebug) printf x
60 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x
61 extern int usbdebug;
62 #else
63 #define DPRINTF(x)
64 #define DPRINTFN(n,x)
65 #endif
66
67 static usbd_status usbd_set_config __P((usbd_device_handle, int));
68 char *usbd_get_string __P((usbd_device_handle, int, char *));
69 usbd_status usbd_get_desc __P((usbd_device_handle dev, int type,
70 int index, int len, void *desc));
71 int usbd_getnewaddr __P((usbd_bus_handle bus));
72 int usbd_print __P((void *aux, const char *pnp));
73 int usbd_submatch __P((struct device *, struct cfdata *cf, void *));
74 usb_interface_descriptor_t *usbd_find_idesc __P((usb_config_descriptor_t *cd,
75 int ifaceidx, int altidx));
76 usbd_status usbd_fill_iface_data __P((usbd_device_handle dev, int i, int a));
77 void usbd_free_iface_data __P((usbd_device_handle dev, int ifcno));
78 void usbd_kill_pipe __P((usbd_pipe_handle));
79
80 #ifdef USBVERBOSE
81 typedef u_int16_t usb_vendor_id_t;
82 typedef u_int16_t usb_product_id_t;
83
84 /*
85 * Descriptions of of known vendors and devices ("products").
86 */
87 struct usb_knowndev {
88 usb_vendor_id_t vendor;
89 usb_product_id_t product;
90 int flags;
91 char *vendorname, *productname;
92 };
93 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
94
95 #include <dev/usb/usbdevs_data.h>
96 #endif /* USBVERBOSE */
97
98 #ifdef USB_DEBUG
99 char *usbd_error_strs[] = {
100 "NORMAL_COMPLETION",
101 "IN_PROGRESS",
102 "PENDING_REQUESTS",
103 "NOT_STARTED",
104 "INVAL",
105 "IS_IDLE",
106 "NOMEM",
107 "CANCELLED",
108 "BAD_ADDRESS",
109 "IN_USE",
110 "INTERFACE_NOT_ACTIVE",
111 "NO_ADDR",
112 "SET_ADDR_FAILED",
113 "NO_POWER",
114 "TOO_DEEP",
115 "IOERROR",
116 "NOT_CONFIGURED",
117 "TIMEOUT",
118 "SHORT_XFER",
119 "STALLED",
120 "XXX",
121 };
122 #endif
123
124 char *
125 usbd_get_string(dev, si, buf)
126 usbd_device_handle dev;
127 int si;
128 char *buf;
129 {
130 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
131 usb_device_request_t req;
132 usb_string_descriptor_t us;
133 char *s;
134 int i, n;
135 u_int16_t c;
136 usbd_status r;
137
138 if (si == 0)
139 return (0);
140 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
141 return (0);
142 req.bmRequestType = UT_READ_DEVICE;
143 req.bRequest = UR_GET_DESCRIPTOR;
144 USETW2(req.wValue, UDESC_STRING, si);
145 USETW(req.wIndex, 0);
146 USETW(req.wLength, 1); /* only size byte first */
147 r = usbd_do_request(dev, &req, &us);
148 if (r != USBD_NORMAL_COMPLETION)
149 return 0;
150 USETW(req.wLength, us.bLength); /* the whole string */
151 r = usbd_do_request(dev, &req, &us);
152 if (r != USBD_NORMAL_COMPLETION)
153 return 0;
154 s = buf;
155 n = us.bLength / 2 - 1;
156 for (i = 0; i < n; i++) {
157 c = UGETW(us.bString[i]);
158 /* Convert from Unicode, handle buggy strings. */
159 if ((c & 0xff00) == 0)
160 *s++ = c;
161 else if ((c & 0x00ff) == 0 && swap)
162 *s++ = c >> 8;
163 else
164 *s++ = '?';
165 }
166 *s++ = 0;
167 return buf;
168 }
169
170 void
171 usbd_devinfo_vp(dev, v, p)
172 usbd_device_handle dev;
173 char *v, *p;
174 {
175 usb_device_descriptor_t *udd = &dev->ddesc;
176 char *vendor = 0, *product = 0;
177 #ifdef USBVERBOSE
178 struct usb_knowndev *kdp;
179 #endif
180
181 vendor = usbd_get_string(dev, udd->iManufacturer, v);
182 product = usbd_get_string(dev, udd->iProduct, p);
183 #ifdef USBVERBOSE
184 if (!vendor) {
185 for(kdp = usb_knowndevs;
186 kdp->vendorname != NULL;
187 kdp++) {
188 if (kdp->vendor == UGETW(udd->idVendor) &&
189 (kdp->product == UGETW(udd->idProduct) ||
190 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
191 break;
192 }
193 if (kdp->vendorname == NULL)
194 vendor = product = NULL;
195 else {
196 vendor = kdp->vendorname;
197 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
198 kdp->productname : NULL;
199 }
200 }
201 #endif
202 if (vendor)
203 strcpy(v, vendor);
204 else
205 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
206 if (product)
207 strcpy(p, product);
208 else
209 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
210 }
211
212 int
213 usbd_printBCD(cp, bcd)
214 char *cp;
215 int bcd;
216 {
217 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
218 }
219
220 void
221 usbd_devinfo(dev, showclass, cp)
222 usbd_device_handle dev;
223 int showclass;
224 char *cp;
225 {
226 usb_device_descriptor_t *udd = &dev->ddesc;
227 char vendor[USB_MAX_STRING_LEN];
228 char product[USB_MAX_STRING_LEN];
229 int bcdDevice, bcdUSB;
230
231 usbd_devinfo_vp(dev, vendor, product);
232 cp += sprintf(cp, "%s %s", vendor, product);
233 if (showclass)
234 cp += sprintf(cp, ", class %d/%d",
235 udd->bDeviceClass, udd->bDeviceSubClass);
236 bcdUSB = UGETW(udd->bcdUSB);
237 bcdDevice = UGETW(udd->bcdDevice);
238 cp += sprintf(cp, ", rev ");
239 cp += usbd_printBCD(cp, bcdUSB);
240 *cp++ = '/';
241 cp += usbd_printBCD(cp, bcdDevice);
242 cp += sprintf(cp, ", addr %d", dev->address);
243 *cp = 0;
244 }
245
246 /* Delay for a certain number of ms */
247 void
248 usbd_delay_ms(bus, ms)
249 usbd_bus_handle bus;
250 int ms;
251 {
252 /* Wait at least two clock ticks so we know the time has passed. */
253 if (bus->use_polling)
254 delay((ms+1) * 1000);
255 else
256 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
257 }
258
259 usbd_status
260 usbd_reset_port(dev, port, ps)
261 usbd_device_handle dev;
262 int port;
263 usb_port_status_t *ps;
264 {
265 usb_device_request_t req;
266 usbd_status r;
267 int n;
268
269 req.bmRequestType = UT_WRITE_CLASS_OTHER;
270 req.bRequest = UR_SET_FEATURE;
271 USETW(req.wValue, UHF_PORT_RESET);
272 USETW(req.wIndex, port);
273 USETW(req.wLength, 0);
274 r = usbd_do_request(dev, &req, 0);
275 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%d(%s)\n",
276 port, r, usbd_error_strs[r]));
277 if (r != USBD_NORMAL_COMPLETION)
278 return (r);
279 n = 10;
280 do {
281 /* Wait for device to recover from reset. */
282 usbd_delay_ms(dev->bus, USB_PORT_RESET_DELAY);
283 r = usbd_get_port_status(dev, port, ps);
284 if (r != USBD_NORMAL_COMPLETION) {
285 DPRINTF(("usbd_reset_port: get status failed %d\n",r));
286 return (r);
287 }
288 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
289 if (n == 0) {
290 printf("usbd_reset_port: timeout\n");
291 return (USBD_IOERROR);
292 }
293 r = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
294 #ifdef USB_DEBUG
295 if (r != USBD_NORMAL_COMPLETION)
296 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",r));
297 #endif
298 return (r);
299 }
300
301 #if 0
302 usb_interface_descriptor_t *
303 usbd_find_idesc(cd, ifaceidx, altidx)
304 usb_config_descriptor_t *cd;
305 int ifaceidx;
306 int altidx;
307 {
308 char *p = (char *)cd;
309 char *end = p + UGETW(cd->wTotalLength);
310 usb_interface_descriptor_t *d;
311 int curidx, lastno;
312
313 for (curidx = lastno = -1; p < end; p += d->bLength) {
314 d = (usb_interface_descriptor_t *)p;
315 if (p + d->bLength <= end &&
316 d->bDescriptorType == UDESC_INTERFACE) {
317 if (d->bInterfaceNumber != lastno) {
318 lastno = d->bInterfaceNumber;
319 curidx++;
320 }
321 if (ifaceidx == curidx && d->bAlternateSetting == ano)
322 return (d);
323 }
324 }
325 return (0);
326 }
327 #endif
328
329 usb_interface_descriptor_t *
330 usbd_find_idesc(cd, ifaceidx, altidx)
331 usb_config_descriptor_t *cd;
332 int ifaceidx;
333 int altidx;
334 {
335 char *p = (char *)cd;
336 char *end = p + UGETW(cd->wTotalLength);
337 usb_interface_descriptor_t *d;
338 int curidx, lastno, curaidx;
339
340 for (curidx = lastno = -1; p < end; ) {
341 d = (usb_interface_descriptor_t *)p;
342 if (d->bLength == 0) /* bad descriptor */
343 break;
344 p += d->bLength;
345 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
346 if (d->bInterfaceNumber != lastno) {
347 lastno = d->bInterfaceNumber;
348 curidx++;
349 curaidx = 0;
350 } else
351 curaidx++;
352 if (ifaceidx == curidx && altidx == curaidx)
353 return (d);
354 }
355 }
356 return (0);
357 }
358
359 usb_endpoint_descriptor_t *
360 usbd_find_edesc(cd, ifaceidx, altidx, endptidx)
361 usb_config_descriptor_t *cd;
362 int ifaceidx;
363 int altidx;
364 int endptidx;
365 {
366 char *p = (char *)cd;
367 char *end = p + UGETW(cd->wTotalLength);
368 usb_interface_descriptor_t *d;
369 usb_endpoint_descriptor_t *e;
370 int curidx;
371
372 d = usbd_find_idesc(cd, ifaceidx, altidx);
373 if (!d)
374 return (0);
375 if (endptidx >= d->bNumEndpoints) /* quick exit */
376 return (0);
377
378 curidx = -1;
379 for (p = (char *)d + d->bLength; p < end; ) {
380 e = (usb_endpoint_descriptor_t *)p;
381 if (e->bLength == 0) /* bad descriptor */
382 break;
383 p += e->bLength;
384 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
385 return (0);
386 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
387 curidx++;
388 if (curidx == endptidx)
389 return (e);
390 }
391 }
392 return (0);
393 }
394
395 usbd_status
396 usbd_fill_iface_data(dev, ifaceidx, ano)
397 usbd_device_handle dev;
398 int ifaceidx;
399 int ano;
400 {
401 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
402 usb_endpoint_descriptor_t *ed;
403 char *p, *end;
404 int endpt, nendpt;
405 usbd_status r;
406
407 DPRINTFN(5,("usbd_fill_iface_data: ifaceidx=%d ano=%d\n", ifaceidx, ano));
408 ifc->device = dev;
409 ifc->state = USBD_INTERFACE_ACTIVE;
410 ifc->idesc = usbd_find_idesc(dev->cdesc, ifaceidx, ano);
411 if (ifc->idesc == 0)
412 return (USBD_INVAL);
413 nendpt = ifc->idesc->bNumEndpoints;
414 DPRINTFN(10,("usbd_fill_iface_data: found idesc n=%d\n", nendpt));
415 if (nendpt != 0) {
416 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
417 M_USB, M_NOWAIT);
418 if (ifc->endpoints == 0)
419 return (USBD_NOMEM);
420 } else
421 ifc->endpoints = 0;
422 ifc->priv = 0;
423 p = (char *)ifc->idesc + ifc->idesc->bLength;
424 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
425 for (endpt = 0; endpt < nendpt; endpt++) {
426 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
427 for (; p < end; p += ed->bLength) {
428 ed = (usb_endpoint_descriptor_t *)p;
429 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p len=%d type=%d\n",
430 p, end, ed->bLength, ed->bDescriptorType));
431 if (p + ed->bLength <= end &&
432 ed->bDescriptorType == UDESC_ENDPOINT)
433 goto found;
434 if (ed->bDescriptorType == UDESC_INTERFACE)
435 break;
436 }
437 r = USBD_INVAL;
438 goto bad;
439 found:
440 ifc->endpoints[endpt].edesc = ed;
441 ifc->endpoints[endpt].state = USBD_ENDPOINT_ACTIVE;
442 ifc->endpoints[endpt].refcnt = 0;
443 ifc->endpoints[endpt].toggle = 0;
444 }
445 LIST_INIT(&ifc->pipes);
446 return (USBD_NORMAL_COMPLETION);
447 bad:
448 free(ifc->endpoints, M_USB);
449 return (r);
450 }
451
452 void
453 usbd_free_iface_data(dev, ifcno)
454 usbd_device_handle dev;
455 int ifcno;
456 {
457 usbd_interface_handle ifc = &dev->ifaces[ifcno];
458 if (ifc->endpoints)
459 free(ifc->endpoints, M_USB);
460 }
461
462 static usbd_status
463 usbd_set_config(dev, conf)
464 usbd_device_handle dev;
465 int conf;
466 {
467 usb_device_request_t req;
468
469 req.bmRequestType = UT_WRITE_DEVICE;
470 req.bRequest = UR_SET_CONFIG;
471 USETW(req.wValue, conf);
472 USETW(req.wIndex, 0);
473 USETW(req.wLength, 0);
474 return (usbd_do_request(dev, &req, 0));
475 }
476
477 usbd_status
478 usbd_set_config_no(dev, no, msg)
479 usbd_device_handle dev;
480 int no;
481 int msg;
482 {
483 int index;
484 usb_config_descriptor_t cd;
485 usbd_status r;
486
487 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
488 /* Figure out what config index to use. */
489 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
490 r = usbd_get_config_desc(dev, index, &cd);
491 if (r != USBD_NORMAL_COMPLETION)
492 return (r);
493 if (cd.bConfigurationValue == no)
494 return (usbd_set_config_index(dev, index, msg));
495 }
496 return (USBD_INVAL);
497 }
498
499 usbd_status
500 usbd_set_config_index(dev, index, msg)
501 usbd_device_handle dev;
502 int index;
503 int msg;
504 {
505 usb_status_t ds;
506 usb_hub_status_t hs;
507 usb_config_descriptor_t cd, *cdp;
508 usbd_status r;
509 int ifcidx, nifc, len, selfpowered, power;
510
511 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
512
513 /* XXX check that all interfaces are idle */
514 if (dev->config != 0) {
515 DPRINTF(("usbd_set_config_index: free old config\n"));
516 /* Free all configuration data structures. */
517 nifc = dev->cdesc->bNumInterface;
518 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
519 usbd_free_iface_data(dev, ifcidx);
520 free(dev->ifaces, M_USB);
521 free(dev->cdesc, M_USB);
522 dev->ifaces = 0;
523 dev->cdesc = 0;
524 dev->config = 0;
525 dev->state = USBD_DEVICE_ADDRESSED;
526 }
527
528 /* Figure out what config number to use. */
529 r = usbd_get_config_desc(dev, index, &cd);
530 if (r != USBD_NORMAL_COMPLETION)
531 return (r);
532 len = UGETW(cd.wTotalLength);
533 cdp = malloc(len, M_USB, M_NOWAIT);
534 if (cdp == 0)
535 return (USBD_NOMEM);
536 r = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
537 if (r != USBD_NORMAL_COMPLETION)
538 goto bad;
539 selfpowered = 0;
540 if (cdp->bmAttributes & UC_SELF_POWERED) {
541 /* May be self powered. */
542 if (cdp->bmAttributes & UC_BUS_POWERED) {
543 /* Must ask device. */
544 if (dev->quirks->uq_flags & UQ_HUB_POWER) {
545 /* Buggy hub, use hub descriptor. */
546 r = usbd_get_hub_status(dev, &hs);
547 if (r == USBD_NORMAL_COMPLETION &&
548 !(UGETW(hs.wHubStatus) & UHS_LOCAL_POWER))
549 selfpowered = 1;
550 } else {
551 r = usbd_get_device_status(dev, &ds);
552 if (r == USBD_NORMAL_COMPLETION &&
553 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
554 selfpowered = 1;
555 }
556 DPRINTF(("usbd_set_config_index: status=0x%04x, error=%d(%s)\n",
557 UGETW(ds.wStatus), r, usbd_error_strs[r]));
558 } else
559 selfpowered = 1;
560 }
561 DPRINTF(("usbd_set_config_index: (addr %d) attr=0x%02x, selfpowered=%d, power=%d, powerquirk=%x\n",
562 dev->address, cdp->bmAttributes,
563 selfpowered, cdp->bMaxPower * 2,
564 dev->quirks->uq_flags & UQ_HUB_POWER));
565 #ifdef USB_DEBUG
566 if (!dev->powersrc) {
567 printf("usbd_set_config_index: No power source?\n");
568 return (EIO);
569 }
570 #endif
571 power = cdp->bMaxPower * 2;
572 if (power > dev->powersrc->power) {
573 /* XXX print nicer message. */
574 if (msg)
575 printf("%s: device addr %d (config %d) exceeds power budget, %d mA > %d mA\n",
576 dev->bus->bdev.dv_xname, dev->address,
577 cdp->bConfigurationValue,
578 power, dev->powersrc->power);
579 r = USBD_NO_POWER;
580 goto bad;
581 }
582 dev->power = power;
583 dev->self_powered = selfpowered;
584
585 r = usbd_set_config(dev, cdp->bConfigurationValue);
586 if (r != USBD_NORMAL_COMPLETION) {
587 DPRINTF(("usbd_set_config_index: setting config=%d failed, error=%d(%s)\n",
588 cdp->bConfigurationValue, r, usbd_error_strs[r]));
589 goto bad;
590 }
591 DPRINTF(("usbd_set_config_index: setting new config %d\n",
592 cdp->bConfigurationValue));
593 nifc = cdp->bNumInterface;
594 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
595 M_USB, M_NOWAIT);
596 if (dev->ifaces == 0) {
597 r = USBD_NOMEM;
598 goto bad;
599 }
600 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
601 dev->cdesc = cdp;
602 dev->config = cdp->bConfigurationValue;
603 dev->state = USBD_DEVICE_CONFIGURED;
604 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
605 r = usbd_fill_iface_data(dev, ifcidx, 0);
606 if (r != USBD_NORMAL_COMPLETION) {
607 while (--ifcidx >= 0)
608 usbd_free_iface_data(dev, ifcidx);
609 goto bad;
610 }
611 }
612
613 return (USBD_NORMAL_COMPLETION);
614
615 bad:
616 free(cdp, M_USB);
617 return (r);
618 }
619
620 /* XXX add function for alternate settings */
621
622 usbd_status
623 usbd_setup_pipe(dev, iface, ep, pipe)
624 usbd_device_handle dev;
625 usbd_interface_handle iface;
626 struct usbd_endpoint *ep;
627 usbd_pipe_handle *pipe;
628 {
629 usbd_pipe_handle p;
630 usbd_status r;
631
632 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
633 dev, iface, ep, pipe));
634 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
635 if (p == 0)
636 return (USBD_NOMEM);
637 p->device = dev;
638 p->iface = iface;
639 p->state = USBD_PIPE_ACTIVE;
640 p->endpoint = ep;
641 ep->refcnt++;
642 p->refcnt = 1;
643 p->intrreqh = 0;
644 p->running = 0;
645 SIMPLEQ_INIT(&p->queue);
646 r = dev->bus->open_pipe(p);
647 if (r != USBD_NORMAL_COMPLETION) {
648 DPRINTF(("usbd_setup_pipe: endpoint=%d failed, error=%d(%s)\n",
649 ep->edesc->bEndpointAddress, r, usbd_error_strs[r]));
650 free(p, M_USB);
651 return (r);
652 }
653 *pipe = p;
654 return (USBD_NORMAL_COMPLETION);
655 }
656
657 /* Abort the device control pipe. */
658 void
659 usbd_kill_pipe(pipe)
660 usbd_pipe_handle pipe;
661 {
662 pipe->methods->close(pipe);
663 pipe->endpoint->refcnt--;
664 free(pipe, M_USB);
665 }
666
667 int
668 usbd_getnewaddr(bus)
669 usbd_bus_handle bus;
670 {
671 int i;
672
673 for (i = 1; i < USB_MAX_DEVICES; i++)
674 if (bus->devices[i] == 0)
675 return (i);
676 return (-1);
677 }
678
679 /*
680 * Called when a new device has been put in the powered state,
681 * but not yet in the addressed state.
682 * Get initial descriptor, set the address, get full descriptor,
683 * and attach a driver.
684 */
685 usbd_status
686 usbd_new_device(parent, bus, depth, lowspeed, port, up)
687 struct device *parent;
688 usbd_bus_handle bus;
689 int depth;
690 int lowspeed;
691 int port;
692 struct usbd_port *up;
693 {
694 usbd_device_handle dev;
695 usb_device_descriptor_t *d;
696 usbd_status r;
697 struct usb_attach_arg uaa;
698 int addr;
699 int found, i, confi;
700
701 DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
702 bus, depth, lowspeed));
703 addr = usbd_getnewaddr(bus);
704 if (addr < 0) {
705 printf("%s: No free USB addresses, new device ignored.\n",
706 bus->bdev.dv_xname);
707 return (USBD_NO_ADDR);
708 }
709
710 dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
711 if (dev == 0)
712 return (USBD_NOMEM);
713 memset(dev, 0, sizeof(*dev));
714
715 dev->bus = bus;
716
717 /* Set up default endpoint handle. */
718 dev->def_ep.edesc = &dev->def_ep_desc;
719 dev->def_ep.state = USBD_ENDPOINT_ACTIVE;
720 dev->def_ep.refcnt = 0;
721 dev->def_ep.toggle = 0; /* XXX */
722
723 /* Set up default endpoint descriptor. */
724 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
725 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
726 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
727 dev->def_ep_desc.bmAttributes = UE_CONTROL;
728 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
729 dev->def_ep_desc.bInterval = 0;
730
731 dev->state = USBD_DEVICE_DEFAULT;
732 dev->quirks = &usbd_no_quirk;
733 dev->address = USB_START_ADDR;
734 dev->ddesc.bMaxPacketSize = 0;
735 dev->lowspeed = lowspeed != 0;
736 dev->depth = depth;
737 dev->powersrc = up;
738
739 /* Establish the the default pipe. */
740 r = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
741 if (r != USBD_NORMAL_COMPLETION)
742 goto bad1;
743
744 up->device = dev;
745 d = &dev->ddesc;
746 /* Try a few times in case the device is slow (i.e. outside specs.) */
747 for (i = 0; i < 5; i++) {
748 /* Get the first 8 bytes of the device descriptor. */
749 r = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, d);
750 if (r == USBD_NORMAL_COMPLETION)
751 break;
752 usbd_delay_ms(dev->bus, 200);
753 }
754 if (r != USBD_NORMAL_COMPLETION) {
755 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc failed\n",
756 addr));
757 goto bad;
758 }
759
760 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, subclass=%d, protocol=%d, maxpacket=%d, ls=%d\n",
761 addr, UGETW(d->bcdUSB), d->bDeviceClass, d->bDeviceSubClass,
762 d->bDeviceProtocol, d->bMaxPacketSize, dev->lowspeed));
763
764 USETW(dev->def_ep_desc.wMaxPacketSize, d->bMaxPacketSize);
765
766 /* Get the full device descriptor. */
767 r = usbd_get_device_desc(dev, d);
768 if (r != USBD_NORMAL_COMPLETION) {
769 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc failed\n", addr));
770 goto bad;
771 }
772
773 /* Figure out what's wrong with this device. */
774 dev->quirks = usbd_find_quirk(d);
775
776 /* Set the address */
777 r = usbd_set_address(dev, addr);
778 if (r != USBD_NORMAL_COMPLETION) {
779 DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
780 r = USBD_SET_ADDR_FAILED;
781 goto bad;
782 }
783 dev->address = addr; /* New device address now */
784 dev->state = USBD_DEVICE_ADDRESSED;
785 bus->devices[addr] = dev;
786
787 /* Assume 100mA bus powered for now. Changed when configured. */
788 dev->power = USB_MIN_POWER;
789 dev->self_powered = 0;
790
791 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
792 addr, dev, parent));
793
794 uaa.device = dev;
795 uaa.iface = 0;
796 uaa.usegeneric = 0;
797 uaa.port = port;
798 uaa.configno = UHUB_UNK_CONFIGURATION;
799 uaa.ifaceno = UHUB_UNK_INTERFACE;
800 /* First try with device specific drivers. */
801 if (config_found_sm(parent, &uaa, usbd_print, usbd_submatch) != 0)
802 return (USBD_NORMAL_COMPLETION);
803
804 DPRINTF(("usbd_new_device: no device driver found\n"));
805
806 /* Next try with interface drivers. */
807 for (confi = 0; confi < d->bNumConfigurations; confi++) {
808 DPRINTFN(1,("usbd_new_device: trying config idx=%d\n", confi));
809 r = usbd_set_config_index(dev, confi, 1);
810 if (r != USBD_NORMAL_COMPLETION) {
811 printf("%s: set config at addr %d failed, error=%d(%s)\n",
812 parent->dv_xname, addr, r, usbd_error_strs[r]);
813 goto bad;
814 }
815 uaa.configno = confi;
816 for (found = i = 0; i < dev->cdesc->bNumInterface; i++) {
817 uaa.iface = &dev->ifaces[i];
818 uaa.ifaceno = dev->ifaces[i].idesc->bInterfaceNumber;
819 if (config_found_sm(parent, &uaa, usbd_print,
820 usbd_submatch))
821 found++;
822 }
823 if (found != 0)
824 return (USBD_NORMAL_COMPLETION);
825 }
826 /* No interfaces were attached in any of the configurations. */
827 if (d->bNumConfigurations > 0)
828 usbd_set_config_index(dev, 0, 0);
829
830 DPRINTF(("usbd_new_device: no interface drivers found\n"));
831
832 /* Finally try the generic driver. */
833 uaa.iface = 0;
834 uaa.usegeneric = 1;
835 uaa.configno = UHUB_UNK_CONFIGURATION;
836 uaa.ifaceno = UHUB_UNK_INTERFACE;
837 if (config_found_sm(parent, &uaa, usbd_print, usbd_submatch) != 0)
838 return (USBD_NORMAL_COMPLETION);
839
840 DPRINTF(("usbd_new_device: generic attach failed\n"));
841
842 return (USBD_NORMAL_COMPLETION);
843
844 bad:
845 usbd_kill_pipe(dev->default_pipe);
846 bad1:
847 up->device = 0;
848 bus->devices[addr] = 0;
849 free(dev, M_USB);
850 return (r);
851 }
852
853 int
854 usbd_print(aux, pnp)
855 void *aux;
856 const char *pnp;
857 {
858 struct usb_attach_arg *uaa = aux;
859 char devinfo[1024];
860
861 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
862 if (pnp) {
863 if (!uaa->usegeneric)
864 return (QUIET);
865 usbd_devinfo(uaa->device, 1, devinfo);
866 printf("%s, %s", devinfo, pnp);
867 }
868 if (uaa->port != 0)
869 printf(" port %d", uaa->port);
870 if (uaa->configno != UHUB_UNK_CONFIGURATION)
871 printf(" configuration %d", uaa->configno);
872 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
873 printf(" interface %d", uaa->ifaceno);
874 return (UNCONF);
875 }
876
877 int
878 usbd_submatch(parent, cf, aux)
879 struct device *parent;
880 struct cfdata *cf;
881 void *aux;
882 {
883 struct usb_attach_arg *uaa = aux;
884
885 if ((uaa->port != 0 &&
886 cf->uhubcf_port != UHUB_UNK_PORT &&
887 cf->uhubcf_port != uaa->port) ||
888 (uaa->configno != UHUB_UNK_CONFIGURATION &&
889 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
890 cf->uhubcf_configuration != uaa->configno) ||
891 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
892 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
893 cf->uhubcf_interface != uaa->ifaceno))
894 return 0;
895 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
896 }
897