1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include "libbb.h"
32
33enum {
34 OPT_m = (1 << 0),
35 OPT_k = (1 << 1),
36};
37
38
39
40
41static int FAST_FUNC fileAction(
42 const char *fileName,
43 struct stat *statbuf UNUSED_PARAM,
44 void *userData UNUSED_PARAM,
45 int depth UNUSED_PARAM)
46{
47 parser_t *parser;
48 char *tokens[3];
49 char *pci_slot_name = NULL, *driver = NULL;
50 int pci_class = 0, pci_vid = 0, pci_did = 0;
51 int pci_subsys_vid = 0, pci_subsys_did = 0;
52
53 char *uevent_filename = concat_path_file(fileName, "/uevent");
54 parser = config_open2(uevent_filename, fopen_for_read);
55 free(uevent_filename);
56
57 while (config_read(parser, tokens, 3, 2, "\0:=", PARSE_NORMAL)) {
58 if (strcmp(tokens[0], "DRIVER") == 0) {
59 driver = xstrdup(tokens[1]);
60 continue;
61 }
62
63 if (strcmp(tokens[0], "PCI_CLASS") == 0) {
64 pci_class = xstrtou(tokens[1], 16)>>8;
65 continue;
66 }
67
68 if (strcmp(tokens[0], "PCI_ID") == 0) {
69 pci_vid = xstrtou(tokens[1], 16);
70 pci_did = xstrtou(tokens[2], 16);
71 continue;
72 }
73
74 if (strcmp(tokens[0], "PCI_SUBSYS_ID") == 0) {
75 pci_subsys_vid = xstrtou(tokens[1], 16);
76 pci_subsys_did = xstrtou(tokens[2], 16);
77 continue;
78 }
79
80 if (strcmp(tokens[0], "PCI_SLOT_NAME") == 0) {
81 pci_slot_name = xstrdup(tokens[2]);
82 continue;
83 }
84 }
85 config_close(parser);
86
87
88 if (option_mask32 & OPT_m) {
89 printf("%s \"Class %04x\" \"%04x\" \"%04x\" \"%04x\" \"%04x\"",
90 pci_slot_name, pci_class, pci_vid, pci_did,
91 pci_subsys_vid, pci_subsys_did);
92 } else {
93 printf("%s Class %04x: %04x:%04x",
94 pci_slot_name, pci_class, pci_vid, pci_did);
95 }
96
97 if ((option_mask32 & OPT_k) && driver) {
98 if (option_mask32 & OPT_m) {
99 printf(" \"%s\"", driver);
100 } else {
101 printf(" %s", driver);
102 }
103 }
104 bb_putchar('\n');
105
106 free(driver);
107 free(pci_slot_name);
108
109 return TRUE;
110}
111
112int lspci_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
113int lspci_main(int argc UNUSED_PARAM, char **argv)
114{
115 getopt32(argv, "m" "k" "nv");
116
117 recursive_action("/sys/bus/pci/devices",
118 ACTION_RECURSE,
119 fileAction,
120 NULL,
121 NULL,
122 0 );
123
124 return EXIT_SUCCESS;
125}
126