1 /* 2 * $Id: proc.c,v 1.7 2000/01/20 21:14:44 mj Exp $ 3 * 4 * The PCI Library -- Configuration Access via /proc/bus/pci 5 * 6 * Copyright (c) 1997--1999 Martin Mares <[email protected]> 7 * 8 * Can be freely distributed and used under the terms of the GNU GPL. 9 */ 10 11 #define _GNU_SOURCE 12 13 #include <stdio.h> 14 #include <string.h> 15 #include <unistd.h> 16 #include <errno.h> 17 #include <fcntl.h> 18 #include <sys/types.h> 19 20 #include "internal.h" 21 22 /* 23 * We'd like to use pread/pwrite for configuration space accesses, but 24 * unfortunately it isn't simple at all since all libc's until glibc 2.1 25 * don't define it. 26 */ 27 28 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0 29 /* glibc 2.1 or newer -> pread/pwrite supported automatically */ 30 31 #elif defined(i386) && defined(__GLIBC__) 32 /* glibc 2.0 on i386 -> call syscalls directly */ 33 #include <asm/unistd.h> 34 #include <syscall-list.h> 35 #ifndef SYS_pread 36 #define SYS_pread 180 37 #endif 38 static int pread(unsigned int fd, void *buf, size_t size, loff_t where) 39 { return syscall(SYS_pread, fd, buf, size, where); } 40 #ifndef SYS_pwrite 41 #define SYS_pwrite 181 42 #endif 43 static int pwrite(unsigned int fd, void *buf, size_t size, loff_t where) 44 { return syscall(SYS_pwrite, fd, buf, size, where); } 45 46 #elif defined(i386) 47 /* old libc on i386 -> call syscalls directly the old way */ 48 #include <asm/unistd.h> 49 static _syscall5(int, pread, unsigned int, fd, void *, buf, size_t, size, u32, where_lo, u32, where_hi); 50 static _syscall5(int, pwrite, unsigned int, fd, void *, buf, size_t, size, u32, where_lo, u32, where_hi); 51 static int do_read(struct pci_dev *d __attribute__((unused)), int fd, void *buf, size_t size, int where) { return pread(fd, buf, size, where, 0); } 52 static int do_write(struct pci_dev *d __attribute__((unused)), int fd, void *buf, size_t size, int where) { return pwrite(fd, buf, size, where, 0); } 53 #define HAVE_DO_READ 54 55 #else 56 /* In all other cases we use lseek/read/write instead to be safe */ 57 #define make_rw_glue(op) \ 58 static int do_##op(struct pci_dev *d, int fd, void *buf, size_t size, int where) \ 59 { \ 60 struct pci_access *a = d->access; \ 61 int r; \ 62 if (a->fd_pos != where && lseek(fd, where, SEEK_SET) < 0) \ 63 return -1; \ 64 r = op(fd, buf, size); \ 65 if (r < 0) \ 66 a->fd_pos = -1; \ 67 else \ 68 a->fd_pos = where + r; \ 69 return r; \ 70 } 71 make_rw_glue(read) 72 make_rw_glue(write) 73 #define HAVE_DO_READ 74 #endif 75 76 #ifndef HAVE_DO_READ 77 #define do_read(d,f,b,l,p) pread(f,b,l,p) 78 #define do_write(d,f,b,l,p) pwrite(f,b,l,p) 79 #endif 80 81 static void 82 proc_config(struct pci_access *a) 83 { 84 a->method_params[PCI_ACCESS_PROC_BUS_PCI] = PATH_PROC_BUS_PCI; 85 } 86 87 static int 88 proc_detect(struct pci_access *a) 89 { 90 char *name = a->method_params[PCI_ACCESS_PROC_BUS_PCI]; 91 92 if (access(name, R_OK)) 93 { 94 a->warning("Cannot open %s", name); 95 return 0; 96 } 97 a->debug("...using %s", name); 98 return 1; 99 } 100 101 static void 102 proc_init(struct pci_access *a) 103 { 104 a->fd = -1; 105 } 106 107 static void 108 proc_cleanup(struct pci_access *a) 109 { 110 if (a->fd >= 0) 111 { 112 close(a->fd); 113 a->fd = -1; 114 } 115 } 116 117 static void 118 proc_scan(struct pci_access *a) 119 { 120 FILE *f; 121 char buf[512]; 122 123 if (snprintf(buf, sizeof(buf), "%s/devices", a->method_params[PCI_ACCESS_PROC_BUS_PCI]) == sizeof(buf)) 124 a->error("File name too long"); 125 f = fopen(buf, "r"); 126 if (!f) 127 a->error("Cannot open %s", buf); 128 while (fgets(buf, sizeof(buf)-1, f)) 129 { 130 struct pci_dev *d = pci_alloc_dev(a); 131 unsigned int dfn, vend, cnt, known; 132 133 cnt = sscanf(buf, 134 #ifdef HAVE_LONG_ADDRESS 135 "%x %x %x %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx", 136 #else 137 "%x %x %x %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx", 138 #endif 139 &dfn, 140 &vend, 141 &d->irq, 142 &d->base_addr[0], 143 &d->base_addr[1], 144 &d->base_addr[2], 145 &d->base_addr[3], 146 &d->base_addr[4], 147 &d->base_addr[5], 148 &d->rom_base_addr, 149 &d->size[0], 150 &d->size[1], 151 &d->size[2], 152 &d->size[3], 153 &d->size[4], 154 &d->size[5], 155 &d->rom_size); 156 if (cnt != 9 && cnt != 10 && cnt != 17) 157 a->error("proc: parse error (read only %d items)", cnt); 158 d->bus = dfn >> 8U; 159 d->dev = PCI_SLOT(dfn & 0xff); 160 d->func = PCI_FUNC(dfn & 0xff); 161 d->vendor_id = vend >> 16U; 162 d->device_id = vend & 0xffff; 163 known = PCI_FILL_IDENT; 164 if (!a->buscentric) 165 { 166 known |= PCI_FILL_IRQ | PCI_FILL_BASES; 167 if (cnt >= 10) 168 known |= PCI_FILL_ROM_BASE; 169 if (cnt >= 17) 170 known |= PCI_FILL_SIZES; 171 } 172 d->known_fields = known; 173 pci_link_dev(a, d); 174 } 175 fclose(f); 176 } 177 178 static int 179 proc_setup(struct pci_dev *d, int rw) 180 { 181 struct pci_access *a = d->access; 182 183 if (a->cached_dev != d || a->fd_rw < rw) 184 { 185 char buf[256]; 186 if (a->fd >= 0) 187 close(a->fd); 188 if (snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d", a->method_params[PCI_ACCESS_PROC_BUS_PCI], 189 d->bus, d->dev, d->func) == sizeof(buf)) 190 a->error("File name too long"); 191 a->fd_rw = a->writeable || rw; 192 a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY); 193 if (a->fd < 0) 194 a->warning("Cannot open %s", buf); 195 a->cached_dev = d; 196 a->fd_pos = 0; 197 } 198 return a->fd; 199 } 200 201 static int 202 proc_read(struct pci_dev *d, int pos, byte *buf, int len) 203 { 204 int fd = proc_setup(d, 0); 205 int res; 206 207 if (fd < 0) 208 return 0; 209 res = do_read(d, fd, buf, len, pos); 210 if (res < 0) 211 { 212 d->access->warning("proc_read: read failed: %s", strerror(errno)); 213 return 0; 214 } 215 else if (res != len) 216 { 217 d->access->warning("proc_read: tried to read %d bytes at %d, but got only %d", len, pos, res); 218 return 0; 219 } 220 return 1; 221 } 222 223 static int 224 proc_write(struct pci_dev *d, int pos, byte *buf, int len) 225 { 226 int fd = proc_setup(d, 1); 227 int res; 228 229 if (fd < 0) 230 return 0; 231 res = do_write(d, fd, buf, len, pos); 232 if (res < 0) 233 { 234 d->access->warning("proc_write: write failed: %s", strerror(errno)); 235 return 0; 236 } 237 else if (res != len) 238 { 239 d->access->warning("proc_write: tried to write %d bytes at %d, but got only %d", len, pos, res); 240 return 0; 241 } 242 return 1; 243 } 244 245 static void 246 proc_cleanup_dev(struct pci_dev *d) 247 { 248 if (d->access->cached_dev == d) 249 d->access->cached_dev = NULL; 250 } 251 252 struct pci_methods pm_linux_proc = { 253 "/proc/bus/pci", 254 proc_config, 255 proc_detect, 256 proc_init, 257 proc_cleanup, 258 proc_scan, 259 pci_generic_fill_info, 260 proc_read, 261 proc_write, 262 NULL, /* init_dev */ 263 proc_cleanup_dev 264 }; 265