3
0

lspci.c 2.3 KB

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