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