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