usb_subr.c revision 1.23 1 /* $NetBSD: usb_subr.c,v 1.23 1998/12/30 18:06:25 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 usb_interface_descriptor_t *usbd_find_idesc __P((usb_config_descriptor_t *cd,
78 int ifaceidx, int altidx));
79 void usbd_free_iface_data __P((usbd_device_handle dev, int ifcno));
80 void usbd_kill_pipe __P((usbd_pipe_handle));
81 usbd_status usbd_probe_and_attach
82 __P((bdevice *parent, usbd_device_handle dev, int port, int addr));
83
84 #ifdef USBVERBOSE
85 typedef u_int16_t usb_vendor_id_t;
86 typedef u_int16_t usb_product_id_t;
87
88 /*
89 * Descriptions of of known vendors and devices ("products").
90 */
91 struct usb_knowndev {
92 usb_vendor_id_t vendor;
93 usb_product_id_t product;
94 int flags;
95 char *vendorname, *productname;
96 };
97 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
98
99 #include <dev/usb/usbdevs_data.h>
100 #endif /* USBVERBOSE */
101
102 #ifdef USB_DEBUG
103 char *usbd_error_strs[] = {
104 "NORMAL_COMPLETION",
105 "IN_PROGRESS",
106 "PENDING_REQUESTS",
107 "NOT_STARTED",
108 "INVAL",
109 "IS_IDLE",
110 "NOMEM",
111 "CANCELLED",
112 "BAD_ADDRESS",
113 "IN_USE",
114 "INTERFACE_NOT_ACTIVE",
115 "NO_ADDR",
116 "SET_ADDR_FAILED",
117 "NO_POWER",
118 "TOO_DEEP",
119 "IOERROR",
120 "NOT_CONFIGURED",
121 "TIMEOUT",
122 "SHORT_XFER",
123 "STALLED",
124 "XXX",
125 };
126 #endif
127
128 usbd_status
129 usbd_get_string_desc(dev, sindex, langid, sdesc)
130 usbd_device_handle dev;
131 int sindex;
132 int langid;
133 usb_string_descriptor_t *sdesc;
134 {
135 usb_device_request_t req;
136 usbd_status r;
137
138 req.bmRequestType = UT_READ_DEVICE;
139 req.bRequest = UR_GET_DESCRIPTOR;
140 USETW2(req.wValue, UDESC_STRING, sindex);
141 USETW(req.wIndex, langid);
142 USETW(req.wLength, 1); /* only size byte first */
143 r = usbd_do_request(dev, &req, sdesc);
144 if (r != USBD_NORMAL_COMPLETION)
145 return (r);
146 USETW(req.wLength, sdesc->bLength); /* the whole string */
147 return (usbd_do_request(dev, &req, sdesc));
148 }
149
150 char *
151 usbd_get_string(dev, si, buf)
152 usbd_device_handle dev;
153 int si;
154 char *buf;
155 {
156 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
157 usb_string_descriptor_t us;
158 char *s;
159 int i, n;
160 u_int16_t c;
161 usbd_status r;
162
163 if (si == 0)
164 return (0);
165 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
166 return (0);
167 if (dev->langid == USBD_NOLANG) {
168 /* Set up default language */
169 r = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us);
170 if (r != USBD_NORMAL_COMPLETION || us.bLength < 4) {
171 dev->langid = 0; /* Well, just pick English then */
172 } else {
173 /* Pick the first language as the default. */
174 dev->langid = UGETW(us.bString[0]);
175 }
176 }
177 r = usbd_get_string_desc(dev, si, dev->langid, &us);
178 if (r != USBD_NORMAL_COMPLETION)
179 return (0);
180 s = buf;
181 n = us.bLength / 2 - 1;
182 for (i = 0; i < n; i++) {
183 c = UGETW(us.bString[i]);
184 /* Convert from Unicode, handle buggy strings. */
185 if ((c & 0xff00) == 0)
186 *s++ = c;
187 else if ((c & 0x00ff) == 0 && swap)
188 *s++ = c >> 8;
189 else
190 *s++ = '?';
191 }
192 *s++ = 0;
193 return buf;
194 }
195
196 void
197 usbd_devinfo_vp(dev, v, p)
198 usbd_device_handle dev;
199 char *v, *p;
200 {
201 usb_device_descriptor_t *udd = &dev->ddesc;
202 char *vendor = 0, *product = 0;
203 #ifdef USBVERBOSE
204 struct usb_knowndev *kdp;
205 #endif
206
207 vendor = usbd_get_string(dev, udd->iManufacturer, v);
208 product = usbd_get_string(dev, udd->iProduct, p);
209 #ifdef USBVERBOSE
210 if (!vendor) {
211 for(kdp = usb_knowndevs;
212 kdp->vendorname != NULL;
213 kdp++) {
214 if (kdp->vendor == UGETW(udd->idVendor) &&
215 (kdp->product == UGETW(udd->idProduct) ||
216 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
217 break;
218 }
219 if (kdp->vendorname == NULL)
220 vendor = product = NULL;
221 else {
222 vendor = kdp->vendorname;
223 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
224 kdp->productname : NULL;
225 }
226 }
227 #endif
228 if (vendor)
229 strcpy(v, vendor);
230 else
231 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
232 if (product)
233 strcpy(p, product);
234 else
235 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
236 }
237
238 int
239 usbd_printBCD(cp, bcd)
240 char *cp;
241 int bcd;
242 {
243 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
244 }
245
246 void
247 usbd_devinfo(dev, showclass, cp)
248 usbd_device_handle dev;
249 int showclass;
250 char *cp;
251 {
252 usb_device_descriptor_t *udd = &dev->ddesc;
253 char vendor[USB_MAX_STRING_LEN];
254 char product[USB_MAX_STRING_LEN];
255 int bcdDevice, bcdUSB;
256
257 usbd_devinfo_vp(dev, vendor, product);
258 cp += sprintf(cp, "%s %s", vendor, product);
259 if (showclass)
260 cp += sprintf(cp, ", class %d/%d",
261 udd->bDeviceClass, udd->bDeviceSubClass);
262 bcdUSB = UGETW(udd->bcdUSB);
263 bcdDevice = UGETW(udd->bcdDevice);
264 cp += sprintf(cp, ", rev ");
265 cp += usbd_printBCD(cp, bcdUSB);
266 *cp++ = '/';
267 cp += usbd_printBCD(cp, bcdDevice);
268 cp += sprintf(cp, ", addr %d", dev->address);
269 *cp = 0;
270 }
271
272 /* Delay for a certain number of ms */
273 void
274 usb_delay_ms(bus, ms)
275 usbd_bus_handle bus;
276 u_int ms;
277 {
278 /* Wait at least two clock ticks so we know the time has passed. */
279 if (bus->use_polling)
280 delay((ms+1) * 1000);
281 else
282 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
283 }
284
285 /* Delay given a device handle. */
286 void
287 usbd_delay_ms(dev, ms)
288 usbd_device_handle dev;
289 u_int ms;
290 {
291 usb_delay_ms(dev->bus, ms);
292 }
293
294 usbd_status
295 usbd_reset_port(dev, port, ps)
296 usbd_device_handle dev;
297 int port;
298 usb_port_status_t *ps;
299 {
300 usb_device_request_t req;
301 usbd_status r;
302 int n;
303
304 req.bmRequestType = UT_WRITE_CLASS_OTHER;
305 req.bRequest = UR_SET_FEATURE;
306 USETW(req.wValue, UHF_PORT_RESET);
307 USETW(req.wIndex, port);
308 USETW(req.wLength, 0);
309 r = usbd_do_request(dev, &req, 0);
310 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%d(%s)\n",
311 port, r, usbd_error_strs[r]));
312 if (r != USBD_NORMAL_COMPLETION)
313 return (r);
314 n = 10;
315 do {
316 /* Wait for device to recover from reset. */
317 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
318 r = usbd_get_port_status(dev, port, ps);
319 if (r != USBD_NORMAL_COMPLETION) {
320 DPRINTF(("usbd_reset_port: get status failed %d\n",r));
321 return (r);
322 }
323 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
324 if (n == 0) {
325 printf("usbd_reset_port: timeout\n");
326 return (USBD_IOERROR);
327 }
328 r = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
329 #ifdef USB_DEBUG
330 if (r != USBD_NORMAL_COMPLETION)
331 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",r));
332 #endif
333
334 /* Wait for the device to recover from reset. */
335 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
336 return (r);
337 }
338
339 usb_interface_descriptor_t *
340 usbd_find_idesc(cd, ifaceidx, altidx)
341 usb_config_descriptor_t *cd;
342 int ifaceidx;
343 int altidx;
344 {
345 char *p = (char *)cd;
346 char *end = p + UGETW(cd->wTotalLength);
347 usb_interface_descriptor_t *d;
348 int curidx, lastidx, curaidx = 0;
349
350 for (curidx = lastidx = -1; p < end; ) {
351 d = (usb_interface_descriptor_t *)p;
352 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
353 "type=%d\n",
354 ifaceidx, curidx, altidx, curaidx,
355 d->bLength, d->bDescriptorType));
356 if (d->bLength == 0) /* bad descriptor */
357 break;
358 p += d->bLength;
359 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
360 if (d->bInterfaceNumber != lastidx) {
361 lastidx = d->bInterfaceNumber;
362 curidx++;
363 curaidx = 0;
364 } else
365 curaidx++;
366 if (ifaceidx == curidx && altidx == curaidx)
367 return (d);
368 }
369 }
370 return (0);
371 }
372
373 usb_endpoint_descriptor_t *
374 usbd_find_edesc(cd, ifaceidx, altidx, endptidx)
375 usb_config_descriptor_t *cd;
376 int ifaceidx;
377 int altidx;
378 int endptidx;
379 {
380 char *p = (char *)cd;
381 char *end = p + UGETW(cd->wTotalLength);
382 usb_interface_descriptor_t *d;
383 usb_endpoint_descriptor_t *e;
384 int curidx;
385
386 d = usbd_find_idesc(cd, ifaceidx, altidx);
387 if (!d)
388 return (0);
389 if (endptidx >= d->bNumEndpoints) /* quick exit */
390 return (0);
391
392 curidx = -1;
393 for (p = (char *)d + d->bLength; p < end; ) {
394 e = (usb_endpoint_descriptor_t *)p;
395 if (e->bLength == 0) /* bad descriptor */
396 break;
397 p += e->bLength;
398 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
399 return (0);
400 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
401 curidx++;
402 if (curidx == endptidx)
403 return (e);
404 }
405 }
406 return (0);
407 }
408
409 usbd_status
410 usbd_fill_iface_data(dev, ifaceidx, altidx)
411 usbd_device_handle dev;
412 int ifaceidx;
413 int altidx;
414 {
415 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
416 char *p, *end;
417 int endpt, nendpt;
418 usbd_status r;
419
420 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
421 ifaceidx, altidx));
422 ifc->device = dev;
423 ifc->idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
424 if (ifc->idesc == 0)
425 return (USBD_INVAL);
426 ifc->index = ifaceidx;
427 ifc->altindex = altidx;
428 nendpt = ifc->idesc->bNumEndpoints;
429 DPRINTFN(10,("usbd_fill_iface_data: found idesc n=%d\n", nendpt));
430 if (nendpt != 0) {
431 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
432 M_USB, M_NOWAIT);
433 if (ifc->endpoints == 0)
434 return (USBD_NOMEM);
435 } else
436 ifc->endpoints = 0;
437 ifc->priv = 0;
438 p = (char *)ifc->idesc + ifc->idesc->bLength;
439 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
440 #define ed ((usb_endpoint_descriptor_t *)p)
441 for (endpt = 0; endpt < nendpt; endpt++) {
442 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
443 for (; p < end; p += ed->bLength) {
444 ed = (usb_endpoint_descriptor_t *)p;
445 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
446 "len=%d type=%d\n",
447 p, end, ed->bLength, ed->bDescriptorType));
448 if (p + ed->bLength <= end &&
449 ed->bDescriptorType == UDESC_ENDPOINT)
450 break;
451 if (ed->bDescriptorType == UDESC_INTERFACE ||
452 ed->bLength == 0) {
453 r = USBD_INVAL;
454 goto bad;
455 }
456 }
457 ifc->endpoints[endpt].edesc = ed;
458 ifc->endpoints[endpt].state = USBD_ENDPOINT_ACTIVE;
459 ifc->endpoints[endpt].refcnt = 0;
460 ifc->endpoints[endpt].toggle = 0;
461 }
462 #undef ed
463 LIST_INIT(&ifc->pipes);
464 ifc->state = USBD_INTERFACE_ACTIVE;
465 return (USBD_NORMAL_COMPLETION);
466 bad:
467 free(ifc->endpoints, M_USB);
468 return (r);
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 /* XXX isn't it bad to build &uaa into a value that has a lifetime
726 longer than this function call? */
727 bdevice bdev;
728 bdev = device_add_child(*parent, NULL, -1, &uaa);
729 if (bdev) {
730 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
731 return (USBD_INVAL);
732 }
733 #endif
734
735 uaa.device = dev;
736 uaa.iface = 0;
737 uaa.ifaces = 0;
738 uaa.nifaces = 0;
739 uaa.usegeneric = 0;
740 uaa.port = port;
741 uaa.configno = UHUB_UNK_CONFIGURATION;
742 uaa.ifaceno = UHUB_UNK_INTERFACE;
743
744 /* First try with device specific drivers. */
745 if (USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch))
746 return (USBD_NORMAL_COMPLETION);
747
748 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
749
750 /* Next try with interface drivers. */
751 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
752 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
753 confi));
754 r = usbd_set_config_index(dev, confi, 1);
755 if (r != USBD_NORMAL_COMPLETION) {
756 #ifdef USB_DEBUG
757 DPRINTF(("%s: port %d, set config at addr %d failed, "
758 "error=%d(%s)\n", USBDEVNAME(*parent), port,
759 addr, r, usbd_error_strs[r]));
760 #else
761 printf("%s: port %d, set config at addr %d failed\n",
762 USBDEVNAME(*parent), port, addr);
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 if (USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch))
798 return (USBD_NORMAL_COMPLETION);
799
800 /*
801 * The generic attach failed, but leave the device as it is.
802 * We just did not find any drivers, that's all. The device is
803 * fully operational and not harming anyone.
804 */
805 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
806 return (USBD_NORMAL_COMPLETION);
807 }
808
809
810
811 /*
812 * Called when a new device has been put in the powered state,
813 * but not yet in the addressed state.
814 * Get initial descriptor, set the address, get full descriptor,
815 * and attach a driver.
816 */
817 usbd_status
818 usbd_new_device(parent, bus, depth, lowspeed, port, up)
819 bdevice *parent;
820 usbd_bus_handle bus;
821 int depth;
822 int lowspeed;
823 int port;
824 struct usbd_port *up;
825 {
826 usbd_device_handle dev;
827 usb_device_descriptor_t *dd;
828 usbd_status r;
829 int addr;
830 int i;
831
832 DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
833 bus, depth, lowspeed));
834 addr = usbd_getnewaddr(bus);
835 if (addr < 0) {
836 printf("%s: No free USB addresses, new device ignored.\n",
837 USBDEVNAME(bus->bdev));
838 return (USBD_NO_ADDR);
839 }
840
841 dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
842 if (dev == 0)
843 return (USBD_NOMEM);
844 memset(dev, 0, sizeof(*dev));
845
846 dev->bus = bus;
847
848 /* Set up default endpoint handle. */
849 dev->def_ep.edesc = &dev->def_ep_desc;
850 dev->def_ep.state = USBD_ENDPOINT_ACTIVE;
851
852 /* Set up default endpoint descriptor. */
853 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
854 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
855 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
856 dev->def_ep_desc.bmAttributes = UE_CONTROL;
857 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
858 dev->def_ep_desc.bInterval = 0;
859
860 dev->state = USBD_DEVICE_DEFAULT;
861 dev->quirks = &usbd_no_quirk;
862 dev->address = USB_START_ADDR;
863 dev->ddesc.bMaxPacketSize = 0;
864 dev->lowspeed = lowspeed != 0;
865 dev->depth = depth;
866 dev->powersrc = up;
867 dev->langid = USBD_NOLANG;
868
869 /* Establish the the default pipe. */
870 r = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
871 if (r != USBD_NORMAL_COMPLETION) {
872 usbd_remove_device(dev, up);
873 return (r);
874 }
875
876 up->device = dev;
877 dd = &dev->ddesc;
878 /* Try a few times in case the device is slow (i.e. outside specs.) */
879 for (i = 0; i < 5; i++) {
880 /* Get the first 8 bytes of the device descriptor. */
881 r = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
882 if (r == USBD_NORMAL_COMPLETION)
883 break;
884 usbd_delay_ms(dev, 200);
885 }
886 if (r != USBD_NORMAL_COMPLETION) {
887 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
888 "failed\n",
889 addr));
890 usbd_remove_device(dev, up);
891 return (r);
892 }
893
894 if (dd->bDescriptorType != UDESC_DEVICE) {
895 /* Illegal device descriptor */
896 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
897 dd->bDescriptorType));
898 usbd_remove_device(dev, up);
899 return (USBD_INVAL);
900 }
901
902 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
903 "subclass=%d, protocol=%d, maxpacket=%d, ls=%d\n",
904 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
905 dd->bDeviceProtocol, dd->bMaxPacketSize, dev->lowspeed));
906
907 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
908
909 /* Get the full device descriptor. */
910 r = usbd_get_device_desc(dev, dd);
911 if (r != USBD_NORMAL_COMPLETION) {
912 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
913 "failed\n", addr));
914 usbd_remove_device(dev, up);
915 return (r);
916 }
917
918 /* Figure out what's wrong with this device. */
919 dev->quirks = usbd_find_quirk(dd);
920
921 /* Set the address */
922 r = usbd_set_address(dev, addr);
923 if (r != USBD_NORMAL_COMPLETION) {
924 DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
925 r = USBD_SET_ADDR_FAILED;
926 usbd_remove_device(dev, up);
927 return (r);
928 }
929 /* Allow device time to set new address */
930 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
931
932 dev->address = addr; /* New device address now */
933 dev->state = USBD_DEVICE_ADDRESSED;
934 bus->devices[addr] = dev;
935
936 /* Assume 100mA bus powered for now. Changed when configured. */
937 dev->power = USB_MIN_POWER;
938 dev->self_powered = 0;
939
940 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
941 addr, dev, parent));
942
943 r = usbd_probe_and_attach(parent, dev, port, addr);
944 if (r != USBD_NORMAL_COMPLETION) {
945 usbd_remove_device(dev, up);
946 return (r);
947 }
948
949 return (USBD_NORMAL_COMPLETION);
950 }
951
952 void
953 usbd_remove_device(dev, up)
954 usbd_device_handle dev;
955 struct usbd_port *up;
956 {
957 DPRINTF(("usbd_remove_device: %p\n", dev));
958
959 #if defined(__NetBSD__)
960 /* XXX bit of a hack, only for hubs the detach is called
961 *
962 * easiest solution, register a detach method in the softc, call that
963 * one and pass the device struct to it, or the softc. Whatever.
964 */
965 /*if (dev->bdev && dev->hub)
966 uhub_detach(dev->hub->hubdata);*/
967 #elif defined(__FreeBSD__)
968 if (dev->bdev)
969 device_delete_child(device_get_parent(dev->bdev), dev->bdev);
970 #endif
971
972 if (dev->default_pipe)
973 usbd_kill_pipe(dev->default_pipe);
974 up->device = 0;
975 dev->bus->devices[dev->address] = 0;
976
977 free(dev, M_USB);
978 }
979
980 #if defined(__NetBSD__)
981 int
982 usbd_print(aux, pnp)
983 void *aux;
984 const char *pnp;
985 {
986 struct usb_attach_arg *uaa = aux;
987 char devinfo[1024];
988
989 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
990 if (pnp) {
991 if (!uaa->usegeneric)
992 return (QUIET);
993 usbd_devinfo(uaa->device, 1, devinfo);
994 printf("%s, %s", devinfo, pnp);
995 }
996 if (uaa->port != 0)
997 printf(" port %d", uaa->port);
998 if (uaa->configno != UHUB_UNK_CONFIGURATION)
999 printf(" configuration %d", uaa->configno);
1000 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1001 printf(" interface %d", uaa->ifaceno);
1002 return (UNCONF);
1003 }
1004
1005 int
1006 usbd_submatch(parent, cf, aux)
1007 struct device *parent;
1008 struct cfdata *cf;
1009 void *aux;
1010 {
1011 struct usb_attach_arg *uaa = aux;
1012
1013 if ((uaa->port != 0 &&
1014 cf->uhubcf_port != UHUB_UNK_PORT &&
1015 cf->uhubcf_port != uaa->port) ||
1016 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1017 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1018 cf->uhubcf_configuration != uaa->configno) ||
1019 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1020 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1021 cf->uhubcf_interface != uaa->ifaceno))
1022 return 0;
1023 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1024 }
1025
1026 #elif defined(__FreeBSD__)
1027 static void
1028 usbd_bus_print_child(device_t bus, device_t dev)
1029 {
1030 /* FIXME print the device address and the configuration used
1031 */
1032 }
1033 #endif
1034
1035 usbd_status
1036 usb_insert_transfer(reqh)
1037 usbd_request_handle reqh;
1038 {
1039 usbd_pipe_handle pipe = reqh->pipe;
1040 usbd_interface_handle iface = pipe->iface;
1041
1042 if (pipe->state == USBD_PIPE_IDLE ||
1043 (iface && iface->state == USBD_INTERFACE_IDLE))
1044 return (USBD_IS_IDLE);
1045 SIMPLEQ_INSERT_TAIL(&pipe->queue, reqh, next);
1046 if (pipe->state != USBD_PIPE_ACTIVE ||
1047 (iface && iface->state != USBD_INTERFACE_ACTIVE))
1048 return (USBD_NOT_STARTED);
1049 if (pipe->running)
1050 return (USBD_IN_PROGRESS);
1051 pipe->running = 1;
1052 return (USBD_NORMAL_COMPLETION);
1053 }
1054
1055 void
1056 usb_start_next(pipe)
1057 usbd_pipe_handle pipe;
1058 {
1059 usbd_request_handle reqh;
1060 usbd_status r;
1061
1062 #ifdef DIAGNOSTIC
1063 if (SIMPLEQ_FIRST(&pipe->queue) == 0) {
1064 printf("usb_start_next: empty\n");
1065 return;
1066 }
1067 #endif
1068
1069 /* First remove remove old */
1070 SIMPLEQ_REMOVE_HEAD(&pipe->queue, SIMPLEQ_FIRST(&pipe->queue), next);
1071 if (pipe->state != USBD_PIPE_ACTIVE) {
1072 pipe->running = 0;
1073 return;
1074 }
1075 reqh = SIMPLEQ_FIRST(&pipe->queue);
1076 DPRINTFN(5, ("usb_start_next: start reqh=%p\n", reqh));
1077 if (!reqh)
1078 pipe->running = 0;
1079 else {
1080 r = pipe->methods->start(reqh);
1081 if (r != USBD_IN_PROGRESS) {
1082 printf("usb_start_next: error=%d\n", r);
1083 pipe->running = 0;
1084 /* XXX do what? */
1085 }
1086 }
1087 }
1088
1089 void
1090 usbd_fill_deviceinfo(dev, di)
1091 usbd_device_handle dev;
1092 struct usb_device_info *di;
1093 {
1094 struct usbd_port *p;
1095 int i, r, s;
1096
1097 di->config = dev->config;
1098 usbd_devinfo_vp(dev, di->vendor, di->product);
1099 usbd_printBCD(di->revision, UGETW(dev->ddesc.bcdDevice));
1100 di->vendorNo = UGETW(dev->ddesc.idVendor);
1101 di->productNo = UGETW(dev->ddesc.idProduct);
1102 di->class = dev->ddesc.bDeviceClass;
1103 di->power = dev->self_powered ? 0 : dev->power;
1104 di->lowspeed = dev->lowspeed;
1105 di->addr = dev->address;
1106 if (dev->hub) {
1107 for (i = 0;
1108 i < sizeof(di->ports) / sizeof(di->ports[0]) &&
1109 i < dev->hub->hubdesc.bNbrPorts;
1110 i++) {
1111 p = &dev->hub->ports[i];
1112 if (p->device)
1113 r = p->device->address;
1114 else {
1115 s = UGETW(p->status.wPortStatus);
1116 if (s & UPS_PORT_ENABLED)
1117 r = USB_PORT_ENABLED;
1118 else if (s & UPS_SUSPEND)
1119 r = USB_PORT_SUSPENDED;
1120 else if (s & UPS_PORT_POWER)
1121 r = USB_PORT_POWERED;
1122 else
1123 r = USB_PORT_DISABLED;
1124 }
1125 di->ports[i] = r;
1126 }
1127 di->nports = dev->hub->hubdesc.bNbrPorts;
1128 } else
1129 di->nports = 0;
1130 }
1131