respect MTU indicated by low-level driver.
[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 static enum rfid_frametype l2_to_frame(unsigned int layer2)
40 {
41         switch (layer2) {
42                 case RFID_LAYER2_ISO14443A:
43                         return RFID_14443A_FRAME_REGULAR;
44                         break;
45                 case RFID_LAYER2_ISO14443B:
46                         return RFID_14443B_FRAME_REGULAR;
47                         break;
48         }
49         return 0;
50 }
51
52 static unsigned int sfgi_to_sfgt(struct rfid_protocol_handle *h, 
53                                  unsigned char sfgi)
54 {
55         unsigned int multiplier;
56         unsigned int tmp;
57
58         if (sfgi > 14)
59                 sfgi = 14;
60
61         multiplier = 1 << sfgi; /* 2 to the power of sfgi */
62
63         /* ISO 14443-4:2000(E) Section 5.2.5:
64          * (256 * 16 / h->l2h->rh->ah->fc) * (2 ^ sfgi) */
65         tmp = (unsigned int) 1000000 * 256 * 16;
66
67         return (tmp / h->l2h->rh->ah->fc) * multiplier;
68 }
69
70 static unsigned int fwi_to_fwt(struct rfid_protocol_handle *h, 
71                                 unsigned char fwi)
72 {
73         unsigned int multiplier, tmp;
74
75         if (fwi > 14)
76                 fwi = 14;
77
78         multiplier  = 1 << fwi; /* 2 to the power of fwi */
79
80         /* ISO 14443-4:2000(E) Section 7.2.:
81          * (256*16 / h->l2h->rh->ah->fc) * (2 ^ fwi) */
82
83         tmp = (unsigned int) 1000000 * 256 * 16;
84
85         return (tmp / h->l2h->rh->ah->fc) * multiplier;
86 }
87
88 /* 4.9seconds as microseconds (4.9 billion seconds) exceeds 2^32 */
89 #define activation_fwt(x) (((u_int64_t)1000000 * 65536 / x->l2h->rh->ah->fc))
90 #define deactivation_fwt(x) activation_fwt(x)
91
92 static int
93 tcl_parse_ats(struct rfid_protocol_handle *h, 
94                 unsigned char *ats, unsigned int size)
95 {
96         unsigned char len = ats[0];
97         unsigned char t0;
98         unsigned char *cur;
99
100         if (len == 0 || size == 0) 
101                 return -1;
102
103         if (size < len)
104                 len = size;
105
106         h->priv.tcl.ta = 0;
107
108         if (len == 1) {
109                 /* FIXME: assume some default values */
110                 h->priv.tcl.fsc = 32;
111                 h->priv.tcl.ta = 0x80;  /* 0x80 (same d for both dirs) */
112                 h->priv.tcl.sfgt = sfgi_to_sfgt(h, 0);
113                 if (h->l2h->l2->id == RFID_LAYER2_ISO14443A) {
114                         /* Section 7.2: fwi default for type A is 4 */
115                         h->priv.tcl.fwt = fwi_to_fwt(h, 4);
116                 } else {
117                         /* Section 7.2: fwi for type B is always in ATQB */
118                         /* Value is assigned in tcl_connect() */
119                         /* This function is never called for Type B, since it has no (R)ATS */
120                 }
121                 return 0;
122         }
123
124         /* guarateed to be at least 2 bytes in size */
125
126         t0 = ats[1];
127         cur = &ats[2];
128
129         iso14443_fsdi_to_fsd(&h->priv.tcl.fsc, t0 & 0x0f);
130         if (h->priv.tcl.fsc > h->l2h->rh->ah->mtu)
131                 h->priv.tcl.fsc = h->l2h->rh->ah->mtu;
132
133         if (t0 & (1 << 4)) {
134                 /* TA is transmitted */
135                 h->priv.tcl.ta = *cur++;
136         }
137
138         if (t0 & (1 << 5)) {
139                 /* TB is transmitted */
140                 h->priv.tcl.sfgt = sfgi_to_sfgt(h, *cur & 0x0f);
141                 h->priv.tcl.fwt = fwi_to_fwt(h, (*cur & 0xf0) >> 4);
142                 cur++;
143         }
144
145         if (t0 & (1 << 6)) {
146                 /* TC is transmitted */
147                 if (*cur & 0x01)
148                         h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
149                 if (*cur & 0x02)
150                         h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
151                 cur++;
152         }
153
154         h->priv.tcl.historical_len = (ats+len) - cur;
155         h->priv.tcl.historical_bytes = cur;
156
157         return 0;
158 }
159
160
161 /* request an ATS from the PICC */
162 static int
163 tcl_request_ats(struct rfid_protocol_handle *h)
164 {
165         int ret;
166         unsigned char rats[2];
167         unsigned char fsdi;
168
169         if (h->priv.tcl.state != TCL_STATE_INITIAL)
170                 return -1;
171
172         ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.tcl.fsd);
173         if (ret < 0) {
174                 DEBUGP("unable to encode FSD of %u as FSDI\n", h->priv.tcl.fsd);
175                 return ret;
176         }
177
178         rats[0] = 0xe0;
179         rats[1] = (h->priv.tcl.cid & 0x0f) | ((fsdi << 4) & 0xf0);
180
181         /* transceive (with CRC) */
182         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
183                                      rats, 2, h->priv.tcl.ats,
184                                      &h->priv.tcl.ats_len, activation_fwt(h),
185                                      TCL_TRANSP_F_TX_CRC);
186         if (ret < 0) {
187                 DEBUGP("transceive of rats failed\n");
188                 h->priv.tcl.state = TCL_STATE_RATS_SENT;
189                 /* FIXME: retransmit */
190                 return ret;
191         }
192         h->priv.tcl.state = TCL_STATE_ATS_RCVD;
193
194         ret = tcl_parse_ats(h, h->priv.tcl.ats, h->priv.tcl.ats_len);
195         if (ret < 0) {
196                 DEBUGP("parsing of ats failed\n");
197                 return ret;
198         }
199
200         return 0;
201 }
202
203 #define ATS_TA_DIV_2    1
204 #define ATS_TA_DIV_4    2
205 #define ATS_TA_DIV_8    4
206
207 #define PPS_DIV_8       3
208 #define PPS_DIV_4       2
209 #define PPS_DIV_2       1
210 #define PPS_DIV_1       0
211 static unsigned char d_to_di(struct rfid_protocol_handle *h, unsigned char D)
212 {
213         static char DI;
214         unsigned int speed = h->l2h->rh->reader->iso14443a.speed;
215         
216         if ((D & ATS_TA_DIV_8) && (speed & RFID_14443A_SPEED_848K))
217                 DI = PPS_DIV_8;
218         else if ((D & ATS_TA_DIV_4) && (speed & RFID_14443A_SPEED_424K))
219                 DI = PPS_DIV_4;
220         else if ((D & ATS_TA_DIV_2) && (speed & RFID_14443A_SPEED_212K))
221                 DI = PPS_DIV_2;
222         else
223                 DI = PPS_DIV_1;
224
225         return DI;
226 }
227
228 static unsigned int di_to_speed(unsigned char DI)
229 {
230         switch (DI) {
231         case PPS_DIV_8:
232                 return RFID_14443A_SPEED_848K;
233                 break;
234         case PPS_DIV_4:
235                 return RFID_14443A_SPEED_424K;
236                 break;
237         case PPS_DIV_2:
238                 return RFID_14443A_SPEED_212K;
239                 break;
240         case PPS_DIV_1:
241                 return RFID_14443A_SPEED_106K;
242                 break;
243         }
244 }
245
246 /* start a PPS run (autimatically configure highest possible speed */
247 static int 
248 tcl_do_pps(struct rfid_protocol_handle *h)
249 {
250         int ret;
251         unsigned char ppss[3];
252         unsigned char pps_response[1];
253         unsigned int rx_len = 1;
254         unsigned char Dr, Ds, DrI, DsI;
255         unsigned int speed;
256
257         if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
258                 return -1;
259
260         Dr = h->priv.tcl.ta & 0x07;
261         Ds = h->priv.tcl.ta & 0x70 >> 4;
262         DEBUGP("Dr = 0x%x, Ds = 0x%x\n", Dr, Ds);
263
264         if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
265                 /* device supports different divisors for rx and tx, but not
266                  * really ?!? */
267                 DEBUGP("PICC has contradictory TA, aborting PPS\n");
268                 return -1;
269         };
270
271         /* ISO 14443-4:2000(E) Section 5.3. */
272
273         ppss[0] = 0xd0 | (h->priv.tcl.cid & 0x0f);
274         ppss[1] = 0x11;
275         ppss[2] = 0x00;
276
277         /* FIXME: deal with different speed for each direction */
278         DrI = d_to_di(h, Dr);
279         DsI = d_to_di(h, Ds);
280         DEBUGP("DrI = 0x%x, DsI = 0x%x\n", DrI, DsI);
281
282         ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
283
284         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
285                                         ppss, 3, pps_response, &rx_len,
286                                         h->priv.tcl.fwt, TCL_TRANSP_F_TX_CRC);
287         if (ret < 0)
288                 return ret;
289
290         if (pps_response[0] != ppss[0]) {
291                 DEBUGP("PPS Response != PPSS\n");
292                 return -1;
293         }
294
295         speed = di_to_speed(DrI);
296
297         ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_RX,
298                                  &speed, sizeof(speed));
299         if (ret < 0)
300                 return ret;
301
302         ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_TX,
303                                  &speed, sizeof(speed));
304         if (ret < 0)
305                 return ret;
306         
307         return 0;
308 }
309
310
311 static int
312 tcl_build_prologue2(struct tcl_handle *th, 
313                     unsigned char *prlg, unsigned int *prlg_len, 
314                     unsigned char pcb)
315 {
316         *prlg_len = 1;
317
318         *prlg = pcb;
319
320         if (th->toggle) {
321                 /* we've sent a toggle bit last time */
322                 th->toggle = 0;
323         } else {
324                 /* we've not sent a toggle last time: send one */
325                 th->toggle = 1;
326                 *prlg |= 0x01;
327         }
328
329         if (th->flags & TCL_HANDLE_F_CID_USED) {
330                 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
331                 *prlg |= TCL_PCB_CID_FOLLOWING;
332                 (*prlg_len)++;
333                 prlg[*prlg_len] = th->cid & 0x0f;
334         }
335
336         /* nad only for I-block (0xc0 == 00) */
337         if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
338             ((pcb & 0xc0) == 0x00)) {
339                 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
340                 /* FIXME: in case of chaining only for first frame */
341                 *prlg |= TCL_PCB_NAD_FOLLOWING;
342                 prlg[*prlg_len] = th->nad;
343                 (*prlg_len)++;
344         }
345
346         return 0;
347 }
348
349 static int
350 tcl_build_prologue_i(struct tcl_handle *th,
351                      unsigned char *prlg, unsigned int *prlg_len)
352 {
353         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
354         return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
355 }
356
357 static int
358 tcl_build_prologue_r(struct tcl_handle *th,
359                      unsigned char *prlg, unsigned int *prlg_len,
360                      unsigned int nak)
361 {
362         unsigned char pcb = 0xa2;
363         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
364
365         if (nak)
366                 pcb |= 0x10;
367
368         return tcl_build_prologue2(th, prlg, prlg_len, pcb);
369 }
370
371 static int
372 tcl_build_prologue_s(struct tcl_handle *th,
373                      unsigned char *prlg, unsigned int *prlg_len)
374 {
375         /* ISO 14443-4:2000(E) Section 7.1.1.1 */
376
377         /* the only S-block from PCD->PICC is DESELECT,
378          * well, actually there is the S(WTX) response. */
379         return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
380 }
381
382 /* FIXME: WTXM implementation */
383
384 static int tcl_prlg_len(struct tcl_handle *th)
385 {
386         int prlg_len = 1;
387
388         if (th->flags & TCL_HANDLE_F_CID_USED)
389                 prlg_len++;
390
391         if (th->flags & TCL_HANDLE_F_NAD_USED)
392                 prlg_len++;
393
394         return prlg_len;
395 }
396
397 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
398
399 static int
400 tcl_connect(struct rfid_protocol_handle *h)
401 {
402         int ret; 
403
404         if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
405             h->priv.tcl.state != TCL_STATE_INITIAL)
406                 return -1;
407
408         switch (h->l2h->l2->id) {
409         case RFID_LAYER2_ISO14443A:
410                 /* Start Type A T=CL Activation Sequence */
411                 ret = tcl_request_ats(h);
412                 if (ret < 0)
413                         return ret;
414
415                 /* Only do PPS if any non-default divisors supported */
416                 if (h->priv.tcl.ta & 0x77) {
417                         ret = tcl_do_pps(h);
418                         if (ret < 0)
419                                 return ret;
420                 }
421                 break;
422         case RFID_LAYER2_ISO14443B:
423                 /* initialized T=CL state from Type B Activation Data */
424                 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
425                 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
426                 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
427                 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
428
429                 /* what about ta? sfgt? */
430
431                 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
432                         h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
433                 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
434                         h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
435
436                 switch (h->l2h->priv.iso14443b.state) {
437                         case ISO14443B_STATE_SELECTED:
438                                 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
439                                 break;
440                         case ISO14443B_STATE_ATTRIB_SENT:
441                                 h->priv.tcl.state = TCL_STATE_RATS_SENT;
442                                 break;
443                 }
444
445                 /* PUPI will be presented as ATS/historical bytes */
446                 memcpy(h->priv.tcl.ats, h->l2h->uid, 4);
447                 h->priv.tcl.ats_len = 4;
448                 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
449
450                 break;
451         default:
452                 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
453                 return -1;
454                 break;
455         }
456
457         h->priv.tcl.state = TCL_STATE_ESTABLISHED;
458
459         return 0;
460 }
461
462 static int
463 tcl_deselect(struct rfid_protocol_handle *h)
464 {
465         /* ISO 14443-4:2000(E) Section 8 */
466         int ret;
467         unsigned char frame[3];         /* 3 bytes prologue, no information */
468         unsigned char rx[3];
469         unsigned int rx_len = sizeof(rx);
470         unsigned int prlg_len;
471         struct tcl_handle *th = &h->priv.tcl;
472
473         if (th->state != TCL_STATE_ESTABLISHED) {
474                 /* FIXME: not sure whether deselect is possible here,
475                  * probably better send a HLTA? */
476         }
477
478         /* build DESELECT S-block */
479         ret = tcl_build_prologue_s(th, frame, &prlg_len);
480         if (ret < 0)
481                 return ret;
482
483         ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
484                                      frame, prlg_len, rx,
485                                      &rx_len, deactivation_fwt(h),
486                                      TCL_TRANSP_F_TX_CRC);
487         if (ret < 0) {
488                 /* FIXME: retransmit, HLT(A|B) */
489                 return ret;
490         }
491
492         th->state = TCL_STATE_DESELECTED;
493
494         return 0;
495 }
496
497 #define is_s_block(x) ((x & 0xc0) == 0xc0)
498 #define is_r_block(x) ((x & 0xc0) == 0x80)
499 #define is_i_block(x) ((x & 0xc0) == 0x00)
500
501 static int
502 tcl_transceive(struct rfid_protocol_handle *h,
503                 const unsigned char *tx_data, unsigned int tx_len,
504                 unsigned char *rx_data, unsigned int *rx_len,
505                 unsigned int timeout, unsigned int flags)
506 {
507         int ret;
508         unsigned char *tx_buf, *rx_buf;
509         unsigned char *_rx_data = rx_data;
510         unsigned int _rx_len;
511         unsigned int max_rx_len = *rx_len; /* maximum number of payoload that
512                                               caller has requested */
513         unsigned int prlg_len;
514         struct tcl_handle *th = &h->priv.tcl;
515
516         unsigned char *_tx;
517         unsigned int _tx_len, _timeout;
518         unsigned char wtx_resp[3];
519         unsigned char ack[10];
520         unsigned int ack_len;
521
522         tx_buf = malloc(tcl_prlg_len(th) + tx_len);
523         if (!tx_buf) {
524                 ret = -ENOMEM;
525                 goto out;
526         }
527         rx_buf = malloc(tcl_prlg_len(th) + *rx_len);
528         if (!rx_buf) {
529                 ret = -ENOMEM;
530                 goto out_txb;
531         }
532
533         if (tcl_build_prologue_i(th, tx_buf, &prlg_len) < 0) {
534                 ret = -1;
535                 goto out_rxb;
536         }
537         memcpy(tx_buf + prlg_len, tx_data, tx_len);
538
539         /* intialize to data-to-be-transferred */
540         _tx = tx_buf;
541         _tx_len = tx_len+prlg_len;
542         _timeout = th->fwt;
543         _rx_len = *rx_len;
544         *rx_len = 0;
545
546
547         if (_tx_len > max_net_tx_framesize(th)+prlg_len) {
548                 /* slow path: we need to use chaining */
549                 _tx_len = max_net_tx_framesize(th)+prlg_len;
550         }
551
552 do_tx:
553         ret = rfid_layer2_transceive(h->l2h, l2_to_frame(h->l2h->l2->id),
554                                      _tx, _tx_len,
555                                      rx_buf, &_rx_len, _timeout, 0);
556         DEBUGP("l2 transceive finished\n");
557         if (ret < 0)
558                 goto out_rxb;
559
560         if ((*rx_buf & 0x01) != h->priv.tcl.toggle) {
561                 DEBUGP("response with wrong toggle bit\n");
562                 goto out_rxb;
563         }
564
565         if (is_r_block(*rx_buf)) {
566                 unsigned int txed = _tx - tx_buf;
567                 DEBUGP("R-Block\n");
568                 /* Handle ACK frame in case of chaining */
569                 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
570                         if (*(rx_buf+1) != h->priv.tcl.cid) {
571                                 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
572                                 goto out_rxb;
573                         }
574                 }
575                 /* set up parameters for next frame in chain */
576                 if (txed < tx_len) {
577                         /* move tx pointer by the amount of bytes transferred
578                          * in last frame */
579                         _tx += _tx_len;
580                         _tx_len = (tx_len - txed);
581                         if (_tx_len > max_net_tx_framesize(th)) {
582                                 /* not last frame in chain */
583                                 _tx_len = max_net_tx_framesize(th);
584                         } else {
585                                 /* last frame in chain */
586                         }
587                         goto do_tx;
588                 } else {
589                         DEBUGP("Received ACK in response to last frame in "
590                                "chain?!? Expected I-frame.\n");
591                         ret = -1;
592                         goto out_rxb;
593                 }
594         } else if (is_s_block(*rx_buf)) {
595                 unsigned char inf;
596                 unsigned int prlg_len;
597
598                 DEBUGP("S-Block\n");
599                 /* Handle Wait Time Extension */
600                 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
601                         if (_rx_len < 3) {
602                                 DEBUGP("S-Block with CID but short len\n");
603                                 ret = -1;
604                                 goto out_rxb;
605                         }
606                         if (*(rx_buf+1) != h->priv.tcl.cid) {
607                                 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
608                                 goto out_rxb;
609                         }
610                         inf = *(rx_buf+2);
611                 } else
612                         inf = *(rx_buf+1);
613
614                 if ((*rx_buf & 0x30) != 0x30) {
615                         DEBUGP("S-Block but not WTX?\n");
616                         ret = -1;
617                         goto out_rxb;
618                 }
619                 inf &= 0x3f;    /* only lower 6 bits code WTXM */
620                 if (inf == 0 || (inf >= 60 && inf <= 63)) {
621                         DEBUGP("WTXM %u is RFU!\n", inf);
622                         ret = -1;
623                         goto out_rxb;
624                 }
625                 
626                 /* Acknowledge WTXM */
627                 tcl_build_prologue_s(&h->priv.tcl, wtx_resp, &prlg_len);
628                 /* set two bits that make this block a wtx */
629                 wtx_resp[0] |= 0x30;
630                 wtx_resp[prlg_len] = inf;
631                 _tx = wtx_resp;
632                 _tx_len = prlg_len+1;
633                 _timeout = th->fwt * inf;
634
635                 /* start over with next transceive */
636                 goto do_tx; /* FIXME: do transceive locally since we use
637                                 totally different buffer */
638
639         } else if (is_i_block(*rx_buf)) {
640                 unsigned char *inf = rx_buf+1;
641                 unsigned int net_payload_len;
642                 /* we're actually receiving payload data */
643
644                 DEBUGP("I-Block: ");
645                 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
646                         if (*(rx_buf+1) != h->priv.tcl.cid) {
647                                 DEBUGPC("CID %u is not valid\n", *(rx_buf)+1);
648                                 goto out_rxb;
649                         }
650                         inf++;
651                 }
652                 if (*rx_buf & TCL_PCB_NAD_FOLLOWING) {
653                         inf++;
654                 }
655                 net_payload_len = _rx_len - (inf - rx_buf);
656                 DEBUGPC("%u bytes\n", net_payload_len);
657                 memcpy(_rx_data, inf, net_payload_len);
658                 /* increment the number of payload bytes that we actually
659                  * received */
660                 *rx_len += net_payload_len;
661                 _rx_data += net_payload_len;
662
663                 if (*rx_buf & 0x10) {
664                         /* we're not the last frame in the chain, continue rx */
665                         DEBUGP("not the last frame in the chain, continue\n");
666                         ack_len = sizeof(ack);
667                         tcl_build_prologue_r(&h->priv.tcl, ack, &ack_len, 0);
668                         _tx = ack;
669                         _tx_len = ack_len;
670                         goto do_tx;
671                 }
672         }
673
674 out_rxb:
675         free(rx_buf);
676 out_txb:
677         free(tx_buf);
678 out:
679         return ret;
680 }
681
682 static struct rfid_protocol_handle *
683 tcl_init(struct rfid_layer2_handle *l2h)
684 {
685         struct rfid_protocol_handle *th;
686         unsigned int mru = l2h->rh->ah->mru;
687
688         th = malloc(sizeof(struct rfid_protocol_handle) + mru);
689         if (!th)
690                 return NULL;
691
692         /* FIXME: mru should be attribute of layer2 (in case it adds/removes
693          * some overhead */
694         memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
695
696         /* maximum received ats length equals mru of asic/reader */
697         th->priv.tcl.state = TCL_STATE_INITIAL;
698         th->priv.tcl.ats_len = mru;
699         th->priv.tcl.toggle = 1;
700
701         th->priv.tcl.fsd = iso14443_fsd_approx(mru);
702
703         return th;
704 }
705
706 static int
707 tcl_fini(struct rfid_protocol_handle *ph)
708 {
709         free(ph);
710         return 0;
711 }
712
713 struct rfid_protocol rfid_protocol_tcl = {
714         .id     = RFID_PROTOCOL_TCL,
715         .name   = "ISO 14443-4 / T=CL",
716         .fn     = {
717                 .init = &tcl_init,
718                 .open = &tcl_connect,
719                 .transceive = &tcl_transceive,
720                 .close = &tcl_deselect,
721                 .fini = &tcl_fini,
722         },
723 };