1 /* $OpenBSD: usbdevs.c,v 1.11 2004/04/03 21:01:25 jmc Exp $ */
2 /* $NetBSD: usbdevs.c,v 1.19 2002/02/21 00:34:31 christos Exp $ */
3
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (augustss@netbsd.org).
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <dev/usb/usb.h>
49
50 #define USBDEV "/dev/usb"
51
52 int verbose = 0;
53 int showdevs = 0;
54
55 void usage(void);
56 void usbdev(int f, int a, int rec);
57 void usbdump(int f);
58 void dumpone(char *name, int f, int addr);
59 int main(int, char **);
60
61 extern char *__progname;
62
63 void
usage(void)64 usage(void)
65 {
66 fprintf(stderr, "Usage: %s [-dv] [-a addr] [-f dev]\n",
67 __progname);
68 exit(1);
69 }
70
71 char done[USB_MAX_DEVICES];
72 int indent;
73
74 void
usbdev(int f,int a,int rec)75 usbdev(int f, int a, int rec)
76 {
77 struct usb_device_info di;
78 int e, p, i;
79
80 di.udi_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 switch (di.udi_speed) {
91 case USB_SPEED_LOW:
92 printf("low speed, ");
93 break;
94 case USB_SPEED_FULL:
95 printf("full speed, ");
96 break;
97 case USB_SPEED_HIGH:
98 printf("high speed, ");
99 break;
100 default:
101 break;
102 }
103
104 if (di.udi_power)
105 printf("power %d mA, ", di.udi_power);
106 else
107 printf("self powered, ");
108 if (di.udi_config)
109 printf("config %d, ", di.udi_config);
110 else
111 printf("unconfigured, ");
112 }
113 if (verbose) {
114 printf("%s(0x%04x), %s(0x%04x), rev %s",
115 di.udi_product, di.udi_productNo,
116 di.udi_vendor, di.udi_vendorNo, di.udi_release);
117 } else
118 printf("%s, %s", di.udi_product, di.udi_vendor);
119 printf("\n");
120 if (showdevs) {
121 for (i = 0; i < USB_MAX_DEVNAMES; i++)
122 if (di.udi_devnames[i][0])
123 printf("%*s %s\n", indent, "",
124 di.udi_devnames[i]);
125 }
126 if (!rec)
127 return;
128 for (p = 0; p < di.udi_nports; p++) {
129 int s = di.udi_ports[p];
130
131 if (s >= USB_MAX_DEVICES) {
132 if (verbose) {
133 printf("%*sport %d %s\n", indent+1, "", p+1,
134 s == USB_PORT_ENABLED ? "enabled" :
135 s == USB_PORT_SUSPENDED ? "suspended" :
136 s == USB_PORT_POWERED ? "powered" :
137 s == USB_PORT_DISABLED ? "disabled" :
138 "???");
139 }
140 continue;
141 }
142 indent++;
143 printf("%*s", indent, "");
144 if (verbose)
145 printf("port %d ", p+1);
146 if (s == 0)
147 printf("addr 0 should never happen!\n");
148 else
149 usbdev(f, s, 1);
150 indent--;
151 }
152 }
153
154 void
usbdump(int f)155 usbdump(int f)
156 {
157 int a;
158
159 for (a = 1; a < USB_MAX_DEVICES; a++) {
160 if (!done[a])
161 usbdev(f, a, 1);
162 }
163 }
164
165 void
dumpone(char * name,int f,int addr)166 dumpone(char *name, int f, int addr)
167 {
168 if (verbose)
169 printf("Controller %s:\n", name);
170 indent = 0;
171 memset(done, 0, sizeof done);
172 if (addr)
173 usbdev(f, addr, 0);
174 else
175 usbdump(f);
176 }
177
178 int
main(int argc,char ** argv)179 main(int argc, char **argv)
180 {
181 int ch, i, f;
182 char buf[50];
183 char *dev = 0;
184 int addr = 0;
185 int ncont;
186
187 while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
188 switch (ch) {
189 case 'a':
190 addr = atoi(optarg);
191 break;
192 case 'd':
193 showdevs++;
194 break;
195 case 'f':
196 dev = optarg;
197 break;
198 case 'v':
199 verbose = 1;
200 break;
201 default:
202 usage();
203 }
204 }
205 argc -= optind;
206 argv += optind;
207
208 if (dev == 0) {
209 for (ncont = 0, i = 0; i < 10; i++) {
210 snprintf(buf, sizeof buf, "%s%d", USBDEV, i);
211 f = open(buf, O_RDONLY);
212 if (f >= 0) {
213 dumpone(buf, f, addr);
214 close(f);
215 } else {
216 if (errno == ENOENT || errno == ENXIO)
217 continue;
218 warn("%s", buf);
219 }
220 ncont++;
221 }
222 if (verbose && ncont == 0)
223 printf("%s: no USB controllers found\n",
224 __progname);
225 } else {
226 f = open(dev, O_RDONLY);
227 if (f >= 0)
228 dumpone(dev, f, addr);
229 else
230 err(1, "%s", dev);
231 }
232 exit(0);
233 }
234