Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 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 | |
Samuel Ortiz | c5da0e4 | 2013-09-20 09:05:48 +0200 | [diff] [blame] | 16 | #define pr_fmt(fmt) "digital: %s: " fmt, __func__ |
| 17 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 18 | #include "digital.h" |
| 19 | |
| 20 | #define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4 |
| 21 | #define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5 |
| 22 | |
| 23 | #define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0 |
| 24 | |
| 25 | #define DIGITAL_CMD_ATR_REQ 0x00 |
| 26 | #define DIGITAL_CMD_ATR_RES 0x01 |
| 27 | #define DIGITAL_CMD_PSL_REQ 0x04 |
| 28 | #define DIGITAL_CMD_PSL_RES 0x05 |
| 29 | #define DIGITAL_CMD_DEP_REQ 0x06 |
| 30 | #define DIGITAL_CMD_DEP_RES 0x07 |
| 31 | |
| 32 | #define DIGITAL_ATR_REQ_MIN_SIZE 16 |
| 33 | #define DIGITAL_ATR_REQ_MAX_SIZE 64 |
| 34 | |
Mark A. Greer | 05afedc | 2014-09-23 16:38:05 -0700 | [diff] [blame] | 35 | #define DIGITAL_DID_MAX 14 |
| 36 | |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 37 | #define DIGITAL_PAYLOAD_SIZE_MAX 254 |
| 38 | #define DIGITAL_PAYLOAD_BITS_TO_PP(s) (((s) & 0x3) << 4) |
| 39 | #define DIGITAL_PAYLOAD_PP_TO_BITS(s) (((s) >> 4) & 0x3) |
| 40 | #define DIGITAL_PAYLOAD_BITS_TO_FSL(s) ((s) & 0x3) |
| 41 | #define DIGITAL_PAYLOAD_FSL_TO_BITS(s) ((s) & 0x3) |
| 42 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 43 | #define DIGITAL_GB_BIT 0x02 |
| 44 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 45 | #define DIGITAL_NFC_DEP_REQ_RES_HEADROOM 2 /* SoD: [SB (NFC-A)] + LEN */ |
| 46 | #define DIGITAL_NFC_DEP_REQ_RES_TAILROOM 2 /* EoD: 2-byte CRC */ |
| 47 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 48 | #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0) |
| 49 | |
| 50 | #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10 |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 51 | #define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10 |
| 52 | #define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10 |
Mark A. Greer | 05afedc | 2014-09-23 16:38:05 -0700 | [diff] [blame] | 53 | #define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04 |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 54 | |
| 55 | #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \ |
| 56 | ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT) |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 57 | #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT) |
| 58 | #define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT) |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 59 | #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08) |
Mark A. Greer | 05afedc | 2014-09-23 16:38:05 -0700 | [diff] [blame] | 60 | #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT) |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 61 | #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03) |
| 62 | |
| 63 | #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00 |
| 64 | #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40 |
| 65 | #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80 |
| 66 | |
| 67 | struct digital_atr_req { |
| 68 | u8 dir; |
| 69 | u8 cmd; |
| 70 | u8 nfcid3[10]; |
| 71 | u8 did; |
| 72 | u8 bs; |
| 73 | u8 br; |
| 74 | u8 pp; |
| 75 | u8 gb[0]; |
| 76 | } __packed; |
| 77 | |
| 78 | struct digital_atr_res { |
| 79 | u8 dir; |
| 80 | u8 cmd; |
| 81 | u8 nfcid3[10]; |
| 82 | u8 did; |
| 83 | u8 bs; |
| 84 | u8 br; |
| 85 | u8 to; |
| 86 | u8 pp; |
| 87 | u8 gb[0]; |
| 88 | } __packed; |
| 89 | |
| 90 | struct digital_psl_req { |
| 91 | u8 dir; |
| 92 | u8 cmd; |
| 93 | u8 did; |
| 94 | u8 brs; |
| 95 | u8 fsl; |
| 96 | } __packed; |
| 97 | |
| 98 | struct digital_psl_res { |
| 99 | u8 dir; |
| 100 | u8 cmd; |
| 101 | u8 did; |
| 102 | } __packed; |
| 103 | |
| 104 | struct digital_dep_req_res { |
| 105 | u8 dir; |
| 106 | u8 cmd; |
| 107 | u8 pfb; |
| 108 | } __packed; |
| 109 | |
| 110 | static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg, |
| 111 | struct sk_buff *resp); |
| 112 | |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 113 | static const u8 digital_payload_bits_map[4] = { |
| 114 | [0] = 64, |
| 115 | [1] = 128, |
| 116 | [2] = 192, |
| 117 | [3] = 254 |
| 118 | }; |
| 119 | |
| 120 | static u8 digital_payload_bits_to_size(u8 payload_bits) |
| 121 | { |
| 122 | if (payload_bits >= ARRAY_SIZE(digital_payload_bits_map)) |
| 123 | return 0; |
| 124 | |
| 125 | return digital_payload_bits_map[payload_bits]; |
| 126 | } |
| 127 | |
| 128 | static u8 digital_payload_size_to_bits(u8 payload_size) |
| 129 | { |
| 130 | int i; |
| 131 | |
| 132 | for (i = 0; i < ARRAY_SIZE(digital_payload_bits_map); i++) |
| 133 | if (digital_payload_bits_map[i] == payload_size) |
| 134 | return i; |
| 135 | |
| 136 | return 0xff; |
| 137 | } |
| 138 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 139 | static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev, |
| 140 | struct sk_buff *skb) |
| 141 | { |
| 142 | skb_push(skb, sizeof(u8)); |
| 143 | |
| 144 | skb->data[0] = skb->len; |
| 145 | |
| 146 | if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A) |
| 147 | *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB; |
| 148 | } |
| 149 | |
| 150 | static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev, |
| 151 | struct sk_buff *skb) |
| 152 | { |
| 153 | u8 size; |
| 154 | |
| 155 | if (skb->len < 2) |
| 156 | return -EIO; |
| 157 | |
| 158 | if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A) |
| 159 | skb_pull(skb, sizeof(u8)); |
| 160 | |
| 161 | size = skb->data[0]; |
| 162 | if (size != skb->len) |
| 163 | return -EIO; |
| 164 | |
| 165 | skb_pull(skb, sizeof(u8)); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 170 | static struct sk_buff * |
| 171 | digital_send_dep_data_prep(struct nfc_digital_dev *ddev, struct sk_buff *skb, |
| 172 | struct digital_dep_req_res *dep_req_res, |
| 173 | struct digital_data_exch *data_exch) |
| 174 | { |
| 175 | struct sk_buff *new_skb; |
| 176 | |
| 177 | if (skb->len > ddev->remote_payload_max) { |
| 178 | dep_req_res->pfb |= DIGITAL_NFC_DEP_PFB_MI_BIT; |
| 179 | |
| 180 | new_skb = digital_skb_alloc(ddev, ddev->remote_payload_max); |
| 181 | if (!new_skb) { |
| 182 | kfree_skb(ddev->chaining_skb); |
| 183 | ddev->chaining_skb = NULL; |
| 184 | |
| 185 | return ERR_PTR(-ENOMEM); |
| 186 | } |
| 187 | |
| 188 | skb_reserve(new_skb, ddev->tx_headroom + NFC_HEADER_SIZE + |
| 189 | DIGITAL_NFC_DEP_REQ_RES_HEADROOM); |
| 190 | memcpy(skb_put(new_skb, ddev->remote_payload_max), skb->data, |
| 191 | ddev->remote_payload_max); |
| 192 | skb_pull(skb, ddev->remote_payload_max); |
| 193 | |
| 194 | ddev->chaining_skb = skb; |
| 195 | ddev->data_exch = data_exch; |
| 196 | } else { |
| 197 | ddev->chaining_skb = NULL; |
| 198 | new_skb = skb; |
| 199 | } |
| 200 | |
| 201 | return new_skb; |
| 202 | } |
| 203 | |
Mark A. Greer | dddb3da | 2014-07-22 20:18:01 -0700 | [diff] [blame] | 204 | static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg, |
| 205 | struct sk_buff *resp) |
| 206 | { |
| 207 | struct nfc_target *target = arg; |
| 208 | struct digital_psl_res *psl_res; |
| 209 | int rc; |
| 210 | |
| 211 | if (IS_ERR(resp)) { |
| 212 | rc = PTR_ERR(resp); |
| 213 | resp = NULL; |
| 214 | goto exit; |
| 215 | } |
| 216 | |
| 217 | rc = ddev->skb_check_crc(resp); |
| 218 | if (rc) { |
| 219 | PROTOCOL_ERR("14.4.1.6"); |
| 220 | goto exit; |
| 221 | } |
| 222 | |
| 223 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 224 | if (rc) { |
| 225 | PROTOCOL_ERR("14.4.1.2"); |
| 226 | goto exit; |
| 227 | } |
| 228 | |
| 229 | psl_res = (struct digital_psl_res *)resp->data; |
| 230 | |
| 231 | if ((resp->len != sizeof(*psl_res)) || |
| 232 | (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) || |
| 233 | (psl_res->cmd != DIGITAL_CMD_PSL_RES)) { |
| 234 | rc = -EIO; |
| 235 | goto exit; |
| 236 | } |
| 237 | |
| 238 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, |
| 239 | NFC_DIGITAL_RF_TECH_424F); |
| 240 | if (rc) |
| 241 | goto exit; |
| 242 | |
| 243 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, |
| 244 | NFC_DIGITAL_FRAMING_NFCF_NFC_DEP); |
| 245 | if (rc) |
| 246 | goto exit; |
| 247 | |
| 248 | if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) && |
| 249 | (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) { |
| 250 | ddev->skb_add_crc = digital_skb_add_crc_f; |
| 251 | ddev->skb_check_crc = digital_skb_check_crc_f; |
| 252 | } |
| 253 | |
| 254 | ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F; |
| 255 | |
| 256 | nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE, |
| 257 | NFC_RF_INITIATOR); |
| 258 | |
| 259 | ddev->curr_nfc_dep_pni = 0; |
| 260 | |
| 261 | exit: |
| 262 | dev_kfree_skb(resp); |
| 263 | |
| 264 | if (rc) |
| 265 | ddev->curr_protocol = 0; |
| 266 | } |
| 267 | |
| 268 | static int digital_in_send_psl_req(struct nfc_digital_dev *ddev, |
| 269 | struct nfc_target *target) |
| 270 | { |
| 271 | struct sk_buff *skb; |
| 272 | struct digital_psl_req *psl_req; |
Mark A. Greer | b15829b | 2014-09-23 16:38:02 -0700 | [diff] [blame] | 273 | int rc; |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 274 | u8 payload_size, payload_bits; |
Mark A. Greer | dddb3da | 2014-07-22 20:18:01 -0700 | [diff] [blame] | 275 | |
| 276 | skb = digital_skb_alloc(ddev, sizeof(*psl_req)); |
| 277 | if (!skb) |
| 278 | return -ENOMEM; |
| 279 | |
| 280 | skb_put(skb, sizeof(*psl_req)); |
| 281 | |
| 282 | psl_req = (struct digital_psl_req *)skb->data; |
| 283 | |
| 284 | psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT; |
| 285 | psl_req->cmd = DIGITAL_CMD_PSL_REQ; |
| 286 | psl_req->did = 0; |
| 287 | psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */ |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 288 | |
| 289 | payload_size = min(ddev->local_payload_max, ddev->remote_payload_max); |
| 290 | payload_bits = digital_payload_size_to_bits(payload_size); |
| 291 | psl_req->fsl = DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits); |
| 292 | |
| 293 | ddev->local_payload_max = payload_size; |
| 294 | ddev->remote_payload_max = payload_size; |
Mark A. Greer | dddb3da | 2014-07-22 20:18:01 -0700 | [diff] [blame] | 295 | |
| 296 | digital_skb_push_dep_sod(ddev, skb); |
| 297 | |
| 298 | ddev->skb_add_crc(skb); |
| 299 | |
Mark A. Greer | b15829b | 2014-09-23 16:38:02 -0700 | [diff] [blame] | 300 | rc = digital_in_send_cmd(ddev, skb, 500, digital_in_recv_psl_res, |
| 301 | target); |
| 302 | if (rc) |
| 303 | kfree_skb(skb); |
| 304 | |
| 305 | return rc; |
Mark A. Greer | dddb3da | 2014-07-22 20:18:01 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 308 | static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg, |
| 309 | struct sk_buff *resp) |
| 310 | { |
| 311 | struct nfc_target *target = arg; |
| 312 | struct digital_atr_res *atr_res; |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 313 | u8 gb_len, payload_bits; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 314 | int rc; |
| 315 | |
| 316 | if (IS_ERR(resp)) { |
| 317 | rc = PTR_ERR(resp); |
| 318 | resp = NULL; |
| 319 | goto exit; |
| 320 | } |
| 321 | |
| 322 | rc = ddev->skb_check_crc(resp); |
| 323 | if (rc) { |
| 324 | PROTOCOL_ERR("14.4.1.6"); |
| 325 | goto exit; |
| 326 | } |
| 327 | |
| 328 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 329 | if (rc) { |
| 330 | PROTOCOL_ERR("14.4.1.2"); |
| 331 | goto exit; |
| 332 | } |
| 333 | |
| 334 | if (resp->len < sizeof(struct digital_atr_res)) { |
| 335 | rc = -EIO; |
| 336 | goto exit; |
| 337 | } |
| 338 | |
| 339 | gb_len = resp->len - sizeof(struct digital_atr_res); |
| 340 | |
| 341 | atr_res = (struct digital_atr_res *)resp->data; |
| 342 | |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 343 | payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_res->pp); |
| 344 | ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits); |
| 345 | |
| 346 | if (!ddev->remote_payload_max) { |
| 347 | rc = -EINVAL; |
| 348 | goto exit; |
| 349 | } |
| 350 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 351 | rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len); |
| 352 | if (rc) |
| 353 | goto exit; |
| 354 | |
Mark A. Greer | dddb3da | 2014-07-22 20:18:01 -0700 | [diff] [blame] | 355 | if ((ddev->protocols & NFC_PROTO_FELICA_MASK) && |
| 356 | (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) { |
| 357 | rc = digital_in_send_psl_req(ddev, target); |
| 358 | if (!rc) |
| 359 | goto exit; |
| 360 | } |
| 361 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 362 | rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE, |
| 363 | NFC_RF_INITIATOR); |
| 364 | |
| 365 | ddev->curr_nfc_dep_pni = 0; |
| 366 | |
| 367 | exit: |
| 368 | dev_kfree_skb(resp); |
| 369 | |
| 370 | if (rc) |
| 371 | ddev->curr_protocol = 0; |
| 372 | } |
| 373 | |
| 374 | int digital_in_send_atr_req(struct nfc_digital_dev *ddev, |
| 375 | struct nfc_target *target, __u8 comm_mode, __u8 *gb, |
| 376 | size_t gb_len) |
| 377 | { |
| 378 | struct sk_buff *skb; |
| 379 | struct digital_atr_req *atr_req; |
| 380 | uint size; |
Mark A. Greer | b15829b | 2014-09-23 16:38:02 -0700 | [diff] [blame] | 381 | int rc; |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 382 | u8 payload_bits; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 383 | |
| 384 | size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len; |
| 385 | |
| 386 | if (size > DIGITAL_ATR_REQ_MAX_SIZE) { |
| 387 | PROTOCOL_ERR("14.6.1.1"); |
| 388 | return -EINVAL; |
| 389 | } |
| 390 | |
| 391 | skb = digital_skb_alloc(ddev, size); |
| 392 | if (!skb) |
| 393 | return -ENOMEM; |
| 394 | |
| 395 | skb_put(skb, sizeof(struct digital_atr_req)); |
| 396 | |
| 397 | atr_req = (struct digital_atr_req *)skb->data; |
| 398 | memset(atr_req, 0, sizeof(struct digital_atr_req)); |
| 399 | |
| 400 | atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT; |
| 401 | atr_req->cmd = DIGITAL_CMD_ATR_REQ; |
| 402 | if (target->nfcid2_len) |
Thierry Escande | 4f319e3 | 2014-01-02 11:58:14 +0100 | [diff] [blame] | 403 | memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 404 | else |
Thierry Escande | 4f319e3 | 2014-01-02 11:58:14 +0100 | [diff] [blame] | 405 | get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 406 | |
| 407 | atr_req->did = 0; |
| 408 | atr_req->bs = 0; |
| 409 | atr_req->br = 0; |
| 410 | |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 411 | ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX; |
| 412 | payload_bits = digital_payload_size_to_bits(ddev->local_payload_max); |
| 413 | atr_req->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 414 | |
| 415 | if (gb_len) { |
| 416 | atr_req->pp |= DIGITAL_GB_BIT; |
| 417 | memcpy(skb_put(skb, gb_len), gb, gb_len); |
| 418 | } |
| 419 | |
| 420 | digital_skb_push_dep_sod(ddev, skb); |
| 421 | |
| 422 | ddev->skb_add_crc(skb); |
| 423 | |
Mark A. Greer | b15829b | 2014-09-23 16:38:02 -0700 | [diff] [blame] | 424 | rc = digital_in_send_cmd(ddev, skb, 500, digital_in_recv_atr_res, |
| 425 | target); |
| 426 | if (rc) |
| 427 | kfree_skb(skb); |
| 428 | |
| 429 | return rc; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static int digital_in_send_rtox(struct nfc_digital_dev *ddev, |
| 433 | struct digital_data_exch *data_exch, u8 rtox) |
| 434 | { |
| 435 | struct digital_dep_req_res *dep_req; |
| 436 | struct sk_buff *skb; |
| 437 | int rc; |
| 438 | |
| 439 | skb = digital_skb_alloc(ddev, 1); |
| 440 | if (!skb) |
| 441 | return -ENOMEM; |
| 442 | |
| 443 | *skb_put(skb, 1) = rtox; |
| 444 | |
| 445 | skb_push(skb, sizeof(struct digital_dep_req_res)); |
| 446 | |
| 447 | dep_req = (struct digital_dep_req_res *)skb->data; |
| 448 | |
| 449 | dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT; |
| 450 | dep_req->cmd = DIGITAL_CMD_DEP_REQ; |
| 451 | dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU | |
| 452 | DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT; |
| 453 | |
| 454 | digital_skb_push_dep_sod(ddev, skb); |
| 455 | |
| 456 | ddev->skb_add_crc(skb); |
| 457 | |
| 458 | rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res, |
| 459 | data_exch); |
Mark A. Greer | b15829b | 2014-09-23 16:38:02 -0700 | [diff] [blame] | 460 | if (rc) |
| 461 | kfree_skb(skb); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 462 | |
| 463 | return rc; |
| 464 | } |
| 465 | |
| 466 | static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg, |
| 467 | struct sk_buff *resp) |
| 468 | { |
| 469 | struct digital_data_exch *data_exch = arg; |
| 470 | struct digital_dep_req_res *dep_res; |
| 471 | u8 pfb; |
| 472 | uint size; |
| 473 | int rc; |
| 474 | |
| 475 | if (IS_ERR(resp)) { |
| 476 | rc = PTR_ERR(resp); |
| 477 | resp = NULL; |
| 478 | goto exit; |
| 479 | } |
| 480 | |
| 481 | rc = ddev->skb_check_crc(resp); |
| 482 | if (rc) { |
| 483 | PROTOCOL_ERR("14.4.1.6"); |
| 484 | goto error; |
| 485 | } |
| 486 | |
| 487 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 488 | if (rc) { |
| 489 | PROTOCOL_ERR("14.4.1.2"); |
| 490 | goto exit; |
| 491 | } |
| 492 | |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 493 | if (resp->len > ddev->local_payload_max) { |
| 494 | rc = -EMSGSIZE; |
| 495 | goto exit; |
| 496 | } |
| 497 | |
Mark A. Greer | 6ce3066 | 2014-09-23 16:38:03 -0700 | [diff] [blame] | 498 | size = sizeof(struct digital_dep_req_res); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 499 | dep_res = (struct digital_dep_req_res *)resp->data; |
| 500 | |
Mark A. Greer | 6ce3066 | 2014-09-23 16:38:03 -0700 | [diff] [blame] | 501 | if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN || |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 502 | dep_res->cmd != DIGITAL_CMD_DEP_RES) { |
| 503 | rc = -EIO; |
| 504 | goto error; |
| 505 | } |
| 506 | |
| 507 | pfb = dep_res->pfb; |
| 508 | |
Mark A. Greer | 3bc3f88 | 2014-09-23 16:38:04 -0700 | [diff] [blame] | 509 | if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) { |
| 510 | PROTOCOL_ERR("14.8.2.1"); |
| 511 | rc = -EIO; |
| 512 | goto error; |
| 513 | } |
Mark A. Greer | 6ce3066 | 2014-09-23 16:38:03 -0700 | [diff] [blame] | 514 | |
Mark A. Greer | 3e6b0de | 2014-09-23 16:38:06 -0700 | [diff] [blame] | 515 | if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) { |
| 516 | rc = -EIO; |
| 517 | goto exit; |
| 518 | } |
| 519 | |
Mark A. Greer | 6ce3066 | 2014-09-23 16:38:03 -0700 | [diff] [blame] | 520 | if (size > resp->len) { |
| 521 | rc = -EIO; |
| 522 | goto error; |
| 523 | } |
| 524 | |
| 525 | skb_pull(resp, size); |
| 526 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 527 | switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) { |
| 528 | case DIGITAL_NFC_DEP_PFB_I_PDU: |
| 529 | if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) { |
| 530 | PROTOCOL_ERR("14.12.3.3"); |
| 531 | rc = -EIO; |
| 532 | goto error; |
| 533 | } |
| 534 | |
| 535 | ddev->curr_nfc_dep_pni = |
| 536 | DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1); |
| 537 | rc = 0; |
| 538 | break; |
| 539 | |
| 540 | case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU: |
Mark A. Greer | 485fdc9 | 2014-09-23 16:38:07 -0700 | [diff] [blame] | 541 | if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) { |
| 542 | PROTOCOL_ERR("14.12.3.3"); |
| 543 | rc = -EIO; |
| 544 | goto exit; |
| 545 | } |
| 546 | |
| 547 | ddev->curr_nfc_dep_pni = |
| 548 | DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1); |
| 549 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 550 | if (ddev->chaining_skb && !DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) { |
| 551 | rc = digital_in_send_dep_req(ddev, NULL, |
| 552 | ddev->chaining_skb, |
| 553 | ddev->data_exch); |
| 554 | if (rc) |
| 555 | goto error; |
| 556 | |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | pr_err("Received a ACK/NACK PDU\n"); |
Mark A. Greer | 485fdc9 | 2014-09-23 16:38:07 -0700 | [diff] [blame] | 561 | rc = -EINVAL; |
| 562 | goto exit; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 563 | |
| 564 | case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU: |
| 565 | if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) { |
| 566 | rc = -EINVAL; |
| 567 | goto error; |
| 568 | } |
| 569 | |
Mark A. Greer | 6ce3066 | 2014-09-23 16:38:03 -0700 | [diff] [blame] | 570 | rc = digital_in_send_rtox(ddev, data_exch, resp->data[0]); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 571 | if (rc) |
| 572 | goto error; |
| 573 | |
| 574 | kfree_skb(resp); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 579 | pr_err("MI bit set. Chained PDU not supported\n"); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 580 | rc = -EIO; |
| 581 | goto error; |
| 582 | } |
| 583 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 584 | exit: |
| 585 | data_exch->cb(data_exch->cb_context, resp, rc); |
| 586 | |
| 587 | error: |
| 588 | kfree(data_exch); |
| 589 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 590 | kfree_skb(ddev->chaining_skb); |
| 591 | ddev->chaining_skb = NULL; |
| 592 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 593 | if (rc) |
| 594 | kfree_skb(resp); |
| 595 | } |
| 596 | |
| 597 | int digital_in_send_dep_req(struct nfc_digital_dev *ddev, |
| 598 | struct nfc_target *target, struct sk_buff *skb, |
| 599 | struct digital_data_exch *data_exch) |
| 600 | { |
| 601 | struct digital_dep_req_res *dep_req; |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 602 | struct sk_buff *chaining_skb, *tmp_skb; |
| 603 | int rc; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 604 | |
| 605 | skb_push(skb, sizeof(struct digital_dep_req_res)); |
| 606 | |
| 607 | dep_req = (struct digital_dep_req_res *)skb->data; |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 608 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 609 | dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT; |
| 610 | dep_req->cmd = DIGITAL_CMD_DEP_REQ; |
| 611 | dep_req->pfb = ddev->curr_nfc_dep_pni; |
| 612 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 613 | chaining_skb = ddev->chaining_skb; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 614 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 615 | tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_req, data_exch); |
| 616 | if (IS_ERR(tmp_skb)) |
| 617 | return PTR_ERR(tmp_skb); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 618 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 619 | digital_skb_push_dep_sod(ddev, tmp_skb); |
| 620 | |
| 621 | ddev->skb_add_crc(tmp_skb); |
| 622 | |
| 623 | rc = digital_in_send_cmd(ddev, tmp_skb, 1500, digital_in_recv_dep_res, |
| 624 | data_exch); |
| 625 | if (rc) { |
| 626 | if (tmp_skb != skb) |
| 627 | kfree_skb(tmp_skb); |
| 628 | |
| 629 | kfree_skb(chaining_skb); |
| 630 | ddev->chaining_skb = NULL; |
| 631 | } |
| 632 | |
| 633 | return rc; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 634 | } |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 635 | |
Thierry Escande | b711ad5 | 2014-01-06 23:34:48 +0100 | [diff] [blame] | 636 | static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech) |
| 637 | { |
| 638 | ddev->curr_rf_tech = rf_tech; |
| 639 | |
| 640 | ddev->skb_add_crc = digital_skb_add_crc_none; |
| 641 | ddev->skb_check_crc = digital_skb_check_crc_none; |
| 642 | |
| 643 | if (DIGITAL_DRV_CAPS_TG_CRC(ddev)) |
| 644 | return; |
| 645 | |
| 646 | switch (ddev->curr_rf_tech) { |
| 647 | case NFC_DIGITAL_RF_TECH_106A: |
| 648 | ddev->skb_add_crc = digital_skb_add_crc_a; |
| 649 | ddev->skb_check_crc = digital_skb_check_crc_a; |
| 650 | break; |
| 651 | |
| 652 | case NFC_DIGITAL_RF_TECH_212F: |
| 653 | case NFC_DIGITAL_RF_TECH_424F: |
| 654 | ddev->skb_add_crc = digital_skb_add_crc_f; |
| 655 | ddev->skb_check_crc = digital_skb_check_crc_f; |
| 656 | break; |
| 657 | |
| 658 | default: |
| 659 | break; |
| 660 | } |
| 661 | } |
| 662 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 663 | static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg, |
| 664 | struct sk_buff *resp) |
| 665 | { |
| 666 | int rc; |
| 667 | struct digital_dep_req_res *dep_req; |
Mark A. Greer | 6ce3066 | 2014-09-23 16:38:03 -0700 | [diff] [blame] | 668 | u8 pfb; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 669 | size_t size; |
| 670 | |
| 671 | if (IS_ERR(resp)) { |
| 672 | rc = PTR_ERR(resp); |
| 673 | resp = NULL; |
| 674 | goto exit; |
| 675 | } |
| 676 | |
| 677 | rc = ddev->skb_check_crc(resp); |
| 678 | if (rc) { |
| 679 | PROTOCOL_ERR("14.4.1.6"); |
| 680 | goto exit; |
| 681 | } |
| 682 | |
| 683 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 684 | if (rc) { |
| 685 | PROTOCOL_ERR("14.4.1.2"); |
| 686 | goto exit; |
| 687 | } |
| 688 | |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 689 | if (resp->len > ddev->local_payload_max) { |
| 690 | rc = -EMSGSIZE; |
| 691 | goto exit; |
| 692 | } |
| 693 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 694 | size = sizeof(struct digital_dep_req_res); |
| 695 | dep_req = (struct digital_dep_req_res *)resp->data; |
| 696 | |
| 697 | if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT || |
| 698 | dep_req->cmd != DIGITAL_CMD_DEP_REQ) { |
| 699 | rc = -EIO; |
| 700 | goto exit; |
| 701 | } |
| 702 | |
Mark A. Greer | 6ce3066 | 2014-09-23 16:38:03 -0700 | [diff] [blame] | 703 | pfb = dep_req->pfb; |
| 704 | |
Mark A. Greer | 05afedc | 2014-09-23 16:38:05 -0700 | [diff] [blame] | 705 | if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) { |
| 706 | if (ddev->did && (ddev->did == resp->data[3])) { |
| 707 | size++; |
| 708 | } else { |
| 709 | rc = -EIO; |
| 710 | goto exit; |
| 711 | } |
| 712 | } else if (ddev->did) { |
| 713 | rc = -EIO; |
| 714 | goto exit; |
| 715 | } |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 716 | |
Mark A. Greer | 3e6b0de | 2014-09-23 16:38:06 -0700 | [diff] [blame] | 717 | if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) { |
| 718 | rc = -EIO; |
| 719 | goto exit; |
| 720 | } |
| 721 | |
Mark A. Greer | 6ce3066 | 2014-09-23 16:38:03 -0700 | [diff] [blame] | 722 | if (size > resp->len) { |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 723 | rc = -EIO; |
| 724 | goto exit; |
| 725 | } |
| 726 | |
Mark A. Greer | 6ce3066 | 2014-09-23 16:38:03 -0700 | [diff] [blame] | 727 | skb_pull(resp, size); |
| 728 | |
| 729 | switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) { |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 730 | case DIGITAL_NFC_DEP_PFB_I_PDU: |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 731 | pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n"); |
Mark A. Greer | 485fdc9 | 2014-09-23 16:38:07 -0700 | [diff] [blame] | 732 | |
| 733 | if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) { |
| 734 | PROTOCOL_ERR("14.12.3.4"); |
| 735 | rc = -EIO; |
| 736 | goto exit; |
| 737 | } |
| 738 | |
| 739 | rc = 0; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 740 | break; |
| 741 | case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU: |
Mark A. Greer | 485fdc9 | 2014-09-23 16:38:07 -0700 | [diff] [blame] | 742 | if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) { |
| 743 | PROTOCOL_ERR("14.12.3.4"); |
| 744 | rc = -EIO; |
| 745 | goto exit; |
| 746 | } |
| 747 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 748 | if (ddev->chaining_skb && !DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) { |
| 749 | rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb); |
| 750 | if (rc) |
| 751 | goto exit; |
| 752 | |
| 753 | return; |
| 754 | } |
| 755 | |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 756 | pr_err("Received a ACK/NACK PDU\n"); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 757 | rc = -EINVAL; |
| 758 | goto exit; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 759 | case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU: |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 760 | pr_err("Received a SUPERVISOR PDU\n"); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 761 | rc = -EINVAL; |
| 762 | goto exit; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 763 | } |
| 764 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 765 | rc = nfc_tm_data_received(ddev->nfc_dev, resp); |
| 766 | |
| 767 | exit: |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 768 | kfree_skb(ddev->chaining_skb); |
| 769 | ddev->chaining_skb = NULL; |
| 770 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 771 | if (rc) |
| 772 | kfree_skb(resp); |
| 773 | } |
| 774 | |
| 775 | int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb) |
| 776 | { |
| 777 | struct digital_dep_req_res *dep_res; |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 778 | struct sk_buff *chaining_skb, *tmp_skb; |
| 779 | int rc; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 780 | |
| 781 | skb_push(skb, sizeof(struct digital_dep_req_res)); |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 782 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 783 | dep_res = (struct digital_dep_req_res *)skb->data; |
| 784 | |
| 785 | dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN; |
| 786 | dep_res->cmd = DIGITAL_CMD_DEP_RES; |
| 787 | dep_res->pfb = ddev->curr_nfc_dep_pni; |
| 788 | |
Mark A. Greer | 05afedc | 2014-09-23 16:38:05 -0700 | [diff] [blame] | 789 | if (ddev->did) { |
| 790 | dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT; |
| 791 | |
| 792 | memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did, |
| 793 | sizeof(ddev->did)); |
| 794 | } |
| 795 | |
Mark A. Greer | 485fdc9 | 2014-09-23 16:38:07 -0700 | [diff] [blame] | 796 | ddev->curr_nfc_dep_pni = |
| 797 | DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1); |
| 798 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 799 | chaining_skb = ddev->chaining_skb; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 800 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 801 | tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_res, NULL); |
| 802 | if (IS_ERR(tmp_skb)) |
| 803 | return PTR_ERR(tmp_skb); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 804 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame^] | 805 | digital_skb_push_dep_sod(ddev, tmp_skb); |
| 806 | |
| 807 | ddev->skb_add_crc(tmp_skb); |
| 808 | |
| 809 | rc = digital_tg_send_cmd(ddev, tmp_skb, 1500, digital_tg_recv_dep_req, |
| 810 | NULL); |
| 811 | if (rc) { |
| 812 | if (tmp_skb != skb) |
| 813 | kfree_skb(tmp_skb); |
| 814 | |
| 815 | kfree_skb(chaining_skb); |
| 816 | ddev->chaining_skb = NULL; |
| 817 | } |
| 818 | |
| 819 | return rc; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev, |
| 823 | void *arg, struct sk_buff *resp) |
| 824 | { |
Thierry Escande | 67af1d7 | 2014-01-02 11:58:13 +0100 | [diff] [blame] | 825 | u8 rf_tech = (unsigned long)arg; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 826 | |
| 827 | if (IS_ERR(resp)) |
| 828 | return; |
| 829 | |
Thierry Escande | b711ad5 | 2014-01-06 23:34:48 +0100 | [diff] [blame] | 830 | digital_tg_set_rf_tech(ddev, rf_tech); |
| 831 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 832 | digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech); |
| 833 | |
| 834 | digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL); |
| 835 | |
| 836 | dev_kfree_skb(resp); |
| 837 | } |
| 838 | |
| 839 | static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did, |
| 840 | u8 rf_tech) |
| 841 | { |
| 842 | struct digital_psl_res *psl_res; |
| 843 | struct sk_buff *skb; |
| 844 | int rc; |
| 845 | |
| 846 | skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res)); |
| 847 | if (!skb) |
| 848 | return -ENOMEM; |
| 849 | |
| 850 | skb_put(skb, sizeof(struct digital_psl_res)); |
| 851 | |
| 852 | psl_res = (struct digital_psl_res *)skb->data; |
| 853 | |
| 854 | psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN; |
| 855 | psl_res->cmd = DIGITAL_CMD_PSL_RES; |
| 856 | psl_res->did = did; |
| 857 | |
| 858 | digital_skb_push_dep_sod(ddev, skb); |
| 859 | |
| 860 | ddev->skb_add_crc(skb); |
| 861 | |
Mark A. Greer | 485fdc9 | 2014-09-23 16:38:07 -0700 | [diff] [blame] | 862 | ddev->curr_nfc_dep_pni = 0; |
| 863 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 864 | rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete, |
Thierry Escande | 67af1d7 | 2014-01-02 11:58:13 +0100 | [diff] [blame] | 865 | (void *)(unsigned long)rf_tech); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 866 | if (rc) |
| 867 | kfree_skb(skb); |
| 868 | |
| 869 | return rc; |
| 870 | } |
| 871 | |
| 872 | static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg, |
| 873 | struct sk_buff *resp) |
| 874 | { |
| 875 | int rc; |
| 876 | struct digital_psl_req *psl_req; |
| 877 | u8 rf_tech; |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 878 | u8 dsi, payload_size, payload_bits; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 879 | |
| 880 | if (IS_ERR(resp)) { |
| 881 | rc = PTR_ERR(resp); |
| 882 | resp = NULL; |
| 883 | goto exit; |
| 884 | } |
| 885 | |
| 886 | rc = ddev->skb_check_crc(resp); |
| 887 | if (rc) { |
| 888 | PROTOCOL_ERR("14.4.1.6"); |
| 889 | goto exit; |
| 890 | } |
| 891 | |
| 892 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 893 | if (rc) { |
| 894 | PROTOCOL_ERR("14.4.1.2"); |
| 895 | goto exit; |
| 896 | } |
| 897 | |
| 898 | psl_req = (struct digital_psl_req *)resp->data; |
| 899 | |
| 900 | if (resp->len != sizeof(struct digital_psl_req) || |
| 901 | psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT || |
| 902 | psl_req->cmd != DIGITAL_CMD_PSL_REQ) { |
| 903 | rc = -EIO; |
| 904 | goto exit; |
| 905 | } |
| 906 | |
| 907 | dsi = (psl_req->brs >> 3) & 0x07; |
| 908 | switch (dsi) { |
| 909 | case 0: |
| 910 | rf_tech = NFC_DIGITAL_RF_TECH_106A; |
| 911 | break; |
| 912 | case 1: |
| 913 | rf_tech = NFC_DIGITAL_RF_TECH_212F; |
| 914 | break; |
| 915 | case 2: |
| 916 | rf_tech = NFC_DIGITAL_RF_TECH_424F; |
| 917 | break; |
| 918 | default: |
Masanari Iida | 77d84ff | 2013-12-09 00:22:53 +0900 | [diff] [blame] | 919 | pr_err("Unsupported dsi value %d\n", dsi); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 920 | goto exit; |
| 921 | } |
| 922 | |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 923 | payload_bits = DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req->fsl); |
| 924 | payload_size = digital_payload_bits_to_size(payload_bits); |
| 925 | |
| 926 | if (!payload_size || (payload_size > min(ddev->local_payload_max, |
| 927 | ddev->remote_payload_max))) { |
| 928 | rc = -EINVAL; |
| 929 | goto exit; |
| 930 | } |
| 931 | |
| 932 | ddev->local_payload_max = payload_size; |
| 933 | ddev->remote_payload_max = payload_size; |
| 934 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 935 | rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech); |
| 936 | |
| 937 | exit: |
| 938 | kfree_skb(resp); |
| 939 | } |
| 940 | |
| 941 | static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev, |
| 942 | void *arg, struct sk_buff *resp) |
| 943 | { |
| 944 | int offset; |
| 945 | |
| 946 | if (IS_ERR(resp)) { |
| 947 | digital_poll_next_tech(ddev); |
| 948 | return; |
| 949 | } |
| 950 | |
| 951 | offset = 2; |
| 952 | if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) |
| 953 | offset++; |
| 954 | |
| 955 | if (resp->data[offset] == DIGITAL_CMD_PSL_REQ) |
| 956 | digital_tg_recv_psl_req(ddev, arg, resp); |
| 957 | else |
| 958 | digital_tg_recv_dep_req(ddev, arg, resp); |
| 959 | } |
| 960 | |
| 961 | static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev, |
| 962 | struct digital_atr_req *atr_req) |
| 963 | { |
| 964 | struct digital_atr_res *atr_res; |
| 965 | struct sk_buff *skb; |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 966 | u8 *gb, payload_bits; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 967 | size_t gb_len; |
| 968 | int rc; |
| 969 | |
| 970 | gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len); |
| 971 | if (!gb) |
| 972 | gb_len = 0; |
| 973 | |
| 974 | skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len); |
| 975 | if (!skb) |
| 976 | return -ENOMEM; |
| 977 | |
| 978 | skb_put(skb, sizeof(struct digital_atr_res)); |
| 979 | atr_res = (struct digital_atr_res *)skb->data; |
| 980 | |
| 981 | memset(atr_res, 0, sizeof(struct digital_atr_res)); |
| 982 | |
| 983 | atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN; |
| 984 | atr_res->cmd = DIGITAL_CMD_ATR_RES; |
| 985 | memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3)); |
| 986 | atr_res->to = 8; |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 987 | |
| 988 | ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX; |
| 989 | payload_bits = digital_payload_size_to_bits(ddev->local_payload_max); |
| 990 | atr_res->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits); |
| 991 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 992 | if (gb_len) { |
| 993 | skb_put(skb, gb_len); |
| 994 | |
| 995 | atr_res->pp |= DIGITAL_GB_BIT; |
| 996 | memcpy(atr_res->gb, gb, gb_len); |
| 997 | } |
| 998 | |
| 999 | digital_skb_push_dep_sod(ddev, skb); |
| 1000 | |
| 1001 | ddev->skb_add_crc(skb); |
| 1002 | |
Mark A. Greer | 485fdc9 | 2014-09-23 16:38:07 -0700 | [diff] [blame] | 1003 | ddev->curr_nfc_dep_pni = 0; |
| 1004 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1005 | rc = digital_tg_send_cmd(ddev, skb, 999, |
| 1006 | digital_tg_send_atr_res_complete, NULL); |
Mark A. Greer | b15829b | 2014-09-23 16:38:02 -0700 | [diff] [blame] | 1007 | if (rc) |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1008 | kfree_skb(skb); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1009 | |
| 1010 | return rc; |
| 1011 | } |
| 1012 | |
| 1013 | void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg, |
| 1014 | struct sk_buff *resp) |
| 1015 | { |
| 1016 | int rc; |
| 1017 | struct digital_atr_req *atr_req; |
| 1018 | size_t gb_len, min_size; |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 1019 | u8 poll_tech_count, payload_bits; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1020 | |
| 1021 | if (IS_ERR(resp)) { |
| 1022 | rc = PTR_ERR(resp); |
| 1023 | resp = NULL; |
| 1024 | goto exit; |
| 1025 | } |
| 1026 | |
| 1027 | if (!resp->len) { |
| 1028 | rc = -EIO; |
| 1029 | goto exit; |
| 1030 | } |
| 1031 | |
| 1032 | if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) { |
| 1033 | min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2; |
Thierry Escande | b711ad5 | 2014-01-06 23:34:48 +0100 | [diff] [blame] | 1034 | digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1035 | } else { |
| 1036 | min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1; |
Thierry Escande | b711ad5 | 2014-01-06 23:34:48 +0100 | [diff] [blame] | 1037 | digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | if (resp->len < min_size) { |
| 1041 | rc = -EIO; |
| 1042 | goto exit; |
| 1043 | } |
| 1044 | |
Thierry Escande | 48e1044 | 2014-01-06 23:34:37 +0100 | [diff] [blame] | 1045 | ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1046 | |
| 1047 | rc = ddev->skb_check_crc(resp); |
| 1048 | if (rc) { |
| 1049 | PROTOCOL_ERR("14.4.1.6"); |
| 1050 | goto exit; |
| 1051 | } |
| 1052 | |
| 1053 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 1054 | if (rc) { |
| 1055 | PROTOCOL_ERR("14.4.1.2"); |
| 1056 | goto exit; |
| 1057 | } |
| 1058 | |
| 1059 | atr_req = (struct digital_atr_req *)resp->data; |
| 1060 | |
| 1061 | if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT || |
Mark A. Greer | 05afedc | 2014-09-23 16:38:05 -0700 | [diff] [blame] | 1062 | atr_req->cmd != DIGITAL_CMD_ATR_REQ || |
| 1063 | atr_req->did > DIGITAL_DID_MAX) { |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1064 | rc = -EINVAL; |
| 1065 | goto exit; |
| 1066 | } |
| 1067 | |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 1068 | payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_req->pp); |
| 1069 | ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits); |
| 1070 | |
| 1071 | if (!ddev->remote_payload_max) { |
| 1072 | rc = -EINVAL; |
| 1073 | goto exit; |
| 1074 | } |
| 1075 | |
Mark A. Greer | 05afedc | 2014-09-23 16:38:05 -0700 | [diff] [blame] | 1076 | ddev->did = atr_req->did; |
| 1077 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1078 | rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, |
| 1079 | NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED); |
| 1080 | if (rc) |
| 1081 | goto exit; |
| 1082 | |
| 1083 | rc = digital_tg_send_atr_res(ddev, atr_req); |
| 1084 | if (rc) |
| 1085 | goto exit; |
| 1086 | |
| 1087 | gb_len = resp->len - sizeof(struct digital_atr_req); |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame] | 1088 | |
| 1089 | poll_tech_count = ddev->poll_tech_count; |
| 1090 | ddev->poll_tech_count = 0; |
| 1091 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1092 | rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK, |
| 1093 | NFC_COMM_PASSIVE, atr_req->gb, gb_len); |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame] | 1094 | if (rc) { |
| 1095 | ddev->poll_tech_count = poll_tech_count; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1096 | goto exit; |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame] | 1097 | } |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 1098 | |
| 1099 | rc = 0; |
| 1100 | exit: |
| 1101 | if (rc) |
| 1102 | digital_poll_next_tech(ddev); |
| 1103 | |
| 1104 | dev_kfree_skb(resp); |
| 1105 | } |