1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * The NFC Controller Interface is the communication protocol between an 4 * NFC Controller (NFCC) and a Device Host (DH). 5 * 6 * Copyright (C) 2011 Texas Instruments, Inc. 7 * Copyright (C) 2014 Marvell International Ltd. 8 * 9 * Written by Ilan Elias <ilane@ti.com> 10 * 11 * Acknowledgements: 12 * This file is based on hci_core.c, which was written 13 * by Maxim Krasnyansky. 14 */ 15 16 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ 17 18 #include <linux/module.h> 19 #include <linux/kernel.h> 20 #include <linux/types.h> 21 #include <linux/workqueue.h> 22 #include <linux/completion.h> 23 #include <linux/export.h> 24 #include <linux/sched.h> 25 #include <linux/bitops.h> 26 #include <linux/skbuff.h> 27 #include <linux/kcov.h> 28 29 #include "../nfc.h" 30 #include <net/nfc/nci.h> 31 #include <net/nfc/nci_core.h> 32 #include <linux/nfc.h> 33 34 struct core_conn_create_data { 35 int length; 36 struct nci_core_conn_create_cmd *cmd; 37 }; 38 39 static void nci_cmd_work(struct work_struct *work); 40 static void nci_rx_work(struct work_struct *work); 41 static void nci_tx_work(struct work_struct *work); 42 43 struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev, 44 int conn_id) 45 { 46 struct nci_conn_info *conn_info; 47 48 list_for_each_entry(conn_info, &ndev->conn_info_list, list) { 49 if (conn_info->conn_id == conn_id) 50 return conn_info; 51 } 52 53 return NULL; 54 } 55 56 int nci_get_conn_info_by_dest_type_params(struct nci_dev *ndev, u8 dest_type, 57 const struct dest_spec_params *params) 58 { 59 const struct nci_conn_info *conn_info; 60 61 list_for_each_entry(conn_info, &ndev->conn_info_list, list) { 62 if (conn_info->dest_type == dest_type) { 63 if (!params) 64 return conn_info->conn_id; 65 66 if (params->id == conn_info->dest_params->id && 67 params->protocol == conn_info->dest_params->protocol) 68 return conn_info->conn_id; 69 } 70 } 71 72 return -EINVAL; 73 } 74 EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params); 75 76 /* ---- NCI requests ---- */ 77 78 void nci_req_complete(struct nci_dev *ndev, int result) 79 { 80 if (ndev->req_status == NCI_REQ_PEND) { 81 ndev->req_result = result; 82 ndev->req_status = NCI_REQ_DONE; 83 complete(&ndev->req_completion); 84 } 85 } 86 EXPORT_SYMBOL(nci_req_complete); 87 88 static void nci_req_cancel(struct nci_dev *ndev, int err) 89 { 90 if (ndev->req_status == NCI_REQ_PEND) { 91 ndev->req_result = err; 92 ndev->req_status = NCI_REQ_CANCELED; 93 complete(&ndev->req_completion); 94 } 95 } 96 97 /* Execute request and wait for completion. */ 98 static int __nci_request(struct nci_dev *ndev, 99 void (*req)(struct nci_dev *ndev, const void *opt), 100 const void *opt, __u32 timeout) 101 { 102 int rc = 0; 103 long completion_rc; 104 105 ndev->req_status = NCI_REQ_PEND; 106 107 reinit_completion(&ndev->req_completion); 108 req(ndev, opt); 109 completion_rc = 110 wait_for_completion_interruptible_timeout(&ndev->req_completion, 111 timeout); 112 113 pr_debug("wait_for_completion return %ld\n", completion_rc); 114 115 if (completion_rc > 0) { 116 switch (ndev->req_status) { 117 case NCI_REQ_DONE: 118 rc = nci_to_errno(ndev->req_result); 119 break; 120 121 case NCI_REQ_CANCELED: 122 rc = -ndev->req_result; 123 break; 124 125 default: 126 rc = -ETIMEDOUT; 127 break; 128 } 129 } else { 130 pr_err("wait_for_completion_interruptible_timeout failed %ld\n", 131 completion_rc); 132 133 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc)); 134 } 135 136 ndev->req_status = ndev->req_result = 0; 137 138 return rc; 139 } 140 141 inline int nci_request(struct nci_dev *ndev, 142 void (*req)(struct nci_dev *ndev, 143 const void *opt), 144 const void *opt, __u32 timeout) 145 { 146 int rc; 147 148 /* Serialize all requests */ 149 mutex_lock(&ndev->req_lock); 150 /* check the state after obtaing the lock against any races 151 * from nci_close_device when the device gets removed. 152 */ 153 if (test_bit(NCI_UP, &ndev->flags)) 154 rc = __nci_request(ndev, req, opt, timeout); 155 else 156 rc = -ENETDOWN; 157 mutex_unlock(&ndev->req_lock); 158 159 return rc; 160 } 161 162 static void nci_reset_req(struct nci_dev *ndev, const void *opt) 163 { 164 struct nci_core_reset_cmd cmd; 165 166 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG; 167 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd); 168 } 169 170 static void nci_init_req(struct nci_dev *ndev, const void *opt) 171 { 172 u8 plen = 0; 173 174 if (opt) 175 plen = sizeof(struct nci_core_init_v2_cmd); 176 177 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, plen, opt); 178 } 179 180 static void nci_init_complete_req(struct nci_dev *ndev, const void *opt) 181 { 182 struct nci_rf_disc_map_cmd cmd; 183 struct disc_map_config *cfg = cmd.mapping_configs; 184 __u8 *num = &cmd.num_mapping_configs; 185 int i; 186 187 /* set rf mapping configurations */ 188 *num = 0; 189 190 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */ 191 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) { 192 if (ndev->supported_rf_interfaces[i] == 193 NCI_RF_INTERFACE_ISO_DEP) { 194 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP; 195 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL | 196 NCI_DISC_MAP_MODE_LISTEN; 197 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP; 198 (*num)++; 199 } else if (ndev->supported_rf_interfaces[i] == 200 NCI_RF_INTERFACE_NFC_DEP) { 201 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP; 202 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL | 203 NCI_DISC_MAP_MODE_LISTEN; 204 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP; 205 (*num)++; 206 } 207 208 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS) 209 break; 210 } 211 212 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD, 213 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd); 214 } 215 216 struct nci_set_config_param { 217 __u8 id; 218 size_t len; 219 const __u8 *val; 220 }; 221 222 static void nci_set_config_req(struct nci_dev *ndev, const void *opt) 223 { 224 const struct nci_set_config_param *param = opt; 225 struct nci_core_set_config_cmd cmd; 226 227 BUG_ON(param->len > NCI_MAX_PARAM_LEN); 228 229 cmd.num_params = 1; 230 cmd.param.id = param->id; 231 cmd.param.len = param->len; 232 memcpy(cmd.param.val, param->val, param->len); 233 234 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd); 235 } 236 237 struct nci_rf_discover_param { 238 __u32 im_protocols; 239 __u32 tm_protocols; 240 }; 241 242 static void nci_rf_discover_req(struct nci_dev *ndev, const void *opt) 243 { 244 const struct nci_rf_discover_param *param = opt; 245 struct nci_rf_disc_cmd cmd; 246 247 cmd.num_disc_configs = 0; 248 249 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && 250 (param->im_protocols & NFC_PROTO_JEWEL_MASK || 251 param->im_protocols & NFC_PROTO_MIFARE_MASK || 252 param->im_protocols & NFC_PROTO_ISO14443_MASK || 253 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) { 254 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = 255 NCI_NFC_A_PASSIVE_POLL_MODE; 256 cmd.disc_configs[cmd.num_disc_configs].frequency = 1; 257 cmd.num_disc_configs++; 258 } 259 260 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && 261 (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) { 262 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = 263 NCI_NFC_B_PASSIVE_POLL_MODE; 264 cmd.disc_configs[cmd.num_disc_configs].frequency = 1; 265 cmd.num_disc_configs++; 266 } 267 268 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && 269 (param->im_protocols & NFC_PROTO_FELICA_MASK || 270 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) { 271 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = 272 NCI_NFC_F_PASSIVE_POLL_MODE; 273 cmd.disc_configs[cmd.num_disc_configs].frequency = 1; 274 cmd.num_disc_configs++; 275 } 276 277 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && 278 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) { 279 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = 280 NCI_NFC_V_PASSIVE_POLL_MODE; 281 cmd.disc_configs[cmd.num_disc_configs].frequency = 1; 282 cmd.num_disc_configs++; 283 } 284 285 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) && 286 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) { 287 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = 288 NCI_NFC_A_PASSIVE_LISTEN_MODE; 289 cmd.disc_configs[cmd.num_disc_configs].frequency = 1; 290 cmd.num_disc_configs++; 291 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = 292 NCI_NFC_F_PASSIVE_LISTEN_MODE; 293 cmd.disc_configs[cmd.num_disc_configs].frequency = 1; 294 cmd.num_disc_configs++; 295 } 296 297 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD, 298 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))), 299 &cmd); 300 } 301 302 struct nci_rf_discover_select_param { 303 __u8 rf_discovery_id; 304 __u8 rf_protocol; 305 }; 306 307 static void nci_rf_discover_select_req(struct nci_dev *ndev, const void *opt) 308 { 309 const struct nci_rf_discover_select_param *param = opt; 310 struct nci_rf_discover_select_cmd cmd; 311 312 cmd.rf_discovery_id = param->rf_discovery_id; 313 cmd.rf_protocol = param->rf_protocol; 314 315 switch (cmd.rf_protocol) { 316 case NCI_RF_PROTOCOL_ISO_DEP: 317 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP; 318 break; 319 320 case NCI_RF_PROTOCOL_NFC_DEP: 321 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP; 322 break; 323 324 default: 325 cmd.rf_interface = NCI_RF_INTERFACE_FRAME; 326 break; 327 } 328 329 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD, 330 sizeof(struct nci_rf_discover_select_cmd), &cmd); 331 } 332 333 static void nci_rf_deactivate_req(struct nci_dev *ndev, const void *opt) 334 { 335 struct nci_rf_deactivate_cmd cmd; 336 337 cmd.type = (unsigned long)opt; 338 339 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD, 340 sizeof(struct nci_rf_deactivate_cmd), &cmd); 341 } 342 343 struct nci_cmd_param { 344 __u16 opcode; 345 size_t len; 346 const __u8 *payload; 347 }; 348 349 static void nci_generic_req(struct nci_dev *ndev, const void *opt) 350 { 351 const struct nci_cmd_param *param = opt; 352 353 nci_send_cmd(ndev, param->opcode, param->len, param->payload); 354 } 355 356 int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, const __u8 *payload) 357 { 358 struct nci_cmd_param param; 359 360 param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid); 361 param.len = len; 362 param.payload = payload; 363 364 return __nci_request(ndev, nci_generic_req, ¶m, 365 msecs_to_jiffies(NCI_CMD_TIMEOUT)); 366 } 367 EXPORT_SYMBOL(nci_prop_cmd); 368 369 int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len, 370 const __u8 *payload) 371 { 372 struct nci_cmd_param param; 373 374 param.opcode = opcode; 375 param.len = len; 376 param.payload = payload; 377 378 return __nci_request(ndev, nci_generic_req, ¶m, 379 msecs_to_jiffies(NCI_CMD_TIMEOUT)); 380 } 381 EXPORT_SYMBOL(nci_core_cmd); 382 383 int nci_core_reset(struct nci_dev *ndev) 384 { 385 return __nci_request(ndev, nci_reset_req, (void *)0, 386 msecs_to_jiffies(NCI_RESET_TIMEOUT)); 387 } 388 EXPORT_SYMBOL(nci_core_reset); 389 390 int nci_core_init(struct nci_dev *ndev) 391 { 392 return __nci_request(ndev, nci_init_req, (void *)0, 393 msecs_to_jiffies(NCI_INIT_TIMEOUT)); 394 } 395 EXPORT_SYMBOL(nci_core_init); 396 397 struct nci_loopback_data { 398 u8 conn_id; 399 struct sk_buff *data; 400 }; 401 402 static void nci_send_data_req(struct nci_dev *ndev, const void *opt) 403 { 404 const struct nci_loopback_data *data = opt; 405 406 nci_send_data(ndev, data->conn_id, data->data); 407 } 408 409 static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err) 410 { 411 struct nci_dev *ndev = (struct nci_dev *)context; 412 struct nci_conn_info *conn_info; 413 414 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id); 415 if (!conn_info) { 416 nci_req_complete(ndev, NCI_STATUS_REJECTED); 417 return; 418 } 419 420 conn_info->rx_skb = skb; 421 422 nci_req_complete(ndev, NCI_STATUS_OK); 423 } 424 425 int nci_nfcc_loopback(struct nci_dev *ndev, const void *data, size_t data_len, 426 struct sk_buff **resp) 427 { 428 int r; 429 struct nci_loopback_data loopback_data; 430 struct nci_conn_info *conn_info; 431 struct sk_buff *skb; 432 int conn_id = nci_get_conn_info_by_dest_type_params(ndev, 433 NCI_DESTINATION_NFCC_LOOPBACK, NULL); 434 435 if (conn_id < 0) { 436 r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCC_LOOPBACK, 437 0, 0, NULL); 438 if (r != NCI_STATUS_OK) 439 return r; 440 441 conn_id = nci_get_conn_info_by_dest_type_params(ndev, 442 NCI_DESTINATION_NFCC_LOOPBACK, 443 NULL); 444 } 445 446 conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id); 447 if (!conn_info) 448 return -EPROTO; 449 450 /* store cb and context to be used on receiving data */ 451 conn_info->data_exchange_cb = nci_nfcc_loopback_cb; 452 conn_info->data_exchange_cb_context = ndev; 453 454 skb = nci_skb_alloc(ndev, NCI_DATA_HDR_SIZE + data_len, GFP_KERNEL); 455 if (!skb) 456 return -ENOMEM; 457 458 skb_reserve(skb, NCI_DATA_HDR_SIZE); 459 skb_put_data(skb, data, data_len); 460 461 loopback_data.conn_id = conn_id; 462 loopback_data.data = skb; 463 464 ndev->cur_conn_id = conn_id; 465 r = nci_request(ndev, nci_send_data_req, &loopback_data, 466 msecs_to_jiffies(NCI_DATA_TIMEOUT)); 467 if (r == NCI_STATUS_OK && resp) 468 *resp = conn_info->rx_skb; 469 470 return r; 471 } 472 EXPORT_SYMBOL(nci_nfcc_loopback); 473 474 static int nci_open_device(struct nci_dev *ndev) 475 { 476 int rc = 0; 477 478 mutex_lock(&ndev->req_lock); 479 480 if (test_bit(NCI_UNREG, &ndev->flags)) { 481 rc = -ENODEV; 482 goto done; 483 } 484 485 if (test_bit(NCI_UP, &ndev->flags)) { 486 rc = -EALREADY; 487 goto done; 488 } 489 490 if (ndev->ops->open(ndev)) { 491 rc = -EIO; 492 goto done; 493 } 494 495 atomic_set(&ndev->cmd_cnt, 1); 496 497 set_bit(NCI_INIT, &ndev->flags); 498 499 if (ndev->ops->init) 500 rc = ndev->ops->init(ndev); 501 502 if (!rc) { 503 rc = __nci_request(ndev, nci_reset_req, (void *)0, 504 msecs_to_jiffies(NCI_RESET_TIMEOUT)); 505 } 506 507 if (!rc && ndev->ops->setup) { 508 rc = ndev->ops->setup(ndev); 509 } 510 511 if (!rc) { 512 struct nci_core_init_v2_cmd nci_init_v2_cmd = { 513 .feature1 = NCI_FEATURE_DISABLE, 514 .feature2 = NCI_FEATURE_DISABLE 515 }; 516 const void *opt = NULL; 517 518 if (ndev->nci_ver & NCI_VER_2_MASK) 519 opt = &nci_init_v2_cmd; 520 521 rc = __nci_request(ndev, nci_init_req, opt, 522 msecs_to_jiffies(NCI_INIT_TIMEOUT)); 523 } 524 525 if (!rc && ndev->ops->post_setup) 526 rc = ndev->ops->post_setup(ndev); 527 528 if (!rc) { 529 rc = __nci_request(ndev, nci_init_complete_req, (void *)0, 530 msecs_to_jiffies(NCI_INIT_TIMEOUT)); 531 } 532 533 clear_bit(NCI_INIT, &ndev->flags); 534 535 if (!rc) { 536 set_bit(NCI_UP, &ndev->flags); 537 nci_clear_target_list(ndev); 538 atomic_set(&ndev->state, NCI_IDLE); 539 } else { 540 /* Init failed, cleanup */ 541 skb_queue_purge(&ndev->cmd_q); 542 skb_queue_purge(&ndev->rx_q); 543 skb_queue_purge(&ndev->tx_q); 544 545 ndev->ops->close(ndev); 546 ndev->flags &= BIT(NCI_UNREG); 547 } 548 549 done: 550 mutex_unlock(&ndev->req_lock); 551 return rc; 552 } 553 554 static int nci_close_device(struct nci_dev *ndev) 555 { 556 nci_req_cancel(ndev, ENODEV); 557 558 /* This mutex needs to be held as a barrier for 559 * caller nci_unregister_device 560 */ 561 mutex_lock(&ndev->req_lock); 562 563 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) { 564 /* Need to flush the cmd wq in case 565 * there is a queued/running cmd_work 566 */ 567 flush_workqueue(ndev->cmd_wq); 568 timer_delete_sync(&ndev->cmd_timer); 569 timer_delete_sync(&ndev->data_timer); 570 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) 571 nci_data_exchange_complete(ndev, NULL, 572 ndev->cur_conn_id, 573 -ENODEV); 574 mutex_unlock(&ndev->req_lock); 575 return 0; 576 } 577 578 /* Drop RX and TX queues */ 579 skb_queue_purge(&ndev->rx_q); 580 skb_queue_purge(&ndev->tx_q); 581 582 /* Flush RX and TX wq */ 583 flush_workqueue(ndev->rx_wq); 584 flush_workqueue(ndev->tx_wq); 585 586 /* Reset device */ 587 skb_queue_purge(&ndev->cmd_q); 588 atomic_set(&ndev->cmd_cnt, 1); 589 590 set_bit(NCI_INIT, &ndev->flags); 591 __nci_request(ndev, nci_reset_req, (void *)0, 592 msecs_to_jiffies(NCI_RESET_TIMEOUT)); 593 594 /* After this point our queues are empty 595 * and no works are scheduled. 596 */ 597 ndev->ops->close(ndev); 598 599 clear_bit(NCI_INIT, &ndev->flags); 600 601 /* Flush cmd wq */ 602 flush_workqueue(ndev->cmd_wq); 603 604 timer_delete_sync(&ndev->cmd_timer); 605 timer_delete_sync(&ndev->data_timer); 606 607 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) 608 nci_data_exchange_complete(ndev, NULL, ndev->cur_conn_id, 609 -ENODEV); 610 611 /* Clear flags except NCI_UNREG */ 612 ndev->flags &= BIT(NCI_UNREG); 613 614 mutex_unlock(&ndev->req_lock); 615 616 return 0; 617 } 618 619 /* NCI command timer function */ 620 static void nci_cmd_timer(struct timer_list *t) 621 { 622 struct nci_dev *ndev = timer_container_of(ndev, t, cmd_timer); 623 624 atomic_set(&ndev->cmd_cnt, 1); 625 queue_work(ndev->cmd_wq, &ndev->cmd_work); 626 } 627 628 /* NCI data exchange timer function */ 629 static void nci_data_timer(struct timer_list *t) 630 { 631 struct nci_dev *ndev = timer_container_of(ndev, t, data_timer); 632 633 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); 634 queue_work(ndev->rx_wq, &ndev->rx_work); 635 } 636 637 static int nci_dev_up(struct nfc_dev *nfc_dev) 638 { 639 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 640 641 return nci_open_device(ndev); 642 } 643 644 static int nci_dev_down(struct nfc_dev *nfc_dev) 645 { 646 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 647 648 return nci_close_device(ndev); 649 } 650 651 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, const __u8 *val) 652 { 653 struct nci_set_config_param param; 654 655 if (!val || !len) 656 return 0; 657 658 param.id = id; 659 param.len = len; 660 param.val = val; 661 662 return __nci_request(ndev, nci_set_config_req, ¶m, 663 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT)); 664 } 665 EXPORT_SYMBOL(nci_set_config); 666 667 static void nci_nfcee_discover_req(struct nci_dev *ndev, const void *opt) 668 { 669 struct nci_nfcee_discover_cmd cmd; 670 __u8 action = (unsigned long)opt; 671 672 cmd.discovery_action = action; 673 674 nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd); 675 } 676 677 int nci_nfcee_discover(struct nci_dev *ndev, u8 action) 678 { 679 unsigned long opt = action; 680 681 return __nci_request(ndev, nci_nfcee_discover_req, (void *)opt, 682 msecs_to_jiffies(NCI_CMD_TIMEOUT)); 683 } 684 EXPORT_SYMBOL(nci_nfcee_discover); 685 686 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, const void *opt) 687 { 688 const struct nci_nfcee_mode_set_cmd *cmd = opt; 689 690 nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD, 691 sizeof(struct nci_nfcee_mode_set_cmd), cmd); 692 } 693 694 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode) 695 { 696 struct nci_nfcee_mode_set_cmd cmd; 697 698 cmd.nfcee_id = nfcee_id; 699 cmd.nfcee_mode = nfcee_mode; 700 701 return __nci_request(ndev, nci_nfcee_mode_set_req, &cmd, 702 msecs_to_jiffies(NCI_CMD_TIMEOUT)); 703 } 704 EXPORT_SYMBOL(nci_nfcee_mode_set); 705 706 static void nci_core_conn_create_req(struct nci_dev *ndev, const void *opt) 707 { 708 const struct core_conn_create_data *data = opt; 709 710 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd); 711 } 712 713 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type, 714 u8 number_destination_params, 715 size_t params_len, 716 const struct core_conn_create_dest_spec_params *params) 717 { 718 int r; 719 struct nci_core_conn_create_cmd *cmd; 720 struct core_conn_create_data data; 721 722 data.length = params_len + sizeof(struct nci_core_conn_create_cmd); 723 cmd = kzalloc(data.length, GFP_KERNEL); 724 if (!cmd) 725 return -ENOMEM; 726 727 cmd->destination_type = destination_type; 728 cmd->number_destination_params = number_destination_params; 729 730 data.cmd = cmd; 731 732 if (params) { 733 memcpy(cmd->params, params, params_len); 734 if (params->length > 0) 735 memcpy(&ndev->cur_params, 736 ¶ms->value[DEST_SPEC_PARAMS_ID_INDEX], 737 sizeof(struct dest_spec_params)); 738 else 739 ndev->cur_params.id = 0; 740 } else { 741 ndev->cur_params.id = 0; 742 } 743 ndev->cur_dest_type = destination_type; 744 745 r = __nci_request(ndev, nci_core_conn_create_req, &data, 746 msecs_to_jiffies(NCI_CMD_TIMEOUT)); 747 kfree(cmd); 748 return r; 749 } 750 EXPORT_SYMBOL(nci_core_conn_create); 751 752 static void nci_core_conn_close_req(struct nci_dev *ndev, const void *opt) 753 { 754 __u8 conn_id = (unsigned long)opt; 755 756 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id); 757 } 758 759 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id) 760 { 761 unsigned long opt = conn_id; 762 763 ndev->cur_conn_id = conn_id; 764 return __nci_request(ndev, nci_core_conn_close_req, (void *)opt, 765 msecs_to_jiffies(NCI_CMD_TIMEOUT)); 766 } 767 EXPORT_SYMBOL(nci_core_conn_close); 768 769 static void nci_set_target_ats(struct nfc_target *target, struct nci_dev *ndev) 770 { 771 if (ndev->target_ats_len > 0) { 772 target->ats_len = ndev->target_ats_len; 773 memcpy(target->ats, ndev->target_ats, target->ats_len); 774 } 775 } 776 777 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev) 778 { 779 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 780 struct nci_set_config_param param; 781 int rc; 782 783 param.val = nfc_get_local_general_bytes(nfc_dev, ¶m.len); 784 if ((param.val == NULL) || (param.len == 0)) 785 return 0; 786 787 if (param.len > NFC_MAX_GT_LEN) 788 return -EINVAL; 789 790 param.id = NCI_PN_ATR_REQ_GEN_BYTES; 791 792 rc = nci_request(ndev, nci_set_config_req, ¶m, 793 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT)); 794 if (rc) 795 return rc; 796 797 param.id = NCI_LN_ATR_RES_GEN_BYTES; 798 799 return nci_request(ndev, nci_set_config_req, ¶m, 800 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT)); 801 } 802 803 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev) 804 { 805 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 806 int rc; 807 __u8 val; 808 809 val = NCI_LA_SEL_INFO_NFC_DEP_MASK; 810 811 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val); 812 if (rc) 813 return rc; 814 815 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK; 816 817 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val); 818 if (rc) 819 return rc; 820 821 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424; 822 823 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val); 824 } 825 826 static int nci_start_poll(struct nfc_dev *nfc_dev, 827 __u32 im_protocols, __u32 tm_protocols) 828 { 829 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 830 struct nci_rf_discover_param param; 831 int rc; 832 833 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) || 834 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) { 835 pr_err("unable to start poll, since poll is already active\n"); 836 return -EBUSY; 837 } 838 839 if (ndev->target_active_prot) { 840 pr_err("there is an active target\n"); 841 return -EBUSY; 842 } 843 844 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) || 845 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) { 846 pr_debug("target active or w4 select, implicitly deactivate\n"); 847 848 rc = nci_request(ndev, nci_rf_deactivate_req, 849 (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE, 850 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); 851 if (rc) 852 return -EBUSY; 853 } 854 855 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) { 856 rc = nci_set_local_general_bytes(nfc_dev); 857 if (rc) { 858 pr_err("failed to set local general bytes\n"); 859 return rc; 860 } 861 } 862 863 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) { 864 rc = nci_set_listen_parameters(nfc_dev); 865 if (rc) 866 pr_err("failed to set listen parameters\n"); 867 } 868 869 param.im_protocols = im_protocols; 870 param.tm_protocols = tm_protocols; 871 rc = nci_request(ndev, nci_rf_discover_req, ¶m, 872 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT)); 873 874 if (!rc) 875 ndev->poll_prots = im_protocols; 876 877 return rc; 878 } 879 880 static void nci_stop_poll(struct nfc_dev *nfc_dev) 881 { 882 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 883 884 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) && 885 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) { 886 pr_err("unable to stop poll, since poll is not active\n"); 887 return; 888 } 889 890 nci_request(ndev, nci_rf_deactivate_req, 891 (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE, 892 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); 893 } 894 895 static int nci_activate_target(struct nfc_dev *nfc_dev, 896 struct nfc_target *target, __u32 protocol) 897 { 898 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 899 struct nci_rf_discover_select_param param; 900 const struct nfc_target *nci_target = NULL; 901 int i; 902 int rc = 0; 903 904 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol); 905 906 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) && 907 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) { 908 pr_err("there is no available target to activate\n"); 909 return -EINVAL; 910 } 911 912 if (ndev->target_active_prot) { 913 pr_err("there is already an active target\n"); 914 return -EBUSY; 915 } 916 917 for (i = 0; i < ndev->n_targets; i++) { 918 if (ndev->targets[i].idx == target->idx) { 919 nci_target = &ndev->targets[i]; 920 break; 921 } 922 } 923 924 if (!nci_target) { 925 pr_err("unable to find the selected target\n"); 926 return -EINVAL; 927 } 928 929 if (protocol >= NFC_PROTO_MAX) { 930 pr_err("the requested nfc protocol is invalid\n"); 931 return -EINVAL; 932 } 933 934 if (!(nci_target->supported_protocols & (1 << protocol))) { 935 pr_err("target does not support the requested protocol 0x%x\n", 936 protocol); 937 return -EINVAL; 938 } 939 940 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) { 941 param.rf_discovery_id = nci_target->logical_idx; 942 943 if (protocol == NFC_PROTO_JEWEL) 944 param.rf_protocol = NCI_RF_PROTOCOL_T1T; 945 else if (protocol == NFC_PROTO_MIFARE) 946 param.rf_protocol = NCI_RF_PROTOCOL_T2T; 947 else if (protocol == NFC_PROTO_FELICA) 948 param.rf_protocol = NCI_RF_PROTOCOL_T3T; 949 else if (protocol == NFC_PROTO_ISO14443 || 950 protocol == NFC_PROTO_ISO14443_B) 951 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP; 952 else 953 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP; 954 955 rc = nci_request(ndev, nci_rf_discover_select_req, ¶m, 956 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT)); 957 } 958 959 if (!rc) { 960 ndev->target_active_prot = protocol; 961 if (protocol == NFC_PROTO_ISO14443) 962 nci_set_target_ats(target, ndev); 963 } 964 965 return rc; 966 } 967 968 static void nci_deactivate_target(struct nfc_dev *nfc_dev, 969 struct nfc_target *target, 970 __u8 mode) 971 { 972 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 973 unsigned long nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE; 974 975 if (!ndev->target_active_prot) { 976 pr_err("unable to deactivate target, no active target\n"); 977 return; 978 } 979 980 ndev->target_active_prot = 0; 981 982 switch (mode) { 983 case NFC_TARGET_MODE_SLEEP: 984 nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE; 985 break; 986 } 987 988 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) { 989 nci_request(ndev, nci_rf_deactivate_req, (void *)nci_mode, 990 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); 991 } 992 } 993 994 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target, 995 __u8 comm_mode, __u8 *gb, size_t gb_len) 996 { 997 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 998 int rc; 999 1000 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode); 1001 1002 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP); 1003 if (rc) 1004 return rc; 1005 1006 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb, 1007 ndev->remote_gb_len); 1008 if (!rc) 1009 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE, 1010 NFC_RF_INITIATOR); 1011 1012 return rc; 1013 } 1014 1015 static int nci_dep_link_down(struct nfc_dev *nfc_dev) 1016 { 1017 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 1018 int rc; 1019 1020 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) { 1021 nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE); 1022 } else { 1023 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE || 1024 atomic_read(&ndev->state) == NCI_DISCOVERY) { 1025 nci_request(ndev, nci_rf_deactivate_req, (void *)0, 1026 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); 1027 } 1028 1029 rc = nfc_tm_deactivated(nfc_dev); 1030 if (rc) 1031 pr_err("error when signaling tm deactivation\n"); 1032 } 1033 1034 return 0; 1035 } 1036 1037 1038 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target, 1039 struct sk_buff *skb, 1040 data_exchange_cb_t cb, void *cb_context) 1041 { 1042 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 1043 int rc; 1044 struct nci_conn_info *conn_info; 1045 1046 conn_info = ndev->rf_conn_info; 1047 if (!conn_info) { 1048 kfree_skb(skb); 1049 return -EPROTO; 1050 } 1051 1052 pr_debug("target_idx %d, len %d\n", target->idx, skb->len); 1053 1054 if (!ndev->target_active_prot) { 1055 pr_err("unable to exchange data, no active target\n"); 1056 kfree_skb(skb); 1057 return -EINVAL; 1058 } 1059 1060 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags)) { 1061 kfree_skb(skb); 1062 return -EBUSY; 1063 } 1064 1065 /* store cb and context to be used on receiving data */ 1066 conn_info->data_exchange_cb = cb; 1067 conn_info->data_exchange_cb_context = cb_context; 1068 1069 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb); 1070 if (rc) 1071 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags); 1072 1073 return rc; 1074 } 1075 1076 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb) 1077 { 1078 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 1079 int rc; 1080 1081 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb); 1082 if (rc) 1083 pr_err("unable to send data\n"); 1084 1085 return rc; 1086 } 1087 1088 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx) 1089 { 1090 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 1091 1092 if (ndev->ops->enable_se) 1093 return ndev->ops->enable_se(ndev, se_idx); 1094 1095 return 0; 1096 } 1097 1098 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx) 1099 { 1100 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 1101 1102 if (ndev->ops->disable_se) 1103 return ndev->ops->disable_se(ndev, se_idx); 1104 1105 return 0; 1106 } 1107 1108 static int nci_discover_se(struct nfc_dev *nfc_dev) 1109 { 1110 int r; 1111 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 1112 1113 if (ndev->ops->discover_se) { 1114 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE); 1115 if (r != NCI_STATUS_OK) 1116 return -EPROTO; 1117 1118 return ndev->ops->discover_se(ndev); 1119 } 1120 1121 return 0; 1122 } 1123 1124 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx, 1125 u8 *apdu, size_t apdu_length, 1126 se_io_cb_t cb, void *cb_context) 1127 { 1128 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 1129 1130 if (ndev->ops->se_io) 1131 return ndev->ops->se_io(ndev, se_idx, apdu, 1132 apdu_length, cb, cb_context); 1133 1134 return 0; 1135 } 1136 1137 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name) 1138 { 1139 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); 1140 1141 if (!ndev->ops->fw_download) 1142 return -ENOTSUPP; 1143 1144 return ndev->ops->fw_download(ndev, firmware_name); 1145 } 1146 1147 static const struct nfc_ops nci_nfc_ops = { 1148 .dev_up = nci_dev_up, 1149 .dev_down = nci_dev_down, 1150 .start_poll = nci_start_poll, 1151 .stop_poll = nci_stop_poll, 1152 .dep_link_up = nci_dep_link_up, 1153 .dep_link_down = nci_dep_link_down, 1154 .activate_target = nci_activate_target, 1155 .deactivate_target = nci_deactivate_target, 1156 .im_transceive = nci_transceive, 1157 .tm_send = nci_tm_send, 1158 .enable_se = nci_enable_se, 1159 .disable_se = nci_disable_se, 1160 .discover_se = nci_discover_se, 1161 .se_io = nci_se_io, 1162 .fw_download = nci_fw_download, 1163 }; 1164 1165 /* ---- Interface to NCI drivers ---- */ 1166 /** 1167 * nci_allocate_device - allocate a new nci device 1168 * 1169 * @ops: device operations 1170 * @supported_protocols: NFC protocols supported by the device 1171 * @tx_headroom: Reserved space at beginning of skb 1172 * @tx_tailroom: Reserved space at end of skb 1173 */ 1174 struct nci_dev *nci_allocate_device(const struct nci_ops *ops, 1175 __u32 supported_protocols, 1176 int tx_headroom, int tx_tailroom) 1177 { 1178 struct nci_dev *ndev; 1179 1180 pr_debug("supported_protocols 0x%x\n", supported_protocols); 1181 1182 if (!ops->open || !ops->close || !ops->send) 1183 return NULL; 1184 1185 if (!supported_protocols) 1186 return NULL; 1187 1188 ndev = kzalloc_obj(struct nci_dev); 1189 if (!ndev) 1190 return NULL; 1191 1192 ndev->ops = ops; 1193 1194 if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) { 1195 pr_err("Too many proprietary commands: %zd\n", 1196 ops->n_prop_ops); 1197 goto free_nci; 1198 } 1199 1200 ndev->tx_headroom = tx_headroom; 1201 ndev->tx_tailroom = tx_tailroom; 1202 init_completion(&ndev->req_completion); 1203 1204 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops, 1205 supported_protocols, 1206 tx_headroom + NCI_DATA_HDR_SIZE, 1207 tx_tailroom); 1208 if (!ndev->nfc_dev) 1209 goto free_nci; 1210 1211 ndev->hci_dev = nci_hci_allocate(ndev); 1212 if (!ndev->hci_dev) 1213 goto free_nfc; 1214 1215 nfc_set_drvdata(ndev->nfc_dev, ndev); 1216 1217 return ndev; 1218 1219 free_nfc: 1220 nfc_free_device(ndev->nfc_dev); 1221 free_nci: 1222 kfree(ndev); 1223 return NULL; 1224 } 1225 EXPORT_SYMBOL(nci_allocate_device); 1226 1227 /** 1228 * nci_free_device - deallocate nci device 1229 * 1230 * @ndev: The nci device to deallocate 1231 */ 1232 void nci_free_device(struct nci_dev *ndev) 1233 { 1234 nfc_free_device(ndev->nfc_dev); 1235 nci_hci_deallocate(ndev); 1236 1237 /* drop partial rx data packet if present */ 1238 if (ndev->rx_data_reassembly) 1239 kfree_skb(ndev->rx_data_reassembly); 1240 kfree(ndev); 1241 } 1242 EXPORT_SYMBOL(nci_free_device); 1243 1244 /** 1245 * nci_register_device - register a nci device in the nfc subsystem 1246 * 1247 * @ndev: The nci device to register 1248 */ 1249 int nci_register_device(struct nci_dev *ndev) 1250 { 1251 int rc; 1252 struct device *dev = &ndev->nfc_dev->dev; 1253 char name[32]; 1254 1255 ndev->flags = 0; 1256 1257 INIT_WORK(&ndev->cmd_work, nci_cmd_work); 1258 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev)); 1259 ndev->cmd_wq = create_singlethread_workqueue(name); 1260 if (!ndev->cmd_wq) { 1261 rc = -ENOMEM; 1262 goto exit; 1263 } 1264 1265 INIT_WORK(&ndev->rx_work, nci_rx_work); 1266 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev)); 1267 ndev->rx_wq = create_singlethread_workqueue(name); 1268 if (!ndev->rx_wq) { 1269 rc = -ENOMEM; 1270 goto destroy_cmd_wq_exit; 1271 } 1272 1273 INIT_WORK(&ndev->tx_work, nci_tx_work); 1274 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev)); 1275 ndev->tx_wq = create_singlethread_workqueue(name); 1276 if (!ndev->tx_wq) { 1277 rc = -ENOMEM; 1278 goto destroy_rx_wq_exit; 1279 } 1280 1281 skb_queue_head_init(&ndev->cmd_q); 1282 skb_queue_head_init(&ndev->rx_q); 1283 skb_queue_head_init(&ndev->tx_q); 1284 1285 timer_setup(&ndev->cmd_timer, nci_cmd_timer, 0); 1286 timer_setup(&ndev->data_timer, nci_data_timer, 0); 1287 1288 mutex_init(&ndev->req_lock); 1289 INIT_LIST_HEAD(&ndev->conn_info_list); 1290 1291 rc = nfc_register_device(ndev->nfc_dev); 1292 if (rc) 1293 goto destroy_tx_wq_exit; 1294 1295 goto exit; 1296 1297 destroy_tx_wq_exit: 1298 destroy_workqueue(ndev->tx_wq); 1299 1300 destroy_rx_wq_exit: 1301 destroy_workqueue(ndev->rx_wq); 1302 1303 destroy_cmd_wq_exit: 1304 destroy_workqueue(ndev->cmd_wq); 1305 1306 exit: 1307 return rc; 1308 } 1309 EXPORT_SYMBOL(nci_register_device); 1310 1311 /** 1312 * nci_unregister_device - unregister a nci device in the nfc subsystem 1313 * 1314 * @ndev: The nci device to unregister 1315 */ 1316 void nci_unregister_device(struct nci_dev *ndev) 1317 { 1318 struct nci_conn_info *conn_info, *n; 1319 1320 nfc_unregister_rfkill(ndev->nfc_dev); 1321 1322 /* This set_bit is not protected with specialized barrier, 1323 * However, it is fine because the mutex_lock(&ndev->req_lock); 1324 * in nci_close_device() will help to emit one. 1325 */ 1326 set_bit(NCI_UNREG, &ndev->flags); 1327 1328 nci_close_device(ndev); 1329 1330 destroy_workqueue(ndev->cmd_wq); 1331 destroy_workqueue(ndev->rx_wq); 1332 destroy_workqueue(ndev->tx_wq); 1333 1334 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) { 1335 list_del(&conn_info->list); 1336 /* conn_info is allocated with devm_kzalloc */ 1337 } 1338 1339 nfc_remove_device(ndev->nfc_dev); 1340 } 1341 EXPORT_SYMBOL(nci_unregister_device); 1342 1343 /** 1344 * nci_recv_frame - receive frame from NCI drivers 1345 * 1346 * @ndev: The nci device 1347 * @skb: The sk_buff to receive 1348 */ 1349 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb) 1350 { 1351 pr_debug("len %d\n", skb->len); 1352 1353 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) && 1354 !test_bit(NCI_INIT, &ndev->flags))) { 1355 kfree_skb(skb); 1356 return -ENXIO; 1357 } 1358 1359 /* Queue frame for rx worker thread */ 1360 skb_queue_tail(&ndev->rx_q, skb); 1361 queue_work(ndev->rx_wq, &ndev->rx_work); 1362 1363 return 0; 1364 } 1365 EXPORT_SYMBOL(nci_recv_frame); 1366 1367 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb) 1368 { 1369 pr_debug("len %d\n", skb->len); 1370 1371 if (!ndev) { 1372 kfree_skb(skb); 1373 return -ENODEV; 1374 } 1375 1376 /* Get rid of skb owner, prior to sending to the driver. */ 1377 skb_orphan(skb); 1378 1379 /* Send copy to sniffer */ 1380 nfc_send_to_raw_sock(ndev->nfc_dev, skb, 1381 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX); 1382 1383 return ndev->ops->send(ndev, skb); 1384 } 1385 EXPORT_SYMBOL(nci_send_frame); 1386 1387 /* Send NCI command */ 1388 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, const void *payload) 1389 { 1390 struct nci_ctrl_hdr *hdr; 1391 struct sk_buff *skb; 1392 1393 pr_debug("opcode 0x%x, plen %d\n", opcode, plen); 1394 1395 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL); 1396 if (!skb) { 1397 pr_err("no memory for command\n"); 1398 return -ENOMEM; 1399 } 1400 1401 hdr = skb_put(skb, NCI_CTRL_HDR_SIZE); 1402 hdr->gid = nci_opcode_gid(opcode); 1403 hdr->oid = nci_opcode_oid(opcode); 1404 hdr->plen = plen; 1405 1406 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT); 1407 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST); 1408 1409 if (plen) 1410 skb_put_data(skb, payload, plen); 1411 1412 skb_queue_tail(&ndev->cmd_q, skb); 1413 queue_work(ndev->cmd_wq, &ndev->cmd_work); 1414 1415 return 0; 1416 } 1417 EXPORT_SYMBOL(nci_send_cmd); 1418 1419 /* Proprietary commands API */ 1420 static const struct nci_driver_ops *ops_cmd_lookup(const struct nci_driver_ops *ops, 1421 size_t n_ops, 1422 __u16 opcode) 1423 { 1424 size_t i; 1425 const struct nci_driver_ops *op; 1426 1427 if (!ops || !n_ops) 1428 return NULL; 1429 1430 for (i = 0; i < n_ops; i++) { 1431 op = &ops[i]; 1432 if (op->opcode == opcode) 1433 return op; 1434 } 1435 1436 return NULL; 1437 } 1438 1439 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode, 1440 struct sk_buff *skb, const struct nci_driver_ops *ops, 1441 size_t n_ops) 1442 { 1443 const struct nci_driver_ops *op; 1444 1445 op = ops_cmd_lookup(ops, n_ops, rsp_opcode); 1446 if (!op || !op->rsp) 1447 return -ENOTSUPP; 1448 1449 return op->rsp(ndev, skb); 1450 } 1451 1452 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode, 1453 struct sk_buff *skb, const struct nci_driver_ops *ops, 1454 size_t n_ops) 1455 { 1456 const struct nci_driver_ops *op; 1457 1458 op = ops_cmd_lookup(ops, n_ops, ntf_opcode); 1459 if (!op || !op->ntf) 1460 return -ENOTSUPP; 1461 1462 return op->ntf(ndev, skb); 1463 } 1464 1465 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode, 1466 struct sk_buff *skb) 1467 { 1468 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops, 1469 ndev->ops->n_prop_ops); 1470 } 1471 1472 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode, 1473 struct sk_buff *skb) 1474 { 1475 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops, 1476 ndev->ops->n_prop_ops); 1477 } 1478 1479 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode, 1480 struct sk_buff *skb) 1481 { 1482 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops, 1483 ndev->ops->n_core_ops); 1484 } 1485 1486 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode, 1487 struct sk_buff *skb) 1488 { 1489 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops, 1490 ndev->ops->n_core_ops); 1491 } 1492 1493 static bool nci_valid_size(struct sk_buff *skb) 1494 { 1495 BUILD_BUG_ON(NCI_CTRL_HDR_SIZE != NCI_DATA_HDR_SIZE); 1496 unsigned int hdr_size = NCI_CTRL_HDR_SIZE; 1497 1498 if (skb->len < hdr_size || 1499 skb->len < hdr_size + nci_plen(skb->data)) { 1500 return false; 1501 } 1502 1503 if (!nci_plen(skb->data)) { 1504 /* Allow zero length in proprietary notifications (0x20 - 0x3F). */ 1505 if (nci_opcode_oid(nci_opcode(skb->data)) >= 0x20 && 1506 nci_mt(skb->data) == NCI_MT_NTF_PKT) 1507 return true; 1508 1509 /* Disallow zero length otherwise. */ 1510 return false; 1511 } 1512 1513 return true; 1514 } 1515 1516 /* ---- NCI TX Data worker thread ---- */ 1517 1518 static void nci_tx_work(struct work_struct *work) 1519 { 1520 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work); 1521 struct nci_conn_info *conn_info; 1522 struct sk_buff *skb; 1523 1524 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id); 1525 if (!conn_info) 1526 return; 1527 1528 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt)); 1529 1530 /* Send queued tx data */ 1531 while (atomic_read(&conn_info->credits_cnt)) { 1532 skb = skb_dequeue(&ndev->tx_q); 1533 if (!skb) 1534 return; 1535 kcov_remote_start_common(skb_get_kcov_handle(skb)); 1536 1537 /* Check if data flow control is used */ 1538 if (atomic_read(&conn_info->credits_cnt) != 1539 NCI_DATA_FLOW_CONTROL_NOT_USED) 1540 atomic_dec(&conn_info->credits_cnt); 1541 1542 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n", 1543 nci_pbf(skb->data), 1544 nci_conn_id(skb->data), 1545 nci_plen(skb->data)); 1546 1547 nci_send_frame(ndev, skb); 1548 1549 mod_timer(&ndev->data_timer, 1550 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT)); 1551 kcov_remote_stop(); 1552 } 1553 } 1554 1555 /* ----- NCI RX worker thread (data & control) ----- */ 1556 1557 static void nci_rx_work(struct work_struct *work) 1558 { 1559 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work); 1560 struct sk_buff *skb; 1561 1562 for (; (skb = skb_dequeue(&ndev->rx_q)); kcov_remote_stop()) { 1563 kcov_remote_start_common(skb_get_kcov_handle(skb)); 1564 1565 /* Send copy to sniffer */ 1566 nfc_send_to_raw_sock(ndev->nfc_dev, skb, 1567 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX); 1568 1569 if (!nci_valid_size(skb)) { 1570 kfree_skb(skb); 1571 continue; 1572 } 1573 1574 /* Process frame */ 1575 switch (nci_mt(skb->data)) { 1576 case NCI_MT_RSP_PKT: 1577 nci_rsp_packet(ndev, skb); 1578 break; 1579 1580 case NCI_MT_NTF_PKT: 1581 nci_ntf_packet(ndev, skb); 1582 break; 1583 1584 case NCI_MT_DATA_PKT: 1585 nci_rx_data_packet(ndev, skb); 1586 break; 1587 1588 default: 1589 pr_err("unknown MT 0x%x\n", nci_mt(skb->data)); 1590 kfree_skb(skb); 1591 break; 1592 } 1593 } 1594 1595 /* check if a data exchange timeout has occurred */ 1596 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) { 1597 /* complete the data exchange transaction, if exists */ 1598 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) 1599 nci_data_exchange_complete(ndev, NULL, 1600 ndev->cur_conn_id, 1601 -ETIMEDOUT); 1602 1603 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); 1604 } 1605 } 1606 1607 /* ----- NCI TX CMD worker thread ----- */ 1608 1609 static void nci_cmd_work(struct work_struct *work) 1610 { 1611 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work); 1612 struct sk_buff *skb; 1613 1614 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt)); 1615 1616 /* Send queued command */ 1617 if (atomic_read(&ndev->cmd_cnt)) { 1618 skb = skb_dequeue(&ndev->cmd_q); 1619 if (!skb) 1620 return; 1621 1622 kcov_remote_start_common(skb_get_kcov_handle(skb)); 1623 atomic_dec(&ndev->cmd_cnt); 1624 1625 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n", 1626 nci_pbf(skb->data), 1627 nci_opcode_gid(nci_opcode(skb->data)), 1628 nci_opcode_oid(nci_opcode(skb->data)), 1629 nci_plen(skb->data)); 1630 1631 nci_send_frame(ndev, skb); 1632 1633 mod_timer(&ndev->cmd_timer, 1634 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT)); 1635 kcov_remote_stop(); 1636 } 1637 } 1638 1639 MODULE_DESCRIPTION("NFC Controller Interface"); 1640 MODULE_LICENSE("GPL"); 1641