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