1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
29#define MAX_FWI 4949
30
31static 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
39
40
41 return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_COMMAND, cmd,
42 param, param_len, cb, cb_context, MAX_FWI);
43}
44
45
46
47
48
49
50
51static void nfc_hci_execute_cb(void *context, struct sk_buff *skb, int err)
52{
53 struct hcp_exec_waiter *hcp_ew = (struct hcp_exec_waiter *)context;
54
55 pr_debug("HCI Cmd completed with result=%d\n", err);
56
57 hcp_ew->exec_result = err;
58 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
77 pr_debug("exec cmd sync through pipe=%d, cmd=%d, plen=%zd\n", pipe,
78 cmd, param_len);
79
80
81
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 MAX_FWI);
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
119
120
121
122
123
124int nfc_hci_send_cmd(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
125 const u8 *param, size_t param_len, struct sk_buff **skb)
126{
127 u8 pipe;
128
129 pr_debug("\n");
130
131 pipe = hdev->gate2pipe[gate];
132 if (pipe == NFC_HCI_INVALID_PIPE)
133 return -EADDRNOTAVAIL;
134
135 return nfc_hci_execute_cmd(hdev, pipe, cmd, param, param_len, skb);
136}
137EXPORT_SYMBOL(nfc_hci_send_cmd);
138
139int nfc_hci_send_cmd_async(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
140 const u8 *param, size_t param_len,
141 data_exchange_cb_t cb, void *cb_context)
142{
143 u8 pipe;
144
145 pr_debug("\n");
146
147 pipe = hdev->gate2pipe[gate];
148 if (pipe == NFC_HCI_INVALID_PIPE)
149 return -EADDRNOTAVAIL;
150
151 return nfc_hci_execute_cmd_async(hdev, pipe, cmd, param, param_len,
152 cb, cb_context);
153}
154EXPORT_SYMBOL(nfc_hci_send_cmd_async);
155
156int nfc_hci_set_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
157 const u8 *param, size_t param_len)
158{
159 int r;
160 u8 *tmp;
161
162
163
164
165
166
167
168
169
170 pr_debug("idx=%d to gate %d\n", idx, gate);
171
172 tmp = kmalloc(1 + param_len, GFP_KERNEL);
173 if (tmp == NULL)
174 return -ENOMEM;
175
176 *tmp = idx;
177 memcpy(tmp + 1, param, param_len);
178
179 r = nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_SET_PARAMETER,
180 tmp, param_len + 1, NULL);
181
182 kfree(tmp);
183
184 return r;
185}
186EXPORT_SYMBOL(nfc_hci_set_param);
187
188int nfc_hci_get_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
189 struct sk_buff **skb)
190{
191 pr_debug("gate=%d regidx=%d\n", gate, idx);
192
193 return nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_GET_PARAMETER,
194 &idx, 1, skb);
195}
196EXPORT_SYMBOL(nfc_hci_get_param);
197
198static int nfc_hci_open_pipe(struct nfc_hci_dev *hdev, u8 pipe)
199{
200 struct sk_buff *skb;
201 int r;
202
203 pr_debug("pipe=%d\n", pipe);
204
205 r = nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_OPEN_PIPE,
206 NULL, 0, &skb);
207 if (r == 0) {
208
209
210
211
212 kfree_skb(skb);
213 }
214
215 return r;
216}
217
218static int nfc_hci_close_pipe(struct nfc_hci_dev *hdev, u8 pipe)
219{
220 pr_debug("\n");
221
222 return nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_CLOSE_PIPE,
223 NULL, 0, NULL);
224}
225
226static u8 nfc_hci_create_pipe(struct nfc_hci_dev *hdev, u8 dest_host,
227 u8 dest_gate, int *result)
228{
229 struct sk_buff *skb;
230 struct hci_create_pipe_params params;
231 struct hci_create_pipe_resp *resp;
232 u8 pipe;
233
234 pr_debug("gate=%d\n", dest_gate);
235
236 params.src_gate = NFC_HCI_ADMIN_GATE;
237 params.dest_host = dest_host;
238 params.dest_gate = dest_gate;
239
240 *result = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
241 NFC_HCI_ADM_CREATE_PIPE,
242 (u8 *) ¶ms, sizeof(params), &skb);
243 if (*result < 0)
244 return NFC_HCI_INVALID_PIPE;
245
246 resp = (struct hci_create_pipe_resp *)skb->data;
247 pipe = resp->pipe;
248 kfree_skb(skb);
249
250 pr_debug("pipe created=%d\n", pipe);
251
252 return pipe;
253}
254
255static int nfc_hci_delete_pipe(struct nfc_hci_dev *hdev, u8 pipe)
256{
257 pr_debug("\n");
258
259 return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
260 NFC_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
261}
262
263static int nfc_hci_clear_all_pipes(struct nfc_hci_dev *hdev)
264{
265 u8 param[2];
266 size_t param_len = 2;
267
268
269
270
271 pr_debug("\n");
272
273 if (test_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &hdev->quirks))
274 param_len = 0;
275
276 return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
277 NFC_HCI_ADM_CLEAR_ALL_PIPE, param, param_len,
278 NULL);
279}
280
281int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate)
282{
283 int r;
284 u8 pipe = hdev->gate2pipe[gate];
285
286 pr_debug("\n");
287
288 if (pipe == NFC_HCI_INVALID_PIPE)
289 return -EADDRNOTAVAIL;
290
291 r = nfc_hci_close_pipe(hdev, pipe);
292 if (r < 0)
293 return r;
294
295 if (pipe != NFC_HCI_LINK_MGMT_PIPE && pipe != NFC_HCI_ADMIN_PIPE) {
296 r = nfc_hci_delete_pipe(hdev, pipe);
297 if (r < 0)
298 return r;
299 }
300
301 hdev->gate2pipe[gate] = NFC_HCI_INVALID_PIPE;
302
303 return 0;
304}
305EXPORT_SYMBOL(nfc_hci_disconnect_gate);
306
307int nfc_hci_disconnect_all_gates(struct nfc_hci_dev *hdev)
308{
309 int r;
310
311 pr_debug("\n");
312
313 r = nfc_hci_clear_all_pipes(hdev);
314 if (r < 0)
315 return r;
316
317 nfc_hci_reset_pipes(hdev);
318
319 return 0;
320}
321EXPORT_SYMBOL(nfc_hci_disconnect_all_gates);
322
323int nfc_hci_connect_gate(struct nfc_hci_dev *hdev, u8 dest_host, u8 dest_gate,
324 u8 pipe)
325{
326 bool pipe_created = false;
327 int r;
328
329 pr_debug("\n");
330
331 if (pipe == NFC_HCI_DO_NOT_CREATE_PIPE)
332 return 0;
333
334 if (hdev->gate2pipe[dest_gate] != NFC_HCI_INVALID_PIPE)
335 return -EADDRINUSE;
336
337 if (pipe != NFC_HCI_INVALID_PIPE)
338 goto open_pipe;
339
340 switch (dest_gate) {
341 case NFC_HCI_LINK_MGMT_GATE:
342 pipe = NFC_HCI_LINK_MGMT_PIPE;
343 break;
344 case NFC_HCI_ADMIN_GATE:
345 pipe = NFC_HCI_ADMIN_PIPE;
346 break;
347 default:
348 pipe = nfc_hci_create_pipe(hdev, dest_host, dest_gate, &r);
349 if (pipe == NFC_HCI_INVALID_PIPE)
350 return r;
351 pipe_created = true;
352 break;
353 }
354
355open_pipe:
356 r = nfc_hci_open_pipe(hdev, pipe);
357 if (r < 0) {
358 if (pipe_created)
359 if (nfc_hci_delete_pipe(hdev, pipe) < 0) {
360
361
362 }
363 return r;
364 }
365
366 hdev->pipes[pipe].gate = dest_gate;
367 hdev->pipes[pipe].dest_host = dest_host;
368 hdev->gate2pipe[dest_gate] = pipe;
369
370 return 0;
371}
372EXPORT_SYMBOL(nfc_hci_connect_gate);
373