xref: /openbsd-src/usr.sbin/usbdevs/usbdevs.c (revision 8445c53715e7030056b779e8ab40efb7820981f2)
1 /*	$OpenBSD: usbdevs.c,v 1.2 2001/09/17 17:29:56 mickey Exp $	*/
2 /*	$NetBSD: usbdevs.c,v 1.11 1999/09/08 02:39:36 augustss 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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <dev/usb/usb.h>
49 
50 #define USBDEV "/dev/usb"
51 
52 int verbose;
53 int showdevs;
54 
55 void usage __P((void));
56 void usbdev __P((int f, int a, int rec));
57 void usbdump __P((int f));
58 void dumpone __P((char *name, int f, int addr));
59 int main __P((int, char **));
60 
61 extern char *__progname;
62 
63 void
64 usage()
65 {
66 	fprintf(stderr, "Usage: %s [-a addr] [-d] [-f dev] [-v]\n",
67 	    __progname);
68 	exit(1);
69 }
70 
71 char done[USB_MAX_DEVICES];
72 int indent;
73 
74 void
75 usbdev(f, a, rec)
76 	int f;
77 	int a;
78 	int rec;
79 {
80 	struct usb_device_info di;
81 	int e, p, i;
82 
83 	di.addr = a;
84 	e = ioctl(f, USB_DEVICEINFO, &di);
85 	if (e) {
86 		if (errno != ENXIO)
87 			printf("addr %d: I/O error\n", a);
88 		return;
89 	}
90 	printf("addr %d: ", a);
91 	done[a] = 1;
92 	if (verbose) {
93 		if (di.lowspeed)
94 			printf("low speed, ");
95 		if (di.power)
96 			printf("power %d mA, ", di.power);
97 		else
98 			printf("self powered, ");
99 		if (di.config)
100 			printf("config %d, ", di.config);
101 		else
102 			printf("unconfigured, ");
103 	}
104 	if (verbose) {
105 		printf("%s(0x%04x), %s(0x%04x), rev %s",
106 		       di.product, di.productNo,
107 		       di.vendor, di.vendorNo, di.release);
108 	} else
109 		printf("%s, %s", di.product, di.vendor);
110 	printf("\n");
111 	if (showdevs) {
112 		for (i = 0; i< USB_MAX_DEVNAMES; i++)
113 			if (di.devnames[i][0])
114 				printf("%*s %s\n", indent, "",
115 				    di.devnames[i]);
116 	}
117 	if (!rec)
118 		return;
119 	for (p = 0; p < di.nports; p++) {
120 		int s = di.ports[p];
121 		if (s >= USB_MAX_DEVICES) {
122 			if (verbose) {
123 				printf("%*sport %d %s\n", indent+1, "", p+1,
124 				       s == USB_PORT_ENABLED ? "enabled" :
125 				       s == USB_PORT_SUSPENDED ? "suspended" :
126 				       s == USB_PORT_POWERED ? "powered" :
127 				       s == USB_PORT_DISABLED ? "disabled" :
128 				       "???");
129 
130 			}
131 			continue;
132 		}
133 		indent++;
134 		printf("%*s", indent, "");
135 		if (verbose)
136 			printf("port %d ", p+1);
137 		if (s == 0)
138 			printf("addr 0 should never happen!\n");
139 		else
140 			usbdev(f, s, 1);
141 		indent--;
142 	}
143 }
144 
145 void
146 usbdump(f)
147 	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(name, f, addr)
159 	char *name;
160 	int f;
161 	int addr;
162 {
163 	if (verbose)
164 		printf("Controller %s:\n", name);
165 	indent = 0;
166 	memset(done, 0, sizeof done);
167 	if (addr)
168 		usbdev(f, addr, 0);
169 	else
170 		usbdump(f);
171 }
172 
173 int
174 main(argc, argv)
175 	int argc;
176 	char **argv;
177 {
178 	int ch, i, f;
179 	char buf[50];
180 	extern int optind;
181 	extern char *optarg;
182 	char *dev = 0;
183 	int addr = 0;
184 	int ncont;
185 
186 	while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
187 		switch(ch) {
188 		case 'a':
189 			addr = atoi(optarg);
190 			break;
191 		case 'd':
192 			showdevs++;
193 			break;
194 		case 'f':
195 			dev = optarg;
196 			break;
197 		case 'v':
198 			verbose = 1;
199 			break;
200 		case '?':
201 		default:
202 			usage();
203 		}
204 	}
205 	argc -= optind;
206 	argv += optind;
207 
208 	if (dev == 0) {
209 		for (ncont = 0, i = 0; i < 10; i++) {
210 			sprintf(buf, "%s%d", USBDEV, i);
211 			f = open(buf, O_RDONLY);
212 			if (f >= 0) {
213 				dumpone(buf, f, addr);
214 				close(f);
215 			} else {
216 				if (errno == ENOENT || errno == ENXIO)
217 					continue;
218 				warn("%s", buf);
219 			}
220 			ncont++;
221 		}
222 		if (verbose && ncont == 0)
223 			printf("%s: no USB controllers found\n", __progname);
224 	} else {
225 		f = open(dev, O_RDONLY);
226 		if (f >= 0)
227 			dumpone(dev, f, addr);
228 		else
229 			err(1, "%s", dev);
230 	}
231 	exit(0);
232 }
233