1 /* lsusb.c - list available USB devices
3 * Copyright 2013 Andre Renaud <andre@bluewatersys.com>
5 USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USR|TOYFLAG_BIN))
13 List USB hosts/devices.
18 static int list_device(struct dirtree *new)
22 int busnum = 0, devnum = 0, pid = 0, vid = 0;
24 if (!new->parent) return DIRTREE_RECURSE;
25 if (new->name[0] == '.') return 0;
26 name = dirtree_path(new, 0);
27 snprintf(toybuf, sizeof(toybuf), "%s/%s", name, "/uevent");
28 file = fopen(toybuf, "r");
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++;
38 printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid);
48 dirtree_read("/sys/bus/usb/devices/", list_device);