lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * lsusb implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2009 Malek Degachi <malek-degachi@laposte.net> |
| 6 | * |
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 8 | */ |
| 9 | |
| 10 | //usage:#define lsusb_trivial_usage NOUSAGE_STR |
| 11 | //usage:#define lsusb_full_usage "" |
| 12 | |
| 13 | #include "libbb.h" |
| 14 | |
| 15 | static 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; |
| 22 | char *tokens[4]; |
| 23 | char *busnum = NULL, *devnum = NULL; |
| 24 | int product_vid = 0, product_did = 0; |
| 25 | char *uevent_filename = concat_path_file(fileName, "/uevent"); |
| 26 | |
| 27 | parser = config_open2(uevent_filename, fopen_for_read); |
| 28 | free(uevent_filename); |
| 29 | |
| 30 | while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) { |
| 31 | if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) { |
| 32 | break; |
| 33 | } |
| 34 | |
| 35 | if (strcmp(tokens[0], "PRODUCT") == 0) { |
| 36 | product_vid = xstrtou(tokens[1], 16); |
| 37 | product_did = xstrtou(tokens[2], 16); |
| 38 | continue; |
| 39 | } |
| 40 | |
| 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 | } |
| 50 | } |
| 51 | config_close(parser); |
| 52 | |
| 53 | if (busnum) { |
| 54 | printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did); |
| 55 | free(busnum); |
| 56 | free(devnum); |
| 57 | } |
| 58 | |
| 59 | return TRUE; |
| 60 | } |
| 61 | |
| 62 | int lsusb_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 63 | int 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 | } |