Home | History | Annotate | Line # | Download | only in usbdevs
usbdevs.c revision 1.29
      1 /*	$NetBSD: usbdevs.c,v 1.29 2013/07/08 14:47:18 jakllsch 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 		default: break;
    116 		}
    117 		if (di.udi_power)
    118 			printf("power %d mA, ", di.udi_power);
    119 		else
    120 			printf("self powered, ");
    121 		if (di.udi_config)
    122 			printf("config %d, ", di.udi_config);
    123 		else
    124 			printf("unconfigured, ");
    125 	}
    126 	u2t(di.udi_product, product);
    127 	u2t(di.udi_vendor, vendor);
    128 	u2t(di.udi_serial, serial);
    129 	if (verbose) {
    130 		printf("%s(0x%04x), %s(0x%04x), rev %s",
    131 		       product, di.udi_productNo,
    132 		       vendor, di.udi_vendorNo, di.udi_release);
    133 		if (di.udi_serial[0])
    134 			printf(", serial %s", serial);
    135 	} else
    136 		printf("%s, %s", product, vendor);
    137 	printf("\n");
    138 	if (showdevs) {
    139 		for (i = 0; i < USB_MAX_DEVNAMES; i++)
    140 			if (di.udi_devnames[i][0])
    141 				printf("%*s  %s\n", indent, "",
    142 				       di.udi_devnames[i]);
    143 	}
    144 	if (!rec)
    145 		return;
    146 	for (p = 0; p < di.udi_nports; p++) {
    147 		int s = di.udi_ports[p];
    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 			}
    158 			continue;
    159 		}
    160 		indent++;
    161 		printf("%*s", indent, "");
    162 		if (verbose)
    163 			printf("port %d ", p+1);
    164 		if (s == 0)
    165 			printf("addr 0 should never happen!\n");
    166 		else
    167 			usbdev(f, s, 1);
    168 		indent--;
    169 	}
    170 }
    171 
    172 static void
    173 usbdump(int f)
    174 {
    175 	int a;
    176 
    177 	for (a = 1; a < USB_MAX_DEVICES; a++) {
    178 		if (!done[a])
    179 			usbdev(f, a, 1);
    180 	}
    181 }
    182 
    183 static void
    184 dumpone(char *name, int f, int addr)
    185 {
    186 	if (verbose)
    187 		printf("Controller %s:\n", name);
    188 	indent = 0;
    189 	memset(done, 0, sizeof done);
    190 	if (addr)
    191 		usbdev(f, addr, 0);
    192 	else
    193 		usbdump(f);
    194 }
    195 
    196 int
    197 main(int argc, char **argv)
    198 {
    199 	int ch, i, f;
    200 	char buf[50];
    201 	char *dev = NULL;
    202 	int addr = 0;
    203 	int ncont;
    204 
    205 	while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
    206 		switch(ch) {
    207 		case 'a':
    208 			addr = atoi(optarg);
    209 			break;
    210 		case 'd':
    211 			showdevs++;
    212 			break;
    213 		case 'f':
    214 			dev = optarg;
    215 			break;
    216 		case 'v':
    217 			verbose = 1;
    218 			break;
    219 		case '?':
    220 		default:
    221 			usage();
    222 		}
    223 	}
    224 	argc -= optind;
    225 	argv += optind;
    226 
    227 	if (dev == NULL) {
    228 		for (ncont = 0, i = 0; i < 10; i++) {
    229 			snprintf(buf, sizeof(buf), "%s%d", USBDEV, i);
    230 			f = open(buf, O_RDONLY);
    231 			if (f >= 0) {
    232 				dumpone(buf, f, addr);
    233 				close(f);
    234 			} else {
    235 				if (errno == ENOENT || errno == ENXIO)
    236 					continue;
    237 				warn("%s", buf);
    238 			}
    239 			ncont++;
    240 		}
    241 		if (verbose && ncont == 0)
    242 			printf("%s: no USB controllers found\n",
    243 			    getprogname());
    244 	} else {
    245 		f = open(dev, O_RDONLY);
    246 		if (f >= 0)
    247 			dumpone(dev, f, addr);
    248 		else
    249 			err(1, "%s", dev);
    250 	}
    251 	exit(EXIT_SUCCESS);
    252 }
    253