1 /* $NetBSD: usb_subr.c,v 1.99 2002/07/11 21:14:34 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.76.2.3 2006/03/01 01:59:05 iedowse Exp $ */
3 /* $DragonFly: src/sys/bus/usb/usb_subr.c,v 1.24 2007/07/03 19:28:16 hasso Exp $ */
5 /* Also already have from NetBSD:
6 * $NetBSD: usb_subr.c,v 1.102 2003/01/01 16:21:50 augustss Exp $
7 * $NetBSD: usb_subr.c,v 1.103 2003/01/10 11:19:13 augustss Exp $
8 * $NetBSD: usb_subr.c,v 1.111 2004/03/15 10:35:04 augustss Exp $
9 * $NetBSD: usb_subr.c,v 1.114 2004/06/23 02:30:52 mycroft Exp $
10 * $NetBSD: usb_subr.c,v 1.115 2004/06/23 05:23:19 mycroft Exp $
11 * $NetBSD: usb_subr.c,v 1.116 2004/06/23 06:27:54 mycroft Exp $
12 * $NetBSD: usb_subr.c,v 1.119 2004/10/23 13:26:33 augustss Exp $
16 * Copyright (c) 1998 The NetBSD Foundation, Inc.
17 * All rights reserved.
19 * This code is derived from software contributed to The NetBSD Foundation
20 * by Lennart Augustsson (lennart@augustsson.net) at
21 * Carlstedt Research & Technology.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * This product includes software developed by the NetBSD
34 * Foundation, Inc. and its contributors.
35 * 4. Neither the name of The NetBSD Foundation nor the names of its
36 * contributors may be used to endorse or promote products derived
37 * from this software without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/module.h>
62 #include <bus/usb/usb.h>
64 #include <bus/usb/usbdi.h>
65 #include <bus/usb/usbdi_util.h>
66 #include <bus/usb/usbdivar.h>
68 #include <bus/usb/usb_quirks.h>
70 #define delay(d) DELAY(d)
73 #define DPRINTF(x) if (usbdebug) kprintf x
74 #define DPRINTFN(n,x) if (usbdebug>(n)) kprintf x
81 static usbd_status
usbd_set_config(usbd_device_handle
, int);
82 static void usbd_devinfo_vp(usbd_device_handle
, char *, char *, int);
83 static int usbd_getnewaddr(usbd_bus_handle bus
);
84 static void usbd_free_iface_data(usbd_device_handle dev
, int ifcno
);
85 static void usbd_kill_pipe(usbd_pipe_handle
);
86 static usbd_status
usbd_probe_and_attach(device_t parent
,
87 usbd_device_handle dev
, int port
, int addr
);
89 static u_int32_t usb_cookie_no
= 0;
92 typedef u_int16_t usb_vendor_id_t
;
93 typedef u_int16_t usb_product_id_t
;
96 * Descriptions of of known vendors and devices ("products").
99 usb_vendor_id_t vendor
;
100 usb_product_id_t product
;
102 char *vendorname
, *productname
;
104 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
106 #include "usbdevs_data.h"
107 #endif /* USBVERBOSE */
109 static const char * const usbd_error_strs
[] = {
133 usbd_errstr(usbd_status err
)
135 static char buffer
[5];
137 if (err
< USBD_ERROR_MAX
) {
138 return usbd_error_strs
[err
];
140 ksnprintf(buffer
, sizeof buffer
, "%d", err
);
146 usbd_get_string_desc(usbd_device_handle dev
, int sindex
, int langid
,
147 usb_string_descriptor_t
*sdesc
, int *sizep
)
149 usb_device_request_t req
;
153 req
.bmRequestType
= UT_READ_DEVICE
;
154 req
.bRequest
= UR_GET_DESCRIPTOR
;
155 USETW2(req
.wValue
, UDESC_STRING
, sindex
);
156 USETW(req
.wIndex
, langid
);
157 USETW(req
.wLength
, 2); /* only size byte first */
158 err
= usbd_do_request_flags(dev
, &req
, sdesc
, USBD_SHORT_XFER_OK
,
159 &actlen
, USBD_DEFAULT_TIMEOUT
);
164 return (USBD_SHORT_XFER
);
166 USETW(req
.wLength
, sdesc
->bLength
); /* the whole string */
167 err
= usbd_do_request_flags(dev
, &req
, sdesc
, USBD_SHORT_XFER_OK
,
168 &actlen
, USBD_DEFAULT_TIMEOUT
);
172 if (actlen
!= sdesc
->bLength
) {
173 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
174 sdesc
->bLength
, actlen
));
178 return (USBD_NORMAL_COMPLETION
);
182 usbd_trim_spaces(char *p
)
189 while (*q
== ' ') /* skip leading spaces */
191 while ((*p
= *q
++)) /* copy string */
192 if (*p
++ != ' ') /* remember last non-space */
194 *e
= 0; /* kill trailing spaces */
198 usbd_devinfo_vp(usbd_device_handle dev
, char *v
, char *p
, int usedev
)
200 usb_device_descriptor_t
*udd
= &dev
->ddesc
;
201 char *vendor
= 0, *product
= 0;
203 const struct usb_knowndev
*kdp
;
212 if (usbd_get_string(dev
, udd
->iManufacturer
, v
))
216 usbd_trim_spaces(vendor
);
217 if (usbd_get_string(dev
, udd
->iProduct
, p
))
221 usbd_trim_spaces(product
);
222 if (vendor
&& !*vendor
)
224 if (product
&& !*product
)
231 if (vendor
== NULL
|| product
== NULL
) {
232 for(kdp
= usb_knowndevs
;
233 kdp
->vendorname
!= NULL
;
235 if (kdp
->vendor
== UGETW(udd
->idVendor
) &&
236 (kdp
->product
== UGETW(udd
->idProduct
) ||
237 (kdp
->flags
& USB_KNOWNDEV_NOPROD
) != 0))
240 if (kdp
->vendorname
!= NULL
) {
242 vendor
= kdp
->vendorname
;
244 product
= (kdp
->flags
& USB_KNOWNDEV_NOPROD
) == 0 ?
245 kdp
->productname
: NULL
;
249 if (vendor
!= NULL
&& *vendor
)
252 ksprintf(v
, "vendor 0x%04x", UGETW(udd
->idVendor
));
253 if (product
!= NULL
&& *product
)
256 ksprintf(p
, "product 0x%04x", UGETW(udd
->idProduct
));
260 usbd_printBCD(char *cp
, int bcd
)
262 return (ksprintf(cp
, "%x.%02x", bcd
>> 8, bcd
& 0xff));
266 usbd_devinfo(usbd_device_handle dev
, int showclass
, char *cp
)
268 usb_device_descriptor_t
*udd
= &dev
->ddesc
;
269 usbd_interface_handle iface
;
270 char vendor
[USB_MAX_STRING_LEN
];
271 char product
[USB_MAX_STRING_LEN
];
272 int bcdDevice
, bcdUSB
;
273 usb_interface_descriptor_t
*id
;
275 usbd_devinfo_vp(dev
, vendor
, product
, 1);
276 cp
+= ksprintf(cp
, "%s %s", vendor
, product
);
277 if (showclass
& USBD_SHOW_DEVICE_CLASS
)
278 cp
+= ksprintf(cp
, ", class %d/%d",
279 udd
->bDeviceClass
, udd
->bDeviceSubClass
);
280 bcdUSB
= UGETW(udd
->bcdUSB
);
281 bcdDevice
= UGETW(udd
->bcdDevice
);
282 cp
+= ksprintf(cp
, ", rev ");
283 cp
+= usbd_printBCD(cp
, bcdUSB
);
285 cp
+= usbd_printBCD(cp
, bcdDevice
);
286 cp
+= ksprintf(cp
, ", addr %d", dev
->address
);
287 if (showclass
& USBD_SHOW_INTERFACE_CLASS
)
289 /* fetch the interface handle for the first interface */
290 (void)usbd_device2interface_handle(dev
, 0, &iface
);
291 id
= usbd_get_interface_descriptor(iface
);
292 cp
+= ksprintf(cp
, ", iclass %d/%d",
293 id
->bInterfaceClass
, id
->bInterfaceSubClass
);
298 /* Delay for a certain number of ms */
300 usb_delay_ms(usbd_bus_handle bus
, u_int ms
)
302 /* Wait at least two clock ticks so we know the time has passed. */
303 if (bus
->use_polling
)
304 delay((ms
+1) * 1000);
306 tsleep(&ms
, 0, "usbdly", (ms
*hz
+999)/1000 + 1);
309 /* Delay given a device handle. */
311 usbd_delay_ms(usbd_device_handle dev
, u_int ms
)
313 usb_delay_ms(dev
->bus
, ms
);
317 usbd_reset_port(usbd_device_handle dev
, int port
, usb_port_status_t
*ps
)
319 usb_device_request_t req
;
323 req
.bmRequestType
= UT_WRITE_CLASS_OTHER
;
324 req
.bRequest
= UR_SET_FEATURE
;
325 USETW(req
.wValue
, UHF_PORT_RESET
);
326 USETW(req
.wIndex
, port
);
327 USETW(req
.wLength
, 0);
328 err
= usbd_do_request(dev
, &req
, 0);
329 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
330 port
, usbd_errstr(err
)));
335 /* Wait for device to recover from reset. */
336 usbd_delay_ms(dev
, USB_PORT_RESET_DELAY
);
337 err
= usbd_get_port_status(dev
, port
, ps
);
339 DPRINTF(("usbd_reset_port: get status failed %d\n",
343 /* If the device disappeared, just give up. */
344 if (!(UGETW(ps
->wPortStatus
) & UPS_CURRENT_CONNECT_STATUS
))
345 return (USBD_NORMAL_COMPLETION
);
346 } while ((UGETW(ps
->wPortChange
) & UPS_C_PORT_RESET
) == 0 && --n
> 0);
348 return (USBD_TIMEOUT
);
349 err
= usbd_clear_port_feature(dev
, port
, UHF_C_PORT_RESET
);
352 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
356 /* Wait for the device to recover from reset. */
357 usbd_delay_ms(dev
, USB_PORT_RESET_RECOVERY
);
361 usb_interface_descriptor_t
*
362 usbd_find_idesc(usb_config_descriptor_t
*cd
, int ifaceidx
, int altidx
)
364 char *p
= (char *)cd
;
365 char *end
= p
+ UGETW(cd
->wTotalLength
);
366 usb_interface_descriptor_t
*d
;
367 int curidx
, lastidx
, curaidx
= 0;
369 for (curidx
= lastidx
= -1; p
< end
; ) {
370 d
= (usb_interface_descriptor_t
*)p
;
371 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
373 ifaceidx
, curidx
, altidx
, curaidx
,
374 d
->bLength
, d
->bDescriptorType
));
375 if (d
->bLength
== 0) /* bad descriptor */
378 if (p
<= end
&& d
->bDescriptorType
== UDESC_INTERFACE
) {
379 if (d
->bInterfaceNumber
!= lastidx
) {
380 lastidx
= d
->bInterfaceNumber
;
385 if (ifaceidx
== curidx
&& altidx
== curaidx
)
392 usb_endpoint_descriptor_t
*
393 usbd_find_edesc(usb_config_descriptor_t
*cd
, int ifaceidx
, int altidx
,
396 char *p
= (char *)cd
;
397 char *end
= p
+ UGETW(cd
->wTotalLength
);
398 usb_interface_descriptor_t
*d
;
399 usb_endpoint_descriptor_t
*e
;
402 d
= usbd_find_idesc(cd
, ifaceidx
, altidx
);
405 if (endptidx
>= d
->bNumEndpoints
) /* quick exit */
409 for (p
= (char *)d
+ d
->bLength
; p
< end
; ) {
410 e
= (usb_endpoint_descriptor_t
*)p
;
411 if (e
->bLength
== 0) /* bad descriptor */
414 if (p
<= end
&& e
->bDescriptorType
== UDESC_INTERFACE
)
416 if (p
<= end
&& e
->bDescriptorType
== UDESC_ENDPOINT
) {
418 if (curidx
== endptidx
)
426 usbd_fill_iface_data(usbd_device_handle dev
, int ifaceidx
, int altidx
)
428 usbd_interface_handle ifc
= &dev
->ifaces
[ifaceidx
];
429 usb_interface_descriptor_t
*idesc
;
433 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
435 idesc
= usbd_find_idesc(dev
->cdesc
, ifaceidx
, altidx
);
440 ifc
->index
= ifaceidx
;
441 ifc
->altindex
= altidx
;
442 nendpt
= ifc
->idesc
->bNumEndpoints
;
443 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt
));
445 ifc
->endpoints
= kmalloc(nendpt
* sizeof(struct usbd_endpoint
),
448 ifc
->endpoints
= NULL
;
451 p
= (char *)ifc
->idesc
+ ifc
->idesc
->bLength
;
452 end
= (char *)dev
->cdesc
+ UGETW(dev
->cdesc
->wTotalLength
);
453 #define ed ((usb_endpoint_descriptor_t *)p)
454 for (endpt
= 0; endpt
< nendpt
; endpt
++) {
455 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt
));
456 for (; p
< end
; p
+= ed
->bLength
) {
457 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
459 p
, end
, ed
->bLength
, ed
->bDescriptorType
));
460 if (p
+ ed
->bLength
<= end
&& ed
->bLength
!= 0 &&
461 ed
->bDescriptorType
== UDESC_ENDPOINT
)
463 if (ed
->bLength
== 0 ||
464 ed
->bDescriptorType
== UDESC_INTERFACE
)
467 /* passed end, or bad desc */
468 kprintf("usbd_fill_iface_data: bad descriptor(s): %s\n",
469 ed
->bLength
== 0 ? "0 length" :
470 ed
->bDescriptorType
== UDESC_INTERFACE
? "iface desc":
474 ifc
->endpoints
[endpt
].edesc
= ed
;
475 if (dev
->speed
== USB_SPEED_HIGH
) {
477 /* Control and bulk endpoints have max packet limits. */
478 switch (UE_GET_XFERTYPE(ed
->bmAttributes
)) {
480 mps
= USB_2_MAX_CTRL_PACKET
;
483 mps
= USB_2_MAX_BULK_PACKET
;
485 if (UGETW(ed
->wMaxPacketSize
) != mps
) {
486 USETW(ed
->wMaxPacketSize
, mps
);
488 kprintf("usbd_fill_iface_data: bad max "
497 ifc
->endpoints
[endpt
].refcnt
= 0;
498 ifc
->endpoints
[endpt
].savedtoggle
= 0;
502 LIST_INIT(&ifc
->pipes
);
503 return (USBD_NORMAL_COMPLETION
);
506 if (ifc
->endpoints
!= NULL
) {
507 kfree(ifc
->endpoints
, M_USB
);
508 ifc
->endpoints
= NULL
;
514 usbd_free_iface_data(usbd_device_handle dev
, int ifcno
)
516 usbd_interface_handle ifc
= &dev
->ifaces
[ifcno
];
518 kfree(ifc
->endpoints
, M_USB
);
522 usbd_set_config(usbd_device_handle dev
, int conf
)
524 usb_device_request_t req
;
526 req
.bmRequestType
= UT_WRITE_DEVICE
;
527 req
.bRequest
= UR_SET_CONFIG
;
528 USETW(req
.wValue
, conf
);
529 USETW(req
.wIndex
, 0);
530 USETW(req
.wLength
, 0);
531 return (usbd_do_request(dev
, &req
, 0));
535 usbd_set_config_no(usbd_device_handle dev
, int no
, int msg
)
538 usb_config_descriptor_t cd
;
541 if (no
== USB_UNCONFIG_NO
)
542 return (usbd_set_config_index(dev
, USB_UNCONFIG_INDEX
, msg
));
544 DPRINTFN(5,("usbd_set_config_no: %d\n", no
));
545 /* Figure out what config index to use. */
546 for (index
= 0; index
< dev
->ddesc
.bNumConfigurations
; index
++) {
547 err
= usbd_get_config_desc(dev
, index
, &cd
);
550 if (cd
.bConfigurationValue
== no
)
551 return (usbd_set_config_index(dev
, index
, msg
));
557 usbd_set_config_index(usbd_device_handle dev
, int index
, int msg
)
560 usb_config_descriptor_t cd
, *cdp
;
562 int i
, ifcidx
, nifc
, len
, selfpowered
, power
;
564 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev
, index
));
566 if (dev
->config
!= USB_UNCONFIG_NO
) {
567 nifc
= dev
->cdesc
->bNumInterface
;
569 /* Check that all interfaces are idle */
570 for (ifcidx
= 0; ifcidx
< nifc
; ifcidx
++) {
571 if (LIST_EMPTY(&dev
->ifaces
[ifcidx
].pipes
))
573 DPRINTF(("usbd_set_config_index: open pipes exist\n"));
574 return (USBD_IN_USE
);
577 DPRINTF(("usbd_set_config_index: free old config\n"));
578 /* Free all configuration data structures. */
579 for (ifcidx
= 0; ifcidx
< nifc
; ifcidx
++)
580 usbd_free_iface_data(dev
, ifcidx
);
581 kfree(dev
->ifaces
, M_USB
);
582 kfree(dev
->cdesc
, M_USB
);
585 dev
->config
= USB_UNCONFIG_NO
;
588 if (index
== USB_UNCONFIG_INDEX
) {
589 /* We are unconfiguring the device, so leave unallocated. */
590 DPRINTF(("usbd_set_config_index: set config 0\n"));
591 err
= usbd_set_config(dev
, USB_UNCONFIG_NO
);
593 DPRINTF(("usbd_set_config_index: setting config=0 "
594 "failed, error=%s\n", usbd_errstr(err
)));
598 /* Get the short descriptor. */
599 err
= usbd_get_config_desc(dev
, index
, &cd
);
602 len
= UGETW(cd
.wTotalLength
);
603 cdp
= kmalloc(len
, M_USB
, M_INTWAIT
);
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
);
609 usbd_delay_ms(dev
, 200);
613 if (cdp
->bDescriptorType
!= UDESC_CONFIG
) {
614 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
615 cdp
->bDescriptorType
));
620 /* Figure out if the device is self or bus powered. */
622 if (!(dev
->quirks
->uq_flags
& UQ_BUS_POWERED
) &&
623 (cdp
->bmAttributes
& UC_SELF_POWERED
)) {
624 /* May be self powered. */
625 if (cdp
->bmAttributes
& UC_BUS_POWERED
) {
626 /* Must ask device. */
627 if (dev
->quirks
->uq_flags
& UQ_POWER_CLAIM
) {
629 * Hub claims to be self powered, but isn't.
630 * It seems that the power status can be
631 * determined by the hub characteristics.
633 usb_hub_descriptor_t hd
;
634 usb_device_request_t req
;
635 req
.bmRequestType
= UT_READ_CLASS_DEVICE
;
636 req
.bRequest
= UR_GET_DESCRIPTOR
;
637 USETW(req
.wValue
, 0);
638 USETW(req
.wIndex
, 0);
639 USETW(req
.wLength
, USB_HUB_DESCRIPTOR_SIZE
);
640 err
= usbd_do_request(dev
, &req
, &hd
);
642 (UGETW(hd
.wHubCharacteristics
) &
645 DPRINTF(("usbd_set_config_index: charac=0x%04x"
647 UGETW(hd
.wHubCharacteristics
),
650 err
= usbd_get_device_status(dev
, &ds
);
652 (UGETW(ds
.wStatus
) & UDS_SELF_POWERED
))
654 DPRINTF(("usbd_set_config_index: status=0x%04x"
656 UGETW(ds
.wStatus
), usbd_errstr(err
)));
661 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
662 "selfpowered=%d, power=%d\n",
663 cdp
->bConfigurationValue
, dev
->address
, cdp
->bmAttributes
,
664 selfpowered
, cdp
->bMaxPower
* 2));
666 /* Check if we have enough power. */
668 if (dev
->powersrc
== NULL
) {
669 DPRINTF(("usbd_set_config_index: No power source?\n"));
670 return (USBD_IOERROR
);
673 power
= cdp
->bMaxPower
* 2;
674 if (power
> dev
->powersrc
->power
) {
675 DPRINTF(("power exceeded %d %d\n", power
,dev
->powersrc
->power
));
676 /* XXX print nicer message. */
678 kprintf("%s: device addr %d (config %d) exceeds power "
679 "budget, %d mA > %d mA\n",
680 device_get_nameunit(dev
->bus
->bdev
), dev
->address
,
681 cdp
->bConfigurationValue
,
682 power
, dev
->powersrc
->power
);
687 dev
->self_powered
= selfpowered
;
689 /* Set the actual configuration value. */
690 DPRINTF(("usbd_set_config_index: set config %d\n",
691 cdp
->bConfigurationValue
));
692 err
= usbd_set_config(dev
, cdp
->bConfigurationValue
);
694 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
696 cdp
->bConfigurationValue
, usbd_errstr(err
)));
700 /* Allocate and fill interface data. */
701 nifc
= cdp
->bNumInterface
;
702 dev
->ifaces
= kmalloc(nifc
* sizeof(struct usbd_interface
),
704 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev
, cdp
));
706 dev
->config
= cdp
->bConfigurationValue
;
707 for (ifcidx
= 0; ifcidx
< nifc
; ifcidx
++) {
708 err
= usbd_fill_iface_data(dev
, ifcidx
, 0);
710 while (--ifcidx
>= 0)
711 usbd_free_iface_data(dev
, ifcidx
);
716 return (USBD_NORMAL_COMPLETION
);
723 /* XXX add function for alternate settings */
726 usbd_setup_pipe(usbd_device_handle dev
, usbd_interface_handle iface
,
727 struct usbd_endpoint
*ep
, int ival
, usbd_pipe_handle
*pipe
)
732 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
733 dev
, iface
, ep
, pipe
));
734 p
= kmalloc(dev
->bus
->pipe_size
, M_USB
, M_INTWAIT
);
745 STAILQ_INIT(&p
->queue
);
746 err
= dev
->bus
->methods
->open_pipe(p
);
748 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
750 ep
->edesc
->bEndpointAddress
, usbd_errstr(err
)));
756 return (USBD_NORMAL_COMPLETION
);
759 /* Abort the device control pipe. */
761 usbd_kill_pipe(usbd_pipe_handle pipe
)
763 usbd_abort_pipe(pipe
);
764 pipe
->methods
->close(pipe
);
765 pipe
->endpoint
->refcnt
--;
770 usbd_getnewaddr(usbd_bus_handle bus
)
774 for (addr
= 1; addr
< USB_MAX_DEVICES
; addr
++)
775 if (bus
->devices
[addr
] == 0)
782 usbd_probe_and_attach(device_t parent
, usbd_device_handle dev
,
785 struct usb_attach_arg uaa
;
786 usb_device_descriptor_t
*dd
= &dev
->ddesc
;
787 int found
, i
, confi
, nifaces
;
790 usbd_interface_handle ifaces
[256]; /* 256 is the absolute max */
792 /* XXX FreeBSD may leak resources on failure cases -- fixme */
794 struct usb_attach_arg
*uaap
;
796 bdev
= device_add_child(parent
, NULL
, -1);
798 device_printf(parent
, "Device creation failed\n");
802 uaap
= kmalloc(sizeof(uaa
), M_USB
, M_WAITOK
);
803 device_set_ivars(bdev
, uaap
);
810 uaa
.configno
= UHUB_UNK_CONFIGURATION
;
811 uaa
.ifaceno
= UHUB_UNK_INTERFACE
;
812 uaa
.vendor
= UGETW(dd
->idVendor
);
813 uaa
.product
= UGETW(dd
->idProduct
);
814 uaa
.release
= UGETW(dd
->bcdDevice
);
816 /* First try with device specific drivers. */
817 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
819 dev
->ifacenums
= NULL
;
820 dev
->subdevs
= kmalloc(2 * sizeof(device_t
), M_USB
, M_WAITOK
);
821 dev
->subdevs
[0] = bdev
;
824 if (device_probe_and_attach(bdev
) == 0) {
825 return (USBD_NORMAL_COMPLETION
);
829 * Free subdevs so we can reallocate it larger for the number of
832 tmpdv
= dev
->subdevs
;
835 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
837 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
838 dd
->bNumConfigurations
));
839 /* Next try with interface drivers. */
840 for (confi
= 0; confi
< dd
->bNumConfigurations
; confi
++) {
841 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
843 err
= usbd_set_config_index(dev
, confi
, 1);
846 DPRINTF(("%s: port %d, set config at addr %d failed, "
847 "error=%s\n", device_get_nameunit(parent
), port
,
848 addr
, usbd_errstr(err
)));
850 kprintf("%s: port %d, set config at addr %d failed\n",
851 device_get_nameunit(parent
), port
, addr
);
855 nifaces
= dev
->cdesc
->bNumInterface
;
856 uaa
.configno
= dev
->cdesc
->bConfigurationValue
;
857 for (i
= 0; i
< nifaces
; i
++)
858 ifaces
[i
] = &dev
->ifaces
[i
];
860 uaa
.nifaces
= nifaces
;
861 dev
->subdevs
= kmalloc((nifaces
+1) * sizeof(device_t
), M_USB
,
863 dev
->ifacenums
= kmalloc((nifaces
) * sizeof(*dev
->ifacenums
),
867 for (i
= 0; i
< nifaces
; i
++) {
868 if (ifaces
[i
] == NULL
)
869 continue; /* interface already claimed */
870 uaa
.iface
= ifaces
[i
];
871 uaa
.ifaceno
= ifaces
[i
]->idesc
->bInterfaceNumber
;
872 dev
->subdevs
[found
] = bdev
;
873 dev
->subdevs
[found
+ 1] = 0;
874 dev
->ifacenums
[found
] = i
;
876 if (device_probe_and_attach(bdev
) == 0) {
877 ifaces
[i
] = 0; /* consumed */
880 /* create another child for the next iface */
881 bdev
= device_add_child(parent
, NULL
, -1);
883 device_printf(parent
,
884 "Device add failed\n");
885 return (USBD_NORMAL_COMPLETION
);
887 uaap
= kmalloc(sizeof(uaa
), M_USB
, M_WAITOK
);
888 device_set_ivars(bdev
, uaap
);
891 dev
->subdevs
[found
] = 0;
895 /* remove the last created child. It is unused */
896 device_delete_child(parent
, bdev
);
897 /* kfree(uaap, M_USB); */ /* May be needed? xxx */
898 return (USBD_NORMAL_COMPLETION
);
900 tmpdv
= dev
->subdevs
;
903 kfree(dev
->ifacenums
, M_USB
);
904 dev
->ifacenums
= NULL
;
906 /* No interfaces were attached in any of the configurations. */
908 if (dd
->bNumConfigurations
> 1) /* don't change if only 1 config */
909 usbd_set_config_index(dev
, 0, 0);
911 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
913 /* Finally try the generic driver. */
916 uaa
.configno
= UHUB_UNK_CONFIGURATION
;
917 uaa
.ifaceno
= UHUB_UNK_INTERFACE
;
918 dev
->subdevs
= kmalloc(2 * sizeof(device_t
), M_USB
, M_WAITOK
);
919 dev
->subdevs
[0] = bdev
;
922 if (device_probe_and_attach(bdev
) == 0)
923 return (USBD_NORMAL_COMPLETION
);
926 * The generic attach failed, but leave the device as it is.
927 * We just did not find any drivers, that's all. The device is
928 * fully operational and not harming anyone.
930 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
931 return (USBD_NORMAL_COMPLETION
);
936 * Called when a new device has been put in the powered state,
937 * but not yet in the addressed state.
938 * Get initial descriptor, set the address, get full descriptor,
939 * and attach a driver.
942 usbd_new_device(device_t parent
, usbd_bus_handle bus
, int depth
,
943 int speed
, int port
, struct usbd_port
*up
)
945 usbd_device_handle dev
, adev
;
946 struct usbd_device
*hub
;
947 usb_device_descriptor_t
*dd
;
948 usb_port_status_t ps
;
954 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
955 bus
, port
, depth
, speed
));
956 addr
= usbd_getnewaddr(bus
);
958 kprintf("%s: No free USB addresses, new device ignored.\n",
959 device_get_nameunit(bus
->bdev
));
960 return (USBD_NO_ADDR
);
963 dev
= kmalloc(sizeof *dev
, M_USB
, M_INTWAIT
| M_ZERO
);
966 /* Set up default endpoint handle. */
967 dev
->def_ep
.edesc
= &dev
->def_ep_desc
;
969 /* Set up default endpoint descriptor. */
970 dev
->def_ep_desc
.bLength
= USB_ENDPOINT_DESCRIPTOR_SIZE
;
971 dev
->def_ep_desc
.bDescriptorType
= UDESC_ENDPOINT
;
972 dev
->def_ep_desc
.bEndpointAddress
= USB_CONTROL_ENDPOINT
;
973 dev
->def_ep_desc
.bmAttributes
= UE_CONTROL
;
974 USETW(dev
->def_ep_desc
.wMaxPacketSize
, USB_MAX_IPACKET
);
975 dev
->def_ep_desc
.bInterval
= 0;
977 dev
->quirks
= &usbd_no_quirk
;
978 dev
->address
= USB_START_ADDR
;
979 dev
->ddesc
.bMaxPacketSize
= 0;
982 dev
->myhub
= up
->parent
;
986 /* Locate port on upstream high speed hub */
987 for (adev
= dev
, hub
= up
->parent
;
988 hub
!= NULL
&& hub
->speed
!= USB_SPEED_HIGH
;
989 adev
= hub
, hub
= hub
->myhub
)
992 for (p
= 0; p
< hub
->hub
->hubdesc
.bNbrPorts
; p
++) {
993 if (hub
->hub
->ports
[p
].device
== adev
) {
994 dev
->myhsport
= &hub
->hub
->ports
[p
];
998 panic("usbd_new_device: cannot find HS port\n");
1000 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p
));
1002 dev
->myhsport
= NULL
;
1005 dev
->langid
= USBD_NOLANG
;
1006 dev
->cookie
.cookie
= ++usb_cookie_no
;
1008 /* Establish the default pipe. */
1009 err
= usbd_setup_pipe(dev
, 0, &dev
->def_ep
, USBD_DEFAULT_INTERVAL
,
1010 &dev
->default_pipe
);
1012 usbd_remove_device(dev
, up
);
1016 /* Set the address. Do this early; some devices need that. */
1017 /* Try a few times in case the device is slow (i.e. outside specs.) */
1018 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr
));
1019 for (i
= 0; i
< 15; i
++) {
1020 err
= usbd_set_address(dev
, addr
);
1023 usbd_delay_ms(dev
, 200);
1025 DPRINTFN(-1,("usb_new_device: set address %d "
1026 "failed - trying a port reset\n", addr
));
1027 usbd_reset_port(up
->parent
, port
, &ps
);
1031 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr
));
1032 err
= USBD_SET_ADDR_FAILED
;
1033 usbd_remove_device(dev
, up
);
1036 /* Allow device time to set new address */
1037 usbd_delay_ms(dev
, USB_SET_ADDRESS_SETTLE
);
1038 dev
->address
= addr
; /* New device address now */
1039 bus
->devices
[addr
] = dev
;
1042 /* Get the first 8 bytes of the device descriptor. */
1043 err
= usbd_get_desc(dev
, UDESC_DEVICE
, 0, USB_MAX_IPACKET
, dd
);
1045 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1047 usbd_remove_device(dev
, up
);
1051 if (speed
== USB_SPEED_HIGH
) {
1052 /* Max packet size must be 64 (sec 5.5.3). */
1053 if (dd
->bMaxPacketSize
!= USB_2_MAX_CTRL_PACKET
) {
1055 kprintf("usbd_new_device: addr=%d bad max packet size\n",
1058 dd
->bMaxPacketSize
= USB_2_MAX_CTRL_PACKET
;
1062 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1063 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1064 addr
,UGETW(dd
->bcdUSB
), dd
->bDeviceClass
, dd
->bDeviceSubClass
,
1065 dd
->bDeviceProtocol
, dd
->bMaxPacketSize
, dd
->bLength
,
1068 if (dd
->bDescriptorType
!= UDESC_DEVICE
) {
1069 /* Illegal device descriptor */
1070 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1071 dd
->bDescriptorType
));
1072 usbd_remove_device(dev
, up
);
1073 return (USBD_INVAL
);
1076 if (dd
->bLength
< USB_DEVICE_DESCRIPTOR_SIZE
) {
1077 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd
->bLength
));
1078 usbd_remove_device(dev
, up
);
1079 return (USBD_INVAL
);
1082 USETW(dev
->def_ep_desc
.wMaxPacketSize
, dd
->bMaxPacketSize
);
1084 err
= usbd_reload_device_desc(dev
);
1086 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1088 usbd_remove_device(dev
, up
);
1092 /* Assume 100mA bus powered for now. Changed when configured. */
1093 dev
->power
= USB_MIN_POWER
;
1094 dev
->self_powered
= 0;
1096 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1097 addr
, dev
, parent
));
1099 err
= usbd_probe_and_attach(parent
, dev
, port
, addr
);
1101 usbd_remove_device(dev
, up
);
1105 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH
, dev
);
1107 return (USBD_NORMAL_COMPLETION
);
1111 usbd_reload_device_desc(usbd_device_handle dev
)
1116 /* Get the full device descriptor. */
1117 for (i
= 0; i
< 3; ++i
) {
1118 err
= usbd_get_device_desc(dev
, &dev
->ddesc
);
1121 usbd_delay_ms(dev
, 200);
1126 /* Figure out what's wrong with this device. */
1127 dev
->quirks
= usbd_find_quirk(&dev
->ddesc
);
1129 return (USBD_NORMAL_COMPLETION
);
1133 usbd_remove_device(usbd_device_handle dev
, struct usbd_port
*up
)
1135 DPRINTF(("usbd_remove_device: %p\n", dev
));
1137 if (dev
->default_pipe
!= NULL
)
1138 usbd_kill_pipe(dev
->default_pipe
);
1140 dev
->bus
->devices
[dev
->address
] = NULL
;
1146 usbd_fill_deviceinfo(usbd_device_handle dev
, struct usb_device_info
*di
,
1149 struct usbd_port
*p
;
1152 di
->udi_bus
= device_get_unit(dev
->bus
->bdev
);
1153 di
->udi_addr
= dev
->address
;
1154 di
->udi_cookie
= dev
->cookie
;
1155 usbd_devinfo_vp(dev
, di
->udi_vendor
, di
->udi_product
, usedev
);
1156 usbd_printBCD(di
->udi_release
, UGETW(dev
->ddesc
.bcdDevice
));
1157 di
->udi_vendorNo
= UGETW(dev
->ddesc
.idVendor
);
1158 di
->udi_productNo
= UGETW(dev
->ddesc
.idProduct
);
1159 di
->udi_releaseNo
= UGETW(dev
->ddesc
.bcdDevice
);
1160 di
->udi_class
= dev
->ddesc
.bDeviceClass
;
1161 di
->udi_subclass
= dev
->ddesc
.bDeviceSubClass
;
1162 di
->udi_protocol
= dev
->ddesc
.bDeviceProtocol
;
1163 di
->udi_config
= dev
->config
;
1164 di
->udi_power
= dev
->self_powered
? 0 : dev
->power
;
1165 di
->udi_speed
= dev
->speed
;
1167 if (dev
->subdevs
!= NULL
) {
1168 for (i
= 0; dev
->subdevs
[i
] && i
< USB_MAX_DEVNAMES
; i
++) {
1169 if (device_is_attached(dev
->subdevs
[i
]))
1170 strlcpy(di
->udi_devnames
[i
],
1171 device_get_nameunit(dev
->subdevs
[i
]),
1172 USB_MAX_DEVNAMELEN
);
1174 di
->udi_devnames
[i
][0] = 0;
1179 for (/*i is set */; i
< USB_MAX_DEVNAMES
; i
++)
1180 di
->udi_devnames
[i
][0] = 0; /* empty */
1184 i
< sizeof(di
->udi_ports
) / sizeof(di
->udi_ports
[0]) &&
1185 i
< dev
->hub
->hubdesc
.bNbrPorts
;
1187 p
= &dev
->hub
->ports
[i
];
1189 err
= p
->device
->address
;
1191 s
= UGETW(p
->status
.wPortStatus
);
1192 if (s
& UPS_PORT_ENABLED
)
1193 err
= USB_PORT_ENABLED
;
1194 else if (s
& UPS_SUSPEND
)
1195 err
= USB_PORT_SUSPENDED
;
1196 else if (s
& UPS_PORT_POWER
)
1197 err
= USB_PORT_POWERED
;
1199 err
= USB_PORT_DISABLED
;
1201 di
->udi_ports
[i
] = err
;
1203 di
->udi_nports
= dev
->hub
->hubdesc
.bNbrPorts
;
1209 usb_free_device(usbd_device_handle dev
)
1213 if (dev
->default_pipe
!= NULL
)
1214 usbd_kill_pipe(dev
->default_pipe
);
1215 if (dev
->ifaces
!= NULL
) {
1216 nifc
= dev
->cdesc
->bNumInterface
;
1217 for (ifcidx
= 0; ifcidx
< nifc
; ifcidx
++)
1218 usbd_free_iface_data(dev
, ifcidx
);
1219 kfree(dev
->ifaces
, M_USB
);
1221 if (dev
->cdesc
!= NULL
)
1222 kfree(dev
->cdesc
, M_USB
);
1223 if (dev
->subdevs
!= NULL
)
1224 kfree(dev
->subdevs
, M_USB
);
1225 if (dev
->ifacenums
!= NULL
)
1226 kfree(dev
->ifacenums
, M_USB
);
1231 * The general mechanism for detaching drivers works as follows: Each
1232 * driver is responsible for maintaining a reference count on the
1233 * number of outstanding references to its softc (e.g. from
1234 * processing hanging in a read or write). The detach method of the
1235 * driver decrements this counter and flags in the softc that the
1236 * driver is dying and then wakes any sleepers. It then sleeps on the
1237 * softc. Each place that can sleep must maintain the reference
1238 * count. When the reference count drops to -1 (0 is the normal value
1239 * of the reference count) the a wakeup on the softc is performed
1240 * signaling to the detach waiter that all references are gone.
1244 * Called from process context when we discover that a port has
1245 * been disconnected.
1248 usb_disconnect_port(struct usbd_port
*up
, device_t parent
)
1250 usbd_device_handle dev
= up
->device
;
1251 const char *hubname
= device_get_nameunit(parent
);
1254 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1255 up
, dev
, up
->portno
));
1259 kprintf("usb_disconnect_port: no device\n");
1264 if (dev
->subdevs
!= NULL
) {
1265 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1266 for (i
= 0; dev
->subdevs
[i
]; i
++) {
1267 kprintf("%s: at %s", device_get_nameunit(dev
->subdevs
[i
]),
1269 if (up
->portno
!= 0)
1270 kprintf(" port %d", up
->portno
);
1271 kprintf(" (addr %d) disconnected\n", dev
->address
);
1272 device_delete_child(device_get_parent(dev
->subdevs
[i
]), dev
->subdevs
[i
]);
1273 dev
->subdevs
[i
] = NULL
;
1277 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH
, dev
);
1278 dev
->bus
->devices
[dev
->address
] = NULL
;
1280 usb_free_device(dev
);