Souf Oued | 982bc71 | 2009-12-05 17:56:25 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 3 | * lspci implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2009 Malek Degachi <malek-degachi@laposte.net> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 8 | */ |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 9 | //config:config LSPCI |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "lspci (6.3 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 11 | //config: default y |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: lspci is a utility for displaying information about PCI buses in the |
| 14 | //config: system and devices connected to them. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 15 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: This version uses sysfs (/sys/bus/pci/devices) only. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 17 | |
Denys Vlasenko | 3239ab8 | 2017-08-05 23:28:19 +0200 | [diff] [blame] | 18 | //applet:IF_LSPCI(APPLET_NOEXEC(lspci, lspci, BB_DIR_USR_BIN, BB_SUID_DROP, lspci)) |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 19 | |
| 20 | //kbuild:lib-$(CONFIG_LSPCI) += lspci.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 21 | |
| 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 Fandrich | b5de0c1 | 2011-07-08 05:47:49 +0200 | [diff] [blame] | 27 | //usage: "\n -m Parsable output" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | //usage: "\n -k Show driver" |
| 29 | |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 30 | #include "libbb.h" |
Souf Oued | 982bc71 | 2009-12-05 17:56:25 +0100 | [diff] [blame] | 31 | |
| 32 | enum { |
| 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 Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 40 | static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM, |
Souf Oued | 982bc71 | 2009-12-05 17:56:25 +0100 | [diff] [blame] | 41 | const char *fileName, |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 42 | struct stat *statbuf UNUSED_PARAM) |
Souf Oued | 982bc71 | 2009-12-05 17:56:25 +0100 | [diff] [blame] | 43 | { |
| 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 Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 87 | pci_slot_name, pci_class, pci_vid, pci_did, |
| 88 | pci_subsys_vid, pci_subsys_did); |
Souf Oued | 982bc71 | 2009-12-05 17:56:25 +0100 | [diff] [blame] | 89 | } else { |
| 90 | printf("%s Class %04x: %04x:%04x", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 91 | pci_slot_name, pci_class, pci_vid, pci_did); |
Souf Oued | 982bc71 | 2009-12-05 17:56:25 +0100 | [diff] [blame] | 92 | } |
| 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 | |
| 109 | int lspci_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 110 | int 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 Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 118 | NULL /* userData */ |
| 119 | ); |
Souf Oued | 982bc71 | 2009-12-05 17:56:25 +0100 | [diff] [blame] | 120 | return EXIT_SUCCESS; |
| 121 | } |