2 * The PCI Library -- User Access
4 * Copyright (c) 1997--2018 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
17 pci_scan_bus(struct pci_access *a)
23 pci_alloc_dev(struct pci_access *a)
25 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
27 memset(d, 0, sizeof(*d));
29 d->methods = a->methods;
32 if (d->methods->init_dev)
33 d->methods->init_dev(d);
38 pci_link_dev(struct pci_access *a, struct pci_dev *d)
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.
50 if (d->domain > 0xffff)
51 d->domain_16 = 0xffff;
53 d->domain_16 = d->domain;
59 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
61 struct pci_dev *d = pci_alloc_dev(a);
71 pci_free_properties(struct pci_dev *d)
73 struct pci_property *p;
75 while (p = d->properties)
77 d->properties = p->next;
82 void pci_free_dev(struct pci_dev *d)
84 if (d->methods->cleanup_dev)
85 d->methods->cleanup_dev(d);
88 pci_free_properties(d);
90 pci_mfree(d->module_alias);
92 pci_mfree(d->phy_slot);
98 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
101 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
102 if (pos + len <= d->cache_len)
103 memcpy(buf, d->cache + pos, len);
104 else if (!d->methods->read(d, pos, buf, len))
105 memset(buf, 0xff, len);
109 pci_read_byte(struct pci_dev *d, int pos)
112 pci_read_data(d, &buf, pos, 1);
117 pci_read_word(struct pci_dev *d, int pos)
120 pci_read_data(d, &buf, pos, 2);
121 return le16_to_cpu(buf);
125 pci_read_long(struct pci_dev *d, int pos)
128 pci_read_data(d, &buf, pos, 4);
129 return le32_to_cpu(buf);
133 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
135 return d->methods->read(d, pos, buf, len);
139 pci_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
141 return d->methods->read_vpd ? d->methods->read_vpd(d, pos, buf, len) : 0;
145 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
148 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
149 if (pos + len <= d->cache_len)
150 memcpy(d->cache + pos, buf, len);
151 return d->methods->write(d, pos, buf, len);
155 pci_write_byte(struct pci_dev *d, int pos, byte data)
157 return pci_write_data(d, &data, pos, 1);
161 pci_write_word(struct pci_dev *d, int pos, word data)
163 word buf = cpu_to_le16(data);
164 return pci_write_data(d, &buf, pos, 2);
168 pci_write_long(struct pci_dev *d, int pos, u32 data)
170 u32 buf = cpu_to_le32(data);
171 return pci_write_data(d, &buf, pos, 4);
175 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
177 if (pos < d->cache_len)
179 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
180 memcpy(d->cache + pos, buf, l);
182 return d->methods->write(d, pos, buf, len);
186 pci_reset_properties(struct pci_dev *d)
190 pci_free_properties(d);
194 pci_fill_info_v35(struct pci_dev *d, int flags)
196 if (flags & PCI_FILL_RESCAN)
198 flags &= ~PCI_FILL_RESCAN;
199 pci_reset_properties(d);
201 if (flags & ~d->known_fields)
202 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
203 return d->known_fields;
206 /* In version 3.1, pci_fill_info got new flags => versioned alias */
207 /* In versions 3.2, 3.3, 3.4 and 3.5, the same has happened */
208 STATIC_ALIAS(int pci_fill_info(struct pci_dev *d, int flags), pci_fill_info_v35(d, flags));
209 DEFINE_ALIAS(int pci_fill_info_v30(struct pci_dev *d, int flags), pci_fill_info_v35);
210 DEFINE_ALIAS(int pci_fill_info_v31(struct pci_dev *d, int flags), pci_fill_info_v35);
211 DEFINE_ALIAS(int pci_fill_info_v32(struct pci_dev *d, int flags), pci_fill_info_v35);
212 DEFINE_ALIAS(int pci_fill_info_v33(struct pci_dev *d, int flags), pci_fill_info_v35);
213 DEFINE_ALIAS(int pci_fill_info_v34(struct pci_dev *d, int flags), pci_fill_info_v35);
214 SYMBOL_VERSION(pci_fill_info_v30, pci_fill_info@LIBPCI_3.0);
215 SYMBOL_VERSION(pci_fill_info_v31, pci_fill_info@LIBPCI_3.1);
216 SYMBOL_VERSION(pci_fill_info_v32, pci_fill_info@LIBPCI_3.2);
217 SYMBOL_VERSION(pci_fill_info_v33, pci_fill_info@LIBPCI_3.3);
218 SYMBOL_VERSION(pci_fill_info_v34, pci_fill_info@LIBPCI_3.4);
219 SYMBOL_VERSION(pci_fill_info_v35, pci_fill_info@@LIBPCI_3.5);
222 pci_setup_cache(struct pci_dev *d, byte *cache, int len)
229 pci_set_property(struct pci_dev *d, u32 key, char *value)
231 struct pci_property *p;
232 struct pci_property **pp = &d->properties;
248 p = pci_malloc(d->access, sizeof(*p) + strlen(value));
252 strcpy(p->value, value);
258 pci_get_string_property(struct pci_dev *d, u32 prop)
260 struct pci_property *p;
262 for (p = d->properties; p; p = p->next)