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