2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
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.
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
16 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
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
26 #define DIGITAL_SDD_REQ_SEL_PAR 0x20
28 #define DIGITAL_SDD_RES_CT 0x88
29 #define DIGITAL_SDD_RES_LEN 5
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_NFC_DEP(sel_res) ((sel_res) & 0x40)
35 #define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x0C00) == 0x0C00)
36 #define DIGITAL_SENS_RES_IS_VALID(sens_res) \
37 ((!((sens_res) & 0x001F) && (((sens_res) & 0x0C00) == 0x0C00)) || \
38 (((sens_res) & 0x001F) && ((sens_res) & 0x0C00) != 0x0C00))
40 #define DIGITAL_MIFARE_READ_RES_LEN 16
41 #define DIGITAL_MIFARE_ACK_RES 0x0A
43 #define DIGITAL_CMD_SENSF_REQ 0x00
44 #define DIGITAL_CMD_SENSF_RES 0x01
46 #define DIGITAL_SENSF_RES_MIN_LENGTH 17
47 #define DIGITAL_SENSF_RES_RD_AP_B1 0x00
48 #define DIGITAL_SENSF_RES_RD_AP_B2 0x8F
50 #define DIGITAL_SENSF_REQ_RC_NONE 0
51 #define DIGITAL_SENSF_REQ_RC_SC 1
52 #define DIGITAL_SENSF_REQ_RC_AP 2
54 struct digital_sdd_res {
59 struct digital_sel_req {
66 struct digital_sensf_req {
74 struct digital_sensf_res {
85 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
86 struct nfc_target *target);
88 static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
91 struct nfc_target *target = arg;
102 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
103 rc = digital_skb_check_crc_a(resp);
105 PROTOCOL_ERR("4.4.1.3");
115 sel_res = resp->data[0];
117 if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) {
118 rc = digital_in_send_sdd_req(ddev, target);
125 if (DIGITAL_SEL_RES_IS_T2T(sel_res)) {
126 nfc_proto = NFC_PROTO_MIFARE;
127 } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) {
128 nfc_proto = NFC_PROTO_NFC_DEP;
134 target->sel_res = sel_res;
136 rc = digital_target_found(ddev, target, nfc_proto);
145 digital_poll_next_tech(ddev);
148 static int digital_in_send_sel_req(struct nfc_digital_dev *ddev,
149 struct nfc_target *target,
150 struct digital_sdd_res *sdd_res)
153 struct digital_sel_req *sel_req;
157 skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req));
161 skb_put(skb, sizeof(struct digital_sel_req));
162 sel_req = (struct digital_sel_req *)skb->data;
164 if (target->nfcid1_len <= 4)
165 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
166 else if (target->nfcid1_len < 10)
167 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
169 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
171 sel_req->sel_cmd = sel_cmd;
173 memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4);
174 sel_req->bcc = sdd_res->bcc;
176 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
177 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
178 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
182 digital_skb_add_crc_a(skb);
185 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res,
194 static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg,
195 struct sk_buff *resp)
197 struct nfc_target *target = arg;
198 struct digital_sdd_res *sdd_res;
209 if (resp->len < DIGITAL_SDD_RES_LEN) {
210 PROTOCOL_ERR("4.7.2.8");
215 sdd_res = (struct digital_sdd_res *)resp->data;
217 for (i = 0, bcc = 0; i < 4; i++)
218 bcc ^= sdd_res->nfcid1[i];
220 if (bcc != sdd_res->bcc) {
221 PROTOCOL_ERR("4.7.2.6");
226 if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) {
234 memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset,
236 target->nfcid1_len += size;
238 rc = digital_in_send_sel_req(ddev, target, sdd_res);
245 digital_poll_next_tech(ddev);
249 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
250 struct nfc_target *target)
256 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
257 NFC_DIGITAL_FRAMING_NFCA_STANDARD);
261 skb = digital_skb_alloc(ddev, 2);
265 if (target->nfcid1_len == 0)
266 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
267 else if (target->nfcid1_len == 3)
268 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
270 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
272 *skb_put(skb, sizeof(u8)) = sel_cmd;
273 *skb_put(skb, sizeof(u8)) = DIGITAL_SDD_REQ_SEL_PAR;
275 return digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res,
279 static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg,
280 struct sk_buff *resp)
282 struct nfc_target *target = NULL;
291 if (resp->len < sizeof(u16)) {
296 target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
302 target->sens_res = __le16_to_cpu(*(__le16 *)resp->data);
304 if (!DIGITAL_SENS_RES_IS_VALID(target->sens_res)) {
305 PROTOCOL_ERR("4.6.3.3");
310 if (DIGITAL_SENS_RES_IS_T1T(target->sens_res))
311 rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL);
313 rc = digital_in_send_sdd_req(ddev, target);
320 digital_poll_next_tech(ddev);
324 int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech)
329 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
330 NFC_DIGITAL_RF_TECH_106A);
334 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
335 NFC_DIGITAL_FRAMING_NFCA_SHORT);
339 skb = digital_skb_alloc(ddev, 1);
343 *skb_put(skb, sizeof(u8)) = DIGITAL_CMD_SENS_REQ;
345 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL);
352 int digital_in_recv_mifare_res(struct sk_buff *resp)
354 /* Successful READ command response is 16 data bytes + 2 CRC bytes long.
355 * Since the driver can't differentiate a ACK/NACK response from a valid
356 * READ response, the CRC calculation must be handled at digital level
357 * even if the driver supports it for this technology.
359 if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) {
360 if (digital_skb_check_crc_a(resp)) {
361 PROTOCOL_ERR("9.4.1.2");
368 /* ACK response (i.e. successful WRITE). */
369 if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) {
374 /* NACK and any other responses are treated as error. */
378 static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
379 struct sk_buff *resp)
383 struct nfc_target target;
384 struct digital_sensf_res *sensf_res;
392 if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) {
397 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
398 rc = digital_skb_check_crc_f(resp);
400 PROTOCOL_ERR("6.4.1.8");
407 memset(&target, 0, sizeof(struct nfc_target));
409 sensf_res = (struct digital_sensf_res *)resp->data;
411 memcpy(target.sensf_res, sensf_res, resp->len);
412 target.sensf_res_len = resp->len;
414 memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE);
415 target.nfcid2_len = NFC_NFCID2_MAXSIZE;
417 if (target.nfcid2[0] == DIGITAL_SENSF_NFCID2_NFC_DEP_B1 &&
418 target.nfcid2[1] == DIGITAL_SENSF_NFCID2_NFC_DEP_B2)
419 proto = NFC_PROTO_NFC_DEP;
421 proto = NFC_PROTO_FELICA;
423 rc = digital_target_found(ddev, &target, proto);
429 digital_poll_next_tech(ddev);
432 int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech)
434 struct digital_sensf_req *sensf_req;
439 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
443 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
444 NFC_DIGITAL_FRAMING_NFCF);
448 size = sizeof(struct digital_sensf_req);
450 skb = digital_skb_alloc(ddev, size);
456 sensf_req = (struct digital_sensf_req *)skb->data;
457 sensf_req->cmd = DIGITAL_CMD_SENSF_REQ;
458 sensf_req->sc1 = 0xFF;
459 sensf_req->sc2 = 0xFF;
463 *skb_push(skb, 1) = size + 1;
465 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev))
466 digital_skb_add_crc_f(skb);
468 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res,
476 static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev)
481 skb = digital_skb_alloc(ddev, 1);
485 *skb_put(skb, 1) = DIGITAL_SEL_RES_NFC_DEP;
487 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
488 digital_skb_add_crc_a(skb);
490 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_atr_req,
498 static void digital_tg_recv_sel_req(struct nfc_digital_dev *ddev, void *arg,
499 struct sk_buff *resp)
509 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
510 rc = digital_skb_check_crc_a(resp);
512 PROTOCOL_ERR("4.4.1.3");
517 /* Silently ignore SEL_REQ content and send a SEL_RES for NFC-DEP */
519 rc = digital_tg_send_sel_res(ddev);
523 digital_poll_next_tech(ddev);
528 static int digital_tg_send_sdd_res(struct nfc_digital_dev *ddev)
531 struct digital_sdd_res *sdd_res;
534 skb = digital_skb_alloc(ddev, sizeof(struct digital_sdd_res));
538 skb_put(skb, sizeof(struct digital_sdd_res));
539 sdd_res = (struct digital_sdd_res *)skb->data;
541 sdd_res->nfcid1[0] = 0x08;
542 get_random_bytes(sdd_res->nfcid1 + 1, 3);
545 for (i = 0; i < 4; i++)
546 sdd_res->bcc ^= sdd_res->nfcid1[i];
548 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sel_req,
556 static void digital_tg_recv_sdd_req(struct nfc_digital_dev *ddev, void *arg,
557 struct sk_buff *resp)
568 sdd_req = resp->data;
570 if (resp->len < 2 || sdd_req[0] != DIGITAL_CMD_SEL_REQ_CL1 ||
571 sdd_req[1] != DIGITAL_SDD_REQ_SEL_PAR) {
576 rc = digital_tg_send_sdd_res(ddev);
580 digital_poll_next_tech(ddev);
585 static int digital_tg_send_sens_res(struct nfc_digital_dev *ddev)
591 skb = digital_skb_alloc(ddev, 2);
595 sens_res = skb_put(skb, 2);
597 sens_res[0] = (DIGITAL_SENS_RES_NFC_DEP >> 8) & 0xFF;
598 sens_res[1] = DIGITAL_SENS_RES_NFC_DEP & 0xFF;
600 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sdd_req,
608 void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
609 struct sk_buff *resp)
620 sens_req = resp->data[0];
622 if (!resp->len || (sens_req != DIGITAL_CMD_SENS_REQ &&
623 sens_req != DIGITAL_CMD_ALL_REQ)) {
628 rc = digital_tg_send_sens_res(ddev);
632 digital_poll_next_tech(ddev);
637 static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
638 struct digital_sensf_req *sensf_req)
643 struct digital_sensf_res *sensf_res;
645 size = sizeof(struct digital_sensf_res);
647 if (sensf_req->rc != DIGITAL_SENSF_REQ_RC_NONE)
648 size -= sizeof(sensf_res->rd);
650 skb = digital_skb_alloc(ddev, size);
656 sensf_res = (struct digital_sensf_res *)skb->data;
658 memset(sensf_res, 0, size);
660 sensf_res->cmd = DIGITAL_CMD_SENSF_RES;
661 sensf_res->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
662 sensf_res->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
663 get_random_bytes(&sensf_res->nfcid2[2], 6);
665 switch (sensf_req->rc) {
666 case DIGITAL_SENSF_REQ_RC_SC:
667 sensf_res->rd[0] = sensf_req->sc1;
668 sensf_res->rd[1] = sensf_req->sc2;
670 case DIGITAL_SENSF_REQ_RC_AP:
671 sensf_res->rd[0] = DIGITAL_SENSF_RES_RD_AP_B1;
672 sensf_res->rd[1] = DIGITAL_SENSF_RES_RD_AP_B2;
676 *skb_push(skb, sizeof(u8)) = size + 1;
678 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
679 digital_skb_add_crc_f(skb);
681 rc = digital_tg_send_cmd(ddev, skb, 300,
682 digital_tg_recv_atr_req, NULL);
689 void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
690 struct sk_buff *resp)
692 struct digital_sensf_req *sensf_req;
701 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
702 rc = digital_skb_check_crc_f(resp);
704 PROTOCOL_ERR("6.4.1.8");
709 if (resp->len != sizeof(struct digital_sensf_req) + 1) {
715 sensf_req = (struct digital_sensf_req *)resp->data;
717 if (sensf_req->cmd != DIGITAL_CMD_SENSF_REQ) {
722 rc = digital_tg_send_sensf_res(ddev, sensf_req);
726 digital_poll_next_tech(ddev);
731 int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech)
735 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
739 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
740 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
744 return digital_tg_listen(ddev, 300, digital_tg_recv_sens_req, NULL);
747 int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
752 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
756 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
757 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
761 nfcid2 = kzalloc(NFC_NFCID2_MAXSIZE, GFP_KERNEL);
765 nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
766 nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
767 get_random_bytes(nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
769 return digital_tg_listen(ddev, 300, digital_tg_recv_sensf_req, nfcid2);