1 |
#define _GNU_SOURCE |
2 |
#include <stdio.h> |
3 |
#include <stdarg.h> |
4 |
#include <stdlib.h> |
5 |
#include <string.h> |
6 |
#include <pci/pci.h> |
7 |
#include <sys/types.h> |
8 |
#include <sys/stat.h> |
9 |
#include <fcntl.h> |
10 |
#include <unistd.h> |
11 |
#include <dirent.h> |
12 |
#include <pci/header.h> |
13 |
#include "common.h" |
14 |
|
15 |
/* /proc files're 256 bytes but we only need first 64 bytes*/ |
16 |
#define CONFIG_SPACE_SIZE 64 |
17 |
|
18 |
static char *proc_pci_path_default = "/proc/bus/pci/devices"; |
19 |
char* proc_pci_path = NULL; |
20 |
|
21 |
static void __attribute__((noreturn)) error_and_die(char *msg, ...) |
22 |
{ |
23 |
va_list args; |
24 |
|
25 |
va_start(args, msg); |
26 |
fprintf(stderr, "%s: ", "lspcidrake"); |
27 |
vfprintf(stderr, msg, args); |
28 |
fputc('\n', stderr); |
29 |
exit(1); |
30 |
} |
31 |
|
32 |
extern struct pciusb_entries pci_probe(void) { |
33 |
u8 buf[CONFIG_SPACE_SIZE]; |
34 |
struct pciusb_entries r; |
35 |
|
36 |
static struct pci_access *pacc; |
37 |
struct pci_dev *dev; |
38 |
char classbuf[128], vendorbuf[128], devbuf[128]; |
39 |
|
40 |
pacc = pci_alloc(); |
41 |
|
42 |
if (proc_pci_path) { |
43 |
pci_set_param(pacc, "proc.path", proc_pci_path); |
44 |
} |
45 |
|
46 |
|
47 |
pci_init(pacc); |
48 |
pacc->error = error_and_die; |
49 |
pci_scan_bus(pacc); |
50 |
|
51 |
r.nb = 0; |
52 |
r.entries = malloc(sizeof(struct pciusb_entry) * MAX_DEVICES); |
53 |
|
54 |
for (dev = pacc->devices; dev && r.nb < MAX_DEVICES; dev = dev->next, r.nb++) { |
55 |
|
56 |
struct pciusb_entry *e = &r.entries[r.nb]; |
57 |
memset(buf, 0, CONFIG_SPACE_SIZE); // make sure not to retrieve values from previous devices |
58 |
pci_setup_cache(dev, (u8*)buf, CONFIG_SPACE_SIZE); |
59 |
pci_read_block(dev, 0, buf, CONFIG_SPACE_SIZE); |
60 |
pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_CAPS); |
61 |
|
62 |
pciusb_initialize(e); |
63 |
|
64 |
asprintf(&e->text, "%s|%s", |
65 |
pci_lookup_name(pacc, vendorbuf, sizeof(vendorbuf), PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id), |
66 |
pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id) |
67 |
); |
68 |
pci_lookup_name(pacc, classbuf, sizeof(classbuf), PCI_LOOKUP_CLASS, dev->device_class), |
69 |
e->class=strdup(classbuf); |
70 |
|
71 |
e->vendor = dev->vendor_id; |
72 |
e->device = dev->device_id; |
73 |
e->pci_domain = dev->domain; |
74 |
e->pci_bus = dev->bus; |
75 |
e->pci_device = dev->dev; |
76 |
e->pci_function = dev->func; |
77 |
|
78 |
e->class_id = dev->device_class; |
79 |
e->subvendor = pci_read_word(dev, PCI_SUBSYSTEM_VENDOR_ID); |
80 |
e->subdevice = pci_read_word(dev, PCI_SUBSYSTEM_ID); |
81 |
e->pci_revision = pci_read_byte(dev, PCI_REVISION_ID); |
82 |
|
83 |
if ((e->subvendor == 0 && e->subdevice == 0) || |
84 |
(e->subvendor == e->vendor && e->subdevice == e->device)) { |
85 |
e->subvendor = 0xffff; |
86 |
e->subdevice = 0xffff; |
87 |
} |
88 |
|
89 |
if (pci_find_cap(dev,PCI_CAP_ID_EXP, PCI_CAP_NORMAL)) |
90 |
e->is_pciexpress = 1; |
91 |
|
92 |
/* special case for realtek 8139 that has two drivers */ |
93 |
if (e->vendor == 0x10ec && e->device == 0x8139) { |
94 |
if (e->pci_revision < 0x20) |
95 |
e->module = strdup("8139too"); |
96 |
else |
97 |
e->module = strdup("8139cp"); |
98 |
} |
99 |
|
100 |
} |
101 |
|
102 |
/* shrink to real size */ |
103 |
r.entries = realloc(r.entries, sizeof(struct pciusb_entry) * r.nb); |
104 |
|
105 |
pci_cleanup(pacc); |
106 |
|
107 |
if (pciusb_find_modules(&r, "pcitable", DO_NOT_LOAD, 1)) |
108 |
return r; |
109 |
|
110 |
/* should not happen */ |
111 |
exit(1); |
112 |
} |