usbdevs.c revision 1.14 1 /* $NetBSD: usbdevs.c,v 1.14 2000/12/30 13:50:43 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 (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(void);
54 void usbdev(int f, int a, int rec);
55 void usbdump(int f);
56 void dumpone(char *name, int f, int addr);
57 int main(int, char **);
58
59 extern char *__progname;
60
61 void
62 usage()
63 {
64 fprintf(stderr, "Usage: %s [-v] [-a addr] [-f dev]\n", __progname);
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;
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 (!rec)
106 return;
107 for (p = 0; p < di.nports; p++) {
108 int s = di.ports[p];
109 if (s >= USB_MAX_DEVICES) {
110 if (verbose) {
111 printf("%*sport %d %s\n", indent+1, "", p+1,
112 s == USB_PORT_ENABLED ? "enabled" :
113 s == USB_PORT_SUSPENDED ? "suspended" :
114 s == USB_PORT_POWERED ? "powered" :
115 s == USB_PORT_DISABLED ? "disabled" :
116 "???");
117
118 }
119 continue;
120 }
121 indent++;
122 printf("%*s", indent, "");
123 if (verbose)
124 printf("port %d ", p+1);
125 if (s == 0)
126 printf("addr 0 should never happen!\n");
127 else
128 usbdev(f, s, 1);
129 indent--;
130 }
131 }
132
133 void
134 usbdump(int f)
135 {
136 int a;
137
138 for (a = 1; a < USB_MAX_DEVICES; a++) {
139 if (!done[a])
140 usbdev(f, a, 1);
141 }
142 }
143
144 void
145 dumpone(char *name, int f, int addr)
146 {
147 if (verbose)
148 printf("Controller %s:\n", name);
149 indent = 0;
150 memset(done, 0, sizeof done);
151 if (addr)
152 usbdev(f, addr, 0);
153 else
154 usbdump(f);
155 }
156
157 int
158 main(int argc, char **argv)
159 {
160 int ch, i, f;
161 char buf[50];
162 char *dev = 0;
163 int addr = 0;
164 int ncont;
165
166 while ((ch = getopt(argc, argv, "a:f:v")) != -1) {
167 switch(ch) {
168 case 'a':
169 addr = atoi(optarg);
170 break;
171 case 'f':
172 dev = optarg;
173 break;
174 case 'v':
175 verbose = 1;
176 break;
177 case '?':
178 default:
179 usage();
180 }
181 }
182 argc -= optind;
183 argv += optind;
184
185 if (dev == 0) {
186 for (ncont = 0, i = 0; i < 10; i++) {
187 sprintf(buf, "%s%d", USBDEV, i);
188 f = open(buf, O_RDONLY);
189 if (f >= 0) {
190 dumpone(buf, f, addr);
191 close(f);
192 } else {
193 if (errno == ENOENT || errno == ENXIO)
194 continue;
195 warn("%s", buf);
196 }
197 ncont++;
198 }
199 if (verbose && ncont == 0)
200 printf("%s: no USB controllers found\n", __progname);
201 } else {
202 f = open(dev, O_RDONLY);
203 if (f >= 0)
204 dumpone(dev, f, addr);
205 else
206 err(1, "%s", dev);
207 }
208 exit(0);
209 }
210