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