Changeset 20a9b85 in mainline for pci/libpci/access.c
- Timestamp:
- 2006-05-09T10:55:02Z (19 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 46ec2c06
- Parents:
- 4a7c273
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pci/libpci/access.c
r4a7c273 r20a9b85 17 17 18 18 static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = { 19 20 19 &pm_intel_conf1, 20 &pm_intel_conf2, 21 21 }; 22 22 23 struct pci_access * 24 pci_alloc(void) 25 { 26 struct pci_access *a = malloc(sizeof(struct pci_access)); 27 int i; 28 29 bzero(a, sizeof(*a)); 30 for(i=0; i<PCI_ACCESS_MAX; i++) 31 if (pci_methods[i] && pci_methods[i]->config) 32 pci_methods[i]->config(a); 33 return a; 34 } 35 36 void * 37 pci_malloc(struct pci_access *a, int size) 38 { 39 void *x = malloc(size); 40 41 if (!x) 42 a->error("Out of memory (allocation of %d bytes failed)", size); 43 return x; 44 } 45 46 void 47 pci_mfree(void *x) 48 { 49 if (x) 50 free(x); 51 } 52 53 static void 54 pci_generic_error(char *msg, ...) 55 { 56 va_list args; 57 58 va_start(args, msg); 59 puts("pcilib: "); 60 vprintf(msg, args); 61 putchar('\n'); 62 exit(1); 63 } 64 65 static void 66 pci_generic_warn(char *msg, ...) 67 { 68 va_list args; 69 70 va_start(args, msg); 71 puts("pcilib: "); 72 vprintf(msg, args); 73 putchar('\n'); 74 } 75 76 static void 77 pci_generic_debug(char *msg, ...) 78 { 79 va_list args; 80 81 va_start(args, msg); 82 vprintf(msg, args); 83 va_end(args); 84 } 85 86 static void 87 pci_null_debug(char *msg UNUSED, ...) 88 { 89 } 90 91 void 92 pci_init(struct pci_access *a) 93 { 94 if (!a->error) 95 a->error = pci_generic_error; 96 if (!a->warning) 97 a->warning = pci_generic_warn; 98 if (!a->debug) 99 a->debug = pci_generic_debug; 100 if (!a->debugging) 101 a->debug = pci_null_debug; 102 103 if (a->method) 104 { 105 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method]) 106 a->error("This access method is not supported."); 107 a->methods = pci_methods[a->method]; 108 } 109 else 110 { 111 unsigned int i; 112 for(i=0; i<PCI_ACCESS_MAX; i++) 113 if (pci_methods[i]) 114 { 115 a->debug("Trying method %d...", i); 116 if (pci_methods[i]->detect(a)) 117 { 118 a->debug("...OK\n"); 119 a->methods = pci_methods[i]; 120 a->method = i; 121 break; 122 } 123 a->debug("...No.\n"); 124 } 125 if (!a->methods) 126 a->error("Cannot find any working access method."); 127 } 128 a->debug("Decided to use %s\n", a->methods->name); 129 a->methods->init(a); 130 } 131 132 void 133 pci_cleanup(struct pci_access *a) 134 { 135 struct pci_dev *d, *e; 136 137 for(d=a->devices; d; d=e) 138 { 139 e = d->next; 140 pci_free_dev(d); 141 } 142 if (a->methods) 143 a->methods->cleanup(a); 144 pci_free_name_list(a); 145 pci_mfree(a); 146 } 147 148 void 149 pci_scan_bus(struct pci_access *a) 150 { 151 a->methods->scan(a); 152 } 153 154 struct pci_dev * 155 pci_alloc_dev(struct pci_access *a) 156 { 157 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev)); 158 159 bzero(d, sizeof(*d)); 160 d->access = a; 161 d->methods = a->methods; 162 d->hdrtype = -1; 163 if (d->methods->init_dev) 164 d->methods->init_dev(d); 165 return d; 166 } 167 168 int 169 pci_link_dev(struct pci_access *a, struct pci_dev *d) 170 { 171 d->next = a->devices; 172 a->devices = d; 173 174 return 1; 175 } 176 177 struct pci_dev * 178 pci_get_dev(struct pci_access *a, int domain, int bus, int dev, int func) 179 { 180 struct pci_dev *d = pci_alloc_dev(a); 181 182 d->domain = domain; 183 d->bus = bus; 184 d->dev = dev; 185 d->func = func; 186 return d; 23 struct pci_access *pci_alloc(void) 24 { 25 struct pci_access *a = malloc(sizeof(struct pci_access)); 26 int i; 27 28 bzero(a, sizeof(*a)); 29 for (i = 0; i < PCI_ACCESS_MAX; i++) 30 if (pci_methods[i] && pci_methods[i]->config) 31 pci_methods[i]->config(a); 32 return a; 33 } 34 35 void *pci_malloc(struct pci_access *a, int size) 36 { 37 void *x = malloc(size); 38 39 if (!x) 40 a->error("Out of memory (allocation of %d bytes failed)", 41 size); 42 return x; 43 } 44 45 void pci_mfree(void *x) 46 { 47 if (x) 48 free(x); 49 } 50 51 static void pci_generic_error(char *msg, ...) 52 { 53 va_list args; 54 55 va_start(args, msg); 56 puts("pcilib: "); 57 vprintf(msg, args); 58 putchar('\n'); 59 exit(1); 60 } 61 62 static void pci_generic_warn(char *msg, ...) 63 { 64 va_list args; 65 66 va_start(args, msg); 67 puts("pcilib: "); 68 vprintf(msg, args); 69 putchar('\n'); 70 } 71 72 static void pci_generic_debug(char *msg, ...) 73 { 74 va_list args; 75 76 va_start(args, msg); 77 vprintf(msg, args); 78 va_end(args); 79 } 80 81 static void pci_null_debug(char *msg UNUSED, ...) 82 { 83 } 84 85 void pci_init(struct pci_access *a) 86 { 87 if (!a->error) 88 a->error = pci_generic_error; 89 if (!a->warning) 90 a->warning = pci_generic_warn; 91 if (!a->debug) 92 a->debug = pci_generic_debug; 93 if (!a->debugging) 94 a->debug = pci_null_debug; 95 96 if (a->method) { 97 if (a->method >= PCI_ACCESS_MAX || !pci_methods[a->method]) 98 a->error("This access method is not supported."); 99 a->methods = pci_methods[a->method]; 100 } else { 101 unsigned int i; 102 for (i = 0; i < PCI_ACCESS_MAX; i++) 103 if (pci_methods[i]) { 104 a->debug("Trying method %d...", i); 105 if (pci_methods[i]->detect(a)) { 106 a->debug("...OK\n"); 107 a->methods = pci_methods[i]; 108 a->method = i; 109 break; 110 } 111 a->debug("...No.\n"); 112 } 113 if (!a->methods) 114 a->error("Cannot find any working access method."); 115 } 116 a->debug("Decided to use %s\n", a->methods->name); 117 a->methods->init(a); 118 } 119 120 void pci_cleanup(struct pci_access *a) 121 { 122 struct pci_dev *d, *e; 123 124 for (d = a->devices; d; d = e) { 125 e = d->next; 126 pci_free_dev(d); 127 } 128 if (a->methods) 129 a->methods->cleanup(a); 130 pci_free_name_list(a); 131 pci_mfree(a); 132 } 133 134 void pci_scan_bus(struct pci_access *a) 135 { 136 a->methods->scan(a); 137 } 138 139 struct pci_dev *pci_alloc_dev(struct pci_access *a) 140 { 141 struct pci_dev *d = pci_malloc(a, sizeof(struct pci_dev)); 142 143 bzero(d, sizeof(*d)); 144 d->access = a; 145 d->methods = a->methods; 146 d->hdrtype = -1; 147 if (d->methods->init_dev) 148 d->methods->init_dev(d); 149 return d; 150 } 151 152 int pci_link_dev(struct pci_access *a, struct pci_dev *d) 153 { 154 d->next = a->devices; 155 a->devices = d; 156 157 return 1; 158 } 159 160 struct pci_dev *pci_get_dev(struct pci_access *a, int domain, int bus, 161 int dev, int func) 162 { 163 struct pci_dev *d = pci_alloc_dev(a); 164 165 d->domain = domain; 166 d->bus = bus; 167 d->dev = dev; 168 d->func = func; 169 return d; 187 170 } 188 171 189 172 void pci_free_dev(struct pci_dev *d) 190 173 { 191 192 193 174 if (d->methods->cleanup_dev) 175 d->methods->cleanup_dev(d); 176 pci_mfree(d); 194 177 } 195 178 … … 197 180 pci_read_data(struct pci_dev *d, void *buf, int pos, int len) 198 181 { 199 if (pos & (len-1)) 200 d->access->error("Unaligned read: pos=%02x, len=%d", pos, len); 201 if (pos + len <= d->cache_len) 202 memcpy(buf, d->cache + pos, len); 203 else if (!d->methods->read(d, pos, buf, len)) 204 memset(buf, 0xff, len); 205 } 206 207 byte 208 pci_read_byte(struct pci_dev *d, int pos) 209 { 210 byte buf; 211 pci_read_data(d, &buf, pos, 1); 212 return buf; 213 } 214 215 word 216 pci_read_word(struct pci_dev *d, int pos) 217 { 218 word buf; 219 pci_read_data(d, &buf, pos, 2); 220 return le16_to_cpu(buf); 221 } 222 223 u32 224 pci_read_long(struct pci_dev *d, int pos) 225 { 226 u32 buf; 227 pci_read_data(d, &buf, pos, 4); 228 return le32_to_cpu(buf); 229 } 230 231 int 232 pci_read_block(struct pci_dev *d, int pos, byte *buf, int len) 233 { 234 return d->methods->read(d, pos, buf, len); 182 if (pos & (len - 1)) 183 d->access->error("Unaligned read: pos=%02x, len=%d", pos, 184 len); 185 if (pos + len <= d->cache_len) 186 memcpy(buf, d->cache + pos, len); 187 else if (!d->methods->read(d, pos, buf, len)) 188 memset(buf, 0xff, len); 189 } 190 191 byte pci_read_byte(struct pci_dev *d, int pos) 192 { 193 byte buf; 194 pci_read_data(d, &buf, pos, 1); 195 return buf; 196 } 197 198 word pci_read_word(struct pci_dev * d, int pos) 199 { 200 word buf; 201 pci_read_data(d, &buf, pos, 2); 202 return le16_to_cpu(buf); 203 } 204 205 u32 pci_read_long(struct pci_dev * d, int pos) 206 { 207 u32 buf; 208 pci_read_data(d, &buf, pos, 4); 209 return le32_to_cpu(buf); 210 } 211 212 int pci_read_block(struct pci_dev *d, int pos, byte * buf, int len) 213 { 214 return d->methods->read(d, pos, buf, len); 235 215 } 236 216 … … 238 218 pci_write_data(struct pci_dev *d, void *buf, int pos, int len) 239 219 { 240 if (pos & (len-1)) 241 d->access->error("Unaligned write: pos=%02x,len=%d", pos, len); 242 if (pos + len <= d->cache_len) 243 memcpy(d->cache + pos, buf, len); 244 return d->methods->write(d, pos, buf, len); 245 } 246 247 int 248 pci_write_byte(struct pci_dev *d, int pos, byte data) 249 { 250 return pci_write_data(d, &data, pos, 1); 251 } 252 253 int 254 pci_write_word(struct pci_dev *d, int pos, word data) 255 { 256 word buf = cpu_to_le16(data); 257 return pci_write_data(d, &buf, pos, 2); 258 } 259 260 int 261 pci_write_long(struct pci_dev *d, int pos, u32 data) 262 { 263 u32 buf = cpu_to_le32(data); 264 return pci_write_data(d, &buf, pos, 4); 265 } 266 267 int 268 pci_write_block(struct pci_dev *d, int pos, byte *buf, int len) 269 { 270 if (pos < d->cache_len) 271 { 272 int l = (pos + len >= d->cache_len) ? (d->cache_len - pos) : len; 273 memcpy(d->cache + pos, buf, l); 274 } 275 return d->methods->write(d, pos, buf, len); 276 } 277 278 int 279 pci_fill_info(struct pci_dev *d, int flags) 280 { 281 if (flags & PCI_FILL_RESCAN) 282 { 283 flags &= ~PCI_FILL_RESCAN; 284 d->known_fields = 0; 285 } 286 if (flags & ~d->known_fields) 287 d->known_fields |= d->methods->fill_info(d, flags & ~d->known_fields); 288 return d->known_fields; 289 } 290 291 void 292 pci_setup_cache(struct pci_dev *d, byte *cache, int len) 293 { 294 d->cache = cache; 295 d->cache_len = len; 296 } 220 if (pos & (len - 1)) 221 d->access->error("Unaligned write: pos=%02x,len=%d", pos, 222 len); 223 if (pos + len <= d->cache_len) 224 memcpy(d->cache + pos, buf, len); 225 return d->methods->write(d, pos, buf, len); 226 } 227 228 int pci_write_byte(struct pci_dev *d, int pos, byte data) 229 { 230 return pci_write_data(d, &data, pos, 1); 231 } 232 233 int pci_write_word(struct pci_dev *d, int pos, word data) 234 { 235 word buf = cpu_to_le16(data); 236 return pci_write_data(d, &buf, pos, 2); 237 } 238 239 int pci_write_long(struct pci_dev *d, int pos, u32 data) 240 { 241 u32 buf = cpu_to_le32(data); 242 return pci_write_data(d, &buf, pos, 4); 243 } 244 245 int pci_write_block(struct pci_dev *d, int pos, byte * buf, int len) 246 { 247 if (pos < d->cache_len) { 248 int l = 249 (pos + len >= 250 d->cache_len) ? (d->cache_len - pos) : len; 251 memcpy(d->cache + pos, buf, l); 252 } 253 return d->methods->write(d, pos, buf, len); 254 } 255 256 int pci_fill_info(struct pci_dev *d, int flags) 257 { 258 if (flags & PCI_FILL_RESCAN) { 259 flags &= ~PCI_FILL_RESCAN; 260 d->known_fields = 0; 261 } 262 if (flags & ~d->known_fields) 263 d->known_fields |= 264 d->methods->fill_info(d, flags & ~d->known_fields); 265 return d->known_fields; 266 } 267 268 void pci_setup_cache(struct pci_dev *d, byte * cache, int len) 269 { 270 d->cache = cache; 271 d->cache_len = len; 272 }
Note:
See TracChangeset
for help on using the changeset viewer.