]> mj.ucw.cz Git - pciutils.git/blob - lib/proc.c
update-pciids: Add support for xz compression
[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 v2+.
7  *
8  *      SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #define _GNU_SOURCE
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <ctype.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <sys/types.h>
20
21 #include "internal.h"
22 #include "pread.h"
23
24 static void
25 proc_config(struct pci_access *a)
26 {
27   pci_define_param(a, "proc.path", PCI_PATH_PROC_BUS_PCI, "Path to the procfs bus tree");
28 }
29
30 static int
31 proc_detect(struct pci_access *a)
32 {
33   char *name = pci_get_param(a, "proc.path");
34
35   if (access(name, R_OK))
36     {
37       a->warning("Cannot open %s", name);
38       return 0;
39     }
40   a->debug("...using %s", name);
41   return 1;
42 }
43
44 static void
45 proc_init(struct pci_access *a)
46 {
47   a->fd = -1;
48 }
49
50 static void
51 proc_cleanup(struct pci_access *a)
52 {
53   if (a->fd >= 0)
54     {
55       close(a->fd);
56       a->fd = -1;
57     }
58 }
59
60 static void
61 proc_scan(struct pci_access *a)
62 {
63   FILE *f;
64   char buf[512];
65
66   if (snprintf(buf, sizeof(buf), "%s/devices", pci_get_param(a, "proc.path")) == sizeof(buf))
67     a->error("File name too long");
68   f = fopen(buf, "r");
69   if (!f)
70     a->error("Cannot open %s", buf);
71   while (fgets(buf, sizeof(buf)-1, f))
72     {
73       struct pci_dev *d = pci_alloc_dev(a);
74       unsigned int dfn, vend, cnt, known;
75       char *driver;
76       int offset;
77
78 #define F " " PCIADDR_T_FMT
79       cnt = sscanf(buf, "%x %x %x" F F F F F F F F F F F F F F "%n",
80              &dfn,
81              &vend,
82              &d->irq,
83              &d->base_addr[0],
84              &d->base_addr[1],
85              &d->base_addr[2],
86              &d->base_addr[3],
87              &d->base_addr[4],
88              &d->base_addr[5],
89              &d->rom_base_addr,
90              &d->size[0],
91              &d->size[1],
92              &d->size[2],
93              &d->size[3],
94              &d->size[4],
95              &d->size[5],
96              &d->rom_size,
97              &offset);
98 #undef F
99       if (cnt != 9 && cnt != 10 && cnt != 17)
100         a->error("proc: parse error (read only %d items)", cnt);
101       d->bus = dfn >> 8U;
102       d->dev = PCI_SLOT(dfn & 0xff);
103       d->func = PCI_FUNC(dfn & 0xff);
104       d->vendor_id = vend >> 16U;
105       d->device_id = vend & 0xffff;
106       known = 0;
107       if (!a->buscentric)
108         {
109           known |= PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES;
110           if (cnt >= 10)
111             known |= PCI_FILL_ROM_BASE;
112           if (cnt >= 17)
113             known |= PCI_FILL_SIZES;
114         }
115       if (cnt >= 17)
116         {
117           while (buf[offset] && isspace(buf[offset]))
118             ++offset;
119           driver = &buf[offset];
120           while (buf[offset] && !isspace(buf[offset]))
121             ++offset;
122           buf[offset] = '\0';
123           if (driver[0])
124             {
125               pci_set_property(d, PCI_FILL_DRIVER, driver);
126               known |= PCI_FILL_DRIVER;
127             }
128         }
129       d->known_fields = known;
130       pci_link_dev(a, d);
131     }
132   fclose(f);
133 }
134
135 static int
136 proc_setup(struct pci_dev *d, int rw)
137 {
138   struct pci_access *a = d->access;
139
140   if (a->cached_dev != d || a->fd_rw < rw)
141     {
142       char buf[1024];
143       int e;
144       if (a->fd >= 0)
145         close(a->fd);
146       e = snprintf(buf, sizeof(buf), "%s/%02x/%02x.%d",
147                    pci_get_param(a, "proc.path"),
148                    d->bus, d->dev, d->func);
149       if (e < 0 || e >= (int) 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         {
155           e = snprintf(buf, sizeof(buf), "%s/%04x:%02x/%02x.%d",
156                        pci_get_param(a, "proc.path"),
157                        d->domain, d->bus, d->dev, d->func);
158           if (e < 0 || e >= (int) sizeof(buf))
159             a->error("File name too long");
160           a->fd = open(buf, a->fd_rw ? O_RDWR : O_RDONLY);
161         }
162       if (a->fd < 0)
163         a->warning("Cannot open %s", buf);
164       a->cached_dev = d;
165       a->fd_pos = 0;
166     }
167   return a->fd;
168 }
169
170 static int
171 proc_read(struct pci_dev *d, int pos, byte *buf, int len)
172 {
173   int fd = proc_setup(d, 0);
174   int res;
175
176   if (fd < 0)
177     return 0;
178   res = do_read(d, fd, buf, len, pos);
179   if (res < 0)
180     {
181       d->access->warning("proc_read: read failed: %s", strerror(errno));
182       return 0;
183     }
184   else if (res != len)
185     return 0;
186   return 1;
187 }
188
189 static int
190 proc_write(struct pci_dev *d, int pos, byte *buf, int len)
191 {
192   int fd = proc_setup(d, 1);
193   int res;
194
195   if (fd < 0)
196     return 0;
197   res = do_write(d, fd, buf, len, pos);
198   if (res < 0)
199     {
200       d->access->warning("proc_write: write failed: %s", strerror(errno));
201       return 0;
202     }
203   else if (res != len)
204     {
205       d->access->warning("proc_write: tried to write %d bytes at %d, but only %d succeeded", len, pos, res);
206       return 0;
207     }
208   return 1;
209 }
210
211 static void
212 proc_cleanup_dev(struct pci_dev *d)
213 {
214   if (d->access->cached_dev == d)
215     d->access->cached_dev = NULL;
216 }
217
218 struct pci_methods pm_linux_proc = {
219   "linux-proc",
220   "The proc file system on Linux",
221   proc_config,
222   proc_detect,
223   proc_init,
224   proc_cleanup,
225   proc_scan,
226   pci_generic_fill_info,
227   proc_read,
228   proc_write,
229   NULL,                                 /* read_vpd */
230   NULL,                                 /* init_dev */
231   proc_cleanup_dev
232 };