]> mj.ucw.cz Git - pciutils.git/blob - lib/proc.c
Preliminary support for PCI domains
[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       known = PCI_FILL_IDENT;
101       if (!a->buscentric)
102         {
103           known |= PCI_FILL_IRQ | PCI_FILL_BASES;
104           if (cnt >= 10)
105             known |= PCI_FILL_ROM_BASE;
106           if (cnt >= 17)
107             known |= PCI_FILL_SIZES;
108         }
109       d->known_fields = known;
110       pci_link_dev(a, d);
111     }
112   fclose(f);
113 }
114
115 static int
116 proc_setup(struct pci_dev *d, int rw)
117 {
118   struct pci_access *a = d->access;
119
120   if (a->cached_dev != d || a->fd_rw < rw)
121     {
122       char buf[1024];
123       int e;
124       if (a->fd >= 0)
125         close(a->fd);
126       e = snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d",
127                    a->method_params[PCI_ACCESS_PROC_BUS_PCI],
128                    d->bus, d->dev, d->func);
129       if (e < 0 || e >= (int) sizeof(buf))
130         a->error("File name too long");
131       a->fd_rw = a->writeable || rw;
132       a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
133       if (a->fd < 0)
134         a->warning("Cannot open %s", buf);
135       a->cached_dev = d;
136       a->fd_pos = 0;
137     }
138   return a->fd;
139 }
140
141 static int
142 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
143 {
144   int fd = proc_setup(d, 0);
145   int res;
146
147   if (fd < 0)
148     return 0;
149   res = do_read(d, fd, buf, len, pos);
150   if (res < 0)
151     {
152       d->access->warning("proc_read: read failed: %s", strerror(errno));
153       return 0;
154     }
155   else if (res != len)
156     {
157       d->access->warning("proc_read: tried to read %d bytes at %d, but got only %d", len, pos, res);
158       return 0;
159     }
160   return 1;
161 }
162
163 static int
164 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
165 {
166   int fd = proc_setup(d, 1);
167   int res;
168
169   if (fd < 0)
170     return 0;
171   res = do_write(d, fd, buf, len, pos);
172   if (res < 0)
173     {
174       d->access->warning("proc_write: write failed: %s", strerror(errno));
175       return 0;
176     }
177   else if (res != len)
178     {
179       d->access->warning("proc_write: tried to write %d bytes at %d, but got only %d", len, pos, res);
180       return 0;
181     }
182   return 1;
183 }
184
185 static void
186 proc_cleanup_dev(struct pci_dev *d)
187 {
188   if (d->access->cached_dev == d)
189     d->access->cached_dev = NULL;
190 }
191
192 struct pci_methods pm_linux_proc = {
193   "Linux-proc",
194   proc_config,
195   proc_detect,
196   proc_init,
197   proc_cleanup,
198   proc_scan,
199   pci_generic_fill_info,
200   proc_read,
201   proc_write,
202   NULL,                                 /* init_dev */
203   proc_cleanup_dev
204 };