]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/usbdevs/usbdevs.c
Add a TestFloat based test suite for floating-point implementations
[FreeBSD/FreeBSD.git] / usr.sbin / usbdevs / usbdevs.c
1 /*      $NetBSD: usbdevs.c,v 1.22 2003/11/12 13:31:08 grant Exp $       */
2 /*      $FreeBSD$       */
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 #if defined(__FreeBSD__)
43 #include <sys/ioctl.h>
44 #endif
45
46 #define USBDEV "/dev/usb"
47
48 int verbose = 0;
49 int showdevs = 0;
50 int oneline = 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 void
59 usage()
60 {
61         fprintf(stderr, "usage: %s [-a addr] [-d] [-f dev] [-o] [-v]\n",
62             getprogname());
63         exit(1);
64 }
65
66 char done[USB_MAX_DEVICES];
67 int indent;
68
69 void
70 usbdev(int f, int a, int rec)
71 {
72         struct usb_device_info di;
73         int e, p, i;
74
75         di.udi_addr = a;
76         e = ioctl(f, USB_DEVICEINFO, &di);
77         if (e) {
78                 if (errno != ENXIO)
79                         printf("addr %d: I/O error\n", a);
80                 return;
81         }
82         printf("addr %d: ", a);
83         done[a] = 1;
84         if (verbose) {
85                 switch (di.udi_speed) {
86                 case USB_SPEED_LOW:  printf("low speed, "); break;
87                 case USB_SPEED_FULL: printf("full speed, "); break;
88                 case USB_SPEED_HIGH: printf("high speed, "); break;
89                 default: break;
90                 }
91                 if (di.udi_power)
92                         printf("power %d mA, ", di.udi_power);
93                 else
94                         printf("self powered, ");
95                 if (di.udi_config)
96                         printf("config %d, ", di.udi_config);
97                 else
98                         printf("unconfigured, ");
99         }
100         if (verbose) {
101                 printf("%s(0x%04x), %s(0x%04x), rev %s",
102                        di.udi_product, di.udi_productNo,
103                        di.udi_vendor, di.udi_vendorNo, di.udi_release);
104         } else
105                 printf("%s, %s", di.udi_product, di.udi_vendor);
106         if (!oneline)
107                 printf("\n");
108         if (showdevs) {
109                 for (i = 0; i < USB_MAX_DEVNAMES; i++) {
110                         if (di.udi_devnames[i][0]) {
111                                 if (oneline)
112                                         printf(", device %s",
113                                             di.udi_devnames[i]);
114                                 else
115                                         printf("%*s  %s\n", indent, "",
116                                             di.udi_devnames[i]);
117                         }
118                 }
119         }
120         if (oneline)
121                 printf("\n");
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                                        "???");
134                                 
135                         }
136                         continue;
137                 }
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--;
147         }
148 }
149
150 void
151 usbdump(int f)
152 {
153         int a;
154
155         for (a = 1; a < USB_MAX_DEVICES; a++) {
156                 if (!done[a])
157                         usbdev(f, a, 1);
158         }
159 }
160
161 void
162 dumpone(char *name, int f, int addr)
163 {
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);
172 }
173
174 int
175 main(int argc, char **argv)
176 {
177         int ch, i, f;
178         char buf[50];
179         char *dev = 0;
180         int addr = 0;
181         int ncont;
182
183         while ((ch = getopt(argc, argv, "a:df:ov?")) != -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 'o':
195                         oneline++;
196                         break;
197                 case 'v':
198                         verbose = 1;
199                         break;
200                 case '?':
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                             getprogname());
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 }