blob: c22cbcc1eba6580970d7d6a51f5f648a02b2bf0d [file] [log] [blame]
Souf Oued982bc712009-12-05 17:56:25 +01001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenkoccd1efc2010-04-03 15:39:47 +02003 * lspci implementation for busybox
4 *
5 * Copyright (C) 2009 Malek Degachi <malek-degachi@laposte.net>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkoccd1efc2010-04-03 15:39:47 +02008 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +01009//config:config LSPCI
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "lspci (6.3 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config: default y
Denys Vlasenkodd898c92016-11-23 11:46:32 +010012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: lspci is a utility for displaying information about PCI buses in the
14//config: system and devices connected to them.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010015//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: This version uses sysfs (/sys/bus/pci/devices) only.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010017
Denys Vlasenko3239ab82017-08-05 23:28:19 +020018//applet:IF_LSPCI(APPLET_NOEXEC(lspci, lspci, BB_DIR_USR_BIN, BB_SUID_DROP, lspci))
Denys Vlasenkodd898c92016-11-23 11:46:32 +010019
20//kbuild:lib-$(CONFIG_LSPCI) += lspci.o
Pere Orga5bc8c002011-04-11 03:29:49 +020021
22//usage:#define lspci_trivial_usage
23//usage: "[-mk]"
24//usage:#define lspci_full_usage "\n\n"
25//usage: "List all PCI devices"
26//usage: "\n"
Dan Fandrichb5de0c12011-07-08 05:47:49 +020027//usage: "\n -m Parsable output"
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage: "\n -k Show driver"
29
Denys Vlasenkoccd1efc2010-04-03 15:39:47 +020030#include "libbb.h"
Souf Oued982bc712009-12-05 17:56:25 +010031
32enum {
33 OPT_m = (1 << 0),
34 OPT_k = (1 << 1),
35};
36
37/*
38 * PCI_SLOT_NAME PCI_CLASS: PCI_VID:PCI_DID [PCI_SUBSYS_VID:PCI_SUBSYS_DID] [DRIVER]
39 */
Denys Vlasenko689d0652020-10-01 21:52:16 +020040static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
Souf Oued982bc712009-12-05 17:56:25 +010041 const char *fileName,
Denys Vlasenko689d0652020-10-01 21:52:16 +020042 struct stat *statbuf UNUSED_PARAM)
Souf Oued982bc712009-12-05 17:56:25 +010043{
44 parser_t *parser;
45 char *tokens[3];
46 char *pci_slot_name = NULL, *driver = NULL;
47 int pci_class = 0, pci_vid = 0, pci_did = 0;
48 int pci_subsys_vid = 0, pci_subsys_did = 0;
49
50 char *uevent_filename = concat_path_file(fileName, "/uevent");
51 parser = config_open2(uevent_filename, fopen_for_read);
52 free(uevent_filename);
53
54 while (config_read(parser, tokens, 3, 2, "\0:=", PARSE_NORMAL)) {
55 if (strcmp(tokens[0], "DRIVER") == 0) {
56 driver = xstrdup(tokens[1]);
57 continue;
58 }
59
60 if (strcmp(tokens[0], "PCI_CLASS") == 0) {
61 pci_class = xstrtou(tokens[1], 16)>>8;
62 continue;
63 }
64
65 if (strcmp(tokens[0], "PCI_ID") == 0) {
66 pci_vid = xstrtou(tokens[1], 16);
67 pci_did = xstrtou(tokens[2], 16);
68 continue;
69 }
70
71 if (strcmp(tokens[0], "PCI_SUBSYS_ID") == 0) {
72 pci_subsys_vid = xstrtou(tokens[1], 16);
73 pci_subsys_did = xstrtou(tokens[2], 16);
74 continue;
75 }
76
77 if (strcmp(tokens[0], "PCI_SLOT_NAME") == 0) {
78 pci_slot_name = xstrdup(tokens[2]);
79 continue;
80 }
81 }
82 config_close(parser);
83
84
85 if (option_mask32 & OPT_m) {
86 printf("%s \"Class %04x\" \"%04x\" \"%04x\" \"%04x\" \"%04x\"",
Denys Vlasenko69675782013-01-14 01:34:48 +010087 pci_slot_name, pci_class, pci_vid, pci_did,
88 pci_subsys_vid, pci_subsys_did);
Souf Oued982bc712009-12-05 17:56:25 +010089 } else {
90 printf("%s Class %04x: %04x:%04x",
Denys Vlasenko69675782013-01-14 01:34:48 +010091 pci_slot_name, pci_class, pci_vid, pci_did);
Souf Oued982bc712009-12-05 17:56:25 +010092 }
93
94 if ((option_mask32 & OPT_k) && driver) {
95 if (option_mask32 & OPT_m) {
96 printf(" \"%s\"", driver);
97 } else {
98 printf(" %s", driver);
99 }
100 }
101 bb_putchar('\n');
102
103 free(driver);
104 free(pci_slot_name);
105
106 return TRUE;
107}
108
109int lspci_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
110int lspci_main(int argc UNUSED_PARAM, char **argv)
111{
112 getopt32(argv, "m" /*non-compat:*/ "k" /*ignored:*/ "nv");
113
114 recursive_action("/sys/bus/pci/devices",
115 ACTION_RECURSE,
116 fileAction,
117 NULL, /* dirAction */
Denys Vlasenko689d0652020-10-01 21:52:16 +0200118 NULL /* userData */
119 );
Souf Oued982bc712009-12-05 17:56:25 +0100120 return EXIT_SUCCESS;
121}