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