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