usbdevs.c revision 1.8 1 /* $NetBSD: usbdevs.c,v 1.8 1999/05/16 17:18:41 tron 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 return;
84 done[a] = 1;
85 printf("addr %d: ", di.addr);
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.revision);
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 usbdev(f, di.ports[p], 1);
126 indent--;
127 }
128 }
129
130 void
131 usbdump(f)
132 int f;
133 {
134 int a;
135
136 for (a = 1; a < USB_MAX_DEVICES; a++) {
137 if (!done[a])
138 usbdev(f, a, 1);
139 }
140 }
141
142 void
143 dumpone(name, f, addr)
144 char *name;
145 int f;
146 int addr;
147 {
148 if (verbose)
149 printf("Controller %s:\n", name);
150 indent = 0;
151 memset(done, 0, sizeof done);
152 if (addr)
153 usbdev(f, addr, 0);
154 else
155 usbdump(f);
156 }
157
158 int
159 main(argc, argv)
160 int argc;
161 char **argv;
162 {
163 int ch, i, f;
164 char buf[50];
165 extern int optind;
166 extern char *optarg;
167 char *dev = 0;
168 int addr = 0;
169 int ncont;
170
171 while ((ch = getopt(argc, argv, "a:f:v")) != -1) {
172 switch(ch) {
173 case 'a':
174 addr = atoi(optarg);
175 break;
176 case 'f':
177 dev = optarg;
178 break;
179 case 'v':
180 verbose = 1;
181 break;
182 case '?':
183 default:
184 usage();
185 }
186 }
187 argc -= optind;
188 argv += optind;
189
190 if (dev == 0) {
191 for (ncont = 0, i = 0; i < 10; i++) {
192 sprintf(buf, "%s%d", USBDEV, i);
193 f = open(buf, O_RDONLY);
194 if (f >= 0) {
195 ncont++;
196 dumpone(buf, f, addr);
197 close(f);
198 } else {
199 if (errno == EACCES)
200 warn("%s", buf);
201 }
202 }
203 if (verbose && ncont == 0)
204 printf("%s: no USB controllers found\n", __progname);
205 } else {
206 f = open(dev, O_RDONLY);
207 if (f >= 0)
208 dumpone(dev, f, addr);
209 else
210 err(1, "%s", dev);
211 }
212 exit(0);
213 }
214