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