busybox/util-linux/lspci.c
<<
>>
Prefs
   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 (6.3 kb)"
  11//config:       default y
  12//config:       help
  13//config:       lspci is a utility for displaying information about PCI buses in the
  14//config:       system and devices connected to them.
  15//config:
  16//config:       This version uses sysfs (/sys/bus/pci/devices) only.
  17
  18//applet:IF_LSPCI(APPLET_NOEXEC(lspci, lspci, BB_DIR_USR_BIN, BB_SUID_DROP, lspci))
  19
  20//kbuild:lib-$(CONFIG_LSPCI) += lspci.o
  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"
  27//usage:     "\n        -m      Parsable output"
  28//usage:     "\n        -k      Show driver"
  29
  30#include "libbb.h"
  31
  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 */
  40static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
  41                const char *fileName,
  42                struct stat *statbuf UNUSED_PARAM)
  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\"",
  87                        pci_slot_name, pci_class, pci_vid, pci_did,
  88                        pci_subsys_vid, pci_subsys_did);
  89        } else {
  90                printf("%s Class %04x: %04x:%04x",
  91                        pci_slot_name, pci_class, pci_vid, pci_did);
  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
 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 */
 118                        NULL /* userData */
 119        );
 120        return EXIT_SUCCESS;
 121}
 122