2 * The PCI Library -- Configuration Access via /sys/bus/pci
4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
7 * Can be freely distributed and used under the terms of the GNU GPL.
20 #include <sys/types.h>
26 sysfs_config(struct pci_access *a)
28 pci_define_param(a, "sysfs.path", PCI_PATH_SYS_BUS_PCI, "Path to the sysfs device tree");
32 sysfs_name(struct pci_access *a)
34 return pci_get_param(a, "sysfs.path");
38 sysfs_detect(struct pci_access *a)
40 if (access(sysfs_name(a), R_OK))
42 a->debug("...cannot open %s", sysfs_name(a));
45 a->debug("...using %s", sysfs_name(a));
50 sysfs_init(struct pci_access *a)
56 sysfs_cleanup(struct pci_access *a)
65 #define OBJNAMELEN 1024
67 sysfs_obj_name(struct pci_dev *d, char *object, char *buf)
69 int n = snprintf(buf, OBJNAMELEN, "%s/devices/%04x:%02x:%02x.%d/%s",
70 sysfs_name(d->access), d->domain, d->bus, d->dev, d->func, object);
71 if (n < 0 || n >= OBJNAMELEN)
72 d->access->error("File name too long");
76 sysfs_get_value(struct pci_dev *d, char *object)
78 struct pci_access *a = d->access;
80 char namebuf[OBJNAMELEN], buf[256];
82 sysfs_obj_name(d, object, namebuf);
83 fd = open(namebuf, O_RDONLY);
85 a->error("Cannot open %s: %s", namebuf, strerror(errno));
86 n = read(fd, buf, sizeof(buf));
89 a->error("Error reading %s: %s", namebuf, strerror(errno));
90 if (n >= (int) sizeof(buf))
91 a->error("Value in %s too long", namebuf);
93 return strtol(buf, NULL, 0);
97 sysfs_get_resources(struct pci_dev *d)
99 struct pci_access *a = d->access;
100 char namebuf[OBJNAMELEN], buf[256];
104 sysfs_obj_name(d, "resource", namebuf);
105 file = fopen(namebuf, "r");
107 a->error("Cannot open %s: %s", namebuf, strerror(errno));
108 for (i = 0; i < 7; i++)
110 unsigned long long start, end, size;
111 if (!fgets(buf, sizeof(buf), file))
113 if (sscanf(buf, "%llx %llx", &start, &end) != 2)
114 a->error("Syntax error in %s", namebuf);
116 size = end - start + 1;
121 d->base_addr[i] = start;
126 d->rom_base_addr = start;
133 static void sysfs_scan(struct pci_access *a)
137 struct dirent *entry;
140 n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
141 if (n < 0 || n >= (int) sizeof(dirname))
142 a->error("Directory name too long");
143 dir = opendir(dirname);
145 a->error("Cannot open %s", dirname);
146 while ((entry = readdir(dir)))
149 unsigned int dom, bus, dev, func;
151 /* ".", ".." or a special non-device perhaps */
152 if (entry->d_name[0] == '.')
155 d = pci_alloc_dev(a);
156 if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
157 a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
164 sysfs_get_resources(d);
165 d->irq = sysfs_get_value(d, "irq");
167 * We could read these faster from the config registers, but we want to give
168 * the kernel a chance to fix up ID's and especially classes of broken devices.
170 d->vendor_id = sysfs_get_value(d, "vendor");
171 d->device_id = sysfs_get_value(d, "device");
172 d->device_class = sysfs_get_value(d, "class") >> 8;
173 d->known_fields = PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES;
181 sysfs_fill_slots(struct pci_access *a)
185 struct dirent *entry;
188 n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a));
189 if (n < 0 || n >= (int) sizeof(dirname))
190 a->error("Directory name too long");
191 dir = opendir(dirname);
195 while (entry = readdir(dir))
197 char namebuf[OBJNAMELEN], buf[16];
199 unsigned int dom, bus, dev;
202 /* ".", ".." or a special non-device perhaps */
203 if (entry->d_name[0] == '.')
206 n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address");
207 if (n < 0 || n >= OBJNAMELEN)
208 a->error("File name too long");
209 file = fopen(namebuf, "r");
212 a->warning("sysfs_fill_slots: Cannot open %s: %s", namebuf, strerror(errno));
216 if (!fgets(buf, sizeof(buf), file) || sscanf(buf, "%x:%x:%x", &dom, &bus, &dev) < 3)
217 a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
220 for (d = a->devices; d; d = d->next)
221 if (dom == d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
223 d->phy_slot = pci_malloc(a, strlen(entry->d_name) + 1);
224 strcpy(d->phy_slot, entry->d_name);
234 sysfs_fill_info(struct pci_dev *d, int flags)
236 if ((flags & PCI_FILL_PHYS_SLOT) && !(d->known_fields & PCI_FILL_PHYS_SLOT))
239 sysfs_fill_slots(d->access);
240 for (pd = d->access->devices; pd; pd = pd->next)
241 pd->known_fields |= PCI_FILL_PHYS_SLOT;
243 return pci_generic_fill_info(d, flags);
246 /* Intent of the sysfs_setup() caller */
249 SETUP_READ_CONFIG = 0,
250 SETUP_WRITE_CONFIG = 1,
255 sysfs_setup(struct pci_dev *d, int intent)
257 struct pci_access *a = d->access;
258 char namebuf[OBJNAMELEN];
260 if (a->cached_dev != d || intent == SETUP_WRITE_CONFIG && !a->fd_rw)
264 sysfs_obj_name(d, "config", namebuf);
265 a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
266 a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
268 a->warning("Cannot open %s", namebuf);
272 if (a->cached_dev != d)
276 sysfs_obj_name(d, "vpd", namebuf);
277 a->fd_vpd = open(namebuf, O_RDONLY);
278 /* No warning on error; vpd may be absent or accessible only to root */
282 return intent == SETUP_READ_VPD ? a->fd_vpd : a->fd;
285 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
287 int fd = sysfs_setup(d, SETUP_READ_CONFIG);
292 res = do_read(d, fd, buf, len, pos);
295 d->access->warning("sysfs_read: read failed: %s", strerror(errno));
303 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
305 int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
310 res = do_write(d, fd, buf, len, pos);
313 d->access->warning("sysfs_write: write failed: %s", strerror(errno));
318 d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
324 #ifdef PCI_HAVE_DO_READ
326 /* pread() is not available and do_read() only works for a single fd, so we
327 * cannot implement read_vpd properly. */
328 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
333 #else /* !PCI_HAVE_DO_READ */
335 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
337 int fd = sysfs_setup(d, SETUP_READ_VPD);
342 res = pread(fd, buf, len, pos);
345 d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
353 #endif /* PCI_HAVE_DO_READ */
355 static void sysfs_cleanup_dev(struct pci_dev *d)
357 struct pci_access *a = d->access;
359 if (a->cached_dev == d)
361 a->cached_dev = NULL;
367 struct pci_methods pm_linux_sysfs = {
369 "The sys filesystem on Linux",