usb_subr.c revision 1.59 1 /* $NetBSD: usb_subr.c,v 1.59 1999/12/15 20:05:08 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, pipe)
675 usbd_device_handle dev;
676 usbd_interface_handle iface;
677 struct usbd_endpoint *ep;
678 usbd_pipe_handle *pipe;
679 {
680 usbd_pipe_handle p;
681 usbd_status err;
682
683 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
684 dev, iface, ep, pipe));
685 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
686 if (p == NULL)
687 return (USBD_NOMEM);
688 p->device = dev;
689 p->iface = iface;
690 p->endpoint = ep;
691 ep->refcnt++;
692 p->refcnt = 1;
693 p->intrxfer = 0;
694 p->running = 0;
695 p->repeat = 0;
696 SIMPLEQ_INIT(&p->queue);
697 err = dev->bus->methods->open_pipe(p);
698 if (err) {
699 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
700 "%s\n",
701 ep->edesc->bEndpointAddress, usbd_errstr(err)));
702 free(p, M_USB);
703 return (err);
704 }
705 /* Clear any stall and make sure DATA0 toggle will be used next. */
706 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT)
707 usbd_clear_endpoint_stall(p);
708 *pipe = p;
709 return (USBD_NORMAL_COMPLETION);
710 }
711
712 /* Abort the device control pipe. */
713 void
714 usbd_kill_pipe(pipe)
715 usbd_pipe_handle pipe;
716 {
717 pipe->methods->close(pipe);
718 pipe->endpoint->refcnt--;
719 free(pipe, M_USB);
720 }
721
722 int
723 usbd_getnewaddr(bus)
724 usbd_bus_handle bus;
725 {
726 int addr;
727
728 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
729 if (bus->devices[addr] == 0)
730 return (addr);
731 return (-1);
732 }
733
734
735 usbd_status
736 usbd_probe_and_attach(parent, dev, port, addr)
737 device_ptr_t parent;
738 usbd_device_handle dev;
739 int port;
740 int addr;
741 {
742 struct usb_attach_arg uaa;
743 usb_device_descriptor_t *dd = &dev->ddesc;
744 int found, i, confi, nifaces;
745 usbd_status err;
746 device_ptr_t dv;
747 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */
748
749 #if defined(__FreeBSD__)
750 /*
751 * XXX uaa is a static var. Not a problem as it _should_ be used only
752 * during probe and attach. Should be changed however.
753 */
754 device_t bdev;
755 bdev = device_add_child(parent, NULL, -1, &uaa);
756 if (!bdev) {
757 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
758 return (USBD_INVAL);
759 }
760 device_quiet(bdev);
761 #endif
762
763 uaa.device = dev;
764 uaa.iface = 0;
765 uaa.ifaces = 0;
766 uaa.nifaces = 0;
767 uaa.usegeneric = 0;
768 uaa.port = port;
769 uaa.configno = UHUB_UNK_CONFIGURATION;
770 uaa.ifaceno = UHUB_UNK_INTERFACE;
771 uaa.vendor = UGETW(dd->idVendor);
772 uaa.product = UGETW(dd->idProduct);
773 uaa.release = UGETW(dd->bcdDevice);
774
775 /* First try with device specific drivers. */
776 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
777 if (dv) {
778 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
779 if (dev->subdevs == NULL)
780 return (USBD_NOMEM);
781 dev->subdevs[0] = dv;
782 dev->subdevs[1] = 0;
783 return (USBD_NORMAL_COMPLETION);
784 }
785
786 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
787
788 /* Next try with interface drivers. */
789 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
790 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
791 confi));
792 err = usbd_set_config_index(dev, confi, 1);
793 if (err) {
794 #ifdef USB_DEBUG
795 DPRINTF(("%s: port %d, set config at addr %d failed, "
796 "error=%s\n", USBDEVPTRNAME(parent), port,
797 addr, usbd_errstr(err)));
798 #else
799 printf("%s: port %d, set config at addr %d failed\n",
800 USBDEVPTRNAME(parent), port, addr);
801 #endif
802 #if defined(__FreeBSD__)
803 device_delete_child(parent, bdev);
804 #endif
805
806 return (err);
807 }
808 nifaces = dev->cdesc->bNumInterface;
809 uaa.configno = dev->cdesc->bConfigurationValue;
810 for (i = 0; i < nifaces; i++)
811 ifaces[i] = &dev->ifaces[i];
812 uaa.ifaces = ifaces;
813 uaa.nifaces = nifaces;
814 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
815 if (dev->subdevs == NULL) {
816 #if defined(__FreeBSD__)
817 device_delete_child(parent, bdev);
818 #endif
819 return (USBD_NOMEM);
820 }
821
822 found = 0;
823 for (i = 0; i < nifaces; i++) {
824 if (ifaces[i] == NULL)
825 continue; /* interface already claimed */
826 uaa.iface = ifaces[i];
827 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
828 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print,
829 usbd_submatch);
830 if (dv != NULL) {
831 dev->subdevs[found++] = dv;
832 dev->subdevs[found] = 0;
833 ifaces[i] = 0; /* consumed */
834
835 #if defined(__FreeBSD__)
836 /* create another child for the next iface */
837 bdev = device_add_child(parent, NULL, -1,&uaa);
838 if (!bdev) {
839 printf("%s: Device creation failed\n",
840 USBDEVNAME(dev->bus->bdev));
841 return (USBD_NORMAL_COMPLETION);
842 }
843 device_quiet(bdev);
844 #endif
845 }
846 }
847 if (found != 0) {
848 #if defined(__FreeBSD__)
849 /* remove the last created child again; it is unused */
850 device_delete_child(parent, bdev);
851 #endif
852 return (USBD_NORMAL_COMPLETION);
853 }
854 free(dev->subdevs, M_USB);
855 dev->subdevs = 0;
856 }
857 /* No interfaces were attached in any of the configurations. */
858
859 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
860 usbd_set_config_index(dev, 0, 0);
861
862 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
863
864 /* Finally try the generic driver. */
865 uaa.iface = 0;
866 uaa.usegeneric = 1;
867 uaa.configno = UHUB_UNK_CONFIGURATION;
868 uaa.ifaceno = UHUB_UNK_INTERFACE;
869 uaa.vendor = UHUB_UNK_VENDOR;
870 uaa.product = UHUB_UNK_PRODUCT;
871 uaa.release = UHUB_UNK_RELEASE;
872 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
873 if (dv != NULL) {
874 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
875 if (dev->subdevs == 0)
876 return (USBD_NOMEM);
877 dev->subdevs[0] = dv;
878 dev->subdevs[1] = 0;
879 return (USBD_NORMAL_COMPLETION);
880 }
881
882 /*
883 * The generic attach failed, but leave the device as it is.
884 * We just did not find any drivers, that's all. The device is
885 * fully operational and not harming anyone.
886 */
887 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
888 #if defined(__FreeBSD__)
889 device_delete_child(parent, bdev);
890 #endif
891 return (USBD_NORMAL_COMPLETION);
892 }
893
894
895 /*
896 * Called when a new device has been put in the powered state,
897 * but not yet in the addressed state.
898 * Get initial descriptor, set the address, get full descriptor,
899 * and attach a driver.
900 */
901 usbd_status
902 usbd_new_device(parent, bus, depth, lowspeed, port, up)
903 device_ptr_t parent;
904 usbd_bus_handle bus;
905 int depth;
906 int lowspeed;
907 int port;
908 struct usbd_port *up;
909 {
910 usbd_device_handle dev;
911 usb_device_descriptor_t *dd;
912 usbd_status err;
913 int addr;
914 int i;
915
916 DPRINTF(("usbd_new_device bus=%p depth=%d lowspeed=%d\n",
917 bus, depth, lowspeed));
918 addr = usbd_getnewaddr(bus);
919 if (addr < 0) {
920 printf("%s: No free USB addresses, new device ignored.\n",
921 USBDEVNAME(bus->bdev));
922 return (USBD_NO_ADDR);
923 }
924
925 dev = malloc(sizeof *dev, M_USB, M_NOWAIT);
926 if (dev == NULL)
927 return (USBD_NOMEM);
928 memset(dev, 0, sizeof(*dev));
929
930 dev->bus = bus;
931
932 /* Set up default endpoint handle. */
933 dev->def_ep.edesc = &dev->def_ep_desc;
934
935 /* Set up default endpoint descriptor. */
936 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
937 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
938 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
939 dev->def_ep_desc.bmAttributes = UE_CONTROL;
940 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
941 dev->def_ep_desc.bInterval = 0;
942
943 dev->quirks = &usbd_no_quirk;
944 dev->address = USB_START_ADDR;
945 dev->ddesc.bMaxPacketSize = 0;
946 dev->lowspeed = lowspeed != 0;
947 dev->depth = depth;
948 dev->powersrc = up;
949 dev->langid = USBD_NOLANG;
950 dev->cookie.cookie = ++usb_cookie_no;
951
952 /* Establish the the default pipe. */
953 err = usbd_setup_pipe(dev, 0, &dev->def_ep, &dev->default_pipe);
954 if (err) {
955 usbd_remove_device(dev, up);
956 return (err);
957 }
958
959 up->device = dev;
960 dd = &dev->ddesc;
961 /* Try a few times in case the device is slow (i.e. outside specs.) */
962 for (i = 0; i < 3; i++) {
963 /* Get the first 8 bytes of the device descriptor. */
964 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
965 if (!err)
966 break;
967 usbd_delay_ms(dev, 200);
968 }
969 if (err) {
970 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
971 "failed\n", addr));
972 usbd_remove_device(dev, up);
973 return (err);
974 }
975
976 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
977 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, ls=%d\n",
978 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
979 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
980 dev->lowspeed));
981
982 if (dd->bDescriptorType != UDESC_DEVICE) {
983 /* Illegal device descriptor */
984 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
985 dd->bDescriptorType));
986 usbd_remove_device(dev, up);
987 return (USBD_INVAL);
988 }
989
990 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
991 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
992 usbd_remove_device(dev, up);
993 return (USBD_INVAL);
994 }
995
996 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
997
998 /* Get the full device descriptor. */
999 err = usbd_get_device_desc(dev, dd);
1000 if (err) {
1001 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1002 "failed\n", addr));
1003 usbd_remove_device(dev, up);
1004 return (err);
1005 }
1006
1007 /* Figure out what's wrong with this device. */
1008 dev->quirks = usbd_find_quirk(dd);
1009
1010 /* Set the address */
1011 err = usbd_set_address(dev, addr);
1012 if (err) {
1013 DPRINTFN(-1,("usb_new_device: set address %d failed\n",addr));
1014 err = USBD_SET_ADDR_FAILED;
1015 usbd_remove_device(dev, up);
1016 return (err);
1017 }
1018 /* Allow device time to set new address */
1019 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1020
1021 dev->address = addr; /* New device address now */
1022 bus->devices[addr] = dev;
1023
1024 /* Assume 100mA bus powered for now. Changed when configured. */
1025 dev->power = USB_MIN_POWER;
1026 dev->self_powered = 0;
1027
1028 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1029 addr, dev, parent));
1030
1031 err = usbd_probe_and_attach(parent, dev, port, addr);
1032 if (err) {
1033 usbd_remove_device(dev, up);
1034 return (err);
1035 }
1036
1037 usbd_add_event(USB_EVENT_ATTACH, dev);
1038 return (USBD_NORMAL_COMPLETION);
1039 }
1040
1041 void
1042 usbd_remove_device(dev, up)
1043 usbd_device_handle dev;
1044 struct usbd_port *up;
1045 {
1046 DPRINTF(("usbd_remove_device: %p\n", dev));
1047
1048 if (dev->default_pipe != NULL)
1049 usbd_kill_pipe(dev->default_pipe);
1050 up->device = 0;
1051 dev->bus->devices[dev->address] = 0;
1052
1053 free(dev, M_USB);
1054 }
1055
1056 #if defined(__NetBSD__) || defined(__OpenBSD__)
1057 int
1058 usbd_print(aux, pnp)
1059 void *aux;
1060 const char *pnp;
1061 {
1062 struct usb_attach_arg *uaa = aux;
1063 char devinfo[1024];
1064
1065 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1066 if (pnp) {
1067 if (!uaa->usegeneric)
1068 return (QUIET);
1069 usbd_devinfo(uaa->device, 1, devinfo);
1070 printf("%s, %s", devinfo, pnp);
1071 }
1072 if (uaa->port != 0)
1073 printf(" port %d", uaa->port);
1074 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1075 printf(" configuration %d", uaa->configno);
1076 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1077 printf(" interface %d", uaa->ifaceno);
1078 #if 0
1079 /*
1080 * It gets very crowded with these locators on the attach line.
1081 * They are not really needed since they are printed in the clear
1082 * by each driver.
1083 */
1084 if (uaa->vendor != UHUB_UNK_VENDOR)
1085 printf(" vendor 0x%04x", uaa->vendor);
1086 if (uaa->product != UHUB_UNK_PRODUCT)
1087 printf(" product 0x%04x", uaa->product);
1088 if (uaa->release != UHUB_UNK_RELEASE)
1089 printf(" release 0x%04x", uaa->release);
1090 #endif
1091 return (UNCONF);
1092 }
1093
1094 #if defined(__NetBSD__)
1095 int
1096 usbd_submatch(parent, cf, aux)
1097 struct device *parent;
1098 struct cfdata *cf;
1099 void *aux;
1100 {
1101 #elif defined(__OpenBSD__)
1102 int
1103 usbd_submatch(parent, match, aux)
1104 struct device *parent;
1105 void *match;
1106 void *aux;
1107 {
1108 struct cfdata *cf = match;
1109 #endif
1110 struct usb_attach_arg *uaa = aux;
1111
1112 if ((uaa->port != 0 &&
1113 cf->uhubcf_port != UHUB_UNK_PORT &&
1114 cf->uhubcf_port != uaa->port) ||
1115 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1116 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1117 cf->uhubcf_configuration != uaa->configno) ||
1118 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1119 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1120 cf->uhubcf_interface != uaa->ifaceno) ||
1121 (uaa->vendor != UHUB_UNK_VENDOR &&
1122 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1123 cf->uhubcf_vendor != uaa->vendor) ||
1124 (uaa->product != UHUB_UNK_PRODUCT &&
1125 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1126 cf->uhubcf_product != uaa->product) ||
1127 (uaa->release != UHUB_UNK_RELEASE &&
1128 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1129 cf->uhubcf_release != uaa->release)
1130 )
1131 return 0;
1132 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
1133 }
1134
1135 #endif
1136
1137 void
1138 usbd_fill_deviceinfo(dev, di)
1139 usbd_device_handle dev;
1140 struct usb_device_info *di;
1141 {
1142 struct usbd_port *p;
1143 int i, err, s;
1144
1145 di->config = dev->config;
1146 usbd_devinfo_vp(dev, di->vendor, di->product);
1147 usbd_printBCD(di->release, UGETW(dev->ddesc.bcdDevice));
1148 di->vendorNo = UGETW(dev->ddesc.idVendor);
1149 di->productNo = UGETW(dev->ddesc.idProduct);
1150 di->class = dev->ddesc.bDeviceClass;
1151 di->power = dev->self_powered ? 0 : dev->power;
1152 di->lowspeed = dev->lowspeed;
1153 di->addr = dev->address;
1154 if (dev->hub) {
1155 for (i = 0;
1156 i < sizeof(di->ports) / sizeof(di->ports[0]) &&
1157 i < dev->hub->hubdesc.bNbrPorts;
1158 i++) {
1159 p = &dev->hub->ports[i];
1160 if (p->device)
1161 err = p->device->address;
1162 else {
1163 s = UGETW(p->status.wPortStatus);
1164 if (s & UPS_PORT_ENABLED)
1165 err = USB_PORT_ENABLED;
1166 else if (s & UPS_SUSPEND)
1167 err = USB_PORT_SUSPENDED;
1168 else if (s & UPS_PORT_POWER)
1169 err = USB_PORT_POWERED;
1170 else
1171 err = USB_PORT_DISABLED;
1172 }
1173 di->ports[i] = err;
1174 }
1175 di->nports = dev->hub->hubdesc.bNbrPorts;
1176 } else
1177 di->nports = 0;
1178 }
1179
1180 void
1181 usb_free_device(dev)
1182 usbd_device_handle dev;
1183 {
1184 int ifcidx, nifc;
1185
1186 if (dev->default_pipe != NULL)
1187 usbd_kill_pipe(dev->default_pipe);
1188 if (dev->ifaces != NULL) {
1189 nifc = dev->cdesc->bNumInterface;
1190 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1191 usbd_free_iface_data(dev, ifcidx);
1192 free(dev->ifaces, M_USB);
1193 }
1194 if (dev->cdesc != NULL)
1195 free(dev->cdesc, M_USB);
1196 if (dev->subdevs != NULL)
1197 free(dev->subdevs, M_USB);
1198 free(dev, M_USB);
1199 }
1200
1201 /*
1202 * The general mechanism for detaching drivers works as follows: Each
1203 * driver is responsible for maintaining a reference count on the
1204 * number of outstanding references to its softc (e.g. from
1205 * processing hanging in a read or write). The detach method of the
1206 * driver decrements this counter and flags in the softc that the
1207 * driver is dying and then wakes any sleepers. It then sleeps on the
1208 * softc. Each place that can sleep must maintain the reference
1209 * count. When the reference count drops to -1 (0 is the normal value
1210 * of the reference count) the a wakeup on the softc is performed
1211 * signaling to the detach waiter that all references are gone.
1212 */
1213
1214 /*
1215 * Called from process context when we discover that a port has
1216 * been disconnected.
1217 */
1218 void
1219 usb_disconnect_port(up, parent)
1220 struct usbd_port *up;
1221 device_ptr_t parent;
1222 {
1223 usbd_device_handle dev = up->device;
1224 char *hubname = USBDEVPTRNAME(parent);
1225 int i;
1226
1227 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1228 up, dev, up->portno));
1229
1230 #ifdef DIAGNOSTIC
1231 if (dev == NULL) {
1232 printf("usb_disconnect_port: no device\n");
1233 return;
1234 }
1235 #endif
1236
1237 if (dev->subdevs != NULL) {
1238 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1239 for (i = 0; dev->subdevs[i]; i++) {
1240 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1241 hubname);
1242 if (up->portno != 0)
1243 printf(" port %d", up->portno);
1244 printf(" (addr %d) disconnected\n", dev->address);
1245 #if defined(__NetBSD__) || defined(__OpenBSD__)
1246 config_detach(dev->subdevs[i], DETACH_FORCE);
1247 #elif defined(__FreeBSD__)
1248 device_delete_child(device_get_parent(dev->subdevs[i]),
1249 dev->subdevs[i]);
1250 #endif
1251
1252 }
1253 }
1254
1255 usbd_add_event(USB_EVENT_DETACH, dev);
1256 dev->bus->devices[dev->address] = NULL;
1257 up->device = NULL;
1258 usb_free_device(dev);
1259 }
1260
1261 #ifdef __OpenBSD__
1262 void *usb_realloc(p, size, pool, flags)
1263 void *p;
1264 u_int size;
1265 int pool;
1266 int flags;
1267 {
1268 void *q;
1269
1270 q = malloc(size, pool, flags);
1271 if (q == NULL)
1272 return (NULL);
1273 bcopy(p, q, size);
1274 free(p, pool);
1275 return (q);
1276 }
1277 #endif
1278