1 /* $NetBSD: usbdevs.c,v 1.39 2019/11/12 07:41:50 mrg 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 <sys/cdefs.h> 33 #ifndef lint 34 __RCSID("$NetBSD: usbdevs.c,v 1.39 2019/11/12 07:41:50 mrg Exp $"); 35 #endif 36 37 #include <sys/types.h> 38 #include <sys/drvctlio.h> 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <fcntl.h> 44 #include <unistd.h> 45 #include <err.h> 46 #include <errno.h> 47 #include <locale.h> 48 #include <langinfo.h> 49 #include <iconv.h> 50 #include <ctype.h> 51 52 #include <dev/usb/usb.h> 53 54 #define USBDEV "/dev/usb" 55 56 static int verbose = 0; 57 static int showdevs = 0; 58 59 struct stringtable { 60 int row, col; 61 const char *string; 62 }; 63 64 __dead static void usage(void); 65 static void getstrings(const struct stringtable *, int, int, const char **, const char **); 66 static void usbdev(int f, int a, int rec); 67 static void usbdump(int f); 68 static void dumpone(char *name, int f, int addr); 69 70 static void 71 usage(void) 72 { 73 74 fprintf(stderr, "usage: %s [-dv] [-a addr] [-f dev]\n", 75 getprogname()); 76 exit(EXIT_FAILURE); 77 } 78 79 static char done[USB_MAX_DEVICES]; 80 static int indent; 81 #define MAXLEN USB_MAX_ENCODED_STRING_LEN /* assume can't grow over UTF-8 */ 82 static char vendor[MAXLEN], product[MAXLEN], serial[MAXLEN]; 83 84 static void 85 u2t(const char *utf8str, char *termstr) 86 { 87 static iconv_t ic; 88 static int iconv_inited = 0; 89 size_t insz, outsz, icres; 90 91 if (!iconv_inited) { 92 setlocale(LC_ALL, ""); 93 ic = iconv_open(nl_langinfo(CODESET), "UTF-8"); 94 if (ic == (iconv_t)-1) 95 ic = iconv_open("ASCII", "UTF-8"); /* g.c.d. */ 96 iconv_inited = 1; 97 } 98 if (ic != (iconv_t)-1) { 99 insz = strlen(utf8str); 100 outsz = MAXLEN - 1; 101 icres = iconv(ic, __UNCONST(&utf8str), &insz, &termstr, 102 &outsz); 103 if (icres != (size_t)-1) { 104 *termstr = '\0'; 105 return; 106 } 107 } 108 strcpy(termstr, "(invalid)"); 109 } 110 111 struct stringtable class_strings[] = { 112 { UICLASS_UNSPEC, -1, "Unspecified" }, 113 114 { UICLASS_AUDIO, -1, "Audio" }, 115 { UICLASS_AUDIO, UISUBCLASS_AUDIOCONTROL, "Audio Control" }, 116 { UICLASS_AUDIO, UISUBCLASS_AUDIOSTREAM, "Audio Streaming" }, 117 { UICLASS_AUDIO, UISUBCLASS_MIDISTREAM, "MIDI Streaming" }, 118 119 { UICLASS_CDC, -1, "Communications and CDC Control" }, 120 { UICLASS_CDC, UISUBCLASS_DIRECT_LINE_CONTROL_MODEL, "Direct Line" }, 121 { UICLASS_CDC, UISUBCLASS_ABSTRACT_CONTROL_MODEL, "Abstract" }, 122 { UICLASS_CDC, UISUBCLASS_TELEPHONE_CONTROL_MODEL, "Telephone" }, 123 { UICLASS_CDC, UISUBCLASS_MULTICHANNEL_CONTROL_MODEL, "Multichannel" }, 124 { UICLASS_CDC, UISUBCLASS_CAPI_CONTROLMODEL, "CAPI" }, 125 { UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, "Ethernet Networking" }, 126 { UICLASS_CDC, UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL, "ATM Networking" }, 127 128 { UICLASS_HID, -1, "Human Interface Device" }, 129 { UICLASS_HID, UISUBCLASS_BOOT, "Boot" }, 130 131 { UICLASS_PHYSICAL, -1, "Physical" }, 132 133 { UICLASS_IMAGE, -1, "Image" }, 134 135 { UICLASS_PRINTER, -1, "Printer" }, 136 { UICLASS_PRINTER, UISUBCLASS_PRINTER, "Printer" }, 137 138 { UICLASS_MASS, -1, "Mass Storage" }, 139 { UICLASS_MASS, UISUBCLASS_RBC, "RBC" }, 140 { UICLASS_MASS, UISUBCLASS_SFF8020I, "SFF8020I" }, 141 { UICLASS_MASS, UISUBCLASS_QIC157, "QIC157" }, 142 { UICLASS_MASS, UISUBCLASS_UFI, "UFI" }, 143 { UICLASS_MASS, UISUBCLASS_SFF8070I, "SFF8070I" }, 144 { UICLASS_MASS, UISUBCLASS_SCSI, "SCSI" }, 145 { UICLASS_MASS, UISUBCLASS_SCSI, "SCSI" }, 146 147 { UICLASS_HUB, -1, "Hub" }, 148 { UICLASS_HUB, UISUBCLASS_HUB, "Hub" }, 149 150 { UICLASS_CDC_DATA, -1, "CDC-Data" }, 151 { UICLASS_CDC_DATA, UISUBCLASS_DATA, "Data" }, 152 153 { UICLASS_SMARTCARD, -1, "Smart Card" }, 154 155 { UICLASS_SECURITY, -1, "Content Security" }, 156 157 { UICLASS_VIDEO, -1, "Video" }, 158 { UICLASS_VIDEO, UISUBCLASS_VIDEOCONTROL, "Video Control" }, 159 { UICLASS_VIDEO, UISUBCLASS_VIDEOSTREAMING, "Video Streaming" }, 160 { UICLASS_VIDEO, UISUBCLASS_VIDEOCOLLECTION, "Video Collection" }, 161 162 #ifdef notyet 163 { UICLASS_HEALTHCARE, -1, "Personal Healthcare" }, 164 { UICLASS_AVDEVICE, -1, "Audio/Video Device" }, 165 { UICLASS_BILLBOARD, -1, "Billboard" }, 166 #endif 167 168 { UICLASS_DIAGNOSTIC, -1, "Diagnostic" }, 169 { UICLASS_WIRELESS, -1, "Wireless" }, 170 { UICLASS_WIRELESS, UISUBCLASS_RF, "Radio Frequency" }, 171 172 #ifdef notyet 173 { UICLASS_MISC, -1, "Miscellaneous" }, 174 #endif 175 176 { UICLASS_APPL_SPEC, -1, "Application Specific" }, 177 { UICLASS_APPL_SPEC, UISUBCLASS_FIRMWARE_DOWNLOAD, "Firmware Download" }, 178 { UICLASS_APPL_SPEC, UISUBCLASS_IRDA, "Irda" }, 179 180 { UICLASS_VENDOR, -1, "Vendor Specific" }, 181 182 { -1, -1, NULL } 183 }; 184 185 static void 186 getstrings(const struct stringtable *table, 187 int row, int col, const char **rp, const char **cp) { 188 static char rbuf[5], cbuf[5]; 189 190 snprintf(rbuf, sizeof(rbuf), "0x%02x", row); 191 snprintf(cbuf, sizeof(cbuf), "0x%02x", col); 192 193 *rp = rbuf; 194 *cp = cbuf; 195 196 while (table->string != NULL) { 197 if (table->row == row) { 198 if (table->col == -1) 199 *rp = table->string; 200 else if (table->col == col) 201 *cp = table->string; 202 } else if (table->row > row) 203 break; 204 205 ++table; 206 } 207 } 208 209 static void 210 usbdev(int f, int a, int rec) 211 { 212 struct usb_device_info di; 213 int e, i; 214 215 di.udi_addr = a; 216 e = ioctl(f, USB_DEVICEINFO, &di); 217 if (e) { 218 if (errno != ENXIO) 219 printf("addr %d: I/O error\n", a); 220 return; 221 } 222 printf("addr %d: ", a); 223 done[a] = 1; 224 if (verbose) { 225 switch (di.udi_speed) { 226 case USB_SPEED_LOW: printf("low speed, "); break; 227 case USB_SPEED_FULL: printf("full speed, "); break; 228 case USB_SPEED_HIGH: printf("high speed, "); break; 229 case USB_SPEED_SUPER: printf("super speed, "); break; 230 case USB_SPEED_SUPER_PLUS: printf("super speed+, "); break; 231 default: break; 232 } 233 if (di.udi_power) 234 printf("power %d mA, ", di.udi_power); 235 else 236 printf("self powered, "); 237 if (di.udi_config) 238 printf("config %d, ", di.udi_config); 239 else 240 printf("unconfigured, "); 241 } 242 u2t(di.udi_product, product); 243 u2t(di.udi_vendor, vendor); 244 u2t(di.udi_serial, serial); 245 if (verbose) { 246 printf("%s(0x%04x), %s(0x%04x), rev %s(0x%04x)", 247 product, di.udi_productNo, 248 vendor, di.udi_vendorNo, 249 di.udi_release, di.udi_releaseNo); 250 if (di.udi_serial[0]) 251 printf(", serial %s", serial); 252 } else 253 printf("%s, %s", product, vendor); 254 printf("\n"); 255 if (verbose > 1 && di.udi_class != UICLASS_UNSPEC) { 256 const char *cstr, *sstr; 257 getstrings(class_strings, di.udi_class, di.udi_subclass, &cstr, &sstr); 258 printf("%*s %s(0x%02x), %s(0x%02x), proto %u\n", indent, "", 259 cstr, di.udi_class, sstr, di.udi_subclass, 260 di.udi_protocol); 261 } 262 if (showdevs) { 263 for (i = 0; i < USB_MAX_DEVNAMES; i++) 264 if (di.udi_devnames[i][0]) 265 printf("%*s %s\n", indent, "", 266 di.udi_devnames[i]); 267 } 268 if (!rec) 269 return; 270 271 unsigned int nports = di.udi_nports; 272 273 for (unsigned int p = 0; p < nports && p < __arraycount(di.udi_ports); p++) { 274 int s = di.udi_ports[p]; 275 if (s >= USB_MAX_DEVICES) { 276 if (verbose) { 277 printf("%*sport %d %s\n", indent+1, "", p+1, 278 s == USB_PORT_ENABLED ? "enabled" : 279 s == USB_PORT_SUSPENDED ? "suspended" : 280 s == USB_PORT_POWERED ? "powered" : 281 s == USB_PORT_DISABLED ? "disabled" : 282 "???"); 283 284 } 285 continue; 286 } 287 indent++; 288 printf("%*s", indent, ""); 289 if (verbose) 290 printf("port %d ", p+1); 291 if (s == 0) 292 printf("addr 0 should never happen!\n"); 293 else 294 usbdev(f, s, 1); 295 indent--; 296 } 297 } 298 299 static void 300 usbdump(int f) 301 { 302 int a; 303 304 for (a = 0; a < USB_MAX_DEVICES; a++) { 305 if (!done[a]) 306 usbdev(f, a, 1); 307 } 308 } 309 310 static void 311 dumpone(char *name, int f, int addr) 312 { 313 if (verbose) 314 printf("Controller %s:\n", name); 315 indent = 0; 316 memset(done, 0, sizeof done); 317 if (addr >= 0) 318 usbdev(f, addr, 0); 319 else 320 usbdump(f); 321 } 322 323 static int 324 getusbcount_device(int fd, const char *dev, int depth) 325 { 326 struct devlistargs laa = { 327 .l_childname = NULL, 328 .l_children = 0, 329 }; 330 size_t i; 331 size_t children; 332 int nbusses = 0; 333 334 if (depth && (dev == NULL || *dev == '\0')) 335 return 0; 336 337 /* 338 * Look for children that match "usb[0-9]*". Could maybe 339 * simply return 1 here, but there's always a chance that 340 * someone has eg, a USB to PCI bridge, with a USB 341 * controller behind PCI. 342 */ 343 if (strncmp(dev, "usb", 3) == 0 && isdigit((int)dev[3])) 344 nbusses++; 345 346 strlcpy(laa.l_devname, dev, sizeof(laa.l_devname)); 347 348 if (ioctl(fd, DRVLISTDEV, &laa) == -1) 349 err(EXIT_FAILURE, "DRVLISTDEV"); 350 children = laa.l_children; 351 352 laa.l_childname = malloc(children * sizeof(laa.l_childname[0])); 353 if (laa.l_childname == NULL) 354 err(EXIT_FAILURE, "out of memory"); 355 if (ioctl(fd, DRVLISTDEV, &laa) == -1) 356 err(EXIT_FAILURE, "DRVLISTDEV"); 357 if (laa.l_children > children) 358 err(EXIT_FAILURE, "DRVLISTDEV: number of children grew"); 359 360 for (i = 0; i < laa.l_children; i++) { 361 nbusses += getusbcount_device(fd, laa.l_childname[i], depth+1); 362 } 363 364 return nbusses; 365 } 366 367 int 368 main(int argc, char **argv) 369 { 370 int ch, i, f; 371 char buf[50]; 372 char *dev = NULL; 373 int addr = -1; 374 int ncont; 375 376 while ((ch = getopt(argc, argv, "a:df:v?")) != -1) { 377 switch(ch) { 378 case 'a': 379 addr = atoi(optarg); 380 break; 381 case 'd': 382 showdevs++; 383 break; 384 case 'f': 385 dev = optarg; 386 break; 387 case 'v': 388 verbose++; 389 break; 390 case '?': 391 default: 392 usage(); 393 } 394 } 395 argc -= optind; 396 argv += optind; 397 398 if (dev == NULL) { 399 int nbusses; 400 int fd = open(DRVCTLDEV, O_RDONLY, 0); 401 402 /* If no drvctl configured, default to 16. */ 403 if (fd != -1) 404 nbusses = getusbcount_device(fd, "", 0); 405 else 406 nbusses = 16; 407 close(fd); 408 409 for (ncont = 0, i = 0; i < nbusses; i++) { 410 snprintf(buf, sizeof(buf), "%s%d", USBDEV, i); 411 f = open(buf, O_RDONLY); 412 if (f >= 0) { 413 dumpone(buf, f, addr); 414 close(f); 415 } else { 416 if (errno == ENOENT || errno == ENXIO) 417 continue; 418 warn("%s", buf); 419 } 420 ncont++; 421 } 422 if (verbose && ncont == 0) 423 printf("%s: no USB controllers found\n", 424 getprogname()); 425 } else { 426 f = open(dev, O_RDONLY); 427 if (f >= 0) 428 dumpone(dev, f, addr); 429 else 430 err(1, "%s", dev); 431 } 432 exit(EXIT_SUCCESS); 433 } 434