summaryrefslogtreecommitdiffstats
path: root/lspcidrake.c
blob: 9637671f8d45c9e8c79b7e261d83f7bb55b982c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include "libldetect.h"

static int verboze = 0;

static void printit(struct pciusb_entries entries, void print_class(unsigned long )) {
	unsigned int i;
	for (i = 0; i < entries.nb; i++) {
		struct pciusb_entry *e = &entries.entries[i];
		printf("%-16s: ", e->module ? e->module : "unknown");
		if (e->text)
			printf(e->text);
		else	printf("unknown (%04x/%04x/%04x/%04x)", e->vendor, e->device, e->subvendor, e->subdevice);
		if (e->class_id) {
			print_class(e->class_id);
		}
		if (verboze && e->text) {
			printf(" (vendor:%04x device:%04x", e->vendor, e->device);
			if (e->subvendor != 0xffff || e->subdevice != 0xffff)
				printf(" subv:%04x subd:%04x", e->subvendor, e->subdevice);
			printf(")");
		}
		if (e->pci_revision)    
                     printf(" (rev: %02x)", e->pci_revision);
		printf("\n");
	}
	pciusb_free(&entries);
}

static void print_pci_class(unsigned long class_id) {
  const char *s = pci_class2text(class_id);
  if (strcmp(s, "NOT_DEFINED") != 0) 
     printf(" [%s]", s);
}

static void print_usb_class(unsigned long class_id) {
  struct usb_class_text s = usb_class2text(class_id);
  if (s.usb_class_text) {
    printf(" [");
    printf("%s", s.usb_class_text);
    if (s.usb_sub_text) printf("|%s", s.usb_sub_text);
    if (s.usb_prot_text) printf("|%s", s.usb_prot_text);
    printf("]");
  }
}

static void print_dmi_entries(struct dmi_entries entries) {
	unsigned int i;
	for (i = 0; i < entries.nb; i++)
		printf("%-16s: %s\n", entries.entries[i].module, entries.entries[i].constraints);
}

static void print_hid_entries(struct hid_entries entries) {
	unsigned int i;
	for (i = 0; i < entries.nb; i++)
		printf("%-16s: %s\n", entries.entries[i].module,
		       entries.entries[i].text);	 
}

static void usage(void)
{
	printf(
	"usage: lspcidrake [options]\n"
	"\t-p, --pci-file <file>\tPCI devices source [/proc/bus/pci/devices by default]\n"
	"\t-u, --usb-file <file>\tUSB devices source  [/proc/bus/usb/devices by default]\n"
	"\t-v, --verbose\t\tVerbose mode [print ids and sub-ids], implies full probe\n"
	"\t-d, --dmidecode <file>\tTo use this dmidecode output instead of calling demicode\n");
}

int main(int argc, char **argv) {
	int opt, fake = 0;
	struct option options[] = { { "verbose", 0, NULL, 'v' },
				    { "pci-file", 1, NULL, 'p' },
				    { "usb-file", 1, NULL, 'u' },
				    { "dmidecode", 0, NULL, 'd' },
				    { NULL, 0, NULL, 0 } };

	while ((opt = getopt_long(argc, argv, "vp:u:d", options, NULL)) != -1) {
		switch (opt) {
			case 'v':
				verboze = 1;
				break;
			case 'p':
				proc_pci_path = optarg;
				fake = 1;
				break;
			case 'u':
				proc_usb_path = optarg;
				fake = 1;
				break;
			case 'd':
				dmidecode_file = optarg;
				fake = 1;
				break;
			default:
				usage();
				return 1;
		}
	}

	if (!fake || proc_pci_path) printit(pci_probe(), print_pci_class);
	if (!fake || proc_usb_path) printit(usb_probe(), print_usb_class);
	
	if (!fake && geteuid() == 0 || dmidecode_file) {
	    struct dmi_entries dmi_entries = dmi_probe();
	    print_dmi_entries(dmi_entries);
	    dmi_entries_free(dmi_entries);
	}

	if (!fake || sysfs_hid_path) {
	    struct hid_entries hid_entries = hid_probe();
	    print_hid_entries(hid_entries);
	    hid_entries_free(&hid_entries);
	}

	return 0;
}