| 1 |
/* |
| 2 |
This file is based on: |
| 3 |
$XFree86: xc/programs/Xserver/hw/xfree86/etc/scanpci.c,v 3.34.2.17 1998/11/10 11:55:40 dawes Exp $ |
| 4 |
Modified for readability by Nick Kurshev |
| 5 |
*/ |
| 6 |
|
| 7 |
static int pci_config_type( void ) { return 1; } |
| 8 |
#ifdef linux |
| 9 |
#include <fcntl.h> |
| 10 |
#include <sys/io.h> |
| 11 |
#include <linux/pci.h> |
| 12 |
#include "../../bswap.h" |
| 13 |
#endif |
| 14 |
|
| 15 |
#ifdef linux |
| 16 |
static int pci_get_vendor( |
| 17 |
unsigned char bus, |
| 18 |
unsigned char dev, |
| 19 |
int func) |
| 20 |
{ |
| 21 |
int retval; |
| 22 |
char path[100]; |
| 23 |
int fd; |
| 24 |
short vendor, device; |
| 25 |
sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev); |
| 26 |
fd = open(path,O_RDONLY|O_SYNC); |
| 27 |
if (fd == -1) { |
| 28 |
retval=0xFFFF; |
| 29 |
} |
| 30 |
else if (pread(fd, &vendor, 2, PCI_VENDOR_ID) == 2 && |
| 31 |
pread(fd, &device, 2, PCI_DEVICE_ID) == 2) { |
| 32 |
vendor = bswap_16(vendor); |
| 33 |
device = bswap_16(device); |
| 34 |
retval = vendor + (device<<16); /*no worries about byte order, |
| 35 |
all ppc are bigendian*/ |
| 36 |
} else { |
| 37 |
retval = 0xFFFF; |
| 38 |
} |
| 39 |
if (fd > 0) { |
| 40 |
close(fd); |
| 41 |
} |
| 42 |
return retval; |
| 43 |
} |
| 44 |
#else |
| 45 |
static int pci_get_vendor( |
| 46 |
unsigned char bus, |
| 47 |
unsigned char dev, |
| 48 |
int func) |
| 49 |
{ |
| 50 |
int retval; |
| 51 |
pciconfig_read(bus, dev<<3, PCI_ID_REG, 4, &retval); |
| 52 |
return retval; |
| 53 |
} |
| 54 |
#endif |
| 55 |
#ifdef linux |
| 56 |
static long pci_config_read_long( |
| 57 |
unsigned char bus, |
| 58 |
unsigned char dev, |
| 59 |
int func, |
| 60 |
unsigned cmd) |
| 61 |
{ |
| 62 |
long retval; |
| 63 |
char path[100]; |
| 64 |
int fd; |
| 65 |
sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev); |
| 66 |
fd = open(path,O_RDONLY|O_SYNC); |
| 67 |
if (fd == -1) { |
| 68 |
retval=0; |
| 69 |
} |
| 70 |
else if (pread(fd, &retval, 4, cmd) == 4) { |
| 71 |
retval = bswap_32(retval); |
| 72 |
} else { |
| 73 |
retval = 0; |
| 74 |
} |
| 75 |
if (fd > 0) { |
| 76 |
close(fd); |
| 77 |
} |
| 78 |
return retval; |
| 79 |
} |
| 80 |
|
| 81 |
static long pci_config_read_word( |
| 82 |
unsigned char bus, |
| 83 |
unsigned char dev, |
| 84 |
int func, |
| 85 |
unsigned cmd) |
| 86 |
{ |
| 87 |
long retval; |
| 88 |
char path[100]; |
| 89 |
int fd; |
| 90 |
sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev); |
| 91 |
fd = open(path,O_RDONLY|O_SYNC); |
| 92 |
if (fd == -1) { |
| 93 |
retval=0; |
| 94 |
} |
| 95 |
else if (pread(fd, &retval, 2, cmd) == 2) { |
| 96 |
retval = bswap_16(retval); |
| 97 |
} else { |
| 98 |
retval = 0; |
| 99 |
} |
| 100 |
if (fd > 0) { |
| 101 |
close(fd); |
| 102 |
} |
| 103 |
return retval; |
| 104 |
} |
| 105 |
|
| 106 |
static long pci_config_read_byte( |
| 107 |
unsigned char bus, |
| 108 |
unsigned char dev, |
| 109 |
int func, |
| 110 |
unsigned cmd) |
| 111 |
{ |
| 112 |
long retval; |
| 113 |
char path[100]; |
| 114 |
int fd; |
| 115 |
sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev); |
| 116 |
fd = open(path,O_RDONLY|O_SYNC); |
| 117 |
if (fd == -1) { |
| 118 |
retval=0; |
| 119 |
} |
| 120 |
else if (pread(fd, &retval, 1, cmd) != 1) { |
| 121 |
retval = 0; |
| 122 |
} |
| 123 |
if (fd > 0) { |
| 124 |
close(fd); |
| 125 |
} |
| 126 |
return retval; |
| 127 |
} |
| 128 |
|
| 129 |
static void pci_config_write_long( |
| 130 |
unsigned char bus, |
| 131 |
unsigned char dev, |
| 132 |
int func, |
| 133 |
unsigned cmd, |
| 134 |
long val) |
| 135 |
{ |
| 136 |
char path[100]; |
| 137 |
int fd; |
| 138 |
val = bswap_32(val); |
| 139 |
sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev); |
| 140 |
fd = open(path,O_RDONLY|O_SYNC); |
| 141 |
if (fd > 0) { |
| 142 |
pwrite(fd, &val, 4, cmd); |
| 143 |
close(fd); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
static void pci_config_write_word( |
| 148 |
unsigned char bus, |
| 149 |
unsigned char dev, |
| 150 |
int func, |
| 151 |
unsigned cmd, |
| 152 |
long val) |
| 153 |
{ |
| 154 |
char path[100]; |
| 155 |
int fd; |
| 156 |
val = bswap_16(val); |
| 157 |
sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev); |
| 158 |
fd = open(path,O_RDONLY|O_SYNC); |
| 159 |
if (fd > 0) { |
| 160 |
pwrite(fd, &val, 2, cmd); |
| 161 |
close(fd); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
static void pci_config_write_byte( |
| 166 |
unsigned char bus, |
| 167 |
unsigned char dev, |
| 168 |
int func, |
| 169 |
unsigned cmd, |
| 170 |
long val) |
| 171 |
{ |
| 172 |
char path[100]; |
| 173 |
int fd; |
| 174 |
sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev); |
| 175 |
fd = open(path,O_RDONLY|O_SYNC); |
| 176 |
if (fd > 0) { |
| 177 |
pwrite(fd, &val, 1, cmd); |
| 178 |
close(fd); |
| 179 |
} |
| 180 |
} |
| 181 |
#else |
| 182 |
static long pci_config_read_long( |
| 183 |
unsigned char bus, |
| 184 |
unsigned char dev, |
| 185 |
int func, |
| 186 |
unsigned cmd) |
| 187 |
{ |
| 188 |
long retval; |
| 189 |
pciconfig_read(bus, dev<<3, cmd, 4, &retval); |
| 190 |
return retval; |
| 191 |
} |
| 192 |
|
| 193 |
static long pci_config_read_word( |
| 194 |
unsigned char bus, |
| 195 |
unsigned char dev, |
| 196 |
int func, |
| 197 |
unsigned cmd) |
| 198 |
{ |
| 199 |
long retval; |
| 200 |
pciconfig_read(bus, dev<<3, cmd, 2, &retval); |
| 201 |
return retval; |
| 202 |
} |
| 203 |
|
| 204 |
static long pci_config_read_byte( |
| 205 |
unsigned char bus, |
| 206 |
unsigned char dev, |
| 207 |
int func, |
| 208 |
unsigned cmd) |
| 209 |
{ |
| 210 |
long retval; |
| 211 |
pciconfig_read(bus, dev<<3, cmd, 1, &retval); |
| 212 |
return retval; |
| 213 |
} |
| 214 |
|
| 215 |
static void pci_config_write_long( |
| 216 |
unsigned char bus, |
| 217 |
unsigned char dev, |
| 218 |
int func, |
| 219 |
unsigned cmd, |
| 220 |
long val) |
| 221 |
{ |
| 222 |
long retval; |
| 223 |
pciconfig_write(bus, dev<<3, cmd, 4, val); |
| 224 |
return retval; |
| 225 |
} |
| 226 |
|
| 227 |
static void pci_config_write_word( |
| 228 |
unsigned char bus, |
| 229 |
unsigned char dev, |
| 230 |
int func, |
| 231 |
unsigned cmd, |
| 232 |
long val) |
| 233 |
{ |
| 234 |
long retval; |
| 235 |
pciconfig_write(bus, dev<<3, cmd, 2, val); |
| 236 |
return retval; |
| 237 |
} |
| 238 |
|
| 239 |
static void pci_config_write_byte( |
| 240 |
unsigned char bus, |
| 241 |
unsigned char dev, |
| 242 |
int func, |
| 243 |
unsigned cmd, |
| 244 |
long val) |
| 245 |
{ |
| 246 |
long retval; |
| 247 |
pciconfig_write(bus, dev<<3, cmd, 1, val); |
| 248 |
return retval; |
| 249 |
} |
| 250 |
#endif |