]> git.ipfire.org Git - thirdparty/pciutils.git/blame - lib/access.c
Backward ABI compatibility for new filters and pci_fill_info
[thirdparty/pciutils.git] / lib / access.c
CommitLineData
727ce158 1/*
727ce158
MM
2 * The PCI Library -- User Access
3 *
52aecc75 4 * Copyright (c) 1997--2014 Martin Mares <mj@ucw.cz>
727ce158
MM
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
727ce158
MM
16void
17pci_scan_bus(struct pci_access *a)
18{
19 a->methods->scan(a);
20}
21
22struct pci_dev *
23pci_alloc_dev(struct pci_access *a)
24{
25 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
26
1ac3a99d 27 memset(d, 0, sizeof(*d));
727ce158
MM
28 d->access = a;
29 d->methods = a->methods;
84c8d1bb 30 d->hdrtype = -1;
727ce158
MM
31 if (d->methods->init_dev)
32 d->methods->init_dev(d);
33 return d;
34}
35
36int
37pci_link_dev(struct pci_access *a, struct pci_dev *d)
38{
39 d->next = a->devices;
40 a->devices = d;
41
42 return 1;
43}
44
45struct pci_dev *
84c8d1bb 46pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
727ce158
MM
47{
48 struct pci_dev *d = pci_alloc_dev(a);
49
84c8d1bb 50 d->domain = domain;
727ce158
MM
51 d->bus = bus;
52 d->dev = dev;
53 d->func = func;
54 return d;
55}
56
57void pci_free_dev(struct pci_dev *d)
58{
59 if (d->methods->cleanup_dev)
60 d->methods->cleanup_dev(d);
6d5e39ac 61 pci_free_caps(d);
b1861a8d 62 pci_mfree(d->module_alias);
aecf5b35 63 pci_mfree(d->label);
2849a165 64 pci_mfree(d->phy_slot);
727ce158
MM
65 pci_mfree(d);
66}
67
68static inline void
69pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
70{
71 if (pos & (len-1))
72 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
3430bafe
MM
73 if (pos + len <= d->cache_len)
74 memcpy(buf, d->cache + pos, len);
75 else if (!d->methods->read(d, pos, buf, len))
727ce158
MM
76 memset(buf, 0xff, len);
77}
78
79byte
80pci_read_byte(struct pci_dev *d, int pos)
81{
82 byte buf;
83 pci_read_data(d, &buf, pos, 1);
84 return buf;
85}
86
87word
88pci_read_word(struct pci_dev *d, int pos)
89{
90 word buf;
91 pci_read_data(d, &buf, pos, 2);
92 return le16_to_cpu(buf);
93}
94
95u32
96pci_read_long(struct pci_dev *d, int pos)
97{
98 u32 buf;
99 pci_read_data(d, &buf, pos, 4);
100 return le32_to_cpu(buf);
101}
102
103int
104pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
105{
106 return d->methods->read(d, pos, buf, len);
107}
108
52c81519
BH
109int
110pci_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
111{
112 return d->methods->read_vpd ? d->methods->read_vpd(d, pos, buf, len) : 0;
113}
114
727ce158
MM
115static inline int
116pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
117{
118 if (pos & (len-1))
119 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
3430bafe
MM
120 if (pos + len <= d->cache_len)
121 memcpy(d->cache + pos, buf, len);
727ce158
MM
122 return d->methods->write(d, pos, buf, len);
123}
124
125int
126pci_write_byte(struct pci_dev *d, int pos, byte data)
127{
128 return pci_write_data(d, &data, pos, 1);
129}
130
131int
132pci_write_word(struct pci_dev *d, int pos, word data)
133{
134 word buf = cpu_to_le16(data);
135 return pci_write_data(d, &buf, pos, 2);
136}
137
138int
139pci_write_long(struct pci_dev *d, int pos, u32 data)
140{
141 u32 buf = cpu_to_le32(data);
142 return pci_write_data(d, &buf, pos, 4);
143}
144
145int
146pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
147{
3430bafe
MM
148 if (pos < d->cache_len)
149 {
150 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
151 memcpy(d->cache + pos, buf, l);
152 }
727ce158
MM
153 return d->methods->write(d, pos, buf, len);
154}
155
52aecc75
MM
156int
157pci_fill_info_v33(struct pci_dev *d, int flags)
727ce158
MM
158{
159 if (flags & PCI_FILL_RESCAN)
160 {
161 flags &= ~PCI_FILL_RESCAN;
162 d->known_fields = 0;
6d5e39ac 163 pci_free_caps(d);
727ce158
MM
164 }
165 if (flags & ~d->known_fields)
e95c8373
MM
166 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
167 return d->known_fields;
727ce158 168}
3430bafe 169
89c51b98 170/* In version 3.1, pci_fill_info got new flags => versioned alias */
52aecc75
MM
171/* In versions 3.2 and 3.3, the same has happened */
172STATIC_ALIAS(int pci_fill_info(struct pci_dev *d, int flags), pci_fill_info_v33(d, flags));
173DEFINE_ALIAS(int pci_fill_info_v30(struct pci_dev *d, int flags), pci_fill_info_v33);
174DEFINE_ALIAS(int pci_fill_info_v31(struct pci_dev *d, int flags), pci_fill_info_v33);
175DEFINE_ALIAS(int pci_fill_info_v32(struct pci_dev *d, int flags), pci_fill_info_v33);
89c51b98 176SYMBOL_VERSION(pci_fill_info_v30, pci_fill_info@LIBPCI_3.0);
dbe1e0a6 177SYMBOL_VERSION(pci_fill_info_v31, pci_fill_info@LIBPCI_3.1);
52aecc75
MM
178SYMBOL_VERSION(pci_fill_info_v32, pci_fill_info@LIBPCI_3.2);
179SYMBOL_VERSION(pci_fill_info_v33, pci_fill_info@@LIBPCI_3.3);
89c51b98 180
3430bafe
MM
181void
182pci_setup_cache(struct pci_dev *d, byte *cache, int len)
183{
184 d->cache = cache;
185 d->cache_len = len;
186}