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"
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))
238 /* start a PPS run (autimatically configure highest possible speed */
240 tcl_do_pps(struct rfid_protocol_handle *h)
244 unsigned char ppss[3];
245 unsigned char pps_response[1];
246 unsigned int rx_len = 1;
247 unsigned char Dr, Ds, DrI, DsI;
248 unsigned int optlen = 0;
250 if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
253 Dr = h->priv.tcl.ta & 0x07;
254 Ds = h->priv.tcl.ta & 0x70 >> 4;
256 if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
257 /* device supports different divisors for rx and tx, but not ?!? */
258 DEBUGP("PICC has contradictory TA, aborting PPS\n");
262 /* ISO 14443-4:2000(E) Section 5.3. */
264 ppss[0] = 0xd0 | (h->priv.tcl.cid & 0x0f);
267 /* FIXME: deal with different speed for each direction */
268 DrI = d_to_di(h, Dr);
269 DsI = d_to_di(h, Ds);
271 ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
273 ret = rfid_layer2_transcieve(h->l2h, RFID_14443A_FRAME_REGULAR,
274 ppss, 3, pps_response, &rx_len,
275 h->priv.tcl.fwt, TCL_TRANSP_F_TX_CRC);
279 if (pps_response[0] != ppss[0]) {
280 DEBUGP("PPS Response != PPSS\n");
284 ret = rfid_layer2_setopt(h->l2h, RFID_OPT_14443A_SPEED,
289 h->priv.tcl.state = TCL_STATE_ESTABLISHED;
296 tcl_build_prologue2(struct tcl_handle *th,
297 unsigned char *prlg, unsigned int *prlg_len,
305 /* we've sent a toggle bit last time */
308 /* we've not sent a toggle last time: send one */
313 if (th->flags & TCL_HANDLE_F_CID_USED) {
314 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
315 *prlg |= TCL_PCB_CID_FOLLOWING;
317 prlg[*prlg_len] = th->cid & 0x0f;
320 /* nad only for I-block (0xc0 == 00) */
321 if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
322 ((pcb & 0xc0) == 0x00)) {
323 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
324 /* FIXME: in case of chaining only for first frame */
325 *prlg |= TCL_PCB_NAD_FOLLOWING;
326 prlg[*prlg_len] = th->nad;
334 tcl_build_prologue_i(struct tcl_handle *th,
335 unsigned char *prlg, unsigned int *prlg_len)
337 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
338 return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
342 tcl_build_prologue_r(struct tcl_handle *th,
343 unsigned char *prlg, unsigned int *prlg_len,
346 unsigned char pcb = 0xa2;
347 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
352 return tcl_build_prologue2(th, prlg, prlg_len, pcb);
356 tcl_build_prologue_s(struct tcl_handle *th,
357 unsigned char *prlg, unsigned int *prlg_len)
359 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
361 /* the only S-block from PCD->PICC is DESELECT,
362 * well, actually there is the S(WTX) response. */
363 return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
366 /* FIXME: WTXM implementation */
368 static int tcl_prlg_len(struct tcl_handle *th)
372 if (th->flags & TCL_HANDLE_F_CID_USED)
375 if (th->flags & TCL_HANDLE_F_NAD_USED)
381 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
384 tcl_connect(struct rfid_protocol_handle *h)
388 if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
389 h->priv.tcl.state != TCL_STATE_INITIAL)
392 switch (h->l2h->l2->id) {
393 case RFID_LAYER2_ISO14443A:
394 /* Start Type A T=CL Activation Sequence */
395 ret = tcl_request_ats(h);
399 /* Only do PPS if any non-default divisors supported */
400 if (h->priv.tcl.ta & 0x77) {
406 case RFID_LAYER2_ISO14443B:
407 /* initialized T=CL state from Type B Activation Data */
408 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
409 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
410 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
411 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
413 /* what about ta? sfgt? */
415 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
416 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
417 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
418 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
420 switch (h->l2h->priv.iso14443b.state) {
421 case ISO14443B_STATE_SELECTED:
422 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
424 case ISO14443B_STATE_ATTRIB_SENT:
425 h->priv.tcl.state = TCL_STATE_RATS_SENT;
429 /* PUPI will be presented as ATS/historical bytes */
430 memcpy(h->priv.tcl.ats, h->l2h->uid, 4);
431 h->priv.tcl.ats_len = 4;
432 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
436 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
445 tcl_deselect(struct rfid_protocol_handle *h)
447 /* ISO 14443-4:2000(E) Section 8 */
449 unsigned char frame[3]; /* 3 bytes prologue, no information */
451 unsigned int rx_len = sizeof(rx);
452 unsigned int prlg_len;
453 struct tcl_handle *th = &h->priv.tcl;
455 if (th->state != TCL_STATE_ESTABLISHED) {
456 /* FIXME: not sure whether deselect is possible here,
457 * probably better send a HLTA? */
460 /* build DESELECT S-block */
461 ret = tcl_build_prologue_s(th, frame, &prlg_len);
465 ret = rfid_layer2_transcieve(h->l2h, RFID_14443A_FRAME_REGULAR,
467 &rx_len, deactivation_fwt(h),
468 TCL_TRANSP_F_TX_CRC);
470 /* FIXME: retransmit, HLT(A|B) */
474 th->state = TCL_STATE_DESELECTED;
479 #define is_s_block(x) ((x & 0xc0) == 0xc0)
480 #define is_r_block(x) ((x & 0xc0) == 0x80)
481 #define is_i_block(x) ((x & 0xc0) == 0x00)
484 tcl_transcieve(struct rfid_protocol_handle *h,
485 const unsigned char *tx_data, unsigned int tx_len,
486 unsigned char *rx_data, unsigned int *rx_len,
487 unsigned int timeout, unsigned int flags)
490 unsigned char *tx_buf, *rx_buf;
491 unsigned char *_rx_data = rx_data;
492 unsigned int _rx_len;
493 unsigned int max_rx_len = *rx_len; /* maximum number of payoload that
494 caller has requested */
495 unsigned int prlg_len;
496 struct tcl_handle *th = &h->priv.tcl;
499 unsigned int _tx_len, _timeout;
500 unsigned char wtx_resp[3];
501 unsigned char ack[10];
502 unsigned int ack_len;
504 if (tx_len > max_net_tx_framesize(th)) {
505 /* slow path: we need to use chaining */
509 tx_buf = malloc(tcl_prlg_len(th) + tx_len);
514 rx_buf = malloc(tcl_prlg_len(th) + *rx_len);
520 if (tcl_build_prologue_i(th, tx_buf, &prlg_len) < 0) {
524 memcpy(tx_buf + prlg_len, tx_data, tx_len);
526 /* intialize to data-to-be-transferred */
528 _tx_len = tx_len+prlg_len;
534 ret = rfid_layer2_transcieve(h->l2h, l2_to_frame(h->l2h->l2->id),
536 rx_buf, &_rx_len, _timeout, 0);
537 DEBUGP("l2 transcieve finished\n");
541 if ((*rx_buf & 0x01) != h->priv.tcl.toggle) {
542 DEBUGP("response with wrong toggle bit\n");
546 if (is_r_block(*rx_buf)) {
547 unsigned int txed = _tx - tx_buf;
549 /* Handle ACK frame in case of chaining */
550 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
551 if (*(rx_buf+1) != h->priv.tcl.cid) {
552 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
556 /* set up parameters for next frame in chain */
558 /* move tx pointer by the amount of bytes transferred
561 _tx_len = (tx_len - txed);
562 if (_tx_len > max_net_tx_framesize(th)) {
563 /* not last frame in chain */
564 _tx_len = max_net_tx_framesize(th);
566 /* last frame in chain */
570 DEBUGP("Received ACK in response to last frame in "
571 "chain?!? Expected I-frame.\n");
575 } else if (is_s_block(*rx_buf)) {
577 unsigned int prlg_len;
580 /* Handle Wait Time Extension */
581 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
583 DEBUGP("S-Block with CID but short len\n");
587 if (*(rx_buf+1) != h->priv.tcl.cid) {
588 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
595 if ((*rx_buf & 0x30) != 0x30) {
596 DEBUGP("S-Block but not WTX?\n");
600 inf &= 0x3f; /* only lower 6 bits code WTXM */
601 if (inf == 0 || (inf >= 60 && inf <= 63)) {
602 DEBUGP("WTXM %u is RFU!\n", inf);
607 /* Acknowledge WTXM */
608 tcl_build_prologue_s(&h->priv.tcl, wtx_resp, &prlg_len);
609 /* set two bits that make this block a wtx */
611 wtx_resp[prlg_len] = inf;
613 _tx_len = prlg_len+1;
614 _timeout = th->fwt * inf;
616 /* start over with next transcieve */
617 goto do_tx; /* FIXME: do transcieve locally since we use
618 totally different buffer */
620 } else if (is_i_block(*rx_buf)) {
621 unsigned char *inf = rx_buf+1;
622 unsigned int net_payload_len;
623 /* we're actually receiving payload data */
626 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
627 if (*(rx_buf+1) != h->priv.tcl.cid) {
628 DEBUGPC("CID %u is not valid\n", *(rx_buf)+1);
633 if (*rx_buf & TCL_PCB_NAD_FOLLOWING) {
636 net_payload_len = _rx_len - (inf - rx_buf);
637 DEBUGPC("%u bytes\n", net_payload_len);
638 memcpy(_rx_data, inf, net_payload_len);
639 /* increment the number of payload bytes that we actually received */
640 *rx_len += net_payload_len;
641 _rx_data += net_payload_len;
643 if (*rx_buf & 0x10) {
644 /* we're not the last frame in the chain, continue rx */
645 DEBUGP("we're not the last frame in the chain, continue\n");
646 ack_len = sizeof(ack);
647 tcl_build_prologue_r(&h->priv.tcl, ack, &ack_len, 0);
662 static struct rfid_protocol_handle *
663 tcl_init(struct rfid_layer2_handle *l2h)
665 struct rfid_protocol_handle *th;
666 unsigned int mru = l2h->rh->ah->mru;
668 th = malloc(sizeof(struct rfid_protocol_handle) + mru);
672 /* FIXME: mru should be attribute of layer2 (in case it adds/removes
674 memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
676 /* maximum received ats length equals mru of asic/reader */
677 th->priv.tcl.state = TCL_STATE_INITIAL;
678 th->priv.tcl.ats_len = mru;
679 th->priv.tcl.toggle = 1;
681 th->priv.tcl.fsd = iso14443_fsd_approx(mru);
687 tcl_fini(struct rfid_protocol_handle *ph)
693 struct rfid_protocol rfid_protocol_tcl = {
694 .id = RFID_PROTOCOL_TCL,
695 .name = "ISO 14443-4 / T=CL",
698 .open = &tcl_connect,
699 .transcieve = &tcl_transcieve,
700 .close = &tcl_deselect,