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