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>
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
;
43 hcp_ew
->exec_complete
= true;
48 static int nfc_hci_execute_cmd(struct nfc_hci_dev
*hdev
, u8 pipe
, u8 cmd
,
49 const u8
*param
, size_t param_len
,
52 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(ew_wq
);
53 struct hcp_exec_waiter hcp_ew
;
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
63 hcp_ew
.exec_result
= nfc_hci_hcp_message_tx(hdev
, pipe
,
64 NFC_HCI_HCP_COMMAND
, cmd
,
66 nfc_hci_execute_cb
, &hcp_ew
,
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) {
75 *skb
= hcp_ew
.result_skb
;
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
)
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
)
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
)
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
)
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
);
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
);
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
,
176 EXPORT_SYMBOL(nfc_hci_get_param
);
178 static int nfc_hci_open_pipe(struct nfc_hci_dev
*hdev
, u8 pipe
)
183 pr_debug("pipe=%d\n", pipe
);
185 r
= nfc_hci_execute_cmd(hdev
, pipe
, NFC_HCI_ANY_OPEN_PIPE
,
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]
198 static int nfc_hci_close_pipe(struct nfc_hci_dev
*hdev
, u8 pipe
)
202 return nfc_hci_execute_cmd(hdev
, pipe
, NFC_HCI_ANY_CLOSE_PIPE
,
206 static u8
nfc_hci_create_pipe(struct nfc_hci_dev
*hdev
, u8 dest_host
,
207 u8 dest_gate
, int *result
)
210 struct hci_create_pipe_params params
;
211 struct hci_create_pipe_resp
*resp
;
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
*) ¶ms
, sizeof(params
), &skb
);
224 resp
= (struct hci_create_pipe_resp
*)skb
->data
;
228 pr_debug("pipe created=%d\n", pipe
);
232 return NFC_HCI_INVALID_PIPE
;
235 static int nfc_hci_delete_pipe(struct nfc_hci_dev
*hdev
, u8 pipe
)
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
)
249 /* TODO: Find out what the identity reference data is
250 * and fill param with it. HCI spec 6.1.3.5 */
254 r
= nfc_hci_execute_cmd(hdev
, NFC_HCI_ADMIN_PIPE
,
255 NFC_HCI_ADM_CLEAR_ALL_PIPE
, param
, 2, NULL
);
260 int nfc_hci_disconnect_gate(struct nfc_hci_dev
*hdev
, u8 gate
)
263 u8 pipe
= hdev
->gate2pipe
[gate
];
267 if (pipe
== NFC_HCI_INVALID_PIPE
)
268 return -EADDRNOTAVAIL
;
270 r
= nfc_hci_close_pipe(hdev
, pipe
);
274 if (pipe
!= NFC_HCI_LINK_MGMT_PIPE
&& pipe
!= NFC_HCI_ADMIN_PIPE
) {
275 r
= nfc_hci_delete_pipe(hdev
, pipe
);
280 hdev
->gate2pipe
[gate
] = NFC_HCI_INVALID_PIPE
;
284 EXPORT_SYMBOL(nfc_hci_disconnect_gate
);
286 int nfc_hci_disconnect_all_gates(struct nfc_hci_dev
*hdev
)
292 r
= nfc_hci_clear_all_pipes(hdev
);
296 memset(hdev
->gate2pipe
, NFC_HCI_INVALID_PIPE
, sizeof(hdev
->gate2pipe
));
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
,
305 bool pipe_created
= false;
310 if (hdev
->gate2pipe
[dest_gate
] != NFC_HCI_INVALID_PIPE
)
313 if (pipe
!= NFC_HCI_INVALID_PIPE
)
317 case NFC_HCI_LINK_MGMT_GATE
:
318 pipe
= NFC_HCI_LINK_MGMT_PIPE
;
320 case NFC_HCI_ADMIN_GATE
:
321 pipe
= NFC_HCI_ADMIN_PIPE
;
324 pipe
= nfc_hci_create_pipe(hdev
, dest_host
, dest_gate
, &r
);
325 if (pipe
== NFC_HCI_INVALID_PIPE
)
331 r
= nfc_hci_open_pipe(hdev
, pipe
);
334 if (nfc_hci_delete_pipe(hdev
, pipe
) < 0) {
335 /* TODO: Cannot clean by deleting pipe...
336 * -> inconsistent state */
342 hdev
->gate2pipe
[dest_gate
] = pipe
;
346 EXPORT_SYMBOL(nfc_hci_connect_gate
);