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