blob: 15e21093c7a5370a1b8faa91b7a2d9fb862febf8 [file] [log] [blame]
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +02001/*
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
20#define pr_fmt(fmt) "hci: %s: " fmt, __func__
21
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/sched.h>
25#include <linux/module.h>
26
27#include <net/nfc/hci.h>
28
29#include "hci.h"
30
Eric Lapuyadeb5faa642012-09-11 10:41:41 +020031/*
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
35 * an error occured
36 */
37static void nfc_hci_execute_cb(void *context, struct sk_buff *skb, int err)
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020038{
Eric Lapuyadeb5faa642012-09-11 10:41:41 +020039 struct hcp_exec_waiter *hcp_ew = (struct hcp_exec_waiter *)context;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020040
Eric Lapuyade6c1c5b92012-05-03 15:35:25 +020041 pr_debug("HCI Cmd completed with result=%d\n", err);
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020042
Eric Lapuyade6c1c5b92012-05-03 15:35:25 +020043 hcp_ew->exec_result = err;
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +020044 if (hcp_ew->exec_result == 0)
45 hcp_ew->result_skb = skb;
46 else
47 kfree_skb(skb);
48 hcp_ew->exec_complete = true;
49
50 wake_up(hcp_ew->wq);
51}
52
53static int nfc_hci_execute_cmd(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
54 const u8 *param, size_t param_len,
55 struct sk_buff **skb)
56{
57 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(ew_wq);
58 struct hcp_exec_waiter hcp_ew;
59 hcp_ew.wq = &ew_wq;
60 hcp_ew.exec_complete = false;
61 hcp_ew.result_skb = NULL;
62
63 pr_debug("through pipe=%d, cmd=%d, plen=%zd\n", pipe, cmd, param_len);
64
65 /* TODO: Define hci cmd execution delay. Should it be the same
66 * for all commands?
67 */
68 hcp_ew.exec_result = nfc_hci_hcp_message_tx(hdev, pipe,
69 NFC_HCI_HCP_COMMAND, cmd,
70 param, param_len,
71 nfc_hci_execute_cb, &hcp_ew,
72 3000);
73 if (hcp_ew.exec_result < 0)
74 return hcp_ew.exec_result;
75
76 wait_event(ew_wq, hcp_ew.exec_complete == true);
77
78 if (hcp_ew.exec_result == 0) {
79 if (skb)
80 *skb = hcp_ew.result_skb;
81 else
82 kfree_skb(hcp_ew.result_skb);
83 }
84
85 return hcp_ew.exec_result;
86}
87
88int nfc_hci_send_event(struct nfc_hci_dev *hdev, u8 gate, u8 event,
89 const u8 *param, size_t param_len)
90{
91 u8 pipe;
92
93 pr_debug("%d to gate %d\n", event, gate);
94
95 pipe = hdev->gate2pipe[gate];
96 if (pipe == NFC_HCI_INVALID_PIPE)
97 return -EADDRNOTAVAIL;
98
99 return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_EVENT, event,
100 param, param_len, NULL, NULL, 0);
101}
102EXPORT_SYMBOL(nfc_hci_send_event);
103
104int nfc_hci_send_response(struct nfc_hci_dev *hdev, u8 gate, u8 response,
105 const u8 *param, size_t param_len)
106{
107 u8 pipe;
108
109 pr_debug("\n");
110
111 pipe = hdev->gate2pipe[gate];
112 if (pipe == NFC_HCI_INVALID_PIPE)
113 return -EADDRNOTAVAIL;
114
115 return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
116 response, param, param_len, NULL, NULL,
117 0);
118}
119EXPORT_SYMBOL(nfc_hci_send_response);
120
121/*
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.
125 */
126int 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)
128{
129 u8 pipe;
130
131 pr_debug("\n");
132
133 pipe = hdev->gate2pipe[gate];
134 if (pipe == NFC_HCI_INVALID_PIPE)
135 return -EADDRNOTAVAIL;
136
137 return nfc_hci_execute_cmd(hdev, pipe, cmd, param, param_len, skb);
138}
139EXPORT_SYMBOL(nfc_hci_send_cmd);
140
141int nfc_hci_set_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
142 const u8 *param, size_t param_len)
143{
144 int r;
145 u8 *tmp;
146
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.
153 */
154
155 pr_debug("idx=%d to gate %d\n", idx, gate);
156
157 tmp = kmalloc(1 + param_len, GFP_KERNEL);
158 if (tmp == NULL)
159 return -ENOMEM;
160
161 *tmp = idx;
162 memcpy(tmp + 1, param, param_len);
163
164 r = nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_SET_PARAMETER,
165 tmp, param_len + 1, NULL);
166
167 kfree(tmp);
168
169 return r;
170}
171EXPORT_SYMBOL(nfc_hci_set_param);
172
173int nfc_hci_get_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
174 struct sk_buff **skb)
175{
176 pr_debug("gate=%d regidx=%d\n", gate, idx);
177
178 return nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_GET_PARAMETER,
179 &idx, 1, skb);
180}
181EXPORT_SYMBOL(nfc_hci_get_param);
182
183static int nfc_hci_open_pipe(struct nfc_hci_dev *hdev, u8 pipe)
184{
185 struct sk_buff *skb;
186 int r;
187
188 pr_debug("pipe=%d\n", pipe);
189
190 r = nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_OPEN_PIPE,
191 NULL, 0, &skb);
192 if (r == 0) {
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]
196 */
197 kfree_skb(skb);
198 }
199
200 return r;
201}
202
203static int nfc_hci_close_pipe(struct nfc_hci_dev *hdev, u8 pipe)
204{
205 pr_debug("\n");
206
207 return nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_CLOSE_PIPE,
208 NULL, 0, NULL);
209}
210
211static u8 nfc_hci_create_pipe(struct nfc_hci_dev *hdev, u8 dest_host,
212 u8 dest_gate, int *result)
213{
214 struct sk_buff *skb;
215 struct hci_create_pipe_params params;
216 struct hci_create_pipe_resp *resp;
217 u8 pipe;
218
219 pr_debug("gate=%d\n", dest_gate);
220
221 params.src_gate = NFC_HCI_ADMIN_GATE;
222 params.dest_host = dest_host;
223 params.dest_gate = dest_gate;
224
225 *result = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
226 NFC_HCI_ADM_CREATE_PIPE,
227 (u8 *) &params, sizeof(params), &skb);
228 if (*result == 0) {
229 resp = (struct hci_create_pipe_resp *)skb->data;
230 pipe = resp->pipe;
231 kfree_skb(skb);
232
233 pr_debug("pipe created=%d\n", pipe);
234
235 return pipe;
236 } else
237 return NFC_HCI_INVALID_PIPE;
238}
239
240static int nfc_hci_delete_pipe(struct nfc_hci_dev *hdev, u8 pipe)
241{
242 pr_debug("\n");
243
244 return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
245 NFC_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
246}
247
248static int nfc_hci_clear_all_pipes(struct nfc_hci_dev *hdev)
249{
250 int r;
251
252 u8 param[2];
253
254 /* TODO: Find out what the identity reference data is
255 * and fill param with it. HCI spec 6.1.3.5 */
256
257 pr_debug("\n");
258
259 r = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
260 NFC_HCI_ADM_CLEAR_ALL_PIPE, param, 2, NULL);
261
262 return 0;
263}
264
265int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate)
266{
267 int r;
268 u8 pipe = hdev->gate2pipe[gate];
269
270 pr_debug("\n");
271
272 if (pipe == NFC_HCI_INVALID_PIPE)
273 return -EADDRNOTAVAIL;
274
275 r = nfc_hci_close_pipe(hdev, pipe);
276 if (r < 0)
277 return r;
278
279 if (pipe != NFC_HCI_LINK_MGMT_PIPE && pipe != NFC_HCI_ADMIN_PIPE) {
280 r = nfc_hci_delete_pipe(hdev, pipe);
281 if (r < 0)
282 return r;
283 }
284
285 hdev->gate2pipe[gate] = NFC_HCI_INVALID_PIPE;
286
287 return 0;
288}
289EXPORT_SYMBOL(nfc_hci_disconnect_gate);
290
291int nfc_hci_disconnect_all_gates(struct nfc_hci_dev *hdev)
292{
293 int r;
294
295 pr_debug("\n");
296
297 r = nfc_hci_clear_all_pipes(hdev);
298 if (r < 0)
299 return r;
300
301 memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
302
303 return 0;
304}
305EXPORT_SYMBOL(nfc_hci_disconnect_all_gates);
306
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200307int nfc_hci_connect_gate(struct nfc_hci_dev *hdev, u8 dest_host, u8 dest_gate,
308 u8 pipe)
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200309{
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200310 bool pipe_created = false;
311 int r;
312
313 pr_debug("\n");
314
315 if (hdev->gate2pipe[dest_gate] != NFC_HCI_INVALID_PIPE)
316 return -EADDRINUSE;
317
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200318 if (pipe != NFC_HCI_INVALID_PIPE)
319 goto pipe_is_open;
320
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200321 switch (dest_gate) {
322 case NFC_HCI_LINK_MGMT_GATE:
323 pipe = NFC_HCI_LINK_MGMT_PIPE;
324 break;
325 case NFC_HCI_ADMIN_GATE:
326 pipe = NFC_HCI_ADMIN_PIPE;
327 break;
328 default:
329 pipe = nfc_hci_create_pipe(hdev, dest_host, dest_gate, &r);
330 if (pipe == NFC_HCI_INVALID_PIPE)
331 return r;
332 pipe_created = true;
333 break;
334 }
335
336 r = nfc_hci_open_pipe(hdev, pipe);
337 if (r < 0) {
338 if (pipe_created)
339 if (nfc_hci_delete_pipe(hdev, pipe) < 0) {
340 /* TODO: Cannot clean by deleting pipe...
341 * -> inconsistent state */
342 }
343 return r;
344 }
345
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200346pipe_is_open:
Eric Lapuyade8b8d2e02012-04-10 19:43:06 +0200347 hdev->gate2pipe[dest_gate] = pipe;
348
349 return 0;
350}
351EXPORT_SYMBOL(nfc_hci_connect_gate);