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