1 /* 2 * $Id: proc.c,v 1.10 2002/12/26 20:24:08 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 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 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 d->hdrtype = pci_read_byte(d, PCI_HEADER_TYPE); 164 known = PCI_FILL_IDENT; 165 if (!a->buscentric) 166 { 167 known |= PCI_FILL_IRQ | PCI_FILL_BASES; 168 if (cnt >= 10) 169 known |= PCI_FILL_ROM_BASE; 170 if (cnt >= 17) 171 known |= PCI_FILL_SIZES; 172 } 173 d->known_fields = known; 174 pci_link_dev(a, d); 175 } 176 fclose(f); 177 } 178 179 static int 180 proc_setup(struct pci_dev *d, int rw) 181 { 182 struct pci_access *a = d->access; 183 184 if (a->cached_dev != d || a->fd_rw < rw) 185 { 186 char buf[256]; 187 if (a->fd >= 0) 188 close(a->fd); 189 if (snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d", a->method_params[PCI_ACCESS_PROC_BUS_PCI], 190 d->bus, d->dev, d->func) == sizeof(buf)) 191 a->error("File name too long"); 192 a->fd_rw = a->writeable || rw; 193 a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY); 194 if (a->fd < 0) 195 a->warning("Cannot open %s", buf); 196 a->cached_dev = d; 197 a->fd_pos = 0; 198 } 199 return a->fd; 200 } 201 202 static int 203 proc_read(struct pci_dev *d, int pos, byte *buf, int len) 204 { 205 int fd = proc_setup(d, 0); 206 int res; 207 208 if (fd < 0) 209 return 0; 210 res = do_read(d, fd, buf, len, pos); 211 if (res < 0) 212 { 213 d->access->warning("proc_read: read failed: %s", strerror(errno)); 214 return 0; 215 } 216 else if (res != len) 217 { 218 d->access->warning("proc_read: tried to read %d bytes at %d, but got only %d", len, pos, res); 219 return 0; 220 } 221 return 1; 222 } 223 224 static int 225 proc_write(struct pci_dev *d, int pos, byte *buf, int len) 226 { 227 int fd = proc_setup(d, 1); 228 int res; 229 230 if (fd < 0) 231 return 0; 232 res = do_write(d, fd, buf, len, pos); 233 if (res < 0) 234 { 235 d->access->warning("proc_write: write failed: %s", strerror(errno)); 236 return 0; 237 } 238 else if (res != len) 239 { 240 d->access->warning("proc_write: tried to write %d bytes at %d, but got only %d", len, pos, res); 241 return 0; 242 } 243 return 1; 244 } 245 246 static void 247 proc_cleanup_dev(struct pci_dev *d) 248 { 249 if (d->access->cached_dev == d) 250 d->access->cached_dev = NULL; 251 } 252 253 struct pci_methods pm_linux_proc = { 254 "/proc/bus/pci", 255 proc_config, 256 proc_detect, 257 proc_init, 258 proc_cleanup, 259 proc_scan, 260 pci_generic_fill_info, 261 proc_read, 262 proc_write, 263 NULL, /* init_dev */ 264 proc_cleanup_dev 265 }; 266