usbdevs.c revision 1.1 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <err.h>
8 #include <dev/usb/usb.h>
9
10 #define USBDEV "/dev/usb"
11
12 int verbose;
13
14 void usage __P((void));
15 void usbdev __P((int f, int a, int rec));
16 void usbdump __P((int f));
17 void dumpone __P((char *name, int f, int addr));
18 int main __P((int, char **));
19
20 void
21 usage()
22 {
23 extern char *__progname;
24
25 fprintf(stderr, "Usage: %s [-a addr] [-f dev] [-v]\n", __progname);
26 exit(1);
27 }
28
29 char done[USB_MAX_DEVICES];
30 int indent;
31
32 void
33 usbdev(f, a, rec)
34 int f;
35 int a;
36 int rec;
37 {
38 struct usb_device_info di;
39 int e, p;
40
41 di.addr = a;
42 e = ioctl(f, USB_DEVICEINFO, &di);
43 if (e)
44 return;
45 done[a] = 1;
46 printf("addr %d: ", di.addr);
47 if (verbose) {
48 if (di.lowspeed)
49 printf("low speed, ");
50 if (di.power)
51 printf("power %d mA, ", di.power);
52 else
53 printf("self powered, ");
54 if (di.config)
55 printf("config %d, ", di.config);
56 else
57 printf("unconfigured, ");
58 }
59 printf("%s, %s", di.product, di.vendor);
60 if (verbose)
61 printf(", rev %s", di.revision);
62 printf("\n");
63 if (!rec)
64 return;
65 for (p = 0; p < di.nports; p++) {
66 int s = di.ports[p];
67 if (s >= USB_MAX_DEVICES) {
68 if (verbose) {
69 printf("%*sport %d %s\n", indent+1, "", p+1,
70 s == USB_PORT_ENABLED ? "enabled" :
71 s == USB_PORT_SUSPENDED ? "suspended" :
72 s == USB_PORT_POWERED ? "powered" :
73 s == USB_PORT_DISABLED ? "disabled" :
74 "???");
75
76 }
77 continue;
78 }
79 indent++;
80 printf("%*s", indent, "");
81 if (verbose)
82 printf("port %d ", p+1);
83 usbdev(f, di.ports[p], 1);
84 indent--;
85 }
86 }
87
88 void
89 usbdump(f)
90 int f;
91 {
92 int a;
93
94 for (a = 1; a < USB_MAX_DEVICES; a++) {
95 if (!done[a])
96 usbdev(f, a, 1);
97 }
98 }
99
100 void
101 dumpone(name, f, addr)
102 char *name;
103 int f;
104 int addr;
105 {
106 if (verbose)
107 printf("Controller %s:\n", name);
108 indent = 0;
109 memset(done, 0, sizeof done);
110 if (addr)
111 usbdev(f, addr, 0);
112 else
113 usbdump(f);
114 }
115
116 int
117 main(argc, argv)
118 int argc;
119 char **argv;
120 {
121 int ch, i, f;
122 char buf[50];
123 extern int optind;
124 extern char *optarg;
125 char *dev = 0;
126 int addr = 0;
127
128 while ((ch = getopt(argc, argv, "a:f:v")) != -1) {
129 switch(ch) {
130 case 'a':
131 addr = atoi(optarg);
132 break;
133 case 'f':
134 dev = optarg;
135 break;
136 case 'v':
137 verbose = 1;
138 break;
139 case '?':
140 default:
141 usage();
142 }
143 }
144 argc -= optind;
145 argv += optind;
146
147 if (dev == 0) {
148 for (i = 0; i < 10; i++) {
149 sprintf(buf, "%s%d", USBDEV, i);
150 f = open(buf, O_RDONLY);
151 if (f >= 0) {
152 dumpone(buf, f, addr);
153 close(f);
154 }
155 }
156 } else {
157 f = open(dev, O_RDONLY);
158 if (f >= 0)
159 dumpone(dev, f, addr);
160 else
161 err(1, "%s", dev);
162 }
163 exit(0);
164 }
165