1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012 Intel Corporation. All rights reserved.
6 #define pr_fmt(fmt) "hci: %s: " fmt, __func__
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/module.h>
13 #include <net/nfc/hci.h>
19 static int nfc_hci_execute_cmd_async(struct nfc_hci_dev
*hdev
, u8 pipe
, u8 cmd
,
20 const u8
*param
, size_t param_len
,
21 data_exchange_cb_t cb
, void *cb_context
)
23 pr_debug("exec cmd async through pipe=%d, cmd=%d, plen=%zd\n", pipe
,
26 /* TODO: Define hci cmd execution delay. Should it be the same
29 return nfc_hci_hcp_message_tx(hdev
, pipe
, NFC_HCI_HCP_COMMAND
, cmd
,
30 param
, param_len
, cb
, cb_context
, MAX_FWI
);
34 * HCI command execution completion callback.
35 * err will be a standard linux error (may be converted from HCI response)
36 * skb contains the response data and must be disposed, or may be NULL if
39 static void nfc_hci_execute_cb(void *context
, struct sk_buff
*skb
, int err
)
41 struct hcp_exec_waiter
*hcp_ew
= (struct hcp_exec_waiter
*)context
;
43 pr_debug("HCI Cmd completed with result=%d\n", err
);
45 hcp_ew
->exec_result
= err
;
46 if (hcp_ew
->exec_result
== 0)
47 hcp_ew
->result_skb
= skb
;
50 hcp_ew
->exec_complete
= true;
55 static int nfc_hci_execute_cmd(struct nfc_hci_dev
*hdev
, u8 pipe
, u8 cmd
,
56 const u8
*param
, size_t param_len
,
59 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(ew_wq
);
60 struct hcp_exec_waiter hcp_ew
;
62 hcp_ew
.exec_complete
= false;
63 hcp_ew
.result_skb
= NULL
;
65 pr_debug("exec cmd sync through pipe=%d, cmd=%d, plen=%zd\n", pipe
,
68 /* TODO: Define hci cmd execution delay. Should it be the same
71 hcp_ew
.exec_result
= nfc_hci_hcp_message_tx(hdev
, pipe
,
72 NFC_HCI_HCP_COMMAND
, cmd
,
74 nfc_hci_execute_cb
, &hcp_ew
,
76 if (hcp_ew
.exec_result
< 0)
77 return hcp_ew
.exec_result
;
79 wait_event(ew_wq
, hcp_ew
.exec_complete
== true);
81 if (hcp_ew
.exec_result
== 0) {
83 *skb
= hcp_ew
.result_skb
;
85 kfree_skb(hcp_ew
.result_skb
);
88 return hcp_ew
.exec_result
;
91 int nfc_hci_send_event(struct nfc_hci_dev
*hdev
, u8 gate
, u8 event
,
92 const u8
*param
, size_t param_len
)
96 pr_debug("%d to gate %d\n", event
, gate
);
98 pipe
= hdev
->gate2pipe
[gate
];
99 if (pipe
== NFC_HCI_INVALID_PIPE
)
100 return -EADDRNOTAVAIL
;
102 return nfc_hci_hcp_message_tx(hdev
, pipe
, NFC_HCI_HCP_EVENT
, event
,
103 param
, param_len
, NULL
, NULL
, 0);
105 EXPORT_SYMBOL(nfc_hci_send_event
);
108 * Execute an hci command sent to gate.
109 * skb will contain response data if success. skb can be NULL if you are not
110 * interested by the response.
112 int nfc_hci_send_cmd(struct nfc_hci_dev
*hdev
, u8 gate
, u8 cmd
,
113 const u8
*param
, size_t param_len
, struct sk_buff
**skb
)
117 pipe
= hdev
->gate2pipe
[gate
];
118 if (pipe
== NFC_HCI_INVALID_PIPE
)
119 return -EADDRNOTAVAIL
;
121 return nfc_hci_execute_cmd(hdev
, pipe
, cmd
, param
, param_len
, skb
);
123 EXPORT_SYMBOL(nfc_hci_send_cmd
);
125 int nfc_hci_send_cmd_async(struct nfc_hci_dev
*hdev
, u8 gate
, u8 cmd
,
126 const u8
*param
, size_t param_len
,
127 data_exchange_cb_t cb
, void *cb_context
)
131 pipe
= hdev
->gate2pipe
[gate
];
132 if (pipe
== NFC_HCI_INVALID_PIPE
)
133 return -EADDRNOTAVAIL
;
135 return nfc_hci_execute_cmd_async(hdev
, pipe
, cmd
, param
, param_len
,
138 EXPORT_SYMBOL(nfc_hci_send_cmd_async
);
140 int nfc_hci_set_param(struct nfc_hci_dev
*hdev
, u8 gate
, u8 idx
,
141 const u8
*param
, size_t param_len
)
146 /* TODO ELa: reg idx must be inserted before param, but we don't want
147 * to ask the caller to do it to keep a simpler API.
148 * For now, just create a new temporary param buffer. This is far from
149 * optimal though, and the plan is to modify APIs to pass idx down to
150 * nfc_hci_hcp_message_tx where the frame is actually built, thereby
151 * eliminating the need for the temp allocation-copy here.
154 pr_debug("idx=%d to gate %d\n", idx
, gate
);
156 tmp
= kmalloc(1 + param_len
, GFP_KERNEL
);
161 memcpy(tmp
+ 1, param
, param_len
);
163 r
= nfc_hci_send_cmd(hdev
, gate
, NFC_HCI_ANY_SET_PARAMETER
,
164 tmp
, param_len
+ 1, NULL
);
170 EXPORT_SYMBOL(nfc_hci_set_param
);
172 int nfc_hci_get_param(struct nfc_hci_dev
*hdev
, u8 gate
, u8 idx
,
173 struct sk_buff
**skb
)
175 pr_debug("gate=%d regidx=%d\n", gate
, idx
);
177 return nfc_hci_send_cmd(hdev
, gate
, NFC_HCI_ANY_GET_PARAMETER
,
180 EXPORT_SYMBOL(nfc_hci_get_param
);
182 static int nfc_hci_open_pipe(struct nfc_hci_dev
*hdev
, u8 pipe
)
187 pr_debug("pipe=%d\n", pipe
);
189 r
= nfc_hci_execute_cmd(hdev
, pipe
, NFC_HCI_ANY_OPEN_PIPE
,
192 /* dest host other than host controller will send
193 * number of pipes already open on this gate before
194 * execution. The number can be found in skb->data[0]
202 static int nfc_hci_close_pipe(struct nfc_hci_dev
*hdev
, u8 pipe
)
204 return nfc_hci_execute_cmd(hdev
, pipe
, NFC_HCI_ANY_CLOSE_PIPE
,
208 static u8
nfc_hci_create_pipe(struct nfc_hci_dev
*hdev
, u8 dest_host
,
209 u8 dest_gate
, int *result
)
212 struct hci_create_pipe_params params
;
213 struct hci_create_pipe_resp
*resp
;
216 pr_debug("gate=%d\n", dest_gate
);
218 params
.src_gate
= NFC_HCI_ADMIN_GATE
;
219 params
.dest_host
= dest_host
;
220 params
.dest_gate
= dest_gate
;
222 *result
= nfc_hci_execute_cmd(hdev
, NFC_HCI_ADMIN_PIPE
,
223 NFC_HCI_ADM_CREATE_PIPE
,
224 (u8
*) ¶ms
, sizeof(params
), &skb
);
226 return NFC_HCI_INVALID_PIPE
;
228 resp
= (struct hci_create_pipe_resp
*)skb
->data
;
232 pr_debug("pipe created=%d\n", pipe
);
237 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
)
246 size_t param_len
= 2;
248 /* TODO: Find out what the identity reference data is
249 * and fill param with it. HCI spec 6.1.3.5 */
251 if (test_bit(NFC_HCI_QUIRK_SHORT_CLEAR
, &hdev
->quirks
))
254 return nfc_hci_execute_cmd(hdev
, NFC_HCI_ADMIN_PIPE
,
255 NFC_HCI_ADM_CLEAR_ALL_PIPE
, param
, param_len
,
259 int nfc_hci_disconnect_gate(struct nfc_hci_dev
*hdev
, u8 gate
)
262 u8 pipe
= hdev
->gate2pipe
[gate
];
264 if (pipe
== NFC_HCI_INVALID_PIPE
)
265 return -EADDRNOTAVAIL
;
267 r
= nfc_hci_close_pipe(hdev
, pipe
);
271 if (pipe
!= NFC_HCI_LINK_MGMT_PIPE
&& pipe
!= NFC_HCI_ADMIN_PIPE
) {
272 r
= nfc_hci_delete_pipe(hdev
, pipe
);
277 hdev
->gate2pipe
[gate
] = NFC_HCI_INVALID_PIPE
;
281 EXPORT_SYMBOL(nfc_hci_disconnect_gate
);
283 int nfc_hci_disconnect_all_gates(struct nfc_hci_dev
*hdev
)
287 r
= nfc_hci_clear_all_pipes(hdev
);
291 nfc_hci_reset_pipes(hdev
);
295 EXPORT_SYMBOL(nfc_hci_disconnect_all_gates
);
297 int nfc_hci_connect_gate(struct nfc_hci_dev
*hdev
, u8 dest_host
, u8 dest_gate
,
300 bool pipe_created
= false;
303 if (pipe
== NFC_HCI_DO_NOT_CREATE_PIPE
)
306 if (hdev
->gate2pipe
[dest_gate
] != NFC_HCI_INVALID_PIPE
)
309 if (pipe
!= NFC_HCI_INVALID_PIPE
)
313 case NFC_HCI_LINK_MGMT_GATE
:
314 pipe
= NFC_HCI_LINK_MGMT_PIPE
;
316 case NFC_HCI_ADMIN_GATE
:
317 pipe
= NFC_HCI_ADMIN_PIPE
;
320 pipe
= nfc_hci_create_pipe(hdev
, dest_host
, dest_gate
, &r
);
321 if (pipe
== NFC_HCI_INVALID_PIPE
)
328 r
= nfc_hci_open_pipe(hdev
, pipe
);
331 if (nfc_hci_delete_pipe(hdev
, pipe
) < 0) {
332 /* TODO: Cannot clean by deleting pipe...
333 * -> inconsistent state */
338 hdev
->pipes
[pipe
].gate
= dest_gate
;
339 hdev
->pipes
[pipe
].dest_host
= dest_host
;
340 hdev
->gate2pipe
[dest_gate
] = pipe
;
344 EXPORT_SYMBOL(nfc_hci_connect_gate
);