]> git.ipfire.org Git - thirdparty/pciutils.git/blob - lib/sysfs.c
Added support for 4096-byte extended configuration space.
[thirdparty/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--2003 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 a->method_params[PCI_ACCESS_SYS_BUS_PCI] = PATH_SYS_BUS_PCI;
29 }
30
31 static inline char *
32 sysfs_name(struct pci_access *a)
33 {
34 return a->method_params[PCI_ACCESS_SYS_BUS_PCI];
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 < 8; 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 != (unsigned long long)(pciaddr_t) start ||
116 end != (unsigned long long)(pciaddr_t) end)
117 {
118 a->warning("Resource %d in %s has a 64-bit address, ignoring", namebuf);
119 start = end = 0;
120 }
121 if (start)
122 size = end - start + 1;
123 else
124 size = 0;
125 if (i < 7)
126 {
127 d->base_addr[i] = start;
128 d->size[i] = size;
129 }
130 else
131 {
132 d->rom_base_addr = start;
133 d->rom_size = size;
134 }
135 }
136 fclose(file);
137 }
138
139 static void sysfs_scan(struct pci_access *a)
140 {
141 char dirname[1024];
142 DIR *dir;
143 struct dirent *entry;
144 int n;
145
146 n = snprintf(dirname, sizeof(dirname), "%s/devices", sysfs_name(a));
147 if (n < 0 || n >= (int) sizeof(dirname))
148 a->error("Directory name too long");
149 dir = opendir(dirname);
150 if (!dir)
151 a->error("Cannot open %s", dirname);
152 while ((entry = readdir(dir)))
153 {
154 struct pci_dev *d;
155 unsigned int dom, bus, dev, func;
156
157 /* ".", ".." or a special non-device perhaps */
158 if (entry->d_name[0] == '.')
159 continue;
160
161 d = pci_alloc_dev(a);
162 if (sscanf(entry->d_name, "%x:%x:%x.%d", &dom, &bus, &dev, &func) < 4)
163 a->error("sysfs_scan: Couldn't parse entry name %s", entry->d_name);
164 d->domain = dom;
165 d->bus = bus;
166 d->dev = dev;
167 d->func = func;
168 if (!a->buscentric)
169 {
170 sysfs_get_resources(d);
171 d->irq = sysfs_get_value(d, "irq");
172 d->known_fields = PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES;
173 #if 0
174 /*
175 * We prefer reading these from the config registers, it's faster.
176 * However, it would be possible and maybe even useful to hack the kernel
177 * to believe that some device has a different ID. If you do it, just
178 * enable this piece of code. --mj
179 */
180 d->vendor_id = sysfs_get_value(d, "vendor");
181 d->device_id = sysfs_get_value(d, "device");
182 d->known_fields |= PCI_FILL_IDENT;
183 #endif
184 }
185 pci_link_dev(a, d);
186 }
187 closedir(dir);
188 }
189
190 static int
191 sysfs_setup(struct pci_dev *d, int rw)
192 {
193 struct pci_access *a = d->access;
194
195 if (a->cached_dev != d || a->fd_rw < rw)
196 {
197 char namebuf[OBJNAMELEN];
198 if (a->fd >= 0)
199 close(a->fd);
200 sysfs_obj_name(d, "config", namebuf);
201 a->fd_rw = a->writeable || rw;
202 a->fd = open(namebuf, a->fd_rw ? O_RDWR : O_RDONLY);
203 if (a->fd < 0)
204 a->warning("Cannot open %s", namebuf);
205 a->cached_dev = d;
206 a->fd_pos = 0;
207 }
208 return a->fd;
209 }
210
211 static int sysfs_read(struct pci_dev *d, int pos, byte *buf, int len)
212 {
213 int fd = sysfs_setup(d, 0);
214 int res;
215
216 if (fd < 0)
217 return 0;
218 res = do_read(d, fd, buf, len, pos);
219 if (res < 0)
220 {
221 d->access->warning("sysfs_read: read failed: %s", strerror(errno));
222 return 0;
223 }
224 else if (res != len)
225 return 0;
226 return 1;
227 }
228
229 static int sysfs_write(struct pci_dev *d, int pos, byte *buf, int len)
230 {
231 int fd = sysfs_setup(d, 1);
232 int res;
233
234 if (fd < 0)
235 return 0;
236 res = do_write(d, fd, buf, len, pos);
237 if (res < 0)
238 {
239 d->access->warning("sysfs_write: write failed: %s", strerror(errno));
240 return 0;
241 }
242 else if (res != len)
243 {
244 d->access->warning("sysfs_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
245 return 0;
246 }
247 return 1;
248 }
249
250 static void sysfs_cleanup_dev(struct pci_dev *d)
251 {
252 struct pci_access *a = d->access;
253
254 if (a->cached_dev == d)
255 {
256 a->cached_dev = NULL;
257 close(a->fd);
258 a->fd = -1;
259 }
260 }
261
262 struct pci_methods pm_linux_sysfs = {
263 "Linux-sysfs",
264 sysfs_config,
265 sysfs_detect,
266 sysfs_init,
267 sysfs_cleanup,
268 sysfs_scan,
269 pci_generic_fill_info,
270 sysfs_read,
271 sysfs_write,
272 NULL, /* init_dev */
273 sysfs_cleanup_dev
274 };