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