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, see <http://www.gnu.org/licenses/>.
18 #define pr_fmt(fmt) "hci: %s: " fmt, __func__
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/module.h>
25 #include <net/nfc/hci.h>
29 static 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
)
33 pr_debug("exec cmd async through pipe=%d, cmd=%d, plen=%zd\n", pipe
,
36 /* TODO: Define hci cmd execution delay. Should it be the same
39 return nfc_hci_hcp_message_tx(hdev
, pipe
, NFC_HCI_HCP_COMMAND
, cmd
,
40 param
, param_len
, cb
, cb_context
, 3000);
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
49 static void nfc_hci_execute_cb(void *context
, struct sk_buff
*skb
, int err
)
51 struct hcp_exec_waiter
*hcp_ew
= (struct hcp_exec_waiter
*)context
;
53 pr_debug("HCI Cmd completed with result=%d\n", err
);
55 hcp_ew
->exec_result
= err
;
56 if (hcp_ew
->exec_result
== 0)
57 hcp_ew
->result_skb
= skb
;
60 hcp_ew
->exec_complete
= true;
65 static int nfc_hci_execute_cmd(struct nfc_hci_dev
*hdev
, u8 pipe
, u8 cmd
,
66 const u8
*param
, size_t param_len
,
69 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(ew_wq
);
70 struct hcp_exec_waiter hcp_ew
;
72 hcp_ew
.exec_complete
= false;
73 hcp_ew
.result_skb
= NULL
;
75 pr_debug("exec cmd sync through pipe=%d, cmd=%d, plen=%zd\n", pipe
,
78 /* TODO: Define hci cmd execution delay. Should it be the same
81 hcp_ew
.exec_result
= nfc_hci_hcp_message_tx(hdev
, pipe
,
82 NFC_HCI_HCP_COMMAND
, cmd
,
84 nfc_hci_execute_cb
, &hcp_ew
,
86 if (hcp_ew
.exec_result
< 0)
87 return hcp_ew
.exec_result
;
89 wait_event(ew_wq
, hcp_ew
.exec_complete
== true);
91 if (hcp_ew
.exec_result
== 0) {
93 *skb
= hcp_ew
.result_skb
;
95 kfree_skb(hcp_ew
.result_skb
);
98 return hcp_ew
.exec_result
;
101 int nfc_hci_send_event(struct nfc_hci_dev
*hdev
, u8 gate
, u8 event
,
102 const u8
*param
, size_t param_len
)
106 pr_debug("%d to gate %d\n", event
, gate
);
108 pipe
= hdev
->gate2pipe
[gate
];
109 if (pipe
== NFC_HCI_INVALID_PIPE
)
110 return -EADDRNOTAVAIL
;
112 return nfc_hci_hcp_message_tx(hdev
, pipe
, NFC_HCI_HCP_EVENT
, event
,
113 param
, param_len
, NULL
, NULL
, 0);
115 EXPORT_SYMBOL(nfc_hci_send_event
);
117 int nfc_hci_send_response(struct nfc_hci_dev
*hdev
, u8 gate
, u8 response
,
118 const u8
*param
, size_t param_len
)
124 pipe
= hdev
->gate2pipe
[gate
];
125 if (pipe
== NFC_HCI_INVALID_PIPE
)
126 return -EADDRNOTAVAIL
;
128 return nfc_hci_hcp_message_tx(hdev
, pipe
, NFC_HCI_HCP_RESPONSE
,
129 response
, param
, param_len
, NULL
, NULL
,
132 EXPORT_SYMBOL(nfc_hci_send_response
);
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.
139 int 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
)
146 pipe
= hdev
->gate2pipe
[gate
];
147 if (pipe
== NFC_HCI_INVALID_PIPE
)
148 return -EADDRNOTAVAIL
;
150 return nfc_hci_execute_cmd(hdev
, pipe
, cmd
, param
, param_len
, skb
);
152 EXPORT_SYMBOL(nfc_hci_send_cmd
);
154 int 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
)
162 pipe
= hdev
->gate2pipe
[gate
];
163 if (pipe
== NFC_HCI_INVALID_PIPE
)
164 return -EADDRNOTAVAIL
;
166 return nfc_hci_execute_cmd_async(hdev
, pipe
, cmd
, param
, param_len
,
169 EXPORT_SYMBOL(nfc_hci_send_cmd_async
);
171 int nfc_hci_set_param(struct nfc_hci_dev
*hdev
, u8 gate
, u8 idx
,
172 const u8
*param
, size_t param_len
)
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.
185 pr_debug("idx=%d to gate %d\n", idx
, gate
);
187 tmp
= kmalloc(1 + param_len
, GFP_KERNEL
);
192 memcpy(tmp
+ 1, param
, param_len
);
194 r
= nfc_hci_send_cmd(hdev
, gate
, NFC_HCI_ANY_SET_PARAMETER
,
195 tmp
, param_len
+ 1, NULL
);
201 EXPORT_SYMBOL(nfc_hci_set_param
);
203 int nfc_hci_get_param(struct nfc_hci_dev
*hdev
, u8 gate
, u8 idx
,
204 struct sk_buff
**skb
)
206 pr_debug("gate=%d regidx=%d\n", gate
, idx
);
208 return nfc_hci_send_cmd(hdev
, gate
, NFC_HCI_ANY_GET_PARAMETER
,
211 EXPORT_SYMBOL(nfc_hci_get_param
);
213 static int nfc_hci_open_pipe(struct nfc_hci_dev
*hdev
, u8 pipe
)
218 pr_debug("pipe=%d\n", pipe
);
220 r
= nfc_hci_execute_cmd(hdev
, pipe
, NFC_HCI_ANY_OPEN_PIPE
,
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]
233 static int nfc_hci_close_pipe(struct nfc_hci_dev
*hdev
, u8 pipe
)
237 return nfc_hci_execute_cmd(hdev
, pipe
, NFC_HCI_ANY_CLOSE_PIPE
,
241 static u8
nfc_hci_create_pipe(struct nfc_hci_dev
*hdev
, u8 dest_host
,
242 u8 dest_gate
, int *result
)
245 struct hci_create_pipe_params params
;
246 struct hci_create_pipe_resp
*resp
;
249 pr_debug("gate=%d\n", dest_gate
);
251 params
.src_gate
= NFC_HCI_ADMIN_GATE
;
252 params
.dest_host
= dest_host
;
253 params
.dest_gate
= dest_gate
;
255 *result
= nfc_hci_execute_cmd(hdev
, NFC_HCI_ADMIN_PIPE
,
256 NFC_HCI_ADM_CREATE_PIPE
,
257 (u8
*) ¶ms
, sizeof(params
), &skb
);
259 return NFC_HCI_INVALID_PIPE
;
261 resp
= (struct hci_create_pipe_resp
*)skb
->data
;
265 pr_debug("pipe created=%d\n", pipe
);
270 static int nfc_hci_delete_pipe(struct nfc_hci_dev
*hdev
, u8 pipe
)
274 return nfc_hci_execute_cmd(hdev
, NFC_HCI_ADMIN_PIPE
,
275 NFC_HCI_ADM_DELETE_PIPE
, &pipe
, 1, NULL
);
278 static int nfc_hci_clear_all_pipes(struct nfc_hci_dev
*hdev
)
281 size_t param_len
= 2;
283 /* TODO: Find out what the identity reference data is
284 * and fill param with it. HCI spec 6.1.3.5 */
288 if (test_bit(NFC_HCI_QUIRK_SHORT_CLEAR
, &hdev
->quirks
))
291 return nfc_hci_execute_cmd(hdev
, NFC_HCI_ADMIN_PIPE
,
292 NFC_HCI_ADM_CLEAR_ALL_PIPE
, param
, param_len
,
296 int nfc_hci_disconnect_gate(struct nfc_hci_dev
*hdev
, u8 gate
)
299 u8 pipe
= hdev
->gate2pipe
[gate
];
303 if (pipe
== NFC_HCI_INVALID_PIPE
)
304 return -EADDRNOTAVAIL
;
306 r
= nfc_hci_close_pipe(hdev
, pipe
);
310 if (pipe
!= NFC_HCI_LINK_MGMT_PIPE
&& pipe
!= NFC_HCI_ADMIN_PIPE
) {
311 r
= nfc_hci_delete_pipe(hdev
, pipe
);
316 hdev
->gate2pipe
[gate
] = NFC_HCI_INVALID_PIPE
;
320 EXPORT_SYMBOL(nfc_hci_disconnect_gate
);
322 int nfc_hci_disconnect_all_gates(struct nfc_hci_dev
*hdev
)
328 r
= nfc_hci_clear_all_pipes(hdev
);
332 memset(hdev
->gate2pipe
, NFC_HCI_INVALID_PIPE
, sizeof(hdev
->gate2pipe
));
336 EXPORT_SYMBOL(nfc_hci_disconnect_all_gates
);
338 int nfc_hci_connect_gate(struct nfc_hci_dev
*hdev
, u8 dest_host
, u8 dest_gate
,
341 bool pipe_created
= false;
346 if (hdev
->gate2pipe
[dest_gate
] != NFC_HCI_INVALID_PIPE
)
349 if (pipe
!= NFC_HCI_INVALID_PIPE
)
353 case NFC_HCI_LINK_MGMT_GATE
:
354 pipe
= NFC_HCI_LINK_MGMT_PIPE
;
356 case NFC_HCI_ADMIN_GATE
:
357 pipe
= NFC_HCI_ADMIN_PIPE
;
360 pipe
= nfc_hci_create_pipe(hdev
, dest_host
, dest_gate
, &r
);
361 if (pipe
== NFC_HCI_INVALID_PIPE
)
368 r
= nfc_hci_open_pipe(hdev
, pipe
);
371 if (nfc_hci_delete_pipe(hdev
, pipe
) < 0) {
372 /* TODO: Cannot clean by deleting pipe...
373 * -> inconsistent state */
378 hdev
->gate2pipe
[dest_gate
] = pipe
;
382 EXPORT_SYMBOL(nfc_hci_connect_gate
);