]>
Commit | Line | Data |
---|---|---|
98e930a2 JP |
1 | /* pci-linux.c |
2 | * Linux user-mode based PCI implementation | |
3 | * NetWatch system management mode administration console | |
4 | * | |
5 | * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved. | |
6 | * This program is free software; you can redistribute and/or modify it under | |
7 | * the terms found in the file LICENSE in the root of this source tree. | |
8 | * | |
9 | */ | |
10 | ||
90d08965 JW |
11 | #include <sys/types.h> |
12 | #include <sys/stat.h> | |
13 | #include <fcntl.h> | |
14 | #include <stdio.h> | |
15 | #include <inttypes.h> | |
16 | ||
81148fa1 | 17 | #include "../include/pci.h" |
7e16b8e6 | 18 | |
90d08965 JW |
19 | static int _open(int bus, int slot, int fn) |
20 | { | |
21 | char fname[512]; | |
22 | ||
23 | sprintf(fname, "/proc/bus/pci/%02x/%02x.%01x", bus, slot, fn); | |
24 | return open(fname, O_RDWR); | |
25 | } | |
26 | ||
27 | void pci_write32(int bus, int slot, int fn, int addr, uint32_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, 4); | |
36 | close(fd); | |
37 | } | |
38 | ||
39 | void pci_write16(int bus, int slot, int fn, int addr, uint16_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, 2); | |
48 | close(fd); | |
49 | } | |
50 | ||
51 | void pci_write8(int bus, int slot, int fn, int addr, uint8_t data) | |
52 | { | |
53 | int fd; | |
54 | ||
55 | fd = _open(bus, slot, fn); | |
56 | if (fd < 0) | |
57 | return; | |
58 | lseek(fd, addr, SEEK_SET); | |
59 | write(fd, &data, 1); | |
60 | close(fd); | |
61 | } | |
62 | ||
63 | uint32_t pci_read32(int bus, int slot, int fn, int addr) | |
64 | { | |
65 | int fd; | |
66 | uint32_t data; | |
67 | ||
68 | fd = _open(bus, slot, fn); | |
69 | if (fd < 0) | |
70 | return; | |
71 | lseek(fd, addr, SEEK_SET); | |
72 | read(fd, &data, 4); | |
73 | close(fd); | |
74 | ||
75 | return data; | |
76 | } | |
77 | ||
78 | uint16_t pci_read16(int bus, int slot, int fn, int addr) | |
79 | { | |
80 | int fd; | |
81 | uint16_t data; | |
82 | ||
83 | fd = _open(bus, slot, fn); | |
84 | if (fd < 0) | |
85 | { | |
86 | perror("open"); | |
87 | return; | |
88 | } | |
89 | lseek(fd, addr, SEEK_SET); | |
90 | read(fd, &data, 2); | |
91 | close(fd); | |
92 | ||
93 | return data; | |
94 | } | |
95 | ||
96 | ||
97 | uint8_t pci_read8(int bus, int slot, int fn, int addr) | |
98 | { | |
99 | int fd; | |
100 | uint8_t data; | |
101 | ||
102 | fd = _open(bus, slot, fn); | |
103 | if (fd < 0) | |
104 | { | |
105 | perror("open"); | |
106 | return; | |
107 | } | |
108 | lseek(fd, addr, SEEK_SET); | |
109 | read(fd, &data, 1); | |
110 | close(fd); | |
111 | ||
112 | return data; | |
113 | } |