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)
220 unsigned char ppss[3];
221 unsigned char pps_response[1];
222 unsigned int rx_len = 1;
223 unsigned char Dr, Ds, DrI, DsI;
225 if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
228 Dr = h->priv.tcl.ta & 0x07;
229 Ds = h->priv.tcl.ta & 0x70 >> 4;
231 if (Dr != Ds && !(h->priv.tcl.ta & 0x80)) {
232 /* device supports different divisors for rx and tx, but not ?!? */
233 DEBUGP("PICC has contradictory TA, aborting PPS\n");
237 /* ISO 14443-4:2000(E) Section 5.3. */
239 ppss[0] = 0xd0 & (h->priv.tcl.cid & 0x0f);
242 /* FIXME: deal with different speed for each direction */
243 DrI = d_to_di(h, Dr);
244 DsI = d_to_di(h, Ds);
246 ppss[2] = (ppss[2] & 0xf0) | (DrI | DsI << 2);
248 ret = h->l2h->l2->fn.transcieve(h->l2h, ppss, 3, pps_response,
249 &rx_len, h->priv.tcl.fwt,
250 TCL_TRANSP_F_TX_CRC);
254 if (pps_response[0] != ppss[0]) {
255 DEBUGP("PPS Response != PPSS\n");
259 h->priv.tcl.state = TCL_STATE_ESTABLISHED;
266 tcl_build_prologue2(struct tcl_handle *th,
267 unsigned char *prlg, unsigned int *prlg_len,
275 /* we've sent a toggle bit last time */
278 /* we've not sent a toggle last time: send one */
283 if (th->flags & TCL_HANDLE_F_CID_USED) {
284 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
285 *prlg |= TCL_PCB_CID_FOLLOWING;
287 prlg[*prlg_len] = th->cid & 0x0f;
290 /* nad only for I-block (0xc0 == 00) */
291 if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
292 ((pcb & 0xc0) == 0x00)) {
293 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
294 /* FIXME: in case of chaining only for first frame */
295 *prlg |= TCL_PCB_NAD_FOLLOWING;
296 prlg[*prlg_len] = th->nad;
304 tcl_build_prologue_i(struct tcl_handle *th,
305 unsigned char *prlg, unsigned int *prlg_len)
307 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
308 return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
312 tcl_build_prologue_r(struct tcl_handle *th,
313 unsigned char *prlg, unsigned int *prlg_len,
316 unsigned char pcb = 0xa2;
317 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
322 return tcl_build_prologue2(th, prlg, prlg_len, pcb);
326 tcl_build_prologue_s(struct tcl_handle *th,
327 unsigned char *prlg, unsigned int *prlg_len)
329 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
331 /* the only S-block from PCD->PICC is DESELECT,
332 * well, actually there is the S(WTX) response. */
333 return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
336 /* FIXME: WTXM implementation */
338 static int tcl_prlg_len(struct tcl_handle *th)
342 if (th->flags & TCL_HANDLE_F_CID_USED)
345 if (th->flags & TCL_HANDLE_F_NAD_USED)
351 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
354 tcl_connect(struct rfid_protocol_handle *h)
358 if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
359 h->priv.tcl.state != TCL_STATE_INITIAL)
362 switch (h->l2h->l2->id) {
363 case RFID_LAYER2_ISO14443A:
364 /* Start Type A T=CL Activation Sequence */
365 ret = tcl_request_ats(h);
369 /* Only do PPS if any non-default divisors supported */
370 if (h->priv.tcl.ta & 0x77) {
376 case RFID_LAYER2_ISO14443B:
377 /* initialized T=CL state from Type B Activation Data */
378 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
379 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
380 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
381 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
383 /* what about ta? sfgt? */
385 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
386 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
387 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
388 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
390 switch (h->l2h->priv.iso14443b.state) {
391 case ISO14443B_STATE_SELECTED:
392 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
394 case ISO14443B_STATE_ATTRIB_SENT:
395 h->priv.tcl.state = TCL_STATE_RATS_SENT;
399 /* PUPI will be presented as ATS/historical bytes */
400 memcpy(h->priv.tcl.ats, h->l2h->priv.iso14443b.pupi, 4);
401 h->priv.tcl.ats_len = 4;
402 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
406 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
415 tcl_deselect(struct rfid_protocol_handle *h)
417 /* ISO 14443-4:2000(E) Section 8 */
419 unsigned char frame[3]; /* 3 bytes prologue, no information */
421 unsigned int rx_len = sizeof(rx);
422 unsigned int prlg_len;
423 struct tcl_handle *th = &h->priv.tcl;
425 if (th->state != TCL_STATE_ESTABLISHED) {
426 /* FIXME: not sure whether deselect is possible here,
427 * probably better send a HLTA? */
430 /* build DESELECT S-block */
431 ret = tcl_build_prologue_s(th, frame, &prlg_len);
435 ret = h->l2h->l2->fn.transcieve(h->l2h, frame, prlg_len, rx,
436 &rx_len, deactivation_fwt(h),
437 TCL_TRANSP_F_TX_CRC);
439 /* FIXME: retransmit, HLT(A|B) */
443 th->state = TCL_STATE_DESELECTED;
448 #define is_s_block(x) ((x & 0xc0) == 0xc0)
449 #define is_r_block(x) ((x & 0xc0) == 0x80)
450 #define is_i_block(x) ((x & 0xc0) == 0x00)
453 tcl_transcieve(struct rfid_protocol_handle *h,
454 const unsigned char *tx_data, unsigned int tx_len,
455 unsigned char *rx_data, unsigned int *rx_len,
456 unsigned int timeout, unsigned int flags)
459 unsigned char *tx_buf, *rx_buf;
460 unsigned int prlg_len;
461 struct tcl_handle *th = &h->priv.tcl;
464 unsigned int _tx_len, _timeout;
465 unsigned char wtx_resp[3];
466 unsigned char ack[10];
467 unsigned int ack_len;
469 if (tx_len > max_net_tx_framesize(th)) {
470 /* slow path: we need to use chaining */
474 tx_buf = malloc(tcl_prlg_len(th) + tx_len);
479 rx_buf = malloc(tcl_prlg_len(th) + *rx_len);
485 if (tcl_build_prologue_i(th, tx_buf, &prlg_len) < 0) {
489 memcpy(tx_buf + prlg_len, tx_data, tx_len);
491 /* intialize to data-to-be-transferred */
493 _tx_len = tx_len+prlg_len;
497 ret = h->l2h->l2->fn.transcieve(h->l2h, _tx, _tx_len,
498 rx_buf, rx_len, _timeout, 0);
499 DEBUGP("l2 transcieve finished\n");
503 if ((*rx_buf & 0x01) != h->priv.tcl.toggle) {
504 DEBUGP("response with wrong toggle bit\n");
508 if (is_r_block(*rx_buf)) {
509 unsigned int txed = _tx - tx_buf;
511 /* Handle ACK frame in case of chaining */
512 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
513 if (*(rx_buf+1) != h->priv.tcl.cid) {
514 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
518 /* set up parameters for next frame in chain */
520 /* move tx pointer by the amount of bytes transferred
523 _tx_len = (tx_len - txed);
524 if (_tx_len > max_net_tx_framesize(th)) {
525 /* not last frame in chain */
526 _tx_len = max_net_tx_framesize(th);
528 /* last frame in chain */
532 DEBUGP("Received ACK in response to last frame in "
533 "chain?!? Expected I-frame.\n");
537 } else if (is_s_block(*rx_buf)) {
539 unsigned int prlg_len;
542 /* Handle Wait Time Extension */
543 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
545 DEBUGP("S-Block with CID but short len\n");
549 if (*(rx_buf+1) != h->priv.tcl.cid) {
550 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
557 if ((*rx_buf & 0x30) != 0x30) {
558 DEBUGP("S-Block but not WTX?\n");
562 inf &= 0x3f; /* only lower 6 bits code WTXM */
563 if (inf == 0 || (inf >= 60 && inf <= 63)) {
564 DEBUGP("WTXM %u is RFU!\n", inf);
569 /* Acknowledge WTXM */
570 tcl_build_prologue_s(&h->priv.tcl, wtx_resp, &prlg_len);
571 /* set two bits that make this block a wtx */
573 wtx_resp[prlg_len] = inf;
575 _tx_len = prlg_len+1;
576 _timeout = th->fwt * inf;
578 /* start over with next transcieve */
579 goto do_tx; /* FIXME: do transcieve locally since we use
580 totally different buffer */
582 } else if (is_i_block(*rx_buf)) {
583 unsigned char *inf = rx_buf+1;
584 /* we're actually receiving payload data */
587 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
588 if (*(rx_buf+1) != h->priv.tcl.cid) {
589 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
594 if (*rx_buf & TCL_PCB_NAD_FOLLOWING) {
597 memcpy(rx_data, inf, *rx_len - (inf - rx_buf));
599 if (*rx_buf & 0x10) {
600 /* we're not the last frame in the chain, continue rx */
601 DEBUGP("we're not the last frame in the chain, continue\n");
602 ack_len = sizeof(ack);
603 tcl_build_prologue_r(&h->priv.tcl, ack, &ack_len, 0);
620 tcl_send(struct tcl_handle *th)
632 static struct rfid_protocol_handle *
633 tcl_init(struct rfid_layer2_handle *l2h)
635 struct rfid_protocol_handle *th;
636 unsigned int mru = l2h->rh->ah->mru;
638 th = malloc(sizeof(struct rfid_protocol_handle) + mru);
642 /* FIXME: mru should be attribute of layer2 (in case it adds/removes
644 memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
646 /* maximum received ats length equals mru of asic/reader */
647 th->priv.tcl.state = TCL_STATE_INITIAL;
648 th->priv.tcl.ats_len = mru;
649 th->priv.tcl.toggle = 1;
651 th->proto = &rfid_protocol_tcl;
653 th->priv.tcl.fsd = iso14443_fsd_approx(mru);
659 tcl_fini(struct rfid_protocol_handle *ph)
665 struct rfid_protocol rfid_protocol_tcl = {
666 .id = RFID_PROTOCOL_TCL,
667 .name = "ISO 14443-4 / T=CL",
670 .open = &tcl_connect,
671 .transcieve = &tcl_transcieve,
672 .close = &tcl_deselect,