Remove main() prototypes.
[dragonfly.git] / usr.sbin / usbdevs / usbdevs.c
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  */
5
6 /*
7  * Copyright (c) 1998 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Lennart Augustsson (augustss@netbsd.org).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sys/types.h>
46 #include <fcntl.h>
47 #include <unistd.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <bus/usb/usb.h>
51 #if defined(__DragonFly__)
52 #include <sys/ioctl.h>
53 #endif
54
55 #define USBDEV "/dev/usb"
56
57 int verbose = 0;
58 int showdevs = 0;
59
60 void usage(void);
61 void usbdev(int f, int a, int rec);
62 void usbdump(int f);
63 void dumpone(char *name, int f, int addr);
64
65 void
66 usage(void)
67 {
68         fprintf(stderr, "usage: %s [-a addr] [-d] [-f dev] [-v]\n",
69             getprogname());
70         exit(1);
71 }
72
73 char done[USB_MAX_DEVICES];
74 int indent;
75
76 void
77 usbdev(int f, int a, int rec)
78 {
79         struct usb_device_info di;
80         int e, p, i;
81
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;
88         }
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;
97                 }
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, ");
106         }
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         printf("\n");
114         if (showdevs) {
115                 for (i = 0; i < USB_MAX_DEVNAMES; i++)
116                         if (di.udi_devnames[i][0])
117                                 printf("%*s  %s\n", indent, "",
118                                        di.udi_devnames[i]);
119         }
120         if (!rec)
121                 return;
122         for (p = 0; p < di.udi_nports; p++) {
123                 int s = di.udi_ports[p];
124                 if (s >= USB_MAX_DEVICES) {
125                         if (verbose) {
126                                 printf("%*sport %d %s\n", indent+1, "", p+1,
127                                        s == USB_PORT_ENABLED ? "enabled" :
128                                        s == USB_PORT_SUSPENDED ? "suspended" : 
129                                        s == USB_PORT_POWERED ? "powered" :
130                                        s == USB_PORT_DISABLED ? "disabled" :
131                                        "???");
132                                 
133                         }
134                         continue;
135                 }
136                 indent++;
137                 printf("%*s", indent, "");
138                 if (verbose)
139                         printf("port %d ", p+1);
140                 if (s == 0)
141                         printf("addr 0 should never happen!\n");
142                 else
143                         usbdev(f, s, 1);
144                 indent--;
145         }
146 }
147
148 void
149 usbdump(int f)
150 {
151         int a;
152
153         for (a = 1; a < USB_MAX_DEVICES; a++) {
154                 if (!done[a])
155                         usbdev(f, a, 1);
156         }
157 }
158
159 void
160 dumpone(char *name, int f, int addr)
161 {
162         if (verbose)
163                 printf("Controller %s:\n", name);
164         indent = 0;
165         memset(done, 0, sizeof done);
166         if (addr)
167                 usbdev(f, addr, 0);
168         else
169                 usbdump(f);
170 }
171
172 int
173 main(int argc, char **argv)
174 {
175         int ch, i, f;
176         char buf[50];
177         char *dev = NULL;
178         int addr = 0;
179         int ncont;
180
181         while ((ch = getopt(argc, argv, "a:df:v")) != -1) {
182                 switch(ch) {
183                 case 'a':
184                         addr = atoi(optarg);
185                         break;
186                 case 'd':
187                         showdevs++;
188                         break;
189                 case 'f':
190                         dev = optarg;
191                         break;
192                 case 'v':
193                         verbose = 1;
194                         break;
195                 default:
196                         usage();
197                 }
198         }
199         argc -= optind;
200         argv += optind;
201
202         if (dev == NULL) {
203                 for (ncont = 0, i = 0; i < 10; i++) {
204                         sprintf(buf, "%s%d", USBDEV, i);
205                         f = open(buf, O_RDONLY);
206                         if (f >= 0) {
207                                 dumpone(buf, f, addr);
208                                 close(f);
209                         } else {
210                                 if (errno == ENOENT || errno == ENXIO)
211                                         continue;
212                                 warn("%s", buf);
213                         }
214                         ncont++;
215                 }
216                 if (verbose && ncont == 0)
217                         printf("%s: no USB controllers found\n",
218                             getprogname());
219         } else {
220                 f = open(dev, O_RDONLY);
221                 if (f >= 0)
222                         dumpone(dev, f, addr);
223                 else
224                         err(1, "%s", dev);
225         }
226         exit(0);
227 }