]> mj.ucw.cz Git - pciutils.git/blob - lib/sysfs.c
6ebabe1c45d0e79da864a93e91c1df2468803578
[pciutils.git] / lib / sysfs.c
1 /*
2  *      The PCI Library -- Configuration Access via /sys/bus/pci
3  *
4  *      Copyright (c) 2003 Matthew Wilcox <willy@fc.hp.com>
5  *      Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
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
25 static void
26 sysfs_config(struct pci_access *a)
27 {
28   pci_define_param(a, "sysfs.path", PCI_PATH_SYS_BUS_PCI, "Path to the sysfs device tree");
29 }
30
31 static inline char *
32 sysfs_name(struct pci_access *a)
33 {
34   return pci_get_param(a, "sysfs.path");
35 }
36
37 static int
38 sysfs_detect(struct pci_access *a)
39 {
40   if (access(sysfs_name(a), R_OK))
41     {
42       a->debug("...cannot open %s", sysfs_name(a));
43       return 0;
44     }
45   a->debug("...using %s", sysfs_name(a));
46   return 1;
47 }
48
49 static void
50 sysfs_init(struct pci_access *a)
51 {
52   a->fd = -1;
53 }
54
55 static void
56 sysfs_cleanup(struct pci_access *a)
57 {
58   if (a->fd >= 0)
59     {
60       close(a->fd);
61       a->fd = -1;
62     }
63 }
64
65 #define OBJNAMELEN 1024
66 static void
67 sysfs_obj_name(struct pci_dev *d, char *object, char *buf)
68 {
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");
73 }
74
75 static int
76 sysfs_get_value(struct pci_dev *d, char *object)
77 {
78   struct pci_access *a = d->access;
79   int fd, n;
80   char namebuf[OBJNAMELEN], buf[256];
81
82   sysfs_obj_name(d, object, namebuf);
83   fd = open(namebuf, O_RDONLY);
84   if (fd < 0)
85     a->error("Cannot open %s: %s", namebuf, strerror(errno));
86   n = read(fd, buf, sizeof(buf));
87   close(fd);
88   if (n < 0)
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);
92   buf[n] = 0;
93   return strtol(buf, NULL, 0);
94 }
95
96 static void
97 sysfs_get_resources(struct pci_dev *d)
98 {
99   struct pci_access *a = d->access;
100   char namebuf[OBJNAMELEN], buf[256];
101   FILE *file;
102   int i;
103
104   sysfs_obj_name(d, "resource", namebuf);
105   file = fopen(namebuf, "r");
106   if (!file)
107     a->error("Cannot open %s: %s", namebuf, strerror(errno));
108   for (i = 0; i < 7; i++)
109     {
110       unsigned long long start, end, size;
111       if (!fgets(buf, sizeof(buf), file))
112         break;
113       if (sscanf(buf, "%llx %llx", &start, &end) != 2)
114         a->error("Syntax error in %s", namebuf);
115       if (start)
116         size = end - start + 1;
117       else
118         size = 0;
119       if (i < 6)
120         {
121           d->base_addr[i] = start;
122           d->size[i] = size;
123         }
124       else
125         {
126           d->rom_base_addr = start;
127           d->rom_size = size;
128         }
129     }
130   fclose(file);
131 }
132
133 static void sysfs_scan(struct pci_access *a)
134 {
135   char dirname[1024];
136   DIR *dir;
137   struct dirent *entry;
138   int n;
139
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);
144   if (!dir)
145     a->error("Cannot open %s", dirname);
146   while ((entry = readdir(dir)))
147     {
148       struct pci_dev *d;
149       unsigned int dom, bus, dev, func;
150
151       /* ".", ".." or a special non-device perhaps */
152       if (entry->d_name[0] == '.')
153         continue;
154
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);
158       d->domain = dom;
159       d->bus = bus;
160       d->dev = dev;
161       d->func = func;
162       if (!a->buscentric)
163         {
164           sysfs_get_resources(d);
165           d->irq = sysfs_get_value(d, "irq");
166           /*
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.
169            */
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;
174         }
175       pci_link_dev(a, d);
176     }
177   closedir(dir);
178 }
179
180 /* Intent of the sysfs_setup() caller */
181 enum
182   {
183     SETUP_READ_CONFIG = 0,
184     SETUP_WRITE_CONFIG = 1,
185     SETUP_READ_VPD = 2
186   };
187
188 static int
189 sysfs_setup(struct pci_dev *d, int intent)
190 {
191   struct pci_access *a = d->access;
192   char namebuf[OBJNAMELEN];
193
194   if (a->cached_dev != d || intent == SETUP_WRITE_CONFIG && !a->fd_rw)
195     {
196       if (a->fd >= 0)
197         close(a->fd);
198       sysfs_obj_name(d, "config", namebuf);
199       a->fd_rw = a->writeable || intent == SETUP_WRITE_CONFIG;
200       a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
201       if (a->fd < 0)
202         a->warning("Cannot open %s", namebuf);
203       a->fd_pos = 0;
204     }
205
206   if (a->cached_dev != d)
207     {
208       if (a->fd_vpd >= 0)
209         close(a->fd_vpd);
210       sysfs_obj_name(d, "vpd", namebuf);
211       a->fd_vpd = open(namebuf, O_RDONLY);
212       /* No warning on error; vpd may be absent or accessible only to root */
213     }
214
215   a->cached_dev = d;
216   return intent == SETUP_READ_VPD ? a->fd_vpd : a->fd;
217 }
218
219 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
220 {
221   int fd = sysfs_setup(d, SETUP_READ_CONFIG);
222   int res;
223
224   if (fd < 0)
225     return 0;
226   res = do_read(d, fd, buf, len, pos);
227   if (res < 0)
228     {
229       d->access->warning("sysfs_read: read failed: %s", strerror(errno));
230       return 0;
231     }
232   else if (res != len)
233     return 0;
234   return 1;
235 }
236
237 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
238 {
239   int fd = sysfs_setup(d, SETUP_WRITE_CONFIG);
240   int res;
241
242   if (fd < 0)
243     return 0;
244   res = do_write(d, fd, buf, len, pos);
245   if (res < 0)
246     {
247       d->access->warning("sysfs_write: write failed: %s", strerror(errno));
248       return 0;
249     }
250   else if (res != len)
251     {
252       d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
253       return 0;
254     }
255   return 1;
256 }
257
258 #ifdef PCI_HAVE_DO_READ
259
260 /* pread() is not available and do_read() only works for a single fd, so we
261  * cannot implement read_vpd properly. */
262 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
263 {
264   return 0;
265 }
266
267 #else /* !PCI_HAVE_DO_READ */
268
269 static int sysfs_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
270 {
271   int fd = sysfs_setup(d, SETUP_READ_VPD);
272   int res;
273
274   if (fd < 0)
275     return 0;
276   res = pread(fd, buf, len, pos);
277   if (res < 0)
278     {
279       d->access->warning("sysfs_read_vpd: read failed: %s", strerror(errno));
280       return 0;
281     }
282   else if (res != len)
283     return 0;
284   return 1;
285 }
286
287 #endif /* PCI_HAVE_DO_READ */
288
289 static void sysfs_cleanup_dev(struct pci_dev *d)
290 {
291   struct pci_access *a = d->access;
292
293   if (a->cached_dev == d)
294     {
295       a->cached_dev = NULL;
296       close(a->fd);
297       a->fd = -1;
298     }
299 }
300
301 struct pci_methods pm_linux_sysfs = {
302   "linux-sysfs",
303   "The sys filesystem on Linux",
304   sysfs_config,
305   sysfs_detect,
306   sysfs_init,
307   sysfs_cleanup,
308   sysfs_scan,
309   pci_generic_fill_info,
310   sysfs_read,
311   sysfs_write,
312   sysfs_read_vpd,
313   NULL,                                 /* init_dev */
314   sysfs_cleanup_dev
315 };