hcp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define pr_fmt(fmt) "hci: %s: " fmt, __func__
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <net/nfc/hci.h>
  22. #include "hci.h"
  23. /*
  24. * Payload is the HCP message data only. Instruction will be prepended.
  25. * Guarantees that cb will be called upon completion or timeout delay
  26. * counted from the moment the cmd is sent to the transport.
  27. */
  28. int nfc_hci_hcp_message_tx(struct nfc_hci_dev *hdev, u8 pipe,
  29. u8 type, u8 instruction,
  30. const u8 *payload, size_t payload_len,
  31. data_exchange_cb_t cb, void *cb_context,
  32. unsigned long completion_delay)
  33. {
  34. struct nfc_dev *ndev = hdev->ndev;
  35. struct hci_msg *cmd;
  36. const u8 *ptr = payload;
  37. int hci_len, err;
  38. bool firstfrag = true;
  39. cmd = kzalloc(sizeof(struct hci_msg), GFP_KERNEL);
  40. if (cmd == NULL)
  41. return -ENOMEM;
  42. INIT_LIST_HEAD(&cmd->msg_l);
  43. skb_queue_head_init(&cmd->msg_frags);
  44. cmd->wait_response = (type == NFC_HCI_HCP_COMMAND) ? true : false;
  45. cmd->cb = cb;
  46. cmd->cb_context = cb_context;
  47. cmd->completion_delay = completion_delay;
  48. hci_len = payload_len + 1;
  49. while (hci_len > 0) {
  50. struct sk_buff *skb;
  51. int skb_len, data_link_len;
  52. struct hcp_packet *packet;
  53. if (NFC_HCI_HCP_PACKET_HEADER_LEN + hci_len <=
  54. hdev->max_data_link_payload)
  55. data_link_len = hci_len;
  56. else
  57. data_link_len = hdev->max_data_link_payload -
  58. NFC_HCI_HCP_PACKET_HEADER_LEN;
  59. skb_len = ndev->tx_headroom + NFC_HCI_HCP_PACKET_HEADER_LEN +
  60. data_link_len + ndev->tx_tailroom;
  61. hci_len -= data_link_len;
  62. skb = alloc_skb(skb_len, GFP_KERNEL);
  63. if (skb == NULL) {
  64. err = -ENOMEM;
  65. goto out_skb_err;
  66. }
  67. skb_reserve(skb, ndev->tx_headroom);
  68. skb_put(skb, NFC_HCI_HCP_PACKET_HEADER_LEN + data_link_len);
  69. /* Only the last fragment will have the cb bit set to 1 */
  70. packet = (struct hcp_packet *)skb->data;
  71. packet->header = pipe;
  72. if (firstfrag) {
  73. firstfrag = false;
  74. packet->message.header = HCP_HEADER(type, instruction);
  75. if (ptr) {
  76. memcpy(packet->message.data, ptr,
  77. data_link_len - 1);
  78. ptr += data_link_len - 1;
  79. }
  80. } else {
  81. memcpy(&packet->message, ptr, data_link_len);
  82. ptr += data_link_len;
  83. }
  84. /* This is the last fragment, set the cb bit */
  85. if (hci_len == 0)
  86. packet->header |= ~NFC_HCI_FRAGMENT;
  87. skb_queue_tail(&cmd->msg_frags, skb);
  88. }
  89. mutex_lock(&hdev->msg_tx_mutex);
  90. if (hdev->shutting_down) {
  91. err = -ESHUTDOWN;
  92. mutex_unlock(&hdev->msg_tx_mutex);
  93. goto out_skb_err;
  94. }
  95. list_add_tail(&cmd->msg_l, &hdev->msg_tx_queue);
  96. mutex_unlock(&hdev->msg_tx_mutex);
  97. schedule_work(&hdev->msg_tx_work);
  98. return 0;
  99. out_skb_err:
  100. skb_queue_purge(&cmd->msg_frags);
  101. kfree(cmd);
  102. return err;
  103. }
  104. /*
  105. * Receive hcp message for pipe, with type and cmd.
  106. * skb contains optional message data only.
  107. */
  108. void nfc_hci_hcp_message_rx(struct nfc_hci_dev *hdev, u8 pipe, u8 type,
  109. u8 instruction, struct sk_buff *skb)
  110. {
  111. switch (type) {
  112. case NFC_HCI_HCP_RESPONSE:
  113. nfc_hci_resp_received(hdev, instruction, skb);
  114. break;
  115. case NFC_HCI_HCP_COMMAND:
  116. nfc_hci_cmd_received(hdev, pipe, instruction, skb);
  117. break;
  118. case NFC_HCI_HCP_EVENT:
  119. nfc_hci_event_received(hdev, pipe, instruction, skb);
  120. break;
  121. default:
  122. pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
  123. type, instruction);
  124. kfree_skb(skb);
  125. break;
  126. }
  127. }