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