comparison toys/other/lsusb.c @ 902:36993c59a3d3

Tighten up lsusb, default to "y".
author Rob Landley <rob@landley.net>
date Tue, 14 May 2013 20:42:54 -0500
parents edd8e6fd418d
children
comparison
equal deleted inserted replaced
901:c44dff160d65 902:36993c59a3d3
4 4
5 USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USR|TOYFLAG_BIN)) 5 USE_LSUSB(NEWTOY(lsusb, NULL, TOYFLAG_USR|TOYFLAG_BIN))
6 6
7 config LSUSB 7 config LSUSB
8 bool "lsusb" 8 bool "lsusb"
9 default n 9 default y
10 help 10 help
11 usage: lsusb 11 usage: lsusb
12
13 List USB hosts/devices.
12 */ 14 */
13 15
14 #include "toys.h" 16 #include "toys.h"
15 17
16 static int list_device(struct dirtree *new) 18 static int list_device(struct dirtree *new)
17 { 19 {
18 FILE *file; 20 FILE *file;
19 char *name; 21 char *name;
20 int busnum = 0; 22 int busnum = 0, devnum = 0, pid = 0, vid = 0;
21 int devnum = 0; 23
22 int pid = 0; 24 if (!new->parent) return DIRTREE_RECURSE;
23 int vid = 0; 25 if (new->name[0] == '.') return 0;
24 if (!new->parent)
25 return DIRTREE_RECURSE;
26 if (new->name[0] == '.')
27 return 0;
28 name = dirtree_path(new, 0); 26 name = dirtree_path(new, 0);
29 snprintf(toybuf, sizeof(toybuf), "%s/%s", name, "/uevent"); 27 snprintf(toybuf, sizeof(toybuf), "%s/%s", name, "/uevent");
30 file = fopen(toybuf, "r"); 28 file = fopen(toybuf, "r");
31 if (!file) 29 if (file) {
32 return 0; 30 int count = 0;
33 if (!fgets(toybuf, sizeof(toybuf), file) || !strncmp(toybuf, "DEVTYPE=", 8)) { 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);
34 fclose(file); 39 fclose(file);
35 return 0;
36 } 40 }
37 while (fgets(toybuf, sizeof(toybuf), file)) { 41 free(name);
38 if (!strncmp(toybuf, "BUSNUM=", 7))
39 busnum = atoi(&toybuf[7]);
40 if (!strncmp(toybuf, "DEVNUM=", 7))
41 devnum = atoi(&toybuf[7]);
42 if (!strncmp(toybuf, "PRODUCT=", 8)) {
43 char *pos = strchr(toybuf, '/');
44 pid = xstrtoul(&toybuf[8], NULL, 16);
45 if (pos)
46 vid = xstrtoul(pos + 1, NULL, 16);
47 }
48 }
49 fclose(file);
50
51 printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid);
52 42
53 return 0; 43 return 0;
54 } 44 }
55 45
56 void lsusb_main(void) 46 void lsusb_main(void)
57 { 47 {
58 dirtree_read("/sys/bus/usb/devices/", list_device); 48 dirtree_read("/sys/bus/usb/devices/", list_device);
59 return;
60 } 49 }