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