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