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