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 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 9 | |
| 10 | //usage:#define lspci_trivial_usage |
| 11 | //usage: "[-mk]" |
| 12 | //usage:#define lspci_full_usage "\n\n" |
| 13 | //usage: "List all PCI devices" |
| 14 | //usage: "\n" |
| 15 | //usage: "\n -m Parseable output" |
| 16 | //usage: "\n -k Show driver" |
| 17 | |
Denys Vlasenko | ccd1efc | 2010-04-03 15:39:47 +0200 | [diff] [blame] | 18 | #include "libbb.h" |
Souf Oued | 982bc71 | 2009-12-05 17:56:25 +0100 | [diff] [blame] | 19 | |
| 20 | enum { |
| 21 | OPT_m = (1 << 0), |
| 22 | OPT_k = (1 << 1), |
| 23 | }; |
| 24 | |
| 25 | /* |
| 26 | * PCI_SLOT_NAME PCI_CLASS: PCI_VID:PCI_DID [PCI_SUBSYS_VID:PCI_SUBSYS_DID] [DRIVER] |
| 27 | */ |
| 28 | static int FAST_FUNC fileAction( |
| 29 | const char *fileName, |
| 30 | struct stat *statbuf UNUSED_PARAM, |
| 31 | void *userData UNUSED_PARAM, |
| 32 | int depth UNUSED_PARAM) |
| 33 | { |
| 34 | parser_t *parser; |
| 35 | char *tokens[3]; |
| 36 | char *pci_slot_name = NULL, *driver = NULL; |
| 37 | int pci_class = 0, pci_vid = 0, pci_did = 0; |
| 38 | int pci_subsys_vid = 0, pci_subsys_did = 0; |
| 39 | |
| 40 | char *uevent_filename = concat_path_file(fileName, "/uevent"); |
| 41 | parser = config_open2(uevent_filename, fopen_for_read); |
| 42 | free(uevent_filename); |
| 43 | |
| 44 | while (config_read(parser, tokens, 3, 2, "\0:=", PARSE_NORMAL)) { |
| 45 | if (strcmp(tokens[0], "DRIVER") == 0) { |
| 46 | driver = xstrdup(tokens[1]); |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | if (strcmp(tokens[0], "PCI_CLASS") == 0) { |
| 51 | pci_class = xstrtou(tokens[1], 16)>>8; |
| 52 | continue; |
| 53 | } |
| 54 | |
| 55 | if (strcmp(tokens[0], "PCI_ID") == 0) { |
| 56 | pci_vid = xstrtou(tokens[1], 16); |
| 57 | pci_did = xstrtou(tokens[2], 16); |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | if (strcmp(tokens[0], "PCI_SUBSYS_ID") == 0) { |
| 62 | pci_subsys_vid = xstrtou(tokens[1], 16); |
| 63 | pci_subsys_did = xstrtou(tokens[2], 16); |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | if (strcmp(tokens[0], "PCI_SLOT_NAME") == 0) { |
| 68 | pci_slot_name = xstrdup(tokens[2]); |
| 69 | continue; |
| 70 | } |
| 71 | } |
| 72 | config_close(parser); |
| 73 | |
| 74 | |
| 75 | if (option_mask32 & OPT_m) { |
| 76 | printf("%s \"Class %04x\" \"%04x\" \"%04x\" \"%04x\" \"%04x\"", |
| 77 | pci_slot_name, pci_class, pci_vid, pci_did, |
| 78 | pci_subsys_vid, pci_subsys_did); |
| 79 | } else { |
| 80 | printf("%s Class %04x: %04x:%04x", |
| 81 | pci_slot_name, pci_class, pci_vid, pci_did); |
| 82 | } |
| 83 | |
| 84 | if ((option_mask32 & OPT_k) && driver) { |
| 85 | if (option_mask32 & OPT_m) { |
| 86 | printf(" \"%s\"", driver); |
| 87 | } else { |
| 88 | printf(" %s", driver); |
| 89 | } |
| 90 | } |
| 91 | bb_putchar('\n'); |
| 92 | |
| 93 | free(driver); |
| 94 | free(pci_slot_name); |
| 95 | |
| 96 | return TRUE; |
| 97 | } |
| 98 | |
| 99 | int lspci_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 100 | int lspci_main(int argc UNUSED_PARAM, char **argv) |
| 101 | { |
| 102 | getopt32(argv, "m" /*non-compat:*/ "k" /*ignored:*/ "nv"); |
| 103 | |
| 104 | recursive_action("/sys/bus/pci/devices", |
| 105 | ACTION_RECURSE, |
| 106 | fileAction, |
| 107 | NULL, /* dirAction */ |
| 108 | NULL, /* userData */ |
| 109 | 0 /* depth */); |
| 110 | |
| 111 | return EXIT_SUCCESS; |
| 112 | } |