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