]> mj.ucw.cz Git - pciutils.git/blob - lib/proc.c
Various PCIutils changes accumulated over last two weeks:
[pciutils.git] / lib / proc.c
1 /*
2  *      $Id: proc.c,v 1.4 1999/07/07 11:23:12 mj 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 #include <asm/unistd.h>
23 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 1
24 #include <syscall-list.h>
25 #endif
26
27 /*
28  * As libc doesn't support pread/pwrite yet, we have to call them directly
29  * or use lseek/read/write instead.
30  */
31 #if !(defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0)
32
33 #if defined(__GLIBC__) && !(defined(__powerpc__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
34 #ifndef SYS_pread
35 #define SYS_pread 180
36 #endif
37 static int
38 pread(unsigned int fd, void *buf, size_t size, loff_t where)
39 {
40   return syscall(SYS_pread, fd, buf, size, where);
41 }
42
43 #ifndef SYS_pwrite
44 #define SYS_pwrite 181
45 #endif
46 static int
47 pwrite(unsigned int fd, void *buf, size_t size, loff_t where)
48 {
49   return syscall(SYS_pwrite, fd, buf, size, where);
50 }
51 #else
52 static _syscall4(int, pread, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
53 static _syscall4(int, pwrite, unsigned int, fd, void *, buf, size_t, size, loff_t, where);
54 #endif
55
56 #endif
57
58 static void
59 proc_config(struct pci_access *a)
60 {
61   a->method_params[PCI_ACCESS_PROC_BUS_PCI] = PATH_PROC_BUS_PCI;
62 }
63
64 static int
65 proc_detect(struct pci_access *a)
66 {
67   char *name = a->method_params[PCI_ACCESS_PROC_BUS_PCI];
68
69   if (access(name, R_OK))
70     {
71       a->warning("Cannot open %s", name);
72       return 0;
73     }
74   a->debug("...using %s", name);
75   return 1;
76 }
77
78 static void
79 proc_init(struct pci_access *a)
80 {
81   a->fd = -1;
82 }
83
84 static void
85 proc_cleanup(struct pci_access *a)
86 {
87   if (a->fd >= 0)
88     {
89       close(a->fd);
90       a->fd = -1;
91     }
92 }
93
94 static void
95 proc_scan(struct pci_access *a)
96 {
97   FILE *f;
98   char buf[256];
99
100   if (snprintf(buf, sizeof(buf), "%s/devices", a->method_params[PCI_ACCESS_PROC_BUS_PCI]) == sizeof(buf))
101     a->error("File name too long");
102   f = fopen(buf, "r");
103   if (!f)
104     a->error("Cannot open %s", buf);
105   while (fgets(buf, sizeof(buf)-1, f))
106     {
107       struct pci_dev *d = pci_alloc_dev(a);
108       unsigned int dfn, vend, cnt, known;
109
110       cnt = sscanf(buf,
111 #ifdef HAVE_LONG_ADDRESS
112              "%x %x %x %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx %llx",
113 #else
114              "%x %x %x %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx %lx",
115 #endif
116              &dfn,
117              &vend,
118              &d->irq,
119              &d->base_addr[0],
120              &d->base_addr[1],
121              &d->base_addr[2],
122              &d->base_addr[3],
123              &d->base_addr[4],
124              &d->base_addr[5],
125              &d->rom_base_addr,
126              &d->size[0],
127              &d->size[1],
128              &d->size[2],
129              &d->size[3],
130              &d->size[4],
131              &d->size[5],
132              &d->rom_size);
133       if (cnt != 9 && cnt != 10 && cnt != 17)
134         a->error("proc: parse error (read only %d items)", cnt);
135       d->bus = dfn >> 8U;
136       d->dev = PCI_SLOT(dfn & 0xff);
137       d->func = PCI_FUNC(dfn & 0xff);
138       d->vendor_id = vend >> 16U;
139       d->device_id = vend & 0xffff;
140       known = PCI_FILL_IDENT;
141       if (!a->buscentric)
142         {
143           known |= PCI_FILL_IRQ | PCI_FILL_BASES;
144           if (cnt >= 10)
145             known |= PCI_FILL_ROM_BASE;
146           if (cnt >= 17)
147             known |= PCI_FILL_SIZES;
148         }
149       d->known_fields = known;
150       pci_link_dev(a, d);
151     }
152   fclose(f);
153 }
154
155 static int
156 proc_setup(struct pci_dev *d, int rw)
157 {
158   struct pci_access *a = d->access;
159
160   if (a->cached_dev != d || a->fd_rw < rw)
161     {
162       char buf[256];
163       if (a->fd >= 0)
164         close(a->fd);
165       if (snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d", a->method_params[PCI_ACCESS_PROC_BUS_PCI],
166                    d->bus, d->dev, d->func) == sizeof(buf))
167         a->error("File name too long");
168       a->fd_rw = a->writeable || rw;
169       a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
170       if (a->fd < 0)
171         a->warning("Cannot open %s", buf);
172       a->cached_dev = d;
173     }
174   return a->fd;
175 }
176
177 static int
178 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
179 {
180   int fd = proc_setup(d, 0);
181   int res;
182
183   if (fd < 0)
184     return 0;
185   res = pread(fd, buf, len, pos);
186   if (res < 0)
187     {
188       d->access->warning("proc_read: read failed: %s", strerror(errno));
189       return 0;
190     }
191   else if (res != len)
192     {
193       d->access->warning("proc_read: tried to read %d bytes at %d, but got only %d", len, pos, res);
194       return 0;
195     }
196   return 1;
197 }
198
199 static int
200 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
201 {
202   int fd = proc_setup(d, 1);
203   int res;
204
205   if (fd < 0)
206     return 0;
207   res = pwrite(fd, buf, len, pos);
208   if (res < 0)
209     {
210       d->access->warning("proc_write: write failed: %s", strerror(errno));
211       return 0;
212     }
213   else if (res != len)
214     {
215       d->access->warning("proc_write: tried to write %d bytes at %d, but got only %d", len, pos, res);
216       return 0;
217     }
218   return 1;
219 }
220
221 static void
222 proc_cleanup_dev(struct pci_dev *d)
223 {
224   if (d->access->cached_dev == d)
225     d->access->cached_dev = NULL;
226 }
227
228 struct pci_methods pm_linux_proc = {
229   "/proc/bus/pci",
230   proc_config,
231   proc_detect,
232   proc_init,
233   proc_cleanup,
234   proc_scan,
235   pci_generic_fill_info,
236   proc_read,
237   proc_write,
238   NULL,                                 /* init_dev */
239   proc_cleanup_dev
240 };