aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ieee1394/csr1212.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/ieee1394/csr1212.c')
-rw-r--r--drivers/ieee1394/csr1212.c870
1 files changed, 352 insertions, 518 deletions
diff --git a/drivers/ieee1394/csr1212.c b/drivers/ieee1394/csr1212.c
index c28f639823d2..d08166bda1c5 100644
--- a/drivers/ieee1394/csr1212.c
+++ b/drivers/ieee1394/csr1212.c
@@ -31,12 +31,13 @@
31/* TODO List: 31/* TODO List:
32 * - Verify interface consistency: i.e., public functions that take a size 32 * - Verify interface consistency: i.e., public functions that take a size
33 * parameter expect size to be in bytes. 33 * parameter expect size to be in bytes.
34 * - Convenience functions for reading a block of data from a given offset.
35 */ 34 */
36 35
37#ifndef __KERNEL__ 36#include <linux/errno.h>
38#include <string.h> 37#include <linux/kernel.h>
39#endif 38#include <linux/string.h>
39#include <asm/bug.h>
40#include <asm/byteorder.h>
40 41
41#include "csr1212.h" 42#include "csr1212.h"
42 43
@@ -46,7 +47,7 @@
46#define __C (1 << CSR1212_KV_TYPE_CSR_OFFSET) 47#define __C (1 << CSR1212_KV_TYPE_CSR_OFFSET)
47#define __D (1 << CSR1212_KV_TYPE_DIRECTORY) 48#define __D (1 << CSR1212_KV_TYPE_DIRECTORY)
48#define __L (1 << CSR1212_KV_TYPE_LEAF) 49#define __L (1 << CSR1212_KV_TYPE_LEAF)
49static const u_int8_t csr1212_key_id_type_map[0x30] = { 50static const u8 csr1212_key_id_type_map[0x30] = {
50 __C, /* used by Apple iSight */ 51 __C, /* used by Apple iSight */
51 __D | __L, /* Descriptor */ 52 __D | __L, /* Descriptor */
52 __I | __D | __L, /* Bus_Dependent_Info */ 53 __I | __D | __L, /* Bus_Dependent_Info */
@@ -82,10 +83,10 @@ static const u_int8_t csr1212_key_id_type_map[0x30] = {
82#undef __L 83#undef __L
83 84
84 85
85#define quads_to_bytes(_q) ((_q) * sizeof(u_int32_t)) 86#define quads_to_bytes(_q) ((_q) * sizeof(u32))
86#define bytes_to_quads(_b) (((_b) + sizeof(u_int32_t) - 1) / sizeof(u_int32_t)) 87#define bytes_to_quads(_b) (((_b) + sizeof(u32) - 1) / sizeof(u32))
87 88
88static inline void free_keyval(struct csr1212_keyval *kv) 89static void free_keyval(struct csr1212_keyval *kv)
89{ 90{
90 if ((kv->key.type == CSR1212_KV_TYPE_LEAF) && 91 if ((kv->key.type == CSR1212_KV_TYPE_LEAF) &&
91 (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)) 92 (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM))
@@ -94,14 +95,14 @@ static inline void free_keyval(struct csr1212_keyval *kv)
94 CSR1212_FREE(kv); 95 CSR1212_FREE(kv);
95} 96}
96 97
97static u_int16_t csr1212_crc16(const u_int32_t *buffer, size_t length) 98static u16 csr1212_crc16(const u32 *buffer, size_t length)
98{ 99{
99 int shift; 100 int shift;
100 u_int32_t data; 101 u32 data;
101 u_int16_t sum, crc = 0; 102 u16 sum, crc = 0;
102 103
103 for (; length; length--) { 104 for (; length; length--) {
104 data = CSR1212_BE32_TO_CPU(*buffer); 105 data = be32_to_cpu(*buffer);
105 buffer++; 106 buffer++;
106 for (shift = 28; shift >= 0; shift -= 4 ) { 107 for (shift = 28; shift >= 0; shift -= 4 ) {
107 sum = ((crc >> 12) ^ (data >> shift)) & 0xf; 108 sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
@@ -110,21 +111,18 @@ static u_int16_t csr1212_crc16(const u_int32_t *buffer, size_t length)
110 crc &= 0xffff; 111 crc &= 0xffff;
111 } 112 }
112 113
113 return CSR1212_CPU_TO_BE16(crc); 114 return cpu_to_be16(crc);
114} 115}
115 116
116#if 0 117/* Microsoft computes the CRC with the bytes in reverse order. */
117/* Microsoft computes the CRC with the bytes in reverse order. Therefore we 118static u16 csr1212_msft_crc16(const u32 *buffer, size_t length)
118 * have a special version of the CRC algorithm to account for their buggy
119 * software. */
120static u_int16_t csr1212_msft_crc16(const u_int32_t *buffer, size_t length)
121{ 119{
122 int shift; 120 int shift;
123 u_int32_t data; 121 u32 data;
124 u_int16_t sum, crc = 0; 122 u16 sum, crc = 0;
125 123
126 for (; length; length--) { 124 for (; length; length--) {
127 data = CSR1212_LE32_TO_CPU(*buffer); 125 data = le32_to_cpu(*buffer);
128 buffer++; 126 buffer++;
129 for (shift = 28; shift >= 0; shift -= 4 ) { 127 for (shift = 28; shift >= 0; shift -= 4 ) {
130 sum = ((crc >> 12) ^ (data >> shift)) & 0xf; 128 sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
@@ -133,38 +131,35 @@ static u_int16_t csr1212_msft_crc16(const u_int32_t *buffer, size_t length)
133 crc &= 0xffff; 131 crc &= 0xffff;
134 } 132 }
135 133
136 return CSR1212_CPU_TO_BE16(crc); 134 return cpu_to_be16(crc);
137} 135}
138#endif
139 136
140static inline struct csr1212_dentry *csr1212_find_keyval(struct csr1212_keyval *dir, 137static struct csr1212_dentry *
141 struct csr1212_keyval *kv) 138csr1212_find_keyval(struct csr1212_keyval *dir, struct csr1212_keyval *kv)
142{ 139{
143 struct csr1212_dentry *pos; 140 struct csr1212_dentry *pos;
144 141
145 for (pos = dir->value.directory.dentries_head; 142 for (pos = dir->value.directory.dentries_head;
146 pos != NULL; pos = pos->next) { 143 pos != NULL; pos = pos->next)
147 if (pos->kv == kv) 144 if (pos->kv == kv)
148 return pos; 145 return pos;
149 }
150 return NULL; 146 return NULL;
151} 147}
152 148
153 149static struct csr1212_keyval *
154static inline struct csr1212_keyval *csr1212_find_keyval_offset(struct csr1212_keyval *kv_list, 150csr1212_find_keyval_offset(struct csr1212_keyval *kv_list, u32 offset)
155 u_int32_t offset)
156{ 151{
157 struct csr1212_keyval *kv; 152 struct csr1212_keyval *kv;
158 153
159 for (kv = kv_list->next; kv && (kv != kv_list); kv = kv->next) { 154 for (kv = kv_list->next; kv && (kv != kv_list); kv = kv->next)
160 if (kv->offset == offset) 155 if (kv->offset == offset)
161 return kv; 156 return kv;
162 }
163 return NULL; 157 return NULL;
164} 158}
165 159
166 160
167/* Creation Routines */ 161/* Creation Routines */
162
168struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops, 163struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
169 size_t bus_info_size, void *private) 164 size_t bus_info_size, void *private)
170{ 165{
@@ -202,27 +197,17 @@ struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
202 return csr; 197 return csr;
203} 198}
204 199
205
206
207void csr1212_init_local_csr(struct csr1212_csr *csr, 200void csr1212_init_local_csr(struct csr1212_csr *csr,
208 const u_int32_t *bus_info_data, int max_rom) 201 const u32 *bus_info_data, int max_rom)
209{ 202{
210 static const int mr_map[] = { 4, 64, 1024, 0 }; 203 static const int mr_map[] = { 4, 64, 1024, 0 };
211 204
212#ifdef __KERNEL__
213 BUG_ON(max_rom & ~0x3); 205 BUG_ON(max_rom & ~0x3);
214 csr->max_rom = mr_map[max_rom]; 206 csr->max_rom = mr_map[max_rom];
215#else
216 if (max_rom & ~0x3) /* caller supplied invalid argument */
217 csr->max_rom = 0;
218 else
219 csr->max_rom = mr_map[max_rom];
220#endif
221 memcpy(csr->bus_info_data, bus_info_data, csr->bus_info_len); 207 memcpy(csr->bus_info_data, bus_info_data, csr->bus_info_len);
222} 208}
223 209
224 210static struct csr1212_keyval *csr1212_new_keyval(u8 type, u8 key)
225static struct csr1212_keyval *csr1212_new_keyval(u_int8_t type, u_int8_t key)
226{ 211{
227 struct csr1212_keyval *kv; 212 struct csr1212_keyval *kv;
228 213
@@ -246,10 +231,11 @@ static struct csr1212_keyval *csr1212_new_keyval(u_int8_t type, u_int8_t key)
246 return kv; 231 return kv;
247} 232}
248 233
249struct csr1212_keyval *csr1212_new_immediate(u_int8_t key, u_int32_t value) 234struct csr1212_keyval *csr1212_new_immediate(u8 key, u32 value)
250{ 235{
251 struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_IMMEDIATE, key); 236 struct csr1212_keyval *kv;
252 237
238 kv = csr1212_new_keyval(CSR1212_KV_TYPE_IMMEDIATE, key);
253 if (!kv) 239 if (!kv)
254 return NULL; 240 return NULL;
255 241
@@ -258,10 +244,12 @@ struct csr1212_keyval *csr1212_new_immediate(u_int8_t key, u_int32_t value)
258 return kv; 244 return kv;
259} 245}
260 246
261struct csr1212_keyval *csr1212_new_leaf(u_int8_t key, const void *data, size_t data_len) 247static struct csr1212_keyval *
248csr1212_new_leaf(u8 key, const void *data, size_t data_len)
262{ 249{
263 struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, key); 250 struct csr1212_keyval *kv;
264 251
252 kv = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, key);
265 if (!kv) 253 if (!kv)
266 return NULL; 254 return NULL;
267 255
@@ -285,10 +273,12 @@ struct csr1212_keyval *csr1212_new_leaf(u_int8_t key, const void *data, size_t d
285 return kv; 273 return kv;
286} 274}
287 275
288struct csr1212_keyval *csr1212_new_csr_offset(u_int8_t key, u_int32_t csr_offset) 276static struct csr1212_keyval *
277csr1212_new_csr_offset(u8 key, u32 csr_offset)
289{ 278{
290 struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_CSR_OFFSET, key); 279 struct csr1212_keyval *kv;
291 280
281 kv = csr1212_new_keyval(CSR1212_KV_TYPE_CSR_OFFSET, key);
292 if (!kv) 282 if (!kv)
293 return NULL; 283 return NULL;
294 284
@@ -299,10 +289,11 @@ struct csr1212_keyval *csr1212_new_csr_offset(u_int8_t key, u_int32_t csr_offset
299 return kv; 289 return kv;
300} 290}
301 291
302struct csr1212_keyval *csr1212_new_directory(u_int8_t key) 292struct csr1212_keyval *csr1212_new_directory(u8 key)
303{ 293{
304 struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_DIRECTORY, key); 294 struct csr1212_keyval *kv;
305 295
296 kv = csr1212_new_keyval(CSR1212_KV_TYPE_DIRECTORY, key);
306 if (!kv) 297 if (!kv)
307 return NULL; 298 return NULL;
308 299
@@ -314,43 +305,29 @@ struct csr1212_keyval *csr1212_new_directory(u_int8_t key)
314 return kv; 305 return kv;
315} 306}
316 307
317int csr1212_associate_keyval(struct csr1212_keyval *kv, 308void csr1212_associate_keyval(struct csr1212_keyval *kv,
318 struct csr1212_keyval *associate) 309 struct csr1212_keyval *associate)
319{ 310{
320 if (!kv || !associate) 311 BUG_ON(!kv || !associate || kv->key.id == CSR1212_KV_ID_DESCRIPTOR ||
321 return CSR1212_EINVAL; 312 (associate->key.id != CSR1212_KV_ID_DESCRIPTOR &&
322 313 associate->key.id != CSR1212_KV_ID_DEPENDENT_INFO &&
323 if (kv->key.id == CSR1212_KV_ID_DESCRIPTOR || 314 associate->key.id != CSR1212_KV_ID_EXTENDED_KEY &&
324 (associate->key.id != CSR1212_KV_ID_DESCRIPTOR && 315 associate->key.id != CSR1212_KV_ID_EXTENDED_DATA &&
325 associate->key.id != CSR1212_KV_ID_DEPENDENT_INFO && 316 associate->key.id < 0x30) ||
326 associate->key.id != CSR1212_KV_ID_EXTENDED_KEY && 317 (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID &&
327 associate->key.id != CSR1212_KV_ID_EXTENDED_DATA && 318 associate->key.id != CSR1212_KV_ID_EXTENDED_KEY) ||
328 associate->key.id < 0x30)) 319 (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
329 return CSR1212_EINVAL; 320 associate->key.id != CSR1212_KV_ID_EXTENDED_DATA) ||
330 321 (associate->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
331 if (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID && 322 kv->key.id != CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) ||
332 associate->key.id != CSR1212_KV_ID_EXTENDED_KEY) 323 (associate->key.id == CSR1212_KV_ID_EXTENDED_DATA &&
333 return CSR1212_EINVAL; 324 kv->key.id != CSR1212_KV_ID_EXTENDED_KEY));
334
335 if (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
336 associate->key.id != CSR1212_KV_ID_EXTENDED_DATA)
337 return CSR1212_EINVAL;
338
339 if (associate->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
340 kv->key.id != CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID)
341 return CSR1212_EINVAL;
342
343 if (associate->key.id == CSR1212_KV_ID_EXTENDED_DATA &&
344 kv->key.id != CSR1212_KV_ID_EXTENDED_KEY)
345 return CSR1212_EINVAL;
346 325
347 if (kv->associate) 326 if (kv->associate)
348 csr1212_release_keyval(kv->associate); 327 csr1212_release_keyval(kv->associate);
349 328
350 associate->refcnt++; 329 associate->refcnt++;
351 kv->associate = associate; 330 kv->associate = associate;
352
353 return CSR1212_SUCCESS;
354} 331}
355 332
356int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir, 333int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
@@ -358,12 +335,11 @@ int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
358{ 335{
359 struct csr1212_dentry *dentry; 336 struct csr1212_dentry *dentry;
360 337
361 if (!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY) 338 BUG_ON(!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY);
362 return CSR1212_EINVAL;
363 339
364 dentry = CSR1212_MALLOC(sizeof(*dentry)); 340 dentry = CSR1212_MALLOC(sizeof(*dentry));
365 if (!dentry) 341 if (!dentry)
366 return CSR1212_ENOMEM; 342 return -ENOMEM;
367 343
368 dentry->kv = kv; 344 dentry->kv = kv;
369 345
@@ -382,66 +358,22 @@ int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
382 return CSR1212_SUCCESS; 358 return CSR1212_SUCCESS;
383} 359}
384 360
385struct csr1212_keyval *csr1212_new_extended_immediate(u_int32_t spec, u_int32_t key, 361#define CSR1212_DESCRIPTOR_LEAF_DATA(kv) \
386 u_int32_t value) 362 (&((kv)->value.leaf.data[1]))
387{ 363
388 struct csr1212_keyval *kvs, *kvk, *kvv; 364#define CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, type) \
389 365 ((kv)->value.leaf.data[0] = \
390 kvs = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID, spec); 366 cpu_to_be32(CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) | \
391 kvk = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY, key); 367 ((type) << CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)))
392 kvv = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_DATA, value); 368#define CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, spec_id) \
393 369 ((kv)->value.leaf.data[0] = \
394 if (!kvs || !kvk || !kvv) { 370 cpu_to_be32((CSR1212_DESCRIPTOR_LEAF_TYPE(kv) << \
395 if (kvs) 371 CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT) | \
396 free_keyval(kvs); 372 ((spec_id) & CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)))
397 if (kvk) 373
398 free_keyval(kvk); 374static struct csr1212_keyval *
399 if (kvv) 375csr1212_new_descriptor_leaf(u8 dtype, u32 specifier_id,
400 free_keyval(kvv); 376 const void *data, size_t data_len)
401 return NULL;
402 }
403
404 /* Don't keep a local reference to the extended key or value. */
405 kvk->refcnt = 0;
406 kvv->refcnt = 0;
407
408 csr1212_associate_keyval(kvk, kvv);
409 csr1212_associate_keyval(kvs, kvk);
410
411 return kvs;
412}
413
414struct csr1212_keyval *csr1212_new_extended_leaf(u_int32_t spec, u_int32_t key,
415 const void *data, size_t data_len)
416{
417 struct csr1212_keyval *kvs, *kvk, *kvv;
418
419 kvs = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID, spec);
420 kvk = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY, key);
421 kvv = csr1212_new_leaf(CSR1212_KV_ID_EXTENDED_DATA, data, data_len);
422
423 if (!kvs || !kvk || !kvv) {
424 if (kvs)
425 free_keyval(kvs);
426 if (kvk)
427 free_keyval(kvk);
428 if (kvv)
429 free_keyval(kvv);
430 return NULL;
431 }
432
433 /* Don't keep a local reference to the extended key or value. */
434 kvk->refcnt = 0;
435 kvv->refcnt = 0;
436
437 csr1212_associate_keyval(kvk, kvv);
438 csr1212_associate_keyval(kvs, kvk);
439
440 return kvs;
441}
442
443struct csr1212_keyval *csr1212_new_descriptor_leaf(u_int8_t dtype, u_int32_t specifier_id,
444 const void *data, size_t data_len)
445{ 377{
446 struct csr1212_keyval *kv; 378 struct csr1212_keyval *kv;
447 379
@@ -453,197 +385,72 @@ struct csr1212_keyval *csr1212_new_descriptor_leaf(u_int8_t dtype, u_int32_t spe
453 CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, dtype); 385 CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, dtype);
454 CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, specifier_id); 386 CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, specifier_id);
455 387
456 if (data) { 388 if (data)
457 memcpy(CSR1212_DESCRIPTOR_LEAF_DATA(kv), data, data_len); 389 memcpy(CSR1212_DESCRIPTOR_LEAF_DATA(kv), data, data_len);
458 }
459
460 return kv;
461}
462
463
464struct csr1212_keyval *csr1212_new_textual_descriptor_leaf(u_int8_t cwidth,
465 u_int16_t cset,
466 u_int16_t language,
467 const void *data,
468 size_t data_len)
469{
470 struct csr1212_keyval *kv;
471 char *lstr;
472
473 kv = csr1212_new_descriptor_leaf(0, 0, NULL, data_len +
474 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD);
475 if (!kv)
476 return NULL;
477
478 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_WIDTH(kv, cwidth);
479 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_CHAR_SET(kv, cset);
480 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language);
481
482 lstr = (char*)CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv);
483
484 /* make sure last quadlet is zeroed out */
485 *((u_int32_t*)&(lstr[(data_len - 1) & ~0x3])) = 0;
486
487 /* don't copy the NUL terminator */
488 memcpy(lstr, data, data_len);
489 390
490 return kv; 391 return kv;
491} 392}
492 393
394/* Check if string conforms to minimal ASCII as per IEEE 1212 clause 7.4 */
493static int csr1212_check_minimal_ascii(const char *s) 395static int csr1212_check_minimal_ascii(const char *s)
494{ 396{
495 static const char minimal_ascii_table[] = { 397 static const char minimal_ascii_table[] = {
496 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 398 /* 1 2 4 8 16 32 64 128 */
497 0x00, 0x00, 0x0a, 0x00, 0x0C, 0x0D, 0x00, 0x00, 399 128, /* --, --, --, --, --, --, --, 07, */
498 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 400 4 + 16 + 32, /* --, --, 0a, --, 0C, 0D, --, --, */
499 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 401 0, /* --, --, --, --, --, --, --, --, */
500 0x20, 0x21, 0x22, 0x00, 0x00, 0x25, 0x26, 0x27, 402 0, /* --, --, --, --, --, --, --, --, */
501 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 403 255 - 8 - 16, /* 20, 21, 22, --, --, 25, 26, 27, */
502 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 404 255, /* 28, 29, 2a, 2b, 2c, 2d, 2e, 2f, */
503 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 405 255, /* 30, 31, 32, 33, 34, 35, 36, 37, */
504 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 406 255, /* 38, 39, 3a, 3b, 3c, 3d, 3e, 3f, */
505 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 407 255, /* 40, 41, 42, 43, 44, 45, 46, 47, */
506 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 408 255, /* 48, 49, 4a, 4b, 4c, 4d, 4e, 4f, */
507 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x5f, 409 255, /* 50, 51, 52, 53, 54, 55, 56, 57, */
508 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 410 1 + 2 + 4 + 128, /* 58, 59, 5a, --, --, --, --, 5f, */
509 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 411 255 - 1, /* --, 61, 62, 63, 64, 65, 66, 67, */
510 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 412 255, /* 68, 69, 6a, 6b, 6c, 6d, 6e, 6f, */
511 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 413 255, /* 70, 71, 72, 73, 74, 75, 76, 77, */
414 1 + 2 + 4, /* 78, 79, 7a, --, --, --, --, --, */
512 }; 415 };
416 int i, j;
417
513 for (; *s; s++) { 418 for (; *s; s++) {
514 if (minimal_ascii_table[*s & 0x7F] != *s) 419 i = *s >> 3; /* i = *s / 8; */
515 return -1; /* failed */ 420 j = 1 << (*s & 3); /* j = 1 << (*s % 8); */
421
422 if (i >= ARRAY_SIZE(minimal_ascii_table) ||
423 !(minimal_ascii_table[i] & j))
424 return -EINVAL;
516 } 425 }
517 /* String conforms to minimal-ascii, as specified by IEEE 1212,
518 * par. 7.4 */
519 return 0; 426 return 0;
520} 427}
521 428
429/* IEEE 1212 clause 7.5.4.1 textual descriptors (English, minimal ASCII) */
522struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s) 430struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s)
523{ 431{
524 /* Check if string conform to minimal_ascii format */
525 if (csr1212_check_minimal_ascii(s))
526 return NULL;
527
528 /* IEEE 1212, par. 7.5.4.1 Textual descriptors (minimal ASCII) */
529 return csr1212_new_textual_descriptor_leaf(0, 0, 0, s, strlen(s));
530}
531
532struct csr1212_keyval *csr1212_new_icon_descriptor_leaf(u_int32_t version,
533 u_int8_t palette_depth,
534 u_int8_t color_space,
535 u_int16_t language,
536 u_int16_t hscan,
537 u_int16_t vscan,
538 u_int32_t *palette,
539 u_int32_t *pixels)
540{
541 static const int pd[4] = { 0, 4, 16, 256 };
542 static const int cs[16] = { 4, 2 };
543 struct csr1212_keyval *kv; 432 struct csr1212_keyval *kv;
544 int palette_size; 433 u32 *text;
545 int pixel_size = (hscan * vscan + 3) & ~0x3; 434 size_t str_len, quads;
546 435
547 if (!pixels || (!palette && palette_depth) || 436 if (!s || !*s || csr1212_check_minimal_ascii(s))
548 (palette_depth & ~0x3) || (color_space & ~0xf))
549 return NULL; 437 return NULL;
550 438
551 palette_size = pd[palette_depth] * cs[color_space]; 439 str_len = strlen(s);
552 440 quads = bytes_to_quads(str_len);
553 kv = csr1212_new_descriptor_leaf(1, 0, NULL, 441 kv = csr1212_new_descriptor_leaf(0, 0, NULL, quads_to_bytes(quads) +
554 palette_size + pixel_size + 442 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD);
555 CSR1212_ICON_DESCRIPTOR_LEAF_OVERHEAD);
556 if (!kv) 443 if (!kv)
557 return NULL; 444 return NULL;
558 445
559 CSR1212_ICON_DESCRIPTOR_LEAF_SET_VERSION(kv, version); 446 kv->value.leaf.data[1] = 0; /* width, character_set, language */
560 CSR1212_ICON_DESCRIPTOR_LEAF_SET_PALETTE_DEPTH(kv, palette_depth); 447 text = CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv);
561 CSR1212_ICON_DESCRIPTOR_LEAF_SET_COLOR_SPACE(kv, color_space); 448 text[quads - 1] = 0; /* padding */
562 CSR1212_ICON_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language); 449 memcpy(text, s, str_len);
563 CSR1212_ICON_DESCRIPTOR_LEAF_SET_HSCAN(kv, hscan);
564 CSR1212_ICON_DESCRIPTOR_LEAF_SET_VSCAN(kv, vscan);
565
566 if (palette_size)
567 memcpy(CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE(kv), palette,
568 palette_size);
569
570 memcpy(CSR1212_ICON_DESCRIPTOR_LEAF_PIXELS(kv), pixels, pixel_size);
571
572 return kv;
573}
574
575struct csr1212_keyval *csr1212_new_modifiable_descriptor_leaf(u_int16_t max_size,
576 u_int64_t address)
577{
578 struct csr1212_keyval *kv;
579
580 /* IEEE 1212, par. 7.5.4.3 Modifiable descriptors */
581 kv = csr1212_new_leaf(CSR1212_KV_ID_MODIFIABLE_DESCRIPTOR, NULL, sizeof(u_int64_t));
582 if(!kv)
583 return NULL;
584
585 CSR1212_MODIFIABLE_DESCRIPTOR_SET_MAX_SIZE(kv, max_size);
586 CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_HI(kv, address);
587 CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_LO(kv, address);
588 450
589 return kv; 451 return kv;
590} 452}
591 453
592static int csr1212_check_keyword(const char *s)
593{
594 for (; *s; s++) {
595
596 if (('A' <= *s) && (*s <= 'Z'))
597 continue;
598 if (('0' <= *s) && (*s <= '9'))
599 continue;
600 if (*s == '-')
601 continue;
602
603 return -1; /* failed */
604 }
605 /* String conforms to keyword, as specified by IEEE 1212,
606 * par. 7.6.5 */
607 return CSR1212_SUCCESS;
608}
609
610struct csr1212_keyval *csr1212_new_keyword_leaf(int strc, const char *strv[])
611{
612 struct csr1212_keyval *kv;
613 char *buffer;
614 int i, data_len = 0;
615
616 /* Check all keywords to see if they conform to restrictions:
617 * Only the following characters is allowed ['A'..'Z','0'..'9','-']
618 * Each word is zero-terminated.
619 * Also calculate the total length of the keywords.
620 */
621 for (i = 0; i < strc; i++) {
622 if (!strv[i] || csr1212_check_keyword(strv[i])) {
623 return NULL;
624 }
625 data_len += strlen(strv[i]) + 1; /* Add zero-termination char. */
626 }
627
628 /* IEEE 1212, par. 7.6.5 Keyword leaves */
629 kv = csr1212_new_leaf(CSR1212_KV_ID_KEYWORD, NULL, data_len);
630 if (!kv)
631 return NULL;
632
633 buffer = (char *)kv->value.leaf.data;
634
635 /* make sure last quadlet is zeroed out */
636 *((u_int32_t*)&(buffer[(data_len - 1) & ~0x3])) = 0;
637
638 /* Copy keyword(s) into leaf data buffer */
639 for (i = 0; i < strc; i++) {
640 int len = strlen(strv[i]) + 1;
641 memcpy(buffer, strv[i], len);
642 buffer += len;
643 }
644 return kv;
645}
646
647 454
648/* Destruction Routines */ 455/* Destruction Routines */
649 456
@@ -674,23 +481,12 @@ void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir,
674 csr1212_release_keyval(kv); 481 csr1212_release_keyval(kv);
675} 482}
676 483
677
678void csr1212_disassociate_keyval(struct csr1212_keyval *kv)
679{
680 if (kv->associate) {
681 csr1212_release_keyval(kv->associate);
682 }
683
684 kv->associate = NULL;
685}
686
687
688/* This function is used to free the memory taken by a keyval. If the given 484/* This function is used to free the memory taken by a keyval. If the given
689 * keyval is a directory type, then any keyvals contained in that directory 485 * keyval is a directory type, then any keyvals contained in that directory
690 * will be destroyed as well if their respective refcnts are 0. By means of 486 * will be destroyed as well if their respective refcnts are 0. By means of
691 * list manipulation, this routine will descend a directory structure in a 487 * list manipulation, this routine will descend a directory structure in a
692 * non-recursive manner. */ 488 * non-recursive manner. */
693void _csr1212_destroy_keyval(struct csr1212_keyval *kv) 489static void csr1212_destroy_keyval(struct csr1212_keyval *kv)
694{ 490{
695 struct csr1212_keyval *k, *a; 491 struct csr1212_keyval *k, *a;
696 struct csr1212_dentry dentry; 492 struct csr1212_dentry dentry;
@@ -715,11 +511,13 @@ void _csr1212_destroy_keyval(struct csr1212_keyval *kv)
715 a = k->associate; 511 a = k->associate;
716 512
717 if (k->key.type == CSR1212_KV_TYPE_DIRECTORY) { 513 if (k->key.type == CSR1212_KV_TYPE_DIRECTORY) {
718 /* If the current entry is a directory, then move all 514 /* If the current entry is a directory, move all
719 * the entries to the destruction list. */ 515 * the entries to the destruction list. */
720 if (k->value.directory.dentries_head) { 516 if (k->value.directory.dentries_head) {
721 tail->next = k->value.directory.dentries_head; 517 tail->next =
722 k->value.directory.dentries_head->prev = tail; 518 k->value.directory.dentries_head;
519 k->value.directory.dentries_head->prev =
520 tail;
723 tail = k->value.directory.dentries_tail; 521 tail = k->value.directory.dentries_tail;
724 } 522 }
725 } 523 }
@@ -729,15 +527,22 @@ void _csr1212_destroy_keyval(struct csr1212_keyval *kv)
729 527
730 head = head->next; 528 head = head->next;
731 if (head) { 529 if (head) {
732 if (head->prev && head->prev != &dentry) { 530 if (head->prev && head->prev != &dentry)
733 CSR1212_FREE(head->prev); 531 CSR1212_FREE(head->prev);
734 }
735 head->prev = NULL; 532 head->prev = NULL;
736 } else if (tail != &dentry) 533 } else if (tail != &dentry) {
737 CSR1212_FREE(tail); 534 CSR1212_FREE(tail);
535 }
738 } 536 }
739} 537}
740 538
539void csr1212_release_keyval(struct csr1212_keyval *kv)
540{
541 if (kv->refcnt > 1)
542 kv->refcnt--;
543 else
544 csr1212_destroy_keyval(kv);
545}
741 546
742void csr1212_destroy_csr(struct csr1212_csr *csr) 547void csr1212_destroy_csr(struct csr1212_csr *csr)
743{ 548{
@@ -763,49 +568,51 @@ void csr1212_destroy_csr(struct csr1212_csr *csr)
763} 568}
764 569
765 570
766
767/* CSR Image Creation */ 571/* CSR Image Creation */
768 572
769static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize) 573static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize)
770{ 574{
771 struct csr1212_csr_rom_cache *cache; 575 struct csr1212_csr_rom_cache *cache;
772 u_int64_t csr_addr; 576 u64 csr_addr;
773 577
774 if (!csr || !csr->ops || !csr->ops->allocate_addr_range || 578 BUG_ON(!csr || !csr->ops || !csr->ops->allocate_addr_range ||
775 !csr->ops->release_addr || csr->max_rom < 1) 579 !csr->ops->release_addr || csr->max_rom < 1);
776 return CSR1212_EINVAL;
777 580
778 /* ROM size must be a multiple of csr->max_rom */ 581 /* ROM size must be a multiple of csr->max_rom */
779 romsize = (romsize + (csr->max_rom - 1)) & ~(csr->max_rom - 1); 582 romsize = (romsize + (csr->max_rom - 1)) & ~(csr->max_rom - 1);
780 583
781 csr_addr = csr->ops->allocate_addr_range(romsize, csr->max_rom, csr->private); 584 csr_addr = csr->ops->allocate_addr_range(romsize, csr->max_rom,
782 if (csr_addr == CSR1212_INVALID_ADDR_SPACE) { 585 csr->private);
783 return CSR1212_ENOMEM; 586 if (csr_addr == CSR1212_INVALID_ADDR_SPACE)
784 } 587 return -ENOMEM;
588
785 if (csr_addr < CSR1212_REGISTER_SPACE_BASE) { 589 if (csr_addr < CSR1212_REGISTER_SPACE_BASE) {
786 /* Invalid address returned from allocate_addr_range(). */ 590 /* Invalid address returned from allocate_addr_range(). */
787 csr->ops->release_addr(csr_addr, csr->private); 591 csr->ops->release_addr(csr_addr, csr->private);
788 return CSR1212_ENOMEM; 592 return -ENOMEM;
789 } 593 }
790 594
791 cache = csr1212_rom_cache_malloc(csr_addr - CSR1212_REGISTER_SPACE_BASE, romsize); 595 cache = csr1212_rom_cache_malloc(csr_addr - CSR1212_REGISTER_SPACE_BASE,
596 romsize);
792 if (!cache) { 597 if (!cache) {
793 csr->ops->release_addr(csr_addr, csr->private); 598 csr->ops->release_addr(csr_addr, csr->private);
794 return CSR1212_ENOMEM; 599 return -ENOMEM;
795 } 600 }
796 601
797 cache->ext_rom = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, CSR1212_KV_ID_EXTENDED_ROM); 602 cache->ext_rom = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF,
603 CSR1212_KV_ID_EXTENDED_ROM);
798 if (!cache->ext_rom) { 604 if (!cache->ext_rom) {
799 csr->ops->release_addr(csr_addr, csr->private); 605 csr->ops->release_addr(csr_addr, csr->private);
800 CSR1212_FREE(cache); 606 CSR1212_FREE(cache);
801 return CSR1212_ENOMEM; 607 return -ENOMEM;
802 } 608 }
803 609
804 if (csr1212_attach_keyval_to_directory(csr->root_kv, cache->ext_rom) != CSR1212_SUCCESS) { 610 if (csr1212_attach_keyval_to_directory(csr->root_kv, cache->ext_rom) !=
611 CSR1212_SUCCESS) {
805 csr1212_release_keyval(cache->ext_rom); 612 csr1212_release_keyval(cache->ext_rom);
806 csr->ops->release_addr(csr_addr, csr->private); 613 csr->ops->release_addr(csr_addr, csr->private);
807 CSR1212_FREE(cache); 614 CSR1212_FREE(cache);
808 return CSR1212_ENOMEM; 615 return -ENOMEM;
809 } 616 }
810 cache->ext_rom->offset = csr_addr - CSR1212_REGISTER_SPACE_BASE; 617 cache->ext_rom->offset = csr_addr - CSR1212_REGISTER_SPACE_BASE;
811 cache->ext_rom->value.leaf.len = -1; 618 cache->ext_rom->value.leaf.len = -1;
@@ -818,8 +625,8 @@ static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize)
818 return CSR1212_SUCCESS; 625 return CSR1212_SUCCESS;
819} 626}
820 627
821static inline void csr1212_remove_cache(struct csr1212_csr *csr, 628static void csr1212_remove_cache(struct csr1212_csr *csr,
822 struct csr1212_csr_rom_cache *cache) 629 struct csr1212_csr_rom_cache *cache)
823{ 630{
824 if (csr->cache_head == cache) 631 if (csr->cache_head == cache)
825 csr->cache_head = cache->next; 632 csr->cache_head = cache->next;
@@ -832,7 +639,8 @@ static inline void csr1212_remove_cache(struct csr1212_csr *csr,
832 cache->next->prev = cache->prev; 639 cache->next->prev = cache->prev;
833 640
834 if (cache->ext_rom) { 641 if (cache->ext_rom) {
835 csr1212_detach_keyval_from_directory(csr->root_kv, cache->ext_rom); 642 csr1212_detach_keyval_from_directory(csr->root_kv,
643 cache->ext_rom);
836 csr1212_release_keyval(cache->ext_rom); 644 csr1212_release_keyval(cache->ext_rom);
837 } 645 }
838 646
@@ -852,28 +660,29 @@ static int csr1212_generate_layout_subdir(struct csr1212_keyval *dir,
852 dentry = dentry->next) { 660 dentry = dentry->next) {
853 for (dkv = dentry->kv; dkv; dkv = dkv->associate) { 661 for (dkv = dentry->kv; dkv; dkv = dkv->associate) {
854 /* Special Case: Extended Key Specifier_ID */ 662 /* Special Case: Extended Key Specifier_ID */
855 if (dkv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) { 663 if (dkv->key.id ==
856 if (last_extkey_spec == NULL) { 664 CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
665 if (last_extkey_spec == NULL)
857 last_extkey_spec = dkv; 666 last_extkey_spec = dkv;
858 } else if (dkv->value.immediate != last_extkey_spec->value.immediate) { 667 else if (dkv->value.immediate !=
668 last_extkey_spec->value.immediate)
859 last_extkey_spec = dkv; 669 last_extkey_spec = dkv;
860 } else { 670 else
861 continue; 671 continue;
862 }
863 /* Special Case: Extended Key */ 672 /* Special Case: Extended Key */
864 } else if (dkv->key.id == CSR1212_KV_ID_EXTENDED_KEY) { 673 } else if (dkv->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
865 if (last_extkey == NULL) { 674 if (last_extkey == NULL)
866 last_extkey = dkv; 675 last_extkey = dkv;
867 } else if (dkv->value.immediate != last_extkey->value.immediate) { 676 else if (dkv->value.immediate !=
677 last_extkey->value.immediate)
868 last_extkey = dkv; 678 last_extkey = dkv;
869 } else { 679 else
870 continue; 680 continue;
871 }
872 } 681 }
873 682
874 num_entries += 1; 683 num_entries += 1;
875 684
876 switch(dkv->key.type) { 685 switch (dkv->key.type) {
877 default: 686 default:
878 case CSR1212_KV_TYPE_IMMEDIATE: 687 case CSR1212_KV_TYPE_IMMEDIATE:
879 case CSR1212_KV_TYPE_CSR_OFFSET: 688 case CSR1212_KV_TYPE_CSR_OFFSET:
@@ -891,8 +700,9 @@ static int csr1212_generate_layout_subdir(struct csr1212_keyval *dir,
891 /* Special case: Extended ROM leafs */ 700 /* Special case: Extended ROM leafs */
892 if (dkv->key.id == CSR1212_KV_ID_EXTENDED_ROM) { 701 if (dkv->key.id == CSR1212_KV_ID_EXTENDED_ROM) {
893 dkv->value.leaf.len = -1; 702 dkv->value.leaf.len = -1;
894 /* Don't add Extended ROM leafs in the layout list, 703 /* Don't add Extended ROM leafs in the
895 * they are handled differently. */ 704 * layout list, they are handled
705 * differently. */
896 break; 706 break;
897 } 707 }
898 708
@@ -908,20 +718,21 @@ static int csr1212_generate_layout_subdir(struct csr1212_keyval *dir,
908 return num_entries; 718 return num_entries;
909} 719}
910 720
911size_t csr1212_generate_layout_order(struct csr1212_keyval *kv) 721static size_t csr1212_generate_layout_order(struct csr1212_keyval *kv)
912{ 722{
913 struct csr1212_keyval *ltail = kv; 723 struct csr1212_keyval *ltail = kv;
914 size_t agg_size = 0; 724 size_t agg_size = 0;
915 725
916 while(kv) { 726 while (kv) {
917 switch(kv->key.type) { 727 switch (kv->key.type) {
918 case CSR1212_KV_TYPE_LEAF: 728 case CSR1212_KV_TYPE_LEAF:
919 /* Add 1 quadlet for crc/len field */ 729 /* Add 1 quadlet for crc/len field */
920 agg_size += kv->value.leaf.len + 1; 730 agg_size += kv->value.leaf.len + 1;
921 break; 731 break;
922 732
923 case CSR1212_KV_TYPE_DIRECTORY: 733 case CSR1212_KV_TYPE_DIRECTORY:
924 kv->value.directory.len = csr1212_generate_layout_subdir(kv, &ltail); 734 kv->value.directory.len =
735 csr1212_generate_layout_subdir(kv, &ltail);
925 /* Add 1 quadlet for crc/len field */ 736 /* Add 1 quadlet for crc/len field */
926 agg_size += kv->value.directory.len + 1; 737 agg_size += kv->value.directory.len + 1;
927 break; 738 break;
@@ -931,9 +742,9 @@ size_t csr1212_generate_layout_order(struct csr1212_keyval *kv)
931 return quads_to_bytes(agg_size); 742 return quads_to_bytes(agg_size);
932} 743}
933 744
934struct csr1212_keyval *csr1212_generate_positions(struct csr1212_csr_rom_cache *cache, 745static struct csr1212_keyval *
935 struct csr1212_keyval *start_kv, 746csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
936 int start_pos) 747 struct csr1212_keyval *start_kv, int start_pos)
937{ 748{
938 struct csr1212_keyval *kv = start_kv; 749 struct csr1212_keyval *kv = start_kv;
939 struct csr1212_keyval *okv = start_kv; 750 struct csr1212_keyval *okv = start_kv;
@@ -942,13 +753,12 @@ struct csr1212_keyval *csr1212_generate_positions(struct csr1212_csr_rom_cache *
942 753
943 cache->layout_head = kv; 754 cache->layout_head = kv;
944 755
945 while(kv && pos < cache->size) { 756 while (kv && pos < cache->size) {
946 /* Special case: Extended ROM leafs */ 757 /* Special case: Extended ROM leafs */
947 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) { 758 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
948 kv->offset = cache->offset + pos; 759 kv->offset = cache->offset + pos;
949 }
950 760
951 switch(kv->key.type) { 761 switch (kv->key.type) {
952 case CSR1212_KV_TYPE_LEAF: 762 case CSR1212_KV_TYPE_LEAF:
953 kv_len = kv->value.leaf.len; 763 kv_len = kv->value.leaf.len;
954 break; 764 break;
@@ -959,6 +769,7 @@ struct csr1212_keyval *csr1212_generate_positions(struct csr1212_csr_rom_cache *
959 769
960 default: 770 default:
961 /* Should never get here */ 771 /* Should never get here */
772 WARN_ON(1);
962 break; 773 break;
963 } 774 }
964 775
@@ -972,46 +783,55 @@ struct csr1212_keyval *csr1212_generate_positions(struct csr1212_csr_rom_cache *
972 } 783 }
973 784
974 cache->layout_tail = okv; 785 cache->layout_tail = okv;
975 cache->len = (okv->offset - cache->offset) + quads_to_bytes(okv_len + 1); 786 cache->len = okv->offset - cache->offset + quads_to_bytes(okv_len + 1);
976 787
977 return kv; 788 return kv;
978} 789}
979 790
980static void csr1212_generate_tree_subdir(struct csr1212_keyval *dir, 791#define CSR1212_KV_KEY_SHIFT 24
981 u_int32_t *data_buffer) 792#define CSR1212_KV_KEY_TYPE_SHIFT 6
793#define CSR1212_KV_KEY_ID_MASK 0x3f
794#define CSR1212_KV_KEY_TYPE_MASK 0x3 /* after shift */
795
796static void
797csr1212_generate_tree_subdir(struct csr1212_keyval *dir, u32 *data_buffer)
982{ 798{
983 struct csr1212_dentry *dentry; 799 struct csr1212_dentry *dentry;
984 struct csr1212_keyval *last_extkey_spec = NULL; 800 struct csr1212_keyval *last_extkey_spec = NULL;
985 struct csr1212_keyval *last_extkey = NULL; 801 struct csr1212_keyval *last_extkey = NULL;
986 int index = 0; 802 int index = 0;
987 803
988 for (dentry = dir->value.directory.dentries_head; dentry; dentry = dentry->next) { 804 for (dentry = dir->value.directory.dentries_head;
805 dentry;
806 dentry = dentry->next) {
989 struct csr1212_keyval *a; 807 struct csr1212_keyval *a;
990 808
991 for (a = dentry->kv; a; a = a->associate) { 809 for (a = dentry->kv; a; a = a->associate) {
992 u_int32_t value = 0; 810 u32 value = 0;
993 811
994 /* Special Case: Extended Key Specifier_ID */ 812 /* Special Case: Extended Key Specifier_ID */
995 if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) { 813 if (a->key.id ==
996 if (last_extkey_spec == NULL) { 814 CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
815 if (last_extkey_spec == NULL)
997 last_extkey_spec = a; 816 last_extkey_spec = a;
998 } else if (a->value.immediate != last_extkey_spec->value.immediate) { 817 else if (a->value.immediate !=
818 last_extkey_spec->value.immediate)
999 last_extkey_spec = a; 819 last_extkey_spec = a;
1000 } else { 820 else
1001 continue; 821 continue;
1002 } 822
1003 /* Special Case: Extended Key */ 823 /* Special Case: Extended Key */
1004 } else if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY) { 824 } else if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
1005 if (last_extkey == NULL) { 825 if (last_extkey == NULL)
1006 last_extkey = a; 826 last_extkey = a;
1007 } else if (a->value.immediate != last_extkey->value.immediate) { 827 else if (a->value.immediate !=
828 last_extkey->value.immediate)
1008 last_extkey = a; 829 last_extkey = a;
1009 } else { 830 else
1010 continue; 831 continue;
1011 }
1012 } 832 }
1013 833
1014 switch(a->key.type) { 834 switch (a->key.type) {
1015 case CSR1212_KV_TYPE_IMMEDIATE: 835 case CSR1212_KV_TYPE_IMMEDIATE:
1016 value = a->value.immediate; 836 value = a->value.immediate;
1017 break; 837 break;
@@ -1030,32 +850,46 @@ static void csr1212_generate_tree_subdir(struct csr1212_keyval *dir,
1030 break; 850 break;
1031 default: 851 default:
1032 /* Should never get here */ 852 /* Should never get here */
1033 break; /* GDB breakpoint */ 853 WARN_ON(1);
854 break;
1034 } 855 }
1035 856
1036 value |= (a->key.id & CSR1212_KV_KEY_ID_MASK) << CSR1212_KV_KEY_SHIFT; 857 value |= (a->key.id & CSR1212_KV_KEY_ID_MASK) <<
858 CSR1212_KV_KEY_SHIFT;
1037 value |= (a->key.type & CSR1212_KV_KEY_TYPE_MASK) << 859 value |= (a->key.type & CSR1212_KV_KEY_TYPE_MASK) <<
1038 (CSR1212_KV_KEY_SHIFT + CSR1212_KV_KEY_TYPE_SHIFT); 860 (CSR1212_KV_KEY_SHIFT +
1039 data_buffer[index] = CSR1212_CPU_TO_BE32(value); 861 CSR1212_KV_KEY_TYPE_SHIFT);
862 data_buffer[index] = cpu_to_be32(value);
1040 index++; 863 index++;
1041 } 864 }
1042 } 865 }
1043} 866}
1044 867
1045void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache) 868struct csr1212_keyval_img {
869 u16 length;
870 u16 crc;
871
872 /* Must be last */
873 u32 data[0]; /* older gcc can't handle [] which is standard */
874};
875
876static void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
1046{ 877{
1047 struct csr1212_keyval *kv, *nkv; 878 struct csr1212_keyval *kv, *nkv;
1048 struct csr1212_keyval_img *kvi; 879 struct csr1212_keyval_img *kvi;
1049 880
1050 for (kv = cache->layout_head; kv != cache->layout_tail->next; kv = nkv) { 881 for (kv = cache->layout_head;
1051 kvi = (struct csr1212_keyval_img *) 882 kv != cache->layout_tail->next;
1052 (cache->data + bytes_to_quads(kv->offset - cache->offset)); 883 kv = nkv) {
1053 switch(kv->key.type) { 884 kvi = (struct csr1212_keyval_img *)(cache->data +
885 bytes_to_quads(kv->offset - cache->offset));
886 switch (kv->key.type) {
1054 default: 887 default:
1055 case CSR1212_KV_TYPE_IMMEDIATE: 888 case CSR1212_KV_TYPE_IMMEDIATE:
1056 case CSR1212_KV_TYPE_CSR_OFFSET: 889 case CSR1212_KV_TYPE_CSR_OFFSET:
1057 /* Should never get here */ 890 /* Should never get here */
1058 break; /* GDB breakpoint */ 891 WARN_ON(1);
892 break;
1059 893
1060 case CSR1212_KV_TYPE_LEAF: 894 case CSR1212_KV_TYPE_LEAF:
1061 /* Don't copy over Extended ROM areas, they are 895 /* Don't copy over Extended ROM areas, they are
@@ -1064,15 +898,16 @@ void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
1064 memcpy(kvi->data, kv->value.leaf.data, 898 memcpy(kvi->data, kv->value.leaf.data,
1065 quads_to_bytes(kv->value.leaf.len)); 899 quads_to_bytes(kv->value.leaf.len));
1066 900
1067 kvi->length = CSR1212_CPU_TO_BE16(kv->value.leaf.len); 901 kvi->length = cpu_to_be16(kv->value.leaf.len);
1068 kvi->crc = csr1212_crc16(kvi->data, kv->value.leaf.len); 902 kvi->crc = csr1212_crc16(kvi->data, kv->value.leaf.len);
1069 break; 903 break;
1070 904
1071 case CSR1212_KV_TYPE_DIRECTORY: 905 case CSR1212_KV_TYPE_DIRECTORY:
1072 csr1212_generate_tree_subdir(kv, kvi->data); 906 csr1212_generate_tree_subdir(kv, kvi->data);
1073 907
1074 kvi->length = CSR1212_CPU_TO_BE16(kv->value.directory.len); 908 kvi->length = cpu_to_be16(kv->value.directory.len);
1075 kvi->crc = csr1212_crc16(kvi->data, kv->value.directory.len); 909 kvi->crc = csr1212_crc16(kvi->data,
910 kv->value.directory.len);
1076 break; 911 break;
1077 } 912 }
1078 913
@@ -1086,6 +921,10 @@ void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
1086 } 921 }
1087} 922}
1088 923
924/* This size is arbitrarily chosen.
925 * The struct overhead is subtracted for more economic allocations. */
926#define CSR1212_EXTENDED_ROM_SIZE (2048 - sizeof(struct csr1212_csr_rom_cache))
927
1089int csr1212_generate_csr_image(struct csr1212_csr *csr) 928int csr1212_generate_csr_image(struct csr1212_csr *csr)
1090{ 929{
1091 struct csr1212_bus_info_block_img *bi; 930 struct csr1212_bus_info_block_img *bi;
@@ -1095,8 +934,7 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
1095 int ret; 934 int ret;
1096 int init_offset; 935 int init_offset;
1097 936
1098 if (!csr) 937 BUG_ON(!csr);
1099 return CSR1212_EINVAL;
1100 938
1101 cache = csr->cache_head; 939 cache = csr->cache_head;
1102 940
@@ -1113,18 +951,21 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
1113 951
1114 init_offset = csr->bus_info_len; 952 init_offset = csr->bus_info_len;
1115 953
1116 for (kv = csr->root_kv, cache = csr->cache_head; kv; cache = cache->next) { 954 for (kv = csr->root_kv, cache = csr->cache_head;
955 kv;
956 cache = cache->next) {
1117 if (!cache) { 957 if (!cache) {
1118 /* Estimate approximate number of additional cache 958 /* Estimate approximate number of additional cache
1119 * regions needed (it assumes that the cache holding 959 * regions needed (it assumes that the cache holding
1120 * the first 1K Config ROM space always exists). */ 960 * the first 1K Config ROM space always exists). */
1121 int est_c = agg_size / (CSR1212_EXTENDED_ROM_SIZE - 961 int est_c = agg_size / (CSR1212_EXTENDED_ROM_SIZE -
1122 (2 * sizeof(u_int32_t))) + 1; 962 (2 * sizeof(u32))) + 1;
1123 963
1124 /* Add additional cache regions, extras will be 964 /* Add additional cache regions, extras will be
1125 * removed later */ 965 * removed later */
1126 for (; est_c; est_c--) { 966 for (; est_c; est_c--) {
1127 ret = csr1212_append_new_cache(csr, CSR1212_EXTENDED_ROM_SIZE); 967 ret = csr1212_append_new_cache(csr,
968 CSR1212_EXTENDED_ROM_SIZE);
1128 if (ret != CSR1212_SUCCESS) 969 if (ret != CSR1212_SUCCESS)
1129 return ret; 970 return ret;
1130 } 971 }
@@ -1136,7 +977,7 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
1136 } 977 }
1137 kv = csr1212_generate_positions(cache, kv, init_offset); 978 kv = csr1212_generate_positions(cache, kv, init_offset);
1138 agg_size -= cache->len; 979 agg_size -= cache->len;
1139 init_offset = sizeof(u_int32_t); 980 init_offset = sizeof(u32);
1140 } 981 }
1141 982
1142 /* Remove unused, excess cache regions */ 983 /* Remove unused, excess cache regions */
@@ -1149,15 +990,14 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
1149 990
1150 /* Go through the list backward so that when done, the correct CRC 991 /* Go through the list backward so that when done, the correct CRC
1151 * will be calculated for the Extended ROM areas. */ 992 * will be calculated for the Extended ROM areas. */
1152 for(cache = csr->cache_tail; cache; cache = cache->prev) { 993 for (cache = csr->cache_tail; cache; cache = cache->prev) {
1153 /* Only Extended ROM caches should have this set. */ 994 /* Only Extended ROM caches should have this set. */
1154 if (cache->ext_rom) { 995 if (cache->ext_rom) {
1155 int leaf_size; 996 int leaf_size;
1156 997
1157 /* Make sure the Extended ROM leaf is a multiple of 998 /* Make sure the Extended ROM leaf is a multiple of
1158 * max_rom in size. */ 999 * max_rom in size. */
1159 if (csr->max_rom < 1) 1000 BUG_ON(csr->max_rom < 1);
1160 return CSR1212_EINVAL;
1161 leaf_size = (cache->len + (csr->max_rom - 1)) & 1001 leaf_size = (cache->len + (csr->max_rom - 1)) &
1162 ~(csr->max_rom - 1); 1002 ~(csr->max_rom - 1);
1163 1003
@@ -1166,7 +1006,7 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
1166 leaf_size - cache->len); 1006 leaf_size - cache->len);
1167 1007
1168 /* Subtract leaf header */ 1008 /* Subtract leaf header */
1169 leaf_size -= sizeof(u_int32_t); 1009 leaf_size -= sizeof(u32);
1170 1010
1171 /* Update the Extended ROM leaf length */ 1011 /* Update the Extended ROM leaf length */
1172 cache->ext_rom->value.leaf.len = 1012 cache->ext_rom->value.leaf.len =
@@ -1184,33 +1024,31 @@ int csr1212_generate_csr_image(struct csr1212_csr *csr)
1184 /* Set the length and CRC of the extended ROM. */ 1024 /* Set the length and CRC of the extended ROM. */
1185 struct csr1212_keyval_img *kvi = 1025 struct csr1212_keyval_img *kvi =
1186 (struct csr1212_keyval_img*)cache->data; 1026 (struct csr1212_keyval_img*)cache->data;
1027 u16 len = bytes_to_quads(cache->len) - 1;
1187 1028
1188 kvi->length = CSR1212_CPU_TO_BE16(bytes_to_quads(cache->len) - 1); 1029 kvi->length = cpu_to_be16(len);
1189 kvi->crc = csr1212_crc16(kvi->data, 1030 kvi->crc = csr1212_crc16(kvi->data, len);
1190 bytes_to_quads(cache->len) - 1);
1191
1192 } 1031 }
1193 } 1032 }
1194 1033
1195 return CSR1212_SUCCESS; 1034 return CSR1212_SUCCESS;
1196} 1035}
1197 1036
1198int csr1212_read(struct csr1212_csr *csr, u_int32_t offset, void *buffer, u_int32_t len) 1037int csr1212_read(struct csr1212_csr *csr, u32 offset, void *buffer, u32 len)
1199{ 1038{
1200 struct csr1212_csr_rom_cache *cache; 1039 struct csr1212_csr_rom_cache *cache;
1201 1040
1202 for (cache = csr->cache_head; cache; cache = cache->next) { 1041 for (cache = csr->cache_head; cache; cache = cache->next)
1203 if (offset >= cache->offset && 1042 if (offset >= cache->offset &&
1204 (offset + len) <= (cache->offset + cache->size)) { 1043 (offset + len) <= (cache->offset + cache->size)) {
1205 memcpy(buffer, 1044 memcpy(buffer, &cache->data[
1206 &cache->data[bytes_to_quads(offset - cache->offset)], 1045 bytes_to_quads(offset - cache->offset)],
1207 len); 1046 len);
1208 return CSR1212_SUCCESS; 1047 return CSR1212_SUCCESS;
1209 } 1048 }
1210 }
1211 return CSR1212_ENOENT;
1212}
1213 1049
1050 return -ENOENT;
1051}
1214 1052
1215 1053
1216/* Parse a chunk of data as a Config ROM */ 1054/* Parse a chunk of data as a Config ROM */
@@ -1227,46 +1065,43 @@ static int csr1212_parse_bus_info_block(struct csr1212_csr *csr)
1227 * Unfortunately, many IEEE 1394 devices do not abide by that, so the 1065 * Unfortunately, many IEEE 1394 devices do not abide by that, so the
1228 * bus info block will be read 1 quadlet at a time. The rest of the 1066 * bus info block will be read 1 quadlet at a time. The rest of the
1229 * ConfigROM will be read according to the max_rom field. */ 1067 * ConfigROM will be read according to the max_rom field. */
1230 for (i = 0; i < csr->bus_info_len; i += sizeof(csr1212_quad_t)) { 1068 for (i = 0; i < csr->bus_info_len; i += sizeof(u32)) {
1231 ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i, 1069 ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
1232 sizeof(csr1212_quad_t), 1070 sizeof(u32), &csr->cache_head->data[bytes_to_quads(i)],
1233 &csr->cache_head->data[bytes_to_quads(i)], 1071 csr->private);
1234 csr->private);
1235 if (ret != CSR1212_SUCCESS) 1072 if (ret != CSR1212_SUCCESS)
1236 return ret; 1073 return ret;
1237 1074
1238 /* check ROM header's info_length */ 1075 /* check ROM header's info_length */
1239 if (i == 0 && 1076 if (i == 0 &&
1240 CSR1212_BE32_TO_CPU(csr->cache_head->data[0]) >> 24 != 1077 be32_to_cpu(csr->cache_head->data[0]) >> 24 !=
1241 bytes_to_quads(csr->bus_info_len) - 1) 1078 bytes_to_quads(csr->bus_info_len) - 1)
1242 return CSR1212_EINVAL; 1079 return -EINVAL;
1243 } 1080 }
1244 1081
1245 bi = (struct csr1212_bus_info_block_img*)csr->cache_head->data; 1082 bi = (struct csr1212_bus_info_block_img*)csr->cache_head->data;
1246 csr->crc_len = quads_to_bytes(bi->crc_length); 1083 csr->crc_len = quads_to_bytes(bi->crc_length);
1247 1084
1248 /* IEEE 1212 recommends that crc_len be equal to bus_info_len, but that is not 1085 /* IEEE 1212 recommends that crc_len be equal to bus_info_len, but that
1249 * always the case, so read the rest of the crc area 1 quadlet at a time. */ 1086 * is not always the case, so read the rest of the crc area 1 quadlet at
1250 for (i = csr->bus_info_len; i <= csr->crc_len; i += sizeof(csr1212_quad_t)) { 1087 * a time. */
1088 for (i = csr->bus_info_len; i <= csr->crc_len; i += sizeof(u32)) {
1251 ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i, 1089 ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
1252 sizeof(csr1212_quad_t), 1090 sizeof(u32), &csr->cache_head->data[bytes_to_quads(i)],
1253 &csr->cache_head->data[bytes_to_quads(i)], 1091 csr->private);
1254 csr->private);
1255 if (ret != CSR1212_SUCCESS) 1092 if (ret != CSR1212_SUCCESS)
1256 return ret; 1093 return ret;
1257 } 1094 }
1258 1095
1259#if 0 1096 /* Apparently there are many different wrong implementations of the CRC
1260 /* Apparently there are too many differnt wrong implementations of the 1097 * algorithm. We don't fail, we just warn. */
1261 * CRC algorithm that verifying them is moot. */
1262 if ((csr1212_crc16(bi->data, bi->crc_length) != bi->crc) && 1098 if ((csr1212_crc16(bi->data, bi->crc_length) != bi->crc) &&
1263 (csr1212_msft_crc16(bi->data, bi->crc_length) != bi->crc)) 1099 (csr1212_msft_crc16(bi->data, bi->crc_length) != bi->crc))
1264 return CSR1212_EINVAL; 1100 printk(KERN_DEBUG "IEEE 1394 device has ROM CRC error\n");
1265#endif
1266 1101
1267 cr = CSR1212_MALLOC(sizeof(*cr)); 1102 cr = CSR1212_MALLOC(sizeof(*cr));
1268 if (!cr) 1103 if (!cr)
1269 return CSR1212_ENOMEM; 1104 return -ENOMEM;
1270 1105
1271 cr->next = NULL; 1106 cr->next = NULL;
1272 cr->prev = NULL; 1107 cr->prev = NULL;
@@ -1279,21 +1114,26 @@ static int csr1212_parse_bus_info_block(struct csr1212_csr *csr)
1279 return CSR1212_SUCCESS; 1114 return CSR1212_SUCCESS;
1280} 1115}
1281 1116
1282static int csr1212_parse_dir_entry(struct csr1212_keyval *dir, 1117#define CSR1212_KV_KEY(q) (be32_to_cpu(q) >> CSR1212_KV_KEY_SHIFT)
1283 csr1212_quad_t ki, 1118#define CSR1212_KV_KEY_TYPE(q) (CSR1212_KV_KEY(q) >> CSR1212_KV_KEY_TYPE_SHIFT)
1284 u_int32_t kv_pos) 1119#define CSR1212_KV_KEY_ID(q) (CSR1212_KV_KEY(q) & CSR1212_KV_KEY_ID_MASK)
1120#define CSR1212_KV_VAL_MASK 0xffffff
1121#define CSR1212_KV_VAL(q) (be32_to_cpu(q) & CSR1212_KV_VAL_MASK)
1122
1123static int
1124csr1212_parse_dir_entry(struct csr1212_keyval *dir, u32 ki, u32 kv_pos)
1285{ 1125{
1286 int ret = CSR1212_SUCCESS; 1126 int ret = CSR1212_SUCCESS;
1287 struct csr1212_keyval *k = NULL; 1127 struct csr1212_keyval *k = NULL;
1288 u_int32_t offset; 1128 u32 offset;
1289 1129
1290 switch(CSR1212_KV_KEY_TYPE(ki)) { 1130 switch (CSR1212_KV_KEY_TYPE(ki)) {
1291 case CSR1212_KV_TYPE_IMMEDIATE: 1131 case CSR1212_KV_TYPE_IMMEDIATE:
1292 k = csr1212_new_immediate(CSR1212_KV_KEY_ID(ki), 1132 k = csr1212_new_immediate(CSR1212_KV_KEY_ID(ki),
1293 CSR1212_KV_VAL(ki)); 1133 CSR1212_KV_VAL(ki));
1294 if (!k) { 1134 if (!k) {
1295 ret = CSR1212_ENOMEM; 1135 ret = -ENOMEM;
1296 goto fail; 1136 goto out;
1297 } 1137 }
1298 1138
1299 k->refcnt = 0; /* Don't keep local reference when parsing. */ 1139 k->refcnt = 0; /* Don't keep local reference when parsing. */
@@ -1303,8 +1143,8 @@ static int csr1212_parse_dir_entry(struct csr1212_keyval *dir,
1303 k = csr1212_new_csr_offset(CSR1212_KV_KEY_ID(ki), 1143 k = csr1212_new_csr_offset(CSR1212_KV_KEY_ID(ki),
1304 CSR1212_KV_VAL(ki)); 1144 CSR1212_KV_VAL(ki));
1305 if (!k) { 1145 if (!k) {
1306 ret = CSR1212_ENOMEM; 1146 ret = -ENOMEM;
1307 goto fail; 1147 goto out;
1308 } 1148 }
1309 k->refcnt = 0; /* Don't keep local reference when parsing. */ 1149 k->refcnt = 0; /* Don't keep local reference when parsing. */
1310 break; 1150 break;
@@ -1316,8 +1156,8 @@ static int csr1212_parse_dir_entry(struct csr1212_keyval *dir,
1316 /* Uh-oh. Can't have a relative offset of 0 for Leaves 1156 /* Uh-oh. Can't have a relative offset of 0 for Leaves
1317 * or Directories. The Config ROM image is most likely 1157 * or Directories. The Config ROM image is most likely
1318 * messed up, so we'll just abort here. */ 1158 * messed up, so we'll just abort here. */
1319 ret = CSR1212_EIO; 1159 ret = -EIO;
1320 goto fail; 1160 goto out;
1321 } 1161 }
1322 1162
1323 k = csr1212_find_keyval_offset(dir, offset); 1163 k = csr1212_find_keyval_offset(dir, offset);
@@ -1325,14 +1165,14 @@ static int csr1212_parse_dir_entry(struct csr1212_keyval *dir,
1325 if (k) 1165 if (k)
1326 break; /* Found it. */ 1166 break; /* Found it. */
1327 1167
1328 if (CSR1212_KV_KEY_TYPE(ki) == CSR1212_KV_TYPE_DIRECTORY) { 1168 if (CSR1212_KV_KEY_TYPE(ki) == CSR1212_KV_TYPE_DIRECTORY)
1329 k = csr1212_new_directory(CSR1212_KV_KEY_ID(ki)); 1169 k = csr1212_new_directory(CSR1212_KV_KEY_ID(ki));
1330 } else { 1170 else
1331 k = csr1212_new_leaf(CSR1212_KV_KEY_ID(ki), NULL, 0); 1171 k = csr1212_new_leaf(CSR1212_KV_KEY_ID(ki), NULL, 0);
1332 } 1172
1333 if (!k) { 1173 if (!k) {
1334 ret = CSR1212_ENOMEM; 1174 ret = -ENOMEM;
1335 goto fail; 1175 goto out;
1336 } 1176 }
1337 k->refcnt = 0; /* Don't keep local reference when parsing. */ 1177 k->refcnt = 0; /* Don't keep local reference when parsing. */
1338 k->valid = 0; /* Contents not read yet so it's not valid. */ 1178 k->valid = 0; /* Contents not read yet so it's not valid. */
@@ -1344,16 +1184,12 @@ static int csr1212_parse_dir_entry(struct csr1212_keyval *dir,
1344 dir->next = k; 1184 dir->next = k;
1345 } 1185 }
1346 ret = csr1212_attach_keyval_to_directory(dir, k); 1186 ret = csr1212_attach_keyval_to_directory(dir, k);
1347 1187out:
1348fail: 1188 if (ret != CSR1212_SUCCESS && k != NULL)
1349 if (ret != CSR1212_SUCCESS) { 1189 free_keyval(k);
1350 if (k)
1351 free_keyval(k);
1352 }
1353 return ret; 1190 return ret;
1354} 1191}
1355 1192
1356
1357int csr1212_parse_keyval(struct csr1212_keyval *kv, 1193int csr1212_parse_keyval(struct csr1212_keyval *kv,
1358 struct csr1212_csr_rom_cache *cache) 1194 struct csr1212_csr_rom_cache *cache)
1359{ 1195{
@@ -1362,24 +1198,20 @@ int csr1212_parse_keyval(struct csr1212_keyval *kv,
1362 int ret = CSR1212_SUCCESS; 1198 int ret = CSR1212_SUCCESS;
1363 int kvi_len; 1199 int kvi_len;
1364 1200
1365 kvi = (struct csr1212_keyval_img*)&cache->data[bytes_to_quads(kv->offset - 1201 kvi = (struct csr1212_keyval_img*)
1366 cache->offset)]; 1202 &cache->data[bytes_to_quads(kv->offset - cache->offset)];
1367 kvi_len = CSR1212_BE16_TO_CPU(kvi->length); 1203 kvi_len = be16_to_cpu(kvi->length);
1368 1204
1369#if 0 1205 /* Apparently there are many different wrong implementations of the CRC
1370 /* Apparently there are too many differnt wrong implementations of the 1206 * algorithm. We don't fail, we just warn. */
1371 * CRC algorithm that verifying them is moot. */
1372 if ((csr1212_crc16(kvi->data, kvi_len) != kvi->crc) && 1207 if ((csr1212_crc16(kvi->data, kvi_len) != kvi->crc) &&
1373 (csr1212_msft_crc16(kvi->data, kvi_len) != kvi->crc)) { 1208 (csr1212_msft_crc16(kvi->data, kvi_len) != kvi->crc))
1374 ret = CSR1212_EINVAL; 1209 printk(KERN_DEBUG "IEEE 1394 device has ROM CRC error\n");
1375 goto fail;
1376 }
1377#endif
1378 1210
1379 switch(kv->key.type) { 1211 switch (kv->key.type) {
1380 case CSR1212_KV_TYPE_DIRECTORY: 1212 case CSR1212_KV_TYPE_DIRECTORY:
1381 for (i = 0; i < kvi_len; i++) { 1213 for (i = 0; i < kvi_len; i++) {
1382 csr1212_quad_t ki = kvi->data[i]; 1214 u32 ki = kvi->data[i];
1383 1215
1384 /* Some devices put null entries in their unit 1216 /* Some devices put null entries in their unit
1385 * directories. If we come across such an entry, 1217 * directories. If we come across such an entry,
@@ -1387,76 +1219,72 @@ int csr1212_parse_keyval(struct csr1212_keyval *kv,
1387 if (ki == 0x0) 1219 if (ki == 0x0)
1388 continue; 1220 continue;
1389 ret = csr1212_parse_dir_entry(kv, ki, 1221 ret = csr1212_parse_dir_entry(kv, ki,
1390 (kv->offset + 1222 kv->offset + quads_to_bytes(i + 1));
1391 quads_to_bytes(i + 1)));
1392 } 1223 }
1393 kv->value.directory.len = kvi_len; 1224 kv->value.directory.len = kvi_len;
1394 break; 1225 break;
1395 1226
1396 case CSR1212_KV_TYPE_LEAF: 1227 case CSR1212_KV_TYPE_LEAF:
1397 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) { 1228 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) {
1398 kv->value.leaf.data = CSR1212_MALLOC(quads_to_bytes(kvi_len)); 1229 size_t size = quads_to_bytes(kvi_len);
1230
1231 kv->value.leaf.data = CSR1212_MALLOC(size);
1399 if (!kv->value.leaf.data) { 1232 if (!kv->value.leaf.data) {
1400 ret = CSR1212_ENOMEM; 1233 ret = -ENOMEM;
1401 goto fail; 1234 goto out;
1402 } 1235 }
1403 1236
1404 kv->value.leaf.len = kvi_len; 1237 kv->value.leaf.len = kvi_len;
1405 memcpy(kv->value.leaf.data, kvi->data, quads_to_bytes(kvi_len)); 1238 memcpy(kv->value.leaf.data, kvi->data, size);
1406 } 1239 }
1407 break; 1240 break;
1408 } 1241 }
1409 1242
1410 kv->valid = 1; 1243 kv->valid = 1;
1411 1244out:
1412fail:
1413 return ret; 1245 return ret;
1414} 1246}
1415 1247
1416 1248static int
1417int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv) 1249csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1418{ 1250{
1419 struct csr1212_cache_region *cr, *ncr, *newcr = NULL; 1251 struct csr1212_cache_region *cr, *ncr, *newcr = NULL;
1420 struct csr1212_keyval_img *kvi = NULL; 1252 struct csr1212_keyval_img *kvi = NULL;
1421 struct csr1212_csr_rom_cache *cache; 1253 struct csr1212_csr_rom_cache *cache;
1422 int cache_index; 1254 int cache_index;
1423 u_int64_t addr; 1255 u64 addr;
1424 u_int32_t *cache_ptr; 1256 u32 *cache_ptr;
1425 u_int16_t kv_len = 0; 1257 u16 kv_len = 0;
1426 1258
1427 if (!csr || !kv || csr->max_rom < 1) 1259 BUG_ON(!csr || !kv || csr->max_rom < 1);
1428 return CSR1212_EINVAL;
1429 1260
1430 /* First find which cache the data should be in (or go in if not read 1261 /* First find which cache the data should be in (or go in if not read
1431 * yet). */ 1262 * yet). */
1432 for (cache = csr->cache_head; cache; cache = cache->next) { 1263 for (cache = csr->cache_head; cache; cache = cache->next)
1433 if (kv->offset >= cache->offset && 1264 if (kv->offset >= cache->offset &&
1434 kv->offset < (cache->offset + cache->size)) 1265 kv->offset < (cache->offset + cache->size))
1435 break; 1266 break;
1436 }
1437 1267
1438 if (!cache) { 1268 if (!cache) {
1439 csr1212_quad_t q; 1269 u32 q, cache_size;
1440 u_int32_t cache_size;
1441 1270
1442 /* Only create a new cache for Extended ROM leaves. */ 1271 /* Only create a new cache for Extended ROM leaves. */
1443 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) 1272 if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
1444 return CSR1212_EINVAL; 1273 return -EINVAL;
1445 1274
1446 if (csr->ops->bus_read(csr, 1275 if (csr->ops->bus_read(csr,
1447 CSR1212_REGISTER_SPACE_BASE + kv->offset, 1276 CSR1212_REGISTER_SPACE_BASE + kv->offset,
1448 sizeof(csr1212_quad_t), &q, csr->private)) { 1277 sizeof(u32), &q, csr->private))
1449 return CSR1212_EIO; 1278 return -EIO;
1450 }
1451 1279
1452 kv->value.leaf.len = CSR1212_BE32_TO_CPU(q) >> 16; 1280 kv->value.leaf.len = be32_to_cpu(q) >> 16;
1453 1281
1454 cache_size = (quads_to_bytes(kv->value.leaf.len + 1) + 1282 cache_size = (quads_to_bytes(kv->value.leaf.len + 1) +
1455 (csr->max_rom - 1)) & ~(csr->max_rom - 1); 1283 (csr->max_rom - 1)) & ~(csr->max_rom - 1);
1456 1284
1457 cache = csr1212_rom_cache_malloc(kv->offset, cache_size); 1285 cache = csr1212_rom_cache_malloc(kv->offset, cache_size);
1458 if (!cache) 1286 if (!cache)
1459 return CSR1212_ENOMEM; 1287 return -ENOMEM;
1460 1288
1461 kv->value.leaf.data = &cache->data[1]; 1289 kv->value.leaf.data = &cache->data[1];
1462 csr->cache_tail->next = cache; 1290 csr->cache_tail->next = cache;
@@ -1465,12 +1293,11 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1465 csr->cache_tail = cache; 1293 csr->cache_tail = cache;
1466 cache->filled_head = 1294 cache->filled_head =
1467 CSR1212_MALLOC(sizeof(*cache->filled_head)); 1295 CSR1212_MALLOC(sizeof(*cache->filled_head));
1468 if (!cache->filled_head) { 1296 if (!cache->filled_head)
1469 return CSR1212_ENOMEM; 1297 return -ENOMEM;
1470 }
1471 1298
1472 cache->filled_head->offset_start = 0; 1299 cache->filled_head->offset_start = 0;
1473 cache->filled_head->offset_end = sizeof(csr1212_quad_t); 1300 cache->filled_head->offset_end = sizeof(u32);
1474 cache->filled_tail = cache->filled_head; 1301 cache->filled_tail = cache->filled_head;
1475 cache->filled_head->next = NULL; 1302 cache->filled_head->next = NULL;
1476 cache->filled_head->prev = NULL; 1303 cache->filled_head->prev = NULL;
@@ -1488,7 +1315,7 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1488 if (cache_index < cr->offset_start) { 1315 if (cache_index < cr->offset_start) {
1489 newcr = CSR1212_MALLOC(sizeof(*newcr)); 1316 newcr = CSR1212_MALLOC(sizeof(*newcr));
1490 if (!newcr) 1317 if (!newcr)
1491 return CSR1212_ENOMEM; 1318 return -ENOMEM;
1492 1319
1493 newcr->offset_start = cache_index & ~(csr->max_rom - 1); 1320 newcr->offset_start = cache_index & ~(csr->max_rom - 1);
1494 newcr->offset_end = newcr->offset_start; 1321 newcr->offset_end = newcr->offset_start;
@@ -1501,18 +1328,18 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1501 (cache_index < cr->offset_end)) { 1328 (cache_index < cr->offset_end)) {
1502 kvi = (struct csr1212_keyval_img*) 1329 kvi = (struct csr1212_keyval_img*)
1503 (&cache->data[bytes_to_quads(cache_index)]); 1330 (&cache->data[bytes_to_quads(cache_index)]);
1504 kv_len = quads_to_bytes(CSR1212_BE16_TO_CPU(kvi->length) + 1331 kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
1505 1);
1506 break; 1332 break;
1507 } else if (cache_index == cr->offset_end) 1333 } else if (cache_index == cr->offset_end) {
1508 break; 1334 break;
1335 }
1509 } 1336 }
1510 1337
1511 if (!cr) { 1338 if (!cr) {
1512 cr = cache->filled_tail; 1339 cr = cache->filled_tail;
1513 newcr = CSR1212_MALLOC(sizeof(*newcr)); 1340 newcr = CSR1212_MALLOC(sizeof(*newcr));
1514 if (!newcr) 1341 if (!newcr)
1515 return CSR1212_ENOMEM; 1342 return -ENOMEM;
1516 1343
1517 newcr->offset_start = cache_index & ~(csr->max_rom - 1); 1344 newcr->offset_start = cache_index & ~(csr->max_rom - 1);
1518 newcr->offset_end = newcr->offset_start; 1345 newcr->offset_end = newcr->offset_start;
@@ -1534,7 +1361,7 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1534 csr->private)) { 1361 csr->private)) {
1535 if (csr->max_rom == 4) 1362 if (csr->max_rom == 4)
1536 /* We've got problems! */ 1363 /* We've got problems! */
1537 return CSR1212_EIO; 1364 return -EIO;
1538 1365
1539 /* Apperently the max_rom value was a lie, set it to 1366 /* Apperently the max_rom value was a lie, set it to
1540 * do quadlet reads and try again. */ 1367 * do quadlet reads and try again. */
@@ -1548,8 +1375,7 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1548 if (!kvi && (cr->offset_end > cache_index)) { 1375 if (!kvi && (cr->offset_end > cache_index)) {
1549 kvi = (struct csr1212_keyval_img*) 1376 kvi = (struct csr1212_keyval_img*)
1550 (&cache->data[bytes_to_quads(cache_index)]); 1377 (&cache->data[bytes_to_quads(cache_index)]);
1551 kv_len = quads_to_bytes(CSR1212_BE16_TO_CPU(kvi->length) + 1378 kv_len = quads_to_bytes(be16_to_cpu(kvi->length) + 1);
1552 1);
1553 } 1379 }
1554 1380
1555 if ((kv_len + (kv->offset - cache->offset)) > cache->size) { 1381 if ((kv_len + (kv->offset - cache->offset)) > cache->size) {
@@ -1557,7 +1383,7 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1557 * beyond the ConfigROM image region and thus beyond the 1383 * beyond the ConfigROM image region and thus beyond the
1558 * end of our cache region. Therefore, we abort now 1384 * end of our cache region. Therefore, we abort now
1559 * rather than seg faulting later. */ 1385 * rather than seg faulting later. */
1560 return CSR1212_EIO; 1386 return -EIO;
1561 } 1387 }
1562 1388
1563 ncr = cr->next; 1389 ncr = cr->next;
@@ -1579,7 +1405,16 @@ int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1579 return csr1212_parse_keyval(kv, cache); 1405 return csr1212_parse_keyval(kv, cache);
1580} 1406}
1581 1407
1582 1408struct csr1212_keyval *
1409csr1212_get_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
1410{
1411 if (!kv)
1412 return NULL;
1413 if (!kv->valid)
1414 if (csr1212_read_keyval(csr, kv) != CSR1212_SUCCESS)
1415 return NULL;
1416 return kv;
1417}
1583 1418
1584int csr1212_parse_csr(struct csr1212_csr *csr) 1419int csr1212_parse_csr(struct csr1212_csr *csr)
1585{ 1420{
@@ -1587,20 +1422,19 @@ int csr1212_parse_csr(struct csr1212_csr *csr)
1587 struct csr1212_dentry *dentry; 1422 struct csr1212_dentry *dentry;
1588 int ret; 1423 int ret;
1589 1424
1590 if (!csr || !csr->ops || !csr->ops->bus_read) 1425 BUG_ON(!csr || !csr->ops || !csr->ops->bus_read);
1591 return CSR1212_EINVAL;
1592 1426
1593 ret = csr1212_parse_bus_info_block(csr); 1427 ret = csr1212_parse_bus_info_block(csr);
1594 if (ret != CSR1212_SUCCESS) 1428 if (ret != CSR1212_SUCCESS)
1595 return ret; 1429 return ret;
1596 1430
1597 if (!csr->ops->get_max_rom) 1431 if (!csr->ops->get_max_rom) {
1598 csr->max_rom = mr_map[0]; /* default value */ 1432 csr->max_rom = mr_map[0]; /* default value */
1599 else { 1433 } else {
1600 int i = csr->ops->get_max_rom(csr->bus_info_data, 1434 int i = csr->ops->get_max_rom(csr->bus_info_data,
1601 csr->private); 1435 csr->private);
1602 if (i & ~0x3) 1436 if (i & ~0x3)
1603 return CSR1212_EINVAL; 1437 return -EINVAL;
1604 csr->max_rom = mr_map[i]; 1438 csr->max_rom = mr_map[i];
1605 } 1439 }
1606 1440
@@ -1613,7 +1447,7 @@ int csr1212_parse_csr(struct csr1212_csr *csr)
1613 csr->root_kv->valid = 0; 1447 csr->root_kv->valid = 0;
1614 csr->root_kv->next = csr->root_kv; 1448 csr->root_kv->next = csr->root_kv;
1615 csr->root_kv->prev = csr->root_kv; 1449 csr->root_kv->prev = csr->root_kv;
1616 ret = _csr1212_read_keyval(csr, csr->root_kv); 1450 ret = csr1212_read_keyval(csr, csr->root_kv);
1617 if (ret != CSR1212_SUCCESS) 1451 if (ret != CSR1212_SUCCESS)
1618 return ret; 1452 return ret;
1619 1453
@@ -1623,7 +1457,7 @@ int csr1212_parse_csr(struct csr1212_csr *csr)
1623 dentry; dentry = dentry->next) { 1457 dentry; dentry = dentry->next) {
1624 if (dentry->kv->key.id == CSR1212_KV_ID_EXTENDED_ROM && 1458 if (dentry->kv->key.id == CSR1212_KV_ID_EXTENDED_ROM &&
1625 !dentry->kv->valid) { 1459 !dentry->kv->valid) {
1626 ret = _csr1212_read_keyval(csr, dentry->kv); 1460 ret = csr1212_read_keyval(csr, dentry->kv);
1627 if (ret != CSR1212_SUCCESS) 1461 if (ret != CSR1212_SUCCESS)
1628 return ret; 1462 return ret;
1629 } 1463 }