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;
48 multiplier = 1 << sfgi; /* 2 to the power of sfgi */
50 /* ISO 14443-4:2000(E) Section 5.2.5:
51 * (256 * 16 / h->l2h->rh->ah->fc) * (2 ^ sfgi) */
53 return (1000000 * 256*16 / h->l2h->rh->ah->fc) * multiplier;
56 static unsigned int fwi_to_fwt(struct rfid_protocol_handle *h,
59 unsigned int multiplier;
64 multiplier = 1 << fwi; /* 2 to the power of fwi */
66 /* ISO 14443-4:2000(E) Section 7.2.:
67 * (256*16 / h->l2h->rh->ah->fc) * (2 ^ fwi) */
69 return (1000000 * 256*16 / h->l2h->rh->ah->fc) * multiplier;
72 #define activation_fwt(x) (65536 / x->l2h->rh->ah->fc)
73 #define deactivation_fwt(x) activation_fwt(x)
76 tcl_parse_ats(struct rfid_protocol_handle *h,
77 unsigned char *ats, unsigned int size)
79 unsigned char len = ats[0];
83 if (len == 0 || size == 0)
90 /* FIXME: assume some default values */
93 h->priv.tcl.sfgt = sfgi_to_sfgt(h, 0);
94 if (1 /* FIXME: is_iso14443a */) {
95 /* Section 7.2: fwi default for type A is 4 */
96 h->priv.tcl.fwt = fwi_to_fwt(h, 4);
98 /* Section 7.2: fwi for type B is always in ATQB */
104 /* guarateed to be at least 2 bytes in size */
109 iso14443_fsdi_to_fsd(&h->priv.tcl.fsc, t0 & 0x0f);
112 /* TA is transmitted */
113 h->priv.tcl.ta = *cur++;
117 /* TB is transmitted */
118 h->priv.tcl.sfgt = sfgi_to_sfgt(h, *cur & 0x0f);
119 h->priv.tcl.fwt = fwi_to_fwt(h, (*cur & 0xf0) >> 4);
124 /* TC is transmitted */
126 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
128 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
132 h->priv.tcl.historical_len = (ats+len) - cur;
133 h->priv.tcl.historical_bytes = cur;
139 /* request an ATS from the PICC */
141 tcl_request_ats(struct rfid_protocol_handle *h)
144 unsigned char rats[2];
147 if (h->priv.tcl.state != TCL_STATE_INITIAL)
150 ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.tcl.fsd);
152 DEBUGP("unable to encode FSD of %u as FSDI\n", h->priv.tcl.fsd);
157 rats[1] = (h->priv.tcl.cid & 0x0f) | ((fsdi << 4) & 0xf0);
159 /* transcieve (with CRC) */
160 ret = h->l2h->l2->fn.transcieve(h->l2h, rats, 2, h->priv.tcl.ats,
161 &h->priv.tcl.ats_len, activation_fwt(h),
162 TCL_TRANSP_F_TX_CRC);
164 DEBUGP("transcieve of rats failed\n");
165 h->priv.tcl.state = TCL_STATE_RATS_SENT;
166 /* FIXME: retransmit */
169 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
171 ret = tcl_parse_ats(h, h->priv.tcl.ats, h->priv.tcl.ats_len);
173 DEBUGP("parsing of ats failed\n");
179 /* start a PSS run (autimatically configure highest possible speed */
181 tcl_do_pss(struct rfid_protocol_handle *h)
183 unsigned char ppss[3];
184 //unsigned char pps_response[1];
186 if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
189 /* ISO 14443-4:2000(E) Section 5.3. */
191 ppss[0] = 0xd0 & (h->priv.tcl.cid & 0x0f);
194 //ppss[2] = 0x00 & foo;
200 h->priv.tcl.state = TCL_STATE_ESTABLISHED;
205 tcl_build_prologue2(struct tcl_handle *th,
206 unsigned char *prlg, unsigned int *prlg_len,
214 /* we've sent a toggle bit last time */
217 /* we've not sent a toggle last time: send one */
222 if (th->flags & TCL_HANDLE_F_CID_USED) {
223 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
224 *prlg |= TCL_PCB_CID_FOLLOWING;
226 prlg[*prlg_len] = th->cid & 0x0f;
229 /* nad only for I-block (0xc0 == 00) */
230 if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
231 ((pcb & 0xc0) == 0x00)) {
232 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
233 /* FIXME: in case of chaining only for first frame */
234 *prlg |= TCL_PCB_NAD_FOLLOWING;
235 prlg[*prlg_len] = th->nad;
243 tcl_build_prologue_i(struct tcl_handle *th,
244 unsigned char *prlg, unsigned int *prlg_len)
246 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
247 return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
251 tcl_build_prologue_r(struct tcl_handle *th,
252 unsigned char *prlg, unsigned int *prlg_len,
255 unsigned char pcb = 0xa2;
256 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
261 return tcl_build_prologue2(th, prlg, prlg_len, pcb);
265 tcl_build_prologue_s(struct tcl_handle *th,
266 unsigned char *prlg, unsigned int *prlg_len)
268 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
270 /* the only S-block from PCD->PICC is DESELECT,
271 * well, actually there is the S(WTX) response. */
272 return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
275 /* FIXME: WTXM implementation */
277 static int tcl_prlg_len(struct tcl_handle *th)
281 if (th->flags & TCL_HANDLE_F_CID_USED)
284 if (th->flags & TCL_HANDLE_F_NAD_USED)
290 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
293 tcl_connect(struct rfid_protocol_handle *h)
297 if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
298 h->priv.tcl.state != TCL_STATE_INITIAL)
301 switch (h->l2h->l2->id) {
302 case RFID_LAYER2_ISO14443A:
303 /* Start Type A T=CL Activation Sequence */
304 ret = tcl_request_ats(h);
314 case RFID_LAYER2_ISO14443B:
315 /* initialized T=CL state from Type B Activation Data */
316 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
317 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
318 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
319 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
321 /* what about ta? sfgt? */
323 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
324 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
325 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
326 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
328 switch (h->l2h->priv.iso14443b.state) {
329 case ISO14443B_STATE_SELECTED:
330 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
332 case ISO14443B_STATE_ATTRIB_SENT:
333 h->priv.tcl.state = TCL_STATE_RATS_SENT;
337 /* PUPI will be presented as ATS/historical bytes */
338 memcpy(h->priv.tcl.ats, h->l2h->priv.iso14443b.pupi, 4);
339 h->priv.tcl.ats_len = 4;
340 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
344 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
353 tcl_deselect(struct rfid_protocol_handle *h)
355 /* ISO 14443-4:2000(E) Section 8 */
357 unsigned char frame[3]; /* 3 bytes prologue, no information */
359 unsigned int rx_len = sizeof(rx);
360 unsigned int prlg_len;
361 struct tcl_handle *th = &h->priv.tcl;
363 if (th->state != TCL_STATE_ESTABLISHED) {
364 /* FIXME: not sure whether deselect is possible here,
365 * probably better send a HLTA? */
368 /* build DESELECT S-block */
369 ret = tcl_build_prologue_s(th, frame, &prlg_len);
373 ret = h->l2h->l2->fn.transcieve(h->l2h, frame, prlg_len, rx,
374 &rx_len, deactivation_fwt(h),
375 TCL_TRANSP_F_TX_CRC);
377 /* FIXME: retransmit, HLT(A|B) */
381 th->state = TCL_STATE_DESELECTED;
386 #define is_s_block(x) ((x & 0xc0) == 0xc0)
387 #define is_r_block(x) ((x & 0xc0) == 0x80)
388 #define is_i_block(x) ((x & 0xc0) == 0x00)
391 tcl_transcieve(struct rfid_protocol_handle *h,
392 const unsigned char *tx_data, unsigned int tx_len,
393 unsigned char *rx_data, unsigned int *rx_len,
394 unsigned int timeout, unsigned int flags)
397 unsigned char *tx_buf, *rx_buf;
398 unsigned int prlg_len;
399 struct tcl_handle *th = &h->priv.tcl;
402 unsigned int _tx_len, _timeout;
403 unsigned char wtx_resp[3];
405 if (tx_len > max_net_tx_framesize(th)) {
406 /* slow path: we need to use chaining */
410 tx_buf = malloc(tcl_prlg_len(th) + tx_len);
415 rx_buf = malloc(tcl_prlg_len(th) + *rx_len);
421 if (tcl_build_prologue_i(th, tx_buf, &prlg_len) < 0) {
425 memcpy(tx_buf + prlg_len, tx_data, tx_len);
427 /* intialize to data-to-be-transferred */
429 _tx_len = tx_len+prlg_len;
433 ret = h->l2h->l2->fn.transcieve(h->l2h, _tx, _tx_len,
434 rx_buf, rx_len, _timeout, 0);
438 if ((*rx_buf & 0x01) != h->priv.tcl.toggle) {
439 DEBUGP("response with wrong toggle bit\n");
446 if (is_r_block(*rx_buf)) {
447 unsigned int txed = _tx - tx_buf;
448 /* Handle ACK frame in case of chaining */
449 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
450 if (*(rx_buf+1) != h->priv.tcl.cid) {
451 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
455 /* set up parameters for next frame in chain */
457 /* move tx pointer by the amount of bytes transferred
460 _tx_len = (tx_len - txed);
461 if (_tx_len > max_net_tx_framesize(th)) {
462 /* not last frame in chain */
463 _tx_len = max_net_tx_framesize(th);
465 /* last frame in chain */
469 DEBUGP("Received ACK in response to last frame in "
470 "chain?!? Expected I-frame.\n");
474 } else if (is_s_block(*rx_buf)) {
476 unsigned int prlg_len;
478 /* Handle Wait Time Extension */
479 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
481 DEBUGP("S-Block with CID but short len\n");
485 if (*(rx_buf+1) != h->priv.tcl.cid) {
486 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
493 if ((*rx_buf & 0x30) != 0x30) {
494 DEBUGP("S-Block but not WTX?\n");
498 inf &= 0x3f; /* only lower 6 bits code WTXM */
499 if (inf == 0 || (inf >= 60 && inf <= 63)) {
500 DEBUGP("WTXM %u is RFU!\n", inf);
505 /* Acknowledge WTXM */
506 tcl_build_prologue_s(&h->priv.tcl, wtx_resp, &prlg_len);
507 /* set two bits that make this block a wtx */
509 wtx_resp[prlg_len] = inf;
511 _tx_len = prlg_len+1;
512 _timeout = th->fwt * inf;
514 /* start over with next transcieve */
515 goto do_tx; /* FIXME: do transcieve locally since we use
516 totally different buffer */
518 } else if (is_i_block(*rx_buf)) {
519 unsigned char *inf = rx_buf+1;
520 /* we're actually receiving payload data */
522 if (*rx_buf & TCL_PCB_CID_FOLLOWING) {
523 if (*(rx_buf+1) != h->priv.tcl.cid) {
524 DEBUGP("CID %u is not valid\n", *(rx_buf)+1);
529 if (*rx_buf & TCL_PCB_NAD_FOLLOWING) {
532 memcpy(rx_data, inf, *rx_len - (inf - rx_buf));
534 if (*rx_buf & 0x10) {
535 /* we're not the last frame in the chain, continue */
550 tcl_send(struct tcl_handle *th)
562 static struct rfid_protocol_handle *
563 tcl_init(struct rfid_layer2_handle *l2h)
565 struct rfid_protocol_handle *th;
566 unsigned int mru = l2h->rh->ah->mru;
568 th = malloc(sizeof(struct rfid_protocol_handle) + mru);
572 /* FIXME: mru should be attribute of layer2 (in case it adds/removes
574 memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
576 /* maximum received ats length equals mru of asic/reader */
577 th->priv.tcl.state = TCL_STATE_INITIAL;
578 th->priv.tcl.ats_len = mru;
579 th->priv.tcl.toggle = 1;
581 th->proto = &rfid_protocol_tcl;
583 th->priv.tcl.fsd = iso14443_fsd_approx(mru);
589 tcl_fini(struct rfid_protocol_handle *ph)
595 struct rfid_protocol rfid_protocol_tcl = {
596 .id = RFID_PROTOCOL_TCL,
597 .name = "ISO 14443-4 / T=CL",
600 .open = &tcl_connect,
601 .transcieve = &tcl_transcieve,
602 .close = &tcl_deselect,