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