hcp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, write to the
  16. * Free Software Foundation, Inc.,
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #define pr_fmt(fmt) "hci: %s: " fmt, __func__
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <net/nfc/hci.h>
  24. #include "hci.h"
  25. /*
  26. * Payload is the HCP message data only. Instruction will be prepended.
  27. * Guarantees that cb will be called upon completion or timeout delay
  28. * counted from the moment the cmd is sent to the transport.
  29. */
  30. int nfc_hci_hcp_message_tx(struct nfc_hci_dev *hdev, u8 pipe,
  31. u8 type, u8 instruction,
  32. const u8 *payload, size_t payload_len,
  33. hci_cmd_cb_t cb, void *cb_data,
  34. unsigned long completion_delay)
  35. {
  36. struct nfc_dev *ndev = hdev->ndev;
  37. struct hci_msg *cmd;
  38. const u8 *ptr = payload;
  39. int hci_len, err;
  40. bool firstfrag = true;
  41. cmd = kzalloc(sizeof(struct hci_msg), GFP_KERNEL);
  42. if (cmd == NULL)
  43. return -ENOMEM;
  44. INIT_LIST_HEAD(&cmd->msg_l);
  45. skb_queue_head_init(&cmd->msg_frags);
  46. cmd->wait_response = (type == NFC_HCI_HCP_COMMAND) ? true : false;
  47. cmd->cb = cb;
  48. cmd->cb_context = cb_data;
  49. cmd->completion_delay = completion_delay;
  50. hci_len = payload_len + 1;
  51. while (hci_len > 0) {
  52. struct sk_buff *skb;
  53. int skb_len, data_link_len;
  54. struct hcp_packet *packet;
  55. if (NFC_HCI_HCP_PACKET_HEADER_LEN + hci_len <=
  56. hdev->max_data_link_payload)
  57. data_link_len = hci_len;
  58. else
  59. data_link_len = hdev->max_data_link_payload -
  60. NFC_HCI_HCP_PACKET_HEADER_LEN;
  61. skb_len = ndev->tx_headroom + NFC_HCI_HCP_PACKET_HEADER_LEN +
  62. data_link_len + ndev->tx_tailroom;
  63. hci_len -= data_link_len;
  64. skb = alloc_skb(skb_len, GFP_KERNEL);
  65. if (skb == NULL) {
  66. err = -ENOMEM;
  67. goto out_skb_err;
  68. }
  69. skb_reserve(skb, ndev->tx_headroom);
  70. skb_put(skb, NFC_HCI_HCP_PACKET_HEADER_LEN + data_link_len);
  71. /* Only the last fragment will have the cb bit set to 1 */
  72. packet = (struct hcp_packet *)skb->data;
  73. packet->header = pipe;
  74. if (firstfrag) {
  75. firstfrag = false;
  76. packet->message.header = HCP_HEADER(type, instruction);
  77. if (ptr) {
  78. memcpy(packet->message.data, ptr,
  79. data_link_len - 1);
  80. ptr += data_link_len - 1;
  81. }
  82. } else {
  83. memcpy(&packet->message, ptr, data_link_len);
  84. ptr += data_link_len;
  85. }
  86. /* This is the last fragment, set the cb bit */
  87. if (hci_len == 0)
  88. packet->header |= ~NFC_HCI_FRAGMENT;
  89. skb_queue_tail(&cmd->msg_frags, skb);
  90. }
  91. mutex_lock(&hdev->msg_tx_mutex);
  92. list_add_tail(&cmd->msg_l, &hdev->msg_tx_queue);
  93. mutex_unlock(&hdev->msg_tx_mutex);
  94. queue_work(hdev->msg_tx_wq, &hdev->msg_tx_work);
  95. return 0;
  96. out_skb_err:
  97. skb_queue_purge(&cmd->msg_frags);
  98. kfree(cmd);
  99. return err;
  100. }
  101. u8 nfc_hci_pipe2gate(struct nfc_hci_dev *hdev, u8 pipe)
  102. {
  103. int gate;
  104. for (gate = 0; gate < NFC_HCI_MAX_GATES; gate++)
  105. if (hdev->gate2pipe[gate] == pipe)
  106. return gate;
  107. return 0xff;
  108. }
  109. /*
  110. * Receive hcp message for pipe, with type and cmd.
  111. * skb contains optional message data only.
  112. */
  113. void nfc_hci_hcp_message_rx(struct nfc_hci_dev *hdev, u8 pipe, u8 type,
  114. u8 instruction, struct sk_buff *skb)
  115. {
  116. switch (type) {
  117. case NFC_HCI_HCP_RESPONSE:
  118. nfc_hci_resp_received(hdev, instruction, skb);
  119. break;
  120. case NFC_HCI_HCP_COMMAND:
  121. nfc_hci_cmd_received(hdev, pipe, instruction, skb);
  122. break;
  123. case NFC_HCI_HCP_EVENT:
  124. nfc_hci_event_received(hdev, pipe, instruction, skb);
  125. break;
  126. default:
  127. pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
  128. type, instruction);
  129. kfree_skb(skb);
  130. break;
  131. }
  132. }