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 /* ISO 14443-4:2000(E) Section 5.2.5. */
44 return (256 * 16 / h->l2h->rh->ah->fc) * (2 ^ sfgi);
47 static unsigned int fwi_to_fwt(struct rfid_protocol_handle *h,
50 /* ISO 14443-4:2000(E) Section 7.2. */
51 return (256*16 / h->l2h->rh->ah->fc) * (2 ^ fwi);
54 #define activation_fwt(x) (65536 / x->l2h->rh->ah->fc)
55 #define deactivation_fwt(x) activation_fwt(x)
58 tcl_parse_ats(struct rfid_protocol_handle *h,
59 unsigned char *ats, unsigned int size)
61 unsigned char len = ats[0];
65 if (len == 0 || size == 0)
72 /* FIXME: assume some default values */
75 h->priv.tcl.sfgt = sfgi_to_sfgt(h, 0);
76 if (1 /* FIXME: is_iso14443a */) {
77 /* Section 7.2: fwi default for type A is 4 */
78 h->priv.tcl.fwt = fwi_to_fwt(h, 4);
80 /* Section 7.2: fwi for type B is always in ATQB */
86 /* guarateed to be at least 2 bytes in size */
91 iso14443_fsdi_to_fsd(&h->priv.tcl.fsc, t0 & 0x0f);
94 /* TA is transmitted */
95 h->priv.tcl.ta = *cur++;
99 /* TB is transmitted */
100 h->priv.tcl.sfgt = sfgi_to_sfgt(h, *cur & 0x0f);
101 h->priv.tcl.fwt = fwi_to_fwt(h, (*cur & 0xf0) >> 4);
106 /* TC is transmitted */
108 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
110 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
114 h->priv.tcl.historical_len = (ats+len) - cur;
115 h->priv.tcl.historical_bytes = cur;
121 /* request an ATS from the PICC */
123 tcl_request_ats(struct rfid_protocol_handle *h)
126 unsigned char rats[2];
129 if (h->priv.tcl.state != TCL_STATE_INITIAL)
132 ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.tcl.fsd);
134 DEBUGP("unable to encode FSD of %u as FSDI\n", h->priv.tcl.fsd);
139 rats[1] = (h->priv.tcl.cid & 0x0f) | ((fsdi << 4) & 0xf0);
141 /* transcieve (with CRC) */
142 ret = h->l2h->l2->fn.transcieve(h->l2h, rats, 2, h->priv.tcl.ats,
143 &h->priv.tcl.ats_len, activation_fwt(h),
144 TCL_TRANSP_F_TX_CRC);
146 DEBUGP("transcieve of rats failed\n");
147 h->priv.tcl.state = TCL_STATE_RATS_SENT;
148 /* FIXME: retransmit */
151 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
153 ret = tcl_parse_ats(h, h->priv.tcl.ats, h->priv.tcl.ats_len);
155 DEBUGP("parsing of ats failed\n");
161 /* start a PSS run (autimatically configure highest possible speed */
163 tcl_do_pss(struct rfid_protocol_handle *h)
165 unsigned char ppss[3];
166 unsigned char pps_response[1];
168 if (h->priv.tcl.state != TCL_STATE_ATS_RCVD)
171 /* ISO 14443-4:2000(E) Section 5.3. */
173 ppss[0] = 0xd0 & (h->priv.tcl.cid & 0x0f);
176 //ppss[2] = 0x00 & foo;
182 h->priv.tcl.state = TCL_STATE_ESTABLISHED;
187 tcl_build_prologue2(struct tcl_handle *th,
188 unsigned char *prlg, unsigned int *prlg_len,
195 if (th->flags & TCL_HANDLE_F_CID_USED) {
196 /* ISO 14443-4:2000(E) Section 7.1.1.2 */
197 *prlg |= TCL_PCB_CID_FOLLOWING;
199 prlg[*prlg_len] = th->cid & 0x0f;
202 /* nad only for I-block (0xc0 == 00) */
203 if ((th->flags & TCL_HANDLE_F_NAD_USED) &&
204 ((pcb & 0xc0) == 0x00)) {
205 /* ISO 14443-4:2000(E) Section 7.1.1.3 */
206 /* FIXME: in case of chaining only for first frame */
207 *prlg |= TCL_PCB_NAD_FOLLOWING;
208 prlg[*prlg_len] = th->nad;
216 tcl_build_prologue_i(struct tcl_handle *th,
217 unsigned char *prlg, unsigned int *prlg_len)
219 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
220 return tcl_build_prologue2(th, prlg, prlg_len, 0x02);
224 tcl_build_prologue_r(struct tcl_handle *th,
225 unsigned char *prlg, unsigned int *prlg_len,
228 unsigned char pcb = 0xa2;
229 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
234 return tcl_build_prologue2(th, prlg, prlg_len, pcb);
238 tcl_build_prologue_s(struct tcl_handle *th,
239 unsigned char *prlg, unsigned int *prlg_len)
241 /* ISO 14443-4:2000(E) Section 7.1.1.1 */
243 /* the only S-block from PCD->PICC is DESELECT: */
244 return tcl_build_prologue2(th, prlg, prlg_len, 0xc2);
247 /* FIXME: WTXM implementation */
249 static int tcl_prlg_len(struct tcl_handle *th)
253 if (th->flags & TCL_HANDLE_F_CID_USED)
256 if (th->flags & TCL_HANDLE_F_NAD_USED)
262 #define max_net_tx_framesize(x) (x->fsc - tcl_prlg_len(x))
265 tcl_connect(struct rfid_protocol_handle *h)
269 if (h->priv.tcl.state != TCL_STATE_DESELECTED &&
270 h->priv.tcl.state != TCL_STATE_INITIAL)
273 switch (h->l2h->l2->id) {
274 case RFID_LAYER2_ISO14443A:
275 /* Start Type A T=CL Activation Sequence */
276 ret = tcl_request_ats(h);
286 case RFID_LAYER2_ISO14443B:
287 /* initialized T=CL state from Type B Activation Data */
288 h->priv.tcl.cid = h->l2h->priv.iso14443b.cid;
289 h->priv.tcl.fsc = h->l2h->priv.iso14443b.fsc;
290 h->priv.tcl.fsd = h->l2h->priv.iso14443b.fsd;
291 h->priv.tcl.fwt = h->l2h->priv.iso14443b.fwt;
293 /* what about ta? sfgt? */
295 if (h->l2h->priv.iso14443b.flags & ISO14443B_CID_SUPPORTED)
296 h->priv.tcl.flags |= TCL_HANDLE_F_CID_SUPPORTED;
297 if (h->l2h->priv.iso14443b.flags & ISO14443B_NAD_SUPPORTED)
298 h->priv.tcl.flags |= TCL_HANDLE_F_NAD_SUPPORTED;
300 switch (h->l2h->priv.iso14443b.state) {
301 case ISO14443B_STATE_SELECTED:
302 h->priv.tcl.state = TCL_STATE_ATS_RCVD;
304 case ISO14443B_STATE_ATTRIB_SENT:
305 h->priv.tcl.state = TCL_STATE_RATS_SENT;
309 /* PUPI will be presented as ATS/historical bytes */
310 memcpy(h->priv.tcl.ats, h->l2h->priv.iso14443b.pupi, 4);
311 h->priv.tcl.ats_len = 4;
312 h->priv.tcl.historical_bytes = h->priv.tcl.ats;
316 DEBUGP("unsupported l2: %u\n", h->l2h->l2->id);
325 tcl_deselect(struct rfid_protocol_handle *h)
327 /* ISO 14443-4:2000(E) Section 8 */
329 unsigned char frame[3]; /* 3 bytes prologue, no information */
331 unsigned int rx_len = sizeof(rx);
332 unsigned int prlg_len;
333 struct tcl_handle *th = &h->priv.tcl;
335 if (th->state != TCL_STATE_ESTABLISHED) {
336 /* FIXME: not sure whether deselect is possible here,
337 * probably better send a HLTA? */
340 /* build DESELECT S-block */
341 ret = tcl_build_prologue_s(th, frame, &prlg_len);
345 ret = h->l2h->l2->fn.transcieve(h->l2h, frame, prlg_len, rx,
346 &rx_len, deactivation_fwt(h),
347 TCL_TRANSP_F_TX_CRC);
349 /* FIXME: retransmit, HLT(A|B) */
353 th->state = TCL_STATE_DESELECTED;
359 tcl_transcieve(struct rfid_protocol_handle *h,
360 const unsigned char *tx_data, unsigned int tx_len,
361 unsigned char *rx_data, unsigned int *rx_len,
362 unsigned int timeout, unsigned int flags)
365 unsigned char *tx_buf, *rx_buf;
366 unsigned int prlg_len;
367 struct tcl_handle *th = &h->priv.tcl;
369 if (tx_len > max_net_tx_framesize(th)) {
370 /* slow path: we need to use chaining */
374 tx_buf = malloc(tcl_prlg_len(th) + tx_len);
379 rx_buf = malloc(tcl_prlg_len(th) + *rx_len);
385 if (tcl_build_prologue_i(th, tx_buf, &prlg_len) < 0) {
389 memcpy(tx_buf + prlg_len, tx_data, tx_len);
391 ret = h->l2h->l2->fn.transcieve(h->l2h, tx_buf, tx_len+prlg_len,
392 rx_buf, rx_len, th->fwt, 0);
397 memcpy(rx_data, rx_buf, *rx_len);
409 tcl_send(struct tcl_handle *th)
421 static struct rfid_protocol_handle *
422 tcl_init(struct rfid_layer2_handle *l2h)
424 struct rfid_protocol_handle *th;
425 unsigned int mru = l2h->rh->ah->mru;
427 th = malloc(sizeof(struct rfid_protocol_handle) + mru);
431 /* FIXME: mru should be attribute of layer2 (in case it adds/removes
433 memset(th, 0, sizeof(struct rfid_protocol_handle) + mru);
435 /* maximum received ats length equals mru of asic/reader */
436 th->priv.tcl.state = TCL_STATE_INITIAL;
437 th->priv.tcl.ats_len = mru;
439 th->proto = &rfid_protocol_tcl;
441 th->priv.tcl.fsd = iso14443_fsd_approx(mru);
447 tcl_fini(struct rfid_protocol_handle *ph)
453 struct rfid_protocol rfid_protocol_tcl = {
454 .id = RFID_PROTOCOL_TCL,
455 .name = "ISO 14443-4 / T=CL",
458 .open = &tcl_connect,
459 .transcieve = &tcl_transcieve,
460 .close = &tcl_deselect,