blob: cabf047cf7fc9786394b1130a5185576ef500e47 [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 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +01009//config:config LSUSB
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "lsusb (3.5 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config: default y
12//config: #select PLATFORM_LINUX
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: lsusb is a utility for displaying information about USB buses in the
15//config: system and devices connected to them.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010016//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: This version uses sysfs (/sys/bus/usb/devices) only.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010018
19//applet:IF_LSUSB(APPLET(lsusb, BB_DIR_USR_BIN, BB_SUID_DROP))
20
21//kbuild:lib-$(CONFIG_LSUSB) += lsusb.o
Pere Orga5bc8c002011-04-11 03:29:49 +020022
23//usage:#define lsusb_trivial_usage NOUSAGE_STR
24//usage:#define lsusb_full_usage ""
25
Denys Vlasenkoccd1efc2010-04-03 15:39:47 +020026#include "libbb.h"
Souf Oued11a802a2009-12-07 01:37:34 +010027
28static int FAST_FUNC fileAction(
29 const char *fileName,
30 struct stat *statbuf UNUSED_PARAM,
31 void *userData UNUSED_PARAM,
32 int depth UNUSED_PARAM)
33{
34 parser_t *parser;
Souf Oued1c3f1172010-03-23 05:06:37 +010035 char *tokens[4];
36 char *busnum = NULL, *devnum = NULL;
Souf Oued11a802a2009-12-07 01:37:34 +010037 int product_vid = 0, product_did = 0;
Souf Oued11a802a2009-12-07 01:37:34 +010038 char *uevent_filename = concat_path_file(fileName, "/uevent");
Souf Oued1c3f1172010-03-23 05:06:37 +010039
Souf Oued11a802a2009-12-07 01:37:34 +010040 parser = config_open2(uevent_filename, fopen_for_read);
41 free(uevent_filename);
42
Souf Oued1c3f1172010-03-23 05:06:37 +010043 while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) {
Souf Oued11a802a2009-12-07 01:37:34 +010044 if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
45 break;
46 }
47
Souf Oued11a802a2009-12-07 01:37:34 +010048 if (strcmp(tokens[0], "PRODUCT") == 0) {
49 product_vid = xstrtou(tokens[1], 16);
50 product_did = xstrtou(tokens[2], 16);
51 continue;
52 }
Souf Oued1c3f1172010-03-23 05:06:37 +010053
54 if (strcmp(tokens[0], "BUSNUM") == 0) {
55 busnum = xstrdup(tokens[1]);
56 continue;
57 }
58
59 if (strcmp(tokens[0], "DEVNUM") == 0) {
60 devnum = xstrdup(tokens[1]);
61 continue;
62 }
Souf Oued11a802a2009-12-07 01:37:34 +010063 }
64 config_close(parser);
65
Souf Oued1c3f1172010-03-23 05:06:37 +010066 if (busnum) {
67 printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did);
68 free(busnum);
69 free(devnum);
Souf Oued11a802a2009-12-07 01:37:34 +010070 }
71
72 return TRUE;
73}
74
75int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
76int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
77{
78 /* no options, no getopt */
79
80 recursive_action("/sys/bus/usb/devices",
81 ACTION_RECURSE,
82 fileAction,
83 NULL, /* dirAction */
84 NULL, /* userData */
85 0 /* depth */);
86
87 return EXIT_SUCCESS;
88}