1 /* lsusb.c - list available USB devices
2  *
3  * Copyright 2013 Andre Renaud <andre@bluewatersys.com>
4 
5 USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USR|TOYFLAG_BIN))
6 
7 config LSUSB
8   bool "lsusb"
9   default y
10   help
11     usage: lsusb
12 
13     List USB hosts/devices.
14 */
15 
16 #include "toys.h"
17 
list_device(struct dirtree * new)18 static int list_device(struct dirtree *new)
19 {
20   FILE *file;
21   char *name;
22   int busnum = 0, devnum = 0, pid = 0, vid = 0;
23 
24   if (!new->parent) return DIRTREE_RECURSE;
25   if (new->name[0] == '.') return 0;
26   name = dirtree_path(new, 0);
27   sprintf(toybuf, "%s/uevent", name);
28   file = fopen(toybuf, "r");
29   if (file) {
30     int count = 0;
31 
32     while (fgets(toybuf, sizeof(toybuf), file))
33       if (sscanf(toybuf, "BUSNUM=%u\n", &busnum)
34           || sscanf(toybuf, "DEVNUM=%u\n", &devnum)
35           || sscanf(toybuf, "PRODUCT=%x/%x/", &pid, &vid)) count++;
36 
37     if (count == 3)
38       printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid);
39     fclose(file);
40   }
41   free(name);
42 
43   return 0;
44 }
45 
lsusb_main(void)46 void lsusb_main(void)
47 {
48   dirtree_read("/sys/bus/usb/devices/", list_device);
49 }
50