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