usbdevs.c revision 1.40 1 /* $NetBSD: usbdevs.c,v 1.40 2019/11/27 17:56:08 christos 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.40 2019/11/27 17:56:08 christos Exp $");
35 #endif
36
37 #include <sys/types.h>
38 #include <sys/drvctlio.h>
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <locale.h>
48 #include <langinfo.h>
49 #include <iconv.h>
50 #include <ctype.h>
51 #include <inttypes.h>
52
53 #include <dev/usb/usb.h>
54
55 #define USBDEV "/dev/usb"
56
57 static int verbose = 0;
58 static int showdevs = 0;
59
60 struct stringtable {
61 int row, col;
62 const char *string;
63 };
64
65 __dead static void usage(void);
66 static void getstrings(const struct stringtable *, int, int, const char **, const char **);
67 static void usbdev(int f, int a, int rec);
68 static void usbdump(int f);
69 static void dumpone(char *name, int f, int addr);
70
71 static void
72 usage(void)
73 {
74
75 fprintf(stderr, "usage: %s [-dv] [-a addr] [-f dev]\n",
76 getprogname());
77 exit(EXIT_FAILURE);
78 }
79
80 static char done[USB_MAX_DEVICES];
81 static int indent;
82 #define MAXLEN USB_MAX_ENCODED_STRING_LEN /* assume can't grow over UTF-8 */
83 static char vendor[MAXLEN], product[MAXLEN], serial[MAXLEN];
84
85 static void
86 u2t(const char *utf8str, char *termstr)
87 {
88 static iconv_t ic;
89 static int iconv_inited = 0;
90 size_t insz, outsz, icres;
91
92 if (!iconv_inited) {
93 setlocale(LC_ALL, "");
94 ic = iconv_open(nl_langinfo(CODESET), "UTF-8");
95 if (ic == (iconv_t)-1)
96 ic = iconv_open("ASCII", "UTF-8"); /* g.c.d. */
97 iconv_inited = 1;
98 }
99 if (ic != (iconv_t)-1) {
100 insz = strlen(utf8str);
101 outsz = MAXLEN - 1;
102 icres = iconv(ic, __UNCONST(&utf8str), &insz, &termstr,
103 &outsz);
104 if (icres != (size_t)-1) {
105 *termstr = '\0';
106 return;
107 }
108 }
109 strcpy(termstr, "(invalid)");
110 }
111
112 struct stringtable class_strings[] = {
113 { UICLASS_UNSPEC, -1, "Unspecified" },
114
115 { UICLASS_AUDIO, -1, "Audio" },
116 { UICLASS_AUDIO, UISUBCLASS_AUDIOCONTROL, "Audio Control" },
117 { UICLASS_AUDIO, UISUBCLASS_AUDIOSTREAM, "Audio Streaming" },
118 { UICLASS_AUDIO, UISUBCLASS_MIDISTREAM, "MIDI Streaming" },
119
120 { UICLASS_CDC, -1, "Communications and CDC Control" },
121 { UICLASS_CDC, UISUBCLASS_DIRECT_LINE_CONTROL_MODEL, "Direct Line" },
122 { UICLASS_CDC, UISUBCLASS_ABSTRACT_CONTROL_MODEL, "Abstract" },
123 { UICLASS_CDC, UISUBCLASS_TELEPHONE_CONTROL_MODEL, "Telephone" },
124 { UICLASS_CDC, UISUBCLASS_MULTICHANNEL_CONTROL_MODEL, "Multichannel" },
125 { UICLASS_CDC, UISUBCLASS_CAPI_CONTROLMODEL, "CAPI" },
126 { UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, "Ethernet Networking" },
127 { UICLASS_CDC, UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL, "ATM Networking" },
128
129 { UICLASS_HID, -1, "Human Interface Device" },
130 { UICLASS_HID, UISUBCLASS_BOOT, "Boot" },
131
132 { UICLASS_PHYSICAL, -1, "Physical" },
133
134 { UICLASS_IMAGE, -1, "Image" },
135
136 { UICLASS_PRINTER, -1, "Printer" },
137 { UICLASS_PRINTER, UISUBCLASS_PRINTER, "Printer" },
138
139 { UICLASS_MASS, -1, "Mass Storage" },
140 { UICLASS_MASS, UISUBCLASS_RBC, "RBC" },
141 { UICLASS_MASS, UISUBCLASS_SFF8020I, "SFF8020I" },
142 { UICLASS_MASS, UISUBCLASS_QIC157, "QIC157" },
143 { UICLASS_MASS, UISUBCLASS_UFI, "UFI" },
144 { UICLASS_MASS, UISUBCLASS_SFF8070I, "SFF8070I" },
145 { UICLASS_MASS, UISUBCLASS_SCSI, "SCSI" },
146 { UICLASS_MASS, UISUBCLASS_SCSI, "SCSI" },
147
148 { UICLASS_HUB, -1, "Hub" },
149 { UICLASS_HUB, UISUBCLASS_HUB, "Hub" },
150
151 { UICLASS_CDC_DATA, -1, "CDC-Data" },
152 { UICLASS_CDC_DATA, UISUBCLASS_DATA, "Data" },
153
154 { UICLASS_SMARTCARD, -1, "Smart Card" },
155
156 { UICLASS_SECURITY, -1, "Content Security" },
157
158 { UICLASS_VIDEO, -1, "Video" },
159 { UICLASS_VIDEO, UISUBCLASS_VIDEOCONTROL, "Video Control" },
160 { UICLASS_VIDEO, UISUBCLASS_VIDEOSTREAMING, "Video Streaming" },
161 { UICLASS_VIDEO, UISUBCLASS_VIDEOCOLLECTION, "Video Collection" },
162
163 #ifdef notyet
164 { UICLASS_HEALTHCARE, -1, "Personal Healthcare" },
165 { UICLASS_AVDEVICE, -1, "Audio/Video Device" },
166 { UICLASS_BILLBOARD, -1, "Billboard" },
167 #endif
168
169 { UICLASS_DIAGNOSTIC, -1, "Diagnostic" },
170 { UICLASS_WIRELESS, -1, "Wireless" },
171 { UICLASS_WIRELESS, UISUBCLASS_RF, "Radio Frequency" },
172
173 #ifdef notyet
174 { UICLASS_MISC, -1, "Miscellaneous" },
175 #endif
176
177 { UICLASS_APPL_SPEC, -1, "Application Specific" },
178 { UICLASS_APPL_SPEC, UISUBCLASS_FIRMWARE_DOWNLOAD, "Firmware Download" },
179 { UICLASS_APPL_SPEC, UISUBCLASS_IRDA, "Irda" },
180
181 { UICLASS_VENDOR, -1, "Vendor Specific" },
182
183 { -1, -1, NULL }
184 };
185
186 static void
187 getstrings(const struct stringtable *table,
188 int row, int col, const char **rp, const char **cp) {
189 static char rbuf[5], cbuf[5];
190
191 snprintf(rbuf, sizeof(rbuf), "0x%02x", row);
192 snprintf(cbuf, sizeof(cbuf), "0x%02x", col);
193
194 *rp = rbuf;
195 *cp = cbuf;
196
197 while (table->string != NULL) {
198 if (table->row == row) {
199 if (table->col == -1)
200 *rp = table->string;
201 else if (table->col == col)
202 *cp = table->string;
203 } else if (table->row > row)
204 break;
205
206 ++table;
207 }
208 }
209
210 static void
211 usbdev(int f, int a, int rec)
212 {
213 struct usb_device_info di;
214 int e, i;
215
216 di.udi_addr = a;
217 e = ioctl(f, USB_DEVICEINFO, &di);
218 if (e) {
219 if (errno != ENXIO)
220 printf("addr %d: I/O error\n", a);
221 return;
222 }
223 printf("addr %d: ", a);
224 done[a] = 1;
225 if (verbose) {
226 switch (di.udi_speed) {
227 case USB_SPEED_LOW: printf("low speed, "); break;
228 case USB_SPEED_FULL: printf("full speed, "); break;
229 case USB_SPEED_HIGH: printf("high speed, "); break;
230 case USB_SPEED_SUPER: printf("super speed, "); break;
231 case USB_SPEED_SUPER_PLUS: printf("super speed+, "); break;
232 default: break;
233 }
234 if (di.udi_power)
235 printf("power %d mA, ", di.udi_power);
236 else
237 printf("self powered, ");
238 if (di.udi_config)
239 printf("config %d, ", di.udi_config);
240 else
241 printf("unconfigured, ");
242 }
243 u2t(di.udi_product, product);
244 u2t(di.udi_vendor, vendor);
245 u2t(di.udi_serial, serial);
246 if (verbose) {
247 printf("%s(0x%04x), %s(0x%04x), rev %s(0x%04x)",
248 product, di.udi_productNo,
249 vendor, di.udi_vendorNo,
250 di.udi_release, di.udi_releaseNo);
251 if (di.udi_serial[0])
252 printf(", serial %s", serial);
253 } else
254 printf("%s, %s", product, vendor);
255 printf("\n");
256 if (verbose > 1 && di.udi_class != UICLASS_UNSPEC) {
257 const char *cstr, *sstr;
258 getstrings(class_strings, di.udi_class, di.udi_subclass, &cstr, &sstr);
259 printf("%*s %s(0x%02x), %s(0x%02x), proto %u\n", indent, "",
260 cstr, di.udi_class, sstr, di.udi_subclass,
261 di.udi_protocol);
262 }
263 if (showdevs) {
264 for (i = 0; i < USB_MAX_DEVNAMES; i++)
265 if (di.udi_devnames[i][0])
266 printf("%*s %s\n", indent, "",
267 di.udi_devnames[i]);
268 }
269 if (!rec)
270 return;
271
272 unsigned int nports = di.udi_nports;
273
274 for (unsigned int p = 0; p < nports && p < __arraycount(di.udi_ports); p++) {
275 int s = di.udi_ports[p];
276 if (s >= USB_MAX_DEVICES) {
277 if (verbose) {
278 printf("%*sport %d %s\n", indent+1, "", p+1,
279 s == USB_PORT_ENABLED ? "enabled" :
280 s == USB_PORT_SUSPENDED ? "suspended" :
281 s == USB_PORT_POWERED ? "powered" :
282 s == USB_PORT_DISABLED ? "disabled" :
283 "???");
284
285 }
286 continue;
287 }
288 indent++;
289 printf("%*s", indent, "");
290 if (verbose)
291 printf("port %d ", p+1);
292 if (s == 0)
293 printf("addr 0 should never happen!\n");
294 else
295 usbdev(f, s, 1);
296 indent--;
297 }
298 }
299
300 static void
301 usbdump(int f)
302 {
303 int a;
304
305 for (a = 0; a < USB_MAX_DEVICES; a++) {
306 if (!done[a])
307 usbdev(f, a, 1);
308 }
309 }
310
311 static void
312 dumpone(char *name, int f, int addr)
313 {
314 if (verbose)
315 printf("Controller %s:\n", name);
316 indent = 0;
317 memset(done, 0, sizeof done);
318 if (addr >= 0)
319 usbdev(f, addr, 0);
320 else
321 usbdump(f);
322 }
323
324 static int
325 getusbcount_device(int fd, const char *dev, int depth)
326 {
327 struct devlistargs laa = {
328 .l_childname = NULL,
329 .l_children = 0,
330 };
331 size_t i;
332 size_t children;
333 int nbusses = 0;
334
335 if (depth && (dev == NULL || *dev == '\0'))
336 return 0;
337
338 /*
339 * Look for children that match "usb[0-9]*". Could maybe
340 * simply return 1 here, but there's always a chance that
341 * someone has eg, a USB to PCI bridge, with a USB
342 * controller behind PCI.
343 */
344 if (strncmp(dev, "usb", 3) == 0 && isdigit((int)dev[3]))
345 nbusses++;
346
347 strlcpy(laa.l_devname, dev, sizeof(laa.l_devname));
348
349 if (ioctl(fd, DRVLISTDEV, &laa) == -1)
350 err(EXIT_FAILURE, "DRVLISTDEV");
351 children = laa.l_children;
352
353 laa.l_childname = malloc(children * sizeof(laa.l_childname[0]));
354 if (laa.l_childname == NULL)
355 err(EXIT_FAILURE, "out of memory");
356 if (ioctl(fd, DRVLISTDEV, &laa) == -1)
357 err(EXIT_FAILURE, "DRVLISTDEV");
358 if (laa.l_children > children)
359 err(EXIT_FAILURE, "DRVLISTDEV: number of children grew");
360
361 for (i = 0; i < laa.l_children; i++) {
362 nbusses += getusbcount_device(fd, laa.l_childname[i], depth+1);
363 }
364
365 return nbusses;
366 }
367
368 int
369 main(int argc, char **argv)
370 {
371 int ch, i, f, error;
372 char buf[50];
373 char *dev = NULL;
374 int addr = -1;
375 int ncont;
376
377 while ((ch = getopt(argc, argv, "a:df:v?")) != -1) {
378 switch(ch) {
379 case 'a':
380 addr = strtoi(optarg, NULL, 10, 0, USB_MAX_DEVICES - 1,
381 &error);
382 if (error) {
383 errc(EXIT_FAILURE, error,
384 "Bad value for device address: `%s'",
385 optarg);
386 }
387 break;
388 case 'd':
389 showdevs++;
390 break;
391 case 'f':
392 dev = optarg;
393 break;
394 case 'v':
395 verbose++;
396 break;
397 case '?':
398 default:
399 usage();
400 }
401 }
402 argc -= optind;
403 argv += optind;
404
405 if (dev == NULL) {
406 int nbusses;
407 int fd = open(DRVCTLDEV, O_RDONLY, 0);
408
409 /* If no drvctl configured, default to 16. */
410 if (fd != -1)
411 nbusses = getusbcount_device(fd, "", 0);
412 else
413 nbusses = 16;
414 close(fd);
415
416 for (ncont = 0, i = 0; i < nbusses; i++) {
417 snprintf(buf, sizeof(buf), "%s%d", USBDEV, i);
418 f = open(buf, O_RDONLY);
419 if (f >= 0) {
420 dumpone(buf, f, addr);
421 close(f);
422 } else {
423 if (errno == ENOENT || errno == ENXIO)
424 continue;
425 warn("%s", buf);
426 }
427 ncont++;
428 }
429 if (verbose && ncont == 0)
430 printf("%s: no USB controllers found\n",
431 getprogname());
432 } else {
433 f = open(dev, O_RDONLY);
434 if (f >= 0)
435 dumpone(dev, f, addr);
436 else
437 err(1, "%s", dev);
438 }
439 return EXIT_SUCCESS;
440 }
441