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