xref: /openbsd-src/usr.sbin/usbdevs/usbdevs.c (revision cd1eb269cafb12c415be1749cd4a4b5422710415)
1 /*	$OpenBSD: usbdevs.c,v 1.18 2010/04/02 05:46:21 ckuethe 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 getstring(int, int, int, char *, int);
52 void usbdump(int f);
53 void dumpone(char *name, int f, int addr);
54 int main(int, char **);
55 
56 extern char *__progname;
57 
58 void
59 usage(void)
60 {
61 	fprintf(stderr, "usage: %s [-dv] [-a addr] [-f dev]\n", __progname);
62 	exit(1);
63 }
64 
65 char done[USB_MAX_DEVICES];
66 int indent;
67 
68 void
69 usbdev(int f, int a, int rec)
70 {
71 	struct usb_device_info di;
72 	usb_device_descriptor_t dd;
73 	char serialnum[USB_MAX_STRING_LEN];
74 	struct usb_ctl_request req;
75 	usb_string_descriptor_t us;
76 	int e, p, i;
77 	int langid = 0;
78 
79 	di.udi_addr = a;
80 	e = ioctl(f, USB_DEVICEINFO, &di);
81 	if (e) {
82 		if (errno != ENXIO)
83 			printf("addr %d: I/O error\n", a);
84 		return;
85 	}
86 
87 	req.ucr_addr = a;
88 	req.ucr_request.bmRequestType = UT_READ_DEVICE;
89 	req.ucr_request.bRequest = UR_GET_DESCRIPTOR;
90 	req.ucr_data = &us;
91 	USETW2(req.ucr_request.wValue, UDESC_STRING, 0);
92 	USETW(req.ucr_request.wIndex, 0);
93 	USETW(req.ucr_request.wLength, 4);
94 	req.ucr_flags = 0;
95 	if (ioctl(f, USB_REQUEST, &req) >= 0)
96 		langid = UGETW(us.bString[0]);
97 
98 	bzero(serialnum, sizeof serialnum);
99 	if (getdevicedesc(f, a, &dd))
100 		getstring(f, a, dd.iSerialNumber, serialnum, langid);
101 
102 	printf("addr %d: ", a);
103 	done[a] = 1;
104 	if (verbose) {
105 		switch (di.udi_speed) {
106 		case USB_SPEED_LOW:
107 			printf("low speed, ");
108 			break;
109 		case USB_SPEED_FULL:
110 			printf("full speed, ");
111 			break;
112 		case USB_SPEED_HIGH:
113 			printf("high speed, ");
114 			break;
115 		default:
116 			break;
117 		}
118 
119 		if (di.udi_power)
120 			printf("power %d mA, ", di.udi_power);
121 		else
122 			printf("self powered, ");
123 		if (di.udi_config)
124 			printf("config %d, ", di.udi_config);
125 		else
126 			printf("unconfigured, ");
127 	}
128 	if (verbose) {
129 		printf("%s(0x%04x), %s(0x%04x), rev %s",
130 		    di.udi_product, di.udi_productNo,
131 		    di.udi_vendor, di.udi_vendorNo, di.udi_release);
132 		if (strlen(serialnum))
133 			printf(", iSerialNumber %s", serialnum);
134 	} else
135 		printf("%s, %s", di.udi_product, di.udi_vendor);
136 	printf("\n");
137 	if (showdevs) {
138 		for (i = 0; i < USB_MAX_DEVNAMES; i++)
139 			if (di.udi_devnames[i][0])
140 				printf("%*s  %s\n", indent, "",
141 				    di.udi_devnames[i]);
142 	}
143 	if (!rec)
144 		return;
145 	for (p = 0; p < di.udi_nports; p++) {
146 		int s = di.udi_ports[p];
147 
148 		if (s >= USB_MAX_DEVICES) {
149 			if (verbose) {
150 				printf("%*sport %d %s\n", indent+1, "", p+1,
151 				    s == USB_PORT_ENABLED ? "enabled" :
152 				    s == USB_PORT_SUSPENDED ? "suspended" :
153 				    s == USB_PORT_POWERED ? "powered" :
154 				    s == USB_PORT_DISABLED ? "disabled" :
155 				    "???");
156 			}
157 			continue;
158 		}
159 		indent++;
160 		printf("%*s", indent, "");
161 		if (verbose)
162 			printf("port %d ", p+1);
163 		if (s == 0)
164 			printf("addr 0 should never happen!\n");
165 		else
166 			usbdev(f, s, 1);
167 		indent--;
168 	}
169 }
170 
171 int
172 getdevicedesc(int f, int addr, usb_device_descriptor_t *d)
173 {
174 	struct usb_ctl_request req;
175 	int r;
176 
177 	req.ucr_addr = addr;
178 	req.ucr_request.bmRequestType = UT_READ_DEVICE;
179 	req.ucr_request.bRequest = UR_GET_DESCRIPTOR;
180 	USETW2(req.ucr_request.wValue, UDESC_DEVICE, 0);
181 	USETW(req.ucr_request.wIndex, 0);
182 	USETW(req.ucr_request.wLength, USB_DEVICE_DESCRIPTOR_SIZE);
183 	req.ucr_data = d;
184 	req.ucr_flags = 0;
185 	if ((r = ioctl(f, USB_REQUEST, &req)) == -1)
186 		perror("getdevicedesc: ioctl");
187 	return (r != -1);
188 }
189 
190 void
191 getstring(int f, int addr, int si, char *s, int langid)
192 {
193 	struct usb_ctl_request req;
194 	usb_string_descriptor_t us;
195 	int r, i, n;
196 	u_int16_t c;
197 
198 	if (si == 0) {
199 		*s = 0;
200 		return;
201 	}
202 	req.ucr_addr = addr;
203 	req.ucr_request.bmRequestType = UT_READ_DEVICE;
204 	req.ucr_request.bRequest = UR_GET_DESCRIPTOR;
205 	req.ucr_data = &us;
206 	USETW2(req.ucr_request.wValue, UDESC_STRING, si);
207 	USETW(req.ucr_request.wIndex, langid);
208 	USETW(req.ucr_request.wLength, 2);
209 	req.ucr_flags = USBD_SHORT_XFER_OK;
210 
211 	if (ioctl(f, USB_REQUEST, &req) == -1){
212 		perror("getstring: ioctl");
213 		*s = 0;
214 		return;
215 	}
216 
217 	USETW(req.ucr_request.wLength, us.bLength);
218 
219 	if (ioctl(f, USB_REQUEST, &req) == -1){
220 		perror("getstring: ioctl");
221 		*s = 0;
222 		return;
223 	}
224 
225 	n = us.bLength / 2 - 1;
226 	for (i = 0; i < n; i++) {
227 		c = UGETW(us.bString[i]);
228 		if ((c & 0xff00) == 0)
229 			*s++ = c;
230 		else if ((c & 0x00ff) == 0)
231 			*s++ = c >> 8;
232 		else {
233 			snprintf(s, 6, "\\u%04x", c);
234 			s += 6;
235 		}
236 	}
237 	*s++ = 0;
238 }
239 
240 void
241 usbdump(int f)
242 {
243 	int a;
244 
245 	for (a = 1; a < USB_MAX_DEVICES; a++) {
246 		if (!done[a])
247 			usbdev(f, a, 1);
248 	}
249 }
250 
251 void
252 dumpone(char *name, int f, int addr)
253 {
254 	if (verbose)
255 		printf("Controller %s:\n", name);
256 	indent = 0;
257 	memset(done, 0, sizeof done);
258 	if (addr)
259 		usbdev(f, addr, 0);
260 	else
261 		usbdump(f);
262 }
263 
264 int
265 main(int argc, char **argv)
266 {
267 	int ch, i, f;
268 	char buf[50];
269 	char *dev = 0;
270 	int addr = 0;
271 	int ncont;
272 
273 	while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
274 		switch (ch) {
275 		case 'a':
276 			addr = atoi(optarg);
277 			break;
278 		case 'd':
279 			showdevs++;
280 			break;
281 		case 'f':
282 			dev = optarg;
283 			break;
284 		case 'v':
285 			verbose = 1;
286 			break;
287 		default:
288 			usage();
289 		}
290 	}
291 	argc -= optind;
292 	argv += optind;
293 
294 	if (dev == 0) {
295 		for (ncont = 0, i = 0; i < 10; i++) {
296 			snprintf(buf, sizeof buf, "%s%d", USBDEV, i);
297 			f = open(buf, O_RDWR);
298 			if (f >= 0) {
299 				dumpone(buf, f, addr);
300 				close(f);
301 			} else {
302 				if (errno == ENOENT || errno == ENXIO)
303 					continue;
304 				warn("%s", buf);
305 			}
306 			ncont++;
307 		}
308 		if (verbose && ncont == 0)
309 			printf("%s: no USB controllers found\n",
310 			    __progname);
311 	} else {
312 		f = open(dev, O_RDWR);
313 		if (f >= 0)
314 			dumpone(dev, f, addr);
315 		else
316 			err(1, "%s", dev);
317 	}
318 	exit(0);
319 }
320