xref: /netbsd-src/usr.sbin/usbdevs/usbdevs.c (revision 6a9b3088d8d2341ca1454531d365c15fe9c1c589)
1 /*	$NetBSD: usbdevs.c,v 1.26 2010/02/02 16:25:30 drochner 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 <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 int verbose = 0;
48 int showdevs = 0;
49 
50 void usage(void);
51 void usbdev(int f, int a, int rec);
52 void usbdump(int f);
53 void dumpone(char *name, int f, int addr);
54 int main(int, char **);
55 
56 void
57 usage()
58 {
59 
60 	fprintf(stderr, "usage: %s [-a addr] [-d] [-f dev] [-v]\n",
61 	    getprogname());
62 	exit(1);
63 }
64 
65 char done[USB_MAX_DEVICES];
66 int indent;
67 #define MAXLEN USB_MAX_ENCODED_STRING_LEN /* assume can't grow over UTF-8 */
68 char vendor[MAXLEN], product[MAXLEN], serial[MAXLEN];
69 
70 static void
71 u2t(const char *utf8str, char *termstr)
72 {
73 	static iconv_t ic;
74 	static int iconv_inited = 0;
75 	size_t insz, outsz, icres;
76 
77 	if (!iconv_inited) {
78 		setlocale(LC_ALL, "");
79 		ic = iconv_open(nl_langinfo(CODESET), "UTF-8");
80 		if (ic == (iconv_t)-1)
81 			ic = iconv_open("ASCII", "UTF-8"); /* g.c.d. */
82 		iconv_inited = 1;
83 	}
84 	if (ic != (iconv_t)-1) {
85 		insz = strlen(utf8str);
86 		outsz = MAXLEN - 1;
87 		icres = iconv(ic, &utf8str, &insz, &termstr, &outsz);
88 		if (icres != (size_t)-1) {
89 			*termstr = '\0';
90 			return;
91 		}
92 	}
93 	strcpy(termstr, "(invalid)");
94 }
95 
96 void
97 usbdev(int f, int a, int rec)
98 {
99 	struct usb_device_info di;
100 	int e, p, i;
101 
102 	di.udi_addr = a;
103 	e = ioctl(f, USB_DEVICEINFO, &di);
104 	if (e) {
105 		if (errno != ENXIO)
106 			printf("addr %d: I/O error\n", a);
107 		return;
108 	}
109 	printf("addr %d: ", a);
110 	done[a] = 1;
111 	if (verbose) {
112 		switch (di.udi_speed) {
113 		case USB_SPEED_LOW:  printf("low speed, "); break;
114 		case USB_SPEED_FULL: printf("full speed, "); break;
115 		case USB_SPEED_HIGH: printf("high 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 void
174 usbdump(int f)
175 {
176 	int a;
177 
178 	for (a = 1; a < USB_MAX_DEVICES; a++) {
179 		if (!done[a])
180 			usbdev(f, a, 1);
181 	}
182 }
183 
184 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)
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 = 0;
203 	int addr = 0;
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 == 0) {
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(0);
253 }
254