2 * The PCI Library -- Configuration Access via /proc/bus/pci
4 * Copyright (c) 1997--2003 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
17 #include <sys/types.h>
23 proc_config(struct pci_access *a)
25 pci_define_param(a, "proc.path", PCI_PATH_PROC_BUS_PCI, "Path to the procfs bus tree");
29 proc_detect(struct pci_access *a)
31 char *name = pci_get_param(a, "proc.path");
33 if (access(name, R_OK))
35 a->warning("Cannot open %s", name);
38 a->debug("...using %s", name);
43 proc_init(struct pci_access *a)
49 proc_cleanup(struct pci_access *a)
59 proc_scan(struct pci_access *a)
64 if (snprintf(buf, sizeof(buf), "%s/devices", pci_get_param(a, "proc.path")) == sizeof(buf))
65 a->error("File name too long");
68 a->error("Cannot open %s", buf);
69 while (fgets(buf, sizeof(buf)-1, f))
71 struct pci_dev *d = pci_alloc_dev(a);
72 unsigned int dfn, vend, cnt, known;
76 #define F " " PCIADDR_T_FMT
77 cnt = sscanf(buf, "%x %x %x" F F F F F F F F F F F F F F "%n",
97 if (cnt != 9 && cnt != 10 && cnt != 17)
98 a->error("proc: parse error (read only %d items)", cnt);
100 d->dev = PCI_SLOT(dfn & 0xff);
101 d->func = PCI_FUNC(dfn & 0xff);
102 d->vendor_id = vend >> 16U;
103 d->device_id = vend & 0xffff;
107 known |= PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES;
109 known |= PCI_FILL_ROM_BASE;
111 known |= PCI_FILL_SIZES;
115 while (buf[offset] && isspace(buf[offset]))
117 driver = &buf[offset];
118 while (buf[offset] && !isspace(buf[offset]))
123 pci_set_property(d, PCI_FILL_DRIVER, driver);
124 known |= PCI_FILL_DRIVER;
127 d->known_fields = known;
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)
144 e = snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d",
145 pci_get_param(a, "proc.path"),
146 d->bus, d->dev, d->func);
147 if (e < 0 || e >= (int) sizeof(buf))
148 a->error("File name too long");
149 a->fd_rw = a->writeable || rw;
150 a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
153 e = snprintf(buf, sizeof(buf), "%s/%04x:%02x/%02x.%d",
154 pci_get_param(a, "proc.path"),
155 d->domain, d->bus, d->dev, d->func);
156 if (e < 0 || e >= (int) sizeof(buf))
157 a->error("File name too long");
158 a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
161 a->warning("Cannot open %s", buf);
169 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
171 int fd = proc_setup(d, 0);
176 res = do_read(d, fd, buf, len, pos);
179 d->access->warning("proc_read: read failed: %s", strerror(errno));
188 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
190 int fd = proc_setup(d, 1);
195 res = do_write(d, fd, buf, len, pos);
198 d->access->warning("proc_write: write failed: %s", strerror(errno));
203 d->access->warning("proc_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
210 proc_cleanup_dev(struct pci_dev *d)
212 if (d->access->cached_dev == d)
213 d->access->cached_dev = NULL;
216 struct pci_methods pm_linux_proc = {
218 "The proc file system on Linux",
224 pci_generic_fill_info,