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, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #define pr_fmt(fmt) "hci: %s: " fmt, __func__
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/module.h>
27 #include <net/nfc/hci.h>
32 * HCI command execution completion callback.
33 * err will be a standard linux error (may be converted from HCI response)
34 * skb contains the response data and must be disposed, or may be NULL if
37 static void nfc_hci_execute_cb(void *context, struct sk_buff *skb, int err)
39 struct hcp_exec_waiter *hcp_ew = (struct hcp_exec_waiter *)context;
41 pr_debug("HCI Cmd completed with result=%d\n", err);
43 hcp_ew->exec_result = err;
44 if (hcp_ew->exec_result == 0)
45 hcp_ew->result_skb = skb;
48 hcp_ew->exec_complete = true;
53 static int nfc_hci_execute_cmd(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
54 const u8 *param, size_t param_len,
57 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(ew_wq);
58 struct hcp_exec_waiter hcp_ew;
60 hcp_ew.exec_complete = false;
61 hcp_ew.result_skb = NULL;
63 pr_debug("through pipe=%d, cmd=%d, plen=%zd\n", pipe, cmd, param_len);
65 /* TODO: Define hci cmd execution delay. Should it be the same
68 hcp_ew.exec_result = nfc_hci_hcp_message_tx(hdev, pipe,
69 NFC_HCI_HCP_COMMAND, cmd,
71 nfc_hci_execute_cb, &hcp_ew,
73 if (hcp_ew.exec_result < 0)
74 return hcp_ew.exec_result;
76 wait_event(ew_wq, hcp_ew.exec_complete == true);
78 if (hcp_ew.exec_result == 0) {
80 *skb = hcp_ew.result_skb;
82 kfree_skb(hcp_ew.result_skb);
85 return hcp_ew.exec_result;
88 int nfc_hci_send_event(struct nfc_hci_dev *hdev, u8 gate, u8 event,
89 const u8 *param, size_t param_len)
93 pr_debug("%d to gate %d\n", event, gate);
95 pipe = hdev->gate2pipe[gate];
96 if (pipe == NFC_HCI_INVALID_PIPE)
97 return -EADDRNOTAVAIL;
99 return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_EVENT, event,
100 param, param_len, NULL, NULL, 0);
102 EXPORT_SYMBOL(nfc_hci_send_event);
104 int nfc_hci_send_response(struct nfc_hci_dev *hdev, u8 gate, u8 response,
105 const u8 *param, size_t param_len)
111 pipe = hdev->gate2pipe[gate];
112 if (pipe == NFC_HCI_INVALID_PIPE)
113 return -EADDRNOTAVAIL;
115 return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
116 response, param, param_len, NULL, NULL,
119 EXPORT_SYMBOL(nfc_hci_send_response);
122 * Execute an hci command sent to gate.
123 * skb will contain response data if success. skb can be NULL if you are not
124 * interested by the response.
126 int nfc_hci_send_cmd(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
127 const u8 *param, size_t param_len, struct sk_buff **skb)
133 pipe = hdev->gate2pipe[gate];
134 if (pipe == NFC_HCI_INVALID_PIPE)
135 return -EADDRNOTAVAIL;
137 return nfc_hci_execute_cmd(hdev, pipe, cmd, param, param_len, skb);
139 EXPORT_SYMBOL(nfc_hci_send_cmd);
141 int nfc_hci_set_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
142 const u8 *param, size_t param_len)
147 /* TODO ELa: reg idx must be inserted before param, but we don't want
148 * to ask the caller to do it to keep a simpler API.
149 * For now, just create a new temporary param buffer. This is far from
150 * optimal though, and the plan is to modify APIs to pass idx down to
151 * nfc_hci_hcp_message_tx where the frame is actually built, thereby
152 * eliminating the need for the temp allocation-copy here.
155 pr_debug("idx=%d to gate %d\n", idx, gate);
157 tmp = kmalloc(1 + param_len, GFP_KERNEL);
162 memcpy(tmp + 1, param, param_len);
164 r = nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_SET_PARAMETER,
165 tmp, param_len + 1, NULL);
171 EXPORT_SYMBOL(nfc_hci_set_param);
173 int nfc_hci_get_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
174 struct sk_buff **skb)
176 pr_debug("gate=%d regidx=%d\n", gate, idx);
178 return nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_GET_PARAMETER,
181 EXPORT_SYMBOL(nfc_hci_get_param);
183 static int nfc_hci_open_pipe(struct nfc_hci_dev *hdev, u8 pipe)
188 pr_debug("pipe=%d\n", pipe);
190 r = nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_OPEN_PIPE,
193 /* dest host other than host controller will send
194 * number of pipes already open on this gate before
195 * execution. The number can be found in skb->data[0]
203 static int nfc_hci_close_pipe(struct nfc_hci_dev *hdev, u8 pipe)
207 return nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_CLOSE_PIPE,
211 static u8 nfc_hci_create_pipe(struct nfc_hci_dev *hdev, u8 dest_host,
212 u8 dest_gate, int *result)
215 struct hci_create_pipe_params params;
216 struct hci_create_pipe_resp *resp;
219 pr_debug("gate=%d\n", dest_gate);
221 params.src_gate = NFC_HCI_ADMIN_GATE;
222 params.dest_host = dest_host;
223 params.dest_gate = dest_gate;
225 *result = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
226 NFC_HCI_ADM_CREATE_PIPE,
227 (u8 *) ¶ms, sizeof(params), &skb);
229 resp = (struct hci_create_pipe_resp *)skb->data;
233 pr_debug("pipe created=%d\n", pipe);
237 return NFC_HCI_INVALID_PIPE;
240 static int nfc_hci_delete_pipe(struct nfc_hci_dev *hdev, u8 pipe)
244 return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
245 NFC_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
248 static int nfc_hci_clear_all_pipes(struct nfc_hci_dev *hdev)
254 /* TODO: Find out what the identity reference data is
255 * and fill param with it. HCI spec 6.1.3.5 */
259 r = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
260 NFC_HCI_ADM_CLEAR_ALL_PIPE, param, 2, NULL);
265 int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate)
268 u8 pipe = hdev->gate2pipe[gate];
272 if (pipe == NFC_HCI_INVALID_PIPE)
273 return -EADDRNOTAVAIL;
275 r = nfc_hci_close_pipe(hdev, pipe);
279 if (pipe != NFC_HCI_LINK_MGMT_PIPE && pipe != NFC_HCI_ADMIN_PIPE) {
280 r = nfc_hci_delete_pipe(hdev, pipe);
285 hdev->gate2pipe[gate] = NFC_HCI_INVALID_PIPE;
289 EXPORT_SYMBOL(nfc_hci_disconnect_gate);
291 int nfc_hci_disconnect_all_gates(struct nfc_hci_dev *hdev)
297 r = nfc_hci_clear_all_pipes(hdev);
301 memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
305 EXPORT_SYMBOL(nfc_hci_disconnect_all_gates);
307 int nfc_hci_connect_gate(struct nfc_hci_dev *hdev, u8 dest_host, u8 dest_gate,
310 bool pipe_created = false;
315 if (hdev->gate2pipe[dest_gate] != NFC_HCI_INVALID_PIPE)
318 if (pipe != NFC_HCI_INVALID_PIPE)
322 case NFC_HCI_LINK_MGMT_GATE:
323 pipe = NFC_HCI_LINK_MGMT_PIPE;
325 case NFC_HCI_ADMIN_GATE:
326 pipe = NFC_HCI_ADMIN_PIPE;
329 pipe = nfc_hci_create_pipe(hdev, dest_host, dest_gate, &r);
330 if (pipe == NFC_HCI_INVALID_PIPE)
336 r = nfc_hci_open_pipe(hdev, pipe);
339 if (nfc_hci_delete_pipe(hdev, pipe) < 0) {
340 /* TODO: Cannot clean by deleting pipe...
341 * -> inconsistent state */
347 hdev->gate2pipe[dest_gate] = pipe;
351 EXPORT_SYMBOL(nfc_hci_connect_gate);