1 /* 2 * The NFC Controller Interface is the communication protocol between an 3 * NFC Controller (NFCC) and a Device Host (DH). 4 * 5 * Copyright (C) 2011 Texas Instruments, Inc. 6 * 7 * Written by Ilan Elias <ilane@ti.com> 8 * 9 * Acknowledgements: 10 * This file is based on hci_core.c, which was written 11 * by Maxim Krasnyansky. 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License version 2 15 * as published by the Free Software Foundation 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, see <http://www.gnu.org/licenses/>. 24 * 25 */ 26 27 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ 28 29 #include <linux/module.h> 30 #include <linux/types.h> 31 #include <linux/workqueue.h> 32 #include <linux/completion.h> 33 #include <linux/export.h> 34 #include <linux/sched.h> 35 #include <linux/bitops.h> 36 #include <linux/skbuff.h> 37 38 #include "../nfc.h" 39 #include <net/nfc/nci.h> 40 #include <net/nfc/nci_core.h> 41 #include <linux/nfc.h> 42 43 static void nci_cmd_work(struct work_struct *work); 44 static void nci_rx_work(struct work_struct *work); 45 static void nci_tx_work(struct work_struct *work); 46 47 /* ---- NCI requests ---- */ 48 49 void nci_req_complete(struct nci_dev *ndev, int result) 50 { 51 if (ndev->req_status == NCI_REQ_PEND) { 52 ndev->req_result = result; 53 ndev->req_status = NCI_REQ_DONE; 54 complete(&ndev->req_completion); 55 } 56 } 57 58 static void nci_req_cancel(struct nci_dev *ndev, int err) 59 { 60 if (ndev->req_status == NCI_REQ_PEND) { 61 ndev->req_result = err; 62 ndev->req_status = NCI_REQ_CANCELED; 63 complete(&ndev->req_completion); 64 } 65 } 66 67 /* Execute request and wait for completion. */ 68 static int __nci_request(struct nci_dev *ndev, 69 void (*req)(struct nci_dev *ndev, unsigned long opt), 70 unsigned long opt, __u32 timeout) 71 { 72 int rc = 0; 73 long completion_rc; 74 75 ndev->req_status = NCI_REQ_PEND; 76 77 init_completion(&ndev->req_completion); 78 req(ndev, opt); 79 completion_rc = 80 wait_for_completion_interruptible_timeout(&ndev->req_completion, 81 timeout); 82 83 pr_debug("wait_for_completion return %ld\n", completion_rc); 84 85 if (completion_rc > 0) { 86 switch (ndev->req_status) { 87 case NCI_REQ_DONE: 88 rc = nci_to_errno(ndev->req_result); 89 break; 90 91 case NCI_REQ_CANCELED: 92 rc = -ndev->req_result; 93 break; 94 95 default: 96 rc = -ETIMEDOUT; 97 break; 98 } 99 } else { 100 pr_err("wait_for_completion_interruptible_timeout failed %ld\n", 101 completion_rc); 102 103 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc)); 104 } 105 106 ndev->req_status = ndev->req_result = 0; 107 108 return rc; 109 } 110 111 static inline int nci_request(struct nci_dev *ndev, 112 void (*req)(struct nci_dev *ndev, 113 unsigned long opt), 114 unsigned long opt, __u32 timeout) 115 { 116 int rc; 117 118 if (!test_bit(NCI_UP, &ndev->flags)) 119 return -ENETDOWN; 120 121 /* Serialize all requests */ 122 mutex_lock(&ndev->req_lock); 123 rc = __nci_request(ndev, req, opt, timeout); 124 mutex_unlock(&ndev->req_lock); 125 126 return rc; 127 } 128 129 static void nci_reset_req(struct nci_dev *ndev, unsigned long opt) 130 { 131 struct nci_core_reset_cmd cmd; 132 133 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG; 134 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd); 135 } 136 137 static void nci_init_req(struct nci_dev *ndev, unsigned long opt) 138 { 139 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL); 140 } 141 142 static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt) 143 { 144 struct nci_rf_disc_map_cmd cmd; 145 struct disc_map_config *cfg = cmd.mapping_configs; 146 __u8 *num = &cmd.num_mapping_configs; 147 int i; 148 149 /* set rf mapping configurations */ 150 *num = 0; 151 152 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */ 153 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) { 154 if (ndev->supported_rf_interfaces[i] == 155 NCI_RF_INTERFACE_ISO_DEP) { 156 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP; 157 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL | 158 NCI_DISC_MAP_MODE_LISTEN; 159 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP; 160 (*num)++; 161 } else if (ndev->supported_rf_interfaces[i] == 162 NCI_RF_INTERFACE_NFC_DEP) { 163 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP; 164 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL | 165 NCI_DISC_MAP_MODE_LISTEN; 166 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP; 167 (*num)++; 168 } 169 170 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS) 171 break; 172 } 173 174 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD, 175 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd); 176 } 177 178 struct nci_set_config_param { 179 __u8 id; 180 size_t len; 181 __u8 *val; 182 }; 183 184 static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt) 185 { 186 struct nci_set_config_param *param = (struct nci_set_config_param *)opt; 187 struct nci_core_set_config_cmd cmd; 188 189 BUG_ON(param->len > NCI_MAX_PARAM_LEN); 190 191 cmd.num_params = 1; 192 cmd.param.id = param->id; 193 cmd.param.len = param->len; 194 memcpy(cmd.param.val, param->val, param->len); 195 196 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd); 197 } 198 199 static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt) 200 { 201 struct nci_rf_disc_cmd cmd; 202 __u32 protocols = opt; 203 204 cmd.num_disc_configs = 0; 205 206 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && 207 (protocols & NFC_PROTO_JEWEL_MASK || 208 protocols & NFC_PROTO_MIFARE_MASK || 209 protocols & NFC_PROTO_ISO14443_MASK || 210 protocols & NFC_PROTO_NFC_DEP_MASK)) { 211 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = 212 NCI_NFC_A_PASSIVE_POLL_MODE; 213 cmd.disc_configs[cmd.num_disc_configs].frequency = 1; 214 cmd.num_disc_configs++; 215 } 216 217 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && 218 (protocols & NFC_PROTO_ISO14443_B_MASK)) { 219 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = 220 NCI_NFC_B_PASSIVE_POLL_MODE; 221 cmd.disc_configs[cmd.num_disc_configs].frequency = 1; 222 cmd.num_disc_configs++; 223 } 224 225 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && 226 (protocols & NFC_PROTO_FELICA_MASK || 227 protocols & NFC_PROTO_NFC_DEP_MASK)) { 228 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = 229 NCI_NFC_F_PASSIVE_POLL_MODE; 230 cmd.disc_configs[cmd.num_disc_configs].frequency = 1; 231 cmd.num_disc_configs++; 232 } 233 234 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD, 235 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))), 236 &cmd); 237 } 238 239 struct nci_rf_discover_select_param { 240 __u8 rf_discovery_id; 241 __u8 rf_protocol; 242 }; 243 244 static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt) 245 { 246 struct nci_rf_discover_select_param *param = 247 (struct nci_rf_discover_select_param *)opt; 248 struct nci_rf_discover_select_cmd cmd; 249 250 cmd.rf_discovery_id = param->rf_discovery_id; 251 cmd.rf_protocol = param->rf_protocol; 252 253 switch (cmd.rf_protocol) { 254 case NCI_RF_PROTOCOL_ISO_DEP: 255 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP; 256 break; 257 258 case NCI_RF_PROTOCOL_NFC_DEP: 259 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP; 260 break; 261 262 default: 263 cmd.rf_interface = NCI_RF_INTERFACE_FRAME; 264 break; 265 } 266 267 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD, 268 sizeof(struct nci_rf_discover_select_cmd), &cmd); 269 } 270 271 static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt) 272 { 273 struct nci_rf_deactivate_cmd cmd; 274 275 cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE; 276 277 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD, 278 sizeof(struct nci_rf_deactivate_cmd), &cmd); 279 } 280 281 static int nci_open_device(struct nci_dev *ndev) 282 { 283 int rc = 0; 284 285 mutex_lock(&ndev->req_lock); 286 287 if (test_bit(NCI_UP, &ndev->flags)) { 288 rc = -EALREADY; 289 goto done; 290 } 291 292 if (ndev->ops->open(ndev)) { 293 rc = -EIO; 294 goto done; 295 } 296 297 atomic_set(&ndev->cmd_cnt, 1); 298 299 set_bit(NCI_INIT, &ndev->flags); 300 301 rc = __nci_request(ndev, nci_reset_req, 0, 302 msecs_to_jiffies(NCI_RESET_TIMEOUT)); 303 304 if (!rc) { 305 rc = __nci_request(ndev, nci_init_req, 0, 306 msecs_to_jiffies(NCI_INIT_TIMEOUT)); 307 } 308 309 if (!rc) { 310 rc = __nci_request(ndev, nci_init_complete_req, 0, 311 msecs_to_jiffies(NCI_INIT_TIMEOUT)); 312 } 313 314 clear_bit(NCI_INIT, &ndev->flags); 315 316 if (!rc) { 317 set_bit(NCI_UP, &ndev->flags); 318 nci_clear_target_list(ndev); 319 atomic_set(&ndev->state, NCI_IDLE); 320 } else { 321 /* Init failed, cleanup */ 322 skb_queue_purge(&ndev->cmd_q); 323 skb_queue_purge(&ndev->rx_q); 324 skb_queue_purge(&ndev->tx_q); 325 326 ndev->ops->close(ndev); 327 ndev->flags = 0; 328 } 329 330 done: 331 mutex_unlock(&ndev->req_lock); 332 return rc; 333 } 334 335 static int nci_close_device(struct nci_dev *ndev) 336 { 337 nci_req_cancel(ndev, ENODEV); 338 mutex_lock(&ndev->req_lock); 339 340 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) { 341 del_timer_sync(&ndev->cmd_timer); 342 del_timer_sync(&ndev->data_timer); 343 mutex_unlock(&ndev->req_lock); 344 return 0; 345 } 346 347 /* Drop RX and TX queues */ 348 skb_queue_purge(&ndev->rx_q); 349 skb_queue_purge(&ndev->tx_q); 350 351 /* Flush RX and TX wq */ 352 flush_workqueue(ndev->rx_wq); 353 flush_workqueue(ndev->tx_wq); 354 355 /* Reset device */ 356 skb_queue_purge(&ndev->cmd_q); 357 atomic_set(&ndev->cmd_cnt, 1); 358 359 set_bit(NCI_INIT, &ndev->flags); 360 __nci_request(ndev, nci_reset_req, 0, 361 msecs_to_jiffies(NCI_RESET_TIMEOUT)); 362 clear_bit(NCI_INIT, &ndev->flags); 363 364 /* Flush cmd wq */ 365 flush_workqueue(ndev->cmd_wq); 366 367 /* After this point our queues are empty 368 * and no works are scheduled. */ 369 ndev->ops->close(ndev); 370 371 /* Clear flags */ 372 ndev->flags = 0; 373 374 mutex_unlock(&ndev->req_lock); 375 376 return 0; 377 } 378 379 /* NCI command timer function */ 380 static void nci_cmd_timer(unsigned long arg) 381 { 382 struct nci_dev *ndev = (void *) arg; 383 384 atomic_set(&ndev->cmd_cnt, 1); 385 queue_work(ndev->cmd_wq, &ndev->cmd_work); 386 } 387 388 /* NCI data exchange timer function */ 389 static void nci_data_timer(unsigned long arg) 390 { 391 struct nci_dev *ndev = (void *) arg; 392 393 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); 394 queue_work(ndev->rx_wq, &ndev->rx_work); 395 } 396 397 static int nci_dev_up(struct nfc_dev *nfc_dev) 398 { 399 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 400 401 return nci_open_device(ndev); 402 } 403 404 static int nci_dev_down(struct nfc_dev *nfc_dev) 405 { 406 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 407 408 return nci_close_device(ndev); 409 } 410 411 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev) 412 { 413 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 414 struct nci_set_config_param param; 415 __u8 local_gb[NFC_MAX_GT_LEN]; 416 int i; 417 418 param.val = nfc_get_local_general_bytes(nfc_dev, ¶m.len); 419 if ((param.val == NULL) || (param.len == 0)) 420 return 0; 421 422 if (param.len > NFC_MAX_GT_LEN) 423 return -EINVAL; 424 425 for (i = 0; i < param.len; i++) 426 local_gb[param.len-1-i] = param.val[i]; 427 428 param.id = NCI_PN_ATR_REQ_GEN_BYTES; 429 param.val = local_gb; 430 431 return nci_request(ndev, nci_set_config_req, (unsigned long)¶m, 432 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT)); 433 } 434 435 static int nci_start_poll(struct nfc_dev *nfc_dev, 436 __u32 im_protocols, __u32 tm_protocols) 437 { 438 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 439 int rc; 440 441 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) || 442 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) { 443 pr_err("unable to start poll, since poll is already active\n"); 444 return -EBUSY; 445 } 446 447 if (ndev->target_active_prot) { 448 pr_err("there is an active target\n"); 449 return -EBUSY; 450 } 451 452 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) || 453 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) { 454 pr_debug("target active or w4 select, implicitly deactivate\n"); 455 456 rc = nci_request(ndev, nci_rf_deactivate_req, 0, 457 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); 458 if (rc) 459 return -EBUSY; 460 } 461 462 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) { 463 rc = nci_set_local_general_bytes(nfc_dev); 464 if (rc) { 465 pr_err("failed to set local general bytes\n"); 466 return rc; 467 } 468 } 469 470 rc = nci_request(ndev, nci_rf_discover_req, im_protocols, 471 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT)); 472 473 if (!rc) 474 ndev->poll_prots = im_protocols; 475 476 return rc; 477 } 478 479 static void nci_stop_poll(struct nfc_dev *nfc_dev) 480 { 481 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 482 483 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) && 484 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) { 485 pr_err("unable to stop poll, since poll is not active\n"); 486 return; 487 } 488 489 nci_request(ndev, nci_rf_deactivate_req, 0, 490 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); 491 } 492 493 static int nci_activate_target(struct nfc_dev *nfc_dev, 494 struct nfc_target *target, __u32 protocol) 495 { 496 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 497 struct nci_rf_discover_select_param param; 498 struct nfc_target *nci_target = NULL; 499 int i; 500 int rc = 0; 501 502 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol); 503 504 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) && 505 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) { 506 pr_err("there is no available target to activate\n"); 507 return -EINVAL; 508 } 509 510 if (ndev->target_active_prot) { 511 pr_err("there is already an active target\n"); 512 return -EBUSY; 513 } 514 515 for (i = 0; i < ndev->n_targets; i++) { 516 if (ndev->targets[i].idx == target->idx) { 517 nci_target = &ndev->targets[i]; 518 break; 519 } 520 } 521 522 if (!nci_target) { 523 pr_err("unable to find the selected target\n"); 524 return -EINVAL; 525 } 526 527 if (!(nci_target->supported_protocols & (1 << protocol))) { 528 pr_err("target does not support the requested protocol 0x%x\n", 529 protocol); 530 return -EINVAL; 531 } 532 533 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) { 534 param.rf_discovery_id = nci_target->logical_idx; 535 536 if (protocol == NFC_PROTO_JEWEL) 537 param.rf_protocol = NCI_RF_PROTOCOL_T1T; 538 else if (protocol == NFC_PROTO_MIFARE) 539 param.rf_protocol = NCI_RF_PROTOCOL_T2T; 540 else if (protocol == NFC_PROTO_FELICA) 541 param.rf_protocol = NCI_RF_PROTOCOL_T3T; 542 else if (protocol == NFC_PROTO_ISO14443 || 543 protocol == NFC_PROTO_ISO14443_B) 544 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP; 545 else 546 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP; 547 548 rc = nci_request(ndev, nci_rf_discover_select_req, 549 (unsigned long)¶m, 550 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT)); 551 } 552 553 if (!rc) 554 ndev->target_active_prot = protocol; 555 556 return rc; 557 } 558 559 static void nci_deactivate_target(struct nfc_dev *nfc_dev, 560 struct nfc_target *target) 561 { 562 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 563 564 pr_debug("entry\n"); 565 566 if (!ndev->target_active_prot) { 567 pr_err("unable to deactivate target, no active target\n"); 568 return; 569 } 570 571 ndev->target_active_prot = 0; 572 573 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) { 574 nci_request(ndev, nci_rf_deactivate_req, 0, 575 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); 576 } 577 } 578 579 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target, 580 __u8 comm_mode, __u8 *gb, size_t gb_len) 581 { 582 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 583 int rc; 584 585 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode); 586 587 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP); 588 if (rc) 589 return rc; 590 591 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb, 592 ndev->remote_gb_len); 593 if (!rc) 594 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE, 595 NFC_RF_INITIATOR); 596 597 return rc; 598 } 599 600 static int nci_dep_link_down(struct nfc_dev *nfc_dev) 601 { 602 pr_debug("entry\n"); 603 604 nci_deactivate_target(nfc_dev, NULL); 605 606 return 0; 607 } 608 609 610 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target, 611 struct sk_buff *skb, 612 data_exchange_cb_t cb, void *cb_context) 613 { 614 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 615 int rc; 616 617 pr_debug("target_idx %d, len %d\n", target->idx, skb->len); 618 619 if (!ndev->target_active_prot) { 620 pr_err("unable to exchange data, no active target\n"); 621 return -EINVAL; 622 } 623 624 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags)) 625 return -EBUSY; 626 627 /* store cb and context to be used on receiving data */ 628 ndev->data_exchange_cb = cb; 629 ndev->data_exchange_cb_context = cb_context; 630 631 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb); 632 if (rc) 633 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags); 634 635 return rc; 636 } 637 638 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx) 639 { 640 return 0; 641 } 642 643 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx) 644 { 645 return 0; 646 } 647 648 static int nci_discover_se(struct nfc_dev *nfc_dev) 649 { 650 return 0; 651 } 652 653 static struct nfc_ops nci_nfc_ops = { 654 .dev_up = nci_dev_up, 655 .dev_down = nci_dev_down, 656 .start_poll = nci_start_poll, 657 .stop_poll = nci_stop_poll, 658 .dep_link_up = nci_dep_link_up, 659 .dep_link_down = nci_dep_link_down, 660 .activate_target = nci_activate_target, 661 .deactivate_target = nci_deactivate_target, 662 .im_transceive = nci_transceive, 663 .enable_se = nci_enable_se, 664 .disable_se = nci_disable_se, 665 .discover_se = nci_discover_se, 666 }; 667 668 /* ---- Interface to NCI drivers ---- */ 669 670 /** 671 * nci_allocate_device - allocate a new nci device 672 * 673 * @ops: device operations 674 * @supported_protocols: NFC protocols supported by the device 675 */ 676 struct nci_dev *nci_allocate_device(struct nci_ops *ops, 677 __u32 supported_protocols, 678 int tx_headroom, int tx_tailroom) 679 { 680 struct nci_dev *ndev; 681 682 pr_debug("supported_protocols 0x%x\n", supported_protocols); 683 684 if (!ops->open || !ops->close || !ops->send) 685 return NULL; 686 687 if (!supported_protocols) 688 return NULL; 689 690 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL); 691 if (!ndev) 692 return NULL; 693 694 ndev->ops = ops; 695 ndev->tx_headroom = tx_headroom; 696 ndev->tx_tailroom = tx_tailroom; 697 698 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops, 699 supported_protocols, 700 tx_headroom + NCI_DATA_HDR_SIZE, 701 tx_tailroom); 702 if (!ndev->nfc_dev) 703 goto free_exit; 704 705 nfc_set_drvdata(ndev->nfc_dev, ndev); 706 707 return ndev; 708 709 free_exit: 710 kfree(ndev); 711 return NULL; 712 } 713 EXPORT_SYMBOL(nci_allocate_device); 714 715 /** 716 * nci_free_device - deallocate nci device 717 * 718 * @ndev: The nci device to deallocate 719 */ 720 void nci_free_device(struct nci_dev *ndev) 721 { 722 nfc_free_device(ndev->nfc_dev); 723 kfree(ndev); 724 } 725 EXPORT_SYMBOL(nci_free_device); 726 727 /** 728 * nci_register_device - register a nci device in the nfc subsystem 729 * 730 * @dev: The nci device to register 731 */ 732 int nci_register_device(struct nci_dev *ndev) 733 { 734 int rc; 735 struct device *dev = &ndev->nfc_dev->dev; 736 char name[32]; 737 738 rc = nfc_register_device(ndev->nfc_dev); 739 if (rc) 740 goto exit; 741 742 ndev->flags = 0; 743 744 INIT_WORK(&ndev->cmd_work, nci_cmd_work); 745 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev)); 746 ndev->cmd_wq = create_singlethread_workqueue(name); 747 if (!ndev->cmd_wq) { 748 rc = -ENOMEM; 749 goto unreg_exit; 750 } 751 752 INIT_WORK(&ndev->rx_work, nci_rx_work); 753 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev)); 754 ndev->rx_wq = create_singlethread_workqueue(name); 755 if (!ndev->rx_wq) { 756 rc = -ENOMEM; 757 goto destroy_cmd_wq_exit; 758 } 759 760 INIT_WORK(&ndev->tx_work, nci_tx_work); 761 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev)); 762 ndev->tx_wq = create_singlethread_workqueue(name); 763 if (!ndev->tx_wq) { 764 rc = -ENOMEM; 765 goto destroy_rx_wq_exit; 766 } 767 768 skb_queue_head_init(&ndev->cmd_q); 769 skb_queue_head_init(&ndev->rx_q); 770 skb_queue_head_init(&ndev->tx_q); 771 772 setup_timer(&ndev->cmd_timer, nci_cmd_timer, 773 (unsigned long) ndev); 774 setup_timer(&ndev->data_timer, nci_data_timer, 775 (unsigned long) ndev); 776 777 mutex_init(&ndev->req_lock); 778 779 goto exit; 780 781 destroy_rx_wq_exit: 782 destroy_workqueue(ndev->rx_wq); 783 784 destroy_cmd_wq_exit: 785 destroy_workqueue(ndev->cmd_wq); 786 787 unreg_exit: 788 nfc_unregister_device(ndev->nfc_dev); 789 790 exit: 791 return rc; 792 } 793 EXPORT_SYMBOL(nci_register_device); 794 795 /** 796 * nci_unregister_device - unregister a nci device in the nfc subsystem 797 * 798 * @dev: The nci device to unregister 799 */ 800 void nci_unregister_device(struct nci_dev *ndev) 801 { 802 nci_close_device(ndev); 803 804 destroy_workqueue(ndev->cmd_wq); 805 destroy_workqueue(ndev->rx_wq); 806 destroy_workqueue(ndev->tx_wq); 807 808 nfc_unregister_device(ndev->nfc_dev); 809 } 810 EXPORT_SYMBOL(nci_unregister_device); 811 812 /** 813 * nci_recv_frame - receive frame from NCI drivers 814 * 815 * @ndev: The nci device 816 * @skb: The sk_buff to receive 817 */ 818 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb) 819 { 820 pr_debug("len %d\n", skb->len); 821 822 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) && 823 !test_bit(NCI_INIT, &ndev->flags))) { 824 kfree_skb(skb); 825 return -ENXIO; 826 } 827 828 /* Queue frame for rx worker thread */ 829 skb_queue_tail(&ndev->rx_q, skb); 830 queue_work(ndev->rx_wq, &ndev->rx_work); 831 832 return 0; 833 } 834 EXPORT_SYMBOL(nci_recv_frame); 835 836 static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb) 837 { 838 pr_debug("len %d\n", skb->len); 839 840 if (!ndev) { 841 kfree_skb(skb); 842 return -ENODEV; 843 } 844 845 /* Get rid of skb owner, prior to sending to the driver. */ 846 skb_orphan(skb); 847 848 return ndev->ops->send(ndev, skb); 849 } 850 851 /* Send NCI command */ 852 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload) 853 { 854 struct nci_ctrl_hdr *hdr; 855 struct sk_buff *skb; 856 857 pr_debug("opcode 0x%x, plen %d\n", opcode, plen); 858 859 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL); 860 if (!skb) { 861 pr_err("no memory for command\n"); 862 return -ENOMEM; 863 } 864 865 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE); 866 hdr->gid = nci_opcode_gid(opcode); 867 hdr->oid = nci_opcode_oid(opcode); 868 hdr->plen = plen; 869 870 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT); 871 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST); 872 873 if (plen) 874 memcpy(skb_put(skb, plen), payload, plen); 875 876 skb_queue_tail(&ndev->cmd_q, skb); 877 queue_work(ndev->cmd_wq, &ndev->cmd_work); 878 879 return 0; 880 } 881 882 /* ---- NCI TX Data worker thread ---- */ 883 884 static void nci_tx_work(struct work_struct *work) 885 { 886 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work); 887 struct sk_buff *skb; 888 889 pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt)); 890 891 /* Send queued tx data */ 892 while (atomic_read(&ndev->credits_cnt)) { 893 skb = skb_dequeue(&ndev->tx_q); 894 if (!skb) 895 return; 896 897 /* Check if data flow control is used */ 898 if (atomic_read(&ndev->credits_cnt) != 899 NCI_DATA_FLOW_CONTROL_NOT_USED) 900 atomic_dec(&ndev->credits_cnt); 901 902 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n", 903 nci_pbf(skb->data), 904 nci_conn_id(skb->data), 905 nci_plen(skb->data)); 906 907 nci_send_frame(ndev, skb); 908 909 mod_timer(&ndev->data_timer, 910 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT)); 911 } 912 } 913 914 /* ----- NCI RX worker thread (data & control) ----- */ 915 916 static void nci_rx_work(struct work_struct *work) 917 { 918 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work); 919 struct sk_buff *skb; 920 921 while ((skb = skb_dequeue(&ndev->rx_q))) { 922 /* Process frame */ 923 switch (nci_mt(skb->data)) { 924 case NCI_MT_RSP_PKT: 925 nci_rsp_packet(ndev, skb); 926 break; 927 928 case NCI_MT_NTF_PKT: 929 nci_ntf_packet(ndev, skb); 930 break; 931 932 case NCI_MT_DATA_PKT: 933 nci_rx_data_packet(ndev, skb); 934 break; 935 936 default: 937 pr_err("unknown MT 0x%x\n", nci_mt(skb->data)); 938 kfree_skb(skb); 939 break; 940 } 941 } 942 943 /* check if a data exchange timout has occurred */ 944 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) { 945 /* complete the data exchange transaction, if exists */ 946 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) 947 nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT); 948 949 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); 950 } 951 } 952 953 /* ----- NCI TX CMD worker thread ----- */ 954 955 static void nci_cmd_work(struct work_struct *work) 956 { 957 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work); 958 struct sk_buff *skb; 959 960 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt)); 961 962 /* Send queued command */ 963 if (atomic_read(&ndev->cmd_cnt)) { 964 skb = skb_dequeue(&ndev->cmd_q); 965 if (!skb) 966 return; 967 968 atomic_dec(&ndev->cmd_cnt); 969 970 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n", 971 nci_pbf(skb->data), 972 nci_opcode_gid(nci_opcode(skb->data)), 973 nci_opcode_oid(nci_opcode(skb->data)), 974 nci_plen(skb->data)); 975 976 nci_send_frame(ndev, skb); 977 978 mod_timer(&ndev->cmd_timer, 979 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT)); 980 } 981 } 982 983 MODULE_LICENSE("GPL"); 984