2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
5 Desc: CopyTuple() function.
9 #include <proto/exec.h>
11 #include "card_intern.h"
13 #define TUPLEDEBUG(x) do { if (buffer == NULL) x; } while(0);
14 #define TUPLEDEBUG2(x) x
15 #define TUPLELOGGING 1
18 static void byte2ascii(UBYTE
**pp
, UBYTE ch
)
22 *p
++ = (ch
>> 4) > 9 ? (ch
>> 4) - 10 + 'A' : (ch
>> 4) + '0';
23 *p
++ = (ch
& 15) > 9 ? (ch
& 15) - 10 + 'A' : (ch
& 15) + '0';
29 static BOOL
getbyte(ULONG addr
, UBYTE
*out
)
34 if (addr
< GAYLE_RAM
|| (addr
>= GAYLE_RAM
+ GAYLE_RAMSIZE
&& addr
< GAYLE_ATTRIBUTE
) || addr
>= GAYLE_ATTRIBUTE
+ GAYLE_ATTRIBUTESIZE
) {
35 TUPLEDEBUG2(bug("getbyte from invalid address %p\n", addr
));
38 if (!pcmcia_havecard())
44 static BOOL
getbytes(ULONG addr
, UBYTE count
, UBYTE nextbyte
, UBYTE
*out
)
47 if (!getbyte(addr
, out
))
55 static BOOL
getlelong(ULONG addr
, UBYTE offset
, UBYTE nextbyte
, ULONG
*outp
)
61 if (!getbyte(addr
+ nextbyte
, &v
))
65 addr
+= offset
* nextbyte
;
66 if (!getbyte(addr
+ 0 * nextbyte
, &v
))
69 if (!getbyte(addr
+ 1 * nextbyte
, &v
))
72 if (!getbyte(addr
+ 2 * nextbyte
, &v
))
75 if (!getbyte(addr
+ 3 * nextbyte
, &v
))
82 AROS_LH4(ULONG
, CopyTuple
,
83 AROS_LHA(struct CardHandle
*, handle
, A1
),
84 AROS_LHA(UBYTE
*, buffer
, A0
),
85 AROS_LHA(ULONG
, tuplecode
, D1
),
86 AROS_LHA(ULONG
, size
, D0
),
87 struct CardResource
*, CardResource
, 12, Card
)
91 volatile struct GayleIO
*gio
= (struct GayleIO
*)GAYLE_BASE
;
93 UWORD nextbyte
, tuplecnt
;
98 CARDDEBUG(bug("CopyTuple(%p,%p,%08x,%d)\n", handle
, buffer
, tuplecode
, size
));
100 /* buffer == NULL: output all tuples to debug log */
109 oldconfig
= gio
->config
;
110 gio
->config
= GAYLE_CFG_720NS
;
112 tuplecnt
= tuplecode
>> 16;
114 nextjump
= GAYLE_RAM
;
115 addr
= GAYLE_ATTRIBUTE
;
124 if (!getbyte(addr
, &type
))
127 TUPLEDEBUG(bug("PCMCIA Tuple %02x @%p\n", type
, addr
));
129 /* First attribute memory tuple must be CISTPL_DEVICE */
130 if (first
&& type
!= CISTPL_DEVICE
)
137 TUPLEDEBUG(bug("CISTPL_NULL\n"));
141 TUPLEDEBUG(bug("CISTPL_END\n"));
144 nextjump
= 0xffffffff;
145 TUPLEDEBUG(bug("CISTPL_NO_LINK\n"));
147 case CISTPL_LONGLINK_A
:
148 if (!getlelong(addr
, 2, nextbyte
, &nextjump
))
150 TUPLEDEBUG(bug("CISTPL_LONGLINK_A %08x\n", nextjump
));
151 if (nextjump
>= GAYLE_ATTRIBUTESIZE
)
153 nextjump
+= GAYLE_ATTRIBUTE
;
155 case CISTPL_LONGLINK_C
:
156 if (!getlelong(addr
, 2, nextbyte
, &nextjump
))
158 TUPLEDEBUG(bug("CISTPL_LONGLINK_C %08x\n", nextjump
));
159 if (nextjump
>= GAYLE_RAMSIZE
)
161 nextjump
+= GAYLE_RAM
;
165 if (buffer
!= NULL
&& type
== (UBYTE
)tuplecode
) {
172 getbyte(addr
, &tuplesize
);
173 *buffer
++ = tuplesize
;
175 while (size
-- > 0 && tuplesize
-- > 0) {
186 if (!final
&& type
!= CISTPL_NULL
) {
187 if (!getbyte(addr
+ nextbyte
, &tuplesize
))
189 if (tuplesize
== 0xff)
194 if (buffer
== NULL
) {
195 UBYTE outbuf
[(256 + 2 + 1) * 3];
198 for (ts
= 0; ts
< tuplesize
+ 2; ts
++) {
199 getbyte(addr
+ ts
* nextbyte
, &v
);
208 TUPLEDEBUG(bug("Next link %08x\n", nextjump
));
209 if (nextjump
== 0xffffffff) {
215 nextbyte
= nextjump
< GAYLE_ATTRIBUTE
? 1 : 2;
216 nextjump
= 0xffffffff;
217 /* valid LINKTARGET? */
218 if (!getbytes(addr
, 5, nextbyte
, buf
))
220 if (buf
[0] != CISTPL_LINKTARGET
|| buf
[1] < 3 ||
221 buf
[2] != 'C' || buf
[3] != 'I' || buf
[4] != 'S') {
222 TUPLEDEBUG(bug("Invalid or missing linktarget\n"));
228 if (type
== CISTPL_NULL
) {
231 if (!getbyte(addr
+ nextbyte
, &v
))
233 addr
+= (2 + tuplesize
) * nextbyte
;
239 gio
->config
= oldconfig
;
243 TUPLEDEBUG(bug ("CopyTuple finished\n"));