usb_subr.c revision 1.24 1 /* $NetBSD: usb_subr.c,v 1.24 1999/01/01 15:21:42 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
419 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
420 ifaceidx, altidx));
421 ifc->device = dev;
422 ifc->idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
423 if (ifc->idesc == 0)
424 return (USBD_INVAL);
425 ifc->index = ifaceidx;
426 ifc->altindex = altidx;
427 nendpt = ifc->idesc->bNumEndpoints;
428 DPRINTFN(10,("usbd_fill_iface_data: found idesc n=%d\n", nendpt));
429 if (nendpt != 0) {
430 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
431 M_USB, M_NOWAIT);
432 if (ifc->endpoints == 0)
433 return (USBD_NOMEM);
434 } else
435 ifc->endpoints = 0;
436 ifc->priv = 0;
437 p = (char *)ifc->idesc + ifc->idesc->bLength;
438 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
439 #define ed ((usb_endpoint_descriptor_t *)p)
440 for (endpt = 0; endpt < nendpt; endpt++) {
441 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
442 for (; p < end; p += ed->bLength) {
443 ed = (usb_endpoint_descriptor_t *)p;
444 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
445 "len=%d type=%d\n",
446 p, end, ed->bLength, ed->bDescriptorType));
447 if (p + ed->bLength <= end && ed->bLength != 0 &&
448 ed->bDescriptorType == UDESC_ENDPOINT)
449 goto found;
450 if (ed->bDescriptorType == UDESC_INTERFACE ||
451 ed->bLength == 0)
452 break;
453 }
454 /* passed end, or bad desc */
455 goto bad;
456 found:
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 p += ed->bLength;
462 }
463 #undef ed
464 LIST_INIT(&ifc->pipes);
465 ifc->state = USBD_INTERFACE_ACTIVE;
466 return (USBD_NORMAL_COMPLETION);
467
468 bad:
469 free(ifc->endpoints, M_USB);
470 return (USBD_INVAL);
471 }
472
473 void
474 usbd_free_iface_data(dev, ifcno)
475 usbd_device_handle dev;
476 int ifcno;
477 {
478 usbd_interface_handle ifc = &dev->ifaces[ifcno];
479 if (ifc->endpoints)
480 free(ifc->endpoints, M_USB);
481 }
482
483 static usbd_status
484 usbd_set_config(dev, conf)
485 usbd_device_handle dev;
486 int conf;
487 {
488 usb_device_request_t req;
489
490 req.bmRequestType = UT_WRITE_DEVICE;
491 req.bRequest = UR_SET_CONFIG;
492 USETW(req.wValue, conf);
493 USETW(req.wIndex, 0);
494 USETW(req.wLength, 0);
495 return (usbd_do_request(dev, &req, 0));
496 }
497
498 usbd_status
499 usbd_set_config_no(dev, no, msg)
500 usbd_device_handle dev;
501 int no;
502 int msg;
503 {
504 int index;
505 usb_config_descriptor_t cd;
506 usbd_status r;
507
508 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
509 /* Figure out what config index to use. */
510 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
511 r = usbd_get_config_desc(dev, index, &cd);
512 if (r != USBD_NORMAL_COMPLETION)
513 return (r);
514 if (cd.bConfigurationValue == no)
515 return (usbd_set_config_index(dev, index, msg));
516 }
517 return (USBD_INVAL);
518 }
519
520 usbd_status
521 usbd_set_config_index(dev, index, msg)
522 usbd_device_handle dev;
523 int index;
524 int msg;
525 {
526 usb_status_t ds;
527 usb_hub_status_t hs;
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 dev->state = USBD_DEVICE_ADDRESSED;
547 }
548
549 /* Figure out what config number to use. */
550 r = usbd_get_config_desc(dev, index, &cd);
551 if (r != USBD_NORMAL_COMPLETION)
552 return (r);
553 len = UGETW(cd.wTotalLength);
554 cdp = malloc(len, M_USB, M_NOWAIT);
555 if (cdp == 0)
556 return (USBD_NOMEM);
557 r = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
558 if (r != USBD_NORMAL_COMPLETION)
559 goto bad;
560 if (cdp->bDescriptorType != UDESC_CONFIG) {
561 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
562 cdp->bDescriptorType));
563 r = USBD_INVAL;
564 goto bad;
565 }
566 selfpowered = 0;
567 if (cdp->bmAttributes & UC_SELF_POWERED) {
568 /* May be self powered. */
569 if (cdp->bmAttributes & UC_BUS_POWERED) {
570 /* Must ask device. */
571 if (dev->quirks->uq_flags & UQ_HUB_POWER) {
572 /* Buggy hub, use hub descriptor. */
573 r = usbd_get_hub_status(dev, &hs);
574 if (r == USBD_NORMAL_COMPLETION &&
575 !(UGETW(hs.wHubStatus) & UHS_LOCAL_POWER))
576 selfpowered = 1;
577 } else {
578 r = usbd_get_device_status(dev, &ds);
579 if (r == USBD_NORMAL_COMPLETION &&
580 (UGETW(ds.wStatus) & UDS_SELF_POWERED))
581 selfpowered = 1;
582 }
583 DPRINTF(("usbd_set_config_index: status=0x%04x, "
584 "error=%d(%s)\n",
585 UGETW(ds.wStatus), r, usbd_error_strs[r]));
586 } else
587 selfpowered = 1;
588 }
589 DPRINTF(("usbd_set_config_index: (addr %d) attr=0x%02x, "
590 "selfpowered=%d, power=%d, powerquirk=%x\n",
591 dev->address, cdp->bmAttributes,
592 selfpowered, cdp->bMaxPower * 2,
593 dev->quirks->uq_flags & UQ_HUB_POWER));
594 #ifdef USB_DEBUG
595 if (!dev->powersrc) {
596 printf("usbd_set_config_index: No power source?\n");
597 return (USBD_IOERROR);
598 }
599 #endif
600 power = cdp->bMaxPower * 2;
601 if (power > dev->powersrc->power) {
602 /* XXX print nicer message. */
603 if (msg)
604 printf("%s: device addr %d (config %d) exceeds power "
605 "budget, %d mA > %d mA\n",
606 USBDEVNAME(dev->bus->bdev), dev->address,
607 cdp->bConfigurationValue,
608 power, dev->powersrc->power);
609 r = USBD_NO_POWER;
610 goto bad;
611 }
612 dev->power = power;
613 dev->self_powered = selfpowered;
614
615 DPRINTF(("usbd_set_config_index: set config %d\n",
616 cdp->bConfigurationValue));
617 r = usbd_set_config(dev, cdp->bConfigurationValue);
618 if (r != USBD_NORMAL_COMPLETION) {
619 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
620 "error=%d(%s)\n",
621 cdp->bConfigurationValue, r, usbd_error_strs[r]));
622 goto bad;
623 }
624 DPRINTF(("usbd_set_config_index: setting new config %d\n",
625 cdp->bConfigurationValue));
626 nifc = cdp->bNumInterface;
627 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
628 M_USB, M_NOWAIT);
629 if (dev->ifaces == 0) {
630 r = USBD_NOMEM;
631 goto bad;
632 }
633 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
634 dev->cdesc = cdp;
635 dev->config = cdp->bConfigurationValue;
636 dev->state = USBD_DEVICE_CONFIGURED;
637 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
638 r = usbd_fill_iface_data(dev, ifcidx, 0);
639 if (r != USBD_NORMAL_COMPLETION) {
640 while (--ifcidx >= 0)
641 usbd_free_iface_data(dev, ifcidx);
642 goto bad;
643 }
644 }
645
646 return (USBD_NORMAL_COMPLETION);
647
648 bad:
649 free(cdp, M_USB);
650 return (r);
651 }
652
653 /* XXX add function for alternate settings */
654
655 usbd_status
656 usbd_setup_pipe(dev, iface, ep, pipe)
657 usbd_device_handle dev;
658 usbd_interface_handle iface;
659 struct usbd_endpoint *ep;
660 usbd_pipe_handle *pipe;
661 {
662 usbd_pipe_handle p;
663 usbd_status r;
664
665 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
666 dev, iface, ep, pipe));
667 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
668 if (p == 0)
669 return (USBD_NOMEM);
670 p->device = dev;
671 p->iface = iface;
672 p->state = USBD_PIPE_ACTIVE;
673 p->endpoint = ep;
674 ep->refcnt++;
675 p->refcnt = 1;
676 p->intrreqh = 0;
677 p->running = 0;
678 SIMPLEQ_INIT(&p->queue);
679 r = dev->bus->open_pipe(p);
680 if (r != USBD_NORMAL_COMPLETION) {
681 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error=%d"
682 "(%s)\n",
683 ep->edesc->bEndpointAddress, r, usbd_error_strs[r]));
684 free(p, M_USB);
685 return (r);
686 }
687 *pipe = p;
688 return (USBD_NORMAL_COMPLETION);
689 }
690
691 /* Abort the device control pipe. */
692 void
693 usbd_kill_pipe(pipe)
694 usbd_pipe_handle pipe;
695 {
696 pipe->methods->close(pipe);
697 pipe->endpoint->refcnt--;
698 free(pipe, M_USB);
699 }
700
701 int
702 usbd_getnewaddr(bus)
703 usbd_bus_handle bus;
704 {
705 int addr;
706
707 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
708 if (bus->devices[addr] == 0)
709 return (addr);
710 return (-1);
711 }
712
713
714 usbd_status
715 usbd_probe_and_attach(parent, dev, port, addr)
716 bdevice *parent;
717 usbd_device_handle dev;
718 int port;
719 int addr;
720 {
721 struct usb_attach_arg uaa;
722 usb_device_descriptor_t *dd = &dev->ddesc;
723 int r, found, i, confi, nifaces;
724 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
725
726 #if defined(__FreeBSD__)
727 /* XXX isn't it bad to build &uaa into a value that has a lifetime
728 longer than this function call? */
729 bdevice bdev;
730 bdev = device_add_child(*parent, NULL, -1, &uaa);
731 if (bdev) {
732 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
733 return (USBD_INVAL);
734 }
735 #endif
736
737 uaa.device = dev;
738 uaa.iface = 0;
739 uaa.ifaces = 0;
740 uaa.nifaces = 0;
741 uaa.usegeneric = 0;
742 uaa.port = port;
743 uaa.configno = UHUB_UNK_CONFIGURATION;
744 uaa.ifaceno = UHUB_UNK_INTERFACE;
745
746 /* First try with device specific drivers. */
747 if (USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch))
748 return (USBD_NORMAL_COMPLETION);
749
750 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
751
752 /* Next try with interface drivers. */
753 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
754 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
755 confi));
756 r = usbd_set_config_index(dev, confi, 1);
757 if (r != USBD_NORMAL_COMPLETION) {
758 #ifdef USB_DEBUG
759 DPRINTF(("%s: port %d, set config at addr %d failed, "
760 "error=%d(%s)\n", USBDEVNAME(*parent), port,
761 addr, r, usbd_error_strs[r]));
762 #else
763 printf("%s: port %d, set config at addr %d failed\n",
764 USBDEVNAME(*parent), port, addr);
765 #endif
766 return (r);
767 }
768 nifaces = dev->cdesc->bNumInterface;
769 uaa.configno = dev->cdesc->bConfigurationValue;
770 for (i = 0; i < nifaces; i++)
771 ifaces[i] = &dev->ifaces[i];
772 uaa.ifaces = ifaces;
773 uaa.nifaces = nifaces;
774 for (found = i = 0; i < nifaces; i++) {
775 if (!ifaces[i])
776 continue; /* interface already claimed */
777 uaa.iface = ifaces[i];
778 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
779 if (USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
780 usbd_submatch)) {
781 found++;
782 ifaces[i] = 0; /* consumed */
783 }
784 }
785 if (found != 0)
786 return (USBD_NORMAL_COMPLETION);
787 }
788 /* No interfaces were attached in any of the configurations. */
789 if (dd->bNumConfigurations > 1)/* don't change if only 1 config */
790 usbd_set_config_index(dev, 0, 0);
791
792 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
793
794 /* Finally try the generic driver. */
795 uaa.iface = 0;
796 uaa.usegeneric = 1;
797 uaa.configno = UHUB_UNK_CONFIGURATION;
798 uaa.ifaceno = UHUB_UNK_INTERFACE;
799 if (USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch))
800 return (USBD_NORMAL_COMPLETION);
801
802 /*
803 * The generic attach failed, but leave the device as it is.
804 * We just did not find any drivers, that's all. The device is
805 * fully operational and not harming anyone.
806 */
807 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
808 return (USBD_NORMAL_COMPLETION);
809 }
810
811
812
813 /*
814 * Called when a new device has been put in the powered state,
815 * but not yet in the addressed state.
816 * Get initial descriptor, set the address, get full descriptor,
817 * and attach a driver.
818 */
819 usbd_status
820 usbd_new_device(parent, bus, depth, lowspeed, port, up)
821 bdevice *parent;
822 usbd_bus_handle bus;
823 int depth;
824 int lowspeed;
825 int port;
826 struct usbd_port *up;
827 {
828 usbd_device_handle dev;
829 usb_device_descriptor_t *dd;
830 usbd_status r;
831 int addr;
832 int i;
833
834 DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
835 bus, depth, lowspeed));
836 addr = usbd_getnewaddr(bus);
837 if (addr < 0) {
838 printf("%s: No free USB addresses, new device ignored.\n",
839 USBDEVNAME(bus->bdev));
840 return (USBD_NO_ADDR);
841 }
842
843 dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
844 if (dev == 0)
845 return (USBD_NOMEM);
846 memset(dev, 0, sizeof(*dev));
847
848 dev->bus = bus;
849
850 /* Set up default endpoint handle. */
851 dev->def_ep.edesc = &dev->def_ep_desc;
852 dev->def_ep.state = USBD_ENDPOINT_ACTIVE;
853
854 /* Set up default endpoint descriptor. */
855 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
856 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
857 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
858 dev->def_ep_desc.bmAttributes = UE_CONTROL;
859 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
860 dev->def_ep_desc.bInterval = 0;
861
862 dev->state = USBD_DEVICE_DEFAULT;
863 dev->quirks = &usbd_no_quirk;
864 dev->address = USB_START_ADDR;
865 dev->ddesc.bMaxPacketSize = 0;
866 dev->lowspeed = lowspeed != 0;
867 dev->depth = depth;
868 dev->powersrc = up;
869 dev->langid = USBD_NOLANG;
870
871 /* Establish the the default pipe. */
872 r = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
873 if (r != USBD_NORMAL_COMPLETION) {
874 usbd_remove_device(dev, up);
875 return (r);
876 }
877
878 up->device = dev;
879 dd = &dev->ddesc;
880 /* Try a few times in case the device is slow (i.e. outside specs.) */
881 for (i = 0; i < 5; i++) {
882 /* Get the first 8 bytes of the device descriptor. */
883 r = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
884 if (r == USBD_NORMAL_COMPLETION)
885 break;
886 usbd_delay_ms(dev, 200);
887 }
888 if (r != USBD_NORMAL_COMPLETION) {
889 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
890 "failed\n",
891 addr));
892 usbd_remove_device(dev, up);
893 return (r);
894 }
895
896 if (dd->bDescriptorType != UDESC_DEVICE) {
897 /* Illegal device descriptor */
898 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
899 dd->bDescriptorType));
900 usbd_remove_device(dev, up);
901 return (USBD_INVAL);
902 }
903
904 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
905 "subclass=%d, protocol=%d, maxpacket=%d, ls=%d\n",
906 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
907 dd->bDeviceProtocol, dd->bMaxPacketSize, dev->lowspeed));
908
909 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
910
911 /* Get the full device descriptor. */
912 r = usbd_get_device_desc(dev, dd);
913 if (r != USBD_NORMAL_COMPLETION) {
914 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
915 "failed\n", addr));
916 usbd_remove_device(dev, up);
917 return (r);
918 }
919
920 /* Figure out what's wrong with this device. */
921 dev->quirks = usbd_find_quirk(dd);
922
923 /* Set the address */
924 r = usbd_set_address(dev, addr);
925 if (r != USBD_NORMAL_COMPLETION) {
926 DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
927 r = USBD_SET_ADDR_FAILED;
928 usbd_remove_device(dev, up);
929 return (r);
930 }
931 /* Allow device time to set new address */
932 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
933
934 dev->address = addr; /* New device address now */
935 dev->state = USBD_DEVICE_ADDRESSED;
936 bus->devices[addr] = dev;
937
938 /* Assume 100mA bus powered for now. Changed when configured. */
939 dev->power = USB_MIN_POWER;
940 dev->self_powered = 0;
941
942 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
943 addr, dev, parent));
944
945 r = usbd_probe_and_attach(parent, dev, port, addr);
946 if (r != USBD_NORMAL_COMPLETION) {
947 usbd_remove_device(dev, up);
948 return (r);
949 }
950
951 return (USBD_NORMAL_COMPLETION);
952 }
953
954 void
955 usbd_remove_device(dev, up)
956 usbd_device_handle dev;
957 struct usbd_port *up;
958 {
959 DPRINTF(("usbd_remove_device: %p\n", dev));
960
961 #if defined(__NetBSD__)
962 /* XXX bit of a hack, only for hubs the detach is called
963 *
964 * easiest solution, register a detach method in the softc, call that
965 * one and pass the device struct to it, or the softc. Whatever.
966 */
967 /*if (dev->bdev && dev->hub)
968 uhub_detach(dev->hub->hubdata);*/
969 #elif defined(__FreeBSD__)
970 if (dev->bdev)
971 device_delete_child(device_get_parent(dev->bdev), dev->bdev);
972 #endif
973
974 if (dev->default_pipe)
975 usbd_kill_pipe(dev->default_pipe);
976 up->device = 0;
977 dev->bus->devices[dev->address] = 0;
978
979 free(dev, M_USB);
980 }
981
982 #if defined(__NetBSD__)
983 int
984 usbd_print(aux, pnp)
985 void *aux;
986 const char *pnp;
987 {
988 struct usb_attach_arg *uaa = aux;
989 char devinfo[1024];
990
991 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
992 if (pnp) {
993 if (!uaa->usegeneric)
994 return (QUIET);
995 usbd_devinfo(uaa->device, 1, devinfo);
996 printf("%s, %s", devinfo, pnp);
997 }
998 if (uaa->port != 0)
999 printf(" port %d", uaa->port);
1000 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1001 printf(" configuration %d", uaa->configno);
1002 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1003 printf(" interface %d", uaa->ifaceno);
1004 return (UNCONF);
1005 }
1006
1007 int
1008 usbd_submatch(parent, cf, aux)
1009 struct device *parent;
1010 struct cfdata *cf;
1011 void *aux;
1012 {
1013 struct usb_attach_arg *uaa = aux;
1014
1015 if ((uaa->port != 0 &&
1016 cf->uhubcf_port != UHUB_UNK_PORT &&
1017 cf->uhubcf_port != uaa->port) ||
1018 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1019 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1020 cf->uhubcf_configuration != uaa->configno) ||
1021 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1022 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1023 cf->uhubcf_interface != uaa->ifaceno))
1024 return 0;
1025 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1026 }
1027
1028 #elif defined(__FreeBSD__)
1029 static void
1030 usbd_bus_print_child(device_t bus, device_t dev)
1031 {
1032 /* FIXME print the device address and the configuration used
1033 */
1034 }
1035 #endif
1036
1037 usbd_status
1038 usb_insert_transfer(reqh)
1039 usbd_request_handle reqh;
1040 {
1041 usbd_pipe_handle pipe = reqh->pipe;
1042 usbd_interface_handle iface = pipe->iface;
1043
1044 if (pipe->state == USBD_PIPE_IDLE ||
1045 (iface && iface->state == USBD_INTERFACE_IDLE))
1046 return (USBD_IS_IDLE);
1047 SIMPLEQ_INSERT_TAIL(&pipe->queue, reqh, next);
1048 if (pipe->state != USBD_PIPE_ACTIVE ||
1049 (iface && iface->state != USBD_INTERFACE_ACTIVE))
1050 return (USBD_NOT_STARTED);
1051 if (pipe->running)
1052 return (USBD_IN_PROGRESS);
1053 pipe->running = 1;
1054 return (USBD_NORMAL_COMPLETION);
1055 }
1056
1057 void
1058 usb_start_next(pipe)
1059 usbd_pipe_handle pipe;
1060 {
1061 usbd_request_handle reqh;
1062 usbd_status r;
1063
1064 #ifdef DIAGNOSTIC
1065 if (SIMPLEQ_FIRST(&pipe->queue) == 0) {
1066 printf("usb_start_next: empty\n");
1067 return;
1068 }
1069 #endif
1070
1071 /* First remove remove old */
1072 SIMPLEQ_REMOVE_HEAD(&pipe->queue, SIMPLEQ_FIRST(&pipe->queue), next);
1073 if (pipe->state != USBD_PIPE_ACTIVE) {
1074 pipe->running = 0;
1075 return;
1076 }
1077 reqh = SIMPLEQ_FIRST(&pipe->queue);
1078 DPRINTFN(5, ("usb_start_next: start reqh=%p\n", reqh));
1079 if (!reqh)
1080 pipe->running = 0;
1081 else {
1082 r = pipe->methods->start(reqh);
1083 if (r != USBD_IN_PROGRESS) {
1084 printf("usb_start_next: error=%d\n", r);
1085 pipe->running = 0;
1086 /* XXX do what? */
1087 }
1088 }
1089 }
1090
1091 void
1092 usbd_fill_deviceinfo(dev, di)
1093 usbd_device_handle dev;
1094 struct usb_device_info *di;
1095 {
1096 struct usbd_port *p;
1097 int i, r, s;
1098
1099 di->config = dev->config;
1100 usbd_devinfo_vp(dev, di->vendor, di->product);
1101 usbd_printBCD(di->revision, UGETW(dev->ddesc.bcdDevice));
1102 di->vendorNo = UGETW(dev->ddesc.idVendor);
1103 di->productNo = UGETW(dev->ddesc.idProduct);
1104 di->class = dev->ddesc.bDeviceClass;
1105 di->power = dev->self_powered ? 0 : dev->power;
1106 di->lowspeed = dev->lowspeed;
1107 di->addr = dev->address;
1108 if (dev->hub) {
1109 for (i = 0;
1110 i < sizeof(di->ports) / sizeof(di->ports[0]) &&
1111 i < dev->hub->hubdesc.bNbrPorts;
1112 i++) {
1113 p = &dev->hub->ports[i];
1114 if (p->device)
1115 r = p->device->address;
1116 else {
1117 s = UGETW(p->status.wPortStatus);
1118 if (s & UPS_PORT_ENABLED)
1119 r = USB_PORT_ENABLED;
1120 else if (s & UPS_SUSPEND)
1121 r = USB_PORT_SUSPENDED;
1122 else if (s & UPS_PORT_POWER)
1123 r = USB_PORT_POWERED;
1124 else
1125 r = USB_PORT_DISABLED;
1126 }
1127 di->ports[i] = r;
1128 }
1129 di->nports = dev->hub->hubdesc.bNbrPorts;
1130 } else
1131 di->nports = 0;
1132 }
1133