2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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/>.
18 #define pr_fmt(fmt) "hci: %s: " fmt, __func__
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
24 #include <net/nfc/hci.h>
29 * Payload is the HCP message data only. Instruction will be prepended.
30 * Guarantees that cb will be called upon completion or timeout delay
31 * counted from the moment the cmd is sent to the transport.
33 int nfc_hci_hcp_message_tx(struct nfc_hci_dev
*hdev
, u8 pipe
,
34 u8 type
, u8 instruction
,
35 const u8
*payload
, size_t payload_len
,
36 data_exchange_cb_t cb
, void *cb_context
,
37 unsigned long completion_delay
)
39 struct nfc_dev
*ndev
= hdev
->ndev
;
41 const u8
*ptr
= payload
;
43 bool firstfrag
= true;
45 cmd
= kzalloc(sizeof(struct hci_msg
), GFP_KERNEL
);
49 INIT_LIST_HEAD(&cmd
->msg_l
);
50 skb_queue_head_init(&cmd
->msg_frags
);
51 cmd
->wait_response
= (type
== NFC_HCI_HCP_COMMAND
) ? true : false;
53 cmd
->cb_context
= cb_context
;
54 cmd
->completion_delay
= completion_delay
;
56 hci_len
= payload_len
+ 1;
59 int skb_len
, data_link_len
;
60 struct hcp_packet
*packet
;
62 if (NFC_HCI_HCP_PACKET_HEADER_LEN
+ hci_len
<=
63 hdev
->max_data_link_payload
)
64 data_link_len
= hci_len
;
66 data_link_len
= hdev
->max_data_link_payload
-
67 NFC_HCI_HCP_PACKET_HEADER_LEN
;
69 skb_len
= ndev
->tx_headroom
+ NFC_HCI_HCP_PACKET_HEADER_LEN
+
70 data_link_len
+ ndev
->tx_tailroom
;
71 hci_len
-= data_link_len
;
73 skb
= alloc_skb(skb_len
, GFP_KERNEL
);
78 skb_reserve(skb
, ndev
->tx_headroom
);
80 skb_put(skb
, NFC_HCI_HCP_PACKET_HEADER_LEN
+ data_link_len
);
82 /* Only the last fragment will have the cb bit set to 1 */
83 packet
= (struct hcp_packet
*)skb
->data
;
84 packet
->header
= pipe
;
87 packet
->message
.header
= HCP_HEADER(type
, instruction
);
89 memcpy(packet
->message
.data
, ptr
,
91 ptr
+= data_link_len
- 1;
94 memcpy(&packet
->message
, ptr
, data_link_len
);
98 /* This is the last fragment, set the cb bit */
100 packet
->header
|= ~NFC_HCI_FRAGMENT
;
102 skb_queue_tail(&cmd
->msg_frags
, skb
);
105 mutex_lock(&hdev
->msg_tx_mutex
);
107 if (hdev
->shutting_down
) {
109 mutex_unlock(&hdev
->msg_tx_mutex
);
113 list_add_tail(&cmd
->msg_l
, &hdev
->msg_tx_queue
);
114 mutex_unlock(&hdev
->msg_tx_mutex
);
116 schedule_work(&hdev
->msg_tx_work
);
121 skb_queue_purge(&cmd
->msg_frags
);
128 * Receive hcp message for pipe, with type and cmd.
129 * skb contains optional message data only.
131 void nfc_hci_hcp_message_rx(struct nfc_hci_dev
*hdev
, u8 pipe
, u8 type
,
132 u8 instruction
, struct sk_buff
*skb
)
135 case NFC_HCI_HCP_RESPONSE
:
136 nfc_hci_resp_received(hdev
, instruction
, skb
);
138 case NFC_HCI_HCP_COMMAND
:
139 nfc_hci_cmd_received(hdev
, pipe
, instruction
, skb
);
141 case NFC_HCI_HCP_EVENT
:
142 nfc_hci_event_received(hdev
, pipe
, instruction
, skb
);
145 pr_err("UNKNOWN MSG Type %d, instruction=%d\n",