summaryrefslogtreecommitdiffstats
path: root/lspcidrake.c
blob: a24bcf3597e47e4ba42dcfe656eca5a08a90e20d (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
#include <stdio.h>
#include <string.h>
#include <unistd.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(")");
		}
		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);
}

int main(int argc, char **argv) {
	char ** ptr = argv;

	while (ptr && *ptr) {
		if (!strcmp(*ptr, "-h") || !strcmp(*ptr, "--help")) {
			printf("usage: lspcidrake [-v|-u]\n"
				"-p <file>: pci devices source [/proc/bus/pci/devices by default]\n"
				"-u <file>: usb devices source [/proc/bus/usb/devices by default]\n"
				"-v : verbose mode [print ids and sub-ids], implies full probe\n"
				"--dmidecode <file>: to use this dmidecode output instead of calling dmidecode\n"
				);	
			return 0;	
		}
		if (!strcmp(*ptr, "-v"))
			verboze = 1;
		else if (!strcmp(*ptr, "-u"))
		  proc_usb_path = *++ptr;
		else if (!strcmp(*ptr, "-p"))
		  proc_pci_path = *++ptr;
		else if (!strcmp(*ptr, "--dmidecode"))
		  dmidecode_file = *++ptr;
		ptr++;
	}

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

	return 0;
}