MIPS: Alchemy: use 36bit addresses for PCMCIA resources.
[safe/jmp/linux-2.6] / drivers / pcmcia / cistpl.c
index e29a6dd..25b1cd2 100644 (file)
@@ -12,7 +12,6 @@
  * (C) 1999            David A. Hinds
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/kernel.h>
 #include <linux/timer.h>
 #include <linux/slab.h>
 #include <linux/mm.h>
-#include <linux/sched.h>
 #include <linux/pci.h>
 #include <linux/ioport.h>
-#include <asm/io.h>
+#include <linux/io.h>
 #include <asm/byteorder.h>
+#include <asm/unaligned.h>
 
 #include <pcmcia/cs_types.h>
 #include <pcmcia/ss.h>
 #include <pcmcia/cs.h>
-#include <pcmcia/bulkmem.h>
 #include <pcmcia/cisreg.h>
 #include <pcmcia/cistpl.h>
 #include "cs_internal.h"
@@ -60,9 +58,9 @@ static const u_int exponent[] = {
 
 /* Parameters that can be set with 'insmod' */
 
-#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
-
-INT_MODULE_PARM(cis_width,     0);             /* 16-bit CIS? */
+/* 16-bit CIS? */
+static int cis_width;
+module_param(cis_width, int, 0444);
 
 void release_cis_mem(struct pcmcia_socket *s)
 {
@@ -88,44 +86,59 @@ EXPORT_SYMBOL(release_cis_mem);
 static void __iomem *
 set_cis_map(struct pcmcia_socket *s, unsigned int card_offset, unsigned int flags)
 {
-    pccard_mem_map *mem = &s->cis_mem;
-    if (!(s->features & SS_CAP_STATIC_MAP) && mem->res == NULL) {
-       mem->res = find_mem_region(0, s->map_size, s->map_size, 0, s);
-       if (mem->res == NULL) {
-           printk(KERN_NOTICE "cs: unable to map card memory!\n");
-           return NULL;
+       pccard_mem_map *mem = &s->cis_mem;
+       int ret;
+
+       if (!(s->features & SS_CAP_STATIC_MAP) && (mem->res == NULL)) {
+               mem->res = pcmcia_find_mem_region(0, s->map_size, s->map_size, 0, s);
+               if (mem->res == NULL) {
+                       dev_printk(KERN_NOTICE, &s->dev,
+                                  "cs: unable to map card memory!\n");
+                       return NULL;
+               }
+               s->cis_virt = NULL;
        }
-       s->cis_virt = ioremap(mem->res->start, s->map_size);
-    }
-    mem->card_start = card_offset;
-    mem->flags = flags;
-    s->ops->set_mem_map(s, mem);
-    if (s->features & SS_CAP_STATIC_MAP) {
-       if (s->cis_virt)
-           iounmap(s->cis_virt);
-       s->cis_virt = ioremap(mem->static_start, s->map_size);
-    }
-    return s->cis_virt;
+
+       if (!(s->features & SS_CAP_STATIC_MAP) && (!s->cis_virt))
+               s->cis_virt = ioremap(mem->res->start, s->map_size);
+
+       mem->card_start = card_offset;
+       mem->flags = flags;
+
+       ret = s->ops->set_mem_map(s, mem);
+       if (ret) {
+               iounmap(s->cis_virt);
+               s->cis_virt = NULL;
+               return NULL;
+       }
+
+       if (s->features & SS_CAP_STATIC_MAP) {
+               if (s->cis_virt)
+                       iounmap(s->cis_virt);
+               s->cis_virt = ioremap(mem->static_start, s->map_size);
+       }
+
+       return s->cis_virt;
 }
 
 /*======================================================================
 
     Low-level functions to read and write CIS memory.  I think the
     write routine is only useful for writing one-byte registers.
-    
+
 ======================================================================*/
 
 /* Bits in attr field */
 #define IS_ATTR                1
 #define IS_INDIRECT    8
 
-int read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
+int pcmcia_read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
                 u_int len, void *ptr)
 {
     void __iomem *sys, *end;
     unsigned char *buf = ptr;
-    
-    cs_dbg(s, 3, "read_cis_mem(%d, %#x, %u)\n", attr, addr, len);
+
+    dev_dbg(&s->dev, "pcmcia_read_cis_mem(%d, %#x, %u)\n", attr, addr, len);
 
     if (attr & IS_INDIRECT) {
        /* Indirect accesses use a bunch of special registers at fixed
@@ -177,19 +190,21 @@ int read_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
            addr = 0;
        }
     }
-    cs_dbg(s, 3, "  %#2.2x %#2.2x %#2.2x %#2.2x ...\n",
+    dev_dbg(&s->dev, "  %#2.2x %#2.2x %#2.2x %#2.2x ...\n",
          *(u_char *)(ptr+0), *(u_char *)(ptr+1),
          *(u_char *)(ptr+2), *(u_char *)(ptr+3));
     return 0;
 }
+EXPORT_SYMBOL(pcmcia_read_cis_mem);
+
 
-void write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
+void pcmcia_write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
                   u_int len, void *ptr)
 {
     void __iomem *sys, *end;
     unsigned char *buf = ptr;
-    
-    cs_dbg(s, 3, "write_cis_mem(%d, %#x, %u)\n", attr, addr, len);
+
+    dev_dbg(&s->dev, "pcmcia_write_cis_mem(%d, %#x, %u)\n", attr, addr, len);
 
     if (attr & IS_INDIRECT) {
        /* Indirect accesses use a bunch of special registers at fixed
@@ -239,23 +254,25 @@ void write_cis_mem(struct pcmcia_socket *s, int attr, u_int addr,
        }
     }
 }
+EXPORT_SYMBOL(pcmcia_write_cis_mem);
+
 
 /*======================================================================
 
     This is a wrapper around read_cis_mem, with the same interface,
     but which caches information, for cards whose CIS may not be
     readable all the time.
-    
+
 ======================================================================*/
 
 static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr,
-                          u_int len, void *ptr)
+                          size_t len, void *ptr)
 {
     struct cis_cache_entry *cis;
     int ret;
 
     if (s->fake_cis) {
-       if (s->fake_cis_len > addr+len)
+       if (s->fake_cis_len >= addr+len)
            memcpy(ptr, s->fake_cis+addr, len);
        else
            memset(ptr, 0xff, len);
@@ -274,7 +291,7 @@ static void read_cis_cache(struct pcmcia_socket *s, int attr, u_int addr,
        ret = read_cb_mem(s, attr, addr, len, ptr);
     else
 #endif
-       ret = read_cis_mem(s, attr, addr, len, ptr);
+       ret = pcmcia_read_cis_mem(s, attr, addr, len, ptr);
 
        if (ret == 0) {
                /* Copy data into the cache */
@@ -316,10 +333,8 @@ void destroy_cis_cache(struct pcmcia_socket *s)
        /*
         * If there was a fake CIS, destroy that as well.
         */
-       if (s->fake_cis) {
-               kfree(s->fake_cis);
-               s->fake_cis = NULL;
-       }
+       kfree(s->fake_cis);
+       s->fake_cis = NULL;
 }
 EXPORT_SYMBOL(destroy_cis_cache);
 
@@ -327,7 +342,7 @@ EXPORT_SYMBOL(destroy_cis_cache);
 
     This verifies if the CIS of a card matches what is in the CIS
     cache.
-    
+
 ======================================================================*/
 
 int verify_cis_cache(struct pcmcia_socket *s)
@@ -336,8 +351,11 @@ int verify_cis_cache(struct pcmcia_socket *s)
        char *buf;
 
        buf = kmalloc(256, GFP_KERNEL);
-       if (buf == NULL)
-               return -1;
+       if (buf == NULL) {
+               dev_printk(KERN_WARNING, &s->dev,
+                          "no memory for verifying CIS\n");
+               return -ENOMEM;
+       }
        list_for_each_entry(cis, &s->cis_cache, node) {
                int len = cis->len;
 
@@ -348,7 +366,7 @@ int verify_cis_cache(struct pcmcia_socket *s)
                        read_cb_mem(s, cis->attr, cis->addr, len, buf);
                else
 #endif
-                       read_cis_mem(s, cis->attr, cis->addr, len, buf);
+                       pcmcia_read_cis_mem(s, cis->attr, cis->addr, len, buf);
 
                if (memcmp(buf, cis->cache, len) != 0) {
                        kfree(buf);
@@ -363,29 +381,32 @@ int verify_cis_cache(struct pcmcia_socket *s)
 
     For really bad cards, we provide a facility for uploading a
     replacement CIS.
-    
+
 ======================================================================*/
 
-int pcmcia_replace_cis(struct pcmcia_socket *s, cisdump_t *cis)
+int pcmcia_replace_cis(struct pcmcia_socket *s,
+                      const u8 *data, const size_t len)
 {
-    if (s->fake_cis != NULL) {
+       if (len > CISTPL_MAX_CIS_SIZE) {
+               dev_printk(KERN_WARNING, &s->dev, "replacement CIS too big\n");
+               return -EINVAL;
+       }
        kfree(s->fake_cis);
-       s->fake_cis = NULL;
-    }
-    if (cis->Length > CISTPL_MAX_CIS_SIZE)
-       return CS_BAD_SIZE;
-    s->fake_cis = kmalloc(cis->Length, GFP_KERNEL);
-    if (s->fake_cis == NULL)
-       return CS_OUT_OF_RESOURCE;
-    s->fake_cis_len = cis->Length;
-    memcpy(s->fake_cis, cis->Data, cis->Length);
-    return CS_SUCCESS;
+       s->fake_cis = kmalloc(len, GFP_KERNEL);
+       if (s->fake_cis == NULL) {
+               dev_printk(KERN_WARNING, &s->dev, "no memory to replace CIS\n");
+               return -ENOMEM;
+       }
+       s->fake_cis_len = len;
+       memcpy(s->fake_cis, data, len);
+       return 0;
 }
+EXPORT_SYMBOL(pcmcia_replace_cis);
 
 /*======================================================================
 
     The high-level CIS tuple services
-    
+
 ======================================================================*/
 
 typedef struct tuple_flags {
@@ -400,14 +421,12 @@ typedef struct tuple_flags {
 #define MFC_FN(f)      (((tuple_flags *)(&(f)))->mfc_fn)
 #define SPACE(f)       (((tuple_flags *)(&(f)))->space)
 
-int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int func, tuple_t *tuple);
-
 int pccard_get_first_tuple(struct pcmcia_socket *s, unsigned int function, tuple_t *tuple)
 {
     if (!s)
-       return CS_BAD_HANDLE;
+       return -EINVAL;
     if (!(s->state & SOCKET_PRESENT))
-       return CS_NO_CARD;
+       return -ENODEV;
     tuple->TupleLink = tuple->Flags = 0;
 #ifdef CONFIG_CARDBUS
     if (s->state & SOCKET_CARDBUS) {
@@ -427,10 +446,10 @@ int pccard_get_first_tuple(struct pcmcia_socket *s, unsigned int function, tuple
        !(tuple->Attributes & TUPLE_RETURN_COMMON)) {
        cisdata_t req = tuple->DesiredTuple;
        tuple->DesiredTuple = CISTPL_LONGLINK_MFC;
-       if (pccard_get_next_tuple(s, function, tuple) == CS_SUCCESS) {
+       if (pccard_get_next_tuple(s, function, tuple) == 0) {
            tuple->DesiredTuple = CISTPL_LINKTARGET;
-           if (pccard_get_next_tuple(s, function, tuple) != CS_SUCCESS)
-               return CS_NO_MORE_ITEMS;
+           if (pccard_get_next_tuple(s, function, tuple) != 0)
+               return -ENOSPC;
        } else
            tuple->CISOffset = tuple->TupleLink = 0;
        tuple->DesiredTuple = req;
@@ -448,7 +467,7 @@ static int follow_link(struct pcmcia_socket *s, tuple_t *tuple)
        /* Get indirect link from the MFC tuple */
        read_cis_cache(s, LINK_SPACE(tuple->Flags),
                       tuple->LinkOffset, 5, link);
-       ofs = le32_to_cpu(*(u_int *)(link+1));
+       ofs = get_unaligned_le32(link + 1);
        SPACE(tuple->Flags) = (link[0] == CISTPL_MFC_ATTR);
        /* Move to the next indirect link */
        tuple->LinkOffset += 5;
@@ -485,9 +504,9 @@ int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int function, tuple_
     int ofs, i, attr;
 
     if (!s)
-       return CS_BAD_HANDLE;
+       return -EINVAL;
     if (!(s->state & SOCKET_PRESENT))
-       return CS_NO_CARD;
+       return -ENODEV;
 
     link[1] = tuple->TupleLink;
     ofs = tuple->CISOffset + tuple->TupleLink;
@@ -502,11 +521,12 @@ int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int function, tuple_
                ofs++; continue;
            }
        }
-       
+
        /* End of chain?  Follow long link if possible */
        if (link[0] == CISTPL_END) {
-           if ((ofs = follow_link(s, tuple)) < 0)
-               return CS_NO_MORE_ITEMS;
+           ofs = follow_link(s, tuple);
+           if (ofs < 0)
+               return -ENOSPC;
            attr = SPACE(tuple->Flags);
            read_cis_cache(s, attr, ofs, 2, link);
        }
@@ -557,20 +577,20 @@ int pccard_get_next_tuple(struct pcmcia_socket *s, unsigned int function, tuple_
        } else
            if (tuple->DesiredTuple == RETURN_FIRST_TUPLE)
                break;
-       
+
        if (link[0] == tuple->DesiredTuple)
            break;
        ofs += link[1] + 2;
     }
     if (i == MAX_TUPLES) {
-       cs_dbg(s, 1, "cs: overrun in pcmcia_get_next_tuple\n");
-       return CS_NO_MORE_ITEMS;
+       dev_dbg(&s->dev, "cs: overrun in pcmcia_get_next_tuple\n");
+       return -ENOSPC;
     }
-    
+
     tuple->TupleCode = link[0];
     tuple->TupleLink = link[1];
     tuple->CISOffset = ofs + 2;
-    return CS_SUCCESS;
+    return 0;
 }
 EXPORT_SYMBOL(pccard_get_next_tuple);
 
@@ -583,18 +603,18 @@ int pccard_get_tuple_data(struct pcmcia_socket *s, tuple_t *tuple)
     u_int len;
 
     if (!s)
-       return CS_BAD_HANDLE;
+       return -EINVAL;
 
     if (tuple->TupleLink < tuple->TupleOffset)
-       return CS_NO_MORE_ITEMS;
+       return -ENOSPC;
     len = tuple->TupleLink - tuple->TupleOffset;
     tuple->TupleDataLen = tuple->TupleLink;
     if (len == 0)
-       return CS_SUCCESS;
+       return 0;
     read_cis_cache(s, SPACE(tuple->Flags),
                   tuple->CISOffset + tuple->TupleOffset,
                   _MIN(len, tuple->TupleDataMax), tuple->TupleData);
-    return CS_SUCCESS;
+    return 0;
 }
 EXPORT_SYMBOL(pccard_get_tuple_data);
 
@@ -602,7 +622,7 @@ EXPORT_SYMBOL(pccard_get_tuple_data);
 /*======================================================================
 
     Parsing routines for individual tuples
-    
+
 ======================================================================*/
 
 static int parse_device(tuple_t *tuple, cistpl_device_t *device)
@@ -616,36 +636,53 @@ static int parse_device(tuple_t *tuple, cistpl_device_t *device)
 
     device->ndev = 0;
     for (i = 0; i < CISTPL_MAX_DEVICES; i++) {
-       
-       if (*p == 0xff) break;
+
+       if (*p == 0xff)
+               break;
        device->dev[i].type = (*p >> 4);
        device->dev[i].wp = (*p & 0x08) ? 1 : 0;
        switch (*p & 0x07) {
-       case 0: device->dev[i].speed = 0;   break;
-       case 1: device->dev[i].speed = 250; break;
-       case 2: device->dev[i].speed = 200; break;
-       case 3: device->dev[i].speed = 150; break;
-       case 4: device->dev[i].speed = 100; break;
+       case 0:
+               device->dev[i].speed = 0;
+               break;
+       case 1:
+               device->dev[i].speed = 250;
+               break;
+       case 2:
+               device->dev[i].speed = 200;
+               break;
+       case 3:
+               device->dev[i].speed = 150;
+               break;
+       case 4:
+               device->dev[i].speed = 100;
+               break;
        case 7:
-           if (++p == q) return CS_BAD_TUPLE;
-           device->dev[i].speed = SPEED_CVT(*p);
-           while (*p & 0x80)
-               if (++p == q) return CS_BAD_TUPLE;
-           break;
+               if (++p == q)
+                       return -EINVAL;
+               device->dev[i].speed = SPEED_CVT(*p);
+               while (*p & 0x80)
+                       if (++p == q)
+                               return -EINVAL;
+               break;
        default:
-           return CS_BAD_TUPLE;
+               return -EINVAL;
        }
 
-       if (++p == q) return CS_BAD_TUPLE;
-       if (*p == 0xff) break;
+       if (++p == q)
+               return -EINVAL;
+       if (*p == 0xff)
+               break;
        scale = *p & 7;
-       if (scale == 7) return CS_BAD_TUPLE;
+       if (scale == 7)
+               return -EINVAL;
        device->dev[i].size = ((*p >> 3) + 1) * (512 << (scale*2));
        device->ndev++;
-       if (++p == q) break;
+       if (++p == q)
+               break;
     }
-    
-    return CS_SUCCESS;
+
+    return 0;
 }
 
 /*====================================================================*/
@@ -654,12 +691,12 @@ static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum)
 {
     u_char *p;
     if (tuple->TupleDataLen < 5)
-       return CS_BAD_TUPLE;
-    p = (u_char *)tuple->TupleData;
-    csum->addr = tuple->CISOffset+(short)le16_to_cpu(*(u_short *)p)-2;
-    csum->len = le16_to_cpu(*(u_short *)(p + 2));
-    csum->sum = *(p+4);
-    return CS_SUCCESS;
+       return -EINVAL;
+    p = (u_char *) tuple->TupleData;
+    csum->addr = tuple->CISOffset + get_unaligned_le16(p) - 2;
+    csum->len = get_unaligned_le16(p + 2);
+    csum->sum = *(p + 4);
+    return 0;
 }
 
 /*====================================================================*/
@@ -667,9 +704,9 @@ static int parse_checksum(tuple_t *tuple, cistpl_checksum_t *csum)
 static int parse_longlink(tuple_t *tuple, cistpl_longlink_t *link)
 {
     if (tuple->TupleDataLen < 4)
-       return CS_BAD_TUPLE;
-    link->addr = le32_to_cpu(*(u_int *)tuple->TupleData);
-    return CS_SUCCESS;
+       return -EINVAL;
+    link->addr = get_unaligned_le32(tuple->TupleData);
+    return 0;
 }
 
 /*====================================================================*/
@@ -679,17 +716,18 @@ static int parse_longlink_mfc(tuple_t *tuple,
 {
     u_char *p;
     int i;
-    
+
     p = (u_char *)tuple->TupleData;
-    
+
     link->nfn = *p; p++;
     if (tuple->TupleDataLen <= link->nfn*5)
-       return CS_BAD_TUPLE;
+       return -EINVAL;
     for (i = 0; i < link->nfn; i++) {
        link->fn[i].space = *p; p++;
-       link->fn[i].addr = le32_to_cpu(*(u_int *)p); p += 4;
+       link->fn[i].addr = get_unaligned_le32(p);
+       p += 4;
     }
-    return CS_SUCCESS;
+    return 0;
 }
 
 /*====================================================================*/
@@ -699,24 +737,29 @@ static int parse_strings(u_char *p, u_char *q, int max,
 {
     int i, j, ns;
 
-    if (p == q) return CS_BAD_TUPLE;
+    if (p == q)
+           return -EINVAL;
     ns = 0; j = 0;
     for (i = 0; i < max; i++) {
-       if (*p == 0xff) break;
+       if (*p == 0xff)
+               break;
        ofs[i] = j;
        ns++;
        for (;;) {
            s[j++] = (*p == 0xff) ? '\0' : *p;
-           if ((*p == '\0') || (*p == 0xff)) break;
-           if (++p == q) return CS_BAD_TUPLE;
+           if ((*p == '\0') || (*p == 0xff))
+                   break;
+           if (++p == q)
+                   return -EINVAL;
        }
-       if ((*p == 0xff) || (++p == q)) break;
+       if ((*p == 0xff) || (++p == q))
+               break;
     }
     if (found) {
        *found = ns;
-       return CS_SUCCESS;
+       return 0;
     } else {
-       return (ns == max) ? CS_SUCCESS : CS_BAD_TUPLE;
+       return (ns == max) ? 0 : -EINVAL;
     }
 }
 
@@ -725,13 +768,14 @@ static int parse_strings(u_char *p, u_char *q, int max,
 static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1)
 {
     u_char *p, *q;
-    
+
     p = (u_char *)tuple->TupleData;
     q = p + tuple->TupleDataLen;
-    
+
     vers_1->major = *p; p++;
     vers_1->minor = *p; p++;
-    if (p >= q) return CS_BAD_TUPLE;
+    if (p >= q)
+           return -EINVAL;
 
     return parse_strings(p, q, CISTPL_VERS_1_MAX_PROD_STRINGS,
                         vers_1->str, vers_1->ofs, &vers_1->ns);
@@ -742,10 +786,10 @@ static int parse_vers_1(tuple_t *tuple, cistpl_vers_1_t *vers_1)
 static int parse_altstr(tuple_t *tuple, cistpl_altstr_t *altstr)
 {
     u_char *p, *q;
-    
+
     p = (u_char *)tuple->TupleData;
     q = p + tuple->TupleDataLen;
-    
+
     return parse_strings(p, q, CISTPL_MAX_ALTSTR_STRINGS,
                         altstr->str, altstr->ofs, &altstr->ns);
 }
@@ -761,26 +805,25 @@ static int parse_jedec(tuple_t *tuple, cistpl_jedec_t *jedec)
     q = p + tuple->TupleDataLen;
 
     for (nid = 0; nid < CISTPL_MAX_DEVICES; nid++) {
-       if (p > q-2) break;
+       if (p > q-2)
+               break;
        jedec->id[nid].mfr = p[0];
        jedec->id[nid].info = p[1];
        p += 2;
     }
     jedec->nid = nid;
-    return CS_SUCCESS;
+    return 0;
 }
 
 /*====================================================================*/
 
 static int parse_manfid(tuple_t *tuple, cistpl_manfid_t *m)
 {
-    u_short *p;
     if (tuple->TupleDataLen < 4)
-       return CS_BAD_TUPLE;
-    p = (u_short *)tuple->TupleData;
-    m->manf = le16_to_cpu(p[0]);
-    m->card = le16_to_cpu(p[1]);
-    return CS_SUCCESS;
+       return -EINVAL;
+    m->manf = get_unaligned_le16(tuple->TupleData);
+    m->card = get_unaligned_le16(tuple->TupleData + 2);
+    return 0;
 }
 
 /*====================================================================*/
@@ -789,11 +832,11 @@ static int parse_funcid(tuple_t *tuple, cistpl_funcid_t *f)
 {
     u_char *p;
     if (tuple->TupleDataLen < 2)
-       return CS_BAD_TUPLE;
+       return -EINVAL;
     p = (u_char *)tuple->TupleData;
     f->func = p[0];
     f->sysinit = p[1];
-    return CS_SUCCESS;
+    return 0;
 }
 
 /*====================================================================*/
@@ -803,12 +846,12 @@ static int parse_funce(tuple_t *tuple, cistpl_funce_t *f)
     u_char *p;
     int i;
     if (tuple->TupleDataLen < 1)
-       return CS_BAD_TUPLE;
+       return -EINVAL;
     p = (u_char *)tuple->TupleData;
     f->type = p[0];
     for (i = 1; i < tuple->TupleDataLen; i++)
        f->data[i-1] = p[i];
-    return CS_SUCCESS;
+    return 0;
 }
 
 /*====================================================================*/
@@ -822,7 +865,7 @@ static int parse_config(tuple_t *tuple, cistpl_config_t *config)
     rasz = *p & 0x03;
     rmsz = (*p & 0x3c) >> 2;
     if (tuple->TupleDataLen < rasz+rmsz+4)
-       return CS_BAD_TUPLE;
+       return -EINVAL;
     config->last_idx = *(++p);
     p++;
     config->base = 0;
@@ -834,14 +877,14 @@ static int parse_config(tuple_t *tuple, cistpl_config_t *config)
     for (i = 0; i <= rmsz; i++)
        config->rmask[i>>2] += p[i] << (8*(i%4));
     config->subtuples = tuple->TupleDataLen - (rasz+rmsz+4);
-    return CS_SUCCESS;
+    return 0;
 }
 
 /*======================================================================
 
     The following routines are all used to parse the nightmarish
     config table entries.
-    
+
 ======================================================================*/
 
 static u_char *parse_power(u_char *p, u_char *q,
@@ -850,17 +893,20 @@ static u_char *parse_power(u_char *p, u_char *q,
     int i;
     u_int scale;
 
-    if (p == q) return NULL;
+    if (p == q)
+           return NULL;
     pwr->present = *p;
     pwr->flags = 0;
     p++;
     for (i = 0; i < 7; i++)
        if (pwr->present & (1<<i)) {
-           if (p == q) return NULL;
+           if (p == q)
+                   return NULL;
            pwr->param[i] = POWER_CVT(*p);
            scale = POWER_SCALE(*p);
            while (*p & 0x80) {
-               if (++p == q) return NULL;
+               if (++p == q)
+                       return NULL;
                if ((*p & 0x7f) < 100)
                    pwr->param[i] += (*p & 0x7f) * scale / 100;
                else if (*p == 0x7d)
@@ -884,24 +930,28 @@ static u_char *parse_timing(u_char *p, u_char *q,
 {
     u_char scale;
 
-    if (p == q) return NULL;
+    if (p == q)
+           return NULL;
     scale = *p;
     if ((scale & 3) != 3) {
-       if (++p == q) return NULL;
+       if (++p == q)
+               return NULL;
        timing->wait = SPEED_CVT(*p);
        timing->waitscale = exponent[scale & 3];
     } else
        timing->wait = 0;
     scale >>= 2;
     if ((scale & 7) != 7) {
-       if (++p == q) return NULL;
+       if (++p == q)
+               return NULL;
        timing->ready = SPEED_CVT(*p);
        timing->rdyscale = exponent[scale & 7];
     } else
        timing->ready = 0;
     scale >>= 3;
     if (scale != 7) {
-       if (++p == q) return NULL;
+       if (++p == q)
+               return NULL;
        timing->reserved = SPEED_CVT(*p);
        timing->rsvscale = exponent[scale];
     } else
@@ -916,7 +966,8 @@ static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io)
 {
     int i, j, bsz, lsz;
 
-    if (p == q) return NULL;
+    if (p == q)
+           return NULL;
     io->flags = *p;
 
     if (!(*p & 0x80)) {
@@ -925,24 +976,29 @@ static u_char *parse_io(u_char *p, u_char *q, cistpl_io_t *io)
        io->win[0].len = (1 << (io->flags & CISTPL_IO_LINES_MASK));
        return p+1;
     }
-    
-    if (++p == q) return NULL;
+
+    if (++p == q)
+           return NULL;
     io->nwin = (*p & 0x0f) + 1;
     bsz = (*p & 0x30) >> 4;
-    if (bsz == 3) bsz++;
+    if (bsz == 3)
+           bsz++;
     lsz = (*p & 0xc0) >> 6;
-    if (lsz == 3) lsz++;
+    if (lsz == 3)
+           lsz++;
     p++;
-    
+
     for (i = 0; i < io->nwin; i++) {
        io->win[i].base = 0;
        io->win[i].len = 1;
        for (j = 0; j < bsz; j++, p++) {
-           if (p == q) return NULL;
+           if (p == q)
+                   return NULL;
            io->win[i].base += *p << (j*8);
        }
        for (j = 0; j < lsz; j++, p++) {
-           if (p == q) return NULL;
+           if (p == q)
+                   return NULL;
            io->win[i].len += *p << (j*8);
        }
     }
@@ -956,27 +1012,32 @@ static u_char *parse_mem(u_char *p, u_char *q, cistpl_mem_t *mem)
     int i, j, asz, lsz, has_ha;
     u_int len, ca, ha;
 
-    if (p == q) return NULL;
+    if (p == q)
+           return NULL;
 
     mem->nwin = (*p & 0x07) + 1;
     lsz = (*p & 0x18) >> 3;
     asz = (*p & 0x60) >> 5;
     has_ha = (*p & 0x80);
-    if (++p == q) return NULL;
-    
+    if (++p == q)
+           return NULL;
+
     for (i = 0; i < mem->nwin; i++) {
        len = ca = ha = 0;
        for (j = 0; j < lsz; j++, p++) {
-           if (p == q) return NULL;
+           if (p == q)
+                   return NULL;
            len += *p << (j*8);
        }
        for (j = 0; j < asz; j++, p++) {
-           if (p == q) return NULL;
+           if (p == q)
+                   return NULL;
            ca += *p << (j*8);
        }
        if (has_ha)
            for (j = 0; j < asz; j++, p++) {
-               if (p == q) return NULL;
+               if (p == q)
+                       return NULL;
                ha += *p << (j*8);
            }
        mem->win[i].len = len << 8;
@@ -990,10 +1051,12 @@ static u_char *parse_mem(u_char *p, u_char *q, cistpl_mem_t *mem)
 
 static u_char *parse_irq(u_char *p, u_char *q, cistpl_irq_t *irq)
 {
-    if (p == q) return NULL;
+    if (p == q)
+           return NULL;
     irq->IRQInfo1 = *p; p++;
     if (irq->IRQInfo1 & IRQ_INFO2_VALID) {
-       if (p+2 > q) return NULL;
+       if (p+2 > q)
+               return NULL;
        irq->IRQInfo2 = (p[1]<<8) + p[0];
        p += 2;
     }
@@ -1014,7 +1077,8 @@ static int parse_cftable_entry(tuple_t *tuple,
     if (*p & 0x40)
        entry->flags |= CISTPL_CFTABLE_DEFAULT;
     if (*p & 0x80) {
-       if (++p == q) return CS_BAD_TUPLE;
+       if (++p == q)
+               return -EINVAL;
        if (*p & 0x10)
            entry->flags |= CISTPL_CFTABLE_BVDS;
        if (*p & 0x20)
@@ -1028,47 +1092,54 @@ static int parse_cftable_entry(tuple_t *tuple,
        entry->interface = 0;
 
     /* Process optional features */
-    if (++p == q) return CS_BAD_TUPLE;
+    if (++p == q)
+           return -EINVAL;
     features = *p; p++;
 
     /* Power options */
     if ((features & 3) > 0) {
        p = parse_power(p, q, &entry->vcc);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
     } else
        entry->vcc.present = 0;
     if ((features & 3) > 1) {
        p = parse_power(p, q, &entry->vpp1);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
     } else
        entry->vpp1.present = 0;
     if ((features & 3) > 2) {
        p = parse_power(p, q, &entry->vpp2);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
     } else
        entry->vpp2.present = 0;
 
     /* Timing options */
     if (features & 0x04) {
        p = parse_timing(p, q, &entry->timing);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
     } else {
        entry->timing.wait = 0;
        entry->timing.ready = 0;
        entry->timing.reserved = 0;
     }
-    
+
     /* I/O window options */
     if (features & 0x08) {
        p = parse_io(p, q, &entry->io);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
     } else
        entry->io.nwin = 0;
-    
+
     /* Interrupt options */
     if (features & 0x10) {
        p = parse_irq(p, q, &entry->irq);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
     } else
        entry->irq.IRQInfo1 = 0;
 
@@ -1078,39 +1149,43 @@ static int parse_cftable_entry(tuple_t *tuple,
        break;
     case 0x20:
        entry->mem.nwin = 1;
-       entry->mem.win[0].len = le16_to_cpu(*(u_short *)p) << 8;
+       entry->mem.win[0].len = get_unaligned_le16(p) << 8;
        entry->mem.win[0].card_addr = 0;
        entry->mem.win[0].host_addr = 0;
        p += 2;
-       if (p > q) return CS_BAD_TUPLE;
+       if (p > q)
+               return -EINVAL;
        break;
     case 0x40:
        entry->mem.nwin = 1;
-       entry->mem.win[0].len = le16_to_cpu(*(u_short *)p) << 8;
-       entry->mem.win[0].card_addr =
-           le16_to_cpu(*(u_short *)(p+2)) << 8;
+       entry->mem.win[0].len = get_unaligned_le16(p) << 8;
+       entry->mem.win[0].card_addr = get_unaligned_le16(p + 2) << 8;
        entry->mem.win[0].host_addr = 0;
        p += 4;
-       if (p > q) return CS_BAD_TUPLE;
+       if (p > q)
+               return -EINVAL;
        break;
     case 0x60:
        p = parse_mem(p, q, &entry->mem);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
        break;
     }
 
     /* Misc features */
     if (features & 0x80) {
-       if (p == q) return CS_BAD_TUPLE;
+       if (p == q)
+               return -EINVAL;
        entry->flags |= (*p << 8);
        while (*p & 0x80)
-           if (++p == q) return CS_BAD_TUPLE;
+           if (++p == q)
+                   return -EINVAL;
        p++;
     }
 
     entry->subtuples = q-p;
-    
-    return CS_SUCCESS;
+
+    return 0;
 }
 
 /*====================================================================*/
@@ -1121,26 +1196,26 @@ static int parse_bar(tuple_t *tuple, cistpl_bar_t *bar)
 {
     u_char *p;
     if (tuple->TupleDataLen < 6)
-       return CS_BAD_TUPLE;
+       return -EINVAL;
     p = (u_char *)tuple->TupleData;
     bar->attr = *p;
     p += 2;
-    bar->size = le32_to_cpu(*(u_int *)p);
-    return CS_SUCCESS;
+    bar->size = get_unaligned_le32(p);
+    return 0;
 }
 
 static int parse_config_cb(tuple_t *tuple, cistpl_config_t *config)
 {
     u_char *p;
-    
+
     p = (u_char *)tuple->TupleData;
     if ((*p != 3) || (tuple->TupleDataLen < 6))
-       return CS_BAD_TUPLE;
+       return -EINVAL;
     config->last_idx = *(++p);
     p++;
-    config->base = le32_to_cpu(*(u_int *)p);
+    config->base = get_unaligned_le32(p);
     config->subtuples = tuple->TupleDataLen - 6;
-    return CS_SUCCESS;
+    return 0;
 }
 
 static int parse_cftable_entry_cb(tuple_t *tuple,
@@ -1156,62 +1231,72 @@ static int parse_cftable_entry_cb(tuple_t *tuple,
        entry->flags |= CISTPL_CFTABLE_DEFAULT;
 
     /* Process optional features */
-    if (++p == q) return CS_BAD_TUPLE;
+    if (++p == q)
+           return -EINVAL;
     features = *p; p++;
 
     /* Power options */
     if ((features & 3) > 0) {
        p = parse_power(p, q, &entry->vcc);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
     } else
        entry->vcc.present = 0;
     if ((features & 3) > 1) {
        p = parse_power(p, q, &entry->vpp1);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
     } else
        entry->vpp1.present = 0;
     if ((features & 3) > 2) {
        p = parse_power(p, q, &entry->vpp2);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
     } else
        entry->vpp2.present = 0;
 
     /* I/O window options */
     if (features & 0x08) {
-       if (p == q) return CS_BAD_TUPLE;
+       if (p == q)
+               return -EINVAL;
        entry->io = *p; p++;
     } else
        entry->io = 0;
-    
+
     /* Interrupt options */
     if (features & 0x10) {
        p = parse_irq(p, q, &entry->irq);
-       if (p == NULL) return CS_BAD_TUPLE;
+       if (p == NULL)
+               return -EINVAL;
     } else
        entry->irq.IRQInfo1 = 0;
 
     if (features & 0x20) {
-       if (p == q) return CS_BAD_TUPLE;
+       if (p == q)
+               return -EINVAL;
        entry->mem = *p; p++;
     } else
        entry->mem = 0;
 
     /* Misc features */
     if (features & 0x80) {
-       if (p == q) return CS_BAD_TUPLE;
+       if (p == q)
+               return -EINVAL;
        entry->flags |= (*p << 8);
        if (*p & 0x80) {
-           if (++p == q) return CS_BAD_TUPLE;
+           if (++p == q)
+                   return -EINVAL;
            entry->flags |= (*p << 16);
        }
        while (*p & 0x80)
-           if (++p == q) return CS_BAD_TUPLE;
+           if (++p == q)
+                   return -EINVAL;
        p++;
     }
 
     entry->subtuples = q-p;
-    
-    return CS_SUCCESS;
+
+    return 0;
 }
 
 #endif
@@ -1227,7 +1312,8 @@ static int parse_device_geo(tuple_t *tuple, cistpl_device_geo_t *geo)
     q = p + tuple->TupleDataLen;
 
     for (n = 0; n < CISTPL_MAX_DEVICES; n++) {
-       if (p > q-6) break;
+       if (p > q-6)
+               break;
        geo->geo[n].buswidth = p[0];
        geo->geo[n].erase_block = 1 << (p[1]-1);
        geo->geo[n].read_block  = 1 << (p[2]-1);
@@ -1237,7 +1323,7 @@ static int parse_device_geo(tuple_t *tuple, cistpl_device_geo_t *geo)
        p += 6;
     }
     geo->ngeo = n;
-    return CS_SUCCESS;
+    return 0;
 }
 
 /*====================================================================*/
@@ -1247,14 +1333,14 @@ static int parse_vers_2(tuple_t *tuple, cistpl_vers_2_t *v2)
     u_char *p, *q;
 
     if (tuple->TupleDataLen < 10)
-       return CS_BAD_TUPLE;
-    
+       return -EINVAL;
+
     p = tuple->TupleData;
     q = p + tuple->TupleDataLen;
 
     v2->vers = p[0];
     v2->comply = p[1];
-    v2->dindex = le16_to_cpu(*(u_short *)(p+2));
+    v2->dindex = get_unaligned_le16(p + 2);
     v2->vspec8 = p[6];
     v2->vspec9 = p[7];
     v2->nhdr = p[8];
@@ -1268,18 +1354,22 @@ static int parse_org(tuple_t *tuple, cistpl_org_t *org)
 {
     u_char *p, *q;
     int i;
-    
+
     p = tuple->TupleData;
     q = p + tuple->TupleDataLen;
-    if (p == q) return CS_BAD_TUPLE;
+    if (p == q)
+           return -EINVAL;
     org->data_org = *p;
-    if (++p == q) return CS_BAD_TUPLE;
+    if (++p == q)
+           return -EINVAL;
     for (i = 0; i < 30; i++) {
        org->desc[i] = *p;
-       if (*p == '\0') break;
-       if (++p == q) return CS_BAD_TUPLE;
+       if (*p == '\0')
+               break;
+       if (++p == q)
+               return -EINVAL;
     }
-    return CS_SUCCESS;
+    return 0;
 }
 
 /*====================================================================*/
@@ -1289,26 +1379,26 @@ static int parse_format(tuple_t *tuple, cistpl_format_t *fmt)
     u_char *p;
 
     if (tuple->TupleDataLen < 10)
-       return CS_BAD_TUPLE;
+       return -EINVAL;
 
     p = tuple->TupleData;
 
     fmt->type = p[0];
     fmt->edc = p[1];
-    fmt->offset = le32_to_cpu(*(u_int *)(p+2));
-    fmt->length = le32_to_cpu(*(u_int *)(p+6));
+    fmt->offset = get_unaligned_le32(p + 2);
+    fmt->length = get_unaligned_le32(p + 6);
 
-    return CS_SUCCESS;
+    return 0;
 }
 
 /*====================================================================*/
 
-int pccard_parse_tuple(tuple_t *tuple, cisparse_t *parse)
+int pcmcia_parse_tuple(tuple_t *tuple, cisparse_t *parse)
 {
-    int ret = CS_SUCCESS;
-    
+    int ret = 0;
+
     if (tuple->TupleDataLen > tuple->TupleDataMax)
-       return CS_BAD_TUPLE;
+       return -EINVAL;
     switch (tuple->TupleCode) {
     case CISTPL_DEVICE:
     case CISTPL_DEVICE_A:
@@ -1376,20 +1466,22 @@ int pccard_parse_tuple(tuple_t *tuple, cisparse_t *parse)
        break;
     case CISTPL_NO_LINK:
     case CISTPL_LINKTARGET:
-       ret = CS_SUCCESS;
+       ret = 0;
        break;
     default:
-       ret = CS_UNSUPPORTED_FUNCTION;
+       ret = -EINVAL;
        break;
     }
+    if (ret)
+           pr_debug("parse_tuple failed %d\n", ret);
     return ret;
 }
-EXPORT_SYMBOL(pccard_parse_tuple);
+EXPORT_SYMBOL(pcmcia_parse_tuple);
 
 /*======================================================================
 
     This is used internally by Card Services to look up CIS stuff.
-    
+
 ======================================================================*/
 
 int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function, cisdata_t code, void *parse)
@@ -1399,24 +1491,91 @@ int pccard_read_tuple(struct pcmcia_socket *s, unsigned int function, cisdata_t
     int ret;
 
     buf = kmalloc(256, GFP_KERNEL);
-    if (buf == NULL)
-       return CS_OUT_OF_RESOURCE;
+    if (buf == NULL) {
+           dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
+           return -ENOMEM;
+    }
     tuple.DesiredTuple = code;
-    tuple.Attributes = TUPLE_RETURN_COMMON;
+    tuple.Attributes = 0;
+    if (function == BIND_FN_ALL)
+           tuple.Attributes = TUPLE_RETURN_COMMON;
     ret = pccard_get_first_tuple(s, function, &tuple);
-    if (ret != CS_SUCCESS) goto done;
+    if (ret != 0)
+           goto done;
     tuple.TupleData = buf;
     tuple.TupleOffset = 0;
     tuple.TupleDataMax = 255;
     ret = pccard_get_tuple_data(s, &tuple);
-    if (ret != CS_SUCCESS) goto done;
-    ret = pccard_parse_tuple(&tuple, parse);
+    if (ret != 0)
+           goto done;
+    ret = pcmcia_parse_tuple(&tuple, parse);
 done:
     kfree(buf);
     return ret;
 }
 EXPORT_SYMBOL(pccard_read_tuple);
 
+
+/**
+ * pccard_loop_tuple() - loop over tuples in the CIS
+ * @s:         the struct pcmcia_socket where the card is inserted
+ * @function:  the device function we loop for
+ * @code:      which CIS code shall we look for?
+ * @parse:     buffer where the tuple shall be parsed (or NULL, if no parse)
+ * @priv_data: private data to be passed to the loop_tuple function.
+ * @loop_tuple:        function to call for each CIS entry of type @function. IT
+ *             gets passed the raw tuple, the paresed tuple (if @parse is
+ *             set) and @priv_data.
+ *
+ * pccard_loop_tuple() loops over all CIS entries of type @function, and
+ * calls the @loop_tuple function for each entry. If the call to @loop_tuple
+ * returns 0, the loop exits. Returns 0 on success or errorcode otherwise.
+ */
+int pccard_loop_tuple(struct pcmcia_socket *s, unsigned int function,
+                     cisdata_t code, cisparse_t *parse, void *priv_data,
+                     int (*loop_tuple) (tuple_t *tuple,
+                                        cisparse_t *parse,
+                                        void *priv_data))
+{
+       tuple_t tuple;
+       cisdata_t *buf;
+       int ret;
+
+       buf = kzalloc(256, GFP_KERNEL);
+       if (buf == NULL) {
+               dev_printk(KERN_WARNING, &s->dev, "no memory to read tuple\n");
+               return -ENOMEM;
+       }
+
+       tuple.TupleData = buf;
+       tuple.TupleDataMax = 255;
+       tuple.TupleOffset = 0;
+       tuple.DesiredTuple = code;
+       tuple.Attributes = 0;
+
+       ret = pccard_get_first_tuple(s, function, &tuple);
+       while (!ret) {
+               if (pccard_get_tuple_data(s, &tuple))
+                       goto next_entry;
+
+               if (parse)
+                       if (pcmcia_parse_tuple(&tuple, parse))
+                               goto next_entry;
+
+               ret = loop_tuple(&tuple, parse, priv_data);
+               if (!ret)
+                       break;
+
+next_entry:
+               ret = pccard_get_next_tuple(s, function, &tuple);
+       }
+
+       kfree(buf);
+       return ret;
+}
+EXPORT_SYMBOL(pccard_loop_tuple);
+
+
 /*======================================================================
 
     This tries to determine if a card has a sensible CIS.  It returns
@@ -1424,67 +1583,74 @@ EXPORT_SYMBOL(pccard_read_tuple);
     checks include making sure several critical tuples are present and
     valid; seeing if the total number of tuples is reasonable; and
     looking for tuples that use reserved codes.
-    
+
 ======================================================================*/
 
-int pccard_validate_cis(struct pcmcia_socket *s, unsigned int function, cisinfo_t *info)
+int pccard_validate_cis(struct pcmcia_socket *s, unsigned int *info)
 {
     tuple_t *tuple;
     cisparse_t *p;
+    unsigned int count = 0;
     int ret, reserved, dev_ok = 0, ident_ok = 0;
 
     if (!s)
-       return CS_BAD_HANDLE;
+       return -EINVAL;
 
     tuple = kmalloc(sizeof(*tuple), GFP_KERNEL);
-    if (tuple == NULL)
-       return CS_OUT_OF_RESOURCE;
+    if (tuple == NULL) {
+           dev_printk(KERN_WARNING, &s->dev, "no memory to validate CIS\n");
+           return -ENOMEM;
+    }
     p = kmalloc(sizeof(*p), GFP_KERNEL);
     if (p == NULL) {
-       kfree(tuple);
-       return CS_OUT_OF_RESOURCE;
+           kfree(tuple);
+           dev_printk(KERN_WARNING, &s->dev, "no memory to validate CIS\n");
+           return -ENOMEM;
     }
 
-    info->Chains = reserved = 0;
+    count = reserved = 0;
     tuple->DesiredTuple = RETURN_FIRST_TUPLE;
     tuple->Attributes = TUPLE_RETURN_COMMON;
-    ret = pccard_get_first_tuple(s, function, tuple);
-    if (ret != CS_SUCCESS)
+    ret = pccard_get_first_tuple(s, BIND_FN_ALL, tuple);
+    if (ret != 0)
        goto done;
 
     /* First tuple should be DEVICE; we should really have either that
        or a CFTABLE_ENTRY of some sort */
     if ((tuple->TupleCode == CISTPL_DEVICE) ||
-       (pccard_read_tuple(s, function, CISTPL_CFTABLE_ENTRY, p) == CS_SUCCESS) ||
-       (pccard_read_tuple(s, function, CISTPL_CFTABLE_ENTRY_CB, p) == CS_SUCCESS))
+       (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY, p) == 0) ||
+       (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_CFTABLE_ENTRY_CB, p) == 0))
        dev_ok++;
 
     /* All cards should have a MANFID tuple, and/or a VERS_1 or VERS_2
        tuple, for card identification.  Certain old D-Link and Linksys
        cards have only a broken VERS_2 tuple; hence the bogus test. */
-    if ((pccard_read_tuple(s, function, CISTPL_MANFID, p) == CS_SUCCESS) ||
-       (pccard_read_tuple(s, function, CISTPL_VERS_1, p) == CS_SUCCESS) ||
-       (pccard_read_tuple(s, function, CISTPL_VERS_2, p) != CS_NO_MORE_ITEMS))
+    if ((pccard_read_tuple(s, BIND_FN_ALL, CISTPL_MANFID, p) == 0) ||
+       (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_1, p) == 0) ||
+       (pccard_read_tuple(s, BIND_FN_ALL, CISTPL_VERS_2, p) != -ENOSPC))
        ident_ok++;
 
     if (!dev_ok && !ident_ok)
        goto done;
 
-    for (info->Chains = 1; info->Chains < MAX_TUPLES; info->Chains++) {
-       ret = pccard_get_next_tuple(s, function, tuple);
-       if (ret != CS_SUCCESS) break;
+    for (count = 1; count < MAX_TUPLES; count++) {
+       ret = pccard_get_next_tuple(s, BIND_FN_ALL, tuple);
+       if (ret != 0)
+               break;
        if (((tuple->TupleCode > 0x23) && (tuple->TupleCode < 0x40)) ||
            ((tuple->TupleCode > 0x47) && (tuple->TupleCode < 0x80)) ||
            ((tuple->TupleCode > 0x90) && (tuple->TupleCode < 0xff)))
            reserved++;
     }
-    if ((info->Chains == MAX_TUPLES) || (reserved > 5) ||
-       ((!dev_ok || !ident_ok) && (info->Chains > 10)))
-       info->Chains = 0;
+    if ((count == MAX_TUPLES) || (reserved > 5) ||
+       ((!dev_ok || !ident_ok) && (count > 10)))
+       count = 0;
 
 done:
+    if (info)
+           *info = count;
     kfree(tuple);
     kfree(p);
-    return CS_SUCCESS;
+    return 0;
 }
 EXPORT_SYMBOL(pccard_validate_cis);