Home | History | Annotate | Line # | Download | only in usbdevs
usbdevs.c revision 1.35.6.1
      1  1.35.6.1    martin /*	$NetBSD: usbdevs.c,v 1.35.6.1 2019/11/16 16:32:23 martin Exp $	*/
      2       1.2  augustss 
      3       1.2  augustss /*
      4       1.2  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5       1.2  augustss  * All rights reserved.
      6       1.2  augustss  *
      7       1.5  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8      1.22     grant  * by Lennart Augustsson (augustss (at) NetBSD.org).
      9       1.2  augustss  *
     10       1.2  augustss  * Redistribution and use in source and binary forms, with or without
     11       1.2  augustss  * modification, are permitted provided that the following conditions
     12       1.2  augustss  * are met:
     13       1.2  augustss  * 1. Redistributions of source code must retain the above copyright
     14       1.2  augustss  *    notice, this list of conditions and the following disclaimer.
     15       1.2  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.2  augustss  *    notice, this list of conditions and the following disclaimer in the
     17       1.2  augustss  *    documentation and/or other materials provided with the distribution.
     18       1.2  augustss  *
     19       1.2  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.2  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.2  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.2  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.2  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.2  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.2  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.2  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.2  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.2  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.2  augustss  * POSSIBILITY OF SUCH DAMAGE.
     30       1.2  augustss  */
     31       1.2  augustss 
     32      1.33     skrll #include <sys/cdefs.h>
     33      1.33     skrll #ifndef lint
     34  1.35.6.1    martin __RCSID("$NetBSD: usbdevs.c,v 1.35.6.1 2019/11/16 16:32:23 martin Exp $");
     35      1.33     skrll #endif
     36      1.33     skrll 
     37       1.1  augustss #include <stdio.h>
     38       1.1  augustss #include <stdlib.h>
     39       1.1  augustss #include <string.h>
     40       1.1  augustss #include <sys/types.h>
     41       1.1  augustss #include <fcntl.h>
     42       1.1  augustss #include <unistd.h>
     43       1.1  augustss #include <err.h>
     44       1.3  augustss #include <errno.h>
     45      1.26  drochner #include <locale.h>
     46      1.26  drochner #include <langinfo.h>
     47      1.26  drochner #include <iconv.h>
     48       1.1  augustss #include <dev/usb/usb.h>
     49       1.1  augustss 
     50       1.1  augustss #define USBDEV "/dev/usb"
     51       1.1  augustss 
     52      1.28     joerg static int verbose = 0;
     53      1.28     joerg static int showdevs = 0;
     54       1.1  augustss 
     55      1.32   mlelstv struct stringtable {
     56      1.32   mlelstv 	int row, col;
     57      1.32   mlelstv 	const char *string;
     58      1.32   mlelstv };
     59      1.32   mlelstv 
     60      1.28     joerg __dead static void usage(void);
     61      1.32   mlelstv static void getstrings(const struct stringtable *, int, int, const char **, const char **);
     62      1.28     joerg static void usbdev(int f, int a, int rec);
     63      1.28     joerg static void usbdump(int f);
     64      1.28     joerg static void dumpone(char *name, int f, int addr);
     65       1.1  augustss 
     66      1.28     joerg static void
     67      1.28     joerg usage(void)
     68       1.1  augustss {
     69      1.20    simonb 
     70      1.27       wiz 	fprintf(stderr, "usage: %s [-dv] [-a addr] [-f dev]\n",
     71      1.17       cgd 	    getprogname());
     72      1.29  jakllsch 	exit(EXIT_FAILURE);
     73       1.1  augustss }
     74       1.1  augustss 
     75      1.28     joerg static char done[USB_MAX_DEVICES];
     76      1.28     joerg static int indent;
     77      1.26  drochner #define MAXLEN USB_MAX_ENCODED_STRING_LEN /* assume can't grow over UTF-8 */
     78      1.28     joerg static char vendor[MAXLEN], product[MAXLEN], serial[MAXLEN];
     79      1.26  drochner 
     80      1.26  drochner static void
     81      1.26  drochner u2t(const char *utf8str, char *termstr)
     82      1.26  drochner {
     83      1.26  drochner 	static iconv_t ic;
     84      1.26  drochner 	static int iconv_inited = 0;
     85      1.26  drochner 	size_t insz, outsz, icres;
     86      1.26  drochner 
     87      1.26  drochner 	if (!iconv_inited) {
     88      1.26  drochner 		setlocale(LC_ALL, "");
     89      1.26  drochner 		ic = iconv_open(nl_langinfo(CODESET), "UTF-8");
     90      1.26  drochner 		if (ic == (iconv_t)-1)
     91      1.26  drochner 			ic = iconv_open("ASCII", "UTF-8"); /* g.c.d. */
     92      1.26  drochner 		iconv_inited = 1;
     93      1.26  drochner 	}
     94      1.26  drochner 	if (ic != (iconv_t)-1) {
     95      1.26  drochner 		insz = strlen(utf8str);
     96      1.26  drochner 		outsz = MAXLEN - 1;
     97      1.26  drochner 		icres = iconv(ic, &utf8str, &insz, &termstr, &outsz);
     98      1.26  drochner 		if (icres != (size_t)-1) {
     99      1.26  drochner 			*termstr = '\0';
    100      1.26  drochner 			return;
    101      1.26  drochner 		}
    102      1.26  drochner 	}
    103      1.26  drochner 	strcpy(termstr, "(invalid)");
    104      1.26  drochner }
    105       1.1  augustss 
    106      1.32   mlelstv struct stringtable class_strings[] = {
    107      1.32   mlelstv 	{ UICLASS_UNSPEC,      -1, "Unspecified" },
    108      1.32   mlelstv 
    109      1.32   mlelstv 	{ UICLASS_AUDIO,       -1, "Audio" },
    110      1.32   mlelstv 	{ UICLASS_AUDIO,       UISUBCLASS_AUDIOCONTROL, "Audio Control" },
    111      1.32   mlelstv 	{ UICLASS_AUDIO,       UISUBCLASS_AUDIOSTREAM, "Audio Streaming" },
    112      1.32   mlelstv 	{ UICLASS_AUDIO,       UISUBCLASS_MIDISTREAM, "MIDI Streaming" },
    113      1.32   mlelstv 
    114      1.32   mlelstv 	{ UICLASS_CDC,         -1, "Communications and CDC Control" },
    115      1.32   mlelstv 	{ UICLASS_CDC,         UISUBCLASS_DIRECT_LINE_CONTROL_MODEL, "Direct Line" },
    116      1.32   mlelstv 	{ UICLASS_CDC,         UISUBCLASS_ABSTRACT_CONTROL_MODEL, "Abstract" },
    117      1.32   mlelstv 	{ UICLASS_CDC,         UISUBCLASS_TELEPHONE_CONTROL_MODEL, "Telephone" },
    118      1.32   mlelstv 	{ UICLASS_CDC,         UISUBCLASS_MULTICHANNEL_CONTROL_MODEL, "Multichannel" },
    119      1.32   mlelstv 	{ UICLASS_CDC,         UISUBCLASS_CAPI_CONTROLMODEL, "CAPI" },
    120      1.32   mlelstv 	{ UICLASS_CDC,         UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, "Ethernet Networking" },
    121      1.32   mlelstv 	{ UICLASS_CDC,         UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL, "ATM Networking" },
    122      1.32   mlelstv 
    123      1.32   mlelstv 	{ UICLASS_HID,         -1, "Human Interface Device" },
    124      1.32   mlelstv 	{ UICLASS_HID,         UISUBCLASS_BOOT, "Boot" },
    125      1.32   mlelstv 
    126      1.32   mlelstv 	{ UICLASS_PHYSICAL,    -1, "Physical" },
    127      1.32   mlelstv 
    128      1.32   mlelstv 	{ UICLASS_IMAGE,       -1, "Image" },
    129      1.32   mlelstv 
    130      1.32   mlelstv 	{ UICLASS_PRINTER,     -1, "Printer" },
    131      1.32   mlelstv 	{ UICLASS_PRINTER,     UISUBCLASS_PRINTER, "Printer" },
    132      1.32   mlelstv 
    133      1.32   mlelstv 	{ UICLASS_MASS,        -1, "Mass Storage" },
    134      1.32   mlelstv 	{ UICLASS_MASS,        UISUBCLASS_RBC, "RBC" },
    135      1.32   mlelstv 	{ UICLASS_MASS,        UISUBCLASS_SFF8020I, "SFF8020I" },
    136      1.32   mlelstv 	{ UICLASS_MASS,        UISUBCLASS_QIC157, "QIC157" },
    137      1.32   mlelstv 	{ UICLASS_MASS,        UISUBCLASS_UFI, "UFI" },
    138      1.32   mlelstv 	{ UICLASS_MASS,        UISUBCLASS_SFF8070I, "SFF8070I" },
    139      1.32   mlelstv 	{ UICLASS_MASS,        UISUBCLASS_SCSI, "SCSI" },
    140      1.32   mlelstv 	{ UICLASS_MASS,        UISUBCLASS_SCSI, "SCSI" },
    141      1.32   mlelstv 
    142      1.32   mlelstv 	{ UICLASS_HUB,         -1, "Hub" },
    143      1.32   mlelstv 	{ UICLASS_HUB,         UISUBCLASS_HUB, "Hub" },
    144      1.32   mlelstv 
    145      1.32   mlelstv 	{ UICLASS_CDC_DATA,    -1, "CDC-Data" },
    146      1.32   mlelstv 	{ UICLASS_CDC_DATA,    UISUBCLASS_DATA, "Data" },
    147      1.32   mlelstv 
    148      1.32   mlelstv 	{ UICLASS_SMARTCARD,   -1, "Smart Card" },
    149      1.32   mlelstv 
    150      1.32   mlelstv 	{ UICLASS_SECURITY,    -1, "Content Security" },
    151      1.32   mlelstv 
    152      1.32   mlelstv 	{ UICLASS_VIDEO,       -1, "Video" },
    153      1.32   mlelstv 	{ UICLASS_VIDEO,       UISUBCLASS_VIDEOCONTROL, "Video Control" },
    154      1.32   mlelstv 	{ UICLASS_VIDEO,       UISUBCLASS_VIDEOSTREAMING, "Video Streaming" },
    155      1.32   mlelstv 	{ UICLASS_VIDEO,       UISUBCLASS_VIDEOCOLLECTION, "Video Collection" },
    156      1.32   mlelstv 
    157      1.32   mlelstv #ifdef notyet
    158      1.32   mlelstv 	{ UICLASS_HEALTHCARE,  -1, "Personal Healthcare" },
    159      1.32   mlelstv 	{ UICLASS_AVDEVICE,    -1, "Audio/Video Device" },
    160      1.32   mlelstv 	{ UICLASS_BILLBOARD,   -1, "Billboard" },
    161      1.32   mlelstv #endif
    162      1.32   mlelstv 
    163      1.32   mlelstv 	{ UICLASS_DIAGNOSTIC,  -1, "Diagnostic" },
    164      1.32   mlelstv 	{ UICLASS_WIRELESS,    -1, "Wireless" },
    165      1.32   mlelstv 	{ UICLASS_WIRELESS,    UISUBCLASS_RF, "Radio Frequency" },
    166      1.32   mlelstv 
    167      1.32   mlelstv #ifdef notyet
    168      1.32   mlelstv 	{ UICLASS_MISC,        -1, "Miscellaneous" },
    169      1.32   mlelstv #endif
    170      1.32   mlelstv 
    171      1.32   mlelstv 	{ UICLASS_APPL_SPEC,   -1, "Application Specific" },
    172      1.32   mlelstv 	{ UICLASS_APPL_SPEC,   UISUBCLASS_FIRMWARE_DOWNLOAD, "Firmware Download" },
    173      1.32   mlelstv 	{ UICLASS_APPL_SPEC,   UISUBCLASS_IRDA,              "Irda" },
    174      1.32   mlelstv 
    175      1.32   mlelstv 	{ UICLASS_VENDOR,      -1, "Vendor Specific" },
    176      1.32   mlelstv 
    177      1.32   mlelstv 	{ -1, -1, NULL }
    178      1.32   mlelstv };
    179      1.32   mlelstv 
    180      1.32   mlelstv static void
    181      1.32   mlelstv getstrings(const struct stringtable *table,
    182      1.32   mlelstv            int row, int col, const char **rp, const char **cp) {
    183      1.32   mlelstv 	static char rbuf[5], cbuf[5];
    184      1.32   mlelstv 
    185      1.32   mlelstv 	snprintf(rbuf, sizeof(rbuf), "0x%02x", row);
    186      1.32   mlelstv 	snprintf(cbuf, sizeof(cbuf), "0x%02x", col);
    187      1.32   mlelstv 
    188      1.32   mlelstv 	*rp = rbuf;
    189      1.32   mlelstv 	*cp = cbuf;
    190      1.32   mlelstv 
    191      1.32   mlelstv 	while (table->string != NULL) {
    192      1.32   mlelstv 		if (table->row == row) {
    193      1.32   mlelstv 			if (table->col == -1)
    194      1.32   mlelstv 				*rp = table->string;
    195      1.32   mlelstv 			else if (table->col == col)
    196      1.32   mlelstv 				*cp = table->string;
    197      1.32   mlelstv 		} else if (table->row > row)
    198      1.32   mlelstv 			break;
    199      1.32   mlelstv 
    200      1.32   mlelstv 		++table;
    201      1.32   mlelstv 	}
    202      1.32   mlelstv }
    203      1.32   mlelstv 
    204      1.28     joerg static void
    205      1.14  augustss usbdev(int f, int a, int rec)
    206       1.1  augustss {
    207       1.1  augustss 	struct usb_device_info di;
    208      1.35     skrll 	int e, i;
    209       1.1  augustss 
    210      1.19  christos 	di.udi_addr = a;
    211       1.1  augustss 	e = ioctl(f, USB_DEVICEINFO, &di);
    212      1.10  augustss 	if (e) {
    213      1.10  augustss 		if (errno != ENXIO)
    214      1.10  augustss 			printf("addr %d: I/O error\n", a);
    215       1.1  augustss 		return;
    216      1.10  augustss 	}
    217      1.10  augustss 	printf("addr %d: ", a);
    218       1.1  augustss 	done[a] = 1;
    219       1.1  augustss 	if (verbose) {
    220      1.19  christos 		switch (di.udi_speed) {
    221      1.18  augustss 		case USB_SPEED_LOW:  printf("low speed, "); break;
    222      1.18  augustss 		case USB_SPEED_FULL: printf("full speed, "); break;
    223      1.18  augustss 		case USB_SPEED_HIGH: printf("high speed, "); break;
    224      1.31     skrll 		case USB_SPEED_SUPER: printf("super speed, "); break;
    225  1.35.6.1    martin 		case USB_SPEED_SUPER_PLUS: printf("super speed+, "); break;
    226      1.18  augustss 		default: break;
    227      1.18  augustss 		}
    228      1.19  christos 		if (di.udi_power)
    229      1.19  christos 			printf("power %d mA, ", di.udi_power);
    230       1.1  augustss 		else
    231       1.1  augustss 			printf("self powered, ");
    232      1.19  christos 		if (di.udi_config)
    233      1.19  christos 			printf("config %d, ", di.udi_config);
    234       1.1  augustss 		else
    235       1.1  augustss 			printf("unconfigured, ");
    236       1.1  augustss 	}
    237      1.26  drochner 	u2t(di.udi_product, product);
    238      1.26  drochner 	u2t(di.udi_vendor, vendor);
    239      1.26  drochner 	u2t(di.udi_serial, serial);
    240       1.6  augustss 	if (verbose) {
    241      1.32   mlelstv 		printf("%s(0x%04x), %s(0x%04x), rev %s(0x%04x)",
    242      1.26  drochner 		       product, di.udi_productNo,
    243      1.32   mlelstv 		       vendor, di.udi_vendorNo,
    244      1.32   mlelstv 			di.udi_release, di.udi_releaseNo);
    245      1.24  augustss 		if (di.udi_serial[0])
    246      1.26  drochner 			printf(", serial %s", serial);
    247       1.6  augustss 	} else
    248      1.26  drochner 		printf("%s, %s", product, vendor);
    249       1.1  augustss 	printf("\n");
    250      1.32   mlelstv 	if (verbose > 1 && di.udi_class != UICLASS_UNSPEC) {
    251      1.32   mlelstv 		const char *cstr, *sstr;
    252      1.32   mlelstv 		getstrings(class_strings, di.udi_class, di.udi_subclass, &cstr, &sstr);
    253      1.32   mlelstv 		printf("%*s  %s(0x%02x), %s(0x%02x), proto %u\n", indent, "",
    254      1.32   mlelstv 			cstr, di.udi_class, sstr, di.udi_subclass,
    255      1.32   mlelstv 			di.udi_protocol);
    256      1.32   mlelstv 	}
    257      1.15  augustss 	if (showdevs) {
    258      1.15  augustss 		for (i = 0; i < USB_MAX_DEVNAMES; i++)
    259      1.19  christos 			if (di.udi_devnames[i][0])
    260      1.15  augustss 				printf("%*s  %s\n", indent, "",
    261      1.19  christos 				       di.udi_devnames[i]);
    262      1.15  augustss 	}
    263       1.1  augustss 	if (!rec)
    264       1.1  augustss 		return;
    265      1.35     skrll 
    266      1.35     skrll 	unsigned int nports = di.udi_nports;
    267      1.35     skrll 
    268      1.35     skrll 	for (unsigned int p = 0; p < nports && p < __arraycount(di.udi_ports); p++) {
    269      1.19  christos 		int s = di.udi_ports[p];
    270       1.1  augustss 		if (s >= USB_MAX_DEVICES) {
    271       1.1  augustss 			if (verbose) {
    272       1.1  augustss 				printf("%*sport %d %s\n", indent+1, "", p+1,
    273       1.1  augustss 				       s == USB_PORT_ENABLED ? "enabled" :
    274       1.1  augustss 				       s == USB_PORT_SUSPENDED ? "suspended" :
    275       1.1  augustss 				       s == USB_PORT_POWERED ? "powered" :
    276       1.1  augustss 				       s == USB_PORT_DISABLED ? "disabled" :
    277       1.1  augustss 				       "???");
    278       1.1  augustss 
    279       1.1  augustss 			}
    280       1.1  augustss 			continue;
    281       1.1  augustss 		}
    282       1.1  augustss 		indent++;
    283       1.1  augustss 		printf("%*s", indent, "");
    284       1.1  augustss 		if (verbose)
    285       1.1  augustss 			printf("port %d ", p+1);
    286      1.10  augustss 		if (s == 0)
    287      1.10  augustss 			printf("addr 0 should never happen!\n");
    288      1.10  augustss 		else
    289      1.10  augustss 			usbdev(f, s, 1);
    290       1.1  augustss 		indent--;
    291       1.1  augustss 	}
    292       1.1  augustss }
    293       1.1  augustss 
    294      1.28     joerg static void
    295      1.14  augustss usbdump(int f)
    296       1.1  augustss {
    297       1.1  augustss 	int a;
    298       1.1  augustss 
    299      1.30  jakllsch 	for (a = 0; a < USB_MAX_DEVICES; a++) {
    300       1.1  augustss 		if (!done[a])
    301       1.1  augustss 			usbdev(f, a, 1);
    302       1.1  augustss 	}
    303       1.1  augustss }
    304       1.1  augustss 
    305      1.28     joerg static void
    306      1.14  augustss dumpone(char *name, int f, int addr)
    307       1.1  augustss {
    308       1.1  augustss 	if (verbose)
    309       1.1  augustss 		printf("Controller %s:\n", name);
    310       1.1  augustss 	indent = 0;
    311       1.1  augustss 	memset(done, 0, sizeof done);
    312      1.30  jakllsch 	if (addr >= 0)
    313       1.1  augustss 		usbdev(f, addr, 0);
    314       1.1  augustss 	else
    315       1.1  augustss 		usbdump(f);
    316       1.1  augustss }
    317       1.1  augustss 
    318       1.1  augustss int
    319      1.14  augustss main(int argc, char **argv)
    320       1.1  augustss {
    321       1.1  augustss 	int ch, i, f;
    322       1.1  augustss 	char buf[50];
    323      1.29  jakllsch 	char *dev = NULL;
    324      1.30  jakllsch 	int addr = -1;
    325       1.4  augustss 	int ncont;
    326       1.1  augustss 
    327      1.16  augustss 	while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
    328       1.1  augustss 		switch(ch) {
    329       1.1  augustss 		case 'a':
    330       1.1  augustss 			addr = atoi(optarg);
    331      1.15  augustss 			break;
    332      1.15  augustss 		case 'd':
    333      1.15  augustss 			showdevs++;
    334       1.1  augustss 			break;
    335       1.1  augustss 		case 'f':
    336       1.1  augustss 			dev = optarg;
    337       1.1  augustss 			break;
    338       1.1  augustss 		case 'v':
    339      1.32   mlelstv 			verbose++;
    340       1.1  augustss 			break;
    341       1.1  augustss 		case '?':
    342       1.1  augustss 		default:
    343       1.1  augustss 			usage();
    344       1.1  augustss 		}
    345       1.1  augustss 	}
    346       1.1  augustss 	argc -= optind;
    347       1.1  augustss 	argv += optind;
    348       1.1  augustss 
    349      1.29  jakllsch 	if (dev == NULL) {
    350       1.4  augustss 		for (ncont = 0, i = 0; i < 10; i++) {
    351      1.21    itojun 			snprintf(buf, sizeof(buf), "%s%d", USBDEV, i);
    352       1.1  augustss 			f = open(buf, O_RDONLY);
    353       1.1  augustss 			if (f >= 0) {
    354       1.1  augustss 				dumpone(buf, f, addr);
    355       1.1  augustss 				close(f);
    356       1.3  augustss 			} else {
    357      1.11  augustss 				if (errno == ENOENT || errno == ENXIO)
    358      1.11  augustss 					continue;
    359      1.11  augustss 				warn("%s", buf);
    360       1.1  augustss 			}
    361      1.11  augustss 			ncont++;
    362       1.1  augustss 		}
    363       1.4  augustss 		if (verbose && ncont == 0)
    364      1.17       cgd 			printf("%s: no USB controllers found\n",
    365      1.17       cgd 			    getprogname());
    366       1.1  augustss 	} else {
    367       1.1  augustss 		f = open(dev, O_RDONLY);
    368       1.1  augustss 		if (f >= 0)
    369       1.1  augustss 			dumpone(dev, f, addr);
    370       1.1  augustss 		else
    371       1.1  augustss 			err(1, "%s", dev);
    372       1.1  augustss 	}
    373      1.29  jakllsch 	exit(EXIT_SUCCESS);
    374       1.1  augustss }
    375