]> git.ipfire.org Git - thirdparty/pciutils.git/blame - lib/access.c
Released as 3.5.2
[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;
90ec4a6d 31 d->numa_node = -1;
727ce158
MM
32 if (d->methods->init_dev)
33 d->methods->init_dev(d);
34 return d;
35}
36
37int
38pci_link_dev(struct pci_access *a, struct pci_dev *d)
39{
40 d->next = a->devices;
41 a->devices = d;
42
4186391b
MM
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
727ce158
MM
55 return 1;
56}
57
58struct pci_dev *
84c8d1bb 59pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
727ce158
MM
60{
61 struct pci_dev *d = pci_alloc_dev(a);
62
84c8d1bb 63 d->domain = domain;
727ce158
MM
64 d->bus = bus;
65 d->dev = dev;
66 d->func = func;
67 return d;
68}
69
70void pci_free_dev(struct pci_dev *d)
71{
72 if (d->methods->cleanup_dev)
73 d->methods->cleanup_dev(d);
6d5e39ac 74 pci_free_caps(d);
b1861a8d 75 pci_mfree(d->module_alias);
aecf5b35 76 pci_mfree(d->label);
2849a165 77 pci_mfree(d->phy_slot);
727ce158
MM
78 pci_mfree(d);
79}
80
81static inline void
82pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
83{
84 if (pos & (len-1))
85 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
3430bafe
MM
86 if (pos + len <= d->cache_len)
87 memcpy(buf, d->cache + pos, len);
88 else if (!d->methods->read(d, pos, buf, len))
727ce158
MM
89 memset(buf, 0xff, len);
90}
91
92byte
93pci_read_byte(struct pci_dev *d, int pos)
94{
95 byte buf;
96 pci_read_data(d, &buf, pos, 1);
97 return buf;
98}
99
100word
101pci_read_word(struct pci_dev *d, int pos)
102{
103 word buf;
104 pci_read_data(d, &buf, pos, 2);
105 return le16_to_cpu(buf);
106}
107
108u32
109pci_read_long(struct pci_dev *d, int pos)
110{
111 u32 buf;
112 pci_read_data(d, &buf, pos, 4);
113 return le32_to_cpu(buf);
114}
115
116int
117pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
118{
119 return d->methods->read(d, pos, buf, len);
120}
121
52c81519
BH
122int
123pci_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
124{
125 return d->methods->read_vpd ? d->methods->read_vpd(d, pos, buf, len) : 0;
126}
127
727ce158
MM
128static inline int
129pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
130{
131 if (pos & (len-1))
132 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
3430bafe
MM
133 if (pos + len <= d->cache_len)
134 memcpy(d->cache + pos, buf, len);
727ce158
MM
135 return d->methods->write(d, pos, buf, len);
136}
137
138int
139pci_write_byte(struct pci_dev *d, int pos, byte data)
140{
141 return pci_write_data(d, &data, pos, 1);
142}
143
144int
145pci_write_word(struct pci_dev *d, int pos, word data)
146{
147 word buf = cpu_to_le16(data);
148 return pci_write_data(d, &buf, pos, 2);
149}
150
151int
152pci_write_long(struct pci_dev *d, int pos, u32 data)
153{
154 u32 buf = cpu_to_le32(data);
155 return pci_write_data(d, &buf, pos, 4);
156}
157
158int
159pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
160{
3430bafe
MM
161 if (pos < d->cache_len)
162 {
163 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
164 memcpy(d->cache + pos, buf, l);
165 }
727ce158
MM
166 return d->methods->write(d, pos, buf, len);
167}
168
52aecc75 169int
558f736b 170pci_fill_info_v35(struct pci_dev *d, int flags)
727ce158
MM
171{
172 if (flags & PCI_FILL_RESCAN)
173 {
174 flags &= ~PCI_FILL_RESCAN;
175 d->known_fields = 0;
6d5e39ac 176 pci_free_caps(d);
727ce158
MM
177 }
178 if (flags & ~d->known_fields)
e95c8373
MM
179 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
180 return d->known_fields;
727ce158 181}
3430bafe 182
89c51b98 183/* In version 3.1, pci_fill_info got new flags => versioned alias */
558f736b
SS
184/* In versions 3.2, 3.3, 3.4 and 3.5, the same has happened */
185STATIC_ALIAS(int pci_fill_info(struct pci_dev *d, int flags), pci_fill_info_v35(d, flags));
186DEFINE_ALIAS(int pci_fill_info_v30(struct pci_dev *d, int flags), pci_fill_info_v35);
187DEFINE_ALIAS(int pci_fill_info_v31(struct pci_dev *d, int flags), pci_fill_info_v35);
188DEFINE_ALIAS(int pci_fill_info_v32(struct pci_dev *d, int flags), pci_fill_info_v35);
189DEFINE_ALIAS(int pci_fill_info_v33(struct pci_dev *d, int flags), pci_fill_info_v35);
190DEFINE_ALIAS(int pci_fill_info_v34(struct pci_dev *d, int flags), pci_fill_info_v35);
89c51b98 191SYMBOL_VERSION(pci_fill_info_v30, pci_fill_info@LIBPCI_3.0);
dbe1e0a6 192SYMBOL_VERSION(pci_fill_info_v31, pci_fill_info@LIBPCI_3.1);
52aecc75 193SYMBOL_VERSION(pci_fill_info_v32, pci_fill_info@LIBPCI_3.2);
7ef7f3ec 194SYMBOL_VERSION(pci_fill_info_v33, pci_fill_info@LIBPCI_3.3);
558f736b
SS
195SYMBOL_VERSION(pci_fill_info_v34, pci_fill_info@LIBPCI_3.4);
196SYMBOL_VERSION(pci_fill_info_v35, pci_fill_info@@LIBPCI_3.5);
89c51b98 197
3430bafe
MM
198void
199pci_setup_cache(struct pci_dev *d, byte *cache, int len)
200{
201 d->cache = cache;
202 d->cache_len = len;
203}