2 * $Id: proc.c,v 1.8 2000/04/21 11:58:00 mj Exp $
4 * The PCI Library -- Configuration Access via /proc/bus/pci
6 * Copyright (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * Can be freely distributed and used under the terms of the GNU GPL.
18 #include <sys/types.h>
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
28 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
29 /* glibc 2.1 or newer -> pread/pwrite supported automatically */
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>
38 static int pread(unsigned int fd, void *buf, size_t size, loff_t where)
39 { return syscall(SYS_pread, fd, buf, size, where); }
41 #define SYS_pwrite 181
43 static int pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
44 { return syscall(SYS_pwrite, fd, buf, size, where); }
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); }
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) \
60 struct pci_access *a = d->access; \
62 if (a->fd_pos != where && lseek(fd, where, SEEK_SET) < 0) \
64 r = op(fd, buf, size); \
68 a->fd_pos = where + r; \
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)
82 proc_config(struct pci_access *a)
84 a->method_params[PCI_ACCESS_PROC_BUS_PCI] = PATH_PROC_BUS_PCI;
88 proc_detect(struct pci_access *a)
90 char *name = a->method_params[PCI_ACCESS_PROC_BUS_PCI];
92 if (access(name, R_OK))
94 a->warning("Cannot open %s", name);
97 a->debug("...using %s", name);
102 proc_init(struct pci_access *a)
108 proc_cleanup(struct pci_access *a)
118 proc_scan(struct pci_access *a)
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");
127 a->error("Cannot open %s", buf);
128 while (fgets(buf, sizeof(buf)-1, f))
130 struct pci_dev *d = pci_alloc_dev(a);
131 unsigned int dfn, vend, cnt, known;
134 #ifdef HAVE_LONG_ADDRESS
135 "%x %x %x %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx",
137 "%x %x %x %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx",
156 if (cnt != 9 && cnt != 10 && cnt != 17)
157 a->error("proc: parse error (read only %d items)", cnt);
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;
166 known |= PCI_FILL_IRQ | PCI_FILL_BASES;
168 known |= PCI_FILL_ROM_BASE;
170 known |= PCI_FILL_SIZES;
172 d->known_fields = known;
179 proc_setup(struct pci_dev *d, int rw)
181 struct pci_access *a = d->access;
183 if (a->cached_dev != d || a->fd_rw < rw)
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);
194 a->warning("Cannot open %s", buf);
202 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
204 int fd = proc_setup(d, 0);
209 res = do_read(d, fd, buf, len, pos);
212 d->access->warning("proc_read: read failed: %s", strerror(errno));
217 d->access->warning("proc_read: tried to read %d bytes at %d, but got only %d", len, pos, res);
224 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
226 int fd = proc_setup(d, 1);
231 res = do_write(d, fd, buf, len, pos);
234 d->access->warning("proc_write: write failed: %s", strerror(errno));
239 d->access->warning("proc_write: tried to write %d bytes at %d, but got only %d", len, pos, res);
246 proc_cleanup_dev(struct pci_dev *d)
248 if (d->access->cached_dev == d)
249 d->access->cached_dev = NULL;
252 struct pci_methods pm_linux_proc = {
259 pci_generic_fill_info,