-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpci.c
More file actions
113 lines (84 loc) · 2.84 KB
/
pci.c
File metadata and controls
113 lines (84 loc) · 2.84 KB
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
#include "pci.h"
#include "pci_id.h"
#include "display.h"
void PCIDeviceLoadSpeed(const char *Path, TDevice *dev)
{
char *Tempstr=NULL, *Token=NULL;
const char *ptr;
double val;
Tempstr=ReadFile(Tempstr, Path, "current_link_speed");
ptr=GetToken(Tempstr, "\\S", &Token, 0);
val=atof(Token);
ptr=GetToken(ptr, "\\S", &Token, 0);
if (strcmp(Token, "GT/s")==0) val *= 1024 * 1024 * 1024;
if (strcmp(Token, "MT/s")==0) val *= 1024 * 1024;
if (strcmp(Token, "kT/s")==0) val *= 1024;
dev->Speed=(uint64_t) val;
Destroy(Tempstr);
Destroy(Token);
}
TDevice *PCIDeviceLoad(const char *Path, ListNode *PCIids)
{
TDevice *dev;
char *Tempstr=NULL, *Key=NULL;
glob_t Glob;
const char *ptr;
dev=(TDevice *) calloc(1, sizeof(TDevice));
dev->BusType=CopyStr(dev->BusType, "pci");
dev->Address=CopyStr(dev->Address, GetBasename(Path));
dev->idProduct=ReadFile(dev->idProduct, Path, "device");
dev->idVendor=ReadFile(dev->idVendor, Path, "vendor");
dev->idClass=ReadFile(dev->idClass, Path, "class");
dev->Serial=ReadFile(dev->Serial, Path, "serial");
dev->BusNum=ReadIntegerFile(Path, "busnum");
dev->DevNum=ReadIntegerFile(Path, "devnum");
dev->Serial=ReadFile(dev->Serial, Path, "serial");
PCIDeviceLoadSpeed(Path, dev);
Key=FormatStr(Key, "P%04x%04x", strtol(dev->idVendor, NULL, 16), strtol(dev->idProduct, NULL, 16));
ptr=GetVar(PCIids, Key);
if (! StrValid(ptr))
{
Tempstr=FormatStr(Tempstr, "P%04x%04x", strtol(dev->idVendor, NULL, 16), strtol(dev->idProduct, NULL, 16));
Key=CopyStrLen(Key, Tempstr, 7);
ptr=GetVar(PCIids, Key);
}
dev->Description=CopyStr(dev->Description, ptr);
Tempstr=FormatStr(Tempstr, "C%06x", strtol(dev->idClass, NULL, 16));
dev->Class=CopyStr(dev->Class, Tempstr);
ptr=GetVar(PCIids, Tempstr);
if (! StrValid(ptr))
{
Key=CopyStrLen(Key, Tempstr, 5);
ptr=GetVar(PCIids, Key);
if (! StrValid(ptr))
{
Key=CopyStrLen(Key, Tempstr, 3);
ptr=GetVar(PCIids, Key);
}
}
dev->Class=CopyStr(dev->Class, ptr);
LookupAnyDevNode(dev, Path);
Key=FormatStr(Key, "V%x", strtol(dev->idVendor, NULL, 16));
dev->Vendor=CopyStr(dev->Vendor, GetVar(PCIids, Key));
Destroy(Tempstr);
Destroy(Key);
return(dev);
}
void PCIShow(int Flags)
{
glob_t Glob;
char *Tempstr=NULL;
ListNode *PCIids;
TDevice *dev;
int i;
if (StrValid(PciIDsFile)) PCIids=PCILoadIDFile(PciIDsFile);
else PCIids=PCILoadIDs(PciIDsDirs, "pci.ids.gz:pci.ids.bz2:pci.ids");
glob("/sys/bus/pci/devices/*", 0, 0, &Glob);
for (i=0; i < Glob.gl_pathc; i++)
{
dev=PCIDeviceLoad(Glob.gl_pathv[i], PCIids);
DeviceDisplay(Flags, dev);
TDeviceDestroy(dev);
}
Destroy(Tempstr);
}