usb_subr.c revision 1.63 1 /* $NetBSD: usb_subr.c,v 1.63 2000/01/19 00:23:58 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (augustss (at) carlstedt.se) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #if defined(__NetBSD__) || defined(__OpenBSD__)
46 #include <sys/device.h>
47 #include <sys/select.h>
48 #elif defined(__FreeBSD__)
49 #include <sys/module.h>
50 #include <sys/bus.h>
51 #endif
52 #include <sys/proc.h>
53
54 #include <machine/bus.h>
55
56 #include <dev/usb/usb.h>
57
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbdivar.h>
61 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/usb_quirks.h>
63
64 #if defined(__FreeBSD__)
65 #include <machine/clock.h>
66 #define delay(d) DELAY(d)
67 #endif
68
69 #ifdef USB_DEBUG
70 #define DPRINTF(x) if (usbdebug) logprintf x
71 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
72 extern int usbdebug;
73 #else
74 #define DPRINTF(x)
75 #define DPRINTFN(n,x)
76 #endif
77
78 static usbd_status usbd_set_config __P((usbd_device_handle, int));
79 static char *usbd_get_string __P((usbd_device_handle, int, char *));
80 static int usbd_getnewaddr __P((usbd_bus_handle bus));
81 #if defined(__NetBSD__)
82 static int usbd_print __P((void *aux, const char *pnp));
83 static int usbd_submatch __P((device_ptr_t, struct cfdata *cf, void *));
84 #elif defined(__OpenBSD__)
85 static int usbd_submatch __P((device_ptr_t, void *, void *));
86 #endif
87 static void usbd_free_iface_data __P((usbd_device_handle dev, int ifcno));
88 static void usbd_kill_pipe __P((usbd_pipe_handle));
89 static usbd_status usbd_probe_and_attach
90 __P((device_ptr_t parent, usbd_device_handle dev, int port, int addr));
91
92 static u_int32_t usb_cookie_no = 0;
93
94 #ifdef USBVERBOSE
95 typedef u_int16_t usb_vendor_id_t;
96 typedef u_int16_t usb_product_id_t;
97
98 /*
99 * Descriptions of of known vendors and devices ("products").
100 */
101 struct usb_knowndev {
102 usb_vendor_id_t vendor;
103 usb_product_id_t product;
104 int flags;
105 char *vendorname, *productname;
106 };
107 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
108
109 #include <dev/usb/usbdevs_data.h>
110 #endif /* USBVERBOSE */
111
112 static const char *usbd_error_strs[] = {
113 "NORMAL_COMPLETION",
114 "IN_PROGRESS",
115 "PENDING_REQUESTS",
116 "NOT_STARTED",
117 "INVAL",
118 "NOMEM",
119 "CANCELLED",
120 "BAD_ADDRESS",
121 "IN_USE",
122 "NO_ADDR",
123 "SET_ADDR_FAILED",
124 "NO_POWER",
125 "TOO_DEEP",
126 "IOERROR",
127 "NOT_CONFIGURED",
128 "TIMEOUT",
129 "SHORT_XFER",
130 "STALLED",
131 "INTERRUPTED",
132 "XXX",
133 };
134
135 const char *
136 usbd_errstr(err)
137 usbd_status err;
138 {
139 static char buffer[5];
140
141 if (err < USBD_ERROR_MAX) {
142 return usbd_error_strs[err];
143 } else {
144 snprintf(buffer, sizeof buffer, "%d", err);
145 return buffer;
146 }
147 }
148
149 usbd_status
150 usbd_get_string_desc(dev, sindex, langid, sdesc)
151 usbd_device_handle dev;
152 int sindex;
153 int langid;
154 usb_string_descriptor_t *sdesc;
155 {
156 usb_device_request_t req;
157 usbd_status err;
158
159 req.bmRequestType = UT_READ_DEVICE;
160 req.bRequest = UR_GET_DESCRIPTOR;
161 USETW2(req.wValue, UDESC_STRING, sindex);
162 USETW(req.wIndex, langid);
163 USETW(req.wLength, 1); /* only size byte first */
164 err = usbd_do_request(dev, &req, sdesc);
165 if (err)
166 return (err);
167 USETW(req.wLength, sdesc->bLength); /* the whole string */
168 return (usbd_do_request(dev, &req, sdesc));
169 }
170
171 char *
172 usbd_get_string(dev, si, buf)
173 usbd_device_handle dev;
174 int si;
175 char *buf;
176 {
177 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
178 usb_string_descriptor_t us;
179 char *s;
180 int i, n;
181 u_int16_t c;
182 usbd_status err;
183
184 if (si == 0)
185 return (0);
186 if (dev->quirks->uq_flags & UQ_NO_STRINGS)
187 return (0);
188 if (dev->langid == USBD_NOLANG) {
189 /* Set up default language */
190 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us);
191 if (err || us.bLength < 4) {
192 dev->langid = 0; /* Well, just pick English then */
193 } else {
194 /* Pick the first language as the default. */
195 dev->langid = UGETW(us.bString[0]);
196 }
197 }
198 err = usbd_get_string_desc(dev, si, dev->langid, &us);
199 if (err)
200 return (0);
201 s = buf;
202 n = us.bLength / 2 - 1;
203 for (i = 0; i < n; i++) {
204 c = UGETW(us.bString[i]);
205 /* Convert from Unicode, handle buggy strings. */
206 if ((c & 0xff00) == 0)
207 *s++ = c;
208 else if ((c & 0x00ff) == 0 && swap)
209 *s++ = c >> 8;
210 else
211 *s++ = '?';
212 }
213 *s++ = 0;
214 return (buf);
215 }
216
217 void
218 usbd_devinfo_vp(dev, v, p)
219 usbd_device_handle dev;
220 char *v, *p;
221 {
222 usb_device_descriptor_t *udd = &dev->ddesc;
223 char *vendor = 0, *product = 0;
224 #ifdef USBVERBOSE
225 struct usb_knowndev *kdp;
226 #endif
227
228 if (dev == NULL) {
229 v[0] = p[0] = '\0';
230 return;
231 }
232
233 vendor = usbd_get_string(dev, udd->iManufacturer, v);
234 product = usbd_get_string(dev, udd->iProduct, p);
235 #ifdef USBVERBOSE
236 if (vendor == NULL) {
237 for(kdp = usb_knowndevs;
238 kdp->vendorname != NULL;
239 kdp++) {
240 if (kdp->vendor == UGETW(udd->idVendor) &&
241 (kdp->product == UGETW(udd->idProduct) ||
242 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
243 break;
244 }
245 if (kdp->vendorname == NULL)
246 vendor = product = NULL;
247 else {
248 vendor = kdp->vendorname;
249 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
250 kdp->productname : NULL;
251 }
252 }
253 #endif
254 if (vendor != NULL)
255 strcpy(v, vendor);
256 else
257 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
258 if (product != NULL)
259 strcpy(p, product);
260 else
261 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
262 }
263
264 int
265 usbd_printBCD(cp, bcd)
266 char *cp;
267 int bcd;
268 {
269 return (sprintf(cp, "%x.%02x", bcd >> 8, bcd & 0xff));
270 }
271
272 void
273 usbd_devinfo(dev, showclass, cp)
274 usbd_device_handle dev;
275 int showclass;
276 char *cp;
277 {
278 usb_device_descriptor_t *udd = &dev->ddesc;
279 char vendor[USB_MAX_STRING_LEN];
280 char product[USB_MAX_STRING_LEN];
281 int bcdDevice, bcdUSB;
282
283 usbd_devinfo_vp(dev, vendor, product);
284 cp += sprintf(cp, "%s %s", vendor, product);
285 if (showclass)
286 cp += sprintf(cp, ", class %d/%d",
287 udd->bDeviceClass, udd->bDeviceSubClass);
288 bcdUSB = UGETW(udd->bcdUSB);
289 bcdDevice = UGETW(udd->bcdDevice);
290 cp += sprintf(cp, ", rev ");
291 cp += usbd_printBCD(cp, bcdUSB);
292 *cp++ = '/';
293 cp += usbd_printBCD(cp, bcdDevice);
294 cp += sprintf(cp, ", addr %d", dev->address);
295 *cp = 0;
296 }
297
298 /* Delay for a certain number of ms */
299 void
300 usb_delay_ms(bus, ms)
301 usbd_bus_handle bus;
302 u_int ms;
303 {
304 /* Wait at least two clock ticks so we know the time has passed. */
305 if (bus->use_polling)
306 delay((ms+1) * 1000);
307 else
308 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
309 }
310
311 /* Delay given a device handle. */
312 void
313 usbd_delay_ms(dev, ms)
314 usbd_device_handle dev;
315 u_int ms;
316 {
317 usb_delay_ms(dev->bus, ms);
318 }
319
320 usbd_status
321 usbd_reset_port(dev, port, ps)
322 usbd_device_handle dev;
323 int port;
324 usb_port_status_t *ps;
325 {
326 usb_device_request_t req;
327 usbd_status err;
328 int n;
329
330 req.bmRequestType = UT_WRITE_CLASS_OTHER;
331 req.bRequest = UR_SET_FEATURE;
332 USETW(req.wValue, UHF_PORT_RESET);
333 USETW(req.wIndex, port);
334 USETW(req.wLength, 0);
335 err = usbd_do_request(dev, &req, 0);
336 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
337 port, usbd_errstr(err)));
338 if (err)
339 return (err);
340 n = 10;
341 do {
342 /* Wait for device to recover from reset. */
343 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
344 err = usbd_get_port_status(dev, port, ps);
345 if (err) {
346 DPRINTF(("usbd_reset_port: get status failed %d\n",
347 err));
348 return (err);
349 }
350 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
351 if (n == 0) {
352 printf("usbd_reset_port: timeout\n");
353 return (USBD_IOERROR);
354 }
355 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
356 #ifdef USB_DEBUG
357 if (err)
358 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
359 err));
360 #endif
361
362 /* Wait for the device to recover from reset. */
363 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
364 return (err);
365 }
366
367 usb_interface_descriptor_t *
368 usbd_find_idesc(cd, ifaceidx, altidx)
369 usb_config_descriptor_t *cd;
370 int ifaceidx;
371 int altidx;
372 {
373 char *p = (char *)cd;
374 char *end = p + UGETW(cd->wTotalLength);
375 usb_interface_descriptor_t *d;
376 int curidx, lastidx, curaidx = 0;
377
378 for (curidx = lastidx = -1; p < end; ) {
379 d = (usb_interface_descriptor_t *)p;
380 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
381 "type=%d\n",
382 ifaceidx, curidx, altidx, curaidx,
383 d->bLength, d->bDescriptorType));
384 if (d->bLength == 0) /* bad descriptor */
385 break;
386 p += d->bLength;
387 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
388 if (d->bInterfaceNumber != lastidx) {
389 lastidx = d->bInterfaceNumber;
390 curidx++;
391 curaidx = 0;
392 } else
393 curaidx++;
394 if (ifaceidx == curidx && altidx == curaidx)
395 return (d);
396 }
397 }
398 return (NULL);
399 }
400
401 usb_endpoint_descriptor_t *
402 usbd_find_edesc(cd, ifaceidx, altidx, endptidx)
403 usb_config_descriptor_t *cd;
404 int ifaceidx;
405 int altidx;
406 int endptidx;
407 {
408 char *p = (char *)cd;
409 char *end = p + UGETW(cd->wTotalLength);
410 usb_interface_descriptor_t *d;
411 usb_endpoint_descriptor_t *e;
412 int curidx;
413
414 d = usbd_find_idesc(cd, ifaceidx, altidx);
415 if (d == NULL)
416 return (NULL);
417 if (endptidx >= d->bNumEndpoints) /* quick exit */
418 return (NULL);
419
420 curidx = -1;
421 for (p = (char *)d + d->bLength; p < end; ) {
422 e = (usb_endpoint_descriptor_t *)p;
423 if (e->bLength == 0) /* bad descriptor */
424 break;
425 p += e->bLength;
426 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
427 return (NULL);
428 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
429 curidx++;
430 if (curidx == endptidx)
431 return (e);
432 }
433 }
434 return (NULL);
435 }
436
437 usbd_status
438 usbd_fill_iface_data(dev, ifaceidx, altidx)
439 usbd_device_handle dev;
440 int ifaceidx;
441 int altidx;
442 {
443 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
444 char *p, *end;
445 int endpt, nendpt;
446
447 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
448 ifaceidx, altidx));
449 ifc->device = dev;
450 ifc->idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
451 if (ifc->idesc == 0)
452 return (USBD_INVAL);
453 ifc->index = ifaceidx;
454 ifc->altindex = altidx;
455 nendpt = ifc->idesc->bNumEndpoints;
456 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
457 if (nendpt != 0) {
458 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
459 M_USB, M_NOWAIT);
460 if (ifc->endpoints == NULL)
461 return (USBD_NOMEM);
462 } else
463 ifc->endpoints = NULL;
464 ifc->priv = NULL;
465 p = (char *)ifc->idesc + ifc->idesc->bLength;
466 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
467 #define ed ((usb_endpoint_descriptor_t *)p)
468 for (endpt = 0; endpt < nendpt; endpt++) {
469 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
470 for (; p < end; p += ed->bLength) {
471 ed = (usb_endpoint_descriptor_t *)p;
472 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
473 "len=%d type=%d\n",
474 p, end, ed->bLength, ed->bDescriptorType));
475 if (p + ed->bLength <= end && ed->bLength != 0 &&
476 ed->bDescriptorType == UDESC_ENDPOINT)
477 goto found;
478 if (ed->bLength == 0 ||
479 ed->bDescriptorType == UDESC_INTERFACE)
480 break;
481 }
482 /* passed end, or bad desc */
483 DPRINTF(("usbd_fill_iface_data: bad descriptor(s): %s\n",
484 ed->bLength == 0 ? "0 length" :
485 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
486 "out of data"));
487 goto bad;
488 found:
489 ifc->endpoints[endpt].edesc = ed;
490 ifc->endpoints[endpt].refcnt = 0;
491 p += ed->bLength;
492 }
493 #undef ed
494 LIST_INIT(&ifc->pipes);
495 return (USBD_NORMAL_COMPLETION);
496
497 bad:
498 if (ifc->endpoints != NULL)
499 free(ifc->endpoints, M_USB);
500 return (USBD_INVAL);
501 }
502
503 void
504 usbd_free_iface_data(dev, ifcno)
505 usbd_device_handle dev;
506 int ifcno;
507 {
508 usbd_interface_handle ifc = &dev->ifaces[ifcno];
509 if (ifc->endpoints)
510 free(ifc->endpoints, M_USB);
511 }
512
513 static usbd_status
514 usbd_set_config(dev, conf)
515 usbd_device_handle dev;
516 int conf;
517 {
518 usb_device_request_t req;
519
520 req.bmRequestType = UT_WRITE_DEVICE;
521 req.bRequest = UR_SET_CONFIG;
522 USETW(req.wValue, conf);
523 USETW(req.wIndex, 0);
524 USETW(req.wLength, 0);
525 return (usbd_do_request(dev, &req, 0));
526 }
527
528 usbd_status
529 usbd_set_config_no(dev, no, msg)
530 usbd_device_handle dev;
531 int no;
532 int msg;
533 {
534 int index;
535 usb_config_descriptor_t cd;
536 usbd_status err;
537
538 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
539 /* Figure out what config index to use. */
540 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
541 err = usbd_get_config_desc(dev, index, &cd);
542 if (err)
543 return (err);
544 if (cd.bConfigurationValue == no)
545 return (usbd_set_config_index(dev, index, msg));
546 }
547 return (USBD_INVAL);
548 }
549
550 usbd_status
551 usbd_set_config_index(dev, index, msg)
552 usbd_device_handle dev;
553 int index;
554 int msg;
555 {
556 usb_status_t ds;
557 usb_config_descriptor_t cd, *cdp;
558 usbd_status err;
559 int ifcidx, nifc, len, selfpowered, power;
560
561 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
562
563 /* XXX check that all interfaces are idle */
564 if (dev->config != 0) {
565 DPRINTF(("usbd_set_config_index: free old config\n"));
566 /* Free all configuration data structures. */
567 nifc = dev->cdesc->bNumInterface;
568 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
569 usbd_free_iface_data(dev, ifcidx);
570 free(dev->ifaces, M_USB);
571 free(dev->cdesc, M_USB);
572 dev->ifaces = NULL;
573 dev->cdesc = NULL;
574 dev->config = 0;
575 }
576
577 /* Figure out what config number to use. */
578 err = usbd_get_config_desc(dev, index, &cd);
579 if (err)
580 return (err);
581 len = UGETW(cd.wTotalLength);
582 cdp = malloc(len, M_USB, M_NOWAIT);
583 if (cdp == NULL)
584 return (USBD_NOMEM);
585 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
586 if (err)
587 goto bad;
588 if (cdp->bDescriptorType != UDESC_CONFIG) {
589 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
590 cdp->bDescriptorType));
591 err = USBD_INVAL;
592 goto bad;
593 }
594 selfpowered = 0;
595 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
596 cdp->bmAttributes & UC_SELF_POWERED) {
597 /* May be self powered. */
598 if (cdp->bmAttributes & UC_BUS_POWERED) {
599 /* Must ask device. */
600 err = usbd_get_device_status(dev, &ds);
601 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
602 selfpowered = 1;
603 DPRINTF(("usbd_set_config_index: status=0x%04x, "
604 "error=%s\n",
605 UGETW(ds.wStatus), usbd_errstr(err)));
606 } else
607 selfpowered = 1;
608 }
609 DPRINTF(("usbd_set_config_index: (addr %d) attr=0x%02x, "
610 "selfpowered=%d, power=%d\n",
611 dev->address, cdp->bmAttributes,
612 selfpowered, cdp->bMaxPower * 2));
613 #ifdef USB_DEBUG
614 if (dev->powersrc == NULL) {
615 DPRINTF(("usbd_set_config_index: No power source?\n"));
616 return (USBD_IOERROR);
617 }
618 #endif
619 power = cdp->bMaxPower * 2;
620 if (power > dev->powersrc->power) {
621 /* XXX print nicer message. */
622 if (msg)
623 printf("%s: device addr %d (config %d) exceeds power "
624 "budget, %d mA > %d mA\n",
625 USBDEVNAME(dev->bus->bdev), dev->address,
626 cdp->bConfigurationValue,
627 power, dev->powersrc->power);
628 err = USBD_NO_POWER;
629 goto bad;
630 }
631 dev->power = power;
632 dev->self_powered = selfpowered;
633
634 DPRINTF(("usbd_set_config_index: set config %d\n",
635 cdp->bConfigurationValue));
636 err = usbd_set_config(dev, cdp->bConfigurationValue);
637 if (err) {
638 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
639 "error=%s\n",
640 cdp->bConfigurationValue, usbd_errstr(err)));
641 goto bad;
642 }
643 DPRINTF(("usbd_set_config_index: setting new config %d\n",
644 cdp->bConfigurationValue));
645 nifc = cdp->bNumInterface;
646 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
647 M_USB, M_NOWAIT);
648 if (dev->ifaces == NULL) {
649 err = USBD_NOMEM;
650 goto bad;
651 }
652 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
653 dev->cdesc = cdp;
654 dev->config = cdp->bConfigurationValue;
655 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
656 err = usbd_fill_iface_data(dev, ifcidx, 0);
657 if (err) {
658 while (--ifcidx >= 0)
659 usbd_free_iface_data(dev, ifcidx);
660 goto bad;
661 }
662 }
663
664 return (USBD_NORMAL_COMPLETION);
665
666 bad:
667 free(cdp, M_USB);
668 return (err);
669 }
670
671 /* XXX add function for alternate settings */
672
673 usbd_status
674 usbd_setup_pipe(dev, iface, ep, ival, pipe)
675 usbd_device_handle dev;
676 usbd_interface_handle iface;
677 struct usbd_endpoint *ep;
678 int ival;
679 usbd_pipe_handle *pipe;
680 {
681 usbd_pipe_handle p;
682 usbd_status err;
683
684 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
685 dev, iface, ep, pipe));
686 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
687 if (p == NULL)
688 return (USBD_NOMEM);
689 p->device = dev;
690 p->iface = iface;
691 p->endpoint = ep;
692 ep->refcnt++;
693 p->refcnt = 1;
694 p->intrxfer = 0;
695 p->running = 0;
696 p->repeat = 0;
697 p->interval = ival;
698 SIMPLEQ_INIT(&p->queue);
699 err = dev->bus->methods->open_pipe(p);
700 if (err) {
701 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
702 "%s\n",
703 ep->edesc->bEndpointAddress, usbd_errstr(err)));
704 free(p, M_USB);
705 return (err);
706 }
707 /* Clear any stall and make sure DATA0 toggle will be used next. */
708 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT)
709 usbd_clear_endpoint_stall(p);
710 *pipe = p;
711 return (USBD_NORMAL_COMPLETION);
712 }
713
714 /* Abort the device control pipe. */
715 void
716 usbd_kill_pipe(pipe)
717 usbd_pipe_handle pipe;
718 {
719 pipe->methods->close(pipe);
720 pipe->endpoint->refcnt--;
721 free(pipe, M_USB);
722 }
723
724 int
725 usbd_getnewaddr(bus)
726 usbd_bus_handle bus;
727 {
728 int addr;
729
730 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
731 if (bus->devices[addr] == 0)
732 return (addr);
733 return (-1);
734 }
735
736
737 usbd_status
738 usbd_probe_and_attach(parent, dev, port, addr)
739 device_ptr_t parent;
740 usbd_device_handle dev;
741 int port;
742 int addr;
743 {
744 struct usb_attach_arg uaa;
745 usb_device_descriptor_t *dd = &dev->ddesc;
746 int found, i, confi, nifaces;
747 usbd_status err;
748 device_ptr_t dv;
749 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
750
751 #if defined(__FreeBSD__)
752 /*
753 * XXX uaa is a static var. Not a problem as it _should_ be used only
754 * during probe and attach. Should be changed however.
755 */
756 device_t bdev;
757 bdev = device_add_child(parent, NULL, -1, &uaa);
758 if (!bdev) {
759 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
760 return (USBD_INVAL);
761 }
762 device_quiet(bdev);
763 #endif
764
765 uaa.device = dev;
766 uaa.iface = 0;
767 uaa.ifaces = 0;
768 uaa.nifaces = 0;
769 uaa.usegeneric = 0;
770 uaa.port = port;
771 uaa.configno = UHUB_UNK_CONFIGURATION;
772 uaa.ifaceno = UHUB_UNK_INTERFACE;
773 uaa.vendor = UGETW(dd->idVendor);
774 uaa.product = UGETW(dd->idProduct);
775 uaa.release = UGETW(dd->bcdDevice);
776
777 /* First try with device specific drivers. */
778 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
779 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
780 if (dv) {
781 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
782 if (dev->subdevs == NULL)
783 return (USBD_NOMEM);
784 dev->subdevs[0] = dv;
785 dev->subdevs[1] = 0;
786 return (USBD_NORMAL_COMPLETION);
787 }
788
789 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
790
791 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
792 dd->bNumConfigurations));
793 /* Next try with interface drivers. */
794 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
795 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
796 confi));
797 err = usbd_set_config_index(dev, confi, 1);
798 if (err) {
799 #ifdef USB_DEBUG
800 DPRINTF(("%s: port %d, set config at addr %d failed, "
801 "error=%s\n", USBDEVPTRNAME(parent), port,
802 addr, usbd_errstr(err)));
803 #else
804 printf("%s: port %d, set config at addr %d failed\n",
805 USBDEVPTRNAME(parent), port, addr);
806 #endif
807 #if defined(__FreeBSD__)
808 device_delete_child(parent, bdev);
809 #endif
810
811 return (err);
812 }
813 nifaces = dev->cdesc->bNumInterface;
814 uaa.configno = dev->cdesc->bConfigurationValue;
815 for (i = 0; i < nifaces; i++)
816 ifaces[i] = &dev->ifaces[i];
817 uaa.ifaces = ifaces;
818 uaa.nifaces = nifaces;
819 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
820 if (dev->subdevs == NULL) {
821 #if defined(__FreeBSD__)
822 device_delete_child(parent, bdev);
823 #endif
824 return (USBD_NOMEM);
825 }
826
827 found = 0;
828 for (i = 0; i < nifaces; i++) {
829 if (ifaces[i] == NULL)
830 continue; /* interface already claimed */
831 uaa.iface = ifaces[i];
832 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
833 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
834 usbd_submatch);
835 if (dv != NULL) {
836 dev->subdevs[found++] = dv;
837 dev->subdevs[found] = 0;
838 ifaces[i] = 0; /* consumed */
839
840 #if defined(__FreeBSD__)
841 /* create another child for the next iface */
842 bdev = device_add_child(parent, NULL, -1,&uaa);
843 if (!bdev) {
844 printf("%s: Device creation failed\n",
845 USBDEVNAME(dev->bus->bdev));
846 return (USBD_NORMAL_COMPLETION);
847 }
848 device_quiet(bdev);
849 #endif
850 }
851 }
852 if (found != 0) {
853 #if defined(__FreeBSD__)
854 /* remove the last created child again; it is unused */
855 device_delete_child(parent, bdev);
856 #endif
857 return (USBD_NORMAL_COMPLETION);
858 }
859 free(dev->subdevs, M_USB);
860 dev->subdevs = 0;
861 }
862 /* No interfaces were attached in any of the configurations. */
863
864 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
865 usbd_set_config_index(dev, 0, 0);
866
867 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
868
869 /* Finally try the generic driver. */
870 uaa.iface = 0;
871 uaa.usegeneric = 1;
872 uaa.configno = UHUB_UNK_CONFIGURATION;
873 uaa.ifaceno = UHUB_UNK_INTERFACE;
874 uaa.vendor = UHUB_UNK_VENDOR;
875 uaa.product = UHUB_UNK_PRODUCT;
876 uaa.release = UHUB_UNK_RELEASE;
877 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
878 if (dv != NULL) {
879 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
880 if (dev->subdevs == 0)
881 return (USBD_NOMEM);
882 dev->subdevs[0] = dv;
883 dev->subdevs[1] = 0;
884 return (USBD_NORMAL_COMPLETION);
885 }
886
887 /*
888 * The generic attach failed, but leave the device as it is.
889 * We just did not find any drivers, that's all. The device is
890 * fully operational and not harming anyone.
891 */
892 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
893 #if defined(__FreeBSD__)
894 device_delete_child(parent, bdev);
895 #endif
896 return (USBD_NORMAL_COMPLETION);
897 }
898
899
900 /*
901 * Called when a new device has been put in the powered state,
902 * but not yet in the addressed state.
903 * Get initial descriptor, set the address, get full descriptor,
904 * and attach a driver.
905 */
906 usbd_status
907 usbd_new_device(parent, bus, depth, lowspeed, port, up)
908 device_ptr_t parent;
909 usbd_bus_handle bus;
910 int depth;
911 int lowspeed;
912 int port;
913 struct usbd_port *up;
914 {
915 usbd_device_handle dev;
916 usb_device_descriptor_t *dd;
917 usbd_status err;
918 int addr;
919 int i;
920
921 DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
922 bus, depth, lowspeed));
923 addr = usbd_getnewaddr(bus);
924 if (addr < 0) {
925 printf("%s: No free USB addresses, new device ignored.\n",
926 USBDEVNAME(bus->bdev));
927 return (USBD_NO_ADDR);
928 }
929
930 dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
931 if (dev == NULL)
932 return (USBD_NOMEM);
933 memset(dev, 0, sizeof(*dev));
934
935 dev->bus = bus;
936
937 /* Set up default endpoint handle. */
938 dev->def_ep.edesc = &dev->def_ep_desc;
939
940 /* Set up default endpoint descriptor. */
941 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
942 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
943 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
944 dev->def_ep_desc.bmAttributes = UE_CONTROL;
945 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
946 dev->def_ep_desc.bInterval = 0;
947
948 dev->quirks = &usbd_no_quirk;
949 dev->address = USB_START_ADDR;
950 dev->ddesc.bMaxPacketSize = 0;
951 dev->lowspeed = lowspeed != 0;
952 dev->depth = depth;
953 dev->powersrc = up;
954 dev->langid = USBD_NOLANG;
955 dev->cookie.cookie = ++usb_cookie_no;
956
957 /* Establish the the default pipe. */
958 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
959 &dev->default_pipe);
960 if (err) {
961 usbd_remove_device(dev, up);
962 return (err);
963 }
964
965 up->device = dev;
966 dd = &dev->ddesc;
967 /* Try a few times in case the device is slow (i.e. outside specs.) */
968 for (i = 0; i < 3; i++) {
969 /* Get the first 8 bytes of the device descriptor. */
970 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
971 if (!err)
972 break;
973 usbd_delay_ms(dev, 200);
974 }
975 if (err) {
976 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
977 "failed\n", addr));
978 usbd_remove_device(dev, up);
979 return (err);
980 }
981
982 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
983 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, ls=%d\n",
984 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
985 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
986 dev->lowspeed));
987
988 if (dd->bDescriptorType != UDESC_DEVICE) {
989 /* Illegal device descriptor */
990 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
991 dd->bDescriptorType));
992 usbd_remove_device(dev, up);
993 return (USBD_INVAL);
994 }
995
996 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
997 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
998 usbd_remove_device(dev, up);
999 return (USBD_INVAL);
1000 }
1001
1002 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1003
1004 err = usbd_reload_device_desc(dev);
1005 if (err) {
1006 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1007 "failed\n", addr));
1008 usbd_remove_device(dev, up);
1009 return (err);
1010 }
1011
1012 /* Set the address */
1013 err = usbd_set_address(dev, addr);
1014 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1015 if (err) {
1016 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1017 err = USBD_SET_ADDR_FAILED;
1018 usbd_remove_device(dev, up);
1019 return (err);
1020 }
1021 /* Allow device time to set new address */
1022 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1023
1024 dev->address = addr; /* New device address now */
1025 bus->devices[addr] = dev;
1026
1027 /* Assume 100mA bus powered for now. Changed when configured. */
1028 dev->power = USB_MIN_POWER;
1029 dev->self_powered = 0;
1030
1031 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1032 addr, dev, parent));
1033
1034 err = usbd_probe_and_attach(parent, dev, port, addr);
1035 if (err) {
1036 usbd_remove_device(dev, up);
1037 return (err);
1038 }
1039
1040 usbd_add_event(USB_EVENT_ATTACH, dev);
1041 return (USBD_NORMAL_COMPLETION);
1042 }
1043
1044 usbd_status
1045 usbd_reload_device_desc(dev)
1046 usbd_device_handle dev;
1047 {
1048 usbd_status err;
1049
1050 /* Get the full device descriptor. */
1051 err = usbd_get_device_desc(dev, &dev->ddesc);
1052 if (err)
1053 return (err);
1054
1055 /* Figure out what's wrong with this device. */
1056 dev->quirks = usbd_find_quirk(&dev->ddesc);
1057
1058 return (USBD_NORMAL_COMPLETION);
1059 }
1060
1061 void
1062 usbd_remove_device(dev, up)
1063 usbd_device_handle dev;
1064 struct usbd_port *up;
1065 {
1066 DPRINTF(("usbd_remove_device: %p\n", dev));
1067
1068 if (dev->default_pipe != NULL)
1069 usbd_kill_pipe(dev->default_pipe);
1070 up->device = 0;
1071 dev->bus->devices[dev->address] = 0;
1072
1073 free(dev, M_USB);
1074 }
1075
1076 #if defined(__NetBSD__) || defined(__OpenBSD__)
1077 int
1078 usbd_print(aux, pnp)
1079 void *aux;
1080 const char *pnp;
1081 {
1082 struct usb_attach_arg *uaa = aux;
1083 char devinfo[1024];
1084
1085 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1086 if (pnp) {
1087 if (!uaa->usegeneric)
1088 return (QUIET);
1089 usbd_devinfo(uaa->device, 1, devinfo);
1090 printf("%s, %s", devinfo, pnp);
1091 }
1092 if (uaa->port != 0)
1093 printf(" port %d", uaa->port);
1094 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1095 printf(" configuration %d", uaa->configno);
1096 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1097 printf(" interface %d", uaa->ifaceno);
1098 #if 0
1099 /*
1100 * It gets very crowded with these locators on the attach line.
1101 * They are not really needed since they are printed in the clear
1102 * by each driver.
1103 */
1104 if (uaa->vendor != UHUB_UNK_VENDOR)
1105 printf(" vendor 0x%04x", uaa->vendor);
1106 if (uaa->product != UHUB_UNK_PRODUCT)
1107 printf(" product 0x%04x", uaa->product);
1108 if (uaa->release != UHUB_UNK_RELEASE)
1109 printf(" release 0x%04x", uaa->release);
1110 #endif
1111 return (UNCONF);
1112 }
1113
1114 #if defined(__NetBSD__)
1115 int
1116 usbd_submatch(parent, cf, aux)
1117 struct device *parent;
1118 struct cfdata *cf;
1119 void *aux;
1120 {
1121 #elif defined(__OpenBSD__)
1122 int
1123 usbd_submatch(parent, match, aux)
1124 struct device *parent;
1125 void *match;
1126 void *aux;
1127 {
1128 struct cfdata *cf = match;
1129 #endif
1130 struct usb_attach_arg *uaa = aux;
1131
1132 if ((uaa->port != 0 &&
1133 cf->uhubcf_port != UHUB_UNK_PORT &&
1134 cf->uhubcf_port != uaa->port) ||
1135 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1136 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1137 cf->uhubcf_configuration != uaa->configno) ||
1138 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1139 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1140 cf->uhubcf_interface != uaa->ifaceno) ||
1141 (uaa->vendor != UHUB_UNK_VENDOR &&
1142 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1143 cf->uhubcf_vendor != uaa->vendor) ||
1144 (uaa->product != UHUB_UNK_PRODUCT &&
1145 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1146 cf->uhubcf_product != uaa->product) ||
1147 (uaa->release != UHUB_UNK_RELEASE &&
1148 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1149 cf->uhubcf_release != uaa->release)
1150 )
1151 return 0;
1152 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1153 }
1154
1155 #endif
1156
1157 void
1158 usbd_fill_deviceinfo(dev, di)
1159 usbd_device_handle dev;
1160 struct usb_device_info *di;
1161 {
1162 struct usbd_port *p;
1163 int i, err, s;
1164
1165 di->config = dev->config;
1166 usbd_devinfo_vp(dev, di->vendor, di->product);
1167 usbd_printBCD(di->release, UGETW(dev->ddesc.bcdDevice));
1168 di->vendorNo = UGETW(dev->ddesc.idVendor);
1169 di->productNo = UGETW(dev->ddesc.idProduct);
1170 di->class = dev->ddesc.bDeviceClass;
1171 di->power = dev->self_powered ? 0 : dev->power;
1172 di->lowspeed = dev->lowspeed;
1173 di->addr = dev->address;
1174 if (dev->hub) {
1175 for (i = 0;
1176 i < sizeof(di->ports) / sizeof(di->ports[0]) &&
1177 i < dev->hub->hubdesc.bNbrPorts;
1178 i++) {
1179 p = &dev->hub->ports[i];
1180 if (p->device)
1181 err = p->device->address;
1182 else {
1183 s = UGETW(p->status.wPortStatus);
1184 if (s & UPS_PORT_ENABLED)
1185 err = USB_PORT_ENABLED;
1186 else if (s & UPS_SUSPEND)
1187 err = USB_PORT_SUSPENDED;
1188 else if (s & UPS_PORT_POWER)
1189 err = USB_PORT_POWERED;
1190 else
1191 err = USB_PORT_DISABLED;
1192 }
1193 di->ports[i] = err;
1194 }
1195 di->nports = dev->hub->hubdesc.bNbrPorts;
1196 } else
1197 di->nports = 0;
1198 }
1199
1200 void
1201 usb_free_device(dev)
1202 usbd_device_handle dev;
1203 {
1204 int ifcidx, nifc;
1205
1206 if (dev->default_pipe != NULL)
1207 usbd_kill_pipe(dev->default_pipe);
1208 if (dev->ifaces != NULL) {
1209 nifc = dev->cdesc->bNumInterface;
1210 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1211 usbd_free_iface_data(dev, ifcidx);
1212 free(dev->ifaces, M_USB);
1213 }
1214 if (dev->cdesc != NULL)
1215 free(dev->cdesc, M_USB);
1216 if (dev->subdevs != NULL)
1217 free(dev->subdevs, M_USB);
1218 free(dev, M_USB);
1219 }
1220
1221 /*
1222 * The general mechanism for detaching drivers works as follows: Each
1223 * driver is responsible for maintaining a reference count on the
1224 * number of outstanding references to its softc (e.g. from
1225 * processing hanging in a read or write). The detach method of the
1226 * driver decrements this counter and flags in the softc that the
1227 * driver is dying and then wakes any sleepers. It then sleeps on the
1228 * softc. Each place that can sleep must maintain the reference
1229 * count. When the reference count drops to -1 (0 is the normal value
1230 * of the reference count) the a wakeup on the softc is performed
1231 * signaling to the detach waiter that all references are gone.
1232 */
1233
1234 /*
1235 * Called from process context when we discover that a port has
1236 * been disconnected.
1237 */
1238 void
1239 usb_disconnect_port(up, parent)
1240 struct usbd_port *up;
1241 device_ptr_t parent;
1242 {
1243 usbd_device_handle dev = up->device;
1244 char *hubname = USBDEVPTRNAME(parent);
1245 int i;
1246
1247 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1248 up, dev, up->portno));
1249
1250 #ifdef DIAGNOSTIC
1251 if (dev == NULL) {
1252 printf("usb_disconnect_port: no device\n");
1253 return;
1254 }
1255 #endif
1256
1257 if (dev->subdevs != NULL) {
1258 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1259 for (i = 0; dev->subdevs[i]; i++) {
1260 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1261 hubname);
1262 if (up->portno != 0)
1263 printf(" port %d", up->portno);
1264 printf(" (addr %d) disconnected\n", dev->address);
1265 #if defined(__NetBSD__) || defined(__OpenBSD__)
1266 config_detach(dev->subdevs[i], DETACH_FORCE);
1267 #elif defined(__FreeBSD__)
1268 device_delete_child(device_get_parent(dev->subdevs[i]),
1269 dev->subdevs[i]);
1270 #endif
1271
1272 }
1273 }
1274
1275 usbd_add_event(USB_EVENT_DETACH, dev);
1276 dev->bus->devices[dev->address] = NULL;
1277 up->device = NULL;
1278 usb_free_device(dev);
1279 }
1280
1281 #ifdef __OpenBSD__
1282 void *usb_realloc(p, size, pool, flags)
1283 void *p;
1284 u_int size;
1285 int pool;
1286 int flags;
1287 {
1288 void *q;
1289
1290 q = malloc(size, pool, flags);
1291 if (q == NULL)
1292 return (NULL);
1293 bcopy(p, q, size);
1294 free(p, pool);
1295 return (q);
1296 }
1297 #endif
1298