1 |
|
|
/* $OpenBSD: usbdevs.c,v 1.25 2015/12/22 08:36:40 mmcc 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 |
|
|
* |
20 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
21 |
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
22 |
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
23 |
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
24 |
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
25 |
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
26 |
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
27 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
28 |
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
29 |
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
30 |
|
|
* POSSIBILITY OF SUCH DAMAGE. |
31 |
|
|
*/ |
32 |
|
|
|
33 |
|
|
#include <stdio.h> |
34 |
|
|
#include <stdlib.h> |
35 |
|
|
#include <string.h> |
36 |
|
|
#include <sys/types.h> |
37 |
|
|
#include <fcntl.h> |
38 |
|
|
#include <unistd.h> |
39 |
|
|
#include <err.h> |
40 |
|
|
#include <errno.h> |
41 |
|
|
#include <dev/usb/usb.h> |
42 |
|
|
|
43 |
|
|
#ifndef nitems |
44 |
|
|
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) |
45 |
|
|
#endif |
46 |
|
|
|
47 |
|
|
#define USBDEV "/dev/usb" |
48 |
|
|
|
49 |
|
|
int verbose = 0; |
50 |
|
|
int showdevs = 0; |
51 |
|
|
|
52 |
|
|
void usage(void); |
53 |
|
|
void usbdev(int f, int a, int rec); |
54 |
|
|
void usbdump(int f); |
55 |
|
|
void dumpone(char *name, int f, int addr); |
56 |
|
|
int main(int, char **); |
57 |
|
|
|
58 |
|
|
extern char *__progname; |
59 |
|
|
|
60 |
|
|
void |
61 |
|
|
usage(void) |
62 |
|
|
{ |
63 |
|
|
fprintf(stderr, "usage: %s [-dv] [-a addr] [-f dev]\n", __progname); |
64 |
|
|
exit(1); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
char done[USB_MAX_DEVICES]; |
68 |
|
|
int indent; |
69 |
|
|
|
70 |
|
|
void |
71 |
|
|
usbdev(int f, int a, int rec) |
72 |
|
|
{ |
73 |
|
|
struct usb_device_info di; |
74 |
|
|
int e, p, i; |
75 |
|
|
|
76 |
|
|
di.udi_addr = a; |
77 |
|
|
e = ioctl(f, USB_DEVICEINFO, &di); |
78 |
|
|
if (e) { |
79 |
|
|
if (errno != ENXIO) |
80 |
|
|
printf("addr %d: I/O error\n", a); |
81 |
|
|
return; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
printf("addr %d: ", a); |
85 |
|
|
done[a] = 1; |
86 |
|
|
if (verbose) { |
87 |
|
|
switch (di.udi_speed) { |
88 |
|
|
case USB_SPEED_LOW: |
89 |
|
|
printf("low speed, "); |
90 |
|
|
break; |
91 |
|
|
case USB_SPEED_FULL: |
92 |
|
|
printf("full speed, "); |
93 |
|
|
break; |
94 |
|
|
case USB_SPEED_HIGH: |
95 |
|
|
printf("high speed, "); |
96 |
|
|
break; |
97 |
|
|
case USB_SPEED_SUPER: |
98 |
|
|
printf("super 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 |
|
|
if (strlen(di.udi_serial)) |
118 |
|
|
printf(", iSerialNumber %s", di.udi_serial); |
119 |
|
|
} else |
120 |
|
|
printf("%s, %s", di.udi_product, di.udi_vendor); |
121 |
|
|
printf("\n"); |
122 |
|
|
if (showdevs) { |
123 |
|
|
for (i = 0; i < USB_MAX_DEVNAMES; i++) |
124 |
|
|
if (di.udi_devnames[i][0]) |
125 |
|
|
printf("%*s %s\n", indent, "", |
126 |
|
|
di.udi_devnames[i]); |
127 |
|
|
} |
128 |
|
|
if (!rec) |
129 |
|
|
return; |
130 |
|
|
for (p = 0; p < di.udi_nports && p < nitems(di.udi_ports); p++) { |
131 |
|
|
int s = di.udi_ports[p]; |
132 |
|
|
|
133 |
|
|
if (s >= USB_MAX_DEVICES) { |
134 |
|
|
if (verbose) { |
135 |
|
|
printf("%*sport %d %s\n", indent+1, "", p+1, |
136 |
|
|
s == USB_PORT_ENABLED ? "enabled" : |
137 |
|
|
s == USB_PORT_SUSPENDED ? "suspended" : |
138 |
|
|
s == USB_PORT_POWERED ? "powered" : |
139 |
|
|
s == USB_PORT_DISABLED ? "disabled" : |
140 |
|
|
"???"); |
141 |
|
|
} |
142 |
|
|
continue; |
143 |
|
|
} |
144 |
|
|
indent++; |
145 |
|
|
printf("%*s", indent, ""); |
146 |
|
|
if (verbose) |
147 |
|
|
printf("port %d ", p+1); |
148 |
|
|
if (s == 0) |
149 |
|
|
printf("addr 0 should never happen!\n"); |
150 |
|
|
else |
151 |
|
|
usbdev(f, s, 1); |
152 |
|
|
indent--; |
153 |
|
|
} |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
void |
157 |
|
|
usbdump(int f) |
158 |
|
|
{ |
159 |
|
|
int a; |
160 |
|
|
|
161 |
|
|
for (a = 1; a < USB_MAX_DEVICES; a++) { |
162 |
|
|
if (!done[a]) |
163 |
|
|
usbdev(f, a, 1); |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
void |
168 |
|
|
dumpone(char *name, int f, int addr) |
169 |
|
|
{ |
170 |
|
|
if (verbose) |
171 |
|
|
printf("Controller %s:\n", name); |
172 |
|
|
indent = 0; |
173 |
|
|
memset(done, 0, sizeof done); |
174 |
|
|
if (addr) |
175 |
|
|
usbdev(f, addr, 0); |
176 |
|
|
else |
177 |
|
|
usbdump(f); |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
int |
181 |
|
|
main(int argc, char **argv) |
182 |
|
|
{ |
183 |
|
|
int ch, i, f; |
184 |
|
|
char buf[50]; |
185 |
|
|
char *dev = NULL; |
186 |
|
|
const char *errstr; |
187 |
|
|
int addr = 0; |
188 |
|
|
int ncont; |
189 |
|
|
|
190 |
|
|
while ((ch = getopt(argc, argv, "a:df:v?")) != -1) { |
191 |
|
|
switch (ch) { |
192 |
|
|
case 'a': |
193 |
|
|
addr = strtonum(optarg, 1, USB_MAX_DEVICES, &errstr); |
194 |
|
|
if (errstr) |
195 |
|
|
errx(1, "addr %s", errstr); |
196 |
|
|
break; |
197 |
|
|
case 'd': |
198 |
|
|
showdevs = 1; |
199 |
|
|
break; |
200 |
|
|
case 'f': |
201 |
|
|
dev = optarg; |
202 |
|
|
break; |
203 |
|
|
case 'v': |
204 |
|
|
verbose = 1; |
205 |
|
|
break; |
206 |
|
|
default: |
207 |
|
|
usage(); |
208 |
|
|
} |
209 |
|
|
} |
210 |
|
|
argc -= optind; |
211 |
|
|
argv += optind; |
212 |
|
|
|
213 |
|
|
if (dev == 0) { |
214 |
|
|
for (ncont = 0, i = 0; i < 10; i++) { |
215 |
|
|
snprintf(buf, sizeof buf, "%s%d", USBDEV, i); |
216 |
|
|
f = open(buf, O_RDONLY); |
217 |
|
|
if (f >= 0) { |
218 |
|
|
dumpone(buf, f, addr); |
219 |
|
|
close(f); |
220 |
|
|
} else { |
221 |
|
|
if (errno == ENOENT || errno == ENXIO) |
222 |
|
|
continue; |
223 |
|
|
warn("%s", buf); |
224 |
|
|
} |
225 |
|
|
ncont++; |
226 |
|
|
} |
227 |
|
|
if (verbose && ncont == 0) |
228 |
|
|
printf("%s: no USB controllers found\n", |
229 |
|
|
__progname); |
230 |
|
|
} else { |
231 |
|
|
f = open(dev, O_RDONLY); |
232 |
|
|
if (f >= 0) |
233 |
|
|
dumpone(dev, f, addr); |
234 |
|
|
else |
235 |
|
|
err(1, "%s", dev); |
236 |
|
|
} |
237 |
|
|
exit(0); |
238 |
|
|
} |