3
0

lspci.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. //config:config LSPCI
  10. //config: bool "lspci (6.3 kb)"
  11. //config: default y
  12. //config: help
  13. //config: lspci is a utility for displaying information about PCI buses in the
  14. //config: system and devices connected to them.
  15. //config:
  16. //config: This version uses sysfs (/sys/bus/pci/devices) only.
  17. //applet:IF_LSPCI(APPLET_NOEXEC(lspci, lspci, BB_DIR_USR_BIN, BB_SUID_DROP, lspci))
  18. //kbuild:lib-$(CONFIG_LSPCI) += lspci.o
  19. //usage:#define lspci_trivial_usage
  20. //usage: "[-mk]"
  21. //usage:#define lspci_full_usage "\n\n"
  22. //usage: "List all PCI devices"
  23. //usage: "\n"
  24. //usage: "\n -m Parsable output"
  25. //usage: "\n -k Show driver"
  26. #include "libbb.h"
  27. enum {
  28. OPT_m = (1 << 0),
  29. OPT_k = (1 << 1),
  30. };
  31. /*
  32. * PCI_SLOT_NAME PCI_CLASS: PCI_VID:PCI_DID [PCI_SUBSYS_VID:PCI_SUBSYS_DID] [DRIVER]
  33. */
  34. static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
  35. const char *fileName,
  36. struct stat *statbuf UNUSED_PARAM)
  37. {
  38. parser_t *parser;
  39. char *tokens[3];
  40. char *pci_slot_name = NULL, *driver = NULL;
  41. int pci_class = 0, pci_vid = 0, pci_did = 0;
  42. int pci_subsys_vid = 0, pci_subsys_did = 0;
  43. char *uevent_filename = concat_path_file(fileName, "/uevent");
  44. parser = config_open2(uevent_filename, fopen_for_read);
  45. free(uevent_filename);
  46. while (config_read(parser, tokens, 3, 2, "\0:=", PARSE_NORMAL)) {
  47. if (strcmp(tokens[0], "DRIVER") == 0) {
  48. driver = xstrdup(tokens[1]);
  49. continue;
  50. }
  51. if (strcmp(tokens[0], "PCI_CLASS") == 0) {
  52. pci_class = xstrtou(tokens[1], 16)>>8;
  53. continue;
  54. }
  55. if (strcmp(tokens[0], "PCI_ID") == 0) {
  56. pci_vid = xstrtou(tokens[1], 16);
  57. pci_did = xstrtou(tokens[2], 16);
  58. continue;
  59. }
  60. if (strcmp(tokens[0], "PCI_SUBSYS_ID") == 0) {
  61. pci_subsys_vid = xstrtou(tokens[1], 16);
  62. pci_subsys_did = xstrtou(tokens[2], 16);
  63. continue;
  64. }
  65. if (strcmp(tokens[0], "PCI_SLOT_NAME") == 0) {
  66. pci_slot_name = xstrdup(tokens[2]);
  67. continue;
  68. }
  69. }
  70. config_close(parser);
  71. if (option_mask32 & OPT_m) {
  72. printf("%s \"Class %04x\" \"%04x\" \"%04x\" \"%04x\" \"%04x\"",
  73. pci_slot_name, pci_class, pci_vid, pci_did,
  74. pci_subsys_vid, pci_subsys_did);
  75. } else {
  76. printf("%s Class %04x: %04x:%04x",
  77. pci_slot_name, pci_class, pci_vid, pci_did);
  78. }
  79. if ((option_mask32 & OPT_k) && driver) {
  80. if (option_mask32 & OPT_m) {
  81. printf(" \"%s\"", driver);
  82. } else {
  83. printf(" %s", driver);
  84. }
  85. }
  86. bb_putchar('\n');
  87. free(driver);
  88. free(pci_slot_name);
  89. return TRUE;
  90. }
  91. int lspci_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  92. int lspci_main(int argc UNUSED_PARAM, char **argv)
  93. {
  94. getopt32(argv, "m" /*non-compat:*/ "k" /*ignored:*/ "nv");
  95. recursive_action("/sys/bus/pci/devices",
  96. ACTION_RECURSE,
  97. fileAction,
  98. NULL, /* dirAction */
  99. NULL /* userData */
  100. );
  101. return EXIT_SUCCESS;
  102. }