]> mj.ucw.cz Git - pciutils.git/blob - lib/proc.c
d18a0dbf655319311e856e0d46bb7cfb37e0e42e
[pciutils.git] / lib / proc.c
1 /*
2  *      The PCI Library -- Configuration Access via /proc/bus/pci
3  *
4  *      Copyright (c) 1997--1999 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #define _GNU_SOURCE
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <sys/types.h>
17
18 #include "internal.h"
19
20 /*
21  *  We'd like to use pread/pwrite for configuration space accesses, but
22  *  unfortunately it isn't simple at all since all libc's until glibc 2.1
23  *  don't define it.
24  */
25
26 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
27 /* glibc 2.1 or newer -> pread/pwrite supported automatically */
28
29 #elif defined(i386) && defined(__GLIBC__)
30 /* glibc 2.0 on i386 -> call syscalls directly */
31 #include <asm/unistd.h>
32 #include <syscall-list.h>
33 #ifndef SYS_pread
34 #define SYS_pread 180
35 #endif
36 static int pread(unsigned int fd, void *buf, size_t size, loff_t where)
37 { return syscall(SYS_pread, fd, buf, size, where); }
38 #ifndef SYS_pwrite
39 #define SYS_pwrite 181
40 #endif
41 static int pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
42 { return syscall(SYS_pwrite, fd, buf, size, where); }
43
44 #elif defined(i386)
45 /* old libc on i386 -> call syscalls directly the old way */
46 #include <asm/unistd.h>
47 static _syscall5(int, pread, unsigned int, fd, void *, buf, size_t, size, u32, where_lo, u32, where_hi);
48 static _syscall5(int, pwrite, unsigned int, fd, void *, buf, size_t, size, u32, where_lo, u32, where_hi);
49 static int do_read(struct pci_dev *d UNUSED, int fd, void *buf, size_t size, int where) { return pread(fd, buf, size, where, 0); }
50 static int do_write(struct pci_dev *d UNUSED, int fd, void *buf, size_t size, int where) { return pwrite(fd, buf, size, where, 0); }
51 #define HAVE_DO_READ
52
53 #else
54 /* In all other cases we use lseek/read/write instead to be safe */
55 #define make_rw_glue(op) \
56         static int do_##op(struct pci_dev *d, int fd, void *buf, size_t size, int where)        \
57         {                                                                                       \
58           struct pci_access *a = d->access;                                                     \
59           int r;                                                                                \
60           if (a->fd_pos != where && lseek(fd, where, SEEK_SET) < 0)                             \
61             return -1;                                                                          \
62           r = op(fd, buf, size);                                                                \
63           if (r < 0)                                                                            \
64             a->fd_pos = -1;                                                                     \
65           else                                                                                  \
66             a->fd_pos = where + r;                                                              \
67           return r;                                                                             \
68         }
69 make_rw_glue(read)
70 make_rw_glue(write)
71 #define HAVE_DO_READ
72 #endif
73
74 #ifndef HAVE_DO_READ
75 #define do_read(d,f,b,l,p) pread(f,b,l,p)
76 #define do_write(d,f,b,l,p) pwrite(f,b,l,p)
77 #endif
78
79 static void
80 proc_config(struct pci_access *a)
81 {
82   a->method_params[PCI_ACCESS_PROC_BUS_PCI] = PATH_PROC_BUS_PCI;
83 }
84
85 static int
86 proc_detect(struct pci_access *a)
87 {
88   char *name = a->method_params[PCI_ACCESS_PROC_BUS_PCI];
89
90   if (access(name, R_OK))
91     {
92       a->warning("Cannot open %s", name);
93       return 0;
94     }
95   a->debug("...using %s", name);
96   return 1;
97 }
98
99 static void
100 proc_init(struct pci_access *a)
101 {
102   a->fd = -1;
103 }
104
105 static void
106 proc_cleanup(struct pci_access *a)
107 {
108   if (a->fd >= 0)
109     {
110       close(a->fd);
111       a->fd = -1;
112     }
113 }
114
115 static void
116 proc_scan(struct pci_access *a)
117 {
118   FILE *f;
119   char buf[512];
120
121   if (snprintf(buf, sizeof(buf), "%s/devices", a->method_params[PCI_ACCESS_PROC_BUS_PCI]) == sizeof(buf))
122     a->error("File name too long");
123   f = fopen(buf, "r");
124   if (!f)
125     a->error("Cannot open %s", buf);
126   while (fgets(buf, sizeof(buf)-1, f))
127     {
128       struct pci_dev *d = pci_alloc_dev(a);
129       unsigned int dfn, vend, cnt, known;
130
131       cnt = sscanf(buf,
132 #ifdef HAVE_LONG_ADDRESS
133              "%x %x %x %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx",
134 #else
135              "%x %x %x %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx",
136 #endif
137              &dfn,
138              &vend,
139              &d->irq,
140              &d->base_addr[0],
141              &d->base_addr[1],
142              &d->base_addr[2],
143              &d->base_addr[3],
144              &d->base_addr[4],
145              &d->base_addr[5],
146              &d->rom_base_addr,
147              &d->size[0],
148              &d->size[1],
149              &d->size[2],
150              &d->size[3],
151              &d->size[4],
152              &d->size[5],
153              &d->rom_size);
154       if (cnt != 9 && cnt != 10 && cnt != 17)
155         a->error("proc: parse error (read only %d items)", cnt);
156       d->bus = dfn >> 8U;
157       d->dev = PCI_SLOT(dfn & 0xff);
158       d->func = PCI_FUNC(dfn & 0xff);
159       d->vendor_id = vend >> 16U;
160       d->device_id = vend & 0xffff;
161       d->hdrtype = pci_read_byte(d, PCI_HEADER_TYPE);
162       known = PCI_FILL_IDENT;
163       if (!a->buscentric)
164         {
165           known |= PCI_FILL_IRQ | PCI_FILL_BASES;
166           if (cnt >= 10)
167             known |= PCI_FILL_ROM_BASE;
168           if (cnt >= 17)
169             known |= PCI_FILL_SIZES;
170         }
171       d->known_fields = known;
172       pci_link_dev(a, d);
173     }
174   fclose(f);
175 }
176
177 static int
178 proc_setup(struct pci_dev *d, int rw)
179 {
180   struct pci_access *a = d->access;
181
182   if (a->cached_dev != d || a->fd_rw < rw)
183     {
184       char buf[256];
185       if (a->fd >= 0)
186         close(a->fd);
187       if (snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d", a->method_params[PCI_ACCESS_PROC_BUS_PCI],
188                    d->bus, d->dev, d->func) == sizeof(buf))
189         a->error("File name too long");
190       a->fd_rw = a->writeable || rw;
191       a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
192       if (a->fd < 0)
193         a->warning("Cannot open %s", buf);
194       a->cached_dev = d;
195       a->fd_pos = 0;
196     }
197   return a->fd;
198 }
199
200 static int
201 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
202 {
203   int fd = proc_setup(d, 0);
204   int res;
205
206   if (fd < 0)
207     return 0;
208   res = do_read(d, fd, buf, len, pos);
209   if (res < 0)
210     {
211       d->access->warning("proc_read: read failed: %s", strerror(errno));
212       return 0;
213     }
214   else if (res != len)
215     {
216       d->access->warning("proc_read: tried to read %d bytes at %d, but got only %d", len, pos, res);
217       return 0;
218     }
219   return 1;
220 }
221
222 static int
223 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
224 {
225   int fd = proc_setup(d, 1);
226   int res;
227
228   if (fd < 0)
229     return 0;
230   res = do_write(d, fd, buf, len, pos);
231   if (res < 0)
232     {
233       d->access->warning("proc_write: write failed: %s", strerror(errno));
234       return 0;
235     }
236   else if (res != len)
237     {
238       d->access->warning("proc_write: tried to write %d bytes at %d, but got only %d", len, pos, res);
239       return 0;
240     }
241   return 1;
242 }
243
244 static void
245 proc_cleanup_dev(struct pci_dev *d)
246 {
247   if (d->access->cached_dev == d)
248     d->access->cached_dev = NULL;
249 }
250
251 struct pci_methods pm_linux_proc = {
252   "/proc/bus/pci",
253   proc_config,
254   proc_detect,
255   proc_init,
256   proc_cleanup,
257   proc_scan,
258   pci_generic_fill_info,
259   proc_read,
260   proc_write,
261   NULL,                                 /* init_dev */
262   proc_cleanup_dev
263 };