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