3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
30 #include <sys/stdint.h>
31 #include <sys/stddef.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/condvar.h>
42 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usb_core.h>
53 #include <dev/usb/usb_debug.h>
54 #include <dev/usb/usb_process.h>
55 #include <dev/usb/usb_device.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_transfer.h>
60 #include <ddb/db_sym.h>
61 #endif /* USB_GLOBAL_INCLUDE_FILE */
64 * Define this unconditionally in case a kernel module is loaded that
65 * has been compiled with debugging options.
69 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
70 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RWTUN,
71 &usb_debug, 0, "Debug level");
75 * Sysctls to modify timings/delays
77 static SYSCTL_NODE(_hw_usb, OID_AUTO, timings, CTLFLAG_RW, 0, "Timings");
78 static int usb_timings_sysctl_handler(SYSCTL_HANDLER_ARGS);
80 SYSCTL_PROC(_hw_usb_timings, OID_AUTO, port_reset_delay, CTLTYPE_UINT | CTLFLAG_RWTUN,
81 &usb_port_reset_delay, sizeof(usb_port_reset_delay),
82 usb_timings_sysctl_handler, "IU", "Port Reset Delay");
83 SYSCTL_PROC(_hw_usb_timings, OID_AUTO, port_root_reset_delay, CTLTYPE_UINT | CTLFLAG_RWTUN,
84 &usb_port_root_reset_delay, sizeof(usb_port_root_reset_delay),
85 usb_timings_sysctl_handler, "IU", "Root Port Reset Delay");
86 SYSCTL_PROC(_hw_usb_timings, OID_AUTO, port_reset_recovery, CTLTYPE_UINT | CTLFLAG_RWTUN,
87 &usb_port_reset_recovery, sizeof(usb_port_reset_recovery),
88 usb_timings_sysctl_handler, "IU", "Port Reset Recovery");
89 SYSCTL_PROC(_hw_usb_timings, OID_AUTO, port_powerup_delay, CTLTYPE_UINT | CTLFLAG_RWTUN,
90 &usb_port_powerup_delay, sizeof(usb_port_powerup_delay),
91 usb_timings_sysctl_handler, "IU", "Port PowerUp Delay");
92 SYSCTL_PROC(_hw_usb_timings, OID_AUTO, port_resume_delay, CTLTYPE_UINT | CTLFLAG_RWTUN,
93 &usb_port_resume_delay, sizeof(usb_port_resume_delay),
94 usb_timings_sysctl_handler, "IU", "Port Resume Delay");
95 SYSCTL_PROC(_hw_usb_timings, OID_AUTO, set_address_settle, CTLTYPE_UINT | CTLFLAG_RWTUN,
96 &usb_set_address_settle, sizeof(usb_set_address_settle),
97 usb_timings_sysctl_handler, "IU", "Set Address Settle");
98 SYSCTL_PROC(_hw_usb_timings, OID_AUTO, resume_delay, CTLTYPE_UINT | CTLFLAG_RWTUN,
99 &usb_resume_delay, sizeof(usb_resume_delay),
100 usb_timings_sysctl_handler, "IU", "Resume Delay");
101 SYSCTL_PROC(_hw_usb_timings, OID_AUTO, resume_wait, CTLTYPE_UINT | CTLFLAG_RWTUN,
102 &usb_resume_wait, sizeof(usb_resume_wait),
103 usb_timings_sysctl_handler, "IU", "Resume Wait");
104 SYSCTL_PROC(_hw_usb_timings, OID_AUTO, resume_recovery, CTLTYPE_UINT | CTLFLAG_RWTUN,
105 &usb_resume_recovery, sizeof(usb_resume_recovery),
106 usb_timings_sysctl_handler, "IU", "Resume Recovery");
107 SYSCTL_PROC(_hw_usb_timings, OID_AUTO, extra_power_up_time, CTLTYPE_UINT | CTLFLAG_RWTUN,
108 &usb_extra_power_up_time, sizeof(usb_extra_power_up_time),
109 usb_timings_sysctl_handler, "IU", "Extra PowerUp Time");
112 /*------------------------------------------------------------------------*
115 * This function dumps information about an USB interface.
116 *------------------------------------------------------------------------*/
118 usb_dump_iface(struct usb_interface *iface)
120 printf("usb_dump_iface: iface=%p\n", iface);
124 printf(" iface=%p idesc=%p altindex=%d\n",
125 iface, iface->idesc, iface->alt_index);
128 /*------------------------------------------------------------------------*
131 * This function dumps information about an USB device.
132 *------------------------------------------------------------------------*/
134 usb_dump_device(struct usb_device *udev)
136 printf("usb_dump_device: dev=%p\n", udev);
141 " address=%d config=%d depth=%d speed=%d self_powered=%d\n"
142 " power=%d langid=%d\n",
144 udev->address, udev->curr_config_no, udev->depth, udev->speed,
145 udev->flags.self_powered, udev->power, udev->langid);
148 /*------------------------------------------------------------------------*
151 * This function dumps the USB transfer that are queued up on an USB endpoint.
152 *------------------------------------------------------------------------*/
154 usb_dump_queue(struct usb_endpoint *ep)
156 struct usb_xfer *xfer;
159 printf("usb_dump_queue: endpoint=%p xfer: ", ep);
160 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
161 TAILQ_FOREACH(xfer, &ep->endpoint_q[x].head, wait_entry)
167 /*------------------------------------------------------------------------*
170 * This function dumps information about an USB endpoint.
171 *------------------------------------------------------------------------*/
173 usb_dump_endpoint(struct usb_endpoint *ep)
176 printf("usb_dump_endpoint: endpoint=%p", ep);
178 printf(" edesc=%p isoc_next=%d toggle_next=%d",
179 ep->edesc, ep->isoc_next, ep->toggle_next);
182 printf(" bEndpointAddress=0x%02x",
183 ep->edesc->bEndpointAddress);
188 printf("usb_dump_endpoint: endpoint=NULL\n");
192 /*------------------------------------------------------------------------*
195 * This function dumps information about an USB transfer.
196 *------------------------------------------------------------------------*/
198 usb_dump_xfer(struct usb_xfer *xfer)
200 struct usb_device *udev;
201 printf("usb_dump_xfer: xfer=%p\n", xfer);
205 if (xfer->endpoint == NULL) {
206 printf("xfer %p: endpoint=NULL\n",
210 udev = xfer->xroot->udev;
211 printf("xfer %p: udev=%p vid=0x%04x pid=0x%04x addr=%d "
212 "endpoint=%p ep=0x%02x attr=0x%02x\n",
214 UGETW(udev->ddesc.idVendor),
215 UGETW(udev->ddesc.idProduct),
216 udev->address, xfer->endpoint,
217 xfer->endpoint->edesc->bEndpointAddress,
218 xfer->endpoint->edesc->bmAttributes);
222 unsigned int usb_port_reset_delay = USB_PORT_RESET_DELAY;
223 unsigned int usb_port_root_reset_delay = USB_PORT_ROOT_RESET_DELAY;
224 unsigned int usb_port_reset_recovery = USB_PORT_RESET_RECOVERY;
225 unsigned int usb_port_powerup_delay = USB_PORT_POWERUP_DELAY;
226 unsigned int usb_port_resume_delay = USB_PORT_RESUME_DELAY;
227 unsigned int usb_set_address_settle = USB_SET_ADDRESS_SETTLE;
228 unsigned int usb_resume_delay = USB_RESUME_DELAY;
229 unsigned int usb_resume_wait = USB_RESUME_WAIT;
230 unsigned int usb_resume_recovery = USB_RESUME_RECOVERY;
231 unsigned int usb_extra_power_up_time = USB_EXTRA_POWER_UP_TIME;
233 /*------------------------------------------------------------------------*
234 * usb_timings_sysctl_handler
236 * This function updates timings variables, adjusting them where necessary.
237 *------------------------------------------------------------------------*/
238 static int usb_timings_sysctl_handler(SYSCTL_HANDLER_ARGS)
244 * Attempt to get a coherent snapshot by making a copy of the data.
247 val = *(unsigned int *)arg1;
250 error = SYSCTL_OUT(req, &val, sizeof(int));
251 if (error || !req->newptr)
257 error = SYSCTL_IN(req, &val, sizeof(unsigned int));
262 * Now make sure the values are decent, and certainly no lower than
263 * what the USB spec prescribes.
265 unsigned int *p = (unsigned int *)arg1;
266 if (p == &usb_port_reset_delay) {
267 if (val < USB_PORT_RESET_DELAY_SPEC)
269 } else if (p == &usb_port_root_reset_delay) {
270 if (val < USB_PORT_ROOT_RESET_DELAY_SPEC)
272 } else if (p == &usb_port_reset_recovery) {
273 if (val < USB_PORT_RESET_RECOVERY_SPEC)
275 } else if (p == &usb_port_powerup_delay) {
276 if (val < USB_PORT_POWERUP_DELAY_SPEC)
278 } else if (p == &usb_port_resume_delay) {
279 if (val < USB_PORT_RESUME_DELAY_SPEC)
281 } else if (p == &usb_set_address_settle) {
282 if (val < USB_SET_ADDRESS_SETTLE_SPEC)
284 } else if (p == &usb_resume_delay) {
285 if (val < USB_RESUME_DELAY_SPEC)
287 } else if (p == &usb_resume_wait) {
288 if (val < USB_RESUME_WAIT_SPEC)
290 } else if (p == &usb_resume_recovery) {
291 if (val < USB_RESUME_RECOVERY_SPEC)
293 } else if (p == &usb_extra_power_up_time) {
294 if (val < USB_EXTRA_POWER_UP_TIME_SPEC)