1 /* $OpenBSD: usbdevs.c,v 1.5 2002/05/10 00:09:17 nate Exp $ */ 2 /* $NetBSD: usbdevs.c,v 1.19 2002/02/21 00:34:31 christos 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 = 0; 53 int showdevs = 0; 54 55 void usage(void); 56 void usbdev(int f, int a, int rec); 57 void usbdump(int f); 58 void dumpone(char *name, int f, int addr); 59 int main(int, char **); 60 61 extern char *__progname; 62 63 void 64 usage() 65 { 66 fprintf(stderr, "Usage: %s [-a addr] [-d] [-f dev] [-v]\n", 67 __progname); 68 exit(1); 69 } 70 71 char done[USB_MAX_DEVICES]; 72 int indent; 73 74 void 75 usbdev(int f, int a, int rec) 76 { 77 struct usb_device_info di; 78 int e, p, i; 79 80 di.udi_addr = a; 81 e = ioctl(f, USB_DEVICEINFO, &di); 82 if (e) { 83 if (errno != ENXIO) 84 printf("addr %d: I/O error\n", a); 85 return; 86 } 87 printf("addr %d: ", a); 88 done[a] = 1; 89 if (verbose) { 90 #ifdef notyet 91 switch (di.udi_speed) { 92 case USB_SPEED_LOW: printf("low speed, "); break; 93 case USB_SPEED_FULL: printf("full speed, "); break; 94 case USB_SPEED_HIGH: printf("high speed, "); break; 95 default: break; 96 } 97 #endif 98 if (di.udi_lowspeed) 99 printf("low speed, "); 100 101 if (di.udi_power) 102 printf("power %d mA, ", di.udi_power); 103 else 104 printf("self powered, "); 105 if (di.udi_config) 106 printf("config %d, ", di.udi_config); 107 else 108 printf("unconfigured, "); 109 } 110 if (verbose) { 111 printf("%s(0x%04x), %s(0x%04x), rev %s", 112 di.udi_product, di.udi_productNo, 113 di.udi_vendor, di.udi_vendorNo, di.udi_release); 114 } else 115 printf("%s, %s", di.udi_product, di.udi_vendor); 116 printf("\n"); 117 if (showdevs) { 118 for (i = 0; i < USB_MAX_DEVNAMES; i++) 119 if (di.udi_devnames[i][0]) 120 printf("%*s %s\n", indent, "", 121 di.udi_devnames[i]); 122 } 123 if (!rec) 124 return; 125 for (p = 0; p < di.udi_nports; p++) { 126 int s = di.udi_ports[p]; 127 if (s >= USB_MAX_DEVICES) { 128 if (verbose) { 129 printf("%*sport %d %s\n", indent+1, "", p+1, 130 s == USB_PORT_ENABLED ? "enabled" : 131 s == USB_PORT_SUSPENDED ? "suspended" : 132 s == USB_PORT_POWERED ? "powered" : 133 s == USB_PORT_DISABLED ? "disabled" : 134 "???"); 135 136 } 137 continue; 138 } 139 indent++; 140 printf("%*s", indent, ""); 141 if (verbose) 142 printf("port %d ", p+1); 143 if (s == 0) 144 printf("addr 0 should never happen!\n"); 145 else 146 usbdev(f, s, 1); 147 indent--; 148 } 149 } 150 151 void 152 usbdump(int f) 153 { 154 int a; 155 156 for (a = 1; a < USB_MAX_DEVICES; a++) { 157 if (!done[a]) 158 usbdev(f, a, 1); 159 } 160 } 161 162 void 163 dumpone(char *name, int f, int addr) 164 { 165 if (verbose) 166 printf("Controller %s:\n", name); 167 indent = 0; 168 memset(done, 0, sizeof done); 169 if (addr) 170 usbdev(f, addr, 0); 171 else 172 usbdump(f); 173 } 174 175 int 176 main(int argc, char **argv) 177 { 178 int ch, i, f; 179 char buf[50]; 180 char *dev = 0; 181 int addr = 0; 182 int ncont; 183 184 while ((ch = getopt(argc, argv, "a:df:v?")) != -1) { 185 switch(ch) { 186 case 'a': 187 addr = atoi(optarg); 188 break; 189 case 'd': 190 showdevs++; 191 break; 192 case 'f': 193 dev = optarg; 194 break; 195 case 'v': 196 verbose = 1; 197 break; 198 case '?': 199 default: 200 usage(); 201 } 202 } 203 argc -= optind; 204 argv += optind; 205 206 if (dev == 0) { 207 for (ncont = 0, i = 0; i < 10; i++) { 208 sprintf(buf, "%s%d", USBDEV, i); 209 f = open(buf, O_RDONLY); 210 if (f >= 0) { 211 dumpone(buf, f, addr); 212 close(f); 213 } else { 214 if (errno == ENOENT || errno == ENXIO) 215 continue; 216 warn("%s", buf); 217 } 218 ncont++; 219 } 220 if (verbose && ncont == 0) 221 printf("%s: no USB controllers found\n", 222 __progname); 223 } else { 224 f = open(dev, O_RDONLY); 225 if (f >= 0) 226 dumpone(dev, f, addr); 227 else 228 err(1, "%s", dev); 229 } 230 exit(0); 231 } 232