1 /* vi: set sw=4 ts=4: */
2 /*
3 * lsusb implementation for busybox
4 *
5 * Copyright (C) 2009 Malek Degachi <malek-degachi@laposte.net>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9 //config:config LSUSB
10 //config: bool "lsusb (4.2 kb)"
11 //config: default y
12 //config: help
13 //config: lsusb is a utility for displaying information about USB buses in the
14 //config: system and devices connected to them.
15 //config:
16 //config: This version uses sysfs (/sys/bus/usb/devices) only.
17
18 //applet:IF_LSUSB(APPLET_NOEXEC(lsusb, lsusb, BB_DIR_USR_BIN, BB_SUID_DROP, lsusb))
19
20 //kbuild:lib-$(CONFIG_LSUSB) += lsusb.o
21
22 //usage:#define lsusb_trivial_usage NOUSAGE_STR
23 //usage:#define lsusb_full_usage ""
24
25 #include "libbb.h"
26
fileAction(struct recursive_state * state UNUSED_PARAM,const char * fileName,struct stat * statbuf UNUSED_PARAM)27 static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
28 const char *fileName,
29 struct stat *statbuf UNUSED_PARAM)
30 {
31 parser_t *parser;
32 char *tokens[4];
33 char *busnum = NULL, *devnum = NULL;
34 int product_vid = 0, product_did = 0;
35 char *uevent_filename = concat_path_file(fileName, "/uevent");
36
37 parser = config_open2(uevent_filename, fopen_for_read);
38 free(uevent_filename);
39
40 while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) {
41 if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
42 break;
43 }
44
45 if (strcmp(tokens[0], "PRODUCT") == 0) {
46 product_vid = xstrtou(tokens[1], 16);
47 product_did = xstrtou(tokens[2], 16);
48 continue;
49 }
50
51 if (strcmp(tokens[0], "BUSNUM") == 0) {
52 busnum = xstrdup(tokens[1]);
53 continue;
54 }
55
56 if (strcmp(tokens[0], "DEVNUM") == 0) {
57 devnum = xstrdup(tokens[1]);
58 continue;
59 }
60 }
61 config_close(parser);
62
63 if (busnum) {
64 printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did);
65 free(busnum);
66 free(devnum);
67 }
68
69 return TRUE;
70 }
71
72 int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
lsusb_main(int argc UNUSED_PARAM,char ** argv UNUSED_PARAM)73 int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
74 {
75 /* no options, no getopt */
76
77 recursive_action("/sys/bus/usb/devices",
78 ACTION_RECURSE,
79 fileAction,
80 NULL, /* dirAction */
81 NULL /* userData */
82 );
83
84 return EXIT_SUCCESS;
85 }
86