blob: 8b38a2366edf9604d7b2043590dd96b2db4d6496
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 | |
19 | //applet:IF_LSPCI(APPLET(lspci, BB_DIR_USR_BIN, BB_SUID_DROP)) |
20 | |
21 | //kbuild:lib-$(CONFIG_LSPCI) += lspci.o |
22 | |
23 | //usage:#define lspci_trivial_usage |
24 | //usage: "[-mk]" |
25 | //usage:#define lspci_full_usage "\n\n" |
26 | //usage: "List all PCI devices" |
27 | //usage: "\n" |
28 | //usage: "\n -m Parsable output" |
29 | //usage: "\n -k Show driver" |
30 | |
31 | #include "libbb.h" |
32 | |
33 | enum { |
34 | OPT_m = (1 << 0), |
35 | OPT_k = (1 << 1), |
36 | }; |
37 | |
38 | /* |
39 | * PCI_SLOT_NAME PCI_CLASS: PCI_VID:PCI_DID [PCI_SUBSYS_VID:PCI_SUBSYS_DID] [DRIVER] |
40 | */ |
41 | static 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 | |
112 | int lspci_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
113 | int lspci_main(int argc UNUSED_PARAM, char **argv) |
114 | { |
115 | getopt32(argv, "m" /*non-compat:*/ "k" /*ignored:*/ "nv"); |
116 | |
117 | recursive_action("/sys/bus/pci/devices", |
118 | ACTION_RECURSE, |
119 | fileAction, |
120 | NULL, /* dirAction */ |
121 | NULL, /* userData */ |
122 | 0 /* depth */); |
123 | |
124 | return EXIT_SUCCESS; |
125 | } |
126 |