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.sys_start != 0) {
89 s->cis_mem.flags &= ~MAP_ACTIVE;
90 s->ops->set_mem_map(s, &s->cis_mem);
91 if (!(s->features & SS_CAP_STATIC_MAP))
92 release_mem_region(s->cis_mem.sys_start, s->map_size);
94 s->cis_mem.sys_start = 0;
100 * Map the card memory at "card_offset" into virtual space.
101 * If flags & MAP_ATTRIB, map the attribute space, otherwise
102 * map the memory space.
104 static unsigned char *
105 set_cis_map(struct pcmcia_socket *s, unsigned int card_offset, unsigned int flags)
107 pccard_mem_map *mem = &s->cis_mem;
108 if (!(s->features & SS_CAP_STATIC_MAP) &&
109 mem->sys_start == 0) {
112 if (find_mem_region(&mem->sys_start, s->map_size,
113 s->map_size, 0, "card services", s)) {
114 printk(KERN_NOTICE "cs: unable to map card memory!\n");
117 mem->sys_stop = mem->sys_start+s->map_size-1;
118 s->cis_virt = ioremap(mem->sys_start, s->map_size);
120 mem->card_start = card_offset;
122 s->ops->set_mem_map(s, mem);
123 if (s->features & SS_CAP_STATIC_MAP) {
125 iounmap(s->cis_virt);
126 s->cis_virt = ioremap(mem->sys_start, s->map_size);
131 /*======================================================================
133 Low-level functions to read and write CIS memory. I think the
134 write routine is only useful for writing one-byte registers.
136 ======================================================================*/
138 /* Bits in attr field */
140 #define IS_INDIRECT 8
142 int read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
143 u_int len, void *ptr)
145 u_char *sys, *end, *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 u_char *sys, *end, *buf = ptr;
210 cs_dbg(s, 3, "write_cis_mem(%d, %#x, %u)\n", attr, addr, len);
212 if (attr & IS_INDIRECT) {
213 /* Indirect accesses use a bunch of special registers at fixed
214 locations in common memory */
215 u_char flags = ICTRL0_COMMON|ICTRL0_AUTOINC|ICTRL0_BYTEGRAN;
216 if (attr & IS_ATTR) {
218 flags = ICTRL0_AUTOINC;
221 sys = set_cis_map(s, 0, MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0));
223 return; /* FIXME: Error */
225 writeb(flags, sys+CISREG_ICTRL0);
226 writeb(addr & 0xff, sys+CISREG_IADDR0);
227 writeb((addr>>8) & 0xff, sys+CISREG_IADDR1);
228 writeb((addr>>16) & 0xff, sys+CISREG_IADDR2);
229 writeb((addr>>24) & 0xff, sys+CISREG_IADDR3);
230 for ( ; len > 0; len--, buf++)
231 writeb(*buf, sys+CISREG_IDATA0);
233 u_int inc = 1, card_offset, flags;
235 flags = MAP_ACTIVE | ((cis_width) ? MAP_16BIT : 0);
236 if (attr & IS_ATTR) {
242 card_offset = addr & ~(s->map_size-1);
244 sys = set_cis_map(s, card_offset, flags);
246 return; /* FIXME: error */
248 end = sys + s->map_size;
249 sys = sys + (addr & (s->map_size-1));
250 for ( ; len > 0; len--, buf++, sys += inc) {
255 card_offset += s->map_size;
261 /*======================================================================
263 This is a wrapper around read_cis_mem, with the same interface,
264 but which caches information, for cards whose CIS may not be
265 readable all the time.
267 ======================================================================*/
269 static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr,
270 u_int len, void *ptr)
272 struct cis_cache_entry *cis;
276 if (s->fake_cis_len > addr+len)
277 memcpy(ptr, s->fake_cis+addr, len);
279 memset(ptr, 0xff, len);
283 list_for_each_entry(cis, &s->cis_cache, node) {
284 if (cis->addr == addr && cis->len == len && cis->attr == attr) {
285 memcpy(ptr, cis->cache, len);
290 #ifdef CONFIG_CARDBUS
291 if (s->state & SOCKET_CARDBUS)
292 ret = read_cb_mem(s, attr, addr, len, ptr);
295 ret = read_cis_mem(s, attr, addr, len, ptr);
298 /* Copy data into the cache */
299 cis = kmalloc(sizeof(struct cis_cache_entry) + len, GFP_KERNEL);
304 memcpy(cis->cache, ptr, len);
305 list_add(&cis->node, &s->cis_cache);
311 remove_cis_cache(struct pcmcia_socket *s, int attr, u_int addr, u_int len)
313 struct cis_cache_entry *cis;
315 list_for_each_entry(cis, &s->cis_cache, node)
316 if (cis->addr == addr && cis->len == len && cis->attr == attr) {
317 list_del(&cis->node);
323 void destroy_cis_cache(struct pcmcia_socket *s)
325 struct list_head *l, *n;
327 list_for_each_safe(l, n, &s->cis_cache) {
328 struct cis_cache_entry *cis = list_entry(l, struct cis_cache_entry, node);
330 list_del(&cis->node);
335 /*======================================================================
337 This verifies if the CIS of a card matches what is in the CIS
340 ======================================================================*/
342 int verify_cis_cache(struct pcmcia_socket *s)
344 struct cis_cache_entry *cis;
347 buf = kmalloc(256, GFP_KERNEL);
350 list_for_each_entry(cis, &s->cis_cache, node) {
355 #ifdef CONFIG_CARDBUS
356 if (s->state & SOCKET_CARDBUS)
357 read_cb_mem(s, cis->attr, cis->addr, len, buf);
360 read_cis_mem(s, cis->attr, cis->addr, len, buf);
362 if (memcmp(buf, cis->cache, len) != 0) {
371 /*======================================================================
373 For really bad cards, we provide a facility for uploading a
376 ======================================================================*/
378 int pcmcia_replace_cis(client_handle_t handle, cisdump_t *cis)
380 struct pcmcia_socket *s;
381 if (CHECK_HANDLE(handle))
382 return CS_BAD_HANDLE;
384 if (s->fake_cis != NULL) {
388 if (cis->Length > CISTPL_MAX_CIS_SIZE)
390 s->fake_cis = kmalloc(cis->Length, GFP_KERNEL);
391 if (s->fake_cis == NULL)
392 return CS_OUT_OF_RESOURCE;
393 s->fake_cis_len = cis->Length;
394 memcpy(s->fake_cis, cis->Data, cis->Length);
398 /*======================================================================
400 The high-level CIS tuple services
402 ======================================================================*/
404 typedef struct tuple_flags {
411 #define LINK_SPACE(f) (((tuple_flags *)(&(f)))->link_space)
412 #define HAS_LINK(f) (((tuple_flags *)(&(f)))->has_link)
413 #define MFC_FN(f) (((tuple_flags *)(&(f)))->mfc_fn)
414 #define SPACE(f) (((tuple_flags *)(&(f)))->space)
416 int pcmcia_get_next_tuple(client_handle_t handle, tuple_t *tuple);
418 int pcmcia_get_first_tuple(client_handle_t handle, tuple_t *tuple)
420 struct pcmcia_socket *s;
421 if (CHECK_HANDLE(handle))
422 return CS_BAD_HANDLE;
424 if (!(s->state & SOCKET_PRESENT))
426 tuple->TupleLink = tuple->Flags = 0;
427 #ifdef CONFIG_CARDBUS
428 if (s->state & SOCKET_CARDBUS) {
429 struct pci_dev *dev = s->cb_dev;
431 pci_bus_read_config_dword(dev->subordinate, 0, PCI_CARDBUS_CIS, &ptr);
432 tuple->CISOffset = ptr & ~7;
433 SPACE(tuple->Flags) = (ptr & 7);
437 /* Assume presence of a LONGLINK_C to address 0 */
438 tuple->CISOffset = tuple->LinkOffset = 0;
439 SPACE(tuple->Flags) = HAS_LINK(tuple->Flags) = 1;
441 if (!(s->state & SOCKET_CARDBUS) && (s->functions > 1) &&
442 !(tuple->Attributes & TUPLE_RETURN_COMMON)) {
443 cisdata_t req = tuple->DesiredTuple;
444 tuple->DesiredTuple = CISTPL_LONGLINK_MFC;
445 if (pcmcia_get_next_tuple(handle, tuple) == CS_SUCCESS) {
446 tuple->DesiredTuple = CISTPL_LINKTARGET;
447 if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
448 return CS_NO_MORE_ITEMS;
450 tuple->CISOffset = tuple->TupleLink = 0;
451 tuple->DesiredTuple = req;
453 return pcmcia_get_next_tuple(handle, tuple);
456 static int follow_link(struct pcmcia_socket *s, tuple_t *tuple)
461 if (MFC_FN(tuple->Flags)) {
462 /* Get indirect link from the MFC tuple */
463 read_cis_cache(s, LINK_SPACE(tuple->Flags),
464 tuple->LinkOffset, 5, link);
465 ofs = le32_to_cpu(*(u_int *)(link+1));
466 SPACE(tuple->Flags) = (link[0] == CISTPL_MFC_ATTR);
467 /* Move to the next indirect link */
468 tuple->LinkOffset += 5;
469 MFC_FN(tuple->Flags)--;
470 } else if (HAS_LINK(tuple->Flags)) {
471 ofs = tuple->LinkOffset;
472 SPACE(tuple->Flags) = LINK_SPACE(tuple->Flags);
473 HAS_LINK(tuple->Flags) = 0;
477 if (!(s->state & SOCKET_CARDBUS) && SPACE(tuple->Flags)) {
478 /* This is ugly, but a common CIS error is to code the long
479 link offset incorrectly, so we check the right spot... */
480 read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
481 if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
482 (strncmp(link+2, "CIS", 3) == 0))
484 remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
485 /* Then, we try the wrong spot... */
488 read_cis_cache(s, SPACE(tuple->Flags), ofs, 5, link);
489 if ((link[0] == CISTPL_LINKTARGET) && (link[1] >= 3) &&
490 (strncmp(link+2, "CIS", 3) == 0))
492 remove_cis_cache(s, SPACE(tuple->Flags), ofs, 5);
496 int pcmcia_get_next_tuple(client_handle_t handle, tuple_t *tuple)
498 struct pcmcia_socket *s;
502 if (CHECK_HANDLE(handle))
503 return CS_BAD_HANDLE;
505 if (!(s->state & SOCKET_PRESENT))
508 link[1] = tuple->TupleLink;
509 ofs = tuple->CISOffset + tuple->TupleLink;
510 attr = SPACE(tuple->Flags);
512 for (i = 0; i < MAX_TUPLES; i++) {
513 if (link[1] == 0xff) {
514 link[0] = CISTPL_END;
516 read_cis_cache(s, attr, ofs, 2, link);
517 if (link[0] == CISTPL_NULL) {
522 /* End of chain? Follow long link if possible */
523 if (link[0] == CISTPL_END) {
524 if ((ofs = follow_link(s, tuple)) < 0)
525 return CS_NO_MORE_ITEMS;
526 attr = SPACE(tuple->Flags);
527 read_cis_cache(s, attr, ofs, 2, link);
530 /* Is this a link tuple? Make a note of it */
531 if ((link[0] == CISTPL_LONGLINK_A) ||
532 (link[0] == CISTPL_LONGLINK_C) ||
533 (link[0] == CISTPL_LONGLINK_MFC) ||
534 (link[0] == CISTPL_LINKTARGET) ||
535 (link[0] == CISTPL_INDIRECT) ||
536 (link[0] == CISTPL_NO_LINK)) {
538 case CISTPL_LONGLINK_A:
539 HAS_LINK(tuple->Flags) = 1;
540 LINK_SPACE(tuple->Flags) = attr | IS_ATTR;
541 read_cis_cache(s, attr, ofs+2, 4, &tuple->LinkOffset);
543 case CISTPL_LONGLINK_C:
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);
548 case CISTPL_INDIRECT:
549 HAS_LINK(tuple->Flags) = 1;
550 LINK_SPACE(tuple->Flags) = IS_ATTR | IS_INDIRECT;
551 tuple->LinkOffset = 0;
553 case CISTPL_LONGLINK_MFC:
554 tuple->LinkOffset = ofs + 3;
555 LINK_SPACE(tuple->Flags) = attr;
556 if (handle->Function == BIND_FN_ALL) {
557 /* Follow all the MFC links */
558 read_cis_cache(s, attr, ofs+2, 1, &tmp);
559 MFC_FN(tuple->Flags) = tmp;
561 /* Follow exactly one of the links */
562 MFC_FN(tuple->Flags) = 1;
563 tuple->LinkOffset += handle->Function * 5;
567 HAS_LINK(tuple->Flags) = 0;
570 if ((tuple->Attributes & TUPLE_RETURN_LINK) &&
571 (tuple->DesiredTuple == RETURN_FIRST_TUPLE))
574 if (tuple->DesiredTuple == RETURN_FIRST_TUPLE)
577 if (link[0] == tuple->DesiredTuple)
581 if (i == MAX_TUPLES) {
582 cs_dbg(s, 1, "cs: overrun in pcmcia_get_next_tuple\n");
583 return CS_NO_MORE_ITEMS;
586 tuple->TupleCode = link[0];
587 tuple->TupleLink = link[1];
588 tuple->CISOffset = ofs + 2;
592 /*====================================================================*/
594 #define _MIN(a, b) (((a) < (b)) ? (a) : (b))
596 int pcmcia_get_tuple_data(client_handle_t handle, tuple_t *tuple)
598 struct pcmcia_socket *s;
601 if (CHECK_HANDLE(handle))
602 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);
618 /*======================================================================
620 Parsing routines for individual tuples
622 ======================================================================*/
624 static int parse_device(tuple_t *tuple, cistpl_device_t *device)
630 p = (u_char *)tuple->TupleData;
631 q = p + tuple->TupleDataLen;
634 for (i = 0; i < CISTPL_MAX_DEVICES; i++) {
636 if (*p == 0xff) break;
637 device->dev[i].type = (*p >> 4);
638 device->dev[i].wp = (*p & 0x08) ? 1 : 0;
640 case 0: device->dev[i].speed = 0; break;
641 case 1: device->dev[i].speed = 250; break;
642 case 2: device->dev[i].speed = 200; break;
643 case 3: device->dev[i].speed = 150; break;
644 case 4: device->dev[i].speed = 100; break;
646 if (++p == q) return CS_BAD_TUPLE;
647 device->dev[i].speed = SPEED_CVT(*p);
649 if (++p == q) return CS_BAD_TUPLE;
655 if (++p == q) return CS_BAD_TUPLE;
656 if (*p == 0xff) break;
658 if (scale == 7) return CS_BAD_TUPLE;
659 device->dev[i].size = ((*p >> 3) + 1) * (512 << (scale*2));
667 /*====================================================================*/
669 static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum)
672 if (tuple->TupleDataLen < 5)
674 p = (u_char *)tuple->TupleData;
675 csum->addr = tuple->CISOffset+(short)le16_to_cpu(*(u_short *)p)-2;
676 csum->len = le16_to_cpu(*(u_short *)(p + 2));
681 /*====================================================================*/
683 static int parse_longlink(tuple_t *tuple, cistpl_longlink_t *link)
685 if (tuple->TupleDataLen < 4)
687 link->addr = le32_to_cpu(*(u_int *)tuple->TupleData);
691 /*====================================================================*/
693 static int parse_longlink_mfc(tuple_t *tuple,
694 cistpl_longlink_mfc_t *link)
699 p = (u_char *)tuple->TupleData;
702 if (tuple->TupleDataLen <= link->nfn*5)
704 for (i = 0; i < link->nfn; i++) {
705 link->fn[i].space = *p; p++;
706 link->fn[i].addr = le32_to_cpu(*(u_int *)p); p += 4;
711 /*====================================================================*/
713 static int parse_strings(u_char *p, u_char *q, int max,
714 char *s, u_char *ofs, u_char *found)
718 if (p == q) return CS_BAD_TUPLE;
720 for (i = 0; i < max; i++) {
721 if (*p == 0xff) break;
725 s[j++] = (*p == 0xff) ? '\0' : *p;
726 if ((*p == '\0') || (*p == 0xff)) break;
727 if (++p == q) return CS_BAD_TUPLE;
729 if ((*p == 0xff) || (++p == q)) break;
735 return (ns == max) ? CS_SUCCESS : CS_BAD_TUPLE;
739 /*====================================================================*/
741 static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1)
745 p = (u_char *)tuple->TupleData;
746 q = p + tuple->TupleDataLen;
748 vers_1->major = *p; p++;
749 vers_1->minor = *p; p++;
750 if (p >= q) return CS_BAD_TUPLE;
752 return parse_strings(p, q, CISTPL_VERS_1_MAX_PROD_STRINGS,
753 vers_1->str, vers_1->ofs, &vers_1->ns);
756 /*====================================================================*/
758 static int parse_altstr(tuple_t *tuple, cistpl_altstr_t *altstr)
762 p = (u_char *)tuple->TupleData;
763 q = p + tuple->TupleDataLen;
765 return parse_strings(p, q, CISTPL_MAX_ALTSTR_STRINGS,
766 altstr->str, altstr->ofs, &altstr->ns);
769 /*====================================================================*/
771 static int parse_jedec(tuple_t *tuple, cistpl_jedec_t *jedec)
776 p = (u_char *)tuple->TupleData;
777 q = p + tuple->TupleDataLen;
779 for (nid = 0; nid < CISTPL_MAX_DEVICES; nid++) {
781 jedec->id[nid].mfr = p[0];
782 jedec->id[nid].info = p[1];
789 /*====================================================================*/
791 static int parse_manfid(tuple_t *tuple, cistpl_manfid_t *m)
794 if (tuple->TupleDataLen < 4)
796 p = (u_short *)tuple->TupleData;
797 m->manf = le16_to_cpu(p[0]);
798 m->card = le16_to_cpu(p[1]);
802 /*====================================================================*/
804 static int parse_funcid(tuple_t *tuple, cistpl_funcid_t *f)
807 if (tuple->TupleDataLen < 2)
809 p = (u_char *)tuple->TupleData;
815 /*====================================================================*/
817 static int parse_funce(tuple_t *tuple, cistpl_funce_t *f)
821 if (tuple->TupleDataLen < 1)
823 p = (u_char *)tuple->TupleData;
825 for (i = 1; i < tuple->TupleDataLen; i++)
830 /*====================================================================*/
832 static int parse_config(tuple_t *tuple, cistpl_config_t *config)
837 p = (u_char *)tuple->TupleData;
839 rmsz = (*p & 0x3c) >> 2;
840 if (tuple->TupleDataLen < rasz+rmsz+4)
842 config->last_idx = *(++p);
845 for (i = 0; i <= rasz; i++)
846 config->base += p[i] << (8*i);
848 for (i = 0; i < 4; i++)
849 config->rmask[i] = 0;
850 for (i = 0; i <= rmsz; i++)
851 config->rmask[i>>2] += p[i] << (8*(i%4));
852 config->subtuples = tuple->TupleDataLen - (rasz+rmsz+4);
856 /*======================================================================
858 The following routines are all used to parse the nightmarish
859 config table entries.
861 ======================================================================*/
863 static u_char *parse_power(u_char *p, u_char *q,
869 if (p == q) return NULL;
873 for (i = 0; i < 7; i++)
874 if (pwr->present & (1<<i)) {
875 if (p == q) return NULL;
876 pwr->param[i] = POWER_CVT(*p);
877 scale = POWER_SCALE(*p);
879 if (++p == q) return NULL;
880 if ((*p & 0x7f) < 100)
881 pwr->param[i] += (*p & 0x7f) * scale / 100;
883 pwr->flags |= CISTPL_POWER_HIGHZ_OK;
887 pwr->flags |= CISTPL_POWER_HIGHZ_REQ;
896 /*====================================================================*/
898 static u_char *parse_timing(u_char *p, u_char *q,
899 cistpl_timing_t *timing)
903 if (p == q) return NULL;
905 if ((scale & 3) != 3) {
906 if (++p == q) return NULL;
907 timing->wait = SPEED_CVT(*p);
908 timing->waitscale = exponent[scale & 3];
912 if ((scale & 7) != 7) {
913 if (++p == q) return NULL;
914 timing->ready = SPEED_CVT(*p);
915 timing->rdyscale = exponent[scale & 7];
920 if (++p == q) return NULL;
921 timing->reserved = SPEED_CVT(*p);
922 timing->rsvscale = exponent[scale];
924 timing->reserved = 0;
929 /*====================================================================*/
931 static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io)
935 if (p == q) return NULL;
941 io->win[0].len = (1 << (io->flags & CISTPL_IO_LINES_MASK));
945 if (++p == q) return NULL;
946 io->nwin = (*p & 0x0f) + 1;
947 bsz = (*p & 0x30) >> 4;
949 lsz = (*p & 0xc0) >> 6;
953 for (i = 0; i < io->nwin; i++) {
956 for (j = 0; j < bsz; j++, p++) {
957 if (p == q) return NULL;
958 io->win[i].base += *p << (j*8);
960 for (j = 0; j < lsz; j++, p++) {
961 if (p == q) return NULL;
962 io->win[i].len += *p << (j*8);
968 /*====================================================================*/
970 static u_char *parse_mem(u_char *p, u_char *q, cistpl_mem_t *mem)
972 int i, j, asz, lsz, has_ha;
975 if (p == q) return NULL;
977 mem->nwin = (*p & 0x07) + 1;
978 lsz = (*p & 0x18) >> 3;
979 asz = (*p & 0x60) >> 5;
980 has_ha = (*p & 0x80);
981 if (++p == q) return NULL;
983 for (i = 0; i < mem->nwin; i++) {
985 for (j = 0; j < lsz; j++, p++) {
986 if (p == q) return NULL;
989 for (j = 0; j < asz; j++, p++) {
990 if (p == q) return NULL;
994 for (j = 0; j < asz; j++, p++) {
995 if (p == q) return NULL;
998 mem->win[i].len = len << 8;
999 mem->win[i].card_addr = ca << 8;
1000 mem->win[i].host_addr = ha << 8;
1005 /*====================================================================*/
1007 static u_char *parse_irq(u_char *p, u_char *q, cistpl_irq_t *irq)
1009 if (p == q) return NULL;
1010 irq->IRQInfo1 = *p; p++;
1011 if (irq->IRQInfo1 & IRQ_INFO2_VALID) {
1012 if (p+2 > q) return NULL;
1013 irq->IRQInfo2 = (p[1]<<8) + p[0];
1019 /*====================================================================*/
1021 static int parse_cftable_entry(tuple_t *tuple,
1022 cistpl_cftable_entry_t *entry)
1024 u_char *p, *q, features;
1026 p = tuple->TupleData;
1027 q = p + tuple->TupleDataLen;
1028 entry->index = *p & 0x3f;
1031 entry->flags |= CISTPL_CFTABLE_DEFAULT;
1033 if (++p == q) return CS_BAD_TUPLE;
1035 entry->flags |= CISTPL_CFTABLE_BVDS;
1037 entry->flags |= CISTPL_CFTABLE_WP;
1039 entry->flags |= CISTPL_CFTABLE_RDYBSY;
1041 entry->flags |= CISTPL_CFTABLE_MWAIT;
1042 entry->interface = *p & 0x0f;
1044 entry->interface = 0;
1046 /* Process optional features */
1047 if (++p == q) return CS_BAD_TUPLE;
1051 if ((features & 3) > 0) {
1052 p = parse_power(p, q, &entry->vcc);
1053 if (p == NULL) return CS_BAD_TUPLE;
1055 entry->vcc.present = 0;
1056 if ((features & 3) > 1) {
1057 p = parse_power(p, q, &entry->vpp1);
1058 if (p == NULL) return CS_BAD_TUPLE;
1060 entry->vpp1.present = 0;
1061 if ((features & 3) > 2) {
1062 p = parse_power(p, q, &entry->vpp2);
1063 if (p == NULL) return CS_BAD_TUPLE;
1065 entry->vpp2.present = 0;
1067 /* Timing options */
1068 if (features & 0x04) {
1069 p = parse_timing(p, q, &entry->timing);
1070 if (p == NULL) return CS_BAD_TUPLE;
1072 entry->timing.wait = 0;
1073 entry->timing.ready = 0;
1074 entry->timing.reserved = 0;
1077 /* I/O window options */
1078 if (features & 0x08) {
1079 p = parse_io(p, q, &entry->io);
1080 if (p == NULL) return CS_BAD_TUPLE;
1084 /* Interrupt options */
1085 if (features & 0x10) {
1086 p = parse_irq(p, q, &entry->irq);
1087 if (p == NULL) return CS_BAD_TUPLE;
1089 entry->irq.IRQInfo1 = 0;
1091 switch (features & 0x60) {
1093 entry->mem.nwin = 0;
1096 entry->mem.nwin = 1;
1097 entry->mem.win[0].len = le16_to_cpu(*(u_short *)p) << 8;
1098 entry->mem.win[0].card_addr = 0;
1099 entry->mem.win[0].host_addr = 0;
1101 if (p > q) return CS_BAD_TUPLE;
1104 entry->mem.nwin = 1;
1105 entry->mem.win[0].len = le16_to_cpu(*(u_short *)p) << 8;
1106 entry->mem.win[0].card_addr =
1107 le16_to_cpu(*(u_short *)(p+2)) << 8;
1108 entry->mem.win[0].host_addr = 0;
1110 if (p > q) return CS_BAD_TUPLE;
1113 p = parse_mem(p, q, &entry->mem);
1114 if (p == NULL) return CS_BAD_TUPLE;
1119 if (features & 0x80) {
1120 if (p == q) return CS_BAD_TUPLE;
1121 entry->flags |= (*p << 8);
1123 if (++p == q) return CS_BAD_TUPLE;
1127 entry->subtuples = q-p;
1132 /*====================================================================*/
1134 #ifdef CONFIG_CARDBUS
1136 static int parse_bar(tuple_t *tuple, cistpl_bar_t *bar)
1139 if (tuple->TupleDataLen < 6)
1140 return CS_BAD_TUPLE;
1141 p = (u_char *)tuple->TupleData;
1144 bar->size = le32_to_cpu(*(u_int *)p);
1148 static int parse_config_cb(tuple_t *tuple, cistpl_config_t *config)
1152 p = (u_char *)tuple->TupleData;
1153 if ((*p != 3) || (tuple->TupleDataLen < 6))
1154 return CS_BAD_TUPLE;
1155 config->last_idx = *(++p);
1157 config->base = le32_to_cpu(*(u_int *)p);
1158 config->subtuples = tuple->TupleDataLen - 6;
1162 static int parse_cftable_entry_cb(tuple_t *tuple,
1163 cistpl_cftable_entry_cb_t *entry)
1165 u_char *p, *q, features;
1167 p = tuple->TupleData;
1168 q = p + tuple->TupleDataLen;
1169 entry->index = *p & 0x3f;
1172 entry->flags |= CISTPL_CFTABLE_DEFAULT;
1174 /* Process optional features */
1175 if (++p == q) return CS_BAD_TUPLE;
1179 if ((features & 3) > 0) {
1180 p = parse_power(p, q, &entry->vcc);
1181 if (p == NULL) return CS_BAD_TUPLE;
1183 entry->vcc.present = 0;
1184 if ((features & 3) > 1) {
1185 p = parse_power(p, q, &entry->vpp1);
1186 if (p == NULL) return CS_BAD_TUPLE;
1188 entry->vpp1.present = 0;
1189 if ((features & 3) > 2) {
1190 p = parse_power(p, q, &entry->vpp2);
1191 if (p == NULL) return CS_BAD_TUPLE;
1193 entry->vpp2.present = 0;
1195 /* I/O window options */
1196 if (features & 0x08) {
1197 if (p == q) return CS_BAD_TUPLE;
1198 entry->io = *p; p++;
1202 /* Interrupt options */
1203 if (features & 0x10) {
1204 p = parse_irq(p, q, &entry->irq);
1205 if (p == NULL) return CS_BAD_TUPLE;
1207 entry->irq.IRQInfo1 = 0;
1209 if (features & 0x20) {
1210 if (p == q) return CS_BAD_TUPLE;
1211 entry->mem = *p; p++;
1216 if (features & 0x80) {
1217 if (p == q) return CS_BAD_TUPLE;
1218 entry->flags |= (*p << 8);
1220 if (++p == q) return CS_BAD_TUPLE;
1221 entry->flags |= (*p << 16);
1224 if (++p == q) return CS_BAD_TUPLE;
1228 entry->subtuples = q-p;
1235 /*====================================================================*/
1237 static int parse_device_geo(tuple_t *tuple, cistpl_device_geo_t *geo)
1242 p = (u_char *)tuple->TupleData;
1243 q = p + tuple->TupleDataLen;
1245 for (n = 0; n < CISTPL_MAX_DEVICES; n++) {
1247 geo->geo[n].buswidth = p[0];
1248 geo->geo[n].erase_block = 1 << (p[1]-1);
1249 geo->geo[n].read_block = 1 << (p[2]-1);
1250 geo->geo[n].write_block = 1 << (p[3]-1);
1251 geo->geo[n].partition = 1 << (p[4]-1);
1252 geo->geo[n].interleave = 1 << (p[5]-1);
1259 /*====================================================================*/
1261 static int parse_vers_2(tuple_t *tuple, cistpl_vers_2_t *v2)
1265 if (tuple->TupleDataLen < 10)
1266 return CS_BAD_TUPLE;
1268 p = tuple->TupleData;
1269 q = p + tuple->TupleDataLen;
1273 v2->dindex = le16_to_cpu(*(u_short *)(p+2));
1278 return parse_strings(p, q, 2, v2->str, &v2->vendor, NULL);
1281 /*====================================================================*/
1283 static int parse_org(tuple_t *tuple, cistpl_org_t *org)
1288 p = tuple->TupleData;
1289 q = p + tuple->TupleDataLen;
1290 if (p == q) return CS_BAD_TUPLE;
1292 if (++p == q) return CS_BAD_TUPLE;
1293 for (i = 0; i < 30; i++) {
1295 if (*p == '\0') break;
1296 if (++p == q) return CS_BAD_TUPLE;
1301 /*====================================================================*/
1303 static int parse_format(tuple_t *tuple, cistpl_format_t *fmt)
1307 if (tuple->TupleDataLen < 10)
1308 return CS_BAD_TUPLE;
1310 p = tuple->TupleData;
1314 fmt->offset = le32_to_cpu(*(u_int *)(p+2));
1315 fmt->length = le32_to_cpu(*(u_int *)(p+6));
1320 /*====================================================================*/
1322 int pcmcia_parse_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
1324 int ret = CS_SUCCESS;
1326 if (tuple->TupleDataLen > tuple->TupleDataMax)
1327 return CS_BAD_TUPLE;
1328 switch (tuple->TupleCode) {
1330 case CISTPL_DEVICE_A:
1331 ret = parse_device(tuple, &parse->device);
1333 #ifdef CONFIG_CARDBUS
1335 ret = parse_bar(tuple, &parse->bar);
1337 case CISTPL_CONFIG_CB:
1338 ret = parse_config_cb(tuple, &parse->config);
1340 case CISTPL_CFTABLE_ENTRY_CB:
1341 ret = parse_cftable_entry_cb(tuple, &parse->cftable_entry_cb);
1344 case CISTPL_CHECKSUM:
1345 ret = parse_checksum(tuple, &parse->checksum);
1347 case CISTPL_LONGLINK_A:
1348 case CISTPL_LONGLINK_C:
1349 ret = parse_longlink(tuple, &parse->longlink);
1351 case CISTPL_LONGLINK_MFC:
1352 ret = parse_longlink_mfc(tuple, &parse->longlink_mfc);
1355 ret = parse_vers_1(tuple, &parse->version_1);
1358 ret = parse_altstr(tuple, &parse->altstr);
1360 case CISTPL_JEDEC_A:
1361 case CISTPL_JEDEC_C:
1362 ret = parse_jedec(tuple, &parse->jedec);
1365 ret = parse_manfid(tuple, &parse->manfid);
1368 ret = parse_funcid(tuple, &parse->funcid);
1371 ret = parse_funce(tuple, &parse->funce);
1374 ret = parse_config(tuple, &parse->config);
1376 case CISTPL_CFTABLE_ENTRY:
1377 ret = parse_cftable_entry(tuple, &parse->cftable_entry);
1379 case CISTPL_DEVICE_GEO:
1380 case CISTPL_DEVICE_GEO_A:
1381 ret = parse_device_geo(tuple, &parse->device_geo);
1384 ret = parse_vers_2(tuple, &parse->vers_2);
1387 ret = parse_org(tuple, &parse->org);
1390 case CISTPL_FORMAT_A:
1391 ret = parse_format(tuple, &parse->format);
1393 case CISTPL_NO_LINK:
1394 case CISTPL_LINKTARGET:
1398 ret = CS_UNSUPPORTED_FUNCTION;
1404 /*======================================================================
1406 This is used internally by Card Services to look up CIS stuff.
1408 ======================================================================*/
1410 int read_tuple(client_handle_t handle, cisdata_t code, void *parse)
1416 buf = kmalloc(256, GFP_KERNEL);
1418 return CS_OUT_OF_RESOURCE;
1419 tuple.DesiredTuple = code;
1420 tuple.Attributes = TUPLE_RETURN_COMMON;
1421 ret = pcmcia_get_first_tuple(handle, &tuple);
1422 if (ret != CS_SUCCESS) goto done;
1423 tuple.TupleData = buf;
1424 tuple.TupleOffset = 0;
1425 tuple.TupleDataMax = 255;
1426 ret = pcmcia_get_tuple_data(handle, &tuple);
1427 if (ret != CS_SUCCESS) goto done;
1428 ret = pcmcia_parse_tuple(handle, &tuple, parse);
1434 /*======================================================================
1436 This tries to determine if a card has a sensible CIS. It returns
1437 the number of tuples in the CIS, or 0 if the CIS looks bad. The
1438 checks include making sure several critical tuples are present and
1439 valid; seeing if the total number of tuples is reasonable; and
1440 looking for tuples that use reserved codes.
1442 ======================================================================*/
1444 int pcmcia_validate_cis(client_handle_t handle, cisinfo_t *info)
1448 int ret, reserved, dev_ok = 0, ident_ok = 0;
1450 if (CHECK_HANDLE(handle))
1451 return CS_BAD_HANDLE;
1452 tuple = kmalloc(sizeof(*tuple), GFP_KERNEL);
1454 return CS_OUT_OF_RESOURCE;
1455 p = kmalloc(sizeof(*p), GFP_KERNEL);
1458 return CS_OUT_OF_RESOURCE;
1461 info->Chains = reserved = 0;
1462 tuple->DesiredTuple = RETURN_FIRST_TUPLE;
1463 tuple->Attributes = TUPLE_RETURN_COMMON;
1464 ret = pcmcia_get_first_tuple(handle, tuple);
1465 if (ret != CS_SUCCESS)
1468 /* First tuple should be DEVICE; we should really have either that
1469 or a CFTABLE_ENTRY of some sort */
1470 if ((tuple->TupleCode == CISTPL_DEVICE) ||
1471 (read_tuple(handle, CISTPL_CFTABLE_ENTRY, p) == CS_SUCCESS) ||
1472 (read_tuple(handle, CISTPL_CFTABLE_ENTRY_CB, p) == CS_SUCCESS))
1475 /* All cards should have a MANFID tuple, and/or a VERS_1 or VERS_2
1476 tuple, for card identification. Certain old D-Link and Linksys
1477 cards have only a broken VERS_2 tuple; hence the bogus test. */
1478 if ((read_tuple(handle, CISTPL_MANFID, p) == CS_SUCCESS) ||
1479 (read_tuple(handle, CISTPL_VERS_1, p) == CS_SUCCESS) ||
1480 (read_tuple(handle, CISTPL_VERS_2, p) != CS_NO_MORE_ITEMS))
1483 if (!dev_ok && !ident_ok)
1486 for (info->Chains = 1; info->Chains < MAX_TUPLES; info->Chains++) {
1487 ret = pcmcia_get_next_tuple(handle, tuple);
1488 if (ret != CS_SUCCESS) break;
1489 if (((tuple->TupleCode > 0x23) && (tuple->TupleCode < 0x40)) ||
1490 ((tuple->TupleCode > 0x47) && (tuple->TupleCode < 0x80)) ||
1491 ((tuple->TupleCode > 0x90) && (tuple->TupleCode < 0xff)))
1494 if ((info->Chains == MAX_TUPLES) || (reserved > 5) ||
1495 ((!dev_ok || !ident_ok) && (info->Chains > 10)))