102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 302ac6454SAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 402ac6454SAndrew Thompson * 502ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 602ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 702ac6454SAndrew Thompson * are met: 802ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 902ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1002ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1202ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1302ac6454SAndrew Thompson * 1402ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1502ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1602ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1702ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1802ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1902ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2002ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2102ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2202ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2302ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2402ac6454SAndrew Thompson * SUCH DAMAGE. 2502ac6454SAndrew Thompson */ 2602ac6454SAndrew Thompson 27d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 28d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 29d2b99310SHans Petter Selasky #else 30ed6d949aSAndrew Thompson #include <sys/stdint.h> 31ed6d949aSAndrew Thompson #include <sys/stddef.h> 32ed6d949aSAndrew Thompson #include <sys/param.h> 33ed6d949aSAndrew Thompson #include <sys/queue.h> 34ed6d949aSAndrew Thompson #include <sys/types.h> 35ed6d949aSAndrew Thompson #include <sys/systm.h> 36ed6d949aSAndrew Thompson #include <sys/kernel.h> 37ed6d949aSAndrew Thompson #include <sys/bus.h> 38ed6d949aSAndrew Thompson #include <sys/module.h> 39ed6d949aSAndrew Thompson #include <sys/lock.h> 40ed6d949aSAndrew Thompson #include <sys/mutex.h> 41ed6d949aSAndrew Thompson #include <sys/condvar.h> 42ed6d949aSAndrew Thompson #include <sys/sysctl.h> 43ed6d949aSAndrew Thompson #include <sys/sx.h> 44ed6d949aSAndrew Thompson #include <sys/unistd.h> 45ed6d949aSAndrew Thompson #include <sys/callout.h> 46ed6d949aSAndrew Thompson #include <sys/malloc.h> 47ed6d949aSAndrew Thompson #include <sys/priv.h> 48ed6d949aSAndrew Thompson 4902ac6454SAndrew Thompson #include <dev/usb/usb.h> 50ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 5102ac6454SAndrew Thompson 5202ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 5302ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 5402ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 5502ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 5602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 5702ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h> 5802ac6454SAndrew Thompson 59ed6d949aSAndrew Thompson #include <ddb/ddb.h> 60ed6d949aSAndrew Thompson #include <ddb/db_sym.h> 61d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */ 62ed6d949aSAndrew Thompson 6302ac6454SAndrew Thompson /* 6402ac6454SAndrew Thompson * Define this unconditionally in case a kernel module is loaded that 6502ac6454SAndrew Thompson * has been compiled with debugging options. 6602ac6454SAndrew Thompson */ 67a593f6b8SAndrew Thompson int usb_debug = 0; 6802ac6454SAndrew Thompson 699360ae40SAndrew Thompson SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging"); 70*af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RWTUN, 71a593f6b8SAndrew Thompson &usb_debug, 0, "Debug level"); 72c13fd8d4SAndrew Thompson 7337506412SHans Petter Selasky #ifdef USB_DEBUG 7437506412SHans Petter Selasky /* 7537506412SHans Petter Selasky * Sysctls to modify timings/delays 7637506412SHans Petter Selasky */ 7737506412SHans Petter Selasky static SYSCTL_NODE(_hw_usb, OID_AUTO, timings, CTLFLAG_RW, 0, "Timings"); 7837506412SHans Petter Selasky static int usb_timings_sysctl_handler(SYSCTL_HANDLER_ARGS); 7937506412SHans Petter Selasky 80*af3b2549SHans Petter Selasky SYSCTL_PROC(_hw_usb_timings, OID_AUTO, port_reset_delay, CTLTYPE_UINT | CTLFLAG_RWTUN, 8137506412SHans Petter Selasky &usb_port_reset_delay, sizeof(usb_port_reset_delay), 8237506412SHans Petter Selasky usb_timings_sysctl_handler, "IU", "Port Reset Delay"); 83*af3b2549SHans Petter Selasky SYSCTL_PROC(_hw_usb_timings, OID_AUTO, port_root_reset_delay, CTLTYPE_UINT | CTLFLAG_RWTUN, 8437506412SHans Petter Selasky &usb_port_root_reset_delay, sizeof(usb_port_root_reset_delay), 8537506412SHans Petter Selasky usb_timings_sysctl_handler, "IU", "Root Port Reset Delay"); 86*af3b2549SHans Petter Selasky SYSCTL_PROC(_hw_usb_timings, OID_AUTO, port_reset_recovery, CTLTYPE_UINT | CTLFLAG_RWTUN, 8737506412SHans Petter Selasky &usb_port_reset_recovery, sizeof(usb_port_reset_recovery), 8837506412SHans Petter Selasky usb_timings_sysctl_handler, "IU", "Port Reset Recovery"); 89*af3b2549SHans Petter Selasky SYSCTL_PROC(_hw_usb_timings, OID_AUTO, port_powerup_delay, CTLTYPE_UINT | CTLFLAG_RWTUN, 9037506412SHans Petter Selasky &usb_port_powerup_delay, sizeof(usb_port_powerup_delay), 9137506412SHans Petter Selasky usb_timings_sysctl_handler, "IU", "Port PowerUp Delay"); 92*af3b2549SHans Petter Selasky SYSCTL_PROC(_hw_usb_timings, OID_AUTO, port_resume_delay, CTLTYPE_UINT | CTLFLAG_RWTUN, 9337506412SHans Petter Selasky &usb_port_resume_delay, sizeof(usb_port_resume_delay), 9437506412SHans Petter Selasky usb_timings_sysctl_handler, "IU", "Port Resume Delay"); 95*af3b2549SHans Petter Selasky SYSCTL_PROC(_hw_usb_timings, OID_AUTO, set_address_settle, CTLTYPE_UINT | CTLFLAG_RWTUN, 9637506412SHans Petter Selasky &usb_set_address_settle, sizeof(usb_set_address_settle), 9737506412SHans Petter Selasky usb_timings_sysctl_handler, "IU", "Set Address Settle"); 98*af3b2549SHans Petter Selasky SYSCTL_PROC(_hw_usb_timings, OID_AUTO, resume_delay, CTLTYPE_UINT | CTLFLAG_RWTUN, 9937506412SHans Petter Selasky &usb_resume_delay, sizeof(usb_resume_delay), 10037506412SHans Petter Selasky usb_timings_sysctl_handler, "IU", "Resume Delay"); 101*af3b2549SHans Petter Selasky SYSCTL_PROC(_hw_usb_timings, OID_AUTO, resume_wait, CTLTYPE_UINT | CTLFLAG_RWTUN, 10237506412SHans Petter Selasky &usb_resume_wait, sizeof(usb_resume_wait), 10337506412SHans Petter Selasky usb_timings_sysctl_handler, "IU", "Resume Wait"); 104*af3b2549SHans Petter Selasky SYSCTL_PROC(_hw_usb_timings, OID_AUTO, resume_recovery, CTLTYPE_UINT | CTLFLAG_RWTUN, 10537506412SHans Petter Selasky &usb_resume_recovery, sizeof(usb_resume_recovery), 10637506412SHans Petter Selasky usb_timings_sysctl_handler, "IU", "Resume Recovery"); 107*af3b2549SHans Petter Selasky SYSCTL_PROC(_hw_usb_timings, OID_AUTO, extra_power_up_time, CTLTYPE_UINT | CTLFLAG_RWTUN, 10837506412SHans Petter Selasky &usb_extra_power_up_time, sizeof(usb_extra_power_up_time), 10937506412SHans Petter Selasky usb_timings_sysctl_handler, "IU", "Extra PowerUp Time"); 11037506412SHans Petter Selasky #endif 11137506412SHans Petter Selasky 11202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 113a593f6b8SAndrew Thompson * usb_dump_iface 11402ac6454SAndrew Thompson * 11502ac6454SAndrew Thompson * This function dumps information about an USB interface. 11602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 11702ac6454SAndrew Thompson void 118a593f6b8SAndrew Thompson usb_dump_iface(struct usb_interface *iface) 11902ac6454SAndrew Thompson { 120a593f6b8SAndrew Thompson printf("usb_dump_iface: iface=%p\n", iface); 12102ac6454SAndrew Thompson if (iface == NULL) { 12202ac6454SAndrew Thompson return; 12302ac6454SAndrew Thompson } 12402ac6454SAndrew Thompson printf(" iface=%p idesc=%p altindex=%d\n", 12502ac6454SAndrew Thompson iface, iface->idesc, iface->alt_index); 12602ac6454SAndrew Thompson } 12702ac6454SAndrew Thompson 12802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 129a593f6b8SAndrew Thompson * usb_dump_device 13002ac6454SAndrew Thompson * 13102ac6454SAndrew Thompson * This function dumps information about an USB device. 13202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 13302ac6454SAndrew Thompson void 134a593f6b8SAndrew Thompson usb_dump_device(struct usb_device *udev) 13502ac6454SAndrew Thompson { 136a593f6b8SAndrew Thompson printf("usb_dump_device: dev=%p\n", udev); 13702ac6454SAndrew Thompson if (udev == NULL) { 13802ac6454SAndrew Thompson return; 13902ac6454SAndrew Thompson } 14002ac6454SAndrew Thompson printf(" bus=%p \n" 14102ac6454SAndrew Thompson " address=%d config=%d depth=%d speed=%d self_powered=%d\n" 14202ac6454SAndrew Thompson " power=%d langid=%d\n", 14302ac6454SAndrew Thompson udev->bus, 14402ac6454SAndrew Thompson udev->address, udev->curr_config_no, udev->depth, udev->speed, 14502ac6454SAndrew Thompson udev->flags.self_powered, udev->power, udev->langid); 14602ac6454SAndrew Thompson } 14702ac6454SAndrew Thompson 14802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 149a593f6b8SAndrew Thompson * usb_dump_queue 15002ac6454SAndrew Thompson * 151ae60fdfbSAndrew Thompson * This function dumps the USB transfer that are queued up on an USB endpoint. 15202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 15302ac6454SAndrew Thompson void 154a593f6b8SAndrew Thompson usb_dump_queue(struct usb_endpoint *ep) 15502ac6454SAndrew Thompson { 156760bc48eSAndrew Thompson struct usb_xfer *xfer; 157a5cf1aaaSHans Petter Selasky usb_stream_t x; 15802ac6454SAndrew Thompson 159a593f6b8SAndrew Thompson printf("usb_dump_queue: endpoint=%p xfer: ", ep); 160a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 161a5cf1aaaSHans Petter Selasky TAILQ_FOREACH(xfer, &ep->endpoint_q[x].head, wait_entry) 16202ac6454SAndrew Thompson printf(" %p", xfer); 16302ac6454SAndrew Thompson } 16402ac6454SAndrew Thompson printf("\n"); 16502ac6454SAndrew Thompson } 16602ac6454SAndrew Thompson 16702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 168a593f6b8SAndrew Thompson * usb_dump_endpoint 16902ac6454SAndrew Thompson * 170ae60fdfbSAndrew Thompson * This function dumps information about an USB endpoint. 17102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 17202ac6454SAndrew Thompson void 173a593f6b8SAndrew Thompson usb_dump_endpoint(struct usb_endpoint *ep) 17402ac6454SAndrew Thompson { 175ae60fdfbSAndrew Thompson if (ep) { 176a593f6b8SAndrew Thompson printf("usb_dump_endpoint: endpoint=%p", ep); 17702ac6454SAndrew Thompson 17802ac6454SAndrew Thompson printf(" edesc=%p isoc_next=%d toggle_next=%d", 179ae60fdfbSAndrew Thompson ep->edesc, ep->isoc_next, ep->toggle_next); 18002ac6454SAndrew Thompson 181ae60fdfbSAndrew Thompson if (ep->edesc) { 18202ac6454SAndrew Thompson printf(" bEndpointAddress=0x%02x", 183ae60fdfbSAndrew Thompson ep->edesc->bEndpointAddress); 18402ac6454SAndrew Thompson } 18502ac6454SAndrew Thompson printf("\n"); 186a593f6b8SAndrew Thompson usb_dump_queue(ep); 18702ac6454SAndrew Thompson } else { 188a593f6b8SAndrew Thompson printf("usb_dump_endpoint: endpoint=NULL\n"); 18902ac6454SAndrew Thompson } 19002ac6454SAndrew Thompson } 19102ac6454SAndrew Thompson 19202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 193a593f6b8SAndrew Thompson * usb_dump_xfer 19402ac6454SAndrew Thompson * 19502ac6454SAndrew Thompson * This function dumps information about an USB transfer. 19602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 19702ac6454SAndrew Thompson void 198a593f6b8SAndrew Thompson usb_dump_xfer(struct usb_xfer *xfer) 19902ac6454SAndrew Thompson { 200760bc48eSAndrew Thompson struct usb_device *udev; 201a593f6b8SAndrew Thompson printf("usb_dump_xfer: xfer=%p\n", xfer); 20202ac6454SAndrew Thompson if (xfer == NULL) { 20302ac6454SAndrew Thompson return; 20402ac6454SAndrew Thompson } 205ae60fdfbSAndrew Thompson if (xfer->endpoint == NULL) { 206ae60fdfbSAndrew Thompson printf("xfer %p: endpoint=NULL\n", 20702ac6454SAndrew Thompson xfer); 20802ac6454SAndrew Thompson return; 20902ac6454SAndrew Thompson } 21002ac6454SAndrew Thompson udev = xfer->xroot->udev; 21102ac6454SAndrew Thompson printf("xfer %p: udev=%p vid=0x%04x pid=0x%04x addr=%d " 212ae60fdfbSAndrew Thompson "endpoint=%p ep=0x%02x attr=0x%02x\n", 21302ac6454SAndrew Thompson xfer, udev, 21402ac6454SAndrew Thompson UGETW(udev->ddesc.idVendor), 21502ac6454SAndrew Thompson UGETW(udev->ddesc.idProduct), 216ae60fdfbSAndrew Thompson udev->address, xfer->endpoint, 217ae60fdfbSAndrew Thompson xfer->endpoint->edesc->bEndpointAddress, 218ae60fdfbSAndrew Thompson xfer->endpoint->edesc->bmAttributes); 21902ac6454SAndrew Thompson } 22037506412SHans Petter Selasky 22137506412SHans Petter Selasky #ifdef USB_DEBUG 22237506412SHans Petter Selasky unsigned int usb_port_reset_delay = USB_PORT_RESET_DELAY; 22337506412SHans Petter Selasky unsigned int usb_port_root_reset_delay = USB_PORT_ROOT_RESET_DELAY; 22437506412SHans Petter Selasky unsigned int usb_port_reset_recovery = USB_PORT_RESET_RECOVERY; 22537506412SHans Petter Selasky unsigned int usb_port_powerup_delay = USB_PORT_POWERUP_DELAY; 22637506412SHans Petter Selasky unsigned int usb_port_resume_delay = USB_PORT_RESUME_DELAY; 22737506412SHans Petter Selasky unsigned int usb_set_address_settle = USB_SET_ADDRESS_SETTLE; 22837506412SHans Petter Selasky unsigned int usb_resume_delay = USB_RESUME_DELAY; 22937506412SHans Petter Selasky unsigned int usb_resume_wait = USB_RESUME_WAIT; 23037506412SHans Petter Selasky unsigned int usb_resume_recovery = USB_RESUME_RECOVERY; 23137506412SHans Petter Selasky unsigned int usb_extra_power_up_time = USB_EXTRA_POWER_UP_TIME; 23237506412SHans Petter Selasky 23337506412SHans Petter Selasky /*------------------------------------------------------------------------* 23437506412SHans Petter Selasky * usb_timings_sysctl_handler 23537506412SHans Petter Selasky * 23637506412SHans Petter Selasky * This function updates timings variables, adjusting them where necessary. 23737506412SHans Petter Selasky *------------------------------------------------------------------------*/ 23837506412SHans Petter Selasky static int usb_timings_sysctl_handler(SYSCTL_HANDLER_ARGS) 23937506412SHans Petter Selasky { 24037506412SHans Petter Selasky int error = 0; 24137506412SHans Petter Selasky unsigned int val; 24237506412SHans Petter Selasky 24337506412SHans Petter Selasky /* 24437506412SHans Petter Selasky * Attempt to get a coherent snapshot by making a copy of the data. 24537506412SHans Petter Selasky */ 24637506412SHans Petter Selasky if (arg1) 24737506412SHans Petter Selasky val = *(unsigned int *)arg1; 24837506412SHans Petter Selasky else 24937506412SHans Petter Selasky val = arg2; 25037506412SHans Petter Selasky error = SYSCTL_OUT(req, &val, sizeof(int)); 25137506412SHans Petter Selasky if (error || !req->newptr) 25237506412SHans Petter Selasky return (error); 25337506412SHans Petter Selasky 25437506412SHans Petter Selasky if (!arg1) 25537506412SHans Petter Selasky return EPERM; 25637506412SHans Petter Selasky 25737506412SHans Petter Selasky error = SYSCTL_IN(req, &val, sizeof(unsigned int)); 25837506412SHans Petter Selasky if (error) 25937506412SHans Petter Selasky return (error); 26037506412SHans Petter Selasky 26137506412SHans Petter Selasky /* 26237506412SHans Petter Selasky * Now make sure the values are decent, and certainly no lower than 26337506412SHans Petter Selasky * what the USB spec prescribes. 26437506412SHans Petter Selasky */ 26537506412SHans Petter Selasky unsigned int *p = (unsigned int *)arg1; 26637506412SHans Petter Selasky if (p == &usb_port_reset_delay) { 26737506412SHans Petter Selasky if (val < USB_PORT_RESET_DELAY_SPEC) 26837506412SHans Petter Selasky return (EINVAL); 26937506412SHans Petter Selasky } else if (p == &usb_port_root_reset_delay) { 27037506412SHans Petter Selasky if (val < USB_PORT_ROOT_RESET_DELAY_SPEC) 27137506412SHans Petter Selasky return (EINVAL); 27237506412SHans Petter Selasky } else if (p == &usb_port_reset_recovery) { 27337506412SHans Petter Selasky if (val < USB_PORT_RESET_RECOVERY_SPEC) 27437506412SHans Petter Selasky return (EINVAL); 27537506412SHans Petter Selasky } else if (p == &usb_port_powerup_delay) { 27637506412SHans Petter Selasky if (val < USB_PORT_POWERUP_DELAY_SPEC) 27737506412SHans Petter Selasky return (EINVAL); 27837506412SHans Petter Selasky } else if (p == &usb_port_resume_delay) { 27937506412SHans Petter Selasky if (val < USB_PORT_RESUME_DELAY_SPEC) 28037506412SHans Petter Selasky return (EINVAL); 28137506412SHans Petter Selasky } else if (p == &usb_set_address_settle) { 28237506412SHans Petter Selasky if (val < USB_SET_ADDRESS_SETTLE_SPEC) 28337506412SHans Petter Selasky return (EINVAL); 28437506412SHans Petter Selasky } else if (p == &usb_resume_delay) { 28537506412SHans Petter Selasky if (val < USB_RESUME_DELAY_SPEC) 28637506412SHans Petter Selasky return (EINVAL); 28737506412SHans Petter Selasky } else if (p == &usb_resume_wait) { 28837506412SHans Petter Selasky if (val < USB_RESUME_WAIT_SPEC) 28937506412SHans Petter Selasky return (EINVAL); 29037506412SHans Petter Selasky } else if (p == &usb_resume_recovery) { 29137506412SHans Petter Selasky if (val < USB_RESUME_RECOVERY_SPEC) 29237506412SHans Petter Selasky return (EINVAL); 29337506412SHans Petter Selasky } else if (p == &usb_extra_power_up_time) { 29437506412SHans Petter Selasky if (val < USB_EXTRA_POWER_UP_TIME_SPEC) 29537506412SHans Petter Selasky return (EINVAL); 29637506412SHans Petter Selasky } else { 29737506412SHans Petter Selasky /* noop */ 29837506412SHans Petter Selasky } 29937506412SHans Petter Selasky 30037506412SHans Petter Selasky *p = val; 30137506412SHans Petter Selasky return 0; 30237506412SHans Petter Selasky } 30337506412SHans Petter Selasky #endif 304