1 /* ISO 14443-4 (T=CL) implementation, PCD side.
3 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
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
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.
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
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>
34 #include <rfid/rfid_asic.h>
35 #include <rfid/rfid_reader.h>
37 #include "rfid_iso14443_common.h"
40 static unsigned int sfgi_to_sfgt(struct rfid_protocol_handle *h,
43 unsigned int multiplier;
49 multiplier = 1 << sfgi; /* 2 to the power of sfgi */
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;
55 return (tmp / h->l2h->rh->ah->fc) * multiplier;
58 static unsigned int fwi_to_fwt(struct rfid_protocol_handle *h,
61 unsigned int multiplier, tmp;
66 multiplier = 1 << fwi; /* 2 to the power of fwi */
68 /* ISO 14443-4:2000(E) Section 7.2.:
69 * (256*16 / h->l2h->rh->ah->fc) * (2 ^ fwi) */
71 tmp = (unsigned int) 1000000 * 256 * 16;
73 return (tmp / h->l2h->rh->ah->fc) * multiplier;
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)
81 tcl_parse_ats(struct rfid_protocol_handle *h,
82 unsigned char *ats, unsigned int size)
84 unsigned char len = ats[0];
88 if (len == 0 || size == 0)
97 /* FIXME: assume some default values */
99 h->priv.tcl.ta = 0x80; /* 0x80 (same d for both dirs) */
100 h->priv.tcl.sfgt = sfgi_to_sfgt(h, 0);
101 if (h->l2h->l2->id == RFID_LAYER2_ISO14443A) {
102 /* Section 7.2: fwi default for type A is 4 */
103 h->priv.tcl.fwt = fwi_to_fwt(h, 4);
105 /* Section 7.2: fwi for type B is always in ATQB */
106 /* Value is assigned in tcl_connect() */
107 /* This function is never called for Type B, since it has no (R)ATS */
112 /* guarateed to be at least 2 bytes in size */
117 iso14443_fsdi_to_fsd(&h->priv.tcl.fsc, t0 & 0x0f);
120 /* TA is transmitted */
121 h->priv.tcl.ta = *cur++;
125 /* TB is transmitted */
126 h->priv.tcl.sfgt = sfgi_to_sfgt(h, *cur & 0x0f);
127 h->priv.tcl.fwt = fwi_to_fwt(h, (*cur & 0xf0) >> 4);
132 /* TC is transmitted */
134 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
136 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
140 h->priv.tcl.historical_len = (ats+len) - cur;
141 h->priv.tcl.historical_bytes = cur;
147 /* request an ATS from the PICC */
149 tcl_request_ats(struct rfid_protocol_handle *h)
152 unsigned char rats[2];
155 if (h->priv.tcl.state != TCL_STATE_INITIAL)
158 ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.tcl.fsd);
160 DEBUGP("unable to encode FSD of %u as FSDI\n", h->priv.tcl.fsd);
165 rats[1] = (h->priv.tcl.cid & 0x0f) | ((fsdi << 4) & 0xf0);
167 /* transcieve (with CRC) */
168 ret = h->l2h->l2->fn.transcieve(h->l2h, RFID_14443A_FRAME_REGULAR,
169 rats, 2, h->priv.tcl.ats,
170 &h->priv.tcl.ats_len, activation_fwt(h),
171 TCL_TRANSP_F_TX_CRC);
173 DEBUGP("transcieve of rats failed\n");
174 h->priv.tcl.state = TCL_STATE_RATS_SENT;
175 /* FIXME: retransmit */
178 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
180 ret = tcl_parse_ats(h, h->priv.tcl.ats, h->priv.tcl.ats_len);
182 DEBUGP("parsing of ats failed\n");
189 #define ATS_TA_DIV_2 1
190 #define ATS_TA_DIV_4 2
191 #define ATS_TA_DIV_8 4
197 static unsigned char d_to_di(struct rfid_protocol_handle *h, unsigned char D)
200 unsigned int speed = h->l2h->rh->reader->iso14443a.speed;
202 if ((D & ATS_TA_DIV_8) && (speed & RFID_READER_SPEED_848K))
204 else if ((D & ATS_TA_DIV_4) && (speed & RFID_READER_SPEED_424K))
206 else if ((D & ATS_TA_DIV_2) && (speed & RFID_READER_SPEED_212K))
215 /* start a PSS run (autimatically configure highest possible speed */
217 tcl_do_pps(struct rfid_protocol_handle *h)
221 unsigned char ppss[3];
222 unsigned char pps_response[1];
223 unsigned int rx_len = 1;
224 unsigned char Dr, Ds, DrI, DsI;
226 if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
229 Dr = h->priv.tcl.ta & 0x07;
230 Ds = h->priv.tcl.ta & 0x70 >> 4;
232 if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
233 /* device supports different divisors for rx and tx, but not ?!? */
234 DEBUGP("PICC has contradictory TA, aborting PPS\n");
238 /* ISO 14443-4:2000(E) Section 5.3. */
240 ppss[0] = 0xd0 & (h->priv.tcl.cid & 0x0f);
243 /* FIXME: deal with different speed for each direction */
244 DrI = d_to_di(h, Dr);
245 DsI = d_to_di(h, Ds);
247 ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
249 ret = h->l2h->l2->fn.transcieve(h->l2h, ppss, 3, pps_response,
250 &rx_len, h->priv.tcl.fwt,
251 TCL_TRANSP_F_TX_CRC);
255 if (pps_response[0] != ppss[0]) {
256 DEBUGP("PPS Response != PPSS\n");
260 h->priv.tcl.state = TCL_STATE_ESTABLISHED;
267 tcl_build_prologue2(struct tcl_handle *th,
268 unsigned char *prlg, unsigned int *prlg_len,
276 /* we've sent a toggle bit last time */
279 /* we've not sent a toggle last time: send one */
284 if (th->flags & TCL_HANDLE_F_CID_USED) {
285 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
286 *prlg |= TCL_PCB_CID_FOLLOWING;
288 prlg[*prlg_len] = th->cid & 0x0f;
291 /* nad only for I-block (0xc0 == 00) */
292 if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
293 ((pcb & 0xc0) == 0x00)) {
294 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
295 /* FIXME: in case of chaining only for first frame */
296 *prlg |= TCL_PCB_NAD_FOLLOWING;
297 prlg[*prlg_len] = th->nad;
305 tcl_build_prologue_i(struct tcl_handle *th,
306 unsigned char *prlg, unsigned int *prlg_len)
308 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
309 return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
313 tcl_build_prologue_r(struct tcl_handle *th,
314 unsigned char *prlg, unsigned int *prlg_len,
317 unsigned char pcb = 0xa2;
318 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
323 return tcl_build_prologue2(th, prlg, prlg_len, pcb);
327 tcl_build_prologue_s(struct tcl_handle *th,
328 unsigned char *prlg, unsigned int *prlg_len)
330 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
332 /* the only S-block from PCD->PICC is DESELECT,
333 * well, actually there is the S(WTX) response. */
334 return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
337 /* FIXME: WTXM implementation */
339 static int tcl_prlg_len(struct tcl_handle *th)
343 if (th->flags & TCL_HANDLE_F_CID_USED)
346 if (th->flags & TCL_HANDLE_F_NAD_USED)
352 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
355 tcl_connect(struct rfid_protocol_handle *h)
359 if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
360 h->priv.tcl.state != TCL_STATE_INITIAL)
363 switch (h->l2h->l2->id) {
364 case RFID_LAYER2_ISO14443A:
365 /* Start Type A T=CL Activation Sequence */
366 ret = tcl_request_ats(h);
370 /* Only do PPS if any non-default divisors supported */
371 if (h->priv.tcl.ta & 0x77) {
377 case RFID_LAYER2_ISO14443B:
378 /* initialized T=CL state from Type B Activation Data */
379 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
380 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
381 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
382 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
384 /* what about ta? sfgt? */
386 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
387 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
388 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
389 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
391 switch (h->l2h->priv.iso14443b.state) {
392 case ISO14443B_STATE_SELECTED:
393 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
395 case ISO14443B_STATE_ATTRIB_SENT:
396 h->priv.tcl.state = TCL_STATE_RATS_SENT;
400 /* PUPI will be presented as ATS/historical bytes */
401 memcpy(h->priv.tcl.ats, h->l2h->uid, 4);
402 h->priv.tcl.ats_len = 4;
403 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
407 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
416 tcl_deselect(struct rfid_protocol_handle *h)
418 /* ISO 14443-4:2000(E) Section 8 */
420 unsigned char frame[3]; /* 3 bytes prologue, no information */
422 unsigned int rx_len = sizeof(rx);
423 unsigned int prlg_len;
424 struct tcl_handle *th = &h->priv.tcl;
426 if (th->state != TCL_STATE_ESTABLISHED) {
427 /* FIXME: not sure whether deselect is possible here,
428 * probably better send a HLTA? */
431 /* build DESELECT S-block */
432 ret = tcl_build_prologue_s(th, frame, &prlg_len);
436 ret = h->l2h->l2->fn.transcieve(h->l2h, RFID_14443A_FRAME_REGULAR,
438 &rx_len, deactivation_fwt(h),
439 TCL_TRANSP_F_TX_CRC);
441 /* FIXME: retransmit, HLT(A|B) */
445 th->state = TCL_STATE_DESELECTED;
450 #define is_s_block(x) ((x & 0xc0) == 0xc0)
451 #define is_r_block(x) ((x & 0xc0) == 0x80)
452 #define is_i_block(x) ((x & 0xc0) == 0x00)
455 tcl_transcieve(struct rfid_protocol_handle *h,
456 const unsigned char *tx_data, unsigned int tx_len,
457 unsigned char *rx_data, unsigned int *rx_len,
458 unsigned int timeout, unsigned int flags)
461 unsigned char *tx_buf, *rx_buf;
462 unsigned char *_rx_data = rx_data;
463 unsigned int _rx_len;
464 unsigned int max_rx_len = *rx_len; /* maximum number of payoload that
465 caller has requested */
466 unsigned int prlg_len;
467 struct tcl_handle *th = &h->priv.tcl;
470 unsigned int _tx_len, _timeout;
471 unsigned char wtx_resp[3];
472 unsigned char ack[10];
473 unsigned int ack_len;
475 if (tx_len > max_net_tx_framesize(th)) {
476 /* slow path: we need to use chaining */
480 tx_buf = malloc(tcl_prlg_len(th) + tx_len);
485 rx_buf = malloc(tcl_prlg_len(th) + *rx_len);
491 if (tcl_build_prologue_i(th, tx_buf, &prlg_len) < 0) {
495 memcpy(tx_buf + prlg_len, tx_data, tx_len);
497 /* intialize to data-to-be-transferred */
499 _tx_len = tx_len+prlg_len;
505 ret = h->l2h->l2->fn.transcieve(h->l2h, RFID_14443A_FRAME_REGULAR,
507 rx_buf, &_rx_len, _timeout, 0);
508 DEBUGP("l2 transcieve finished\n");
513 _rx_len -= 2; /* CRC is not removed by ASIC ?!? */
515 if ((*rx_buf & 0x01) != h->priv.tcl.toggle) {
516 DEBUGP("response with wrong toggle bit\n");
520 if (is_r_block(*rx_buf)) {
521 unsigned int txed = _tx - tx_buf;
523 /* Handle ACK frame in case of chaining */
524 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
525 if (*(rx_buf+1) != h->priv.tcl.cid) {
526 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
530 /* set up parameters for next frame in chain */
532 /* move tx pointer by the amount of bytes transferred
535 _tx_len = (tx_len - txed);
536 if (_tx_len > max_net_tx_framesize(th)) {
537 /* not last frame in chain */
538 _tx_len = max_net_tx_framesize(th);
540 /* last frame in chain */
544 DEBUGP("Received ACK in response to last frame in "
545 "chain?!? Expected I-frame.\n");
549 } else if (is_s_block(*rx_buf)) {
551 unsigned int prlg_len;
554 /* Handle Wait Time Extension */
555 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
557 DEBUGP("S-Block with CID but short len\n");
561 if (*(rx_buf+1) != h->priv.tcl.cid) {
562 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
569 if ((*rx_buf & 0x30) != 0x30) {
570 DEBUGP("S-Block but not WTX?\n");
574 inf &= 0x3f; /* only lower 6 bits code WTXM */
575 if (inf == 0 || (inf >= 60 && inf <= 63)) {
576 DEBUGP("WTXM %u is RFU!\n", inf);
581 /* Acknowledge WTXM */
582 tcl_build_prologue_s(&h->priv.tcl, wtx_resp, &prlg_len);
583 /* set two bits that make this block a wtx */
585 wtx_resp[prlg_len] = inf;
587 _tx_len = prlg_len+1;
588 _timeout = th->fwt * inf;
590 /* start over with next transcieve */
591 goto do_tx; /* FIXME: do transcieve locally since we use
592 totally different buffer */
594 } else if (is_i_block(*rx_buf)) {
595 unsigned char *inf = rx_buf+1;
596 unsigned int net_payload_len;
597 /* we're actually receiving payload data */
600 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
601 if (*(rx_buf+1) != h->priv.tcl.cid) {
602 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
607 if (*rx_buf & TCL_PCB_NAD_FOLLOWING) {
610 net_payload_len = _rx_len - (inf - rx_buf);
611 memcpy(_rx_data, inf, net_payload_len);
612 /* increment the number of payload bytes that we actually received */
613 *rx_len += net_payload_len;
614 _rx_data += net_payload_len;
616 if (*rx_buf & 0x10) {
617 /* we're not the last frame in the chain, continue rx */
618 DEBUGP("we're not the last frame in the chain, continue\n");
619 ack_len = sizeof(ack);
620 tcl_build_prologue_r(&h->priv.tcl, ack, &ack_len, 0);
637 tcl_send(struct tcl_handle *th)
649 static struct rfid_protocol_handle *
650 tcl_init(struct rfid_layer2_handle *l2h)
652 struct rfid_protocol_handle *th;
653 unsigned int mru = l2h->rh->ah->mru;
655 th = malloc(sizeof(struct rfid_protocol_handle) + mru);
659 /* FIXME: mru should be attribute of layer2 (in case it adds/removes
661 memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
663 /* maximum received ats length equals mru of asic/reader */
664 th->priv.tcl.state = TCL_STATE_INITIAL;
665 th->priv.tcl.ats_len = mru;
666 th->priv.tcl.toggle = 1;
668 th->priv.tcl.fsd = iso14443_fsd_approx(mru);
674 tcl_fini(struct rfid_protocol_handle *ph)
680 struct rfid_protocol rfid_protocol_tcl = {
681 .id = RFID_PROTOCOL_TCL,
682 .name = "ISO 14443-4 / T=CL",
685 .open = &tcl_connect,
686 .transcieve = &tcl_transcieve,
687 .close = &tcl_deselect,