]> git.ipfire.org Git - thirdparty/pciutils.git/blame - lib/sysfs.c
pkg-config: Use Libs.private for libraries needed for static linking
[thirdparty/pciutils.git] / lib / sysfs.c
CommitLineData
11f7b31b
MM
1/*
2 * The PCI Library -- Configuration Access via /sys/bus/pci
3 *
535fca0e 4 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
cb6ee324 5 * Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
11f7b31b
MM
6 *
7 * Can be freely distributed and used under the terms of the GNU GPL.
8 */
9
10#define _GNU_SOURCE
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <stdarg.h>
16#include <unistd.h>
17#include <errno.h>
18#include <dirent.h>
19#include <fcntl.h>
20#include <sys/types.h>
21
22#include "internal.h"
23#include "pread.h"
24
25static void
26sysfs_config(struct pci_access *a)
27{
cb6ee324 28 pci_define_param(a, "sysfs.path", PCI_PATH_SYS_BUS_PCI, "Path to the sysfs device tree");
11f7b31b
MM
29}
30
31static inline char *
32sysfs_name(struct pci_access *a)
33{
cb6ee324 34 return pci_get_param(a, "sysfs.path");
11f7b31b
MM
35}
36
37static int
38sysfs_detect(struct pci_access *a)
39{
40 if (access(sysfs_name(a), R_OK))
41 {
84c8d1bb 42 a->debug("...cannot open %s", sysfs_name(a));
11f7b31b
MM
43 return 0;
44 }
45 a->debug("...using %s", sysfs_name(a));
46 return 1;
47}
48
49static void
50sysfs_init(struct pci_access *a)
51{
52 a->fd = -1;
07ad085d 53 a->fd_vpd = -1;
11f7b31b
MM
54}
55
56static void
07ad085d 57sysfs_flush_cache(struct pci_access *a)
11f7b31b
MM
58{
59 if (a->fd >= 0)
60 {
61 close(a->fd);
62 a->fd = -1;
63 }
07ad085d
MM
64 if (a->fd_vpd >= 0)
65 {
66 close(a->fd_vpd);
67 a->fd_vpd = -1;
68 }
69 a->cached_dev = NULL;
70}
71
72static void
73sysfs_cleanup(struct pci_access *a)
74{
75 sysfs_flush_cache(a);
11f7b31b
MM
76}
77
78#define OBJNAMELEN 1024
79static void
80sysfs_obj_name(struct pci_dev *d, char *object, char *buf)
81{
82 int n = snprintf(buf, OBJNAMELEN, "%s/devices/%04x:%02x:%02x.%d/%s",
83 sysfs_name(d->access), d->domain, d->bus, d->dev, d->func, object);
84 if (n < 0 || n >= OBJNAMELEN)
85 d->access->error("File name too long");
86}
87
88static int
89sysfs_get_value(struct pci_dev *d, char *object)
90{
91 struct pci_access *a = d->access;
92 int fd, n;
93 char namebuf[OBJNAMELEN], buf[256];
94
95 sysfs_obj_name(d, object, namebuf);
96 fd = open(namebuf, O_RDONLY);
97 if (fd < 0)
98 a->error("Cannot open %s: %s", namebuf, strerror(errno));
99 n = read(fd, buf, sizeof(buf));
100 close(fd);
101 if (n < 0)
102 a->error("Error reading %s: %s", namebuf, strerror(errno));
103 if (n >= (int) sizeof(buf))
104 a->error("Value in %s too long", namebuf);
105 buf[n] = 0;
106 return strtol(buf, NULL, 0);
107}
108
109static void
110sysfs_get_resources(struct pci_dev *d)
111{
112 struct pci_access *a = d->access;
113 char namebuf[OBJNAMELEN], buf[256];
114 FILE *file;
115 int i;
116
117 sysfs_obj_name(d, "resource", namebuf);
118 file = fopen(namebuf, "r");
119 if (!file)
120 a->error("Cannot open %s: %s", namebuf, strerror(errno));
9bb4b4ea 121 for (i = 0; i < 7; i++)
11f7b31b 122 {
6d143c32 123 unsigned long long start, end, size, flags;
11f7b31b
MM
124 if (!fgets(buf, sizeof(buf), file))
125 break;
6d143c32 126 if (sscanf(buf, "%llx %llx %llx", &start, &end, &flags) != 3)
11f7b31b 127 a->error("Syntax error in %s", namebuf);
11f7b31b
MM
128 if (start)
129 size = end - start + 1;
130 else
131 size = 0;
6d143c32 132 flags &= PCI_ADDR_FLAG_MASK;
9bb4b4ea 133 if (i < 6)
11f7b31b 134 {
6d143c32 135 d->base_addr[i] = start | flags;
11f7b31b
MM
136 d->size[i] = size;
137 }
138 else
139 {
6d143c32 140 d->rom_base_addr = start | flags;
11f7b31b
MM
141 d->rom_size = size;
142 }
143 }
144 fclose(file);
145}
146
147static void sysfs_scan(struct pci_access *a)
148{
149 char dirname[1024];
150 DIR *dir;
151 struct dirent *entry;
152 int n;
153
154 n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
155 if (n < 0 || n >= (int) sizeof(dirname))
156 a->error("Directory name too long");
157 dir = opendir(dirname);
158 if (!dir)
159 a->error("Cannot open %s", dirname);
160 while ((entry = readdir(dir)))
161 {
162 struct pci_dev *d;
163 unsigned int dom, bus, dev, func;
164
165 /* ".", ".." or a special non-device perhaps */
166 if (entry->d_name[0] == '.')
167 continue;
168
169 d = pci_alloc_dev(a);
170 if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
171 a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
172 d->domain = dom;
173 d->bus = bus;
174 d->dev = dev;
175 d->func = func;
11f7b31b
MM
176 if (!a->buscentric)
177 {
178 sysfs_get_resources(d);
84c8d1bb 179 d->irq = sysfs_get_value(d, "irq");
84c8d1bb 180 /*
c2b144ef
MM
181 * We could read these faster from the config registers, but we want to give
182 * the kernel a chance to fix up ID's and especially classes of broken devices.
84c8d1bb 183 */
11f7b31b
MM
184 d->vendor_id = sysfs_get_value(d, "vendor");
185 d->device_id = sysfs_get_value(d, "device");
c2b144ef
MM
186 d->device_class = sysfs_get_value(d, "class") >> 8;
187 d->known_fields = PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES;
11f7b31b
MM
188 }
189 pci_link_dev(a, d);
190 }
191 closedir(dir);
192}
193
2849a165 194static void
6171c06d 195sysfs_fill_slots(struct pci_access *a)
2849a165 196{
2849a165
AC
197 char dirname[1024];
198 DIR *dir;
199 struct dirent *entry;
200 int n;
201
202 n = snprintf(dirname, sizeof(dirname), "%s/slots", sysfs_name(a));
203 if (n < 0 || n >= (int) sizeof(dirname))
204 a->error("Directory name too long");
205 dir = opendir(dirname);
206 if (!dir)
6171c06d
MM
207 return;
208
209 while (entry = readdir(dir))
2849a165
AC
210 {
211 char namebuf[OBJNAMELEN], buf[16];
212 FILE *file;
213 unsigned int dom, bus, dev;
6171c06d 214 struct pci_dev *d;
2849a165
AC
215
216 /* ".", ".." or a special non-device perhaps */
217 if (entry->d_name[0] == '.')
218 continue;
219
6171c06d 220 n = snprintf(namebuf, OBJNAMELEN, "%s/%s/%s", dirname, entry->d_name, "address");
2849a165 221 if (n < 0 || n >= OBJNAMELEN)
6171c06d 222 a->error("File name too long");
2849a165 223 file = fopen(namebuf, "r");
534fbba9
MW
224 /*
225 * Old versions of Linux had a fakephp which didn't have an 'address'
226 * file. There's no useful information to be gleaned from these
227 * devices, pretend they're not there.
228 */
2849a165 229 if (!file)
534fbba9 230 continue;
6171c06d
MM
231
232 if (!fgets(buf, sizeof(buf), file) || sscanf(buf, "%x:%x:%x", &dom, &bus, &dev) < 3)
233 a->warning("sysfs_fill_slots: Couldn't parse entry address %s", buf);
234 else
235 {
236 for (d = a->devices; d; d = d->next)
237 if (dom == d->domain && bus == d->bus && dev == d->dev && !d->phy_slot)
238 {
239 d->phy_slot = pci_malloc(a, strlen(entry->d_name) + 1);
240 strcpy(d->phy_slot, entry->d_name);
6171c06d 241 }
2849a165
AC
242 }
243 fclose(file);
244 }
245 closedir(dir);
246}
247
248static int
249sysfs_fill_info(struct pci_dev *d, int flags)
250{
6171c06d
MM
251 if ((flags & PCI_FILL_PHYS_SLOT) && !(d->known_fields & PCI_FILL_PHYS_SLOT))
252 {
253 struct pci_dev *pd;
254 sysfs_fill_slots(d->access);
255 for (pd = d->access->devices; pd; pd = pd->next)
256 pd->known_fields |= PCI_FILL_PHYS_SLOT;
257 }
258 return pci_generic_fill_info(d, flags);
2849a165
AC
259}
260
9bd5b1cf
BH
261/* Intent of the sysfs_setup() caller */
262enum
263 {
264 SETUP_READ_CONFIG = 0,
265 SETUP_WRITE_CONFIG = 1,
266 SETUP_READ_VPD = 2
267 };
268
11f7b31b 269static int
9bd5b1cf 270sysfs_setup(struct pci_dev *d, int intent)
11f7b31b
MM
271{
272 struct pci_access *a = d->access;
9bd5b1cf 273 char namebuf[OBJNAMELEN];
11f7b31b 274
07ad085d 275 if (a->cached_dev != d || (intent == SETUP_WRITE_CONFIG && !a->fd_rw))
11f7b31b 276 {
07ad085d
MM
277 sysfs_flush_cache(a);
278 a->cached_dev = d;
11f7b31b 279 }
9bd5b1cf 280
07ad085d 281 if (intent == SETUP_READ_VPD)
9bd5b1cf 282 {
07ad085d
MM
283 if (a->fd_vpd < 0)
284 {
285 sysfs_obj_name(d, "vpd", namebuf);
286 a->fd_vpd = open(namebuf, O_RDONLY);
287 /* No warning on error; vpd may be absent or accessible only to root */
288 }
289 return a->fd_vpd;
9bd5b1cf
BH
290 }
291
07ad085d 292 if (a->fd < 0)
c886948c
MM
293 {
294 sysfs_obj_name(d, "config", namebuf);
295 a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
296 a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
297 if (a->fd < 0)
298 a->warning("Cannot open %s", namebuf);
299 a->fd_pos = 0;
300 }
07ad085d 301 return a->fd;
11f7b31b
MM
302}
303
304static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
305{
9bd5b1cf 306 int fd = sysfs_setup(d, SETUP_READ_CONFIG);
11f7b31b
MM
307 int res;
308
309 if (fd < 0)
310 return 0;
311 res = do_read(d, fd, buf, len, pos);
312 if (res < 0)
313 {
314 d->access->warning("sysfs_read: read failed: %s", strerror(errno));
315 return 0;
316 }
317 else if (res != len)
09817437 318 return 0;
11f7b31b
MM
319 return 1;
320}
321
322static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
323{
9bd5b1cf 324 int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
11f7b31b
MM
325 int res;
326
327 if (fd < 0)
328 return 0;
329 res = do_write(d, fd, buf, len, pos);
330 if (res < 0)
331 {
332 d->access->warning("sysfs_write: write failed: %s", strerror(errno));
333 return 0;
334 }
335 else if (res != len)
336 {
09817437 337 d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
11f7b31b
MM
338 return 0;
339 }
340 return 1;
341}
342
9bd5b1cf
BH
343#ifdef PCI_HAVE_DO_READ
344
345/* pread() is not available and do_read() only works for a single fd, so we
346 * cannot implement read_vpd properly. */
347static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
348{
349 return 0;
350}
351
352#else /* !PCI_HAVE_DO_READ */
353
354static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
355{
356 int fd = sysfs_setup(d, SETUP_READ_VPD);
357 int res;
358
359 if (fd < 0)
360 return 0;
361 res = pread(fd, buf, len, pos);
362 if (res < 0)
363 {
364 d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
365 return 0;
366 }
367 else if (res != len)
368 return 0;
369 return 1;
370}
371
372#endif /* PCI_HAVE_DO_READ */
373
11f7b31b
MM
374static void sysfs_cleanup_dev(struct pci_dev *d)
375{
376 struct pci_access *a = d->access;
377
378 if (a->cached_dev == d)
07ad085d 379 sysfs_flush_cache(a);
11f7b31b
MM
380}
381
382struct pci_methods pm_linux_sysfs = {
9ff67879
MM
383 "linux-sysfs",
384 "The sys filesystem on Linux",
11f7b31b
MM
385 sysfs_config,
386 sysfs_detect,
387 sysfs_init,
388 sysfs_cleanup,
389 sysfs_scan,
2849a165 390 sysfs_fill_info,
11f7b31b
MM
391 sysfs_read,
392 sysfs_write,
9bd5b1cf 393 sysfs_read_vpd,
11f7b31b
MM
394 NULL, /* init_dev */
395 sysfs_cleanup_dev
396};