usb_subr.c revision 1.172 1 /* $NetBSD: usb_subr.c,v 1.172 2010/07/20 20:56:06 drochner Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart (at) augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.172 2010/07/20 20:56:06 drochner Exp $");
36
37 #include "opt_compat_netbsd.h"
38 #include "opt_usbverbose.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #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 dev, char *v, char *p,
74 int usedev, int useencoded);
75 Static int usbd_getnewaddr(usbd_bus_handle bus);
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 dev, int ifcno);
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 parent,
82 usbd_device_handle dev, int port, int addr);
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 *, usb_vendor_id_t);
112 void get_usb_product_stub(char *, usb_vendor_id_t, usb_product_id_t);
113
114 void (*get_usb_vendor)(char *, usb_vendor_id_t) = get_usb_vendor_stub;
115 void (*get_usb_product)(char *, 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)
126 return;
127
128 mutex_enter(&module_lock);
129 if (module_autoload("usbverbose", MODULE_CLASS_MISC) == 0)
130 usb_verbose_loaded++;
131 mutex_exit(&module_lock);
132 }
133
134 void get_usb_vendor_stub(char *v, usb_vendor_id_t v_id)
135 {
136 usb_load_verbose();
137 if (usb_verbose_loaded)
138 get_usb_vendor(v, v_id);
139 }
140
141 void get_usb_product_stub(char *p, usb_vendor_id_t v_id, usb_product_id_t p_id)
142 {
143 usb_load_verbose();
144 if (usb_verbose_loaded)
145 get_usb_product(p, v_id, p_id);
146 }
147
148 const char *
149 usbd_errstr(usbd_status err)
150 {
151 static char buffer[5];
152
153 if (err < USBD_ERROR_MAX) {
154 return usbd_error_strs[err];
155 } else {
156 snprintf(buffer, sizeof buffer, "%d", err);
157 return buffer;
158 }
159 }
160
161 usbd_status
162 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid,
163 usb_string_descriptor_t *sdesc, int *sizep)
164 {
165 usb_device_request_t req;
166 usbd_status err;
167 int actlen;
168
169 req.bmRequestType = UT_READ_DEVICE;
170 req.bRequest = UR_GET_DESCRIPTOR;
171 USETW2(req.wValue, UDESC_STRING, sindex);
172 USETW(req.wIndex, langid);
173 USETW(req.wLength, 2); /* only size byte first */
174 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
175 &actlen, USBD_DEFAULT_TIMEOUT);
176 if (err)
177 return (err);
178
179 if (actlen < 2)
180 return (USBD_SHORT_XFER);
181
182 USETW(req.wLength, sdesc->bLength); /* the whole string */
183 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
184 &actlen, USBD_DEFAULT_TIMEOUT);
185 if (err)
186 return (err);
187
188 if (actlen != sdesc->bLength) {
189 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
190 sdesc->bLength, actlen));
191 }
192
193 *sizep = actlen;
194 return (USBD_NORMAL_COMPLETION);
195 }
196
197 static void
198 usbd_trim_spaces(char *p)
199 {
200 char *q, *e;
201
202 q = e = p;
203 while (*q == ' ') /* skip leading spaces */
204 q++;
205 while ((*p = *q++)) /* copy string */
206 if (*p++ != ' ') /* remember last non-space */
207 e = p;
208 *e = '\0'; /* kill trailing spaces */
209 }
210
211 Static void
212 usbd_devinfo_vp(usbd_device_handle dev, char *v, char *p, int usedev,
213 int useencoded)
214 {
215 usb_device_descriptor_t *udd = &dev->ddesc;
216
217 v[0] = p[0] = '\0';
218 if (dev == NULL)
219 return;
220
221 if (usedev) {
222 if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) ==
223 USBD_NORMAL_COMPLETION)
224 usbd_trim_spaces(v);
225 if (usbd_get_string0(dev, udd->iProduct, p, useencoded) ==
226 USBD_NORMAL_COMPLETION)
227 usbd_trim_spaces(p);
228 }
229 if (v[0] == '\0')
230 get_usb_vendor(v, UGETW(udd->idVendor));
231 if (p[0] == '\0')
232 get_usb_product(p, UGETW(udd->idVendor), UGETW(udd->idProduct));
233
234 /* There is no need for snprintf below. */
235 if (v[0] == '\0')
236 sprintf(v, "vendor 0x%04x", UGETW(udd->idVendor));
237 if (p[0] == '\0')
238 sprintf(p, "product 0x%04x", UGETW(udd->idProduct));
239 }
240
241 int
242 usbd_printBCD(char *cp, size_t l, int bcd)
243 {
244 return (snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff));
245 }
246
247 Static void
248 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l)
249 {
250 usb_device_descriptor_t *udd = &dev->ddesc;
251 char *vendor, *product;
252 int bcdDevice, bcdUSB;
253 char *ep;
254
255 vendor = malloc(USB_MAX_ENCODED_STRING_LEN * 2, M_USB, M_NOWAIT);
256 if (vendor == NULL) {
257 *cp = '\0';
258 return;
259 }
260 product = &vendor[USB_MAX_ENCODED_STRING_LEN];
261
262 ep = cp + l;
263
264 usbd_devinfo_vp(dev, vendor, product, 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 if (speed == USB_SPEED_HIGH) {
1165 /* Max packet size must be 64 (sec 5.5.3). */
1166 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1167 #ifdef DIAGNOSTIC
1168 printf("usbd_new_device: addr=%d bad max packet "
1169 "size=%d. adjusting to %d.\n",
1170 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET);
1171 #endif
1172 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1173 }
1174 }
1175
1176 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1177 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1178 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1179 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1180 dev->speed));
1181
1182 if (dd->bDescriptorType != UDESC_DEVICE) {
1183 /* Illegal device descriptor */
1184 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1185 dd->bDescriptorType));
1186 usbd_remove_device(dev, up);
1187 return (USBD_INVAL);
1188 }
1189
1190 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1191 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1192 usbd_remove_device(dev, up);
1193 return (USBD_INVAL);
1194 }
1195
1196 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1197
1198 /* Set the address */
1199 DPRINTFN(5, ("usbd_new_device: setting device address=%d\n", addr));
1200 err = usbd_set_address(dev, addr);
1201 if (err) {
1202 DPRINTFN(-1, ("usbd_new_device: set address %d failed\n", addr));
1203 err = USBD_SET_ADDR_FAILED;
1204 usbd_remove_device(dev, up);
1205 return err;
1206 }
1207
1208 /* Allow device time to set new address */
1209 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1210 dev->address = addr; /* new device address now */
1211 bus->devices[addr] = dev;
1212
1213 err = usbd_reload_device_desc(dev);
1214 if (err) {
1215 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1216 "failed\n", addr));
1217 usbd_remove_device(dev, up);
1218 return (err);
1219 }
1220
1221 /* Re-establish the default pipe with the new address. */
1222 usbd_kill_pipe(dev->default_pipe);
1223 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1224 &dev->default_pipe);
1225 if (err) {
1226 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n"));
1227 usbd_remove_device(dev, up);
1228 return err;
1229 }
1230
1231 /* Assume 100mA bus powered for now. Changed when configured. */
1232 dev->power = USB_MIN_POWER;
1233 dev->self_powered = 0;
1234
1235 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1236 addr, dev, parent));
1237
1238 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1239
1240 if (port == 0) { /* root hub */
1241 KASSERT(addr == 1);
1242 usbd_attach_roothub(parent, dev);
1243 return (USBD_NORMAL_COMPLETION);
1244 }
1245
1246 err = usbd_probe_and_attach(parent, dev, port, addr);
1247 if (err) {
1248 usbd_remove_device(dev, up);
1249 return (err);
1250 }
1251
1252 return (USBD_NORMAL_COMPLETION);
1253 }
1254
1255 usbd_status
1256 usbd_reload_device_desc(usbd_device_handle dev)
1257 {
1258 usbd_status err;
1259
1260 /* Get the full device descriptor. */
1261 err = usbd_get_device_desc(dev, &dev->ddesc);
1262 if (err)
1263 return (err);
1264
1265 /* Figure out what's wrong with this device. */
1266 dev->quirks = usbd_find_quirk(&dev->ddesc);
1267
1268 return (USBD_NORMAL_COMPLETION);
1269 }
1270
1271 void
1272 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1273 {
1274 DPRINTF(("usbd_remove_device: %p\n", dev));
1275
1276 if (dev->default_pipe != NULL)
1277 usbd_kill_pipe(dev->default_pipe);
1278 up->device = NULL;
1279 dev->bus->devices[dev->address] = NULL;
1280
1281 free(dev, M_USB);
1282 }
1283
1284 int
1285 usbd_print(void *aux, const char *pnp)
1286 {
1287 struct usb_attach_arg *uaa = aux;
1288
1289 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1290 if (pnp) {
1291 #define USB_DEVINFO 1024
1292 char *devinfo;
1293 if (!uaa->usegeneric)
1294 return (QUIET);
1295 devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK);
1296 usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO);
1297 aprint_normal("%s, %s", devinfo, pnp);
1298 free(devinfo, M_TEMP);
1299 }
1300 aprint_normal(" port %d", uaa->port);
1301 #if 0
1302 /*
1303 * It gets very crowded with these locators on the attach line.
1304 * They are not really needed since they are printed in the clear
1305 * by each driver.
1306 */
1307 if (uaa->vendor != UHUB_UNK_VENDOR)
1308 aprint_normal(" vendor 0x%04x", uaa->vendor);
1309 if (uaa->product != UHUB_UNK_PRODUCT)
1310 aprint_normal(" product 0x%04x", uaa->product);
1311 if (uaa->release != UHUB_UNK_RELEASE)
1312 aprint_normal(" release 0x%04x", uaa->release);
1313 #endif
1314 return (UNCONF);
1315 }
1316
1317 int
1318 usbd_ifprint(void *aux, const char *pnp)
1319 {
1320 struct usbif_attach_arg *uaa = aux;
1321
1322 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1323 if (pnp)
1324 return (QUIET);
1325 aprint_normal(" port %d", uaa->port);
1326 aprint_normal(" configuration %d", uaa->configno);
1327 aprint_normal(" interface %d", uaa->ifaceno);
1328 #if 0
1329 /*
1330 * It gets very crowded with these locators on the attach line.
1331 * They are not really needed since they are printed in the clear
1332 * by each driver.
1333 */
1334 if (uaa->vendor != UHUB_UNK_VENDOR)
1335 aprint_normal(" vendor 0x%04x", uaa->vendor);
1336 if (uaa->product != UHUB_UNK_PRODUCT)
1337 aprint_normal(" product 0x%04x", uaa->product);
1338 if (uaa->release != UHUB_UNK_RELEASE)
1339 aprint_normal(" release 0x%04x", uaa->release);
1340 #endif
1341 return (UNCONF);
1342 }
1343
1344 void
1345 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1346 int usedev)
1347 {
1348 struct usbd_port *p;
1349 int i, j, err, s;
1350
1351 di->udi_bus = device_unit(dev->bus->usbctl);
1352 di->udi_addr = dev->address;
1353 di->udi_cookie = dev->cookie;
1354 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 1);
1355 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1356 UGETW(dev->ddesc.bcdDevice));
1357 di->udi_serial[0] = 0;
1358 if (usedev)
1359 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
1360 di->udi_serial);
1361 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1362 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1363 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1364 di->udi_class = dev->ddesc.bDeviceClass;
1365 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1366 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1367 di->udi_config = dev->config;
1368 di->udi_power = dev->self_powered ? 0 : dev->power;
1369 di->udi_speed = dev->speed;
1370
1371 if (dev->subdevlen > 0) {
1372 for (i = 0, j = 0; i < dev->subdevlen &&
1373 j < USB_MAX_DEVNAMES; i++) {
1374 if (!dev->subdevs[i])
1375 continue;
1376 strncpy(di->udi_devnames[j],
1377 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN);
1378 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1379 j++;
1380 }
1381 } else {
1382 j = 0;
1383 }
1384 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1385 di->udi_devnames[j][0] = 0; /* empty */
1386
1387 if (dev->hub) {
1388 for (i = 0;
1389 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1390 i < dev->hub->hubdesc.bNbrPorts;
1391 i++) {
1392 p = &dev->hub->ports[i];
1393 if (p->device)
1394 err = p->device->address;
1395 else {
1396 s = UGETW(p->status.wPortStatus);
1397 if (s & UPS_PORT_ENABLED)
1398 err = USB_PORT_ENABLED;
1399 else if (s & UPS_SUSPEND)
1400 err = USB_PORT_SUSPENDED;
1401 else if (s & UPS_PORT_POWER)
1402 err = USB_PORT_POWERED;
1403 else
1404 err = USB_PORT_DISABLED;
1405 }
1406 di->udi_ports[i] = err;
1407 }
1408 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1409 } else
1410 di->udi_nports = 0;
1411 }
1412
1413 #ifdef COMPAT_30
1414 void
1415 usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di,
1416 int usedev)
1417 {
1418 struct usbd_port *p;
1419 int i, j, err, s;
1420
1421 di->udi_bus = device_unit(dev->bus->usbctl);
1422 di->udi_addr = dev->address;
1423 di->udi_cookie = dev->cookie;
1424 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 0);
1425 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1426 UGETW(dev->ddesc.bcdDevice));
1427 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1428 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1429 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1430 di->udi_class = dev->ddesc.bDeviceClass;
1431 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1432 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1433 di->udi_config = dev->config;
1434 di->udi_power = dev->self_powered ? 0 : dev->power;
1435 di->udi_speed = dev->speed;
1436
1437 if (dev->subdevlen > 0) {
1438 for (i = 0, j = 0; i < dev->subdevlen &&
1439 j < USB_MAX_DEVNAMES; i++) {
1440 if (!dev->subdevs[i])
1441 continue;
1442 strncpy(di->udi_devnames[j],
1443 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN);
1444 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1445 j++;
1446 }
1447 } else {
1448 j = 0;
1449 }
1450 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1451 di->udi_devnames[j][0] = 0; /* empty */
1452
1453 if (dev->hub) {
1454 for (i = 0;
1455 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1456 i < dev->hub->hubdesc.bNbrPorts;
1457 i++) {
1458 p = &dev->hub->ports[i];
1459 if (p->device)
1460 err = p->device->address;
1461 else {
1462 s = UGETW(p->status.wPortStatus);
1463 if (s & UPS_PORT_ENABLED)
1464 err = USB_PORT_ENABLED;
1465 else if (s & UPS_SUSPEND)
1466 err = USB_PORT_SUSPENDED;
1467 else if (s & UPS_PORT_POWER)
1468 err = USB_PORT_POWERED;
1469 else
1470 err = USB_PORT_DISABLED;
1471 }
1472 di->udi_ports[i] = err;
1473 }
1474 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1475 } else
1476 di->udi_nports = 0;
1477 }
1478 #endif
1479
1480
1481 void
1482 usb_free_device(usbd_device_handle dev)
1483 {
1484 int ifcidx, nifc;
1485
1486 if (dev->default_pipe != NULL)
1487 usbd_kill_pipe(dev->default_pipe);
1488 if (dev->ifaces != NULL) {
1489 nifc = dev->cdesc->bNumInterface;
1490 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1491 usbd_free_iface_data(dev, ifcidx);
1492 free(dev->ifaces, M_USB);
1493 }
1494 if (dev->cdesc != NULL)
1495 free(dev->cdesc, M_USB);
1496 if (dev->subdevlen > 0) {
1497 free(dev->subdevs, M_USB);
1498 dev->subdevlen = 0;
1499 }
1500 free(dev, M_USB);
1501 }
1502
1503 /*
1504 * The general mechanism for detaching drivers works as follows: Each
1505 * driver is responsible for maintaining a reference count on the
1506 * number of outstanding references to its softc (e.g. from
1507 * processing hanging in a read or write). The detach method of the
1508 * driver decrements this counter and flags in the softc that the
1509 * driver is dying and then wakes any sleepers. It then sleeps on the
1510 * softc. Each place that can sleep must maintain the reference
1511 * count. When the reference count drops to -1 (0 is the normal value
1512 * of the reference count) the a wakeup on the softc is performed
1513 * signaling to the detach waiter that all references are gone.
1514 */
1515
1516 /*
1517 * Called from process context when we discover that a port has
1518 * been disconnected.
1519 */
1520 int
1521 usb_disconnect_port(struct usbd_port *up, device_t parent, int flags)
1522 {
1523 usbd_device_handle dev = up->device;
1524 device_t subdev;
1525 char subdevname[16];
1526 const char *hubname = device_xname(parent);
1527 int i, rc;
1528
1529 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1530 up, dev, up->portno));
1531
1532 if (dev == NULL) {
1533 #ifdef DIAGNOSTIC
1534 printf("usb_disconnect_port: no device\n");
1535 #endif
1536 return 0;
1537 }
1538
1539 if (dev->subdevlen > 0) {
1540 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1541 for (i = 0; i < dev->subdevlen; i++) {
1542 if ((subdev = dev->subdevs[i]) == NULL)
1543 continue;
1544 strlcpy(subdevname, device_xname(subdev),
1545 sizeof(subdevname));
1546 if ((rc = config_detach(subdev, flags)) != 0)
1547 return rc;
1548 printf("%s: at %s", subdevname, hubname);
1549 if (up->portno != 0)
1550 printf(" port %d", up->portno);
1551 printf(" (addr %d) disconnected\n", dev->address);
1552 }
1553 KASSERT(!dev->nifaces_claimed);
1554 }
1555
1556 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1557 dev->bus->devices[dev->address] = NULL;
1558 up->device = NULL;
1559 usb_free_device(dev);
1560 return 0;
1561 }
1562