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 <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>
34 #include <librfid/rfid_asic.h>
35 #include <librfid/rfid_reader.h>
37 #include "rfid_iso14443_common.h"
39 #define RFID_MAX_FRAMELEN 256
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)
45 static enum rfid_frametype l2_to_frame(unsigned int layer2)
48 case RFID_LAYER2_ISO14443A:
49 return RFID_14443A_FRAME_REGULAR;
51 case RFID_LAYER2_ISO14443B:
52 return RFID_14443B_FRAME_REGULAR;
58 static unsigned int sfgi_to_sfgt(struct rfid_protocol_handle *h,
61 unsigned int multiplier;
67 multiplier = 1 << sfgi; /* 2 to the power of sfgi */
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;
73 return (tmp / h->l2h->rh->ah->fc) * multiplier;
76 static unsigned int fwi_to_fwt(struct rfid_protocol_handle *h,
79 unsigned int multiplier, tmp;
84 multiplier = 1 << fwi; /* 2 to the power of fwi */
86 /* ISO 14443-4:2000(E) Section 7.2.:
87 * (256*16 / h->l2h->rh->ah->fc) * (2 ^ fwi) */
89 tmp = (unsigned int) 1000000 * 256 * 16;
91 return (tmp / h->l2h->rh->ah->fc) * multiplier;
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)
99 tcl_parse_ats(struct rfid_protocol_handle *h,
100 unsigned char *ats, unsigned int size)
102 unsigned char len = ats[0];
106 if (len == 0 || size == 0)
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);
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, since it has no (R)ATS */
130 /* guarateed to be at least 2 bytes in size */
135 iso14443_fsdi_to_fsd(&h->priv.tcl.fsc, t0 & 0x0f);
136 if (h->priv.tcl.fsc > h->l2h->rh->ah->mtu)
137 h->priv.tcl.fsc = h->l2h->rh->ah->mtu;
140 /* TA is transmitted */
141 h->priv.tcl.ta = *cur++;
145 /* TB is transmitted */
146 h->priv.tcl.sfgt = sfgi_to_sfgt(h, *cur & 0x0f);
147 h->priv.tcl.fwt = fwi_to_fwt(h, (*cur & 0xf0) >> 4);
152 /* TC is transmitted */
154 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
155 DEBUGP("This PICC supports NAD\n");
158 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
159 DEBUGP("This PICC supports CID\n");
164 h->priv.tcl.historical_len = (ats+len) - cur;
165 h->priv.tcl.historical_bytes = cur;
171 /* request an ATS from the PICC */
173 tcl_request_ats(struct rfid_protocol_handle *h)
176 unsigned char rats[2];
179 if (h->priv.tcl.state != TCL_STATE_INITIAL)
182 ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.tcl.fsd);
184 DEBUGP("unable to encode FSD of %u as FSDI\n", h->priv.tcl.fsd);
189 rats[1] = (h->priv.tcl.cid & 0x0f) | ((fsdi << 4) & 0xf0);
191 /* transceive (with CRC) */
192 ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
193 rats, 2, h->priv.tcl.ats,
194 &h->priv.tcl.ats_len, activation_fwt(h),
195 TCL_TRANSP_F_TX_CRC);
197 DEBUGP("transceive of rats failed\n");
198 h->priv.tcl.state = TCL_STATE_RATS_SENT;
199 /* FIXME: retransmit */
202 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
204 ret = tcl_parse_ats(h, h->priv.tcl.ats, h->priv.tcl.ats_len);
206 DEBUGP("parsing of ats failed\n");
213 #define ATS_TA_DIV_2 1
214 #define ATS_TA_DIV_4 2
215 #define ATS_TA_DIV_8 4
221 static unsigned char d_to_di(struct rfid_protocol_handle *h, unsigned char D)
224 unsigned int speed = h->l2h->rh->reader->iso14443a.speed;
226 if ((D & ATS_TA_DIV_8) && (speed & RFID_14443A_SPEED_848K))
228 else if ((D & ATS_TA_DIV_4) && (speed & RFID_14443A_SPEED_424K))
230 else if ((D & ATS_TA_DIV_2) && (speed & RFID_14443A_SPEED_212K))
238 static unsigned int di_to_speed(unsigned char DI)
242 return RFID_14443A_SPEED_848K;
245 return RFID_14443A_SPEED_424K;
248 return RFID_14443A_SPEED_212K;
251 return RFID_14443A_SPEED_106K;
256 /* start a PPS run (autimatically configure highest possible speed */
258 tcl_do_pps(struct rfid_protocol_handle *h)
261 unsigned char ppss[3];
262 unsigned char pps_response[1];
263 unsigned int rx_len = 1;
264 unsigned char Dr, Ds, DrI, DsI;
267 if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
270 Dr = h->priv.tcl.ta & 0x07;
271 Ds = h->priv.tcl.ta & 0x70 >> 4;
272 DEBUGP("Dr = 0x%x, Ds = 0x%x\n", Dr, Ds);
274 if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
275 /* device supports different divisors for rx and tx, but not
277 DEBUGP("PICC has contradictory TA, aborting PPS\n");
281 /* ISO 14443-4:2000(E) Section 5.3. */
283 ppss[0] = 0xd0 | (h->priv.tcl.cid & 0x0f);
287 /* FIXME: deal with different speed for each direction */
288 DrI = d_to_di(h, Dr);
289 DsI = d_to_di(h, Ds);
290 DEBUGP("DrI = 0x%x, DsI = 0x%x\n", DrI, DsI);
292 ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
294 ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
295 ppss, 3, pps_response, &rx_len,
296 h->priv.tcl.fwt, TCL_TRANSP_F_TX_CRC);
300 if (pps_response[0] != ppss[0]) {
301 DEBUGP("PPS Response != PPSS\n");
305 speed = di_to_speed(DrI);
307 ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_RX,
308 &speed, sizeof(speed));
312 ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_TX,
313 &speed, sizeof(speed));
322 tcl_build_prologue2(struct tcl_handle *th,
323 unsigned char *prlg, unsigned int *prlg_len,
330 if (!is_s_block(pcb)) {
332 /* we've sent a toggle bit last time */
335 /* we've not sent a toggle last time: send one */
341 if (th->flags & TCL_HANDLE_F_CID_USED) {
342 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
343 *prlg |= TCL_PCB_CID_FOLLOWING;
345 prlg[*prlg_len] = th->cid & 0x0f;
348 /* nad only for I-block */
349 if ((th->flags & TCL_HANDLE_F_NAD_USED) && is_i_block(pcb)) {
350 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
351 /* FIXME: in case of chaining only for first frame */
352 *prlg |= TCL_PCB_NAD_FOLLOWING;
353 prlg[*prlg_len] = th->nad;
361 tcl_build_prologue_i(struct tcl_handle *th,
362 unsigned char *prlg, unsigned int *prlg_len)
364 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
365 return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
369 tcl_build_prologue_r(struct tcl_handle *th,
370 unsigned char *prlg, unsigned int *prlg_len,
373 unsigned char pcb = 0xa2;
374 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
379 return tcl_build_prologue2(th, prlg, prlg_len, pcb);
383 tcl_build_prologue_s(struct tcl_handle *th,
384 unsigned char *prlg, unsigned int *prlg_len)
386 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
388 /* the only S-block from PCD->PICC is DESELECT,
389 * well, actually there is the S(WTX) response. */
390 return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
393 /* FIXME: WTXM implementation */
395 static int tcl_prlg_len(struct tcl_handle *th)
399 if (th->flags & TCL_HANDLE_F_CID_USED)
402 if (th->flags & TCL_HANDLE_F_NAD_USED)
408 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
411 tcl_connect(struct rfid_protocol_handle *h)
415 if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
416 h->priv.tcl.state != TCL_STATE_INITIAL)
419 switch (h->l2h->l2->id) {
420 case RFID_LAYER2_ISO14443A:
421 /* Start Type A T=CL Activation Sequence */
422 ret = tcl_request_ats(h);
426 /* Only do PPS if any non-default divisors supported */
427 if (h->priv.tcl.ta & 0x77) {
433 case RFID_LAYER2_ISO14443B:
434 /* initialized T=CL state from Type B Activation Data */
435 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
436 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
437 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
438 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
440 /* what about ta? sfgt? */
442 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
443 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
444 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
445 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
447 switch (h->l2h->priv.iso14443b.state) {
448 case ISO14443B_STATE_SELECTED:
449 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
451 case ISO14443B_STATE_ATTRIB_SENT:
452 h->priv.tcl.state = TCL_STATE_RATS_SENT;
456 /* PUPI will be presented as ATS/historical bytes */
457 memcpy(h->priv.tcl.ats, h->l2h->uid, 4);
458 h->priv.tcl.ats_len = 4;
459 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
463 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
468 h->priv.tcl.state = TCL_STATE_ESTABLISHED;
474 tcl_deselect(struct rfid_protocol_handle *h)
476 /* ISO 14443-4:2000(E) Section 8 */
478 unsigned char frame[3]; /* 3 bytes prologue, no information */
480 unsigned int rx_len = sizeof(rx);
481 unsigned int prlg_len;
482 struct tcl_handle *th = &h->priv.tcl;
484 if (th->state != TCL_STATE_ESTABLISHED) {
485 /* FIXME: not sure whether deselect is possible here,
486 * probably better send a HLTA? */
489 /* build DESELECT S-block */
490 ret = tcl_build_prologue_s(th, frame, &prlg_len);
494 ret = rfid_layer2_transceive(h->l2h, RFID_14443A_FRAME_REGULAR,
496 &rx_len, deactivation_fwt(h),
497 TCL_TRANSP_F_TX_CRC);
499 /* FIXME: retransmit, HLT(A|B) */
503 th->state = TCL_STATE_DESELECTED;
509 unsigned int frame_len; /* length of frame */
510 unsigned int hdr_len; /* length of header within frame */
511 unsigned char data[RFID_MAX_FRAMELEN];
514 #define frb_payload(x) (x.data + x.hdr_len)
517 /* RFID transceive buffer. */
519 struct rfix_xcvb *next; /* next in queue of buffers */
521 u_int64_t timeout; /* timeout to wait for reply */
525 //struct rfid_protocol_handle *h; /* connection to which we belong */
528 struct tcl_tx_context {
529 const unsigned char *tx;
531 const unsigned char *next_tx_byte;
532 unsigned char *next_rx_byte;
535 struct rfid_protocol_handle *h;
538 #define tcl_ctx_todo(ctx) (ctx->tx_len - (ctx->next_tx_byte - ctx->tx))
541 tcl_refill_xcvb(struct rfid_xcvb *xcvb, struct tcl_tx_context *ctx)
543 struct tcl_handle *th = &ctx->h->priv.tcl;
545 if (ctx->next_tx_byte >= ctx->tx + ctx->tx_len) {
546 DEBUGP("tyring to refill tx xcvb but no data left!\n");
550 if (tcl_build_prologue_i(th, xcvb->tx.data,
551 &xcvb->tx.hdr_len) < 0)
554 if (tcl_ctx_todo(ctx) > th->fsc - xcvb->tx.hdr_len)
555 xcvb->tx.frame_len = max_net_tx_framesize(th);
557 xcvb->tx.frame_len = tcl_ctx_todo(ctx);
559 memcpy(frb_payload(xcvb->tx), ctx->next_tx_byte,
562 ctx->next_tx_byte += xcvb->tx.frame_len;
564 /* check whether we need to set the chaining bit */
565 if (ctx->next_tx_byte < ctx->tx + ctx->tx_len)
566 xcvb->tx.data[0] |= 0x10;
568 /* add hdr_len after copying the net payload */
569 xcvb->tx.frame_len += xcvb->tx.hdr_len;
571 xcvb->timeout = th->fwt;
576 static void fill_xcvb_wtxm(struct tcl_handle *th, struct rfid_xcvb *xcvb,
579 /* Acknowledge WTXM */
580 tcl_build_prologue_s(th, xcvb->tx.data, &xcvb->tx.hdr_len);
581 /* set two bits that make this block a wtx */
582 xcvb->tx.data[0] |= 0x30;
583 xcvb->tx.data[xcvb->tx.hdr_len] = inf;
584 xcvb->tx.frame_len = xcvb->tx.hdr_len+1;
585 xcvb->timeout = th->fwt * inf;
588 static int check_cid(struct tcl_handle *th, struct rfid_xcvb *xcvb)
590 if (xcvb->rx.data[0] & TCL_PCB_CID_FOLLOWING) {
591 if (xcvb->rx.data[1] != th->cid) {
592 DEBUGP("CID %u is not valid, we expected %u\n",
593 xcvb->rx.data[1], th->cid);
601 tcl_transceive(struct rfid_protocol_handle *h,
602 const unsigned char *tx_data, unsigned int tx_len,
603 unsigned char *rx_data, unsigned int *rx_len,
604 unsigned int timeout, unsigned int flags)
608 struct rfid_xcvb xcvb;
609 struct tcl_tx_context tcl_ctx;
610 struct tcl_handle *th = &h->priv.tcl;
612 unsigned char ack[10];
613 unsigned int ack_len;
615 /* initialize context */
616 tcl_ctx.next_tx_byte = tcl_ctx.tx = tx_data;
617 tcl_ctx.next_rx_byte = tcl_ctx.rx = rx_data;
618 tcl_ctx.rx_len = *rx_len;
619 tcl_ctx.tx_len = tx_len;
622 /* initialize xcvb */
623 xcvb.timeout = th->fwt;
626 if (tcl_refill_xcvb(&xcvb, &tcl_ctx) < 0) {
632 xcvb.rx.frame_len = sizeof(xcvb.rx.data);
633 ret = rfid_layer2_transceive(h->l2h, l2_to_frame(h->l2h->l2->id),
634 xcvb.tx.data, xcvb.tx.frame_len,
635 xcvb.rx.data, &xcvb.rx.frame_len,
638 DEBUGP("l2 transceive finished\n");
642 if (is_r_block(xcvb.rx.data[0])) {
645 if ((xcvb.rx.data[0] & 0x01) != h->priv.tcl.toggle) {
646 DEBUGP("response with wrong toggle bit\n");
650 /* Handle ACK frame in case of chaining */
651 if (!check_cid(th, &xcvb))
655 } else if (is_s_block(xcvb.rx.data[0])) {
657 unsigned int prlg_len;
660 /* Handle Wait Time Extension */
662 if (!check_cid(th, &xcvb))
665 if (xcvb.rx.data[0] & TCL_PCB_CID_FOLLOWING) {
666 if (xcvb.rx.frame_len < 3) {
667 DEBUGP("S-Block with CID but short len\n");
671 inf = xcvb.rx.data[2];
673 inf = xcvb.rx.data[1];
675 if ((xcvb.rx.data[0] & 0x30) != 0x30) {
676 DEBUGP("S-Block but not WTX?\n");
680 inf &= 0x3f; /* only lower 6 bits code WTXM */
681 if (inf == 0 || (inf >= 60 && inf <= 63)) {
682 DEBUGP("WTXM %u is RFU!\n", inf);
687 fill_xcvb_wtxm(th, &xcvb, inf);
688 /* start over with next transceive */
690 } else if (is_i_block(xcvb.rx.data[0])) {
691 unsigned int net_payload_len;
692 /* we're actually receiving payload data */
696 if ((xcvb.rx.data[0] & 0x01) != h->priv.tcl.toggle) {
697 DEBUGP("response with wrong toggle bit\n");
703 if (!check_cid(th, &xcvb))
706 if (xcvb.rx.data[0] & TCL_PCB_CID_FOLLOWING)
708 if (xcvb.rx.data[0] & TCL_PCB_NAD_FOLLOWING)
711 net_payload_len = xcvb.rx.frame_len - xcvb.rx.hdr_len;
712 DEBUGPC("%u bytes\n", net_payload_len);
713 memcpy(tcl_ctx.next_rx_byte, &xcvb.rx.data[xcvb.rx.hdr_len],
715 tcl_ctx.next_rx_byte += net_payload_len;
717 if (xcvb.rx.data[0] & 0x10) {
718 /* we're not the last frame in the chain, continue rx */
719 DEBUGP("not the last frame in the chain, continue\n");
720 ack_len = sizeof(ack);
721 tcl_build_prologue_r(th, xcvb.tx.data, &xcvb.tx.frame_len, 0);
722 xcvb.timeout = th->fwt;
728 *rx_len = tcl_ctx.next_rx_byte - tcl_ctx.rx;
732 static struct rfid_protocol_handle *
733 tcl_init(struct rfid_layer2_handle *l2h)
735 struct rfid_protocol_handle *th;
736 unsigned int mru = l2h->rh->ah->mru;
738 th = malloc(sizeof(struct rfid_protocol_handle) + mru);
742 /* FIXME: mru should be attribute of layer2 (in case it adds/removes
744 memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
746 /* maximum received ats length equals mru of asic/reader */
747 th->priv.tcl.state = TCL_STATE_INITIAL;
748 th->priv.tcl.ats_len = mru;
749 th->priv.tcl.toggle = 1;
751 th->priv.tcl.fsd = iso14443_fsd_approx(mru);
757 tcl_fini(struct rfid_protocol_handle *ph)
763 struct rfid_protocol rfid_protocol_tcl = {
764 .id = RFID_PROTOCOL_TCL,
765 .name = "ISO 14443-4 / T=CL",
768 .open = &tcl_connect,
769 .transceive = &tcl_transceive,
770 .close = &tcl_deselect,