usb_subr.c revision 1.42 1 /* $NetBSD: usb_subr.c,v 1.42 1999/08/29 19:41:27 thorpej 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 #if defined(__NetBSD__) || defined(__OpenBSD__)
45 #include <sys/device.h>
46 #elif defined(__FreeBSD__)
47 #include <sys/module.h>
48 #include <sys/bus.h>
49 #endif
50 #include <sys/proc.h>
51 #include <sys/select.h>
52
53 #include <dev/usb/usb.h>
54
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbdivar.h>
58 #include <dev/usb/usbdevs.h>
59 #include <dev/usb/usb_quirks.h>
60
61 #if defined(__FreeBSD__)
62 #include <machine/clock.h>
63 #define delay(d) DELAY(d)
64 #endif
65
66 #ifdef USB_DEBUG
67 #define DPRINTF(x) if (usbdebug) logprintf x
68 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
69 extern int usbdebug;
70 #else
71 #define DPRINTF(x)
72 #define DPRINTFN(n,x)
73 #endif
74
75 static usbd_status usbd_set_config __P((usbd_device_handle, int));
76 char *usbd_get_string __P((usbd_device_handle, int, char *));
77 int usbd_getnewaddr __P((usbd_bus_handle bus));
78 int usbd_print __P((void *aux, const char *pnp));
79 #if defined(__NetBSD__)
80 int usbd_submatch __P((bdevice *, struct cfdata *cf, void *));
81 #elif defined(__OpenBSD__)
82 int usbd_submatch __P((bdevice *, void *, void *));
83 #endif
84 void usbd_free_iface_data __P((usbd_device_handle dev, int ifcno));
85 void usbd_kill_pipe __P((usbd_pipe_handle));
86 usbd_status usbd_probe_and_attach
87 __P((bdevice *parent, usbd_device_handle dev, int port, int addr));
88
89
90 #ifdef USBVERBOSE
91 typedef u_int16_t usb_vendor_id_t;
92 typedef u_int16_t usb_product_id_t;
93
94 /*
95 * Descriptions of of known vendors and devices ("products").
96 */
97 struct usb_knowndev {
98 usb_vendor_id_t vendor;
99 usb_product_id_t product;
100 int flags;
101 char *vendorname, *productname;
102 };
103 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
104
105 #include <dev/usb/usbdevs_data.h>
106 #endif /* USBVERBOSE */
107
108 const char *usbd_error_strs[] = {
109 "NORMAL_COMPLETION",
110 "IN_PROGRESS",
111 "PENDING_REQUESTS",
112 "NOT_STARTED",
113 "INVAL",
114 "NOMEM",
115 "CANCELLED",
116 "BAD_ADDRESS",
117 "IN_USE",
118 "NO_ADDR",
119 "SET_ADDR_FAILED",
120 "NO_POWER",
121 "TOO_DEEP",
122 "IOERROR",
123 "NOT_CONFIGURED",
124 "TIMEOUT",
125 "SHORT_XFER",
126 "STALLED",
127 "INTERRUPTED",
128 "XXX",
129 };
130
131 const char *
132 usbd_errstr(err)
133 usbd_status err;
134 {
135 static char buffer[5];
136
137 if (err < USBD_ERROR_MAX) {
138 return usbd_error_strs[err];
139 } else {
140 snprintf(buffer, sizeof buffer, "%d", err);
141 return buffer;
142 }
143 }
144
145 usbd_status
146 usbd_get_string_desc(dev, sindex, langid, sdesc)
147 usbd_device_handle dev;
148 int sindex;
149 int langid;
150 usb_string_descriptor_t *sdesc;
151 {
152 usb_device_request_t req;
153 usbd_status r;
154
155 req.bmRequestType = UT_READ_DEVICE;
156 req.bRequest = UR_GET_DESCRIPTOR;
157 USETW2(req.wValue, UDESC_STRING, sindex);
158 USETW(req.wIndex, langid);
159 USETW(req.wLength, 1); /* only size byte first */
160 r = usbd_do_request(dev, &req, sdesc);
161 if (r != USBD_NORMAL_COMPLETION)
162 return (r);
163 USETW(req.wLength, sdesc->bLength); /* the whole string */
164 return (usbd_do_request(dev, &req, sdesc));
165 }
166
167 char *
168 usbd_get_string(dev, si, buf)
169 usbd_device_handle dev;
170 int si;
171 char *buf;
172 {
173 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
174 usb_string_descriptor_t us;
175 char *s;
176 int i, n;
177 u_int16_t c;
178 usbd_status r;
179
180 if (si == 0)
181 return (0);
182 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
183 return (0);
184 if (dev->langid == USBD_NOLANG) {
185 /* Set up default language */
186 r = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us);
187 if (r != USBD_NORMAL_COMPLETION || us.bLength < 4) {
188 dev->langid = 0; /* Well, just pick English then */
189 } else {
190 /* Pick the first language as the default. */
191 dev->langid = UGETW(us.bString[0]);
192 }
193 }
194 r = usbd_get_string_desc(dev, si, dev->langid, &us);
195 if (r != USBD_NORMAL_COMPLETION)
196 return (0);
197 s = buf;
198 n = us.bLength / 2 - 1;
199 for (i = 0; i < n; i++) {
200 c = UGETW(us.bString[i]);
201 /* Convert from Unicode, handle buggy strings. */
202 if ((c & 0xff00) == 0)
203 *s++ = c;
204 else if ((c & 0x00ff) == 0 && swap)
205 *s++ = c >> 8;
206 else
207 *s++ = '?';
208 }
209 *s++ = 0;
210 return buf;
211 }
212
213 void
214 usbd_devinfo_vp(dev, v, p)
215 usbd_device_handle dev;
216 char *v, *p;
217 {
218 usb_device_descriptor_t *udd = &dev->ddesc;
219 char *vendor = 0, *product = 0;
220 #ifdef USBVERBOSE
221 struct usb_knowndev *kdp;
222 #endif
223
224 vendor = usbd_get_string(dev, udd->iManufacturer, v);
225 product = usbd_get_string(dev, udd->iProduct, p);
226 #ifdef USBVERBOSE
227 if (!vendor) {
228 for(kdp = usb_knowndevs;
229 kdp->vendorname != NULL;
230 kdp++) {
231 if (kdp->vendor == UGETW(udd->idVendor) &&
232 (kdp->product == UGETW(udd->idProduct) ||
233 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
234 break;
235 }
236 if (kdp->vendorname == NULL)
237 vendor = product = NULL;
238 else {
239 vendor = kdp->vendorname;
240 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
241 kdp->productname : NULL;
242 }
243 }
244 #endif
245 if (vendor)
246 strcpy(v, vendor);
247 else
248 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
249 if (product)
250 strcpy(p, product);
251 else
252 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
253 }
254
255 int
256 usbd_printBCD(cp, bcd)
257 char *cp;
258 int bcd;
259 {
260 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
261 }
262
263 void
264 usbd_devinfo(dev, showclass, cp)
265 usbd_device_handle dev;
266 int showclass;
267 char *cp;
268 {
269 usb_device_descriptor_t *udd = &dev->ddesc;
270 char vendor[USB_MAX_STRING_LEN];
271 char product[USB_MAX_STRING_LEN];
272 int bcdDevice, bcdUSB;
273
274 usbd_devinfo_vp(dev, vendor, product);
275 cp += sprintf(cp, "%s %s", vendor, product);
276 if (showclass)
277 cp += sprintf(cp, ", class %d/%d",
278 udd->bDeviceClass, udd->bDeviceSubClass);
279 bcdUSB = UGETW(udd->bcdUSB);
280 bcdDevice = UGETW(udd->bcdDevice);
281 cp += sprintf(cp, ", rev ");
282 cp += usbd_printBCD(cp, bcdUSB);
283 *cp++ = '/';
284 cp += usbd_printBCD(cp, bcdDevice);
285 cp += sprintf(cp, ", addr %d", dev->address);
286 *cp = 0;
287 }
288
289 /* Delay for a certain number of ms */
290 void
291 usb_delay_ms(bus, ms)
292 usbd_bus_handle bus;
293 u_int ms;
294 {
295 /* Wait at least two clock ticks so we know the time has passed. */
296 if (bus->use_polling)
297 delay((ms+1) * 1000);
298 else
299 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
300 }
301
302 /* Delay given a device handle. */
303 void
304 usbd_delay_ms(dev, ms)
305 usbd_device_handle dev;
306 u_int ms;
307 {
308 usb_delay_ms(dev->bus, ms);
309 }
310
311 usbd_status
312 usbd_reset_port(dev, port, ps)
313 usbd_device_handle dev;
314 int port;
315 usb_port_status_t *ps;
316 {
317 usb_device_request_t req;
318 usbd_status r;
319 int n;
320
321 req.bmRequestType = UT_WRITE_CLASS_OTHER;
322 req.bRequest = UR_SET_FEATURE;
323 USETW(req.wValue, UHF_PORT_RESET);
324 USETW(req.wIndex, port);
325 USETW(req.wLength, 0);
326 r = usbd_do_request(dev, &req, 0);
327 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
328 port, usbd_errstr(r)));
329 if (r != USBD_NORMAL_COMPLETION)
330 return (r);
331 n = 10;
332 do {
333 /* Wait for device to recover from reset. */
334 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
335 r = usbd_get_port_status(dev, port, ps);
336 if (r != USBD_NORMAL_COMPLETION) {
337 DPRINTF(("usbd_reset_port: get status failed %d\n",r));
338 return (r);
339 }
340 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
341 if (n == 0) {
342 printf("usbd_reset_port: timeout\n");
343 return (USBD_IOERROR);
344 }
345 r = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
346 #ifdef USB_DEBUG
347 if (r != USBD_NORMAL_COMPLETION)
348 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",r));
349 #endif
350
351 /* Wait for the device to recover from reset. */
352 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
353 return (r);
354 }
355
356 usb_interface_descriptor_t *
357 usbd_find_idesc(cd, ifaceidx, altidx)
358 usb_config_descriptor_t *cd;
359 int ifaceidx;
360 int altidx;
361 {
362 char *p = (char *)cd;
363 char *end = p + UGETW(cd->wTotalLength);
364 usb_interface_descriptor_t *d;
365 int curidx, lastidx, curaidx = 0;
366
367 for (curidx = lastidx = -1; p < end; ) {
368 d = (usb_interface_descriptor_t *)p;
369 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
370 "type=%d\n",
371 ifaceidx, curidx, altidx, curaidx,
372 d->bLength, d->bDescriptorType));
373 if (d->bLength == 0) /* bad descriptor */
374 break;
375 p += d->bLength;
376 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
377 if (d->bInterfaceNumber != lastidx) {
378 lastidx = d->bInterfaceNumber;
379 curidx++;
380 curaidx = 0;
381 } else
382 curaidx++;
383 if (ifaceidx == curidx && altidx == curaidx)
384 return (d);
385 }
386 }
387 return (0);
388 }
389
390 usb_endpoint_descriptor_t *
391 usbd_find_edesc(cd, ifaceidx, altidx, endptidx)
392 usb_config_descriptor_t *cd;
393 int ifaceidx;
394 int altidx;
395 int endptidx;
396 {
397 char *p = (char *)cd;
398 char *end = p + UGETW(cd->wTotalLength);
399 usb_interface_descriptor_t *d;
400 usb_endpoint_descriptor_t *e;
401 int curidx;
402
403 d = usbd_find_idesc(cd, ifaceidx, altidx);
404 if (!d)
405 return (0);
406 if (endptidx >= d->bNumEndpoints) /* quick exit */
407 return (0);
408
409 curidx = -1;
410 for (p = (char *)d + d->bLength; p < end; ) {
411 e = (usb_endpoint_descriptor_t *)p;
412 if (e->bLength == 0) /* bad descriptor */
413 break;
414 p += e->bLength;
415 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
416 return (0);
417 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
418 curidx++;
419 if (curidx == endptidx)
420 return (e);
421 }
422 }
423 return (0);
424 }
425
426 usbd_status
427 usbd_fill_iface_data(dev, ifaceidx, altidx)
428 usbd_device_handle dev;
429 int ifaceidx;
430 int altidx;
431 {
432 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
433 char *p, *end;
434 int endpt, nendpt;
435
436 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
437 ifaceidx, altidx));
438 ifc->device = dev;
439 ifc->idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
440 if (ifc->idesc == 0)
441 return (USBD_INVAL);
442 ifc->index = ifaceidx;
443 ifc->altindex = altidx;
444 nendpt = ifc->idesc->bNumEndpoints;
445 DPRINTFN(10,("usbd_fill_iface_data: found idesc n=%d\n", nendpt));
446 if (nendpt != 0) {
447 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
448 M_USB, M_NOWAIT);
449 if (ifc->endpoints == 0)
450 return (USBD_NOMEM);
451 } else
452 ifc->endpoints = 0;
453 ifc->priv = 0;
454 p = (char *)ifc->idesc + ifc->idesc->bLength;
455 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
456 #define ed ((usb_endpoint_descriptor_t *)p)
457 for (endpt = 0; endpt < nendpt; endpt++) {
458 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
459 for (; p < end; p += ed->bLength) {
460 ed = (usb_endpoint_descriptor_t *)p;
461 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
462 "len=%d type=%d\n",
463 p, end, ed->bLength, ed->bDescriptorType));
464 if (p + ed->bLength <= end && ed->bLength != 0 &&
465 ed->bDescriptorType == UDESC_ENDPOINT)
466 goto found;
467 if (ed->bDescriptorType == UDESC_INTERFACE ||
468 ed->bLength == 0)
469 break;
470 }
471 /* passed end, or bad desc */
472 goto bad;
473 found:
474 ifc->endpoints[endpt].edesc = ed;
475 ifc->endpoints[endpt].refcnt = 0;
476 p += ed->bLength;
477 }
478 #undef ed
479 LIST_INIT(&ifc->pipes);
480 return (USBD_NORMAL_COMPLETION);
481
482 bad:
483 free(ifc->endpoints, M_USB);
484 return (USBD_INVAL);
485 }
486
487 void
488 usbd_free_iface_data(dev, ifcno)
489 usbd_device_handle dev;
490 int ifcno;
491 {
492 usbd_interface_handle ifc = &dev->ifaces[ifcno];
493 if (ifc->endpoints)
494 free(ifc->endpoints, M_USB);
495 }
496
497 static usbd_status
498 usbd_set_config(dev, conf)
499 usbd_device_handle dev;
500 int conf;
501 {
502 usb_device_request_t req;
503
504 req.bmRequestType = UT_WRITE_DEVICE;
505 req.bRequest = UR_SET_CONFIG;
506 USETW(req.wValue, conf);
507 USETW(req.wIndex, 0);
508 USETW(req.wLength, 0);
509 return (usbd_do_request(dev, &req, 0));
510 }
511
512 usbd_status
513 usbd_set_config_no(dev, no, msg)
514 usbd_device_handle dev;
515 int no;
516 int msg;
517 {
518 int index;
519 usb_config_descriptor_t cd;
520 usbd_status r;
521
522 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
523 /* Figure out what config index to use. */
524 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
525 r = usbd_get_config_desc(dev, index, &cd);
526 if (r != USBD_NORMAL_COMPLETION)
527 return (r);
528 if (cd.bConfigurationValue == no)
529 return (usbd_set_config_index(dev, index, msg));
530 }
531 return (USBD_INVAL);
532 }
533
534 usbd_status
535 usbd_set_config_index(dev, index, msg)
536 usbd_device_handle dev;
537 int index;
538 int msg;
539 {
540 usb_status_t ds;
541 usb_config_descriptor_t cd, *cdp;
542 usbd_status r;
543 int ifcidx, nifc, len, selfpowered, power;
544
545 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
546
547 /* XXX check that all interfaces are idle */
548 if (dev->config != 0) {
549 DPRINTF(("usbd_set_config_index: free old config\n"));
550 /* Free all configuration data structures. */
551 nifc = dev->cdesc->bNumInterface;
552 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
553 usbd_free_iface_data(dev, ifcidx);
554 free(dev->ifaces, M_USB);
555 free(dev->cdesc, M_USB);
556 dev->ifaces = 0;
557 dev->cdesc = 0;
558 dev->config = 0;
559 }
560
561 /* Figure out what config number to use. */
562 r = usbd_get_config_desc(dev, index, &cd);
563 if (r != USBD_NORMAL_COMPLETION)
564 return (r);
565 len = UGETW(cd.wTotalLength);
566 cdp = malloc(len, M_USB, M_NOWAIT);
567 if (cdp == 0)
568 return (USBD_NOMEM);
569 r = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
570 if (r != USBD_NORMAL_COMPLETION)
571 goto bad;
572 if (cdp->bDescriptorType != UDESC_CONFIG) {
573 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
574 cdp->bDescriptorType));
575 r = USBD_INVAL;
576 goto bad;
577 }
578 selfpowered = 0;
579 if (cdp->bmAttributes & UC_SELF_POWERED) {
580 /* May be self powered. */
581 if (cdp->bmAttributes & UC_BUS_POWERED) {
582 /* Must ask device. */
583 r = usbd_get_device_status(dev, &ds);
584 if (r == USBD_NORMAL_COMPLETION &&
585 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
586 selfpowered = 1;
587 DPRINTF(("usbd_set_config_index: status=0x%04x, "
588 "error=%s\n",
589 UGETW(ds.wStatus), usbd_errstr(r)));
590 } else
591 selfpowered = 1;
592 }
593 DPRINTF(("usbd_set_config_index: (addr %d) attr=0x%02x, "
594 "selfpowered=%d, power=%d\n",
595 dev->address, cdp->bmAttributes,
596 selfpowered, cdp->bMaxPower * 2));
597 #ifdef USB_DEBUG
598 if (!dev->powersrc) {
599 printf("usbd_set_config_index: No power source?\n");
600 return (USBD_IOERROR);
601 }
602 #endif
603 power = cdp->bMaxPower * 2;
604 if (power > dev->powersrc->power) {
605 /* XXX print nicer message. */
606 if (msg)
607 printf("%s: device addr %d (config %d) exceeds power "
608 "budget, %d mA > %d mA\n",
609 USBDEVNAME(dev->bus->bdev), dev->address,
610 cdp->bConfigurationValue,
611 power, dev->powersrc->power);
612 r = USBD_NO_POWER;
613 goto bad;
614 }
615 dev->power = power;
616 dev->self_powered = selfpowered;
617
618 DPRINTF(("usbd_set_config_index: set config %d\n",
619 cdp->bConfigurationValue));
620 r = usbd_set_config(dev, cdp->bConfigurationValue);
621 if (r != USBD_NORMAL_COMPLETION) {
622 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
623 "error=%s\n",
624 cdp->bConfigurationValue, usbd_errstr(r)));
625 goto bad;
626 }
627 DPRINTF(("usbd_set_config_index: setting new config %d\n",
628 cdp->bConfigurationValue));
629 nifc = cdp->bNumInterface;
630 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
631 M_USB, M_NOWAIT);
632 if (dev->ifaces == 0) {
633 r = USBD_NOMEM;
634 goto bad;
635 }
636 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
637 dev->cdesc = cdp;
638 dev->config = cdp->bConfigurationValue;
639 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
640 r = usbd_fill_iface_data(dev, ifcidx, 0);
641 if (r != USBD_NORMAL_COMPLETION) {
642 while (--ifcidx >= 0)
643 usbd_free_iface_data(dev, ifcidx);
644 goto bad;
645 }
646 }
647
648 return (USBD_NORMAL_COMPLETION);
649
650 bad:
651 free(cdp, M_USB);
652 return (r);
653 }
654
655 /* XXX add function for alternate settings */
656
657 usbd_status
658 usbd_setup_pipe(dev, iface, ep, pipe)
659 usbd_device_handle dev;
660 usbd_interface_handle iface;
661 struct usbd_endpoint *ep;
662 usbd_pipe_handle *pipe;
663 {
664 usbd_pipe_handle p;
665 usbd_status r;
666
667 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
668 dev, iface, ep, pipe));
669 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
670 if (p == 0)
671 return (USBD_NOMEM);
672 p->device = dev;
673 p->iface = iface;
674 p->endpoint = ep;
675 ep->refcnt++;
676 p->refcnt = 1;
677 p->intrreqh = 0;
678 p->running = 0;
679 p->repeat = 0;
680 SIMPLEQ_INIT(&p->queue);
681 r = dev->bus->open_pipe(p);
682 if (r != USBD_NORMAL_COMPLETION) {
683 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
684 "%s\n",
685 ep->edesc->bEndpointAddress, usbd_errstr(r)));
686 free(p, M_USB);
687 return (r);
688 }
689 /* Clear any stall and make sure DATA0 toggle will be used next. */
690 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT)
691 usbd_clear_endpoint_stall(p);
692 *pipe = p;
693 return (USBD_NORMAL_COMPLETION);
694 }
695
696 /* Abort the device control pipe. */
697 void
698 usbd_kill_pipe(pipe)
699 usbd_pipe_handle pipe;
700 {
701 pipe->methods->close(pipe);
702 pipe->endpoint->refcnt--;
703 free(pipe, M_USB);
704 }
705
706 int
707 usbd_getnewaddr(bus)
708 usbd_bus_handle bus;
709 {
710 int addr;
711
712 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
713 if (bus->devices[addr] == 0)
714 return (addr);
715 return (-1);
716 }
717
718
719 usbd_status
720 usbd_probe_and_attach(parent, dev, port, addr)
721 bdevice *parent;
722 usbd_device_handle dev;
723 int port;
724 int addr;
725 {
726 struct usb_attach_arg uaa;
727 usb_device_descriptor_t *dd = &dev->ddesc;
728 int r, found, i, confi, nifaces;
729 bdevice *dv;
730 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
731
732 #if defined(__FreeBSD__)
733 /*
734 * XXX uaa is a static var. Not a problem as it _should_ be used only
735 * during probe and attach. Should be changed however.
736 */
737 bdevice bdev;
738 bdev = device_add_child(*parent, NULL, -1, &uaa);
739 if (!bdev) {
740 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
741 return (USBD_INVAL);
742 }
743 #endif
744
745 uaa.device = dev;
746 uaa.iface = 0;
747 uaa.ifaces = 0;
748 uaa.nifaces = 0;
749 uaa.usegeneric = 0;
750 uaa.port = port;
751 uaa.configno = UHUB_UNK_CONFIGURATION;
752 uaa.ifaceno = UHUB_UNK_INTERFACE;
753 uaa.vendor = UGETW(dd->idVendor);
754 uaa.product = UGETW(dd->idProduct);
755 uaa.release = UGETW(dd->bcdDevice);
756
757 /* First try with device specific drivers. */
758 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
759 if (dv) {
760 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
761 if (dev->subdevs == 0)
762 return (USBD_NOMEM);
763 dev->subdevs[0] = dv;
764 dev->subdevs[1] = 0;
765 return (USBD_NORMAL_COMPLETION);
766 }
767
768 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
769
770 /* Next try with interface drivers. */
771 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
772 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
773 confi));
774 r = usbd_set_config_index(dev, confi, 1);
775 if (r != USBD_NORMAL_COMPLETION) {
776 #ifdef USB_DEBUG
777 DPRINTF(("%s: port %d, set config at addr %d failed, "
778 "error=%s\n", USBDEVNAME(*parent), port,
779 addr, usbd_errstr(r)));
780 #else
781 printf("%s: port %d, set config at addr %d failed\n",
782 USBDEVNAME(*parent), port, addr);
783 #endif
784 #if defined(__FreeBSD__)
785 device_delete_child(*parent, bdev);
786 #endif
787 return (r);
788 }
789 nifaces = dev->cdesc->bNumInterface;
790 uaa.configno = dev->cdesc->bConfigurationValue;
791 for (i = 0; i < nifaces; i++)
792 ifaces[i] = &dev->ifaces[i];
793 uaa.ifaces = ifaces;
794 uaa.nifaces = nifaces;
795 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
796 if (dev->subdevs == 0)
797 return (USBD_NOMEM);
798 for (found = i = 0; i < nifaces; i++) {
799 if (!ifaces[i])
800 continue; /* interface already claimed */
801 uaa.iface = ifaces[i];
802 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
803 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
804 usbd_submatch);
805 if (dv) {
806 dev->subdevs[found++] = dv;
807 dev->subdevs[found] = 0;
808 ifaces[i] = 0; /* consumed */
809 }
810 }
811 if (found != 0)
812 return (USBD_NORMAL_COMPLETION);
813 free(dev->subdevs, M_USB);
814 dev->subdevs = 0;
815 }
816 /* No interfaces were attached in any of the configurations. */
817
818 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
819 usbd_set_config_index(dev, 0, 0);
820
821 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
822
823 /* Finally try the generic driver. */
824 uaa.iface = 0;
825 uaa.usegeneric = 1;
826 uaa.configno = UHUB_UNK_CONFIGURATION;
827 uaa.ifaceno = UHUB_UNK_INTERFACE;
828 uaa.vendor = UHUB_UNK_VENDOR;
829 uaa.product = UHUB_UNK_PRODUCT;
830 uaa.release = UHUB_UNK_RELEASE;
831 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
832 if (dv) {
833 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
834 if (dev->subdevs == 0)
835 return (USBD_NOMEM);
836 dev->subdevs[0] = dv;
837 dev->subdevs[1] = 0;
838 return (USBD_NORMAL_COMPLETION);
839 }
840
841 /*
842 * The generic attach failed, but leave the device as it is.
843 * We just did not find any drivers, that's all. The device is
844 * fully operational and not harming anyone.
845 */
846 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
847 #if defined(__FreeBSD__)
848 /*
849 * XXX should we delete the child again? Left for now to avoid dangling
850 * references.
851 device_delete_child(*parent, bdev);
852 */
853 #endif
854 return (USBD_NORMAL_COMPLETION);
855 }
856
857
858
859 /*
860 * Called when a new device has been put in the powered state,
861 * but not yet in the addressed state.
862 * Get initial descriptor, set the address, get full descriptor,
863 * and attach a driver.
864 */
865 usbd_status
866 usbd_new_device(parent, bus, depth, lowspeed, port, up)
867 bdevice *parent;
868 usbd_bus_handle bus;
869 int depth;
870 int lowspeed;
871 int port;
872 struct usbd_port *up;
873 {
874 usbd_device_handle dev;
875 usb_device_descriptor_t *dd;
876 usbd_status r;
877 int addr;
878 int i;
879
880 DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
881 bus, depth, lowspeed));
882 addr = usbd_getnewaddr(bus);
883 if (addr < 0) {
884 printf("%s: No free USB addresses, new device ignored.\n",
885 USBDEVNAME(bus->bdev));
886 return (USBD_NO_ADDR);
887 }
888
889 dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
890 if (dev == 0)
891 return (USBD_NOMEM);
892 memset(dev, 0, sizeof(*dev));
893
894 dev->bus = bus;
895
896 /* Set up default endpoint handle. */
897 dev->def_ep.edesc = &dev->def_ep_desc;
898
899 /* Set up default endpoint descriptor. */
900 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
901 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
902 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
903 dev->def_ep_desc.bmAttributes = UE_CONTROL;
904 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
905 dev->def_ep_desc.bInterval = 0;
906
907 dev->quirks = &usbd_no_quirk;
908 dev->address = USB_START_ADDR;
909 dev->ddesc.bMaxPacketSize = 0;
910 dev->lowspeed = lowspeed != 0;
911 dev->depth = depth;
912 dev->powersrc = up;
913 dev->langid = USBD_NOLANG;
914
915 /* Establish the the default pipe. */
916 r = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
917 if (r != USBD_NORMAL_COMPLETION) {
918 usbd_remove_device(dev, up);
919 return (r);
920 }
921
922 up->device = dev;
923 dd = &dev->ddesc;
924 /* Try a few times in case the device is slow (i.e. outside specs.) */
925 for (i = 0; i < 5; i++) {
926 /* Get the first 8 bytes of the device descriptor. */
927 r = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
928 if (r == USBD_NORMAL_COMPLETION)
929 break;
930 usbd_delay_ms(dev, 200);
931 }
932 if (r != USBD_NORMAL_COMPLETION) {
933 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
934 "failed\n",
935 addr));
936 usbd_remove_device(dev, up);
937 return (r);
938 }
939
940 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
941 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, ls=%d\n",
942 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
943 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
944 dev->lowspeed));
945
946 if (dd->bDescriptorType != UDESC_DEVICE) {
947 /* Illegal device descriptor */
948 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
949 dd->bDescriptorType));
950 usbd_remove_device(dev, up);
951 return (USBD_INVAL);
952 }
953
954 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
955 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
956 usbd_remove_device(dev, up);
957 return (USBD_INVAL);
958 }
959
960 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
961
962 /* Get the full device descriptor. */
963 r = usbd_get_device_desc(dev, dd);
964 if (r != USBD_NORMAL_COMPLETION) {
965 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
966 "failed\n", addr));
967 usbd_remove_device(dev, up);
968 return (r);
969 }
970
971 /* Figure out what's wrong with this device. */
972 dev->quirks = usbd_find_quirk(dd);
973
974 /* Set the address */
975 r = usbd_set_address(dev, addr);
976 if (r != USBD_NORMAL_COMPLETION) {
977 DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
978 r = USBD_SET_ADDR_FAILED;
979 usbd_remove_device(dev, up);
980 return (r);
981 }
982 /* Allow device time to set new address */
983 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
984
985 dev->address = addr; /* New device address now */
986 bus->devices[addr] = dev;
987
988 /* Assume 100mA bus powered for now. Changed when configured. */
989 dev->power = USB_MIN_POWER;
990 dev->self_powered = 0;
991
992 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
993 addr, dev, parent));
994
995 r = usbd_probe_and_attach(parent, dev, port, addr);
996 if (r != USBD_NORMAL_COMPLETION) {
997 usbd_remove_device(dev, up);
998 return (r);
999 }
1000
1001 return (USBD_NORMAL_COMPLETION);
1002 }
1003
1004 void
1005 usbd_remove_device(dev, up)
1006 usbd_device_handle dev;
1007 struct usbd_port *up;
1008 {
1009 DPRINTF(("usbd_remove_device: %p\n", dev));
1010
1011 if (dev->default_pipe)
1012 usbd_kill_pipe(dev->default_pipe);
1013 up->device = 0;
1014 dev->bus->devices[dev->address] = 0;
1015
1016 free(dev, M_USB);
1017 }
1018
1019 #if defined(__NetBSD__) || defined(__OpenBSD__)
1020 int
1021 usbd_print(aux, pnp)
1022 void *aux;
1023 const char *pnp;
1024 {
1025 struct usb_attach_arg *uaa = aux;
1026 char devinfo[1024];
1027
1028 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1029 if (pnp) {
1030 if (!uaa->usegeneric)
1031 return (QUIET);
1032 usbd_devinfo(uaa->device, 1, devinfo);
1033 printf("%s, %s", devinfo, pnp);
1034 }
1035 if (uaa->port != 0)
1036 printf(" port %d", uaa->port);
1037 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1038 printf(" configuration %d", uaa->configno);
1039 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1040 printf(" interface %d", uaa->ifaceno);
1041 #if 0
1042 /*
1043 * It gets very crowded with these locators on the attach line.
1044 * They are not really needed since they are printed in the clear
1045 * by each driver.
1046 */
1047 if (uaa->vendor != UHUB_UNK_VENDOR)
1048 printf(" vendor 0x%04x", uaa->vendor);
1049 if (uaa->product != UHUB_UNK_PRODUCT)
1050 printf(" product 0x%04x", uaa->product);
1051 if (uaa->release != UHUB_UNK_RELEASE)
1052 printf(" release 0x%04x", uaa->release);
1053 #endif
1054 return (UNCONF);
1055 }
1056
1057 #if defined(__NetBSD__)
1058 int
1059 usbd_submatch(parent, cf, aux)
1060 struct device *parent;
1061 struct cfdata *cf;
1062 void *aux;
1063 {
1064 #elif defined(__OpenBSD__)
1065 int
1066 usbd_submatch(parent, match, aux)
1067 struct device *parent;
1068 void *match;
1069 void *aux;
1070 {
1071 struct cfdata *cf = match;
1072 #endif
1073 struct usb_attach_arg *uaa = aux;
1074
1075 if ((uaa->port != 0 &&
1076 cf->uhubcf_port != UHUB_UNK_PORT &&
1077 cf->uhubcf_port != uaa->port) ||
1078 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1079 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1080 cf->uhubcf_configuration != uaa->configno) ||
1081 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1082 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1083 cf->uhubcf_interface != uaa->ifaceno) ||
1084 (uaa->vendor != UHUB_UNK_VENDOR &&
1085 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1086 cf->uhubcf_vendor != uaa->vendor) ||
1087 (uaa->product != UHUB_UNK_PRODUCT &&
1088 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1089 cf->uhubcf_product != uaa->product) ||
1090 (uaa->release != UHUB_UNK_RELEASE &&
1091 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1092 cf->uhubcf_release != uaa->release)
1093 )
1094 return 0;
1095 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1096 }
1097
1098 #elif defined(__FreeBSD__)
1099 static void
1100 usbd_bus_print_child(device_t bus, device_t dev)
1101 {
1102 /* FIXME print the device address and the configuration used
1103 */
1104 }
1105 #endif
1106
1107 void
1108 usbd_fill_deviceinfo(dev, di)
1109 usbd_device_handle dev;
1110 struct usb_device_info *di;
1111 {
1112 struct usbd_port *p;
1113 int i, r, s;
1114
1115 di->config = dev->config;
1116 usbd_devinfo_vp(dev, di->vendor, di->product);
1117 usbd_printBCD(di->release, UGETW(dev->ddesc.bcdDevice));
1118 di->vendorNo = UGETW(dev->ddesc.idVendor);
1119 di->productNo = UGETW(dev->ddesc.idProduct);
1120 di->class = dev->ddesc.bDeviceClass;
1121 di->power = dev->self_powered ? 0 : dev->power;
1122 di->lowspeed = dev->lowspeed;
1123 di->addr = dev->address;
1124 if (dev->hub) {
1125 for (i = 0;
1126 i < sizeof(di->ports) / sizeof(di->ports[0]) &&
1127 i < dev->hub->hubdesc.bNbrPorts;
1128 i++) {
1129 p = &dev->hub->ports[i];
1130 if (p->device)
1131 r = p->device->address;
1132 else {
1133 s = UGETW(p->status.wPortStatus);
1134 if (s & UPS_PORT_ENABLED)
1135 r = USB_PORT_ENABLED;
1136 else if (s & UPS_SUSPEND)
1137 r = USB_PORT_SUSPENDED;
1138 else if (s & UPS_PORT_POWER)
1139 r = USB_PORT_POWERED;
1140 else
1141 r = USB_PORT_DISABLED;
1142 }
1143 di->ports[i] = r;
1144 }
1145 di->nports = dev->hub->hubdesc.bNbrPorts;
1146 } else
1147 di->nports = 0;
1148 }
1149
1150 void
1151 usb_free_device(dev)
1152 usbd_device_handle dev;
1153 {
1154 int ifcidx, nifc;
1155
1156 if (dev->default_pipe)
1157 usbd_kill_pipe(dev->default_pipe);
1158 if (dev->ifaces) {
1159 nifc = dev->cdesc->bNumInterface;
1160 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1161 usbd_free_iface_data(dev, ifcidx);
1162 free(dev->ifaces, M_USB);
1163 }
1164 if (dev->cdesc)
1165 free(dev->cdesc, M_USB);
1166 if (dev->subdevs)
1167 free(dev->subdevs, M_USB);
1168 free(dev, M_USB);
1169 }
1170