]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/usbdevs/usbdevs.c
This commit was generated by cvs2svn to compensate for changes in r78986,
[FreeBSD/FreeBSD.git] / usr.sbin / usbdevs / usbdevs.c
1 /*      $NetBSD: usbdevs.c,v 1.4 1998/07/23 13:57:51 augustss 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  * 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.
26  *
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.
38  */
39
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
52
53 #define USBDEV "/dev/usb"
54
55 int verbose;
56
57 void usage __P((void));
58 void usbdev __P((int f, int a, int rec));
59 void usbdump __P((int f));
60 void dumpone __P((char *name, int f, int addr));
61 int main __P((int, char **));
62
63 extern char *__progname;
64
65 void
66 usage()
67 {
68         fprintf(stderr, "Usage: %s [-a addr] [-f dev] [-v]\n", __progname);
69         exit(1);
70 }
71
72 char done[USB_MAX_DEVICES];
73 int indent;
74
75 void
76 usbdev(f, a, rec)
77         int f;
78         int a;
79         int rec;
80 {
81         struct usb_device_info di;
82         int e, p;
83
84         di.addr = a;
85         e = ioctl(f, USB_DEVICEINFO, &di);
86         if (e)
87                 return;
88         done[a] = 1;
89         printf("addr %d: ", di.addr);
90         if (verbose) {
91                 if (di.lowspeed)
92                         printf("low speed, ");
93                 if (di.power)
94                         printf("power %d mA, ", di.power);
95                 else
96                         printf("self powered, ");
97                 if (di.config)
98                         printf("config %d, ", di.config);
99                 else
100                         printf("unconfigured, ");
101         }
102         if (verbose) {
103                 printf("%s(0x%04x), %s(0x%04x), rev 0x%04x",
104                         di.product, di.productNo,
105                         di.vendor, di.vendorNo, di.releaseNo);
106         } else
107                 printf("%s, %s", di.product, di.vendor);
108         printf("\n");
109         if (!rec)
110                 return;
111         for (p = 0; p < di.nports; p++) {
112                 int s = di.ports[p];
113                 if (s >= USB_MAX_DEVICES) {
114                         if (verbose) {
115                                 printf("%*sport %d %s\n", indent+1, "", p+1,
116                                        s == USB_PORT_ENABLED ? "enabled" :
117                                        s == USB_PORT_SUSPENDED ? "suspended" : 
118                                        s == USB_PORT_POWERED ? "powered" :
119                                        s == USB_PORT_DISABLED ? "disabled" :
120                                        "???");
121                                 
122                         }
123                         continue;
124                 }
125                 indent++;
126                 printf("%*s", indent, "");
127                 if (verbose)
128                         printf("port %d ", p+1);
129                 usbdev(f, di.ports[p], 1);
130                 indent--;
131         }
132 }
133
134 void
135 usbdump(f)
136         int f;
137 {
138         int a;
139
140         for (a = 1; a < USB_MAX_DEVICES; a++) {
141                 if (!done[a])
142                         usbdev(f, a, 1);
143         }
144 }
145
146 void
147 dumpone(name, f, addr)
148         char *name;
149         int f;
150         int addr;
151 {
152         if (verbose)
153                 printf("Controller %s:\n", name);
154         indent = 0;
155         memset(done, 0, sizeof done);
156         if (addr)
157                 usbdev(f, addr, 0);
158         else
159                 usbdump(f);
160 }
161
162 int
163 main(argc, argv)
164         int argc;
165         char **argv;
166 {
167         int ch, i, f;
168         char buf[50];
169         extern int optind;
170         extern char *optarg;
171         char *dev = 0;
172         int addr = 0;
173         int ncont;
174
175         while ((ch = getopt(argc, argv, "a:f:v")) != -1) {
176                 switch(ch) {
177                 case 'a':
178                         addr = atoi(optarg);
179                         break;
180                 case 'f':
181                         dev = optarg;
182                         break;
183                 case 'v':
184                         verbose = 1;
185                         break;
186                 case '?':
187                 default:
188                         usage();
189                 }
190         }
191         argc -= optind;
192         argv += optind;
193
194         if (dev == 0) {
195                 for (ncont = 0, i = 0; i < 10; i++) {
196                         sprintf(buf, "%s%d", USBDEV, i);
197                         f = open(buf, O_RDONLY);
198                         if (f >= 0) {
199                                 ncont++;
200                                 dumpone(buf, f, addr);
201                                 close(f);
202                         } else {
203                                 if (errno == EACCES)
204                                         warn("%s", buf);
205                         }
206                 }
207                 if (verbose && ncont == 0)
208                         printf("%s: no USB controllers found\n", __progname);
209         } else {
210                 f = open(dev, O_RDONLY);
211                 if (f >= 0)
212                         dumpone(dev, f, addr);
213                 else
214                         err(1, "%s", dev);
215         }
216         exit(0);
217 }