2
* The PCI Library -- User Access
4
* Copyright (c) 1997--2003 Martin Mares <mj@ucw.cz>
6
* May 8, 2006 - Modified and ported to HelenOS by Jakub Jermar.
8
* Can be freely distributed and used under the terms of the GNU GPL.
18
static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = {
23
struct pci_access *pci_alloc(void)
25
struct pci_access *a = malloc(sizeof(struct pci_access));
32
for (i = 0; i < PCI_ACCESS_MAX; i++)
33
if (pci_methods[i] && pci_methods[i]->config)
34
pci_methods[i]->config(a);
38
void *pci_malloc(struct pci_access *a, int size)
40
void *x = malloc(size);
43
a->error("Out of memory (allocation of %d bytes failed)", size);
47
void pci_mfree(void *x)
53
static void pci_generic_error(char *msg, ...)
64
static void pci_generic_warn(char *msg, ...)
74
static void pci_generic_debug(char *msg, ...)
83
static void pci_null_debug(char *msg UNUSED, ...)
87
void pci_init(struct pci_access *a)
90
a->error = pci_generic_error;
92
a->warning = pci_generic_warn;
94
a->debug = pci_generic_debug;
96
a->debug = pci_null_debug;
99
if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method])
100
a->error("This access method is not supported.");
101
a->methods = pci_methods[a->method];
104
for (i = 0; i < PCI_ACCESS_MAX; i++)
105
if (pci_methods[i]) {
106
a->debug("Trying method %d...", i);
107
if (pci_methods[i]->detect(a)) {
109
a->methods = pci_methods[i];
113
a->debug("...No.\n");
116
a->error("Cannot find any working access method.");
118
a->debug("Decided to use %s\n", a->methods->name);
122
void pci_cleanup(struct pci_access *a)
124
struct pci_dev *d, *e;
126
for (d = a->devices; d; d = e) {
131
a->methods->cleanup(a);
132
pci_free_name_list(a);
136
void pci_scan_bus(struct pci_access *a)
141
struct pci_dev *pci_alloc_dev(struct pci_access *a)
143
struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
145
bzero(d, sizeof(*d));
147
d->methods = a->methods;
149
if (d->methods->init_dev)
150
d->methods->init_dev(d);
154
int pci_link_dev(struct pci_access *a, struct pci_dev *d)
156
d->next = a->devices;
162
struct pci_dev *pci_get_dev(struct pci_access *a, int domain, int bus,
165
struct pci_dev *d = pci_alloc_dev(a);
174
void pci_free_dev(struct pci_dev *d)
176
if (d->methods->cleanup_dev)
177
d->methods->cleanup_dev(d);
182
pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
185
d->access->error("Unaligned read: pos=%02x, len=%d", pos,
187
if (pos + len <= d->cache_len)
188
memcpy(buf, d->cache + pos, len);
189
else if (!d->methods->read(d, pos, buf, len))
190
memset(buf, 0xff, len);
193
byte pci_read_byte(struct pci_dev *d, int pos)
196
pci_read_data(d, &buf, pos, 1);
200
word pci_read_word(struct pci_dev * d, int pos)
203
pci_read_data(d, &buf, pos, 2);
204
return le16_to_cpu(buf);
207
u32 pci_read_long(struct pci_dev * d, int pos)
210
pci_read_data(d, &buf, pos, 4);
211
return le32_to_cpu(buf);
214
int pci_read_block(struct pci_dev *d, int pos, byte * buf, int len)
216
return d->methods->read(d, pos, buf, len);
220
pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
223
d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
224
if (pos + len <= d->cache_len)
225
memcpy(d->cache + pos, buf, len);
226
return d->methods->write(d, pos, buf, len);
229
int pci_write_byte(struct pci_dev *d, int pos, byte data)
231
return pci_write_data(d, &data, pos, 1);
234
int pci_write_word(struct pci_dev *d, int pos, word data)
236
word buf = cpu_to_le16(data);
237
return pci_write_data(d, &buf, pos, 2);
240
int pci_write_long(struct pci_dev *d, int pos, u32 data)
242
u32 buf = cpu_to_le32(data);
243
return pci_write_data(d, &buf, pos, 4);
246
int pci_write_block(struct pci_dev *d, int pos, byte * buf, int len)
248
if (pos < d->cache_len) {
249
int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
250
memcpy(d->cache + pos, buf, l);
252
return d->methods->write(d, pos, buf, len);
255
int pci_fill_info(struct pci_dev *d, int flags)
257
if (flags & PCI_FILL_RESCAN) {
258
flags &= ~PCI_FILL_RESCAN;
261
if (flags & ~d->known_fields)
262
d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
263
return d->known_fields;
266
void pci_setup_cache(struct pci_dev *d, byte * cache, int len)