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