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 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 35 | #define DIGITAL_LR_BITS_PAYLOAD_SIZE_254B 0x30 |
| 36 | #define DIGITAL_GB_BIT 0x02 |
| 37 | |
| 38 | #define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0) |
| 39 | |
| 40 | #define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10 |
| 41 | |
| 42 | #define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \ |
| 43 | ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT) |
| 44 | #define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & 0x10) |
| 45 | #define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08) |
| 46 | #define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04) |
| 47 | #define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03) |
| 48 | |
| 49 | #define DIGITAL_NFC_DEP_PFB_I_PDU 0x00 |
| 50 | #define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40 |
| 51 | #define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80 |
| 52 | |
| 53 | struct digital_atr_req { |
| 54 | u8 dir; |
| 55 | u8 cmd; |
| 56 | u8 nfcid3[10]; |
| 57 | u8 did; |
| 58 | u8 bs; |
| 59 | u8 br; |
| 60 | u8 pp; |
| 61 | u8 gb[0]; |
| 62 | } __packed; |
| 63 | |
| 64 | struct digital_atr_res { |
| 65 | u8 dir; |
| 66 | u8 cmd; |
| 67 | u8 nfcid3[10]; |
| 68 | u8 did; |
| 69 | u8 bs; |
| 70 | u8 br; |
| 71 | u8 to; |
| 72 | u8 pp; |
| 73 | u8 gb[0]; |
| 74 | } __packed; |
| 75 | |
| 76 | struct digital_psl_req { |
| 77 | u8 dir; |
| 78 | u8 cmd; |
| 79 | u8 did; |
| 80 | u8 brs; |
| 81 | u8 fsl; |
| 82 | } __packed; |
| 83 | |
| 84 | struct digital_psl_res { |
| 85 | u8 dir; |
| 86 | u8 cmd; |
| 87 | u8 did; |
| 88 | } __packed; |
| 89 | |
| 90 | struct digital_dep_req_res { |
| 91 | u8 dir; |
| 92 | u8 cmd; |
| 93 | u8 pfb; |
| 94 | } __packed; |
| 95 | |
| 96 | static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg, |
| 97 | struct sk_buff *resp); |
| 98 | |
| 99 | static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev, |
| 100 | struct sk_buff *skb) |
| 101 | { |
| 102 | skb_push(skb, sizeof(u8)); |
| 103 | |
| 104 | skb->data[0] = skb->len; |
| 105 | |
| 106 | if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A) |
| 107 | *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB; |
| 108 | } |
| 109 | |
| 110 | static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev, |
| 111 | struct sk_buff *skb) |
| 112 | { |
| 113 | u8 size; |
| 114 | |
| 115 | if (skb->len < 2) |
| 116 | return -EIO; |
| 117 | |
| 118 | if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A) |
| 119 | skb_pull(skb, sizeof(u8)); |
| 120 | |
| 121 | size = skb->data[0]; |
| 122 | if (size != skb->len) |
| 123 | return -EIO; |
| 124 | |
| 125 | skb_pull(skb, sizeof(u8)); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg, |
| 131 | struct sk_buff *resp) |
| 132 | { |
| 133 | struct nfc_target *target = arg; |
| 134 | struct digital_atr_res *atr_res; |
| 135 | u8 gb_len; |
| 136 | int rc; |
| 137 | |
| 138 | if (IS_ERR(resp)) { |
| 139 | rc = PTR_ERR(resp); |
| 140 | resp = NULL; |
| 141 | goto exit; |
| 142 | } |
| 143 | |
| 144 | rc = ddev->skb_check_crc(resp); |
| 145 | if (rc) { |
| 146 | PROTOCOL_ERR("14.4.1.6"); |
| 147 | goto exit; |
| 148 | } |
| 149 | |
| 150 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 151 | if (rc) { |
| 152 | PROTOCOL_ERR("14.4.1.2"); |
| 153 | goto exit; |
| 154 | } |
| 155 | |
| 156 | if (resp->len < sizeof(struct digital_atr_res)) { |
| 157 | rc = -EIO; |
| 158 | goto exit; |
| 159 | } |
| 160 | |
| 161 | gb_len = resp->len - sizeof(struct digital_atr_res); |
| 162 | |
| 163 | atr_res = (struct digital_atr_res *)resp->data; |
| 164 | |
| 165 | rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len); |
| 166 | if (rc) |
| 167 | goto exit; |
| 168 | |
| 169 | rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE, |
| 170 | NFC_RF_INITIATOR); |
| 171 | |
| 172 | ddev->curr_nfc_dep_pni = 0; |
| 173 | |
| 174 | exit: |
| 175 | dev_kfree_skb(resp); |
| 176 | |
| 177 | if (rc) |
| 178 | ddev->curr_protocol = 0; |
| 179 | } |
| 180 | |
| 181 | int digital_in_send_atr_req(struct nfc_digital_dev *ddev, |
| 182 | struct nfc_target *target, __u8 comm_mode, __u8 *gb, |
| 183 | size_t gb_len) |
| 184 | { |
| 185 | struct sk_buff *skb; |
| 186 | struct digital_atr_req *atr_req; |
| 187 | uint size; |
| 188 | |
| 189 | size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len; |
| 190 | |
| 191 | if (size > DIGITAL_ATR_REQ_MAX_SIZE) { |
| 192 | PROTOCOL_ERR("14.6.1.1"); |
| 193 | return -EINVAL; |
| 194 | } |
| 195 | |
| 196 | skb = digital_skb_alloc(ddev, size); |
| 197 | if (!skb) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | skb_put(skb, sizeof(struct digital_atr_req)); |
| 201 | |
| 202 | atr_req = (struct digital_atr_req *)skb->data; |
| 203 | memset(atr_req, 0, sizeof(struct digital_atr_req)); |
| 204 | |
| 205 | atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT; |
| 206 | atr_req->cmd = DIGITAL_CMD_ATR_REQ; |
| 207 | if (target->nfcid2_len) |
Thierry Escande | 4f319e3 | 2014-01-02 11:58:14 +0100 | [diff] [blame] | 208 | memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 209 | else |
Thierry Escande | 4f319e3 | 2014-01-02 11:58:14 +0100 | [diff] [blame] | 210 | get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 211 | |
| 212 | atr_req->did = 0; |
| 213 | atr_req->bs = 0; |
| 214 | atr_req->br = 0; |
| 215 | |
| 216 | atr_req->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B; |
| 217 | |
| 218 | if (gb_len) { |
| 219 | atr_req->pp |= DIGITAL_GB_BIT; |
| 220 | memcpy(skb_put(skb, gb_len), gb, gb_len); |
| 221 | } |
| 222 | |
| 223 | digital_skb_push_dep_sod(ddev, skb); |
| 224 | |
| 225 | ddev->skb_add_crc(skb); |
| 226 | |
Thierry Escande | 00e625d | 2014-04-12 00:03:08 +0200 | [diff] [blame] | 227 | return digital_in_send_cmd(ddev, skb, 500, digital_in_recv_atr_res, |
| 228 | target); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int digital_in_send_rtox(struct nfc_digital_dev *ddev, |
| 232 | struct digital_data_exch *data_exch, u8 rtox) |
| 233 | { |
| 234 | struct digital_dep_req_res *dep_req; |
| 235 | struct sk_buff *skb; |
| 236 | int rc; |
| 237 | |
| 238 | skb = digital_skb_alloc(ddev, 1); |
| 239 | if (!skb) |
| 240 | return -ENOMEM; |
| 241 | |
| 242 | *skb_put(skb, 1) = rtox; |
| 243 | |
| 244 | skb_push(skb, sizeof(struct digital_dep_req_res)); |
| 245 | |
| 246 | dep_req = (struct digital_dep_req_res *)skb->data; |
| 247 | |
| 248 | dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT; |
| 249 | dep_req->cmd = DIGITAL_CMD_DEP_REQ; |
| 250 | dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU | |
| 251 | DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT; |
| 252 | |
| 253 | digital_skb_push_dep_sod(ddev, skb); |
| 254 | |
| 255 | ddev->skb_add_crc(skb); |
| 256 | |
| 257 | rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res, |
| 258 | data_exch); |
| 259 | |
| 260 | return rc; |
| 261 | } |
| 262 | |
| 263 | static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg, |
| 264 | struct sk_buff *resp) |
| 265 | { |
| 266 | struct digital_data_exch *data_exch = arg; |
| 267 | struct digital_dep_req_res *dep_res; |
| 268 | u8 pfb; |
| 269 | uint size; |
| 270 | int rc; |
| 271 | |
| 272 | if (IS_ERR(resp)) { |
| 273 | rc = PTR_ERR(resp); |
| 274 | resp = NULL; |
| 275 | goto exit; |
| 276 | } |
| 277 | |
| 278 | rc = ddev->skb_check_crc(resp); |
| 279 | if (rc) { |
| 280 | PROTOCOL_ERR("14.4.1.6"); |
| 281 | goto error; |
| 282 | } |
| 283 | |
| 284 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 285 | if (rc) { |
| 286 | PROTOCOL_ERR("14.4.1.2"); |
| 287 | goto exit; |
| 288 | } |
| 289 | |
| 290 | dep_res = (struct digital_dep_req_res *)resp->data; |
| 291 | |
| 292 | if (resp->len < sizeof(struct digital_dep_req_res) || |
| 293 | dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN || |
| 294 | dep_res->cmd != DIGITAL_CMD_DEP_RES) { |
| 295 | rc = -EIO; |
| 296 | goto error; |
| 297 | } |
| 298 | |
| 299 | pfb = dep_res->pfb; |
| 300 | |
| 301 | switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) { |
| 302 | case DIGITAL_NFC_DEP_PFB_I_PDU: |
| 303 | if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) { |
| 304 | PROTOCOL_ERR("14.12.3.3"); |
| 305 | rc = -EIO; |
| 306 | goto error; |
| 307 | } |
| 308 | |
| 309 | ddev->curr_nfc_dep_pni = |
| 310 | DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1); |
| 311 | rc = 0; |
| 312 | break; |
| 313 | |
| 314 | case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU: |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 315 | pr_err("Received a ACK/NACK PDU\n"); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 316 | rc = -EIO; |
| 317 | goto error; |
| 318 | |
| 319 | case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU: |
| 320 | if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) { |
| 321 | rc = -EINVAL; |
| 322 | goto error; |
| 323 | } |
| 324 | |
| 325 | rc = digital_in_send_rtox(ddev, data_exch, resp->data[3]); |
| 326 | if (rc) |
| 327 | goto error; |
| 328 | |
| 329 | kfree_skb(resp); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 334 | pr_err("MI bit set. Chained PDU not supported\n"); |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 335 | rc = -EIO; |
| 336 | goto error; |
| 337 | } |
| 338 | |
| 339 | size = sizeof(struct digital_dep_req_res); |
| 340 | |
| 341 | if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) |
| 342 | size++; |
| 343 | |
| 344 | if (size > resp->len) { |
| 345 | rc = -EIO; |
| 346 | goto error; |
| 347 | } |
| 348 | |
| 349 | skb_pull(resp, size); |
| 350 | |
| 351 | exit: |
| 352 | data_exch->cb(data_exch->cb_context, resp, rc); |
| 353 | |
| 354 | error: |
| 355 | kfree(data_exch); |
| 356 | |
| 357 | if (rc) |
| 358 | kfree_skb(resp); |
| 359 | } |
| 360 | |
| 361 | int digital_in_send_dep_req(struct nfc_digital_dev *ddev, |
| 362 | struct nfc_target *target, struct sk_buff *skb, |
| 363 | struct digital_data_exch *data_exch) |
| 364 | { |
| 365 | struct digital_dep_req_res *dep_req; |
| 366 | |
| 367 | skb_push(skb, sizeof(struct digital_dep_req_res)); |
| 368 | |
| 369 | dep_req = (struct digital_dep_req_res *)skb->data; |
| 370 | dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT; |
| 371 | dep_req->cmd = DIGITAL_CMD_DEP_REQ; |
| 372 | dep_req->pfb = ddev->curr_nfc_dep_pni; |
| 373 | |
| 374 | digital_skb_push_dep_sod(ddev, skb); |
| 375 | |
| 376 | ddev->skb_add_crc(skb); |
| 377 | |
| 378 | return digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res, |
| 379 | data_exch); |
| 380 | } |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 381 | |
Thierry Escande | b711ad5 | 2014-01-06 23:34:48 +0100 | [diff] [blame] | 382 | static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech) |
| 383 | { |
| 384 | ddev->curr_rf_tech = rf_tech; |
| 385 | |
| 386 | ddev->skb_add_crc = digital_skb_add_crc_none; |
| 387 | ddev->skb_check_crc = digital_skb_check_crc_none; |
| 388 | |
| 389 | if (DIGITAL_DRV_CAPS_TG_CRC(ddev)) |
| 390 | return; |
| 391 | |
| 392 | switch (ddev->curr_rf_tech) { |
| 393 | case NFC_DIGITAL_RF_TECH_106A: |
| 394 | ddev->skb_add_crc = digital_skb_add_crc_a; |
| 395 | ddev->skb_check_crc = digital_skb_check_crc_a; |
| 396 | break; |
| 397 | |
| 398 | case NFC_DIGITAL_RF_TECH_212F: |
| 399 | case NFC_DIGITAL_RF_TECH_424F: |
| 400 | ddev->skb_add_crc = digital_skb_add_crc_f; |
| 401 | ddev->skb_check_crc = digital_skb_check_crc_f; |
| 402 | break; |
| 403 | |
| 404 | default: |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 409 | static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg, |
| 410 | struct sk_buff *resp) |
| 411 | { |
| 412 | int rc; |
| 413 | struct digital_dep_req_res *dep_req; |
| 414 | size_t size; |
| 415 | |
| 416 | if (IS_ERR(resp)) { |
| 417 | rc = PTR_ERR(resp); |
| 418 | resp = NULL; |
| 419 | goto exit; |
| 420 | } |
| 421 | |
| 422 | rc = ddev->skb_check_crc(resp); |
| 423 | if (rc) { |
| 424 | PROTOCOL_ERR("14.4.1.6"); |
| 425 | goto exit; |
| 426 | } |
| 427 | |
| 428 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 429 | if (rc) { |
| 430 | PROTOCOL_ERR("14.4.1.2"); |
| 431 | goto exit; |
| 432 | } |
| 433 | |
| 434 | size = sizeof(struct digital_dep_req_res); |
| 435 | dep_req = (struct digital_dep_req_res *)resp->data; |
| 436 | |
| 437 | if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT || |
| 438 | dep_req->cmd != DIGITAL_CMD_DEP_REQ) { |
| 439 | rc = -EIO; |
| 440 | goto exit; |
| 441 | } |
| 442 | |
| 443 | if (DIGITAL_NFC_DEP_DID_BIT_SET(dep_req->pfb)) |
| 444 | size++; |
| 445 | |
| 446 | if (resp->len < size) { |
| 447 | rc = -EIO; |
| 448 | goto exit; |
| 449 | } |
| 450 | |
| 451 | switch (DIGITAL_NFC_DEP_PFB_TYPE(dep_req->pfb)) { |
| 452 | case DIGITAL_NFC_DEP_PFB_I_PDU: |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 453 | pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n"); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 454 | ddev->curr_nfc_dep_pni = DIGITAL_NFC_DEP_PFB_PNI(dep_req->pfb); |
| 455 | break; |
| 456 | case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU: |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 457 | pr_err("Received a ACK/NACK PDU\n"); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 458 | rc = -EINVAL; |
| 459 | goto exit; |
| 460 | break; |
| 461 | case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU: |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 462 | pr_err("Received a SUPERVISOR PDU\n"); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 463 | rc = -EINVAL; |
| 464 | goto exit; |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | skb_pull(resp, size); |
| 469 | |
| 470 | rc = nfc_tm_data_received(ddev->nfc_dev, resp); |
| 471 | |
| 472 | exit: |
| 473 | if (rc) |
| 474 | kfree_skb(resp); |
| 475 | } |
| 476 | |
| 477 | int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb) |
| 478 | { |
| 479 | struct digital_dep_req_res *dep_res; |
| 480 | |
| 481 | skb_push(skb, sizeof(struct digital_dep_req_res)); |
| 482 | dep_res = (struct digital_dep_req_res *)skb->data; |
| 483 | |
| 484 | dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN; |
| 485 | dep_res->cmd = DIGITAL_CMD_DEP_RES; |
| 486 | dep_res->pfb = ddev->curr_nfc_dep_pni; |
| 487 | |
| 488 | digital_skb_push_dep_sod(ddev, skb); |
| 489 | |
| 490 | ddev->skb_add_crc(skb); |
| 491 | |
| 492 | return digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req, |
| 493 | NULL); |
| 494 | } |
| 495 | |
| 496 | static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev, |
| 497 | void *arg, struct sk_buff *resp) |
| 498 | { |
Thierry Escande | 67af1d7 | 2014-01-02 11:58:13 +0100 | [diff] [blame] | 499 | u8 rf_tech = (unsigned long)arg; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 500 | |
| 501 | if (IS_ERR(resp)) |
| 502 | return; |
| 503 | |
Thierry Escande | b711ad5 | 2014-01-06 23:34:48 +0100 | [diff] [blame] | 504 | digital_tg_set_rf_tech(ddev, rf_tech); |
| 505 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 506 | digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech); |
| 507 | |
| 508 | digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL); |
| 509 | |
| 510 | dev_kfree_skb(resp); |
| 511 | } |
| 512 | |
| 513 | static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did, |
| 514 | u8 rf_tech) |
| 515 | { |
| 516 | struct digital_psl_res *psl_res; |
| 517 | struct sk_buff *skb; |
| 518 | int rc; |
| 519 | |
| 520 | skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res)); |
| 521 | if (!skb) |
| 522 | return -ENOMEM; |
| 523 | |
| 524 | skb_put(skb, sizeof(struct digital_psl_res)); |
| 525 | |
| 526 | psl_res = (struct digital_psl_res *)skb->data; |
| 527 | |
| 528 | psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN; |
| 529 | psl_res->cmd = DIGITAL_CMD_PSL_RES; |
| 530 | psl_res->did = did; |
| 531 | |
| 532 | digital_skb_push_dep_sod(ddev, skb); |
| 533 | |
| 534 | ddev->skb_add_crc(skb); |
| 535 | |
| 536 | 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] | 537 | (void *)(unsigned long)rf_tech); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 538 | |
| 539 | if (rc) |
| 540 | kfree_skb(skb); |
| 541 | |
| 542 | return rc; |
| 543 | } |
| 544 | |
| 545 | static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg, |
| 546 | struct sk_buff *resp) |
| 547 | { |
| 548 | int rc; |
| 549 | struct digital_psl_req *psl_req; |
| 550 | u8 rf_tech; |
| 551 | u8 dsi; |
| 552 | |
| 553 | if (IS_ERR(resp)) { |
| 554 | rc = PTR_ERR(resp); |
| 555 | resp = NULL; |
| 556 | goto exit; |
| 557 | } |
| 558 | |
| 559 | rc = ddev->skb_check_crc(resp); |
| 560 | if (rc) { |
| 561 | PROTOCOL_ERR("14.4.1.6"); |
| 562 | goto exit; |
| 563 | } |
| 564 | |
| 565 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 566 | if (rc) { |
| 567 | PROTOCOL_ERR("14.4.1.2"); |
| 568 | goto exit; |
| 569 | } |
| 570 | |
| 571 | psl_req = (struct digital_psl_req *)resp->data; |
| 572 | |
| 573 | if (resp->len != sizeof(struct digital_psl_req) || |
| 574 | psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT || |
| 575 | psl_req->cmd != DIGITAL_CMD_PSL_REQ) { |
| 576 | rc = -EIO; |
| 577 | goto exit; |
| 578 | } |
| 579 | |
| 580 | dsi = (psl_req->brs >> 3) & 0x07; |
| 581 | switch (dsi) { |
| 582 | case 0: |
| 583 | rf_tech = NFC_DIGITAL_RF_TECH_106A; |
| 584 | break; |
| 585 | case 1: |
| 586 | rf_tech = NFC_DIGITAL_RF_TECH_212F; |
| 587 | break; |
| 588 | case 2: |
| 589 | rf_tech = NFC_DIGITAL_RF_TECH_424F; |
| 590 | break; |
| 591 | default: |
Masanari Iida | 77d84ff | 2013-12-09 00:22:53 +0900 | [diff] [blame] | 592 | pr_err("Unsupported dsi value %d\n", dsi); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 593 | goto exit; |
| 594 | } |
| 595 | |
| 596 | rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech); |
| 597 | |
| 598 | exit: |
| 599 | kfree_skb(resp); |
| 600 | } |
| 601 | |
| 602 | static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev, |
| 603 | void *arg, struct sk_buff *resp) |
| 604 | { |
| 605 | int offset; |
| 606 | |
| 607 | if (IS_ERR(resp)) { |
| 608 | digital_poll_next_tech(ddev); |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | offset = 2; |
| 613 | if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) |
| 614 | offset++; |
| 615 | |
| 616 | if (resp->data[offset] == DIGITAL_CMD_PSL_REQ) |
| 617 | digital_tg_recv_psl_req(ddev, arg, resp); |
| 618 | else |
| 619 | digital_tg_recv_dep_req(ddev, arg, resp); |
| 620 | } |
| 621 | |
| 622 | static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev, |
| 623 | struct digital_atr_req *atr_req) |
| 624 | { |
| 625 | struct digital_atr_res *atr_res; |
| 626 | struct sk_buff *skb; |
| 627 | u8 *gb; |
| 628 | size_t gb_len; |
| 629 | int rc; |
| 630 | |
| 631 | gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len); |
| 632 | if (!gb) |
| 633 | gb_len = 0; |
| 634 | |
| 635 | skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len); |
| 636 | if (!skb) |
| 637 | return -ENOMEM; |
| 638 | |
| 639 | skb_put(skb, sizeof(struct digital_atr_res)); |
| 640 | atr_res = (struct digital_atr_res *)skb->data; |
| 641 | |
| 642 | memset(atr_res, 0, sizeof(struct digital_atr_res)); |
| 643 | |
| 644 | atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN; |
| 645 | atr_res->cmd = DIGITAL_CMD_ATR_RES; |
| 646 | memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3)); |
| 647 | atr_res->to = 8; |
| 648 | atr_res->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B; |
| 649 | if (gb_len) { |
| 650 | skb_put(skb, gb_len); |
| 651 | |
| 652 | atr_res->pp |= DIGITAL_GB_BIT; |
| 653 | memcpy(atr_res->gb, gb, gb_len); |
| 654 | } |
| 655 | |
| 656 | digital_skb_push_dep_sod(ddev, skb); |
| 657 | |
| 658 | ddev->skb_add_crc(skb); |
| 659 | |
| 660 | rc = digital_tg_send_cmd(ddev, skb, 999, |
| 661 | digital_tg_send_atr_res_complete, NULL); |
| 662 | if (rc) { |
| 663 | kfree_skb(skb); |
| 664 | return rc; |
| 665 | } |
| 666 | |
| 667 | return rc; |
| 668 | } |
| 669 | |
| 670 | void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg, |
| 671 | struct sk_buff *resp) |
| 672 | { |
| 673 | int rc; |
| 674 | struct digital_atr_req *atr_req; |
| 675 | size_t gb_len, min_size; |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame^] | 676 | u8 poll_tech_count; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 677 | |
| 678 | if (IS_ERR(resp)) { |
| 679 | rc = PTR_ERR(resp); |
| 680 | resp = NULL; |
| 681 | goto exit; |
| 682 | } |
| 683 | |
| 684 | if (!resp->len) { |
| 685 | rc = -EIO; |
| 686 | goto exit; |
| 687 | } |
| 688 | |
| 689 | if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) { |
| 690 | min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2; |
Thierry Escande | b711ad5 | 2014-01-06 23:34:48 +0100 | [diff] [blame] | 691 | digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 692 | } else { |
| 693 | min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1; |
Thierry Escande | b711ad5 | 2014-01-06 23:34:48 +0100 | [diff] [blame] | 694 | digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | if (resp->len < min_size) { |
| 698 | rc = -EIO; |
| 699 | goto exit; |
| 700 | } |
| 701 | |
Thierry Escande | 48e1044 | 2014-01-06 23:34:37 +0100 | [diff] [blame] | 702 | ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 703 | |
| 704 | rc = ddev->skb_check_crc(resp); |
| 705 | if (rc) { |
| 706 | PROTOCOL_ERR("14.4.1.6"); |
| 707 | goto exit; |
| 708 | } |
| 709 | |
| 710 | rc = digital_skb_pull_dep_sod(ddev, resp); |
| 711 | if (rc) { |
| 712 | PROTOCOL_ERR("14.4.1.2"); |
| 713 | goto exit; |
| 714 | } |
| 715 | |
| 716 | atr_req = (struct digital_atr_req *)resp->data; |
| 717 | |
| 718 | if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT || |
| 719 | atr_req->cmd != DIGITAL_CMD_ATR_REQ) { |
| 720 | rc = -EINVAL; |
| 721 | goto exit; |
| 722 | } |
| 723 | |
| 724 | rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, |
| 725 | NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED); |
| 726 | if (rc) |
| 727 | goto exit; |
| 728 | |
| 729 | rc = digital_tg_send_atr_res(ddev, atr_req); |
| 730 | if (rc) |
| 731 | goto exit; |
| 732 | |
| 733 | gb_len = resp->len - sizeof(struct digital_atr_req); |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame^] | 734 | |
| 735 | poll_tech_count = ddev->poll_tech_count; |
| 736 | ddev->poll_tech_count = 0; |
| 737 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 738 | rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK, |
| 739 | NFC_COMM_PASSIVE, atr_req->gb, gb_len); |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame^] | 740 | if (rc) { |
| 741 | ddev->poll_tech_count = poll_tech_count; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 742 | goto exit; |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame^] | 743 | } |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 744 | |
| 745 | rc = 0; |
| 746 | exit: |
| 747 | if (rc) |
| 748 | digital_poll_next_tech(ddev); |
| 749 | |
| 750 | dev_kfree_skb(resp); |
| 751 | } |