2 * The PCI Library -- User Access
4 * Copyright (c) 1997--2022 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
19 pci_scan_bus(struct pci_access *a)
25 pci_alloc_dev(struct pci_access *a)
27 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
29 memset(d, 0, sizeof(*d));
31 d->methods = a->methods;
34 if (d->methods->init_dev)
35 d->methods->init_dev(d);
40 pci_link_dev(struct pci_access *a, struct pci_dev *d)
46 * Applications compiled with older versions of libpci do not expect
47 * 32-bit domain numbers. To keep them working, we keep a 16-bit
48 * version of the domain number at the previous location in struct
49 * pci_dev. This will keep backward compatibility on systems which
50 * don't require large domain numbers.
52 if (d->domain > 0xffff)
53 d->domain_16 = 0xffff;
55 d->domain_16 = d->domain;
61 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
63 struct pci_dev *d = pci_alloc_dev(a);
73 pci_free_properties(struct pci_dev *d)
75 struct pci_property *p;
77 while (p = d->properties)
79 d->properties = p->next;
84 void pci_free_dev(struct pci_dev *d)
86 if (d->methods->cleanup_dev)
87 d->methods->cleanup_dev(d);
90 pci_free_properties(d);
95 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
98 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
99 if (pos + len <= d->cache_len)
100 memcpy(buf, d->cache + pos, len);
101 else if (!d->methods->read(d, pos, buf, len))
102 memset(buf, 0xff, len);
106 pci_read_byte(struct pci_dev *d, int pos)
109 pci_read_data(d, &buf, pos, 1);
114 pci_read_word(struct pci_dev *d, int pos)
117 pci_read_data(d, &buf, pos, 2);
118 return le16_to_cpu(buf);
122 pci_read_long(struct pci_dev *d, int pos)
125 pci_read_data(d, &buf, pos, 4);
126 return le32_to_cpu(buf);
130 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
132 return d->methods->read(d, pos, buf, len);
136 pci_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
138 return d->methods->read_vpd ? d->methods->read_vpd(d, pos, buf, len) : 0;
142 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
145 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
146 if (pos + len <= d->cache_len)
147 memcpy(d->cache + pos, buf, len);
148 return d->methods->write(d, pos, buf, len);
152 pci_write_byte(struct pci_dev *d, int pos, byte data)
154 return pci_write_data(d, &data, pos, 1);
158 pci_write_word(struct pci_dev *d, int pos, word data)
160 word buf = cpu_to_le16(data);
161 return pci_write_data(d, &buf, pos, 2);
165 pci_write_long(struct pci_dev *d, int pos, u32 data)
167 u32 buf = cpu_to_le32(data);
168 return pci_write_data(d, &buf, pos, 4);
172 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
174 if (pos < d->cache_len)
176 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
177 memcpy(d->cache + pos, buf, l);
179 return d->methods->write(d, pos, buf, len);
183 pci_reset_properties(struct pci_dev *d)
187 d->module_alias = NULL;
190 pci_free_properties(d);
194 pci_fill_info_v38(struct pci_dev *d, int flags)
196 unsigned int uflags = flags;
197 if (uflags & PCI_FILL_RESCAN)
199 uflags &= ~PCI_FILL_RESCAN;
200 pci_reset_properties(d);
202 if (uflags & ~d->known_fields)
203 d->methods->fill_info(d, uflags);
204 return d->known_fields;
207 /* In version 3.1, pci_fill_info got new flags => versioned alias */
208 /* In versions 3.2, 3.3, 3.4, 3.5 and 3.8, the same has happened */
209 STATIC_ALIAS(int pci_fill_info(struct pci_dev *d, int flags), pci_fill_info_v38(d, flags));
210 DEFINE_ALIAS(int pci_fill_info_v30(struct pci_dev *d, int flags), pci_fill_info_v38);
211 DEFINE_ALIAS(int pci_fill_info_v31(struct pci_dev *d, int flags), pci_fill_info_v38);
212 DEFINE_ALIAS(int pci_fill_info_v32(struct pci_dev *d, int flags), pci_fill_info_v38);
213 DEFINE_ALIAS(int pci_fill_info_v33(struct pci_dev *d, int flags), pci_fill_info_v38);
214 DEFINE_ALIAS(int pci_fill_info_v34(struct pci_dev *d, int flags), pci_fill_info_v38);
215 DEFINE_ALIAS(int pci_fill_info_v35(struct pci_dev *d, int flags), pci_fill_info_v38);
216 SYMBOL_VERSION(pci_fill_info_v30, pci_fill_info@LIBPCI_3.0);
217 SYMBOL_VERSION(pci_fill_info_v31, pci_fill_info@LIBPCI_3.1);
218 SYMBOL_VERSION(pci_fill_info_v32, pci_fill_info@LIBPCI_3.2);
219 SYMBOL_VERSION(pci_fill_info_v33, pci_fill_info@LIBPCI_3.3);
220 SYMBOL_VERSION(pci_fill_info_v34, pci_fill_info@LIBPCI_3.4);
221 SYMBOL_VERSION(pci_fill_info_v35, pci_fill_info@LIBPCI_3.5);
222 SYMBOL_VERSION(pci_fill_info_v38, pci_fill_info@@LIBPCI_3.8);
225 pci_setup_cache(struct pci_dev *d, byte *cache, int len)
232 pci_set_property(struct pci_dev *d, u32 key, char *value)
234 struct pci_property *p;
235 struct pci_property **pp = &d->properties;
251 p = pci_malloc(d->access, sizeof(*p) + strlen(value));
255 strcpy(p->value, value);
261 pci_get_string_property(struct pci_dev *d, u32 prop)
263 struct pci_property *p;
265 for (p = d->properties; p; p = p->next)