Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  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
 18#define pr_fmt(fmt) "hci: %s: " fmt, __func__
 19
 20#include <linux/init.h>
 21#include <linux/kernel.h>
 22#include <linux/sched.h>
 23#include <linux/module.h>
 24
 25#include <net/nfc/hci.h>
 26
 27#include "hci.h"
 28
 29static int nfc_hci_execute_cmd_async(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
 30			       const u8 *param, size_t param_len,
 31			       data_exchange_cb_t cb, void *cb_context)
 32{
 33	pr_debug("exec cmd async through pipe=%d, cmd=%d, plen=%zd\n", pipe,
 34		 cmd, param_len);
 35
 36	/* TODO: Define hci cmd execution delay. Should it be the same
 37	 * for all commands?
 38	 */
 39	return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_COMMAND, cmd,
 40				      param, param_len, cb, cb_context, 3000);
 41}
 42
 43/*
 44 * HCI command execution completion callback.
 45 * err will be a standard linux error (may be converted from HCI response)
 46 * skb contains the response data and must be disposed, or may be NULL if
 47 * an error occured
 48 */
 49static void nfc_hci_execute_cb(void *context, struct sk_buff *skb, int err)
 50{
 51	struct hcp_exec_waiter *hcp_ew = (struct hcp_exec_waiter *)context;
 52
 53	pr_debug("HCI Cmd completed with result=%d\n", err);
 54
 55	hcp_ew->exec_result = err;
 56	if (hcp_ew->exec_result == 0)
 57		hcp_ew->result_skb = skb;
 58	else
 59		kfree_skb(skb);
 60	hcp_ew->exec_complete = true;
 61
 62	wake_up(hcp_ew->wq);
 63}
 64
 65static int nfc_hci_execute_cmd(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
 66			       const u8 *param, size_t param_len,
 67			       struct sk_buff **skb)
 68{
 69	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(ew_wq);
 70	struct hcp_exec_waiter hcp_ew;
 71	hcp_ew.wq = &ew_wq;
 72	hcp_ew.exec_complete = false;
 73	hcp_ew.result_skb = NULL;
 74
 75	pr_debug("exec cmd sync through pipe=%d, cmd=%d, plen=%zd\n", pipe,
 76		 cmd, param_len);
 77
 78	/* TODO: Define hci cmd execution delay. Should it be the same
 79	 * for all commands?
 80	 */
 81	hcp_ew.exec_result = nfc_hci_hcp_message_tx(hdev, pipe,
 82						    NFC_HCI_HCP_COMMAND, cmd,
 83						    param, param_len,
 84						    nfc_hci_execute_cb, &hcp_ew,
 85						    3000);
 86	if (hcp_ew.exec_result < 0)
 87		return hcp_ew.exec_result;
 88
 89	wait_event(ew_wq, hcp_ew.exec_complete == true);
 90
 91	if (hcp_ew.exec_result == 0) {
 92		if (skb)
 93			*skb = hcp_ew.result_skb;
 94		else
 95			kfree_skb(hcp_ew.result_skb);
 96	}
 97
 98	return hcp_ew.exec_result;
 99}
100
101int nfc_hci_send_event(struct nfc_hci_dev *hdev, u8 gate, u8 event,
102		       const u8 *param, size_t param_len)
103{
104	u8 pipe;
105
106	pr_debug("%d to gate %d\n", event, gate);
107
108	pipe = hdev->gate2pipe[gate];
109	if (pipe == NFC_HCI_INVALID_PIPE)
110		return -EADDRNOTAVAIL;
111
112	return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_EVENT, event,
113				      param, param_len, NULL, NULL, 0);
114}
115EXPORT_SYMBOL(nfc_hci_send_event);
116
117int nfc_hci_send_response(struct nfc_hci_dev *hdev, u8 gate, u8 response,
118			  const u8 *param, size_t param_len)
119{
120	u8 pipe;
121
122	pr_debug("\n");
123
124	pipe = hdev->gate2pipe[gate];
125	if (pipe == NFC_HCI_INVALID_PIPE)
126		return -EADDRNOTAVAIL;
127
128	return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
129				      response, param, param_len, NULL, NULL,
130				      0);
131}
132EXPORT_SYMBOL(nfc_hci_send_response);
133
134/*
135 * Execute an hci command sent to gate.
136 * skb will contain response data if success. skb can be NULL if you are not
137 * interested by the response.
138 */
139int nfc_hci_send_cmd(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
140		     const u8 *param, size_t param_len, struct sk_buff **skb)
141{
142	u8 pipe;
143
144	pr_debug("\n");
145
146	pipe = hdev->gate2pipe[gate];
147	if (pipe == NFC_HCI_INVALID_PIPE)
148		return -EADDRNOTAVAIL;
149
150	return nfc_hci_execute_cmd(hdev, pipe, cmd, param, param_len, skb);
151}
152EXPORT_SYMBOL(nfc_hci_send_cmd);
153
154int nfc_hci_send_cmd_async(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
155			   const u8 *param, size_t param_len,
156			   data_exchange_cb_t cb, void *cb_context)
157{
158	u8 pipe;
159
160	pr_debug("\n");
161
162	pipe = hdev->gate2pipe[gate];
163	if (pipe == NFC_HCI_INVALID_PIPE)
164		return -EADDRNOTAVAIL;
165
166	return nfc_hci_execute_cmd_async(hdev, pipe, cmd, param, param_len,
167					 cb, cb_context);
168}
169EXPORT_SYMBOL(nfc_hci_send_cmd_async);
170
171int nfc_hci_set_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
172		      const u8 *param, size_t param_len)
173{
174	int r;
175	u8 *tmp;
176
177	/* TODO ELa: reg idx must be inserted before param, but we don't want
178	 * to ask the caller to do it to keep a simpler API.
179	 * For now, just create a new temporary param buffer. This is far from
180	 * optimal though, and the plan is to modify APIs to pass idx down to
181	 * nfc_hci_hcp_message_tx where the frame is actually built, thereby
182	 * eliminating the need for the temp allocation-copy here.
183	 */
184
185	pr_debug("idx=%d to gate %d\n", idx, gate);
186
187	tmp = kmalloc(1 + param_len, GFP_KERNEL);
188	if (tmp == NULL)
189		return -ENOMEM;
190
191	*tmp = idx;
192	memcpy(tmp + 1, param, param_len);
193
194	r = nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_SET_PARAMETER,
195			     tmp, param_len + 1, NULL);
196
197	kfree(tmp);
198
199	return r;
200}
201EXPORT_SYMBOL(nfc_hci_set_param);
202
203int nfc_hci_get_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
204		      struct sk_buff **skb)
205{
206	pr_debug("gate=%d regidx=%d\n", gate, idx);
207
208	return nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_GET_PARAMETER,
209				&idx, 1, skb);
210}
211EXPORT_SYMBOL(nfc_hci_get_param);
212
213static int nfc_hci_open_pipe(struct nfc_hci_dev *hdev, u8 pipe)
214{
215	struct sk_buff *skb;
216	int r;
217
218	pr_debug("pipe=%d\n", pipe);
219
220	r = nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_OPEN_PIPE,
221				NULL, 0, &skb);
222	if (r == 0) {
223		/* dest host other than host controller will send
224		 * number of pipes already open on this gate before
225		 * execution. The number can be found in skb->data[0]
226		 */
227		kfree_skb(skb);
228	}
229
230	return r;
231}
232
233static int nfc_hci_close_pipe(struct nfc_hci_dev *hdev, u8 pipe)
234{
235	pr_debug("\n");
236
237	return nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_CLOSE_PIPE,
238				   NULL, 0, NULL);
239}
240
241static u8 nfc_hci_create_pipe(struct nfc_hci_dev *hdev, u8 dest_host,
242			      u8 dest_gate, int *result)
243{
244	struct sk_buff *skb;
245	struct hci_create_pipe_params params;
246	struct hci_create_pipe_resp *resp;
247	u8 pipe;
248
249	pr_debug("gate=%d\n", dest_gate);
250
251	params.src_gate = NFC_HCI_ADMIN_GATE;
252	params.dest_host = dest_host;
253	params.dest_gate = dest_gate;
254
255	*result = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
256				      NFC_HCI_ADM_CREATE_PIPE,
257				      (u8 *) &params, sizeof(params), &skb);
258	if (*result < 0)
259		return NFC_HCI_INVALID_PIPE;
260
261	resp = (struct hci_create_pipe_resp *)skb->data;
262	pipe = resp->pipe;
263	kfree_skb(skb);
264
265	pr_debug("pipe created=%d\n", pipe);
266
267	return pipe;
268}
269
270static int nfc_hci_delete_pipe(struct nfc_hci_dev *hdev, u8 pipe)
271{
272	pr_debug("\n");
273
274	return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
275				   NFC_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
276}
277
278static int nfc_hci_clear_all_pipes(struct nfc_hci_dev *hdev)
279{
280	u8 param[2];
281	size_t param_len = 2;
282
283	/* TODO: Find out what the identity reference data is
284	 * and fill param with it. HCI spec 6.1.3.5 */
285
286	pr_debug("\n");
287
288	if (test_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &hdev->quirks))
289		param_len = 0;
290
291	return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
292				   NFC_HCI_ADM_CLEAR_ALL_PIPE, param, param_len,
293				   NULL);
294}
295
296int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate)
297{
298	int r;
299	u8 pipe = hdev->gate2pipe[gate];
300
301	pr_debug("\n");
302
303	if (pipe == NFC_HCI_INVALID_PIPE)
304		return -EADDRNOTAVAIL;
305
306	r = nfc_hci_close_pipe(hdev, pipe);
307	if (r < 0)
308		return r;
309
310	if (pipe != NFC_HCI_LINK_MGMT_PIPE && pipe != NFC_HCI_ADMIN_PIPE) {
311		r = nfc_hci_delete_pipe(hdev, pipe);
312		if (r < 0)
313			return r;
314	}
315
316	hdev->gate2pipe[gate] = NFC_HCI_INVALID_PIPE;
317
318	return 0;
319}
320EXPORT_SYMBOL(nfc_hci_disconnect_gate);
321
322int nfc_hci_disconnect_all_gates(struct nfc_hci_dev *hdev)
323{
324	int r;
325
326	pr_debug("\n");
327
328	r = nfc_hci_clear_all_pipes(hdev);
329	if (r < 0)
330		return r;
331
332	memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
333
334	return 0;
335}
336EXPORT_SYMBOL(nfc_hci_disconnect_all_gates);
337
338int nfc_hci_connect_gate(struct nfc_hci_dev *hdev, u8 dest_host, u8 dest_gate,
339			 u8 pipe)
340{
341	bool pipe_created = false;
342	int r;
343
344	pr_debug("\n");
345
346	if (hdev->gate2pipe[dest_gate] != NFC_HCI_INVALID_PIPE)
347		return -EADDRINUSE;
348
349	if (pipe != NFC_HCI_INVALID_PIPE)
350		goto open_pipe;
351
352	switch (dest_gate) {
353	case NFC_HCI_LINK_MGMT_GATE:
354		pipe = NFC_HCI_LINK_MGMT_PIPE;
355		break;
356	case NFC_HCI_ADMIN_GATE:
357		pipe = NFC_HCI_ADMIN_PIPE;
358		break;
359	default:
360		pipe = nfc_hci_create_pipe(hdev, dest_host, dest_gate, &r);
361		if (pipe == NFC_HCI_INVALID_PIPE)
362			return r;
363		pipe_created = true;
364		break;
365	}
366
367open_pipe:
368	r = nfc_hci_open_pipe(hdev, pipe);
369	if (r < 0) {
370		if (pipe_created)
371			if (nfc_hci_delete_pipe(hdev, pipe) < 0) {
372				/* TODO: Cannot clean by deleting pipe...
373				 * -> inconsistent state */
374			}
375		return r;
376	}
377
378	hdev->gate2pipe[dest_gate] = pipe;
379
380	return 0;
381}
382EXPORT_SYMBOL(nfc_hci_connect_gate);