]> mj.ucw.cz Git - pciutils.git/blob - lib/access.c
lspci: Add L1 PM Substate capability reporting
[pciutils.git] / lib / access.c
1 /*
2  *      The PCI Library -- User Access
3  *
4  *      Copyright (c) 1997--2013 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->module_alias);
63   pci_mfree(d->phy_slot);
64   pci_mfree(d);
65 }
66
67 static inline void
68 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
69 {
70   if (pos & (len-1))
71     d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
72   if (pos + len <= d->cache_len)
73     memcpy(buf, d->cache + pos, len);
74   else if (!d->methods->read(d, pos, buf, len))
75     memset(buf, 0xff, len);
76 }
77
78 byte
79 pci_read_byte(struct pci_dev *d, int pos)
80 {
81   byte buf;
82   pci_read_data(d, &buf, pos, 1);
83   return buf;
84 }
85
86 word
87 pci_read_word(struct pci_dev *d, int pos)
88 {
89   word buf;
90   pci_read_data(d, &buf, pos, 2);
91   return le16_to_cpu(buf);
92 }
93
94 u32
95 pci_read_long(struct pci_dev *d, int pos)
96 {
97   u32 buf;
98   pci_read_data(d, &buf, pos, 4);
99   return le32_to_cpu(buf);
100 }
101
102 int
103 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
104 {
105   return d->methods->read(d, pos, buf, len);
106 }
107
108 int
109 pci_read_vpd(struct pci_dev *d, int pos, byte *buf, int len)
110 {
111   return d->methods->read_vpd ? d->methods->read_vpd(d, pos, buf, len) : 0;
112 }
113
114 static inline int
115 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
116 {
117   if (pos & (len-1))
118     d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
119   if (pos + len <= d->cache_len)
120     memcpy(d->cache + pos, buf, len);
121   return d->methods->write(d, pos, buf, len);
122 }
123
124 int
125 pci_write_byte(struct pci_dev *d, int pos, byte data)
126 {
127   return pci_write_data(d, &data, pos, 1);
128 }
129
130 int
131 pci_write_word(struct pci_dev *d, int pos, word data)
132 {
133   word buf = cpu_to_le16(data);
134   return pci_write_data(d, &buf, pos, 2);
135 }
136
137 int
138 pci_write_long(struct pci_dev *d, int pos, u32 data)
139 {
140   u32 buf = cpu_to_le32(data);
141   return pci_write_data(d, &buf, pos, 4);
142 }
143
144 int
145 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
146 {
147   if (pos < d->cache_len)
148     {
149       int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
150       memcpy(d->cache + pos, buf, l);
151     }
152   return d->methods->write(d, pos, buf, len);
153 }
154
155 int
156 pci_fill_info_v32(struct pci_dev *d, int flags)
157 {
158   if (flags & PCI_FILL_RESCAN)
159     {
160       flags &= ~PCI_FILL_RESCAN;
161       d->known_fields = 0;
162       pci_free_caps(d);
163     }
164   if (flags & ~d->known_fields)
165     d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
166   return d->known_fields;
167 }
168
169 /* In version 3.1, pci_fill_info got new flags => versioned alias */
170 /* In version 3.2, the same has happened */
171 STATIC_ALIAS(int pci_fill_info(struct pci_dev *d, int flags), pci_fill_info_v32(d, flags));
172 DEFINE_ALIAS(int pci_fill_info_v30(struct pci_dev *d, int flags), pci_fill_info_v32);
173 DEFINE_ALIAS(int pci_fill_info_v31(struct pci_dev *d, int flags), pci_fill_info_v32);
174 SYMBOL_VERSION(pci_fill_info_v30, pci_fill_info@LIBPCI_3.0);
175 SYMBOL_VERSION(pci_fill_info_v31, pci_fill_info@LIBPCI_3.1);
176 SYMBOL_VERSION(pci_fill_info_v32, pci_fill_info@@LIBPCI_3.2);
177
178 void
179 pci_setup_cache(struct pci_dev *d, byte *cache, int len)
180 {
181   d->cache = cache;
182   d->cache_len = len;
183 }