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 v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
19 #include <sys/types.h>
25 proc_config(struct pci_access *a)
27 pci_define_param(a, "proc.path", PCI_PATH_PROC_BUS_PCI, "Path to the procfs bus tree");
31 proc_detect(struct pci_access *a)
33 char *name = pci_get_param(a, "proc.path");
35 if (access(name, R_OK))
37 a->warning("Cannot open %s", name);
40 a->debug("...using %s", name);
45 proc_init(struct pci_access *a)
51 proc_cleanup(struct pci_access *a)
61 proc_scan(struct pci_access *a)
66 if (snprintf(buf, sizeof(buf), "%s/devices", pci_get_param(a, "proc.path")) == sizeof(buf))
67 a->error("File name too long");
70 a->error("Cannot open %s", buf);
71 while (fgets(buf, sizeof(buf)-1, f))
73 struct pci_dev *d = pci_alloc_dev(a);
74 unsigned int dfn, vend, cnt, known;
78 #define F " " PCIADDR_T_FMT
79 cnt = sscanf(buf, "%x %x %x" F F F F F F F F F F F F F F "%n",
99 if (cnt != 9 && cnt != 10 && cnt != 17)
100 a->error("proc: parse error (read only %d items)", cnt);
102 d->dev = PCI_SLOT(dfn & 0xff);
103 d->func = PCI_FUNC(dfn & 0xff);
104 d->vendor_id = vend >> 16U;
105 d->device_id = vend & 0xffff;
109 known |= PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES;
111 known |= PCI_FILL_ROM_BASE;
113 known |= PCI_FILL_SIZES;
117 while (buf[offset] && isspace(buf[offset]))
119 driver = &buf[offset];
120 while (buf[offset] && !isspace(buf[offset]))
125 pci_set_property(d, PCI_FILL_DRIVER, driver);
126 known |= PCI_FILL_DRIVER;
129 d->known_fields = known;
136 proc_setup(struct pci_dev *d, int rw)
138 struct pci_access *a = d->access;
140 if (a->cached_dev != d || a->fd_rw < rw)
146 e = snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d",
147 pci_get_param(a, "proc.path"),
148 d->bus, d->dev, d->func);
149 if (e < 0 || e >= (int) sizeof(buf))
150 a->error("File name too long");
151 a->fd_rw = a->writeable || rw;
152 a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
155 e = snprintf(buf, sizeof(buf), "%s/%04x:%02x/%02x.%d",
156 pci_get_param(a, "proc.path"),
157 d->domain, d->bus, d->dev, d->func);
158 if (e < 0 || e >= (int) sizeof(buf))
159 a->error("File name too long");
160 a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
163 a->warning("Cannot open %s", buf);
171 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
173 int fd = proc_setup(d, 0);
178 res = do_read(d, fd, buf, len, pos);
181 d->access->warning("proc_read: read failed: %s", strerror(errno));
190 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
192 int fd = proc_setup(d, 1);
197 res = do_write(d, fd, buf, len, pos);
200 d->access->warning("proc_write: write failed: %s", strerror(errno));
205 d->access->warning("proc_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
212 proc_cleanup_dev(struct pci_dev *d)
214 if (d->access->cached_dev == d)
215 d->access->cached_dev = NULL;
218 struct pci_methods pm_linux_proc = {
220 "The proc file system on Linux",
226 pci_generic_fill_info,