GNU Linux-libre 4.4.295-gnu1
[releases.git] / net / nfc / digital_technology.c
1 /*
2  * NFC Digital Protocol stack
3  * Copyright (c) 2013, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  */
15
16 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
17
18 #include "digital.h"
19
20 #define DIGITAL_CMD_SENS_REQ    0x26
21 #define DIGITAL_CMD_ALL_REQ     0x52
22 #define DIGITAL_CMD_SEL_REQ_CL1 0x93
23 #define DIGITAL_CMD_SEL_REQ_CL2 0x95
24 #define DIGITAL_CMD_SEL_REQ_CL3 0x97
25
26 #define DIGITAL_SDD_REQ_SEL_PAR 0x20
27
28 #define DIGITAL_SDD_RES_CT  0x88
29 #define DIGITAL_SDD_RES_LEN 5
30
31 #define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04))
32 #define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60))
33 #define DIGITAL_SEL_RES_IS_T4T(sel_res) ((sel_res) & 0x20)
34 #define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40)
35
36 #define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x0C00) == 0x0C00)
37 #define DIGITAL_SENS_RES_IS_VALID(sens_res) \
38         ((!((sens_res) & 0x001F) && (((sens_res) & 0x0C00) == 0x0C00)) || \
39         (((sens_res) & 0x001F) && ((sens_res) & 0x0C00) != 0x0C00))
40
41 #define DIGITAL_MIFARE_READ_RES_LEN 16
42 #define DIGITAL_MIFARE_ACK_RES  0x0A
43
44 #define DIGITAL_CMD_SENSB_REQ                   0x05
45 #define DIGITAL_SENSB_ADVANCED                  BIT(5)
46 #define DIGITAL_SENSB_EXTENDED                  BIT(4)
47 #define DIGITAL_SENSB_ALLB_REQ                  BIT(3)
48 #define DIGITAL_SENSB_N(n)                      ((n) & 0x7)
49
50 #define DIGITAL_CMD_SENSB_RES                   0x50
51
52 #define DIGITAL_CMD_ATTRIB_REQ                  0x1D
53 #define DIGITAL_ATTRIB_P1_TR0_DEFAULT           (0x0 << 6)
54 #define DIGITAL_ATTRIB_P1_TR1_DEFAULT           (0x0 << 4)
55 #define DIGITAL_ATTRIB_P1_SUPRESS_EOS           BIT(3)
56 #define DIGITAL_ATTRIB_P1_SUPRESS_SOS           BIT(2)
57 #define DIGITAL_ATTRIB_P2_LISTEN_POLL_1         (0x0 << 6)
58 #define DIGITAL_ATTRIB_P2_POLL_LISTEN_1         (0x0 << 4)
59 #define DIGITAL_ATTRIB_P2_MAX_FRAME_256         0x8
60 #define DIGITAL_ATTRIB_P4_DID(n)                ((n) & 0xf)
61
62 #define DIGITAL_CMD_SENSF_REQ   0x00
63 #define DIGITAL_CMD_SENSF_RES   0x01
64
65 #define DIGITAL_SENSF_RES_MIN_LENGTH 17
66 #define DIGITAL_SENSF_RES_RD_AP_B1   0x00
67 #define DIGITAL_SENSF_RES_RD_AP_B2   0x8F
68
69 #define DIGITAL_SENSF_REQ_RC_NONE 0
70 #define DIGITAL_SENSF_REQ_RC_SC   1
71 #define DIGITAL_SENSF_REQ_RC_AP   2
72
73 #define DIGITAL_CMD_ISO15693_INVENTORY_REQ      0x01
74
75 #define DIGITAL_ISO15693_REQ_FLAG_DATA_RATE     BIT(1)
76 #define DIGITAL_ISO15693_REQ_FLAG_INVENTORY     BIT(2)
77 #define DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS      BIT(5)
78 #define DIGITAL_ISO15693_RES_FLAG_ERROR         BIT(0)
79 #define DIGITAL_ISO15693_RES_IS_VALID(flags) \
80         (!((flags) & DIGITAL_ISO15693_RES_FLAG_ERROR))
81
82 #define DIGITAL_ISO_DEP_I_PCB    0x02
83 #define DIGITAL_ISO_DEP_PNI(pni) ((pni) & 0x01)
84
85 #define DIGITAL_ISO_DEP_PCB_TYPE(pcb) ((pcb) & 0xC0)
86
87 #define DIGITAL_ISO_DEP_I_BLOCK 0x00
88
89 #define DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb) ((pcb) & 0x08)
90
91 static const u8 digital_ats_fsc[] = {
92          16,  24,  32,  40,  48,  64,  96, 128,
93 };
94
95 #define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F)
96 #define DIGITAL_SENSB_FSCI(pi2) (((pi2) & 0xF0) >> 4)
97 #define DIGITAL_ATS_MAX_FSC  256
98
99 #define DIGITAL_RATS_BYTE1 0xE0
100 #define DIGITAL_RATS_PARAM 0x80
101
102 struct digital_sdd_res {
103         u8 nfcid1[4];
104         u8 bcc;
105 } __packed;
106
107 struct digital_sel_req {
108         u8 sel_cmd;
109         u8 b2;
110         u8 nfcid1[4];
111         u8 bcc;
112 } __packed;
113
114 struct digital_sensb_req {
115         u8 cmd;
116         u8 afi;
117         u8 param;
118 } __packed;
119
120 struct digital_sensb_res {
121         u8 cmd;
122         u8 nfcid0[4];
123         u8 app_data[4];
124         u8 proto_info[3];
125 } __packed;
126
127 struct digital_attrib_req {
128         u8 cmd;
129         u8 nfcid0[4];
130         u8 param1;
131         u8 param2;
132         u8 param3;
133         u8 param4;
134 } __packed;
135
136 struct digital_attrib_res {
137         u8 mbli_did;
138 } __packed;
139
140 struct digital_sensf_req {
141         u8 cmd;
142         u8 sc1;
143         u8 sc2;
144         u8 rc;
145         u8 tsn;
146 } __packed;
147
148 struct digital_sensf_res {
149         u8 cmd;
150         u8 nfcid2[8];
151         u8 pad0[2];
152         u8 pad1[3];
153         u8 mrti_check;
154         u8 mrti_update;
155         u8 pad2;
156         u8 rd[2];
157 } __packed;
158
159 struct digital_iso15693_inv_req {
160         u8 flags;
161         u8 cmd;
162         u8 mask_len;
163         u64 mask;
164 } __packed;
165
166 struct digital_iso15693_inv_res {
167         u8 flags;
168         u8 dsfid;
169         u64 uid;
170 } __packed;
171
172 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
173                                    struct nfc_target *target);
174
175 int digital_in_iso_dep_pull_sod(struct nfc_digital_dev *ddev,
176                                 struct sk_buff *skb)
177 {
178         u8 pcb;
179         u8 block_type;
180
181         if (skb->len < 1)
182                 return -EIO;
183
184         pcb = *skb->data;
185         block_type = DIGITAL_ISO_DEP_PCB_TYPE(pcb);
186
187         /* No support fo R-block nor S-block */
188         if (block_type != DIGITAL_ISO_DEP_I_BLOCK) {
189                 pr_err("ISO_DEP R-block and S-block not supported\n");
190                 return -EIO;
191         }
192
193         if (DIGITAL_ISO_DEP_BLOCK_HAS_DID(pcb)) {
194                 pr_err("DID field in ISO_DEP PCB not supported\n");
195                 return -EIO;
196         }
197
198         skb_pull(skb, 1);
199
200         return 0;
201 }
202
203 int digital_in_iso_dep_push_sod(struct nfc_digital_dev *ddev,
204                                 struct sk_buff *skb)
205 {
206         /*
207          * Chaining not supported so skb->len + 1 PCB byte + 2 CRC bytes must
208          * not be greater than remote FSC
209          */
210         if (skb->len + 3 > ddev->target_fsc)
211                 return -EIO;
212
213         skb_push(skb, 1);
214
215         *skb->data = DIGITAL_ISO_DEP_I_PCB | ddev->curr_nfc_dep_pni;
216
217         ddev->curr_nfc_dep_pni =
218                 DIGITAL_ISO_DEP_PNI(ddev->curr_nfc_dep_pni + 1);
219
220         return 0;
221 }
222
223 static void digital_in_recv_ats(struct nfc_digital_dev *ddev, void *arg,
224                                 struct sk_buff *resp)
225 {
226         struct nfc_target *target = arg;
227         u8 fsdi;
228         int rc;
229
230         if (IS_ERR(resp)) {
231                 rc = PTR_ERR(resp);
232                 resp = NULL;
233                 goto exit;
234         }
235
236         if (resp->len < 2) {
237                 rc = -EIO;
238                 goto exit;
239         }
240
241         fsdi = DIGITAL_ATS_FSCI(resp->data[1]);
242         if (fsdi >= 8)
243                 ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
244         else
245                 ddev->target_fsc = digital_ats_fsc[fsdi];
246
247         ddev->curr_nfc_dep_pni = 0;
248
249         rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443);
250
251 exit:
252         dev_kfree_skb(resp);
253         kfree(target);
254
255         if (rc)
256                 digital_poll_next_tech(ddev);
257 }
258
259 static int digital_in_send_rats(struct nfc_digital_dev *ddev,
260                                 struct nfc_target *target)
261 {
262         int rc;
263         struct sk_buff *skb;
264
265         skb = digital_skb_alloc(ddev, 2);
266         if (!skb)
267                 return -ENOMEM;
268
269         *skb_put(skb, 1) = DIGITAL_RATS_BYTE1;
270         *skb_put(skb, 1) = DIGITAL_RATS_PARAM;
271
272         rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_ats,
273                                  target);
274         if (rc)
275                 kfree_skb(skb);
276
277         return rc;
278 }
279
280 static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
281                                     struct sk_buff *resp)
282 {
283         struct nfc_target *target = arg;
284         int rc;
285         u8 sel_res;
286         u8 nfc_proto;
287
288         if (IS_ERR(resp)) {
289                 rc = PTR_ERR(resp);
290                 resp = NULL;
291                 goto exit;
292         }
293
294         if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
295                 rc = digital_skb_check_crc_a(resp);
296                 if (rc) {
297                         PROTOCOL_ERR("4.4.1.3");
298                         goto exit;
299                 }
300         }
301
302         if (!resp->len) {
303                 rc = -EIO;
304                 goto exit;
305         }
306
307         sel_res = resp->data[0];
308
309         if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) {
310                 rc = digital_in_send_sdd_req(ddev, target);
311                 if (rc)
312                         goto exit;
313
314                 goto exit_free_skb;
315         }
316
317         target->sel_res = sel_res;
318
319         if (DIGITAL_SEL_RES_IS_T2T(sel_res)) {
320                 nfc_proto = NFC_PROTO_MIFARE;
321         } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) {
322                 nfc_proto = NFC_PROTO_NFC_DEP;
323         } else if (DIGITAL_SEL_RES_IS_T4T(sel_res)) {
324                 rc = digital_in_send_rats(ddev, target);
325                 if (rc)
326                         goto exit;
327                 /*
328                  * Skip target_found and don't free it for now. This will be
329                  * done when receiving the ATS
330                  */
331                 goto exit_free_skb;
332         } else {
333                 rc = -EOPNOTSUPP;
334                 goto exit;
335         }
336
337         rc = digital_target_found(ddev, target, nfc_proto);
338
339 exit:
340         kfree(target);
341
342 exit_free_skb:
343         dev_kfree_skb(resp);
344
345         if (rc)
346                 digital_poll_next_tech(ddev);
347 }
348
349 static int digital_in_send_sel_req(struct nfc_digital_dev *ddev,
350                                    struct nfc_target *target,
351                                    struct digital_sdd_res *sdd_res)
352 {
353         struct sk_buff *skb;
354         struct digital_sel_req *sel_req;
355         u8 sel_cmd;
356         int rc;
357
358         skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req));
359         if (!skb)
360                 return -ENOMEM;
361
362         skb_put(skb, sizeof(struct digital_sel_req));
363         sel_req = (struct digital_sel_req *)skb->data;
364
365         if (target->nfcid1_len <= 4)
366                 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
367         else if (target->nfcid1_len < 10)
368                 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
369         else
370                 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
371
372         sel_req->sel_cmd = sel_cmd;
373         sel_req->b2 = 0x70;
374         memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4);
375         sel_req->bcc = sdd_res->bcc;
376
377         if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
378                 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
379                                 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
380                 if (rc)
381                         goto exit;
382         } else {
383                 digital_skb_add_crc_a(skb);
384         }
385
386         rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res,
387                                  target);
388 exit:
389         if (rc)
390                 kfree_skb(skb);
391
392         return rc;
393 }
394
395 static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg,
396                                     struct sk_buff *resp)
397 {
398         struct nfc_target *target = arg;
399         struct digital_sdd_res *sdd_res;
400         int rc;
401         u8 offset, size;
402         u8 i, bcc;
403
404         if (IS_ERR(resp)) {
405                 rc = PTR_ERR(resp);
406                 resp = NULL;
407                 goto exit;
408         }
409
410         if (resp->len < DIGITAL_SDD_RES_LEN) {
411                 PROTOCOL_ERR("4.7.2.8");
412                 rc = -EINVAL;
413                 goto exit;
414         }
415
416         sdd_res = (struct digital_sdd_res *)resp->data;
417
418         for (i = 0, bcc = 0; i < 4; i++)
419                 bcc ^= sdd_res->nfcid1[i];
420
421         if (bcc != sdd_res->bcc) {
422                 PROTOCOL_ERR("4.7.2.6");
423                 rc = -EINVAL;
424                 goto exit;
425         }
426
427         if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) {
428                 offset = 1;
429                 size = 3;
430         } else {
431                 offset = 0;
432                 size = 4;
433         }
434
435         memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset,
436                size);
437         target->nfcid1_len += size;
438
439         rc = digital_in_send_sel_req(ddev, target, sdd_res);
440
441 exit:
442         dev_kfree_skb(resp);
443
444         if (rc) {
445                 kfree(target);
446                 digital_poll_next_tech(ddev);
447         }
448 }
449
450 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
451                                    struct nfc_target *target)
452 {
453         int rc;
454         struct sk_buff *skb;
455         u8 sel_cmd;
456
457         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
458                                      NFC_DIGITAL_FRAMING_NFCA_STANDARD);
459         if (rc)
460                 return rc;
461
462         skb = digital_skb_alloc(ddev, 2);
463         if (!skb)
464                 return -ENOMEM;
465
466         if (target->nfcid1_len == 0)
467                 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
468         else if (target->nfcid1_len == 3)
469                 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
470         else
471                 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
472
473         *skb_put(skb, sizeof(u8)) = sel_cmd;
474         *skb_put(skb, sizeof(u8)) = DIGITAL_SDD_REQ_SEL_PAR;
475
476         rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res,
477                                  target);
478         if (rc)
479                 kfree_skb(skb);
480
481         return rc;
482 }
483
484 static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg,
485                                      struct sk_buff *resp)
486 {
487         struct nfc_target *target = NULL;
488         int rc;
489
490         if (IS_ERR(resp)) {
491                 rc = PTR_ERR(resp);
492                 resp = NULL;
493                 goto exit;
494         }
495
496         if (resp->len < sizeof(u16)) {
497                 rc = -EIO;
498                 goto exit;
499         }
500
501         target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
502         if (!target) {
503                 rc = -ENOMEM;
504                 goto exit;
505         }
506
507         target->sens_res = __le16_to_cpu(*(__le16 *)resp->data);
508
509         if (!DIGITAL_SENS_RES_IS_VALID(target->sens_res)) {
510                 PROTOCOL_ERR("4.6.3.3");
511                 rc = -EINVAL;
512                 goto exit;
513         }
514
515         if (DIGITAL_SENS_RES_IS_T1T(target->sens_res))
516                 rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL);
517         else
518                 rc = digital_in_send_sdd_req(ddev, target);
519
520 exit:
521         dev_kfree_skb(resp);
522
523         if (rc) {
524                 kfree(target);
525                 digital_poll_next_tech(ddev);
526         }
527 }
528
529 int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech)
530 {
531         struct sk_buff *skb;
532         int rc;
533
534         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
535                                      NFC_DIGITAL_RF_TECH_106A);
536         if (rc)
537                 return rc;
538
539         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
540                                      NFC_DIGITAL_FRAMING_NFCA_SHORT);
541         if (rc)
542                 return rc;
543
544         skb = digital_skb_alloc(ddev, 1);
545         if (!skb)
546                 return -ENOMEM;
547
548         *skb_put(skb, sizeof(u8)) = DIGITAL_CMD_SENS_REQ;
549
550         rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL);
551         if (rc)
552                 kfree_skb(skb);
553
554         return rc;
555 }
556
557 int digital_in_recv_mifare_res(struct sk_buff *resp)
558 {
559         /* Successful READ command response is 16 data bytes + 2 CRC bytes long.
560          * Since the driver can't differentiate a ACK/NACK response from a valid
561          * READ response, the CRC calculation must be handled at digital level
562          * even if the driver supports it for this technology.
563          */
564         if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) {
565                 if (digital_skb_check_crc_a(resp)) {
566                         PROTOCOL_ERR("9.4.1.2");
567                         return -EIO;
568                 }
569
570                 return 0;
571         }
572
573         /* ACK response (i.e. successful WRITE). */
574         if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) {
575                 resp->data[0] = 0;
576                 return 0;
577         }
578
579         /* NACK and any other responses are treated as error. */
580         return -EIO;
581 }
582
583 static void digital_in_recv_attrib_res(struct nfc_digital_dev *ddev, void *arg,
584                                        struct sk_buff *resp)
585 {
586         struct nfc_target *target = arg;
587         struct digital_attrib_res *attrib_res;
588         int rc;
589
590         if (IS_ERR(resp)) {
591                 rc = PTR_ERR(resp);
592                 resp = NULL;
593                 goto exit;
594         }
595
596         if (resp->len < sizeof(*attrib_res)) {
597                 PROTOCOL_ERR("12.6.2");
598                 rc = -EIO;
599                 goto exit;
600         }
601
602         attrib_res = (struct digital_attrib_res *)resp->data;
603
604         if (attrib_res->mbli_did & 0x0f) {
605                 PROTOCOL_ERR("12.6.2.1");
606                 rc = -EIO;
607                 goto exit;
608         }
609
610         rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443_B);
611
612 exit:
613         dev_kfree_skb(resp);
614         kfree(target);
615
616         if (rc)
617                 digital_poll_next_tech(ddev);
618 }
619
620 static int digital_in_send_attrib_req(struct nfc_digital_dev *ddev,
621                                struct nfc_target *target,
622                                struct digital_sensb_res *sensb_res)
623 {
624         struct digital_attrib_req *attrib_req;
625         struct sk_buff *skb;
626         int rc;
627
628         skb = digital_skb_alloc(ddev, sizeof(*attrib_req));
629         if (!skb)
630                 return -ENOMEM;
631
632         attrib_req = (struct digital_attrib_req *)skb_put(skb,
633                                                           sizeof(*attrib_req));
634
635         attrib_req->cmd = DIGITAL_CMD_ATTRIB_REQ;
636         memcpy(attrib_req->nfcid0, sensb_res->nfcid0,
637                sizeof(attrib_req->nfcid0));
638         attrib_req->param1 = DIGITAL_ATTRIB_P1_TR0_DEFAULT |
639                              DIGITAL_ATTRIB_P1_TR1_DEFAULT;
640         attrib_req->param2 = DIGITAL_ATTRIB_P2_LISTEN_POLL_1 |
641                              DIGITAL_ATTRIB_P2_POLL_LISTEN_1 |
642                              DIGITAL_ATTRIB_P2_MAX_FRAME_256;
643         attrib_req->param3 = sensb_res->proto_info[1] & 0x07;
644         attrib_req->param4 = DIGITAL_ATTRIB_P4_DID(0);
645
646         rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_attrib_res,
647                                  target);
648         if (rc)
649                 kfree_skb(skb);
650
651         return rc;
652 }
653
654 static void digital_in_recv_sensb_res(struct nfc_digital_dev *ddev, void *arg,
655                                       struct sk_buff *resp)
656 {
657         struct nfc_target *target = NULL;
658         struct digital_sensb_res *sensb_res;
659         u8 fsci;
660         int rc;
661
662         if (IS_ERR(resp)) {
663                 rc = PTR_ERR(resp);
664                 resp = NULL;
665                 goto exit;
666         }
667
668         if (resp->len != sizeof(*sensb_res)) {
669                 PROTOCOL_ERR("5.6.2.1");
670                 rc = -EIO;
671                 goto exit;
672         }
673
674         sensb_res = (struct digital_sensb_res *)resp->data;
675
676         if (sensb_res->cmd != DIGITAL_CMD_SENSB_RES) {
677                 PROTOCOL_ERR("5.6.2");
678                 rc = -EIO;
679                 goto exit;
680         }
681
682         if (!(sensb_res->proto_info[1] & BIT(0))) {
683                 PROTOCOL_ERR("5.6.2.12");
684                 rc = -EIO;
685                 goto exit;
686         }
687
688         if (sensb_res->proto_info[1] & BIT(3)) {
689                 PROTOCOL_ERR("5.6.2.16");
690                 rc = -EIO;
691                 goto exit;
692         }
693
694         fsci = DIGITAL_SENSB_FSCI(sensb_res->proto_info[1]);
695         if (fsci >= 8)
696                 ddev->target_fsc = DIGITAL_ATS_MAX_FSC;
697         else
698                 ddev->target_fsc = digital_ats_fsc[fsci];
699
700         target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
701         if (!target) {
702                 rc = -ENOMEM;
703                 goto exit;
704         }
705
706         rc = digital_in_send_attrib_req(ddev, target, sensb_res);
707
708 exit:
709         dev_kfree_skb(resp);
710
711         if (rc) {
712                 kfree(target);
713                 digital_poll_next_tech(ddev);
714         }
715 }
716
717 int digital_in_send_sensb_req(struct nfc_digital_dev *ddev, u8 rf_tech)
718 {
719         struct digital_sensb_req *sensb_req;
720         struct sk_buff *skb;
721         int rc;
722
723         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
724                                      NFC_DIGITAL_RF_TECH_106B);
725         if (rc)
726                 return rc;
727
728         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
729                                      NFC_DIGITAL_FRAMING_NFCB);
730         if (rc)
731                 return rc;
732
733         skb = digital_skb_alloc(ddev, sizeof(*sensb_req));
734         if (!skb)
735                 return -ENOMEM;
736
737         sensb_req = (struct digital_sensb_req *)skb_put(skb,
738                                                         sizeof(*sensb_req));
739
740         sensb_req->cmd = DIGITAL_CMD_SENSB_REQ;
741         sensb_req->afi = 0x00; /* All families and sub-families */
742         sensb_req->param = DIGITAL_SENSB_N(0);
743
744         rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensb_res,
745                                  NULL);
746         if (rc)
747                 kfree_skb(skb);
748
749         return rc;
750 }
751
752 static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
753                                    struct sk_buff *resp)
754 {
755         int rc;
756         u8 proto;
757         struct nfc_target target;
758         struct digital_sensf_res *sensf_res;
759
760         if (IS_ERR(resp)) {
761                 rc = PTR_ERR(resp);
762                 resp = NULL;
763                 goto exit;
764         }
765
766         if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) {
767                 rc = -EIO;
768                 goto exit;
769         }
770
771         if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
772                 rc = digital_skb_check_crc_f(resp);
773                 if (rc) {
774                         PROTOCOL_ERR("6.4.1.8");
775                         goto exit;
776                 }
777         }
778
779         skb_pull(resp, 1);
780
781         memset(&target, 0, sizeof(struct nfc_target));
782
783         sensf_res = (struct digital_sensf_res *)resp->data;
784
785         memcpy(target.sensf_res, sensf_res, resp->len);
786         target.sensf_res_len = resp->len;
787
788         memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE);
789         target.nfcid2_len = NFC_NFCID2_MAXSIZE;
790
791         if (target.nfcid2[0] == DIGITAL_SENSF_NFCID2_NFC_DEP_B1 &&
792             target.nfcid2[1] == DIGITAL_SENSF_NFCID2_NFC_DEP_B2)
793                 proto = NFC_PROTO_NFC_DEP;
794         else
795                 proto = NFC_PROTO_FELICA;
796
797         rc = digital_target_found(ddev, &target, proto);
798
799 exit:
800         dev_kfree_skb(resp);
801
802         if (rc)
803                 digital_poll_next_tech(ddev);
804 }
805
806 int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech)
807 {
808         struct digital_sensf_req *sensf_req;
809         struct sk_buff *skb;
810         int rc;
811         u8 size;
812
813         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
814         if (rc)
815                 return rc;
816
817         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
818                                      NFC_DIGITAL_FRAMING_NFCF);
819         if (rc)
820                 return rc;
821
822         size = sizeof(struct digital_sensf_req);
823
824         skb = digital_skb_alloc(ddev, size);
825         if (!skb)
826                 return -ENOMEM;
827
828         skb_put(skb, size);
829
830         sensf_req = (struct digital_sensf_req *)skb->data;
831         sensf_req->cmd = DIGITAL_CMD_SENSF_REQ;
832         sensf_req->sc1 = 0xFF;
833         sensf_req->sc2 = 0xFF;
834         sensf_req->rc = 0;
835         sensf_req->tsn = 0;
836
837         *skb_push(skb, 1) = size + 1;
838
839         if (!DIGITAL_DRV_CAPS_IN_CRC(ddev))
840                 digital_skb_add_crc_f(skb);
841
842         rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res,
843                                  NULL);
844         if (rc)
845                 kfree_skb(skb);
846
847         return rc;
848 }
849
850 static void digital_in_recv_iso15693_inv_res(struct nfc_digital_dev *ddev,
851                 void *arg, struct sk_buff *resp)
852 {
853         struct digital_iso15693_inv_res *res;
854         struct nfc_target *target = NULL;
855         int rc;
856
857         if (IS_ERR(resp)) {
858                 rc = PTR_ERR(resp);
859                 resp = NULL;
860                 goto out_free_skb;
861         }
862
863         if (resp->len != sizeof(*res)) {
864                 rc = -EIO;
865                 goto out_free_skb;
866         }
867
868         res = (struct digital_iso15693_inv_res *)resp->data;
869
870         if (!DIGITAL_ISO15693_RES_IS_VALID(res->flags)) {
871                 PROTOCOL_ERR("ISO15693 - 10.3.1");
872                 rc = -EINVAL;
873                 goto out_free_skb;
874         }
875
876         target = kzalloc(sizeof(*target), GFP_KERNEL);
877         if (!target) {
878                 rc = -ENOMEM;
879                 goto out_free_skb;
880         }
881
882         target->is_iso15693 = 1;
883         target->iso15693_dsfid = res->dsfid;
884         memcpy(target->iso15693_uid, &res->uid, sizeof(target->iso15693_uid));
885
886         rc = digital_target_found(ddev, target, NFC_PROTO_ISO15693);
887
888         kfree(target);
889
890 out_free_skb:
891         dev_kfree_skb(resp);
892
893         if (rc)
894                 digital_poll_next_tech(ddev);
895 }
896
897 int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech)
898 {
899         struct digital_iso15693_inv_req *req;
900         struct sk_buff *skb;
901         int rc;
902
903         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
904                                      NFC_DIGITAL_RF_TECH_ISO15693);
905         if (rc)
906                 return rc;
907
908         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
909                                      NFC_DIGITAL_FRAMING_ISO15693_INVENTORY);
910         if (rc)
911                 return rc;
912
913         skb = digital_skb_alloc(ddev, sizeof(*req));
914         if (!skb)
915                 return -ENOMEM;
916
917         skb_put(skb, sizeof(*req) - sizeof(req->mask)); /* No mask */
918         req = (struct digital_iso15693_inv_req *)skb->data;
919
920         /* Single sub-carrier, high data rate, no AFI, single slot
921          * Inventory command
922          */
923         req->flags = DIGITAL_ISO15693_REQ_FLAG_DATA_RATE |
924                      DIGITAL_ISO15693_REQ_FLAG_INVENTORY |
925                      DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS;
926         req->cmd = DIGITAL_CMD_ISO15693_INVENTORY_REQ;
927         req->mask_len = 0;
928
929         rc = digital_in_send_cmd(ddev, skb, 30,
930                                  digital_in_recv_iso15693_inv_res, NULL);
931         if (rc)
932                 kfree_skb(skb);
933
934         return rc;
935 }
936
937 static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev)
938 {
939         struct sk_buff *skb;
940         int rc;
941
942         skb = digital_skb_alloc(ddev, 1);
943         if (!skb)
944                 return -ENOMEM;
945
946         *skb_put(skb, 1) = DIGITAL_SEL_RES_NFC_DEP;
947
948         if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
949                 digital_skb_add_crc_a(skb);
950
951         rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
952                                      NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE);
953         if (rc) {
954                 kfree_skb(skb);
955                 return rc;
956         }
957
958         rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_atr_req,
959                                  NULL);
960         if (rc)
961                 kfree_skb(skb);
962
963         return rc;
964 }
965
966 static void digital_tg_recv_sel_req(struct nfc_digital_dev *ddev, void *arg,
967                                     struct sk_buff *resp)
968 {
969         int rc;
970
971         if (IS_ERR(resp)) {
972                 rc = PTR_ERR(resp);
973                 resp = NULL;
974                 goto exit;
975         }
976
977         if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
978                 rc = digital_skb_check_crc_a(resp);
979                 if (rc) {
980                         PROTOCOL_ERR("4.4.1.3");
981                         goto exit;
982                 }
983         }
984
985         /* Silently ignore SEL_REQ content and send a SEL_RES for NFC-DEP */
986
987         rc = digital_tg_send_sel_res(ddev);
988
989 exit:
990         if (rc)
991                 digital_poll_next_tech(ddev);
992
993         dev_kfree_skb(resp);
994 }
995
996 static int digital_tg_send_sdd_res(struct nfc_digital_dev *ddev)
997 {
998         struct sk_buff *skb;
999         struct digital_sdd_res *sdd_res;
1000         int rc, i;
1001
1002         skb = digital_skb_alloc(ddev, sizeof(struct digital_sdd_res));
1003         if (!skb)
1004                 return -ENOMEM;
1005
1006         skb_put(skb, sizeof(struct digital_sdd_res));
1007         sdd_res = (struct digital_sdd_res *)skb->data;
1008
1009         sdd_res->nfcid1[0] = 0x08;
1010         get_random_bytes(sdd_res->nfcid1 + 1, 3);
1011
1012         sdd_res->bcc = 0;
1013         for (i = 0; i < 4; i++)
1014                 sdd_res->bcc ^= sdd_res->nfcid1[i];
1015
1016         rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1017                                 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
1018         if (rc) {
1019                 kfree_skb(skb);
1020                 return rc;
1021         }
1022
1023         rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sel_req,
1024                                  NULL);
1025         if (rc)
1026                 kfree_skb(skb);
1027
1028         return rc;
1029 }
1030
1031 static void digital_tg_recv_sdd_req(struct nfc_digital_dev *ddev, void *arg,
1032                                     struct sk_buff *resp)
1033 {
1034         u8 *sdd_req;
1035         int rc;
1036
1037         if (IS_ERR(resp)) {
1038                 rc = PTR_ERR(resp);
1039                 resp = NULL;
1040                 goto exit;
1041         }
1042
1043         sdd_req = resp->data;
1044
1045         if (resp->len < 2 || sdd_req[0] != DIGITAL_CMD_SEL_REQ_CL1 ||
1046             sdd_req[1] != DIGITAL_SDD_REQ_SEL_PAR) {
1047                 rc = -EINVAL;
1048                 goto exit;
1049         }
1050
1051         rc = digital_tg_send_sdd_res(ddev);
1052
1053 exit:
1054         if (rc)
1055                 digital_poll_next_tech(ddev);
1056
1057         dev_kfree_skb(resp);
1058 }
1059
1060 static int digital_tg_send_sens_res(struct nfc_digital_dev *ddev)
1061 {
1062         struct sk_buff *skb;
1063         u8 *sens_res;
1064         int rc;
1065
1066         skb = digital_skb_alloc(ddev, 2);
1067         if (!skb)
1068                 return -ENOMEM;
1069
1070         sens_res = skb_put(skb, 2);
1071
1072         sens_res[0] = (DIGITAL_SENS_RES_NFC_DEP >> 8) & 0xFF;
1073         sens_res[1] = DIGITAL_SENS_RES_NFC_DEP & 0xFF;
1074
1075         rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1076                                      NFC_DIGITAL_FRAMING_NFCA_STANDARD);
1077         if (rc) {
1078                 kfree_skb(skb);
1079                 return rc;
1080         }
1081
1082         rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sdd_req,
1083                                  NULL);
1084         if (rc)
1085                 kfree_skb(skb);
1086
1087         return rc;
1088 }
1089
1090 void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
1091                               struct sk_buff *resp)
1092 {
1093         u8 sens_req;
1094         int rc;
1095
1096         if (IS_ERR(resp)) {
1097                 rc = PTR_ERR(resp);
1098                 resp = NULL;
1099                 goto exit;
1100         }
1101
1102         sens_req = resp->data[0];
1103
1104         if (!resp->len || (sens_req != DIGITAL_CMD_SENS_REQ &&
1105             sens_req != DIGITAL_CMD_ALL_REQ)) {
1106                 rc = -EINVAL;
1107                 goto exit;
1108         }
1109
1110         rc = digital_tg_send_sens_res(ddev);
1111
1112 exit:
1113         if (rc)
1114                 digital_poll_next_tech(ddev);
1115
1116         dev_kfree_skb(resp);
1117 }
1118
1119 static void digital_tg_recv_atr_or_sensf_req(struct nfc_digital_dev *ddev,
1120                 void *arg, struct sk_buff *resp)
1121 {
1122         if (!IS_ERR(resp) && (resp->len >= 2) &&
1123                         (resp->data[1] == DIGITAL_CMD_SENSF_REQ))
1124                 digital_tg_recv_sensf_req(ddev, arg, resp);
1125         else
1126                 digital_tg_recv_atr_req(ddev, arg, resp);
1127
1128         return;
1129 }
1130
1131 static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
1132                               struct digital_sensf_req *sensf_req)
1133 {
1134         struct sk_buff *skb;
1135         u8 size;
1136         int rc;
1137         struct digital_sensf_res *sensf_res;
1138
1139         size = sizeof(struct digital_sensf_res);
1140
1141         if (sensf_req->rc == DIGITAL_SENSF_REQ_RC_NONE)
1142                 size -= sizeof(sensf_res->rd);
1143
1144         skb = digital_skb_alloc(ddev, size);
1145         if (!skb)
1146                 return -ENOMEM;
1147
1148         skb_put(skb, size);
1149
1150         sensf_res = (struct digital_sensf_res *)skb->data;
1151
1152         memset(sensf_res, 0, size);
1153
1154         sensf_res->cmd = DIGITAL_CMD_SENSF_RES;
1155         sensf_res->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
1156         sensf_res->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
1157         get_random_bytes(&sensf_res->nfcid2[2], 6);
1158
1159         switch (sensf_req->rc) {
1160         case DIGITAL_SENSF_REQ_RC_SC:
1161                 sensf_res->rd[0] = sensf_req->sc1;
1162                 sensf_res->rd[1] = sensf_req->sc2;
1163                 break;
1164         case DIGITAL_SENSF_REQ_RC_AP:
1165                 sensf_res->rd[0] = DIGITAL_SENSF_RES_RD_AP_B1;
1166                 sensf_res->rd[1] = DIGITAL_SENSF_RES_RD_AP_B2;
1167                 break;
1168         }
1169
1170         *skb_push(skb, sizeof(u8)) = size + 1;
1171
1172         if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
1173                 digital_skb_add_crc_f(skb);
1174
1175         rc = digital_tg_send_cmd(ddev, skb, 300,
1176                                  digital_tg_recv_atr_or_sensf_req, NULL);
1177         if (rc)
1178                 kfree_skb(skb);
1179
1180         return rc;
1181 }
1182
1183 void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
1184                                struct sk_buff *resp)
1185 {
1186         struct digital_sensf_req *sensf_req;
1187         int rc;
1188
1189         if (IS_ERR(resp)) {
1190                 rc = PTR_ERR(resp);
1191                 resp = NULL;
1192                 goto exit;
1193         }
1194
1195         if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
1196                 rc = digital_skb_check_crc_f(resp);
1197                 if (rc) {
1198                         PROTOCOL_ERR("6.4.1.8");
1199                         goto exit;
1200                 }
1201         }
1202
1203         if (resp->len != sizeof(struct digital_sensf_req) + 1) {
1204                 rc = -EINVAL;
1205                 goto exit;
1206         }
1207
1208         skb_pull(resp, 1);
1209         sensf_req = (struct digital_sensf_req *)resp->data;
1210
1211         if (sensf_req->cmd != DIGITAL_CMD_SENSF_REQ) {
1212                 rc = -EINVAL;
1213                 goto exit;
1214         }
1215
1216         rc = digital_tg_send_sensf_res(ddev, sensf_req);
1217
1218 exit:
1219         if (rc)
1220                 digital_poll_next_tech(ddev);
1221
1222         dev_kfree_skb(resp);
1223 }
1224
1225 static int digital_tg_config_nfca(struct nfc_digital_dev *ddev)
1226 {
1227         int rc;
1228
1229         rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1230                                      NFC_DIGITAL_RF_TECH_106A);
1231         if (rc)
1232                 return rc;
1233
1234         return digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1235                                        NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1236 }
1237
1238 int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech)
1239 {
1240         int rc;
1241
1242         rc = digital_tg_config_nfca(ddev);
1243         if (rc)
1244                 return rc;
1245
1246         return digital_tg_listen(ddev, 300, digital_tg_recv_sens_req, NULL);
1247 }
1248
1249 static int digital_tg_config_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
1250 {
1251         int rc;
1252
1253         rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1254         if (rc)
1255                 return rc;
1256
1257         return digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1258                                        NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
1259 }
1260
1261 int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
1262 {
1263         int rc;
1264         u8 *nfcid2;
1265
1266         rc = digital_tg_config_nfcf(ddev, rf_tech);
1267         if (rc)
1268                 return rc;
1269
1270         nfcid2 = kzalloc(NFC_NFCID2_MAXSIZE, GFP_KERNEL);
1271         if (!nfcid2)
1272                 return -ENOMEM;
1273
1274         nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
1275         nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
1276         get_random_bytes(nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
1277
1278         return digital_tg_listen(ddev, 300, digital_tg_recv_sensf_req, nfcid2);
1279 }
1280
1281 void digital_tg_recv_md_req(struct nfc_digital_dev *ddev, void *arg,
1282                             struct sk_buff *resp)
1283 {
1284         u8 rf_tech;
1285         int rc;
1286
1287         if (IS_ERR(resp)) {
1288                 resp = NULL;
1289                 goto exit_free_skb;
1290         }
1291
1292         rc = ddev->ops->tg_get_rf_tech(ddev, &rf_tech);
1293         if (rc)
1294                 goto exit_free_skb;
1295
1296         switch (rf_tech) {
1297         case NFC_DIGITAL_RF_TECH_106A:
1298                 rc = digital_tg_config_nfca(ddev);
1299                 if (rc)
1300                         goto exit_free_skb;
1301                 digital_tg_recv_sens_req(ddev, arg, resp);
1302                 break;
1303         case NFC_DIGITAL_RF_TECH_212F:
1304         case NFC_DIGITAL_RF_TECH_424F:
1305                 rc = digital_tg_config_nfcf(ddev, rf_tech);
1306                 if (rc)
1307                         goto exit_free_skb;
1308                 digital_tg_recv_sensf_req(ddev, arg, resp);
1309                 break;
1310         default:
1311                 goto exit_free_skb;
1312         }
1313
1314         return;
1315
1316 exit_free_skb:
1317         digital_poll_next_tech(ddev);
1318         dev_kfree_skb(resp);
1319 }