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