usb_subr.c revision 1.56 1 /* $NetBSD: usb_subr.c,v 1.56 1999/11/18 23:32:32 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 (0);
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 (0);
417 if (endptidx >= d->bNumEndpoints) /* quick exit */
418 return (0);
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 (0);
428 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
429 curidx++;
430 if (curidx == endptidx)
431 return (e);
432 }
433 }
434 return (0);
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(10,("usbd_fill_iface_data: found idesc n=%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 == 0)
461 return (USBD_NOMEM);
462 } else
463 ifc->endpoints = 0;
464 ifc->priv = 0;
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->bDescriptorType == UDESC_INTERFACE ||
479 ed->bLength == 0)
480 break;
481 }
482 /* passed end, or bad desc */
483 goto bad;
484 found:
485 ifc->endpoints[endpt].edesc = ed;
486 ifc->endpoints[endpt].refcnt = 0;
487 p += ed->bLength;
488 }
489 #undef ed
490 LIST_INIT(&ifc->pipes);
491 return (USBD_NORMAL_COMPLETION);
492
493 bad:
494 free(ifc->endpoints, M_USB);
495 return (USBD_INVAL);
496 }
497
498 void
499 usbd_free_iface_data(dev, ifcno)
500 usbd_device_handle dev;
501 int ifcno;
502 {
503 usbd_interface_handle ifc = &dev->ifaces[ifcno];
504 if (ifc->endpoints)
505 free(ifc->endpoints, M_USB);
506 }
507
508 static usbd_status
509 usbd_set_config(dev, conf)
510 usbd_device_handle dev;
511 int conf;
512 {
513 usb_device_request_t req;
514
515 req.bmRequestType = UT_WRITE_DEVICE;
516 req.bRequest = UR_SET_CONFIG;
517 USETW(req.wValue, conf);
518 USETW(req.wIndex, 0);
519 USETW(req.wLength, 0);
520 return (usbd_do_request(dev, &req, 0));
521 }
522
523 usbd_status
524 usbd_set_config_no(dev, no, msg)
525 usbd_device_handle dev;
526 int no;
527 int msg;
528 {
529 int index;
530 usb_config_descriptor_t cd;
531 usbd_status err;
532
533 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
534 /* Figure out what config index to use. */
535 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
536 err = usbd_get_config_desc(dev, index, &cd);
537 if (err)
538 return (err);
539 if (cd.bConfigurationValue == no)
540 return (usbd_set_config_index(dev, index, msg));
541 }
542 return (USBD_INVAL);
543 }
544
545 usbd_status
546 usbd_set_config_index(dev, index, msg)
547 usbd_device_handle dev;
548 int index;
549 int msg;
550 {
551 usb_status_t ds;
552 usb_config_descriptor_t cd, *cdp;
553 usbd_status err;
554 int ifcidx, nifc, len, selfpowered, power;
555
556 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
557
558 /* XXX check that all interfaces are idle */
559 if (dev->config != 0) {
560 DPRINTF(("usbd_set_config_index: free old config\n"));
561 /* Free all configuration data structures. */
562 nifc = dev->cdesc->bNumInterface;
563 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
564 usbd_free_iface_data(dev, ifcidx);
565 free(dev->ifaces, M_USB);
566 free(dev->cdesc, M_USB);
567 dev->ifaces = 0;
568 dev->cdesc = 0;
569 dev->config = 0;
570 }
571
572 /* Figure out what config number to use. */
573 err = usbd_get_config_desc(dev, index, &cd);
574 if (err)
575 return (err);
576 len = UGETW(cd.wTotalLength);
577 cdp = malloc(len, M_USB, M_NOWAIT);
578 if (cdp == NULL)
579 return (USBD_NOMEM);
580 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
581 if (err)
582 goto bad;
583 if (cdp->bDescriptorType != UDESC_CONFIG) {
584 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
585 cdp->bDescriptorType));
586 err = USBD_INVAL;
587 goto bad;
588 }
589 selfpowered = 0;
590 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
591 cdp->bmAttributes & UC_SELF_POWERED) {
592 /* May be self powered. */
593 if (cdp->bmAttributes & UC_BUS_POWERED) {
594 /* Must ask device. */
595 err = usbd_get_device_status(dev, &ds);
596 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
597 selfpowered = 1;
598 DPRINTF(("usbd_set_config_index: status=0x%04x, "
599 "error=%s\n",
600 UGETW(ds.wStatus), usbd_errstr(err)));
601 } else
602 selfpowered = 1;
603 }
604 DPRINTF(("usbd_set_config_index: (addr %d) attr=0x%02x, "
605 "selfpowered=%d, power=%d\n",
606 dev->address, cdp->bmAttributes,
607 selfpowered, cdp->bMaxPower * 2));
608 #ifdef USB_DEBUG
609 if (dev->powersrc == NULL) {
610 DPRINTF(("usbd_set_config_index: No power source?\n"));
611 return (USBD_IOERROR);
612 }
613 #endif
614 power = cdp->bMaxPower * 2;
615 if (power > dev->powersrc->power) {
616 /* XXX print nicer message. */
617 if (msg)
618 printf("%s: device addr %d (config %d) exceeds power "
619 "budget, %d mA > %d mA\n",
620 USBDEVNAME(dev->bus->bdev), dev->address,
621 cdp->bConfigurationValue,
622 power, dev->powersrc->power);
623 err = USBD_NO_POWER;
624 goto bad;
625 }
626 dev->power = power;
627 dev->self_powered = selfpowered;
628
629 DPRINTF(("usbd_set_config_index: set config %d\n",
630 cdp->bConfigurationValue));
631 err = usbd_set_config(dev, cdp->bConfigurationValue);
632 if (err) {
633 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
634 "error=%s\n",
635 cdp->bConfigurationValue, usbd_errstr(err)));
636 goto bad;
637 }
638 DPRINTF(("usbd_set_config_index: setting new config %d\n",
639 cdp->bConfigurationValue));
640 nifc = cdp->bNumInterface;
641 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
642 M_USB, M_NOWAIT);
643 if (dev->ifaces == 0) {
644 err = USBD_NOMEM;
645 goto bad;
646 }
647 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
648 dev->cdesc = cdp;
649 dev->config = cdp->bConfigurationValue;
650 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
651 err = usbd_fill_iface_data(dev, ifcidx, 0);
652 if (err) {
653 while (--ifcidx >= 0)
654 usbd_free_iface_data(dev, ifcidx);
655 goto bad;
656 }
657 }
658
659 return (USBD_NORMAL_COMPLETION);
660
661 bad:
662 free(cdp, M_USB);
663 return (err);
664 }
665
666 /* XXX add function for alternate settings */
667
668 usbd_status
669 usbd_setup_pipe(dev, iface, ep, pipe)
670 usbd_device_handle dev;
671 usbd_interface_handle iface;
672 struct usbd_endpoint *ep;
673 usbd_pipe_handle *pipe;
674 {
675 usbd_pipe_handle p;
676 usbd_status err;
677
678 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
679 dev, iface, ep, pipe));
680 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
681 if (p == NULL)
682 return (USBD_NOMEM);
683 p->device = dev;
684 p->iface = iface;
685 p->endpoint = ep;
686 ep->refcnt++;
687 p->refcnt = 1;
688 p->intrxfer = 0;
689 p->running = 0;
690 p->repeat = 0;
691 SIMPLEQ_INIT(&p->queue);
692 err = dev->bus->methods->open_pipe(p);
693 if (err) {
694 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
695 "%s\n",
696 ep->edesc->bEndpointAddress, usbd_errstr(err)));
697 free(p, M_USB);
698 return (err);
699 }
700 /* Clear any stall and make sure DATA0 toggle will be used next. */
701 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT)
702 usbd_clear_endpoint_stall(p);
703 *pipe = p;
704 return (USBD_NORMAL_COMPLETION);
705 }
706
707 /* Abort the device control pipe. */
708 void
709 usbd_kill_pipe(pipe)
710 usbd_pipe_handle pipe;
711 {
712 pipe->methods->close(pipe);
713 pipe->endpoint->refcnt--;
714 free(pipe, M_USB);
715 }
716
717 int
718 usbd_getnewaddr(bus)
719 usbd_bus_handle bus;
720 {
721 int addr;
722
723 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
724 if (bus->devices[addr] == 0)
725 return (addr);
726 return (-1);
727 }
728
729
730 usbd_status
731 usbd_probe_and_attach(parent, dev, port, addr)
732 device_ptr_t parent;
733 usbd_device_handle dev;
734 int port;
735 int addr;
736 {
737 struct usb_attach_arg uaa;
738 usb_device_descriptor_t *dd = &dev->ddesc;
739 int found, i, confi, nifaces;
740 usbd_status err;
741 device_ptr_t dv;
742 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
743
744 #if defined(__FreeBSD__)
745 /*
746 * XXX uaa is a static var. Not a problem as it _should_ be used only
747 * during probe and attach. Should be changed however.
748 */
749 device_t bdev;
750 bdev = device_add_child(parent, NULL, -1, &uaa);
751 if (!bdev) {
752 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
753 return (USBD_INVAL);
754 }
755 device_quiet(bdev);
756 #endif
757
758 uaa.device = dev;
759 uaa.iface = 0;
760 uaa.ifaces = 0;
761 uaa.nifaces = 0;
762 uaa.usegeneric = 0;
763 uaa.port = port;
764 uaa.configno = UHUB_UNK_CONFIGURATION;
765 uaa.ifaceno = UHUB_UNK_INTERFACE;
766 uaa.vendor = UGETW(dd->idVendor);
767 uaa.product = UGETW(dd->idProduct);
768 uaa.release = UGETW(dd->bcdDevice);
769
770 /* First try with device specific drivers. */
771 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
772 if (dv) {
773 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
774 if (dev->subdevs == 0)
775 return (USBD_NOMEM);
776 dev->subdevs[0] = dv;
777 dev->subdevs[1] = 0;
778 return (USBD_NORMAL_COMPLETION);
779 }
780
781 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
782
783 /* Next try with interface drivers. */
784 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
785 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
786 confi));
787 err = usbd_set_config_index(dev, confi, 1);
788 if (err) {
789 #ifdef USB_DEBUG
790 DPRINTF(("%s: port %d, set config at addr %d failed, "
791 "error=%s\n", USBDEVPTRNAME(parent), port,
792 addr, usbd_errstr(err)));
793 #else
794 printf("%s: port %d, set config at addr %d failed\n",
795 USBDEVPTRNAME(parent), port, addr);
796 #endif
797 #if defined(__FreeBSD__)
798 device_delete_child(parent, bdev);
799 #endif
800
801 return (err);
802 }
803 nifaces = dev->cdesc->bNumInterface;
804 uaa.configno = dev->cdesc->bConfigurationValue;
805 for (i = 0; i < nifaces; i++)
806 ifaces[i] = &dev->ifaces[i];
807 uaa.ifaces = ifaces;
808 uaa.nifaces = nifaces;
809 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
810 if (dev->subdevs == 0) {
811 #if defined(__FreeBSD__)
812 device_delete_child(parent, bdev);
813 #endif
814 return (USBD_NOMEM);
815 }
816
817 found = 0;
818 for (i = 0; i < nifaces; i++) {
819 if (ifaces[i] == NULL)
820 continue; /* interface already claimed */
821 uaa.iface = ifaces[i];
822 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
823 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
824 usbd_submatch);
825 if (dv != NULL) {
826 dev->subdevs[found++] = dv;
827 dev->subdevs[found] = 0;
828 ifaces[i] = 0; /* consumed */
829
830 #if defined(__FreeBSD__)
831 /* create another child for the next iface */
832 bdev = device_add_child(parent, NULL, -1,&uaa);
833 if (!bdev) {
834 printf("%s: Device creation failed\n",
835 USBDEVNAME(dev->bus->bdev));
836 return (USBD_NORMAL_COMPLETION);
837 }
838 device_quiet(bdev);
839 #endif
840 }
841 }
842 if (found != 0) {
843 #if defined(__FreeBSD__)
844 /* remove the last created child again; it is unused */
845 device_delete_child(parent, bdev);
846 #endif
847 return (USBD_NORMAL_COMPLETION);
848 }
849 free(dev->subdevs, M_USB);
850 dev->subdevs = 0;
851 }
852 /* No interfaces were attached in any of the configurations. */
853
854 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
855 usbd_set_config_index(dev, 0, 0);
856
857 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
858
859 /* Finally try the generic driver. */
860 uaa.iface = 0;
861 uaa.usegeneric = 1;
862 uaa.configno = UHUB_UNK_CONFIGURATION;
863 uaa.ifaceno = UHUB_UNK_INTERFACE;
864 uaa.vendor = UHUB_UNK_VENDOR;
865 uaa.product = UHUB_UNK_PRODUCT;
866 uaa.release = UHUB_UNK_RELEASE;
867 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
868 if (dv != NULL) {
869 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
870 if (dev->subdevs == 0)
871 return (USBD_NOMEM);
872 dev->subdevs[0] = dv;
873 dev->subdevs[1] = 0;
874 return (USBD_NORMAL_COMPLETION);
875 }
876
877 /*
878 * The generic attach failed, but leave the device as it is.
879 * We just did not find any drivers, that's all. The device is
880 * fully operational and not harming anyone.
881 */
882 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
883 #if defined(__FreeBSD__)
884 device_delete_child(parent, bdev);
885 #endif
886 return (USBD_NORMAL_COMPLETION);
887 }
888
889
890 /*
891 * Called when a new device has been put in the powered state,
892 * but not yet in the addressed state.
893 * Get initial descriptor, set the address, get full descriptor,
894 * and attach a driver.
895 */
896 usbd_status
897 usbd_new_device(parent, bus, depth, lowspeed, port, up)
898 device_ptr_t parent;
899 usbd_bus_handle bus;
900 int depth;
901 int lowspeed;
902 int port;
903 struct usbd_port *up;
904 {
905 usbd_device_handle dev;
906 usb_device_descriptor_t *dd;
907 usbd_status err;
908 int addr;
909 int i;
910
911 DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
912 bus, depth, lowspeed));
913 addr = usbd_getnewaddr(bus);
914 if (addr < 0) {
915 printf("%s: No free USB addresses, new device ignored.\n",
916 USBDEVNAME(bus->bdev));
917 return (USBD_NO_ADDR);
918 }
919
920 dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
921 if (dev == NULL)
922 return (USBD_NOMEM);
923 memset(dev, 0, sizeof(*dev));
924
925 dev->bus = bus;
926
927 /* Set up default endpoint handle. */
928 dev->def_ep.edesc = &dev->def_ep_desc;
929
930 /* Set up default endpoint descriptor. */
931 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
932 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
933 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
934 dev->def_ep_desc.bmAttributes = UE_CONTROL;
935 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
936 dev->def_ep_desc.bInterval = 0;
937
938 dev->quirks = &usbd_no_quirk;
939 dev->address = USB_START_ADDR;
940 dev->ddesc.bMaxPacketSize = 0;
941 dev->lowspeed = lowspeed != 0;
942 dev->depth = depth;
943 dev->powersrc = up;
944 dev->langid = USBD_NOLANG;
945 dev->cookie.cookie = ++usb_cookie_no;
946
947 /* Establish the the default pipe. */
948 err = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
949 if (err) {
950 usbd_remove_device(dev, up);
951 return (err);
952 }
953
954 up->device = dev;
955 dd = &dev->ddesc;
956 /* Try a few times in case the device is slow (i.e. outside specs.) */
957 for (i = 0; i < 3; i++) {
958 /* Get the first 8 bytes of the device descriptor. */
959 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
960 if (!err)
961 break;
962 usbd_delay_ms(dev, 200);
963 }
964 if (err) {
965 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
966 "failed\n", addr));
967 usbd_remove_device(dev, up);
968 return (err);
969 }
970
971 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
972 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, ls=%d\n",
973 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
974 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
975 dev->lowspeed));
976
977 if (dd->bDescriptorType != UDESC_DEVICE) {
978 /* Illegal device descriptor */
979 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
980 dd->bDescriptorType));
981 usbd_remove_device(dev, up);
982 return (USBD_INVAL);
983 }
984
985 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
986 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
987 usbd_remove_device(dev, up);
988 return (USBD_INVAL);
989 }
990
991 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
992
993 /* Get the full device descriptor. */
994 err = usbd_get_device_desc(dev, dd);
995 if (err) {
996 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
997 "failed\n", addr));
998 usbd_remove_device(dev, up);
999 return (err);
1000 }
1001
1002 /* Figure out what's wrong with this device. */
1003 dev->quirks = usbd_find_quirk(dd);
1004
1005 /* Set the address */
1006 err = usbd_set_address(dev, addr);
1007 if (err) {
1008 DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
1009 err = USBD_SET_ADDR_FAILED;
1010 usbd_remove_device(dev, up);
1011 return (err);
1012 }
1013 /* Allow device time to set new address */
1014 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1015
1016 dev->address = addr; /* New device address now */
1017 bus->devices[addr] = dev;
1018
1019 /* Assume 100mA bus powered for now. Changed when configured. */
1020 dev->power = USB_MIN_POWER;
1021 dev->self_powered = 0;
1022
1023 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1024 addr, dev, parent));
1025
1026 err = usbd_probe_and_attach(parent, dev, port, addr);
1027 if (err) {
1028 usbd_remove_device(dev, up);
1029 return (err);
1030 }
1031
1032 usbd_add_event(USB_EVENT_ATTACH, dev);
1033 return (USBD_NORMAL_COMPLETION);
1034 }
1035
1036 void
1037 usbd_remove_device(dev, up)
1038 usbd_device_handle dev;
1039 struct usbd_port *up;
1040 {
1041 DPRINTF(("usbd_remove_device: %p\n", dev));
1042
1043 if (dev->default_pipe != NULL)
1044 usbd_kill_pipe(dev->default_pipe);
1045 up->device = 0;
1046 dev->bus->devices[dev->address] = 0;
1047
1048 free(dev, M_USB);
1049 }
1050
1051 #if defined(__NetBSD__) || defined(__OpenBSD__)
1052 int
1053 usbd_print(aux, pnp)
1054 void *aux;
1055 const char *pnp;
1056 {
1057 struct usb_attach_arg *uaa = aux;
1058 char devinfo[1024];
1059
1060 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1061 if (pnp) {
1062 if (!uaa->usegeneric)
1063 return (QUIET);
1064 usbd_devinfo(uaa->device, 1, devinfo);
1065 printf("%s, %s", devinfo, pnp);
1066 }
1067 if (uaa->port != 0)
1068 printf(" port %d", uaa->port);
1069 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1070 printf(" configuration %d", uaa->configno);
1071 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1072 printf(" interface %d", uaa->ifaceno);
1073 #if 0
1074 /*
1075 * It gets very crowded with these locators on the attach line.
1076 * They are not really needed since they are printed in the clear
1077 * by each driver.
1078 */
1079 if (uaa->vendor != UHUB_UNK_VENDOR)
1080 printf(" vendor 0x%04x", uaa->vendor);
1081 if (uaa->product != UHUB_UNK_PRODUCT)
1082 printf(" product 0x%04x", uaa->product);
1083 if (uaa->release != UHUB_UNK_RELEASE)
1084 printf(" release 0x%04x", uaa->release);
1085 #endif
1086 return (UNCONF);
1087 }
1088
1089 #if defined(__NetBSD__)
1090 int
1091 usbd_submatch(parent, cf, aux)
1092 struct device *parent;
1093 struct cfdata *cf;
1094 void *aux;
1095 {
1096 #elif defined(__OpenBSD__)
1097 int
1098 usbd_submatch(parent, match, aux)
1099 struct device *parent;
1100 void *match;
1101 void *aux;
1102 {
1103 struct cfdata *cf = match;
1104 #endif
1105 struct usb_attach_arg *uaa = aux;
1106
1107 if ((uaa->port != 0 &&
1108 cf->uhubcf_port != UHUB_UNK_PORT &&
1109 cf->uhubcf_port != uaa->port) ||
1110 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1111 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1112 cf->uhubcf_configuration != uaa->configno) ||
1113 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1114 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1115 cf->uhubcf_interface != uaa->ifaceno) ||
1116 (uaa->vendor != UHUB_UNK_VENDOR &&
1117 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1118 cf->uhubcf_vendor != uaa->vendor) ||
1119 (uaa->product != UHUB_UNK_PRODUCT &&
1120 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1121 cf->uhubcf_product != uaa->product) ||
1122 (uaa->release != UHUB_UNK_RELEASE &&
1123 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1124 cf->uhubcf_release != uaa->release)
1125 )
1126 return 0;
1127 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1128 }
1129
1130 #endif
1131
1132 void
1133 usbd_fill_deviceinfo(dev, di)
1134 usbd_device_handle dev;
1135 struct usb_device_info *di;
1136 {
1137 struct usbd_port *p;
1138 int i, err, s;
1139
1140 di->config = dev->config;
1141 usbd_devinfo_vp(dev, di->vendor, di->product);
1142 usbd_printBCD(di->release, UGETW(dev->ddesc.bcdDevice));
1143 di->vendorNo = UGETW(dev->ddesc.idVendor);
1144 di->productNo = UGETW(dev->ddesc.idProduct);
1145 di->class = dev->ddesc.bDeviceClass;
1146 di->power = dev->self_powered ? 0 : dev->power;
1147 di->lowspeed = dev->lowspeed;
1148 di->addr = dev->address;
1149 if (dev->hub) {
1150 for (i = 0;
1151 i < sizeof(di->ports) / sizeof(di->ports[0]) &&
1152 i < dev->hub->hubdesc.bNbrPorts;
1153 i++) {
1154 p = &dev->hub->ports[i];
1155 if (p->device)
1156 err = p->device->address;
1157 else {
1158 s = UGETW(p->status.wPortStatus);
1159 if (s & UPS_PORT_ENABLED)
1160 err = USB_PORT_ENABLED;
1161 else if (s & UPS_SUSPEND)
1162 err = USB_PORT_SUSPENDED;
1163 else if (s & UPS_PORT_POWER)
1164 err = USB_PORT_POWERED;
1165 else
1166 err = USB_PORT_DISABLED;
1167 }
1168 di->ports[i] = err;
1169 }
1170 di->nports = dev->hub->hubdesc.bNbrPorts;
1171 } else
1172 di->nports = 0;
1173 }
1174
1175 void
1176 usb_free_device(dev)
1177 usbd_device_handle dev;
1178 {
1179 int ifcidx, nifc;
1180
1181 if (dev->default_pipe != NULL)
1182 usbd_kill_pipe(dev->default_pipe);
1183 if (dev->ifaces != NULL) {
1184 nifc = dev->cdesc->bNumInterface;
1185 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1186 usbd_free_iface_data(dev, ifcidx);
1187 free(dev->ifaces, M_USB);
1188 }
1189 if (dev->cdesc != NULL)
1190 free(dev->cdesc, M_USB);
1191 if (dev->subdevs != NULL)
1192 free(dev->subdevs, M_USB);
1193 free(dev, M_USB);
1194 }
1195
1196 /*
1197 * The general mechanism for detaching drivers works as follows: Each
1198 * driver is responsible for maintaining a reference count on the
1199 * number of outstanding references to its softc (e.g. from
1200 * processing hanging in a read or write). The detach method of the
1201 * driver decrements this counter and flags in the softc that the
1202 * driver is dying and then wakes any sleepers. It then sleeps on the
1203 * softc. Each place that can sleep must maintain the reference
1204 * count. When the reference count drops to -1 (0 is the normal value
1205 * of the reference count) the a wakeup on the softc is performed
1206 * signaling to the detach waiter that all references are gone.
1207 */
1208
1209 /*
1210 * Called from process context when we discover that a port has
1211 * been disconnected.
1212 */
1213 void
1214 usb_disconnect_port(up, parent)
1215 struct usbd_port *up;
1216 device_ptr_t parent;
1217 {
1218 usbd_device_handle dev = up->device;
1219 char *hubname = USBDEVPTRNAME(parent);
1220 int i;
1221
1222 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1223 up, dev, up->portno));
1224
1225 #ifdef DIAGNOSTIC
1226 if (!dev) {
1227 printf("usb_disconnect_port: no device\n");
1228 return;
1229 }
1230 #endif
1231
1232 if (!dev->cdesc) {
1233 /* Partially attached device, just drop it. */
1234 dev->bus->devices[dev->address] = 0;
1235 up->device = 0;
1236 return;
1237 }
1238
1239 if (dev->subdevs != NULL) {
1240 for (i = 0; dev->subdevs[i]; i++) {
1241 if (!dev->subdevs[i]) /* skip empty elements */
1242 continue;
1243
1244 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1245 hubname);
1246 if (up->portno != 0)
1247 printf(" port %d", up->portno);
1248 printf(" (addr %d) disconnected\n", dev->address);
1249 #if defined(__NetBSD__) || defined(__OpenBSD__)
1250 config_detach(dev->subdevs[i], DETACH_FORCE);
1251 #elif defined(__FreeBSD__)
1252 device_delete_child(device_get_parent(dev->subdevs[i]),
1253 dev->subdevs[i]);
1254 #endif
1255
1256 }
1257 }
1258
1259 usbd_add_event(USB_EVENT_DETACH, dev);
1260 dev->bus->devices[dev->address] = 0;
1261 up->device = 0;
1262 usb_free_device(dev);
1263 }
1264
1265 #ifdef __OpenBSD__
1266 void *usb_realloc(p, size, pool, flags)
1267 void *p;
1268 u_int size;
1269 int pool;
1270 int flags;
1271 {
1272 void *q;
1273
1274 q = malloc(size, pool, flags);
1275 if (q == NULL)
1276 return (NULL);
1277 bcopy(p, q, size);
1278 free(p, pool);
1279 return (q);
1280 }
1281 #endif
1282