]> err.no Git - linux-2.6/blobdiff - drivers/ieee1394/csr1212.c
ieee1394: stricter error checks in csr1212
[linux-2.6] / drivers / ieee1394 / csr1212.c
index 61ddd5d37effab34520797ca6811ddafef5cfac1..54408cbe8f8f25c52c04d0f334eb750ec0719bfe 100644 (file)
 /* TODO List:
  * - Verify interface consistency: i.e., public functions that take a size
  *   parameter expect size to be in bytes.
- * - Convenience functions for reading a block of data from a given offset.
  */
 
-#ifndef __KERNEL__
-#include <string.h>
-#endif
+#include <linux/errno.h>
+#include <linux/string.h>
+#include <asm/bug.h>
+#include <asm/byteorder.h>
 
 #include "csr1212.h"
 
 #define __C (1 << CSR1212_KV_TYPE_CSR_OFFSET)
 #define __D (1 << CSR1212_KV_TYPE_DIRECTORY)
 #define __L (1 << CSR1212_KV_TYPE_LEAF)
-static const u_int8_t csr1212_key_id_type_map[0x30] = {
-       0,                      /* Reserved */
+static const u8 csr1212_key_id_type_map[0x30] = {
+       __C,                    /* used by Apple iSight */
        __D | __L,              /* Descriptor */
        __I | __D | __L,        /* Bus_Dependent_Info */
        __I | __D | __L,        /* Vendor */
        __I,                    /* Hardware_Version */
        0, 0,                   /* Reserved */
-       __D | __L,              /* Module */
-       0, 0, 0, 0,             /* Reserved */
+       __D | __L | __I,        /* Module */
+       __I, 0, 0, 0,           /* used by Apple iSight, Reserved */
        __I,                    /* Node_Capabilities */
        __L,                    /* EUI_64 */
        0, 0, 0,                /* Reserved */
@@ -82,10 +82,10 @@ static const u_int8_t csr1212_key_id_type_map[0x30] = {
 #undef __L
 
 
-#define quads_to_bytes(_q) ((_q) * sizeof(u_int32_t))
-#define bytes_to_quads(_b) (((_b) + sizeof(u_int32_t) - 1) / sizeof(u_int32_t))
+#define quads_to_bytes(_q) ((_q) * sizeof(u32))
+#define bytes_to_quads(_b) (((_b) + sizeof(u32) - 1) / sizeof(u32))
 
-static inline void free_keyval(struct csr1212_keyval *kv)
+static void free_keyval(struct csr1212_keyval *kv)
 {
        if ((kv->key.type == CSR1212_KV_TYPE_LEAF) &&
            (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM))
@@ -94,14 +94,14 @@ static inline void free_keyval(struct csr1212_keyval *kv)
        CSR1212_FREE(kv);
 }
 
-static u_int16_t csr1212_crc16(const u_int32_t *buffer, size_t length)
+static u16 csr1212_crc16(const u32 *buffer, size_t length)
 {
        int shift;
-       u_int32_t data;
-       u_int16_t sum, crc = 0;
+       u32 data;
+       u16 sum, crc = 0;
 
        for (; length; length--) {
-               data = CSR1212_BE32_TO_CPU(*buffer);
+               data = be32_to_cpu(*buffer);
                buffer++;
                for (shift = 28; shift >= 0; shift -= 4 ) {
                        sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
@@ -110,21 +110,21 @@ static u_int16_t csr1212_crc16(const u_int32_t *buffer, size_t length)
                crc &= 0xffff;
        }
 
-       return CSR1212_CPU_TO_BE16(crc);
+       return cpu_to_be16(crc);
 }
 
 #if 0
 /* Microsoft computes the CRC with the bytes in reverse order.  Therefore we
  * have a special version of the CRC algorithm to account for their buggy
  * software. */
-static u_int16_t csr1212_msft_crc16(const u_int32_t *buffer, size_t length)
+static u16 csr1212_msft_crc16(const u32 *buffer, size_t length)
 {
        int shift;
-       u_int32_t data;
-       u_int16_t sum, crc = 0;
+       u32 data;
+       u16 sum, crc = 0;
 
        for (; length; length--) {
-               data = CSR1212_LE32_TO_CPU(*buffer);
+               data = le32_to_cpu(*buffer);
                buffer++;
                for (shift = 28; shift >= 0; shift -= 4 ) {
                        sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
@@ -133,12 +133,12 @@ static u_int16_t csr1212_msft_crc16(const u_int32_t *buffer, size_t length)
                crc &= 0xffff;
        }
 
-       return CSR1212_CPU_TO_BE16(crc);
+       return cpu_to_be16(crc);
 }
 #endif
 
-static inline struct csr1212_dentry *csr1212_find_keyval(struct csr1212_keyval *dir,
-                                                        struct csr1212_keyval *kv)
+static struct csr1212_dentry *
+csr1212_find_keyval(struct csr1212_keyval *dir, struct csr1212_keyval *kv)
 {
        struct csr1212_dentry *pos;
 
@@ -150,9 +150,8 @@ static inline struct csr1212_dentry *csr1212_find_keyval(struct csr1212_keyval *
        return NULL;
 }
 
-
-static inline struct csr1212_keyval *csr1212_find_keyval_offset(struct csr1212_keyval *kv_list,
-                                                               u_int32_t offset)
+static struct csr1212_keyval *
+csr1212_find_keyval_offset(struct csr1212_keyval *kv_list, u32 offset)
 {
        struct csr1212_keyval *kv;
 
@@ -165,6 +164,7 @@ static inline struct csr1212_keyval *csr1212_find_keyval_offset(struct csr1212_k
 
 
 /* Creation Routines */
+
 struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
                                       size_t bus_info_size, void *private)
 {
@@ -202,27 +202,17 @@ struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
        return csr;
 }
 
-
-
 void csr1212_init_local_csr(struct csr1212_csr *csr,
-                           const u_int32_t *bus_info_data, int max_rom)
+                           const u32 *bus_info_data, int max_rom)
 {
        static const int mr_map[] = { 4, 64, 1024, 0 };
 
-#ifdef __KERNEL__
        BUG_ON(max_rom & ~0x3);
        csr->max_rom = mr_map[max_rom];
-#else
-       if (max_rom & ~0x3) /* caller supplied invalid argument */
-               csr->max_rom = 0;
-       else
-               csr->max_rom = mr_map[max_rom];
-#endif
        memcpy(csr->bus_info_data, bus_info_data, csr->bus_info_len);
 }
 
-
-static struct csr1212_keyval *csr1212_new_keyval(u_int8_t type, u_int8_t key)
+static struct csr1212_keyval *csr1212_new_keyval(u8 type, u8 key)
 {
        struct csr1212_keyval *kv;
 
@@ -246,7 +236,7 @@ static struct csr1212_keyval *csr1212_new_keyval(u_int8_t type, u_int8_t key)
        return kv;
 }
 
-struct csr1212_keyval *csr1212_new_immediate(u_int8_t key, u_int32_t value)
+struct csr1212_keyval *csr1212_new_immediate(u8 key, u32 value)
 {
        struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_IMMEDIATE, key);
 
@@ -258,7 +248,8 @@ struct csr1212_keyval *csr1212_new_immediate(u_int8_t key, u_int32_t value)
        return kv;
 }
 
-struct csr1212_keyval *csr1212_new_leaf(u_int8_t key, const void *data, size_t data_len)
+static struct csr1212_keyval *
+csr1212_new_leaf(u8 key, const void *data, size_t data_len)
 {
        struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, key);
 
@@ -285,7 +276,8 @@ struct csr1212_keyval *csr1212_new_leaf(u_int8_t key, const void *data, size_t d
        return kv;
 }
 
-struct csr1212_keyval *csr1212_new_csr_offset(u_int8_t key, u_int32_t csr_offset)
+static struct csr1212_keyval *
+csr1212_new_csr_offset(u8 key, u32 csr_offset)
 {
        struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_CSR_OFFSET, key);
 
@@ -299,7 +291,7 @@ struct csr1212_keyval *csr1212_new_csr_offset(u_int8_t key, u_int32_t csr_offset
        return kv;
 }
 
-struct csr1212_keyval *csr1212_new_directory(u_int8_t key)
+struct csr1212_keyval *csr1212_new_directory(u8 key)
 {
        struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_DIRECTORY, key);
 
@@ -314,43 +306,29 @@ struct csr1212_keyval *csr1212_new_directory(u_int8_t key)
        return kv;
 }
 
-int csr1212_associate_keyval(struct csr1212_keyval *kv,
-                            struct csr1212_keyval *associate)
+void csr1212_associate_keyval(struct csr1212_keyval *kv,
+                             struct csr1212_keyval *associate)
 {
-       if (!kv || !associate)
-               return CSR1212_EINVAL;
-
-       if (kv->key.id == CSR1212_KV_ID_DESCRIPTOR ||
-          (associate->key.id != CSR1212_KV_ID_DESCRIPTOR &&
-           associate->key.id != CSR1212_KV_ID_DEPENDENT_INFO &&
-           associate->key.id != CSR1212_KV_ID_EXTENDED_KEY &&
-           associate->key.id != CSR1212_KV_ID_EXTENDED_DATA &&
-           associate->key.id < 0x30))
-               return CSR1212_EINVAL;
-
-       if (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID &&
-          associate->key.id != CSR1212_KV_ID_EXTENDED_KEY)
-               return CSR1212_EINVAL;
-
-       if (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
-          associate->key.id != CSR1212_KV_ID_EXTENDED_DATA)
-               return CSR1212_EINVAL;
-
-       if (associate->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
-          kv->key.id != CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID)
-               return CSR1212_EINVAL;
-
-       if (associate->key.id == CSR1212_KV_ID_EXTENDED_DATA &&
-          kv->key.id != CSR1212_KV_ID_EXTENDED_KEY)
-               return CSR1212_EINVAL;
+       BUG_ON(!kv || !associate || kv->key.id == CSR1212_KV_ID_DESCRIPTOR ||
+              (associate->key.id != CSR1212_KV_ID_DESCRIPTOR &&
+               associate->key.id != CSR1212_KV_ID_DEPENDENT_INFO &&
+               associate->key.id != CSR1212_KV_ID_EXTENDED_KEY &&
+               associate->key.id != CSR1212_KV_ID_EXTENDED_DATA &&
+               associate->key.id < 0x30) ||
+              (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID &&
+               associate->key.id != CSR1212_KV_ID_EXTENDED_KEY) ||
+              (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
+               associate->key.id != CSR1212_KV_ID_EXTENDED_DATA) ||
+              (associate->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
+               kv->key.id != CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) ||
+              (associate->key.id == CSR1212_KV_ID_EXTENDED_DATA &&
+               kv->key.id != CSR1212_KV_ID_EXTENDED_KEY));
 
        if (kv->associate)
                csr1212_release_keyval(kv->associate);
 
        associate->refcnt++;
        kv->associate = associate;
-
-       return CSR1212_SUCCESS;
 }
 
 int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
@@ -358,12 +336,11 @@ int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
 {
        struct csr1212_dentry *dentry;
 
-       if (!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY)
-               return CSR1212_EINVAL;
+       BUG_ON(!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY);
 
        dentry = CSR1212_MALLOC(sizeof(*dentry));
        if (!dentry)
-               return CSR1212_ENOMEM;
+               return -ENOMEM;
 
        dentry->kv = kv;
 
@@ -382,66 +359,22 @@ int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
        return CSR1212_SUCCESS;
 }
 
-struct csr1212_keyval *csr1212_new_extended_immediate(u_int32_t spec, u_int32_t key,
-                                                     u_int32_t value)
-{
-       struct csr1212_keyval *kvs, *kvk, *kvv;
-
-       kvs = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID, spec);
-       kvk = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY, key);
-       kvv = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_DATA, value);
-
-       if (!kvs || !kvk || !kvv) {
-               if (kvs)
-                       free_keyval(kvs);
-               if (kvk)
-                       free_keyval(kvk);
-               if (kvv)
-                       free_keyval(kvv);
-               return NULL;
-       }
-
-       /* Don't keep a local reference to the extended key or value. */
-       kvk->refcnt = 0;
-       kvv->refcnt = 0;
-
-       csr1212_associate_keyval(kvk, kvv);
-       csr1212_associate_keyval(kvs, kvk);
-
-       return kvs;
-}
-
-struct csr1212_keyval *csr1212_new_extended_leaf(u_int32_t spec, u_int32_t key,
-                                                const void *data, size_t data_len)
-{
-       struct csr1212_keyval *kvs, *kvk, *kvv;
-
-       kvs = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID, spec);
-       kvk = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY, key);
-       kvv = csr1212_new_leaf(CSR1212_KV_ID_EXTENDED_DATA, data, data_len);
-
-       if (!kvs || !kvk || !kvv) {
-               if (kvs)
-                       free_keyval(kvs);
-               if (kvk)
-                       free_keyval(kvk);
-               if (kvv)
-                       free_keyval(kvv);
-               return NULL;
-       }
-
-       /* Don't keep a local reference to the extended key or value. */
-       kvk->refcnt = 0;
-       kvv->refcnt = 0;
-
-       csr1212_associate_keyval(kvk, kvv);
-       csr1212_associate_keyval(kvs, kvk);
-
-       return kvs;
-}
-
-struct csr1212_keyval *csr1212_new_descriptor_leaf(u_int8_t dtype, u_int32_t specifier_id,
-                                                  const void *data, size_t data_len)
+#define CSR1212_DESCRIPTOR_LEAF_DATA(kv) \
+       (&((kv)->value.leaf.data[1]))
+
+#define CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, type) \
+       ((kv)->value.leaf.data[0] = \
+        cpu_to_be32(CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) | \
+                    ((type) << CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)))
+#define CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, spec_id) \
+       ((kv)->value.leaf.data[0] = \
+        cpu_to_be32((CSR1212_DESCRIPTOR_LEAF_TYPE(kv) << \
+                     CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT) | \
+                    ((spec_id) & CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)))
+
+static struct csr1212_keyval *
+csr1212_new_descriptor_leaf(u8 dtype, u32 specifier_id,
+                           const void *data, size_t data_len)
 {
        struct csr1212_keyval *kv;
 
@@ -460,12 +393,33 @@ struct csr1212_keyval *csr1212_new_descriptor_leaf(u_int8_t dtype, u_int32_t spe
        return kv;
 }
 
-
-struct csr1212_keyval *csr1212_new_textual_descriptor_leaf(u_int8_t cwidth,
-                                                          u_int16_t cset,
-                                                          u_int16_t language,
-                                                          const void *data,
-                                                          size_t data_len)
+#define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_WIDTH(kv, width) \
+       ((kv)->value.leaf.data[1] = \
+        ((kv)->value.leaf.data[1] & \
+         cpu_to_be32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK << \
+                       CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT))) | \
+        cpu_to_be32(((width) & CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK) << \
+                    CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT))
+
+#define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_CHAR_SET(kv, char_set) \
+       ((kv)->value.leaf.data[1] = \
+        ((kv)->value.leaf.data[1] & \
+         cpu_to_be32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK << \
+                       CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT))) | \
+        cpu_to_be32(((char_set) & \
+                     CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK) << \
+                    CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT))
+
+#define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language) \
+       ((kv)->value.leaf.data[1] = \
+        ((kv)->value.leaf.data[1] & \
+         cpu_to_be32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK))) | \
+        cpu_to_be32(((language) & \
+                     CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK)))
+
+static struct csr1212_keyval *
+csr1212_new_textual_descriptor_leaf(u8 cwidth, u16 cset, u16 language,
+                                   const void *data, size_t data_len)
 {
        struct csr1212_keyval *kv;
        char *lstr;
@@ -482,7 +436,7 @@ struct csr1212_keyval *csr1212_new_textual_descriptor_leaf(u_int8_t cwidth,
        lstr = (char*)CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv);
 
        /* make sure last quadlet is zeroed out */
-       *((u_int32_t*)&(lstr[(data_len - 1) & ~0x3])) = 0;
+       *((u32*)&(lstr[(data_len - 1) & ~0x3])) = 0;
 
        /* don't copy the NUL terminator */
        memcpy(lstr, data, data_len);
@@ -529,121 +483,6 @@ struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s)
        return csr1212_new_textual_descriptor_leaf(0, 0, 0, s, strlen(s));
 }
 
-struct csr1212_keyval *csr1212_new_icon_descriptor_leaf(u_int32_t version,
-                                                       u_int8_t palette_depth,
-                                                       u_int8_t color_space,
-                                                       u_int16_t language,
-                                                       u_int16_t hscan,
-                                                       u_int16_t vscan,
-                                                       u_int32_t *palette,
-                                                       u_int32_t *pixels)
-{
-       static const int pd[4] = { 0, 4, 16, 256 };
-       static const int cs[16] = { 4, 2 };
-       struct csr1212_keyval *kv;
-       int palette_size;
-       int pixel_size = (hscan * vscan + 3) & ~0x3;
-
-       if (!pixels || (!palette && palette_depth) ||
-           (palette_depth & ~0x3) || (color_space & ~0xf))
-               return NULL;
-
-       palette_size = pd[palette_depth] * cs[color_space];
-
-       kv = csr1212_new_descriptor_leaf(1, 0, NULL,
-                                        palette_size + pixel_size +
-                                        CSR1212_ICON_DESCRIPTOR_LEAF_OVERHEAD);
-       if (!kv)
-               return NULL;
-
-       CSR1212_ICON_DESCRIPTOR_LEAF_SET_VERSION(kv, version);
-       CSR1212_ICON_DESCRIPTOR_LEAF_SET_PALETTE_DEPTH(kv, palette_depth);
-       CSR1212_ICON_DESCRIPTOR_LEAF_SET_COLOR_SPACE(kv, color_space);
-       CSR1212_ICON_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language);
-       CSR1212_ICON_DESCRIPTOR_LEAF_SET_HSCAN(kv, hscan);
-       CSR1212_ICON_DESCRIPTOR_LEAF_SET_VSCAN(kv, vscan);
-
-       if (palette_size)
-               memcpy(CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE(kv), palette,
-                      palette_size);
-
-       memcpy(CSR1212_ICON_DESCRIPTOR_LEAF_PIXELS(kv), pixels, pixel_size);
-
-       return kv;
-}
-
-struct csr1212_keyval *csr1212_new_modifiable_descriptor_leaf(u_int16_t max_size,
-                                                             u_int64_t address)
-{
-       struct csr1212_keyval *kv;
-
-       /* IEEE 1212, par. 7.5.4.3  Modifiable descriptors */
-       kv = csr1212_new_leaf(CSR1212_KV_ID_MODIFIABLE_DESCRIPTOR, NULL, sizeof(u_int64_t));
-       if(!kv)
-               return NULL;
-
-       CSR1212_MODIFIABLE_DESCRIPTOR_SET_MAX_SIZE(kv, max_size);
-       CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_HI(kv, address);
-       CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_LO(kv, address);
-
-       return kv;
-}
-
-static int csr1212_check_keyword(const char *s)
-{
-       for (; *s; s++) {
-
-               if (('A' <= *s) && (*s <= 'Z'))
-                       continue;
-               if (('0' <= *s) && (*s <= '9'))
-                       continue;
-               if (*s == '-')
-                       continue;
-
-               return -1; /* failed */
-       }
-       /* String conforms to keyword, as specified by IEEE 1212,
-        * par. 7.6.5 */
-       return CSR1212_SUCCESS;
-}
-
-struct csr1212_keyval *csr1212_new_keyword_leaf(int strc, const char *strv[])
-{
-       struct csr1212_keyval *kv;
-       char *buffer;
-       int i, data_len = 0;
-
-       /* Check all keywords to see if they conform to restrictions:
-        * Only the following characters is allowed ['A'..'Z','0'..'9','-']
-        * Each word is zero-terminated.
-        * Also calculate the total length of the keywords.
-        */
-       for (i = 0; i < strc; i++) {
-               if (!strv[i] || csr1212_check_keyword(strv[i])) {
-                       return NULL;
-               }
-               data_len += strlen(strv[i]) + 1; /* Add zero-termination char. */
-       }
-
-       /* IEEE 1212, par. 7.6.5 Keyword leaves */
-       kv = csr1212_new_leaf(CSR1212_KV_ID_KEYWORD, NULL, data_len);
-       if (!kv)
-               return NULL;
-
-       buffer = (char *)kv->value.leaf.data;
-
-       /* make sure last quadlet is zeroed out */
-       *((u_int32_t*)&(buffer[(data_len - 1) & ~0x3])) = 0;
-
-       /* Copy keyword(s) into leaf data buffer */
-       for (i = 0; i < strc; i++) {
-               int len = strlen(strv[i]) + 1;
-               memcpy(buffer, strv[i], len);
-               buffer += len;
-       }
-       return kv;
-}
-
 
 /* Destruction Routines */
 
@@ -674,17 +513,6 @@ void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir,
        csr1212_release_keyval(kv);
 }
 
-
-void csr1212_disassociate_keyval(struct csr1212_keyval *kv)
-{
-       if (kv->associate) {
-               csr1212_release_keyval(kv->associate);
-       }
-
-       kv->associate = NULL;
-}
-
-
 /* This function is used to free the memory taken by a keyval.  If the given
  * keyval is a directory type, then any keyvals contained in that directory
  * will be destroyed as well if their respective refcnts are 0.  By means of
@@ -738,7 +566,6 @@ void _csr1212_destroy_keyval(struct csr1212_keyval *kv)
        }
 }
 
-
 void csr1212_destroy_csr(struct csr1212_csr *csr)
 {
        struct csr1212_csr_rom_cache *c, *oc;
@@ -763,49 +590,47 @@ void csr1212_destroy_csr(struct csr1212_csr *csr)
 }
 
 
-
 /* CSR Image Creation */
 
 static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize)
 {
        struct csr1212_csr_rom_cache *cache;
-       u_int64_t csr_addr;
+       u64 csr_addr;
 
-       if (!csr || !csr->ops || !csr->ops->allocate_addr_range ||
-           !csr->ops->release_addr || csr->max_rom < 1)
-               return CSR1212_EINVAL;
+       BUG_ON(!csr || !csr->ops || !csr->ops->allocate_addr_range ||
+              !csr->ops->release_addr || csr->max_rom < 1);
 
        /* ROM size must be a multiple of csr->max_rom */
        romsize = (romsize + (csr->max_rom - 1)) & ~(csr->max_rom - 1);
 
        csr_addr = csr->ops->allocate_addr_range(romsize, csr->max_rom, csr->private);
-       if (csr_addr == ~0ULL) {
-               return CSR1212_ENOMEM;
+       if (csr_addr == CSR1212_INVALID_ADDR_SPACE) {
+               return -ENOMEM;
        }
        if (csr_addr < CSR1212_REGISTER_SPACE_BASE) {
                /* Invalid address returned from allocate_addr_range(). */
                csr->ops->release_addr(csr_addr, csr->private);
-               return CSR1212_ENOMEM;
+               return -ENOMEM;
        }
 
        cache = csr1212_rom_cache_malloc(csr_addr - CSR1212_REGISTER_SPACE_BASE, romsize);
        if (!cache) {
                csr->ops->release_addr(csr_addr, csr->private);
-               return CSR1212_ENOMEM;
+               return -ENOMEM;
        }
 
        cache->ext_rom = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, CSR1212_KV_ID_EXTENDED_ROM);
        if (!cache->ext_rom) {
                csr->ops->release_addr(csr_addr, csr->private);
                CSR1212_FREE(cache);
-               return CSR1212_ENOMEM;
+               return -ENOMEM;
        }
 
        if (csr1212_attach_keyval_to_directory(csr->root_kv, cache->ext_rom) != CSR1212_SUCCESS) {
                csr1212_release_keyval(cache->ext_rom);
                csr->ops->release_addr(csr_addr, csr->private);
                CSR1212_FREE(cache);
-               return CSR1212_ENOMEM;
+               return -ENOMEM;
        }
        cache->ext_rom->offset = csr_addr - CSR1212_REGISTER_SPACE_BASE;
        cache->ext_rom->value.leaf.len = -1;
@@ -818,8 +643,8 @@ static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize)
        return CSR1212_SUCCESS;
 }
 
-static inline void csr1212_remove_cache(struct csr1212_csr *csr,
-                                       struct csr1212_csr_rom_cache *cache)
+static void csr1212_remove_cache(struct csr1212_csr *csr,
+                                struct csr1212_csr_rom_cache *cache)
 {
        if (csr->cache_head == cache)
                csr->cache_head = cache->next;
@@ -908,7 +733,7 @@ static int csr1212_generate_layout_subdir(struct csr1212_keyval *dir,
        return num_entries;
 }
 
-size_t csr1212_generate_layout_order(struct csr1212_keyval *kv)
+static size_t csr1212_generate_layout_order(struct csr1212_keyval *kv)
 {
        struct csr1212_keyval *ltail = kv;
        size_t agg_size = 0;
@@ -931,9 +756,9 @@ size_t csr1212_generate_layout_order(struct csr1212_keyval *kv)
        return quads_to_bytes(agg_size);
 }
 
-struct csr1212_keyval *csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
-                                                 struct csr1212_keyval *start_kv,
-                                                 int start_pos)
+static struct csr1212_keyval *
+csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
+                          struct csr1212_keyval *start_kv, int start_pos)
 {
        struct csr1212_keyval *kv = start_kv;
        struct csr1212_keyval *okv = start_kv;
@@ -977,8 +802,13 @@ struct csr1212_keyval *csr1212_generate_positions(struct csr1212_csr_rom_cache *
        return kv;
 }
 
-static void csr1212_generate_tree_subdir(struct csr1212_keyval *dir,
-                                        u_int32_t *data_buffer)
+#define CSR1212_KV_KEY_SHIFT           24
+#define CSR1212_KV_KEY_TYPE_SHIFT      6
+#define CSR1212_KV_KEY_ID_MASK         0x3f
+#define CSR1212_KV_KEY_TYPE_MASK       0x3     /* after shift */
+
+static void
+csr1212_generate_tree_subdir(struct csr1212_keyval *dir, u32 *data_buffer)
 {
        struct csr1212_dentry *dentry;
        struct csr1212_keyval *last_extkey_spec = NULL;
@@ -989,7 +819,7 @@ static void csr1212_generate_tree_subdir(struct csr1212_keyval *dir,
                struct csr1212_keyval *a;
 
                for (a = dentry->kv; a; a = a->associate) {
-                       u_int32_t value = 0;
+                       u32 value = 0;
 
                        /* Special Case: Extended Key Specifier_ID */
                        if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
@@ -1036,13 +866,21 @@ static void csr1212_generate_tree_subdir(struct csr1212_keyval *dir,
                        value |= (a->key.id & CSR1212_KV_KEY_ID_MASK) << CSR1212_KV_KEY_SHIFT;
                        value |= (a->key.type & CSR1212_KV_KEY_TYPE_MASK) <<
                                (CSR1212_KV_KEY_SHIFT + CSR1212_KV_KEY_TYPE_SHIFT);
-                       data_buffer[index] = CSR1212_CPU_TO_BE32(value);
+                       data_buffer[index] = cpu_to_be32(value);
                        index++;
                }
        }
 }
 
-void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
+struct csr1212_keyval_img {
+       u16 length;
+       u16 crc;
+
+       /* Must be last */
+       u32 data[0];    /* older gcc can't handle [] which is standard */
+};
+
+static void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
 {
        struct csr1212_keyval *kv, *nkv;
        struct csr1212_keyval_img *kvi;
@@ -1064,14 +902,14 @@ void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
                                memcpy(kvi->data, kv->value.leaf.data,
                                       quads_to_bytes(kv->value.leaf.len));
 
-                       kvi->length = CSR1212_CPU_TO_BE16(kv->value.leaf.len);
+                       kvi->length = cpu_to_be16(kv->value.leaf.len);
                        kvi->crc = csr1212_crc16(kvi->data, kv->value.leaf.len);
                        break;
 
                case CSR1212_KV_TYPE_DIRECTORY:
                        csr1212_generate_tree_subdir(kv, kvi->data);
 
-                       kvi->length = CSR1212_CPU_TO_BE16(kv->value.directory.len);
+                       kvi->length = cpu_to_be16(kv->value.directory.len);
                        kvi->crc = csr1212_crc16(kvi->data, kv->value.directory.len);
                        break;
                }
@@ -1086,6 +924,8 @@ void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
        }
 }
 
+#define CSR1212_EXTENDED_ROM_SIZE (0x10000 * sizeof(u32))
+
 int csr1212_generate_csr_image(struct csr1212_csr *csr)
 {
        struct csr1212_bus_info_block_img *bi;
@@ -1095,8 +935,7 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
        int ret;
        int init_offset;
 
-       if (!csr)
-               return CSR1212_EINVAL;
+       BUG_ON(!csr);
 
        cache = csr->cache_head;
 
@@ -1119,7 +958,7 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
                         * regions needed (it assumes that the cache holding
                         * the first 1K Config ROM space always exists). */
                        int est_c = agg_size / (CSR1212_EXTENDED_ROM_SIZE -
-                                               (2 * sizeof(u_int32_t))) + 1;
+                                               (2 * sizeof(u32))) + 1;
 
                        /* Add additional cache regions, extras will be
                         * removed later */
@@ -1136,7 +975,7 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
                }
                kv = csr1212_generate_positions(cache, kv, init_offset);
                agg_size -= cache->len;
-               init_offset = sizeof(u_int32_t);
+               init_offset = sizeof(u32);
        }
 
        /* Remove unused, excess cache regions */
@@ -1156,8 +995,7 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
 
                        /* Make sure the Extended ROM leaf is a multiple of
                         * max_rom in size. */
-                       if (csr->max_rom < 1)
-                               return CSR1212_EINVAL;
+                       BUG_ON(csr->max_rom < 1);
                        leaf_size = (cache->len + (csr->max_rom - 1)) &
                                ~(csr->max_rom - 1);
 
@@ -1166,7 +1004,7 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
                               leaf_size - cache->len);
 
                        /* Subtract leaf header */
-                       leaf_size -= sizeof(u_int32_t);
+                       leaf_size -= sizeof(u32);
 
                        /* Update the Extended ROM leaf length */
                        cache->ext_rom->value.leaf.len =
@@ -1184,18 +1022,17 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
                        /* Set the length and CRC of the extended ROM. */
                        struct csr1212_keyval_img *kvi =
                                (struct csr1212_keyval_img*)cache->data;
+                       u16 len = bytes_to_quads(cache->len) - 1;
 
-                       kvi->length = CSR1212_CPU_TO_BE16(bytes_to_quads(cache->len) - 1);
-                       kvi->crc = csr1212_crc16(kvi->data,
-                                                bytes_to_quads(cache->len) - 1);
-
+                       kvi->length = cpu_to_be16(len);
+                       kvi->crc = csr1212_crc16(kvi->data, len);
                }
        }
 
        return CSR1212_SUCCESS;
 }
 
-int csr1212_read(struct csr1212_csr *csr, u_int32_t offset, void *buffer, u_int32_t len)
+int csr1212_read(struct csr1212_csr *csr, u32 offset, void *buffer, u32 len)
 {
        struct csr1212_csr_rom_cache *cache;
 
@@ -1208,11 +1045,10 @@ int csr1212_read(struct csr1212_csr *csr, u_int32_t offset, void *buffer, u_int3
                        return CSR1212_SUCCESS;
                }
        }
-       return CSR1212_ENOENT;
+       return -ENOENT;
 }
 
 
-
 /* Parse a chunk of data as a Config ROM */
 
 static int csr1212_parse_bus_info_block(struct csr1212_csr *csr)
@@ -1227,13 +1063,19 @@ static int csr1212_parse_bus_info_block(struct csr1212_csr *csr)
         * Unfortunately, many IEEE 1394 devices do not abide by that, so the
         * bus info block will be read 1 quadlet at a time.  The rest of the
         * ConfigROM will be read according to the max_rom field. */
-       for (i = 0; i < csr->bus_info_len; i += sizeof(csr1212_quad_t)) {
+       for (i = 0; i < csr->bus_info_len; i += sizeof(u32)) {
                ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
-                                        sizeof(csr1212_quad_t),
+                                        sizeof(u32),
                                         &csr->cache_head->data[bytes_to_quads(i)],
                                         csr->private);
                if (ret != CSR1212_SUCCESS)
                        return ret;
+
+               /* check ROM header's info_length */
+               if (i == 0 &&
+                   be32_to_cpu(csr->cache_head->data[0]) >> 24 !=
+                   bytes_to_quads(csr->bus_info_len) - 1)
+                       return -EINVAL;
        }
 
        bi = (struct csr1212_bus_info_block_img*)csr->cache_head->data;
@@ -1241,29 +1083,26 @@ static int csr1212_parse_bus_info_block(struct csr1212_csr *csr)
 
        /* IEEE 1212 recommends that crc_len be equal to bus_info_len, but that is not
         * always the case, so read the rest of the crc area 1 quadlet at a time. */
-       for (i = csr->bus_info_len; i <= csr->crc_len; i += sizeof(csr1212_quad_t)) {
+       for (i = csr->bus_info_len; i <= csr->crc_len; i += sizeof(u32)) {
                ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
-                                        sizeof(csr1212_quad_t),
+                                        sizeof(u32),
                                         &csr->cache_head->data[bytes_to_quads(i)],
                                         csr->private);
                if (ret != CSR1212_SUCCESS)
                        return ret;
        }
 
-       if (bytes_to_quads(csr->bus_info_len - sizeof(csr1212_quad_t)) != bi->length)
-               return CSR1212_EINVAL;
-
 #if 0
        /* Apparently there are too many differnt wrong implementations of the
         * CRC algorithm that verifying them is moot. */
        if ((csr1212_crc16(bi->data, bi->crc_length) != bi->crc) &&
            (csr1212_msft_crc16(bi->data, bi->crc_length) != bi->crc))
-               return CSR1212_EINVAL;
+               return -EINVAL;
 #endif
 
-       cr = CSR1212_MALLOC(sizeof(struct csr1212_cache_region));
+       cr = CSR1212_MALLOC(sizeof(*cr));
        if (!cr)
-               return CSR1212_ENOMEM;
+               return -ENOMEM;
 
        cr->next = NULL;
        cr->prev = NULL;
@@ -1276,20 +1115,25 @@ static int csr1212_parse_bus_info_block(struct csr1212_csr *csr)
        return CSR1212_SUCCESS;
 }
 
+#define CSR1212_KV_KEY(q)      (be32_to_cpu(q) >> CSR1212_KV_KEY_SHIFT)
+#define CSR1212_KV_KEY_TYPE(q) (CSR1212_KV_KEY(q) >> CSR1212_KV_KEY_TYPE_SHIFT)
+#define CSR1212_KV_KEY_ID(q)   (CSR1212_KV_KEY(q) & CSR1212_KV_KEY_ID_MASK)
+#define CSR1212_KV_VAL_MASK    0xffffff
+#define CSR1212_KV_VAL(q)      (be32_to_cpu(q) & CSR1212_KV_VAL_MASK)
+
 static int csr1212_parse_dir_entry(struct csr1212_keyval *dir,
-                                  csr1212_quad_t ki,
-                                  u_int32_t kv_pos)
+                                  u32 ki, u32 kv_pos)
 {
        int ret = CSR1212_SUCCESS;
        struct csr1212_keyval *k = NULL;
-       u_int32_t offset;
+       u32 offset;
 
        switch(CSR1212_KV_KEY_TYPE(ki)) {
        case CSR1212_KV_TYPE_IMMEDIATE:
                k = csr1212_new_immediate(CSR1212_KV_KEY_ID(ki),
                                          CSR1212_KV_VAL(ki));
                if (!k) {
-                       ret = CSR1212_ENOMEM;
+                       ret = -ENOMEM;
                        goto fail;
                }
 
@@ -1300,7 +1144,7 @@ static int csr1212_parse_dir_entry(struct csr1212_keyval *dir,
                k = csr1212_new_csr_offset(CSR1212_KV_KEY_ID(ki),
                                           CSR1212_KV_VAL(ki));
                if (!k) {
-                       ret = CSR1212_ENOMEM;
+                       ret = -ENOMEM;
                        goto fail;
                }
                k->refcnt = 0;  /* Don't keep local reference when parsing. */
@@ -1313,7 +1157,7 @@ static int csr1212_parse_dir_entry(struct csr1212_keyval *dir,
                        /* Uh-oh.  Can't have a relative offset of 0 for Leaves
                         * or Directories.  The Config ROM image is most likely
                         * messed up, so we'll just abort here. */
-                       ret = CSR1212_EIO;
+                       ret = -EIO;
                        goto fail;
                }
 
@@ -1328,7 +1172,7 @@ static int csr1212_parse_dir_entry(struct csr1212_keyval *dir,
                        k = csr1212_new_leaf(CSR1212_KV_KEY_ID(ki), NULL, 0);
                }
                if (!k) {
-                       ret = CSR1212_ENOMEM;
+                       ret = -ENOMEM;
                        goto fail;
                }
                k->refcnt = 0;  /* Don't keep local reference when parsing. */
@@ -1343,14 +1187,11 @@ static int csr1212_parse_dir_entry(struct csr1212_keyval *dir,
        ret = csr1212_attach_keyval_to_directory(dir, k);
 
 fail:
-       if (ret != CSR1212_SUCCESS) {
-               if (k)
-                       free_keyval(k);
-       }
+       if (ret != CSR1212_SUCCESS && k != NULL)
+               free_keyval(k);
        return ret;
 }
 
-
 int csr1212_parse_keyval(struct csr1212_keyval *kv,
                         struct csr1212_csr_rom_cache *cache)
 {
@@ -1361,14 +1202,14 @@ int csr1212_parse_keyval(struct csr1212_keyval *kv,
 
        kvi = (struct csr1212_keyval_img*)&cache->data[bytes_to_quads(kv->offset -
                                                                      cache->offset)];
-       kvi_len = CSR1212_BE16_TO_CPU(kvi->length);
+       kvi_len = be16_to_cpu(kvi->length);
 
 #if 0
        /* Apparently there are too many differnt wrong implementations of the
         * CRC algorithm that verifying them is moot. */
        if ((csr1212_crc16(kvi->data, kvi_len) != kvi->crc) &&
            (csr1212_msft_crc16(kvi->data, kvi_len) != kvi->crc)) {
-               ret = CSR1212_EINVAL;
+               ret = -EINVAL;
                goto fail;
        }
 #endif
@@ -1376,7 +1217,7 @@ int csr1212_parse_keyval(struct csr1212_keyval *kv,
        switch(kv->key.type) {
        case CSR1212_KV_TYPE_DIRECTORY:
                for (i = 0; i < kvi_len; i++) {
-                       csr1212_quad_t ki = kvi->data[i];
+                       u32 ki = kvi->data[i];
 
                        /* Some devices put null entries in their unit
                         * directories.  If we come across such an entry,
@@ -1393,9 +1234,8 @@ int csr1212_parse_keyval(struct csr1212_keyval *kv,
        case CSR1212_KV_TYPE_LEAF:
                if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) {
                        kv->value.leaf.data = CSR1212_MALLOC(quads_to_bytes(kvi_len));
-                       if (!kv->value.leaf.data)
-                       {
-                               ret = CSR1212_ENOMEM;
+                       if (!kv->value.leaf.data) {
+                               ret = -ENOMEM;
                                goto fail;
                        }
 
@@ -1411,19 +1251,17 @@ fail:
        return ret;
 }
 
-
 int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
 {
        struct csr1212_cache_region *cr, *ncr, *newcr = NULL;
        struct csr1212_keyval_img *kvi = NULL;
        struct csr1212_csr_rom_cache *cache;
        int cache_index;
-       u_int64_t addr;
-       u_int32_t *cache_ptr;
-       u_int16_t kv_len = 0;
+       u64 addr;
+       u32 *cache_ptr;
+       u16 kv_len = 0;
 
-       if (!csr || !kv || csr->max_rom < 1)
-               return CSR1212_EINVAL;
+       BUG_ON(!csr || !kv || csr->max_rom < 1);
 
        /* First find which cache the data should be in (or go in if not read
         * yet). */
@@ -1434,27 +1272,26 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
        }
 
        if (!cache) {
-               csr1212_quad_t q;
-               u_int32_t cache_size;
+               u32 q, cache_size;
 
                /* Only create a new cache for Extended ROM leaves. */
                if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
-                       return CSR1212_EINVAL;
+                       return -EINVAL;
 
                if (csr->ops->bus_read(csr,
                                       CSR1212_REGISTER_SPACE_BASE + kv->offset,
-                                      sizeof(csr1212_quad_t), &q, csr->private)) {
-                       return CSR1212_EIO;
+                                      sizeof(u32), &q, csr->private)) {
+                       return -EIO;
                }
 
-               kv->value.leaf.len = CSR1212_BE32_TO_CPU(q) >> 16;
+               kv->value.leaf.len = be32_to_cpu(q) >> 16;
 
                cache_size = (quads_to_bytes(kv->value.leaf.len + 1) +
                              (csr->max_rom - 1)) & ~(csr->max_rom - 1);
 
                cache = csr1212_rom_cache_malloc(kv->offset, cache_size);
                if (!cache)
-                       return CSR1212_ENOMEM;
+                       return -ENOMEM;
 
                kv->value.leaf.data = &cache->data[1];
                csr->cache_tail->next = cache;
@@ -1462,13 +1299,13 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
                cache->next = NULL;
                csr->cache_tail = cache;
                cache->filled_head =
-                       CSR1212_MALLOC(sizeof(struct csr1212_cache_region));
+                       CSR1212_MALLOC(sizeof(*cache->filled_head));
                if (!cache->filled_head) {
-                       return CSR1212_ENOMEM;
+                       return -ENOMEM;
                }
 
                cache->filled_head->offset_start = 0;
-               cache->filled_head->offset_end = sizeof(csr1212_quad_t);
+               cache->filled_head->offset_end = sizeof(u32);
                cache->filled_tail = cache->filled_head;
                cache->filled_head->next = NULL;
                cache->filled_head->prev = NULL;
@@ -1484,9 +1321,9 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
        /* Now seach read portions of the cache to see if it is there. */
        for (cr = cache->filled_head; cr; cr = cr->next) {
                if (cache_index < cr->offset_start) {
-                       newcr = CSR1212_MALLOC(sizeof(struct csr1212_cache_region));
+                       newcr = CSR1212_MALLOC(sizeof(*newcr));
                        if (!newcr)
-                               return CSR1212_ENOMEM;
+                               return -ENOMEM;
 
                        newcr->offset_start = cache_index & ~(csr->max_rom - 1);
                        newcr->offset_end = newcr->offset_start;
@@ -1499,8 +1336,7 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
                           (cache_index < cr->offset_end)) {
                        kvi = (struct csr1212_keyval_img*)
                                (&cache->data[bytes_to_quads(cache_index)]);
-                       kv_len = quads_to_bytes(CSR1212_BE16_TO_CPU(kvi->length) +
-                                               1);
+                       kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
                        break;
                } else if (cache_index == cr->offset_end)
                        break;
@@ -1508,9 +1344,9 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
 
        if (!cr) {
                cr = cache->filled_tail;
-               newcr = CSR1212_MALLOC(sizeof(struct csr1212_cache_region));
+               newcr = CSR1212_MALLOC(sizeof(*newcr));
                if (!newcr)
-                       return CSR1212_ENOMEM;
+                       return -ENOMEM;
 
                newcr->offset_start = cache_index & ~(csr->max_rom - 1);
                newcr->offset_end = newcr->offset_start;
@@ -1532,7 +1368,7 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
                                       csr->private)) {
                        if (csr->max_rom == 4)
                                /* We've got problems! */
-                               return CSR1212_EIO;
+                               return -EIO;
 
                        /* Apperently the max_rom value was a lie, set it to
                         * do quadlet reads and try again. */
@@ -1546,8 +1382,7 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
                if (!kvi && (cr->offset_end > cache_index)) {
                        kvi = (struct csr1212_keyval_img*)
                                (&cache->data[bytes_to_quads(cache_index)]);
-                       kv_len = quads_to_bytes(CSR1212_BE16_TO_CPU(kvi->length) +
-                                               1);
+                       kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
                }
 
                if ((kv_len + (kv->offset - cache->offset)) > cache->size) {
@@ -1555,7 +1390,7 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
                         * beyond the ConfigROM image region and thus beyond the
                         * end of our cache region.  Therefore, we abort now
                         * rather than seg faulting later. */
-                       return CSR1212_EIO;
+                       return -EIO;
                }
 
                ncr = cr->next;
@@ -1577,16 +1412,13 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
        return csr1212_parse_keyval(kv, cache);
 }
 
-
-
 int csr1212_parse_csr(struct csr1212_csr *csr)
 {
        static const int mr_map[] = { 4, 64, 1024, 0 };
        struct csr1212_dentry *dentry;
        int ret;
 
-       if (!csr || !csr->ops || !csr->ops->bus_read)
-               return CSR1212_EINVAL;
+       BUG_ON(!csr || !csr->ops || !csr->ops->bus_read);
 
        ret = csr1212_parse_bus_info_block(csr);
        if (ret != CSR1212_SUCCESS)
@@ -1598,7 +1430,7 @@ int csr1212_parse_csr(struct csr1212_csr *csr)
                int i = csr->ops->get_max_rom(csr->bus_info_data,
                                              csr->private);
                if (i & ~0x3)
-                       return CSR1212_EINVAL;
+                       return -EINVAL;
                csr->max_rom = mr_map[i];
        }
 
@@ -1611,15 +1443,17 @@ int csr1212_parse_csr(struct csr1212_csr *csr)
        csr->root_kv->valid = 0;
        csr->root_kv->next = csr->root_kv;
        csr->root_kv->prev = csr->root_kv;
-       csr1212_get_keyval(csr, csr->root_kv);
+       ret = _csr1212_read_keyval(csr, csr->root_kv);
+       if (ret != CSR1212_SUCCESS)
+               return ret;
 
        /* Scan through the Root directory finding all extended ROM regions
         * and make cache regions for them */
        for (dentry = csr->root_kv->value.directory.dentries_head;
             dentry; dentry = dentry->next) {
-               if (dentry->kv->key.id == CSR1212_KV_ID_EXTENDED_ROM) {
-                       csr1212_get_keyval(csr, dentry->kv);
-
+               if (dentry->kv->key.id == CSR1212_KV_ID_EXTENDED_ROM &&
+                       !dentry->kv->valid) {
+                       ret = _csr1212_read_keyval(csr, dentry->kv);
                        if (ret != CSR1212_SUCCESS)
                                return ret;
                }