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