]> mj.ucw.cz Git - pciutils.git/blob - lib/proc.c
Added sysfs support
[pciutils.git] / lib / proc.c
1 /*
2  *      The PCI Library -- Configuration Access via /proc/bus/pci
3  *
4  *      Copyright (c) 1997--2003 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 #include "pread.h"
20
21 static void
22 proc_config(struct pci_access *a)
23 {
24   a->method_params[PCI_ACCESS_PROC_BUS_PCI] = PATH_PROC_BUS_PCI;
25 }
26
27 static int
28 proc_detect(struct pci_access *a)
29 {
30   char *name = a->method_params[PCI_ACCESS_PROC_BUS_PCI];
31
32   if (access(name, R_OK))
33     {
34       a->warning("Cannot open %s", name);
35       return 0;
36     }
37   a->debug("...using %s", name);
38   return 1;
39 }
40
41 static void
42 proc_init(struct pci_access *a)
43 {
44   a->fd = -1;
45 }
46
47 static void
48 proc_cleanup(struct pci_access *a)
49 {
50   if (a->fd >= 0)
51     {
52       close(a->fd);
53       a->fd = -1;
54     }
55 }
56
57 static void
58 proc_scan(struct pci_access *a)
59 {
60   FILE *f;
61   char buf[512];
62
63   if (snprintf(buf, sizeof(buf), "%s/devices", a->method_params[PCI_ACCESS_PROC_BUS_PCI]) == sizeof(buf))
64     a->error("File name too long");
65   f = fopen(buf, "r");
66   if (!f)
67     a->error("Cannot open %s", buf);
68   while (fgets(buf, sizeof(buf)-1, f))
69     {
70       struct pci_dev *d = pci_alloc_dev(a);
71       unsigned int dfn, vend, cnt, known;
72
73 #define F " " PCIADDR_T_FMT
74       cnt = sscanf(buf, "%x %x %x" F F F F F F F F F F F F F F,
75              &dfn,
76              &vend,
77              &d->irq,
78              &d->base_addr[0],
79              &d->base_addr[1],
80              &d->base_addr[2],
81              &d->base_addr[3],
82              &d->base_addr[4],
83              &d->base_addr[5],
84              &d->rom_base_addr,
85              &d->size[0],
86              &d->size[1],
87              &d->size[2],
88              &d->size[3],
89              &d->size[4],
90              &d->size[5],
91              &d->rom_size);
92 #undef F
93       if (cnt != 9 && cnt != 10 && cnt != 17)
94         a->error("proc: parse error (read only %d items)", cnt);
95       d->bus = dfn >> 8U;
96       d->dev = PCI_SLOT(dfn & 0xff);
97       d->func = PCI_FUNC(dfn & 0xff);
98       d->vendor_id = vend >> 16U;
99       d->device_id = vend & 0xffff;
100       d->hdrtype = pci_read_byte(d, PCI_HEADER_TYPE) & 0x7f;
101       known = PCI_FILL_IDENT;
102       if (!a->buscentric)
103         {
104           known |= PCI_FILL_IRQ | PCI_FILL_BASES;
105           if (cnt >= 10)
106             known |= PCI_FILL_ROM_BASE;
107           if (cnt >= 17)
108             known |= PCI_FILL_SIZES;
109         }
110       d->known_fields = known;
111       pci_link_dev(a, d);
112     }
113   fclose(f);
114 }
115
116 static int
117 proc_setup(struct pci_dev *d, int rw)
118 {
119   struct pci_access *a = d->access;
120
121   if (a->cached_dev != d || a->fd_rw < rw)
122     {
123       char buf[1024];
124       int e;
125       if (a->fd >= 0)
126         close(a->fd);
127       e = snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d",
128                    a->method_params[PCI_ACCESS_PROC_BUS_PCI],
129                    d->bus, d->dev, d->func);
130       if (e < 0 || e >= (int) sizeof(buf))
131         a->error("File name too long");
132       a->fd_rw = a->writeable || rw;
133       a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
134       if (a->fd < 0)
135         a->warning("Cannot open %s", buf);
136       a->cached_dev = d;
137       a->fd_pos = 0;
138     }
139   return a->fd;
140 }
141
142 static int
143 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
144 {
145   int fd = proc_setup(d, 0);
146   int res;
147
148   if (fd < 0)
149     return 0;
150   res = do_read(d, fd, buf, len, pos);
151   if (res < 0)
152     {
153       d->access->warning("proc_read: read failed: %s", strerror(errno));
154       return 0;
155     }
156   else if (res != len)
157     {
158       d->access->warning("proc_read: tried to read %d bytes at %d, but got only %d", len, pos, res);
159       return 0;
160     }
161   return 1;
162 }
163
164 static int
165 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
166 {
167   int fd = proc_setup(d, 1);
168   int res;
169
170   if (fd < 0)
171     return 0;
172   res = do_write(d, fd, buf, len, pos);
173   if (res < 0)
174     {
175       d->access->warning("proc_write: write failed: %s", strerror(errno));
176       return 0;
177     }
178   else if (res != len)
179     {
180       d->access->warning("proc_write: tried to write %d bytes at %d, but got only %d", len, pos, res);
181       return 0;
182     }
183   return 1;
184 }
185
186 static void
187 proc_cleanup_dev(struct pci_dev *d)
188 {
189   if (d->access->cached_dev == d)
190     d->access->cached_dev = NULL;
191 }
192
193 struct pci_methods pm_linux_proc = {
194   "Linux-proc",
195   proc_config,
196   proc_detect,
197   proc_init,
198   proc_cleanup,
199   proc_scan,
200   pci_generic_fill_info,
201   proc_read,
202   proc_write,
203   NULL,                                 /* init_dev */
204   proc_cleanup_dev
205 };