xref: /openbsd/usr.sbin/usbdevs/usbdevs.c (revision 891d7ab6)
1 /*	$OpenBSD: usbdevs.c,v 1.20 2011/01/16 00:04:47 jakemsr Exp $	*/
2 /*	$NetBSD: usbdevs.c,v 1.19 2002/02/21 00:34:31 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (augustss@netbsd.org).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <dev/usb/usb.h>
42 
43 #define USBDEV "/dev/usb"
44 
45 int verbose = 0;
46 int showdevs = 0;
47 
48 void usage(void);
49 void usbdev(int f, int a, int rec);
50 int getdevicedesc(int, int, usb_device_descriptor_t *);
51 void usbdump(int f);
52 void dumpone(char *name, int f, int addr);
53 int main(int, char **);
54 
55 extern char *__progname;
56 
57 void
58 usage(void)
59 {
60 	fprintf(stderr, "usage: %s [-dv] [-a addr] [-f dev]\n", __progname);
61 	exit(1);
62 }
63 
64 char done[USB_MAX_DEVICES];
65 int indent;
66 
67 void
68 usbdev(int f, int a, int rec)
69 {
70 	struct usb_device_info di;
71 	int e, p, i;
72 
73 	di.udi_addr = a;
74 	e = ioctl(f, USB_DEVICEINFO, &di);
75 	if (e) {
76 		if (errno != ENXIO)
77 			printf("addr %d: I/O error\n", a);
78 		return;
79 	}
80 
81 	printf("addr %d: ", a);
82 	done[a] = 1;
83 	if (verbose) {
84 		switch (di.udi_speed) {
85 		case USB_SPEED_LOW:
86 			printf("low speed, ");
87 			break;
88 		case USB_SPEED_FULL:
89 			printf("full speed, ");
90 			break;
91 		case USB_SPEED_HIGH:
92 			printf("high speed, ");
93 			break;
94 		default:
95 			break;
96 		}
97 
98 		if (di.udi_power)
99 			printf("power %d mA, ", di.udi_power);
100 		else
101 			printf("self powered, ");
102 		if (di.udi_config)
103 			printf("config %d, ", di.udi_config);
104 		else
105 			printf("unconfigured, ");
106 	}
107 	if (verbose) {
108 		printf("%s(0x%04x), %s(0x%04x), rev %s",
109 		    di.udi_product, di.udi_productNo,
110 		    di.udi_vendor, di.udi_vendorNo, di.udi_release);
111 		if (strlen(di.udi_serial))
112 			printf(", iSerialNumber %s", di.udi_serial);
113 	} else
114 		printf("%s, %s", di.udi_product, di.udi_vendor);
115 	printf("\n");
116 	if (showdevs) {
117 		for (i = 0; i < USB_MAX_DEVNAMES; i++)
118 			if (di.udi_devnames[i][0])
119 				printf("%*s  %s\n", indent, "",
120 				    di.udi_devnames[i]);
121 	}
122 	if (!rec)
123 		return;
124 	for (p = 0; p < di.udi_nports; p++) {
125 		int s = di.udi_ports[p];
126 
127 		if (s >= USB_MAX_DEVICES) {
128 			if (verbose) {
129 				printf("%*sport %d %s\n", indent+1, "", p+1,
130 				    s == USB_PORT_ENABLED ? "enabled" :
131 				    s == USB_PORT_SUSPENDED ? "suspended" :
132 				    s == USB_PORT_POWERED ? "powered" :
133 				    s == USB_PORT_DISABLED ? "disabled" :
134 				    "???");
135 			}
136 			continue;
137 		}
138 		indent++;
139 		printf("%*s", indent, "");
140 		if (verbose)
141 			printf("port %d ", p+1);
142 		if (s == 0)
143 			printf("addr 0 should never happen!\n");
144 		else
145 			usbdev(f, s, 1);
146 		indent--;
147 	}
148 }
149 
150 int
151 getdevicedesc(int f, int addr, usb_device_descriptor_t *d)
152 {
153 	struct usb_ctl_request req;
154 	int r;
155 
156 	req.ucr_addr = addr;
157 	req.ucr_request.bmRequestType = UT_READ_DEVICE;
158 	req.ucr_request.bRequest = UR_GET_DESCRIPTOR;
159 	USETW2(req.ucr_request.wValue, UDESC_DEVICE, 0);
160 	USETW(req.ucr_request.wIndex, 0);
161 	USETW(req.ucr_request.wLength, USB_DEVICE_DESCRIPTOR_SIZE);
162 	req.ucr_data = d;
163 	req.ucr_flags = 0;
164 	if ((r = ioctl(f, USB_REQUEST, &req)) == -1)
165 		perror("getdevicedesc: ioctl");
166 	return (r != -1);
167 }
168 
169 void
170 usbdump(int f)
171 {
172 	int a;
173 
174 	for (a = 1; a < USB_MAX_DEVICES; a++) {
175 		if (!done[a])
176 			usbdev(f, a, 1);
177 	}
178 }
179 
180 void
181 dumpone(char *name, int f, int addr)
182 {
183 	if (verbose)
184 		printf("Controller %s:\n", name);
185 	indent = 0;
186 	memset(done, 0, sizeof done);
187 	if (addr)
188 		usbdev(f, addr, 0);
189 	else
190 		usbdump(f);
191 }
192 
193 int
194 main(int argc, char **argv)
195 {
196 	int ch, i, f;
197 	char buf[50];
198 	char *dev = 0;
199 	const char *errstr;
200 	int addr = 0;
201 	int ncont;
202 
203 	while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
204 		switch (ch) {
205 		case 'a':
206 			addr = strtonum(optarg, 1, USB_MAX_DEVICES, &errstr);
207 			if (errstr)
208 				errx(1, "addr %s", errstr);
209 			break;
210 		case 'd':
211 			showdevs++;
212 			break;
213 		case 'f':
214 			dev = optarg;
215 			break;
216 		case 'v':
217 			verbose = 1;
218 			break;
219 		default:
220 			usage();
221 		}
222 	}
223 	argc -= optind;
224 	argv += optind;
225 
226 	if (dev == 0) {
227 		for (ncont = 0, i = 0; i < 10; i++) {
228 			snprintf(buf, sizeof buf, "%s%d", USBDEV, i);
229 			f = open(buf, O_RDONLY);
230 			if (f >= 0) {
231 				dumpone(buf, f, addr);
232 				close(f);
233 			} else {
234 				if (errno == ENOENT || errno == ENXIO)
235 					continue;
236 				warn("%s", buf);
237 			}
238 			ncont++;
239 		}
240 		if (verbose && ncont == 0)
241 			printf("%s: no USB controllers found\n",
242 			    __progname);
243 	} else {
244 		f = open(dev, O_RDONLY);
245 		if (f >= 0)
246 			dumpone(dev, f, addr);
247 		else
248 			err(1, "%s", dev);
249 	}
250 	exit(0);
251 }
252