usb_subr.c revision 1.145 1 /* $NetBSD: usb_subr.c,v 1.145 2007/03/15 15:11:19 drochner 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, 2004 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 (lennart (at) augustsson.net) 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/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.145 2007/03/15 15:11:19 drochner Exp $");
43
44 #include "opt_compat_netbsd.h"
45 #include "opt_usbverbose.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/device.h>
53 #include <sys/select.h>
54 #elif defined(__FreeBSD__)
55 #include <sys/module.h>
56 #include <sys/bus.h>
57 #endif
58 #include <sys/proc.h>
59
60 #include <machine/bus.h>
61
62 #include <dev/usb/usb.h>
63
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbdivar.h>
67 #include <dev/usb/usbdevs.h>
68 #include <dev/usb/usb_quirks.h>
69
70 #if defined(__FreeBSD__)
71 #include <machine/clock.h>
72 #define delay(d) DELAY(d)
73 #endif
74
75 #ifdef USB_DEBUG
76 #define DPRINTF(x) if (usbdebug) logprintf x
77 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
78 extern int usbdebug;
79 #else
80 #define DPRINTF(x)
81 #define DPRINTFN(n,x)
82 #endif
83
84 Static usbd_status usbd_set_config(usbd_device_handle, int);
85 Static void usbd_devinfo(usbd_device_handle, int, char *, size_t);
86 Static void usbd_devinfo_vp(usbd_device_handle dev,
87 char *v,
88 char *p, int usedev,
89 int useencoded );
90 Static int usbd_getnewaddr(usbd_bus_handle bus);
91 #if defined(__NetBSD__)
92 Static int usbd_print(void *, const char *);
93 Static int usbd_ifprint(void *, const char *);
94 Static int usbd_submatch(device_ptr_t, struct cfdata *,
95 const int *, void *);
96 Static int usbd_ifsubmatch(device_ptr_t, struct cfdata *,
97 const int *, void *);
98 #elif defined(__OpenBSD__)
99 Static int usbd_print(void *aux, const char *pnp);
100 Static int usbd_submatch(device_ptr_t, void *, void *);
101 #endif
102 Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno);
103 Static void usbd_kill_pipe(usbd_pipe_handle);
104 Static usbd_status usbd_probe_and_attach(device_ptr_t parent,
105 usbd_device_handle dev, int port, int addr);
106
107 Static u_int32_t usb_cookie_no = 0;
108
109 #ifdef USBVERBOSE
110 typedef u_int16_t usb_vendor_id_t;
111 typedef u_int16_t usb_product_id_t;
112
113 /*
114 * Descriptions of of known vendors and devices ("products").
115 */
116 struct usb_vendor {
117 usb_vendor_id_t vendor;
118 const char *vendorname;
119 };
120 struct usb_product {
121 usb_vendor_id_t vendor;
122 usb_product_id_t product;
123 const char *productname;
124 };
125
126 #include <dev/usb/usbdevs_data.h>
127 #endif /* USBVERBOSE */
128
129 Static const char * const usbd_error_strs[] = {
130 "NORMAL_COMPLETION",
131 "IN_PROGRESS",
132 "PENDING_REQUESTS",
133 "NOT_STARTED",
134 "INVAL",
135 "NOMEM",
136 "CANCELLED",
137 "BAD_ADDRESS",
138 "IN_USE",
139 "NO_ADDR",
140 "SET_ADDR_FAILED",
141 "NO_POWER",
142 "TOO_DEEP",
143 "IOERROR",
144 "NOT_CONFIGURED",
145 "TIMEOUT",
146 "SHORT_XFER",
147 "STALLED",
148 "INTERRUPTED",
149 "XXX",
150 };
151
152 const char *
153 usbd_errstr(usbd_status err)
154 {
155 static char buffer[5];
156
157 if (err < USBD_ERROR_MAX) {
158 return usbd_error_strs[err];
159 } else {
160 snprintf(buffer, sizeof buffer, "%d", err);
161 return buffer;
162 }
163 }
164
165 usbd_status
166 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
167 usb_string_descriptor_t *sdesc, int *sizep)
168 {
169 usb_device_request_t req;
170 usbd_status err;
171 int actlen;
172
173 req.bmRequestType = UT_READ_DEVICE;
174 req.bRequest = UR_GET_DESCRIPTOR;
175 USETW2(req.wValue, UDESC_STRING, sindex);
176 USETW(req.wIndex, langid);
177 USETW(req.wLength, sizeof(usb_string_descriptor_t));
178 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
179 &actlen, USBD_DEFAULT_TIMEOUT);
180 if (err)
181 return (err);
182
183 if (actlen != sdesc->bLength) {
184 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
185 sdesc->bLength, actlen));
186 }
187
188 *sizep = actlen;
189 return (USBD_NORMAL_COMPLETION);
190 }
191
192 static void
193 usbd_trim_spaces(char *p)
194 {
195 char *q, *e;
196
197 q = e = p;
198 while (*q == ' ') /* skip leading spaces */
199 q++;
200 while ((*p = *q++)) /* copy string */
201 if (*p++ != ' ') /* remember last non-space */
202 e = p;
203 *e = '\0'; /* kill trailing spaces */
204 }
205
206 Static void
207 usbd_devinfo_vp(usbd_device_handle dev, char *v,
208 char *p, int usedev, int useencoded)
209 {
210 usb_device_descriptor_t *udd = &dev->ddesc;
211 #ifdef USBVERBOSE
212 int n;
213 #endif
214
215 v[0] = p[0] = '\0';
216 if (dev == NULL)
217 return;
218
219 if (usedev) {
220 if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) ==
221 USBD_NORMAL_COMPLETION)
222 usbd_trim_spaces(v);
223 if (usbd_get_string0(dev, udd->iProduct, p, useencoded) ==
224 USBD_NORMAL_COMPLETION)
225 usbd_trim_spaces(p);
226 }
227 /* There is no need for strlcpy & snprintf below. */
228 #ifdef USBVERBOSE
229 if (v[0] == '\0')
230 for (n = 0; n < usb_nvendors; n++)
231 if (usb_vendors[n].vendor == UGETW(udd->idVendor)) {
232 strcpy(v, usb_vendors[n].vendorname);
233 break;
234 }
235 if (p[0] == '\0')
236 for (n = 0; n < usb_nproducts; n++)
237 if (usb_products[n].vendor == UGETW(udd->idVendor) &&
238 usb_products[n].product == UGETW(udd->idProduct)) {
239 strcpy(p, usb_products[n].productname);
240 break;
241 }
242 #endif
243 if (v[0] == '\0')
244 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
245 if (p[0] == '\0')
246 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
247 }
248
249 int
250 usbd_printBCD(char *cp, size_t l, int bcd)
251 {
252 return (snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff));
253 }
254
255 Static void
256 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l)
257 {
258 usb_device_descriptor_t *udd = &dev->ddesc;
259 char *vendor, *product;
260 int bcdDevice, bcdUSB;
261 char *ep;
262
263 vendor = malloc(USB_MAX_ENCODED_STRING_LEN * 2, M_USB, M_NOWAIT);
264 if (vendor == NULL) {
265 *cp = '\0';
266 return;
267 }
268 product = &vendor[USB_MAX_ENCODED_STRING_LEN];
269
270 ep = cp + l;
271
272 usbd_devinfo_vp(dev, vendor, product, 1, 1);
273 cp += snprintf(cp, ep - cp, "%s %s", vendor, product);
274 if (showclass)
275 cp += snprintf(cp, ep - cp, ", class %d/%d",
276 udd->bDeviceClass, udd->bDeviceSubClass);
277 bcdUSB = UGETW(udd->bcdUSB);
278 bcdDevice = UGETW(udd->bcdDevice);
279 cp += snprintf(cp, ep - cp, ", rev ");
280 cp += usbd_printBCD(cp, ep - cp, bcdUSB);
281 *cp++ = '/';
282 cp += usbd_printBCD(cp, ep - cp, bcdDevice);
283 cp += snprintf(cp, ep - cp, ", addr %d", dev->address);
284 *cp = 0;
285 free(vendor, M_USB);
286 }
287
288 char *
289 usbd_devinfo_alloc(usbd_device_handle dev, int showclass)
290 {
291 char *devinfop;
292
293 devinfop = malloc(DEVINFOSIZE, M_TEMP, M_WAITOK);
294 usbd_devinfo(dev, showclass, devinfop, DEVINFOSIZE);
295 return devinfop;
296 }
297
298 void
299 usbd_devinfo_free(char *devinfop)
300 {
301 free(devinfop, M_TEMP);
302 }
303
304 /* Delay for a certain number of ms */
305 void
306 usb_delay_ms(usbd_bus_handle bus, u_int ms)
307 {
308 /* Wait at least two clock ticks so we know the time has passed. */
309 if (bus->use_polling || cold)
310 delay((ms+1) * 1000);
311 else
312 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1);
313 }
314
315 /* Delay given a device handle. */
316 void
317 usbd_delay_ms(usbd_device_handle dev, u_int ms)
318 {
319 usb_delay_ms(dev->bus, ms);
320 }
321
322 usbd_status
323 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps)
324 {
325 usb_device_request_t req;
326 usbd_status err;
327 int n;
328
329 req.bmRequestType = UT_WRITE_CLASS_OTHER;
330 req.bRequest = UR_SET_FEATURE;
331 USETW(req.wValue, UHF_PORT_RESET);
332 USETW(req.wIndex, port);
333 USETW(req.wLength, 0);
334 err = usbd_do_request(dev, &req, 0);
335 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
336 port, usbd_errstr(err)));
337 if (err)
338 return (err);
339 n = 10;
340 do {
341 /* Wait for device to recover from reset. */
342 usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
343 err = usbd_get_port_status(dev, port, ps);
344 if (err) {
345 DPRINTF(("usbd_reset_port: get status failed %d\n",
346 err));
347 return (err);
348 }
349 /* If the device disappeared, just give up. */
350 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
351 return (USBD_NORMAL_COMPLETION);
352 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
353 if (n == 0)
354 return (USBD_TIMEOUT);
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(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
369 {
370 char *p = (char *)cd;
371 char *end = p + UGETW(cd->wTotalLength);
372 usb_interface_descriptor_t *d;
373 int curidx, lastidx, curaidx = 0;
374
375 for (curidx = lastidx = -1; p < end; ) {
376 d = (usb_interface_descriptor_t *)p;
377 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
378 "type=%d\n",
379 ifaceidx, curidx, altidx, curaidx,
380 d->bLength, d->bDescriptorType));
381 if (d->bLength == 0) /* bad descriptor */
382 break;
383 p += d->bLength;
384 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
385 if (d->bInterfaceNumber != lastidx) {
386 lastidx = d->bInterfaceNumber;
387 curidx++;
388 curaidx = 0;
389 } else
390 curaidx++;
391 if (ifaceidx == curidx && altidx == curaidx)
392 return (d);
393 }
394 }
395 return (NULL);
396 }
397
398 usb_endpoint_descriptor_t *
399 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
400 int endptidx)
401 {
402 char *p = (char *)cd;
403 char *end = p + UGETW(cd->wTotalLength);
404 usb_interface_descriptor_t *d;
405 usb_endpoint_descriptor_t *e;
406 int curidx;
407
408 d = usbd_find_idesc(cd, ifaceidx, altidx);
409 if (d == NULL)
410 return (NULL);
411 if (endptidx >= d->bNumEndpoints) /* quick exit */
412 return (NULL);
413
414 curidx = -1;
415 for (p = (char *)d + d->bLength; p < end; ) {
416 e = (usb_endpoint_descriptor_t *)p;
417 if (e->bLength == 0) /* bad descriptor */
418 break;
419 p += e->bLength;
420 if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
421 return (NULL);
422 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
423 curidx++;
424 if (curidx == endptidx)
425 return (e);
426 }
427 }
428 return (NULL);
429 }
430
431 usbd_status
432 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx)
433 {
434 usbd_interface_handle ifc = &dev->ifaces[ifaceidx];
435 usb_interface_descriptor_t *idesc;
436 char *p, *end;
437 int endpt, nendpt;
438
439 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
440 ifaceidx, altidx));
441 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
442 if (idesc == NULL)
443 return (USBD_INVAL);
444 ifc->device = dev;
445 ifc->idesc = idesc;
446 ifc->index = ifaceidx;
447 ifc->altindex = altidx;
448 nendpt = ifc->idesc->bNumEndpoints;
449 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt));
450 if (nendpt != 0) {
451 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint),
452 M_USB, M_NOWAIT);
453 if (ifc->endpoints == NULL)
454 return (USBD_NOMEM);
455 } else
456 ifc->endpoints = NULL;
457 ifc->priv = NULL;
458 p = (char *)ifc->idesc + ifc->idesc->bLength;
459 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);
460 #define ed ((usb_endpoint_descriptor_t *)p)
461 for (endpt = 0; endpt < nendpt; endpt++) {
462 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt));
463 for (; p < end; p += ed->bLength) {
464 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
465 "len=%d type=%d\n",
466 p, end, ed->bLength, ed->bDescriptorType));
467 if (p + ed->bLength <= end && ed->bLength != 0 &&
468 ed->bDescriptorType == UDESC_ENDPOINT)
469 goto found;
470 if (ed->bLength == 0 ||
471 ed->bDescriptorType == UDESC_INTERFACE)
472 break;
473 }
474 /* passed end, or bad desc */
475 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
476 ed->bLength == 0 ? "0 length" :
477 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc":
478 "out of data");
479 goto bad;
480 found:
481 ifc->endpoints[endpt].edesc = ed;
482 if (dev->speed == USB_SPEED_HIGH) {
483 u_int mps;
484 /* Control and bulk endpoints have max packet limits. */
485 switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
486 case UE_CONTROL:
487 mps = USB_2_MAX_CTRL_PACKET;
488 goto check;
489 case UE_BULK:
490 mps = USB_2_MAX_BULK_PACKET;
491 check:
492 if (UGETW(ed->wMaxPacketSize) != mps) {
493 USETW(ed->wMaxPacketSize, mps);
494 #ifdef DIAGNOSTIC
495 printf("usbd_fill_iface_data: bad max "
496 "packet size\n");
497 #endif
498 }
499 break;
500 default:
501 break;
502 }
503 }
504 ifc->endpoints[endpt].refcnt = 0;
505 p += ed->bLength;
506 }
507 #undef ed
508 LIST_INIT(&ifc->pipes);
509 return (USBD_NORMAL_COMPLETION);
510
511 bad:
512 if (ifc->endpoints != NULL) {
513 free(ifc->endpoints, M_USB);
514 ifc->endpoints = NULL;
515 }
516 return (USBD_INVAL);
517 }
518
519 void
520 usbd_free_iface_data(usbd_device_handle dev, int ifcno)
521 {
522 usbd_interface_handle ifc = &dev->ifaces[ifcno];
523 if (ifc->endpoints)
524 free(ifc->endpoints, M_USB);
525 }
526
527 Static usbd_status
528 usbd_set_config(usbd_device_handle dev, int conf)
529 {
530 usb_device_request_t req;
531
532 req.bmRequestType = UT_WRITE_DEVICE;
533 req.bRequest = UR_SET_CONFIG;
534 USETW(req.wValue, conf);
535 USETW(req.wIndex, 0);
536 USETW(req.wLength, 0);
537 return (usbd_do_request(dev, &req, 0));
538 }
539
540 usbd_status
541 usbd_set_config_no(usbd_device_handle dev, int no, int msg)
542 {
543 int index;
544 usb_config_descriptor_t cd;
545 usbd_status err;
546
547 if (no == USB_UNCONFIG_NO)
548 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg));
549
550 DPRINTFN(5,("usbd_set_config_no: %d\n", no));
551 /* Figure out what config index to use. */
552 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
553 err = usbd_get_config_desc(dev, index, &cd);
554 if (err)
555 return (err);
556 if (cd.bConfigurationValue == no)
557 return (usbd_set_config_index(dev, index, msg));
558 }
559 return (USBD_INVAL);
560 }
561
562 usbd_status
563 usbd_set_config_index(usbd_device_handle dev, int index, int msg)
564 {
565 usb_config_descriptor_t cd, *cdp;
566 usbd_status err;
567 int i, ifcidx, nifc, len, selfpowered, power;
568
569 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index));
570
571 if (index >= dev->ddesc.bNumConfigurations &&
572 index != USB_UNCONFIG_NO) {
573 /* panic? */
574 printf("usbd_set_config_index: illegal index\n");
575 return (USBD_INVAL);
576 }
577
578 /* XXX check that all interfaces are idle */
579 if (dev->config != USB_UNCONFIG_NO) {
580 DPRINTF(("usbd_set_config_index: free old config\n"));
581 /* Free all configuration data structures. */
582 nifc = dev->cdesc->bNumInterface;
583 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
584 usbd_free_iface_data(dev, ifcidx);
585 free(dev->ifaces, M_USB);
586 free(dev->cdesc, M_USB);
587 dev->ifaces = NULL;
588 dev->cdesc = NULL;
589 dev->config = USB_UNCONFIG_NO;
590 }
591
592 if (index == USB_UNCONFIG_INDEX) {
593 /* We are unconfiguring the device, so leave unallocated. */
594 DPRINTF(("usbd_set_config_index: set config 0\n"));
595 err = usbd_set_config(dev, USB_UNCONFIG_NO);
596 if (err) {
597 DPRINTF(("usbd_set_config_index: setting config=0 "
598 "failed, error=%s\n", usbd_errstr(err)));
599 }
600 return (err);
601 }
602
603 /* Get the short descriptor. */
604 err = usbd_get_config_desc(dev, index, &cd);
605 if (err)
606 return (err);
607 len = UGETW(cd.wTotalLength);
608 cdp = malloc(len, M_USB, M_NOWAIT);
609 if (cdp == NULL)
610 return (USBD_NOMEM);
611
612 /* Get the full descriptor. Try a few times for slow devices. */
613 for (i = 0; i < 3; i++) {
614 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
615 if (!err)
616 break;
617 usbd_delay_ms(dev, 200);
618 }
619 if (err)
620 goto bad;
621
622 if (cdp->bDescriptorType != UDESC_CONFIG) {
623 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
624 cdp->bDescriptorType));
625 err = USBD_INVAL;
626 goto bad;
627 }
628
629 /*
630 * Figure out if the device is self or bus powered.
631 */
632 #if 0 /* XXX various devices don't report the power state correctly */
633 selfpowered = 0;
634 err = usbd_get_device_status(dev, &ds);
635 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
636 selfpowered = 1;
637 #endif
638 /*
639 * Use the power state in the configuration we are going
640 * to set. This doesn't necessarily reflect the actual
641 * power state of the device; the driver can control this
642 * by choosing the appropriate configuration.
643 */
644 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
645
646 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
647 "selfpowered=%d, power=%d\n",
648 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
649 selfpowered, cdp->bMaxPower * 2));
650
651 /* Check if we have enough power. */
652 #if 0 /* this is a no-op, see above */
653 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
654 if (msg)
655 printf("%s: device addr %d (config %d): "
656 "can't set self powered configuration\n",
657 USBDEVNAME(dev->bus->bdev), dev->address,
658 cdp->bConfigurationValue);
659 err = USBD_NO_POWER;
660 goto bad;
661 }
662 #endif
663 #ifdef USB_DEBUG
664 if (dev->powersrc == NULL) {
665 DPRINTF(("usbd_set_config_index: No power source?\n"));
666 err = USBD_IOERROR;
667 goto bad;
668 }
669 #endif
670 power = cdp->bMaxPower * 2;
671 if (power > dev->powersrc->power) {
672 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
673 /* XXX print nicer message. */
674 if (msg)
675 printf("%s: device addr %d (config %d) exceeds power "
676 "budget, %d mA > %d mA\n",
677 USBDEVNAME(dev->bus->bdev), dev->address,
678 cdp->bConfigurationValue,
679 power, dev->powersrc->power);
680 err = USBD_NO_POWER;
681 goto bad;
682 }
683 dev->power = power;
684 dev->self_powered = selfpowered;
685
686 /* Set the actual configuration value. */
687 DPRINTF(("usbd_set_config_index: set config %d\n",
688 cdp->bConfigurationValue));
689 err = usbd_set_config(dev, cdp->bConfigurationValue);
690 if (err) {
691 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
692 "error=%s\n",
693 cdp->bConfigurationValue, usbd_errstr(err)));
694 goto bad;
695 }
696
697 /* Allocate and fill interface data. */
698 nifc = cdp->bNumInterface;
699 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
700 M_USB, M_NOWAIT);
701 if (dev->ifaces == NULL) {
702 err = USBD_NOMEM;
703 goto bad;
704 }
705 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
706 dev->cdesc = cdp;
707 dev->config = cdp->bConfigurationValue;
708 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
709 err = usbd_fill_iface_data(dev, ifcidx, 0);
710 if (err) {
711 while (--ifcidx >= 0)
712 usbd_free_iface_data(dev, ifcidx);
713 goto bad;
714 }
715 }
716
717 return (USBD_NORMAL_COMPLETION);
718
719 bad:
720 free(cdp, M_USB);
721 return (err);
722 }
723
724 /* XXX add function for alternate settings */
725
726 usbd_status
727 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
728 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
729 {
730 usbd_pipe_handle p;
731 usbd_status err;
732
733 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
734 dev, iface, ep, pipe));
735 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
736 if (p == NULL)
737 return (USBD_NOMEM);
738 p->device = dev;
739 p->iface = iface;
740 p->endpoint = ep;
741 ep->refcnt++;
742 p->refcnt = 1;
743 p->intrxfer = 0;
744 p->running = 0;
745 p->aborting = 0;
746 p->repeat = 0;
747 p->interval = ival;
748 SIMPLEQ_INIT(&p->queue);
749 err = dev->bus->methods->open_pipe(p);
750 if (err) {
751 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
752 "%s\n",
753 ep->edesc->bEndpointAddress, usbd_errstr(err)));
754 free(p, M_USB);
755 return (err);
756 }
757 *pipe = p;
758 return (USBD_NORMAL_COMPLETION);
759 }
760
761 /* Abort the device control pipe. */
762 void
763 usbd_kill_pipe(usbd_pipe_handle pipe)
764 {
765 usbd_abort_pipe(pipe);
766 pipe->methods->close(pipe);
767 pipe->endpoint->refcnt--;
768 free(pipe, M_USB);
769 }
770
771 int
772 usbd_getnewaddr(usbd_bus_handle bus)
773 {
774 int addr;
775
776 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
777 if (bus->devices[addr] == 0)
778 return (addr);
779 return (-1);
780 }
781
782
783 usbd_status
784 usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev,
785 int port, int addr)
786 {
787 struct usb_attach_arg uaa;
788 struct usbif_attach_arg uiaa;
789 usb_device_descriptor_t *dd = &dev->ddesc;
790 int found, i, confi, nifaces;
791 usbd_status err;
792 device_ptr_t dv;
793 usbd_interface_handle *ifaces;
794
795 #if defined(__FreeBSD__)
796 /*
797 * XXX uaa is a static var. Not a problem as it _should_ be used only
798 * during probe and attach. Should be changed however.
799 */
800 device_t bdev;
801 bdev = device_add_child(parent, NULL, -1, &uaa);
802 if (!bdev) {
803 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev));
804 return (USBD_INVAL);
805 }
806 device_quiet(bdev);
807 #endif
808
809 uaa.device = dev;
810 uaa.usegeneric = 0;
811 uaa.port = port;
812 uaa.vendor = UGETW(dd->idVendor);
813 uaa.product = UGETW(dd->idProduct);
814 uaa.release = UGETW(dd->bcdDevice);
815 uaa.class = dd->bDeviceClass;
816 uaa.subclass = dd->bDeviceSubClass;
817 uaa.proto = dd->bDeviceProtocol;
818
819 /* First try with device specific drivers. */
820 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
821 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
822 if (dv) {
823 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
824 if (dev->subdevs == NULL)
825 return (USBD_NOMEM);
826 dev->subdevs[0] = dv;
827 dev->subdevs[1] = 0;
828 return (USBD_NORMAL_COMPLETION);
829 }
830
831 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
832
833 uiaa.device = dev;
834 uiaa.port = port;
835 uiaa.vendor = UGETW(dd->idVendor);
836 uiaa.product = UGETW(dd->idProduct);
837 uiaa.release = UGETW(dd->bcdDevice);
838
839 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
840 dd->bNumConfigurations));
841 /* Next try with interface drivers. */
842 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
843 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
844 confi));
845 err = usbd_set_config_index(dev, confi, 1);
846 if (err) {
847 #ifdef USB_DEBUG
848 DPRINTF(("%s: port %d, set config at addr %d failed, "
849 "error=%s\n", USBDEVPTRNAME(parent), port,
850 addr, usbd_errstr(err)));
851 #else
852 printf("%s: port %d, set config at addr %d failed\n",
853 USBDEVPTRNAME(parent), port, addr);
854 #endif
855 #if defined(__FreeBSD__)
856 device_delete_child(parent, bdev);
857 #endif
858
859 return (err);
860 }
861 nifaces = dev->cdesc->bNumInterface;
862 uiaa.configno = dev->cdesc->bConfigurationValue;
863 ifaces = malloc(nifaces * sizeof(*ifaces), M_USB, M_NOWAIT);
864 if (ifaces == NULL)
865 goto nomem;
866 for (i = 0; i < nifaces; i++)
867 ifaces[i] = &dev->ifaces[i];
868 uiaa.ifaces = ifaces;
869 uiaa.nifaces = nifaces;
870 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT);
871 if (dev->subdevs == NULL) {
872 free(ifaces, M_USB);
873 nomem:
874 #if defined(__FreeBSD__)
875 device_delete_child(parent, bdev);
876 #endif
877 return (USBD_NOMEM);
878 }
879
880 found = 0;
881 for (i = 0; i < nifaces; i++) {
882 if (ifaces[i] == NULL)
883 continue; /* interface already claimed */
884 uiaa.iface = ifaces[i];
885 uiaa.class = ifaces[i]->idesc->bInterfaceClass;
886 uiaa.subclass = ifaces[i]->idesc->bInterfaceSubClass;
887 uiaa.proto = ifaces[i]->idesc->bInterfaceProtocol;
888 uiaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
889 dv = USB_DO_IFATTACH(dev, bdev, parent, &uiaa, usbd_ifprint,
890 usbd_ifsubmatch);
891 if (dv != NULL) {
892 dev->subdevs[found++] = dv;
893 dev->subdevs[found] = 0;
894 ifaces[i] = 0; /* consumed */
895
896 #if defined(__FreeBSD__)
897 /* create another child for the next iface */
898 bdev = device_add_child(parent, NULL, -1,&uaa);
899 if (!bdev) {
900 printf("%s: Device creation failed\n",
901 USBDEVNAME(dev->bus->bdev));
902 free(ifaces, M_USB);
903 return (USBD_NORMAL_COMPLETION);
904 }
905 device_quiet(bdev);
906 #endif
907 }
908 }
909 if (found != 0) {
910 #if defined(__FreeBSD__)
911 /* remove the last created child again; it is unused */
912 device_delete_child(parent, bdev);
913 #endif
914 free(ifaces, M_USB);
915 return (USBD_NORMAL_COMPLETION);
916 }
917 free(ifaces, M_USB);
918 free(dev->subdevs, M_USB);
919 dev->subdevs = 0;
920 }
921 /* No interfaces were attached in any of the configurations. */
922
923 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
924 usbd_set_config_index(dev, 0, 0);
925
926 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
927
928 /* Finally try the generic driver. */
929 uaa.usegeneric = 1;
930 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch);
931 if (dv != NULL) {
932 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT);
933 if (dev->subdevs == 0)
934 return (USBD_NOMEM);
935 dev->subdevs[0] = dv;
936 dev->subdevs[1] = 0;
937 return (USBD_NORMAL_COMPLETION);
938 }
939
940 /*
941 * The generic attach failed, but leave the device as it is.
942 * We just did not find any drivers, that's all. The device is
943 * fully operational and not harming anyone.
944 */
945 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
946 #if defined(__FreeBSD__)
947 device_delete_child(parent, bdev);
948 #endif
949 return (USBD_NORMAL_COMPLETION);
950 }
951
952
953 /*
954 * Called when a new device has been put in the powered state,
955 * but not yet in the addressed state.
956 * Get initial descriptor, set the address, get full descriptor,
957 * and attach a driver.
958 */
959 usbd_status
960 usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth,
961 int speed, int port, struct usbd_port *up)
962 {
963 usbd_device_handle dev, adev;
964 struct usbd_device *hub;
965 usb_device_descriptor_t *dd;
966 usb_port_status_t ps;
967 usbd_status err;
968 int addr;
969 int i;
970 int p;
971
972 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
973 bus, port, depth, speed));
974 addr = usbd_getnewaddr(bus);
975 if (addr < 0) {
976 printf("%s: No free USB addresses, new device ignored.\n",
977 USBDEVNAME(bus->bdev));
978 return (USBD_NO_ADDR);
979 }
980
981 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
982 if (dev == NULL)
983 return (USBD_NOMEM);
984
985 dev->bus = bus;
986
987 /* Set up default endpoint handle. */
988 dev->def_ep.edesc = &dev->def_ep_desc;
989
990 /* Set up default endpoint descriptor. */
991 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
992 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
993 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
994 dev->def_ep_desc.bmAttributes = UE_CONTROL;
995 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
996 dev->def_ep_desc.bInterval = 0;
997
998 dev->quirks = &usbd_no_quirk;
999 dev->address = USB_START_ADDR;
1000 dev->ddesc.bMaxPacketSize = 0;
1001 dev->depth = depth;
1002 dev->powersrc = up;
1003 dev->myhub = up->parent;
1004
1005 up->device = dev;
1006
1007 /* Locate port on upstream high speed hub */
1008 for (adev = dev, hub = up->parent;
1009 hub != NULL && hub->speed != USB_SPEED_HIGH;
1010 adev = hub, hub = hub->myhub)
1011 ;
1012 if (hub) {
1013 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1014 if (hub->hub->ports[p].device == adev) {
1015 dev->myhsport = &hub->hub->ports[p];
1016 goto found;
1017 }
1018 }
1019 panic("usbd_new_device: cannot find HS port\n");
1020 found:
1021 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1022 } else {
1023 dev->myhsport = NULL;
1024 }
1025 dev->speed = speed;
1026 dev->langid = USBD_NOLANG;
1027 dev->cookie.cookie = ++usb_cookie_no;
1028
1029 /* Establish the default pipe. */
1030 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1031 &dev->default_pipe);
1032 if (err) {
1033 usbd_remove_device(dev, up);
1034 return (err);
1035 }
1036
1037 /* Set the address. Do this early; some devices need that. */
1038 err = usbd_set_address(dev, addr);
1039 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr));
1040 if (err) {
1041 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr));
1042 err = USBD_SET_ADDR_FAILED;
1043 usbd_remove_device(dev, up);
1044 return (err);
1045 }
1046 /* Allow device time to set new address */
1047 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1048 dev->address = addr; /* New device address now */
1049 bus->devices[addr] = dev;
1050
1051 dd = &dev->ddesc;
1052 /* Try a few times in case the device is slow (i.e. outside specs.) */
1053 for (i = 0; i < 10; i++) {
1054 /* Get the first 8 bytes of the device descriptor. */
1055 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
1056 if (!err)
1057 break;
1058 usbd_delay_ms(dev, 200);
1059 if ((i & 3) == 3)
1060 usbd_reset_port(up->parent, port, &ps);
1061 }
1062 if (err) {
1063 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1064 "failed\n", addr));
1065 usbd_remove_device(dev, up);
1066 return (err);
1067 }
1068
1069 if (speed == USB_SPEED_HIGH) {
1070 /* Max packet size must be 64 (sec 5.5.3). */
1071 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1072 #ifdef DIAGNOSTIC
1073 printf("usbd_new_device: addr=%d bad max packet size\n",
1074 addr);
1075 #endif
1076 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1077 }
1078 }
1079
1080 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1081 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1082 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1083 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1084 dev->speed));
1085
1086 if (dd->bDescriptorType != UDESC_DEVICE) {
1087 /* Illegal device descriptor */
1088 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1089 dd->bDescriptorType));
1090 usbd_remove_device(dev, up);
1091 return (USBD_INVAL);
1092 }
1093
1094 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1095 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1096 usbd_remove_device(dev, up);
1097 return (USBD_INVAL);
1098 }
1099
1100 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1101
1102 err = usbd_reload_device_desc(dev);
1103 if (err) {
1104 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1105 "failed\n", addr));
1106 usbd_remove_device(dev, up);
1107 return (err);
1108 }
1109
1110 /* Assume 100mA bus powered for now. Changed when configured. */
1111 dev->power = USB_MIN_POWER;
1112 dev->self_powered = 0;
1113
1114 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1115 addr, dev, parent));
1116
1117 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1118
1119 err = usbd_probe_and_attach(parent, dev, port, addr);
1120 if (err) {
1121 usbd_remove_device(dev, up);
1122 return (err);
1123 }
1124
1125 return (USBD_NORMAL_COMPLETION);
1126 }
1127
1128 usbd_status
1129 usbd_reload_device_desc(usbd_device_handle dev)
1130 {
1131 usbd_status err;
1132
1133 /* Get the full device descriptor. */
1134 err = usbd_get_device_desc(dev, &dev->ddesc);
1135 if (err)
1136 return (err);
1137
1138 /* Figure out what's wrong with this device. */
1139 dev->quirks = usbd_find_quirk(&dev->ddesc);
1140
1141 return (USBD_NORMAL_COMPLETION);
1142 }
1143
1144 void
1145 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1146 {
1147 DPRINTF(("usbd_remove_device: %p\n", dev));
1148
1149 if (dev->default_pipe != NULL)
1150 usbd_kill_pipe(dev->default_pipe);
1151 up->device = NULL;
1152 dev->bus->devices[dev->address] = NULL;
1153
1154 free(dev, M_USB);
1155 }
1156
1157 #if defined(__NetBSD__) || defined(__OpenBSD__)
1158 int
1159 usbd_print(void *aux, const char *pnp)
1160 {
1161 struct usb_attach_arg *uaa = aux;
1162
1163 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1164 if (pnp) {
1165 #define USB_DEVINFO 1024
1166 char *devinfo;
1167 if (!uaa->usegeneric)
1168 return (QUIET);
1169 devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK);
1170 usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO);
1171 aprint_normal("%s, %s", devinfo, pnp);
1172 free(devinfo, M_TEMP);
1173 }
1174 if (uaa->port != 0)
1175 aprint_normal(" port %d", uaa->port);
1176 #if 0
1177 /*
1178 * It gets very crowded with these locators on the attach line.
1179 * They are not really needed since they are printed in the clear
1180 * by each driver.
1181 */
1182 if (uaa->vendor != UHUB_UNK_VENDOR)
1183 aprint_normal(" vendor 0x%04x", uaa->vendor);
1184 if (uaa->product != UHUB_UNK_PRODUCT)
1185 aprint_normal(" product 0x%04x", uaa->product);
1186 if (uaa->release != UHUB_UNK_RELEASE)
1187 aprint_normal(" release 0x%04x", uaa->release);
1188 #endif
1189 return (UNCONF);
1190 }
1191
1192 int
1193 usbd_ifprint(void *aux, const char *pnp)
1194 {
1195 struct usbif_attach_arg *uaa = aux;
1196
1197 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1198 if (pnp)
1199 return (QUIET);
1200 if (uaa->port != 0)
1201 aprint_normal(" port %d", uaa->port);
1202 if (uaa->configno != UHUB_UNK_CONFIGURATION)
1203 aprint_normal(" configuration %d", uaa->configno);
1204 if (uaa->ifaceno != UHUB_UNK_INTERFACE)
1205 aprint_normal(" interface %d", uaa->ifaceno);
1206 #if 0
1207 /*
1208 * It gets very crowded with these locators on the attach line.
1209 * They are not really needed since they are printed in the clear
1210 * by each driver.
1211 */
1212 if (uaa->vendor != UHUB_UNK_VENDOR)
1213 aprint_normal(" vendor 0x%04x", uaa->vendor);
1214 if (uaa->product != UHUB_UNK_PRODUCT)
1215 aprint_normal(" product 0x%04x", uaa->product);
1216 if (uaa->release != UHUB_UNK_RELEASE)
1217 aprint_normal(" release 0x%04x", uaa->release);
1218 #endif
1219 return (UNCONF);
1220 }
1221
1222 #if defined(__NetBSD__)
1223 int
1224 usbd_submatch(struct device *parent, struct cfdata *cf,
1225 const int *ldesc, void *aux)
1226 {
1227 #elif defined(__OpenBSD__)
1228 int
1229 usbd_submatch(struct device *parent, void *match, void *aux)
1230 {
1231 struct cfdata *cf = match;
1232 #endif
1233 struct usb_attach_arg *uaa = aux;
1234
1235 DPRINTFN(5,("usbd_submatch port=%d,%d "
1236 "vendor=%d,%d product=%d,%d release=%d,%d\n",
1237 uaa->port, cf->uhubcf_port,
1238 uaa->vendor, cf->uhubcf_vendor,
1239 uaa->product, cf->uhubcf_product,
1240 uaa->release, cf->uhubcf_release));
1241 if (uaa->port != 0 && /* root hub has port 0, it should match */
1242 ((cf->uhubcf_port != UHUB_UNK_PORT &&
1243 cf->uhubcf_port != uaa->port) ||
1244 (uaa->vendor != UHUB_UNK_VENDOR &&
1245 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1246 cf->uhubcf_vendor != uaa->vendor) ||
1247 (uaa->product != UHUB_UNK_PRODUCT &&
1248 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1249 cf->uhubcf_product != uaa->product) ||
1250 (uaa->release != UHUB_UNK_RELEASE &&
1251 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1252 cf->uhubcf_release != uaa->release)
1253 )
1254 )
1255 return 0;
1256 return (config_match(parent, cf, aux));
1257 }
1258
1259 int
1260 usbd_ifsubmatch(struct device *parent, struct cfdata *cf,
1261 const int *ldesc, void *aux)
1262 {
1263 struct usbif_attach_arg *uaa = aux;
1264
1265 DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d "
1266 "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n",
1267 uaa->port, cf->uhubcf_port,
1268 uaa->configno, cf->uhubcf_configuration,
1269 uaa->ifaceno, cf->uhubcf_interface,
1270 uaa->vendor, cf->uhubcf_vendor,
1271 uaa->product, cf->uhubcf_product,
1272 uaa->release, cf->uhubcf_release));
1273 if (uaa->port != 0 && /* root hub has port 0, it should match */
1274 ((cf->uhubcf_port != UHUB_UNK_PORT &&
1275 cf->uhubcf_port != uaa->port) ||
1276 (uaa->configno != UHUB_UNK_CONFIGURATION &&
1277 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION &&
1278 cf->uhubcf_configuration != uaa->configno) ||
1279 (uaa->ifaceno != UHUB_UNK_INTERFACE &&
1280 cf->uhubcf_interface != UHUB_UNK_INTERFACE &&
1281 cf->uhubcf_interface != uaa->ifaceno) ||
1282 (uaa->vendor != UHUB_UNK_VENDOR &&
1283 cf->uhubcf_vendor != UHUB_UNK_VENDOR &&
1284 cf->uhubcf_vendor != uaa->vendor) ||
1285 (uaa->product != UHUB_UNK_PRODUCT &&
1286 cf->uhubcf_product != UHUB_UNK_PRODUCT &&
1287 cf->uhubcf_product != uaa->product) ||
1288 (uaa->release != UHUB_UNK_RELEASE &&
1289 cf->uhubcf_release != UHUB_UNK_RELEASE &&
1290 cf->uhubcf_release != uaa->release)
1291 )
1292 )
1293 return 0;
1294 return (config_match(parent, cf, aux));
1295 }
1296
1297 #endif
1298
1299 void
1300 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1301 int usedev)
1302 {
1303 struct usbd_port *p;
1304 int i, err, s;
1305
1306 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1307 di->udi_addr = dev->address;
1308 di->udi_cookie = dev->cookie;
1309 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 1);
1310 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1311 UGETW(dev->ddesc.bcdDevice));
1312 di->udi_serial[0] = 0;
1313 if (usedev)
1314 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
1315 di->udi_serial);
1316 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1317 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1318 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1319 di->udi_class = dev->ddesc.bDeviceClass;
1320 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1321 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1322 di->udi_config = dev->config;
1323 di->udi_power = dev->self_powered ? 0 : dev->power;
1324 di->udi_speed = dev->speed;
1325
1326 if (dev->subdevs != NULL) {
1327 for (i = 0; dev->subdevs[i] &&
1328 i < USB_MAX_DEVNAMES; i++) {
1329 strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1330 USB_MAX_DEVNAMELEN);
1331 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
1332 }
1333 } else {
1334 i = 0;
1335 }
1336 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1337 di->udi_devnames[i][0] = 0; /* empty */
1338
1339 if (dev->hub) {
1340 for (i = 0;
1341 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1342 i < dev->hub->hubdesc.bNbrPorts;
1343 i++) {
1344 p = &dev->hub->ports[i];
1345 if (p->device)
1346 err = p->device->address;
1347 else {
1348 s = UGETW(p->status.wPortStatus);
1349 if (s & UPS_PORT_ENABLED)
1350 err = USB_PORT_ENABLED;
1351 else if (s & UPS_SUSPEND)
1352 err = USB_PORT_SUSPENDED;
1353 else if (s & UPS_PORT_POWER)
1354 err = USB_PORT_POWERED;
1355 else
1356 err = USB_PORT_DISABLED;
1357 }
1358 di->udi_ports[i] = err;
1359 }
1360 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1361 } else
1362 di->udi_nports = 0;
1363 }
1364
1365 #ifdef COMPAT_30
1366 void
1367 usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di,
1368 int usedev)
1369 {
1370 struct usbd_port *p;
1371 int i, err, s;
1372
1373 di->udi_bus = USBDEVUNIT(dev->bus->bdev);
1374 di->udi_addr = dev->address;
1375 di->udi_cookie = dev->cookie;
1376 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 0);
1377 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1378 UGETW(dev->ddesc.bcdDevice));
1379 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1380 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1381 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1382 di->udi_class = dev->ddesc.bDeviceClass;
1383 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1384 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1385 di->udi_config = dev->config;
1386 di->udi_power = dev->self_powered ? 0 : dev->power;
1387 di->udi_speed = dev->speed;
1388
1389 if (dev->subdevs != NULL) {
1390 for (i = 0; dev->subdevs[i] &&
1391 i < USB_MAX_DEVNAMES; i++) {
1392 strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]),
1393 USB_MAX_DEVNAMELEN);
1394 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
1395 }
1396 } else {
1397 i = 0;
1398 }
1399 for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
1400 di->udi_devnames[i][0] = 0; /* empty */
1401
1402 if (dev->hub) {
1403 for (i = 0;
1404 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1405 i < dev->hub->hubdesc.bNbrPorts;
1406 i++) {
1407 p = &dev->hub->ports[i];
1408 if (p->device)
1409 err = p->device->address;
1410 else {
1411 s = UGETW(p->status.wPortStatus);
1412 if (s & UPS_PORT_ENABLED)
1413 err = USB_PORT_ENABLED;
1414 else if (s & UPS_SUSPEND)
1415 err = USB_PORT_SUSPENDED;
1416 else if (s & UPS_PORT_POWER)
1417 err = USB_PORT_POWERED;
1418 else
1419 err = USB_PORT_DISABLED;
1420 }
1421 di->udi_ports[i] = err;
1422 }
1423 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1424 } else
1425 di->udi_nports = 0;
1426 }
1427 #endif
1428
1429
1430 void
1431 usb_free_device(usbd_device_handle dev)
1432 {
1433 int ifcidx, nifc;
1434
1435 if (dev->default_pipe != NULL)
1436 usbd_kill_pipe(dev->default_pipe);
1437 if (dev->ifaces != NULL) {
1438 nifc = dev->cdesc->bNumInterface;
1439 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1440 usbd_free_iface_data(dev, ifcidx);
1441 free(dev->ifaces, M_USB);
1442 }
1443 if (dev->cdesc != NULL)
1444 free(dev->cdesc, M_USB);
1445 if (dev->subdevs != NULL)
1446 free(dev->subdevs, M_USB);
1447 free(dev, M_USB);
1448 }
1449
1450 /*
1451 * The general mechanism for detaching drivers works as follows: Each
1452 * driver is responsible for maintaining a reference count on the
1453 * number of outstanding references to its softc (e.g. from
1454 * processing hanging in a read or write). The detach method of the
1455 * driver decrements this counter and flags in the softc that the
1456 * driver is dying and then wakes any sleepers. It then sleeps on the
1457 * softc. Each place that can sleep must maintain the reference
1458 * count. When the reference count drops to -1 (0 is the normal value
1459 * of the reference count) the a wakeup on the softc is performed
1460 * signaling to the detach waiter that all references are gone.
1461 */
1462
1463 /*
1464 * Called from process context when we discover that a port has
1465 * been disconnected.
1466 */
1467 void
1468 usb_disconnect_port(struct usbd_port *up, device_ptr_t parent)
1469 {
1470 usbd_device_handle dev = up->device;
1471 char *hubname = USBDEVPTRNAME(parent);
1472 int i;
1473
1474 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1475 up, dev, up->portno));
1476
1477 #ifdef DIAGNOSTIC
1478 if (dev == NULL) {
1479 printf("usb_disconnect_port: no device\n");
1480 return;
1481 }
1482 #endif
1483
1484 if (dev->subdevs != NULL) {
1485 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1486 for (i = 0; dev->subdevs[i]; i++) {
1487 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
1488 hubname);
1489 if (up->portno != 0)
1490 printf(" port %d", up->portno);
1491 printf(" (addr %d) disconnected\n", dev->address);
1492 config_detach(dev->subdevs[i], DETACH_FORCE);
1493 dev->subdevs[i] = 0;
1494 }
1495 }
1496
1497 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1498 dev->bus->devices[dev->address] = NULL;
1499 up->device = NULL;
1500 usb_free_device(dev);
1501 }
1502
1503 #ifdef __OpenBSD__
1504 void *usb_realloc(void *p, u_int size, int pool, int flags)
1505 {
1506 void *q;
1507
1508 q = malloc(size, pool, flags);
1509 if (q == NULL)
1510 return (NULL);
1511 bcopy(p, q, size);
1512 free(p, pool);
1513 return (q);
1514 }
1515 #endif
1516