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 | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame^] | 10 | //config: bool "lsusb (3.5 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: #select PLATFORM_LINUX |
| 13 | //config: help |
| 14 | //config: lsusb is a utility for displaying information about USB buses in the |
| 15 | //config: system and devices connected to them. |
| 16 | //config: |
| 17 | //config: This version uses sysfs (/sys/bus/usb/devices) only. |
| 18 | |
| 19 | //applet:IF_LSUSB(APPLET(lsusb, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 20 | |
| 21 | //kbuild:lib-$(CONFIG_LSUSB) += lsusb.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 22 | |
| 23 | //usage:#define lsusb_trivial_usage NOUSAGE_STR |
| 24 | //usage:#define lsusb_full_usage "" |
| 25 | |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 26 | #include "libbb.h" |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 27 | |
| 28 | static 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 Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 35 | char *tokens[4]; |
| 36 | char *busnum = NULL, *devnum = NULL; |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 37 | int product_vid = 0, product_did = 0; |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 38 | char *uevent_filename = concat_path_file(fileName, "/uevent"); |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 39 | |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 40 | parser = config_open2(uevent_filename, fopen_for_read); |
| 41 | free(uevent_filename); |
| 42 | |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 43 | while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) { |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 44 | if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) { |
| 45 | break; |
| 46 | } |
| 47 | |
Souf Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 48 | if (strcmp(tokens[0], "PRODUCT") == 0) { |
| 49 | product_vid = xstrtou(tokens[1], 16); |
| 50 | product_did = xstrtou(tokens[2], 16); |
| 51 | continue; |
| 52 | } |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 53 | |
| 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 Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 63 | } |
| 64 | config_close(parser); |
| 65 | |
Souf Oued | 1c3f117 | 2010-03-23 05:06:37 +0100 | [diff] [blame] | 66 | 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 Oued | 11a802a | 2009-12-07 01:37:34 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return TRUE; |
| 73 | } |
| 74 | |
| 75 | int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 76 | int 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 | } |