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