turns printfs back on
[freebsd-src/fkvm-freebsd.git] / usr.sbin / usbdevs / usbdevs.c
blob640dd512f7f29df4e3a711223bf3a35616d8ce81
1 /* $NetBSD: usbdevs.c,v 1.22 2003/11/12 13:31:08 grant Exp $ */
2 /* $FreeBSD$ */
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (augustss@NetBSD.org).
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.
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.
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 #if defined(__FreeBSD__)
50 #include <sys/ioctl.h>
51 #endif
53 #define USBDEV "/dev/usb"
55 int verbose = 0;
56 int showdevs = 0;
57 int oneline = 0;
59 void usage(void);
60 void usbdev(int f, int a, int rec);
61 void usbdump(int f);
62 void dumpone(char *name, int f, int addr);
63 int main(int, char **);
65 void
66 usage()
68 fprintf(stderr, "usage: %s [-a addr] [-d] [-f dev] [-v]\n",
69 getprogname());
70 exit(1);
73 char done[USB_MAX_DEVICES];
74 int indent;
76 void
77 usbdev(int f, int a, int rec)
79 struct usb_device_info di;
80 int e, p, i;
82 di.udi_addr = a;
83 e = ioctl(f, USB_DEVICEINFO, &di);
84 if (e) {
85 if (errno != ENXIO)
86 printf("addr %d: I/O error\n", a);
87 return;
89 printf("addr %d: ", a);
90 done[a] = 1;
91 if (verbose) {
92 switch (di.udi_speed) {
93 case USB_SPEED_LOW: printf("low speed, "); break;
94 case USB_SPEED_FULL: printf("full speed, "); break;
95 case USB_SPEED_HIGH: printf("high speed, "); break;
96 default: break;
98 if (di.udi_power)
99 printf("power %d mA, ", di.udi_power);
100 else
101 printf("self powered, ");
102 if (di.udi_config)
103 printf("config %d, ", di.udi_config);
104 else
105 printf("unconfigured, ");
107 if (verbose) {
108 printf("%s(0x%04x), %s(0x%04x), rev %s",
109 di.udi_product, di.udi_productNo,
110 di.udi_vendor, di.udi_vendorNo, di.udi_release);
111 } else
112 printf("%s, %s", di.udi_product, di.udi_vendor);
113 if (!oneline)
114 printf("\n");
115 if (showdevs) {
116 for (i = 0; i < USB_MAX_DEVNAMES; i++) {
117 if (di.udi_devnames[i][0]) {
118 if (oneline)
119 printf(", device %s",
120 di.udi_devnames[i]);
121 else
122 printf("%*s %s\n", indent, "",
123 di.udi_devnames[i]);
127 if (oneline)
128 printf("\n");
129 if (!rec)
130 return;
131 for (p = 0; p < di.udi_nports; p++) {
132 int s = di.udi_ports[p];
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 "???");
143 continue;
145 indent++;
146 printf("%*s", indent, "");
147 if (verbose)
148 printf("port %d ", p+1);
149 if (s == 0)
150 printf("addr 0 should never happen!\n");
151 else
152 usbdev(f, s, 1);
153 indent--;
157 void
158 usbdump(int f)
160 int a;
162 for (a = 1; a < USB_MAX_DEVICES; a++) {
163 if (!done[a])
164 usbdev(f, a, 1);
168 void
169 dumpone(char *name, int f, int addr)
171 if (verbose)
172 printf("Controller %s:\n", name);
173 indent = 0;
174 memset(done, 0, sizeof done);
175 if (addr)
176 usbdev(f, addr, 0);
177 else
178 usbdump(f);
182 main(int argc, char **argv)
184 int ch, i, f;
185 char buf[50];
186 char *dev = 0;
187 int addr = 0;
188 int ncont;
190 while ((ch = getopt(argc, argv, "a:df:ov?")) != -1) {
191 switch(ch) {
192 case 'a':
193 addr = atoi(optarg);
194 break;
195 case 'd':
196 showdevs++;
197 break;
198 case 'f':
199 dev = optarg;
200 break;
201 case 'o':
202 oneline++;
203 break;
204 case 'v':
205 verbose = 1;
206 break;
207 case '?':
208 default:
209 usage();
212 argc -= optind;
213 argv += optind;
215 if (dev == 0) {
216 for (ncont = 0, i = 0; i < 10; i++) {
217 snprintf(buf, sizeof(buf), "%s%d", USBDEV, i);
218 f = open(buf, O_RDONLY);
219 if (f >= 0) {
220 dumpone(buf, f, addr);
221 close(f);
222 } else {
223 if (errno == ENOENT || errno == ENXIO)
224 continue;
225 warn("%s", buf);
227 ncont++;
229 if (verbose && ncont == 0)
230 printf("%s: no USB controllers found\n",
231 getprogname());
232 } else {
233 f = open(dev, O_RDONLY);
234 if (f >= 0)
235 dumpone(dev, f, addr);
236 else
237 err(1, "%s", dev);
239 exit(0);