1 /* $NetBSD: usb_subr.c,v 1.166 2009/11/12 08:41:49 uebayasi Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */
5 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.166 2009/11/12 08:41:49 uebayasi Exp $");
37 #include "opt_compat_netbsd.h"
38 #include "opt_usbverbose.h"
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>
50 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usbdi_util.h>
54 #include <dev/usb/usbdivar.h>
55 #include <dev/usb/usbdevs.h>
56 #include <dev/usb/usb_quirks.h>
61 #define DPRINTF(x) if (usbdebug) logprintf x
62 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
69 Static usbd_status
usbd_set_config(usbd_device_handle
, int);
70 Static
void usbd_devinfo(usbd_device_handle
, int, char *, size_t);
71 Static
void usbd_devinfo_vp(usbd_device_handle dev
, char *v
, char *p
,
72 int usedev
, int useencoded
);
73 Static
int usbd_getnewaddr(usbd_bus_handle bus
);
74 Static
int usbd_print(void *, const char *);
75 Static
int usbd_ifprint(void *, const char *);
76 Static
void usbd_free_iface_data(usbd_device_handle dev
, int ifcno
);
77 Static
void usbd_kill_pipe(usbd_pipe_handle
);
78 usbd_status
usbd_attach_roothub(device_t
, usbd_device_handle
);
79 Static usbd_status
usbd_probe_and_attach(device_t parent
,
80 usbd_device_handle dev
, int port
, int addr
);
82 Static u_int32_t usb_cookie_no
= 0;
85 typedef u_int16_t usb_vendor_id_t
;
86 typedef u_int16_t usb_product_id_t
;
89 * Descriptions of of known vendors and devices ("products").
92 usb_vendor_id_t vendor
;
93 const char *vendorname
;
96 usb_vendor_id_t vendor
;
97 usb_product_id_t product
;
98 const char *productname
;
101 #include <dev/usb/usbdevs_data.h>
102 #endif /* USBVERBOSE */
104 Static
const char * const usbd_error_strs
[] = {
128 usbd_errstr(usbd_status err
)
130 static char buffer
[5];
132 if (err
< USBD_ERROR_MAX
) {
133 return usbd_error_strs
[err
];
135 snprintf(buffer
, sizeof buffer
, "%d", err
);
141 usbd_get_string_desc(usbd_device_handle dev
, int sindex
, int langid
,
142 usb_string_descriptor_t
*sdesc
, int *sizep
)
144 usb_device_request_t req
;
148 req
.bmRequestType
= UT_READ_DEVICE
;
149 req
.bRequest
= UR_GET_DESCRIPTOR
;
150 USETW2(req
.wValue
, UDESC_STRING
, sindex
);
151 USETW(req
.wIndex
, langid
);
152 USETW(req
.wLength
, 2); /* only size byte first */
153 err
= usbd_do_request_flags(dev
, &req
, sdesc
, USBD_SHORT_XFER_OK
,
154 &actlen
, USBD_DEFAULT_TIMEOUT
);
159 return (USBD_SHORT_XFER
);
161 USETW(req
.wLength
, sdesc
->bLength
); /* the whole string */
162 err
= usbd_do_request_flags(dev
, &req
, sdesc
, USBD_SHORT_XFER_OK
,
163 &actlen
, USBD_DEFAULT_TIMEOUT
);
167 if (actlen
!= sdesc
->bLength
) {
168 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n",
169 sdesc
->bLength
, actlen
));
173 return (USBD_NORMAL_COMPLETION
);
177 usbd_trim_spaces(char *p
)
182 while (*q
== ' ') /* skip leading spaces */
184 while ((*p
= *q
++)) /* copy string */
185 if (*p
++ != ' ') /* remember last non-space */
187 *e
= '\0'; /* kill trailing spaces */
191 usbd_devinfo_vp(usbd_device_handle dev
, char *v
, char *p
, int usedev
,
194 usb_device_descriptor_t
*udd
= &dev
->ddesc
;
204 if (usbd_get_string0(dev
, udd
->iManufacturer
, v
, useencoded
) ==
205 USBD_NORMAL_COMPLETION
)
207 if (usbd_get_string0(dev
, udd
->iProduct
, p
, useencoded
) ==
208 USBD_NORMAL_COMPLETION
)
211 /* There is no need for strlcpy & snprintf below. */
214 for (n
= 0; n
< usb_nvendors
; n
++)
215 if (usb_vendors
[n
].vendor
== UGETW(udd
->idVendor
)) {
216 strcpy(v
, usb_vendors
[n
].vendorname
);
220 for (n
= 0; n
< usb_nproducts
; n
++)
221 if (usb_products
[n
].vendor
== UGETW(udd
->idVendor
) &&
222 usb_products
[n
].product
== UGETW(udd
->idProduct
)) {
223 strcpy(p
, usb_products
[n
].productname
);
228 sprintf(v
, "vendor 0x%04x", UGETW(udd
->idVendor
));
230 sprintf(p
, "product 0x%04x", UGETW(udd
->idProduct
));
234 usbd_printBCD(char *cp
, size_t l
, int bcd
)
236 return (snprintf(cp
, l
, "%x.%02x", bcd
>> 8, bcd
& 0xff));
240 usbd_devinfo(usbd_device_handle dev
, int showclass
, char *cp
, size_t l
)
242 usb_device_descriptor_t
*udd
= &dev
->ddesc
;
243 char *vendor
, *product
;
244 int bcdDevice
, bcdUSB
;
247 vendor
= malloc(USB_MAX_ENCODED_STRING_LEN
* 2, M_USB
, M_NOWAIT
);
248 if (vendor
== NULL
) {
252 product
= &vendor
[USB_MAX_ENCODED_STRING_LEN
];
256 usbd_devinfo_vp(dev
, vendor
, product
, 1, 1);
257 cp
+= snprintf(cp
, ep
- cp
, "%s %s", vendor
, product
);
259 cp
+= snprintf(cp
, ep
- cp
, ", class %d/%d",
260 udd
->bDeviceClass
, udd
->bDeviceSubClass
);
261 bcdUSB
= UGETW(udd
->bcdUSB
);
262 bcdDevice
= UGETW(udd
->bcdDevice
);
263 cp
+= snprintf(cp
, ep
- cp
, ", rev ");
264 cp
+= usbd_printBCD(cp
, ep
- cp
, bcdUSB
);
266 cp
+= usbd_printBCD(cp
, ep
- cp
, bcdDevice
);
267 cp
+= snprintf(cp
, ep
- cp
, ", addr %d", dev
->address
);
273 usbd_devinfo_alloc(usbd_device_handle dev
, int showclass
)
277 devinfop
= malloc(DEVINFOSIZE
, M_TEMP
, M_WAITOK
);
278 usbd_devinfo(dev
, showclass
, devinfop
, DEVINFOSIZE
);
283 usbd_devinfo_free(char *devinfop
)
285 free(devinfop
, M_TEMP
);
288 /* Delay for a certain number of ms */
290 usb_delay_ms(usbd_bus_handle bus
, u_int ms
)
292 /* Wait at least two clock ticks so we know the time has passed. */
293 if (bus
->use_polling
|| cold
)
294 delay((ms
+1) * 1000);
296 tsleep(&ms
, PRIBIO
, "usbdly", (ms
*hz
+999)/1000 + 1);
299 /* Delay given a device handle. */
301 usbd_delay_ms(usbd_device_handle dev
, u_int ms
)
303 usb_delay_ms(dev
->bus
, ms
);
307 usbd_reset_port(usbd_device_handle dev
, int port
, usb_port_status_t
*ps
)
309 usb_device_request_t req
;
313 req
.bmRequestType
= UT_WRITE_CLASS_OTHER
;
314 req
.bRequest
= UR_SET_FEATURE
;
315 USETW(req
.wValue
, UHF_PORT_RESET
);
316 USETW(req
.wIndex
, port
);
317 USETW(req
.wLength
, 0);
318 err
= usbd_do_request(dev
, &req
, 0);
319 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n",
320 port
, usbd_errstr(err
)));
325 /* Wait for device to recover from reset. */
326 usbd_delay_ms(dev
, USB_PORT_RESET_DELAY
);
327 err
= usbd_get_port_status(dev
, port
, ps
);
329 DPRINTF(("usbd_reset_port: get status failed %d\n",
333 /* If the device disappeared, just give up. */
334 if (!(UGETW(ps
->wPortStatus
) & UPS_CURRENT_CONNECT_STATUS
))
335 return (USBD_NORMAL_COMPLETION
);
336 } while ((UGETW(ps
->wPortChange
) & UPS_C_PORT_RESET
) == 0 && --n
> 0);
338 return (USBD_TIMEOUT
);
339 err
= usbd_clear_port_feature(dev
, port
, UHF_C_PORT_RESET
);
342 DPRINTF(("usbd_reset_port: clear port feature failed %d\n",
346 /* Wait for the device to recover from reset. */
347 usbd_delay_ms(dev
, USB_PORT_RESET_RECOVERY
);
351 usb_interface_descriptor_t
*
352 usbd_find_idesc(usb_config_descriptor_t
*cd
, int ifaceidx
, int altidx
)
354 char *p
= (char *)cd
;
355 char *end
= p
+ UGETW(cd
->wTotalLength
);
356 usb_interface_descriptor_t
*d
;
357 int curidx
, lastidx
, curaidx
= 0;
359 for (curidx
= lastidx
= -1; p
< end
; ) {
360 d
= (usb_interface_descriptor_t
*)p
;
361 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
363 ifaceidx
, curidx
, altidx
, curaidx
,
364 d
->bLength
, d
->bDescriptorType
));
365 if (d
->bLength
== 0) /* bad descriptor */
368 if (p
<= end
&& d
->bDescriptorType
== UDESC_INTERFACE
) {
369 if (d
->bInterfaceNumber
!= lastidx
) {
370 lastidx
= d
->bInterfaceNumber
;
375 if (ifaceidx
== curidx
&& altidx
== curaidx
)
382 usb_endpoint_descriptor_t
*
383 usbd_find_edesc(usb_config_descriptor_t
*cd
, int ifaceidx
, int altidx
,
386 char *p
= (char *)cd
;
387 char *end
= p
+ UGETW(cd
->wTotalLength
);
388 usb_interface_descriptor_t
*d
;
389 usb_endpoint_descriptor_t
*e
;
392 d
= usbd_find_idesc(cd
, ifaceidx
, altidx
);
395 if (endptidx
>= d
->bNumEndpoints
) /* quick exit */
399 for (p
= (char *)d
+ d
->bLength
; p
< end
; ) {
400 e
= (usb_endpoint_descriptor_t
*)p
;
401 if (e
->bLength
== 0) /* bad descriptor */
404 if (p
<= end
&& e
->bDescriptorType
== UDESC_INTERFACE
)
406 if (p
<= end
&& e
->bDescriptorType
== UDESC_ENDPOINT
) {
408 if (curidx
== endptidx
)
416 usbd_fill_iface_data(usbd_device_handle dev
, int ifaceidx
, int altidx
)
418 usbd_interface_handle ifc
= &dev
->ifaces
[ifaceidx
];
419 usb_interface_descriptor_t
*idesc
;
423 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n",
425 idesc
= usbd_find_idesc(dev
->cdesc
, ifaceidx
, altidx
);
430 ifc
->index
= ifaceidx
;
431 ifc
->altindex
= altidx
;
432 nendpt
= ifc
->idesc
->bNumEndpoints
;
433 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt
));
435 ifc
->endpoints
= malloc(nendpt
* sizeof(struct usbd_endpoint
),
437 if (ifc
->endpoints
== NULL
)
440 ifc
->endpoints
= NULL
;
442 p
= (char *)ifc
->idesc
+ ifc
->idesc
->bLength
;
443 end
= (char *)dev
->cdesc
+ UGETW(dev
->cdesc
->wTotalLength
);
444 #define ed ((usb_endpoint_descriptor_t *)p)
445 for (endpt
= 0; endpt
< nendpt
; endpt
++) {
446 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt
));
447 for (; p
< end
; p
+= ed
->bLength
) {
448 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p "
450 p
, end
, ed
->bLength
, ed
->bDescriptorType
));
451 if (p
+ ed
->bLength
<= end
&& ed
->bLength
!= 0 &&
452 ed
->bDescriptorType
== UDESC_ENDPOINT
)
454 if (ed
->bLength
== 0 ||
455 ed
->bDescriptorType
== UDESC_INTERFACE
)
458 /* passed end, or bad desc */
459 printf("usbd_fill_iface_data: bad descriptor(s): %s\n",
460 ed
->bLength
== 0 ? "0 length" :
461 ed
->bDescriptorType
== UDESC_INTERFACE
? "iface desc":
465 ifc
->endpoints
[endpt
].edesc
= ed
;
466 if (dev
->speed
== USB_SPEED_HIGH
) {
468 /* Control and bulk endpoints have max packet limits. */
469 switch (UE_GET_XFERTYPE(ed
->bmAttributes
)) {
471 mps
= USB_2_MAX_CTRL_PACKET
;
474 mps
= USB_2_MAX_BULK_PACKET
;
476 if (UGETW(ed
->wMaxPacketSize
) != mps
) {
477 USETW(ed
->wMaxPacketSize
, mps
);
479 printf("usbd_fill_iface_data: bad max "
488 ifc
->endpoints
[endpt
].refcnt
= 0;
492 LIST_INIT(&ifc
->pipes
);
493 return (USBD_NORMAL_COMPLETION
);
496 if (ifc
->endpoints
!= NULL
) {
497 free(ifc
->endpoints
, M_USB
);
498 ifc
->endpoints
= NULL
;
504 usbd_free_iface_data(usbd_device_handle dev
, int ifcno
)
506 usbd_interface_handle ifc
= &dev
->ifaces
[ifcno
];
508 free(ifc
->endpoints
, M_USB
);
512 usbd_set_config(usbd_device_handle dev
, int conf
)
514 usb_device_request_t req
;
516 req
.bmRequestType
= UT_WRITE_DEVICE
;
517 req
.bRequest
= UR_SET_CONFIG
;
518 USETW(req
.wValue
, conf
);
519 USETW(req
.wIndex
, 0);
520 USETW(req
.wLength
, 0);
521 return (usbd_do_request(dev
, &req
, 0));
525 usbd_set_config_no(usbd_device_handle dev
, int no
, int msg
)
528 usb_config_descriptor_t cd
;
531 if (no
== USB_UNCONFIG_NO
)
532 return (usbd_set_config_index(dev
, USB_UNCONFIG_INDEX
, msg
));
534 DPRINTFN(5,("usbd_set_config_no: %d\n", no
));
535 /* Figure out what config index to use. */
536 for (index
= 0; index
< dev
->ddesc
.bNumConfigurations
; index
++) {
537 err
= usbd_get_config_desc(dev
, index
, &cd
);
540 if (cd
.bConfigurationValue
== no
)
541 return (usbd_set_config_index(dev
, index
, msg
));
547 usbd_set_config_index(usbd_device_handle dev
, int index
, int msg
)
549 usb_config_descriptor_t cd
, *cdp
;
551 int i
, ifcidx
, nifc
, len
, selfpowered
, power
;
553 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev
, index
));
555 if (index
>= dev
->ddesc
.bNumConfigurations
&&
556 index
!= USB_UNCONFIG_INDEX
) {
558 printf("usbd_set_config_index: illegal index\n");
562 /* XXX check that all interfaces are idle */
563 if (dev
->config
!= USB_UNCONFIG_NO
) {
564 DPRINTF(("usbd_set_config_index: free old config\n"));
565 /* Free all configuration data structures. */
566 nifc
= dev
->cdesc
->bNumInterface
;
567 for (ifcidx
= 0; ifcidx
< nifc
; ifcidx
++)
568 usbd_free_iface_data(dev
, ifcidx
);
569 free(dev
->ifaces
, M_USB
);
570 free(dev
->cdesc
, M_USB
);
573 dev
->config
= USB_UNCONFIG_NO
;
576 if (index
== USB_UNCONFIG_INDEX
) {
577 /* We are unconfiguring the device, so leave unallocated. */
578 DPRINTF(("usbd_set_config_index: set config 0\n"));
579 err
= usbd_set_config(dev
, USB_UNCONFIG_NO
);
581 DPRINTF(("usbd_set_config_index: setting config=0 "
582 "failed, error=%s\n", usbd_errstr(err
)));
587 /* Get the short descriptor. */
588 err
= usbd_get_config_desc(dev
, index
, &cd
);
591 len
= UGETW(cd
.wTotalLength
);
592 cdp
= malloc(len
, M_USB
, M_NOWAIT
);
596 /* Get the full descriptor. Try a few times for slow devices. */
597 for (i
= 0; i
< 3; i
++) {
598 err
= usbd_get_desc(dev
, UDESC_CONFIG
, index
, len
, cdp
);
601 usbd_delay_ms(dev
, 200);
606 if (cdp
->bDescriptorType
!= UDESC_CONFIG
) {
607 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n",
608 cdp
->bDescriptorType
));
614 * Figure out if the device is self or bus powered.
616 #if 0 /* XXX various devices don't report the power state correctly */
618 err
= usbd_get_device_status(dev
, &ds
);
619 if (!err
&& (UGETW(ds
.wStatus
) & UDS_SELF_POWERED
))
623 * Use the power state in the configuration we are going
624 * to set. This doesn't necessarily reflect the actual
625 * power state of the device; the driver can control this
626 * by choosing the appropriate configuration.
628 selfpowered
= !!(cdp
->bmAttributes
& UC_SELF_POWERED
);
630 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, "
631 "selfpowered=%d, power=%d\n",
632 cdp
->bConfigurationValue
, dev
->address
, cdp
->bmAttributes
,
633 selfpowered
, cdp
->bMaxPower
* 2));
635 /* Check if we have enough power. */
636 #if 0 /* this is a no-op, see above */
637 if ((cdp
->bmAttributes
& UC_SELF_POWERED
) && !selfpowered
) {
639 printf("%s: device addr %d (config %d): "
640 "can't set self powered configuration\n",
641 device_xname(dev
->bus
->bdev
), dev
->address
,
642 cdp
->bConfigurationValue
);
648 if (dev
->powersrc
== NULL
) {
649 DPRINTF(("usbd_set_config_index: No power source?\n"));
654 power
= cdp
->bMaxPower
* 2;
655 if (power
> dev
->powersrc
->power
) {
656 DPRINTF(("power exceeded %d %d\n", power
,dev
->powersrc
->power
));
657 /* XXX print nicer message. */
659 printf("%s: device addr %d (config %d) exceeds power "
660 "budget, %d mA > %d mA\n",
661 device_xname(dev
->bus
->usbctl
), dev
->address
,
662 cdp
->bConfigurationValue
,
663 power
, dev
->powersrc
->power
);
668 dev
->self_powered
= selfpowered
;
670 /* Set the actual configuration value. */
671 DPRINTF(("usbd_set_config_index: set config %d\n",
672 cdp
->bConfigurationValue
));
673 err
= usbd_set_config(dev
, cdp
->bConfigurationValue
);
675 DPRINTF(("usbd_set_config_index: setting config=%d failed, "
677 cdp
->bConfigurationValue
, usbd_errstr(err
)));
681 /* Allocate and fill interface data. */
682 nifc
= cdp
->bNumInterface
;
683 dev
->ifaces
= malloc(nifc
* sizeof(struct usbd_interface
),
685 if (dev
->ifaces
== NULL
) {
689 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev
, cdp
));
691 dev
->config
= cdp
->bConfigurationValue
;
692 for (ifcidx
= 0; ifcidx
< nifc
; ifcidx
++) {
693 err
= usbd_fill_iface_data(dev
, ifcidx
, 0);
695 while (--ifcidx
>= 0)
696 usbd_free_iface_data(dev
, ifcidx
);
701 return (USBD_NORMAL_COMPLETION
);
708 /* XXX add function for alternate settings */
711 usbd_setup_pipe(usbd_device_handle dev
, usbd_interface_handle iface
,
712 struct usbd_endpoint
*ep
, int ival
, usbd_pipe_handle
*pipe
)
717 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n",
718 dev
, iface
, ep
, pipe
));
719 p
= malloc(dev
->bus
->pipe_size
, M_USB
, M_NOWAIT
);
732 SIMPLEQ_INIT(&p
->queue
);
733 err
= dev
->bus
->methods
->open_pipe(p
);
735 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error="
737 ep
->edesc
->bEndpointAddress
, usbd_errstr(err
)));
742 return (USBD_NORMAL_COMPLETION
);
745 /* Abort the device control pipe. */
747 usbd_kill_pipe(usbd_pipe_handle pipe
)
749 usbd_abort_pipe(pipe
);
750 pipe
->methods
->close(pipe
);
751 pipe
->endpoint
->refcnt
--;
756 usbd_getnewaddr(usbd_bus_handle bus
)
760 for (addr
= 1; addr
< USB_MAX_DEVICES
; addr
++)
761 if (bus
->devices
[addr
] == 0)
767 usbd_attach_roothub(device_t parent
, usbd_device_handle dev
)
769 struct usb_attach_arg uaa
;
770 usb_device_descriptor_t
*dd
= &dev
->ddesc
;
776 uaa
.vendor
= UGETW(dd
->idVendor
);
777 uaa
.product
= UGETW(dd
->idProduct
);
778 uaa
.release
= UGETW(dd
->bcdDevice
);
779 uaa
.class = dd
->bDeviceClass
;
780 uaa
.subclass
= dd
->bDeviceSubClass
;
781 uaa
.proto
= dd
->bDeviceProtocol
;
783 dv
= config_found_ia(parent
, "usbroothubif", &uaa
, 0);
785 dev
->subdevs
= malloc(sizeof dv
, M_USB
, M_NOWAIT
);
786 if (dev
->subdevs
== NULL
)
788 dev
->subdevs
[0] = dv
;
791 return (USBD_NORMAL_COMPLETION
);
795 usbd_attachwholedevice(device_t parent
, usbd_device_handle dev
, int port
,
798 struct usb_attach_arg uaa
;
799 usb_device_descriptor_t
*dd
= &dev
->ddesc
;
801 int dlocs
[USBDEVIFCF_NLOCS
];
804 uaa
.usegeneric
= usegeneric
;
806 uaa
.vendor
= UGETW(dd
->idVendor
);
807 uaa
.product
= UGETW(dd
->idProduct
);
808 uaa
.release
= UGETW(dd
->bcdDevice
);
809 uaa
.class = dd
->bDeviceClass
;
810 uaa
.subclass
= dd
->bDeviceSubClass
;
811 uaa
.proto
= dd
->bDeviceProtocol
;
813 dlocs
[USBDEVIFCF_PORT
] = uaa
.port
;
814 dlocs
[USBDEVIFCF_VENDOR
] = uaa
.vendor
;
815 dlocs
[USBDEVIFCF_PRODUCT
] = uaa
.product
;
816 dlocs
[USBDEVIFCF_RELEASE
] = uaa
.release
;
817 /* the rest is historical ballast */
818 dlocs
[USBDEVIFCF_CONFIGURATION
] = -1;
819 dlocs
[USBDEVIFCF_INTERFACE
] = -1;
821 dv
= config_found_sm_loc(parent
, "usbdevif", dlocs
, &uaa
, usbd_print
,
824 dev
->subdevs
= malloc(sizeof dv
, M_USB
, M_NOWAIT
);
825 if (dev
->subdevs
== NULL
)
827 dev
->subdevs
[0] = dv
;
829 dev
->nifaces_claimed
= 1; /* XXX */
831 return (USBD_NORMAL_COMPLETION
);
835 usbd_attachinterfaces(device_t parent
, usbd_device_handle dev
,
836 int port
, const int *locators
)
838 struct usbif_attach_arg uiaa
;
839 int ilocs
[USBIFIFCF_NLOCS
];
840 usb_device_descriptor_t
*dd
= &dev
->ddesc
;
842 usbd_interface_handle
*ifaces
;
846 nifaces
= dev
->cdesc
->bNumInterface
;
847 ifaces
= malloc(nifaces
* sizeof(*ifaces
), M_USB
, M_NOWAIT
|M_ZERO
);
850 for (i
= 0; i
< nifaces
; i
++)
851 if (!dev
->subdevs
[i
])
852 ifaces
[i
] = &dev
->ifaces
[i
];
856 uiaa
.vendor
= UGETW(dd
->idVendor
);
857 uiaa
.product
= UGETW(dd
->idProduct
);
858 uiaa
.release
= UGETW(dd
->bcdDevice
);
859 uiaa
.configno
= dev
->cdesc
->bConfigurationValue
;
860 uiaa
.ifaces
= ifaces
;
861 uiaa
.nifaces
= nifaces
;
862 ilocs
[USBIFIFCF_PORT
] = uiaa
.port
;
863 ilocs
[USBIFIFCF_VENDOR
] = uiaa
.vendor
;
864 ilocs
[USBIFIFCF_PRODUCT
] = uiaa
.product
;
865 ilocs
[USBIFIFCF_RELEASE
] = uiaa
.release
;
866 ilocs
[USBIFIFCF_CONFIGURATION
] = uiaa
.configno
;
868 for (i
= 0; i
< nifaces
; i
++) {
870 continue; /* interface already claimed */
871 uiaa
.iface
= ifaces
[i
];
872 uiaa
.class = ifaces
[i
]->idesc
->bInterfaceClass
;
873 uiaa
.subclass
= ifaces
[i
]->idesc
->bInterfaceSubClass
;
874 uiaa
.proto
= ifaces
[i
]->idesc
->bInterfaceProtocol
;
875 uiaa
.ifaceno
= ifaces
[i
]->idesc
->bInterfaceNumber
;
876 ilocs
[USBIFIFCF_INTERFACE
] = uiaa
.ifaceno
;
877 if (locators
!= NULL
) {
878 loc
= locators
[USBIFIFCF_CONFIGURATION
];
879 if (loc
!= USBIFIFCF_CONFIGURATION_DEFAULT
&&
880 loc
!= uiaa
.configno
)
882 loc
= locators
[USBIFIFCF_INTERFACE
];
883 if (loc
!= USBIFIFCF_INTERFACE
&& loc
!= uiaa
.ifaceno
)
886 dv
= config_found_sm_loc(parent
, "usbifif", ilocs
, &uiaa
,
887 usbd_ifprint
, config_stdsubmatch
);
890 ifaces
[i
] = 0; /* claim */
891 /* account for ifaces claimed by the driver behind our back */
892 for (j
= 0; j
< nifaces
; j
++) {
893 if (!ifaces
[j
] && !dev
->subdevs
[j
]) {
894 dev
->subdevs
[j
] = dv
;
895 dev
->nifaces_claimed
++;
901 return (USBD_NORMAL_COMPLETION
);
905 usbd_probe_and_attach(device_t parent
, usbd_device_handle dev
,
908 usb_device_descriptor_t
*dd
= &dev
->ddesc
;
912 /* First try with device specific drivers. */
913 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n"));
914 err
= usbd_attachwholedevice(parent
, dev
, port
, 0);
915 if (dev
->nifaces_claimed
|| err
)
917 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n"));
919 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n",
920 dd
->bNumConfigurations
));
921 for (confi
= 0; confi
< dd
->bNumConfigurations
; confi
++) {
922 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n",
924 err
= usbd_set_config_index(dev
, confi
, 1);
927 DPRINTF(("%s: port %d, set config at addr %d failed, "
928 "error=%s\n", device_xname(parent
), port
,
929 addr
, usbd_errstr(err
)));
931 printf("%s: port %d, set config at addr %d failed\n",
932 device_xname(parent
), port
, addr
);
936 nifaces
= dev
->cdesc
->bNumInterface
;
937 dev
->subdevs
= malloc(nifaces
* sizeof(device_t
), M_USB
,
939 if (dev
->subdevs
== NULL
)
941 dev
->subdevlen
= nifaces
;
943 err
= usbd_attachinterfaces(parent
, dev
, port
, NULL
);
945 if (!dev
->nifaces_claimed
) {
946 free(dev
->subdevs
, M_USB
);
950 if (dev
->nifaces_claimed
|| err
)
953 /* No interfaces were attached in any of the configurations. */
955 if (dd
->bNumConfigurations
> 1) /* don't change if only 1 config */
956 usbd_set_config_index(dev
, 0, 0);
958 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n"));
960 /* Finally try the generic driver. */
961 err
= usbd_attachwholedevice(parent
, dev
, port
, 1);
964 * The generic attach failed, but leave the device as it is.
965 * We just did not find any drivers, that's all. The device is
966 * fully operational and not harming anyone.
968 DPRINTF(("usbd_probe_and_attach: generic attach failed\n"));
969 return (USBD_NORMAL_COMPLETION
);
973 * Called from uhub_rescan(). usbd_new_device() for the target dev must be
974 * called before calling this.
977 usbd_reattach_device(device_t parent
, usbd_device_handle dev
,
978 int port
, const int *locators
)
982 if (locators
!= NULL
) {
983 loc
= locators
[USBIFIFCF_PORT
];
984 if (loc
!= USBIFIFCF_PORT_DEFAULT
&& loc
!= port
)
985 return USBD_NORMAL_COMPLETION
;
986 loc
= locators
[USBIFIFCF_VENDOR
];
987 if (loc
!= USBIFIFCF_VENDOR_DEFAULT
&&
988 loc
!= UGETW(dev
->ddesc
.idVendor
))
989 return USBD_NORMAL_COMPLETION
;
990 loc
= locators
[USBIFIFCF_PRODUCT
];
991 if (loc
!= USBIFIFCF_PRODUCT_DEFAULT
&&
992 loc
!= UGETW(dev
->ddesc
.idProduct
))
993 return USBD_NORMAL_COMPLETION
;
994 loc
= locators
[USBIFIFCF_RELEASE
];
995 if (loc
!= USBIFIFCF_RELEASE_DEFAULT
&&
996 loc
!= UGETW(dev
->ddesc
.bcdDevice
))
997 return USBD_NORMAL_COMPLETION
;
999 if (dev
->subdevlen
== 0) {
1000 /* XXX: check USBIFIFCF_CONFIGURATION and
1001 * USBIFIFCF_INTERFACE too */
1002 return usbd_probe_and_attach(parent
, dev
, port
, dev
->address
);
1003 } else if (dev
->subdevlen
!= dev
->cdesc
->bNumInterface
) {
1004 /* device-specific or generic driver is already attached. */
1005 return USBD_NORMAL_COMPLETION
;
1007 /* Does the device have unconfigured interfaces? */
1008 for (i
= 0; i
< dev
->subdevlen
; i
++) {
1009 if (dev
->subdevs
[i
] == NULL
) {
1013 if (i
>= dev
->subdevlen
)
1014 return USBD_NORMAL_COMPLETION
;
1015 return usbd_attachinterfaces(parent
, dev
, port
, locators
);
1019 * Called when a new device has been put in the powered state,
1020 * but not yet in the addressed state.
1021 * Get initial descriptor, set the address, get full descriptor,
1022 * and attach a driver.
1025 usbd_new_device(device_t parent
, usbd_bus_handle bus
, int depth
,
1026 int speed
, int port
, struct usbd_port
*up
)
1028 usbd_device_handle dev
, adev
;
1029 struct usbd_device
*hub
;
1030 usb_device_descriptor_t
*dd
;
1031 usb_port_status_t ps
;
1037 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n",
1038 bus
, port
, depth
, speed
));
1039 addr
= usbd_getnewaddr(bus
);
1041 printf("%s: No free USB addresses, new device ignored.\n",
1042 device_xname(bus
->usbctl
));
1043 return (USBD_NO_ADDR
);
1046 dev
= malloc(sizeof *dev
, M_USB
, M_NOWAIT
|M_ZERO
);
1048 return (USBD_NOMEM
);
1052 /* Set up default endpoint handle. */
1053 dev
->def_ep
.edesc
= &dev
->def_ep_desc
;
1055 /* Set up default endpoint descriptor. */
1056 dev
->def_ep_desc
.bLength
= USB_ENDPOINT_DESCRIPTOR_SIZE
;
1057 dev
->def_ep_desc
.bDescriptorType
= UDESC_ENDPOINT
;
1058 dev
->def_ep_desc
.bEndpointAddress
= USB_CONTROL_ENDPOINT
;
1059 dev
->def_ep_desc
.bmAttributes
= UE_CONTROL
;
1060 if (speed
== USB_SPEED_HIGH
)
1061 USETW(dev
->def_ep_desc
.wMaxPacketSize
, 64);
1063 USETW(dev
->def_ep_desc
.wMaxPacketSize
, USB_MAX_IPACKET
);
1064 dev
->def_ep_desc
.bInterval
= 0;
1066 dev
->quirks
= &usbd_no_quirk
;
1067 dev
->address
= USB_START_ADDR
;
1068 dev
->ddesc
.bMaxPacketSize
= 0;
1071 dev
->myhub
= up
->parent
;
1075 /* Locate port on upstream high speed hub */
1076 for (adev
= dev
, hub
= up
->parent
;
1077 hub
!= NULL
&& hub
->speed
!= USB_SPEED_HIGH
;
1078 adev
= hub
, hub
= hub
->myhub
)
1081 for (p
= 0; p
< hub
->hub
->hubdesc
.bNbrPorts
; p
++) {
1082 if (hub
->hub
->ports
[p
].device
== adev
) {
1083 dev
->myhsport
= &hub
->hub
->ports
[p
];
1087 panic("usbd_new_device: cannot find HS port\n");
1089 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p
));
1091 dev
->myhsport
= NULL
;
1094 dev
->langid
= USBD_NOLANG
;
1095 dev
->cookie
.cookie
= ++usb_cookie_no
;
1097 /* Establish the default pipe. */
1098 err
= usbd_setup_pipe(dev
, 0, &dev
->def_ep
, USBD_DEFAULT_INTERVAL
,
1099 &dev
->default_pipe
);
1101 usbd_remove_device(dev
, up
);
1106 /* Try a few times in case the device is slow (i.e. outside specs.) */
1107 for (i
= 0; i
< 10; i
++) {
1108 /* Get the first 8 bytes of the device descriptor. */
1109 err
= usbd_get_desc(dev
, UDESC_DEVICE
, 0,
1110 (speed
== USB_SPEED_HIGH
) ? USB_DEVICE_DESCRIPTOR_SIZE
1116 usbd_delay_ms(dev
, 200);
1118 usbd_reset_port(up
->parent
, port
, &ps
);
1121 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc "
1123 usbd_remove_device(dev
, up
);
1127 if (speed
== USB_SPEED_HIGH
) {
1128 /* Max packet size must be 64 (sec 5.5.3). */
1129 if (dd
->bMaxPacketSize
!= USB_2_MAX_CTRL_PACKET
) {
1131 printf("usbd_new_device: addr=%d bad max packet "
1132 "size=%d. adjusting to %d.\n",
1133 addr
, dd
->bMaxPacketSize
, USB_2_MAX_CTRL_PACKET
);
1135 dd
->bMaxPacketSize
= USB_2_MAX_CTRL_PACKET
;
1139 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, "
1140 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1141 addr
,UGETW(dd
->bcdUSB
), dd
->bDeviceClass
, dd
->bDeviceSubClass
,
1142 dd
->bDeviceProtocol
, dd
->bMaxPacketSize
, dd
->bLength
,
1145 if (dd
->bDescriptorType
!= UDESC_DEVICE
) {
1146 /* Illegal device descriptor */
1147 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n",
1148 dd
->bDescriptorType
));
1149 usbd_remove_device(dev
, up
);
1150 return (USBD_INVAL
);
1153 if (dd
->bLength
< USB_DEVICE_DESCRIPTOR_SIZE
) {
1154 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd
->bLength
));
1155 usbd_remove_device(dev
, up
);
1156 return (USBD_INVAL
);
1159 USETW(dev
->def_ep_desc
.wMaxPacketSize
, dd
->bMaxPacketSize
);
1161 err
= usbd_reload_device_desc(dev
);
1163 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc "
1165 usbd_remove_device(dev
, up
);
1169 /* Set the address */
1170 DPRINTFN(5, ("usbd_new_device: setting device address=%d\n", addr
));
1171 err
= usbd_set_address(dev
, addr
);
1173 DPRINTFN(-1, ("usbd_new_device: set address %d failed\n", addr
));
1174 err
= USBD_SET_ADDR_FAILED
;
1175 usbd_remove_device(dev
, up
);
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
;
1184 /* Re-establish the default pipe with the new address. */
1185 usbd_kill_pipe(dev
->default_pipe
);
1186 err
= usbd_setup_pipe(dev
, 0, &dev
->def_ep
, USBD_DEFAULT_INTERVAL
,
1187 &dev
->default_pipe
);
1189 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n"));
1190 usbd_remove_device(dev
, up
);
1194 /* Assume 100mA bus powered for now. Changed when configured. */
1195 dev
->power
= USB_MIN_POWER
;
1196 dev
->self_powered
= 0;
1198 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n",
1199 addr
, dev
, parent
));
1201 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH
, dev
);
1203 if (port
== 0) { /* root hub */
1205 usbd_attach_roothub(parent
, dev
);
1206 return (USBD_NORMAL_COMPLETION
);
1209 err
= usbd_probe_and_attach(parent
, dev
, port
, addr
);
1211 usbd_remove_device(dev
, up
);
1215 return (USBD_NORMAL_COMPLETION
);
1219 usbd_reload_device_desc(usbd_device_handle dev
)
1223 /* Get the full device descriptor. */
1224 err
= usbd_get_device_desc(dev
, &dev
->ddesc
);
1228 /* Figure out what's wrong with this device. */
1229 dev
->quirks
= usbd_find_quirk(&dev
->ddesc
);
1231 return (USBD_NORMAL_COMPLETION
);
1235 usbd_remove_device(usbd_device_handle dev
, struct usbd_port
*up
)
1237 DPRINTF(("usbd_remove_device: %p\n", dev
));
1239 if (dev
->default_pipe
!= NULL
)
1240 usbd_kill_pipe(dev
->default_pipe
);
1242 dev
->bus
->devices
[dev
->address
] = NULL
;
1248 usbd_print(void *aux
, const char *pnp
)
1250 struct usb_attach_arg
*uaa
= aux
;
1252 DPRINTFN(15, ("usbd_print dev=%p\n", uaa
->device
));
1254 #define USB_DEVINFO 1024
1256 if (!uaa
->usegeneric
)
1258 devinfo
= malloc(USB_DEVINFO
, M_TEMP
, M_WAITOK
);
1259 usbd_devinfo(uaa
->device
, 1, devinfo
, USB_DEVINFO
);
1260 aprint_normal("%s, %s", devinfo
, pnp
);
1261 free(devinfo
, M_TEMP
);
1263 aprint_normal(" port %d", uaa
->port
);
1266 * It gets very crowded with these locators on the attach line.
1267 * They are not really needed since they are printed in the clear
1270 if (uaa
->vendor
!= UHUB_UNK_VENDOR
)
1271 aprint_normal(" vendor 0x%04x", uaa
->vendor
);
1272 if (uaa
->product
!= UHUB_UNK_PRODUCT
)
1273 aprint_normal(" product 0x%04x", uaa
->product
);
1274 if (uaa
->release
!= UHUB_UNK_RELEASE
)
1275 aprint_normal(" release 0x%04x", uaa
->release
);
1281 usbd_ifprint(void *aux
, const char *pnp
)
1283 struct usbif_attach_arg
*uaa
= aux
;
1285 DPRINTFN(15, ("usbd_print dev=%p\n", uaa
->device
));
1288 aprint_normal(" port %d", uaa
->port
);
1289 aprint_normal(" configuration %d", uaa
->configno
);
1290 aprint_normal(" interface %d", uaa
->ifaceno
);
1293 * It gets very crowded with these locators on the attach line.
1294 * They are not really needed since they are printed in the clear
1297 if (uaa
->vendor
!= UHUB_UNK_VENDOR
)
1298 aprint_normal(" vendor 0x%04x", uaa
->vendor
);
1299 if (uaa
->product
!= UHUB_UNK_PRODUCT
)
1300 aprint_normal(" product 0x%04x", uaa
->product
);
1301 if (uaa
->release
!= UHUB_UNK_RELEASE
)
1302 aprint_normal(" release 0x%04x", uaa
->release
);
1308 usbd_fill_deviceinfo(usbd_device_handle dev
, struct usb_device_info
*di
,
1311 struct usbd_port
*p
;
1314 di
->udi_bus
= device_unit(dev
->bus
->usbctl
);
1315 di
->udi_addr
= dev
->address
;
1316 di
->udi_cookie
= dev
->cookie
;
1317 usbd_devinfo_vp(dev
, di
->udi_vendor
, di
->udi_product
, usedev
, 1);
1318 usbd_printBCD(di
->udi_release
, sizeof(di
->udi_release
),
1319 UGETW(dev
->ddesc
.bcdDevice
));
1320 di
->udi_serial
[0] = 0;
1322 (void)usbd_get_string(dev
, dev
->ddesc
.iSerialNumber
,
1324 di
->udi_vendorNo
= UGETW(dev
->ddesc
.idVendor
);
1325 di
->udi_productNo
= UGETW(dev
->ddesc
.idProduct
);
1326 di
->udi_releaseNo
= UGETW(dev
->ddesc
.bcdDevice
);
1327 di
->udi_class
= dev
->ddesc
.bDeviceClass
;
1328 di
->udi_subclass
= dev
->ddesc
.bDeviceSubClass
;
1329 di
->udi_protocol
= dev
->ddesc
.bDeviceProtocol
;
1330 di
->udi_config
= dev
->config
;
1331 di
->udi_power
= dev
->self_powered
? 0 : dev
->power
;
1332 di
->udi_speed
= dev
->speed
;
1334 if (dev
->subdevlen
> 0) {
1335 for (i
= 0, j
= 0; i
< dev
->subdevlen
&&
1336 j
< USB_MAX_DEVNAMES
; i
++) {
1337 if (!dev
->subdevs
[i
])
1339 strncpy(di
->udi_devnames
[j
],
1340 device_xname(dev
->subdevs
[i
]), USB_MAX_DEVNAMELEN
);
1341 di
->udi_devnames
[j
][USB_MAX_DEVNAMELEN
-1] = '\0';
1347 for (/* j is set */; j
< USB_MAX_DEVNAMES
; j
++)
1348 di
->udi_devnames
[j
][0] = 0; /* empty */
1352 i
< sizeof(di
->udi_ports
) / sizeof(di
->udi_ports
[0]) &&
1353 i
< dev
->hub
->hubdesc
.bNbrPorts
;
1355 p
= &dev
->hub
->ports
[i
];
1357 err
= p
->device
->address
;
1359 s
= UGETW(p
->status
.wPortStatus
);
1360 if (s
& UPS_PORT_ENABLED
)
1361 err
= USB_PORT_ENABLED
;
1362 else if (s
& UPS_SUSPEND
)
1363 err
= USB_PORT_SUSPENDED
;
1364 else if (s
& UPS_PORT_POWER
)
1365 err
= USB_PORT_POWERED
;
1367 err
= USB_PORT_DISABLED
;
1369 di
->udi_ports
[i
] = err
;
1371 di
->udi_nports
= dev
->hub
->hubdesc
.bNbrPorts
;
1378 usbd_fill_deviceinfo_old(usbd_device_handle dev
, struct usb_device_info_old
*di
,
1381 struct usbd_port
*p
;
1384 di
->udi_bus
= device_unit(dev
->bus
->usbctl
);
1385 di
->udi_addr
= dev
->address
;
1386 di
->udi_cookie
= dev
->cookie
;
1387 usbd_devinfo_vp(dev
, di
->udi_vendor
, di
->udi_product
, usedev
, 0);
1388 usbd_printBCD(di
->udi_release
, sizeof(di
->udi_release
),
1389 UGETW(dev
->ddesc
.bcdDevice
));
1390 di
->udi_vendorNo
= UGETW(dev
->ddesc
.idVendor
);
1391 di
->udi_productNo
= UGETW(dev
->ddesc
.idProduct
);
1392 di
->udi_releaseNo
= UGETW(dev
->ddesc
.bcdDevice
);
1393 di
->udi_class
= dev
->ddesc
.bDeviceClass
;
1394 di
->udi_subclass
= dev
->ddesc
.bDeviceSubClass
;
1395 di
->udi_protocol
= dev
->ddesc
.bDeviceProtocol
;
1396 di
->udi_config
= dev
->config
;
1397 di
->udi_power
= dev
->self_powered
? 0 : dev
->power
;
1398 di
->udi_speed
= dev
->speed
;
1400 if (dev
->subdevlen
> 0) {
1401 for (i
= 0, j
= 0; i
< dev
->subdevlen
&&
1402 j
< USB_MAX_DEVNAMES
; i
++) {
1403 if (!dev
->subdevs
[i
])
1405 strncpy(di
->udi_devnames
[j
],
1406 device_xname(dev
->subdevs
[i
]), USB_MAX_DEVNAMELEN
);
1407 di
->udi_devnames
[j
][USB_MAX_DEVNAMELEN
-1] = '\0';
1413 for (/* j is set */; j
< USB_MAX_DEVNAMES
; j
++)
1414 di
->udi_devnames
[j
][0] = 0; /* empty */
1418 i
< sizeof(di
->udi_ports
) / sizeof(di
->udi_ports
[0]) &&
1419 i
< dev
->hub
->hubdesc
.bNbrPorts
;
1421 p
= &dev
->hub
->ports
[i
];
1423 err
= p
->device
->address
;
1425 s
= UGETW(p
->status
.wPortStatus
);
1426 if (s
& UPS_PORT_ENABLED
)
1427 err
= USB_PORT_ENABLED
;
1428 else if (s
& UPS_SUSPEND
)
1429 err
= USB_PORT_SUSPENDED
;
1430 else if (s
& UPS_PORT_POWER
)
1431 err
= USB_PORT_POWERED
;
1433 err
= USB_PORT_DISABLED
;
1435 di
->udi_ports
[i
] = err
;
1437 di
->udi_nports
= dev
->hub
->hubdesc
.bNbrPorts
;
1445 usb_free_device(usbd_device_handle dev
)
1449 if (dev
->default_pipe
!= NULL
)
1450 usbd_kill_pipe(dev
->default_pipe
);
1451 if (dev
->ifaces
!= NULL
) {
1452 nifc
= dev
->cdesc
->bNumInterface
;
1453 for (ifcidx
= 0; ifcidx
< nifc
; ifcidx
++)
1454 usbd_free_iface_data(dev
, ifcidx
);
1455 free(dev
->ifaces
, M_USB
);
1457 if (dev
->cdesc
!= NULL
)
1458 free(dev
->cdesc
, M_USB
);
1459 if (dev
->subdevlen
> 0) {
1460 free(dev
->subdevs
, M_USB
);
1467 * The general mechanism for detaching drivers works as follows: Each
1468 * driver is responsible for maintaining a reference count on the
1469 * number of outstanding references to its softc (e.g. from
1470 * processing hanging in a read or write). The detach method of the
1471 * driver decrements this counter and flags in the softc that the
1472 * driver is dying and then wakes any sleepers. It then sleeps on the
1473 * softc. Each place that can sleep must maintain the reference
1474 * count. When the reference count drops to -1 (0 is the normal value
1475 * of the reference count) the a wakeup on the softc is performed
1476 * signaling to the detach waiter that all references are gone.
1480 * Called from process context when we discover that a port has
1481 * been disconnected.
1484 usb_disconnect_port(struct usbd_port
*up
, device_t parent
, int flags
)
1486 usbd_device_handle dev
= up
->device
;
1488 char subdevname
[16];
1489 const char *hubname
= device_xname(parent
);
1492 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
1493 up
, dev
, up
->portno
));
1497 printf("usb_disconnect_port: no device\n");
1502 if (dev
->subdevlen
> 0) {
1503 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n"));
1504 for (i
= 0; i
< dev
->subdevlen
; i
++) {
1505 if ((subdev
= dev
->subdevs
[i
]) == NULL
)
1507 strlcpy(subdevname
, device_xname(subdev
),
1508 sizeof(subdevname
));
1509 if ((rc
= config_detach(subdev
, flags
)) != 0)
1511 printf("%s: at %s", subdevname
, hubname
);
1512 if (up
->portno
!= 0)
1513 printf(" port %d", up
->portno
);
1514 printf(" (addr %d) disconnected\n", dev
->address
);
1516 KASSERT(!dev
->nifaces_claimed
);
1519 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH
, dev
);
1520 dev
->bus
->devices
[dev
->address
] = NULL
;
1522 usb_free_device(dev
);