lspci.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * lspci 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. //usage:#define lspci_trivial_usage
  10. //usage: "[-mk]"
  11. //usage:#define lspci_full_usage "\n\n"
  12. //usage: "List all PCI devices"
  13. //usage: "\n"
  14. //usage: "\n -m Parsable output"
  15. //usage: "\n -k Show driver"
  16. #include "libbb.h"
  17. enum {
  18. OPT_m = (1 << 0),
  19. OPT_k = (1 << 1),
  20. };
  21. /*
  22. * PCI_SLOT_NAME PCI_CLASS: PCI_VID:PCI_DID [PCI_SUBSYS_VID:PCI_SUBSYS_DID] [DRIVER]
  23. */
  24. static int FAST_FUNC fileAction(
  25. const char *fileName,
  26. struct stat *statbuf UNUSED_PARAM,
  27. void *userData UNUSED_PARAM,
  28. int depth UNUSED_PARAM)
  29. {
  30. parser_t *parser;
  31. char *tokens[3];
  32. char *pci_slot_name = NULL, *driver = NULL;
  33. int pci_class = 0, pci_vid = 0, pci_did = 0;
  34. int pci_subsys_vid = 0, pci_subsys_did = 0;
  35. char *uevent_filename = concat_path_file(fileName, "/uevent");
  36. parser = config_open2(uevent_filename, fopen_for_read);
  37. free(uevent_filename);
  38. while (config_read(parser, tokens, 3, 2, "\0:=", PARSE_NORMAL)) {
  39. if (strcmp(tokens[0], "DRIVER") == 0) {
  40. driver = xstrdup(tokens[1]);
  41. continue;
  42. }
  43. if (strcmp(tokens[0], "PCI_CLASS") == 0) {
  44. pci_class = xstrtou(tokens[1], 16)>>8;
  45. continue;
  46. }
  47. if (strcmp(tokens[0], "PCI_ID") == 0) {
  48. pci_vid = xstrtou(tokens[1], 16);
  49. pci_did = xstrtou(tokens[2], 16);
  50. continue;
  51. }
  52. if (strcmp(tokens[0], "PCI_SUBSYS_ID") == 0) {
  53. pci_subsys_vid = xstrtou(tokens[1], 16);
  54. pci_subsys_did = xstrtou(tokens[2], 16);
  55. continue;
  56. }
  57. if (strcmp(tokens[0], "PCI_SLOT_NAME") == 0) {
  58. pci_slot_name = xstrdup(tokens[2]);
  59. continue;
  60. }
  61. }
  62. config_close(parser);
  63. if (option_mask32 & OPT_m) {
  64. printf("%s \"Class %04x\" \"%04x\" \"%04x\" \"%04x\" \"%04x\"",
  65. pci_slot_name, pci_class, pci_vid, pci_did,
  66. pci_subsys_vid, pci_subsys_did);
  67. } else {
  68. printf("%s Class %04x: %04x:%04x",
  69. pci_slot_name, pci_class, pci_vid, pci_did);
  70. }
  71. if ((option_mask32 & OPT_k) && driver) {
  72. if (option_mask32 & OPT_m) {
  73. printf(" \"%s\"", driver);
  74. } else {
  75. printf(" %s", driver);
  76. }
  77. }
  78. bb_putchar('\n');
  79. free(driver);
  80. free(pci_slot_name);
  81. return TRUE;
  82. }
  83. int lspci_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  84. int lspci_main(int argc UNUSED_PARAM, char **argv)
  85. {
  86. getopt32(argv, "m" /*non-compat:*/ "k" /*ignored:*/ "nv");
  87. recursive_action("/sys/bus/pci/devices",
  88. ACTION_RECURSE,
  89. fileAction,
  90. NULL, /* dirAction */
  91. NULL, /* userData */
  92. 0 /* depth */);
  93. return EXIT_SUCCESS;
  94. }