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