xref: /netbsd-src/usr.sbin/usbdevs/usbdevs.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: usbdevs.c,v 1.17 2001/02/19 23:22:48 cgd 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 	fprintf(stderr, "Usage: %s [-a addr] [-d] [-f dev] [-v]\n",
64 	    getprogname());
65 	exit(1);
66 }
67 
68 char done[USB_MAX_DEVICES];
69 int indent;
70 
71 void
72 usbdev(int f, int a, int rec)
73 {
74 	struct usb_device_info di;
75 	int e, p, i;
76 
77 	di.addr = a;
78 	e = ioctl(f, USB_DEVICEINFO, &di);
79 	if (e) {
80 		if (errno != ENXIO)
81 			printf("addr %d: I/O error\n", a);
82 		return;
83 	}
84 	printf("addr %d: ", a);
85 	done[a] = 1;
86 	if (verbose) {
87 		if (di.lowspeed)
88 			printf("low speed, ");
89 		if (di.power)
90 			printf("power %d mA, ", di.power);
91 		else
92 			printf("self powered, ");
93 		if (di.config)
94 			printf("config %d, ", di.config);
95 		else
96 			printf("unconfigured, ");
97 	}
98 	if (verbose) {
99 		printf("%s(0x%04x), %s(0x%04x), rev %s",
100 		       di.product, di.productNo,
101 		       di.vendor, di.vendorNo, di.release);
102 	} else
103 		printf("%s, %s", di.product, di.vendor);
104 	printf("\n");
105 	if (showdevs) {
106 		for (i = 0; i < USB_MAX_DEVNAMES; i++)
107 			if (di.devnames[i][0])
108 				printf("%*s  %s\n", indent, "",
109 				       di.devnames[i]);
110 	}
111 	if (!rec)
112 		return;
113 	for (p = 0; p < di.nports; p++) {
114 		int s = di.ports[p];
115 		if (s >= USB_MAX_DEVICES) {
116 			if (verbose) {
117 				printf("%*sport %d %s\n", indent+1, "", p+1,
118 				       s == USB_PORT_ENABLED ? "enabled" :
119 				       s == USB_PORT_SUSPENDED ? "suspended" :
120 				       s == USB_PORT_POWERED ? "powered" :
121 				       s == USB_PORT_DISABLED ? "disabled" :
122 				       "???");
123 
124 			}
125 			continue;
126 		}
127 		indent++;
128 		printf("%*s", indent, "");
129 		if (verbose)
130 			printf("port %d ", p+1);
131 		if (s == 0)
132 			printf("addr 0 should never happen!\n");
133 		else
134 			usbdev(f, s, 1);
135 		indent--;
136 	}
137 }
138 
139 void
140 usbdump(int f)
141 {
142 	int a;
143 
144 	for (a = 1; a < USB_MAX_DEVICES; a++) {
145 		if (!done[a])
146 			usbdev(f, a, 1);
147 	}
148 }
149 
150 void
151 dumpone(char *name, int f, int addr)
152 {
153 	if (verbose)
154 		printf("Controller %s:\n", name);
155 	indent = 0;
156 	memset(done, 0, sizeof done);
157 	if (addr)
158 		usbdev(f, addr, 0);
159 	else
160 		usbdump(f);
161 }
162 
163 int
164 main(int argc, char **argv)
165 {
166 	int ch, i, f;
167 	char buf[50];
168 	char *dev = 0;
169 	int addr = 0;
170 	int ncont;
171 
172 	while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
173 		switch(ch) {
174 		case 'a':
175 			addr = atoi(optarg);
176 			break;
177 		case 'd':
178 			showdevs++;
179 			break;
180 		case 'f':
181 			dev = optarg;
182 			break;
183 		case 'v':
184 			verbose = 1;
185 			break;
186 		case '?':
187 		default:
188 			usage();
189 		}
190 	}
191 	argc -= optind;
192 	argv += optind;
193 
194 	if (dev == 0) {
195 		for (ncont = 0, i = 0; i < 10; i++) {
196 			sprintf(buf, "%s%d", USBDEV, i);
197 			f = open(buf, O_RDONLY);
198 			if (f >= 0) {
199 				dumpone(buf, f, addr);
200 				close(f);
201 			} else {
202 				if (errno == ENOENT || errno == ENXIO)
203 					continue;
204 				warn("%s", buf);
205 			}
206 			ncont++;
207 		}
208 		if (verbose && ncont == 0)
209 			printf("%s: no USB controllers found\n",
210 			    getprogname());
211 	} else {
212 		f = open(dev, O_RDONLY);
213 		if (f >= 0)
214 			dumpone(dev, f, addr);
215 		else
216 			err(1, "%s", dev);
217 	}
218 	exit(0);
219 }
220