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