1 /* $OpenBSD: usbdevs.c,v 1.9 2003/07/08 13:20:06 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 switch (di.udi_speed) { 91 case USB_SPEED_LOW: printf("low speed, "); break; 92 case USB_SPEED_FULL: printf("full speed, "); break; 93 case USB_SPEED_HIGH: printf("high speed, "); break; 94 default: break; 95 } 96 97 if (di.udi_power) 98 printf("power %d mA, ", di.udi_power); 99 else 100 printf("self powered, "); 101 if (di.udi_config) 102 printf("config %d, ", di.udi_config); 103 else 104 printf("unconfigured, "); 105 } 106 if (verbose) { 107 printf("%s(0x%04x), %s(0x%04x), rev %s", 108 di.udi_product, di.udi_productNo, 109 di.udi_vendor, di.udi_vendorNo, di.udi_release); 110 } else 111 printf("%s, %s", di.udi_product, di.udi_vendor); 112 printf("\n"); 113 if (showdevs) { 114 for (i = 0; i < USB_MAX_DEVNAMES; i++) 115 if (di.udi_devnames[i][0]) 116 printf("%*s %s\n", indent, "", 117 di.udi_devnames[i]); 118 } 119 if (!rec) 120 return; 121 for (p = 0; p < di.udi_nports; p++) { 122 int s = di.udi_ports[p]; 123 if (s >= USB_MAX_DEVICES) { 124 if (verbose) { 125 printf("%*sport %d %s\n", indent+1, "", p+1, 126 s == USB_PORT_ENABLED ? "enabled" : 127 s == USB_PORT_SUSPENDED ? "suspended" : 128 s == USB_PORT_POWERED ? "powered" : 129 s == USB_PORT_DISABLED ? "disabled" : 130 "???"); 131 } 132 continue; 133 } 134 indent++; 135 printf("%*s", indent, ""); 136 if (verbose) 137 printf("port %d ", p+1); 138 if (s == 0) 139 printf("addr 0 should never happen!\n"); 140 else 141 usbdev(f, s, 1); 142 indent--; 143 } 144 } 145 146 void 147 usbdump(int f) 148 { 149 int a; 150 151 for (a = 1; a < USB_MAX_DEVICES; a++) { 152 if (!done[a]) 153 usbdev(f, a, 1); 154 } 155 } 156 157 void 158 dumpone(char *name, int f, int addr) 159 { 160 if (verbose) 161 printf("Controller %s:\n", name); 162 indent = 0; 163 memset(done, 0, sizeof done); 164 if (addr) 165 usbdev(f, addr, 0); 166 else 167 usbdump(f); 168 } 169 170 int 171 main(int argc, char **argv) 172 { 173 int ch, i, f; 174 char buf[50]; 175 char *dev = 0; 176 int addr = 0; 177 int ncont; 178 179 while ((ch = getopt(argc, argv, "a:df:v?")) != -1) { 180 switch (ch) { 181 case 'a': 182 addr = atoi(optarg); 183 break; 184 case 'd': 185 showdevs++; 186 break; 187 case 'f': 188 dev = optarg; 189 break; 190 case 'v': 191 verbose = 1; 192 break; 193 case '?': 194 default: 195 usage(); 196 } 197 } 198 argc -= optind; 199 argv += optind; 200 201 if (dev == 0) { 202 for (ncont = 0, i = 0; i < 10; i++) { 203 snprintf(buf, sizeof buf, "%s%d", USBDEV, i); 204 f = open(buf, O_RDONLY); 205 if (f >= 0) { 206 dumpone(buf, f, addr); 207 close(f); 208 } else { 209 if (errno == ENOENT || errno == ENXIO) 210 continue; 211 warn("%s", buf); 212 } 213 ncont++; 214 } 215 if (verbose && ncont == 0) 216 printf("%s: no USB controllers found\n", 217 __progname); 218 } else { 219 f = open(dev, O_RDONLY); 220 if (f >= 0) 221 dumpone(dev, f, addr); 222 else 223 err(1, "%s", dev); 224 } 225 exit(0); 226 } 227