fix more compiler warnings, based on a patch by Rainer Keller
[librfid] / src / rfid_proto_tcl.c
1 /* ISO 14443-4 (T=CL) implementation, PCD side.
2  *
3  * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
4  *
5  */
6
7 /*
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 
10  *  as published by the Free Software Foundation
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include <librfid/rfid.h>
29 #include <librfid/rfid_protocol_tcl.h>
30 #include <librfid/rfid_protocol.h>
31 #include <librfid/rfid_layer2.h>
32 #include <librfid/rfid_layer2_iso14443b.h>
33
34 #include <librfid/rfid_asic.h>
35 #include <librfid/rfid_reader.h>
36
37 #include "rfid_iso14443_common.h"
38
39 #define RFID_MAX_FRAMELEN       256
40
41 #define is_s_block(x) ((x & 0xc0) == 0xc0)
42 #define is_r_block(x) ((x & 0xc0) == 0x80)
43 #define is_i_block(x) ((x & 0xc0) == 0x00)
44
45 static enum rfid_frametype l2_to_frame(unsigned int layer2)
46 {
47         switch (layer2) {
48                 case RFID_LAYER2_ISO14443A:
49                         return RFID_14443A_FRAME_REGULAR;
50                         break;
51                 case RFID_LAYER2_ISO14443B:
52                         return RFID_14443B_FRAME_REGULAR;
53                         break;
54         }
55         return 0;
56 }
57
58 static unsigned int sfgi_to_sfgt(struct rfid_protocol_handle *h, 
59                                  unsigned char sfgi)
60 {
61         unsigned int multiplier;
62         unsigned int tmp;
63
64         if (sfgi > 14)
65                 sfgi = 14;
66
67         multiplier = 1 << sfgi; /* 2 to the power of sfgi */
68
69         /* ISO 14443-4:2000(E) Section 5.2.5:
70          * (256 * 16 / h->l2h->rh->ah->fc) * (2 ^ sfgi) */
71         tmp = (unsigned int) 1000000 * 256 * 16;
72
73         return (tmp / h->l2h->rh->ah->fc) * multiplier;
74 }
75
76 static unsigned int fwi_to_fwt(struct rfid_protocol_handle *h, 
77                                 unsigned char fwi)
78 {
79         unsigned int multiplier, tmp;
80
81         if (fwi > 14)
82                 fwi = 14;
83
84         multiplier  = 1 << fwi; /* 2 to the power of fwi */
85
86         /* ISO 14443-4:2000(E) Section 7.2.:
87          * (256*16 / h->l2h->rh->ah->fc) * (2 ^ fwi) */
88
89         tmp = (unsigned int) 1000000 * 256 * 16;
90
91         return (tmp / h->l2h->rh->ah->fc) * multiplier;
92 }
93
94 /* 4.9seconds as microseconds (4.9 billion seconds) exceeds 2^32 */
95 #define activation_fwt(x) (((u_int64_t)1000000 * 65536 / x->l2h->rh->ah->fc))
96 #define deactivation_fwt(x) activation_fwt(x)
97
98 static int
99 tcl_parse_ats(struct rfid_protocol_handle *h, 
100                 unsigned char *ats, unsigned int size)
101 {
102         unsigned char len = ats[0];
103         unsigned char t0;
104         unsigned char *cur;
105
106         if (len == 0 || size == 0) 
107                 return -1;
108
109         if (size < len)
110                 len = size;
111
112         h->priv.tcl.ta = 0;
113
114         if (len == 1) {
115                 /* FIXME: assume some default values */
116                 h->priv.tcl.fsc = 32;
117                 h->priv.tcl.ta = 0x80;  /* 0x80 (same d for both dirs) */
118                 h->priv.tcl.sfgt = sfgi_to_sfgt(h, 0);
119                 if (h->l2h->l2->id == RFID_LAYER2_ISO14443A) {
120                         /* Section 7.2: fwi default for type A is 4 */
121                         h->priv.tcl.fwt = fwi_to_fwt(h, 4);
122                 } else {
123                         /* Section 7.2: fwi for type B is always in ATQB */
124                         /* Value is assigned in tcl_connect() */
125                         /* This function is never called for Type B, 
126                          * since Type B has no (R)ATS */
127                 }
128                 return 0;
129         }
130
131         /* guarateed to be at least 2 bytes in size */
132
133         t0 = ats[1];
134         cur = &ats[2];
135
136         iso14443_fsdi_to_fsd(&h->priv.tcl.fsc, t0 & 0x0f);
137         if (h->priv.tcl.fsc > h->l2h->rh->ah->mtu)
138                 h->priv.tcl.fsc = h->l2h->rh->ah->mtu;
139
140         if (t0 & (1 << 4)) {
141                 /* TA is transmitted */
142                 h->priv.tcl.ta = *cur++;
143         }
144
145         if (t0 & (1 << 5)) {
146                 /* TB is transmitted */
147                 h->priv.tcl.sfgt = sfgi_to_sfgt(h, *cur & 0x0f);
148                 h->priv.tcl.fwt = fwi_to_fwt(h, (*cur & 0xf0) >> 4);
149                 cur++;
150         }
151
152         if (t0 & (1 << 6)) {
153                 /* TC is transmitted */
154                 if (*cur & 0x01) {
155                         h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
156                         DEBUGP("This PICC supports NAD\n");
157                 }
158                 if (*cur & 0x02) {
159                         h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
160                         DEBUGP("This PICC supports CID\n");
161                 }
162                 cur++;
163         }
164
165         h->priv.tcl.historical_len = (ats+len) - cur;
166         h->priv.tcl.historical_bytes = cur;
167         
168         DEBUGP("ATS parsed: %s\n", rfid_hexdump(ats, size));
169
170         return 0;
171 }
172
173
174 /* request an ATS from the PICC */
175 static int
176 tcl_request_ats(struct rfid_protocol_handle *h)
177 {
178         int ret;
179         unsigned char rats[2];
180         unsigned char fsdi;
181
182         if (h->priv.tcl.state != TCL_STATE_INITIAL)
183                 return -1;
184
185         ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.tcl.fsd);
186         if (ret < 0) {
187                 DEBUGP("unable to encode FSD of %u as FSDI\n", h->priv.tcl.fsd);
188                 return ret;
189         }
190
191         rats[0] = 0xe0;
192         rats[1] = (h->priv.tcl.cid & 0x0f) | ((fsdi << 4) & 0xf0);
193
194         /* transceive (with CRC) */
195         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
196                                      rats, 2, h->priv.tcl.ats,
197                                      &h->priv.tcl.ats_len, activation_fwt(h),
198                                      TCL_TRANSP_F_TX_CRC);
199         if (ret < 0) {
200                 DEBUGP("transceive of rats failed\n");
201                 h->priv.tcl.state = TCL_STATE_RATS_SENT;
202                 /* FIXME: retransmit */
203                 return ret;
204         }
205         h->priv.tcl.state = TCL_STATE_ATS_RCVD;
206
207         ret = tcl_parse_ats(h, h->priv.tcl.ats, h->priv.tcl.ats_len);
208         if (ret < 0) {
209                 DEBUGP("parsing of ats failed\n");
210                 return ret;
211         }
212
213         return 0;
214 }
215
216 #define ATS_TA_DIV_2    1
217 #define ATS_TA_DIV_4    2
218 #define ATS_TA_DIV_8    4
219
220 #define PPS_DIV_8       3
221 #define PPS_DIV_4       2
222 #define PPS_DIV_2       1
223 #define PPS_DIV_1       0
224 static unsigned char d_to_di(struct rfid_protocol_handle *h, unsigned char D)
225 {
226         static char DI;
227         unsigned int speed = h->l2h->rh->reader->iso14443a.speed;
228         
229         if ((D & ATS_TA_DIV_8) && (speed & RFID_14443A_SPEED_848K))
230                 DI = PPS_DIV_8;
231         else if ((D & ATS_TA_DIV_4) && (speed & RFID_14443A_SPEED_424K))
232                 DI = PPS_DIV_4;
233         else if ((D & ATS_TA_DIV_2) && (speed & RFID_14443A_SPEED_212K))
234                 DI = PPS_DIV_2;
235         else
236                 DI = PPS_DIV_1;
237
238         return DI;
239 }
240
241 static unsigned int di_to_speed(unsigned char DI)
242 {
243         switch (DI) {
244         case PPS_DIV_8:
245                 return RFID_14443A_SPEED_848K;
246                 break;
247         case PPS_DIV_4:
248                 return RFID_14443A_SPEED_424K;
249                 break;
250         case PPS_DIV_2:
251                 return RFID_14443A_SPEED_212K;
252                 break;
253         case PPS_DIV_1:
254                 return RFID_14443A_SPEED_106K;
255                 break;
256         }
257 }
258
259 /* start a PPS run (autimatically configure highest possible speed */
260 static int 
261 tcl_do_pps(struct rfid_protocol_handle *h)
262 {
263         int ret;
264         unsigned char ppss[3];
265         /* FIXME: this stinks like hell. IF we reduce pps_response size to one,
266            we'll get stack corruption! */
267         unsigned char pps_response[10];
268         unsigned int rx_len = 1;
269         unsigned char Dr, Ds, DrI, DsI;
270         unsigned int speed;
271
272         if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
273                 return -1;
274
275         Dr = h->priv.tcl.ta & 0x07;
276         Ds = h->priv.tcl.ta & 0x70 >> 4;
277         DEBUGP("Dr = 0x%x, Ds = 0x%x\n", Dr, Ds);
278
279         if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
280                 /* device supports different divisors for rx and tx, but not
281                  * really ?!? */
282                 DEBUGP("PICC has contradictory TA, aborting PPS\n");
283                 return -1;
284         };
285
286         /* ISO 14443-4:2000(E) Section 5.3. */
287
288         ppss[0] = 0xd0 | (h->priv.tcl.cid & 0x0f);
289         ppss[1] = 0x11;
290         ppss[2] = 0x00;
291
292         /* FIXME: deal with different speed for each direction */
293         DrI = d_to_di(h, Dr);
294         DsI = d_to_di(h, Ds);
295         DEBUGP("DrI = 0x%x, DsI = 0x%x\n", DrI, DsI);
296
297         ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
298
299         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
300                                         ppss, 3, pps_response, &rx_len,
301                                         h->priv.tcl.fwt, TCL_TRANSP_F_TX_CRC);
302         if (ret < 0)
303                 return ret;
304
305         if (pps_response[0] != ppss[0]) {
306                 DEBUGP("PPS Response != PPSS\n");
307                 return -1;
308         }
309
310         speed = di_to_speed(DrI);
311
312         ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_RX,
313                                  &speed, sizeof(speed));
314         if (ret < 0)
315                 return ret;
316
317         ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_TX,
318                                  &speed, sizeof(speed));
319         if (ret < 0)
320                 return ret;
321         
322         return 0;
323 }
324
325
326 static int
327 tcl_build_prologue2(struct tcl_handle *th, 
328                     unsigned char *prlg, unsigned int *prlg_len, 
329                     unsigned char pcb)
330 {
331         *prlg_len = 1;
332
333         *prlg = pcb;
334
335         if (!is_s_block(pcb)) {
336                 if (th->toggle) {
337                         /* we've sent a toggle bit last time */
338                         th->toggle = 0;
339                 } else {
340                         /* we've not sent a toggle last time: send one */
341                         th->toggle = 1;
342                         *prlg |= 0x01;
343                 }
344         }
345
346         if (th->flags & TCL_HANDLE_F_CID_USED) {
347                 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
348                 *prlg |= TCL_PCB_CID_FOLLOWING;
349                 (*prlg_len)++;
350                 prlg[*prlg_len] = th->cid & 0x0f;
351         }
352
353         /* nad only for I-block */
354         if ((th->flags & TCL_HANDLE_F_NAD_USED) && is_i_block(pcb)) {
355                 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
356                 /* FIXME: in case of chaining only for first frame */
357                 *prlg |= TCL_PCB_NAD_FOLLOWING;
358                 prlg[*prlg_len] = th->nad;
359                 (*prlg_len)++;
360         }
361
362         return 0;
363 }
364
365 static int
366 tcl_build_prologue_i(struct tcl_handle *th,
367                      unsigned char *prlg, unsigned int *prlg_len)
368 {
369         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
370         return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
371 }
372
373 static int
374 tcl_build_prologue_r(struct tcl_handle *th,
375                      unsigned char *prlg, unsigned int *prlg_len,
376                      unsigned int nak)
377 {
378         unsigned char pcb = 0xa2;
379         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
380
381         if (nak)
382                 pcb |= 0x10;
383
384         return tcl_build_prologue2(th, prlg, prlg_len, pcb);
385 }
386
387 static int
388 tcl_build_prologue_s(struct tcl_handle *th,
389                      unsigned char *prlg, unsigned int *prlg_len)
390 {
391         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
392
393         /* the only S-block from PCD->PICC is DESELECT,
394          * well, actually there is the S(WTX) response. */
395         return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
396 }
397
398 /* FIXME: WTXM implementation */
399
400 static int tcl_prlg_len(struct tcl_handle *th)
401 {
402         int prlg_len = 1;
403
404         if (th->flags & TCL_HANDLE_F_CID_USED)
405                 prlg_len++;
406
407         if (th->flags & TCL_HANDLE_F_NAD_USED)
408                 prlg_len++;
409
410         return prlg_len;
411 }
412
413 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
414
415 static int
416 tcl_connect(struct rfid_protocol_handle *h)
417 {
418         int ret; 
419
420         if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
421             h->priv.tcl.state != TCL_STATE_INITIAL)
422                 return -1;
423
424         switch (h->l2h->l2->id) {
425         case RFID_LAYER2_ISO14443A:
426                 /* Start Type A T=CL Activation Sequence */
427                 ret = tcl_request_ats(h);
428                 if (ret < 0)
429                         return ret;
430
431                 /* Only do PPS if any non-default divisors supported */
432                 if (h->priv.tcl.ta & 0x77) {
433                         ret = tcl_do_pps(h);
434                         if (ret < 0)
435                                 return ret;
436                 }
437                 break;
438         case RFID_LAYER2_ISO14443B:
439                 /* initialized T=CL state from Type B Activation Data */
440                 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
441                 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
442                 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
443                 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
444
445                 /* what about ta? sfgt? */
446
447                 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
448                         h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
449                 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
450                         h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
451
452                 switch (h->l2h->priv.iso14443b.state) {
453                         case ISO14443B_STATE_SELECTED:
454                                 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
455                                 break;
456                         case ISO14443B_STATE_ATTRIB_SENT:
457                                 h->priv.tcl.state = TCL_STATE_RATS_SENT;
458                                 break;
459                 }
460
461                 /* PUPI will be presented as ATS/historical bytes */
462                 memcpy(h->priv.tcl.ats, h->l2h->uid, 4);
463                 h->priv.tcl.ats_len = 4;
464                 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
465
466                 break;
467         default:
468                 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
469                 return -1;
470                 break;
471         }
472
473         h->priv.tcl.state = TCL_STATE_ESTABLISHED;
474
475         return 0;
476 }
477
478 static int
479 tcl_deselect(struct rfid_protocol_handle *h)
480 {
481         /* ISO 14443-4:2000(E) Section 8 */
482         int ret;
483         unsigned char frame[3];         /* 3 bytes prologue, no information */
484         unsigned char rx[3];
485         unsigned int rx_len = sizeof(rx);
486         unsigned int prlg_len;
487         struct tcl_handle *th = &h->priv.tcl;
488
489         if (th->state != TCL_STATE_ESTABLISHED) {
490                 /* FIXME: not sure whether deselect is possible here,
491                  * probably better send a HLTA? */
492         }
493
494         /* build DESELECT S-block */
495         ret = tcl_build_prologue_s(th, frame, &prlg_len);
496         if (ret < 0)
497                 return ret;
498
499         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
500                                      frame, prlg_len, rx,
501                                      &rx_len, deactivation_fwt(h),
502                                      TCL_TRANSP_F_TX_CRC);
503         if (ret < 0) {
504                 /* FIXME: retransmit, HLT(A|B) */
505                 return ret;
506         }
507
508         th->state = TCL_STATE_DESELECTED;
509
510         return 0;
511 }
512
513 struct fr_buff {
514         unsigned int frame_len;         /* length of frame */
515         unsigned int hdr_len;           /* length of header within frame */
516         unsigned char data[RFID_MAX_FRAMELEN];
517 };
518
519 #define frb_payload(x)  (x.data + x.hdr_len)
520
521
522 /* RFID transceive buffer. */
523 struct rfid_xcvb {
524         struct rfix_xcvb *next;         /* next in queue of buffers */
525
526         u_int64_t timeout;              /* timeout to wait for reply */
527         struct fr_buff tx;
528         struct fr_buff rx;
529
530         //struct rfid_protocol_handle *h;       /* connection to which we belong */
531 };
532
533 struct tcl_tx_context {
534         const unsigned char *tx;
535         unsigned char *rx;
536         const unsigned char *next_tx_byte;
537         unsigned char *next_rx_byte;
538         unsigned int rx_len;
539         unsigned int tx_len;
540         struct rfid_protocol_handle *h;
541 };
542
543 #define tcl_ctx_todo(ctx) (ctx->tx_len - (ctx->next_tx_byte - ctx->tx))
544
545 static int 
546 tcl_refill_xcvb(struct rfid_xcvb *xcvb, struct tcl_tx_context *ctx)
547 {
548         struct tcl_handle *th = &ctx->h->priv.tcl;
549
550         if (ctx->next_tx_byte >= ctx->tx + ctx->tx_len) {
551                 DEBUGP("tyring to refill tx xcvb but no data left!\n");
552                 return -1;
553         }
554
555         if (tcl_build_prologue_i(th, xcvb->tx.data, 
556                                  &xcvb->tx.hdr_len) < 0)
557                 return -1;
558
559         if (tcl_ctx_todo(ctx) > th->fsc - xcvb->tx.hdr_len)
560                 xcvb->tx.frame_len = max_net_tx_framesize(th);
561         else
562                 xcvb->tx.frame_len = tcl_ctx_todo(ctx);
563
564         memcpy(frb_payload(xcvb->tx), ctx->next_tx_byte,
565                 xcvb->tx.frame_len);
566
567         ctx->next_tx_byte += xcvb->tx.frame_len;
568
569         /* check whether we need to set the chaining bit */
570         if (ctx->next_tx_byte < ctx->tx + ctx->tx_len)
571                 xcvb->tx.data[0] |= 0x10;
572
573         /* add hdr_len after copying the net payload */
574         xcvb->tx.frame_len += xcvb->tx.hdr_len;
575
576         xcvb->timeout = th->fwt;
577
578         return 0;
579 }
580
581 static void fill_xcvb_wtxm(struct tcl_handle *th, struct rfid_xcvb *xcvb,
582                           unsigned char inf)
583 {
584         /* Acknowledge WTXM */
585         tcl_build_prologue_s(th, xcvb->tx.data, &xcvb->tx.hdr_len);
586         /* set two bits that make this block a wtx */
587         xcvb->tx.data[0] |= 0x30;
588         xcvb->tx.data[xcvb->tx.hdr_len] = inf;
589         xcvb->tx.frame_len = xcvb->tx.hdr_len+1;
590         xcvb->timeout = th->fwt * inf;
591 }
592
593 static int check_cid(struct tcl_handle *th, struct rfid_xcvb *xcvb)
594 {
595         if (xcvb->rx.data[0] & TCL_PCB_CID_FOLLOWING) {
596                 if (xcvb->rx.data[1] != th->cid) {
597                         DEBUGP("CID %u is not valid, we expected %u\n", 
598                                 xcvb->rx.data[1], th->cid);
599                         return 0;
600                 }
601         }
602         return 1;
603 }
604
605 static int
606 tcl_transceive(struct rfid_protocol_handle *h,
607                 const unsigned char *tx_data, unsigned int tx_len,
608                 unsigned char *rx_data, unsigned int *rx_len,
609                 unsigned int timeout, unsigned int flags)
610 {
611         int ret;
612
613         struct rfid_xcvb xcvb;
614         struct tcl_tx_context tcl_ctx;
615         struct tcl_handle *th = &h->priv.tcl;
616
617         unsigned char ack[10];
618         unsigned int ack_len;
619
620         /* initialize context */
621         tcl_ctx.next_tx_byte = tcl_ctx.tx = tx_data;
622         tcl_ctx.next_rx_byte = tcl_ctx.rx = rx_data;
623         tcl_ctx.rx_len = *rx_len;
624         tcl_ctx.tx_len = tx_len;
625         tcl_ctx.h = h;
626
627         /* initialize xcvb */
628         xcvb.timeout = th->fwt;
629
630 tx_refill:
631         if (tcl_refill_xcvb(&xcvb, &tcl_ctx) < 0) {
632                 ret = -1;
633                 goto out;
634         }
635
636 do_tx:
637         xcvb.rx.frame_len = sizeof(xcvb.rx.data);
638         ret = rfid_layer2_transceive(h->l2h, l2_to_frame(h->l2h->l2->id),
639                                      xcvb.tx.data, xcvb.tx.frame_len,
640                                      xcvb.rx.data, &xcvb.rx.frame_len,
641                                      xcvb.timeout, 0);
642
643         DEBUGP("l2 transceive finished\n");
644         if (ret < 0)
645                 goto out;
646
647         if (is_r_block(xcvb.rx.data[0])) {
648                 DEBUGP("R-Block\n");
649
650                 if ((xcvb.rx.data[0] & 0x01) != h->priv.tcl.toggle) {
651                         DEBUGP("response with wrong toggle bit\n");
652                         goto out;
653                 }
654
655                 /* Handle ACK frame in case of chaining */
656                 if (!check_cid(th, &xcvb))
657                         goto out;
658
659                 goto tx_refill;
660         } else if (is_s_block(xcvb.rx.data[0])) {
661                 unsigned char inf;
662                 unsigned int prlg_len;
663
664                 DEBUGP("S-Block\n");
665                 /* Handle Wait Time Extension */
666                 
667                 if (!check_cid(th, &xcvb))
668                         goto out;
669
670                 if (xcvb.rx.data[0] & TCL_PCB_CID_FOLLOWING) {
671                         if (xcvb.rx.frame_len < 3) {
672                                 DEBUGP("S-Block with CID but short len\n");
673                                 ret = -1;
674                                 goto out;
675                         }
676                         inf = xcvb.rx.data[2];
677                 } else
678                         inf = xcvb.rx.data[1];
679
680                 if ((xcvb.rx.data[0] & 0x30) != 0x30) {
681                         DEBUGP("S-Block but not WTX?\n");
682                         ret = -1;
683                         goto out;
684                 }
685                 inf &= 0x3f;    /* only lower 6 bits code WTXM */
686                 if (inf == 0 || (inf >= 60 && inf <= 63)) {
687                         DEBUGP("WTXM %u is RFU!\n", inf);
688                         ret = -1;
689                         goto out;
690                 }
691                 
692                 fill_xcvb_wtxm(th, &xcvb, inf);
693                 /* start over with next transceive */
694                 goto do_tx; 
695         } else if (is_i_block(xcvb.rx.data[0])) {
696                 unsigned int net_payload_len;
697                 /* we're actually receiving payload data */
698
699                 DEBUGP("I-Block: ");
700
701                 if ((xcvb.rx.data[0] & 0x01) != h->priv.tcl.toggle) {
702                         DEBUGP("response with wrong toggle bit\n");
703                         goto out;
704                 }
705
706                 xcvb.rx.hdr_len = 1;
707
708                 if (!check_cid(th, &xcvb))
709                         goto out;
710
711                 if (xcvb.rx.data[0] & TCL_PCB_CID_FOLLOWING)
712                         xcvb.rx.hdr_len++;
713                 if (xcvb.rx.data[0] & TCL_PCB_NAD_FOLLOWING)
714                         xcvb.rx.hdr_len++;
715         
716                 net_payload_len = xcvb.rx.frame_len - xcvb.rx.hdr_len;
717                 DEBUGPC("%u bytes\n", net_payload_len);
718                 memcpy(tcl_ctx.next_rx_byte, &xcvb.rx.data[xcvb.rx.hdr_len], 
719                         net_payload_len);
720                 tcl_ctx.next_rx_byte += net_payload_len;
721
722                 if (xcvb.rx.data[0] & 0x10) {
723                         /* we're not the last frame in the chain, continue rx */
724                         DEBUGP("not the last frame in the chain, continue\n");
725                         ack_len = sizeof(ack);
726                         tcl_build_prologue_r(th, xcvb.tx.data, &xcvb.tx.frame_len,                                                0);
727                         xcvb.timeout = th->fwt;
728                         goto do_tx;
729                 }
730         }
731
732 out:
733         *rx_len = tcl_ctx.next_rx_byte - tcl_ctx.rx;
734         return ret;
735 }
736
737 static struct rfid_protocol_handle *
738 tcl_init(struct rfid_layer2_handle *l2h)
739 {
740         struct rfid_protocol_handle *th;
741         unsigned int mru = l2h->rh->ah->mru;
742
743         th = malloc_protocol_handle(sizeof(struct rfid_protocol_handle));
744         if (!th)
745                 return NULL;
746
747         /* FIXME: mru should be attribute of layer2 (in case it adds/removes
748          * some overhead */
749         memset(th, 0, sizeof(struct rfid_protocol_handle));
750
751         /* maximum received ats length equals mru of asic/reader */
752         th->priv.tcl.state = TCL_STATE_INITIAL;
753         th->priv.tcl.ats_len = mru;
754         th->priv.tcl.toggle = 1;
755
756         th->priv.tcl.fsd = iso14443_fsd_approx(mru);
757
758         return th;
759 }
760
761 static int
762 tcl_fini(struct rfid_protocol_handle *ph)
763 {
764         free_protocol_handle(ph);
765         return 0;
766 }
767
768 int 
769 tcl_getopt(struct rfid_protocol_handle *h, int optname, void *optval,
770            unsigned int *optlen)
771 {
772         u_int8_t *opt_str = optval;
773
774         switch (optname) {
775         case RFID_OPT_P_TCL_ATS:
776                 if (h->priv.tcl.ats_len < *optlen)
777                         *optlen = h->priv.tcl.ats_len;
778                 memcpy(opt_str, h->priv.tcl.ats, *optlen);
779                 break;
780         case RFID_OPT_P_TCL_ATS_LEN:
781                 if (*optlen < sizeof(u_int8_t))
782                         return -E2BIG;
783                 *optlen = sizeof(u_int8_t);
784                 *opt_str = h->priv.tcl.ats_len & 0xff;
785                 break;
786         default:
787                 return -EINVAL;
788         }
789
790         return 0;
791 }
792
793 int
794 tcl_setopt(struct rfid_protocol_handle *h, int optname, const void *optval,
795            unsigned int optlen)
796 {
797         int ret = -EINVAL;
798
799         switch (optname) {
800         default:
801                 break;
802         }
803
804         return ret;
805 }
806
807 const struct rfid_protocol rfid_protocol_tcl = {
808         .id     = RFID_PROTOCOL_TCL,
809         .name   = "ISO 14443-4 / T=CL",
810         .fn     = {
811                 .init = &tcl_init,
812                 .open = &tcl_connect,
813                 .transceive = &tcl_transceive,
814                 .close = &tcl_deselect,
815                 .fini = &tcl_fini,
816                 .getopt = &tcl_getopt,
817                 .setopt = &tcl_setopt,
818         },
819 };