aboutsummaryrefslogtreecommitdiff
path: root/toys/other/lsusb.c
blob: 07886e8cfe076243c28f33b9b06a60fc6d168fa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* lsusb.c - list available USB devices
 *
 * Copyright 2013 Andre Renaud <andre@bluewatersys.com>

USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USR|TOYFLAG_BIN))

config LSUSB
  bool "lsusb"
  default y
  help
    usage: lsusb

    List USB hosts/devices.
*/

#include "toys.h"

static int list_device(struct dirtree *new)
{
  FILE *file;
  char *name;
  int busnum = 0, devnum = 0, pid = 0, vid = 0;

  if (!new->parent) return DIRTREE_RECURSE;
  if (new->name[0] == '.') return 0;
  name = dirtree_path(new, 0);
  snprintf(toybuf, sizeof(toybuf), "%s/%s", name, "/uevent");
  file = fopen(toybuf, "r");
  if (file) {
    int count = 0;

    while (fgets(toybuf, sizeof(toybuf), file))
      if (sscanf(toybuf, "BUSNUM=%u\n", &busnum)
          || sscanf(toybuf, "DEVNUM=%u\n", &devnum)
          || sscanf(toybuf, "PRODUCT=%x/%x/", &pid, &vid)) count++;

    if (count == 3)
      printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid);
    fclose(file);
  }
  free(name);

  return 0;
}

void lsusb_main(void)
{
  dirtree_read("/sys/bus/usb/devices/", list_device);
}