1 /* ISO 14443-3 B anticollision implementation
3 * (C) 2005-2006 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
27 #include <librfid/rfid.h>
28 #include <librfid/rfid_layer2.h>
29 #include <librfid/rfid_reader.h>
30 #include <librfid/rfid_layer2_iso14443b.h>
31 #include <librfid/rfid_protocol.h>
33 #include "rfid_iso14443_common.h"
35 #define ATQB_TIMEOUT ((256*10e6/ISO14443_FREQ_SUBCARRIER) \
36 +(200*10e6/ISO14443_FREQ_SUBCARRIER))
39 #define ATQB_TIMEOUT 1
42 fwi_to_fwt(struct rfid_layer2_handle *h, unsigned int *fwt, unsigned int fwi)
44 unsigned int multiplier, tmp;
50 /* According to ISO 14443-3:200(E), Chapter 7.9.4.3, the forumala is
51 * (256 * 16 / fC) * 2^fwi We avoid floating point computations by
52 * shifting everything into the microsecond range. In integer
53 * calculations 1000000*256*16/13560000 evaluates to 302 (instead of
54 * 302.064897), which provides sufficient precision, IMHO. The max
55 * result is 302 * 16384 (4947968), which fits well within the 31/32
56 * bit range of an integer */
58 multiplier = 1 << fwi; /* 2 to the power of fwi */
60 tmp = (unsigned int) 1000000 * 256 * 16;
62 return (tmp / h->rh->ah->asic->fc) * multiplier;
66 parse_atqb(struct rfid_layer2_handle *h, struct iso14443b_atqb *atqb)
70 if (atqb->fifty != 0x50)
73 if (atqb->protocol_info.fo & 0x01)
74 h->priv.iso14443b.flags |= ISO14443B_CID_SUPPORTED;
75 if (atqb->protocol_info.fo & 0x02)
76 h->priv.iso14443b.flags |= ISO14443B_NAD_SUPPORTED;
78 ret = fwi_to_fwt(h, &h->priv.iso14443b.fwt, atqb->protocol_info.fwi);
80 DEBUGP("invalid fwi %u\n", atqb->protocol_info.fwi);
84 if (atqb->protocol_info.protocol_type == 0x1) {
85 DEBUGP("we have a T=CL compliant PICC\n");
86 h->priv.iso14443b.tcl_capable = 1;
87 h->proto_supported = (1 << RFID_PROTOCOL_TCL);
89 DEBUGP("we have a T!=CL PICC\n");
90 h->priv.iso14443b.tcl_capable = 0;
91 /* FIXME: what protocols do we support? */
94 iso14443_fsdi_to_fsd(&h->priv.iso14443b.fsc,
95 atqb->protocol_info.max_frame_size);
97 /* FIXME: speed capability */
99 memcpy(h->uid, atqb->pupi, sizeof(atqb->pupi));
100 h->uid_len = sizeof(atqb->pupi);
106 send_reqb(struct rfid_layer2_handle *h, unsigned char afi,
107 unsigned int is_wup, unsigned int num_initial_slots)
110 unsigned char reqb[3];
111 struct iso14443b_atqb atqb;
112 unsigned int atqb_len = sizeof(atqb);
113 unsigned int num_slot_idx = num_initial_slots;
118 for (num_slot_idx = num_initial_slots; num_slot_idx <= 4;
120 reqb[2] = num_slot_idx & 0x07;
124 ret = h->rh->reader->transceive(h->rh, RFID_14443B_FRAME_REGULAR,
126 (unsigned char *)&atqb,
127 &atqb_len, ATQB_TIMEOUT, 0);
128 h->priv.iso14443b.state = ISO14443B_STATE_REQB_SENT;
130 DEBUGP("error during transceive of REQB/WUBP\n");
134 /* FIXME: send N-1 slot marker frames */
136 if (atqb_len != sizeof(atqb)) {
137 DEBUGP("error: atqb_len = %u instead of %Zu\n",
138 atqb_len, sizeof(atqb));
142 /* FIXME: how to detect a collission at 14443B ? I guess we
143 * can only rely on the CRC checking (CRCErr in ErrorFlag
146 if (parse_atqb(h, &atqb) >= 0) {
147 h->priv.iso14443b.state = ISO14443B_STATE_ATQB_RCVD;
155 static inline unsigned int mbli_to_mbl(struct rfid_layer2_handle *h,
158 return (h->priv.iso14443b.fsc * 2 ^ (mbli-1));
162 transceive_attrib(struct rfid_layer2_handle *h, const unsigned char *inf,
163 unsigned int inf_len, unsigned char *rx_data, unsigned int *rx_len)
166 struct iso14443b_attrib_hdr attrib;
170 struct iso14443b_attrib_hdr *attrib = &_attrib_buf.attrib;
171 unsigned char rx_buf[256];
175 DEBUGP("fsd is %u\n", h->priv.iso14443b.fsd);
176 if (rx_len >= rx_len-1)
179 /* initialize attrib frame */
180 memset(&_attrib_buf, 0, sizeof(_attrib_buf));
182 memcpy((unsigned char *)attrib+sizeof(*attrib), inf, inf_len);
184 attrib->one_d = 0x1d;
185 memcpy(attrib->identifier, h->uid, 4);
187 /* FIXME: do we want to change TR0/TR1 from its default ? */
188 /* FIXME: do we want to change SOF/EOF from its default ? */
190 ret = iso14443_fsd_to_fsdi(&fsdi, h->priv.iso14443b.fsd);
192 DEBUGP("unable to map FSD(%u) to FSDI\n",
193 h->priv.iso14443b.fsd);
196 attrib->param2.fsdi = fsdi;
198 /* FIXME: spd_in / spd_out */
199 if (h->priv.iso14443b.tcl_capable == 1)
200 attrib->param3.protocol_type = 0x1;
202 attrib->param4.cid = h->priv.iso14443b.cid & 0xf;
204 *rx_len = *rx_len + 1;
205 ret = h->rh->reader->transceive(h->rh, RFID_14443B_FRAME_REGULAR,
206 (unsigned char *) attrib,
207 sizeof(*attrib)+inf_len,
208 rx_buf, rx_len, h->priv.iso14443b.fwt,
210 h->priv.iso14443b.state = ISO14443B_STATE_ATTRIB_SENT;
212 DEBUGP("transceive problem\n");
216 if ((rx_buf[0] & 0x0f) != h->priv.iso14443b.cid) {
217 DEBUGP("ATTRIB response with invalid CID %u (should be %u)\n",
218 rx_buf[0] & 0x0f, h->priv.iso14443b.cid);
223 h->priv.iso14443b.state = ISO14443B_STATE_SELECTED;
225 h->priv.iso14443b.mbl = mbli_to_mbl(h, (rx_data[0] & 0xf0) >> 4);
227 *rx_len = *rx_len - 1;
228 memcpy(rx_data, rx_buf+1, *rx_len);
237 iso14443b_hltb(struct rfid_layer2_handle *h)
240 unsigned char hltb[5];
241 unsigned char hltb_resp[1];
242 unsigned int hltb_len = 1;
245 memcpy(hltb+1, h->uid, 4);
247 ret = h->rh->reader->transceive(h->rh, RFID_14443B_FRAME_REGULAR,
249 hltb_resp, &hltb_len,
250 h->priv.iso14443b.fwt, 0);
251 h->priv.iso14443b.state = ISO14443B_STATE_HLTB_SENT;
253 DEBUGP("transceive problem\n");
257 if (hltb_len != 1 || hltb_resp[0] != 0x00) {
258 DEBUGP("bad HLTB response\n");
261 h->priv.iso14443b.state = ISO14443B_STATE_HALTED;
267 iso14443b_anticol(struct rfid_layer2_handle *handle)
269 unsigned char afi = 0; /* FIXME */
271 unsigned char buf[255];
272 unsigned int buf_len = sizeof(buf);
274 ret = send_reqb(handle, afi, 0, 0);
278 ret = transceive_attrib(handle, NULL, 0, buf, &buf_len);
285 static struct rfid_layer2_handle *
286 iso14443b_init(struct rfid_reader_handle *rh)
289 struct rfid_layer2_handle *h = malloc_layer2_handle(sizeof(*h));
293 h->l2 = &rfid_layer2_iso14443b;
295 h->priv.iso14443b.state = ISO14443B_STATE_NONE;
297 /* FIXME: if we want to support multiple PICC's, we need some
298 * fancy allocation scheme for CID's */
299 h->priv.iso14443b.cid = 0;
301 h->priv.iso14443b.fsd = iso14443_fsd_approx(rh->ah->mru);
302 DEBUGP("fsd is %u\n", h->priv.iso14443b.fsd);
304 /* 14443-3 Section 7.1.6 */
305 h->priv.iso14443b.tr0 = (256/ISO14443_FREQ_SUBCARRIER)*10e6;
306 h->priv.iso14443b.tr1 = (200/ISO14443_FREQ_SUBCARRIER)*10e6;
308 ret = h->rh->reader->iso14443b.init(h->rh);
310 DEBUGP("error during reader 14443b init\n");
311 free_layer2_handle(h);
319 iso14443b_fini(struct rfid_layer2_handle *handle)
321 free_layer2_handle(handle);
326 iso14443b_transceive(struct rfid_layer2_handle *handle,
327 enum rfid_frametype frametype,
328 const unsigned char *tx_buf, unsigned int tx_len,
329 unsigned char *rx_buf, unsigned int *rx_len,
330 u_int64_t timeout, unsigned int flags)
332 DEBUGP("transcieving %u bytes, expecting max %u\n", tx_len, *rx_len);
333 return handle->rh->reader->transceive(handle->rh, frametype,
335 rx_buf, rx_len, timeout, flags);
339 iso14443b_getopt(struct rfid_layer2_handle *handle,
340 int optname, void *optval, unsigned int *optlen)
342 unsigned int *opt_ui = optval;
345 case RFID_OPT_14443B_CID:
346 *opt_ui = handle->priv.iso14443b.cid;
348 case RFID_OPT_14443B_FSC:
349 *opt_ui = handle->priv.iso14443b.fsc;
351 case RFID_OPT_14443B_FSD:
352 *opt_ui = handle->priv.iso14443b.fsd;
354 case RFID_OPT_14443B_FWT:
355 *opt_ui = handle->priv.iso14443b.fwt;
357 case RFID_OPT_14443B_TR0:
358 *opt_ui = handle->priv.iso14443b.tr0;
360 case RFID_OPT_14443B_TR1:
361 *opt_ui = handle->priv.iso14443b.tr1;
371 iso14443b_setopt(struct rfid_layer2_handle *handle,
372 int optname, const void *optval, unsigned int optlen)
374 const unsigned int *opt_ui = optval;
377 case RFID_OPT_14443B_CID:
378 handle->priv.iso14443b.cid = (*opt_ui & 0xf);
388 const struct rfid_layer2 rfid_layer2_iso14443b = {
389 .id = RFID_LAYER2_ISO14443B,
390 .name = "ISO 14443-3 B",
392 .init = &iso14443b_init,
393 .open = &iso14443b_anticol,
394 .transceive = &iso14443b_transceive,
395 .close = &iso14443b_hltb,
396 .fini = &iso14443b_fini,
397 .getopt = &iso14443b_getopt,
398 .setopt = &iso14443b_setopt,