blob: 540f21ec6d61cf1f8827fceb65f046d8bf649986 [file] [log] [blame]
Souf Oued11a802a2009-12-07 01:37:34 +01001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenkoccd1efc2010-04-03 15:39:47 +02003 * lsusb implementation for busybox
4 *
5 * Copyright (C) 2009 Malek Degachi <malek-degachi@laposte.net>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkoccd1efc2010-04-03 15:39:47 +02008 */
Pere Orga5bc8c002011-04-11 03:29:49 +02009
10//usage:#define lsusb_trivial_usage NOUSAGE_STR
11//usage:#define lsusb_full_usage ""
12
Denys Vlasenkoccd1efc2010-04-03 15:39:47 +020013#include "libbb.h"
Souf Oued11a802a2009-12-07 01:37:34 +010014
15static int FAST_FUNC fileAction(
16 const char *fileName,
17 struct stat *statbuf UNUSED_PARAM,
18 void *userData UNUSED_PARAM,
19 int depth UNUSED_PARAM)
20{
21 parser_t *parser;
Souf Oued1c3f1172010-03-23 05:06:37 +010022 char *tokens[4];
23 char *busnum = NULL, *devnum = NULL;
Souf Oued11a802a2009-12-07 01:37:34 +010024 int product_vid = 0, product_did = 0;
Souf Oued11a802a2009-12-07 01:37:34 +010025 char *uevent_filename = concat_path_file(fileName, "/uevent");
Souf Oued1c3f1172010-03-23 05:06:37 +010026
Souf Oued11a802a2009-12-07 01:37:34 +010027 parser = config_open2(uevent_filename, fopen_for_read);
28 free(uevent_filename);
29
Souf Oued1c3f1172010-03-23 05:06:37 +010030 while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) {
Souf Oued11a802a2009-12-07 01:37:34 +010031 if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
32 break;
33 }
34
Souf Oued11a802a2009-12-07 01:37:34 +010035 if (strcmp(tokens[0], "PRODUCT") == 0) {
36 product_vid = xstrtou(tokens[1], 16);
37 product_did = xstrtou(tokens[2], 16);
38 continue;
39 }
Souf Oued1c3f1172010-03-23 05:06:37 +010040
41 if (strcmp(tokens[0], "BUSNUM") == 0) {
42 busnum = xstrdup(tokens[1]);
43 continue;
44 }
45
46 if (strcmp(tokens[0], "DEVNUM") == 0) {
47 devnum = xstrdup(tokens[1]);
48 continue;
49 }
Souf Oued11a802a2009-12-07 01:37:34 +010050 }
51 config_close(parser);
52
Souf Oued1c3f1172010-03-23 05:06:37 +010053 if (busnum) {
54 printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did);
55 free(busnum);
56 free(devnum);
Souf Oued11a802a2009-12-07 01:37:34 +010057 }
58
59 return TRUE;
60}
61
62int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
63int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
64{
65 /* no options, no getopt */
66
67 recursive_action("/sys/bus/usb/devices",
68 ACTION_RECURSE,
69 fileAction,
70 NULL, /* dirAction */
71 NULL, /* userData */
72 0 /* depth */);
73
74 return EXIT_SUCCESS;
75}