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