4b78a356b82cb4de572048aa78a23a65f9beb396
[profile/ivi/neard.git] / plugins / nfctype1.c
1 /*
2  *
3  *  neard - Near Field Communication manager
4  *
5  *  Copyright (C) 2011  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdint.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/socket.h>
30
31 #include <linux/socket.h>
32 #include <linux/nfc.h>
33
34 #include <near/plugin.h>
35 #include <near/log.h>
36 #include <near/types.h>
37 #include <near/adapter.h>
38 #include <near/tag.h>
39 #include <near/ndef.h>
40 #include <near/tlv.h>
41
42 #define CMD_READ_ALL            0x00    /* Read seg 0 (incl: HR) */
43 #define CMD_READ_SEGS           0x10    /* Read 16 blocks (128 bytes) */
44 #define CMD_RID                 0x78    /* Read tag UID */
45
46 #define CMD_WRITE_E             0x53    /* Write with erase */
47 #define CMD_WRITE_NE            0x1A    /* Write no erase */
48
49 #define OFFSET_STATUS_CMD       0x00
50 #define OFFSET_HEADER_ROM       0x01
51
52 #define HR0_TYPE1_STATIC        0x11
53 #define HR0_TYPE2_HIGH          0x10
54 #define HR0_TYPE2_LOW           0x0F
55
56 #define BLOCK_SIZE              8
57 #define LEN_STATUS_BYTE         0x01    /* Status byte */
58 #define LEN_SPEC_BYTES          (LEN_STATUS_BYTE + 0x02)        /* HRx */
59 #define LEN_UID_BYTES           (LEN_STATUS_BYTE + 0x07)        /* UID bytes */
60 #define LEN_CC_BYTES            0x04    /* Capab. container */
61 #define LEN_DYN_BYTES           0x0A    /* Bytes CtrlIT and TLV - Dyn. only */
62
63 #define TYPE1_MAGIC 0xe1
64
65 #define TAG_T1_DATA_UID(data) ((data) + LEN_SPEC_BYTES)
66 #define TAG_T1_DATA_CC(data) ((data) + LEN_SPEC_BYTES + LEN_UID_BYTES)
67 #define TAG_T1_DATA_LENGTH(cc) ((cc[2] + 1) * 8 - LEN_CC_BYTES)
68
69 #define TAG_T1_DATA_NFC(cc) ((cc)[0] & TYPE1_MAGIC)
70
71 #define TYPE1_NOWRITE_ACCESS    0x0F
72 #define TAG_T1_WRITE_FLAG(cc) ((cc)[3] & TYPE1_NOWRITE_ACCESS)
73 #define TAG_T1_SEGMENT_SIZE     128
74
75 #define TYPE1_STATIC_MAX_DATA_SIZE      0x60
76
77 #define UID_LENGTH 4
78
79 #define TYPE1_TAG_VER_1_1 0x11
80 #define TYPE1_TAG_STATIC_SIZE_120 0x0E
81 #define TYPE1_READ_WRITE_ACCESS 0x00
82 #define TYPE1_STATIC_TAG_DATA_LENGTH 116
83
84 struct type1_cmd {
85         uint8_t cmd;
86         uint8_t addr;
87         uint8_t data[1];
88         uint8_t uid[UID_LENGTH];
89 } __attribute__((packed));
90
91 struct type1_tag {
92         uint32_t adapter_idx;
93         uint16_t current_block;
94         uint16_t current_seg;
95         uint16_t last_seg;
96         uint16_t data_read;
97         uint8_t uid[UID_LENGTH];
98
99         near_tag_io_cb cb;
100         struct near_tag *tag;
101 };
102
103 struct t1_cookie {
104         uint32_t adapter_idx;
105         uint32_t target_idx;
106         uint8_t uid[UID_LENGTH];
107         uint32_t current_block; /* Static tag */
108         uint32_t current_byte;  /* Static tag */
109         struct near_ndef_message *ndef;
110         near_tag_io_cb cb;
111         uint8_t cc[LEN_CC_BYTES];
112 };
113
114 static void t1_init_cmd(struct type1_tag *tag, struct type1_cmd *cmd)
115 {
116         if (tag == NULL || cmd == NULL)
117                 return;
118
119         memcpy(cmd->uid, tag->uid, UID_LENGTH);
120 }
121
122 static void t1_cookie_release(struct t1_cookie *cookie)
123 {
124         if (cookie == NULL)
125                 return;
126
127         if (cookie->ndef)
128                 g_free(cookie->ndef->data);
129
130         g_free(cookie->ndef);
131         g_free(cookie);
132         cookie = NULL;
133 }
134
135 /* Read segments (128 bytes)and store them to the tag data block */
136 static int segment_read_recv(uint8_t *resp, int length, void *data)
137 {
138         struct type1_tag *t1_tag = data;
139         struct type1_cmd t1_cmd;
140         uint8_t *tagdata;
141         size_t data_length;
142
143         int err;
144
145         DBG("%d", length);
146
147         if (length < 0) {
148                 err = length;
149                 goto out_err;
150         }
151
152         length = length - LEN_STATUS_BYTE;  /* ignore first byte */
153
154         /* Add data to tag mem*/
155         tagdata = near_tag_get_data(t1_tag->tag, &data_length);
156         memcpy(tagdata + t1_tag->data_read, resp+1, length);
157
158         /* Next segment */
159         t1_tag->data_read =  t1_tag->data_read + length;
160         t1_tag->current_seg = t1_tag->current_seg + 1;
161
162         if (t1_tag->current_seg <= t1_tag->last_seg) {
163                 /* RSEG cmd */
164                 t1_init_cmd(t1_tag, &t1_cmd);
165
166                 t1_cmd.cmd = CMD_READ_SEGS;
167                 t1_cmd.addr = (t1_tag->current_seg << 4) & 0xFF;
168
169                 err = near_adapter_send(t1_tag->adapter_idx,
170                                 (uint8_t *)&t1_cmd, sizeof(t1_cmd),
171                                 segment_read_recv, t1_tag);
172                 if (err < 0)
173                         goto out_err;
174         } else { /* This is the end */
175                 GList *records;
176
177                 DBG("READ complete");
178
179                 records = near_tlv_parse(tagdata, data_length);
180                 near_tag_add_records(t1_tag->tag, records, t1_tag->cb, 0);
181
182                 err = 0;
183
184                 /* free memory */
185                 g_free(t1_tag);
186         }
187
188 out_err:
189         return err;
190 }
191
192 /* The dynamic read function:
193  * Bytes [0..3] : CC
194  * [4..8]: TLV Lock ControlIT (0x01, 0x03, v1, V2, V3)
195  * [9..13]: TLV Reserved Memory Control (0x02, 0x03, V1, V2, V3)
196  * [14..]: TLV NDEF (0x03, L0, L1, L2, V1,V2 ...)
197  */
198 static int read_dynamic_tag(uint8_t *cc, int length, void *data)
199 {
200         struct type1_tag *t1_tag = data;
201         struct type1_cmd t1_cmd;
202
203         uint8_t *tagdata;
204         uint8_t *pndef;
205         size_t data_length;
206
207         DBG("Dynamic Mode");
208
209         tagdata = near_tag_get_data(t1_tag->tag, &data_length);
210
211         /* Skip un-needed bytes */
212         pndef = cc + 4;         /* right after CC bytes */
213         pndef = pndef + 5;      /* skip TLV Lock bits bytes */
214         pndef = pndef + 5;      /* skip TLV ControlIT bytes */
215
216         /* Save first NFC bytes to tag memory
217          * 10 blocks[0x3..0xC] of 8 bytes + 2 bytes from block 2
218          * */
219         memcpy(tagdata, pndef, 10 * BLOCK_SIZE + 2);
220
221         /* Read the next one, up to the end of the data area */
222         t1_tag->current_seg = 1;
223         t1_tag->last_seg = ((cc[2] * BLOCK_SIZE) / TAG_T1_SEGMENT_SIZE);
224         t1_tag->data_read = 10 * BLOCK_SIZE + 2;
225
226         t1_init_cmd(t1_tag, &t1_cmd);
227
228         /* T1 read segment */
229         t1_cmd.cmd = CMD_READ_SEGS;
230         /* 5.3.3 ADDS operand is [b8..b5] */
231         t1_cmd.addr = (t1_tag->current_seg << 4) & 0xFF;
232
233         return near_adapter_send(t1_tag->adapter_idx,
234                         (uint8_t *)&t1_cmd, sizeof(t1_cmd),
235                         segment_read_recv, t1_tag);
236 }
237
238 static int meta_recv(uint8_t *resp, int length, void *data)
239 {
240         struct t1_cookie *cookie = data;
241         struct near_tag *tag;
242         struct type1_tag *t1_tag = NULL;
243
244         uint8_t *cc;
245         int err = -EOPNOTSUPP;
246
247         DBG("%d", length);
248
249         if (length < 0) {
250                 err = length;
251                 goto out_err;
252         }
253
254         /* First byte is cmd status */
255         if (resp[OFFSET_STATUS_CMD] != 0) {
256                 DBG("Command failed: 0x%x",resp[OFFSET_STATUS_CMD]);
257                 err = -EIO;
258                 goto out_err;
259         }
260
261         /* Check Magic NFC tag */
262         cc = TAG_T1_DATA_CC(resp);
263         if (TAG_T1_DATA_NFC(cc) == 0) {
264                 if (resp[OFFSET_HEADER_ROM] == HR0_TYPE1_STATIC) {
265                         err = near_tag_add_data(cookie->adapter_idx,
266                                                 cookie->target_idx,
267                                                 NULL,
268                                                 TYPE1_STATIC_TAG_DATA_LENGTH);
269                 } else {
270                         near_error("Not a valid NFC magic tag 0x%x", cc[0]);
271                         err = -EINVAL;
272                         goto out_err;
273                 }
274         } else {
275                 /* Add data to the tag */
276                 err = near_tag_add_data(cookie->adapter_idx, cookie->target_idx,
277                                                 NULL, TAG_T1_DATA_LENGTH(cc));
278         }
279
280         if (err < 0)
281                 goto out_err;
282
283         tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
284         if (tag == NULL) {
285                 err = -ENOMEM;
286                 goto out_err;
287         }
288
289         t1_tag = g_try_malloc0(sizeof(struct type1_tag));
290         if (t1_tag == NULL) {
291                 err = -ENOMEM;
292                 goto out_err;
293         }
294
295         t1_tag->adapter_idx = cookie->adapter_idx;
296         t1_tag->cb = cookie->cb;
297         t1_tag->tag = tag;
298         memcpy(t1_tag->uid, cookie->uid, UID_LENGTH);
299
300         /*s Set the ReadWrite flag */
301         if (TAG_T1_WRITE_FLAG(cc) == TYPE1_NOWRITE_ACCESS)
302                 near_tag_set_ro(tag, TRUE);
303         else
304                 near_tag_set_ro(tag, FALSE);
305
306         /* Check Static or Dynamic memory model */
307         if (resp[OFFSET_HEADER_ROM] == HR0_TYPE1_STATIC) {
308                 uint8_t *tagdata;
309                 size_t data_length;
310                 GList *records;
311
312                 if (TAG_T1_DATA_NFC(cc) == 0)
313                         near_tag_set_blank(tag, TRUE);
314                 else
315                         near_tag_set_blank(tag, FALSE);
316
317                 DBG("READ Static complete");
318
319                 tagdata = near_tag_get_data(t1_tag->tag, &data_length);
320                 memcpy(tagdata, cc + LEN_CC_BYTES, TAG_T1_DATA_LENGTH(cc));
321
322                 near_tag_set_memory_layout(tag, NEAR_TAG_MEMORY_STATIC);
323
324                 records = near_tlv_parse(tagdata, data_length);
325                 near_tag_add_records(t1_tag->tag, records, t1_tag->cb, 0);
326
327                 g_free(t1_tag);
328
329                 return 0;
330         } else if ((resp[OFFSET_HEADER_ROM] & 0xF0) == HR0_TYPE2_HIGH) {
331                 near_tag_set_memory_layout(tag, NEAR_TAG_MEMORY_DYNAMIC);
332                 err = read_dynamic_tag(cc, length, t1_tag);
333         } else {
334                 err = -EOPNOTSUPP ;
335         }
336
337 out_err:
338         DBG("err %d", err);
339
340         if (err < 0 && cookie->cb)
341                 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
342
343         t1_cookie_release(cookie);
344
345         return err;
346 }
347
348 /*
349  * READALL to read a maximum of 124 bytes.
350  * This cmd is common to static and dynamic targets
351  * This should allow to get the HR0 byte.
352  */
353 static int rid_resp(uint8_t *resp, int length, void *data)
354 {
355         struct t1_cookie *cookie = data;
356         struct type1_cmd t1_cmd;
357         uint8_t *uid;
358         int err;
359
360         DBG("");
361
362         /* First byte is cmd status */
363         if (resp[OFFSET_STATUS_CMD] != 0) {
364                 DBG("Command failed: 0x%x",resp[OFFSET_STATUS_CMD]);
365                 err = -EIO;
366                 goto out_err;
367         }
368
369         uid = TAG_T1_DATA_UID(resp);
370
371         DBG("UID 0x%x 0x%x 0x%x 0x%x", uid[0], uid[1], uid[2], uid[3]);
372
373         near_tag_set_nfcid(cookie->adapter_idx, cookie->target_idx,
374                                                         uid, UID_LENGTH);
375
376         t1_cmd.cmd = CMD_READ_ALL;     /* Read ALL cmd give 124 bytes */
377         t1_cmd.addr = 0;               /* NA */
378         t1_cmd.data[0] = 0;
379         memcpy(t1_cmd.uid, uid, UID_LENGTH);
380
381         memcpy(cookie->uid, uid, UID_LENGTH);
382
383         return near_adapter_send(cookie->adapter_idx,
384                                 (uint8_t *)&t1_cmd, sizeof(t1_cmd),
385                                 meta_recv, cookie);
386
387 out_err:
388         DBG("err %d", err);
389
390         if (err < 0 && cookie->cb)
391                 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
392
393         t1_cookie_release(cookie);
394
395         return err;
396 }
397
398 /* First step: RID to get the tag UID */
399 static int nfctype1_read(uint32_t adapter_idx,
400                                 uint32_t target_idx, near_tag_io_cb cb)
401 {
402         struct type1_cmd t1_cmd;
403         struct t1_cookie *cookie;
404         uint8_t *uid, uid_length;
405
406         DBG("");
407
408         uid = near_tag_get_nfcid(adapter_idx, target_idx, &uid_length);
409         if (uid == NULL || uid_length != UID_LENGTH) {
410                 if (uid != NULL && uid_length != UID_LENGTH) {
411                         near_error("Invalid UID");
412
413                         g_free(uid);
414                         return -EINVAL;
415                 }
416
417                 t1_cmd.cmd = CMD_RID;
418                 t1_cmd.addr = 0;
419                 t1_cmd.data[0] = 0;
420                 memset(t1_cmd.uid, 0, UID_LENGTH);
421
422                 cookie = g_try_malloc0(sizeof(struct t1_cookie));
423                 if (cookie == NULL) {
424                         g_free(uid);
425                         return -ENOMEM;
426                 }
427
428                 cookie->adapter_idx = adapter_idx;
429                 cookie->target_idx = target_idx;
430                 cookie->cb = cb;
431
432                 return near_adapter_send(adapter_idx,
433                                         (uint8_t *)&t1_cmd, sizeof(t1_cmd),
434                                         rid_resp, cookie);
435         }
436
437         t1_cmd.cmd = CMD_READ_ALL;     /* Read ALL cmd give 124 bytes */
438         t1_cmd.addr = 0;               /* NA */
439         t1_cmd.data[0] = 0;
440         memcpy(t1_cmd.uid, uid, UID_LENGTH);
441
442         cookie = g_try_malloc0(sizeof(struct t1_cookie));
443         if (cookie == NULL) {
444                 g_free(uid);
445                 return -ENOMEM;
446         }
447
448         cookie->adapter_idx = adapter_idx;
449         cookie->target_idx = target_idx;
450         memcpy(cookie->uid, uid, UID_LENGTH);
451         cookie->cb = cb;
452
453         g_free(uid);
454
455         return near_adapter_send(adapter_idx, (uint8_t *)&t1_cmd, sizeof(t1_cmd),
456                                                         meta_recv, cookie);
457 }
458
459 static int write_nmn_e1_resp(uint8_t *resp, int length, void *data)
460 {
461         int err = 0;
462         struct t1_cookie *cookie = data;
463
464         DBG("");
465
466         if (length < 0)
467                 err = length;
468
469         if (resp[OFFSET_STATUS_CMD] != 0)
470                 err = -EIO;
471
472         DBG("Done writing");
473
474         cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
475
476         t1_cookie_release(cookie);
477
478         return err;
479 }
480
481 static int write_nmn_e1(struct t1_cookie *cookie)
482 {
483         struct type1_cmd cmd;
484
485         DBG("");
486
487         cmd.cmd = CMD_WRITE_E;
488         cmd.addr = 0x08;
489         cmd.data[0] = TYPE1_MAGIC;
490         memcpy(cmd.uid, cookie->uid, UID_LENGTH);
491
492         return near_adapter_send(cookie->adapter_idx, (uint8_t *)&cmd,
493                                         sizeof(cmd), write_nmn_e1_resp, cookie);
494 }
495
496 static int data_write(uint8_t *resp, int length, void *data)
497 {
498         struct t1_cookie *cookie = data;
499         uint8_t addr = 0;
500         struct type1_cmd cmd;
501         int err;
502
503         DBG("");
504
505         if (length < 0) {
506                 err = length;
507                 goto out;
508         }
509
510         if (resp[OFFSET_STATUS_CMD] != 0) {
511                 err = -EIO;
512                 goto out;
513         }
514
515         if (cookie->ndef->offset > cookie->ndef->length)
516                 return write_nmn_e1(cookie);
517
518         if (cookie->current_byte >= BLOCK_SIZE) {
519                 cookie->current_byte = 0;
520                 cookie->current_block++;
521         }
522
523         cmd.cmd = CMD_WRITE_E;
524         addr = cookie->current_block << 3;
525         cmd.addr = addr | (cookie->current_byte & 0x7);
526         cmd.data[0] = cookie->ndef->data[cookie->ndef->offset];
527         memcpy(cmd.uid, cookie->uid, UID_LENGTH);
528         cookie->ndef->offset++;
529         cookie->current_byte++;
530
531         err = near_adapter_send(cookie->adapter_idx, (uint8_t *)&cmd,
532                                         sizeof(cmd), data_write, cookie);
533         if (err < 0)
534                 goto out;
535
536         return 0;
537
538 out:
539         if (err < 0 && cookie->cb)
540                 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
541
542         t1_cookie_release(cookie);
543
544         return err;
545 }
546
547 static int write_nmn_0(uint32_t adapter_idx, uint32_t target_idx,
548                         struct near_ndef_message *ndef, near_tag_io_cb cb)
549 {
550         int err;
551         struct type1_cmd cmd;
552         struct t1_cookie *cookie;
553         uint8_t *uid, uid_length;
554
555         DBG("");
556
557         uid = near_tag_get_nfcid(adapter_idx, target_idx, &uid_length);
558         if (uid == NULL || uid_length != UID_LENGTH) {
559                 near_error("Invalid type 1 UID");
560                 return -EINVAL;
561         }
562
563         cmd.cmd  = CMD_WRITE_E;
564         cmd.addr = 0x08;
565         cmd.data[0] = 0x00;
566         memcpy(cmd.uid, uid, UID_LENGTH);
567
568         cookie = g_try_malloc0(sizeof(struct t1_cookie));
569         if (cookie == NULL) {
570                 g_free(uid);
571                 err = -ENOMEM;
572                 return err;
573         }
574
575         cookie->adapter_idx = adapter_idx;
576         cookie->target_idx = target_idx;
577         memcpy(cookie->uid, uid, UID_LENGTH);
578         cookie->current_block = 1;
579         cookie->current_byte = LEN_CC_BYTES;
580         cookie->ndef = ndef;
581         cookie->cb = cb;
582
583         g_free(uid);
584
585         err = near_adapter_send(cookie->adapter_idx, (uint8_t *)&cmd,
586                                         sizeof(cmd), data_write, cookie);
587         if (err < 0)
588                 goto out;
589
590         return 0;
591
592 out:
593         t1_cookie_release(cookie);
594
595         return err;
596 }
597
598 /*
599  * The writing of a new NDEF message SHALL occur as follows:
600  * Write NMN = 00h to indicate that no valid NDEF message is present
601  * during writing to allow error detection in the event that the tag
602  * is removed from the field prior to completion of operation.
603  * Write VNo and RWA if required
604  * Write NDEF Message TLV
605  * Write NDEF Message data
606  * Write NMN = E1h as the last byte to be written
607  */
608 static int nfctype1_write(uint32_t adapter_idx, uint32_t target_idx,
609                                 struct near_ndef_message *ndef,
610                                 near_tag_io_cb cb)
611 {
612         struct near_tag *tag;
613
614         DBG("");
615
616         if (ndef == NULL || cb == NULL)
617                 return -EINVAL;
618
619         tag = near_tag_get_tag(adapter_idx, target_idx);
620         if (tag == NULL)
621                 return -EINVAL;
622
623         /* This check is valid for only static tags.
624          * Max data length on Type 2 Tag including TLV's
625          * is TYPE1_STATIC_MAX_DATA_SIZE */
626         if (near_tag_get_memory_layout(tag) == NEAR_TAG_MEMORY_STATIC) {
627                 if ((ndef->length + 3) > TYPE1_STATIC_MAX_DATA_SIZE) {
628                         near_error("not enough space on data");
629                         return -ENOMEM;
630                 }
631         }
632
633         return write_nmn_0(adapter_idx, target_idx, ndef, cb);
634 }
635
636 static int check_presence(uint8_t *resp, int length, void *data)
637 {
638         struct t1_cookie *cookie = data;
639         int err = 0;
640
641         DBG("%d", length);
642
643         if (length < 0)
644                 err = -EIO;
645
646         if (cookie->cb)
647                 cookie->cb(cookie->adapter_idx,
648                                 cookie->target_idx, err);
649
650         t1_cookie_release(cookie);
651
652         return err;
653 }
654
655 static int nfctype1_check_presence(uint32_t adapter_idx,
656                                 uint32_t target_idx, near_tag_io_cb cb)
657 {
658         struct type1_cmd t1_cmd;
659         struct t1_cookie *cookie;
660         uint8_t *uid, uid_length;
661         int err;
662
663         DBG("");
664
665         uid = near_tag_get_nfcid(adapter_idx, target_idx, &uid_length);
666         if (uid == NULL || uid_length != UID_LENGTH) {
667                 near_error("Invalid type 1 UID");
668                 return -EINVAL;
669         }
670
671         t1_cmd.cmd = CMD_READ_ALL;     /* Read ALL cmd give 124 bytes */
672         t1_cmd.addr = 0;               /* NA */
673         t1_cmd.data[0] = 0;
674         memcpy(t1_cmd.uid, uid, UID_LENGTH);
675
676         g_free(uid);
677
678         cookie = g_try_malloc0(sizeof(struct t1_cookie));
679         if (cookie == NULL)
680                 return -ENOMEM;
681
682         cookie->adapter_idx = adapter_idx;
683         cookie->target_idx = target_idx;
684         cookie->cb = cb;
685
686         err = near_adapter_send(adapter_idx, (uint8_t *)&t1_cmd, sizeof(t1_cmd),
687                                                         check_presence, cookie);
688         if (err < 0)
689                 goto out;
690
691         return 0;
692
693 out:
694         t1_cookie_release(cookie);
695
696         return err;
697 }
698
699 static int format_resp(uint8_t *resp, int length, void *data)
700 {
701         int err = 0;
702         struct t1_cookie *cookie = data;
703         struct near_tag *tag;
704         struct type1_cmd cmd;
705         uint8_t addr;
706
707         DBG("");
708
709         if (length < 0 || resp[0] != 0) {
710                 err = -EIO;
711                 goto out;
712         }
713
714         if (cookie->current_byte < LEN_CC_BYTES) {
715                 cmd.cmd  = CMD_WRITE_E;
716                 addr = cookie->current_block << 3;
717                 cmd.addr = addr | (cookie->current_byte & 0x7);
718                 cmd.data[0] = cookie->cc[cookie->current_byte];
719                 cookie->current_byte++;
720                 memcpy(cmd.uid, cookie->uid, UID_LENGTH);
721
722                 err = near_adapter_send(cookie->adapter_idx, (uint8_t *)&cmd,
723                                         sizeof(cmd), format_resp, cookie);
724                 if (err < 0)
725                         goto out;
726
727                 return 0;
728         } else {
729                 tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
730                 if (tag == NULL) {
731                         err = -EINVAL;
732                         goto out;
733                 }
734
735                 DBG("Done formatting");
736                 near_tag_set_blank(tag, FALSE);
737         }
738
739 out:
740         if (cookie->cb)
741                 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
742
743         t1_cookie_release(cookie);
744
745         return err;
746 }
747
748 static int nfctype1_format(uint32_t adapter_idx, uint32_t target_idx,
749                                 near_tag_io_cb cb)
750 {
751         int err;
752         struct near_tag *tag;
753         struct type1_cmd cmd;
754         struct t1_cookie *cookie;
755         uint8_t *uid, uid_length, addr;
756
757         DBG("");
758
759         tag = near_tag_get_tag(adapter_idx, target_idx);
760         if (tag == NULL)
761                 return -EINVAL;
762
763         /* TODO: Dynamic tag format */
764         if (near_tag_get_memory_layout(tag) != NEAR_TAG_MEMORY_STATIC)
765                 return -EOPNOTSUPP;
766
767         uid = near_tag_get_nfcid(adapter_idx, target_idx, &uid_length);
768         if (uid == NULL || uid_length != UID_LENGTH) {
769                 near_error("Invalid type 1 UID");
770                 return -EINVAL;
771         }
772
773         cookie = g_try_malloc0(sizeof(struct t1_cookie));
774         if (cookie == NULL) {
775                 err = -EINVAL;
776                 goto out;
777         }
778
779         cookie->adapter_idx = adapter_idx;
780         cookie->target_idx = target_idx;
781         cookie->cb = cb;
782         memcpy(cookie->uid, uid, UID_LENGTH);
783         cookie->cc[0] = TYPE1_MAGIC;
784         cookie->cc[1] = TYPE1_TAG_VER_1_1;
785         cookie->cc[2] = TYPE1_TAG_STATIC_SIZE_120;
786         cookie->cc[3] = TYPE1_READ_WRITE_ACCESS;
787         cookie->current_block = 1;
788         cookie->current_byte = 0;
789
790         cmd.cmd  = CMD_WRITE_E;
791         addr = cookie->current_block << 3;
792         cmd.addr = addr | (cookie->current_byte & 0x7);
793         cmd.data[0] = cookie->cc[cookie->current_byte];
794         cookie->current_byte++;
795         memcpy(cmd.uid, cookie->uid, UID_LENGTH);
796         g_free(uid);
797
798         err = near_adapter_send(cookie->adapter_idx, (uint8_t *)&cmd,
799                                         sizeof(cmd), format_resp, cookie);
800         if (err < 0)
801                 goto out;
802
803         return 0;
804
805 out:
806         g_free(cookie);
807         g_free(uid);
808
809         return err;
810 }
811
812 static struct near_tag_driver type1_driver = {
813         .type           = NFC_PROTO_JEWEL,
814         .priority       = NEAR_TAG_PRIORITY_DEFAULT,
815         .read           = nfctype1_read,
816         .write          = nfctype1_write,
817         .check_presence = nfctype1_check_presence,
818         .format         = nfctype1_format,
819 };
820
821 static int nfctype1_init(void)
822 {
823         DBG("");
824
825         return near_tag_driver_register(&type1_driver);
826 }
827
828 static void nfctype1_exit(void)
829 {
830         DBG("");
831
832         near_tag_driver_unregister(&type1_driver);
833 }
834
835 NEAR_PLUGIN_DEFINE(nfctype1, "NFC Forum Type 1 tags support", VERSION,
836                         NEAR_PLUGIN_PRIORITY_HIGH, nfctype1_init, nfctype1_exit)