]> mj.ucw.cz Git - pciutils.git/blob - lib/access.c
bd829a6e37537be6451f9c9a7caec9ec563982f3
[pciutils.git] / lib / access.c
1 /*
2  *      The PCI Library -- User Access
3  *
4  *      Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
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
16 void
17 pci_scan_bus(struct pci_access *a)
18 {
19   a->methods->scan(a);
20 }
21
22 struct pci_dev *
23 pci_alloc_dev(struct pci_access *a)
24 {
25   struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev));
26
27   memset(d, 0, sizeof(*d));
28   d->access = a;
29   d->methods = a->methods;
30   d->hdrtype = -1;
31   if (d->methods->init_dev)
32     d->methods->init_dev(d);
33   return d;
34 }
35
36 int
37 pci_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
45 struct pci_dev *
46 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func)
47 {
48   struct pci_dev *d = pci_alloc_dev(a);
49
50   d->domain = domain;
51   d->bus = bus;
52   d->dev = dev;
53   d->func = func;
54   return d;
55 }
56
57 void pci_free_dev(struct pci_dev *d)
58 {
59   if (d->methods->cleanup_dev)
60     d->methods->cleanup_dev(d);
61   pci_free_caps(d);
62   pci_mfree(d);
63 }
64
65 static inline void
66 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
67 {
68   if (pos & (len-1))
69     d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
70   if (pos + len <= d->cache_len)
71     memcpy(buf, d->cache + pos, len);
72   else if (!d->methods->read(d, pos, buf, len))
73     memset(buf, 0xff, len);
74 }
75
76 byte
77 pci_read_byte(struct pci_dev *d, int pos)
78 {
79   byte buf;
80   pci_read_data(d, &buf, pos, 1);
81   return buf;
82 }
83
84 word
85 pci_read_word(struct pci_dev *d, int pos)
86 {
87   word buf;
88   pci_read_data(d, &buf, pos, 2);
89   return le16_to_cpu(buf);
90 }
91
92 u32
93 pci_read_long(struct pci_dev *d, int pos)
94 {
95   u32 buf;
96   pci_read_data(d, &buf, pos, 4);
97   return le32_to_cpu(buf);
98 }
99
100 int
101 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
102 {
103   return d->methods->read(d, pos, buf, len);
104 }
105
106 static inline int
107 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
108 {
109   if (pos & (len-1))
110     d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
111   if (pos + len <= d->cache_len)
112     memcpy(d->cache + pos, buf, len);
113   return d->methods->write(d, pos, buf, len);
114 }
115
116 int
117 pci_write_byte(struct pci_dev *d, int pos, byte data)
118 {
119   return pci_write_data(d, &data, pos, 1);
120 }
121
122 int
123 pci_write_word(struct pci_dev *d, int pos, word data)
124 {
125   word buf = cpu_to_le16(data);
126   return pci_write_data(d, &buf, pos, 2);
127 }
128
129 int
130 pci_write_long(struct pci_dev *d, int pos, u32 data)
131 {
132   u32 buf = cpu_to_le32(data);
133   return pci_write_data(d, &buf, pos, 4);
134 }
135
136 int
137 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
138 {
139   if (pos < d->cache_len)
140     {
141       int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
142       memcpy(d->cache + pos, buf, l);
143     }
144   return d->methods->write(d, pos, buf, len);
145 }
146
147 int
148 pci_fill_info_v31(struct pci_dev *d, int flags)
149 {
150   if (flags & PCI_FILL_RESCAN)
151     {
152       flags &= ~PCI_FILL_RESCAN;
153       d->known_fields = 0;
154       pci_free_caps(d);
155     }
156   if (flags & ~d->known_fields)
157     d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
158   return d->known_fields;
159 }
160
161 /* In version 3.1, pci_fill_info got new flags => versioned alias */
162 STATIC_ALIAS(int pci_fill_info(struct pci_dev *d, int flags), pci_fill_info_v31(d,flags));
163 DEFINE_ALIAS(int pci_fill_info_v30(struct pci_dev *d, int flags), pci_fill_info_v31);
164 SYMBOL_VERSION(pci_fill_info_v30, pci_fill_info@LIBPCI_3.0);
165 SYMBOL_VERSION(pci_fill_info_v31, pci_fill_info@@LIBPCI_3.1);
166
167 void
168 pci_setup_cache(struct pci_dev *d, byte *cache, int len)
169 {
170   d->cache = cache;
171   d->cache_len = len;
172 }