blob: 19518a231e5715bba0d36fdeef5cf69d9dfe597a [file] [log] [blame]
Ilan Elias6a2968a2011-09-18 11:19:35 +03001/*
2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
Julien Lefrique772dccf2014-10-21 16:52:44 +02006 * Copyright (C) 2014 Marvell International Ltd.
Ilan Elias6a2968a2011-09-18 11:19:35 +03007 *
8 * Written by Ilan Elias <ilane@ti.com>
9 *
10 * Acknowledgements:
11 * This file is based on hci_core.c, which was written
12 * by Maxim Krasnyansky.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2
16 * as published by the Free Software Foundation
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
Jeff Kirsher98b32de2013-12-06 08:56:16 -080024 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Ilan Elias6a2968a2011-09-18 11:19:35 +030025 *
26 */
27
Samuel Ortiz52858b52011-12-14 16:43:05 +010028#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080029
Dave Jones8a70e7f2012-07-12 19:17:34 +020030#include <linux/module.h>
Samuel Ortizb6355e92015-06-06 13:16:37 +020031#include <linux/kernel.h>
Ilan Elias6a2968a2011-09-18 11:19:35 +030032#include <linux/types.h>
33#include <linux/workqueue.h>
34#include <linux/completion.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040035#include <linux/export.h>
Ilan Elias6a2968a2011-09-18 11:19:35 +030036#include <linux/sched.h>
37#include <linux/bitops.h>
38#include <linux/skbuff.h>
39
40#include "../nfc.h"
41#include <net/nfc/nci.h>
42#include <net/nfc/nci_core.h>
43#include <linux/nfc.h>
44
Christophe Ricardb16ae712015-02-03 19:48:05 +010045struct core_conn_create_data {
46 int length;
47 struct nci_core_conn_create_cmd *cmd;
48};
49
Ilan Elias6a2968a2011-09-18 11:19:35 +030050static void nci_cmd_work(struct work_struct *work);
51static void nci_rx_work(struct work_struct *work);
52static void nci_tx_work(struct work_struct *work);
53
Christophe Ricard4aeee682015-02-01 22:26:08 +010054struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
55 int conn_id)
56{
57 struct nci_conn_info *conn_info;
58
59 list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
60 if (conn_info->conn_id == conn_id)
61 return conn_info;
62 }
63
64 return NULL;
65}
66
Christophe Ricard9b8d1a42016-04-30 09:12:51 +020067int nci_get_conn_info_by_dest_type_params(struct nci_dev *ndev, u8 dest_type,
68 struct dest_spec_params *params)
Robert Dolca85b9ce92015-10-22 12:11:41 +030069{
70 struct nci_conn_info *conn_info;
71
72 list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
Christophe Ricard9b8d1a42016-04-30 09:12:51 +020073 if (conn_info->dest_type == dest_type) {
74 if (!params)
75 return conn_info->conn_id;
76 if (conn_info) {
77 if (params->id == conn_info->dest_params->id &&
78 params->protocol == conn_info->dest_params->protocol)
79 return conn_info->conn_id;
80 }
81 }
Robert Dolca85b9ce92015-10-22 12:11:41 +030082 }
83
84 return -EINVAL;
85}
Christophe Ricard9b8d1a42016-04-30 09:12:51 +020086EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params);
Robert Dolca85b9ce92015-10-22 12:11:41 +030087
Ilan Elias6a2968a2011-09-18 11:19:35 +030088/* ---- NCI requests ---- */
89
90void nci_req_complete(struct nci_dev *ndev, int result)
91{
92 if (ndev->req_status == NCI_REQ_PEND) {
93 ndev->req_result = result;
94 ndev->req_status = NCI_REQ_DONE;
95 complete(&ndev->req_completion);
96 }
97}
Samuel Ortiz2df7f8c2015-06-10 12:50:22 +020098EXPORT_SYMBOL(nci_req_complete);
Ilan Elias6a2968a2011-09-18 11:19:35 +030099
100static void nci_req_cancel(struct nci_dev *ndev, int err)
101{
102 if (ndev->req_status == NCI_REQ_PEND) {
103 ndev->req_result = err;
104 ndev->req_status = NCI_REQ_CANCELED;
105 complete(&ndev->req_completion);
106 }
107}
108
109/* Execute request and wait for completion. */
110static int __nci_request(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100111 void (*req)(struct nci_dev *ndev, unsigned long opt),
112 unsigned long opt, __u32 timeout)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300113{
114 int rc = 0;
Dan Carpenterf8c141c2011-12-09 09:35:39 +0300115 long completion_rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300116
117 ndev->req_status = NCI_REQ_PEND;
118
Axel Lin9bec44b2014-02-13 13:25:48 +0800119 reinit_completion(&ndev->req_completion);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300120 req(ndev, opt);
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100121 completion_rc =
122 wait_for_completion_interruptible_timeout(&ndev->req_completion,
123 timeout);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300124
Joe Perches20c239c2011-11-29 11:37:33 -0800125 pr_debug("wait_for_completion return %ld\n", completion_rc);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300126
127 if (completion_rc > 0) {
128 switch (ndev->req_status) {
129 case NCI_REQ_DONE:
130 rc = nci_to_errno(ndev->req_result);
131 break;
132
133 case NCI_REQ_CANCELED:
134 rc = -ndev->req_result;
135 break;
136
137 default:
138 rc = -ETIMEDOUT;
139 break;
140 }
141 } else {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800142 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
143 completion_rc);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300144
145 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
146 }
147
148 ndev->req_status = ndev->req_result = 0;
149
150 return rc;
151}
152
Christophe Ricard11f54f22015-02-01 22:26:14 +0100153inline int nci_request(struct nci_dev *ndev,
154 void (*req)(struct nci_dev *ndev,
155 unsigned long opt),
156 unsigned long opt, __u32 timeout)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300157{
158 int rc;
159
Ilan Elias6a2968a2011-09-18 11:19:35 +0300160 /* Serialize all requests */
161 mutex_lock(&ndev->req_lock);
Lin Ma4a59a362021-11-15 22:56:00 +0800162 /* check the state after obtaing the lock against any races
163 * from nci_close_device when the device gets removed.
164 */
165 if (test_bit(NCI_UP, &ndev->flags))
166 rc = __nci_request(ndev, req, opt, timeout);
167 else
168 rc = -ENETDOWN;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300169 mutex_unlock(&ndev->req_lock);
170
171 return rc;
172}
173
174static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
175{
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200176 struct nci_core_reset_cmd cmd;
177
178 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
179 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300180}
181
182static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
183{
184 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
185}
186
187static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
188{
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300189 struct nci_rf_disc_map_cmd cmd;
190 struct disc_map_config *cfg = cmd.mapping_configs;
191 __u8 *num = &cmd.num_mapping_configs;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300192 int i;
193
Ilan Elias6a2968a2011-09-18 11:19:35 +0300194 /* set rf mapping configurations */
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300195 *num = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300196
197 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
198 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
199 if (ndev->supported_rf_interfaces[i] ==
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100200 NCI_RF_INTERFACE_ISO_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300201 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200202 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
203 NCI_DISC_MAP_MODE_LISTEN;
204 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300205 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300206 } else if (ndev->supported_rf_interfaces[i] ==
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100207 NCI_RF_INTERFACE_NFC_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300208 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200209 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
210 NCI_DISC_MAP_MODE_LISTEN;
211 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300212 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300213 }
214
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300215 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300216 break;
217 }
218
219 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100220 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300221}
222
Ilan Elias7e035232012-08-15 11:46:22 +0300223struct nci_set_config_param {
224 __u8 id;
225 size_t len;
226 __u8 *val;
227};
228
229static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
230{
231 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
232 struct nci_core_set_config_cmd cmd;
233
234 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
235
236 cmd.num_params = 1;
237 cmd.param.id = param->id;
238 cmd.param.len = param->len;
239 memcpy(cmd.param.val, param->val, param->len);
240
241 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
242}
243
Julien Lefrique772dccf2014-10-21 16:52:44 +0200244struct nci_rf_discover_param {
245 __u32 im_protocols;
246 __u32 tm_protocols;
247};
248
Ilan Elias6a2968a2011-09-18 11:19:35 +0300249static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
250{
Julien Lefrique772dccf2014-10-21 16:52:44 +0200251 struct nci_rf_discover_param *param =
252 (struct nci_rf_discover_param *)opt;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300253 struct nci_rf_disc_cmd cmd;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300254
255 cmd.num_disc_configs = 0;
256
257 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200258 (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
259 param->im_protocols & NFC_PROTO_MIFARE_MASK ||
260 param->im_protocols & NFC_PROTO_ISO14443_MASK ||
261 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200262 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100263 NCI_NFC_A_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300264 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
265 cmd.num_disc_configs++;
266 }
267
268 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200269 (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200270 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100271 NCI_NFC_B_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300272 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
273 cmd.num_disc_configs++;
274 }
275
276 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200277 (param->im_protocols & NFC_PROTO_FELICA_MASK ||
278 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200279 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100280 NCI_NFC_F_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300281 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
282 cmd.num_disc_configs++;
283 }
284
Vincent Cuissardcfdbeea2014-07-22 19:48:38 +0200285 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200286 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
Vincent Cuissardcfdbeea2014-07-22 19:48:38 +0200287 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
288 NCI_NFC_V_PASSIVE_POLL_MODE;
289 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
290 cmd.num_disc_configs++;
291 }
292
Julien Lefrique772dccf2014-10-21 16:52:44 +0200293 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
294 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
295 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
296 NCI_NFC_A_PASSIVE_LISTEN_MODE;
297 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
298 cmd.num_disc_configs++;
299 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
300 NCI_NFC_F_PASSIVE_LISTEN_MODE;
301 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
302 cmd.num_disc_configs++;
303 }
304
Ilan Elias6a2968a2011-09-18 11:19:35 +0300305 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100306 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
307 &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300308}
309
Ilan Elias019c4fb2012-01-18 13:16:14 +0200310struct nci_rf_discover_select_param {
311 __u8 rf_discovery_id;
312 __u8 rf_protocol;
313};
314
315static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
316{
317 struct nci_rf_discover_select_param *param =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100318 (struct nci_rf_discover_select_param *)opt;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200319 struct nci_rf_discover_select_cmd cmd;
320
321 cmd.rf_discovery_id = param->rf_discovery_id;
322 cmd.rf_protocol = param->rf_protocol;
323
324 switch (cmd.rf_protocol) {
325 case NCI_RF_PROTOCOL_ISO_DEP:
326 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
327 break;
328
329 case NCI_RF_PROTOCOL_NFC_DEP:
330 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
331 break;
332
333 default:
334 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
335 break;
336 }
337
338 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100339 sizeof(struct nci_rf_discover_select_cmd), &cmd);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200340}
341
Ilan Elias6a2968a2011-09-18 11:19:35 +0300342static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
343{
344 struct nci_rf_deactivate_cmd cmd;
345
Christophe Ricard9295b5b2014-12-02 21:27:49 +0100346 cmd.type = opt;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300347
348 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100349 sizeof(struct nci_rf_deactivate_cmd), &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300350}
351
Robert Dolca7bc48242015-10-22 12:11:37 +0300352struct nci_cmd_param {
Christophe Ricard759afb82015-06-06 13:16:41 +0200353 __u16 opcode;
354 size_t len;
355 __u8 *payload;
356};
357
Robert Dolca7bc48242015-10-22 12:11:37 +0300358static void nci_generic_req(struct nci_dev *ndev, unsigned long opt)
Christophe Ricard759afb82015-06-06 13:16:41 +0200359{
Robert Dolca7bc48242015-10-22 12:11:37 +0300360 struct nci_cmd_param *param =
361 (struct nci_cmd_param *)opt;
Christophe Ricard759afb82015-06-06 13:16:41 +0200362
363 nci_send_cmd(ndev, param->opcode, param->len, param->payload);
364}
365
366int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, __u8 *payload)
367{
Robert Dolca7bc48242015-10-22 12:11:37 +0300368 struct nci_cmd_param param;
Christophe Ricard759afb82015-06-06 13:16:41 +0200369
370 param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
371 param.len = len;
372 param.payload = payload;
373
Robert Dolca7bc48242015-10-22 12:11:37 +0300374 return __nci_request(ndev, nci_generic_req, (unsigned long)&param,
Christophe Ricard759afb82015-06-06 13:16:41 +0200375 msecs_to_jiffies(NCI_CMD_TIMEOUT));
376}
377EXPORT_SYMBOL(nci_prop_cmd);
378
Robert Dolca7bc48242015-10-22 12:11:37 +0300379int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len, __u8 *payload)
380{
381 struct nci_cmd_param param;
382
383 param.opcode = opcode;
384 param.len = len;
385 param.payload = payload;
386
387 return __nci_request(ndev, nci_generic_req, (unsigned long)&param,
388 msecs_to_jiffies(NCI_CMD_TIMEOUT));
389}
390EXPORT_SYMBOL(nci_core_cmd);
391
Robert Baldyga025a0cb2015-08-20 17:26:01 +0200392int nci_core_reset(struct nci_dev *ndev)
393{
394 return __nci_request(ndev, nci_reset_req, 0,
395 msecs_to_jiffies(NCI_RESET_TIMEOUT));
396}
397EXPORT_SYMBOL(nci_core_reset);
398
399int nci_core_init(struct nci_dev *ndev)
400{
401 return __nci_request(ndev, nci_init_req, 0,
402 msecs_to_jiffies(NCI_INIT_TIMEOUT));
403}
404EXPORT_SYMBOL(nci_core_init);
405
Christophe Ricard1c538552016-04-30 09:12:52 +0200406struct nci_loopback_data {
407 u8 conn_id;
408 struct sk_buff *data;
409};
410
411static void nci_send_data_req(struct nci_dev *ndev, unsigned long opt)
412{
413 struct nci_loopback_data *data = (struct nci_loopback_data *)opt;
414
415 nci_send_data(ndev, data->conn_id, data->data);
416}
417
418static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
419{
420 struct nci_dev *ndev = (struct nci_dev *)context;
421 struct nci_conn_info *conn_info;
422
423 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
424 if (!conn_info) {
425 nci_req_complete(ndev, NCI_STATUS_REJECTED);
426 return;
427 }
428
429 conn_info->rx_skb = skb;
430
431 nci_req_complete(ndev, NCI_STATUS_OK);
432}
433
434int nci_nfcc_loopback(struct nci_dev *ndev, void *data, size_t data_len,
435 struct sk_buff **resp)
436{
437 int r;
438 struct nci_loopback_data loopback_data;
439 struct nci_conn_info *conn_info;
440 struct sk_buff *skb;
441 int conn_id = nci_get_conn_info_by_dest_type_params(ndev,
442 NCI_DESTINATION_NFCC_LOOPBACK, NULL);
443
444 if (conn_id < 0) {
445 r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCC_LOOPBACK,
446 0, 0, NULL);
447 if (r != NCI_STATUS_OK)
448 return r;
449
450 conn_id = nci_get_conn_info_by_dest_type_params(ndev,
451 NCI_DESTINATION_NFCC_LOOPBACK,
452 NULL);
453 }
454
455 conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
456 if (!conn_info)
457 return -EPROTO;
458
459 /* store cb and context to be used on receiving data */
460 conn_info->data_exchange_cb = nci_nfcc_loopback_cb;
461 conn_info->data_exchange_cb_context = ndev;
462
463 skb = nci_skb_alloc(ndev, NCI_DATA_HDR_SIZE + data_len, GFP_KERNEL);
464 if (!skb)
465 return -ENOMEM;
466
467 skb_reserve(skb, NCI_DATA_HDR_SIZE);
468 memcpy(skb_put(skb, data_len), data, data_len);
469
470 loopback_data.conn_id = conn_id;
471 loopback_data.data = skb;
472
473 ndev->cur_conn_id = conn_id;
474 r = nci_request(ndev, nci_send_data_req, (unsigned long)&loopback_data,
475 msecs_to_jiffies(NCI_DATA_TIMEOUT));
476 if (r == NCI_STATUS_OK && resp)
477 *resp = conn_info->rx_skb;
478
479 return r;
480}
481EXPORT_SYMBOL(nci_nfcc_loopback);
482
Ilan Elias6a2968a2011-09-18 11:19:35 +0300483static int nci_open_device(struct nci_dev *ndev)
484{
485 int rc = 0;
486
487 mutex_lock(&ndev->req_lock);
488
489 if (test_bit(NCI_UP, &ndev->flags)) {
490 rc = -EALREADY;
491 goto done;
492 }
493
494 if (ndev->ops->open(ndev)) {
495 rc = -EIO;
496 goto done;
497 }
498
499 atomic_set(&ndev->cmd_cnt, 1);
500
501 set_bit(NCI_INIT, &ndev->flags);
502
Christophe Ricardc39daee2015-06-06 13:16:40 +0200503 if (ndev->ops->init)
504 rc = ndev->ops->init(ndev);
505
506 if (!rc) {
507 rc = __nci_request(ndev, nci_reset_req, 0,
508 msecs_to_jiffies(NCI_RESET_TIMEOUT));
509 }
Ilan Elias6a2968a2011-09-18 11:19:35 +0300510
Christophe Ricard81859ab2015-06-06 13:16:39 +0200511 if (!rc && ndev->ops->setup) {
512 rc = ndev->ops->setup(ndev);
513 }
Amitkumar Karwar86e85862014-01-06 12:58:17 -0800514
Ilan Elias6a2968a2011-09-18 11:19:35 +0300515 if (!rc) {
516 rc = __nci_request(ndev, nci_init_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100517 msecs_to_jiffies(NCI_INIT_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300518 }
519
Robert Dolcae4dbd622015-10-22 12:11:36 +0300520 if (!rc && ndev->ops->post_setup)
Robert Baldygafdf79bd2015-08-20 17:26:00 +0200521 rc = ndev->ops->post_setup(ndev);
Robert Baldygafdf79bd2015-08-20 17:26:00 +0200522
Ilan Elias6a2968a2011-09-18 11:19:35 +0300523 if (!rc) {
524 rc = __nci_request(ndev, nci_init_complete_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100525 msecs_to_jiffies(NCI_INIT_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300526 }
527
528 clear_bit(NCI_INIT, &ndev->flags);
529
530 if (!rc) {
531 set_bit(NCI_UP, &ndev->flags);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200532 nci_clear_target_list(ndev);
Ilan Elias8939e472012-01-18 13:16:12 +0200533 atomic_set(&ndev->state, NCI_IDLE);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300534 } else {
535 /* Init failed, cleanup */
536 skb_queue_purge(&ndev->cmd_q);
537 skb_queue_purge(&ndev->rx_q);
538 skb_queue_purge(&ndev->tx_q);
539
540 ndev->ops->close(ndev);
541 ndev->flags = 0;
542 }
543
544done:
545 mutex_unlock(&ndev->req_lock);
546 return rc;
547}
548
549static int nci_close_device(struct nci_dev *ndev)
550{
551 nci_req_cancel(ndev, ENODEV);
552 mutex_lock(&ndev->req_lock);
553
554 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
555 del_timer_sync(&ndev->cmd_timer);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200556 del_timer_sync(&ndev->data_timer);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300557 mutex_unlock(&ndev->req_lock);
558 return 0;
559 }
560
561 /* Drop RX and TX queues */
562 skb_queue_purge(&ndev->rx_q);
563 skb_queue_purge(&ndev->tx_q);
564
565 /* Flush RX and TX wq */
566 flush_workqueue(ndev->rx_wq);
567 flush_workqueue(ndev->tx_wq);
568
569 /* Reset device */
570 skb_queue_purge(&ndev->cmd_q);
571 atomic_set(&ndev->cmd_cnt, 1);
572
573 set_bit(NCI_INIT, &ndev->flags);
574 __nci_request(ndev, nci_reset_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100575 msecs_to_jiffies(NCI_RESET_TIMEOUT));
Christophe Ricard0e70cba72015-06-06 13:16:48 +0200576
577 /* After this point our queues are empty
578 * and no works are scheduled.
579 */
580 ndev->ops->close(ndev);
581
Ilan Elias6a2968a2011-09-18 11:19:35 +0300582 clear_bit(NCI_INIT, &ndev->flags);
583
Amitkumar Karwarfa9be5f2013-12-23 14:15:13 -0800584 del_timer_sync(&ndev->cmd_timer);
585
Ilan Elias6a2968a2011-09-18 11:19:35 +0300586 /* Flush cmd wq */
587 flush_workqueue(ndev->cmd_wq);
588
Ilan Elias6a2968a2011-09-18 11:19:35 +0300589 /* Clear flags */
590 ndev->flags = 0;
591
592 mutex_unlock(&ndev->req_lock);
593
594 return 0;
595}
596
597/* NCI command timer function */
598static void nci_cmd_timer(unsigned long arg)
599{
600 struct nci_dev *ndev = (void *) arg;
601
Ilan Elias6a2968a2011-09-18 11:19:35 +0300602 atomic_set(&ndev->cmd_cnt, 1);
603 queue_work(ndev->cmd_wq, &ndev->cmd_work);
604}
605
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200606/* NCI data exchange timer function */
607static void nci_data_timer(unsigned long arg)
608{
609 struct nci_dev *ndev = (void *) arg;
610
611 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
612 queue_work(ndev->rx_wq, &ndev->rx_work);
613}
614
Ilan Elias6a2968a2011-09-18 11:19:35 +0300615static int nci_dev_up(struct nfc_dev *nfc_dev)
616{
617 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
618
Ilan Elias6a2968a2011-09-18 11:19:35 +0300619 return nci_open_device(ndev);
620}
621
622static int nci_dev_down(struct nfc_dev *nfc_dev)
623{
624 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
625
Ilan Elias6a2968a2011-09-18 11:19:35 +0300626 return nci_close_device(ndev);
627}
628
Amitkumar Karwar22c15bf2014-01-06 12:58:18 -0800629int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
630{
631 struct nci_set_config_param param;
632
633 if (!val || !len)
634 return 0;
635
636 param.id = id;
637 param.len = len;
638 param.val = val;
639
640 return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
641 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
642}
643EXPORT_SYMBOL(nci_set_config);
644
Christophe Ricardaf9c8aa2015-02-01 22:26:10 +0100645static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
646{
647 struct nci_nfcee_discover_cmd cmd;
648 __u8 action = opt;
649
650 cmd.discovery_action = action;
651
652 nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
653}
654
655int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
656{
Samuel Ortiz21d19f82015-10-03 05:02:28 +0200657 return __nci_request(ndev, nci_nfcee_discover_req, action,
Christophe Ricardaf9c8aa2015-02-01 22:26:10 +0100658 msecs_to_jiffies(NCI_CMD_TIMEOUT));
659}
660EXPORT_SYMBOL(nci_nfcee_discover);
661
Christophe Ricardf7f793f2015-02-01 22:26:11 +0100662static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
663{
664 struct nci_nfcee_mode_set_cmd *cmd =
665 (struct nci_nfcee_mode_set_cmd *)opt;
666
667 nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
668 sizeof(struct nci_nfcee_mode_set_cmd), cmd);
669}
670
671int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
672{
673 struct nci_nfcee_mode_set_cmd cmd;
674
675 cmd.nfcee_id = nfcee_id;
676 cmd.nfcee_mode = nfcee_mode;
677
Samuel Ortiz21d19f82015-10-03 05:02:28 +0200678 return __nci_request(ndev, nci_nfcee_mode_set_req,
679 (unsigned long)&cmd,
680 msecs_to_jiffies(NCI_CMD_TIMEOUT));
Christophe Ricardf7f793f2015-02-01 22:26:11 +0100681}
682EXPORT_SYMBOL(nci_nfcee_mode_set);
683
Christophe Ricard736bb952015-02-01 22:26:12 +0100684static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
685{
Christophe Ricardb16ae712015-02-03 19:48:05 +0100686 struct core_conn_create_data *data =
687 (struct core_conn_create_data *)opt;
Christophe Ricard736bb952015-02-01 22:26:12 +0100688
Christophe Ricardb16ae712015-02-03 19:48:05 +0100689 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
Christophe Ricard736bb952015-02-01 22:26:12 +0100690}
691
Christophe Ricardb16ae712015-02-03 19:48:05 +0100692int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
693 u8 number_destination_params,
694 size_t params_len,
Christophe Ricard736bb952015-02-01 22:26:12 +0100695 struct core_conn_create_dest_spec_params *params)
696{
Christophe Ricardb16ae712015-02-03 19:48:05 +0100697 int r;
698 struct nci_core_conn_create_cmd *cmd;
699 struct core_conn_create_data data;
700
701 data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
702 cmd = kzalloc(data.length, GFP_KERNEL);
703 if (!cmd)
704 return -ENOMEM;
705
706 cmd->destination_type = destination_type;
707 cmd->number_destination_params = number_destination_params;
Christophe Ricardb16ae712015-02-03 19:48:05 +0100708
709 data.cmd = cmd;
Robert Dolcacaa575a82015-10-22 12:11:40 +0300710
Christophe Ricard18836022016-04-30 09:12:49 +0200711 if (params) {
712 memcpy(cmd->params, params, params_len);
713 if (params->length > 0)
Christophe Ricard9b8d1a42016-04-30 09:12:51 +0200714 memcpy(&ndev->cur_params,
715 &params->value[DEST_SPEC_PARAMS_ID_INDEX],
716 sizeof(struct dest_spec_params));
Christophe Ricard18836022016-04-30 09:12:49 +0200717 else
Christophe Ricard9b8d1a42016-04-30 09:12:51 +0200718 ndev->cur_params.id = 0;
Christophe Ricard18836022016-04-30 09:12:49 +0200719 } else {
Christophe Ricard9b8d1a42016-04-30 09:12:51 +0200720 ndev->cur_params.id = 0;
Christophe Ricard18836022016-04-30 09:12:49 +0200721 }
Christophe Ricard9b8d1a42016-04-30 09:12:51 +0200722 ndev->cur_dest_type = destination_type;
Christophe Ricardb16ae712015-02-03 19:48:05 +0100723
Christophe Ricard18836022016-04-30 09:12:49 +0200724 r = __nci_request(ndev, nci_core_conn_create_req, (unsigned long)&data,
Christophe Ricardb16ae712015-02-03 19:48:05 +0100725 msecs_to_jiffies(NCI_CMD_TIMEOUT));
726 kfree(cmd);
727 return r;
Christophe Ricard736bb952015-02-01 22:26:12 +0100728}
729EXPORT_SYMBOL(nci_core_conn_create);
730
731static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
732{
733 __u8 conn_id = opt;
734
735 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
736}
737
738int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
739{
Christophe Ricardde5ea852016-04-30 09:12:50 +0200740 ndev->cur_conn_id = conn_id;
Samuel Ortiz21d19f82015-10-03 05:02:28 +0200741 return __nci_request(ndev, nci_core_conn_close_req, conn_id,
742 msecs_to_jiffies(NCI_CMD_TIMEOUT));
Christophe Ricard736bb952015-02-01 22:26:12 +0100743}
744EXPORT_SYMBOL(nci_core_conn_close);
745
Ilan Elias7e035232012-08-15 11:46:22 +0300746static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
747{
748 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
749 struct nci_set_config_param param;
Julien Lefrique529ee062014-10-21 16:52:47 +0200750 int rc;
Ilan Elias7e035232012-08-15 11:46:22 +0300751
752 param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
753 if ((param.val == NULL) || (param.len == 0))
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200754 return 0;
Ilan Elias7e035232012-08-15 11:46:22 +0300755
Szymon Janc460d8f92012-10-04 15:15:45 +0200756 if (param.len > NFC_MAX_GT_LEN)
Ilan Elias7e035232012-08-15 11:46:22 +0300757 return -EINVAL;
758
Ilan Elias7e035232012-08-15 11:46:22 +0300759 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
Ilan Elias7e035232012-08-15 11:46:22 +0300760
Julien Lefrique529ee062014-10-21 16:52:47 +0200761 rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
762 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
763 if (rc)
764 return rc;
765
766 param.id = NCI_LN_ATR_RES_GEN_BYTES;
767
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200768 return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
769 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
Ilan Elias7e035232012-08-15 11:46:22 +0300770}
771
Julien Lefrique90d78c12014-10-21 16:52:45 +0200772static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
773{
774 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
775 int rc;
776 __u8 val;
777
778 val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
779
780 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
781 if (rc)
782 return rc;
783
784 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
785
786 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
787 if (rc)
788 return rc;
789
790 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
791
792 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
793}
794
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200795static int nci_start_poll(struct nfc_dev *nfc_dev,
796 __u32 im_protocols, __u32 tm_protocols)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300797{
798 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Julien Lefrique772dccf2014-10-21 16:52:44 +0200799 struct nci_rf_discover_param param;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300800 int rc;
801
Ilan Elias019c4fb2012-01-18 13:16:14 +0200802 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100803 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800804 pr_err("unable to start poll, since poll is already active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300805 return -EBUSY;
806 }
807
Ilan Eliasde054792011-09-22 11:13:01 +0300808 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800809 pr_err("there is an active target\n");
Ilan Eliasde054792011-09-22 11:13:01 +0300810 return -EBUSY;
811 }
812
Ilan Elias019c4fb2012-01-18 13:16:14 +0200813 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100814 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200815 pr_debug("target active or w4 select, implicitly deactivate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300816
Christophe Ricard9295b5b2014-12-02 21:27:49 +0100817 rc = nci_request(ndev, nci_rf_deactivate_req,
818 NCI_DEACTIVATE_TYPE_IDLE_MODE,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100819 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300820 if (rc)
821 return -EBUSY;
822 }
823
Julien Lefrique529ee062014-10-21 16:52:47 +0200824 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
Ilan Elias7e035232012-08-15 11:46:22 +0300825 rc = nci_set_local_general_bytes(nfc_dev);
826 if (rc) {
827 pr_err("failed to set local general bytes\n");
828 return rc;
829 }
830 }
831
Julien Lefrique90d78c12014-10-21 16:52:45 +0200832 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
833 rc = nci_set_listen_parameters(nfc_dev);
834 if (rc)
835 pr_err("failed to set listen parameters\n");
836 }
837
Julien Lefrique772dccf2014-10-21 16:52:44 +0200838 param.im_protocols = im_protocols;
839 param.tm_protocols = tm_protocols;
840 rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100841 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300842
843 if (!rc)
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200844 ndev->poll_prots = im_protocols;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300845
846 return rc;
847}
848
849static void nci_stop_poll(struct nfc_dev *nfc_dev)
850{
851 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
852
Ilan Elias019c4fb2012-01-18 13:16:14 +0200853 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100854 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800855 pr_err("unable to stop poll, since poll is not active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300856 return;
857 }
858
Christophe Ricard9295b5b2014-12-02 21:27:49 +0100859 nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100860 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300861}
862
Eric Lapuyade90099432012-05-07 12:31:13 +0200863static int nci_activate_target(struct nfc_dev *nfc_dev,
864 struct nfc_target *target, __u32 protocol)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300865{
866 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200867 struct nci_rf_discover_select_param param;
Eric Lapuyade90099432012-05-07 12:31:13 +0200868 struct nfc_target *nci_target = NULL;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200869 int i;
870 int rc = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300871
Eric Lapuyade90099432012-05-07 12:31:13 +0200872 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300873
Ilan Elias019c4fb2012-01-18 13:16:14 +0200874 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100875 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800876 pr_err("there is no available target to activate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300877 return -EINVAL;
878 }
879
880 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800881 pr_err("there is already an active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300882 return -EBUSY;
883 }
884
Ilan Elias019c4fb2012-01-18 13:16:14 +0200885 for (i = 0; i < ndev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200886 if (ndev->targets[i].idx == target->idx) {
887 nci_target = &ndev->targets[i];
Ilan Elias019c4fb2012-01-18 13:16:14 +0200888 break;
889 }
890 }
891
Eric Lapuyade90099432012-05-07 12:31:13 +0200892 if (!nci_target) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200893 pr_err("unable to find the selected target\n");
894 return -EINVAL;
895 }
896
Eric Lapuyade90099432012-05-07 12:31:13 +0200897 if (!(nci_target->supported_protocols & (1 << protocol))) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800898 pr_err("target does not support the requested protocol 0x%x\n",
899 protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300900 return -EINVAL;
901 }
902
Ilan Elias019c4fb2012-01-18 13:16:14 +0200903 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200904 param.rf_discovery_id = nci_target->logical_idx;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300905
Ilan Elias019c4fb2012-01-18 13:16:14 +0200906 if (protocol == NFC_PROTO_JEWEL)
907 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
908 else if (protocol == NFC_PROTO_MIFARE)
909 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
910 else if (protocol == NFC_PROTO_FELICA)
911 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200912 else if (protocol == NFC_PROTO_ISO14443 ||
913 protocol == NFC_PROTO_ISO14443_B)
Ilan Elias019c4fb2012-01-18 13:16:14 +0200914 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
915 else
916 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
917
918 rc = nci_request(ndev, nci_rf_discover_select_req,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100919 (unsigned long)&param,
920 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
Ilan Elias019c4fb2012-01-18 13:16:14 +0200921 }
922
923 if (!rc)
924 ndev->target_active_prot = protocol;
925
926 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300927}
928
Eric Lapuyade90099432012-05-07 12:31:13 +0200929static void nci_deactivate_target(struct nfc_dev *nfc_dev,
Christophe Ricard96d45812015-10-25 22:54:43 +0100930 struct nfc_target *target,
931 __u8 mode)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300932{
933 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Christophe Ricard96d45812015-10-25 22:54:43 +0100934 u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300935
Ilan Elias767f19a2012-08-15 11:46:24 +0300936 pr_debug("entry\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300937
938 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800939 pr_err("unable to deactivate target, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300940 return;
941 }
942
943 ndev->target_active_prot = 0;
944
Christophe Ricard96d45812015-10-25 22:54:43 +0100945 switch (mode) {
946 case NFC_TARGET_MODE_SLEEP:
947 nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
948 break;
949 }
950
Ilan Elias8939e472012-01-18 13:16:12 +0200951 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
Christophe Ricard96d45812015-10-25 22:54:43 +0100952 nci_request(ndev, nci_rf_deactivate_req, nci_mode,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100953 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300954 }
955}
956
Ilan Elias767f19a2012-08-15 11:46:24 +0300957static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
958 __u8 comm_mode, __u8 *gb, size_t gb_len)
959{
960 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
961 int rc;
962
963 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
964
965 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
966 if (rc)
967 return rc;
968
969 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
970 ndev->remote_gb_len);
971 if (!rc)
972 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
973 NFC_RF_INITIATOR);
974
975 return rc;
976}
977
978static int nci_dep_link_down(struct nfc_dev *nfc_dev)
979{
Julien Lefriqued7979e12014-10-21 16:52:53 +0200980 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
981 int rc;
982
Ilan Elias767f19a2012-08-15 11:46:24 +0300983 pr_debug("entry\n");
984
Julien Lefriqued7979e12014-10-21 16:52:53 +0200985 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
Christophe Ricard96d45812015-10-25 22:54:43 +0100986 nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
Julien Lefriqued7979e12014-10-21 16:52:53 +0200987 } else {
988 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
989 atomic_read(&ndev->state) == NCI_DISCOVERY) {
990 nci_request(ndev, nci_rf_deactivate_req, 0,
991 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
992 }
993
994 rc = nfc_tm_deactivated(nfc_dev);
995 if (rc)
996 pr_err("error when signaling tm deactivation\n");
997 }
Ilan Elias767f19a2012-08-15 11:46:24 +0300998
999 return 0;
1000}
1001
1002
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001003static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1004 struct sk_buff *skb,
1005 data_exchange_cb_t cb, void *cb_context)
Ilan Elias6a2968a2011-09-18 11:19:35 +03001006{
1007 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias38f04c62011-09-22 11:36:19 +03001008 int rc;
Christophe Ricard4aeee682015-02-01 22:26:08 +01001009 struct nci_conn_info *conn_info;
1010
Christophe Ricard12bdf272015-02-03 19:48:04 +01001011 conn_info = ndev->rf_conn_info;
Christophe Ricard4aeee682015-02-01 22:26:08 +01001012 if (!conn_info)
1013 return -EPROTO;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001014
Eric Lapuyade90099432012-05-07 12:31:13 +02001015 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001016
1017 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001018 pr_err("unable to exchange data, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +03001019 return -EINVAL;
1020 }
1021
Ilan Elias38f04c62011-09-22 11:36:19 +03001022 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1023 return -EBUSY;
1024
Ilan Elias6a2968a2011-09-18 11:19:35 +03001025 /* store cb and context to be used on receiving data */
Christophe Ricard4aeee682015-02-01 22:26:08 +01001026 conn_info->data_exchange_cb = cb;
1027 conn_info->data_exchange_cb_context = cb_context;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001028
Ilan Eliase8c0dac2011-11-09 12:09:14 +02001029 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
Ilan Elias38f04c62011-09-22 11:36:19 +03001030 if (rc)
1031 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1032
1033 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001034}
1035
Julien Lefrique485f4422014-10-21 16:52:48 +02001036static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1037{
1038 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1039 int rc;
1040
1041 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1042 if (rc)
1043 pr_err("unable to send data\n");
1044
1045 return rc;
1046}
1047
Samuel Ortiz0a946302013-05-10 11:57:06 +02001048static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1049{
Christophe Ricard93bca2b2014-11-13 00:30:36 +01001050 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1051
1052 if (ndev->ops->enable_se)
1053 return ndev->ops->enable_se(ndev, se_idx);
1054
Samuel Ortiz0a946302013-05-10 11:57:06 +02001055 return 0;
1056}
1057
1058static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1059{
Christophe Ricarde9ef9432014-11-13 00:30:37 +01001060 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1061
1062 if (ndev->ops->disable_se)
1063 return ndev->ops->disable_se(ndev, se_idx);
1064
Samuel Ortiz0a946302013-05-10 11:57:06 +02001065 return 0;
1066}
1067
1068static int nci_discover_se(struct nfc_dev *nfc_dev)
1069{
Christophe Ricardfa00e8f2015-02-03 19:48:08 +01001070 int r;
Christophe Ricardba4db552014-11-13 00:30:35 +01001071 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1072
Christophe Ricardfa00e8f2015-02-03 19:48:08 +01001073 if (ndev->ops->discover_se) {
1074 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1075 if (r != NCI_STATUS_OK)
1076 return -EPROTO;
1077
Christophe Ricardba4db552014-11-13 00:30:35 +01001078 return ndev->ops->discover_se(ndev);
Christophe Ricardfa00e8f2015-02-03 19:48:08 +01001079 }
Christophe Ricardba4db552014-11-13 00:30:35 +01001080
Samuel Ortiz0a946302013-05-10 11:57:06 +02001081 return 0;
1082}
1083
Christophe Ricarda688bf52014-11-13 00:30:38 +01001084static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1085 u8 *apdu, size_t apdu_length,
1086 se_io_cb_t cb, void *cb_context)
1087{
1088 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1089
1090 if (ndev->ops->se_io)
1091 return ndev->ops->se_io(ndev, se_idx, apdu,
1092 apdu_length, cb, cb_context);
1093
1094 return 0;
1095}
1096
Clément Perrochaud25af01e2015-03-09 11:12:03 +01001097static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1098{
1099 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1100
1101 if (!ndev->ops->fw_download)
1102 return -ENOTSUPP;
1103
1104 return ndev->ops->fw_download(ndev, firmware_name);
1105}
1106
Ilan Elias6a2968a2011-09-18 11:19:35 +03001107static struct nfc_ops nci_nfc_ops = {
1108 .dev_up = nci_dev_up,
1109 .dev_down = nci_dev_down,
1110 .start_poll = nci_start_poll,
1111 .stop_poll = nci_stop_poll,
Ilan Elias767f19a2012-08-15 11:46:24 +03001112 .dep_link_up = nci_dep_link_up,
1113 .dep_link_down = nci_dep_link_down,
Ilan Elias6a2968a2011-09-18 11:19:35 +03001114 .activate_target = nci_activate_target,
1115 .deactivate_target = nci_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +02001116 .im_transceive = nci_transceive,
Julien Lefrique485f4422014-10-21 16:52:48 +02001117 .tm_send = nci_tm_send,
Samuel Ortiz0a946302013-05-10 11:57:06 +02001118 .enable_se = nci_enable_se,
1119 .disable_se = nci_disable_se,
1120 .discover_se = nci_discover_se,
Christophe Ricarda688bf52014-11-13 00:30:38 +01001121 .se_io = nci_se_io,
Clément Perrochaud25af01e2015-03-09 11:12:03 +01001122 .fw_download = nci_fw_download,
Ilan Elias6a2968a2011-09-18 11:19:35 +03001123};
1124
1125/* ---- Interface to NCI drivers ---- */
Ilan Elias6a2968a2011-09-18 11:19:35 +03001126/**
1127 * nci_allocate_device - allocate a new nci device
1128 *
1129 * @ops: device operations
1130 * @supported_protocols: NFC protocols supported by the device
1131 */
1132struct nci_dev *nci_allocate_device(struct nci_ops *ops,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001133 __u32 supported_protocols,
1134 int tx_headroom, int tx_tailroom)
Ilan Elias6a2968a2011-09-18 11:19:35 +03001135{
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001136 struct nci_dev *ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001137
Joe Perches24bf3302011-11-29 11:37:35 -08001138 pr_debug("supported_protocols 0x%x\n", supported_protocols);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001139
1140 if (!ops->open || !ops->close || !ops->send)
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001141 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001142
1143 if (!supported_protocols)
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001144 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001145
1146 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1147 if (!ndev)
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001148 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001149
1150 ndev->ops = ops;
Samuel Ortizb6355e92015-06-06 13:16:37 +02001151
1152 if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1153 pr_err("Too many proprietary commands: %zd\n",
1154 ops->n_prop_ops);
1155 ops->prop_ops = NULL;
1156 ops->n_prop_ops = 0;
1157 }
1158
Ilan Elias6a2968a2011-09-18 11:19:35 +03001159 ndev->tx_headroom = tx_headroom;
1160 ndev->tx_tailroom = tx_tailroom;
Axel Lin9bec44b2014-02-13 13:25:48 +08001161 init_completion(&ndev->req_completion);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001162
1163 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001164 supported_protocols,
1165 tx_headroom + NCI_DATA_HDR_SIZE,
1166 tx_tailroom);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001167 if (!ndev->nfc_dev)
Christophe Ricard11f54f22015-02-01 22:26:14 +01001168 goto free_nci;
1169
1170 ndev->hci_dev = nci_hci_allocate(ndev);
1171 if (!ndev->hci_dev)
1172 goto free_nfc;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001173
1174 nfc_set_drvdata(ndev->nfc_dev, ndev);
1175
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001176 return ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001177
Christophe Ricard11f54f22015-02-01 22:26:14 +01001178free_nfc:
Johan Hovoldf73de3f2017-03-30 12:15:35 +02001179 nfc_free_device(ndev->nfc_dev);
Christophe Ricard11f54f22015-02-01 22:26:14 +01001180free_nci:
Ilan Elias6a2968a2011-09-18 11:19:35 +03001181 kfree(ndev);
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001182 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001183}
1184EXPORT_SYMBOL(nci_allocate_device);
1185
1186/**
1187 * nci_free_device - deallocate nci device
1188 *
1189 * @ndev: The nci device to deallocate
1190 */
1191void nci_free_device(struct nci_dev *ndev)
1192{
Ilan Elias6a2968a2011-09-18 11:19:35 +03001193 nfc_free_device(ndev->nfc_dev);
Dongliang Mu4a621622021-05-15 07:29:06 +08001194 nci_hci_deallocate(ndev);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001195 kfree(ndev);
1196}
1197EXPORT_SYMBOL(nci_free_device);
1198
1199/**
1200 * nci_register_device - register a nci device in the nfc subsystem
1201 *
1202 * @dev: The nci device to register
1203 */
1204int nci_register_device(struct nci_dev *ndev)
1205{
1206 int rc;
1207 struct device *dev = &ndev->nfc_dev->dev;
1208 char name[32];
1209
Ilan Elias6a2968a2011-09-18 11:19:35 +03001210 ndev->flags = 0;
1211
1212 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1213 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1214 ndev->cmd_wq = create_singlethread_workqueue(name);
1215 if (!ndev->cmd_wq) {
1216 rc = -ENOMEM;
Vincent Cuissard3c1c0f52014-07-22 19:48:39 +02001217 goto exit;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001218 }
1219
1220 INIT_WORK(&ndev->rx_work, nci_rx_work);
1221 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1222 ndev->rx_wq = create_singlethread_workqueue(name);
1223 if (!ndev->rx_wq) {
1224 rc = -ENOMEM;
1225 goto destroy_cmd_wq_exit;
1226 }
1227
1228 INIT_WORK(&ndev->tx_work, nci_tx_work);
1229 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1230 ndev->tx_wq = create_singlethread_workqueue(name);
1231 if (!ndev->tx_wq) {
1232 rc = -ENOMEM;
1233 goto destroy_rx_wq_exit;
1234 }
1235
1236 skb_queue_head_init(&ndev->cmd_q);
1237 skb_queue_head_init(&ndev->rx_q);
1238 skb_queue_head_init(&ndev->tx_q);
1239
1240 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001241 (unsigned long) ndev);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +02001242 setup_timer(&ndev->data_timer, nci_data_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001243 (unsigned long) ndev);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001244
1245 mutex_init(&ndev->req_lock);
Christophe Ricard4aeee682015-02-01 22:26:08 +01001246 INIT_LIST_HEAD(&ndev->conn_info_list);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001247
Vincent Cuissard3c1c0f52014-07-22 19:48:39 +02001248 rc = nfc_register_device(ndev->nfc_dev);
1249 if (rc)
1250 goto destroy_rx_wq_exit;
1251
Ilan Elias6a2968a2011-09-18 11:19:35 +03001252 goto exit;
1253
1254destroy_rx_wq_exit:
1255 destroy_workqueue(ndev->rx_wq);
1256
1257destroy_cmd_wq_exit:
1258 destroy_workqueue(ndev->cmd_wq);
1259
Ilan Elias6a2968a2011-09-18 11:19:35 +03001260exit:
1261 return rc;
1262}
1263EXPORT_SYMBOL(nci_register_device);
1264
1265/**
1266 * nci_unregister_device - unregister a nci device in the nfc subsystem
1267 *
1268 * @dev: The nci device to unregister
1269 */
1270void nci_unregister_device(struct nci_dev *ndev)
1271{
Christophe Ricard4aeee682015-02-01 22:26:08 +01001272 struct nci_conn_info *conn_info, *n;
1273
Ilan Elias6a2968a2011-09-18 11:19:35 +03001274 nci_close_device(ndev);
1275
1276 destroy_workqueue(ndev->cmd_wq);
1277 destroy_workqueue(ndev->rx_wq);
1278 destroy_workqueue(ndev->tx_wq);
1279
Christophe Ricard4aeee682015-02-01 22:26:08 +01001280 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1281 list_del(&conn_info->list);
1282 /* conn_info is allocated with devm_kzalloc */
1283 }
1284
Ilan Elias6a2968a2011-09-18 11:19:35 +03001285 nfc_unregister_device(ndev->nfc_dev);
1286}
1287EXPORT_SYMBOL(nci_unregister_device);
1288
1289/**
1290 * nci_recv_frame - receive frame from NCI drivers
1291 *
Frederic Danis1095e692013-05-22 11:36:17 +02001292 * @ndev: The nci device
Ilan Elias6a2968a2011-09-18 11:19:35 +03001293 * @skb: The sk_buff to receive
1294 */
Frederic Danis1095e692013-05-22 11:36:17 +02001295int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +03001296{
Joe Perches24bf3302011-11-29 11:37:35 -08001297 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001298
Szymon Janc874934f2012-10-04 15:15:51 +02001299 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1300 !test_bit(NCI_INIT, &ndev->flags))) {
Ilan Elias6a2968a2011-09-18 11:19:35 +03001301 kfree_skb(skb);
1302 return -ENXIO;
1303 }
1304
1305 /* Queue frame for rx worker thread */
1306 skb_queue_tail(&ndev->rx_q, skb);
1307 queue_work(ndev->rx_wq, &ndev->rx_work);
1308
1309 return 0;
1310}
1311EXPORT_SYMBOL(nci_recv_frame);
1312
Vincent Cuissarde5629d22015-10-26 10:27:38 +01001313int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +03001314{
Joe Perches24bf3302011-11-29 11:37:35 -08001315 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001316
1317 if (!ndev) {
1318 kfree_skb(skb);
1319 return -ENODEV;
1320 }
1321
1322 /* Get rid of skb owner, prior to sending to the driver. */
1323 skb_orphan(skb);
1324
Hiren Tandel05158292014-05-05 19:52:27 +09001325 /* Send copy to sniffer */
1326 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1327 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1328
Frederic Danis1095e692013-05-22 11:36:17 +02001329 return ndev->ops->send(ndev, skb);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001330}
Vincent Cuissarde5629d22015-10-26 10:27:38 +01001331EXPORT_SYMBOL(nci_send_frame);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001332
1333/* Send NCI command */
1334int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1335{
1336 struct nci_ctrl_hdr *hdr;
1337 struct sk_buff *skb;
1338
Joe Perches24bf3302011-11-29 11:37:35 -08001339 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001340
1341 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1342 if (!skb) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001343 pr_err("no memory for command\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +03001344 return -ENOMEM;
1345 }
1346
1347 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
1348 hdr->gid = nci_opcode_gid(opcode);
1349 hdr->oid = nci_opcode_oid(opcode);
1350 hdr->plen = plen;
1351
1352 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1353 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1354
1355 if (plen)
1356 memcpy(skb_put(skb, plen), payload, plen);
1357
Ilan Elias6a2968a2011-09-18 11:19:35 +03001358 skb_queue_tail(&ndev->cmd_q, skb);
1359 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1360
1361 return 0;
1362}
Vincent Cuissarde5629d22015-10-26 10:27:38 +01001363EXPORT_SYMBOL(nci_send_cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001364
Samuel Ortizb6355e92015-06-06 13:16:37 +02001365/* Proprietary commands API */
Robert Dolca22e4bd02015-10-22 12:11:39 +03001366static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
1367 size_t n_ops,
1368 __u16 opcode)
Samuel Ortizb6355e92015-06-06 13:16:37 +02001369{
1370 size_t i;
Robert Dolca22e4bd02015-10-22 12:11:39 +03001371 struct nci_driver_ops *op;
Samuel Ortizb6355e92015-06-06 13:16:37 +02001372
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001373 if (!ops || !n_ops)
Samuel Ortizb6355e92015-06-06 13:16:37 +02001374 return NULL;
1375
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001376 for (i = 0; i < n_ops; i++) {
1377 op = &ops[i];
1378 if (op->opcode == opcode)
1379 return op;
Samuel Ortizb6355e92015-06-06 13:16:37 +02001380 }
1381
1382 return NULL;
1383}
1384
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001385static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
Robert Dolca22e4bd02015-10-22 12:11:39 +03001386 struct sk_buff *skb, struct nci_driver_ops *ops,
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001387 size_t n_ops)
Samuel Ortizb6355e92015-06-06 13:16:37 +02001388{
Robert Dolca22e4bd02015-10-22 12:11:39 +03001389 struct nci_driver_ops *op;
Samuel Ortizb6355e92015-06-06 13:16:37 +02001390
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001391 op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1392 if (!op || !op->rsp)
Samuel Ortizb6355e92015-06-06 13:16:37 +02001393 return -ENOTSUPP;
1394
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001395 return op->rsp(ndev, skb);
Samuel Ortizb6355e92015-06-06 13:16:37 +02001396}
1397
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001398static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
Robert Dolca22e4bd02015-10-22 12:11:39 +03001399 struct sk_buff *skb, struct nci_driver_ops *ops,
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001400 size_t n_ops)
Samuel Ortizb6355e92015-06-06 13:16:37 +02001401{
Robert Dolca22e4bd02015-10-22 12:11:39 +03001402 struct nci_driver_ops *op;
Samuel Ortizb6355e92015-06-06 13:16:37 +02001403
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001404 op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1405 if (!op || !op->ntf)
Samuel Ortizb6355e92015-06-06 13:16:37 +02001406 return -ENOTSUPP;
1407
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001408 return op->ntf(ndev, skb);
1409}
1410
Robert Dolcaf1163172015-10-26 13:58:54 +02001411int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1412 struct sk_buff *skb)
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001413{
1414 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1415 ndev->ops->n_prop_ops);
1416}
1417
Robert Dolcaf1163172015-10-26 13:58:54 +02001418int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1419 struct sk_buff *skb)
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001420{
1421 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1422 ndev->ops->n_prop_ops);
1423}
1424
Robert Dolcaf1163172015-10-26 13:58:54 +02001425int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1426 struct sk_buff *skb)
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001427{
1428 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1429 ndev->ops->n_core_ops);
1430}
1431
Robert Dolcaf1163172015-10-26 13:58:54 +02001432int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1433 struct sk_buff *skb)
Robert Dolca0a97a3c2015-10-22 12:11:38 +03001434{
1435 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1436 ndev->ops->n_core_ops);
Samuel Ortizb6355e92015-06-06 13:16:37 +02001437}
1438
Ilan Elias6a2968a2011-09-18 11:19:35 +03001439/* ---- NCI TX Data worker thread ---- */
1440
1441static void nci_tx_work(struct work_struct *work)
1442{
1443 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
Christophe Ricard4aeee682015-02-01 22:26:08 +01001444 struct nci_conn_info *conn_info;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001445 struct sk_buff *skb;
1446
Christophe Ricard4aeee682015-02-01 22:26:08 +01001447 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1448 if (!conn_info)
1449 return;
1450
1451 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001452
1453 /* Send queued tx data */
Christophe Ricard4aeee682015-02-01 22:26:08 +01001454 while (atomic_read(&conn_info->credits_cnt)) {
Ilan Elias6a2968a2011-09-18 11:19:35 +03001455 skb = skb_dequeue(&ndev->tx_q);
1456 if (!skb)
1457 return;
1458
Ilan Eliasdb98c822011-11-09 12:09:16 +02001459 /* Check if data flow control is used */
Christophe Ricard4aeee682015-02-01 22:26:08 +01001460 if (atomic_read(&conn_info->credits_cnt) !=
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001461 NCI_DATA_FLOW_CONTROL_NOT_USED)
Christophe Ricard4aeee682015-02-01 22:26:08 +01001462 atomic_dec(&conn_info->credits_cnt);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001463
Joe Perches20c239c2011-11-29 11:37:33 -08001464 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1465 nci_pbf(skb->data),
1466 nci_conn_id(skb->data),
1467 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001468
Frederic Danis1095e692013-05-22 11:36:17 +02001469 nci_send_frame(ndev, skb);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +02001470
1471 mod_timer(&ndev->data_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001472 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001473 }
1474}
1475
1476/* ----- NCI RX worker thread (data & control) ----- */
1477
1478static void nci_rx_work(struct work_struct *work)
1479{
1480 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1481 struct sk_buff *skb;
1482
1483 while ((skb = skb_dequeue(&ndev->rx_q))) {
Hiren Tandel05158292014-05-05 19:52:27 +09001484
1485 /* Send copy to sniffer */
1486 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1487 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1488
Ilan Elias6a2968a2011-09-18 11:19:35 +03001489 /* Process frame */
1490 switch (nci_mt(skb->data)) {
1491 case NCI_MT_RSP_PKT:
1492 nci_rsp_packet(ndev, skb);
1493 break;
1494
1495 case NCI_MT_NTF_PKT:
1496 nci_ntf_packet(ndev, skb);
1497 break;
1498
1499 case NCI_MT_DATA_PKT:
1500 nci_rx_data_packet(ndev, skb);
1501 break;
1502
1503 default:
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001504 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001505 kfree_skb(skb);
1506 break;
1507 }
1508 }
Ilan Eliasc4bf98b2012-01-17 12:03:50 +02001509
1510 /* check if a data exchange timout has occurred */
1511 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1512 /* complete the data exchange transaction, if exists */
1513 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
Christophe Ricard4aeee682015-02-01 22:26:08 +01001514 nci_data_exchange_complete(ndev, NULL,
1515 ndev->cur_conn_id,
1516 -ETIMEDOUT);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +02001517
1518 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1519 }
Ilan Elias6a2968a2011-09-18 11:19:35 +03001520}
1521
1522/* ----- NCI TX CMD worker thread ----- */
1523
1524static void nci_cmd_work(struct work_struct *work)
1525{
1526 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1527 struct sk_buff *skb;
1528
Joe Perches24bf3302011-11-29 11:37:35 -08001529 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001530
1531 /* Send queued command */
1532 if (atomic_read(&ndev->cmd_cnt)) {
1533 skb = skb_dequeue(&ndev->cmd_q);
1534 if (!skb)
1535 return;
1536
1537 atomic_dec(&ndev->cmd_cnt);
1538
Joe Perches20c239c2011-11-29 11:37:33 -08001539 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1540 nci_pbf(skb->data),
1541 nci_opcode_gid(nci_opcode(skb->data)),
1542 nci_opcode_oid(nci_opcode(skb->data)),
1543 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001544
Frederic Danis1095e692013-05-22 11:36:17 +02001545 nci_send_frame(ndev, skb);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001546
1547 mod_timer(&ndev->cmd_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001548 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001549 }
1550}
Dave Jones8a70e7f2012-07-12 19:17:34 +02001551
1552MODULE_LICENSE("GPL");