blob: 9abb748ce3a5e5526ea49e0953782f343afd4a1f [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 Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "lsusb (4.2 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config: default y
Denys Vlasenkodd898c92016-11-23 11:46:32 +010012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: lsusb is a utility for displaying information about USB buses in the
14//config: system and devices connected to them.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010015//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: This version uses sysfs (/sys/bus/usb/devices) only.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010017
Denys Vlasenko3239ab82017-08-05 23:28:19 +020018//applet:IF_LSUSB(APPLET_NOEXEC(lsusb, lsusb, BB_DIR_USR_BIN, BB_SUID_DROP, lsusb))
Denys Vlasenkodd898c92016-11-23 11:46:32 +010019
20//kbuild:lib-$(CONFIG_LSUSB) += lsusb.o
Pere Orga5bc8c002011-04-11 03:29:49 +020021
22//usage:#define lsusb_trivial_usage NOUSAGE_STR
23//usage:#define lsusb_full_usage ""
24
Denys Vlasenkoccd1efc2010-04-03 15:39:47 +020025#include "libbb.h"
Souf Oued11a802a2009-12-07 01:37:34 +010026
Denys Vlasenko689d0652020-10-01 21:52:16 +020027static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
Souf Oued11a802a2009-12-07 01:37:34 +010028 const char *fileName,
Denys Vlasenko689d0652020-10-01 21:52:16 +020029 struct stat *statbuf UNUSED_PARAM)
Souf Oued11a802a2009-12-07 01:37:34 +010030{
31 parser_t *parser;
Souf Oued1c3f1172010-03-23 05:06:37 +010032 char *tokens[4];
33 char *busnum = NULL, *devnum = NULL;
Souf Oued11a802a2009-12-07 01:37:34 +010034 int product_vid = 0, product_did = 0;
Souf Oued11a802a2009-12-07 01:37:34 +010035 char *uevent_filename = concat_path_file(fileName, "/uevent");
Souf Oued1c3f1172010-03-23 05:06:37 +010036
Souf Oued11a802a2009-12-07 01:37:34 +010037 parser = config_open2(uevent_filename, fopen_for_read);
38 free(uevent_filename);
39
Souf Oued1c3f1172010-03-23 05:06:37 +010040 while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) {
Souf Oued11a802a2009-12-07 01:37:34 +010041 if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
42 break;
43 }
44
Souf Oued11a802a2009-12-07 01:37:34 +010045 if (strcmp(tokens[0], "PRODUCT") == 0) {
46 product_vid = xstrtou(tokens[1], 16);
47 product_did = xstrtou(tokens[2], 16);
48 continue;
49 }
Souf Oued1c3f1172010-03-23 05:06:37 +010050
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 }
Souf Oued11a802a2009-12-07 01:37:34 +010060 }
61 config_close(parser);
62
Souf Oued1c3f1172010-03-23 05:06:37 +010063 if (busnum) {
64 printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did);
65 free(busnum);
66 free(devnum);
Souf Oued11a802a2009-12-07 01:37:34 +010067 }
68
69 return TRUE;
70}
71
72int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
73int 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 */
Denys Vlasenko689d0652020-10-01 21:52:16 +020081 NULL /* userData */
82 );
Souf Oued11a802a2009-12-07 01:37:34 +010083
84 return EXIT_SUCCESS;
85}