]> mj.ucw.cz Git - pciutils.git/blob - lib/access.c
9bd69891dde5c082ce1d81fe250248d1a8c153d2
[pciutils.git] / lib / access.c
1 /*
2  *      The PCI Library -- User Access
3  *
4  *      Copyright (c) 1997--2022 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13
14 #include "internal.h"
15
16 void
17 pci_scan_bus(struct pci_access *a)
18 {
19   a->methods->scan(a);
20 }
21
22 struct pci_dev *
23 pci_alloc_dev(struct pci_access *a)
24 {
25   struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
26
27   memset(d, 0, sizeof(*d));
28   d->access = a;
29   d->methods = a->methods;
30   d->hdrtype = -1;
31   d->numa_node = -1;
32   if (d->methods->init_dev)
33     d->methods->init_dev(d);
34   return d;
35 }
36
37 int
38 pci_link_dev(struct pci_access *a, struct pci_dev *d)
39 {
40   d->next = a->devices;
41   a->devices = d;
42
43   /*
44    * Applications compiled with older versions of libpci do not expect
45    * 32-bit domain numbers. To keep them working, we keep a 16-bit
46    * version of the domain number at the previous location in struct
47    * pci_dev. This will keep backward compatibility on systems which
48    * don't require large domain numbers.
49    */
50   if (d->domain > 0xffff)
51     d->domain_16 = 0xffff;
52   else
53     d->domain_16 = d->domain;
54
55   return 1;
56 }
57
58 struct pci_dev *
59 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
60 {
61   struct pci_dev *d = pci_alloc_dev(a);
62
63   d->domain = domain;
64   d->bus = bus;
65   d->dev = dev;
66   d->func = func;
67   return d;
68 }
69
70 static void
71 pci_free_properties(struct pci_dev *d)
72 {
73   struct pci_property *p;
74
75   while (p = d->properties)
76     {
77       d->properties = p->next;
78       pci_mfree(p);
79     }
80 }
81
82 void pci_free_dev(struct pci_dev *d)
83 {
84   if (d->methods->cleanup_dev)
85     d->methods->cleanup_dev(d);
86
87   pci_free_caps(d);
88   pci_free_properties(d);
89   pci_mfree(d);
90 }
91
92 static inline void
93 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
94 {
95   if (pos & (len-1))
96     d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
97   if (pos + len <= d->cache_len)
98     memcpy(buf, d->cache + pos, len);
99   else if (!d->methods->read(d, pos, buf, len))
100     memset(buf, 0xff, len);
101 }
102
103 byte
104 pci_read_byte(struct pci_dev *d, int pos)
105 {
106   byte buf;
107   pci_read_data(d, &buf, pos, 1);
108   return buf;
109 }
110
111 word
112 pci_read_word(struct pci_dev *d, int pos)
113 {
114   word buf;
115   pci_read_data(d, &buf, pos, 2);
116   return le16_to_cpu(buf);
117 }
118
119 u32
120 pci_read_long(struct pci_dev *d, int pos)
121 {
122   u32 buf;
123   pci_read_data(d, &buf, pos, 4);
124   return le32_to_cpu(buf);
125 }
126
127 int
128 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
129 {
130   return d->methods->read(d, pos, buf, len);
131 }
132
133 int
134 pci_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
135 {
136   return d->methods->read_vpd ? d->methods->read_vpd(d, pos, buf, len) : 0;
137 }
138
139 static inline int
140 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
141 {
142   if (pos & (len-1))
143     d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
144   if (pos + len <= d->cache_len)
145     memcpy(d->cache + pos, buf, len);
146   return d->methods->write(d, pos, buf, len);
147 }
148
149 int
150 pci_write_byte(struct pci_dev *d, int pos, byte data)
151 {
152   return pci_write_data(d, &data, pos, 1);
153 }
154
155 int
156 pci_write_word(struct pci_dev *d, int pos, word data)
157 {
158   word buf = cpu_to_le16(data);
159   return pci_write_data(d, &buf, pos, 2);
160 }
161
162 int
163 pci_write_long(struct pci_dev *d, int pos, u32 data)
164 {
165   u32 buf = cpu_to_le32(data);
166   return pci_write_data(d, &buf, pos, 4);
167 }
168
169 int
170 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
171 {
172   if (pos < d->cache_len)
173     {
174       int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
175       memcpy(d->cache + pos, buf, l);
176     }
177   return d->methods->write(d, pos, buf, len);
178 }
179
180 static void
181 pci_reset_properties(struct pci_dev *d)
182 {
183   d->known_fields = 0;
184   d->phy_slot = NULL;
185   d->module_alias = NULL;
186   d->label = NULL;
187   pci_free_caps(d);
188   pci_free_properties(d);
189 }
190
191 int
192 pci_fill_info_v35(struct pci_dev *d, int flags)
193 {
194   unsigned int uflags = flags;
195   if (uflags & PCI_FILL_RESCAN)
196     {
197       uflags &= ~PCI_FILL_RESCAN;
198       pci_reset_properties(d);
199     }
200   if (uflags & ~d->known_fields)
201     d->methods->fill_info(d, uflags);
202   return d->known_fields;
203 }
204
205 /* In version 3.1, pci_fill_info got new flags => versioned alias */
206 /* In versions 3.2, 3.3, 3.4 and 3.5, the same has happened */
207 STATIC_ALIAS(int pci_fill_info(struct pci_dev *d, int flags), pci_fill_info_v35(d, flags));
208 DEFINE_ALIAS(int pci_fill_info_v30(struct pci_dev *d, int flags), pci_fill_info_v35);
209 DEFINE_ALIAS(int pci_fill_info_v31(struct pci_dev *d, int flags), pci_fill_info_v35);
210 DEFINE_ALIAS(int pci_fill_info_v32(struct pci_dev *d, int flags), pci_fill_info_v35);
211 DEFINE_ALIAS(int pci_fill_info_v33(struct pci_dev *d, int flags), pci_fill_info_v35);
212 DEFINE_ALIAS(int pci_fill_info_v34(struct pci_dev *d, int flags), pci_fill_info_v35);
213 SYMBOL_VERSION(pci_fill_info_v30, pci_fill_info@LIBPCI_3.0);
214 SYMBOL_VERSION(pci_fill_info_v31, pci_fill_info@LIBPCI_3.1);
215 SYMBOL_VERSION(pci_fill_info_v32, pci_fill_info@LIBPCI_3.2);
216 SYMBOL_VERSION(pci_fill_info_v33, pci_fill_info@LIBPCI_3.3);
217 SYMBOL_VERSION(pci_fill_info_v34, pci_fill_info@LIBPCI_3.4);
218 SYMBOL_VERSION(pci_fill_info_v35, pci_fill_info@@LIBPCI_3.5);
219
220 void
221 pci_setup_cache(struct pci_dev *d, byte *cache, int len)
222 {
223   d->cache = cache;
224   d->cache_len = len;
225 }
226
227 char *
228 pci_set_property(struct pci_dev *d, u32 key, char *value)
229 {
230   struct pci_property *p;
231   struct pci_property **pp = &d->properties;
232
233   while (p = *pp)
234     {
235       if (p->key == key)
236         {
237           *pp = p->next;
238           pci_mfree(p);
239         }
240       else
241         pp = &p->next;
242     }
243
244   if (!value)
245     return NULL;
246
247   p = pci_malloc(d->access, sizeof(*p) + strlen(value));
248   *pp = p;
249   p->next = NULL;
250   p->key = key;
251   strcpy(p->value, value);
252
253   return p->value;
254 }
255
256 char *
257 pci_get_string_property(struct pci_dev *d, u32 prop)
258 {
259   struct pci_property *p;
260
261   for (p = d->properties; p; p = p->next)
262     if (p->key == prop)
263       return p->value;
264
265   return NULL;
266 }