Prepare RFID compilation in firmware mode
[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., 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         /* FIXME: this stinks like hell. IF we reduce pps_response size to one,
263            we'll get stack corruption! */
264         unsigned char pps_response[10];
265         unsigned int rx_len = 1;
266         unsigned char Dr, Ds, DrI, DsI;
267         unsigned int speed;
268
269         if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
270                 return -1;
271
272         Dr = h->priv.tcl.ta & 0x07;
273         Ds = h->priv.tcl.ta & 0x70 >> 4;
274         DEBUGP("Dr = 0x%x, Ds = 0x%x\n", Dr, Ds);
275
276         if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
277                 /* device supports different divisors for rx and tx, but not
278                  * really ?!? */
279                 DEBUGP("PICC has contradictory TA, aborting PPS\n");
280                 return -1;
281         };
282
283         /* ISO 14443-4:2000(E) Section 5.3. */
284
285         ppss[0] = 0xd0 | (h->priv.tcl.cid & 0x0f);
286         ppss[1] = 0x11;
287         ppss[2] = 0x00;
288
289         /* FIXME: deal with different speed for each direction */
290         DrI = d_to_di(h, Dr);
291         DsI = d_to_di(h, Ds);
292         DEBUGP("DrI = 0x%x, DsI = 0x%x\n", DrI, DsI);
293
294         ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
295
296         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
297                                         ppss, 3, pps_response, &rx_len,
298                                         h->priv.tcl.fwt, TCL_TRANSP_F_TX_CRC);
299         if (ret < 0)
300                 return ret;
301
302         if (pps_response[0] != ppss[0]) {
303                 DEBUGP("PPS Response != PPSS\n");
304                 return -1;
305         }
306
307         speed = di_to_speed(DrI);
308
309         ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_RX,
310                                  &speed, sizeof(speed));
311         if (ret < 0)
312                 return ret;
313
314         ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_TX,
315                                  &speed, sizeof(speed));
316         if (ret < 0)
317                 return ret;
318         
319         return 0;
320 }
321
322
323 static int
324 tcl_build_prologue2(struct tcl_handle *th, 
325                     unsigned char *prlg, unsigned int *prlg_len, 
326                     unsigned char pcb)
327 {
328         *prlg_len = 1;
329
330         *prlg = pcb;
331
332         if (!is_s_block(pcb)) {
333                 if (th->toggle) {
334                         /* we've sent a toggle bit last time */
335                         th->toggle = 0;
336                 } else {
337                         /* we've not sent a toggle last time: send one */
338                         th->toggle = 1;
339                         *prlg |= 0x01;
340                 }
341         }
342
343         if (th->flags & TCL_HANDLE_F_CID_USED) {
344                 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
345                 *prlg |= TCL_PCB_CID_FOLLOWING;
346                 (*prlg_len)++;
347                 prlg[*prlg_len] = th->cid & 0x0f;
348         }
349
350         /* nad only for I-block */
351         if ((th->flags & TCL_HANDLE_F_NAD_USED) && is_i_block(pcb)) {
352                 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
353                 /* FIXME: in case of chaining only for first frame */
354                 *prlg |= TCL_PCB_NAD_FOLLOWING;
355                 prlg[*prlg_len] = th->nad;
356                 (*prlg_len)++;
357         }
358
359         return 0;
360 }
361
362 static int
363 tcl_build_prologue_i(struct tcl_handle *th,
364                      unsigned char *prlg, unsigned int *prlg_len)
365 {
366         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
367         return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
368 }
369
370 static int
371 tcl_build_prologue_r(struct tcl_handle *th,
372                      unsigned char *prlg, unsigned int *prlg_len,
373                      unsigned int nak)
374 {
375         unsigned char pcb = 0xa2;
376         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
377
378         if (nak)
379                 pcb |= 0x10;
380
381         return tcl_build_prologue2(th, prlg, prlg_len, pcb);
382 }
383
384 static int
385 tcl_build_prologue_s(struct tcl_handle *th,
386                      unsigned char *prlg, unsigned int *prlg_len)
387 {
388         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
389
390         /* the only S-block from PCD->PICC is DESELECT,
391          * well, actually there is the S(WTX) response. */
392         return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
393 }
394
395 /* FIXME: WTXM implementation */
396
397 static int tcl_prlg_len(struct tcl_handle *th)
398 {
399         int prlg_len = 1;
400
401         if (th->flags & TCL_HANDLE_F_CID_USED)
402                 prlg_len++;
403
404         if (th->flags & TCL_HANDLE_F_NAD_USED)
405                 prlg_len++;
406
407         return prlg_len;
408 }
409
410 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
411
412 static int
413 tcl_connect(struct rfid_protocol_handle *h)
414 {
415         int ret; 
416
417         if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
418             h->priv.tcl.state != TCL_STATE_INITIAL)
419                 return -1;
420
421         switch (h->l2h->l2->id) {
422         case RFID_LAYER2_ISO14443A:
423                 /* Start Type A T=CL Activation Sequence */
424                 ret = tcl_request_ats(h);
425                 if (ret < 0)
426                         return ret;
427
428                 /* Only do PPS if any non-default divisors supported */
429                 if (h->priv.tcl.ta & 0x77) {
430                         ret = tcl_do_pps(h);
431                         if (ret < 0)
432                                 return ret;
433                 }
434                 break;
435         case RFID_LAYER2_ISO14443B:
436                 /* initialized T=CL state from Type B Activation Data */
437                 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
438                 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
439                 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
440                 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
441
442                 /* what about ta? sfgt? */
443
444                 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
445                         h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
446                 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
447                         h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
448
449                 switch (h->l2h->priv.iso14443b.state) {
450                         case ISO14443B_STATE_SELECTED:
451                                 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
452                                 break;
453                         case ISO14443B_STATE_ATTRIB_SENT:
454                                 h->priv.tcl.state = TCL_STATE_RATS_SENT;
455                                 break;
456                 }
457
458                 /* PUPI will be presented as ATS/historical bytes */
459                 memcpy(h->priv.tcl.ats, h->l2h->uid, 4);
460                 h->priv.tcl.ats_len = 4;
461                 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
462
463                 break;
464         default:
465                 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
466                 return -1;
467                 break;
468         }
469
470         h->priv.tcl.state = TCL_STATE_ESTABLISHED;
471
472         return 0;
473 }
474
475 static int
476 tcl_deselect(struct rfid_protocol_handle *h)
477 {
478         /* ISO 14443-4:2000(E) Section 8 */
479         int ret;
480         unsigned char frame[3];         /* 3 bytes prologue, no information */
481         unsigned char rx[3];
482         unsigned int rx_len = sizeof(rx);
483         unsigned int prlg_len;
484         struct tcl_handle *th = &h->priv.tcl;
485
486         if (th->state != TCL_STATE_ESTABLISHED) {
487                 /* FIXME: not sure whether deselect is possible here,
488                  * probably better send a HLTA? */
489         }
490
491         /* build DESELECT S-block */
492         ret = tcl_build_prologue_s(th, frame, &prlg_len);
493         if (ret < 0)
494                 return ret;
495
496         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
497                                      frame, prlg_len, rx,
498                                      &rx_len, deactivation_fwt(h),
499                                      TCL_TRANSP_F_TX_CRC);
500         if (ret < 0) {
501                 /* FIXME: retransmit, HLT(A|B) */
502                 return ret;
503         }
504
505         th->state = TCL_STATE_DESELECTED;
506
507         return 0;
508 }
509
510 struct fr_buff {
511         unsigned int frame_len;         /* length of frame */
512         unsigned int hdr_len;           /* length of header within frame */
513         unsigned char data[RFID_MAX_FRAMELEN];
514 };
515
516 #define frb_payload(x)  (x.data + x.hdr_len)
517
518
519 /* RFID transceive buffer. */
520 struct rfid_xcvb {
521         struct rfix_xcvb *next;         /* next in queue of buffers */
522
523         u_int64_t timeout;              /* timeout to wait for reply */
524         struct fr_buff tx;
525         struct fr_buff rx;
526
527         //struct rfid_protocol_handle *h;       /* connection to which we belong */
528 };
529
530 struct tcl_tx_context {
531         const unsigned char *tx;
532         unsigned char *rx;
533         const unsigned char *next_tx_byte;
534         unsigned char *next_rx_byte;
535         unsigned int rx_len;
536         unsigned int tx_len;
537         struct rfid_protocol_handle *h;
538 };
539
540 #define tcl_ctx_todo(ctx) (ctx->tx_len - (ctx->next_tx_byte - ctx->tx))
541
542 static int 
543 tcl_refill_xcvb(struct rfid_xcvb *xcvb, struct tcl_tx_context *ctx)
544 {
545         struct tcl_handle *th = &ctx->h->priv.tcl;
546
547         if (ctx->next_tx_byte >= ctx->tx + ctx->tx_len) {
548                 DEBUGP("tyring to refill tx xcvb but no data left!\n");
549                 return -1;
550         }
551
552         if (tcl_build_prologue_i(th, xcvb->tx.data, 
553                                  &xcvb->tx.hdr_len) < 0)
554                 return -1;
555
556         if (tcl_ctx_todo(ctx) > th->fsc - xcvb->tx.hdr_len)
557                 xcvb->tx.frame_len = max_net_tx_framesize(th);
558         else
559                 xcvb->tx.frame_len = tcl_ctx_todo(ctx);
560
561         memcpy(frb_payload(xcvb->tx), ctx->next_tx_byte,
562                 xcvb->tx.frame_len);
563
564         ctx->next_tx_byte += xcvb->tx.frame_len;
565
566         /* check whether we need to set the chaining bit */
567         if (ctx->next_tx_byte < ctx->tx + ctx->tx_len)
568                 xcvb->tx.data[0] |= 0x10;
569
570         /* add hdr_len after copying the net payload */
571         xcvb->tx.frame_len += xcvb->tx.hdr_len;
572
573         xcvb->timeout = th->fwt;
574
575         return 0;
576 }
577
578 static void fill_xcvb_wtxm(struct tcl_handle *th, struct rfid_xcvb *xcvb,
579                           unsigned char inf)
580 {
581         /* Acknowledge WTXM */
582         tcl_build_prologue_s(th, xcvb->tx.data, &xcvb->tx.hdr_len);
583         /* set two bits that make this block a wtx */
584         xcvb->tx.data[0] |= 0x30;
585         xcvb->tx.data[xcvb->tx.hdr_len] = inf;
586         xcvb->tx.frame_len = xcvb->tx.hdr_len+1;
587         xcvb->timeout = th->fwt * inf;
588 }
589
590 static int check_cid(struct tcl_handle *th, struct rfid_xcvb *xcvb)
591 {
592         if (xcvb->rx.data[0] & TCL_PCB_CID_FOLLOWING) {
593                 if (xcvb->rx.data[1] != th->cid) {
594                         DEBUGP("CID %u is not valid, we expected %u\n", 
595                                 xcvb->rx.data[1], th->cid);
596                         return 0;
597                 }
598         }
599         return 1;
600 }
601
602 static int
603 tcl_transceive(struct rfid_protocol_handle *h,
604                 const unsigned char *tx_data, unsigned int tx_len,
605                 unsigned char *rx_data, unsigned int *rx_len,
606                 unsigned int timeout, unsigned int flags)
607 {
608         int ret;
609
610         struct rfid_xcvb xcvb;
611         struct tcl_tx_context tcl_ctx;
612         struct tcl_handle *th = &h->priv.tcl;
613
614         unsigned char ack[10];
615         unsigned int ack_len;
616
617         /* initialize context */
618         tcl_ctx.next_tx_byte = tcl_ctx.tx = tx_data;
619         tcl_ctx.next_rx_byte = tcl_ctx.rx = rx_data;
620         tcl_ctx.rx_len = *rx_len;
621         tcl_ctx.tx_len = tx_len;
622         tcl_ctx.h = h;
623
624         /* initialize xcvb */
625         xcvb.timeout = th->fwt;
626
627 tx_refill:
628         if (tcl_refill_xcvb(&xcvb, &tcl_ctx) < 0) {
629                 ret = -1;
630                 goto out;
631         }
632
633 do_tx:
634         xcvb.rx.frame_len = sizeof(xcvb.rx.data);
635         ret = rfid_layer2_transceive(h->l2h, l2_to_frame(h->l2h->l2->id),
636                                      xcvb.tx.data, xcvb.tx.frame_len,
637                                      xcvb.rx.data, &xcvb.rx.frame_len,
638                                      xcvb.timeout, 0);
639
640         DEBUGP("l2 transceive finished\n");
641         if (ret < 0)
642                 goto out;
643
644         if (is_r_block(xcvb.rx.data[0])) {
645                 DEBUGP("R-Block\n");
646
647                 if ((xcvb.rx.data[0] & 0x01) != h->priv.tcl.toggle) {
648                         DEBUGP("response with wrong toggle bit\n");
649                         goto out;
650                 }
651
652                 /* Handle ACK frame in case of chaining */
653                 if (!check_cid(th, &xcvb))
654                         goto out;
655
656                 goto tx_refill;
657         } else if (is_s_block(xcvb.rx.data[0])) {
658                 unsigned char inf;
659                 unsigned int prlg_len;
660
661                 DEBUGP("S-Block\n");
662                 /* Handle Wait Time Extension */
663                 
664                 if (!check_cid(th, &xcvb))
665                         goto out;
666
667                 if (xcvb.rx.data[0] & TCL_PCB_CID_FOLLOWING) {
668                         if (xcvb.rx.frame_len < 3) {
669                                 DEBUGP("S-Block with CID but short len\n");
670                                 ret = -1;
671                                 goto out;
672                         }
673                         inf = xcvb.rx.data[2];
674                 } else
675                         inf = xcvb.rx.data[1];
676
677                 if ((xcvb.rx.data[0] & 0x30) != 0x30) {
678                         DEBUGP("S-Block but not WTX?\n");
679                         ret = -1;
680                         goto out;
681                 }
682                 inf &= 0x3f;    /* only lower 6 bits code WTXM */
683                 if (inf == 0 || (inf >= 60 && inf <= 63)) {
684                         DEBUGP("WTXM %u is RFU!\n", inf);
685                         ret = -1;
686                         goto out;
687                 }
688                 
689                 fill_xcvb_wtxm(th, &xcvb, inf);
690                 /* start over with next transceive */
691                 goto do_tx; 
692         } else if (is_i_block(xcvb.rx.data[0])) {
693                 unsigned int net_payload_len;
694                 /* we're actually receiving payload data */
695
696                 DEBUGP("I-Block: ");
697
698                 if ((xcvb.rx.data[0] & 0x01) != h->priv.tcl.toggle) {
699                         DEBUGP("response with wrong toggle bit\n");
700                         goto out;
701                 }
702
703                 xcvb.rx.hdr_len = 1;
704
705                 if (!check_cid(th, &xcvb))
706                         goto out;
707
708                 if (xcvb.rx.data[0] & TCL_PCB_CID_FOLLOWING)
709                         xcvb.rx.hdr_len++;
710                 if (xcvb.rx.data[0] & TCL_PCB_NAD_FOLLOWING)
711                         xcvb.rx.hdr_len++;
712         
713                 net_payload_len = xcvb.rx.frame_len - xcvb.rx.hdr_len;
714                 DEBUGPC("%u bytes\n", net_payload_len);
715                 memcpy(tcl_ctx.next_rx_byte, &xcvb.rx.data[xcvb.rx.hdr_len], 
716                         net_payload_len);
717                 tcl_ctx.next_rx_byte += net_payload_len;
718
719                 if (xcvb.rx.data[0] & 0x10) {
720                         /* we're not the last frame in the chain, continue rx */
721                         DEBUGP("not the last frame in the chain, continue\n");
722                         ack_len = sizeof(ack);
723                         tcl_build_prologue_r(th, xcvb.tx.data, &xcvb.tx.frame_len,                                                0);
724                         xcvb.timeout = th->fwt;
725                         goto do_tx;
726                 }
727         }
728
729 out:
730         *rx_len = tcl_ctx.next_rx_byte - tcl_ctx.rx;
731         return ret;
732 }
733
734 static struct rfid_protocol_handle *
735 tcl_init(struct rfid_layer2_handle *l2h)
736 {
737         struct rfid_protocol_handle *th;
738         unsigned int mru = l2h->rh->ah->mru;
739
740         th = malloc_protocol_handle(sizeof(struct rfid_protocol_handle));
741         if (!th)
742                 return NULL;
743
744         /* FIXME: mru should be attribute of layer2 (in case it adds/removes
745          * some overhead */
746         memset(th, 0, sizeof(struct rfid_protocol_handle));
747
748         /* maximum received ats length equals mru of asic/reader */
749         th->priv.tcl.state = TCL_STATE_INITIAL;
750         th->priv.tcl.ats_len = mru;
751         th->priv.tcl.toggle = 1;
752
753         th->priv.tcl.fsd = iso14443_fsd_approx(mru);
754
755         return th;
756 }
757
758 static int
759 tcl_fini(struct rfid_protocol_handle *ph)
760 {
761         free_protocol_handle(ph);
762         return 0;
763 }
764
765 const struct rfid_protocol rfid_protocol_tcl = {
766         .id     = RFID_PROTOCOL_TCL,
767         .name   = "ISO 14443-4 / T=CL",
768         .fn     = {
769                 .init = &tcl_init,
770                 .open = &tcl_connect,
771                 .transceive = &tcl_transceive,
772                 .close = &tcl_deselect,
773                 .fini = &tcl_fini,
774         },
775 };