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);
46 #define CMD_READ_SIZE 0x02
48 #define CMD_WRITE 0xA2
53 #define META_BLOCK_START 0
54 #define DATA_BLOCK_START 4
55 #define TYPE2_MAGIC 0xe1
57 #define TAG_DATA_CC(data) ((data) + 12)
58 #define TAG_DATA_LENGTH(cc) ((cc)[2] * 8)
59 #define TAG_DATA_NFC(cc) ((cc)[0] & TYPE2_MAGIC)
61 #define TYPE2_NOWRITE_ACCESS 0x0F
62 #define TYPE2_READWRITE_ACCESS 0x00
63 #define TAG_T2_WRITE_FLAG(cc) ((cc)[3] & TYPE2_NOWRITE_ACCESS)
65 #define NDEF_MAX_SIZE 0x30
67 #define CC_BLOCK_START 3
68 #define TYPE2_TAG_VER_1_0 0x10
69 #define TYPE2_DATA_SIZE_48 0x6
74 uint8_t data[BLOCK_SIZE];
75 } __attribute__((packed));
79 uint16_t current_block;
88 uint8_t current_block;
89 struct near_ndef_message *ndef;
100 static void t2_cookie_release(struct t2_cookie *cookie)
106 g_free(cookie->ndef->data);
108 g_free(cookie->ndef);
113 static int data_recv(uint8_t *resp, int length, void *data)
115 struct type2_tag *tag = data;
116 struct type2_cmd cmd;
118 size_t current_length, length_read, data_length;
119 uint32_t adapter_idx;
130 nfc_data = near_tag_get_data(tag->tag, &data_length);
131 adapter_idx = near_tag_get_adapter_idx(tag->tag);
133 length_read = length - NFC_HEADER_SIZE;
134 current_length = tag->current_block * BLOCK_SIZE;
135 if (current_length + length - NFC_HEADER_SIZE > data_length)
136 length_read = data_length - current_length;
138 memcpy(nfc_data + current_length, resp + NFC_HEADER_SIZE, length_read);
140 if (current_length + length_read == data_length) {
143 /* TODO parse tag->data for NDEFS, and notify target.c */
144 tag->current_block = 0;
148 records = near_tlv_parse(nfc_data, data_length);
149 near_tag_add_records(tag->tag, records, tag->cb, 0);
156 read_blocks = length / BLOCK_SIZE;
157 tag->current_block += read_blocks;
160 cmd.block = DATA_BLOCK_START + tag->current_block;
162 DBG("adapter %d", adapter_idx);
164 return near_adapter_send(adapter_idx,
165 (uint8_t *)&cmd, CMD_READ_SIZE,
169 static int data_read(struct type2_tag *tag)
171 struct type2_cmd cmd;
172 uint32_t adapter_idx;
176 tag->current_block = 0;
179 cmd.block = DATA_BLOCK_START;
181 adapter_idx = near_tag_get_adapter_idx(tag->tag);
183 return near_adapter_send(adapter_idx,
184 (uint8_t *)&cmd, CMD_READ_SIZE,
188 static int meta_recv(uint8_t *resp, int length, void *data)
190 struct t2_cookie *cookie = data;
191 struct near_tag *tag;
192 struct type2_tag *t2_tag;
208 cc = TAG_DATA_CC(resp + NFC_HEADER_SIZE);
210 /* Default to 48 bytes data size in case of blank tag */
211 err = near_tag_add_data(cookie->adapter_idx, cookie->target_idx,
212 NULL, (TAG_DATA_LENGTH(cc) ? TAG_DATA_LENGTH(cc) :
213 TYPE2_DATA_SIZE_48 << 3));
218 tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
224 t2_tag = g_try_malloc0(sizeof(struct type2_tag));
225 if (t2_tag == NULL) {
230 t2_tag->adapter_idx = cookie->adapter_idx;
231 t2_tag->cb = cookie->cb;
234 /* Set the ReadWrite flag */
235 if (TAG_T2_WRITE_FLAG(cc) == TYPE2_NOWRITE_ACCESS)
236 near_tag_set_ro(tag, TRUE);
238 near_tag_set_ro(tag, FALSE);
240 near_tag_set_memory_layout(tag, NEAR_TAG_MEMORY_STATIC);
242 if (TAG_DATA_NFC(cc) == 0) {
243 DBG("Mark as blank tag");
244 near_tag_set_blank(tag, TRUE);
246 near_tag_set_blank(tag, FALSE);
249 err = data_read(t2_tag);
253 t2_cookie_release(cookie);
258 if (err < 0 && cookie->cb)
259 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
261 t2_cookie_release(cookie);
266 static int nfctype2_read_meta(uint32_t adapter_idx, uint32_t target_idx,
269 struct type2_cmd cmd;
270 struct t2_cookie *cookie;
276 cmd.block = META_BLOCK_START;
278 cookie = g_try_malloc0(sizeof(struct t2_cookie));
282 cookie->adapter_idx = adapter_idx;
283 cookie->target_idx = target_idx;
286 err = near_adapter_send(adapter_idx, (uint8_t *)&cmd, CMD_READ_SIZE,
294 static int nfctype2_read(uint32_t adapter_idx,
295 uint32_t target_idx, near_tag_io_cb cb)
298 enum near_tag_sub_type tgt_subtype;
302 tgt_subtype = near_tag_get_subtype(adapter_idx, target_idx);
304 switch (tgt_subtype) {
305 case NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT:
306 err = nfctype2_read_meta(adapter_idx, target_idx, cb);
309 /* Specific Mifare read access */
310 case NEAR_TAG_NFC_T2_MIFARE_CLASSIC_1K:
311 case NEAR_TAG_NFC_T2_MIFARE_CLASSIC_4K:
312 err= mifare_read( adapter_idx, target_idx,
317 DBG("Unknown TAG Type 2 subtype (%d)", tgt_subtype);
325 static int data_write_resp(uint8_t *resp, int length, void *data)
328 struct t2_cookie *cookie = data;
329 struct type2_cmd cmd;
333 if (length < 0 || resp[0] != 0) {
338 if (cookie->ndef->offset > cookie->ndef->length) {
342 cookie->cb(cookie->adapter_idx, cookie->target_idx, 0);
344 t2_cookie_release(cookie);
350 cmd.block = cookie->current_block;
351 cookie->current_block++;
353 if ((cookie->ndef->offset + BLOCK_SIZE) <
354 cookie->ndef->length) {
355 memcpy(cmd.data, cookie->ndef->data +
356 cookie->ndef->offset, BLOCK_SIZE);
357 cookie->ndef->offset += BLOCK_SIZE;
359 memcpy(cmd.data, cookie->ndef->data + cookie->ndef->offset,
360 cookie->ndef->length - cookie->ndef->offset);
361 cookie->ndef->offset = cookie->ndef->length + 1;
364 err = near_adapter_send(cookie->adapter_idx, (uint8_t *)&cmd,
365 sizeof(cmd), data_write_resp, cookie);
374 if (err < 0 && cookie->cb)
375 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
377 t2_cookie_release(cookie);
382 static int data_write(uint32_t adapter_idx, uint32_t target_idx,
383 struct near_ndef_message *ndef,
386 struct type2_cmd cmd;
387 struct t2_cookie *cookie;
392 cookie = g_try_malloc0(sizeof(struct t2_cookie));
396 cookie->adapter_idx = adapter_idx;
397 cookie->target_idx = target_idx;
398 cookie->current_block = DATA_BLOCK_START;
403 cmd.block = cookie->current_block;
404 memcpy(cmd.data, cookie->ndef->data, BLOCK_SIZE);
405 cookie->ndef->offset += BLOCK_SIZE;
406 cookie->current_block++;
408 err = near_adapter_send(cookie->adapter_idx, (uint8_t *)&cmd,
409 sizeof(cmd), data_write_resp, cookie);
417 t2_cookie_release(cookie);
422 static int nfctype2_write(uint32_t adapter_idx, uint32_t target_idx,
423 struct near_ndef_message *ndef,
426 struct near_tag *tag;
427 enum near_tag_sub_type tgt_subtype;
431 if (ndef == NULL || cb == NULL)
434 tag = near_tag_get_tag(adapter_idx, target_idx);
438 tgt_subtype = near_tag_get_subtype(adapter_idx, target_idx);
440 if (tgt_subtype != NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT) {
441 DBG("Unknown Tag Type 2 subtype (%d)", tgt_subtype);
445 /* This check is valid for only static tags.
446 * Max data length on Type 2 Tag including TLV's is NDEF_MAX_SIZE */
447 if (near_tag_get_memory_layout(tag) == NEAR_TAG_MEMORY_STATIC) {
448 if ((ndef->length + 3) > NDEF_MAX_SIZE) {
449 near_error("not enough space on tag");
454 return data_write(adapter_idx, target_idx, ndef, cb);
457 static int check_presence(uint8_t *resp, int length, void *data)
459 struct t2_cookie *cookie = data;
468 cookie->cb(cookie->adapter_idx,
469 cookie->target_idx, err);
471 t2_cookie_release(cookie);
476 static int nfctype2_check_presence(uint32_t adapter_idx, uint32_t target_idx,
479 struct type2_cmd cmd;
480 struct t2_cookie *cookie;
486 cmd.block = META_BLOCK_START;
488 cookie = g_try_malloc0(sizeof(struct t2_cookie));
492 cookie->adapter_idx = adapter_idx;
493 cookie->target_idx = target_idx;
496 err = near_adapter_send(adapter_idx, (uint8_t *)&cmd, CMD_READ_SIZE,
497 check_presence, cookie);
504 t2_cookie_release(cookie);
509 static int format_resp(uint8_t *resp, int length, void *data)
512 struct t2_cookie *cookie = data;
513 struct near_tag *tag;
517 if (length < 0 || resp[0] != 0) {
522 tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
528 DBG("Done formatting");
529 near_tag_set_blank(tag, FALSE);
533 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
535 t2_cookie_release(cookie);
540 static int nfctype2_format(uint32_t adapter_idx, uint32_t target_idx,
543 struct type2_cmd cmd;
544 struct t2_cookie *cookie;
545 struct near_ndef_message *cc_ndef;
546 struct type2_cc *t2_cc;
547 struct near_tag *tag;
548 enum near_tag_sub_type tgt_subtype;
553 tag = near_tag_get_tag(adapter_idx, target_idx);
558 tgt_subtype = near_tag_get_subtype(adapter_idx, target_idx);
560 if (tgt_subtype != NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT) {
561 DBG("Unknown Tag Type 2 subtype (%d)", tgt_subtype);
565 t2_cc = g_try_malloc0(sizeof(struct type2_cc));
566 cc_ndef = g_try_malloc0(sizeof(struct near_ndef_message));
567 cookie = g_try_malloc0(sizeof(struct t2_cookie));
569 if (t2_cc == NULL || cc_ndef == NULL || cookie == NULL) {
574 t2_cc->magic = TYPE2_MAGIC;
575 t2_cc->version = TYPE2_TAG_VER_1_0;
576 t2_cc->mem_size = TYPE2_DATA_SIZE_48;
577 t2_cc->read_write = TYPE2_READWRITE_ACCESS;
579 cookie->adapter_idx = adapter_idx;
580 cookie->target_idx = target_idx;
581 cookie->current_block = CC_BLOCK_START;
582 cookie->ndef = cc_ndef;
583 cookie->ndef->data = (uint8_t *)t2_cc;
587 cmd.block = CC_BLOCK_START;
588 memcpy(cmd.data, (uint8_t *)t2_cc, BLOCK_SIZE);
590 err = near_adapter_send(cookie->adapter_idx, (uint8_t *)&cmd,
591 sizeof(cmd), format_resp, cookie);
603 static struct near_tag_driver type2_driver = {
604 .type = NFC_PROTO_MIFARE,
605 .priority = NEAR_TAG_PRIORITY_DEFAULT,
606 .read = nfctype2_read,
607 .write = nfctype2_write,
608 .check_presence = nfctype2_check_presence,
609 .format = nfctype2_format,
612 static int nfctype2_init(void)
616 return near_tag_driver_register(&type2_driver);
619 static void nfctype2_exit(void)
623 near_tag_driver_unregister(&type2_driver);
626 NEAR_PLUGIN_DEFINE(nfctype2, "NFC Forum Type 2 tags support", VERSION,
627 NEAR_PLUGIN_PRIORITY_HIGH, nfctype2_init, nfctype2_exit)