1 /* $OpenBSD: usbdevs.c,v 1.21 2012/05/07 11:12:54 mpi 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <sys/types.h> 37 #include <fcntl.h> 38 #include <unistd.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <dev/usb/usb.h> 42 43 #define USBDEV "/dev/usb" 44 45 int verbose = 0; 46 int showdevs = 0; 47 48 void usage(void); 49 void usbdev(int f, int a, int rec); 50 void usbdump(int f); 51 void dumpone(char *name, int f, int addr); 52 int main(int, char **); 53 54 extern char *__progname; 55 56 void 57 usage(void) 58 { 59 fprintf(stderr, "usage: %s [-dv] [-a addr] [-f dev]\n", __progname); 60 exit(1); 61 } 62 63 char done[USB_MAX_DEVICES]; 64 int indent; 65 66 void 67 usbdev(int f, int a, int rec) 68 { 69 struct usb_device_info di; 70 int e, p, i; 71 72 di.udi_addr = a; 73 e = ioctl(f, USB_DEVICEINFO, &di); 74 if (e) { 75 if (errno != ENXIO) 76 printf("addr %d: I/O error\n", a); 77 return; 78 } 79 80 printf("addr %d: ", a); 81 done[a] = 1; 82 if (verbose) { 83 switch (di.udi_speed) { 84 case USB_SPEED_LOW: 85 printf("low speed, "); 86 break; 87 case USB_SPEED_FULL: 88 printf("full speed, "); 89 break; 90 case USB_SPEED_HIGH: 91 printf("high speed, "); 92 break; 93 default: 94 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 if (strlen(di.udi_serial)) 111 printf(", iSerialNumber %s", di.udi_serial); 112 } else 113 printf("%s, %s", di.udi_product, di.udi_vendor); 114 printf("\n"); 115 if (showdevs) { 116 for (i = 0; i < USB_MAX_DEVNAMES; i++) 117 if (di.udi_devnames[i][0]) 118 printf("%*s %s\n", indent, "", 119 di.udi_devnames[i]); 120 } 121 if (!rec) 122 return; 123 for (p = 0; p < di.udi_nports; p++) { 124 int s = di.udi_ports[p]; 125 126 if (s >= USB_MAX_DEVICES) { 127 if (verbose) { 128 printf("%*sport %d %s\n", indent+1, "", p+1, 129 s == USB_PORT_ENABLED ? "enabled" : 130 s == USB_PORT_SUSPENDED ? "suspended" : 131 s == USB_PORT_POWERED ? "powered" : 132 s == USB_PORT_DISABLED ? "disabled" : 133 "???"); 134 } 135 continue; 136 } 137 indent++; 138 printf("%*s", indent, ""); 139 if (verbose) 140 printf("port %d ", p+1); 141 if (s == 0) 142 printf("addr 0 should never happen!\n"); 143 else 144 usbdev(f, s, 1); 145 indent--; 146 } 147 } 148 149 void 150 usbdump(int f) 151 { 152 int a; 153 154 for (a = 1; a < USB_MAX_DEVICES; a++) { 155 if (!done[a]) 156 usbdev(f, a, 1); 157 } 158 } 159 160 void 161 dumpone(char *name, int f, int addr) 162 { 163 if (verbose) 164 printf("Controller %s:\n", name); 165 indent = 0; 166 memset(done, 0, sizeof done); 167 if (addr) 168 usbdev(f, addr, 0); 169 else 170 usbdump(f); 171 } 172 173 int 174 main(int argc, char **argv) 175 { 176 int ch, i, f; 177 char buf[50]; 178 char *dev = 0; 179 const char *errstr; 180 int addr = 0; 181 int ncont; 182 183 while ((ch = getopt(argc, argv, "a:df:v?")) != -1) { 184 switch (ch) { 185 case 'a': 186 addr = strtonum(optarg, 1, USB_MAX_DEVICES, &errstr); 187 if (errstr) 188 errx(1, "addr %s", errstr); 189 break; 190 case 'd': 191 showdevs++; 192 break; 193 case 'f': 194 dev = optarg; 195 break; 196 case 'v': 197 verbose = 1; 198 break; 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 snprintf(buf, sizeof 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