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 #define DIGITAL_CMD_SENSF_REQ 0x00
41 #define DIGITAL_CMD_SENSF_RES 0x01
43 #define DIGITAL_SENSF_RES_MIN_LENGTH 17
44 #define DIGITAL_SENSF_RES_RD_AP_B1 0x00
45 #define DIGITAL_SENSF_RES_RD_AP_B2 0x8F
47 #define DIGITAL_SENSF_REQ_RC_NONE 0
48 #define DIGITAL_SENSF_REQ_RC_SC 1
49 #define DIGITAL_SENSF_REQ_RC_AP 2
51 struct digital_sdd_res
{
56 struct digital_sel_req
{
63 struct digital_sensf_req
{
71 struct digital_sensf_res
{
82 static int digital_in_send_sdd_req(struct nfc_digital_dev
*ddev
,
83 struct nfc_target
*target
);
85 static void digital_in_recv_sel_res(struct nfc_digital_dev
*ddev
, void *arg
,
88 struct nfc_target
*target
= arg
;
99 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev
)) {
100 rc
= digital_skb_check_crc_a(resp
);
102 PROTOCOL_ERR("4.4.1.3");
112 sel_res
= resp
->data
[0];
114 if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res
)) {
115 rc
= digital_in_send_sdd_req(ddev
, target
);
122 if (DIGITAL_SEL_RES_IS_T2T(sel_res
)) {
123 nfc_proto
= NFC_PROTO_MIFARE
;
129 target
->sel_res
= sel_res
;
131 rc
= digital_target_found(ddev
, target
, nfc_proto
);
140 digital_poll_next_tech(ddev
);
143 static int digital_in_send_sel_req(struct nfc_digital_dev
*ddev
,
144 struct nfc_target
*target
,
145 struct digital_sdd_res
*sdd_res
)
148 struct digital_sel_req
*sel_req
;
152 skb
= digital_skb_alloc(ddev
, sizeof(struct digital_sel_req
));
156 skb_put(skb
, sizeof(struct digital_sel_req
));
157 sel_req
= (struct digital_sel_req
*)skb
->data
;
159 if (target
->nfcid1_len
<= 4)
160 sel_cmd
= DIGITAL_CMD_SEL_REQ_CL1
;
161 else if (target
->nfcid1_len
< 10)
162 sel_cmd
= DIGITAL_CMD_SEL_REQ_CL2
;
164 sel_cmd
= DIGITAL_CMD_SEL_REQ_CL3
;
166 sel_req
->sel_cmd
= sel_cmd
;
168 memcpy(sel_req
->nfcid1
, sdd_res
->nfcid1
, 4);
169 sel_req
->bcc
= sdd_res
->bcc
;
171 if (DIGITAL_DRV_CAPS_IN_CRC(ddev
)) {
172 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
173 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A
);
177 digital_skb_add_crc_a(skb
);
180 rc
= digital_in_send_cmd(ddev
, skb
, 30, digital_in_recv_sel_res
,
189 static void digital_in_recv_sdd_res(struct nfc_digital_dev
*ddev
, void *arg
,
190 struct sk_buff
*resp
)
192 struct nfc_target
*target
= arg
;
193 struct digital_sdd_res
*sdd_res
;
204 if (resp
->len
< DIGITAL_SDD_RES_LEN
) {
205 PROTOCOL_ERR("4.7.2.8");
210 sdd_res
= (struct digital_sdd_res
*)resp
->data
;
212 for (i
= 0, bcc
= 0; i
< 4; i
++)
213 bcc
^= sdd_res
->nfcid1
[i
];
215 if (bcc
!= sdd_res
->bcc
) {
216 PROTOCOL_ERR("4.7.2.6");
221 if (sdd_res
->nfcid1
[0] == DIGITAL_SDD_RES_CT
) {
229 memcpy(target
->nfcid1
+ target
->nfcid1_len
, sdd_res
->nfcid1
+ offset
,
231 target
->nfcid1_len
+= size
;
233 rc
= digital_in_send_sel_req(ddev
, target
, sdd_res
);
240 digital_poll_next_tech(ddev
);
244 static int digital_in_send_sdd_req(struct nfc_digital_dev
*ddev
,
245 struct nfc_target
*target
)
251 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
252 NFC_DIGITAL_FRAMING_NFCA_STANDARD
);
256 skb
= digital_skb_alloc(ddev
, 2);
258 PR_ERR("alloc_skb failed");
262 if (target
->nfcid1_len
== 0)
263 sel_cmd
= DIGITAL_CMD_SEL_REQ_CL1
;
264 else if (target
->nfcid1_len
== 3)
265 sel_cmd
= DIGITAL_CMD_SEL_REQ_CL2
;
267 sel_cmd
= DIGITAL_CMD_SEL_REQ_CL3
;
269 *skb_put(skb
, sizeof(u8
)) = sel_cmd
;
270 *skb_put(skb
, sizeof(u8
)) = DIGITAL_SDD_REQ_SEL_PAR
;
272 return digital_in_send_cmd(ddev
, skb
, 30, digital_in_recv_sdd_res
,
276 static void digital_in_recv_sens_res(struct nfc_digital_dev
*ddev
, void *arg
,
277 struct sk_buff
*resp
)
279 struct nfc_target
*target
= NULL
;
289 if (resp
->len
< sizeof(u16
)) {
294 target
= kzalloc(sizeof(struct nfc_target
), GFP_KERNEL
);
300 memcpy(&target
->sens_res
, resp
->data
, sizeof(u16
));
302 sens_res
= be16_to_cpu(target
->sens_res
);
304 if (!DIGITAL_SENS_RES_IS_VALID(sens_res
)) {
305 PROTOCOL_ERR("4.6.3.3");
310 if (DIGITAL_SENS_RES_IS_T1T(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
)
382 struct nfc_target target
;
383 struct digital_sensf_res
*sensf_res
;
391 if (resp
->len
< DIGITAL_SENSF_RES_MIN_LENGTH
) {
396 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev
)) {
397 rc
= digital_skb_check_crc_f(resp
);
399 PROTOCOL_ERR("6.4.1.8");
406 memset(&target
, 0, sizeof(struct nfc_target
));
408 sensf_res
= (struct digital_sensf_res
*)resp
->data
;
410 memcpy(target
.sensf_res
, sensf_res
, resp
->len
);
411 target
.sensf_res_len
= resp
->len
;
413 memcpy(target
.nfcid2
, sensf_res
->nfcid2
, NFC_NFCID2_MAXSIZE
);
414 target
.nfcid2_len
= NFC_NFCID2_MAXSIZE
;
416 rc
= digital_target_found(ddev
, &target
, NFC_PROTO_FELICA
);
422 digital_poll_next_tech(ddev
);
425 int digital_in_send_sensf_req(struct nfc_digital_dev
*ddev
, u8 rf_tech
)
427 struct digital_sensf_req
*sensf_req
;
432 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_RF_TECH
, rf_tech
);
436 rc
= digital_in_configure_hw(ddev
, NFC_DIGITAL_CONFIG_FRAMING
,
437 NFC_DIGITAL_FRAMING_NFCF
);
441 size
= sizeof(struct digital_sensf_req
);
443 skb
= digital_skb_alloc(ddev
, size
);
449 sensf_req
= (struct digital_sensf_req
*)skb
->data
;
450 sensf_req
->cmd
= DIGITAL_CMD_SENSF_REQ
;
451 sensf_req
->sc1
= 0xFF;
452 sensf_req
->sc2
= 0xFF;
456 *skb_push(skb
, 1) = size
+ 1;
458 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev
))
459 digital_skb_add_crc_f(skb
);
461 rc
= digital_in_send_cmd(ddev
, skb
, 30, digital_in_recv_sensf_res
,