]>
Commit | Line | Data |
---|---|---|
90d08965 JW |
1 | #include <sys/types.h> |
2 | #include <sys/stat.h> | |
3 | #include <fcntl.h> | |
4 | #include <stdio.h> | |
5 | #include <inttypes.h> | |
6 | ||
7 | static int _open(int bus, int slot, int fn) | |
8 | { | |
9 | char fname[512]; | |
10 | ||
11 | sprintf(fname, "/proc/bus/pci/%02x/%02x.%01x", bus, slot, fn); | |
12 | return open(fname, O_RDWR); | |
13 | } | |
14 | ||
15 | void pci_write32(int bus, int slot, int fn, int addr, uint32_t data) | |
16 | { | |
17 | int fd; | |
18 | ||
19 | fd = _open(bus, slot, fn); | |
20 | if (fd < 0) | |
21 | return; | |
22 | lseek(fd, addr, SEEK_SET); | |
23 | write(fd, &data, 4); | |
24 | close(fd); | |
25 | } | |
26 | ||
27 | void pci_write16(int bus, int slot, int fn, int addr, uint16_t data) | |
28 | { | |
29 | int fd; | |
30 | ||
31 | fd = _open(bus, slot, fn); | |
32 | if (fd < 0) | |
33 | return; | |
34 | lseek(fd, addr, SEEK_SET); | |
35 | write(fd, &data, 2); | |
36 | close(fd); | |
37 | } | |
38 | ||
39 | void pci_write8(int bus, int slot, int fn, int addr, uint8_t data) | |
40 | { | |
41 | int fd; | |
42 | ||
43 | fd = _open(bus, slot, fn); | |
44 | if (fd < 0) | |
45 | return; | |
46 | lseek(fd, addr, SEEK_SET); | |
47 | write(fd, &data, 1); | |
48 | close(fd); | |
49 | } | |
50 | ||
51 | uint32_t pci_read32(int bus, int slot, int fn, int addr) | |
52 | { | |
53 | int fd; | |
54 | uint32_t data; | |
55 | ||
56 | fd = _open(bus, slot, fn); | |
57 | if (fd < 0) | |
58 | return; | |
59 | lseek(fd, addr, SEEK_SET); | |
60 | read(fd, &data, 4); | |
61 | close(fd); | |
62 | ||
63 | return data; | |
64 | } | |
65 | ||
66 | uint16_t pci_read16(int bus, int slot, int fn, int addr) | |
67 | { | |
68 | int fd; | |
69 | uint16_t data; | |
70 | ||
71 | fd = _open(bus, slot, fn); | |
72 | if (fd < 0) | |
73 | { | |
74 | perror("open"); | |
75 | return; | |
76 | } | |
77 | lseek(fd, addr, SEEK_SET); | |
78 | read(fd, &data, 2); | |
79 | close(fd); | |
80 | ||
81 | return data; | |
82 | } | |
83 | ||
84 | ||
85 | uint8_t pci_read8(int bus, int slot, int fn, int addr) | |
86 | { | |
87 | int fd; | |
88 | uint8_t data; | |
89 | ||
90 | fd = _open(bus, slot, fn); | |
91 | if (fd < 0) | |
92 | { | |
93 | perror("open"); | |
94 | return; | |
95 | } | |
96 | lseek(fd, addr, SEEK_SET); | |
97 | read(fd, &data, 1); | |
98 | close(fd); | |
99 | ||
100 | return data; | |
101 | } |