3 * neard - Near Field Communication manager
5 * Copyright (C) 2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/socket.h>
31 #include <linux/socket.h>
32 #include <linux/nfc.h>
34 #include <near/plugin.h>
36 #include <near/types.h>
37 #include <near/adapter.h>
39 #include <near/ndef.h>
42 extern int mifare_read(uint32_t adapter_idx, uint32_t target_idx,
43 near_tag_io_cb cb, enum near_tag_sub_type tgt_subtype);
45 extern int mifare_check_presence(uint32_t adapter_idx, uint32_t target_idx,
46 near_tag_io_cb cb, enum near_tag_sub_type tgt_subtype);
48 extern int mifare_write(uint32_t adapter_idx, uint32_t target_idx,
49 struct near_ndef_message *ndef,
50 near_tag_io_cb cb, enum near_tag_sub_type tgt_subtype);
53 #define CMD_READ_SIZE 0x02
55 #define CMD_WRITE 0xA2
60 #define META_BLOCK_START 0
61 #define DATA_BLOCK_START 4
62 #define TYPE2_MAGIC 0xe1
64 #define TAG_DATA_CC(data) ((data) + 12)
65 #define TAG_DATA_LENGTH(cc) ((cc)[2] * 8)
66 #define TAG_DATA_NFC(cc) ((cc)[0] & TYPE2_MAGIC)
68 #define TYPE2_NOWRITE_ACCESS 0x0F
69 #define TYPE2_READWRITE_ACCESS 0x00
70 #define TAG_T2_WRITE_FLAG(cc) ((cc)[3] & TYPE2_NOWRITE_ACCESS)
72 #define NDEF_MAX_SIZE 0x30
74 #define CC_BLOCK_START 3
75 #define TYPE2_TAG_VER_1_0 0x10
76 #define TYPE2_DATA_SIZE_48 0x6
81 uint8_t data[BLOCK_SIZE];
82 } __attribute__((packed));
86 uint16_t current_block;
95 uint8_t current_block;
96 struct near_ndef_message *ndef;
107 static int t2_cookie_release(int err, void *data)
109 struct t2_cookie *cookie = data;
117 g_free(cookie->ndef->data);
119 g_free(cookie->ndef);
126 static int data_recv(uint8_t *resp, int length, void *data)
128 struct type2_tag *tag = data;
129 struct type2_cmd cmd;
131 size_t current_length, length_read, data_length;
132 uint32_t adapter_idx;
143 nfc_data = near_tag_get_data(tag->tag, &data_length);
144 adapter_idx = near_tag_get_adapter_idx(tag->tag);
146 length_read = length - NFC_HEADER_SIZE;
147 current_length = tag->current_block * BLOCK_SIZE;
148 if (current_length + length - NFC_HEADER_SIZE > data_length)
149 length_read = data_length - current_length;
151 memcpy(nfc_data + current_length, resp + NFC_HEADER_SIZE, length_read);
153 if (current_length + length_read == data_length) {
156 /* TODO parse tag->data for NDEFS, and notify target.c */
157 tag->current_block = 0;
161 records = near_tlv_parse(nfc_data, data_length);
162 near_tag_add_records(tag->tag, records, tag->cb, 0);
169 read_blocks = length / BLOCK_SIZE;
170 tag->current_block += read_blocks;
173 cmd.block = DATA_BLOCK_START + tag->current_block;
175 DBG("adapter %d", adapter_idx);
177 return near_adapter_send(adapter_idx,
178 (uint8_t *) &cmd, CMD_READ_SIZE,
182 static int data_read(struct type2_tag *tag)
184 struct type2_cmd cmd;
185 uint32_t adapter_idx;
189 tag->current_block = 0;
192 cmd.block = DATA_BLOCK_START;
194 adapter_idx = near_tag_get_adapter_idx(tag->tag);
196 return near_adapter_send(adapter_idx,
197 (uint8_t *) &cmd, CMD_READ_SIZE,
201 static int meta_recv(uint8_t *resp, int length, void *data)
203 struct t2_cookie *cookie = data;
204 struct near_tag *tag;
205 struct type2_tag *t2_tag;
221 cc = TAG_DATA_CC(resp + NFC_HEADER_SIZE);
223 /* Default to 48 bytes data size in case of blank tag */
224 err = near_tag_add_data(cookie->adapter_idx, cookie->target_idx,
225 NULL, (TAG_DATA_LENGTH(cc) ? TAG_DATA_LENGTH(cc) :
226 TYPE2_DATA_SIZE_48 << 3));
231 tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
237 t2_tag = g_try_malloc0(sizeof(struct type2_tag));
238 if (t2_tag == NULL) {
243 t2_tag->adapter_idx = cookie->adapter_idx;
244 t2_tag->cb = cookie->cb;
247 /* Set the ReadWrite flag */
248 if (TAG_T2_WRITE_FLAG(cc) == TYPE2_NOWRITE_ACCESS)
249 near_tag_set_ro(tag, TRUE);
251 near_tag_set_ro(tag, FALSE);
253 near_tag_set_memory_layout(tag, NEAR_TAG_MEMORY_STATIC);
255 if (TAG_DATA_NFC(cc) == 0) {
256 DBG("Mark as blank tag");
257 near_tag_set_blank(tag, TRUE);
259 near_tag_set_blank(tag, FALSE);
262 err = data_read(t2_tag);
266 return t2_cookie_release(err, cookie);
272 if (err < 0 && cookie->cb)
273 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
275 return t2_cookie_release(err, cookie);
278 static int nfctype2_read_meta(uint32_t adapter_idx, uint32_t target_idx,
281 struct type2_cmd cmd;
282 struct t2_cookie *cookie;
288 cmd.block = META_BLOCK_START;
290 cookie = g_try_malloc0(sizeof(struct t2_cookie));
294 cookie->adapter_idx = adapter_idx;
295 cookie->target_idx = target_idx;
298 err = near_adapter_send(adapter_idx, (uint8_t *) &cmd, CMD_READ_SIZE,
306 static int nfctype2_read(uint32_t adapter_idx,
307 uint32_t target_idx, near_tag_io_cb cb)
310 enum near_tag_sub_type tgt_subtype;
314 tgt_subtype = near_tag_get_subtype(adapter_idx, target_idx);
316 switch (tgt_subtype) {
317 case NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT:
318 err = nfctype2_read_meta(adapter_idx, target_idx, cb);
321 /* Specific Mifare read access */
322 case NEAR_TAG_NFC_T2_MIFARE_CLASSIC_1K:
323 case NEAR_TAG_NFC_T2_MIFARE_CLASSIC_4K:
324 err = mifare_read(adapter_idx, target_idx,
329 DBG("Unknown Tag Type 2 subtype %d", tgt_subtype);
337 static int data_write_resp(uint8_t *resp, int length, void *data)
340 struct t2_cookie *cookie = data;
341 struct type2_cmd cmd;
345 if (length < 0 || resp[0] != 0) {
350 if (cookie->ndef->offset > cookie->ndef->length) {
354 cookie->cb(cookie->adapter_idx, cookie->target_idx, 0);
356 return t2_cookie_release(0, cookie);
360 cmd.block = cookie->current_block;
361 cookie->current_block++;
363 if ((cookie->ndef->offset + BLOCK_SIZE) <
364 cookie->ndef->length) {
365 memcpy(cmd.data, cookie->ndef->data +
366 cookie->ndef->offset, BLOCK_SIZE);
367 cookie->ndef->offset += BLOCK_SIZE;
369 memcpy(cmd.data, cookie->ndef->data + cookie->ndef->offset,
370 cookie->ndef->length - cookie->ndef->offset);
371 cookie->ndef->offset = cookie->ndef->length + 1;
374 err = near_adapter_send(cookie->adapter_idx, (uint8_t *) &cmd,
375 sizeof(cmd), data_write_resp, cookie);
383 if (err < 0 && cookie->cb)
384 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
386 return t2_cookie_release(err, cookie);
389 static int data_write(uint32_t adapter_idx, uint32_t target_idx,
390 struct near_ndef_message *ndef,
393 struct type2_cmd cmd;
394 struct t2_cookie *cookie;
399 cookie = g_try_malloc0(sizeof(struct t2_cookie));
403 cookie->adapter_idx = adapter_idx;
404 cookie->target_idx = target_idx;
405 cookie->current_block = DATA_BLOCK_START;
410 cmd.block = cookie->current_block;
411 memcpy(cmd.data, cookie->ndef->data, BLOCK_SIZE);
412 cookie->ndef->offset += BLOCK_SIZE;
413 cookie->current_block++;
415 err = near_adapter_send(cookie->adapter_idx, (uint8_t *) &cmd,
416 sizeof(cmd), data_write_resp, cookie);
424 return t2_cookie_release(err, cookie);
427 static int nfctype2_write(uint32_t adapter_idx, uint32_t target_idx,
428 struct near_ndef_message *ndef,
431 struct near_tag *tag;
432 enum near_tag_sub_type tgt_subtype;
437 if (ndef == NULL || cb == NULL)
440 tag = near_tag_get_tag(adapter_idx, target_idx);
444 tgt_subtype = near_tag_get_subtype(adapter_idx, target_idx);
446 switch (tgt_subtype) {
447 case NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT:
449 * This check is valid for only static tags.
450 * Max data length on Type 2 Tag
451 * including TLV's is NDEF_MAX_SIZE
453 if (near_tag_get_memory_layout(tag) == NEAR_TAG_MEMORY_STATIC) {
454 if ((ndef->length + 3) > NDEF_MAX_SIZE) {
455 near_error("not enough space on tag");
460 err = data_write(adapter_idx, target_idx, ndef, cb);
462 /* Specific Mifare write access */
463 case NEAR_TAG_NFC_T2_MIFARE_CLASSIC_1K:
464 case NEAR_TAG_NFC_T2_MIFARE_CLASSIC_4K:
465 err = mifare_write(adapter_idx, target_idx, ndef,
469 DBG("Unknown TAG Type 2 subtype %d", tgt_subtype);
477 static int check_presence(uint8_t *resp, int length, void *data)
479 struct t2_cookie *cookie = data;
488 cookie->cb(cookie->adapter_idx,
489 cookie->target_idx, err);
491 return t2_cookie_release(err, cookie);
494 static int nfctype2_check_presence(uint32_t adapter_idx, uint32_t target_idx,
497 struct type2_cmd cmd;
498 struct t2_cookie *cookie;
499 enum near_tag_sub_type tgt_subtype;
504 tgt_subtype = near_tag_get_subtype(adapter_idx, target_idx);
506 switch (tgt_subtype) {
507 case NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT:
509 cmd.block = META_BLOCK_START;
511 cookie = g_try_malloc0(sizeof(struct t2_cookie));
515 cookie->adapter_idx = adapter_idx;
516 cookie->target_idx = target_idx;
519 err = near_adapter_send(adapter_idx, (uint8_t *) &cmd,
520 CMD_READ_SIZE, check_presence, cookie);
525 /* Specific Mifare check presence */
526 case NEAR_TAG_NFC_T2_MIFARE_CLASSIC_1K:
527 case NEAR_TAG_NFC_T2_MIFARE_CLASSIC_4K:
528 err = mifare_check_presence(adapter_idx, target_idx,
533 DBG("Unknown TAG Type 2 subtype %d", tgt_subtype);
541 return t2_cookie_release(err, cookie);
544 static int format_resp(uint8_t *resp, int length, void *data)
547 struct t2_cookie *cookie = data;
548 struct near_tag *tag;
552 if (length < 0 || resp[0] != 0) {
557 tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
563 DBG("Done formatting");
564 near_tag_set_blank(tag, FALSE);
568 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
570 return t2_cookie_release(err, cookie);
573 static int nfctype2_format(uint32_t adapter_idx, uint32_t target_idx,
576 struct type2_cmd cmd;
577 struct t2_cookie *cookie;
578 struct near_ndef_message *cc_ndef;
579 struct type2_cc *t2_cc;
580 struct near_tag *tag;
581 enum near_tag_sub_type tgt_subtype;
586 tag = near_tag_get_tag(adapter_idx, target_idx);
591 tgt_subtype = near_tag_get_subtype(adapter_idx, target_idx);
593 if (tgt_subtype != NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT) {
594 DBG("Unknown Tag Type 2 subtype %d", tgt_subtype);
598 t2_cc = g_try_malloc0(sizeof(struct type2_cc));
599 cc_ndef = g_try_malloc0(sizeof(struct near_ndef_message));
600 cookie = g_try_malloc0(sizeof(struct t2_cookie));
602 if (t2_cc == NULL || cc_ndef == NULL || cookie == NULL) {
607 t2_cc->magic = TYPE2_MAGIC;
608 t2_cc->version = TYPE2_TAG_VER_1_0;
609 t2_cc->mem_size = TYPE2_DATA_SIZE_48;
610 t2_cc->read_write = TYPE2_READWRITE_ACCESS;
612 cookie->adapter_idx = adapter_idx;
613 cookie->target_idx = target_idx;
614 cookie->current_block = CC_BLOCK_START;
615 cookie->ndef = cc_ndef;
616 cookie->ndef->data = (uint8_t *) t2_cc;
620 cmd.block = CC_BLOCK_START;
621 memcpy(cmd.data, (uint8_t *) t2_cc, BLOCK_SIZE);
623 err = near_adapter_send(cookie->adapter_idx, (uint8_t *) &cmd,
624 sizeof(cmd), format_resp, cookie);
636 static struct near_tag_driver type2_driver = {
637 .type = NFC_PROTO_MIFARE,
638 .priority = NEAR_TAG_PRIORITY_DEFAULT,
639 .read = nfctype2_read,
640 .write = nfctype2_write,
641 .check_presence = nfctype2_check_presence,
642 .format = nfctype2_format,
645 static int nfctype2_init(void)
649 return near_tag_driver_register(&type2_driver);
652 static void nfctype2_exit(void)
656 near_tag_driver_unregister(&type2_driver);
659 NEAR_PLUGIN_DEFINE(nfctype2, "NFC Forum Type 2 tags support", VERSION,
660 NEAR_PLUGIN_PRIORITY_HIGH, nfctype2_init, nfctype2_exit)