2 * $Id: proc.c,v 1.1 1999/01/22 21:05:39 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>
22 #include <asm/unistd.h>
23 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1
24 #include <syscall-list.h>
28 * As libc doesn't support pread/pwrite yet, we have to call them directly
29 * or use lseek/read/write instead.
31 #if !(defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0)
33 #if defined(__GLIBC__) && !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
35 #define SYS_pread __NR_pread
38 pread(unsigned int fd, void *buf, size_t size, loff_t where)
40 return syscall(SYS_pread, fd, buf, size, where);
44 #define SYS_pwrite __NR_pwrite
47 pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
49 return syscall(SYS_pwrite, fd, buf, size, where);
52 static _syscall4(int, pread, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
53 static _syscall4(int, pwrite, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
59 proc_config(struct pci_access *a)
61 a->method_params[PCI_ACCESS_PROC_BUS_PCI] = PATH_PROC_BUS_PCI;
65 proc_detect(struct pci_access *a)
67 char *name = a->method_params[PCI_ACCESS_PROC_BUS_PCI];
69 if (access(name, R_OK))
71 a->warning("Cannot open %s", name);
74 a->debug("...using %s", name);
79 proc_init(struct pci_access *a)
85 proc_cleanup(struct pci_access *a)
95 proc_scan(struct pci_access *a)
100 if (snprintf(buf, sizeof(buf), "%s/devices", a->method_params[PCI_ACCESS_PROC_BUS_PCI]) == sizeof(buf))
101 a->error("File name too long");
104 a->error("Cannot open %s", buf);
105 while (fgets(buf, sizeof(buf)-1, f))
107 struct pci_dev *d = pci_alloc_dev(a);
108 unsigned int dfn, vend;
110 sscanf(buf, "%x %x %x %lx %lx %lx %lx %lx %lx %lx",
122 d->dev = PCI_SLOT(dfn & 0xff);
123 d->func = PCI_FUNC(dfn & 0xff);
124 d->vendor_id = vend >> 16U;
125 d->device_id = vend & 0xffff;
126 d->known_fields = a->buscentric ? PCI_FILL_IDENT
127 : (PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE);
134 proc_setup(struct pci_dev *d, int rw)
136 struct pci_access *a = d->access;
138 if (a->cached_dev != d || a->fd_rw < rw)
143 if (snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d", a->method_params[PCI_ACCESS_PROC_BUS_PCI],
144 d->bus, d->dev, d->func) == sizeof(buf))
145 a->error("File name too long");
146 a->fd_rw = a->writeable || rw;
147 a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
149 a->warning("Cannot open %s", buf);
156 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
158 int fd = proc_setup(d, 0);
163 res = pread(fd, buf, len, pos);
166 d->access->warning("proc_read: read failed: %s", strerror(errno));
171 d->access->warning("proc_read: tried to read %d bytes at %d, but got only %d", len, pos, res);
178 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
180 int fd = proc_setup(d, 1);
185 res = pwrite(fd, buf, len, pos);
188 d->access->warning("proc_write: write failed: %s", strerror(errno));
193 d->access->warning("proc_write: tried to write %d bytes at %d, but got only %d", len, pos, res);
200 proc_cleanup_dev(struct pci_dev *d)
202 if (d->access->cached_dev == d)
203 d->access->cached_dev = NULL;
206 struct pci_methods pm_linux_proc = {
213 pci_generic_fill_info,