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, rats, 2, h->priv.tcl.ats,
169 &h->priv.tcl.ats_len, activation_fwt(h),
170 TCL_TRANSP_F_TX_CRC);
172 DEBUGP("transcieve of rats failed\n");
173 h->priv.tcl.state = TCL_STATE_RATS_SENT;
174 /* FIXME: retransmit */
177 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
179 ret = tcl_parse_ats(h, h->priv.tcl.ats, h->priv.tcl.ats_len);
181 DEBUGP("parsing of ats failed\n");
188 #define ATS_TA_DIV_2 1
189 #define ATS_TA_DIV_4 2
190 #define ATS_TA_DIV_8 4
196 static unsigned char d_to_di(struct rfid_protocol_handle *h, unsigned char D)
199 unsigned int speed = h->l2h->rh->reader->iso14443a.speed;
201 if ((D & ATS_TA_DIV_8) && (speed & RFID_READER_SPEED_848K))
203 else if ((D & ATS_TA_DIV_4) && (speed & RFID_READER_SPEED_424K))
205 else if ((D & ATS_TA_DIV_2) && (speed & RFID_READER_SPEED_212K))
214 /* start a PSS run (autimatically configure highest possible speed */
216 tcl_do_pps(struct rfid_protocol_handle *h)
219 unsigned char ppss[3];
220 unsigned char pps_response[1];
221 unsigned int rx_len = 1;
222 unsigned char Dr, Ds, DrI, DsI;
224 if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
227 Dr = h->priv.tcl.ta & 0x07;
228 Ds = h->priv.tcl.ta & 0x70 >> 4;
230 if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
231 /* device supports different divisors for rx and tx, but not ?!? */
232 DEBUGP("PICC has contradictory TA, aborting PPS\n");
236 /* ISO 14443-4:2000(E) Section 5.3. */
238 ppss[0] = 0xd0 & (h->priv.tcl.cid & 0x0f);
241 /* FIXME: deal with different speed for each direction */
242 DrI = d_to_di(h, Dr);
243 DsI = d_to_di(h, Ds);
245 ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
247 ret = h->l2h->l2->fn.transcieve(h->l2h, ppss, 3, pps_response,
248 &rx_len, h->priv.tcl.fwt,
249 TCL_TRANSP_F_TX_CRC);
253 if (pps_response[0] != ppss[0]) {
254 DEBUGP("PPS Response != PPSS\n");
258 h->priv.tcl.state = TCL_STATE_ESTABLISHED;
265 tcl_build_prologue2(struct tcl_handle *th,
266 unsigned char *prlg, unsigned int *prlg_len,
274 /* we've sent a toggle bit last time */
277 /* we've not sent a toggle last time: send one */
282 if (th->flags & TCL_HANDLE_F_CID_USED) {
283 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
284 *prlg |= TCL_PCB_CID_FOLLOWING;
286 prlg[*prlg_len] = th->cid & 0x0f;
289 /* nad only for I-block (0xc0 == 00) */
290 if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
291 ((pcb & 0xc0) == 0x00)) {
292 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
293 /* FIXME: in case of chaining only for first frame */
294 *prlg |= TCL_PCB_NAD_FOLLOWING;
295 prlg[*prlg_len] = th->nad;
303 tcl_build_prologue_i(struct tcl_handle *th,
304 unsigned char *prlg, unsigned int *prlg_len)
306 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
307 return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
311 tcl_build_prologue_r(struct tcl_handle *th,
312 unsigned char *prlg, unsigned int *prlg_len,
315 unsigned char pcb = 0xa2;
316 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
321 return tcl_build_prologue2(th, prlg, prlg_len, pcb);
325 tcl_build_prologue_s(struct tcl_handle *th,
326 unsigned char *prlg, unsigned int *prlg_len)
328 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
330 /* the only S-block from PCD->PICC is DESELECT,
331 * well, actually there is the S(WTX) response. */
332 return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
335 /* FIXME: WTXM implementation */
337 static int tcl_prlg_len(struct tcl_handle *th)
341 if (th->flags & TCL_HANDLE_F_CID_USED)
344 if (th->flags & TCL_HANDLE_F_NAD_USED)
350 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
353 tcl_connect(struct rfid_protocol_handle *h)
357 if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
358 h->priv.tcl.state != TCL_STATE_INITIAL)
361 switch (h->l2h->l2->id) {
362 case RFID_LAYER2_ISO14443A:
363 /* Start Type A T=CL Activation Sequence */
364 ret = tcl_request_ats(h);
368 /* Only do PPS if any non-default divisors supported */
369 if (h->priv.tcl.ta & 0x77) {
375 case RFID_LAYER2_ISO14443B:
376 /* initialized T=CL state from Type B Activation Data */
377 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
378 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
379 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
380 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
382 /* what about ta? sfgt? */
384 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
385 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
386 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
387 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
389 switch (h->l2h->priv.iso14443b.state) {
390 case ISO14443B_STATE_SELECTED:
391 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
393 case ISO14443B_STATE_ATTRIB_SENT:
394 h->priv.tcl.state = TCL_STATE_RATS_SENT;
398 /* PUPI will be presented as ATS/historical bytes */
399 memcpy(h->priv.tcl.ats, h->l2h->priv.iso14443b.pupi, 4);
400 h->priv.tcl.ats_len = 4;
401 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
405 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
414 tcl_deselect(struct rfid_protocol_handle *h)
416 /* ISO 14443-4:2000(E) Section 8 */
418 unsigned char frame[3]; /* 3 bytes prologue, no information */
420 unsigned int rx_len = sizeof(rx);
421 unsigned int prlg_len;
422 struct tcl_handle *th = &h->priv.tcl;
424 if (th->state != TCL_STATE_ESTABLISHED) {
425 /* FIXME: not sure whether deselect is possible here,
426 * probably better send a HLTA? */
429 /* build DESELECT S-block */
430 ret = tcl_build_prologue_s(th, frame, &prlg_len);
434 ret = h->l2h->l2->fn.transcieve(h->l2h, frame, prlg_len, rx,
435 &rx_len, deactivation_fwt(h),
436 TCL_TRANSP_F_TX_CRC);
438 /* FIXME: retransmit, HLT(A|B) */
442 th->state = TCL_STATE_DESELECTED;
447 #define is_s_block(x) ((x & 0xc0) == 0xc0)
448 #define is_r_block(x) ((x & 0xc0) == 0x80)
449 #define is_i_block(x) ((x & 0xc0) == 0x00)
452 tcl_transcieve(struct rfid_protocol_handle *h,
453 const unsigned char *tx_data, unsigned int tx_len,
454 unsigned char *rx_data, unsigned int *rx_len,
455 unsigned int timeout, unsigned int flags)
458 unsigned char *tx_buf, *rx_buf;
459 unsigned int prlg_len;
460 struct tcl_handle *th = &h->priv.tcl;
463 unsigned int _tx_len, _timeout;
464 unsigned char wtx_resp[3];
466 if (tx_len > max_net_tx_framesize(th)) {
467 /* slow path: we need to use chaining */
471 tx_buf = malloc(tcl_prlg_len(th) + tx_len);
476 rx_buf = malloc(tcl_prlg_len(th) + *rx_len);
482 if (tcl_build_prologue_i(th, tx_buf, &prlg_len) < 0) {
486 memcpy(tx_buf + prlg_len, tx_data, tx_len);
488 /* intialize to data-to-be-transferred */
490 _tx_len = tx_len+prlg_len;
494 ret = h->l2h->l2->fn.transcieve(h->l2h, _tx, _tx_len,
495 rx_buf, rx_len, _timeout, 0);
499 if ((*rx_buf & 0x01) != h->priv.tcl.toggle) {
500 DEBUGP("response with wrong toggle bit\n");
507 if (is_r_block(*rx_buf)) {
508 unsigned int txed = _tx - tx_buf;
509 /* Handle ACK frame in case of chaining */
510 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
511 if (*(rx_buf+1) != h->priv.tcl.cid) {
512 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
516 /* set up parameters for next frame in chain */
518 /* move tx pointer by the amount of bytes transferred
521 _tx_len = (tx_len - txed);
522 if (_tx_len > max_net_tx_framesize(th)) {
523 /* not last frame in chain */
524 _tx_len = max_net_tx_framesize(th);
526 /* last frame in chain */
530 DEBUGP("Received ACK in response to last frame in "
531 "chain?!? Expected I-frame.\n");
535 } else if (is_s_block(*rx_buf)) {
537 unsigned int prlg_len;
539 /* Handle Wait Time Extension */
540 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
542 DEBUGP("S-Block with CID but short len\n");
546 if (*(rx_buf+1) != h->priv.tcl.cid) {
547 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
554 if ((*rx_buf & 0x30) != 0x30) {
555 DEBUGP("S-Block but not WTX?\n");
559 inf &= 0x3f; /* only lower 6 bits code WTXM */
560 if (inf == 0 || (inf >= 60 && inf <= 63)) {
561 DEBUGP("WTXM %u is RFU!\n", inf);
566 /* Acknowledge WTXM */
567 tcl_build_prologue_s(&h->priv.tcl, wtx_resp, &prlg_len);
568 /* set two bits that make this block a wtx */
570 wtx_resp[prlg_len] = inf;
572 _tx_len = prlg_len+1;
573 _timeout = th->fwt * inf;
575 /* start over with next transcieve */
576 goto do_tx; /* FIXME: do transcieve locally since we use
577 totally different buffer */
579 } else if (is_i_block(*rx_buf)) {
580 unsigned char *inf = rx_buf+1;
581 /* we're actually receiving payload data */
583 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
584 if (*(rx_buf+1) != h->priv.tcl.cid) {
585 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
590 if (*rx_buf & TCL_PCB_NAD_FOLLOWING) {
593 memcpy(rx_data, inf, *rx_len - (inf - rx_buf));
595 if (*rx_buf & 0x10) {
596 /* we're not the last frame in the chain, continue */
611 tcl_send(struct tcl_handle *th)
623 static struct rfid_protocol_handle *
624 tcl_init(struct rfid_layer2_handle *l2h)
626 struct rfid_protocol_handle *th;
627 unsigned int mru = l2h->rh->ah->mru;
629 th = malloc(sizeof(struct rfid_protocol_handle) + mru);
633 /* FIXME: mru should be attribute of layer2 (in case it adds/removes
635 memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
637 /* maximum received ats length equals mru of asic/reader */
638 th->priv.tcl.state = TCL_STATE_INITIAL;
639 th->priv.tcl.ats_len = mru;
640 th->priv.tcl.toggle = 1;
642 th->proto = &rfid_protocol_tcl;
644 th->priv.tcl.fsd = iso14443_fsd_approx(mru);
650 tcl_fini(struct rfid_protocol_handle *ph)
656 struct rfid_protocol rfid_protocol_tcl = {
657 .id = RFID_PROTOCOL_TCL,
658 .name = "ISO 14443-4 / T=CL",
661 .open = &tcl_connect,
662 .transcieve = &tcl_transcieve,
663 .close = &tcl_deselect,