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