Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * cistpl.c -- 16-bit PCMCIA Card Information Structure parser | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * The initial developer of the original code is David A. Hinds | |
9 | * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds | |
10 | * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. | |
11 | * | |
12 | * (C) 1999 David A. Hinds | |
13 | */ | |
14 | ||
1da177e4 LT |
15 | #include <linux/module.h> |
16 | #include <linux/moduleparam.h> | |
17 | #include <linux/kernel.h> | |
18 | #include <linux/string.h> | |
19 | #include <linux/major.h> | |
20 | #include <linux/errno.h> | |
21 | #include <linux/timer.h> | |
22 | #include <linux/slab.h> | |
23 | #include <linux/mm.h> | |
1da177e4 LT |
24 | #include <linux/pci.h> |
25 | #include <linux/ioport.h> | |
9fea84f4 | 26 | #include <linux/io.h> |
1da177e4 | 27 | #include <asm/byteorder.h> |
dc0cf6a2 | 28 | #include <asm/unaligned.h> |
1da177e4 LT |
29 | |
30 | #include <pcmcia/cs_types.h> | |
31 | #include <pcmcia/ss.h> | |
32 | #include <pcmcia/cs.h> | |
1da177e4 LT |
33 | #include <pcmcia/cisreg.h> |
34 | #include <pcmcia/cistpl.h> | |
35 | #include "cs_internal.h" | |
36 | ||
37 | static const u_char mantissa[] = { | |
38 | 10, 12, 13, 15, 20, 25, 30, 35, | |
39 | 40, 45, 50, 55, 60, 70, 80, 90 | |
40 | }; | |
41 | ||
42 | static const u_int exponent[] = { | |
43 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 | |
44 | }; | |
45 | ||
46 | /* Convert an extended speed byte to a time in nanoseconds */ | |
47 | #define SPEED_CVT(v) \ | |
48 | (mantissa[(((v)>>3)&15)-1] * exponent[(v)&7] / 10) | |
49 | /* Convert a power byte to a current in 0.1 microamps */ | |
50 | #define POWER_CVT(v) \ | |
51 | (mantissa[((v)>>3)&15] * exponent[(v)&7] / 10) | |
52 | #define POWER_SCALE(v) (exponent[(v)&7]) | |
53 | ||
54 | /* Upper limit on reasonable # of tuples */ | |
55 | #define MAX_TUPLES 200 | |
56 | ||
57 | /*====================================================================*/ | |
58 | ||
59 | /* Parameters that can be set with 'insmod' */ | |
60 | ||
37f77955 PM |
61 | /* 16-bit CIS? */ |
62 | static int cis_width; | |
63 | module_param(cis_width, int, 0444); | |
1da177e4 LT |
64 | |
65 | void release_cis_mem(struct pcmcia_socket *s) | |
66 | { | |
6b8e087b | 67 | mutex_lock(&s->ops_mutex); |
1da177e4 LT |
68 | if (s->cis_mem.flags & MAP_ACTIVE) { |
69 | s->cis_mem.flags &= ~MAP_ACTIVE; | |
70 | s->ops->set_mem_map(s, &s->cis_mem); | |
71 | if (s->cis_mem.res) { | |
72 | release_resource(s->cis_mem.res); | |
73 | kfree(s->cis_mem.res); | |
74 | s->cis_mem.res = NULL; | |
75 | } | |
76 | iounmap(s->cis_virt); | |
77 | s->cis_virt = NULL; | |
78 | } | |
6b8e087b | 79 | mutex_unlock(&s->ops_mutex); |
1da177e4 | 80 | } |
1da177e4 LT |
81 | |
82 | /* | |
83 | * Map the card memory at "card_offset" into virtual space. | |
84 | * If flags & MAP_ATTRIB, map the attribute space, otherwise | |
85 | * map the memory space. | |
86 | */ | |
87 | static void __iomem * | |
88 | set_cis_map(struct pcmcia_socket *s, unsigned int card_offset, unsigned int flags) | |
89 | { | |
2e5a3e79 DB |
90 | pccard_mem_map *mem = &s->cis_mem; |
91 | int ret; | |
92 | ||
6b8e087b | 93 | mutex_lock(&s->ops_mutex); |
2e5a3e79 DB |
94 | if (!(s->features & SS_CAP_STATIC_MAP) && (mem->res == NULL)) { |
95 | mem->res = pcmcia_find_mem_region(0, s->map_size, s->map_size, 0, s); | |
96 | if (mem->res == NULL) { | |
2e55bf6b DB |
97 | dev_printk(KERN_NOTICE, &s->dev, |
98 | "cs: unable to map card memory!\n"); | |
6b8e087b | 99 | mutex_unlock(&s->ops_mutex); |
2e5a3e79 DB |
100 | return NULL; |
101 | } | |
102 | s->cis_virt = NULL; | |
1da177e4 | 103 | } |
2ad0a0a7 | 104 | |
2e5a3e79 DB |
105 | if (!(s->features & SS_CAP_STATIC_MAP) && (!s->cis_virt)) |
106 | s->cis_virt = ioremap(mem->res->start, s->map_size); | |
107 | ||
108 | mem->card_start = card_offset; | |
109 | mem->flags = flags; | |
110 | ||
111 | ret = s->ops->set_mem_map(s, mem); | |
112 | if (ret) { | |
113 | iounmap(s->cis_virt); | |
114 | s->cis_virt = NULL; | |
6b8e087b | 115 | mutex_unlock(&s->ops_mutex); |
2e5a3e79 DB |
116 | return NULL; |
117 | } | |
118 | ||
119 | if (s->features & SS_CAP_STATIC_MAP) { | |
120 | if (s->cis_virt) | |
121 | iounmap(s->cis_virt); | |
122 | s->cis_virt = ioremap(mem->static_start, s->map_size); | |
123 | } | |
124 | ||
6b8e087b | 125 | mutex_unlock(&s->ops_mutex); |
2e5a3e79 | 126 | return s->cis_virt; |
1da177e4 LT |
127 | } |
128 | ||
129 | /*====================================================================== | |
130 | ||
131 | Low-level functions to read and write CIS memory. I think the | |
132 | write routine is only useful for writing one-byte registers. | |
9fea84f4 | 133 | |
1da177e4 LT |
134 | ======================================================================*/ |
135 | ||
136 | /* Bits in attr field */ | |
137 | #define IS_ATTR 1 | |
138 | #define IS_INDIRECT 8 | |
139 | ||
e6ea0b9e | 140 | int pcmcia_read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr, |
1da177e4 LT |
141 | u_int len, void *ptr) |
142 | { | |
143 | void __iomem *sys, *end; | |
144 | unsigned char *buf = ptr; | |
9fea84f4 | 145 | |
d50dbec3 | 146 | dev_dbg(&s->dev, "pcmcia_read_cis_mem(%d, %#x, %u)\n", attr, addr, len); |
1da177e4 LT |
147 | |
148 | if (attr & IS_INDIRECT) { | |
149 | /* Indirect accesses use a bunch of special registers at fixed | |
150 | locations in common memory */ | |
151 | u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN; | |
152 | if (attr & IS_ATTR) { | |
153 | addr *= 2; | |
154 | flags = ICTRL0_AUTOINC; | |
155 | } | |
156 | ||
157 | sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0)); | |
158 | if (!sys) { | |
159 | memset(ptr, 0xff, len); | |
160 | return -1; | |
161 | } | |
162 | ||
163 | writeb(flags, sys+CISREG_ICTRL0); | |
164 | writeb(addr & 0xff, sys+CISREG_IADDR0); | |
165 | writeb((addr>>8) & 0xff, sys+CISREG_IADDR1); | |
166 | writeb((addr>>16) & 0xff, sys+CISREG_IADDR2); | |
167 | writeb((addr>>24) & 0xff, sys+CISREG_IADDR3); | |
168 | for ( ; len > 0; len--, buf++) | |
169 | *buf = readb(sys+CISREG_IDATA0); | |
170 | } else { | |
171 | u_int inc = 1, card_offset, flags; | |
172 | ||
173 | flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0); | |
174 | if (attr) { | |
175 | flags |= MAP_ATTRIB; | |
176 | inc++; | |
177 | addr *= 2; | |
178 | } | |
179 | ||
180 | card_offset = addr & ~(s->map_size-1); | |
181 | while (len) { | |
182 | sys = set_cis_map(s, card_offset, flags); | |
183 | if (!sys) { | |
184 | memset(ptr, 0xff, len); | |
185 | return -1; | |
186 | } | |
187 | end = sys + s->map_size; | |
188 | sys = sys + (addr & (s->map_size-1)); | |
189 | for ( ; len > 0; len--, buf++, sys += inc) { | |
190 | if (sys == end) | |
191 | break; | |
192 | *buf = readb(sys); | |
193 | } | |
194 | card_offset += s->map_size; | |
195 | addr = 0; | |
196 | } | |
197 | } | |
d50dbec3 | 198 | dev_dbg(&s->dev, " %#2.2x %#2.2x %#2.2x %#2.2x ...\n", |
1da177e4 LT |
199 | *(u_char *)(ptr+0), *(u_char *)(ptr+1), |
200 | *(u_char *)(ptr+2), *(u_char *)(ptr+3)); | |
201 | return 0; | |
202 | } | |
1a8d4663 | 203 | |
1da177e4 | 204 | |
e6ea0b9e | 205 | void pcmcia_write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr, |
1da177e4 LT |
206 | u_int len, void *ptr) |
207 | { | |
208 | void __iomem *sys, *end; | |
209 | unsigned char *buf = ptr; | |
9fea84f4 | 210 | |
d50dbec3 | 211 | dev_dbg(&s->dev, "pcmcia_write_cis_mem(%d, %#x, %u)\n", attr, addr, len); |
1da177e4 LT |
212 | |
213 | if (attr & IS_INDIRECT) { | |
214 | /* Indirect accesses use a bunch of special registers at fixed | |
215 | locations in common memory */ | |
216 | u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN; | |
217 | if (attr & IS_ATTR) { | |
218 | addr *= 2; | |
219 | flags = ICTRL0_AUTOINC; | |
220 | } | |
221 | ||
222 | sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0)); | |
223 | if (!sys) | |
224 | return; /* FIXME: Error */ | |
225 | ||
226 | writeb(flags, sys+CISREG_ICTRL0); | |
227 | writeb(addr & 0xff, sys+CISREG_IADDR0); | |
228 | writeb((addr>>8) & 0xff, sys+CISREG_IADDR1); | |
229 | writeb((addr>>16) & 0xff, sys+CISREG_IADDR2); | |
230 | writeb((addr>>24) & 0xff, sys+CISREG_IADDR3); | |
231 | for ( ; len > 0; len--, buf++) | |
232 | writeb(*buf, sys+CISREG_IDATA0); | |
233 | } else { | |
234 | u_int inc = 1, card_offset, flags; | |
235 | ||
236 | flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0); | |
237 | if (attr & IS_ATTR) { | |
238 | flags |= MAP_ATTRIB; | |
239 | inc++; | |
240 | addr *= 2; | |
241 | } | |
242 | ||
243 | card_offset = addr & ~(s->map_size-1); | |
244 | while (len) { | |
245 | sys = set_cis_map(s, card_offset, flags); | |
246 | if (!sys) | |
247 | return; /* FIXME: error */ | |
248 | ||
249 | end = sys + s->map_size; | |
250 | sys = sys + (addr & (s->map_size-1)); | |
251 | for ( ; len > 0; len--, buf++, sys += inc) { | |
252 | if (sys == end) | |
253 | break; | |
254 | writeb(*buf, sys); | |
255 | } | |
256 | card_offset += s->map_size; | |
257 | addr = 0; | |
258 | } | |
259 | } | |
260 | } | |
1a8d4663 | 261 | |
1da177e4 LT |
262 | |
263 | /*====================================================================== | |
264 | ||
265 | This is a wrapper around read_cis_mem, with the same interface, | |
266 | but which caches information, for cards whose CIS may not be | |
267 | readable all the time. | |
9fea84f4 | 268 | |
1da177e4 LT |
269 | ======================================================================*/ |
270 | ||
271 | static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, | |
53efec95 | 272 | size_t len, void *ptr) |
1da177e4 | 273 | { |
57197b9b DB |
274 | struct cis_cache_entry *cis; |
275 | int ret; | |
1da177e4 | 276 | |
57197b9b DB |
277 | if (s->state & SOCKET_CARDBUS) |
278 | return; | |
1da177e4 | 279 | |
8680c4b3 | 280 | mutex_lock(&s->ops_mutex); |
57197b9b DB |
281 | if (s->fake_cis) { |
282 | if (s->fake_cis_len >= addr+len) | |
283 | memcpy(ptr, s->fake_cis+addr, len); | |
284 | else | |
285 | memset(ptr, 0xff, len); | |
8680c4b3 | 286 | mutex_unlock(&s->ops_mutex); |
57197b9b DB |
287 | return; |
288 | } | |
289 | ||
290 | list_for_each_entry(cis, &s->cis_cache, node) { | |
291 | if (cis->addr == addr && cis->len == len && cis->attr == attr) { | |
292 | memcpy(ptr, cis->cache, len); | |
8680c4b3 | 293 | mutex_unlock(&s->ops_mutex); |
57197b9b DB |
294 | return; |
295 | } | |
1da177e4 | 296 | } |
8680c4b3 | 297 | mutex_unlock(&s->ops_mutex); |
1da177e4 | 298 | |
e6ea0b9e | 299 | ret = pcmcia_read_cis_mem(s, attr, addr, len, ptr); |
1da177e4 LT |
300 | |
301 | if (ret == 0) { | |
302 | /* Copy data into the cache */ | |
303 | cis = kmalloc(sizeof(struct cis_cache_entry) + len, GFP_KERNEL); | |
304 | if (cis) { | |
305 | cis->addr = addr; | |
306 | cis->len = len; | |
307 | cis->attr = attr; | |
308 | memcpy(cis->cache, ptr, len); | |
8680c4b3 | 309 | mutex_lock(&s->ops_mutex); |
1da177e4 | 310 | list_add(&cis->node, &s->cis_cache); |
8680c4b3 | 311 | mutex_unlock(&s->ops_mutex); |
1da177e4 LT |
312 | } |
313 | } | |
314 | } | |
315 | ||
316 | static void | |
317 | remove_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, u_int len) | |
318 | { | |
319 | struct cis_cache_entry *cis; | |
320 | ||
8680c4b3 | 321 | mutex_lock(&s->ops_mutex); |
1da177e4 LT |
322 | list_for_each_entry(cis, &s->cis_cache, node) |
323 | if (cis->addr == addr && cis->len == len && cis->attr == attr) { | |
324 | list_del(&cis->node); | |
325 | kfree(cis); | |
326 | break; | |
327 | } | |
8680c4b3 | 328 | mutex_unlock(&s->ops_mutex); |
1da177e4 LT |
329 | } |
330 | ||
904e3777 DB |
331 | /** |
332 | * destroy_cis_cache() - destroy the CIS cache | |
333 | * @s: pcmcia_socket for which CIS cache shall be destroyed | |
334 | * | |
8680c4b3 DB |
335 | * This destroys the CIS cache but keeps any fake CIS alive. Must be |
336 | * called with ops_mutex held. | |
904e3777 DB |
337 | */ |
338 | ||
1da177e4 LT |
339 | void destroy_cis_cache(struct pcmcia_socket *s) |
340 | { | |
341 | struct list_head *l, *n; | |
904e3777 | 342 | struct cis_cache_entry *cis; |
1da177e4 LT |
343 | |
344 | list_for_each_safe(l, n, &s->cis_cache) { | |
904e3777 | 345 | cis = list_entry(l, struct cis_cache_entry, node); |
1da177e4 LT |
346 | list_del(&cis->node); |
347 | kfree(cis); | |
348 | } | |
1da177e4 | 349 | } |
1da177e4 LT |
350 | |
351 | /*====================================================================== | |
352 | ||
353 | This verifies if the CIS of a card matches what is in the CIS | |
354 | cache. | |
9fea84f4 | 355 | |
1da177e4 LT |
356 | ======================================================================*/ |
357 | ||
358 | int verify_cis_cache(struct pcmcia_socket *s) | |
359 | { | |
360 | struct cis_cache_entry *cis; | |
361 | char *buf; | |
362 | ||
57197b9b DB |
363 | if (s->state & SOCKET_CARDBUS) |
364 | return -EINVAL; | |
365 | ||
1da177e4 | 366 | buf = kmalloc(256, GFP_KERNEL); |
e689597f | 367 | if (buf == NULL) { |
1168386a DB |
368 | dev_printk(KERN_WARNING, &s->dev, |
369 | "no memory for verifying CIS\n"); | |
370 | return -ENOMEM; | |
e689597f | 371 | } |
1da177e4 LT |
372 | list_for_each_entry(cis, &s->cis_cache, node) { |
373 | int len = cis->len; | |
374 | ||
375 | if (len > 256) | |
376 | len = 256; | |
57197b9b DB |
377 | |
378 | pcmcia_read_cis_mem(s, cis->attr, cis->addr, len, buf); | |
1da177e4 LT |
379 | |
380 | if (memcmp(buf, cis->cache, len) != 0) { | |
381 | kfree(buf); | |
382 | return -1; | |
383 | } | |
384 | } | |
385 | kfree(buf); | |
386 | return 0; | |
387 | } | |
388 | ||
389 | /*====================================================================== | |
390 | ||
391 | For really bad cards, we provide a facility for uploading a | |
392 | replacement CIS. | |
9fea84f4 | 393 | |
1da177e4 LT |
394 | ======================================================================*/ |
395 | ||
53efec95 DB |
396 | int pcmcia_replace_cis(struct pcmcia_socket *s, |
397 | const u8 *data, const size_t len) | |
1da177e4 | 398 | { |
1168386a DB |
399 | if (len > CISTPL_MAX_CIS_SIZE) { |
400 | dev_printk(KERN_WARNING, &s->dev, "replacement CIS too big\n"); | |
401 | return -EINVAL; | |
402 | } | |
8680c4b3 | 403 | mutex_lock(&s->ops_mutex); |
1168386a DB |
404 | kfree(s->fake_cis); |
405 | s->fake_cis = kmalloc(len, GFP_KERNEL); | |
406 | if (s->fake_cis == NULL) { | |
407 | dev_printk(KERN_WARNING, &s->dev, "no memory to replace CIS\n"); | |
8680c4b3 | 408 | mutex_unlock(&s->ops_mutex); |
1168386a DB |
409 | return -ENOMEM; |
410 | } | |
411 | s->fake_cis_len = len; | |
412 | memcpy(s->fake_cis, data, len); | |
8680c4b3 | 413 | mutex_unlock(&s->ops_mutex); |
1168386a | 414 | return 0; |
1da177e4 LT |
415 | } |
416 | ||
417 | /*====================================================================== | |
418 | ||
419 | The high-level CIS tuple services | |
9fea84f4 | 420 | |
1da177e4 LT |
421 | ======================================================================*/ |
422 | ||
423 | typedef struct tuple_flags { | |
424 | u_int link_space:4; | |
425 | u_int has_link:1; | |
426 | u_int mfc_fn:3; | |
427 | u_int space:4; | |
428 | } tuple_flags; | |
429 | ||
430 | #define LINK_SPACE(f) (((tuple_flags *)(&(f)))->link_space) | |
431 | #define HAS_LINK(f) (((tuple_flags *)(&(f)))->has_link) | |
432 | #define MFC_FN(f) (((tuple_flags *)(&(f)))->mfc_fn) | |
433 | #define SPACE(f) (((tuple_flags *)(&(f)))->space) | |
434 | ||
1da177e4 LT |
435 | int pccard_get_first_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple) |
436 | { | |
437 | if (!s) | |
ffb8da20 | 438 | return -EINVAL; |
57197b9b DB |
439 | |
440 | if (!(s->state & SOCKET_PRESENT) || (s->state & SOCKET_CARDBUS)) | |
3939c1ef | 441 | return -ENODEV; |
1da177e4 | 442 | tuple->TupleLink = tuple->Flags = 0; |
57197b9b DB |
443 | |
444 | /* Assume presence of a LONGLINK_C to address 0 */ | |
445 | tuple->CISOffset = tuple->LinkOffset = 0; | |
446 | SPACE(tuple->Flags) = HAS_LINK(tuple->Flags) = 1; | |
447 | ||
448 | if ((s->functions > 1) && !(tuple->Attributes & TUPLE_RETURN_COMMON)) { | |
1da177e4 LT |
449 | cisdata_t req = tuple->DesiredTuple; |
450 | tuple->DesiredTuple = CISTPL_LONGLINK_MFC; | |
4c89e88b | 451 | if (pccard_get_next_tuple(s, function, tuple) == 0) { |
1da177e4 | 452 | tuple->DesiredTuple = CISTPL_LINKTARGET; |
4c89e88b | 453 | if (pccard_get_next_tuple(s, function, tuple) != 0) |
635d19be | 454 | return -ENOSPC; |
1da177e4 LT |
455 | } else |
456 | tuple->CISOffset = tuple->TupleLink = 0; | |
457 | tuple->DesiredTuple = req; | |
458 | } | |
459 | return pccard_get_next_tuple(s, function, tuple); | |
460 | } | |
1da177e4 LT |
461 | |
462 | static int follow_link(struct pcmcia_socket *s, tuple_t *tuple) | |
463 | { | |
464 | u_char link[5]; | |
465 | u_int ofs; | |
466 | ||
467 | if (MFC_FN(tuple->Flags)) { | |
468 | /* Get indirect link from the MFC tuple */ | |
469 | read_cis_cache(s, LINK_SPACE(tuple->Flags), | |
470 | tuple->LinkOffset, 5, link); | |
6b1e6f63 | 471 | ofs = get_unaligned_le32(link + 1); |
1da177e4 LT |
472 | SPACE(tuple->Flags) = (link[0] == CISTPL_MFC_ATTR); |
473 | /* Move to the next indirect link */ | |
474 | tuple->LinkOffset += 5; | |
475 | MFC_FN(tuple->Flags)--; | |
476 | } else if (HAS_LINK(tuple->Flags)) { | |
477 | ofs = tuple->LinkOffset; | |
478 | SPACE(tuple->Flags) = LINK_SPACE(tuple->Flags); | |
479 | HAS_LINK(tuple->Flags) = 0; | |
480 | } else { | |
481 | return -1; | |
482 | } | |
57197b9b | 483 | if (SPACE(tuple->Flags)) { |
1da177e4 LT |
484 | /* This is ugly, but a common CIS error is to code the long |
485 | link offset incorrectly, so we check the right spot... */ | |
486 | read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link); | |
487 | if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) && | |
488 | (strncmp(link+2, "CIS", 3) == 0)) | |
489 | return ofs; | |
490 | remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5); | |
491 | /* Then, we try the wrong spot... */ | |
492 | ofs = ofs >> 1; | |
493 | } | |
494 | read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link); | |
495 | if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) && | |
496 | (strncmp(link+2, "CIS", 3) == 0)) | |
497 | return ofs; | |
498 | remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5); | |
499 | return -1; | |
500 | } | |
501 | ||
502 | int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple) | |
503 | { | |
504 | u_char link[2], tmp; | |
505 | int ofs, i, attr; | |
506 | ||
507 | if (!s) | |
ffb8da20 | 508 | return -EINVAL; |
57197b9b | 509 | if (!(s->state & SOCKET_PRESENT) || (s->state & SOCKET_CARDBUS)) |
3939c1ef | 510 | return -ENODEV; |
1da177e4 LT |
511 | |
512 | link[1] = tuple->TupleLink; | |
513 | ofs = tuple->CISOffset + tuple->TupleLink; | |
514 | attr = SPACE(tuple->Flags); | |
515 | ||
516 | for (i = 0; i < MAX_TUPLES; i++) { | |
517 | if (link[1] == 0xff) { | |
518 | link[0] = CISTPL_END; | |
519 | } else { | |
520 | read_cis_cache(s, attr, ofs, 2, link); | |
521 | if (link[0] == CISTPL_NULL) { | |
522 | ofs++; continue; | |
523 | } | |
524 | } | |
9fea84f4 | 525 | |
1da177e4 LT |
526 | /* End of chain? Follow long link if possible */ |
527 | if (link[0] == CISTPL_END) { | |
9fea84f4 DB |
528 | ofs = follow_link(s, tuple); |
529 | if (ofs < 0) | |
635d19be | 530 | return -ENOSPC; |
1da177e4 LT |
531 | attr = SPACE(tuple->Flags); |
532 | read_cis_cache(s, attr, ofs, 2, link); | |
533 | } | |
534 | ||
535 | /* Is this a link tuple? Make a note of it */ | |
536 | if ((link[0] == CISTPL_LONGLINK_A) || | |
537 | (link[0] == CISTPL_LONGLINK_C) || | |
538 | (link[0] == CISTPL_LONGLINK_MFC) || | |
539 | (link[0] == CISTPL_LINKTARGET) || | |
540 | (link[0] == CISTPL_INDIRECT) || | |
541 | (link[0] == CISTPL_NO_LINK)) { | |
542 | switch (link[0]) { | |
543 | case CISTPL_LONGLINK_A: | |
544 | HAS_LINK(tuple->Flags) = 1; | |
545 | LINK_SPACE(tuple->Flags) = attr | IS_ATTR; | |
546 | read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset); | |
547 | break; | |
548 | case CISTPL_LONGLINK_C: | |
549 | HAS_LINK(tuple->Flags) = 1; | |
550 | LINK_SPACE(tuple->Flags) = attr & ~IS_ATTR; | |
551 | read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset); | |
552 | break; | |
553 | case CISTPL_INDIRECT: | |
554 | HAS_LINK(tuple->Flags) = 1; | |
555 | LINK_SPACE(tuple->Flags) = IS_ATTR | IS_INDIRECT; | |
556 | tuple->LinkOffset = 0; | |
557 | break; | |
558 | case CISTPL_LONGLINK_MFC: | |
559 | tuple->LinkOffset = ofs + 3; | |
560 | LINK_SPACE(tuple->Flags) = attr; | |
561 | if (function == BIND_FN_ALL) { | |
562 | /* Follow all the MFC links */ | |
563 | read_cis_cache(s, attr, ofs+2, 1, &tmp); | |
564 | MFC_FN(tuple->Flags) = tmp; | |
565 | } else { | |
566 | /* Follow exactly one of the links */ | |
567 | MFC_FN(tuple->Flags) = 1; | |
568 | tuple->LinkOffset += function * 5; | |
569 | } | |
570 | break; | |
571 | case CISTPL_NO_LINK: | |
572 | HAS_LINK(tuple->Flags) = 0; | |
573 | break; | |
574 | } | |
575 | if ((tuple->Attributes & TUPLE_RETURN_LINK) && | |
576 | (tuple->DesiredTuple == RETURN_FIRST_TUPLE)) | |
577 | break; | |
578 | } else | |
579 | if (tuple->DesiredTuple == RETURN_FIRST_TUPLE) | |
580 | break; | |
9fea84f4 | 581 | |
1da177e4 LT |
582 | if (link[0] == tuple->DesiredTuple) |
583 | break; | |
584 | ofs += link[1] + 2; | |
585 | } | |
586 | if (i == MAX_TUPLES) { | |
d50dbec3 | 587 | dev_dbg(&s->dev, "cs: overrun in pcmcia_get_next_tuple\n"); |
635d19be | 588 | return -ENOSPC; |
1da177e4 | 589 | } |
9fea84f4 | 590 | |
1da177e4 LT |
591 | tuple->TupleCode = link[0]; |
592 | tuple->TupleLink = link[1]; | |
593 | tuple->CISOffset = ofs + 2; | |
4c89e88b | 594 | return 0; |
1da177e4 | 595 | } |
1da177e4 LT |
596 | |
597 | /*====================================================================*/ | |
598 | ||
599 | #define _MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
600 | ||
601 | int pccard_get_tuple_data(struct pcmcia_socket *s, tuple_t *tuple) | |
602 | { | |
603 | u_int len; | |
604 | ||
605 | if (!s) | |
ffb8da20 | 606 | return -EINVAL; |
1da177e4 LT |
607 | |
608 | if (tuple->TupleLink < tuple->TupleOffset) | |
635d19be | 609 | return -ENOSPC; |
1da177e4 LT |
610 | len = tuple->TupleLink - tuple->TupleOffset; |
611 | tuple->TupleDataLen = tuple->TupleLink; | |
612 | if (len == 0) | |
4c89e88b | 613 | return 0; |
1da177e4 LT |
614 | read_cis_cache(s, SPACE(tuple->Flags), |
615 | tuple->CISOffset + tuple->TupleOffset, | |
616 | _MIN(len, tuple->TupleDataMax), tuple->TupleData); | |
4c89e88b | 617 | return 0; |
1da177e4 | 618 | } |
1da177e4 LT |
619 | |
620 | ||
621 | /*====================================================================== | |
622 | ||
623 | Parsing routines for individual tuples | |
9fea84f4 | 624 | |
1da177e4 LT |
625 | ======================================================================*/ |
626 | ||
627 | static int parse_device(tuple_t *tuple, cistpl_device_t *device) | |
628 | { | |
629 | int i; | |
630 | u_char scale; | |
631 | u_char *p, *q; | |
632 | ||
633 | p = (u_char *)tuple->TupleData; | |
634 | q = p + tuple->TupleDataLen; | |
635 | ||
636 | device->ndev = 0; | |
637 | for (i = 0; i < CISTPL_MAX_DEVICES; i++) { | |
9fea84f4 DB |
638 | |
639 | if (*p == 0xff) | |
640 | break; | |
1da177e4 LT |
641 | device->dev[i].type = (*p >> 4); |
642 | device->dev[i].wp = (*p & 0x08) ? 1 : 0; | |
643 | switch (*p & 0x07) { | |
9fea84f4 DB |
644 | case 0: |
645 | device->dev[i].speed = 0; | |
646 | break; | |
647 | case 1: | |
648 | device->dev[i].speed = 250; | |
649 | break; | |
650 | case 2: | |
651 | device->dev[i].speed = 200; | |
652 | break; | |
653 | case 3: | |
654 | device->dev[i].speed = 150; | |
655 | break; | |
656 | case 4: | |
657 | device->dev[i].speed = 100; | |
658 | break; | |
1da177e4 | 659 | case 7: |
3f9c5f4c DB |
660 | if (++p == q) |
661 | return -EINVAL; | |
9fea84f4 DB |
662 | device->dev[i].speed = SPEED_CVT(*p); |
663 | while (*p & 0x80) | |
664 | if (++p == q) | |
665 | return -EINVAL; | |
666 | break; | |
1da177e4 | 667 | default: |
9fea84f4 | 668 | return -EINVAL; |
1da177e4 LT |
669 | } |
670 | ||
3f9c5f4c DB |
671 | if (++p == q) |
672 | return -EINVAL; | |
673 | if (*p == 0xff) | |
674 | break; | |
1da177e4 | 675 | scale = *p & 7; |
3f9c5f4c DB |
676 | if (scale == 7) |
677 | return -EINVAL; | |
1da177e4 LT |
678 | device->dev[i].size = ((*p >> 3) + 1) * (512 << (scale*2)); |
679 | device->ndev++; | |
3f9c5f4c DB |
680 | if (++p == q) |
681 | break; | |
1da177e4 | 682 | } |
9fea84f4 | 683 | |
4c89e88b | 684 | return 0; |
1da177e4 LT |
685 | } |
686 | ||
687 | /*====================================================================*/ | |
688 | ||
689 | static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum) | |
690 | { | |
691 | u_char *p; | |
692 | if (tuple->TupleDataLen < 5) | |
3f9c5f4c | 693 | return -EINVAL; |
dc0cf6a2 | 694 | p = (u_char *) tuple->TupleData; |
6b1e6f63 HH |
695 | csum->addr = tuple->CISOffset + get_unaligned_le16(p) - 2; |
696 | csum->len = get_unaligned_le16(p + 2); | |
dc0cf6a2 | 697 | csum->sum = *(p + 4); |
4c89e88b | 698 | return 0; |
1da177e4 LT |
699 | } |
700 | ||
701 | /*====================================================================*/ | |
702 | ||
703 | static int parse_longlink(tuple_t *tuple, cistpl_longlink_t *link) | |
704 | { | |
705 | if (tuple->TupleDataLen < 4) | |
3f9c5f4c | 706 | return -EINVAL; |
6b1e6f63 | 707 | link->addr = get_unaligned_le32(tuple->TupleData); |
4c89e88b | 708 | return 0; |
1da177e4 LT |
709 | } |
710 | ||
711 | /*====================================================================*/ | |
712 | ||
713 | static int parse_longlink_mfc(tuple_t *tuple, | |
714 | cistpl_longlink_mfc_t *link) | |
715 | { | |
716 | u_char *p; | |
717 | int i; | |
9fea84f4 | 718 | |
1da177e4 | 719 | p = (u_char *)tuple->TupleData; |
9fea84f4 | 720 | |
1da177e4 LT |
721 | link->nfn = *p; p++; |
722 | if (tuple->TupleDataLen <= link->nfn*5) | |
3f9c5f4c | 723 | return -EINVAL; |
1da177e4 LT |
724 | for (i = 0; i < link->nfn; i++) { |
725 | link->fn[i].space = *p; p++; | |
6b1e6f63 | 726 | link->fn[i].addr = get_unaligned_le32(p); |
dc0cf6a2 | 727 | p += 4; |
1da177e4 | 728 | } |
4c89e88b | 729 | return 0; |
1da177e4 LT |
730 | } |
731 | ||
732 | /*====================================================================*/ | |
733 | ||
734 | static int parse_strings(u_char *p, u_char *q, int max, | |
735 | char *s, u_char *ofs, u_char *found) | |
736 | { | |
737 | int i, j, ns; | |
738 | ||
3f9c5f4c DB |
739 | if (p == q) |
740 | return -EINVAL; | |
1da177e4 LT |
741 | ns = 0; j = 0; |
742 | for (i = 0; i < max; i++) { | |
3f9c5f4c DB |
743 | if (*p == 0xff) |
744 | break; | |
1da177e4 LT |
745 | ofs[i] = j; |
746 | ns++; | |
747 | for (;;) { | |
748 | s[j++] = (*p == 0xff) ? '\0' : *p; | |
9fea84f4 DB |
749 | if ((*p == '\0') || (*p == 0xff)) |
750 | break; | |
3f9c5f4c DB |
751 | if (++p == q) |
752 | return -EINVAL; | |
1da177e4 | 753 | } |
9fea84f4 DB |
754 | if ((*p == 0xff) || (++p == q)) |
755 | break; | |
1da177e4 LT |
756 | } |
757 | if (found) { | |
758 | *found = ns; | |
4c89e88b | 759 | return 0; |
1da177e4 | 760 | } else { |
3f9c5f4c | 761 | return (ns == max) ? 0 : -EINVAL; |
1da177e4 LT |
762 | } |
763 | } | |
764 | ||
765 | /*====================================================================*/ | |
766 | ||
767 | static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1) | |
768 | { | |
769 | u_char *p, *q; | |
9fea84f4 | 770 | |
1da177e4 LT |
771 | p = (u_char *)tuple->TupleData; |
772 | q = p + tuple->TupleDataLen; | |
9fea84f4 | 773 | |
1da177e4 LT |
774 | vers_1->major = *p; p++; |
775 | vers_1->minor = *p; p++; | |
3f9c5f4c DB |
776 | if (p >= q) |
777 | return -EINVAL; | |
1da177e4 LT |
778 | |
779 | return parse_strings(p, q, CISTPL_VERS_1_MAX_PROD_STRINGS, | |
780 | vers_1->str, vers_1->ofs, &vers_1->ns); | |
781 | } | |
782 | ||
783 | /*====================================================================*/ | |
784 | ||
785 | static int parse_altstr(tuple_t *tuple, cistpl_altstr_t *altstr) | |
786 | { | |
787 | u_char *p, *q; | |
9fea84f4 | 788 | |
1da177e4 LT |
789 | p = (u_char *)tuple->TupleData; |
790 | q = p + tuple->TupleDataLen; | |
9fea84f4 | 791 | |
1da177e4 LT |
792 | return parse_strings(p, q, CISTPL_MAX_ALTSTR_STRINGS, |
793 | altstr->str, altstr->ofs, &altstr->ns); | |
794 | } | |
795 | ||
796 | /*====================================================================*/ | |
797 | ||
798 | static int parse_jedec(tuple_t *tuple, cistpl_jedec_t *jedec) | |
799 | { | |
800 | u_char *p, *q; | |
801 | int nid; | |
802 | ||
803 | p = (u_char *)tuple->TupleData; | |
804 | q = p + tuple->TupleDataLen; | |
805 | ||
806 | for (nid = 0; nid < CISTPL_MAX_DEVICES; nid++) { | |
9fea84f4 DB |
807 | if (p > q-2) |
808 | break; | |
1da177e4 LT |
809 | jedec->id[nid].mfr = p[0]; |
810 | jedec->id[nid].info = p[1]; | |
811 | p += 2; | |
812 | } | |
813 | jedec->nid = nid; | |
4c89e88b | 814 | return 0; |
1da177e4 LT |
815 | } |
816 | ||
817 | /*====================================================================*/ | |
818 | ||
819 | static int parse_manfid(tuple_t *tuple, cistpl_manfid_t *m) | |
820 | { | |
1da177e4 | 821 | if (tuple->TupleDataLen < 4) |
3f9c5f4c | 822 | return -EINVAL; |
6b1e6f63 HH |
823 | m->manf = get_unaligned_le16(tuple->TupleData); |
824 | m->card = get_unaligned_le16(tuple->TupleData + 2); | |
4c89e88b | 825 | return 0; |
1da177e4 LT |
826 | } |
827 | ||
828 | /*====================================================================*/ | |
829 | ||
830 | static int parse_funcid(tuple_t *tuple, cistpl_funcid_t *f) | |
831 | { | |
832 | u_char *p; | |
833 | if (tuple->TupleDataLen < 2) | |
3f9c5f4c | 834 | return -EINVAL; |
1da177e4 LT |
835 | p = (u_char *)tuple->TupleData; |
836 | f->func = p[0]; | |
837 | f->sysinit = p[1]; | |
4c89e88b | 838 | return 0; |
1da177e4 LT |
839 | } |
840 | ||
841 | /*====================================================================*/ | |
842 | ||
843 | static int parse_funce(tuple_t *tuple, cistpl_funce_t *f) | |
844 | { | |
845 | u_char *p; | |
846 | int i; | |
847 | if (tuple->TupleDataLen < 1) | |
3f9c5f4c | 848 | return -EINVAL; |
1da177e4 LT |
849 | p = (u_char *)tuple->TupleData; |
850 | f->type = p[0]; | |
851 | for (i = 1; i < tuple->TupleDataLen; i++) | |
852 | f->data[i-1] = p[i]; | |
4c89e88b | 853 | return 0; |
1da177e4 LT |
854 | } |
855 | ||
856 | /*====================================================================*/ | |
857 | ||
858 | static int parse_config(tuple_t *tuple, cistpl_config_t *config) | |
859 | { | |
860 | int rasz, rmsz, i; | |
861 | u_char *p; | |
862 | ||
863 | p = (u_char *)tuple->TupleData; | |
864 | rasz = *p & 0x03; | |
865 | rmsz = (*p & 0x3c) >> 2; | |
866 | if (tuple->TupleDataLen < rasz+rmsz+4) | |
3f9c5f4c | 867 | return -EINVAL; |
1da177e4 LT |
868 | config->last_idx = *(++p); |
869 | p++; | |
870 | config->base = 0; | |
871 | for (i = 0; i <= rasz; i++) | |
872 | config->base += p[i] << (8*i); | |
873 | p += rasz+1; | |
874 | for (i = 0; i < 4; i++) | |
875 | config->rmask[i] = 0; | |
876 | for (i = 0; i <= rmsz; i++) | |
877 | config->rmask[i>>2] += p[i] << (8*(i%4)); | |
878 | config->subtuples = tuple->TupleDataLen - (rasz+rmsz+4); | |
4c89e88b | 879 | return 0; |
1da177e4 LT |
880 | } |
881 | ||
882 | /*====================================================================== | |
883 | ||
884 | The following routines are all used to parse the nightmarish | |
885 | config table entries. | |
9fea84f4 | 886 | |
1da177e4 LT |
887 | ======================================================================*/ |
888 | ||
889 | static u_char *parse_power(u_char *p, u_char *q, | |
890 | cistpl_power_t *pwr) | |
891 | { | |
892 | int i; | |
893 | u_int scale; | |
894 | ||
9fea84f4 DB |
895 | if (p == q) |
896 | return NULL; | |
1da177e4 LT |
897 | pwr->present = *p; |
898 | pwr->flags = 0; | |
899 | p++; | |
900 | for (i = 0; i < 7; i++) | |
901 | if (pwr->present & (1<<i)) { | |
9fea84f4 DB |
902 | if (p == q) |
903 | return NULL; | |
1da177e4 LT |
904 | pwr->param[i] = POWER_CVT(*p); |
905 | scale = POWER_SCALE(*p); | |
906 | while (*p & 0x80) { | |
9fea84f4 DB |
907 | if (++p == q) |
908 | return NULL; | |
1da177e4 LT |
909 | if ((*p & 0x7f) < 100) |
910 | pwr->param[i] += (*p & 0x7f) * scale / 100; | |
911 | else if (*p == 0x7d) | |
912 | pwr->flags |= CISTPL_POWER_HIGHZ_OK; | |
913 | else if (*p == 0x7e) | |
914 | pwr->param[i] = 0; | |
915 | else if (*p == 0x7f) | |
916 | pwr->flags |= CISTPL_POWER_HIGHZ_REQ; | |
917 | else | |
918 | return NULL; | |
919 | } | |
920 | p++; | |
921 | } | |
922 | return p; | |
923 | } | |
924 | ||
925 | /*====================================================================*/ | |
926 | ||
927 | static u_char *parse_timing(u_char *p, u_char *q, | |
928 | cistpl_timing_t *timing) | |
929 | { | |
930 | u_char scale; | |
931 | ||
9fea84f4 DB |
932 | if (p == q) |
933 | return NULL; | |
1da177e4 LT |
934 | scale = *p; |
935 | if ((scale & 3) != 3) { | |
9fea84f4 DB |
936 | if (++p == q) |
937 | return NULL; | |
1da177e4 LT |
938 | timing->wait = SPEED_CVT(*p); |
939 | timing->waitscale = exponent[scale & 3]; | |
940 | } else | |
941 | timing->wait = 0; | |
942 | scale >>= 2; | |
943 | if ((scale & 7) != 7) { | |
9fea84f4 DB |
944 | if (++p == q) |
945 | return NULL; | |
1da177e4 LT |
946 | timing->ready = SPEED_CVT(*p); |
947 | timing->rdyscale = exponent[scale & 7]; | |
948 | } else | |
949 | timing->ready = 0; | |
950 | scale >>= 3; | |
951 | if (scale != 7) { | |
9fea84f4 DB |
952 | if (++p == q) |
953 | return NULL; | |
1da177e4 LT |
954 | timing->reserved = SPEED_CVT(*p); |
955 | timing->rsvscale = exponent[scale]; | |
956 | } else | |
957 | timing->reserved = 0; | |
958 | p++; | |
959 | return p; | |
960 | } | |
961 | ||
962 | /*====================================================================*/ | |
963 | ||
964 | static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io) | |
965 | { | |
966 | int i, j, bsz, lsz; | |
967 | ||
9fea84f4 DB |
968 | if (p == q) |
969 | return NULL; | |
1da177e4 LT |
970 | io->flags = *p; |
971 | ||
972 | if (!(*p & 0x80)) { | |
973 | io->nwin = 1; | |
974 | io->win[0].base = 0; | |
975 | io->win[0].len = (1 << (io->flags & CISTPL_IO_LINES_MASK)); | |
976 | return p+1; | |
977 | } | |
9fea84f4 DB |
978 | |
979 | if (++p == q) | |
980 | return NULL; | |
1da177e4 LT |
981 | io->nwin = (*p & 0x0f) + 1; |
982 | bsz = (*p & 0x30) >> 4; | |
9fea84f4 DB |
983 | if (bsz == 3) |
984 | bsz++; | |
1da177e4 | 985 | lsz = (*p & 0xc0) >> 6; |
9fea84f4 DB |
986 | if (lsz == 3) |
987 | lsz++; | |
1da177e4 | 988 | p++; |
9fea84f4 | 989 | |
1da177e4 LT |
990 | for (i = 0; i < io->nwin; i++) { |
991 | io->win[i].base = 0; | |
992 | io->win[i].len = 1; | |
993 | for (j = 0; j < bsz; j++, p++) { | |
9fea84f4 DB |
994 | if (p == q) |
995 | return NULL; | |
1da177e4 LT |
996 | io->win[i].base += *p << (j*8); |
997 | } | |
998 | for (j = 0; j < lsz; j++, p++) { | |
9fea84f4 DB |
999 | if (p == q) |
1000 | return NULL; | |
1da177e4 LT |
1001 | io->win[i].len += *p << (j*8); |
1002 | } | |
1003 | } | |
1004 | return p; | |
1005 | } | |
1006 | ||
1007 | /*====================================================================*/ | |
1008 | ||
1009 | static u_char *parse_mem(u_char *p, u_char *q, cistpl_mem_t *mem) | |
1010 | { | |
1011 | int i, j, asz, lsz, has_ha; | |
1012 | u_int len, ca, ha; | |
1013 | ||
9fea84f4 DB |
1014 | if (p == q) |
1015 | return NULL; | |
1da177e4 LT |
1016 | |
1017 | mem->nwin = (*p & 0x07) + 1; | |
1018 | lsz = (*p & 0x18) >> 3; | |
1019 | asz = (*p & 0x60) >> 5; | |
1020 | has_ha = (*p & 0x80); | |
9fea84f4 DB |
1021 | if (++p == q) |
1022 | return NULL; | |
1023 | ||
1da177e4 LT |
1024 | for (i = 0; i < mem->nwin; i++) { |
1025 | len = ca = ha = 0; | |
1026 | for (j = 0; j < lsz; j++, p++) { | |
9fea84f4 DB |
1027 | if (p == q) |
1028 | return NULL; | |
1da177e4 LT |
1029 | len += *p << (j*8); |
1030 | } | |
1031 | for (j = 0; j < asz; j++, p++) { | |
9fea84f4 DB |
1032 | if (p == q) |
1033 | return NULL; | |
1da177e4 LT |
1034 | ca += *p << (j*8); |
1035 | } | |
1036 | if (has_ha) | |
1037 | for (j = 0; j < asz; j++, p++) { | |
9fea84f4 DB |
1038 | if (p == q) |
1039 | return NULL; | |
1da177e4 LT |
1040 | ha += *p << (j*8); |
1041 | } | |
1042 | mem->win[i].len = len << 8; | |
1043 | mem->win[i].card_addr = ca << 8; | |
1044 | mem->win[i].host_addr = ha << 8; | |
1045 | } | |
1046 | return p; | |
1047 | } | |
1048 | ||
1049 | /*====================================================================*/ | |
1050 | ||
1051 | static u_char *parse_irq(u_char *p, u_char *q, cistpl_irq_t *irq) | |
1052 | { | |
3f9c5f4c DB |
1053 | if (p == q) |
1054 | return NULL; | |
1da177e4 LT |
1055 | irq->IRQInfo1 = *p; p++; |
1056 | if (irq->IRQInfo1 & IRQ_INFO2_VALID) { | |
3f9c5f4c DB |
1057 | if (p+2 > q) |
1058 | return NULL; | |
1da177e4 LT |
1059 | irq->IRQInfo2 = (p[1]<<8) + p[0]; |
1060 | p += 2; | |
1061 | } | |
1062 | return p; | |
1063 | } | |
1064 | ||
1065 | /*====================================================================*/ | |
1066 | ||
1067 | static int parse_cftable_entry(tuple_t *tuple, | |
1068 | cistpl_cftable_entry_t *entry) | |
1069 | { | |
1070 | u_char *p, *q, features; | |
1071 | ||
1072 | p = tuple->TupleData; | |
1073 | q = p + tuple->TupleDataLen; | |
1074 | entry->index = *p & 0x3f; | |
1075 | entry->flags = 0; | |
1076 | if (*p & 0x40) | |
1077 | entry->flags |= CISTPL_CFTABLE_DEFAULT; | |
1078 | if (*p & 0x80) { | |
3f9c5f4c DB |
1079 | if (++p == q) |
1080 | return -EINVAL; | |
1da177e4 LT |
1081 | if (*p & 0x10) |
1082 | entry->flags |= CISTPL_CFTABLE_BVDS; | |
1083 | if (*p & 0x20) | |
1084 | entry->flags |= CISTPL_CFTABLE_WP; | |
1085 | if (*p & 0x40) | |
1086 | entry->flags |= CISTPL_CFTABLE_RDYBSY; | |
1087 | if (*p & 0x80) | |
1088 | entry->flags |= CISTPL_CFTABLE_MWAIT; | |
1089 | entry->interface = *p & 0x0f; | |
1090 | } else | |
1091 | entry->interface = 0; | |
1092 | ||
1093 | /* Process optional features */ | |
3f9c5f4c DB |
1094 | if (++p == q) |
1095 | return -EINVAL; | |
1da177e4 LT |
1096 | features = *p; p++; |
1097 | ||
1098 | /* Power options */ | |
1099 | if ((features & 3) > 0) { | |
1100 | p = parse_power(p, q, &entry->vcc); | |
3f9c5f4c DB |
1101 | if (p == NULL) |
1102 | return -EINVAL; | |
1da177e4 LT |
1103 | } else |
1104 | entry->vcc.present = 0; | |
1105 | if ((features & 3) > 1) { | |
1106 | p = parse_power(p, q, &entry->vpp1); | |
3f9c5f4c DB |
1107 | if (p == NULL) |
1108 | return -EINVAL; | |
1da177e4 LT |
1109 | } else |
1110 | entry->vpp1.present = 0; | |
1111 | if ((features & 3) > 2) { | |
1112 | p = parse_power(p, q, &entry->vpp2); | |
3f9c5f4c DB |
1113 | if (p == NULL) |
1114 | return -EINVAL; | |
1da177e4 LT |
1115 | } else |
1116 | entry->vpp2.present = 0; | |
1117 | ||
1118 | /* Timing options */ | |
1119 | if (features & 0x04) { | |
1120 | p = parse_timing(p, q, &entry->timing); | |
3f9c5f4c DB |
1121 | if (p == NULL) |
1122 | return -EINVAL; | |
1da177e4 LT |
1123 | } else { |
1124 | entry->timing.wait = 0; | |
1125 | entry->timing.ready = 0; | |
1126 | entry->timing.reserved = 0; | |
1127 | } | |
9fea84f4 | 1128 | |
1da177e4 LT |
1129 | /* I/O window options */ |
1130 | if (features & 0x08) { | |
1131 | p = parse_io(p, q, &entry->io); | |
3f9c5f4c DB |
1132 | if (p == NULL) |
1133 | return -EINVAL; | |
1da177e4 LT |
1134 | } else |
1135 | entry->io.nwin = 0; | |
9fea84f4 | 1136 | |
1da177e4 LT |
1137 | /* Interrupt options */ |
1138 | if (features & 0x10) { | |
1139 | p = parse_irq(p, q, &entry->irq); | |
3f9c5f4c DB |
1140 | if (p == NULL) |
1141 | return -EINVAL; | |
1da177e4 LT |
1142 | } else |
1143 | entry->irq.IRQInfo1 = 0; | |
1144 | ||
1145 | switch (features & 0x60) { | |
1146 | case 0x00: | |
1147 | entry->mem.nwin = 0; | |
1148 | break; | |
1149 | case 0x20: | |
1150 | entry->mem.nwin = 1; | |
6b1e6f63 | 1151 | entry->mem.win[0].len = get_unaligned_le16(p) << 8; |
1da177e4 LT |
1152 | entry->mem.win[0].card_addr = 0; |
1153 | entry->mem.win[0].host_addr = 0; | |
1154 | p += 2; | |
3f9c5f4c DB |
1155 | if (p > q) |
1156 | return -EINVAL; | |
1da177e4 LT |
1157 | break; |
1158 | case 0x40: | |
1159 | entry->mem.nwin = 1; | |
6b1e6f63 HH |
1160 | entry->mem.win[0].len = get_unaligned_le16(p) << 8; |
1161 | entry->mem.win[0].card_addr = get_unaligned_le16(p + 2) << 8; | |
1da177e4 LT |
1162 | entry->mem.win[0].host_addr = 0; |
1163 | p += 4; | |
3f9c5f4c DB |
1164 | if (p > q) |
1165 | return -EINVAL; | |
1da177e4 LT |
1166 | break; |
1167 | case 0x60: | |
1168 | p = parse_mem(p, q, &entry->mem); | |
3f9c5f4c DB |
1169 | if (p == NULL) |
1170 | return -EINVAL; | |
1da177e4 LT |
1171 | break; |
1172 | } | |
1173 | ||
1174 | /* Misc features */ | |
1175 | if (features & 0x80) { | |
3f9c5f4c DB |
1176 | if (p == q) |
1177 | return -EINVAL; | |
1da177e4 LT |
1178 | entry->flags |= (*p << 8); |
1179 | while (*p & 0x80) | |
3f9c5f4c DB |
1180 | if (++p == q) |
1181 | return -EINVAL; | |
1da177e4 LT |
1182 | p++; |
1183 | } | |
1184 | ||
1185 | entry->subtuples = q-p; | |
9fea84f4 | 1186 | |
4c89e88b | 1187 | return 0; |
1da177e4 LT |
1188 | } |
1189 | ||
1190 | /*====================================================================*/ | |
1191 | ||
1da177e4 LT |
1192 | static int parse_device_geo(tuple_t *tuple, cistpl_device_geo_t *geo) |
1193 | { | |
1194 | u_char *p, *q; | |
1195 | int n; | |
1196 | ||
1197 | p = (u_char *)tuple->TupleData; | |
1198 | q = p + tuple->TupleDataLen; | |
1199 | ||
1200 | for (n = 0; n < CISTPL_MAX_DEVICES; n++) { | |
9fea84f4 DB |
1201 | if (p > q-6) |
1202 | break; | |
1da177e4 LT |
1203 | geo->geo[n].buswidth = p[0]; |
1204 | geo->geo[n].erase_block = 1 << (p[1]-1); | |
1205 | geo->geo[n].read_block = 1 << (p[2]-1); | |
1206 | geo->geo[n].write_block = 1 << (p[3]-1); | |
1207 | geo->geo[n].partition = 1 << (p[4]-1); | |
1208 | geo->geo[n].interleave = 1 << (p[5]-1); | |
1209 | p += 6; | |
1210 | } | |
1211 | geo->ngeo = n; | |
4c89e88b | 1212 | return 0; |
1da177e4 LT |
1213 | } |
1214 | ||
1215 | /*====================================================================*/ | |
1216 | ||
1217 | static int parse_vers_2(tuple_t *tuple, cistpl_vers_2_t *v2) | |
1218 | { | |
1219 | u_char *p, *q; | |
1220 | ||
1221 | if (tuple->TupleDataLen < 10) | |
3f9c5f4c | 1222 | return -EINVAL; |
9fea84f4 | 1223 | |
1da177e4 LT |
1224 | p = tuple->TupleData; |
1225 | q = p + tuple->TupleDataLen; | |
1226 | ||
1227 | v2->vers = p[0]; | |
1228 | v2->comply = p[1]; | |
9fea84f4 | 1229 | v2->dindex = get_unaligned_le16(p + 2); |
1da177e4 LT |
1230 | v2->vspec8 = p[6]; |
1231 | v2->vspec9 = p[7]; | |
1232 | v2->nhdr = p[8]; | |
1233 | p += 9; | |
1234 | return parse_strings(p, q, 2, v2->str, &v2->vendor, NULL); | |
1235 | } | |
1236 | ||
1237 | /*====================================================================*/ | |
1238 | ||
1239 | static int parse_org(tuple_t *tuple, cistpl_org_t *org) | |
1240 | { | |
1241 | u_char *p, *q; | |
1242 | int i; | |
9fea84f4 | 1243 | |
1da177e4 LT |
1244 | p = tuple->TupleData; |
1245 | q = p + tuple->TupleDataLen; | |
3f9c5f4c DB |
1246 | if (p == q) |
1247 | return -EINVAL; | |
1da177e4 | 1248 | org->data_org = *p; |
3f9c5f4c DB |
1249 | if (++p == q) |
1250 | return -EINVAL; | |
1da177e4 LT |
1251 | for (i = 0; i < 30; i++) { |
1252 | org->desc[i] = *p; | |
9fea84f4 DB |
1253 | if (*p == '\0') |
1254 | break; | |
3f9c5f4c DB |
1255 | if (++p == q) |
1256 | return -EINVAL; | |
1da177e4 | 1257 | } |
4c89e88b | 1258 | return 0; |
1da177e4 LT |
1259 | } |
1260 | ||
1261 | /*====================================================================*/ | |
1262 | ||
1263 | static int parse_format(tuple_t *tuple, cistpl_format_t *fmt) | |
1264 | { | |
1265 | u_char *p; | |
1266 | ||
1267 | if (tuple->TupleDataLen < 10) | |
3f9c5f4c | 1268 | return -EINVAL; |
1da177e4 LT |
1269 | |
1270 | p = tuple->TupleData; | |
1271 | ||
1272 | fmt->type = p[0]; | |
1273 | fmt->edc = p[1]; | |
6b1e6f63 HH |
1274 | fmt->offset = get_unaligned_le32(p + 2); |
1275 | fmt->length = get_unaligned_le32(p + 6); | |
1da177e4 | 1276 | |
4c89e88b | 1277 | return 0; |
1da177e4 LT |
1278 | } |
1279 | ||
1280 | /*====================================================================*/ | |
1281 | ||
2f3061eb | 1282 | int pcmcia_parse_tuple(tuple_t *tuple, cisparse_t *parse) |
1da177e4 | 1283 | { |
4c89e88b | 1284 | int ret = 0; |
9fea84f4 | 1285 | |
1da177e4 | 1286 | if (tuple->TupleDataLen > tuple->TupleDataMax) |
3f9c5f4c | 1287 | return -EINVAL; |
1da177e4 LT |
1288 | switch (tuple->TupleCode) { |
1289 | case CISTPL_DEVICE: | |
1290 | case CISTPL_DEVICE_A: | |
1291 | ret = parse_device(tuple, &parse->device); | |
1292 | break; | |
1da177e4 LT |
1293 | case CISTPL_CHECKSUM: |
1294 | ret = parse_checksum(tuple, &parse->checksum); | |
1295 | break; | |
1296 | case CISTPL_LONGLINK_A: | |
1297 | case CISTPL_LONGLINK_C: | |
1298 | ret = parse_longlink(tuple, &parse->longlink); | |
1299 | break; | |
1300 | case CISTPL_LONGLINK_MFC: | |
1301 | ret = parse_longlink_mfc(tuple, &parse->longlink_mfc); | |
1302 | break; | |
1303 | case CISTPL_VERS_1: | |
1304 | ret = parse_vers_1(tuple, &parse->version_1); | |
1305 | break; | |
1306 | case CISTPL_ALTSTR: | |
1307 | ret = parse_altstr(tuple, &parse->altstr); | |
1308 | break; | |
1309 | case CISTPL_JEDEC_A: | |
1310 | case CISTPL_JEDEC_C: | |
1311 | ret = parse_jedec(tuple, &parse->jedec); | |
1312 | break; | |
1313 | case CISTPL_MANFID: | |
1314 | ret = parse_manfid(tuple, &parse->manfid); | |
1315 | break; | |
1316 | case CISTPL_FUNCID: | |
1317 | ret = parse_funcid(tuple, &parse->funcid); | |
1318 | break; | |
1319 | case CISTPL_FUNCE: | |
1320 | ret = parse_funce(tuple, &parse->funce); | |
1321 | break; | |
1322 | case CISTPL_CONFIG: | |
1323 | ret = parse_config(tuple, &parse->config); | |
1324 | break; | |
1325 | case CISTPL_CFTABLE_ENTRY: | |
1326 | ret = parse_cftable_entry(tuple, &parse->cftable_entry); | |
1327 | break; | |
1328 | case CISTPL_DEVICE_GEO: | |
1329 | case CISTPL_DEVICE_GEO_A: | |
1330 | ret = parse_device_geo(tuple, &parse->device_geo); | |
1331 | break; | |
1332 | case CISTPL_VERS_2: | |
1333 | ret = parse_vers_2(tuple, &parse->vers_2); | |
1334 | break; | |
1335 | case CISTPL_ORG: | |
1336 | ret = parse_org(tuple, &parse->org); | |
1337 | break; | |
1338 | case CISTPL_FORMAT: | |
1339 | case CISTPL_FORMAT_A: | |
1340 | ret = parse_format(tuple, &parse->format); | |
1341 | break; | |
1342 | case CISTPL_NO_LINK: | |
1343 | case CISTPL_LINKTARGET: | |
4c89e88b | 1344 | ret = 0; |
1da177e4 LT |
1345 | break; |
1346 | default: | |
de6405e9 | 1347 | ret = -EINVAL; |
1da177e4 LT |
1348 | break; |
1349 | } | |
3f9c5f4c | 1350 | if (ret) |
d50dbec3 | 1351 | pr_debug("parse_tuple failed %d\n", ret); |
1da177e4 LT |
1352 | return ret; |
1353 | } | |
2f3061eb | 1354 | EXPORT_SYMBOL(pcmcia_parse_tuple); |
1da177e4 LT |
1355 | |
1356 | /*====================================================================== | |
1357 | ||
1358 | This is used internally by Card Services to look up CIS stuff. | |
9fea84f4 | 1359 | |
1da177e4 LT |
1360 | ======================================================================*/ |
1361 | ||
1362 | int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function, cisdata_t code, void *parse) | |
1363 | { | |
1364 | tuple_t tuple; | |
1365 | cisdata_t *buf; | |
1366 | int ret; | |
1367 | ||
1368 | buf = kmalloc(256, GFP_KERNEL); | |
1168386a DB |
1369 | if (buf == NULL) { |
1370 | dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n"); | |
1371 | return -ENOMEM; | |
1372 | } | |
1da177e4 | 1373 | tuple.DesiredTuple = code; |
84897fc0 DB |
1374 | tuple.Attributes = 0; |
1375 | if (function == BIND_FN_ALL) | |
1376 | tuple.Attributes = TUPLE_RETURN_COMMON; | |
1da177e4 | 1377 | ret = pccard_get_first_tuple(s, function, &tuple); |
4c89e88b DB |
1378 | if (ret != 0) |
1379 | goto done; | |
1da177e4 LT |
1380 | tuple.TupleData = buf; |
1381 | tuple.TupleOffset = 0; | |
1382 | tuple.TupleDataMax = 255; | |
1383 | ret = pccard_get_tuple_data(s, &tuple); | |
4c89e88b DB |
1384 | if (ret != 0) |
1385 | goto done; | |
2f3061eb | 1386 | ret = pcmcia_parse_tuple(&tuple, parse); |
1da177e4 LT |
1387 | done: |
1388 | kfree(buf); | |
1389 | return ret; | |
1390 | } | |
1da177e4 | 1391 | |
91284224 DB |
1392 | |
1393 | /** | |
1394 | * pccard_loop_tuple() - loop over tuples in the CIS | |
1395 | * @s: the struct pcmcia_socket where the card is inserted | |
1396 | * @function: the device function we loop for | |
1397 | * @code: which CIS code shall we look for? | |
1398 | * @parse: buffer where the tuple shall be parsed (or NULL, if no parse) | |
1399 | * @priv_data: private data to be passed to the loop_tuple function. | |
1400 | * @loop_tuple: function to call for each CIS entry of type @function. IT | |
1401 | * gets passed the raw tuple, the paresed tuple (if @parse is | |
1402 | * set) and @priv_data. | |
1403 | * | |
1404 | * pccard_loop_tuple() loops over all CIS entries of type @function, and | |
1405 | * calls the @loop_tuple function for each entry. If the call to @loop_tuple | |
1406 | * returns 0, the loop exits. Returns 0 on success or errorcode otherwise. | |
1407 | */ | |
1408 | int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function, | |
1409 | cisdata_t code, cisparse_t *parse, void *priv_data, | |
1410 | int (*loop_tuple) (tuple_t *tuple, | |
1411 | cisparse_t *parse, | |
1412 | void *priv_data)) | |
1413 | { | |
1414 | tuple_t tuple; | |
1415 | cisdata_t *buf; | |
1416 | int ret; | |
1417 | ||
1418 | buf = kzalloc(256, GFP_KERNEL); | |
1419 | if (buf == NULL) { | |
1420 | dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n"); | |
1421 | return -ENOMEM; | |
1422 | } | |
1423 | ||
1424 | tuple.TupleData = buf; | |
1425 | tuple.TupleDataMax = 255; | |
1426 | tuple.TupleOffset = 0; | |
1427 | tuple.DesiredTuple = code; | |
1428 | tuple.Attributes = 0; | |
1429 | ||
1430 | ret = pccard_get_first_tuple(s, function, &tuple); | |
1431 | while (!ret) { | |
1432 | if (pccard_get_tuple_data(s, &tuple)) | |
1433 | goto next_entry; | |
1434 | ||
1435 | if (parse) | |
1436 | if (pcmcia_parse_tuple(&tuple, parse)) | |
1437 | goto next_entry; | |
1438 | ||
1439 | ret = loop_tuple(&tuple, parse, priv_data); | |
1440 | if (!ret) | |
1441 | break; | |
1442 | ||
1443 | next_entry: | |
1444 | ret = pccard_get_next_tuple(s, function, &tuple); | |
1445 | } | |
1446 | ||
1447 | kfree(buf); | |
1448 | return ret; | |
1449 | } | |
91284224 DB |
1450 | |
1451 | ||
f131ddc4 DB |
1452 | /** |
1453 | * pccard_validate_cis() - check whether card has a sensible CIS | |
1454 | * @s: the struct pcmcia_socket we are to check | |
1455 | * @info: returns the number of tuples in the (valid) CIS, or 0 | |
1456 | * | |
1457 | * This tries to determine if a card has a sensible CIS. In @info, it | |
1458 | * returns the number of tuples in the CIS, or 0 if the CIS looks bad. The | |
1459 | * checks include making sure several critical tuples are present and | |
1460 | * valid; seeing if the total number of tuples is reasonable; and | |
1461 | * looking for tuples that use reserved codes. | |
1462 | * | |
1463 | * The function returns 0 on success. | |
1464 | */ | |
84897fc0 | 1465 | int pccard_validate_cis(struct pcmcia_socket *s, unsigned int *info) |
1da177e4 | 1466 | { |
f131ddc4 DB |
1467 | tuple_t *tuple; |
1468 | cisparse_t *p; | |
1469 | unsigned int count = 0; | |
1470 | int ret, reserved, dev_ok = 0, ident_ok = 0; | |
1da177e4 | 1471 | |
f131ddc4 DB |
1472 | if (!s) |
1473 | return -EINVAL; | |
1da177e4 | 1474 | |
f131ddc4 | 1475 | /* We do not want to validate the CIS cache... */ |
8680c4b3 | 1476 | mutex_lock(&s->ops_mutex); |
f131ddc4 | 1477 | destroy_cis_cache(s); |
8680c4b3 | 1478 | mutex_unlock(&s->ops_mutex); |
904e3777 | 1479 | |
f131ddc4 DB |
1480 | tuple = kmalloc(sizeof(*tuple), GFP_KERNEL); |
1481 | if (tuple == NULL) { | |
1482 | dev_warn(&s->dev, "no memory to validate CIS\n"); | |
1483 | return -ENOMEM; | |
1484 | } | |
1485 | p = kmalloc(sizeof(*p), GFP_KERNEL); | |
1486 | if (p == NULL) { | |
1487 | kfree(tuple); | |
1488 | dev_warn(&s->dev, "no memory to validate CIS\n"); | |
1489 | return -ENOMEM; | |
1490 | } | |
1da177e4 | 1491 | |
f131ddc4 DB |
1492 | count = reserved = 0; |
1493 | tuple->DesiredTuple = RETURN_FIRST_TUPLE; | |
1494 | tuple->Attributes = TUPLE_RETURN_COMMON; | |
1495 | ret = pccard_get_first_tuple(s, BIND_FN_ALL, tuple); | |
4c89e88b | 1496 | if (ret != 0) |
f131ddc4 DB |
1497 | goto done; |
1498 | ||
1499 | /* First tuple should be DEVICE; we should really have either that | |
1500 | or a CFTABLE_ENTRY of some sort */ | |
1501 | if ((tuple->TupleCode == CISTPL_DEVICE) || | |
1502 | (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY, p)) || | |
1503 | (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY_CB, p))) | |
1504 | dev_ok++; | |
1505 | ||
1506 | /* All cards should have a MANFID tuple, and/or a VERS_1 or VERS_2 | |
1507 | tuple, for card identification. Certain old D-Link and Linksys | |
1508 | cards have only a broken VERS_2 tuple; hence the bogus test. */ | |
1509 | if ((pccard_read_tuple(s, BIND_FN_ALL, CISTPL_MANFID, p) == 0) || | |
1510 | (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_1, p) == 0) || | |
1511 | (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_2, p) != -ENOSPC)) | |
1512 | ident_ok++; | |
1513 | ||
1514 | if (!dev_ok && !ident_ok) | |
1515 | goto done; | |
1516 | ||
1517 | for (count = 1; count < MAX_TUPLES; count++) { | |
1518 | ret = pccard_get_next_tuple(s, BIND_FN_ALL, tuple); | |
1519 | if (ret != 0) | |
1520 | break; | |
1521 | if (((tuple->TupleCode > 0x23) && (tuple->TupleCode < 0x40)) || | |
1522 | ((tuple->TupleCode > 0x47) && (tuple->TupleCode < 0x80)) || | |
1523 | ((tuple->TupleCode > 0x90) && (tuple->TupleCode < 0xff))) | |
1524 | reserved++; | |
1525 | } | |
1526 | if ((count == MAX_TUPLES) || (reserved > 5) || | |
1527 | ((!dev_ok || !ident_ok) && (count > 10))) | |
1528 | count = 0; | |
1529 | ||
1530 | ret = 0; | |
1da177e4 LT |
1531 | |
1532 | done: | |
f131ddc4 DB |
1533 | /* invalidate CIS cache on failure */ |
1534 | if (!dev_ok || !ident_ok || !count) { | |
8680c4b3 | 1535 | mutex_lock(&s->ops_mutex); |
f131ddc4 | 1536 | destroy_cis_cache(s); |
8680c4b3 | 1537 | mutex_unlock(&s->ops_mutex); |
f131ddc4 DB |
1538 | ret = -EIO; |
1539 | } | |
1540 | ||
1541 | if (info) | |
1542 | *info = count; | |
1543 | kfree(tuple); | |
1544 | kfree(p); | |
1545 | return ret; | |
1da177e4 | 1546 | } |
6e7b51a7 DB |
1547 | |
1548 | ||
1549 | #define to_socket(_dev) container_of(_dev, struct pcmcia_socket, dev) | |
1550 | ||
1551 | static ssize_t pccard_extract_cis(struct pcmcia_socket *s, char *buf, | |
1552 | loff_t off, size_t count) | |
1553 | { | |
1554 | tuple_t tuple; | |
1555 | int status, i; | |
1556 | loff_t pointer = 0; | |
1557 | ssize_t ret = 0; | |
1558 | u_char *tuplebuffer; | |
1559 | u_char *tempbuffer; | |
1560 | ||
1561 | tuplebuffer = kmalloc(sizeof(u_char) * 256, GFP_KERNEL); | |
1562 | if (!tuplebuffer) | |
1563 | return -ENOMEM; | |
1564 | ||
1565 | tempbuffer = kmalloc(sizeof(u_char) * 258, GFP_KERNEL); | |
1566 | if (!tempbuffer) { | |
1567 | ret = -ENOMEM; | |
1568 | goto free_tuple; | |
1569 | } | |
1570 | ||
1571 | memset(&tuple, 0, sizeof(tuple_t)); | |
1572 | ||
1573 | tuple.Attributes = TUPLE_RETURN_LINK | TUPLE_RETURN_COMMON; | |
1574 | tuple.DesiredTuple = RETURN_FIRST_TUPLE; | |
1575 | tuple.TupleOffset = 0; | |
1576 | ||
1577 | status = pccard_get_first_tuple(s, BIND_FN_ALL, &tuple); | |
1578 | while (!status) { | |
1579 | tuple.TupleData = tuplebuffer; | |
1580 | tuple.TupleDataMax = 255; | |
1581 | memset(tuplebuffer, 0, sizeof(u_char) * 255); | |
1582 | ||
1583 | status = pccard_get_tuple_data(s, &tuple); | |
1584 | if (status) | |
1585 | break; | |
1586 | ||
1587 | if (off < (pointer + 2 + tuple.TupleDataLen)) { | |
1588 | tempbuffer[0] = tuple.TupleCode & 0xff; | |
1589 | tempbuffer[1] = tuple.TupleLink & 0xff; | |
1590 | for (i = 0; i < tuple.TupleDataLen; i++) | |
1591 | tempbuffer[i + 2] = tuplebuffer[i] & 0xff; | |
1592 | ||
1593 | for (i = 0; i < (2 + tuple.TupleDataLen); i++) { | |
1594 | if (((i + pointer) >= off) && | |
1595 | (i + pointer) < (off + count)) { | |
1596 | buf[ret] = tempbuffer[i]; | |
1597 | ret++; | |
1598 | } | |
1599 | } | |
1600 | } | |
1601 | ||
1602 | pointer += 2 + tuple.TupleDataLen; | |
1603 | ||
1604 | if (pointer >= (off + count)) | |
1605 | break; | |
1606 | ||
1607 | if (tuple.TupleCode == CISTPL_END) | |
1608 | break; | |
1609 | status = pccard_get_next_tuple(s, BIND_FN_ALL, &tuple); | |
1610 | } | |
1611 | ||
1612 | kfree(tempbuffer); | |
1613 | free_tuple: | |
1614 | kfree(tuplebuffer); | |
1615 | ||
1616 | return ret; | |
1617 | } | |
1618 | ||
1619 | ||
1620 | static ssize_t pccard_show_cis(struct kobject *kobj, | |
1621 | struct bin_attribute *bin_attr, | |
1622 | char *buf, loff_t off, size_t count) | |
1623 | { | |
1624 | unsigned int size = 0x200; | |
1625 | ||
1626 | if (off >= size) | |
1627 | count = 0; | |
1628 | else { | |
1629 | struct pcmcia_socket *s; | |
1630 | unsigned int chains; | |
1631 | ||
1632 | if (off + count > size) | |
1633 | count = size - off; | |
1634 | ||
1635 | s = to_socket(container_of(kobj, struct device, kobj)); | |
1636 | ||
1637 | if (!(s->state & SOCKET_PRESENT)) | |
1638 | return -ENODEV; | |
1639 | if (pccard_validate_cis(s, &chains)) | |
1640 | return -EIO; | |
1641 | if (!chains) | |
1642 | return -ENODATA; | |
1643 | ||
1644 | count = pccard_extract_cis(s, buf, off, count); | |
1645 | } | |
1646 | ||
1647 | return count; | |
1648 | } | |
1649 | ||
1650 | ||
1651 | static ssize_t pccard_store_cis(struct kobject *kobj, | |
1652 | struct bin_attribute *bin_attr, | |
1653 | char *buf, loff_t off, size_t count) | |
1654 | { | |
1655 | struct pcmcia_socket *s; | |
1656 | int error; | |
1657 | ||
1658 | s = to_socket(container_of(kobj, struct device, kobj)); | |
1659 | ||
1660 | if (off) | |
1661 | return -EINVAL; | |
1662 | ||
1663 | if (count >= CISTPL_MAX_CIS_SIZE) | |
1664 | return -EINVAL; | |
1665 | ||
1666 | if (!(s->state & SOCKET_PRESENT)) | |
1667 | return -ENODEV; | |
1668 | ||
1669 | error = pcmcia_replace_cis(s, buf, count); | |
1670 | if (error) | |
1671 | return -EIO; | |
1672 | ||
af461fc1 | 1673 | pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY); |
6e7b51a7 DB |
1674 | |
1675 | return count; | |
1676 | } | |
1677 | ||
1678 | ||
1679 | struct bin_attribute pccard_cis_attr = { | |
1680 | .attr = { .name = "cis", .mode = S_IRUGO | S_IWUSR }, | |
1681 | .size = 0x200, | |
1682 | .read = pccard_show_cis, | |
1683 | .write = pccard_store_cis, | |
1684 | }; |