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