1 /* $OpenBSD: usbdevs.c,v 1.1 2000/02/03 21:52:15 jakob Exp $ */ 2 /* $NetBSD: usbdevs.c,v 1.11 1999/09/08 02:39:36 augustss Exp $ */ 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 50 #define USBDEV "/dev/usb" 51 52 int verbose; 53 54 void usage __P((void)); 55 void usbdev __P((int f, int a, int rec)); 56 void usbdump __P((int f)); 57 void dumpone __P((char *name, int f, int addr)); 58 int main __P((int, char **)); 59 60 extern char *__progname; 61 62 void 63 usage() 64 { 65 fprintf(stderr, "Usage: %s [-a addr] [-f dev] [-v]\n", __progname); 66 exit(1); 67 } 68 69 char done[USB_MAX_DEVICES]; 70 int indent; 71 72 void 73 usbdev(f, a, rec) 74 int f; 75 int a; 76 int rec; 77 { 78 struct usb_device_info di; 79 int e, p; 80 81 di.addr = a; 82 e = ioctl(f, USB_DEVICEINFO, &di); 83 if (e) { 84 if (errno != ENXIO) 85 printf("addr %d: I/O error\n", a); 86 return; 87 } 88 printf("addr %d: ", a); 89 done[a] = 1; 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 %s", 104 di.product, di.productNo, 105 di.vendor, di.vendorNo, di.release); 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 if (s == 0) 130 printf("addr 0 should never happen!\n"); 131 else 132 usbdev(f, s, 1); 133 indent--; 134 } 135 } 136 137 void 138 usbdump(f) 139 int f; 140 { 141 int a; 142 143 for (a = 1; a < USB_MAX_DEVICES; a++) { 144 if (!done[a]) 145 usbdev(f, a, 1); 146 } 147 } 148 149 void 150 dumpone(name, f, addr) 151 char *name; 152 int f; 153 int addr; 154 { 155 if (verbose) 156 printf("Controller %s:\n", name); 157 indent = 0; 158 memset(done, 0, sizeof done); 159 if (addr) 160 usbdev(f, addr, 0); 161 else 162 usbdump(f); 163 } 164 165 int 166 main(argc, argv) 167 int argc; 168 char **argv; 169 { 170 int ch, i, f; 171 char buf[50]; 172 extern int optind; 173 extern char *optarg; 174 char *dev = 0; 175 int addr = 0; 176 int ncont; 177 178 while ((ch = getopt(argc, argv, "a:f:v")) != -1) { 179 switch(ch) { 180 case 'a': 181 addr = atoi(optarg); 182 break; 183 case 'f': 184 dev = optarg; 185 break; 186 case 'v': 187 verbose = 1; 188 break; 189 case '?': 190 default: 191 usage(); 192 } 193 } 194 argc -= optind; 195 argv += optind; 196 197 if (dev == 0) { 198 for (ncont = 0, i = 0; i < 10; i++) { 199 sprintf(buf, "%s%d", USBDEV, i); 200 f = open(buf, O_RDONLY); 201 if (f >= 0) { 202 dumpone(buf, f, addr); 203 close(f); 204 } else { 205 if (errno == ENOENT || errno == ENXIO) 206 continue; 207 warn("%s", buf); 208 } 209 ncont++; 210 } 211 if (verbose && ncont == 0) 212 printf("%s: no USB controllers found\n", __progname); 213 } else { 214 f = open(dev, O_RDONLY); 215 if (f >= 0) 216 dumpone(dev, f, addr); 217 else 218 err(1, "%s", dev); 219 } 220 exit(0); 221 } 222