Home | History | Annotate | Line # | Download | only in usbdevs
usbdevs.c revision 1.31
      1 /*	$NetBSD: usbdevs.c,v 1.31 2014/08/12 13:40:07 skrll 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 (at) 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 <stdio.h>
     33 #include <stdlib.h>
     34 #include <string.h>
     35 #include <sys/types.h>
     36 #include <fcntl.h>
     37 #include <unistd.h>
     38 #include <err.h>
     39 #include <errno.h>
     40 #include <locale.h>
     41 #include <langinfo.h>
     42 #include <iconv.h>
     43 #include <dev/usb/usb.h>
     44 
     45 #define USBDEV "/dev/usb"
     46 
     47 static int verbose = 0;
     48 static int showdevs = 0;
     49 
     50 __dead static void usage(void);
     51 static void usbdev(int f, int a, int rec);
     52 static void usbdump(int f);
     53 static void dumpone(char *name, int f, int addr);
     54 
     55 static void
     56 usage(void)
     57 {
     58 
     59 	fprintf(stderr, "usage: %s [-dv] [-a addr] [-f dev]\n",
     60 	    getprogname());
     61 	exit(EXIT_FAILURE);
     62 }
     63 
     64 static char done[USB_MAX_DEVICES];
     65 static int indent;
     66 #define MAXLEN USB_MAX_ENCODED_STRING_LEN /* assume can't grow over UTF-8 */
     67 static char vendor[MAXLEN], product[MAXLEN], serial[MAXLEN];
     68 
     69 static void
     70 u2t(const char *utf8str, char *termstr)
     71 {
     72 	static iconv_t ic;
     73 	static int iconv_inited = 0;
     74 	size_t insz, outsz, icres;
     75 
     76 	if (!iconv_inited) {
     77 		setlocale(LC_ALL, "");
     78 		ic = iconv_open(nl_langinfo(CODESET), "UTF-8");
     79 		if (ic == (iconv_t)-1)
     80 			ic = iconv_open("ASCII", "UTF-8"); /* g.c.d. */
     81 		iconv_inited = 1;
     82 	}
     83 	if (ic != (iconv_t)-1) {
     84 		insz = strlen(utf8str);
     85 		outsz = MAXLEN - 1;
     86 		icres = iconv(ic, &utf8str, &insz, &termstr, &outsz);
     87 		if (icres != (size_t)-1) {
     88 			*termstr = '\0';
     89 			return;
     90 		}
     91 	}
     92 	strcpy(termstr, "(invalid)");
     93 }
     94 
     95 static void
     96 usbdev(int f, int a, int rec)
     97 {
     98 	struct usb_device_info di;
     99 	int e, p, i;
    100 
    101 	di.udi_addr = a;
    102 	e = ioctl(f, USB_DEVICEINFO, &di);
    103 	if (e) {
    104 		if (errno != ENXIO)
    105 			printf("addr %d: I/O error\n", a);
    106 		return;
    107 	}
    108 	printf("addr %d: ", a);
    109 	done[a] = 1;
    110 	if (verbose) {
    111 		switch (di.udi_speed) {
    112 		case USB_SPEED_LOW:  printf("low speed, "); break;
    113 		case USB_SPEED_FULL: printf("full speed, "); break;
    114 		case USB_SPEED_HIGH: printf("high speed, "); break;
    115 		case USB_SPEED_SUPER: printf("super speed, "); break;
    116 		default: break;
    117 		}
    118 		if (di.udi_power)
    119 			printf("power %d mA, ", di.udi_power);
    120 		else
    121 			printf("self powered, ");
    122 		if (di.udi_config)
    123 			printf("config %d, ", di.udi_config);
    124 		else
    125 			printf("unconfigured, ");
    126 	}
    127 	u2t(di.udi_product, product);
    128 	u2t(di.udi_vendor, vendor);
    129 	u2t(di.udi_serial, serial);
    130 	if (verbose) {
    131 		printf("%s(0x%04x), %s(0x%04x), rev %s",
    132 		       product, di.udi_productNo,
    133 		       vendor, di.udi_vendorNo, di.udi_release);
    134 		if (di.udi_serial[0])
    135 			printf(", serial %s", serial);
    136 	} else
    137 		printf("%s, %s", product, vendor);
    138 	printf("\n");
    139 	if (showdevs) {
    140 		for (i = 0; i < USB_MAX_DEVNAMES; i++)
    141 			if (di.udi_devnames[i][0])
    142 				printf("%*s  %s\n", indent, "",
    143 				       di.udi_devnames[i]);
    144 	}
    145 	if (!rec)
    146 		return;
    147 	for (p = 0; p < di.udi_nports; p++) {
    148 		int s = di.udi_ports[p];
    149 		if (s >= USB_MAX_DEVICES) {
    150 			if (verbose) {
    151 				printf("%*sport %d %s\n", indent+1, "", p+1,
    152 				       s == USB_PORT_ENABLED ? "enabled" :
    153 				       s == USB_PORT_SUSPENDED ? "suspended" :
    154 				       s == USB_PORT_POWERED ? "powered" :
    155 				       s == USB_PORT_DISABLED ? "disabled" :
    156 				       "???");
    157 
    158 			}
    159 			continue;
    160 		}
    161 		indent++;
    162 		printf("%*s", indent, "");
    163 		if (verbose)
    164 			printf("port %d ", p+1);
    165 		if (s == 0)
    166 			printf("addr 0 should never happen!\n");
    167 		else
    168 			usbdev(f, s, 1);
    169 		indent--;
    170 	}
    171 }
    172 
    173 static void
    174 usbdump(int f)
    175 {
    176 	int a;
    177 
    178 	for (a = 0; a < USB_MAX_DEVICES; a++) {
    179 		if (!done[a])
    180 			usbdev(f, a, 1);
    181 	}
    182 }
    183 
    184 static void
    185 dumpone(char *name, int f, int addr)
    186 {
    187 	if (verbose)
    188 		printf("Controller %s:\n", name);
    189 	indent = 0;
    190 	memset(done, 0, sizeof done);
    191 	if (addr >= 0)
    192 		usbdev(f, addr, 0);
    193 	else
    194 		usbdump(f);
    195 }
    196 
    197 int
    198 main(int argc, char **argv)
    199 {
    200 	int ch, i, f;
    201 	char buf[50];
    202 	char *dev = NULL;
    203 	int addr = -1;
    204 	int ncont;
    205 
    206 	while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
    207 		switch(ch) {
    208 		case 'a':
    209 			addr = atoi(optarg);
    210 			break;
    211 		case 'd':
    212 			showdevs++;
    213 			break;
    214 		case 'f':
    215 			dev = optarg;
    216 			break;
    217 		case 'v':
    218 			verbose = 1;
    219 			break;
    220 		case '?':
    221 		default:
    222 			usage();
    223 		}
    224 	}
    225 	argc -= optind;
    226 	argv += optind;
    227 
    228 	if (dev == NULL) {
    229 		for (ncont = 0, i = 0; i < 10; i++) {
    230 			snprintf(buf, sizeof(buf), "%s%d", USBDEV, i);
    231 			f = open(buf, O_RDONLY);
    232 			if (f >= 0) {
    233 				dumpone(buf, f, addr);
    234 				close(f);
    235 			} else {
    236 				if (errno == ENOENT || errno == ENXIO)
    237 					continue;
    238 				warn("%s", buf);
    239 			}
    240 			ncont++;
    241 		}
    242 		if (verbose && ncont == 0)
    243 			printf("%s: no USB controllers found\n",
    244 			    getprogname());
    245 	} else {
    246 		f = open(dev, O_RDONLY);
    247 		if (f >= 0)
    248 			dumpone(dev, f, addr);
    249 		else
    250 			err(1, "%s", dev);
    251 	}
    252 	exit(EXIT_SUCCESS);
    253 }
    254