Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +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 | |
| 16 | #include "digital.h" |
| 17 | |
| 18 | #define DIGITAL_CMD_SENS_REQ 0x26 |
| 19 | #define DIGITAL_CMD_ALL_REQ 0x52 |
| 20 | #define DIGITAL_CMD_SEL_REQ_CL1 0x93 |
| 21 | #define DIGITAL_CMD_SEL_REQ_CL2 0x95 |
| 22 | #define DIGITAL_CMD_SEL_REQ_CL3 0x97 |
| 23 | |
| 24 | #define DIGITAL_SDD_REQ_SEL_PAR 0x20 |
| 25 | |
| 26 | #define DIGITAL_SDD_RES_CT 0x88 |
| 27 | #define DIGITAL_SDD_RES_LEN 5 |
| 28 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 29 | #define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04)) |
| 30 | #define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60)) |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame^] | 31 | #define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40) |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 32 | |
| 33 | #define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x000C) == 0x000C) |
| 34 | #define DIGITAL_SENS_RES_IS_VALID(sens_res) \ |
| 35 | ((!((sens_res) & 0x1F00) && (((sens_res) & 0x000C) == 0x000C)) || \ |
| 36 | (((sens_res) & 0x1F00) && ((sens_res) & 0x000C) != 0x000C)) |
| 37 | |
| 38 | #define DIGITAL_MIFARE_READ_RES_LEN 16 |
| 39 | #define DIGITAL_MIFARE_ACK_RES 0x0A |
| 40 | |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 41 | #define DIGITAL_CMD_SENSF_REQ 0x00 |
| 42 | #define DIGITAL_CMD_SENSF_RES 0x01 |
| 43 | |
| 44 | #define DIGITAL_SENSF_RES_MIN_LENGTH 17 |
| 45 | #define DIGITAL_SENSF_RES_RD_AP_B1 0x00 |
| 46 | #define DIGITAL_SENSF_RES_RD_AP_B2 0x8F |
| 47 | |
| 48 | #define DIGITAL_SENSF_REQ_RC_NONE 0 |
| 49 | #define DIGITAL_SENSF_REQ_RC_SC 1 |
| 50 | #define DIGITAL_SENSF_REQ_RC_AP 2 |
| 51 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 52 | struct digital_sdd_res { |
| 53 | u8 nfcid1[4]; |
| 54 | u8 bcc; |
| 55 | } __packed; |
| 56 | |
| 57 | struct digital_sel_req { |
| 58 | u8 sel_cmd; |
| 59 | u8 b2; |
| 60 | u8 nfcid1[4]; |
| 61 | u8 bcc; |
| 62 | } __packed; |
| 63 | |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 64 | struct digital_sensf_req { |
| 65 | u8 cmd; |
| 66 | u8 sc1; |
| 67 | u8 sc2; |
| 68 | u8 rc; |
| 69 | u8 tsn; |
| 70 | } __packed; |
| 71 | |
| 72 | struct digital_sensf_res { |
| 73 | u8 cmd; |
| 74 | u8 nfcid2[8]; |
| 75 | u8 pad0[2]; |
| 76 | u8 pad1[3]; |
| 77 | u8 mrti_check; |
| 78 | u8 mrti_update; |
| 79 | u8 pad2; |
| 80 | u8 rd[2]; |
| 81 | } __packed; |
| 82 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 83 | static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev, |
| 84 | struct nfc_target *target); |
| 85 | |
| 86 | static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg, |
| 87 | struct sk_buff *resp) |
| 88 | { |
| 89 | struct nfc_target *target = arg; |
| 90 | int rc; |
| 91 | u8 sel_res; |
| 92 | u8 nfc_proto; |
| 93 | |
| 94 | if (IS_ERR(resp)) { |
| 95 | rc = PTR_ERR(resp); |
| 96 | resp = NULL; |
| 97 | goto exit; |
| 98 | } |
| 99 | |
| 100 | if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) { |
| 101 | rc = digital_skb_check_crc_a(resp); |
| 102 | if (rc) { |
| 103 | PROTOCOL_ERR("4.4.1.3"); |
| 104 | goto exit; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (!resp->len) { |
| 109 | rc = -EIO; |
| 110 | goto exit; |
| 111 | } |
| 112 | |
| 113 | sel_res = resp->data[0]; |
| 114 | |
| 115 | if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) { |
| 116 | rc = digital_in_send_sdd_req(ddev, target); |
| 117 | if (rc) |
| 118 | goto exit; |
| 119 | |
| 120 | goto exit_free_skb; |
| 121 | } |
| 122 | |
| 123 | if (DIGITAL_SEL_RES_IS_T2T(sel_res)) { |
| 124 | nfc_proto = NFC_PROTO_MIFARE; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame^] | 125 | } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) { |
| 126 | nfc_proto = NFC_PROTO_NFC_DEP; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 127 | } else { |
| 128 | rc = -EOPNOTSUPP; |
| 129 | goto exit; |
| 130 | } |
| 131 | |
| 132 | target->sel_res = sel_res; |
| 133 | |
| 134 | rc = digital_target_found(ddev, target, nfc_proto); |
| 135 | |
| 136 | exit: |
| 137 | kfree(target); |
| 138 | |
| 139 | exit_free_skb: |
| 140 | dev_kfree_skb(resp); |
| 141 | |
| 142 | if (rc) |
| 143 | digital_poll_next_tech(ddev); |
| 144 | } |
| 145 | |
| 146 | static int digital_in_send_sel_req(struct nfc_digital_dev *ddev, |
| 147 | struct nfc_target *target, |
| 148 | struct digital_sdd_res *sdd_res) |
| 149 | { |
| 150 | struct sk_buff *skb; |
| 151 | struct digital_sel_req *sel_req; |
| 152 | u8 sel_cmd; |
| 153 | int rc; |
| 154 | |
| 155 | skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req)); |
| 156 | if (!skb) |
| 157 | return -ENOMEM; |
| 158 | |
| 159 | skb_put(skb, sizeof(struct digital_sel_req)); |
| 160 | sel_req = (struct digital_sel_req *)skb->data; |
| 161 | |
| 162 | if (target->nfcid1_len <= 4) |
| 163 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL1; |
| 164 | else if (target->nfcid1_len < 10) |
| 165 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL2; |
| 166 | else |
| 167 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL3; |
| 168 | |
| 169 | sel_req->sel_cmd = sel_cmd; |
| 170 | sel_req->b2 = 0x70; |
| 171 | memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4); |
| 172 | sel_req->bcc = sdd_res->bcc; |
| 173 | |
| 174 | if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) { |
| 175 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, |
| 176 | NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A); |
| 177 | if (rc) |
| 178 | goto exit; |
| 179 | } else { |
| 180 | digital_skb_add_crc_a(skb); |
| 181 | } |
| 182 | |
| 183 | rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res, |
| 184 | target); |
| 185 | exit: |
| 186 | if (rc) |
| 187 | kfree_skb(skb); |
| 188 | |
| 189 | return rc; |
| 190 | } |
| 191 | |
| 192 | static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg, |
| 193 | struct sk_buff *resp) |
| 194 | { |
| 195 | struct nfc_target *target = arg; |
| 196 | struct digital_sdd_res *sdd_res; |
| 197 | int rc; |
| 198 | u8 offset, size; |
| 199 | u8 i, bcc; |
| 200 | |
| 201 | if (IS_ERR(resp)) { |
| 202 | rc = PTR_ERR(resp); |
| 203 | resp = NULL; |
| 204 | goto exit; |
| 205 | } |
| 206 | |
| 207 | if (resp->len < DIGITAL_SDD_RES_LEN) { |
| 208 | PROTOCOL_ERR("4.7.2.8"); |
| 209 | rc = -EINVAL; |
| 210 | goto exit; |
| 211 | } |
| 212 | |
| 213 | sdd_res = (struct digital_sdd_res *)resp->data; |
| 214 | |
| 215 | for (i = 0, bcc = 0; i < 4; i++) |
| 216 | bcc ^= sdd_res->nfcid1[i]; |
| 217 | |
| 218 | if (bcc != sdd_res->bcc) { |
| 219 | PROTOCOL_ERR("4.7.2.6"); |
| 220 | rc = -EINVAL; |
| 221 | goto exit; |
| 222 | } |
| 223 | |
| 224 | if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) { |
| 225 | offset = 1; |
| 226 | size = 3; |
| 227 | } else { |
| 228 | offset = 0; |
| 229 | size = 4; |
| 230 | } |
| 231 | |
| 232 | memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset, |
| 233 | size); |
| 234 | target->nfcid1_len += size; |
| 235 | |
| 236 | rc = digital_in_send_sel_req(ddev, target, sdd_res); |
| 237 | |
| 238 | exit: |
| 239 | dev_kfree_skb(resp); |
| 240 | |
| 241 | if (rc) { |
| 242 | kfree(target); |
| 243 | digital_poll_next_tech(ddev); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev, |
| 248 | struct nfc_target *target) |
| 249 | { |
| 250 | int rc; |
| 251 | struct sk_buff *skb; |
| 252 | u8 sel_cmd; |
| 253 | |
| 254 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, |
| 255 | NFC_DIGITAL_FRAMING_NFCA_STANDARD); |
| 256 | if (rc) |
| 257 | return rc; |
| 258 | |
| 259 | skb = digital_skb_alloc(ddev, 2); |
| 260 | if (!skb) { |
| 261 | PR_ERR("alloc_skb failed"); |
| 262 | return -ENOMEM; |
| 263 | } |
| 264 | |
| 265 | if (target->nfcid1_len == 0) |
| 266 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL1; |
| 267 | else if (target->nfcid1_len == 3) |
| 268 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL2; |
| 269 | else |
| 270 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL3; |
| 271 | |
| 272 | *skb_put(skb, sizeof(u8)) = sel_cmd; |
| 273 | *skb_put(skb, sizeof(u8)) = DIGITAL_SDD_REQ_SEL_PAR; |
| 274 | |
| 275 | return digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res, |
| 276 | target); |
| 277 | } |
| 278 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 279 | static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg, |
| 280 | struct sk_buff *resp) |
| 281 | { |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 282 | struct nfc_target *target = NULL; |
| 283 | u16 sens_res; |
| 284 | int rc; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 285 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 286 | if (IS_ERR(resp)) { |
| 287 | rc = PTR_ERR(resp); |
| 288 | resp = NULL; |
| 289 | goto exit; |
| 290 | } |
| 291 | |
| 292 | if (resp->len < sizeof(u16)) { |
| 293 | rc = -EIO; |
| 294 | goto exit; |
| 295 | } |
| 296 | |
| 297 | target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL); |
| 298 | if (!target) { |
| 299 | rc = -ENOMEM; |
| 300 | goto exit; |
| 301 | } |
| 302 | |
| 303 | memcpy(&target->sens_res, resp->data, sizeof(u16)); |
| 304 | |
| 305 | sens_res = be16_to_cpu(target->sens_res); |
| 306 | |
| 307 | if (!DIGITAL_SENS_RES_IS_VALID(sens_res)) { |
| 308 | PROTOCOL_ERR("4.6.3.3"); |
| 309 | rc = -EINVAL; |
| 310 | goto exit; |
| 311 | } |
| 312 | |
| 313 | if (DIGITAL_SENS_RES_IS_T1T(sens_res)) |
| 314 | rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL); |
| 315 | else |
| 316 | rc = digital_in_send_sdd_req(ddev, target); |
| 317 | |
| 318 | exit: |
| 319 | dev_kfree_skb(resp); |
| 320 | |
| 321 | if (rc) { |
| 322 | kfree(target); |
| 323 | digital_poll_next_tech(ddev); |
| 324 | } |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech) |
| 328 | { |
| 329 | struct sk_buff *skb; |
| 330 | int rc; |
| 331 | |
| 332 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, |
| 333 | NFC_DIGITAL_RF_TECH_106A); |
| 334 | if (rc) |
| 335 | return rc; |
| 336 | |
| 337 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, |
| 338 | NFC_DIGITAL_FRAMING_NFCA_SHORT); |
| 339 | if (rc) |
| 340 | return rc; |
| 341 | |
| 342 | skb = digital_skb_alloc(ddev, 1); |
| 343 | if (!skb) |
| 344 | return -ENOMEM; |
| 345 | |
| 346 | *skb_put(skb, sizeof(u8)) = DIGITAL_CMD_SENS_REQ; |
| 347 | |
| 348 | rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL); |
| 349 | if (rc) |
| 350 | kfree_skb(skb); |
| 351 | |
| 352 | return rc; |
| 353 | } |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 354 | |
| 355 | int digital_in_recv_mifare_res(struct sk_buff *resp) |
| 356 | { |
| 357 | /* Successful READ command response is 16 data bytes + 2 CRC bytes long. |
| 358 | * Since the driver can't differentiate a ACK/NACK response from a valid |
| 359 | * READ response, the CRC calculation must be handled at digital level |
| 360 | * even if the driver supports it for this technology. |
| 361 | */ |
| 362 | if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) { |
| 363 | if (digital_skb_check_crc_a(resp)) { |
| 364 | PROTOCOL_ERR("9.4.1.2"); |
| 365 | return -EIO; |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /* ACK response (i.e. successful WRITE). */ |
| 372 | if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) { |
| 373 | resp->data[0] = 0; |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /* NACK and any other responses are treated as error. */ |
| 378 | return -EIO; |
| 379 | } |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 380 | |
| 381 | static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg, |
| 382 | struct sk_buff *resp) |
| 383 | { |
| 384 | int rc; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame^] | 385 | u8 proto; |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 386 | struct nfc_target target; |
| 387 | struct digital_sensf_res *sensf_res; |
| 388 | |
| 389 | if (IS_ERR(resp)) { |
| 390 | rc = PTR_ERR(resp); |
| 391 | resp = NULL; |
| 392 | goto exit; |
| 393 | } |
| 394 | |
| 395 | if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) { |
| 396 | rc = -EIO; |
| 397 | goto exit; |
| 398 | } |
| 399 | |
| 400 | if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) { |
| 401 | rc = digital_skb_check_crc_f(resp); |
| 402 | if (rc) { |
| 403 | PROTOCOL_ERR("6.4.1.8"); |
| 404 | goto exit; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | skb_pull(resp, 1); |
| 409 | |
| 410 | memset(&target, 0, sizeof(struct nfc_target)); |
| 411 | |
| 412 | sensf_res = (struct digital_sensf_res *)resp->data; |
| 413 | |
| 414 | memcpy(target.sensf_res, sensf_res, resp->len); |
| 415 | target.sensf_res_len = resp->len; |
| 416 | |
| 417 | memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE); |
| 418 | target.nfcid2_len = NFC_NFCID2_MAXSIZE; |
| 419 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame^] | 420 | if (target.nfcid2[0] == DIGITAL_SENSF_NFCID2_NFC_DEP_B1 && |
| 421 | target.nfcid2[1] == DIGITAL_SENSF_NFCID2_NFC_DEP_B2) |
| 422 | proto = NFC_PROTO_NFC_DEP; |
| 423 | else |
| 424 | proto = NFC_PROTO_FELICA; |
| 425 | |
| 426 | rc = digital_target_found(ddev, &target, proto); |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 427 | |
| 428 | exit: |
| 429 | dev_kfree_skb(resp); |
| 430 | |
| 431 | if (rc) |
| 432 | digital_poll_next_tech(ddev); |
| 433 | } |
| 434 | |
| 435 | int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech) |
| 436 | { |
| 437 | struct digital_sensf_req *sensf_req; |
| 438 | struct sk_buff *skb; |
| 439 | int rc; |
| 440 | u8 size; |
| 441 | |
| 442 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech); |
| 443 | if (rc) |
| 444 | return rc; |
| 445 | |
| 446 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, |
| 447 | NFC_DIGITAL_FRAMING_NFCF); |
| 448 | if (rc) |
| 449 | return rc; |
| 450 | |
| 451 | size = sizeof(struct digital_sensf_req); |
| 452 | |
| 453 | skb = digital_skb_alloc(ddev, size); |
| 454 | if (!skb) |
| 455 | return -ENOMEM; |
| 456 | |
| 457 | skb_put(skb, size); |
| 458 | |
| 459 | sensf_req = (struct digital_sensf_req *)skb->data; |
| 460 | sensf_req->cmd = DIGITAL_CMD_SENSF_REQ; |
| 461 | sensf_req->sc1 = 0xFF; |
| 462 | sensf_req->sc2 = 0xFF; |
| 463 | sensf_req->rc = 0; |
| 464 | sensf_req->tsn = 0; |
| 465 | |
| 466 | *skb_push(skb, 1) = size + 1; |
| 467 | |
| 468 | if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) |
| 469 | digital_skb_add_crc_f(skb); |
| 470 | |
| 471 | rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res, |
| 472 | NULL); |
| 473 | if (rc) |
| 474 | kfree_skb(skb); |
| 475 | |
| 476 | return rc; |
| 477 | } |