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