2 * $Id: proc.c,v 1.6 1999/08/31 05:58:07 ecd 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 _syscall4(int, pread, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
50 static _syscall4(int, pwrite, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
53 /* In all other cases we use lseek/read/write instead to be safe */
54 #define make_rw_glue(op) \
55 static int do_##op(struct pci_dev *d, int fd, void *buf, size_t size, loff_t where) \
57 struct pci_access *a = d->access; \
59 if (a->fd_pos != where && lseek(fd, where, SEEK_SET) < 0) \
61 r = op(fd, buf, size); \
65 a->fd_pos = where + r; \
74 #define do_read(d,f,b,l,p) pread(f,b,l,p)
75 #define do_write(d,f,b,l,p) pwrite(f,b,l,p)
79 proc_config(struct pci_access *a)
81 a->method_params[PCI_ACCESS_PROC_BUS_PCI] = PATH_PROC_BUS_PCI;
85 proc_detect(struct pci_access *a)
87 char *name = a->method_params[PCI_ACCESS_PROC_BUS_PCI];
89 if (access(name, R_OK))
91 a->warning("Cannot open %s", name);
94 a->debug("...using %s", name);
99 proc_init(struct pci_access *a)
105 proc_cleanup(struct pci_access *a)
115 proc_scan(struct pci_access *a)
120 if (snprintf(buf, sizeof(buf), "%s/devices", a->method_params[PCI_ACCESS_PROC_BUS_PCI]) == sizeof(buf))
121 a->error("File name too long");
124 a->error("Cannot open %s", buf);
125 while (fgets(buf, sizeof(buf)-1, f))
127 struct pci_dev *d = pci_alloc_dev(a);
128 unsigned int dfn, vend, cnt, known;
131 #ifdef HAVE_LONG_ADDRESS
132 "%x %x %x %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx",
134 "%x %x %x %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx",
153 if (cnt != 9 && cnt != 10 && cnt != 17)
154 a->error("proc: parse error (read only %d items)", cnt);
156 d->dev = PCI_SLOT(dfn & 0xff);
157 d->func = PCI_FUNC(dfn & 0xff);
158 d->vendor_id = vend >> 16U;
159 d->device_id = vend & 0xffff;
160 known = PCI_FILL_IDENT;
163 known |= PCI_FILL_IRQ | PCI_FILL_BASES;
165 known |= PCI_FILL_ROM_BASE;
167 known |= PCI_FILL_SIZES;
169 d->known_fields = known;
176 proc_setup(struct pci_dev *d, int rw)
178 struct pci_access *a = d->access;
180 if (a->cached_dev != d || a->fd_rw < rw)
185 if (snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d", a->method_params[PCI_ACCESS_PROC_BUS_PCI],
186 d->bus, d->dev, d->func) == sizeof(buf))
187 a->error("File name too long");
188 a->fd_rw = a->writeable || rw;
189 a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
191 a->warning("Cannot open %s", buf);
199 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
201 int fd = proc_setup(d, 0);
206 res = do_read(d, fd, buf, len, pos);
209 d->access->warning("proc_read: read failed: %s", strerror(errno));
214 d->access->warning("proc_read: tried to read %d bytes at %d, but got only %d", len, pos, res);
221 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
223 int fd = proc_setup(d, 1);
228 res = do_write(d, fd, buf, len, pos);
231 d->access->warning("proc_write: write failed: %s", strerror(errno));
236 d->access->warning("proc_write: tried to write %d bytes at %d, but got only %d", len, pos, res);
243 proc_cleanup_dev(struct pci_dev *d)
245 if (d->access->cached_dev == d)
246 d->access->cached_dev = NULL;
249 struct pci_methods pm_linux_proc = {
256 pci_generic_fill_info,