xref: /freebsd/sys/dev/usb/usb_debug.c (revision a5cf1aaaffd3716d6246b5214216c89e278bfce6)
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 
27ed6d949aSAndrew Thompson #include <sys/stdint.h>
28ed6d949aSAndrew Thompson #include <sys/stddef.h>
29ed6d949aSAndrew Thompson #include <sys/param.h>
30ed6d949aSAndrew Thompson #include <sys/queue.h>
31ed6d949aSAndrew Thompson #include <sys/types.h>
32ed6d949aSAndrew Thompson #include <sys/systm.h>
33ed6d949aSAndrew Thompson #include <sys/kernel.h>
34ed6d949aSAndrew Thompson #include <sys/bus.h>
35ed6d949aSAndrew Thompson #include <sys/module.h>
36ed6d949aSAndrew Thompson #include <sys/lock.h>
37ed6d949aSAndrew Thompson #include <sys/mutex.h>
38ed6d949aSAndrew Thompson #include <sys/condvar.h>
39ed6d949aSAndrew Thompson #include <sys/sysctl.h>
40ed6d949aSAndrew Thompson #include <sys/sx.h>
41ed6d949aSAndrew Thompson #include <sys/unistd.h>
42ed6d949aSAndrew Thompson #include <sys/callout.h>
43ed6d949aSAndrew Thompson #include <sys/malloc.h>
44ed6d949aSAndrew Thompson #include <sys/priv.h>
45ed6d949aSAndrew Thompson 
4602ac6454SAndrew Thompson #include <dev/usb/usb.h>
47ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
4802ac6454SAndrew Thompson 
4902ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5002ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5102ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5202ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
5302ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
5402ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
5502ac6454SAndrew Thompson 
56ed6d949aSAndrew Thompson #include <ddb/ddb.h>
57ed6d949aSAndrew Thompson #include <ddb/db_sym.h>
58ed6d949aSAndrew Thompson 
5902ac6454SAndrew Thompson /*
6002ac6454SAndrew Thompson  * Define this unconditionally in case a kernel module is loaded that
6102ac6454SAndrew Thompson  * has been compiled with debugging options.
6202ac6454SAndrew Thompson  */
63a593f6b8SAndrew Thompson int	usb_debug = 0;
6402ac6454SAndrew Thompson 
659360ae40SAndrew Thompson SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
669360ae40SAndrew Thompson SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
67a593f6b8SAndrew Thompson     &usb_debug, 0, "Debug level");
6802ac6454SAndrew Thompson 
69c13fd8d4SAndrew Thompson TUNABLE_INT("hw.usb.debug", &usb_debug);
70c13fd8d4SAndrew Thompson 
7102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
72a593f6b8SAndrew Thompson  *	usb_dump_iface
7302ac6454SAndrew Thompson  *
7402ac6454SAndrew Thompson  * This function dumps information about an USB interface.
7502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
7602ac6454SAndrew Thompson void
77a593f6b8SAndrew Thompson usb_dump_iface(struct usb_interface *iface)
7802ac6454SAndrew Thompson {
79a593f6b8SAndrew Thompson 	printf("usb_dump_iface: iface=%p\n", iface);
8002ac6454SAndrew Thompson 	if (iface == NULL) {
8102ac6454SAndrew Thompson 		return;
8202ac6454SAndrew Thompson 	}
8302ac6454SAndrew Thompson 	printf(" iface=%p idesc=%p altindex=%d\n",
8402ac6454SAndrew Thompson 	    iface, iface->idesc, iface->alt_index);
8502ac6454SAndrew Thompson }
8602ac6454SAndrew Thompson 
8702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
88a593f6b8SAndrew Thompson  *	usb_dump_device
8902ac6454SAndrew Thompson  *
9002ac6454SAndrew Thompson  * This function dumps information about an USB device.
9102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
9202ac6454SAndrew Thompson void
93a593f6b8SAndrew Thompson usb_dump_device(struct usb_device *udev)
9402ac6454SAndrew Thompson {
95a593f6b8SAndrew Thompson 	printf("usb_dump_device: dev=%p\n", udev);
9602ac6454SAndrew Thompson 	if (udev == NULL) {
9702ac6454SAndrew Thompson 		return;
9802ac6454SAndrew Thompson 	}
9902ac6454SAndrew Thompson 	printf(" bus=%p \n"
10002ac6454SAndrew Thompson 	    " address=%d config=%d depth=%d speed=%d self_powered=%d\n"
10102ac6454SAndrew Thompson 	    " power=%d langid=%d\n",
10202ac6454SAndrew Thompson 	    udev->bus,
10302ac6454SAndrew Thompson 	    udev->address, udev->curr_config_no, udev->depth, udev->speed,
10402ac6454SAndrew Thompson 	    udev->flags.self_powered, udev->power, udev->langid);
10502ac6454SAndrew Thompson }
10602ac6454SAndrew Thompson 
10702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
108a593f6b8SAndrew Thompson  *	usb_dump_queue
10902ac6454SAndrew Thompson  *
110ae60fdfbSAndrew Thompson  * This function dumps the USB transfer that are queued up on an USB endpoint.
11102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
11202ac6454SAndrew Thompson void
113a593f6b8SAndrew Thompson usb_dump_queue(struct usb_endpoint *ep)
11402ac6454SAndrew Thompson {
115760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
116*a5cf1aaaSHans Petter Selasky 	usb_stream_t x;
11702ac6454SAndrew Thompson 
118a593f6b8SAndrew Thompson 	printf("usb_dump_queue: endpoint=%p xfer: ", ep);
119*a5cf1aaaSHans Petter Selasky 	for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
120*a5cf1aaaSHans Petter Selasky 		TAILQ_FOREACH(xfer, &ep->endpoint_q[x].head, wait_entry)
12102ac6454SAndrew Thompson 			printf(" %p", xfer);
12202ac6454SAndrew Thompson 	}
12302ac6454SAndrew Thompson 	printf("\n");
12402ac6454SAndrew Thompson }
12502ac6454SAndrew Thompson 
12602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
127a593f6b8SAndrew Thompson  *	usb_dump_endpoint
12802ac6454SAndrew Thompson  *
129ae60fdfbSAndrew Thompson  * This function dumps information about an USB endpoint.
13002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
13102ac6454SAndrew Thompson void
132a593f6b8SAndrew Thompson usb_dump_endpoint(struct usb_endpoint *ep)
13302ac6454SAndrew Thompson {
134ae60fdfbSAndrew Thompson 	if (ep) {
135a593f6b8SAndrew Thompson 		printf("usb_dump_endpoint: endpoint=%p", ep);
13602ac6454SAndrew Thompson 
13702ac6454SAndrew Thompson 		printf(" edesc=%p isoc_next=%d toggle_next=%d",
138ae60fdfbSAndrew Thompson 		    ep->edesc, ep->isoc_next, ep->toggle_next);
13902ac6454SAndrew Thompson 
140ae60fdfbSAndrew Thompson 		if (ep->edesc) {
14102ac6454SAndrew Thompson 			printf(" bEndpointAddress=0x%02x",
142ae60fdfbSAndrew Thompson 			    ep->edesc->bEndpointAddress);
14302ac6454SAndrew Thompson 		}
14402ac6454SAndrew Thompson 		printf("\n");
145a593f6b8SAndrew Thompson 		usb_dump_queue(ep);
14602ac6454SAndrew Thompson 	} else {
147a593f6b8SAndrew Thompson 		printf("usb_dump_endpoint: endpoint=NULL\n");
14802ac6454SAndrew Thompson 	}
14902ac6454SAndrew Thompson }
15002ac6454SAndrew Thompson 
15102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
152a593f6b8SAndrew Thompson  *	usb_dump_xfer
15302ac6454SAndrew Thompson  *
15402ac6454SAndrew Thompson  * This function dumps information about an USB transfer.
15502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
15602ac6454SAndrew Thompson void
157a593f6b8SAndrew Thompson usb_dump_xfer(struct usb_xfer *xfer)
15802ac6454SAndrew Thompson {
159760bc48eSAndrew Thompson 	struct usb_device *udev;
160a593f6b8SAndrew Thompson 	printf("usb_dump_xfer: xfer=%p\n", xfer);
16102ac6454SAndrew Thompson 	if (xfer == NULL) {
16202ac6454SAndrew Thompson 		return;
16302ac6454SAndrew Thompson 	}
164ae60fdfbSAndrew Thompson 	if (xfer->endpoint == NULL) {
165ae60fdfbSAndrew Thompson 		printf("xfer %p: endpoint=NULL\n",
16602ac6454SAndrew Thompson 		    xfer);
16702ac6454SAndrew Thompson 		return;
16802ac6454SAndrew Thompson 	}
16902ac6454SAndrew Thompson 	udev = xfer->xroot->udev;
17002ac6454SAndrew Thompson 	printf("xfer %p: udev=%p vid=0x%04x pid=0x%04x addr=%d "
171ae60fdfbSAndrew Thompson 	    "endpoint=%p ep=0x%02x attr=0x%02x\n",
17202ac6454SAndrew Thompson 	    xfer, udev,
17302ac6454SAndrew Thompson 	    UGETW(udev->ddesc.idVendor),
17402ac6454SAndrew Thompson 	    UGETW(udev->ddesc.idProduct),
175ae60fdfbSAndrew Thompson 	    udev->address, xfer->endpoint,
176ae60fdfbSAndrew Thompson 	    xfer->endpoint->edesc->bEndpointAddress,
177ae60fdfbSAndrew Thompson 	    xfer->endpoint->edesc->bmAttributes);
17802ac6454SAndrew Thompson }
179