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