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