blob: 943889b87a34bdd33e9c4a8c1b01df04808b3e00 [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
Ilan Elias6a2968a2011-09-18 11:19:35 +030067/* ---- NCI requests ---- */
68
69void nci_req_complete(struct nci_dev *ndev, int result)
70{
71 if (ndev->req_status == NCI_REQ_PEND) {
72 ndev->req_result = result;
73 ndev->req_status = NCI_REQ_DONE;
74 complete(&ndev->req_completion);
75 }
76}
Samuel Ortiz2df7f8c2015-06-10 12:50:22 +020077EXPORT_SYMBOL(nci_req_complete);
Ilan Elias6a2968a2011-09-18 11:19:35 +030078
79static void nci_req_cancel(struct nci_dev *ndev, int err)
80{
81 if (ndev->req_status == NCI_REQ_PEND) {
82 ndev->req_result = err;
83 ndev->req_status = NCI_REQ_CANCELED;
84 complete(&ndev->req_completion);
85 }
86}
87
88/* Execute request and wait for completion. */
89static int __nci_request(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010090 void (*req)(struct nci_dev *ndev, unsigned long opt),
91 unsigned long opt, __u32 timeout)
Ilan Elias6a2968a2011-09-18 11:19:35 +030092{
93 int rc = 0;
Dan Carpenterf8c141c2011-12-09 09:35:39 +030094 long completion_rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +030095
96 ndev->req_status = NCI_REQ_PEND;
97
Axel Lin9bec44b2014-02-13 13:25:48 +080098 reinit_completion(&ndev->req_completion);
Ilan Elias6a2968a2011-09-18 11:19:35 +030099 req(ndev, opt);
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100100 completion_rc =
101 wait_for_completion_interruptible_timeout(&ndev->req_completion,
102 timeout);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300103
Joe Perches20c239c2011-11-29 11:37:33 -0800104 pr_debug("wait_for_completion return %ld\n", completion_rc);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300105
106 if (completion_rc > 0) {
107 switch (ndev->req_status) {
108 case NCI_REQ_DONE:
109 rc = nci_to_errno(ndev->req_result);
110 break;
111
112 case NCI_REQ_CANCELED:
113 rc = -ndev->req_result;
114 break;
115
116 default:
117 rc = -ETIMEDOUT;
118 break;
119 }
120 } else {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800121 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
122 completion_rc);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300123
124 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
125 }
126
127 ndev->req_status = ndev->req_result = 0;
128
129 return rc;
130}
131
Christophe Ricard11f54f22015-02-01 22:26:14 +0100132inline int nci_request(struct nci_dev *ndev,
133 void (*req)(struct nci_dev *ndev,
134 unsigned long opt),
135 unsigned long opt, __u32 timeout)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300136{
137 int rc;
138
139 if (!test_bit(NCI_UP, &ndev->flags))
140 return -ENETDOWN;
141
142 /* Serialize all requests */
143 mutex_lock(&ndev->req_lock);
144 rc = __nci_request(ndev, req, opt, timeout);
145 mutex_unlock(&ndev->req_lock);
146
147 return rc;
148}
149
150static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
151{
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200152 struct nci_core_reset_cmd cmd;
153
154 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
155 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300156}
157
158static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
159{
160 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
161}
162
163static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
164{
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300165 struct nci_rf_disc_map_cmd cmd;
166 struct disc_map_config *cfg = cmd.mapping_configs;
167 __u8 *num = &cmd.num_mapping_configs;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300168 int i;
169
Ilan Elias6a2968a2011-09-18 11:19:35 +0300170 /* set rf mapping configurations */
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300171 *num = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300172
173 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
174 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
175 if (ndev->supported_rf_interfaces[i] ==
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100176 NCI_RF_INTERFACE_ISO_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300177 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200178 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
179 NCI_DISC_MAP_MODE_LISTEN;
180 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300181 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300182 } else if (ndev->supported_rf_interfaces[i] ==
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100183 NCI_RF_INTERFACE_NFC_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300184 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200185 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
186 NCI_DISC_MAP_MODE_LISTEN;
187 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300188 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300189 }
190
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300191 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300192 break;
193 }
194
195 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100196 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300197}
198
Ilan Elias7e035232012-08-15 11:46:22 +0300199struct nci_set_config_param {
200 __u8 id;
201 size_t len;
202 __u8 *val;
203};
204
205static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
206{
207 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
208 struct nci_core_set_config_cmd cmd;
209
210 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
211
212 cmd.num_params = 1;
213 cmd.param.id = param->id;
214 cmd.param.len = param->len;
215 memcpy(cmd.param.val, param->val, param->len);
216
217 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
218}
219
Julien Lefrique772dccf2014-10-21 16:52:44 +0200220struct nci_rf_discover_param {
221 __u32 im_protocols;
222 __u32 tm_protocols;
223};
224
Ilan Elias6a2968a2011-09-18 11:19:35 +0300225static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
226{
Julien Lefrique772dccf2014-10-21 16:52:44 +0200227 struct nci_rf_discover_param *param =
228 (struct nci_rf_discover_param *)opt;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300229 struct nci_rf_disc_cmd cmd;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300230
231 cmd.num_disc_configs = 0;
232
233 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200234 (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
235 param->im_protocols & NFC_PROTO_MIFARE_MASK ||
236 param->im_protocols & NFC_PROTO_ISO14443_MASK ||
237 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200238 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100239 NCI_NFC_A_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300240 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
241 cmd.num_disc_configs++;
242 }
243
244 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200245 (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200246 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100247 NCI_NFC_B_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300248 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
249 cmd.num_disc_configs++;
250 }
251
252 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200253 (param->im_protocols & NFC_PROTO_FELICA_MASK ||
254 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200255 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100256 NCI_NFC_F_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300257 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
258 cmd.num_disc_configs++;
259 }
260
Vincent Cuissardcfdbeea2014-07-22 19:48:38 +0200261 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Julien Lefrique772dccf2014-10-21 16:52:44 +0200262 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
Vincent Cuissardcfdbeea2014-07-22 19:48:38 +0200263 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
264 NCI_NFC_V_PASSIVE_POLL_MODE;
265 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
266 cmd.num_disc_configs++;
267 }
268
Julien Lefrique772dccf2014-10-21 16:52:44 +0200269 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
270 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
271 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
272 NCI_NFC_A_PASSIVE_LISTEN_MODE;
273 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
274 cmd.num_disc_configs++;
275 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
276 NCI_NFC_F_PASSIVE_LISTEN_MODE;
277 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
278 cmd.num_disc_configs++;
279 }
280
Ilan Elias6a2968a2011-09-18 11:19:35 +0300281 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100282 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
283 &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300284}
285
Ilan Elias019c4fb2012-01-18 13:16:14 +0200286struct nci_rf_discover_select_param {
287 __u8 rf_discovery_id;
288 __u8 rf_protocol;
289};
290
291static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
292{
293 struct nci_rf_discover_select_param *param =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100294 (struct nci_rf_discover_select_param *)opt;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200295 struct nci_rf_discover_select_cmd cmd;
296
297 cmd.rf_discovery_id = param->rf_discovery_id;
298 cmd.rf_protocol = param->rf_protocol;
299
300 switch (cmd.rf_protocol) {
301 case NCI_RF_PROTOCOL_ISO_DEP:
302 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
303 break;
304
305 case NCI_RF_PROTOCOL_NFC_DEP:
306 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
307 break;
308
309 default:
310 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
311 break;
312 }
313
314 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100315 sizeof(struct nci_rf_discover_select_cmd), &cmd);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200316}
317
Ilan Elias6a2968a2011-09-18 11:19:35 +0300318static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
319{
320 struct nci_rf_deactivate_cmd cmd;
321
Christophe Ricard9295b5b2014-12-02 21:27:49 +0100322 cmd.type = opt;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300323
324 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100325 sizeof(struct nci_rf_deactivate_cmd), &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300326}
327
Christophe Ricard759afb82015-06-06 13:16:41 +0200328struct nci_prop_cmd_param {
329 __u16 opcode;
330 size_t len;
331 __u8 *payload;
332};
333
334static void nci_prop_cmd_req(struct nci_dev *ndev, unsigned long opt)
335{
336 struct nci_prop_cmd_param *param = (struct nci_prop_cmd_param *)opt;
337
338 nci_send_cmd(ndev, param->opcode, param->len, param->payload);
339}
340
341int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, __u8 *payload)
342{
343 struct nci_prop_cmd_param param;
344
345 param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
346 param.len = len;
347 param.payload = payload;
348
349 return __nci_request(ndev, nci_prop_cmd_req, (unsigned long)&param,
350 msecs_to_jiffies(NCI_CMD_TIMEOUT));
351}
352EXPORT_SYMBOL(nci_prop_cmd);
353
Robert Baldyga025a0cb2015-08-20 17:26:01 +0200354int nci_core_reset(struct nci_dev *ndev)
355{
356 return __nci_request(ndev, nci_reset_req, 0,
357 msecs_to_jiffies(NCI_RESET_TIMEOUT));
358}
359EXPORT_SYMBOL(nci_core_reset);
360
361int nci_core_init(struct nci_dev *ndev)
362{
363 return __nci_request(ndev, nci_init_req, 0,
364 msecs_to_jiffies(NCI_INIT_TIMEOUT));
365}
366EXPORT_SYMBOL(nci_core_init);
367
Ilan Elias6a2968a2011-09-18 11:19:35 +0300368static int nci_open_device(struct nci_dev *ndev)
369{
370 int rc = 0;
371
372 mutex_lock(&ndev->req_lock);
373
374 if (test_bit(NCI_UP, &ndev->flags)) {
375 rc = -EALREADY;
376 goto done;
377 }
378
379 if (ndev->ops->open(ndev)) {
380 rc = -EIO;
381 goto done;
382 }
383
384 atomic_set(&ndev->cmd_cnt, 1);
385
386 set_bit(NCI_INIT, &ndev->flags);
387
Christophe Ricardc39daee2015-06-06 13:16:40 +0200388 if (ndev->ops->init)
389 rc = ndev->ops->init(ndev);
390
391 if (!rc) {
392 rc = __nci_request(ndev, nci_reset_req, 0,
393 msecs_to_jiffies(NCI_RESET_TIMEOUT));
394 }
Ilan Elias6a2968a2011-09-18 11:19:35 +0300395
Christophe Ricard81859ab2015-06-06 13:16:39 +0200396 if (!rc && ndev->ops->setup) {
397 rc = ndev->ops->setup(ndev);
398 }
Amitkumar Karwar86e85862014-01-06 12:58:17 -0800399
Ilan Elias6a2968a2011-09-18 11:19:35 +0300400 if (!rc) {
401 rc = __nci_request(ndev, nci_init_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100402 msecs_to_jiffies(NCI_INIT_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300403 }
404
Robert Baldygafdf79bd2015-08-20 17:26:00 +0200405 if (ndev->ops->post_setup) {
406 rc = ndev->ops->post_setup(ndev);
407 }
408
Ilan Elias6a2968a2011-09-18 11:19:35 +0300409 if (!rc) {
410 rc = __nci_request(ndev, nci_init_complete_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100411 msecs_to_jiffies(NCI_INIT_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300412 }
413
414 clear_bit(NCI_INIT, &ndev->flags);
415
416 if (!rc) {
417 set_bit(NCI_UP, &ndev->flags);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200418 nci_clear_target_list(ndev);
Ilan Elias8939e472012-01-18 13:16:12 +0200419 atomic_set(&ndev->state, NCI_IDLE);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300420 } else {
421 /* Init failed, cleanup */
422 skb_queue_purge(&ndev->cmd_q);
423 skb_queue_purge(&ndev->rx_q);
424 skb_queue_purge(&ndev->tx_q);
425
426 ndev->ops->close(ndev);
427 ndev->flags = 0;
428 }
429
430done:
431 mutex_unlock(&ndev->req_lock);
432 return rc;
433}
434
435static int nci_close_device(struct nci_dev *ndev)
436{
437 nci_req_cancel(ndev, ENODEV);
438 mutex_lock(&ndev->req_lock);
439
440 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
441 del_timer_sync(&ndev->cmd_timer);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200442 del_timer_sync(&ndev->data_timer);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300443 mutex_unlock(&ndev->req_lock);
444 return 0;
445 }
446
447 /* Drop RX and TX queues */
448 skb_queue_purge(&ndev->rx_q);
449 skb_queue_purge(&ndev->tx_q);
450
451 /* Flush RX and TX wq */
452 flush_workqueue(ndev->rx_wq);
453 flush_workqueue(ndev->tx_wq);
454
455 /* Reset device */
456 skb_queue_purge(&ndev->cmd_q);
457 atomic_set(&ndev->cmd_cnt, 1);
458
459 set_bit(NCI_INIT, &ndev->flags);
460 __nci_request(ndev, nci_reset_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100461 msecs_to_jiffies(NCI_RESET_TIMEOUT));
Christophe Ricard0e70cba72015-06-06 13:16:48 +0200462
463 /* After this point our queues are empty
464 * and no works are scheduled.
465 */
466 ndev->ops->close(ndev);
467
Ilan Elias6a2968a2011-09-18 11:19:35 +0300468 clear_bit(NCI_INIT, &ndev->flags);
469
Amitkumar Karwarfa9be5f2013-12-23 14:15:13 -0800470 del_timer_sync(&ndev->cmd_timer);
471
Ilan Elias6a2968a2011-09-18 11:19:35 +0300472 /* Flush cmd wq */
473 flush_workqueue(ndev->cmd_wq);
474
Ilan Elias6a2968a2011-09-18 11:19:35 +0300475 /* Clear flags */
476 ndev->flags = 0;
477
478 mutex_unlock(&ndev->req_lock);
479
480 return 0;
481}
482
483/* NCI command timer function */
484static void nci_cmd_timer(unsigned long arg)
485{
486 struct nci_dev *ndev = (void *) arg;
487
Ilan Elias6a2968a2011-09-18 11:19:35 +0300488 atomic_set(&ndev->cmd_cnt, 1);
489 queue_work(ndev->cmd_wq, &ndev->cmd_work);
490}
491
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200492/* NCI data exchange timer function */
493static void nci_data_timer(unsigned long arg)
494{
495 struct nci_dev *ndev = (void *) arg;
496
497 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
498 queue_work(ndev->rx_wq, &ndev->rx_work);
499}
500
Ilan Elias6a2968a2011-09-18 11:19:35 +0300501static int nci_dev_up(struct nfc_dev *nfc_dev)
502{
503 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
504
Ilan Elias6a2968a2011-09-18 11:19:35 +0300505 return nci_open_device(ndev);
506}
507
508static int nci_dev_down(struct nfc_dev *nfc_dev)
509{
510 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
511
Ilan Elias6a2968a2011-09-18 11:19:35 +0300512 return nci_close_device(ndev);
513}
514
Amitkumar Karwar22c15bf2014-01-06 12:58:18 -0800515int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
516{
517 struct nci_set_config_param param;
518
519 if (!val || !len)
520 return 0;
521
522 param.id = id;
523 param.len = len;
524 param.val = val;
525
526 return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
527 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
528}
529EXPORT_SYMBOL(nci_set_config);
530
Christophe Ricardaf9c8aa2015-02-01 22:26:10 +0100531static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
532{
533 struct nci_nfcee_discover_cmd cmd;
534 __u8 action = opt;
535
536 cmd.discovery_action = action;
537
538 nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
539}
540
541int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
542{
543 return nci_request(ndev, nci_nfcee_discover_req, action,
544 msecs_to_jiffies(NCI_CMD_TIMEOUT));
545}
546EXPORT_SYMBOL(nci_nfcee_discover);
547
Christophe Ricardf7f793f2015-02-01 22:26:11 +0100548static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
549{
550 struct nci_nfcee_mode_set_cmd *cmd =
551 (struct nci_nfcee_mode_set_cmd *)opt;
552
553 nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
554 sizeof(struct nci_nfcee_mode_set_cmd), cmd);
555}
556
557int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
558{
559 struct nci_nfcee_mode_set_cmd cmd;
560
561 cmd.nfcee_id = nfcee_id;
562 cmd.nfcee_mode = nfcee_mode;
563
564 return nci_request(ndev, nci_nfcee_mode_set_req, (unsigned long)&cmd,
565 msecs_to_jiffies(NCI_CMD_TIMEOUT));
566}
567EXPORT_SYMBOL(nci_nfcee_mode_set);
568
Christophe Ricard736bb952015-02-01 22:26:12 +0100569static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
570{
Christophe Ricardb16ae712015-02-03 19:48:05 +0100571 struct core_conn_create_data *data =
572 (struct core_conn_create_data *)opt;
Christophe Ricard736bb952015-02-01 22:26:12 +0100573
Christophe Ricardb16ae712015-02-03 19:48:05 +0100574 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
Christophe Ricard736bb952015-02-01 22:26:12 +0100575}
576
Christophe Ricardb16ae712015-02-03 19:48:05 +0100577int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
578 u8 number_destination_params,
579 size_t params_len,
Christophe Ricard736bb952015-02-01 22:26:12 +0100580 struct core_conn_create_dest_spec_params *params)
581{
Christophe Ricardb16ae712015-02-03 19:48:05 +0100582 int r;
583 struct nci_core_conn_create_cmd *cmd;
584 struct core_conn_create_data data;
585
586 data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
587 cmd = kzalloc(data.length, GFP_KERNEL);
588 if (!cmd)
589 return -ENOMEM;
590
591 cmd->destination_type = destination_type;
592 cmd->number_destination_params = number_destination_params;
593 memcpy(cmd->params, params, params_len);
594
595 data.cmd = cmd;
596 ndev->cur_id = params->value[DEST_SPEC_PARAMS_ID_INDEX];
597
598 r = __nci_request(ndev, nci_core_conn_create_req,
599 (unsigned long)&data,
600 msecs_to_jiffies(NCI_CMD_TIMEOUT));
601 kfree(cmd);
602 return r;
Christophe Ricard736bb952015-02-01 22:26:12 +0100603}
604EXPORT_SYMBOL(nci_core_conn_create);
605
606static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
607{
608 __u8 conn_id = opt;
609
610 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
611}
612
613int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
614{
615 return nci_request(ndev, nci_core_conn_close_req, conn_id,
616 msecs_to_jiffies(NCI_CMD_TIMEOUT));
617}
618EXPORT_SYMBOL(nci_core_conn_close);
619
Ilan Elias7e035232012-08-15 11:46:22 +0300620static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
621{
622 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
623 struct nci_set_config_param param;
Julien Lefrique529ee062014-10-21 16:52:47 +0200624 int rc;
Ilan Elias7e035232012-08-15 11:46:22 +0300625
626 param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
627 if ((param.val == NULL) || (param.len == 0))
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200628 return 0;
Ilan Elias7e035232012-08-15 11:46:22 +0300629
Szymon Janc460d8f92012-10-04 15:15:45 +0200630 if (param.len > NFC_MAX_GT_LEN)
Ilan Elias7e035232012-08-15 11:46:22 +0300631 return -EINVAL;
632
Ilan Elias7e035232012-08-15 11:46:22 +0300633 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
Ilan Elias7e035232012-08-15 11:46:22 +0300634
Julien Lefrique529ee062014-10-21 16:52:47 +0200635 rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
636 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
637 if (rc)
638 return rc;
639
640 param.id = NCI_LN_ATR_RES_GEN_BYTES;
641
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200642 return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
643 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
Ilan Elias7e035232012-08-15 11:46:22 +0300644}
645
Julien Lefrique90d78c12014-10-21 16:52:45 +0200646static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
647{
648 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
649 int rc;
650 __u8 val;
651
652 val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
653
654 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
655 if (rc)
656 return rc;
657
658 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
659
660 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
661 if (rc)
662 return rc;
663
664 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
665
666 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
667}
668
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200669static int nci_start_poll(struct nfc_dev *nfc_dev,
670 __u32 im_protocols, __u32 tm_protocols)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300671{
672 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Julien Lefrique772dccf2014-10-21 16:52:44 +0200673 struct nci_rf_discover_param param;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300674 int rc;
675
Ilan Elias019c4fb2012-01-18 13:16:14 +0200676 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100677 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800678 pr_err("unable to start poll, since poll is already active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300679 return -EBUSY;
680 }
681
Ilan Eliasde054792011-09-22 11:13:01 +0300682 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800683 pr_err("there is an active target\n");
Ilan Eliasde054792011-09-22 11:13:01 +0300684 return -EBUSY;
685 }
686
Ilan Elias019c4fb2012-01-18 13:16:14 +0200687 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100688 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200689 pr_debug("target active or w4 select, implicitly deactivate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300690
Christophe Ricard9295b5b2014-12-02 21:27:49 +0100691 rc = nci_request(ndev, nci_rf_deactivate_req,
692 NCI_DEACTIVATE_TYPE_IDLE_MODE,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100693 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300694 if (rc)
695 return -EBUSY;
696 }
697
Julien Lefrique529ee062014-10-21 16:52:47 +0200698 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
Ilan Elias7e035232012-08-15 11:46:22 +0300699 rc = nci_set_local_general_bytes(nfc_dev);
700 if (rc) {
701 pr_err("failed to set local general bytes\n");
702 return rc;
703 }
704 }
705
Julien Lefrique90d78c12014-10-21 16:52:45 +0200706 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
707 rc = nci_set_listen_parameters(nfc_dev);
708 if (rc)
709 pr_err("failed to set listen parameters\n");
710 }
711
Julien Lefrique772dccf2014-10-21 16:52:44 +0200712 param.im_protocols = im_protocols;
713 param.tm_protocols = tm_protocols;
714 rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100715 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300716
717 if (!rc)
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200718 ndev->poll_prots = im_protocols;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300719
720 return rc;
721}
722
723static void nci_stop_poll(struct nfc_dev *nfc_dev)
724{
725 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
726
Ilan Elias019c4fb2012-01-18 13:16:14 +0200727 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100728 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800729 pr_err("unable to stop poll, since poll is not active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300730 return;
731 }
732
Christophe Ricard9295b5b2014-12-02 21:27:49 +0100733 nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100734 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300735}
736
Eric Lapuyade90099432012-05-07 12:31:13 +0200737static int nci_activate_target(struct nfc_dev *nfc_dev,
738 struct nfc_target *target, __u32 protocol)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300739{
740 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200741 struct nci_rf_discover_select_param param;
Eric Lapuyade90099432012-05-07 12:31:13 +0200742 struct nfc_target *nci_target = NULL;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200743 int i;
744 int rc = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300745
Eric Lapuyade90099432012-05-07 12:31:13 +0200746 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300747
Ilan Elias019c4fb2012-01-18 13:16:14 +0200748 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100749 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800750 pr_err("there is no available target to activate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300751 return -EINVAL;
752 }
753
754 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800755 pr_err("there is already an active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300756 return -EBUSY;
757 }
758
Ilan Elias019c4fb2012-01-18 13:16:14 +0200759 for (i = 0; i < ndev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200760 if (ndev->targets[i].idx == target->idx) {
761 nci_target = &ndev->targets[i];
Ilan Elias019c4fb2012-01-18 13:16:14 +0200762 break;
763 }
764 }
765
Eric Lapuyade90099432012-05-07 12:31:13 +0200766 if (!nci_target) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200767 pr_err("unable to find the selected target\n");
768 return -EINVAL;
769 }
770
Eric Lapuyade90099432012-05-07 12:31:13 +0200771 if (!(nci_target->supported_protocols & (1 << protocol))) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800772 pr_err("target does not support the requested protocol 0x%x\n",
773 protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300774 return -EINVAL;
775 }
776
Ilan Elias019c4fb2012-01-18 13:16:14 +0200777 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200778 param.rf_discovery_id = nci_target->logical_idx;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300779
Ilan Elias019c4fb2012-01-18 13:16:14 +0200780 if (protocol == NFC_PROTO_JEWEL)
781 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
782 else if (protocol == NFC_PROTO_MIFARE)
783 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
784 else if (protocol == NFC_PROTO_FELICA)
785 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200786 else if (protocol == NFC_PROTO_ISO14443 ||
787 protocol == NFC_PROTO_ISO14443_B)
Ilan Elias019c4fb2012-01-18 13:16:14 +0200788 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
789 else
790 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
791
792 rc = nci_request(ndev, nci_rf_discover_select_req,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100793 (unsigned long)&param,
794 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
Ilan Elias019c4fb2012-01-18 13:16:14 +0200795 }
796
797 if (!rc)
798 ndev->target_active_prot = protocol;
799
800 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300801}
802
Eric Lapuyade90099432012-05-07 12:31:13 +0200803static void nci_deactivate_target(struct nfc_dev *nfc_dev,
804 struct nfc_target *target)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300805{
806 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
807
Ilan Elias767f19a2012-08-15 11:46:24 +0300808 pr_debug("entry\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300809
810 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800811 pr_err("unable to deactivate target, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300812 return;
813 }
814
815 ndev->target_active_prot = 0;
816
Ilan Elias8939e472012-01-18 13:16:12 +0200817 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
Christophe Ricard9295b5b2014-12-02 21:27:49 +0100818 nci_request(ndev, nci_rf_deactivate_req,
Vincent Cuissard34ac4962015-06-12 15:35:53 +0200819 NCI_DEACTIVATE_TYPE_IDLE_MODE,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100820 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300821 }
822}
823
Ilan Elias767f19a2012-08-15 11:46:24 +0300824static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
825 __u8 comm_mode, __u8 *gb, size_t gb_len)
826{
827 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
828 int rc;
829
830 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
831
832 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
833 if (rc)
834 return rc;
835
836 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
837 ndev->remote_gb_len);
838 if (!rc)
839 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
840 NFC_RF_INITIATOR);
841
842 return rc;
843}
844
845static int nci_dep_link_down(struct nfc_dev *nfc_dev)
846{
Julien Lefriqued7979e12014-10-21 16:52:53 +0200847 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
848 int rc;
849
Ilan Elias767f19a2012-08-15 11:46:24 +0300850 pr_debug("entry\n");
851
Julien Lefriqued7979e12014-10-21 16:52:53 +0200852 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
853 nci_deactivate_target(nfc_dev, NULL);
854 } else {
855 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
856 atomic_read(&ndev->state) == NCI_DISCOVERY) {
857 nci_request(ndev, nci_rf_deactivate_req, 0,
858 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
859 }
860
861 rc = nfc_tm_deactivated(nfc_dev);
862 if (rc)
863 pr_err("error when signaling tm deactivation\n");
864 }
Ilan Elias767f19a2012-08-15 11:46:24 +0300865
866 return 0;
867}
868
869
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200870static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
871 struct sk_buff *skb,
872 data_exchange_cb_t cb, void *cb_context)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300873{
874 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias38f04c62011-09-22 11:36:19 +0300875 int rc;
Christophe Ricard4aeee682015-02-01 22:26:08 +0100876 struct nci_conn_info *conn_info;
877
Christophe Ricard12bdf272015-02-03 19:48:04 +0100878 conn_info = ndev->rf_conn_info;
Christophe Ricard4aeee682015-02-01 22:26:08 +0100879 if (!conn_info)
880 return -EPROTO;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300881
Eric Lapuyade90099432012-05-07 12:31:13 +0200882 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300883
884 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800885 pr_err("unable to exchange data, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300886 return -EINVAL;
887 }
888
Ilan Elias38f04c62011-09-22 11:36:19 +0300889 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
890 return -EBUSY;
891
Ilan Elias6a2968a2011-09-18 11:19:35 +0300892 /* store cb and context to be used on receiving data */
Christophe Ricard4aeee682015-02-01 22:26:08 +0100893 conn_info->data_exchange_cb = cb;
894 conn_info->data_exchange_cb_context = cb_context;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300895
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200896 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
Ilan Elias38f04c62011-09-22 11:36:19 +0300897 if (rc)
898 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
899
900 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300901}
902
Julien Lefrique485f4422014-10-21 16:52:48 +0200903static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
904{
905 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
906 int rc;
907
908 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
909 if (rc)
910 pr_err("unable to send data\n");
911
912 return rc;
913}
914
Samuel Ortiz0a946302013-05-10 11:57:06 +0200915static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
916{
Christophe Ricard93bca2b2014-11-13 00:30:36 +0100917 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
918
919 if (ndev->ops->enable_se)
920 return ndev->ops->enable_se(ndev, se_idx);
921
Samuel Ortiz0a946302013-05-10 11:57:06 +0200922 return 0;
923}
924
925static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
926{
Christophe Ricarde9ef9432014-11-13 00:30:37 +0100927 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
928
929 if (ndev->ops->disable_se)
930 return ndev->ops->disable_se(ndev, se_idx);
931
Samuel Ortiz0a946302013-05-10 11:57:06 +0200932 return 0;
933}
934
935static int nci_discover_se(struct nfc_dev *nfc_dev)
936{
Christophe Ricardfa00e8f2015-02-03 19:48:08 +0100937 int r;
Christophe Ricardba4db552014-11-13 00:30:35 +0100938 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
939
Christophe Ricardfa00e8f2015-02-03 19:48:08 +0100940 if (ndev->ops->discover_se) {
941 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
942 if (r != NCI_STATUS_OK)
943 return -EPROTO;
944
Christophe Ricardba4db552014-11-13 00:30:35 +0100945 return ndev->ops->discover_se(ndev);
Christophe Ricardfa00e8f2015-02-03 19:48:08 +0100946 }
Christophe Ricardba4db552014-11-13 00:30:35 +0100947
Samuel Ortiz0a946302013-05-10 11:57:06 +0200948 return 0;
949}
950
Christophe Ricarda688bf52014-11-13 00:30:38 +0100951static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
952 u8 *apdu, size_t apdu_length,
953 se_io_cb_t cb, void *cb_context)
954{
955 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
956
957 if (ndev->ops->se_io)
958 return ndev->ops->se_io(ndev, se_idx, apdu,
959 apdu_length, cb, cb_context);
960
961 return 0;
962}
963
Clément Perrochaud25af01e2015-03-09 11:12:03 +0100964static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
965{
966 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
967
968 if (!ndev->ops->fw_download)
969 return -ENOTSUPP;
970
971 return ndev->ops->fw_download(ndev, firmware_name);
972}
973
Ilan Elias6a2968a2011-09-18 11:19:35 +0300974static struct nfc_ops nci_nfc_ops = {
975 .dev_up = nci_dev_up,
976 .dev_down = nci_dev_down,
977 .start_poll = nci_start_poll,
978 .stop_poll = nci_stop_poll,
Ilan Elias767f19a2012-08-15 11:46:24 +0300979 .dep_link_up = nci_dep_link_up,
980 .dep_link_down = nci_dep_link_down,
Ilan Elias6a2968a2011-09-18 11:19:35 +0300981 .activate_target = nci_activate_target,
982 .deactivate_target = nci_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200983 .im_transceive = nci_transceive,
Julien Lefrique485f4422014-10-21 16:52:48 +0200984 .tm_send = nci_tm_send,
Samuel Ortiz0a946302013-05-10 11:57:06 +0200985 .enable_se = nci_enable_se,
986 .disable_se = nci_disable_se,
987 .discover_se = nci_discover_se,
Christophe Ricarda688bf52014-11-13 00:30:38 +0100988 .se_io = nci_se_io,
Clément Perrochaud25af01e2015-03-09 11:12:03 +0100989 .fw_download = nci_fw_download,
Ilan Elias6a2968a2011-09-18 11:19:35 +0300990};
991
992/* ---- Interface to NCI drivers ---- */
Ilan Elias6a2968a2011-09-18 11:19:35 +0300993/**
994 * nci_allocate_device - allocate a new nci device
995 *
996 * @ops: device operations
997 * @supported_protocols: NFC protocols supported by the device
998 */
999struct nci_dev *nci_allocate_device(struct nci_ops *ops,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001000 __u32 supported_protocols,
1001 int tx_headroom, int tx_tailroom)
Ilan Elias6a2968a2011-09-18 11:19:35 +03001002{
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001003 struct nci_dev *ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001004
Joe Perches24bf3302011-11-29 11:37:35 -08001005 pr_debug("supported_protocols 0x%x\n", supported_protocols);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001006
1007 if (!ops->open || !ops->close || !ops->send)
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001008 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001009
1010 if (!supported_protocols)
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001011 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001012
1013 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1014 if (!ndev)
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001015 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001016
1017 ndev->ops = ops;
Samuel Ortizb6355e92015-06-06 13:16:37 +02001018
1019 if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1020 pr_err("Too many proprietary commands: %zd\n",
1021 ops->n_prop_ops);
1022 ops->prop_ops = NULL;
1023 ops->n_prop_ops = 0;
1024 }
1025
Ilan Elias6a2968a2011-09-18 11:19:35 +03001026 ndev->tx_headroom = tx_headroom;
1027 ndev->tx_tailroom = tx_tailroom;
Axel Lin9bec44b2014-02-13 13:25:48 +08001028 init_completion(&ndev->req_completion);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001029
1030 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001031 supported_protocols,
1032 tx_headroom + NCI_DATA_HDR_SIZE,
1033 tx_tailroom);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001034 if (!ndev->nfc_dev)
Christophe Ricard11f54f22015-02-01 22:26:14 +01001035 goto free_nci;
1036
1037 ndev->hci_dev = nci_hci_allocate(ndev);
1038 if (!ndev->hci_dev)
1039 goto free_nfc;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001040
1041 nfc_set_drvdata(ndev->nfc_dev, ndev);
1042
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001043 return ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001044
Christophe Ricard11f54f22015-02-01 22:26:14 +01001045free_nfc:
1046 kfree(ndev->nfc_dev);
1047
1048free_nci:
Ilan Elias6a2968a2011-09-18 11:19:35 +03001049 kfree(ndev);
Dan Carpenter8ebafde2011-09-23 09:14:35 +03001050 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001051}
1052EXPORT_SYMBOL(nci_allocate_device);
1053
1054/**
1055 * nci_free_device - deallocate nci device
1056 *
1057 * @ndev: The nci device to deallocate
1058 */
1059void nci_free_device(struct nci_dev *ndev)
1060{
Ilan Elias6a2968a2011-09-18 11:19:35 +03001061 nfc_free_device(ndev->nfc_dev);
1062 kfree(ndev);
1063}
1064EXPORT_SYMBOL(nci_free_device);
1065
1066/**
1067 * nci_register_device - register a nci device in the nfc subsystem
1068 *
1069 * @dev: The nci device to register
1070 */
1071int nci_register_device(struct nci_dev *ndev)
1072{
1073 int rc;
1074 struct device *dev = &ndev->nfc_dev->dev;
1075 char name[32];
1076
Ilan Elias6a2968a2011-09-18 11:19:35 +03001077 ndev->flags = 0;
1078
1079 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1080 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1081 ndev->cmd_wq = create_singlethread_workqueue(name);
1082 if (!ndev->cmd_wq) {
1083 rc = -ENOMEM;
Vincent Cuissard3c1c0f52014-07-22 19:48:39 +02001084 goto exit;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001085 }
1086
1087 INIT_WORK(&ndev->rx_work, nci_rx_work);
1088 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1089 ndev->rx_wq = create_singlethread_workqueue(name);
1090 if (!ndev->rx_wq) {
1091 rc = -ENOMEM;
1092 goto destroy_cmd_wq_exit;
1093 }
1094
1095 INIT_WORK(&ndev->tx_work, nci_tx_work);
1096 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1097 ndev->tx_wq = create_singlethread_workqueue(name);
1098 if (!ndev->tx_wq) {
1099 rc = -ENOMEM;
1100 goto destroy_rx_wq_exit;
1101 }
1102
1103 skb_queue_head_init(&ndev->cmd_q);
1104 skb_queue_head_init(&ndev->rx_q);
1105 skb_queue_head_init(&ndev->tx_q);
1106
1107 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001108 (unsigned long) ndev);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +02001109 setup_timer(&ndev->data_timer, nci_data_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001110 (unsigned long) ndev);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001111
1112 mutex_init(&ndev->req_lock);
Christophe Ricard4aeee682015-02-01 22:26:08 +01001113 INIT_LIST_HEAD(&ndev->conn_info_list);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001114
Vincent Cuissard3c1c0f52014-07-22 19:48:39 +02001115 rc = nfc_register_device(ndev->nfc_dev);
1116 if (rc)
1117 goto destroy_rx_wq_exit;
1118
Ilan Elias6a2968a2011-09-18 11:19:35 +03001119 goto exit;
1120
1121destroy_rx_wq_exit:
1122 destroy_workqueue(ndev->rx_wq);
1123
1124destroy_cmd_wq_exit:
1125 destroy_workqueue(ndev->cmd_wq);
1126
Ilan Elias6a2968a2011-09-18 11:19:35 +03001127exit:
1128 return rc;
1129}
1130EXPORT_SYMBOL(nci_register_device);
1131
1132/**
1133 * nci_unregister_device - unregister a nci device in the nfc subsystem
1134 *
1135 * @dev: The nci device to unregister
1136 */
1137void nci_unregister_device(struct nci_dev *ndev)
1138{
Christophe Ricard4aeee682015-02-01 22:26:08 +01001139 struct nci_conn_info *conn_info, *n;
1140
Ilan Elias6a2968a2011-09-18 11:19:35 +03001141 nci_close_device(ndev);
1142
1143 destroy_workqueue(ndev->cmd_wq);
1144 destroy_workqueue(ndev->rx_wq);
1145 destroy_workqueue(ndev->tx_wq);
1146
Christophe Ricard4aeee682015-02-01 22:26:08 +01001147 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1148 list_del(&conn_info->list);
1149 /* conn_info is allocated with devm_kzalloc */
1150 }
1151
Ilan Elias6a2968a2011-09-18 11:19:35 +03001152 nfc_unregister_device(ndev->nfc_dev);
1153}
1154EXPORT_SYMBOL(nci_unregister_device);
1155
1156/**
1157 * nci_recv_frame - receive frame from NCI drivers
1158 *
Frederic Danis1095e692013-05-22 11:36:17 +02001159 * @ndev: The nci device
Ilan Elias6a2968a2011-09-18 11:19:35 +03001160 * @skb: The sk_buff to receive
1161 */
Frederic Danis1095e692013-05-22 11:36:17 +02001162int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +03001163{
Joe Perches24bf3302011-11-29 11:37:35 -08001164 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001165
Szymon Janc874934f2012-10-04 15:15:51 +02001166 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1167 !test_bit(NCI_INIT, &ndev->flags))) {
Ilan Elias6a2968a2011-09-18 11:19:35 +03001168 kfree_skb(skb);
1169 return -ENXIO;
1170 }
1171
1172 /* Queue frame for rx worker thread */
1173 skb_queue_tail(&ndev->rx_q, skb);
1174 queue_work(ndev->rx_wq, &ndev->rx_work);
1175
1176 return 0;
1177}
1178EXPORT_SYMBOL(nci_recv_frame);
1179
Frederic Danis1095e692013-05-22 11:36:17 +02001180static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +03001181{
Joe Perches24bf3302011-11-29 11:37:35 -08001182 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001183
1184 if (!ndev) {
1185 kfree_skb(skb);
1186 return -ENODEV;
1187 }
1188
1189 /* Get rid of skb owner, prior to sending to the driver. */
1190 skb_orphan(skb);
1191
Hiren Tandel05158292014-05-05 19:52:27 +09001192 /* Send copy to sniffer */
1193 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1194 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1195
Frederic Danis1095e692013-05-22 11:36:17 +02001196 return ndev->ops->send(ndev, skb);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001197}
1198
1199/* Send NCI command */
1200int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1201{
1202 struct nci_ctrl_hdr *hdr;
1203 struct sk_buff *skb;
1204
Joe Perches24bf3302011-11-29 11:37:35 -08001205 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001206
1207 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1208 if (!skb) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001209 pr_err("no memory for command\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +03001210 return -ENOMEM;
1211 }
1212
1213 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
1214 hdr->gid = nci_opcode_gid(opcode);
1215 hdr->oid = nci_opcode_oid(opcode);
1216 hdr->plen = plen;
1217
1218 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1219 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1220
1221 if (plen)
1222 memcpy(skb_put(skb, plen), payload, plen);
1223
Ilan Elias6a2968a2011-09-18 11:19:35 +03001224 skb_queue_tail(&ndev->cmd_q, skb);
1225 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1226
1227 return 0;
1228}
1229
Samuel Ortizb6355e92015-06-06 13:16:37 +02001230/* Proprietary commands API */
1231static struct nci_prop_ops *prop_cmd_lookup(struct nci_dev *ndev,
1232 __u16 opcode)
1233{
1234 size_t i;
1235 struct nci_prop_ops *prop_op;
1236
1237 if (!ndev->ops->prop_ops || !ndev->ops->n_prop_ops)
1238 return NULL;
1239
1240 for (i = 0; i < ndev->ops->n_prop_ops; i++) {
1241 prop_op = &ndev->ops->prop_ops[i];
1242 if (prop_op->opcode == opcode)
1243 return prop_op;
1244 }
1245
1246 return NULL;
1247}
1248
1249int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1250 struct sk_buff *skb)
1251{
1252 struct nci_prop_ops *prop_op;
1253
1254 prop_op = prop_cmd_lookup(ndev, rsp_opcode);
1255 if (!prop_op || !prop_op->rsp)
1256 return -ENOTSUPP;
1257
1258 return prop_op->rsp(ndev, skb);
1259}
1260
1261int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1262 struct sk_buff *skb)
1263{
1264 struct nci_prop_ops *prop_op;
1265
1266 prop_op = prop_cmd_lookup(ndev, ntf_opcode);
1267 if (!prop_op || !prop_op->ntf)
1268 return -ENOTSUPP;
1269
1270 return prop_op->ntf(ndev, skb);
1271}
1272
Ilan Elias6a2968a2011-09-18 11:19:35 +03001273/* ---- NCI TX Data worker thread ---- */
1274
1275static void nci_tx_work(struct work_struct *work)
1276{
1277 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
Christophe Ricard4aeee682015-02-01 22:26:08 +01001278 struct nci_conn_info *conn_info;
Ilan Elias6a2968a2011-09-18 11:19:35 +03001279 struct sk_buff *skb;
1280
Christophe Ricard4aeee682015-02-01 22:26:08 +01001281 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1282 if (!conn_info)
1283 return;
1284
1285 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001286
1287 /* Send queued tx data */
Christophe Ricard4aeee682015-02-01 22:26:08 +01001288 while (atomic_read(&conn_info->credits_cnt)) {
Ilan Elias6a2968a2011-09-18 11:19:35 +03001289 skb = skb_dequeue(&ndev->tx_q);
1290 if (!skb)
1291 return;
1292
Ilan Eliasdb98c822011-11-09 12:09:16 +02001293 /* Check if data flow control is used */
Christophe Ricard4aeee682015-02-01 22:26:08 +01001294 if (atomic_read(&conn_info->credits_cnt) !=
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001295 NCI_DATA_FLOW_CONTROL_NOT_USED)
Christophe Ricard4aeee682015-02-01 22:26:08 +01001296 atomic_dec(&conn_info->credits_cnt);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001297
Joe Perches20c239c2011-11-29 11:37:33 -08001298 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1299 nci_pbf(skb->data),
1300 nci_conn_id(skb->data),
1301 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001302
Frederic Danis1095e692013-05-22 11:36:17 +02001303 nci_send_frame(ndev, skb);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +02001304
1305 mod_timer(&ndev->data_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001306 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001307 }
1308}
1309
1310/* ----- NCI RX worker thread (data & control) ----- */
1311
1312static void nci_rx_work(struct work_struct *work)
1313{
1314 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1315 struct sk_buff *skb;
1316
1317 while ((skb = skb_dequeue(&ndev->rx_q))) {
Hiren Tandel05158292014-05-05 19:52:27 +09001318
1319 /* Send copy to sniffer */
1320 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1321 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1322
Ilan Elias6a2968a2011-09-18 11:19:35 +03001323 /* Process frame */
1324 switch (nci_mt(skb->data)) {
1325 case NCI_MT_RSP_PKT:
1326 nci_rsp_packet(ndev, skb);
1327 break;
1328
1329 case NCI_MT_NTF_PKT:
1330 nci_ntf_packet(ndev, skb);
1331 break;
1332
1333 case NCI_MT_DATA_PKT:
1334 nci_rx_data_packet(ndev, skb);
1335 break;
1336
1337 default:
Joe Perchesed1e0ad2011-11-29 11:37:32 -08001338 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001339 kfree_skb(skb);
1340 break;
1341 }
1342 }
Ilan Eliasc4bf98b2012-01-17 12:03:50 +02001343
1344 /* check if a data exchange timout has occurred */
1345 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1346 /* complete the data exchange transaction, if exists */
1347 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
Christophe Ricard4aeee682015-02-01 22:26:08 +01001348 nci_data_exchange_complete(ndev, NULL,
1349 ndev->cur_conn_id,
1350 -ETIMEDOUT);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +02001351
1352 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1353 }
Ilan Elias6a2968a2011-09-18 11:19:35 +03001354}
1355
1356/* ----- NCI TX CMD worker thread ----- */
1357
1358static void nci_cmd_work(struct work_struct *work)
1359{
1360 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1361 struct sk_buff *skb;
1362
Joe Perches24bf3302011-11-29 11:37:35 -08001363 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001364
1365 /* Send queued command */
1366 if (atomic_read(&ndev->cmd_cnt)) {
1367 skb = skb_dequeue(&ndev->cmd_q);
1368 if (!skb)
1369 return;
1370
1371 atomic_dec(&ndev->cmd_cnt);
1372
Joe Perches20c239c2011-11-29 11:37:33 -08001373 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1374 nci_pbf(skb->data),
1375 nci_opcode_gid(nci_opcode(skb->data)),
1376 nci_opcode_oid(nci_opcode(skb->data)),
1377 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001378
Frederic Danis1095e692013-05-22 11:36:17 +02001379 nci_send_frame(ndev, skb);
Ilan Elias6a2968a2011-09-18 11:19:35 +03001380
1381 mod_timer(&ndev->cmd_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +01001382 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +03001383 }
1384}
Dave Jones8a70e7f2012-07-12 19:17:34 +02001385
1386MODULE_LICENSE("GPL");