1 /* $OpenBSD: usbdevs.c,v 1.17 2008/09/04 11:46:18 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 int getdevicedesc(int, int, usb_device_descriptor_t *); 51 void getstring(int, int, int, char *, int); 52 void usbdump(int f); 53 void dumpone(char *name, int f, int addr); 54 int main(int, char **); 55 56 extern char *__progname; 57 58 void 59 usage(void) 60 { 61 fprintf(stderr, "usage: %s [-dv] [-a addr] [-f dev]\n", __progname); 62 exit(1); 63 } 64 65 char done[USB_MAX_DEVICES]; 66 int indent; 67 68 void 69 usbdev(int f, int a, int rec) 70 { 71 struct usb_device_info di; 72 usb_device_descriptor_t dd; 73 char serialnum[USB_MAX_STRING_LEN]; 74 struct usb_ctl_request req; 75 usb_string_descriptor_t us; 76 int e, p, i; 77 int langid = 0; 78 79 di.udi_addr = a; 80 e = ioctl(f, USB_DEVICEINFO, &di); 81 if (e) { 82 if (errno != ENXIO) 83 printf("addr %d: I/O error\n", a); 84 return; 85 } 86 87 req.ucr_addr = a; 88 req.ucr_request.bmRequestType = UT_READ_DEVICE; 89 req.ucr_request.bRequest = UR_GET_DESCRIPTOR; 90 req.ucr_data = &us; 91 USETW2(req.ucr_request.wValue, UDESC_STRING, 0); 92 USETW(req.ucr_request.wIndex, 0); 93 USETW(req.ucr_request.wLength, 4); 94 req.ucr_flags = 0; 95 if (ioctl(f, USB_REQUEST, &req) >= 0) 96 langid = UGETW(us.bString[0]); 97 98 bzero(serialnum, sizeof serialnum); 99 if (getdevicedesc(f, a, &dd)) 100 getstring(f, a, dd.iSerialNumber, serialnum, langid); 101 102 printf("addr %d: ", a); 103 done[a] = 1; 104 if (verbose) { 105 switch (di.udi_speed) { 106 case USB_SPEED_LOW: 107 printf("low speed, "); 108 break; 109 case USB_SPEED_FULL: 110 printf("full speed, "); 111 break; 112 case USB_SPEED_HIGH: 113 printf("high speed, "); 114 break; 115 default: 116 break; 117 } 118 119 if (di.udi_power) 120 printf("power %d mA, ", di.udi_power); 121 else 122 printf("self powered, "); 123 if (di.udi_config) 124 printf("config %d, ", di.udi_config); 125 else 126 printf("unconfigured, "); 127 } 128 if (verbose) { 129 printf("%s(0x%04x), %s(0x%04x), rev %s", 130 di.udi_product, di.udi_productNo, 131 di.udi_vendor, di.udi_vendorNo, di.udi_release); 132 if (strlen(serialnum)) 133 printf(", iSerialNumber %s", serialnum); 134 } else 135 printf("%s, %s", di.udi_product, di.udi_vendor); 136 printf("\n"); 137 if (showdevs) { 138 for (i = 0; i < USB_MAX_DEVNAMES; i++) 139 if (di.udi_devnames[i][0]) 140 printf("%*s %s\n", indent, "", 141 di.udi_devnames[i]); 142 } 143 if (!rec) 144 return; 145 for (p = 0; p < di.udi_nports; p++) { 146 int s = di.udi_ports[p]; 147 148 if (s >= USB_MAX_DEVICES) { 149 if (verbose) { 150 printf("%*sport %d %s\n", indent+1, "", p+1, 151 s == USB_PORT_ENABLED ? "enabled" : 152 s == USB_PORT_SUSPENDED ? "suspended" : 153 s == USB_PORT_POWERED ? "powered" : 154 s == USB_PORT_DISABLED ? "disabled" : 155 "???"); 156 } 157 continue; 158 } 159 indent++; 160 printf("%*s", indent, ""); 161 if (verbose) 162 printf("port %d ", p+1); 163 if (s == 0) 164 printf("addr 0 should never happen!\n"); 165 else 166 usbdev(f, s, 1); 167 indent--; 168 } 169 } 170 171 int 172 getdevicedesc(int f, int addr, usb_device_descriptor_t *d) 173 { 174 struct usb_ctl_request req; 175 int r; 176 177 req.ucr_addr = addr; 178 req.ucr_request.bmRequestType = UT_READ_DEVICE; 179 req.ucr_request.bRequest = UR_GET_DESCRIPTOR; 180 USETW2(req.ucr_request.wValue, UDESC_DEVICE, 0); 181 USETW(req.ucr_request.wIndex, 0); 182 USETW(req.ucr_request.wLength, USB_DEVICE_DESCRIPTOR_SIZE); 183 req.ucr_data = d; 184 req.ucr_flags = 0; 185 if ((r = ioctl(f, USB_REQUEST, &req)) == -1) 186 perror("getdevicedesc: ioctl"); 187 return (r != -1); 188 } 189 190 void 191 getstring(int f, int addr, int si, char *s, int langid) 192 { 193 struct usb_ctl_request req; 194 usb_string_descriptor_t us; 195 int r, i, n; 196 u_int16_t c; 197 198 if (si == 0) { 199 *s = 0; 200 return; 201 } 202 req.ucr_addr = addr; 203 req.ucr_request.bmRequestType = UT_READ_DEVICE; 204 req.ucr_request.bRequest = UR_GET_DESCRIPTOR; 205 req.ucr_data = &us; 206 USETW2(req.ucr_request.wValue, UDESC_STRING, si); 207 USETW(req.ucr_request.wIndex, langid); 208 USETW(req.ucr_request.wLength, sizeof(usb_string_descriptor_t)); 209 req.ucr_flags = USBD_SHORT_XFER_OK; 210 211 if (ioctl(f, USB_REQUEST, &req) == -1){ 212 perror("getstring: ioctl"); 213 *s = 0; 214 return; 215 } 216 217 n = us.bLength / 2 - 1; 218 for (i = 0; i < n; i++) { 219 c = UGETW(us.bString[i]); 220 if ((c & 0xff00) == 0) 221 *s++ = c; 222 else if ((c & 0x00ff) == 0) 223 *s++ = c >> 8; 224 else { 225 snprintf(s, 6, "\\u%04x", c); 226 s += 6; 227 } 228 } 229 *s++ = 0; 230 } 231 232 void 233 usbdump(int f) 234 { 235 int a; 236 237 for (a = 1; a < USB_MAX_DEVICES; a++) { 238 if (!done[a]) 239 usbdev(f, a, 1); 240 } 241 } 242 243 void 244 dumpone(char *name, int f, int addr) 245 { 246 if (verbose) 247 printf("Controller %s:\n", name); 248 indent = 0; 249 memset(done, 0, sizeof done); 250 if (addr) 251 usbdev(f, addr, 0); 252 else 253 usbdump(f); 254 } 255 256 int 257 main(int argc, char **argv) 258 { 259 int ch, i, f; 260 char buf[50]; 261 char *dev = 0; 262 int addr = 0; 263 int ncont; 264 265 while ((ch = getopt(argc, argv, "a:df:v?")) != -1) { 266 switch (ch) { 267 case 'a': 268 addr = atoi(optarg); 269 break; 270 case 'd': 271 showdevs++; 272 break; 273 case 'f': 274 dev = optarg; 275 break; 276 case 'v': 277 verbose = 1; 278 break; 279 default: 280 usage(); 281 } 282 } 283 argc -= optind; 284 argv += optind; 285 286 if (dev == 0) { 287 for (ncont = 0, i = 0; i < 10; i++) { 288 snprintf(buf, sizeof buf, "%s%d", USBDEV, i); 289 f = open(buf, O_RDWR); 290 if (f >= 0) { 291 dumpone(buf, f, addr); 292 close(f); 293 } else { 294 if (errno == ENOENT || errno == ENXIO) 295 continue; 296 warn("%s", buf); 297 } 298 ncont++; 299 } 300 if (verbose && ncont == 0) 301 printf("%s: no USB controllers found\n", 302 __progname); 303 } else { 304 f = open(dev, O_RDWR); 305 if (f >= 0) 306 dumpone(dev, f, addr); 307 else 308 err(1, "%s", dev); 309 } 310 exit(0); 311 } 312