lsusb.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. //config:config LSUSB
  10. //config: bool "lsusb (4.2 kb)"
  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. //applet:IF_LSUSB(APPLET_NOEXEC(lsusb, lsusb, BB_DIR_USR_BIN, BB_SUID_DROP, lsusb))
  19. //kbuild:lib-$(CONFIG_LSUSB) += lsusb.o
  20. //usage:#define lsusb_trivial_usage NOUSAGE_STR
  21. //usage:#define lsusb_full_usage ""
  22. #include "libbb.h"
  23. static int FAST_FUNC fileAction(
  24. const char *fileName,
  25. struct stat *statbuf UNUSED_PARAM,
  26. void *userData UNUSED_PARAM,
  27. int depth UNUSED_PARAM)
  28. {
  29. parser_t *parser;
  30. char *tokens[4];
  31. char *busnum = NULL, *devnum = NULL;
  32. int product_vid = 0, product_did = 0;
  33. char *uevent_filename = concat_path_file(fileName, "/uevent");
  34. parser = config_open2(uevent_filename, fopen_for_read);
  35. free(uevent_filename);
  36. while (config_read(parser, tokens, 4, 2, "\\/=", PARSE_NORMAL)) {
  37. if ((parser->lineno == 1) && strcmp(tokens[0], "DEVTYPE") == 0) {
  38. break;
  39. }
  40. if (strcmp(tokens[0], "PRODUCT") == 0) {
  41. product_vid = xstrtou(tokens[1], 16);
  42. product_did = xstrtou(tokens[2], 16);
  43. continue;
  44. }
  45. if (strcmp(tokens[0], "BUSNUM") == 0) {
  46. busnum = xstrdup(tokens[1]);
  47. continue;
  48. }
  49. if (strcmp(tokens[0], "DEVNUM") == 0) {
  50. devnum = xstrdup(tokens[1]);
  51. continue;
  52. }
  53. }
  54. config_close(parser);
  55. if (busnum) {
  56. printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did);
  57. free(busnum);
  58. free(devnum);
  59. }
  60. return TRUE;
  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. recursive_action("/sys/bus/usb/devices",
  67. ACTION_RECURSE,
  68. fileAction,
  69. NULL, /* dirAction */
  70. NULL, /* userData */
  71. 0 /* depth */);
  72. return EXIT_SUCCESS;
  73. }