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