3
0

lspci.c 3.0 KB

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