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