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
18 #define DIGITAL_CMD_SENS_REQ 0x26
19 #define DIGITAL_CMD_ALL_REQ 0x52
20 #define DIGITAL_CMD_SEL_REQ_CL1 0x93
21 #define DIGITAL_CMD_SEL_REQ_CL2 0x95
22 #define DIGITAL_CMD_SEL_REQ_CL3 0x97
24 #define DIGITAL_SDD_REQ_SEL_PAR 0x20
26 #define DIGITAL_SDD_RES_CT 0x88
27 #define DIGITAL_SDD_RES_LEN 5
29 #define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04))
30 #define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60))
32 #define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x000C) == 0x000C)
33 #define DIGITAL_SENS_RES_IS_VALID(sens_res) \
34 ((!((sens_res) & 0x1F00) && (((sens_res) & 0x000C) == 0x000C)) || \
35 (((sens_res) & 0x1F00) && ((sens_res) & 0x000C) != 0x000C))
37 #define DIGITAL_MIFARE_READ_RES_LEN 16
38 #define DIGITAL_MIFARE_ACK_RES 0x0A
40 struct digital_sdd_res {
45 struct digital_sel_req {
52 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
53 struct nfc_target *target);
55 static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
58 struct nfc_target *target = arg;
69 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
70 rc = digital_skb_check_crc_a(resp);
72 PROTOCOL_ERR("4.4.1.3");
82 sel_res = resp->data[0];
84 if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) {
85 rc = digital_in_send_sdd_req(ddev, target);
92 if (DIGITAL_SEL_RES_IS_T2T(sel_res)) {
93 nfc_proto = NFC_PROTO_MIFARE;
99 target->sel_res = sel_res;
101 rc = digital_target_found(ddev, target, nfc_proto);
110 digital_poll_next_tech(ddev);
113 static int digital_in_send_sel_req(struct nfc_digital_dev *ddev,
114 struct nfc_target *target,
115 struct digital_sdd_res *sdd_res)
118 struct digital_sel_req *sel_req;
122 skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req));
126 skb_put(skb, sizeof(struct digital_sel_req));
127 sel_req = (struct digital_sel_req *)skb->data;
129 if (target->nfcid1_len <= 4)
130 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
131 else if (target->nfcid1_len < 10)
132 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
134 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
136 sel_req->sel_cmd = sel_cmd;
138 memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4);
139 sel_req->bcc = sdd_res->bcc;
141 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
142 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
143 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
147 digital_skb_add_crc_a(skb);
150 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res,
159 static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg,
160 struct sk_buff *resp)
162 struct nfc_target *target = arg;
163 struct digital_sdd_res *sdd_res;
174 if (resp->len < DIGITAL_SDD_RES_LEN) {
175 PROTOCOL_ERR("4.7.2.8");
180 sdd_res = (struct digital_sdd_res *)resp->data;
182 for (i = 0, bcc = 0; i < 4; i++)
183 bcc ^= sdd_res->nfcid1[i];
185 if (bcc != sdd_res->bcc) {
186 PROTOCOL_ERR("4.7.2.6");
191 if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) {
199 memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset,
201 target->nfcid1_len += size;
203 rc = digital_in_send_sel_req(ddev, target, sdd_res);
210 digital_poll_next_tech(ddev);
214 static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
215 struct nfc_target *target)
221 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
222 NFC_DIGITAL_FRAMING_NFCA_STANDARD);
226 skb = digital_skb_alloc(ddev, 2);
228 PR_ERR("alloc_skb failed");
232 if (target->nfcid1_len == 0)
233 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
234 else if (target->nfcid1_len == 3)
235 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
237 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
239 *skb_put(skb, sizeof(u8)) = sel_cmd;
240 *skb_put(skb, sizeof(u8)) = DIGITAL_SDD_REQ_SEL_PAR;
242 return digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res,
246 static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg,
247 struct sk_buff *resp)
249 struct nfc_target *target = NULL;
259 if (resp->len < sizeof(u16)) {
264 target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
270 memcpy(&target->sens_res, resp->data, sizeof(u16));
272 sens_res = be16_to_cpu(target->sens_res);
274 if (!DIGITAL_SENS_RES_IS_VALID(sens_res)) {
275 PROTOCOL_ERR("4.6.3.3");
280 if (DIGITAL_SENS_RES_IS_T1T(sens_res))
281 rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL);
283 rc = digital_in_send_sdd_req(ddev, target);
290 digital_poll_next_tech(ddev);
294 int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech)
299 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
300 NFC_DIGITAL_RF_TECH_106A);
304 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
305 NFC_DIGITAL_FRAMING_NFCA_SHORT);
309 skb = digital_skb_alloc(ddev, 1);
313 *skb_put(skb, sizeof(u8)) = DIGITAL_CMD_SENS_REQ;
315 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL);
322 int digital_in_recv_mifare_res(struct sk_buff *resp)
324 /* Successful READ command response is 16 data bytes + 2 CRC bytes long.
325 * Since the driver can't differentiate a ACK/NACK response from a valid
326 * READ response, the CRC calculation must be handled at digital level
327 * even if the driver supports it for this technology.
329 if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) {
330 if (digital_skb_check_crc_a(resp)) {
331 PROTOCOL_ERR("9.4.1.2");
338 /* ACK response (i.e. successful WRITE). */
339 if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) {
344 /* NACK and any other responses are treated as error. */