2 * cistpl.c -- 16-bit PCMCIA Card Information Structure parser
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * The initial developer of the original code is David A. Hinds
9 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
10 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
12 * (C) 1999 David A. Hinds
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/major.h>
21 #include <linux/errno.h>
22 #include <linux/timer.h>
23 #include <linux/slab.h>
25 #include <linux/sched.h>
26 #include <linux/pci.h>
27 #include <linux/ioport.h>
29 #include <asm/byteorder.h>
31 #include <pcmcia/cs_types.h>
32 #include <pcmcia/ss.h>
33 #include <pcmcia/cs.h>
34 #include <pcmcia/bulkmem.h>
35 #include <pcmcia/cisreg.h>
36 #include <pcmcia/cistpl.h>
37 #include "cs_internal.h"
39 static const u_char mantissa
[] = {
40 10, 12, 13, 15, 20, 25, 30, 35,
41 40, 45, 50, 55, 60, 70, 80, 90
44 static const u_int exponent
[] = {
45 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
48 /* Convert an extended speed byte to a time in nanoseconds */
49 #define SPEED_CVT(v) \
50 (mantissa[(((v)>>3)&15)-1] * exponent[(v)&7] / 10)
51 /* Convert a power byte to a current in 0.1 microamps */
52 #define POWER_CVT(v) \
53 (mantissa[((v)>>3)&15] * exponent[(v)&7] / 10)
54 #define POWER_SCALE(v) (exponent[(v)&7])
56 /* Upper limit on reasonable # of tuples */
57 #define MAX_TUPLES 200
59 /*====================================================================*/
61 /* Parameters that can be set with 'insmod' */
63 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
65 INT_MODULE_PARM(cis_width
, 0); /* 16-bit CIS? */
67 void release_cis_mem(struct pcmcia_socket
*s
)
69 if (s
->cis_mem
.flags
& MAP_ACTIVE
) {
70 s
->cis_mem
.flags
&= ~MAP_ACTIVE
;
71 s
->ops
->set_mem_map(s
, &s
->cis_mem
);
73 release_resource(s
->cis_mem
.res
);
74 kfree(s
->cis_mem
.res
);
75 s
->cis_mem
.res
= NULL
;
81 EXPORT_SYMBOL(release_cis_mem
);
84 * Map the card memory at "card_offset" into virtual space.
85 * If flags & MAP_ATTRIB, map the attribute space, otherwise
86 * map the memory space.
89 set_cis_map(struct pcmcia_socket
*s
, unsigned int card_offset
, unsigned int flags
)
91 pccard_mem_map
*mem
= &s
->cis_mem
;
92 if (!(s
->features
& SS_CAP_STATIC_MAP
) && mem
->res
== NULL
) {
93 mem
->res
= find_mem_region(0, s
->map_size
, s
->map_size
, 0, s
);
94 if (mem
->res
== NULL
) {
95 printk(KERN_NOTICE
"cs: unable to map card memory!\n");
98 s
->cis_virt
= ioremap(mem
->res
->start
, s
->map_size
);
100 mem
->card_start
= card_offset
;
102 s
->ops
->set_mem_map(s
, mem
);
103 if (s
->features
& SS_CAP_STATIC_MAP
) {
105 iounmap(s
->cis_virt
);
106 s
->cis_virt
= ioremap(mem
->static_start
, s
->map_size
);
111 /*======================================================================
113 Low-level functions to read and write CIS memory. I think the
114 write routine is only useful for writing one-byte registers.
116 ======================================================================*/
118 /* Bits in attr field */
120 #define IS_INDIRECT 8
122 int read_cis_mem(struct pcmcia_socket
*s
, int attr
, u_int addr
,
123 u_int len
, void *ptr
)
125 void __iomem
*sys
, *end
;
126 unsigned char *buf
= ptr
;
128 cs_dbg(s
, 3, "read_cis_mem(%d, %#x, %u)\n", attr
, addr
, len
);
130 if (attr
& IS_INDIRECT
) {
131 /* Indirect accesses use a bunch of special registers at fixed
132 locations in common memory */
133 u_char flags
= ICTRL0_COMMON
|ICTRL0_AUTOINC
|ICTRL0_BYTEGRAN
;
134 if (attr
& IS_ATTR
) {
136 flags
= ICTRL0_AUTOINC
;
139 sys
= set_cis_map(s
, 0, MAP_ACTIVE
| ((cis_width
) ? MAP_16BIT
: 0));
141 memset(ptr
, 0xff, len
);
145 writeb(flags
, sys
+CISREG_ICTRL0
);
146 writeb(addr
& 0xff, sys
+CISREG_IADDR0
);
147 writeb((addr
>>8) & 0xff, sys
+CISREG_IADDR1
);
148 writeb((addr
>>16) & 0xff, sys
+CISREG_IADDR2
);
149 writeb((addr
>>24) & 0xff, sys
+CISREG_IADDR3
);
150 for ( ; len
> 0; len
--, buf
++)
151 *buf
= readb(sys
+CISREG_IDATA0
);
153 u_int inc
= 1, card_offset
, flags
;
155 flags
= MAP_ACTIVE
| ((cis_width
) ? MAP_16BIT
: 0);
162 card_offset
= addr
& ~(s
->map_size
-1);
164 sys
= set_cis_map(s
, card_offset
, flags
);
166 memset(ptr
, 0xff, len
);
169 end
= sys
+ s
->map_size
;
170 sys
= sys
+ (addr
& (s
->map_size
-1));
171 for ( ; len
> 0; len
--, buf
++, sys
+= inc
) {
176 card_offset
+= s
->map_size
;
180 cs_dbg(s
, 3, " %#2.2x %#2.2x %#2.2x %#2.2x ...\n",
181 *(u_char
*)(ptr
+0), *(u_char
*)(ptr
+1),
182 *(u_char
*)(ptr
+2), *(u_char
*)(ptr
+3));
186 void write_cis_mem(struct pcmcia_socket
*s
, int attr
, u_int addr
,
187 u_int len
, void *ptr
)
189 void __iomem
*sys
, *end
;
190 unsigned char *buf
= ptr
;
192 cs_dbg(s
, 3, "write_cis_mem(%d, %#x, %u)\n", attr
, addr
, len
);
194 if (attr
& IS_INDIRECT
) {
195 /* Indirect accesses use a bunch of special registers at fixed
196 locations in common memory */
197 u_char flags
= ICTRL0_COMMON
|ICTRL0_AUTOINC
|ICTRL0_BYTEGRAN
;
198 if (attr
& IS_ATTR
) {
200 flags
= ICTRL0_AUTOINC
;
203 sys
= set_cis_map(s
, 0, MAP_ACTIVE
| ((cis_width
) ? MAP_16BIT
: 0));
205 return; /* FIXME: Error */
207 writeb(flags
, sys
+CISREG_ICTRL0
);
208 writeb(addr
& 0xff, sys
+CISREG_IADDR0
);
209 writeb((addr
>>8) & 0xff, sys
+CISREG_IADDR1
);
210 writeb((addr
>>16) & 0xff, sys
+CISREG_IADDR2
);
211 writeb((addr
>>24) & 0xff, sys
+CISREG_IADDR3
);
212 for ( ; len
> 0; len
--, buf
++)
213 writeb(*buf
, sys
+CISREG_IDATA0
);
215 u_int inc
= 1, card_offset
, flags
;
217 flags
= MAP_ACTIVE
| ((cis_width
) ? MAP_16BIT
: 0);
218 if (attr
& IS_ATTR
) {
224 card_offset
= addr
& ~(s
->map_size
-1);
226 sys
= set_cis_map(s
, card_offset
, flags
);
228 return; /* FIXME: error */
230 end
= sys
+ s
->map_size
;
231 sys
= sys
+ (addr
& (s
->map_size
-1));
232 for ( ; len
> 0; len
--, buf
++, sys
+= inc
) {
237 card_offset
+= s
->map_size
;
243 /*======================================================================
245 This is a wrapper around read_cis_mem, with the same interface,
246 but which caches information, for cards whose CIS may not be
247 readable all the time.
249 ======================================================================*/
251 static void read_cis_cache(struct pcmcia_socket
*s
, int attr
, u_int addr
,
252 u_int len
, void *ptr
)
254 struct cis_cache_entry
*cis
;
258 if (s
->fake_cis_len
> addr
+len
)
259 memcpy(ptr
, s
->fake_cis
+addr
, len
);
261 memset(ptr
, 0xff, len
);
265 list_for_each_entry(cis
, &s
->cis_cache
, node
) {
266 if (cis
->addr
== addr
&& cis
->len
== len
&& cis
->attr
== attr
) {
267 memcpy(ptr
, cis
->cache
, len
);
272 #ifdef CONFIG_CARDBUS
273 if (s
->state
& SOCKET_CARDBUS
)
274 ret
= read_cb_mem(s
, attr
, addr
, len
, ptr
);
277 ret
= read_cis_mem(s
, attr
, addr
, len
, ptr
);
280 /* Copy data into the cache */
281 cis
= kmalloc(sizeof(struct cis_cache_entry
) + len
, GFP_KERNEL
);
286 memcpy(cis
->cache
, ptr
, len
);
287 list_add(&cis
->node
, &s
->cis_cache
);
293 remove_cis_cache(struct pcmcia_socket
*s
, int attr
, u_int addr
, u_int len
)
295 struct cis_cache_entry
*cis
;
297 list_for_each_entry(cis
, &s
->cis_cache
, node
)
298 if (cis
->addr
== addr
&& cis
->len
== len
&& cis
->attr
== attr
) {
299 list_del(&cis
->node
);
305 void destroy_cis_cache(struct pcmcia_socket
*s
)
307 struct list_head
*l
, *n
;
309 list_for_each_safe(l
, n
, &s
->cis_cache
) {
310 struct cis_cache_entry
*cis
= list_entry(l
, struct cis_cache_entry
, node
);
312 list_del(&cis
->node
);
317 * If there was a fake CIS, destroy that as well.
324 EXPORT_SYMBOL(destroy_cis_cache
);
326 /*======================================================================
328 This verifies if the CIS of a card matches what is in the CIS
331 ======================================================================*/
333 int verify_cis_cache(struct pcmcia_socket
*s
)
335 struct cis_cache_entry
*cis
;
338 buf
= kmalloc(256, GFP_KERNEL
);
341 list_for_each_entry(cis
, &s
->cis_cache
, node
) {
346 #ifdef CONFIG_CARDBUS
347 if (s
->state
& SOCKET_CARDBUS
)
348 read_cb_mem(s
, cis
->attr
, cis
->addr
, len
, buf
);
351 read_cis_mem(s
, cis
->attr
, cis
->addr
, len
, buf
);
353 if (memcmp(buf
, cis
->cache
, len
) != 0) {
362 /*======================================================================
364 For really bad cards, we provide a facility for uploading a
367 ======================================================================*/
369 int pcmcia_replace_cis(struct pcmcia_socket
*s
, cisdump_t
*cis
)
371 if (s
->fake_cis
!= NULL
) {
375 if (cis
->Length
> CISTPL_MAX_CIS_SIZE
)
377 s
->fake_cis
= kmalloc(cis
->Length
, GFP_KERNEL
);
378 if (s
->fake_cis
== NULL
)
379 return CS_OUT_OF_RESOURCE
;
380 s
->fake_cis_len
= cis
->Length
;
381 memcpy(s
->fake_cis
, cis
->Data
, cis
->Length
);
385 /*======================================================================
387 The high-level CIS tuple services
389 ======================================================================*/
391 typedef struct tuple_flags
{
398 #define LINK_SPACE(f) (((tuple_flags *)(&(f)))->link_space)
399 #define HAS_LINK(f) (((tuple_flags *)(&(f)))->has_link)
400 #define MFC_FN(f) (((tuple_flags *)(&(f)))->mfc_fn)
401 #define SPACE(f) (((tuple_flags *)(&(f)))->space)
403 int pccard_get_next_tuple(struct pcmcia_socket
*s
, unsigned int func
, tuple_t
*tuple
);
405 int pccard_get_first_tuple(struct pcmcia_socket
*s
, unsigned int function
, tuple_t
*tuple
)
408 return CS_BAD_HANDLE
;
409 if (!(s
->state
& SOCKET_PRESENT
))
411 tuple
->TupleLink
= tuple
->Flags
= 0;
412 #ifdef CONFIG_CARDBUS
413 if (s
->state
& SOCKET_CARDBUS
) {
414 struct pci_dev
*dev
= s
->cb_dev
;
416 pci_bus_read_config_dword(dev
->subordinate
, 0, PCI_CARDBUS_CIS
, &ptr
);
417 tuple
->CISOffset
= ptr
& ~7;
418 SPACE(tuple
->Flags
) = (ptr
& 7);
422 /* Assume presence of a LONGLINK_C to address 0 */
423 tuple
->CISOffset
= tuple
->LinkOffset
= 0;
424 SPACE(tuple
->Flags
) = HAS_LINK(tuple
->Flags
) = 1;
426 if (!(s
->state
& SOCKET_CARDBUS
) && (s
->functions
> 1) &&
427 !(tuple
->Attributes
& TUPLE_RETURN_COMMON
)) {
428 cisdata_t req
= tuple
->DesiredTuple
;
429 tuple
->DesiredTuple
= CISTPL_LONGLINK_MFC
;
430 if (pccard_get_next_tuple(s
, function
, tuple
) == CS_SUCCESS
) {
431 tuple
->DesiredTuple
= CISTPL_LINKTARGET
;
432 if (pccard_get_next_tuple(s
, function
, tuple
) != CS_SUCCESS
)
433 return CS_NO_MORE_ITEMS
;
435 tuple
->CISOffset
= tuple
->TupleLink
= 0;
436 tuple
->DesiredTuple
= req
;
438 return pccard_get_next_tuple(s
, function
, tuple
);
440 EXPORT_SYMBOL(pccard_get_first_tuple
);
442 static int follow_link(struct pcmcia_socket
*s
, tuple_t
*tuple
)
447 if (MFC_FN(tuple
->Flags
)) {
448 /* Get indirect link from the MFC tuple */
449 read_cis_cache(s
, LINK_SPACE(tuple
->Flags
),
450 tuple
->LinkOffset
, 5, link
);
451 ofs
= le32_to_cpu(*(u_int
*)(link
+1));
452 SPACE(tuple
->Flags
) = (link
[0] == CISTPL_MFC_ATTR
);
453 /* Move to the next indirect link */
454 tuple
->LinkOffset
+= 5;
455 MFC_FN(tuple
->Flags
)--;
456 } else if (HAS_LINK(tuple
->Flags
)) {
457 ofs
= tuple
->LinkOffset
;
458 SPACE(tuple
->Flags
) = LINK_SPACE(tuple
->Flags
);
459 HAS_LINK(tuple
->Flags
) = 0;
463 if (!(s
->state
& SOCKET_CARDBUS
) && SPACE(tuple
->Flags
)) {
464 /* This is ugly, but a common CIS error is to code the long
465 link offset incorrectly, so we check the right spot... */
466 read_cis_cache(s
, SPACE(tuple
->Flags
), ofs
, 5, link
);
467 if ((link
[0] == CISTPL_LINKTARGET
) && (link
[1] >= 3) &&
468 (strncmp(link
+2, "CIS", 3) == 0))
470 remove_cis_cache(s
, SPACE(tuple
->Flags
), ofs
, 5);
471 /* Then, we try the wrong spot... */
474 read_cis_cache(s
, SPACE(tuple
->Flags
), ofs
, 5, link
);
475 if ((link
[0] == CISTPL_LINKTARGET
) && (link
[1] >= 3) &&
476 (strncmp(link
+2, "CIS", 3) == 0))
478 remove_cis_cache(s
, SPACE(tuple
->Flags
), ofs
, 5);
482 int pccard_get_next_tuple(struct pcmcia_socket
*s
, unsigned int function
, tuple_t
*tuple
)
488 return CS_BAD_HANDLE
;
489 if (!(s
->state
& SOCKET_PRESENT
))
492 link
[1] = tuple
->TupleLink
;
493 ofs
= tuple
->CISOffset
+ tuple
->TupleLink
;
494 attr
= SPACE(tuple
->Flags
);
496 for (i
= 0; i
< MAX_TUPLES
; i
++) {
497 if (link
[1] == 0xff) {
498 link
[0] = CISTPL_END
;
500 read_cis_cache(s
, attr
, ofs
, 2, link
);
501 if (link
[0] == CISTPL_NULL
) {
506 /* End of chain? Follow long link if possible */
507 if (link
[0] == CISTPL_END
) {
508 if ((ofs
= follow_link(s
, tuple
)) < 0)
509 return CS_NO_MORE_ITEMS
;
510 attr
= SPACE(tuple
->Flags
);
511 read_cis_cache(s
, attr
, ofs
, 2, link
);
514 /* Is this a link tuple? Make a note of it */
515 if ((link
[0] == CISTPL_LONGLINK_A
) ||
516 (link
[0] == CISTPL_LONGLINK_C
) ||
517 (link
[0] == CISTPL_LONGLINK_MFC
) ||
518 (link
[0] == CISTPL_LINKTARGET
) ||
519 (link
[0] == CISTPL_INDIRECT
) ||
520 (link
[0] == CISTPL_NO_LINK
)) {
522 case CISTPL_LONGLINK_A
:
523 HAS_LINK(tuple
->Flags
) = 1;
524 LINK_SPACE(tuple
->Flags
) = attr
| IS_ATTR
;
525 read_cis_cache(s
, attr
, ofs
+2, 4, &tuple
->LinkOffset
);
527 case CISTPL_LONGLINK_C
:
528 HAS_LINK(tuple
->Flags
) = 1;
529 LINK_SPACE(tuple
->Flags
) = attr
& ~IS_ATTR
;
530 read_cis_cache(s
, attr
, ofs
+2, 4, &tuple
->LinkOffset
);
532 case CISTPL_INDIRECT
:
533 HAS_LINK(tuple
->Flags
) = 1;
534 LINK_SPACE(tuple
->Flags
) = IS_ATTR
| IS_INDIRECT
;
535 tuple
->LinkOffset
= 0;
537 case CISTPL_LONGLINK_MFC
:
538 tuple
->LinkOffset
= ofs
+ 3;
539 LINK_SPACE(tuple
->Flags
) = attr
;
540 if (function
== BIND_FN_ALL
) {
541 /* Follow all the MFC links */
542 read_cis_cache(s
, attr
, ofs
+2, 1, &tmp
);
543 MFC_FN(tuple
->Flags
) = tmp
;
545 /* Follow exactly one of the links */
546 MFC_FN(tuple
->Flags
) = 1;
547 tuple
->LinkOffset
+= function
* 5;
551 HAS_LINK(tuple
->Flags
) = 0;
554 if ((tuple
->Attributes
& TUPLE_RETURN_LINK
) &&
555 (tuple
->DesiredTuple
== RETURN_FIRST_TUPLE
))
558 if (tuple
->DesiredTuple
== RETURN_FIRST_TUPLE
)
561 if (link
[0] == tuple
->DesiredTuple
)
565 if (i
== MAX_TUPLES
) {
566 cs_dbg(s
, 1, "cs: overrun in pcmcia_get_next_tuple\n");
567 return CS_NO_MORE_ITEMS
;
570 tuple
->TupleCode
= link
[0];
571 tuple
->TupleLink
= link
[1];
572 tuple
->CISOffset
= ofs
+ 2;
575 EXPORT_SYMBOL(pccard_get_next_tuple
);
577 /*====================================================================*/
579 #define _MIN(a, b) (((a) < (b)) ? (a) : (b))
581 int pccard_get_tuple_data(struct pcmcia_socket
*s
, tuple_t
*tuple
)
586 return CS_BAD_HANDLE
;
588 if (tuple
->TupleLink
< tuple
->TupleOffset
)
589 return CS_NO_MORE_ITEMS
;
590 len
= tuple
->TupleLink
- tuple
->TupleOffset
;
591 tuple
->TupleDataLen
= tuple
->TupleLink
;
594 read_cis_cache(s
, SPACE(tuple
->Flags
),
595 tuple
->CISOffset
+ tuple
->TupleOffset
,
596 _MIN(len
, tuple
->TupleDataMax
), tuple
->TupleData
);
599 EXPORT_SYMBOL(pccard_get_tuple_data
);
602 /*======================================================================
604 Parsing routines for individual tuples
606 ======================================================================*/
608 static int parse_device(tuple_t
*tuple
, cistpl_device_t
*device
)
614 p
= (u_char
*)tuple
->TupleData
;
615 q
= p
+ tuple
->TupleDataLen
;
618 for (i
= 0; i
< CISTPL_MAX_DEVICES
; i
++) {
620 if (*p
== 0xff) break;
621 device
->dev
[i
].type
= (*p
>> 4);
622 device
->dev
[i
].wp
= (*p
& 0x08) ? 1 : 0;
624 case 0: device
->dev
[i
].speed
= 0; break;
625 case 1: device
->dev
[i
].speed
= 250; break;
626 case 2: device
->dev
[i
].speed
= 200; break;
627 case 3: device
->dev
[i
].speed
= 150; break;
628 case 4: device
->dev
[i
].speed
= 100; break;
630 if (++p
== q
) return CS_BAD_TUPLE
;
631 device
->dev
[i
].speed
= SPEED_CVT(*p
);
633 if (++p
== q
) return CS_BAD_TUPLE
;
639 if (++p
== q
) return CS_BAD_TUPLE
;
640 if (*p
== 0xff) break;
642 if (scale
== 7) return CS_BAD_TUPLE
;
643 device
->dev
[i
].size
= ((*p
>> 3) + 1) * (512 << (scale
*2));
651 /*====================================================================*/
653 static int parse_checksum(tuple_t
*tuple
, cistpl_checksum_t
*csum
)
656 if (tuple
->TupleDataLen
< 5)
658 p
= (u_char
*)tuple
->TupleData
;
659 csum
->addr
= tuple
->CISOffset
+(short)le16_to_cpu(*(u_short
*)p
)-2;
660 csum
->len
= le16_to_cpu(*(u_short
*)(p
+ 2));
665 /*====================================================================*/
667 static int parse_longlink(tuple_t
*tuple
, cistpl_longlink_t
*link
)
669 if (tuple
->TupleDataLen
< 4)
671 link
->addr
= le32_to_cpu(*(u_int
*)tuple
->TupleData
);
675 /*====================================================================*/
677 static int parse_longlink_mfc(tuple_t
*tuple
,
678 cistpl_longlink_mfc_t
*link
)
683 p
= (u_char
*)tuple
->TupleData
;
686 if (tuple
->TupleDataLen
<= link
->nfn
*5)
688 for (i
= 0; i
< link
->nfn
; i
++) {
689 link
->fn
[i
].space
= *p
; p
++;
690 link
->fn
[i
].addr
= le32_to_cpu(*(u_int
*)p
); p
+= 4;
695 /*====================================================================*/
697 static int parse_strings(u_char
*p
, u_char
*q
, int max
,
698 char *s
, u_char
*ofs
, u_char
*found
)
702 if (p
== q
) return CS_BAD_TUPLE
;
704 for (i
= 0; i
< max
; i
++) {
705 if (*p
== 0xff) break;
709 s
[j
++] = (*p
== 0xff) ? '\0' : *p
;
710 if ((*p
== '\0') || (*p
== 0xff)) break;
711 if (++p
== q
) return CS_BAD_TUPLE
;
713 if ((*p
== 0xff) || (++p
== q
)) break;
719 return (ns
== max
) ? CS_SUCCESS
: CS_BAD_TUPLE
;
723 /*====================================================================*/
725 static int parse_vers_1(tuple_t
*tuple
, cistpl_vers_1_t
*vers_1
)
729 p
= (u_char
*)tuple
->TupleData
;
730 q
= p
+ tuple
->TupleDataLen
;
732 vers_1
->major
= *p
; p
++;
733 vers_1
->minor
= *p
; p
++;
734 if (p
>= q
) return CS_BAD_TUPLE
;
736 return parse_strings(p
, q
, CISTPL_VERS_1_MAX_PROD_STRINGS
,
737 vers_1
->str
, vers_1
->ofs
, &vers_1
->ns
);
740 /*====================================================================*/
742 static int parse_altstr(tuple_t
*tuple
, cistpl_altstr_t
*altstr
)
746 p
= (u_char
*)tuple
->TupleData
;
747 q
= p
+ tuple
->TupleDataLen
;
749 return parse_strings(p
, q
, CISTPL_MAX_ALTSTR_STRINGS
,
750 altstr
->str
, altstr
->ofs
, &altstr
->ns
);
753 /*====================================================================*/
755 static int parse_jedec(tuple_t
*tuple
, cistpl_jedec_t
*jedec
)
760 p
= (u_char
*)tuple
->TupleData
;
761 q
= p
+ tuple
->TupleDataLen
;
763 for (nid
= 0; nid
< CISTPL_MAX_DEVICES
; nid
++) {
765 jedec
->id
[nid
].mfr
= p
[0];
766 jedec
->id
[nid
].info
= p
[1];
773 /*====================================================================*/
775 static int parse_manfid(tuple_t
*tuple
, cistpl_manfid_t
*m
)
778 if (tuple
->TupleDataLen
< 4)
780 p
= (u_short
*)tuple
->TupleData
;
781 m
->manf
= le16_to_cpu(p
[0]);
782 m
->card
= le16_to_cpu(p
[1]);
786 /*====================================================================*/
788 static int parse_funcid(tuple_t
*tuple
, cistpl_funcid_t
*f
)
791 if (tuple
->TupleDataLen
< 2)
793 p
= (u_char
*)tuple
->TupleData
;
799 /*====================================================================*/
801 static int parse_funce(tuple_t
*tuple
, cistpl_funce_t
*f
)
805 if (tuple
->TupleDataLen
< 1)
807 p
= (u_char
*)tuple
->TupleData
;
809 for (i
= 1; i
< tuple
->TupleDataLen
; i
++)
814 /*====================================================================*/
816 static int parse_config(tuple_t
*tuple
, cistpl_config_t
*config
)
821 p
= (u_char
*)tuple
->TupleData
;
823 rmsz
= (*p
& 0x3c) >> 2;
824 if (tuple
->TupleDataLen
< rasz
+rmsz
+4)
826 config
->last_idx
= *(++p
);
829 for (i
= 0; i
<= rasz
; i
++)
830 config
->base
+= p
[i
] << (8*i
);
832 for (i
= 0; i
< 4; i
++)
833 config
->rmask
[i
] = 0;
834 for (i
= 0; i
<= rmsz
; i
++)
835 config
->rmask
[i
>>2] += p
[i
] << (8*(i
%4));
836 config
->subtuples
= tuple
->TupleDataLen
- (rasz
+rmsz
+4);
840 /*======================================================================
842 The following routines are all used to parse the nightmarish
843 config table entries.
845 ======================================================================*/
847 static u_char
*parse_power(u_char
*p
, u_char
*q
,
853 if (p
== q
) return NULL
;
857 for (i
= 0; i
< 7; i
++)
858 if (pwr
->present
& (1<<i
)) {
859 if (p
== q
) return NULL
;
860 pwr
->param
[i
] = POWER_CVT(*p
);
861 scale
= POWER_SCALE(*p
);
863 if (++p
== q
) return NULL
;
864 if ((*p
& 0x7f) < 100)
865 pwr
->param
[i
] += (*p
& 0x7f) * scale
/ 100;
867 pwr
->flags
|= CISTPL_POWER_HIGHZ_OK
;
871 pwr
->flags
|= CISTPL_POWER_HIGHZ_REQ
;
880 /*====================================================================*/
882 static u_char
*parse_timing(u_char
*p
, u_char
*q
,
883 cistpl_timing_t
*timing
)
887 if (p
== q
) return NULL
;
889 if ((scale
& 3) != 3) {
890 if (++p
== q
) return NULL
;
891 timing
->wait
= SPEED_CVT(*p
);
892 timing
->waitscale
= exponent
[scale
& 3];
896 if ((scale
& 7) != 7) {
897 if (++p
== q
) return NULL
;
898 timing
->ready
= SPEED_CVT(*p
);
899 timing
->rdyscale
= exponent
[scale
& 7];
904 if (++p
== q
) return NULL
;
905 timing
->reserved
= SPEED_CVT(*p
);
906 timing
->rsvscale
= exponent
[scale
];
908 timing
->reserved
= 0;
913 /*====================================================================*/
915 static u_char
*parse_io(u_char
*p
, u_char
*q
, cistpl_io_t
*io
)
919 if (p
== q
) return NULL
;
925 io
->win
[0].len
= (1 << (io
->flags
& CISTPL_IO_LINES_MASK
));
929 if (++p
== q
) return NULL
;
930 io
->nwin
= (*p
& 0x0f) + 1;
931 bsz
= (*p
& 0x30) >> 4;
933 lsz
= (*p
& 0xc0) >> 6;
937 for (i
= 0; i
< io
->nwin
; i
++) {
940 for (j
= 0; j
< bsz
; j
++, p
++) {
941 if (p
== q
) return NULL
;
942 io
->win
[i
].base
+= *p
<< (j
*8);
944 for (j
= 0; j
< lsz
; j
++, p
++) {
945 if (p
== q
) return NULL
;
946 io
->win
[i
].len
+= *p
<< (j
*8);
952 /*====================================================================*/
954 static u_char
*parse_mem(u_char
*p
, u_char
*q
, cistpl_mem_t
*mem
)
956 int i
, j
, asz
, lsz
, has_ha
;
959 if (p
== q
) return NULL
;
961 mem
->nwin
= (*p
& 0x07) + 1;
962 lsz
= (*p
& 0x18) >> 3;
963 asz
= (*p
& 0x60) >> 5;
964 has_ha
= (*p
& 0x80);
965 if (++p
== q
) return NULL
;
967 for (i
= 0; i
< mem
->nwin
; i
++) {
969 for (j
= 0; j
< lsz
; j
++, p
++) {
970 if (p
== q
) return NULL
;
973 for (j
= 0; j
< asz
; j
++, p
++) {
974 if (p
== q
) return NULL
;
978 for (j
= 0; j
< asz
; j
++, p
++) {
979 if (p
== q
) return NULL
;
982 mem
->win
[i
].len
= len
<< 8;
983 mem
->win
[i
].card_addr
= ca
<< 8;
984 mem
->win
[i
].host_addr
= ha
<< 8;
989 /*====================================================================*/
991 static u_char
*parse_irq(u_char
*p
, u_char
*q
, cistpl_irq_t
*irq
)
993 if (p
== q
) return NULL
;
994 irq
->IRQInfo1
= *p
; p
++;
995 if (irq
->IRQInfo1
& IRQ_INFO2_VALID
) {
996 if (p
+2 > q
) return NULL
;
997 irq
->IRQInfo2
= (p
[1]<<8) + p
[0];
1003 /*====================================================================*/
1005 static int parse_cftable_entry(tuple_t
*tuple
,
1006 cistpl_cftable_entry_t
*entry
)
1008 u_char
*p
, *q
, features
;
1010 p
= tuple
->TupleData
;
1011 q
= p
+ tuple
->TupleDataLen
;
1012 entry
->index
= *p
& 0x3f;
1015 entry
->flags
|= CISTPL_CFTABLE_DEFAULT
;
1017 if (++p
== q
) return CS_BAD_TUPLE
;
1019 entry
->flags
|= CISTPL_CFTABLE_BVDS
;
1021 entry
->flags
|= CISTPL_CFTABLE_WP
;
1023 entry
->flags
|= CISTPL_CFTABLE_RDYBSY
;
1025 entry
->flags
|= CISTPL_CFTABLE_MWAIT
;
1026 entry
->interface
= *p
& 0x0f;
1028 entry
->interface
= 0;
1030 /* Process optional features */
1031 if (++p
== q
) return CS_BAD_TUPLE
;
1035 if ((features
& 3) > 0) {
1036 p
= parse_power(p
, q
, &entry
->vcc
);
1037 if (p
== NULL
) return CS_BAD_TUPLE
;
1039 entry
->vcc
.present
= 0;
1040 if ((features
& 3) > 1) {
1041 p
= parse_power(p
, q
, &entry
->vpp1
);
1042 if (p
== NULL
) return CS_BAD_TUPLE
;
1044 entry
->vpp1
.present
= 0;
1045 if ((features
& 3) > 2) {
1046 p
= parse_power(p
, q
, &entry
->vpp2
);
1047 if (p
== NULL
) return CS_BAD_TUPLE
;
1049 entry
->vpp2
.present
= 0;
1051 /* Timing options */
1052 if (features
& 0x04) {
1053 p
= parse_timing(p
, q
, &entry
->timing
);
1054 if (p
== NULL
) return CS_BAD_TUPLE
;
1056 entry
->timing
.wait
= 0;
1057 entry
->timing
.ready
= 0;
1058 entry
->timing
.reserved
= 0;
1061 /* I/O window options */
1062 if (features
& 0x08) {
1063 p
= parse_io(p
, q
, &entry
->io
);
1064 if (p
== NULL
) return CS_BAD_TUPLE
;
1068 /* Interrupt options */
1069 if (features
& 0x10) {
1070 p
= parse_irq(p
, q
, &entry
->irq
);
1071 if (p
== NULL
) return CS_BAD_TUPLE
;
1073 entry
->irq
.IRQInfo1
= 0;
1075 switch (features
& 0x60) {
1077 entry
->mem
.nwin
= 0;
1080 entry
->mem
.nwin
= 1;
1081 entry
->mem
.win
[0].len
= le16_to_cpu(*(u_short
*)p
) << 8;
1082 entry
->mem
.win
[0].card_addr
= 0;
1083 entry
->mem
.win
[0].host_addr
= 0;
1085 if (p
> q
) return CS_BAD_TUPLE
;
1088 entry
->mem
.nwin
= 1;
1089 entry
->mem
.win
[0].len
= le16_to_cpu(*(u_short
*)p
) << 8;
1090 entry
->mem
.win
[0].card_addr
=
1091 le16_to_cpu(*(u_short
*)(p
+2)) << 8;
1092 entry
->mem
.win
[0].host_addr
= 0;
1094 if (p
> q
) return CS_BAD_TUPLE
;
1097 p
= parse_mem(p
, q
, &entry
->mem
);
1098 if (p
== NULL
) return CS_BAD_TUPLE
;
1103 if (features
& 0x80) {
1104 if (p
== q
) return CS_BAD_TUPLE
;
1105 entry
->flags
|= (*p
<< 8);
1107 if (++p
== q
) return CS_BAD_TUPLE
;
1111 entry
->subtuples
= q
-p
;
1116 /*====================================================================*/
1118 #ifdef CONFIG_CARDBUS
1120 static int parse_bar(tuple_t
*tuple
, cistpl_bar_t
*bar
)
1123 if (tuple
->TupleDataLen
< 6)
1124 return CS_BAD_TUPLE
;
1125 p
= (u_char
*)tuple
->TupleData
;
1128 bar
->size
= le32_to_cpu(*(u_int
*)p
);
1132 static int parse_config_cb(tuple_t
*tuple
, cistpl_config_t
*config
)
1136 p
= (u_char
*)tuple
->TupleData
;
1137 if ((*p
!= 3) || (tuple
->TupleDataLen
< 6))
1138 return CS_BAD_TUPLE
;
1139 config
->last_idx
= *(++p
);
1141 config
->base
= le32_to_cpu(*(u_int
*)p
);
1142 config
->subtuples
= tuple
->TupleDataLen
- 6;
1146 static int parse_cftable_entry_cb(tuple_t
*tuple
,
1147 cistpl_cftable_entry_cb_t
*entry
)
1149 u_char
*p
, *q
, features
;
1151 p
= tuple
->TupleData
;
1152 q
= p
+ tuple
->TupleDataLen
;
1153 entry
->index
= *p
& 0x3f;
1156 entry
->flags
|= CISTPL_CFTABLE_DEFAULT
;
1158 /* Process optional features */
1159 if (++p
== q
) return CS_BAD_TUPLE
;
1163 if ((features
& 3) > 0) {
1164 p
= parse_power(p
, q
, &entry
->vcc
);
1165 if (p
== NULL
) return CS_BAD_TUPLE
;
1167 entry
->vcc
.present
= 0;
1168 if ((features
& 3) > 1) {
1169 p
= parse_power(p
, q
, &entry
->vpp1
);
1170 if (p
== NULL
) return CS_BAD_TUPLE
;
1172 entry
->vpp1
.present
= 0;
1173 if ((features
& 3) > 2) {
1174 p
= parse_power(p
, q
, &entry
->vpp2
);
1175 if (p
== NULL
) return CS_BAD_TUPLE
;
1177 entry
->vpp2
.present
= 0;
1179 /* I/O window options */
1180 if (features
& 0x08) {
1181 if (p
== q
) return CS_BAD_TUPLE
;
1182 entry
->io
= *p
; p
++;
1186 /* Interrupt options */
1187 if (features
& 0x10) {
1188 p
= parse_irq(p
, q
, &entry
->irq
);
1189 if (p
== NULL
) return CS_BAD_TUPLE
;
1191 entry
->irq
.IRQInfo1
= 0;
1193 if (features
& 0x20) {
1194 if (p
== q
) return CS_BAD_TUPLE
;
1195 entry
->mem
= *p
; p
++;
1200 if (features
& 0x80) {
1201 if (p
== q
) return CS_BAD_TUPLE
;
1202 entry
->flags
|= (*p
<< 8);
1204 if (++p
== q
) return CS_BAD_TUPLE
;
1205 entry
->flags
|= (*p
<< 16);
1208 if (++p
== q
) return CS_BAD_TUPLE
;
1212 entry
->subtuples
= q
-p
;
1219 /*====================================================================*/
1221 static int parse_device_geo(tuple_t
*tuple
, cistpl_device_geo_t
*geo
)
1226 p
= (u_char
*)tuple
->TupleData
;
1227 q
= p
+ tuple
->TupleDataLen
;
1229 for (n
= 0; n
< CISTPL_MAX_DEVICES
; n
++) {
1231 geo
->geo
[n
].buswidth
= p
[0];
1232 geo
->geo
[n
].erase_block
= 1 << (p
[1]-1);
1233 geo
->geo
[n
].read_block
= 1 << (p
[2]-1);
1234 geo
->geo
[n
].write_block
= 1 << (p
[3]-1);
1235 geo
->geo
[n
].partition
= 1 << (p
[4]-1);
1236 geo
->geo
[n
].interleave
= 1 << (p
[5]-1);
1243 /*====================================================================*/
1245 static int parse_vers_2(tuple_t
*tuple
, cistpl_vers_2_t
*v2
)
1249 if (tuple
->TupleDataLen
< 10)
1250 return CS_BAD_TUPLE
;
1252 p
= tuple
->TupleData
;
1253 q
= p
+ tuple
->TupleDataLen
;
1257 v2
->dindex
= le16_to_cpu(*(u_short
*)(p
+2));
1262 return parse_strings(p
, q
, 2, v2
->str
, &v2
->vendor
, NULL
);
1265 /*====================================================================*/
1267 static int parse_org(tuple_t
*tuple
, cistpl_org_t
*org
)
1272 p
= tuple
->TupleData
;
1273 q
= p
+ tuple
->TupleDataLen
;
1274 if (p
== q
) return CS_BAD_TUPLE
;
1276 if (++p
== q
) return CS_BAD_TUPLE
;
1277 for (i
= 0; i
< 30; i
++) {
1279 if (*p
== '\0') break;
1280 if (++p
== q
) return CS_BAD_TUPLE
;
1285 /*====================================================================*/
1287 static int parse_format(tuple_t
*tuple
, cistpl_format_t
*fmt
)
1291 if (tuple
->TupleDataLen
< 10)
1292 return CS_BAD_TUPLE
;
1294 p
= tuple
->TupleData
;
1298 fmt
->offset
= le32_to_cpu(*(u_int
*)(p
+2));
1299 fmt
->length
= le32_to_cpu(*(u_int
*)(p
+6));
1304 /*====================================================================*/
1306 int pccard_parse_tuple(tuple_t
*tuple
, cisparse_t
*parse
)
1308 int ret
= CS_SUCCESS
;
1310 if (tuple
->TupleDataLen
> tuple
->TupleDataMax
)
1311 return CS_BAD_TUPLE
;
1312 switch (tuple
->TupleCode
) {
1314 case CISTPL_DEVICE_A
:
1315 ret
= parse_device(tuple
, &parse
->device
);
1317 #ifdef CONFIG_CARDBUS
1319 ret
= parse_bar(tuple
, &parse
->bar
);
1321 case CISTPL_CONFIG_CB
:
1322 ret
= parse_config_cb(tuple
, &parse
->config
);
1324 case CISTPL_CFTABLE_ENTRY_CB
:
1325 ret
= parse_cftable_entry_cb(tuple
, &parse
->cftable_entry_cb
);
1328 case CISTPL_CHECKSUM
:
1329 ret
= parse_checksum(tuple
, &parse
->checksum
);
1331 case CISTPL_LONGLINK_A
:
1332 case CISTPL_LONGLINK_C
:
1333 ret
= parse_longlink(tuple
, &parse
->longlink
);
1335 case CISTPL_LONGLINK_MFC
:
1336 ret
= parse_longlink_mfc(tuple
, &parse
->longlink_mfc
);
1339 ret
= parse_vers_1(tuple
, &parse
->version_1
);
1342 ret
= parse_altstr(tuple
, &parse
->altstr
);
1344 case CISTPL_JEDEC_A
:
1345 case CISTPL_JEDEC_C
:
1346 ret
= parse_jedec(tuple
, &parse
->jedec
);
1349 ret
= parse_manfid(tuple
, &parse
->manfid
);
1352 ret
= parse_funcid(tuple
, &parse
->funcid
);
1355 ret
= parse_funce(tuple
, &parse
->funce
);
1358 ret
= parse_config(tuple
, &parse
->config
);
1360 case CISTPL_CFTABLE_ENTRY
:
1361 ret
= parse_cftable_entry(tuple
, &parse
->cftable_entry
);
1363 case CISTPL_DEVICE_GEO
:
1364 case CISTPL_DEVICE_GEO_A
:
1365 ret
= parse_device_geo(tuple
, &parse
->device_geo
);
1368 ret
= parse_vers_2(tuple
, &parse
->vers_2
);
1371 ret
= parse_org(tuple
, &parse
->org
);
1374 case CISTPL_FORMAT_A
:
1375 ret
= parse_format(tuple
, &parse
->format
);
1377 case CISTPL_NO_LINK
:
1378 case CISTPL_LINKTARGET
:
1382 ret
= CS_UNSUPPORTED_FUNCTION
;
1387 EXPORT_SYMBOL(pccard_parse_tuple
);
1389 /*======================================================================
1391 This is used internally by Card Services to look up CIS stuff.
1393 ======================================================================*/
1395 int pccard_read_tuple(struct pcmcia_socket
*s
, unsigned int function
, cisdata_t code
, void *parse
)
1401 buf
= kmalloc(256, GFP_KERNEL
);
1403 return CS_OUT_OF_RESOURCE
;
1404 tuple
.DesiredTuple
= code
;
1405 tuple
.Attributes
= TUPLE_RETURN_COMMON
;
1406 ret
= pccard_get_first_tuple(s
, function
, &tuple
);
1407 if (ret
!= CS_SUCCESS
) goto done
;
1408 tuple
.TupleData
= buf
;
1409 tuple
.TupleOffset
= 0;
1410 tuple
.TupleDataMax
= 255;
1411 ret
= pccard_get_tuple_data(s
, &tuple
);
1412 if (ret
!= CS_SUCCESS
) goto done
;
1413 ret
= pccard_parse_tuple(&tuple
, parse
);
1418 EXPORT_SYMBOL(pccard_read_tuple
);
1420 /*======================================================================
1422 This tries to determine if a card has a sensible CIS. It returns
1423 the number of tuples in the CIS, or 0 if the CIS looks bad. The
1424 checks include making sure several critical tuples are present and
1425 valid; seeing if the total number of tuples is reasonable; and
1426 looking for tuples that use reserved codes.
1428 ======================================================================*/
1430 int pccard_validate_cis(struct pcmcia_socket
*s
, unsigned int function
, cisinfo_t
*info
)
1434 int ret
, reserved
, dev_ok
= 0, ident_ok
= 0;
1437 return CS_BAD_HANDLE
;
1439 tuple
= kmalloc(sizeof(*tuple
), GFP_KERNEL
);
1441 return CS_OUT_OF_RESOURCE
;
1442 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
1445 return CS_OUT_OF_RESOURCE
;
1448 info
->Chains
= reserved
= 0;
1449 tuple
->DesiredTuple
= RETURN_FIRST_TUPLE
;
1450 tuple
->Attributes
= TUPLE_RETURN_COMMON
;
1451 ret
= pccard_get_first_tuple(s
, function
, tuple
);
1452 if (ret
!= CS_SUCCESS
)
1455 /* First tuple should be DEVICE; we should really have either that
1456 or a CFTABLE_ENTRY of some sort */
1457 if ((tuple
->TupleCode
== CISTPL_DEVICE
) ||
1458 (pccard_read_tuple(s
, function
, CISTPL_CFTABLE_ENTRY
, p
) == CS_SUCCESS
) ||
1459 (pccard_read_tuple(s
, function
, CISTPL_CFTABLE_ENTRY_CB
, p
) == CS_SUCCESS
))
1462 /* All cards should have a MANFID tuple, and/or a VERS_1 or VERS_2
1463 tuple, for card identification. Certain old D-Link and Linksys
1464 cards have only a broken VERS_2 tuple; hence the bogus test. */
1465 if ((pccard_read_tuple(s
, function
, CISTPL_MANFID
, p
) == CS_SUCCESS
) ||
1466 (pccard_read_tuple(s
, function
, CISTPL_VERS_1
, p
) == CS_SUCCESS
) ||
1467 (pccard_read_tuple(s
, function
, CISTPL_VERS_2
, p
) != CS_NO_MORE_ITEMS
))
1470 if (!dev_ok
&& !ident_ok
)
1473 for (info
->Chains
= 1; info
->Chains
< MAX_TUPLES
; info
->Chains
++) {
1474 ret
= pccard_get_next_tuple(s
, function
, tuple
);
1475 if (ret
!= CS_SUCCESS
) break;
1476 if (((tuple
->TupleCode
> 0x23) && (tuple
->TupleCode
< 0x40)) ||
1477 ((tuple
->TupleCode
> 0x47) && (tuple
->TupleCode
< 0x80)) ||
1478 ((tuple
->TupleCode
> 0x90) && (tuple
->TupleCode
< 0xff)))
1481 if ((info
->Chains
== MAX_TUPLES
) || (reserved
> 5) ||
1482 ((!dev_ok
|| !ident_ok
) && (info
->Chains
> 10)))
1490 EXPORT_SYMBOL(pccard_validate_cis
);