add more debugging about CID/NAD capability of card
[librfid] / src / rfid_proto_tcl.c
1 /* ISO 14443-4 (T=CL) implementation, PCD side.
2  *
3  * (C) 2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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, since it has no (R)ATS */
126                 }
127                 return 0;
128         }
129
130         /* guarateed to be at least 2 bytes in size */
131
132         t0 = ats[1];
133         cur = &ats[2];
134
135         iso14443_fsdi_to_fsd(&h->priv.tcl.fsc, t0 & 0x0f);
136         if (h->priv.tcl.fsc > h->l2h->rh->ah->mtu)
137                 h->priv.tcl.fsc = h->l2h->rh->ah->mtu;
138
139         if (t0 & (1 << 4)) {
140                 /* TA is transmitted */
141                 h->priv.tcl.ta = *cur++;
142         }
143
144         if (t0 & (1 << 5)) {
145                 /* TB is transmitted */
146                 h->priv.tcl.sfgt = sfgi_to_sfgt(h, *cur & 0x0f);
147                 h->priv.tcl.fwt = fwi_to_fwt(h, (*cur & 0xf0) >> 4);
148                 cur++;
149         }
150
151         if (t0 & (1 << 6)) {
152                 /* TC is transmitted */
153                 if (*cur & 0x01) {
154                         h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
155                         DEBUGP("This PICC supports NAD\n");
156                 }
157                 if (*cur & 0x02) {
158                         h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
159                         DEBUGP("This PICC supports CID\n");
160                 }
161                 cur++;
162         }
163
164         h->priv.tcl.historical_len = (ats+len) - cur;
165         h->priv.tcl.historical_bytes = cur;
166
167         return 0;
168 }
169
170
171 /* request an ATS from the PICC */
172 static int
173 tcl_request_ats(struct rfid_protocol_handle *h)
174 {
175         int ret;
176         unsigned char rats[2];
177         unsigned char fsdi;
178
179         if (h->priv.tcl.state != TCL_STATE_INITIAL)
180                 return -1;
181
182         ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.tcl.fsd);
183         if (ret < 0) {
184                 DEBUGP("unable to encode FSD of %u as FSDI\n", h->priv.tcl.fsd);
185                 return ret;
186         }
187
188         rats[0] = 0xe0;
189         rats[1] = (h->priv.tcl.cid & 0x0f) | ((fsdi << 4) & 0xf0);
190
191         /* transceive (with CRC) */
192         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
193                                      rats, 2, h->priv.tcl.ats,
194                                      &h->priv.tcl.ats_len, activation_fwt(h),
195                                      TCL_TRANSP_F_TX_CRC);
196         if (ret < 0) {
197                 DEBUGP("transceive of rats failed\n");
198                 h->priv.tcl.state = TCL_STATE_RATS_SENT;
199                 /* FIXME: retransmit */
200                 return ret;
201         }
202         h->priv.tcl.state = TCL_STATE_ATS_RCVD;
203
204         ret = tcl_parse_ats(h, h->priv.tcl.ats, h->priv.tcl.ats_len);
205         if (ret < 0) {
206                 DEBUGP("parsing of ats failed\n");
207                 return ret;
208         }
209
210         return 0;
211 }
212
213 #define ATS_TA_DIV_2    1
214 #define ATS_TA_DIV_4    2
215 #define ATS_TA_DIV_8    4
216
217 #define PPS_DIV_8       3
218 #define PPS_DIV_4       2
219 #define PPS_DIV_2       1
220 #define PPS_DIV_1       0
221 static unsigned char d_to_di(struct rfid_protocol_handle *h, unsigned char D)
222 {
223         static char DI;
224         unsigned int speed = h->l2h->rh->reader->iso14443a.speed;
225         
226         if ((D & ATS_TA_DIV_8) && (speed & RFID_14443A_SPEED_848K))
227                 DI = PPS_DIV_8;
228         else if ((D & ATS_TA_DIV_4) && (speed & RFID_14443A_SPEED_424K))
229                 DI = PPS_DIV_4;
230         else if ((D & ATS_TA_DIV_2) && (speed & RFID_14443A_SPEED_212K))
231                 DI = PPS_DIV_2;
232         else
233                 DI = PPS_DIV_1;
234
235         return DI;
236 }
237
238 static unsigned int di_to_speed(unsigned char DI)
239 {
240         switch (DI) {
241         case PPS_DIV_8:
242                 return RFID_14443A_SPEED_848K;
243                 break;
244         case PPS_DIV_4:
245                 return RFID_14443A_SPEED_424K;
246                 break;
247         case PPS_DIV_2:
248                 return RFID_14443A_SPEED_212K;
249                 break;
250         case PPS_DIV_1:
251                 return RFID_14443A_SPEED_106K;
252                 break;
253         }
254 }
255
256 /* start a PPS run (autimatically configure highest possible speed */
257 static int 
258 tcl_do_pps(struct rfid_protocol_handle *h)
259 {
260         int ret;
261         unsigned char ppss[3];
262         unsigned char pps_response[1];
263         unsigned int rx_len = 1;
264         unsigned char Dr, Ds, DrI, DsI;
265         unsigned int speed;
266
267         if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
268                 return -1;
269
270         Dr = h->priv.tcl.ta & 0x07;
271         Ds = h->priv.tcl.ta & 0x70 >> 4;
272         DEBUGP("Dr = 0x%x, Ds = 0x%x\n", Dr, Ds);
273
274         if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
275                 /* device supports different divisors for rx and tx, but not
276                  * really ?!? */
277                 DEBUGP("PICC has contradictory TA, aborting PPS\n");
278                 return -1;
279         };
280
281         /* ISO 14443-4:2000(E) Section 5.3. */
282
283         ppss[0] = 0xd0 | (h->priv.tcl.cid & 0x0f);
284         ppss[1] = 0x11;
285         ppss[2] = 0x00;
286
287         /* FIXME: deal with different speed for each direction */
288         DrI = d_to_di(h, Dr);
289         DsI = d_to_di(h, Ds);
290         DEBUGP("DrI = 0x%x, DsI = 0x%x\n", DrI, DsI);
291
292         ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
293
294         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
295                                         ppss, 3, pps_response, &rx_len,
296                                         h->priv.tcl.fwt, TCL_TRANSP_F_TX_CRC);
297         if (ret < 0)
298                 return ret;
299
300         if (pps_response[0] != ppss[0]) {
301                 DEBUGP("PPS Response != PPSS\n");
302                 return -1;
303         }
304
305         speed = di_to_speed(DrI);
306
307         ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_RX,
308                                  &speed, sizeof(speed));
309         if (ret < 0)
310                 return ret;
311
312         ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_TX,
313                                  &speed, sizeof(speed));
314         if (ret < 0)
315                 return ret;
316         
317         return 0;
318 }
319
320
321 static int
322 tcl_build_prologue2(struct tcl_handle *th, 
323                     unsigned char *prlg, unsigned int *prlg_len, 
324                     unsigned char pcb)
325 {
326         *prlg_len = 1;
327
328         *prlg = pcb;
329
330         if (!is_s_block(pcb)) {
331                 if (th->toggle) {
332                         /* we've sent a toggle bit last time */
333                         th->toggle = 0;
334                 } else {
335                         /* we've not sent a toggle last time: send one */
336                         th->toggle = 1;
337                         *prlg |= 0x01;
338                 }
339         }
340
341         if (th->flags & TCL_HANDLE_F_CID_USED) {
342                 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
343                 *prlg |= TCL_PCB_CID_FOLLOWING;
344                 (*prlg_len)++;
345                 prlg[*prlg_len] = th->cid & 0x0f;
346         }
347
348         /* nad only for I-block (0xc0 == 00) */
349         if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
350             ((pcb & 0xc0) == 0x00)) {
351                 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
352                 /* FIXME: in case of chaining only for first frame */
353                 *prlg |= TCL_PCB_NAD_FOLLOWING;
354                 prlg[*prlg_len] = th->nad;
355                 (*prlg_len)++;
356         }
357
358         return 0;
359 }
360
361 static int
362 tcl_build_prologue_i(struct tcl_handle *th,
363                      unsigned char *prlg, unsigned int *prlg_len)
364 {
365         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
366         return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
367 }
368
369 static int
370 tcl_build_prologue_r(struct tcl_handle *th,
371                      unsigned char *prlg, unsigned int *prlg_len,
372                      unsigned int nak)
373 {
374         unsigned char pcb = 0xa2;
375         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
376
377         if (nak)
378                 pcb |= 0x10;
379
380         return tcl_build_prologue2(th, prlg, prlg_len, pcb);
381 }
382
383 static int
384 tcl_build_prologue_s(struct tcl_handle *th,
385                      unsigned char *prlg, unsigned int *prlg_len)
386 {
387         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
388
389         /* the only S-block from PCD->PICC is DESELECT,
390          * well, actually there is the S(WTX) response. */
391         return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
392 }
393
394 /* FIXME: WTXM implementation */
395
396 static int tcl_prlg_len(struct tcl_handle *th)
397 {
398         int prlg_len = 1;
399
400         if (th->flags & TCL_HANDLE_F_CID_USED)
401                 prlg_len++;
402
403         if (th->flags & TCL_HANDLE_F_NAD_USED)
404                 prlg_len++;
405
406         return prlg_len;
407 }
408
409 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
410
411 static int
412 tcl_connect(struct rfid_protocol_handle *h)
413 {
414         int ret; 
415
416         if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
417             h->priv.tcl.state != TCL_STATE_INITIAL)
418                 return -1;
419
420         switch (h->l2h->l2->id) {
421         case RFID_LAYER2_ISO14443A:
422                 /* Start Type A T=CL Activation Sequence */
423                 ret = tcl_request_ats(h);
424                 if (ret < 0)
425                         return ret;
426
427                 /* Only do PPS if any non-default divisors supported */
428                 if (h->priv.tcl.ta & 0x77) {
429                         ret = tcl_do_pps(h);
430                         if (ret < 0)
431                                 return ret;
432                 }
433                 break;
434         case RFID_LAYER2_ISO14443B:
435                 /* initialized T=CL state from Type B Activation Data */
436                 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
437                 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
438                 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
439                 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
440
441                 /* what about ta? sfgt? */
442
443                 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
444                         h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
445                 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
446                         h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
447
448                 switch (h->l2h->priv.iso14443b.state) {
449                         case ISO14443B_STATE_SELECTED:
450                                 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
451                                 break;
452                         case ISO14443B_STATE_ATTRIB_SENT:
453                                 h->priv.tcl.state = TCL_STATE_RATS_SENT;
454                                 break;
455                 }
456
457                 /* PUPI will be presented as ATS/historical bytes */
458                 memcpy(h->priv.tcl.ats, h->l2h->uid, 4);
459                 h->priv.tcl.ats_len = 4;
460                 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
461
462                 break;
463         default:
464                 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
465                 return -1;
466                 break;
467         }
468
469         h->priv.tcl.state = TCL_STATE_ESTABLISHED;
470
471         return 0;
472 }
473
474 static int
475 tcl_deselect(struct rfid_protocol_handle *h)
476 {
477         /* ISO 14443-4:2000(E) Section 8 */
478         int ret;
479         unsigned char frame[3];         /* 3 bytes prologue, no information */
480         unsigned char rx[3];
481         unsigned int rx_len = sizeof(rx);
482         unsigned int prlg_len;
483         struct tcl_handle *th = &h->priv.tcl;
484
485         if (th->state != TCL_STATE_ESTABLISHED) {
486                 /* FIXME: not sure whether deselect is possible here,
487                  * probably better send a HLTA? */
488         }
489
490         /* build DESELECT S-block */
491         ret = tcl_build_prologue_s(th, frame, &prlg_len);
492         if (ret < 0)
493                 return ret;
494
495         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
496                                      frame, prlg_len, rx,
497                                      &rx_len, deactivation_fwt(h),
498                                      TCL_TRANSP_F_TX_CRC);
499         if (ret < 0) {
500                 /* FIXME: retransmit, HLT(A|B) */
501                 return ret;
502         }
503
504         th->state = TCL_STATE_DESELECTED;
505
506         return 0;
507 }
508
509 struct fr_buff {
510         unsigned int frame_len;         /* length of frame */
511         unsigned int hdr_len;           /* length of header within frame */
512         unsigned char data[RFID_MAX_FRAMELEN];
513 };
514
515 #define frb_payload(x)  (x.data + x.hdr_len)
516
517
518 /* RFID transceive buffer. */
519 struct rfid_xcvb {
520         struct rfix_xcvb *next;         /* next in queue of buffers */
521
522         u_int64_t timeout;              /* timeout to wait for reply */
523         struct fr_buff tx;
524         struct fr_buff rx;
525
526         //struct rfid_protocol_handle *h;       /* connection to which we belong */
527 };
528
529 struct tcl_tx_context {
530         const unsigned char *tx;
531         unsigned char *rx;
532         const unsigned char *next_tx_byte;
533         unsigned char *next_rx_byte;
534         unsigned int rx_len;
535         unsigned int tx_len;
536         struct rfid_protocol_handle *h;
537 };
538
539 #define tcl_ctx_todo(ctx) (ctx->tx_len - (ctx->next_tx_byte - ctx->tx))
540
541 static int 
542 tcl_refill_xcvb(struct rfid_xcvb *xcvb, struct tcl_tx_context *ctx)
543 {
544         struct tcl_handle *th = &ctx->h->priv.tcl;
545
546         if (ctx->next_tx_byte >= ctx->tx + ctx->tx_len) {
547                 DEBUGP("tyring to refill tx xcvb but no data left!\n");
548                 return -1;
549         }
550
551         if (tcl_build_prologue_i(th, xcvb->tx.data, 
552                                  &xcvb->tx.hdr_len) < 0)
553                 return -1;
554
555         if (tcl_ctx_todo(ctx) > th->fsc - xcvb->tx.hdr_len)
556                 xcvb->tx.frame_len = max_net_tx_framesize(th);
557         else
558                 xcvb->tx.frame_len = tcl_ctx_todo(ctx);
559
560         memcpy(frb_payload(xcvb->tx), ctx->next_tx_byte,
561                 xcvb->tx.frame_len);
562
563         ctx->next_tx_byte += xcvb->tx.frame_len;
564
565         /* check whether we need to set the chaining bit */
566         if (ctx->next_tx_byte < ctx->tx + ctx->tx_len)
567                 xcvb->tx.data[0] |= 0x10;
568
569         /* add hdr_len after copying the net payload */
570         xcvb->tx.frame_len += xcvb->tx.hdr_len;
571
572         xcvb->timeout = th->fwt;
573
574         return 0;
575 }
576
577 static void fill_xcvb_wtxm(struct tcl_handle *th, struct rfid_xcvb *xcvb,
578                           unsigned char inf)
579 {
580         /* Acknowledge WTXM */
581         tcl_build_prologue_s(th, xcvb->tx.data, &xcvb->tx.hdr_len);
582         /* set two bits that make this block a wtx */
583         xcvb->tx.data[0] |= 0x30;
584         xcvb->tx.data[xcvb->tx.hdr_len] = inf;
585         xcvb->tx.frame_len = xcvb->tx.hdr_len+1;
586         xcvb->timeout = th->fwt * inf;
587 }
588
589 static int check_cid(struct tcl_handle *th, struct rfid_xcvb *xcvb)
590 {
591         if (xcvb->rx.data[0] & TCL_PCB_CID_FOLLOWING) {
592                 if (xcvb->rx.data[1] != th->cid) {
593                         DEBUGP("CID %u is not valid, we expected %u\n", 
594                                 xcvb->rx.data[1], th->cid);
595                         return 0;
596                 }
597         }
598         return 1;
599 }
600
601 static int
602 tcl_transceive(struct rfid_protocol_handle *h,
603                 const unsigned char *tx_data, unsigned int tx_len,
604                 unsigned char *rx_data, unsigned int *rx_len,
605                 unsigned int timeout, unsigned int flags)
606 {
607         int ret;
608
609         struct rfid_xcvb xcvb;
610         struct tcl_tx_context tcl_ctx;
611         struct tcl_handle *th = &h->priv.tcl;
612
613         unsigned char ack[10];
614         unsigned int ack_len;
615
616         /* initialize context */
617         tcl_ctx.next_tx_byte = tcl_ctx.tx = tx_data;
618         tcl_ctx.next_rx_byte = tcl_ctx.rx = rx_data;
619         tcl_ctx.rx_len = *rx_len;
620         tcl_ctx.tx_len = tx_len;
621         tcl_ctx.h = h;
622
623         /* initialize xcvb */
624         xcvb.timeout = th->fwt;
625
626 tx_refill:
627         if (tcl_refill_xcvb(&xcvb, &tcl_ctx) < 0) {
628                 ret = -1;
629                 goto out;
630         }
631
632 do_tx:
633         xcvb.rx.frame_len = sizeof(xcvb.rx.data);
634         ret = rfid_layer2_transceive(h->l2h, l2_to_frame(h->l2h->l2->id),
635                                      xcvb.tx.data, xcvb.tx.frame_len,
636                                      xcvb.rx.data, &xcvb.rx.frame_len,
637                                      xcvb.timeout, 0);
638
639         DEBUGP("l2 transceive finished\n");
640         if (ret < 0)
641                 goto out;
642
643         if (is_r_block(xcvb.rx.data[0])) {
644                 DEBUGP("R-Block\n");
645
646                 if ((xcvb.rx.data[0] & 0x01) != h->priv.tcl.toggle) {
647                         DEBUGP("response with wrong toggle bit\n");
648                         goto out;
649                 }
650
651                 /* Handle ACK frame in case of chaining */
652                 if (!check_cid(th, &xcvb))
653                         goto out;
654
655                 goto tx_refill;
656         } else if (is_s_block(xcvb.rx.data[0])) {
657                 unsigned char inf;
658                 unsigned int prlg_len;
659
660                 DEBUGP("S-Block\n");
661                 /* Handle Wait Time Extension */
662                 
663                 if (!check_cid(th, &xcvb))
664                         goto out;
665
666                 if (xcvb.rx.data[0] & TCL_PCB_CID_FOLLOWING) {
667                         if (xcvb.rx.frame_len < 3) {
668                                 DEBUGP("S-Block with CID but short len\n");
669                                 ret = -1;
670                                 goto out;
671                         }
672                         inf = xcvb.rx.data[2];
673                 } else
674                         inf = xcvb.rx.data[1];
675
676                 if ((xcvb.rx.data[0] & 0x30) != 0x30) {
677                         DEBUGP("S-Block but not WTX?\n");
678                         ret = -1;
679                         goto out;
680                 }
681                 inf &= 0x3f;    /* only lower 6 bits code WTXM */
682                 if (inf == 0 || (inf >= 60 && inf <= 63)) {
683                         DEBUGP("WTXM %u is RFU!\n", inf);
684                         ret = -1;
685                         goto out;
686                 }
687                 
688                 fill_xcvb_wtxm(th, &xcvb, inf);
689                 /* start over with next transceive */
690                 goto do_tx; 
691         } else if (is_i_block(xcvb.rx.data[0])) {
692                 unsigned int net_payload_len;
693                 /* we're actually receiving payload data */
694
695                 DEBUGP("I-Block: ");
696
697                 if ((xcvb.rx.data[0] & 0x01) != h->priv.tcl.toggle) {
698                         DEBUGP("response with wrong toggle bit\n");
699                         goto out;
700                 }
701
702                 xcvb.rx.hdr_len = 1;
703
704                 if (!check_cid(th, &xcvb))
705                         goto out;
706
707                 if (xcvb.rx.data[0] & TCL_PCB_CID_FOLLOWING)
708                         xcvb.rx.hdr_len++;
709                 if (xcvb.rx.data[0] & TCL_PCB_NAD_FOLLOWING)
710                         xcvb.rx.hdr_len++;
711         
712                 net_payload_len = xcvb.rx.frame_len - xcvb.rx.hdr_len;
713                 DEBUGPC("%u bytes\n", net_payload_len);
714                 memcpy(tcl_ctx.next_rx_byte, &xcvb.rx.data[xcvb.rx.hdr_len], 
715                         net_payload_len);
716                 tcl_ctx.next_rx_byte += net_payload_len;
717
718                 if (xcvb.rx.data[0] & 0x10) {
719                         /* we're not the last frame in the chain, continue rx */
720                         DEBUGP("not the last frame in the chain, continue\n");
721                         ack_len = sizeof(ack);
722                         tcl_build_prologue_r(th, xcvb.tx.data, &xcvb.tx.frame_len,                                                0);
723                         xcvb.timeout = th->fwt;
724                         goto do_tx;
725                 }
726         }
727
728 out:
729         *rx_len = tcl_ctx.next_rx_byte - tcl_ctx.rx;
730         return ret;
731 }
732
733 static struct rfid_protocol_handle *
734 tcl_init(struct rfid_layer2_handle *l2h)
735 {
736         struct rfid_protocol_handle *th;
737         unsigned int mru = l2h->rh->ah->mru;
738
739         th = malloc(sizeof(struct rfid_protocol_handle) + mru);
740         if (!th)
741                 return NULL;
742
743         /* FIXME: mru should be attribute of layer2 (in case it adds/removes
744          * some overhead */
745         memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
746
747         /* maximum received ats length equals mru of asic/reader */
748         th->priv.tcl.state = TCL_STATE_INITIAL;
749         th->priv.tcl.ats_len = mru;
750         th->priv.tcl.toggle = 1;
751
752         th->priv.tcl.fsd = iso14443_fsd_approx(mru);
753
754         return th;
755 }
756
757 static int
758 tcl_fini(struct rfid_protocol_handle *ph)
759 {
760         free(ph);
761         return 0;
762 }
763
764 struct rfid_protocol rfid_protocol_tcl = {
765         .id     = RFID_PROTOCOL_TCL,
766         .name   = "ISO 14443-4 / T=CL",
767         .fn     = {
768                 .init = &tcl_init,
769                 .open = &tcl_connect,
770                 .transceive = &tcl_transceive,
771                 .close = &tcl_deselect,
772                 .fini = &tcl_fini,
773         },
774 };