usbdevs.c revision 1.12 1 /* $NetBSD: usbdevs.c,v 1.12 2000/04/14 06:26:54 simonb 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 (at) 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;
52
53 void usage __P((void));
54 void usbdev __P((int f, int a, int rec));
55 void usbdump __P((int f));
56 void dumpone __P((char *name, int f, int addr));
57 int main __P((int, char **));
58
59 extern char *__progname;
60
61 void
62 usage()
63 {
64 fprintf(stderr, "Usage: %s [-a addr] [-f dev] [-v]\n", __progname);
65 exit(1);
66 }
67
68 char done[USB_MAX_DEVICES];
69 int indent;
70
71 void
72 usbdev(f, a, rec)
73 int f;
74 int a;
75 int rec;
76 {
77 struct usb_device_info di;
78 int e, p;
79
80 di.addr = a;
81 e = ioctl(f, USB_DEVICEINFO, &di);
82 if (e) {
83 if (errno != ENXIO)
84 printf("addr %d: I/O error\n", a);
85 return;
86 }
87 printf("addr %d: ", a);
88 done[a] = 1;
89 if (verbose) {
90 if (di.lowspeed)
91 printf("low speed, ");
92 if (di.power)
93 printf("power %d mA, ", di.power);
94 else
95 printf("self powered, ");
96 if (di.config)
97 printf("config %d, ", di.config);
98 else
99 printf("unconfigured, ");
100 }
101 if (verbose) {
102 printf("%s(0x%04x), %s(0x%04x), rev %s",
103 di.product, di.productNo,
104 di.vendor, di.vendorNo, di.release);
105 } else
106 printf("%s, %s", di.product, di.vendor);
107 printf("\n");
108 if (!rec)
109 return;
110 for (p = 0; p < di.nports; p++) {
111 int s = di.ports[p];
112 if (s >= USB_MAX_DEVICES) {
113 if (verbose) {
114 printf("%*sport %d %s\n", indent+1, "", p+1,
115 s == USB_PORT_ENABLED ? "enabled" :
116 s == USB_PORT_SUSPENDED ? "suspended" :
117 s == USB_PORT_POWERED ? "powered" :
118 s == USB_PORT_DISABLED ? "disabled" :
119 "???");
120
121 }
122 continue;
123 }
124 indent++;
125 printf("%*s", indent, "");
126 if (verbose)
127 printf("port %d ", p+1);
128 if (s == 0)
129 printf("addr 0 should never happen!\n");
130 else
131 usbdev(f, s, 1);
132 indent--;
133 }
134 }
135
136 void
137 usbdump(f)
138 int f;
139 {
140 int a;
141
142 for (a = 1; a < USB_MAX_DEVICES; a++) {
143 if (!done[a])
144 usbdev(f, a, 1);
145 }
146 }
147
148 void
149 dumpone(name, f, addr)
150 char *name;
151 int f;
152 int addr;
153 {
154 if (verbose)
155 printf("Controller %s:\n", name);
156 indent = 0;
157 memset(done, 0, sizeof done);
158 if (addr)
159 usbdev(f, addr, 0);
160 else
161 usbdump(f);
162 }
163
164 int
165 main(argc, argv)
166 int argc;
167 char **argv;
168 {
169 int ch, i, f;
170 char buf[50];
171 char *dev = 0;
172 int addr = 0;
173 int ncont;
174
175 while ((ch = getopt(argc, argv, "a:f:v")) != -1) {
176 switch(ch) {
177 case 'a':
178 addr = atoi(optarg);
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", __progname);
210 } else {
211 f = open(dev, O_RDONLY);
212 if (f >= 0)
213 dumpone(dev, f, addr);
214 else
215 err(1, "%s", dev);
216 }
217 exit(0);
218 }
219