Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 3 | * lsusb implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2009 Malek Degachi <malek-degachi@laposte.net> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 8 | */ |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 9 | //config:config LSUSB |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "lsusb (4.2 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 11 | //config: default y |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: lsusb is a utility for displaying information about USB buses in the |
| 14 | //config: system and devices connected to them. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 15 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: This version uses sysfs (/sys/bus/usb/devices) only. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 17 | |
Denys Vlasenko | 3239ab8 | 2017-08-05 23:28:19 +0200 | [diff] [blame] | 18 | //applet:IF_LSUSB(APPLET_NOEXEC(lsusb, lsusb, BB_DIR_USR_BIN, BB_SUID_DROP, lsusb)) |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 19 | |
| 20 | //kbuild:lib-$(CONFIG_LSUSB) += lsusb.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 21 | |
| 22 | //usage:#define lsusb_trivial_usage NOUSAGE_STR |
| 23 | //usage:#define lsusb_full_usage "" |
| 24 | |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 25 | #include "libbb.h" |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 26 | |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 27 | static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM, |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 28 | const char *fileName, |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 29 | struct stat *statbuf UNUSED_PARAM) |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 30 | { |
| 31 | parser_t *parser; |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 32 | char *tokens[4]; |
| 33 | char *busnum = NULL, *devnum = NULL; |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 34 | int product_vid = 0, product_did = 0; |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 35 | char *uevent_filename = concat_path_file(fileName, "/uevent"); |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 36 | |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 37 | parser = config_open2(uevent_filename, fopen_for_read); |
| 38 | free(uevent_filename); |
| 39 | |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 40 | while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) { |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 41 | if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) { |
| 42 | break; |
| 43 | } |
| 44 | |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 45 | if (strcmp(tokens[0], "PRODUCT") == 0) { |
| 46 | product_vid = xstrtou(tokens[1], 16); |
| 47 | product_did = xstrtou(tokens[2], 16); |
| 48 | continue; |
| 49 | } |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 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 | } |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 60 | } |
| 61 | config_close(parser); |
| 62 | |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 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); |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return TRUE; |
| 70 | } |
| 71 | |
| 72 | int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 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 */ |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 81 | NULL /* userData */ |
| 82 | ); |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 83 | |
| 84 | return EXIT_SUCCESS; |
| 85 | } |