]> mj.ucw.cz Git - pciutils.git/blob - lib/proc.c
87b06317508b84f3c7804886828e73e2c815c46e
[pciutils.git] / lib / proc.c
1 /*
2  *      $Id: proc.c,v 1.2 1999/04/18 19:08: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 __NR_pread
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 __NR_pwrite
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;
109
110       sscanf(buf,
111 #ifdef HAVE_64BIT_ADDRESS
112              "%x %x %x %llx %llx %llx %llx %llx %llx %llx",
113 #else
114              "%x %x %x %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->bus = dfn >> 8U;
127       d->dev = PCI_SLOT(dfn & 0xff);
128       d->func = PCI_FUNC(dfn & 0xff);
129       d->vendor_id = vend >> 16U;
130       d->device_id = vend & 0xffff;
131       d->known_fields = a->buscentric ? PCI_FILL_IDENT
132                                       : (PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE);
133       pci_link_dev(a, d);
134     }
135   fclose(f);
136 }
137
138 static int
139 proc_setup(struct pci_dev *d, int rw)
140 {
141   struct pci_access *a = d->access;
142
143   if (a->cached_dev != d || a->fd_rw < rw)
144     {
145       char buf[256];
146       if (a->fd >= 0)
147         close(a->fd);
148       if (snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d", a->method_params[PCI_ACCESS_PROC_BUS_PCI],
149                    d->bus, d->dev, d->func) == sizeof(buf))
150         a->error("File name too long");
151       a->fd_rw = a->writeable || rw;
152       a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
153       if (a->fd < 0)
154         a->warning("Cannot open %s", buf);
155       a->cached_dev = d;
156     }
157   return a->fd;
158 }
159
160 static int
161 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
162 {
163   int fd = proc_setup(d, 0);
164   int res;
165
166   if (fd < 0)
167     return 0;
168   res = pread(fd, buf, len, pos);
169   if (res < 0)
170     {
171       d->access->warning("proc_read: read failed: %s", strerror(errno));
172       return 0;
173     }
174   else if (res != len)
175     {
176       d->access->warning("proc_read: tried to read %d bytes at %d, but got only %d", len, pos, res);
177       return 0;
178     }
179   return 1;
180 }
181
182 static int
183 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
184 {
185   int fd = proc_setup(d, 1);
186   int res;
187
188   if (fd < 0)
189     return 0;
190   res = pwrite(fd, buf, len, pos);
191   if (res < 0)
192     {
193       d->access->warning("proc_write: write failed: %s", strerror(errno));
194       return 0;
195     }
196   else if (res != len)
197     {
198       d->access->warning("proc_write: tried to write %d bytes at %d, but got only %d", len, pos, res);
199       return 0;
200     }
201   return 1;
202 }
203
204 static void
205 proc_cleanup_dev(struct pci_dev *d)
206 {
207   if (d->access->cached_dev == d)
208     d->access->cached_dev = NULL;
209 }
210
211 struct pci_methods pm_linux_proc = {
212   "/proc/bus/pci",
213   proc_config,
214   proc_detect,
215   proc_init,
216   proc_cleanup,
217   proc_scan,
218   pci_generic_fill_info,
219   proc_read,
220   proc_write,
221   NULL,                                 /* init_dev */
222   proc_cleanup_dev
223 };