1 /*======================================================================
3 PCMCIA Card Information Structure parser
5 cistpl.c 1.99 2002/10/24 06:11:48
7 The contents of this file are subject to the Mozilla Public
8 License Version 1.1 (the "License"); you may not use this file
9 except in compliance with the License. You may obtain a copy of
10 the License at http://www.mozilla.org/MPL/
12 Software distributed under the License is distributed on an "AS
13 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 implied. See the License for the specific language governing
15 rights and limitations under the License.
17 The initial developer of the original code is David A. Hinds
18 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
19 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
21 Alternatively, the contents of this file may be used under the
22 terms of the GNU General Public License version 2 (the "GPL"), in
23 which case the provisions of the GPL are applicable instead of the
24 above. If you wish to allow the use of your version of this file
25 only under the terms of the GPL and not to allow others to use
26 your version of this file under the MPL, indicate your decision
27 by deleting the provisions above and replace them with the notice
28 and other provisions required by the GPL. If you do not delete
29 the provisions above, a recipient may use your version of this
30 file under either the MPL or the GPL.
32 ======================================================================*/
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/kernel.h>
38 #include <linux/string.h>
39 #include <linux/major.h>
40 #include <linux/errno.h>
41 #include <linux/timer.h>
42 #include <linux/slab.h>
44 #include <linux/sched.h>
45 #include <linux/pci.h>
46 #include <linux/ioport.h>
48 #include <asm/byteorder.h>
50 #include <pcmcia/cs_types.h>
51 #include <pcmcia/ss.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/bulkmem.h>
54 #include <pcmcia/cisreg.h>
55 #include <pcmcia/cistpl.h>
56 #include "cs_internal.h"
58 static const u_char mantissa[] = {
59 10, 12, 13, 15, 20, 25, 30, 35,
60 40, 45, 50, 55, 60, 70, 80, 90
63 static const u_int exponent[] = {
64 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
67 /* Convert an extended speed byte to a time in nanoseconds */
68 #define SPEED_CVT(v) \
69 (mantissa[(((v)>>3)&15)-1] * exponent[(v)&7] / 10)
70 /* Convert a power byte to a current in 0.1 microamps */
71 #define POWER_CVT(v) \
72 (mantissa[((v)>>3)&15] * exponent[(v)&7] / 10)
73 #define POWER_SCALE(v) (exponent[(v)&7])
75 /* Upper limit on reasonable # of tuples */
76 #define MAX_TUPLES 200
78 /*====================================================================*/
80 /* Parameters that can be set with 'insmod' */
82 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
84 INT_MODULE_PARM(cis_width, 0); /* 16-bit CIS? */
86 void release_cis_mem(struct pcmcia_socket *s)
88 if (s->cis_mem.flags & MAP_ACTIVE) {
89 s->cis_mem.flags &= ~MAP_ACTIVE;
90 s->ops->set_mem_map(s, &s->cis_mem);
92 release_resource(s->cis_mem.res);
93 kfree(s->cis_mem.res);
94 s->cis_mem.res = NULL;
102 * Map the card memory at "card_offset" into virtual space.
103 * If flags & MAP_ATTRIB, map the attribute space, otherwise
104 * map the memory space.
106 static void __iomem *
107 set_cis_map(struct pcmcia_socket *s, unsigned int card_offset, unsigned int flags)
109 pccard_mem_map *mem = &s->cis_mem;
110 if (!(s->features & SS_CAP_STATIC_MAP) && mem->res == NULL) {
111 mem->res = find_mem_region(0, s->map_size, s->map_size, 0,
113 if (mem->res == NULL) {
114 printk(KERN_NOTICE "cs: unable to map card memory!\n");
117 s->cis_virt = ioremap(mem->res->start, s->map_size);
119 mem->card_start = card_offset;
121 s->ops->set_mem_map(s, mem);
122 if (s->features & SS_CAP_STATIC_MAP) {
124 iounmap(s->cis_virt);
125 s->cis_virt = ioremap(mem->static_start, s->map_size);
130 /*======================================================================
132 Low-level functions to read and write CIS memory. I think the
133 write routine is only useful for writing one-byte registers.
135 ======================================================================*/
137 /* Bits in attr field */
139 #define IS_INDIRECT 8
141 int read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
142 u_int len, void *ptr)
144 void __iomem *sys, *end;
145 unsigned char *buf = ptr;
147 cs_dbg(s, 3, "read_cis_mem(%d, %#x, %u)\n", attr, addr, len);
149 if (attr & IS_INDIRECT) {
150 /* Indirect accesses use a bunch of special registers at fixed
151 locations in common memory */
152 u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN;
153 if (attr & IS_ATTR) {
155 flags = ICTRL0_AUTOINC;
158 sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
160 memset(ptr, 0xff, len);
164 writeb(flags, sys+CISREG_ICTRL0);
165 writeb(addr & 0xff, sys+CISREG_IADDR0);
166 writeb((addr>>8) & 0xff, sys+CISREG_IADDR1);
167 writeb((addr>>16) & 0xff, sys+CISREG_IADDR2);
168 writeb((addr>>24) & 0xff, sys+CISREG_IADDR3);
169 for ( ; len > 0; len--, buf++)
170 *buf = readb(sys+CISREG_IDATA0);
172 u_int inc = 1, card_offset, flags;
174 flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
181 card_offset = addr & ~(s->map_size-1);
183 sys = set_cis_map(s, card_offset, flags);
185 memset(ptr, 0xff, len);
188 end = sys + s->map_size;
189 sys = sys + (addr & (s->map_size-1));
190 for ( ; len > 0; len--, buf++, sys += inc) {
195 card_offset += s->map_size;
199 cs_dbg(s, 3, " %#2.2x %#2.2x %#2.2x %#2.2x ...\n",
200 *(u_char *)(ptr+0), *(u_char *)(ptr+1),
201 *(u_char *)(ptr+2), *(u_char *)(ptr+3));
205 void write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
206 u_int len, void *ptr)
208 void __iomem *sys, *end;
209 unsigned char *buf = ptr;
211 cs_dbg(s, 3, "write_cis_mem(%d, %#x, %u)\n", attr, addr, len);
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) {
219 flags = ICTRL0_AUTOINC;
222 sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
224 return; /* FIXME: Error */
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);
234 u_int inc = 1, card_offset, flags;
236 flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
237 if (attr & IS_ATTR) {
243 card_offset = addr & ~(s->map_size-1);
245 sys = set_cis_map(s, card_offset, flags);
247 return; /* FIXME: error */
249 end = sys + s->map_size;
250 sys = sys + (addr & (s->map_size-1));
251 for ( ; len > 0; len--, buf++, sys += inc) {
256 card_offset += s->map_size;
262 /*======================================================================
264 This is a wrapper around read_cis_mem, with the same interface,
265 but which caches information, for cards whose CIS may not be
266 readable all the time.
268 ======================================================================*/
270 static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr,
271 u_int len, void *ptr)
273 struct cis_cache_entry *cis;
277 if (s->fake_cis_len > addr+len)
278 memcpy(ptr, s->fake_cis+addr, len);
280 memset(ptr, 0xff, len);
284 list_for_each_entry(cis, &s->cis_cache, node) {
285 if (cis->addr == addr && cis->len == len && cis->attr == attr) {
286 memcpy(ptr, cis->cache, len);
291 #ifdef CONFIG_CARDBUS
292 if (s->state & SOCKET_CARDBUS)
293 ret = read_cb_mem(s, attr, addr, len, ptr);
296 ret = read_cis_mem(s, attr, addr, len, ptr);
299 /* Copy data into the cache */
300 cis = kmalloc(sizeof(struct cis_cache_entry) + len, GFP_KERNEL);
305 memcpy(cis->cache, ptr, len);
306 list_add(&cis->node, &s->cis_cache);
312 remove_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, u_int len)
314 struct cis_cache_entry *cis;
316 list_for_each_entry(cis, &s->cis_cache, node)
317 if (cis->addr == addr && cis->len == len && cis->attr == attr) {
318 list_del(&cis->node);
324 void destroy_cis_cache(struct pcmcia_socket *s)
326 struct list_head *l, *n;
328 list_for_each_safe(l, n, &s->cis_cache) {
329 struct cis_cache_entry *cis = list_entry(l, struct cis_cache_entry, node);
331 list_del(&cis->node);
336 * If there was a fake CIS, destroy that as well.
344 /*======================================================================
346 This verifies if the CIS of a card matches what is in the CIS
349 ======================================================================*/
351 int verify_cis_cache(struct pcmcia_socket *s)
353 struct cis_cache_entry *cis;
356 buf = kmalloc(256, GFP_KERNEL);
359 list_for_each_entry(cis, &s->cis_cache, node) {
364 #ifdef CONFIG_CARDBUS
365 if (s->state & SOCKET_CARDBUS)
366 read_cb_mem(s, cis->attr, cis->addr, len, buf);
369 read_cis_mem(s, cis->attr, cis->addr, len, buf);
371 if (memcmp(buf, cis->cache, len) != 0) {
380 /*======================================================================
382 For really bad cards, we provide a facility for uploading a
385 ======================================================================*/
387 int pcmcia_replace_cis(struct pcmcia_socket *s, cisdump_t *cis)
389 if (s->fake_cis != NULL) {
393 if (cis->Length > CISTPL_MAX_CIS_SIZE)
395 s->fake_cis = kmalloc(cis->Length, GFP_KERNEL);
396 if (s->fake_cis == NULL)
397 return CS_OUT_OF_RESOURCE;
398 s->fake_cis_len = cis->Length;
399 memcpy(s->fake_cis, cis->Data, cis->Length);
403 /*======================================================================
405 The high-level CIS tuple services
407 ======================================================================*/
409 typedef struct tuple_flags {
416 #define LINK_SPACE(f) (((tuple_flags *)(&(f)))->link_space)
417 #define HAS_LINK(f) (((tuple_flags *)(&(f)))->has_link)
418 #define MFC_FN(f) (((tuple_flags *)(&(f)))->mfc_fn)
419 #define SPACE(f) (((tuple_flags *)(&(f)))->space)
421 int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int func, tuple_t *tuple);
423 int pccard_get_first_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple)
426 return CS_BAD_HANDLE;
427 if (!(s->state & SOCKET_PRESENT))
429 tuple->TupleLink = tuple->Flags = 0;
430 #ifdef CONFIG_CARDBUS
431 if (s->state & SOCKET_CARDBUS) {
432 struct pci_dev *dev = s->cb_dev;
434 pci_bus_read_config_dword(dev->subordinate, 0, PCI_CARDBUS_CIS, &ptr);
435 tuple->CISOffset = ptr & ~7;
436 SPACE(tuple->Flags) = (ptr & 7);
440 /* Assume presence of a LONGLINK_C to address 0 */
441 tuple->CISOffset = tuple->LinkOffset = 0;
442 SPACE(tuple->Flags) = HAS_LINK(tuple->Flags) = 1;
444 if (!(s->state & SOCKET_CARDBUS) && (s->functions > 1) &&
445 !(tuple->Attributes & TUPLE_RETURN_COMMON)) {
446 cisdata_t req = tuple->DesiredTuple;
447 tuple->DesiredTuple = CISTPL_LONGLINK_MFC;
448 if (pccard_get_next_tuple(s, function, tuple) == CS_SUCCESS) {
449 tuple->DesiredTuple = CISTPL_LINKTARGET;
450 if (pccard_get_next_tuple(s, function, tuple) != CS_SUCCESS)
451 return CS_NO_MORE_ITEMS;
453 tuple->CISOffset = tuple->TupleLink = 0;
454 tuple->DesiredTuple = req;
456 return pccard_get_next_tuple(s, function, tuple);
458 EXPORT_SYMBOL(pccard_get_first_tuple);
460 static int follow_link(struct pcmcia_socket *s, tuple_t *tuple)
465 if (MFC_FN(tuple->Flags)) {
466 /* Get indirect link from the MFC tuple */
467 read_cis_cache(s, LINK_SPACE(tuple->Flags),
468 tuple->LinkOffset, 5, link);
469 ofs = le32_to_cpu(*(u_int *)(link+1));
470 SPACE(tuple->Flags) = (link[0] == CISTPL_MFC_ATTR);
471 /* Move to the next indirect link */
472 tuple->LinkOffset += 5;
473 MFC_FN(tuple->Flags)--;
474 } else if (HAS_LINK(tuple->Flags)) {
475 ofs = tuple->LinkOffset;
476 SPACE(tuple->Flags) = LINK_SPACE(tuple->Flags);
477 HAS_LINK(tuple->Flags) = 0;
481 if (!(s->state & SOCKET_CARDBUS) && SPACE(tuple->Flags)) {
482 /* This is ugly, but a common CIS error is to code the long
483 link offset incorrectly, so we check the right spot... */
484 read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
485 if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
486 (strncmp(link+2, "CIS", 3) == 0))
488 remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
489 /* Then, we try the wrong spot... */
492 read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
493 if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
494 (strncmp(link+2, "CIS", 3) == 0))
496 remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
500 int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple)
506 return CS_BAD_HANDLE;
507 if (!(s->state & SOCKET_PRESENT))
510 link[1] = tuple->TupleLink;
511 ofs = tuple->CISOffset + tuple->TupleLink;
512 attr = SPACE(tuple->Flags);
514 for (i = 0; i < MAX_TUPLES; i++) {
515 if (link[1] == 0xff) {
516 link[0] = CISTPL_END;
518 read_cis_cache(s, attr, ofs, 2, link);
519 if (link[0] == CISTPL_NULL) {
524 /* End of chain? Follow long link if possible */
525 if (link[0] == CISTPL_END) {
526 if ((ofs = follow_link(s, tuple)) < 0)
527 return CS_NO_MORE_ITEMS;
528 attr = SPACE(tuple->Flags);
529 read_cis_cache(s, attr, ofs, 2, link);
532 /* Is this a link tuple? Make a note of it */
533 if ((link[0] == CISTPL_LONGLINK_A) ||
534 (link[0] == CISTPL_LONGLINK_C) ||
535 (link[0] == CISTPL_LONGLINK_MFC) ||
536 (link[0] == CISTPL_LINKTARGET) ||
537 (link[0] == CISTPL_INDIRECT) ||
538 (link[0] == CISTPL_NO_LINK)) {
540 case CISTPL_LONGLINK_A:
541 HAS_LINK(tuple->Flags) = 1;
542 LINK_SPACE(tuple->Flags) = attr | IS_ATTR;
543 read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
545 case CISTPL_LONGLINK_C:
546 HAS_LINK(tuple->Flags) = 1;
547 LINK_SPACE(tuple->Flags) = attr & ~IS_ATTR;
548 read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
550 case CISTPL_INDIRECT:
551 HAS_LINK(tuple->Flags) = 1;
552 LINK_SPACE(tuple->Flags) = IS_ATTR | IS_INDIRECT;
553 tuple->LinkOffset = 0;
555 case CISTPL_LONGLINK_MFC:
556 tuple->LinkOffset = ofs + 3;
557 LINK_SPACE(tuple->Flags) = attr;
558 if (function == BIND_FN_ALL) {
559 /* Follow all the MFC links */
560 read_cis_cache(s, attr, ofs+2, 1, &tmp);
561 MFC_FN(tuple->Flags) = tmp;
563 /* Follow exactly one of the links */
564 MFC_FN(tuple->Flags) = 1;
565 tuple->LinkOffset += function * 5;
569 HAS_LINK(tuple->Flags) = 0;
572 if ((tuple->Attributes & TUPLE_RETURN_LINK) &&
573 (tuple->DesiredTuple == RETURN_FIRST_TUPLE))
576 if (tuple->DesiredTuple == RETURN_FIRST_TUPLE)
579 if (link[0] == tuple->DesiredTuple)
583 if (i == MAX_TUPLES) {
584 cs_dbg(s, 1, "cs: overrun in pcmcia_get_next_tuple\n");
585 return CS_NO_MORE_ITEMS;
588 tuple->TupleCode = link[0];
589 tuple->TupleLink = link[1];
590 tuple->CISOffset = ofs + 2;
593 EXPORT_SYMBOL(pccard_get_next_tuple);
595 /*====================================================================*/
597 #define _MIN(a, b) (((a) < (b)) ? (a) : (b))
599 int pccard_get_tuple_data(struct pcmcia_socket *s, tuple_t *tuple)
604 return CS_BAD_HANDLE;
606 if (tuple->TupleLink < tuple->TupleOffset)
607 return CS_NO_MORE_ITEMS;
608 len = tuple->TupleLink - tuple->TupleOffset;
609 tuple->TupleDataLen = tuple->TupleLink;
612 read_cis_cache(s, SPACE(tuple->Flags),
613 tuple->CISOffset + tuple->TupleOffset,
614 _MIN(len, tuple->TupleDataMax), tuple->TupleData);
617 EXPORT_SYMBOL(pccard_get_tuple_data);
620 /*======================================================================
622 Parsing routines for individual tuples
624 ======================================================================*/
626 static int parse_device(tuple_t *tuple, cistpl_device_t *device)
632 p = (u_char *)tuple->TupleData;
633 q = p + tuple->TupleDataLen;
636 for (i = 0; i < CISTPL_MAX_DEVICES; i++) {
638 if (*p == 0xff) break;
639 device->dev[i].type = (*p >> 4);
640 device->dev[i].wp = (*p & 0x08) ? 1 : 0;
642 case 0: device->dev[i].speed = 0; break;
643 case 1: device->dev[i].speed = 250; break;
644 case 2: device->dev[i].speed = 200; break;
645 case 3: device->dev[i].speed = 150; break;
646 case 4: device->dev[i].speed = 100; break;
648 if (++p == q) return CS_BAD_TUPLE;
649 device->dev[i].speed = SPEED_CVT(*p);
651 if (++p == q) return CS_BAD_TUPLE;
657 if (++p == q) return CS_BAD_TUPLE;
658 if (*p == 0xff) break;
660 if (scale == 7) return CS_BAD_TUPLE;
661 device->dev[i].size = ((*p >> 3) + 1) * (512 << (scale*2));
669 /*====================================================================*/
671 static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum)
674 if (tuple->TupleDataLen < 5)
676 p = (u_char *)tuple->TupleData;
677 csum->addr = tuple->CISOffset+(short)le16_to_cpu(*(u_short *)p)-2;
678 csum->len = le16_to_cpu(*(u_short *)(p + 2));
683 /*====================================================================*/
685 static int parse_longlink(tuple_t *tuple, cistpl_longlink_t *link)
687 if (tuple->TupleDataLen < 4)
689 link->addr = le32_to_cpu(*(u_int *)tuple->TupleData);
693 /*====================================================================*/
695 static int parse_longlink_mfc(tuple_t *tuple,
696 cistpl_longlink_mfc_t *link)
701 p = (u_char *)tuple->TupleData;
704 if (tuple->TupleDataLen <= link->nfn*5)
706 for (i = 0; i < link->nfn; i++) {
707 link->fn[i].space = *p; p++;
708 link->fn[i].addr = le32_to_cpu(*(u_int *)p); p += 4;
713 /*====================================================================*/
715 static int parse_strings(u_char *p, u_char *q, int max,
716 char *s, u_char *ofs, u_char *found)
720 if (p == q) return CS_BAD_TUPLE;
722 for (i = 0; i < max; i++) {
723 if (*p == 0xff) break;
727 s[j++] = (*p == 0xff) ? '\0' : *p;
728 if ((*p == '\0') || (*p == 0xff)) break;
729 if (++p == q) return CS_BAD_TUPLE;
731 if ((*p == 0xff) || (++p == q)) break;
737 return (ns == max) ? CS_SUCCESS : CS_BAD_TUPLE;
741 /*====================================================================*/
743 static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1)
747 p = (u_char *)tuple->TupleData;
748 q = p + tuple->TupleDataLen;
750 vers_1->major = *p; p++;
751 vers_1->minor = *p; p++;
752 if (p >= q) return CS_BAD_TUPLE;
754 return parse_strings(p, q, CISTPL_VERS_1_MAX_PROD_STRINGS,
755 vers_1->str, vers_1->ofs, &vers_1->ns);
758 /*====================================================================*/
760 static int parse_altstr(tuple_t *tuple, cistpl_altstr_t *altstr)
764 p = (u_char *)tuple->TupleData;
765 q = p + tuple->TupleDataLen;
767 return parse_strings(p, q, CISTPL_MAX_ALTSTR_STRINGS,
768 altstr->str, altstr->ofs, &altstr->ns);
771 /*====================================================================*/
773 static int parse_jedec(tuple_t *tuple, cistpl_jedec_t *jedec)
778 p = (u_char *)tuple->TupleData;
779 q = p + tuple->TupleDataLen;
781 for (nid = 0; nid < CISTPL_MAX_DEVICES; nid++) {
783 jedec->id[nid].mfr = p[0];
784 jedec->id[nid].info = p[1];
791 /*====================================================================*/
793 static int parse_manfid(tuple_t *tuple, cistpl_manfid_t *m)
796 if (tuple->TupleDataLen < 4)
798 p = (u_short *)tuple->TupleData;
799 m->manf = le16_to_cpu(p[0]);
800 m->card = le16_to_cpu(p[1]);
804 /*====================================================================*/
806 static int parse_funcid(tuple_t *tuple, cistpl_funcid_t *f)
809 if (tuple->TupleDataLen < 2)
811 p = (u_char *)tuple->TupleData;
817 /*====================================================================*/
819 static int parse_funce(tuple_t *tuple, cistpl_funce_t *f)
823 if (tuple->TupleDataLen < 1)
825 p = (u_char *)tuple->TupleData;
827 for (i = 1; i < tuple->TupleDataLen; i++)
832 /*====================================================================*/
834 static int parse_config(tuple_t *tuple, cistpl_config_t *config)
839 p = (u_char *)tuple->TupleData;
841 rmsz = (*p & 0x3c) >> 2;
842 if (tuple->TupleDataLen < rasz+rmsz+4)
844 config->last_idx = *(++p);
847 for (i = 0; i <= rasz; i++)
848 config->base += p[i] << (8*i);
850 for (i = 0; i < 4; i++)
851 config->rmask[i] = 0;
852 for (i = 0; i <= rmsz; i++)
853 config->rmask[i>>2] += p[i] << (8*(i%4));
854 config->subtuples = tuple->TupleDataLen - (rasz+rmsz+4);
858 /*======================================================================
860 The following routines are all used to parse the nightmarish
861 config table entries.
863 ======================================================================*/
865 static u_char *parse_power(u_char *p, u_char *q,
871 if (p == q) return NULL;
875 for (i = 0; i < 7; i++)
876 if (pwr->present & (1<<i)) {
877 if (p == q) return NULL;
878 pwr->param[i] = POWER_CVT(*p);
879 scale = POWER_SCALE(*p);
881 if (++p == q) return NULL;
882 if ((*p & 0x7f) < 100)
883 pwr->param[i] += (*p & 0x7f) * scale / 100;
885 pwr->flags |= CISTPL_POWER_HIGHZ_OK;
889 pwr->flags |= CISTPL_POWER_HIGHZ_REQ;
898 /*====================================================================*/
900 static u_char *parse_timing(u_char *p, u_char *q,
901 cistpl_timing_t *timing)
905 if (p == q) return NULL;
907 if ((scale & 3) != 3) {
908 if (++p == q) return NULL;
909 timing->wait = SPEED_CVT(*p);
910 timing->waitscale = exponent[scale & 3];
914 if ((scale & 7) != 7) {
915 if (++p == q) return NULL;
916 timing->ready = SPEED_CVT(*p);
917 timing->rdyscale = exponent[scale & 7];
922 if (++p == q) return NULL;
923 timing->reserved = SPEED_CVT(*p);
924 timing->rsvscale = exponent[scale];
926 timing->reserved = 0;
931 /*====================================================================*/
933 static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io)
937 if (p == q) return NULL;
943 io->win[0].len = (1 << (io->flags & CISTPL_IO_LINES_MASK));
947 if (++p == q) return NULL;
948 io->nwin = (*p & 0x0f) + 1;
949 bsz = (*p & 0x30) >> 4;
951 lsz = (*p & 0xc0) >> 6;
955 for (i = 0; i < io->nwin; i++) {
958 for (j = 0; j < bsz; j++, p++) {
959 if (p == q) return NULL;
960 io->win[i].base += *p << (j*8);
962 for (j = 0; j < lsz; j++, p++) {
963 if (p == q) return NULL;
964 io->win[i].len += *p << (j*8);
970 /*====================================================================*/
972 static u_char *parse_mem(u_char *p, u_char *q, cistpl_mem_t *mem)
974 int i, j, asz, lsz, has_ha;
977 if (p == q) return NULL;
979 mem->nwin = (*p & 0x07) + 1;
980 lsz = (*p & 0x18) >> 3;
981 asz = (*p & 0x60) >> 5;
982 has_ha = (*p & 0x80);
983 if (++p == q) return NULL;
985 for (i = 0; i < mem->nwin; i++) {
987 for (j = 0; j < lsz; j++, p++) {
988 if (p == q) return NULL;
991 for (j = 0; j < asz; j++, p++) {
992 if (p == q) return NULL;
996 for (j = 0; j < asz; j++, p++) {
997 if (p == q) return NULL;
1000 mem->win[i].len = len << 8;
1001 mem->win[i].card_addr = ca << 8;
1002 mem->win[i].host_addr = ha << 8;
1007 /*====================================================================*/
1009 static u_char *parse_irq(u_char *p, u_char *q, cistpl_irq_t *irq)
1011 if (p == q) return NULL;
1012 irq->IRQInfo1 = *p; p++;
1013 if (irq->IRQInfo1 & IRQ_INFO2_VALID) {
1014 if (p+2 > q) return NULL;
1015 irq->IRQInfo2 = (p[1]<<8) + p[0];
1021 /*====================================================================*/
1023 static int parse_cftable_entry(tuple_t *tuple,
1024 cistpl_cftable_entry_t *entry)
1026 u_char *p, *q, features;
1028 p = tuple->TupleData;
1029 q = p + tuple->TupleDataLen;
1030 entry->index = *p & 0x3f;
1033 entry->flags |= CISTPL_CFTABLE_DEFAULT;
1035 if (++p == q) return CS_BAD_TUPLE;
1037 entry->flags |= CISTPL_CFTABLE_BVDS;
1039 entry->flags |= CISTPL_CFTABLE_WP;
1041 entry->flags |= CISTPL_CFTABLE_RDYBSY;
1043 entry->flags |= CISTPL_CFTABLE_MWAIT;
1044 entry->interface = *p & 0x0f;
1046 entry->interface = 0;
1048 /* Process optional features */
1049 if (++p == q) return CS_BAD_TUPLE;
1053 if ((features & 3) > 0) {
1054 p = parse_power(p, q, &entry->vcc);
1055 if (p == NULL) return CS_BAD_TUPLE;
1057 entry->vcc.present = 0;
1058 if ((features & 3) > 1) {
1059 p = parse_power(p, q, &entry->vpp1);
1060 if (p == NULL) return CS_BAD_TUPLE;
1062 entry->vpp1.present = 0;
1063 if ((features & 3) > 2) {
1064 p = parse_power(p, q, &entry->vpp2);
1065 if (p == NULL) return CS_BAD_TUPLE;
1067 entry->vpp2.present = 0;
1069 /* Timing options */
1070 if (features & 0x04) {
1071 p = parse_timing(p, q, &entry->timing);
1072 if (p == NULL) return CS_BAD_TUPLE;
1074 entry->timing.wait = 0;
1075 entry->timing.ready = 0;
1076 entry->timing.reserved = 0;
1079 /* I/O window options */
1080 if (features & 0x08) {
1081 p = parse_io(p, q, &entry->io);
1082 if (p == NULL) return CS_BAD_TUPLE;
1086 /* Interrupt options */
1087 if (features & 0x10) {
1088 p = parse_irq(p, q, &entry->irq);
1089 if (p == NULL) return CS_BAD_TUPLE;
1091 entry->irq.IRQInfo1 = 0;
1093 switch (features & 0x60) {
1095 entry->mem.nwin = 0;
1098 entry->mem.nwin = 1;
1099 entry->mem.win[0].len = le16_to_cpu(*(u_short *)p) << 8;
1100 entry->mem.win[0].card_addr = 0;
1101 entry->mem.win[0].host_addr = 0;
1103 if (p > q) return CS_BAD_TUPLE;
1106 entry->mem.nwin = 1;
1107 entry->mem.win[0].len = le16_to_cpu(*(u_short *)p) << 8;
1108 entry->mem.win[0].card_addr =
1109 le16_to_cpu(*(u_short *)(p+2)) << 8;
1110 entry->mem.win[0].host_addr = 0;
1112 if (p > q) return CS_BAD_TUPLE;
1115 p = parse_mem(p, q, &entry->mem);
1116 if (p == NULL) return CS_BAD_TUPLE;
1121 if (features & 0x80) {
1122 if (p == q) return CS_BAD_TUPLE;
1123 entry->flags |= (*p << 8);
1125 if (++p == q) return CS_BAD_TUPLE;
1129 entry->subtuples = q-p;
1134 /*====================================================================*/
1136 #ifdef CONFIG_CARDBUS
1138 static int parse_bar(tuple_t *tuple, cistpl_bar_t *bar)
1141 if (tuple->TupleDataLen < 6)
1142 return CS_BAD_TUPLE;
1143 p = (u_char *)tuple->TupleData;
1146 bar->size = le32_to_cpu(*(u_int *)p);
1150 static int parse_config_cb(tuple_t *tuple, cistpl_config_t *config)
1154 p = (u_char *)tuple->TupleData;
1155 if ((*p != 3) || (tuple->TupleDataLen < 6))
1156 return CS_BAD_TUPLE;
1157 config->last_idx = *(++p);
1159 config->base = le32_to_cpu(*(u_int *)p);
1160 config->subtuples = tuple->TupleDataLen - 6;
1164 static int parse_cftable_entry_cb(tuple_t *tuple,
1165 cistpl_cftable_entry_cb_t *entry)
1167 u_char *p, *q, features;
1169 p = tuple->TupleData;
1170 q = p + tuple->TupleDataLen;
1171 entry->index = *p & 0x3f;
1174 entry->flags |= CISTPL_CFTABLE_DEFAULT;
1176 /* Process optional features */
1177 if (++p == q) return CS_BAD_TUPLE;
1181 if ((features & 3) > 0) {
1182 p = parse_power(p, q, &entry->vcc);
1183 if (p == NULL) return CS_BAD_TUPLE;
1185 entry->vcc.present = 0;
1186 if ((features & 3) > 1) {
1187 p = parse_power(p, q, &entry->vpp1);
1188 if (p == NULL) return CS_BAD_TUPLE;
1190 entry->vpp1.present = 0;
1191 if ((features & 3) > 2) {
1192 p = parse_power(p, q, &entry->vpp2);
1193 if (p == NULL) return CS_BAD_TUPLE;
1195 entry->vpp2.present = 0;
1197 /* I/O window options */
1198 if (features & 0x08) {
1199 if (p == q) return CS_BAD_TUPLE;
1200 entry->io = *p; p++;
1204 /* Interrupt options */
1205 if (features & 0x10) {
1206 p = parse_irq(p, q, &entry->irq);
1207 if (p == NULL) return CS_BAD_TUPLE;
1209 entry->irq.IRQInfo1 = 0;
1211 if (features & 0x20) {
1212 if (p == q) return CS_BAD_TUPLE;
1213 entry->mem = *p; p++;
1218 if (features & 0x80) {
1219 if (p == q) return CS_BAD_TUPLE;
1220 entry->flags |= (*p << 8);
1222 if (++p == q) return CS_BAD_TUPLE;
1223 entry->flags |= (*p << 16);
1226 if (++p == q) return CS_BAD_TUPLE;
1230 entry->subtuples = q-p;
1237 /*====================================================================*/
1239 static int parse_device_geo(tuple_t *tuple, cistpl_device_geo_t *geo)
1244 p = (u_char *)tuple->TupleData;
1245 q = p + tuple->TupleDataLen;
1247 for (n = 0; n < CISTPL_MAX_DEVICES; n++) {
1249 geo->geo[n].buswidth = p[0];
1250 geo->geo[n].erase_block = 1 << (p[1]-1);
1251 geo->geo[n].read_block = 1 << (p[2]-1);
1252 geo->geo[n].write_block = 1 << (p[3]-1);
1253 geo->geo[n].partition = 1 << (p[4]-1);
1254 geo->geo[n].interleave = 1 << (p[5]-1);
1261 /*====================================================================*/
1263 static int parse_vers_2(tuple_t *tuple, cistpl_vers_2_t *v2)
1267 if (tuple->TupleDataLen < 10)
1268 return CS_BAD_TUPLE;
1270 p = tuple->TupleData;
1271 q = p + tuple->TupleDataLen;
1275 v2->dindex = le16_to_cpu(*(u_short *)(p+2));
1280 return parse_strings(p, q, 2, v2->str, &v2->vendor, NULL);
1283 /*====================================================================*/
1285 static int parse_org(tuple_t *tuple, cistpl_org_t *org)
1290 p = tuple->TupleData;
1291 q = p + tuple->TupleDataLen;
1292 if (p == q) return CS_BAD_TUPLE;
1294 if (++p == q) return CS_BAD_TUPLE;
1295 for (i = 0; i < 30; i++) {
1297 if (*p == '\0') break;
1298 if (++p == q) return CS_BAD_TUPLE;
1303 /*====================================================================*/
1305 static int parse_format(tuple_t *tuple, cistpl_format_t *fmt)
1309 if (tuple->TupleDataLen < 10)
1310 return CS_BAD_TUPLE;
1312 p = tuple->TupleData;
1316 fmt->offset = le32_to_cpu(*(u_int *)(p+2));
1317 fmt->length = le32_to_cpu(*(u_int *)(p+6));
1322 /*====================================================================*/
1324 int pccard_parse_tuple(tuple_t *tuple, cisparse_t *parse)
1326 int ret = CS_SUCCESS;
1328 if (tuple->TupleDataLen > tuple->TupleDataMax)
1329 return CS_BAD_TUPLE;
1330 switch (tuple->TupleCode) {
1332 case CISTPL_DEVICE_A:
1333 ret = parse_device(tuple, &parse->device);
1335 #ifdef CONFIG_CARDBUS
1337 ret = parse_bar(tuple, &parse->bar);
1339 case CISTPL_CONFIG_CB:
1340 ret = parse_config_cb(tuple, &parse->config);
1342 case CISTPL_CFTABLE_ENTRY_CB:
1343 ret = parse_cftable_entry_cb(tuple, &parse->cftable_entry_cb);
1346 case CISTPL_CHECKSUM:
1347 ret = parse_checksum(tuple, &parse->checksum);
1349 case CISTPL_LONGLINK_A:
1350 case CISTPL_LONGLINK_C:
1351 ret = parse_longlink(tuple, &parse->longlink);
1353 case CISTPL_LONGLINK_MFC:
1354 ret = parse_longlink_mfc(tuple, &parse->longlink_mfc);
1357 ret = parse_vers_1(tuple, &parse->version_1);
1360 ret = parse_altstr(tuple, &parse->altstr);
1362 case CISTPL_JEDEC_A:
1363 case CISTPL_JEDEC_C:
1364 ret = parse_jedec(tuple, &parse->jedec);
1367 ret = parse_manfid(tuple, &parse->manfid);
1370 ret = parse_funcid(tuple, &parse->funcid);
1373 ret = parse_funce(tuple, &parse->funce);
1376 ret = parse_config(tuple, &parse->config);
1378 case CISTPL_CFTABLE_ENTRY:
1379 ret = parse_cftable_entry(tuple, &parse->cftable_entry);
1381 case CISTPL_DEVICE_GEO:
1382 case CISTPL_DEVICE_GEO_A:
1383 ret = parse_device_geo(tuple, &parse->device_geo);
1386 ret = parse_vers_2(tuple, &parse->vers_2);
1389 ret = parse_org(tuple, &parse->org);
1392 case CISTPL_FORMAT_A:
1393 ret = parse_format(tuple, &parse->format);
1395 case CISTPL_NO_LINK:
1396 case CISTPL_LINKTARGET:
1400 ret = CS_UNSUPPORTED_FUNCTION;
1405 EXPORT_SYMBOL(pccard_parse_tuple);
1407 /*======================================================================
1409 This is used internally by Card Services to look up CIS stuff.
1411 ======================================================================*/
1413 int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function, cisdata_t code, void *parse)
1419 buf = kmalloc(256, GFP_KERNEL);
1421 return CS_OUT_OF_RESOURCE;
1422 tuple.DesiredTuple = code;
1423 tuple.Attributes = TUPLE_RETURN_COMMON;
1424 ret = pccard_get_first_tuple(s, function, &tuple);
1425 if (ret != CS_SUCCESS) goto done;
1426 tuple.TupleData = buf;
1427 tuple.TupleOffset = 0;
1428 tuple.TupleDataMax = 255;
1429 ret = pccard_get_tuple_data(s, &tuple);
1430 if (ret != CS_SUCCESS) goto done;
1431 ret = pccard_parse_tuple(&tuple, parse);
1436 EXPORT_SYMBOL(pccard_read_tuple);
1438 /*======================================================================
1440 This tries to determine if a card has a sensible CIS. It returns
1441 the number of tuples in the CIS, or 0 if the CIS looks bad. The
1442 checks include making sure several critical tuples are present and
1443 valid; seeing if the total number of tuples is reasonable; and
1444 looking for tuples that use reserved codes.
1446 ======================================================================*/
1448 int pccard_validate_cis(struct pcmcia_socket *s, unsigned int function, cisinfo_t *info)
1452 int ret, reserved, dev_ok = 0, ident_ok = 0;
1455 return CS_BAD_HANDLE;
1457 tuple = kmalloc(sizeof(*tuple), GFP_KERNEL);
1459 return CS_OUT_OF_RESOURCE;
1460 p = kmalloc(sizeof(*p), GFP_KERNEL);
1463 return CS_OUT_OF_RESOURCE;
1466 info->Chains = reserved = 0;
1467 tuple->DesiredTuple = RETURN_FIRST_TUPLE;
1468 tuple->Attributes = TUPLE_RETURN_COMMON;
1469 ret = pccard_get_first_tuple(s, function, tuple);
1470 if (ret != CS_SUCCESS)
1473 /* First tuple should be DEVICE; we should really have either that
1474 or a CFTABLE_ENTRY of some sort */
1475 if ((tuple->TupleCode == CISTPL_DEVICE) ||
1476 (pccard_read_tuple(s, function, CISTPL_CFTABLE_ENTRY, p) == CS_SUCCESS) ||
1477 (pccard_read_tuple(s, function, CISTPL_CFTABLE_ENTRY_CB, p) == CS_SUCCESS))
1480 /* All cards should have a MANFID tuple, and/or a VERS_1 or VERS_2
1481 tuple, for card identification. Certain old D-Link and Linksys
1482 cards have only a broken VERS_2 tuple; hence the bogus test. */
1483 if ((pccard_read_tuple(s, function, CISTPL_MANFID, p) == CS_SUCCESS) ||
1484 (pccard_read_tuple(s, function, CISTPL_VERS_1, p) == CS_SUCCESS) ||
1485 (pccard_read_tuple(s, function, CISTPL_VERS_2, p) != CS_NO_MORE_ITEMS))
1488 if (!dev_ok && !ident_ok)
1491 for (info->Chains = 1; info->Chains < MAX_TUPLES; info->Chains++) {
1492 ret = pccard_get_next_tuple(s, function, tuple);
1493 if (ret != CS_SUCCESS) break;
1494 if (((tuple->TupleCode > 0x23) && (tuple->TupleCode < 0x40)) ||
1495 ((tuple->TupleCode > 0x47) && (tuple->TupleCode < 0x80)) ||
1496 ((tuple->TupleCode > 0x90) && (tuple->TupleCode < 0xff)))
1499 if ((info->Chains == MAX_TUPLES) || (reserved > 5) ||
1500 ((!dev_ok || !ident_ok) && (info->Chains > 10)))
1508 EXPORT_SYMBOL(pccard_validate_cis);