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