usb_subr.c revision 1.171 1 /* $NetBSD: usb_subr.c,v 1.171 2010/06/06 18:58:26 pgoyette 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.171 2010/06/06 18:58:26 pgoyette 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 return (err);
599 len = UGETW(cd.wTotalLength);
600 cdp = malloc(len, M_USB, M_NOWAIT);
601 if (cdp == NULL)
602 return (USBD_NOMEM);
603
604 /* Get the full descriptor. Try a few times for slow devices. */
605 for (i = 0; i < 3; i++) {
606 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
607 if (!err)
608 break;
609 usbd_delay_ms(dev, 200);
610 }
611 if (err)
612 goto bad;
613
614 if (cdp->bDescriptorType != UDESC_CONFIG) {
615 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
616 cdp->bDescriptorType));
617 err = USBD_INVAL;
618 goto bad;
619 }
620
621 /*
622 * Figure out if the device is self or bus powered.
623 */
624 #if 0 /* XXX various devices don't report the power state correctly */
625 selfpowered = 0;
626 err = usbd_get_device_status(dev, &ds);
627 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
628 selfpowered = 1;
629 #endif
630 /*
631 * Use the power state in the configuration we are going
632 * to set. This doesn't necessarily reflect the actual
633 * power state of the device; the driver can control this
634 * by choosing the appropriate configuration.
635 */
636 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
637
638 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
639 "selfpowered=%d, power=%d\n",
640 cdp->bConfigurationValue, dev->address, cdp->bmAttributes,
641 selfpowered, cdp->bMaxPower * 2));
642
643 /* Check if we have enough power. */
644 #if 0 /* this is a no-op, see above */
645 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
646 if (msg)
647 printf("%s: device addr %d (config %d): "
648 "can't set self powered configuration\n",
649 device_xname(dev->bus->bdev), dev->address,
650 cdp->bConfigurationValue);
651 err = USBD_NO_POWER;
652 goto bad;
653 }
654 #endif
655 #ifdef USB_DEBUG
656 if (dev->powersrc == NULL) {
657 DPRINTF(("usbd_set_config_index: No power source?\n"));
658 err = USBD_IOERROR;
659 goto bad;
660 }
661 #endif
662 power = cdp->bMaxPower * 2;
663 if (power > dev->powersrc->power) {
664 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
665 /* XXX print nicer message. */
666 if (msg)
667 printf("%s: device addr %d (config %d) exceeds power "
668 "budget, %d mA > %d mA\n",
669 device_xname(dev->bus->usbctl), dev->address,
670 cdp->bConfigurationValue,
671 power, dev->powersrc->power);
672 err = USBD_NO_POWER;
673 goto bad;
674 }
675 dev->power = power;
676 dev->self_powered = selfpowered;
677
678 /* Set the actual configuration value. */
679 DPRINTF(("usbd_set_config_index: set config %d\n",
680 cdp->bConfigurationValue));
681 err = usbd_set_config(dev, cdp->bConfigurationValue);
682 if (err) {
683 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
684 "error=%s\n",
685 cdp->bConfigurationValue, usbd_errstr(err)));
686 goto bad;
687 }
688
689 /* Allocate and fill interface data. */
690 nifc = cdp->bNumInterface;
691 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface),
692 M_USB, M_NOWAIT);
693 if (dev->ifaces == NULL) {
694 err = USBD_NOMEM;
695 goto bad;
696 }
697 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp));
698 dev->cdesc = cdp;
699 dev->config = cdp->bConfigurationValue;
700 for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
701 err = usbd_fill_iface_data(dev, ifcidx, 0);
702 if (err) {
703 while (--ifcidx >= 0)
704 usbd_free_iface_data(dev, ifcidx);
705 goto bad;
706 }
707 }
708
709 return (USBD_NORMAL_COMPLETION);
710
711 bad:
712 free(cdp, M_USB);
713 return (err);
714 }
715
716 /* XXX add function for alternate settings */
717
718 usbd_status
719 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface,
720 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe)
721 {
722 usbd_pipe_handle p;
723 usbd_status err;
724
725 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
726 dev, iface, ep, pipe));
727 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT);
728 if (p == NULL)
729 return (USBD_NOMEM);
730 p->device = dev;
731 p->iface = iface;
732 p->endpoint = ep;
733 ep->refcnt++;
734 p->refcnt = 1;
735 p->intrxfer = 0;
736 p->running = 0;
737 p->aborting = 0;
738 p->repeat = 0;
739 p->interval = ival;
740 SIMPLEQ_INIT(&p->queue);
741 err = dev->bus->methods->open_pipe(p);
742 if (err) {
743 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
744 "%s\n",
745 ep->edesc->bEndpointAddress, usbd_errstr(err)));
746 free(p, M_USB);
747 return (err);
748 }
749 *pipe = p;
750 return (USBD_NORMAL_COMPLETION);
751 }
752
753 /* Abort the device control pipe. */
754 void
755 usbd_kill_pipe(usbd_pipe_handle pipe)
756 {
757 usbd_abort_pipe(pipe);
758 pipe->methods->close(pipe);
759 pipe->endpoint->refcnt--;
760 free(pipe, M_USB);
761 }
762
763 int
764 usbd_getnewaddr(usbd_bus_handle bus)
765 {
766 int addr;
767
768 for (addr = 1; addr < USB_MAX_DEVICES; addr++)
769 if (bus->devices[addr] == 0)
770 return (addr);
771 return (-1);
772 }
773
774 usbd_status
775 usbd_attach_roothub(device_t parent, usbd_device_handle dev)
776 {
777 struct usb_attach_arg uaa;
778 usb_device_descriptor_t *dd = &dev->ddesc;
779 device_t dv;
780
781 uaa.device = dev;
782 uaa.usegeneric = 0;
783 uaa.port = 0;
784 uaa.vendor = UGETW(dd->idVendor);
785 uaa.product = UGETW(dd->idProduct);
786 uaa.release = UGETW(dd->bcdDevice);
787 uaa.class = dd->bDeviceClass;
788 uaa.subclass = dd->bDeviceSubClass;
789 uaa.proto = dd->bDeviceProtocol;
790
791 dv = config_found_ia(parent, "usbroothubif", &uaa, 0);
792 if (dv) {
793 dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT);
794 if (dev->subdevs == NULL)
795 return (USBD_NOMEM);
796 dev->subdevs[0] = dv;
797 dev->subdevlen = 1;
798 }
799 return (USBD_NORMAL_COMPLETION);
800 }
801
802 static usbd_status
803 usbd_attachwholedevice(device_t parent, usbd_device_handle dev, int port,
804 int usegeneric)
805 {
806 struct usb_attach_arg uaa;
807 usb_device_descriptor_t *dd = &dev->ddesc;
808 device_t dv;
809 int dlocs[USBDEVIFCF_NLOCS];
810
811 uaa.device = dev;
812 uaa.usegeneric = usegeneric;
813 uaa.port = port;
814 uaa.vendor = UGETW(dd->idVendor);
815 uaa.product = UGETW(dd->idProduct);
816 uaa.release = UGETW(dd->bcdDevice);
817 uaa.class = dd->bDeviceClass;
818 uaa.subclass = dd->bDeviceSubClass;
819 uaa.proto = dd->bDeviceProtocol;
820
821 dlocs[USBDEVIFCF_PORT] = uaa.port;
822 dlocs[USBDEVIFCF_VENDOR] = uaa.vendor;
823 dlocs[USBDEVIFCF_PRODUCT] = uaa.product;
824 dlocs[USBDEVIFCF_RELEASE] = uaa.release;
825 /* the rest is historical ballast */
826 dlocs[USBDEVIFCF_CONFIGURATION] = -1;
827 dlocs[USBDEVIFCF_INTERFACE] = -1;
828
829 dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print,
830 config_stdsubmatch);
831 if (dv) {
832 dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT);
833 if (dev->subdevs == NULL)
834 return (USBD_NOMEM);
835 dev->subdevs[0] = dv;
836 dev->subdevlen = 1;
837 dev->nifaces_claimed = 1; /* XXX */
838 }
839 return (USBD_NORMAL_COMPLETION);
840 }
841
842 static usbd_status
843 usbd_attachinterfaces(device_t parent, usbd_device_handle dev,
844 int port, const int *locators)
845 {
846 struct usbif_attach_arg uiaa;
847 int ilocs[USBIFIFCF_NLOCS];
848 usb_device_descriptor_t *dd = &dev->ddesc;
849 int nifaces;
850 usbd_interface_handle *ifaces;
851 int i, j, loc;
852 device_t dv;
853
854 nifaces = dev->cdesc->bNumInterface;
855 ifaces = malloc(nifaces * sizeof(*ifaces), M_USB, M_NOWAIT|M_ZERO);
856 if (!ifaces)
857 return (USBD_NOMEM);
858 for (i = 0; i < nifaces; i++)
859 if (!dev->subdevs[i])
860 ifaces[i] = &dev->ifaces[i];
861
862 uiaa.device = dev;
863 uiaa.port = port;
864 uiaa.vendor = UGETW(dd->idVendor);
865 uiaa.product = UGETW(dd->idProduct);
866 uiaa.release = UGETW(dd->bcdDevice);
867 uiaa.configno = dev->cdesc->bConfigurationValue;
868 uiaa.ifaces = ifaces;
869 uiaa.nifaces = nifaces;
870 ilocs[USBIFIFCF_PORT] = uiaa.port;
871 ilocs[USBIFIFCF_VENDOR] = uiaa.vendor;
872 ilocs[USBIFIFCF_PRODUCT] = uiaa.product;
873 ilocs[USBIFIFCF_RELEASE] = uiaa.release;
874 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.configno;
875
876 for (i = 0; i < nifaces; i++) {
877 if (!ifaces[i])
878 continue; /* interface already claimed */
879 uiaa.iface = ifaces[i];
880 uiaa.class = ifaces[i]->idesc->bInterfaceClass;
881 uiaa.subclass = ifaces[i]->idesc->bInterfaceSubClass;
882 uiaa.proto = ifaces[i]->idesc->bInterfaceProtocol;
883 uiaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
884 ilocs[USBIFIFCF_INTERFACE] = uiaa.ifaceno;
885 if (locators != NULL) {
886 loc = locators[USBIFIFCF_CONFIGURATION];
887 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT &&
888 loc != uiaa.configno)
889 continue;
890 loc = locators[USBIFIFCF_INTERFACE];
891 if (loc != USBIFIFCF_INTERFACE && loc != uiaa.ifaceno)
892 continue;
893 }
894 dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa,
895 usbd_ifprint, config_stdsubmatch);
896 if (!dv)
897 continue;
898 ifaces[i] = 0; /* claim */
899 /* account for ifaces claimed by the driver behind our back */
900 for (j = 0; j < nifaces; j++) {
901 if (!ifaces[j] && !dev->subdevs[j]) {
902 dev->subdevs[j] = dv;
903 dev->nifaces_claimed++;
904 }
905 }
906 }
907
908 free(ifaces, M_USB);
909 return (USBD_NORMAL_COMPLETION);
910 }
911
912 usbd_status
913 usbd_probe_and_attach(device_t parent, usbd_device_handle dev,
914 int port, int addr)
915 {
916 usb_device_descriptor_t *dd = &dev->ddesc;
917 int confi, nifaces;
918 usbd_status err;
919
920 /* First try with device specific drivers. */
921 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
922 err = usbd_attachwholedevice(parent, dev, port, 0);
923 if (dev->nifaces_claimed || err)
924 return (err);
925 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
926
927 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
928 dd->bNumConfigurations));
929 for (confi = 0; confi < dd->bNumConfigurations; confi++) {
930 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
931 confi));
932 err = usbd_set_config_index(dev, confi, 1);
933 if (err) {
934 #ifdef USB_DEBUG
935 DPRINTF(("%s: port %d, set config at addr %d failed, "
936 "error=%s\n", device_xname(parent), port,
937 addr, usbd_errstr(err)));
938 #else
939 printf("%s: port %d, set config at addr %d failed\n",
940 device_xname(parent), port, addr);
941 #endif
942 return (err);
943 }
944 nifaces = dev->cdesc->bNumInterface;
945 dev->subdevs = malloc(nifaces * sizeof(device_t), M_USB,
946 M_NOWAIT|M_ZERO);
947 if (dev->subdevs == NULL)
948 return (USBD_NOMEM);
949 dev->subdevlen = nifaces;
950
951 err = usbd_attachinterfaces(parent, dev, port, NULL);
952
953 if (!dev->nifaces_claimed) {
954 free(dev->subdevs, M_USB);
955 dev->subdevs = 0;
956 dev->subdevlen = 0;
957 }
958 if (dev->nifaces_claimed || err)
959 return (err);
960 }
961 /* No interfaces were attached in any of the configurations. */
962
963 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
964 usbd_set_config_index(dev, 0, 0);
965
966 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
967
968 /* Finally try the generic driver. */
969 err = usbd_attachwholedevice(parent, dev, port, 1);
970
971 /*
972 * The generic attach failed, but leave the device as it is.
973 * We just did not find any drivers, that's all. The device is
974 * fully operational and not harming anyone.
975 */
976 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
977 return (USBD_NORMAL_COMPLETION);
978 }
979
980 /**
981 * Called from uhub_rescan(). usbd_new_device() for the target dev must be
982 * called before calling this.
983 */
984 usbd_status
985 usbd_reattach_device(device_t parent, usbd_device_handle dev,
986 int port, const int *locators)
987 {
988 int i, loc;
989
990 if (locators != NULL) {
991 loc = locators[USBIFIFCF_PORT];
992 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port)
993 return USBD_NORMAL_COMPLETION;
994 loc = locators[USBIFIFCF_VENDOR];
995 if (loc != USBIFIFCF_VENDOR_DEFAULT &&
996 loc != UGETW(dev->ddesc.idVendor))
997 return USBD_NORMAL_COMPLETION;
998 loc = locators[USBIFIFCF_PRODUCT];
999 if (loc != USBIFIFCF_PRODUCT_DEFAULT &&
1000 loc != UGETW(dev->ddesc.idProduct))
1001 return USBD_NORMAL_COMPLETION;
1002 loc = locators[USBIFIFCF_RELEASE];
1003 if (loc != USBIFIFCF_RELEASE_DEFAULT &&
1004 loc != UGETW(dev->ddesc.bcdDevice))
1005 return USBD_NORMAL_COMPLETION;
1006 }
1007 if (dev->subdevlen == 0) {
1008 /* XXX: check USBIFIFCF_CONFIGURATION and
1009 * USBIFIFCF_INTERFACE too */
1010 return usbd_probe_and_attach(parent, dev, port, dev->address);
1011 } else if (dev->subdevlen != dev->cdesc->bNumInterface) {
1012 /* device-specific or generic driver is already attached. */
1013 return USBD_NORMAL_COMPLETION;
1014 }
1015 /* Does the device have unconfigured interfaces? */
1016 for (i = 0; i < dev->subdevlen; i++) {
1017 if (dev->subdevs[i] == NULL) {
1018 break;
1019 }
1020 }
1021 if (i >= dev->subdevlen)
1022 return USBD_NORMAL_COMPLETION;
1023 return usbd_attachinterfaces(parent, dev, port, locators);
1024 }
1025
1026 /*
1027 * Called when a new device has been put in the powered state,
1028 * but not yet in the addressed state.
1029 * Get initial descriptor, set the address, get full descriptor,
1030 * and attach a driver.
1031 */
1032 usbd_status
1033 usbd_new_device(device_t parent, usbd_bus_handle bus, int depth,
1034 int speed, int port, struct usbd_port *up)
1035 {
1036 usbd_device_handle dev, adev;
1037 struct usbd_device *hub;
1038 usb_device_descriptor_t *dd;
1039 usb_port_status_t ps;
1040 usbd_status err;
1041 int addr;
1042 int i;
1043 int p;
1044
1045 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
1046 bus, port, depth, speed));
1047 addr = usbd_getnewaddr(bus);
1048 if (addr < 0) {
1049 printf("%s: No free USB addresses, new device ignored.\n",
1050 device_xname(bus->usbctl));
1051 return (USBD_NO_ADDR);
1052 }
1053
1054 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO);
1055 if (dev == NULL)
1056 return (USBD_NOMEM);
1057
1058 dev->bus = bus;
1059
1060 /* Set up default endpoint handle. */
1061 dev->def_ep.edesc = &dev->def_ep_desc;
1062
1063 /* Set up default endpoint descriptor. */
1064 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
1065 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1066 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1067 dev->def_ep_desc.bmAttributes = UE_CONTROL;
1068 if (speed == USB_SPEED_HIGH)
1069 USETW(dev->def_ep_desc.wMaxPacketSize, 64);
1070 else
1071 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
1072 dev->def_ep_desc.bInterval = 0;
1073
1074 dev->quirks = &usbd_no_quirk;
1075 dev->address = USB_START_ADDR;
1076 dev->ddesc.bMaxPacketSize = 0;
1077 dev->depth = depth;
1078 dev->powersrc = up;
1079 dev->myhub = up->parent;
1080
1081 up->device = dev;
1082
1083 /* Locate port on upstream high speed hub */
1084 for (adev = dev, hub = up->parent;
1085 hub != NULL && hub->speed != USB_SPEED_HIGH;
1086 adev = hub, hub = hub->myhub)
1087 ;
1088 if (hub) {
1089 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) {
1090 if (hub->hub->ports[p].device == adev) {
1091 dev->myhsport = &hub->hub->ports[p];
1092 goto found;
1093 }
1094 }
1095 panic("usbd_new_device: cannot find HS port\n");
1096 found:
1097 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p));
1098 } else {
1099 dev->myhsport = NULL;
1100 }
1101 dev->speed = speed;
1102 dev->langid = USBD_NOLANG;
1103 dev->cookie.cookie = ++usb_cookie_no;
1104
1105 /* Establish the default pipe. */
1106 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1107 &dev->default_pipe);
1108 if (err) {
1109 usbd_remove_device(dev, up);
1110 return (err);
1111 }
1112
1113 dd = &dev->ddesc;
1114 /* Try a few times in case the device is slow (i.e. outside specs.) */
1115 for (i = 0; i < 10; i++) {
1116 /* Get the first 8 bytes of the device descriptor. */
1117 err = usbd_get_desc(dev, UDESC_DEVICE, 0,
1118 (speed == USB_SPEED_HIGH) ? USB_DEVICE_DESCRIPTOR_SIZE
1119 : USB_MAX_IPACKET,
1120 dd);
1121
1122 if (!err)
1123 break;
1124 usbd_delay_ms(dev, 200);
1125 if ((i & 3) == 3)
1126 usbd_reset_port(up->parent, port, &ps);
1127 }
1128 if (err) {
1129 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1130 "failed\n", addr));
1131 usbd_remove_device(dev, up);
1132 return (err);
1133 }
1134
1135 if (speed == USB_SPEED_HIGH) {
1136 /* Max packet size must be 64 (sec 5.5.3). */
1137 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) {
1138 #ifdef DIAGNOSTIC
1139 printf("usbd_new_device: addr=%d bad max packet "
1140 "size=%d. adjusting to %d.\n",
1141 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET);
1142 #endif
1143 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET;
1144 }
1145 }
1146
1147 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1148 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1149 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
1150 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
1151 dev->speed));
1152
1153 if (dd->bDescriptorType != UDESC_DEVICE) {
1154 /* Illegal device descriptor */
1155 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1156 dd->bDescriptorType));
1157 usbd_remove_device(dev, up);
1158 return (USBD_INVAL);
1159 }
1160
1161 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) {
1162 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength));
1163 usbd_remove_device(dev, up);
1164 return (USBD_INVAL);
1165 }
1166
1167 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize);
1168
1169 /* Set the address */
1170 DPRINTFN(5, ("usbd_new_device: setting device address=%d\n", addr));
1171 err = usbd_set_address(dev, addr);
1172 if (err) {
1173 DPRINTFN(-1, ("usbd_new_device: set address %d failed\n", addr));
1174 err = USBD_SET_ADDR_FAILED;
1175 usbd_remove_device(dev, up);
1176 return err;
1177 }
1178
1179 /* Allow device time to set new address */
1180 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
1181 dev->address = addr; /* new device address now */
1182 bus->devices[addr] = dev;
1183
1184 err = usbd_reload_device_desc(dev);
1185 if (err) {
1186 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1187 "failed\n", addr));
1188 usbd_remove_device(dev, up);
1189 return (err);
1190 }
1191
1192 /* Re-establish the default pipe with the new address. */
1193 usbd_kill_pipe(dev->default_pipe);
1194 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
1195 &dev->default_pipe);
1196 if (err) {
1197 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n"));
1198 usbd_remove_device(dev, up);
1199 return err;
1200 }
1201
1202 /* Assume 100mA bus powered for now. Changed when configured. */
1203 dev->power = USB_MIN_POWER;
1204 dev->self_powered = 0;
1205
1206 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1207 addr, dev, parent));
1208
1209 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev);
1210
1211 if (port == 0) { /* root hub */
1212 KASSERT(addr == 1);
1213 usbd_attach_roothub(parent, dev);
1214 return (USBD_NORMAL_COMPLETION);
1215 }
1216
1217 err = usbd_probe_and_attach(parent, dev, port, addr);
1218 if (err) {
1219 usbd_remove_device(dev, up);
1220 return (err);
1221 }
1222
1223 return (USBD_NORMAL_COMPLETION);
1224 }
1225
1226 usbd_status
1227 usbd_reload_device_desc(usbd_device_handle dev)
1228 {
1229 usbd_status err;
1230
1231 /* Get the full device descriptor. */
1232 err = usbd_get_device_desc(dev, &dev->ddesc);
1233 if (err)
1234 return (err);
1235
1236 /* Figure out what's wrong with this device. */
1237 dev->quirks = usbd_find_quirk(&dev->ddesc);
1238
1239 return (USBD_NORMAL_COMPLETION);
1240 }
1241
1242 void
1243 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up)
1244 {
1245 DPRINTF(("usbd_remove_device: %p\n", dev));
1246
1247 if (dev->default_pipe != NULL)
1248 usbd_kill_pipe(dev->default_pipe);
1249 up->device = NULL;
1250 dev->bus->devices[dev->address] = NULL;
1251
1252 free(dev, M_USB);
1253 }
1254
1255 int
1256 usbd_print(void *aux, const char *pnp)
1257 {
1258 struct usb_attach_arg *uaa = aux;
1259
1260 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1261 if (pnp) {
1262 #define USB_DEVINFO 1024
1263 char *devinfo;
1264 if (!uaa->usegeneric)
1265 return (QUIET);
1266 devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK);
1267 usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO);
1268 aprint_normal("%s, %s", devinfo, pnp);
1269 free(devinfo, M_TEMP);
1270 }
1271 aprint_normal(" port %d", uaa->port);
1272 #if 0
1273 /*
1274 * It gets very crowded with these locators on the attach line.
1275 * They are not really needed since they are printed in the clear
1276 * by each driver.
1277 */
1278 if (uaa->vendor != UHUB_UNK_VENDOR)
1279 aprint_normal(" vendor 0x%04x", uaa->vendor);
1280 if (uaa->product != UHUB_UNK_PRODUCT)
1281 aprint_normal(" product 0x%04x", uaa->product);
1282 if (uaa->release != UHUB_UNK_RELEASE)
1283 aprint_normal(" release 0x%04x", uaa->release);
1284 #endif
1285 return (UNCONF);
1286 }
1287
1288 int
1289 usbd_ifprint(void *aux, const char *pnp)
1290 {
1291 struct usbif_attach_arg *uaa = aux;
1292
1293 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
1294 if (pnp)
1295 return (QUIET);
1296 aprint_normal(" port %d", uaa->port);
1297 aprint_normal(" configuration %d", uaa->configno);
1298 aprint_normal(" interface %d", uaa->ifaceno);
1299 #if 0
1300 /*
1301 * It gets very crowded with these locators on the attach line.
1302 * They are not really needed since they are printed in the clear
1303 * by each driver.
1304 */
1305 if (uaa->vendor != UHUB_UNK_VENDOR)
1306 aprint_normal(" vendor 0x%04x", uaa->vendor);
1307 if (uaa->product != UHUB_UNK_PRODUCT)
1308 aprint_normal(" product 0x%04x", uaa->product);
1309 if (uaa->release != UHUB_UNK_RELEASE)
1310 aprint_normal(" release 0x%04x", uaa->release);
1311 #endif
1312 return (UNCONF);
1313 }
1314
1315 void
1316 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di,
1317 int usedev)
1318 {
1319 struct usbd_port *p;
1320 int i, j, err, s;
1321
1322 di->udi_bus = device_unit(dev->bus->usbctl);
1323 di->udi_addr = dev->address;
1324 di->udi_cookie = dev->cookie;
1325 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 1);
1326 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1327 UGETW(dev->ddesc.bcdDevice));
1328 di->udi_serial[0] = 0;
1329 if (usedev)
1330 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber,
1331 di->udi_serial);
1332 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1333 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1334 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1335 di->udi_class = dev->ddesc.bDeviceClass;
1336 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1337 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1338 di->udi_config = dev->config;
1339 di->udi_power = dev->self_powered ? 0 : dev->power;
1340 di->udi_speed = dev->speed;
1341
1342 if (dev->subdevlen > 0) {
1343 for (i = 0, j = 0; i < dev->subdevlen &&
1344 j < USB_MAX_DEVNAMES; i++) {
1345 if (!dev->subdevs[i])
1346 continue;
1347 strncpy(di->udi_devnames[j],
1348 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN);
1349 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1350 j++;
1351 }
1352 } else {
1353 j = 0;
1354 }
1355 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1356 di->udi_devnames[j][0] = 0; /* empty */
1357
1358 if (dev->hub) {
1359 for (i = 0;
1360 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1361 i < dev->hub->hubdesc.bNbrPorts;
1362 i++) {
1363 p = &dev->hub->ports[i];
1364 if (p->device)
1365 err = p->device->address;
1366 else {
1367 s = UGETW(p->status.wPortStatus);
1368 if (s & UPS_PORT_ENABLED)
1369 err = USB_PORT_ENABLED;
1370 else if (s & UPS_SUSPEND)
1371 err = USB_PORT_SUSPENDED;
1372 else if (s & UPS_PORT_POWER)
1373 err = USB_PORT_POWERED;
1374 else
1375 err = USB_PORT_DISABLED;
1376 }
1377 di->udi_ports[i] = err;
1378 }
1379 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1380 } else
1381 di->udi_nports = 0;
1382 }
1383
1384 #ifdef COMPAT_30
1385 void
1386 usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di,
1387 int usedev)
1388 {
1389 struct usbd_port *p;
1390 int i, j, err, s;
1391
1392 di->udi_bus = device_unit(dev->bus->usbctl);
1393 di->udi_addr = dev->address;
1394 di->udi_cookie = dev->cookie;
1395 usbd_devinfo_vp(dev, di->udi_vendor, di->udi_product, usedev, 0);
1396 usbd_printBCD(di->udi_release, sizeof(di->udi_release),
1397 UGETW(dev->ddesc.bcdDevice));
1398 di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
1399 di->udi_productNo = UGETW(dev->ddesc.idProduct);
1400 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
1401 di->udi_class = dev->ddesc.bDeviceClass;
1402 di->udi_subclass = dev->ddesc.bDeviceSubClass;
1403 di->udi_protocol = dev->ddesc.bDeviceProtocol;
1404 di->udi_config = dev->config;
1405 di->udi_power = dev->self_powered ? 0 : dev->power;
1406 di->udi_speed = dev->speed;
1407
1408 if (dev->subdevlen > 0) {
1409 for (i = 0, j = 0; i < dev->subdevlen &&
1410 j < USB_MAX_DEVNAMES; i++) {
1411 if (!dev->subdevs[i])
1412 continue;
1413 strncpy(di->udi_devnames[j],
1414 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN);
1415 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0';
1416 j++;
1417 }
1418 } else {
1419 j = 0;
1420 }
1421 for (/* j is set */; j < USB_MAX_DEVNAMES; j++)
1422 di->udi_devnames[j][0] = 0; /* empty */
1423
1424 if (dev->hub) {
1425 for (i = 0;
1426 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) &&
1427 i < dev->hub->hubdesc.bNbrPorts;
1428 i++) {
1429 p = &dev->hub->ports[i];
1430 if (p->device)
1431 err = p->device->address;
1432 else {
1433 s = UGETW(p->status.wPortStatus);
1434 if (s & UPS_PORT_ENABLED)
1435 err = USB_PORT_ENABLED;
1436 else if (s & UPS_SUSPEND)
1437 err = USB_PORT_SUSPENDED;
1438 else if (s & UPS_PORT_POWER)
1439 err = USB_PORT_POWERED;
1440 else
1441 err = USB_PORT_DISABLED;
1442 }
1443 di->udi_ports[i] = err;
1444 }
1445 di->udi_nports = dev->hub->hubdesc.bNbrPorts;
1446 } else
1447 di->udi_nports = 0;
1448 }
1449 #endif
1450
1451
1452 void
1453 usb_free_device(usbd_device_handle dev)
1454 {
1455 int ifcidx, nifc;
1456
1457 if (dev->default_pipe != NULL)
1458 usbd_kill_pipe(dev->default_pipe);
1459 if (dev->ifaces != NULL) {
1460 nifc = dev->cdesc->bNumInterface;
1461 for (ifcidx = 0; ifcidx < nifc; ifcidx++)
1462 usbd_free_iface_data(dev, ifcidx);
1463 free(dev->ifaces, M_USB);
1464 }
1465 if (dev->cdesc != NULL)
1466 free(dev->cdesc, M_USB);
1467 if (dev->subdevlen > 0) {
1468 free(dev->subdevs, M_USB);
1469 dev->subdevlen = 0;
1470 }
1471 free(dev, M_USB);
1472 }
1473
1474 /*
1475 * The general mechanism for detaching drivers works as follows: Each
1476 * driver is responsible for maintaining a reference count on the
1477 * number of outstanding references to its softc (e.g. from
1478 * processing hanging in a read or write). The detach method of the
1479 * driver decrements this counter and flags in the softc that the
1480 * driver is dying and then wakes any sleepers. It then sleeps on the
1481 * softc. Each place that can sleep must maintain the reference
1482 * count. When the reference count drops to -1 (0 is the normal value
1483 * of the reference count) the a wakeup on the softc is performed
1484 * signaling to the detach waiter that all references are gone.
1485 */
1486
1487 /*
1488 * Called from process context when we discover that a port has
1489 * been disconnected.
1490 */
1491 int
1492 usb_disconnect_port(struct usbd_port *up, device_t parent, int flags)
1493 {
1494 usbd_device_handle dev = up->device;
1495 device_t subdev;
1496 char subdevname[16];
1497 const char *hubname = device_xname(parent);
1498 int i, rc;
1499
1500 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1501 up, dev, up->portno));
1502
1503 if (dev == NULL) {
1504 #ifdef DIAGNOSTIC
1505 printf("usb_disconnect_port: no device\n");
1506 #endif
1507 return 0;
1508 }
1509
1510 if (dev->subdevlen > 0) {
1511 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1512 for (i = 0; i < dev->subdevlen; i++) {
1513 if ((subdev = dev->subdevs[i]) == NULL)
1514 continue;
1515 strlcpy(subdevname, device_xname(subdev),
1516 sizeof(subdevname));
1517 if ((rc = config_detach(subdev, flags)) != 0)
1518 return rc;
1519 printf("%s: at %s", subdevname, hubname);
1520 if (up->portno != 0)
1521 printf(" port %d", up->portno);
1522 printf(" (addr %d) disconnected\n", dev->address);
1523 }
1524 KASSERT(!dev->nifaces_claimed);
1525 }
1526
1527 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev);
1528 dev->bus->devices[dev->address] = NULL;
1529 up->device = NULL;
1530 usb_free_device(dev);
1531 return 0;
1532 }
1533