usbdevs.c revision 1.35.12.1 1 /* $NetBSD: usbdevs.c,v 1.35.12.1 2018/07/28 04:38:15 pgoyette 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.35.12.1 2018/07/28 04:38:15 pgoyette 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 case USB_SPEED_SUPER_PLUS: printf("super speed+, "); break;
226 default: break;
227 }
228 if (di.udi_power)
229 printf("power %d mA, ", di.udi_power);
230 else
231 printf("self powered, ");
232 if (di.udi_config)
233 printf("config %d, ", di.udi_config);
234 else
235 printf("unconfigured, ");
236 }
237 u2t(di.udi_product, product);
238 u2t(di.udi_vendor, vendor);
239 u2t(di.udi_serial, serial);
240 if (verbose) {
241 printf("%s(0x%04x), %s(0x%04x), rev %s(0x%04x)",
242 product, di.udi_productNo,
243 vendor, di.udi_vendorNo,
244 di.udi_release, di.udi_releaseNo);
245 if (di.udi_serial[0])
246 printf(", serial %s", serial);
247 } else
248 printf("%s, %s", product, vendor);
249 printf("\n");
250 if (verbose > 1 && di.udi_class != UICLASS_UNSPEC) {
251 const char *cstr, *sstr;
252 getstrings(class_strings, di.udi_class, di.udi_subclass, &cstr, &sstr);
253 printf("%*s %s(0x%02x), %s(0x%02x), proto %u\n", indent, "",
254 cstr, di.udi_class, sstr, di.udi_subclass,
255 di.udi_protocol);
256 }
257 if (showdevs) {
258 for (i = 0; i < USB_MAX_DEVNAMES; i++)
259 if (di.udi_devnames[i][0])
260 printf("%*s %s\n", indent, "",
261 di.udi_devnames[i]);
262 }
263 if (!rec)
264 return;
265
266 unsigned int nports = di.udi_nports;
267
268 for (unsigned int p = 0; p < nports && p < __arraycount(di.udi_ports); p++) {
269 int s = di.udi_ports[p];
270 if (s >= USB_MAX_DEVICES) {
271 if (verbose) {
272 printf("%*sport %d %s\n", indent+1, "", p+1,
273 s == USB_PORT_ENABLED ? "enabled" :
274 s == USB_PORT_SUSPENDED ? "suspended" :
275 s == USB_PORT_POWERED ? "powered" :
276 s == USB_PORT_DISABLED ? "disabled" :
277 "???");
278
279 }
280 continue;
281 }
282 indent++;
283 printf("%*s", indent, "");
284 if (verbose)
285 printf("port %d ", p+1);
286 if (s == 0)
287 printf("addr 0 should never happen!\n");
288 else
289 usbdev(f, s, 1);
290 indent--;
291 }
292 }
293
294 static void
295 usbdump(int f)
296 {
297 int a;
298
299 for (a = 0; a < USB_MAX_DEVICES; a++) {
300 if (!done[a])
301 usbdev(f, a, 1);
302 }
303 }
304
305 static void
306 dumpone(char *name, int f, int addr)
307 {
308 if (verbose)
309 printf("Controller %s:\n", name);
310 indent = 0;
311 memset(done, 0, sizeof done);
312 if (addr >= 0)
313 usbdev(f, addr, 0);
314 else
315 usbdump(f);
316 }
317
318 int
319 main(int argc, char **argv)
320 {
321 int ch, i, f;
322 char buf[50];
323 char *dev = NULL;
324 int addr = -1;
325 int ncont;
326
327 while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
328 switch(ch) {
329 case 'a':
330 addr = atoi(optarg);
331 break;
332 case 'd':
333 showdevs++;
334 break;
335 case 'f':
336 dev = optarg;
337 break;
338 case 'v':
339 verbose++;
340 break;
341 case '?':
342 default:
343 usage();
344 }
345 }
346 argc -= optind;
347 argv += optind;
348
349 if (dev == NULL) {
350 for (ncont = 0, i = 0; i < 10; i++) {
351 snprintf(buf, sizeof(buf), "%s%d", USBDEV, i);
352 f = open(buf, O_RDONLY);
353 if (f >= 0) {
354 dumpone(buf, f, addr);
355 close(f);
356 } else {
357 if (errno == ENOENT || errno == ENXIO)
358 continue;
359 warn("%s", buf);
360 }
361 ncont++;
362 }
363 if (verbose && ncont == 0)
364 printf("%s: no USB controllers found\n",
365 getprogname());
366 } else {
367 f = open(dev, O_RDONLY);
368 if (f >= 0)
369 dumpone(dev, f, addr);
370 else
371 err(1, "%s", dev);
372 }
373 exit(EXIT_SUCCESS);
374 }
375