fault-injection: notifier error injection
[linux-2.6.git] / net / nfc / hci / command.c
blob46362ef979db1ca88b4ad69d4b6adb7bd440f844
1 /*
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>
29 #include "hci.h"
31 static void nfc_hci_execute_cb(struct nfc_hci_dev *hdev, int err,
32 struct sk_buff *skb, void *cb_data)
34 struct hcp_exec_waiter *hcp_ew = (struct hcp_exec_waiter *)cb_data;
36 pr_debug("HCI Cmd completed with result=%d\n", err);
38 hcp_ew->exec_result = err;
39 if (hcp_ew->exec_result == 0)
40 hcp_ew->result_skb = skb;
41 else
42 kfree_skb(skb);
43 hcp_ew->exec_complete = true;
45 wake_up(hcp_ew->wq);
48 static int nfc_hci_execute_cmd(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
49 const u8 *param, size_t param_len,
50 struct sk_buff **skb)
52 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(ew_wq);
53 struct hcp_exec_waiter hcp_ew;
54 hcp_ew.wq = &ew_wq;
55 hcp_ew.exec_complete = false;
56 hcp_ew.result_skb = NULL;
58 pr_debug("through pipe=%d, cmd=%d, plen=%zd\n", pipe, cmd, param_len);
60 /* TODO: Define hci cmd execution delay. Should it be the same
61 * for all commands?
63 hcp_ew.exec_result = nfc_hci_hcp_message_tx(hdev, pipe,
64 NFC_HCI_HCP_COMMAND, cmd,
65 param, param_len,
66 nfc_hci_execute_cb, &hcp_ew,
67 3000);
68 if (hcp_ew.exec_result < 0)
69 return hcp_ew.exec_result;
71 wait_event(ew_wq, hcp_ew.exec_complete == true);
73 if (hcp_ew.exec_result == 0) {
74 if (skb)
75 *skb = hcp_ew.result_skb;
76 else
77 kfree_skb(hcp_ew.result_skb);
80 return hcp_ew.exec_result;
83 int nfc_hci_send_event(struct nfc_hci_dev *hdev, u8 gate, u8 event,
84 const u8 *param, size_t param_len)
86 u8 pipe;
88 pr_debug("%d to gate %d\n", event, gate);
90 pipe = hdev->gate2pipe[gate];
91 if (pipe == NFC_HCI_INVALID_PIPE)
92 return -EADDRNOTAVAIL;
94 return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_EVENT, event,
95 param, param_len, NULL, NULL, 0);
97 EXPORT_SYMBOL(nfc_hci_send_event);
99 int nfc_hci_send_response(struct nfc_hci_dev *hdev, u8 gate, u8 response,
100 const u8 *param, size_t param_len)
102 u8 pipe;
104 pr_debug("\n");
106 pipe = hdev->gate2pipe[gate];
107 if (pipe == NFC_HCI_INVALID_PIPE)
108 return -EADDRNOTAVAIL;
110 return nfc_hci_hcp_message_tx(hdev, pipe, NFC_HCI_HCP_RESPONSE,
111 response, param, param_len, NULL, NULL,
114 EXPORT_SYMBOL(nfc_hci_send_response);
117 * Execute an hci command sent to gate.
118 * skb will contain response data if success. skb can be NULL if you are not
119 * interested by the response.
121 int nfc_hci_send_cmd(struct nfc_hci_dev *hdev, u8 gate, u8 cmd,
122 const u8 *param, size_t param_len, struct sk_buff **skb)
124 u8 pipe;
126 pr_debug("\n");
128 pipe = hdev->gate2pipe[gate];
129 if (pipe == NFC_HCI_INVALID_PIPE)
130 return -EADDRNOTAVAIL;
132 return nfc_hci_execute_cmd(hdev, pipe, cmd, param, param_len, skb);
134 EXPORT_SYMBOL(nfc_hci_send_cmd);
136 int nfc_hci_set_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
137 const u8 *param, size_t param_len)
139 int r;
140 u8 *tmp;
142 /* TODO ELa: reg idx must be inserted before param, but we don't want
143 * to ask the caller to do it to keep a simpler API.
144 * For now, just create a new temporary param buffer. This is far from
145 * optimal though, and the plan is to modify APIs to pass idx down to
146 * nfc_hci_hcp_message_tx where the frame is actually built, thereby
147 * eliminating the need for the temp allocation-copy here.
150 pr_debug("idx=%d to gate %d\n", idx, gate);
152 tmp = kmalloc(1 + param_len, GFP_KERNEL);
153 if (tmp == NULL)
154 return -ENOMEM;
156 *tmp = idx;
157 memcpy(tmp + 1, param, param_len);
159 r = nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_SET_PARAMETER,
160 tmp, param_len + 1, NULL);
162 kfree(tmp);
164 return r;
166 EXPORT_SYMBOL(nfc_hci_set_param);
168 int nfc_hci_get_param(struct nfc_hci_dev *hdev, u8 gate, u8 idx,
169 struct sk_buff **skb)
171 pr_debug("gate=%d regidx=%d\n", gate, idx);
173 return nfc_hci_send_cmd(hdev, gate, NFC_HCI_ANY_GET_PARAMETER,
174 &idx, 1, skb);
176 EXPORT_SYMBOL(nfc_hci_get_param);
178 static int nfc_hci_open_pipe(struct nfc_hci_dev *hdev, u8 pipe)
180 struct sk_buff *skb;
181 int r;
183 pr_debug("pipe=%d\n", pipe);
185 r = nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_OPEN_PIPE,
186 NULL, 0, &skb);
187 if (r == 0) {
188 /* dest host other than host controller will send
189 * number of pipes already open on this gate before
190 * execution. The number can be found in skb->data[0]
192 kfree_skb(skb);
195 return r;
198 static int nfc_hci_close_pipe(struct nfc_hci_dev *hdev, u8 pipe)
200 pr_debug("\n");
202 return nfc_hci_execute_cmd(hdev, pipe, NFC_HCI_ANY_CLOSE_PIPE,
203 NULL, 0, NULL);
206 static u8 nfc_hci_create_pipe(struct nfc_hci_dev *hdev, u8 dest_host,
207 u8 dest_gate, int *result)
209 struct sk_buff *skb;
210 struct hci_create_pipe_params params;
211 struct hci_create_pipe_resp *resp;
212 u8 pipe;
214 pr_debug("gate=%d\n", dest_gate);
216 params.src_gate = NFC_HCI_ADMIN_GATE;
217 params.dest_host = dest_host;
218 params.dest_gate = dest_gate;
220 *result = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
221 NFC_HCI_ADM_CREATE_PIPE,
222 (u8 *) &params, sizeof(params), &skb);
223 if (*result == 0) {
224 resp = (struct hci_create_pipe_resp *)skb->data;
225 pipe = resp->pipe;
226 kfree_skb(skb);
228 pr_debug("pipe created=%d\n", pipe);
230 return pipe;
231 } else
232 return NFC_HCI_INVALID_PIPE;
235 static int nfc_hci_delete_pipe(struct nfc_hci_dev *hdev, u8 pipe)
237 pr_debug("\n");
239 return nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
240 NFC_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
243 static int nfc_hci_clear_all_pipes(struct nfc_hci_dev *hdev)
245 int r;
247 u8 param[2];
249 /* TODO: Find out what the identity reference data is
250 * and fill param with it. HCI spec 6.1.3.5 */
252 pr_debug("\n");
254 r = nfc_hci_execute_cmd(hdev, NFC_HCI_ADMIN_PIPE,
255 NFC_HCI_ADM_CLEAR_ALL_PIPE, param, 2, NULL);
257 return 0;
260 int nfc_hci_disconnect_gate(struct nfc_hci_dev *hdev, u8 gate)
262 int r;
263 u8 pipe = hdev->gate2pipe[gate];
265 pr_debug("\n");
267 if (pipe == NFC_HCI_INVALID_PIPE)
268 return -EADDRNOTAVAIL;
270 r = nfc_hci_close_pipe(hdev, pipe);
271 if (r < 0)
272 return r;
274 if (pipe != NFC_HCI_LINK_MGMT_PIPE && pipe != NFC_HCI_ADMIN_PIPE) {
275 r = nfc_hci_delete_pipe(hdev, pipe);
276 if (r < 0)
277 return r;
280 hdev->gate2pipe[gate] = NFC_HCI_INVALID_PIPE;
282 return 0;
284 EXPORT_SYMBOL(nfc_hci_disconnect_gate);
286 int nfc_hci_disconnect_all_gates(struct nfc_hci_dev *hdev)
288 int r;
290 pr_debug("\n");
292 r = nfc_hci_clear_all_pipes(hdev);
293 if (r < 0)
294 return r;
296 memset(hdev->gate2pipe, NFC_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
298 return 0;
300 EXPORT_SYMBOL(nfc_hci_disconnect_all_gates);
302 int nfc_hci_connect_gate(struct nfc_hci_dev *hdev, u8 dest_host, u8 dest_gate,
303 u8 pipe)
305 bool pipe_created = false;
306 int r;
308 pr_debug("\n");
310 if (hdev->gate2pipe[dest_gate] != NFC_HCI_INVALID_PIPE)
311 return -EADDRINUSE;
313 if (pipe != NFC_HCI_INVALID_PIPE)
314 goto pipe_is_open;
316 switch (dest_gate) {
317 case NFC_HCI_LINK_MGMT_GATE:
318 pipe = NFC_HCI_LINK_MGMT_PIPE;
319 break;
320 case NFC_HCI_ADMIN_GATE:
321 pipe = NFC_HCI_ADMIN_PIPE;
322 break;
323 default:
324 pipe = nfc_hci_create_pipe(hdev, dest_host, dest_gate, &r);
325 if (pipe == NFC_HCI_INVALID_PIPE)
326 return r;
327 pipe_created = true;
328 break;
331 r = nfc_hci_open_pipe(hdev, pipe);
332 if (r < 0) {
333 if (pipe_created)
334 if (nfc_hci_delete_pipe(hdev, pipe) < 0) {
335 /* TODO: Cannot clean by deleting pipe...
336 * -> inconsistent state */
338 return r;
341 pipe_is_open:
342 hdev->gate2pipe[dest_gate] = pipe;
344 return 0;
346 EXPORT_SYMBOL(nfc_hci_connect_gate);