1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012 Intel Corporation. All rights reserved.
6 #define pr_fmt(fmt) "hci: %s: " fmt, __func__
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <net/nfc/hci.h>
17 * Payload is the HCP message data only. Instruction will be prepended.
18 * Guarantees that cb will be called upon completion or timeout delay
19 * counted from the moment the cmd is sent to the transport.
21 int nfc_hci_hcp_message_tx(struct nfc_hci_dev *hdev, u8 pipe,
22 u8 type, u8 instruction,
23 const u8 *payload, size_t payload_len,
24 data_exchange_cb_t cb, void *cb_context,
25 unsigned long completion_delay)
27 struct nfc_dev *ndev = hdev->ndev;
29 const u8 *ptr = payload;
31 bool firstfrag = true;
33 cmd = kzalloc(sizeof(struct hci_msg), GFP_KERNEL);
37 INIT_LIST_HEAD(&cmd->msg_l);
38 skb_queue_head_init(&cmd->msg_frags);
39 cmd->wait_response = (type == NFC_HCI_HCP_COMMAND) ? true : false;
41 cmd->cb_context = cb_context;
42 cmd->completion_delay = completion_delay;
44 hci_len = payload_len + 1;
47 int skb_len, data_link_len;
48 struct hcp_packet *packet;
50 if (NFC_HCI_HCP_PACKET_HEADER_LEN + hci_len <=
51 hdev->max_data_link_payload)
52 data_link_len = hci_len;
54 data_link_len = hdev->max_data_link_payload -
55 NFC_HCI_HCP_PACKET_HEADER_LEN;
57 skb_len = ndev->tx_headroom + NFC_HCI_HCP_PACKET_HEADER_LEN +
58 data_link_len + ndev->tx_tailroom;
59 hci_len -= data_link_len;
61 skb = alloc_skb(skb_len, GFP_KERNEL);
66 skb_reserve(skb, ndev->tx_headroom);
68 skb_put(skb, NFC_HCI_HCP_PACKET_HEADER_LEN + data_link_len);
70 /* Only the last fragment will have the cb bit set to 1 */
71 packet = (struct hcp_packet *)skb->data;
72 packet->header = pipe;
75 packet->message.header = HCP_HEADER(type, instruction);
77 memcpy(packet->message.data, ptr,
79 ptr += data_link_len - 1;
82 memcpy(&packet->message, ptr, data_link_len);
86 /* This is the last fragment, set the cb bit */
88 packet->header |= ~NFC_HCI_FRAGMENT;
90 skb_queue_tail(&cmd->msg_frags, skb);
93 mutex_lock(&hdev->msg_tx_mutex);
95 if (hdev->shutting_down) {
97 mutex_unlock(&hdev->msg_tx_mutex);
101 list_add_tail(&cmd->msg_l, &hdev->msg_tx_queue);
102 mutex_unlock(&hdev->msg_tx_mutex);
104 schedule_work(&hdev->msg_tx_work);
109 skb_queue_purge(&cmd->msg_frags);
116 * Receive hcp message for pipe, with type and cmd.
117 * skb contains optional message data only.
119 void nfc_hci_hcp_message_rx(struct nfc_hci_dev *hdev, u8 pipe, u8 type,
120 u8 instruction, struct sk_buff *skb)
123 case NFC_HCI_HCP_RESPONSE:
124 nfc_hci_resp_received(hdev, instruction, skb);
126 case NFC_HCI_HCP_COMMAND:
127 nfc_hci_cmd_received(hdev, pipe, instruction, skb);
129 case NFC_HCI_HCP_EVENT:
130 nfc_hci_event_received(hdev, pipe, instruction, skb);
133 pr_err("UNKNOWN MSG Type %d, instruction=%d\n",