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"
42 #define DEBUGP(x, ...)
46 #define DEBUGPC(x, ...)
50 static enum rfid_frametype l2_to_frame(unsigned int layer2)
53 case RFID_LAYER2_ISO14443A:
54 return RFID_14443A_FRAME_REGULAR;
56 case RFID_LAYER2_ISO14443B:
57 return RFID_14443B_FRAME_REGULAR;
63 static unsigned int sfgi_to_sfgt(struct rfid_protocol_handle *h,
66 unsigned int multiplier;
72 multiplier = 1 << sfgi; /* 2 to the power of sfgi */
74 /* ISO 14443-4:2000(E) Section 5.2.5:
75 * (256 * 16 / h->l2h->rh->ah->fc) * (2 ^ sfgi) */
76 tmp = (unsigned int) 1000000 * 256 * 16;
78 return (tmp / h->l2h->rh->ah->fc) * multiplier;
81 static unsigned int fwi_to_fwt(struct rfid_protocol_handle *h,
84 unsigned int multiplier, tmp;
89 multiplier = 1 << fwi; /* 2 to the power of fwi */
91 /* ISO 14443-4:2000(E) Section 7.2.:
92 * (256*16 / h->l2h->rh->ah->fc) * (2 ^ fwi) */
94 tmp = (unsigned int) 1000000 * 256 * 16;
96 return (tmp / h->l2h->rh->ah->fc) * multiplier;
99 /* 4.9seconds as microseconds (4.9 billion seconds) exceeds 2^32 */
100 #define activation_fwt(x) (((u_int64_t)1000000 * 65536 / x->l2h->rh->ah->fc))
101 #define deactivation_fwt(x) activation_fwt(x)
104 tcl_parse_ats(struct rfid_protocol_handle *h,
105 unsigned char *ats, unsigned int size)
107 unsigned char len = ats[0];
111 if (len == 0 || size == 0)
120 /* FIXME: assume some default values */
121 h->priv.tcl.fsc = 32;
122 h->priv.tcl.ta = 0x80; /* 0x80 (same d for both dirs) */
123 h->priv.tcl.sfgt = sfgi_to_sfgt(h, 0);
124 if (h->l2h->l2->id == RFID_LAYER2_ISO14443A) {
125 /* Section 7.2: fwi default for type A is 4 */
126 h->priv.tcl.fwt = fwi_to_fwt(h, 4);
128 /* Section 7.2: fwi for type B is always in ATQB */
129 /* Value is assigned in tcl_connect() */
130 /* This function is never called for Type B, since it has no (R)ATS */
135 /* guarateed to be at least 2 bytes in size */
140 iso14443_fsdi_to_fsd(&h->priv.tcl.fsc, t0 & 0x0f);
143 /* TA is transmitted */
144 h->priv.tcl.ta = *cur++;
148 /* TB is transmitted */
149 h->priv.tcl.sfgt = sfgi_to_sfgt(h, *cur & 0x0f);
150 h->priv.tcl.fwt = fwi_to_fwt(h, (*cur & 0xf0) >> 4);
155 /* TC is transmitted */
157 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
159 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
163 h->priv.tcl.historical_len = (ats+len) - cur;
164 h->priv.tcl.historical_bytes = cur;
170 /* request an ATS from the PICC */
172 tcl_request_ats(struct rfid_protocol_handle *h)
175 unsigned char rats[2];
178 if (h->priv.tcl.state != TCL_STATE_INITIAL)
181 ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.tcl.fsd);
183 DEBUGP("unable to encode FSD of %u as FSDI\n", h->priv.tcl.fsd);
188 rats[1] = (h->priv.tcl.cid & 0x0f) | ((fsdi << 4) & 0xf0);
190 /* transcieve (with CRC) */
191 ret = rfid_layer2_transcieve(h->l2h, RFID_14443A_FRAME_REGULAR,
192 rats, 2, h->priv.tcl.ats,
193 &h->priv.tcl.ats_len, activation_fwt(h),
194 TCL_TRANSP_F_TX_CRC);
196 DEBUGP("transcieve of rats failed\n");
197 h->priv.tcl.state = TCL_STATE_RATS_SENT;
198 /* FIXME: retransmit */
201 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
203 ret = tcl_parse_ats(h, h->priv.tcl.ats, h->priv.tcl.ats_len);
205 DEBUGP("parsing of ats failed\n");
212 #define ATS_TA_DIV_2 1
213 #define ATS_TA_DIV_4 2
214 #define ATS_TA_DIV_8 4
220 static unsigned char d_to_di(struct rfid_protocol_handle *h, unsigned char D)
223 unsigned int speed = h->l2h->rh->reader->iso14443a.speed;
225 if ((D & ATS_TA_DIV_8) && (speed & RFID_14443A_SPEED_848K))
227 else if ((D & ATS_TA_DIV_4) && (speed & RFID_14443A_SPEED_424K))
229 else if ((D & ATS_TA_DIV_2) && (speed & RFID_14443A_SPEED_212K))
237 static unsigned int di_to_speed(unsigned char DI)
241 return RFID_14443A_SPEED_848K;
244 return RFID_14443A_SPEED_424K;
247 return RFID_14443A_SPEED_212K;
250 return RFID_14443A_SPEED_106K;
255 /* start a PPS run (autimatically configure highest possible speed */
257 tcl_do_pps(struct rfid_protocol_handle *h)
260 unsigned char ppss[3];
261 unsigned char pps_response[1];
262 unsigned int rx_len = 1;
263 unsigned char Dr, Ds, DrI, DsI;
266 if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
269 Dr = h->priv.tcl.ta & 0x07;
270 Ds = h->priv.tcl.ta & 0x70 >> 4;
271 DEBUGP("Dr = 0x%x, Ds = 0x%x\n", Dr, Ds);
273 if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
274 /* device supports different divisors for rx and tx, but not
276 DEBUGP("PICC has contradictory TA, aborting PPS\n");
280 /* ISO 14443-4:2000(E) Section 5.3. */
282 ppss[0] = 0xd0 | (h->priv.tcl.cid & 0x0f);
285 /* FIXME: deal with different speed for each direction */
286 DrI = d_to_di(h, Dr);
287 DsI = d_to_di(h, Ds);
288 DEBUGP("DrI = 0x%x, DsI = 0x%x\n", DrI, DsI);
290 ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
292 ret = rfid_layer2_transcieve(h->l2h, RFID_14443A_FRAME_REGULAR,
293 ppss, 3, pps_response, &rx_len,
294 h->priv.tcl.fwt, TCL_TRANSP_F_TX_CRC);
298 if (pps_response[0] != ppss[0]) {
299 DEBUGP("PPS Response != PPSS\n");
303 speed = di_to_speed(DrI);
305 ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_RX,
306 &speed, sizeof(speed));
310 ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED_TX,
311 &speed, sizeof(speed));
320 tcl_build_prologue2(struct tcl_handle *th,
321 unsigned char *prlg, unsigned int *prlg_len,
329 /* we've sent a toggle bit last time */
332 /* we've not sent a toggle last time: send one */
337 if (th->flags & TCL_HANDLE_F_CID_USED) {
338 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
339 *prlg |= TCL_PCB_CID_FOLLOWING;
341 prlg[*prlg_len] = th->cid & 0x0f;
344 /* nad only for I-block (0xc0 == 00) */
345 if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
346 ((pcb & 0xc0) == 0x00)) {
347 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
348 /* FIXME: in case of chaining only for first frame */
349 *prlg |= TCL_PCB_NAD_FOLLOWING;
350 prlg[*prlg_len] = th->nad;
358 tcl_build_prologue_i(struct tcl_handle *th,
359 unsigned char *prlg, unsigned int *prlg_len)
361 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
362 return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
366 tcl_build_prologue_r(struct tcl_handle *th,
367 unsigned char *prlg, unsigned int *prlg_len,
370 unsigned char pcb = 0xa2;
371 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
376 return tcl_build_prologue2(th, prlg, prlg_len, pcb);
380 tcl_build_prologue_s(struct tcl_handle *th,
381 unsigned char *prlg, unsigned int *prlg_len)
383 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
385 /* the only S-block from PCD->PICC is DESELECT,
386 * well, actually there is the S(WTX) response. */
387 return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
390 /* FIXME: WTXM implementation */
392 static int tcl_prlg_len(struct tcl_handle *th)
396 if (th->flags & TCL_HANDLE_F_CID_USED)
399 if (th->flags & TCL_HANDLE_F_NAD_USED)
405 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
408 tcl_connect(struct rfid_protocol_handle *h)
412 if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
413 h->priv.tcl.state != TCL_STATE_INITIAL)
416 switch (h->l2h->l2->id) {
417 case RFID_LAYER2_ISO14443A:
418 /* Start Type A T=CL Activation Sequence */
419 ret = tcl_request_ats(h);
423 /* Only do PPS if any non-default divisors supported */
424 if (h->priv.tcl.ta & 0x77) {
430 case RFID_LAYER2_ISO14443B:
431 /* initialized T=CL state from Type B Activation Data */
432 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
433 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
434 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
435 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
437 /* what about ta? sfgt? */
439 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
440 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
441 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
442 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
444 switch (h->l2h->priv.iso14443b.state) {
445 case ISO14443B_STATE_SELECTED:
446 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
448 case ISO14443B_STATE_ATTRIB_SENT:
449 h->priv.tcl.state = TCL_STATE_RATS_SENT;
453 /* PUPI will be presented as ATS/historical bytes */
454 memcpy(h->priv.tcl.ats, h->l2h->uid, 4);
455 h->priv.tcl.ats_len = 4;
456 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
460 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
465 h->priv.tcl.state = TCL_STATE_ESTABLISHED;
471 tcl_deselect(struct rfid_protocol_handle *h)
473 /* ISO 14443-4:2000(E) Section 8 */
475 unsigned char frame[3]; /* 3 bytes prologue, no information */
477 unsigned int rx_len = sizeof(rx);
478 unsigned int prlg_len;
479 struct tcl_handle *th = &h->priv.tcl;
481 if (th->state != TCL_STATE_ESTABLISHED) {
482 /* FIXME: not sure whether deselect is possible here,
483 * probably better send a HLTA? */
486 /* build DESELECT S-block */
487 ret = tcl_build_prologue_s(th, frame, &prlg_len);
491 ret = rfid_layer2_transcieve(h->l2h, RFID_14443A_FRAME_REGULAR,
493 &rx_len, deactivation_fwt(h),
494 TCL_TRANSP_F_TX_CRC);
496 /* FIXME: retransmit, HLT(A|B) */
500 th->state = TCL_STATE_DESELECTED;
505 #define is_s_block(x) ((x & 0xc0) == 0xc0)
506 #define is_r_block(x) ((x & 0xc0) == 0x80)
507 #define is_i_block(x) ((x & 0xc0) == 0x00)
510 tcl_transcieve(struct rfid_protocol_handle *h,
511 const unsigned char *tx_data, unsigned int tx_len,
512 unsigned char *rx_data, unsigned int *rx_len,
513 unsigned int timeout, unsigned int flags)
516 unsigned char *tx_buf, *rx_buf;
517 unsigned char *_rx_data = rx_data;
518 unsigned int _rx_len;
519 unsigned int max_rx_len = *rx_len; /* maximum number of payoload that
520 caller has requested */
521 unsigned int prlg_len;
522 struct tcl_handle *th = &h->priv.tcl;
525 unsigned int _tx_len, _timeout;
526 unsigned char wtx_resp[3];
527 unsigned char ack[10];
528 unsigned int ack_len;
530 if (tx_len > max_net_tx_framesize(th)) {
531 /* slow path: we need to use chaining */
535 tx_buf = malloc(tcl_prlg_len(th) + tx_len);
540 rx_buf = malloc(tcl_prlg_len(th) + *rx_len);
546 if (tcl_build_prologue_i(th, tx_buf, &prlg_len) < 0) {
550 memcpy(tx_buf + prlg_len, tx_data, tx_len);
552 /* intialize to data-to-be-transferred */
554 _tx_len = tx_len+prlg_len;
560 ret = rfid_layer2_transcieve(h->l2h, l2_to_frame(h->l2h->l2->id),
562 rx_buf, &_rx_len, _timeout, 0);
563 DEBUGP("l2 transcieve finished\n");
567 if ((*rx_buf & 0x01) != h->priv.tcl.toggle) {
568 DEBUGP("response with wrong toggle bit\n");
572 if (is_r_block(*rx_buf)) {
573 unsigned int txed = _tx - tx_buf;
575 /* Handle ACK frame in case of chaining */
576 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
577 if (*(rx_buf+1) != h->priv.tcl.cid) {
578 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
582 /* set up parameters for next frame in chain */
584 /* move tx pointer by the amount of bytes transferred
587 _tx_len = (tx_len - txed);
588 if (_tx_len > max_net_tx_framesize(th)) {
589 /* not last frame in chain */
590 _tx_len = max_net_tx_framesize(th);
592 /* last frame in chain */
596 DEBUGP("Received ACK in response to last frame in "
597 "chain?!? Expected I-frame.\n");
601 } else if (is_s_block(*rx_buf)) {
603 unsigned int prlg_len;
606 /* Handle Wait Time Extension */
607 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
609 DEBUGP("S-Block with CID but short len\n");
613 if (*(rx_buf+1) != h->priv.tcl.cid) {
614 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
621 if ((*rx_buf & 0x30) != 0x30) {
622 DEBUGP("S-Block but not WTX?\n");
626 inf &= 0x3f; /* only lower 6 bits code WTXM */
627 if (inf == 0 || (inf >= 60 && inf <= 63)) {
628 DEBUGP("WTXM %u is RFU!\n", inf);
633 /* Acknowledge WTXM */
634 tcl_build_prologue_s(&h->priv.tcl, wtx_resp, &prlg_len);
635 /* set two bits that make this block a wtx */
637 wtx_resp[prlg_len] = inf;
639 _tx_len = prlg_len+1;
640 _timeout = th->fwt * inf;
642 /* start over with next transcieve */
643 goto do_tx; /* FIXME: do transcieve locally since we use
644 totally different buffer */
646 } else if (is_i_block(*rx_buf)) {
647 unsigned char *inf = rx_buf+1;
648 unsigned int net_payload_len;
649 /* we're actually receiving payload data */
652 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
653 if (*(rx_buf+1) != h->priv.tcl.cid) {
654 DEBUGPC("CID %u is not valid\n", *(rx_buf)+1);
659 if (*rx_buf & TCL_PCB_NAD_FOLLOWING) {
662 net_payload_len = _rx_len - (inf - rx_buf);
663 DEBUGPC("%u bytes\n", net_payload_len);
664 memcpy(_rx_data, inf, net_payload_len);
665 /* increment the number of payload bytes that we actually
667 *rx_len += net_payload_len;
668 _rx_data += net_payload_len;
670 if (*rx_buf & 0x10) {
671 /* we're not the last frame in the chain, continue rx */
672 DEBUGP("not the last frame in the chain, continue\n");
673 ack_len = sizeof(ack);
674 tcl_build_prologue_r(&h->priv.tcl, ack, &ack_len, 0);
689 static struct rfid_protocol_handle *
690 tcl_init(struct rfid_layer2_handle *l2h)
692 struct rfid_protocol_handle *th;
693 unsigned int mru = l2h->rh->ah->mru;
695 th = malloc(sizeof(struct rfid_protocol_handle) + mru);
699 /* FIXME: mru should be attribute of layer2 (in case it adds/removes
701 memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
703 /* maximum received ats length equals mru of asic/reader */
704 th->priv.tcl.state = TCL_STATE_INITIAL;
705 th->priv.tcl.ats_len = mru;
706 th->priv.tcl.toggle = 1;
708 th->priv.tcl.fsd = iso14443_fsd_approx(mru);
714 tcl_fini(struct rfid_protocol_handle *ph)
720 struct rfid_protocol rfid_protocol_tcl = {
721 .id = RFID_PROTOCOL_TCL,
722 .name = "ISO 14443-4 / T=CL",
725 .open = &tcl_connect,
726 .transcieve = &tcl_transcieve,
727 .close = &tcl_deselect,