Tizen 2.1 base
[framework/system/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_mfree(d);
62 }
63
64 static inline void
65 pci_read_data(struct pci_dev *d, void *buf, int pos, int len)
66 {
67   if (pos & (len-1))
68     d->access->error("Unaligned read: pos=%02x, len=%d", pos, len);
69   if (pos + len <= d->cache_len)
70     memcpy(buf, d->cache + pos, len);
71   else if (!d->methods->read(d, pos, buf, len))
72     memset(buf, 0xff, len);
73 }
74
75 byte
76 pci_read_byte(struct pci_dev *d, int pos)
77 {
78   byte buf;
79   pci_read_data(d, &buf, pos, 1);
80   return buf;
81 }
82
83 word
84 pci_read_word(struct pci_dev *d, int pos)
85 {
86   word buf;
87   pci_read_data(d, &buf, pos, 2);
88   return le16_to_cpu(buf);
89 }
90
91 u32
92 pci_read_long(struct pci_dev *d, int pos)
93 {
94   u32 buf;
95   pci_read_data(d, &buf, pos, 4);
96   return le32_to_cpu(buf);
97 }
98
99 int
100 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len)
101 {
102   return d->methods->read(d, pos, buf, len);
103 }
104
105 static inline int
106 pci_write_data(struct pci_dev *d, void *buf, int pos, int len)
107 {
108   if (pos & (len-1))
109     d->access->error("Unaligned write: pos=%02x,len=%d", pos, len);
110   if (pos + len <= d->cache_len)
111     memcpy(d->cache + pos, buf, len);
112   return d->methods->write(d, pos, buf, len);
113 }
114
115 int
116 pci_write_byte(struct pci_dev *d, int pos, byte data)
117 {
118   return pci_write_data(d, &data, pos, 1);
119 }
120
121 int
122 pci_write_word(struct pci_dev *d, int pos, word data)
123 {
124   word buf = cpu_to_le16(data);
125   return pci_write_data(d, &buf, pos, 2);
126 }
127
128 int
129 pci_write_long(struct pci_dev *d, int pos, u32 data)
130 {
131   u32 buf = cpu_to_le32(data);
132   return pci_write_data(d, &buf, pos, 4);
133 }
134
135 int
136 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len)
137 {
138   if (pos < d->cache_len)
139     {
140       int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len;
141       memcpy(d->cache + pos, buf, l);
142     }
143   return d->methods->write(d, pos, buf, len);
144 }
145
146 int
147 pci_fill_info(struct pci_dev *d, int flags)
148 {
149   if (flags & PCI_FILL_RESCAN)
150     {
151       flags &= ~PCI_FILL_RESCAN;
152       d->known_fields = 0;
153     }
154   if (flags & ~d->known_fields)
155     d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields);
156   return d->known_fields;
157 }
158
159 void
160 pci_setup_cache(struct pci_dev *d, byte *cache, int len)
161 {
162   d->cache = cache;
163   d->cache_len = len;
164 }