blob: 56db888b1cd56785e23f0e0272d4ae3af87e32e6 [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.
6 *
7 * Written by Ilan Elias <ilane@ti.com>
8 *
9 * Acknowledgements:
10 * This file is based on hci_core.c, which was written
11 * by Maxim Krasnyansky.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
Jeff Kirsher98b32de2013-12-06 08:56:16 -080023 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Ilan Elias6a2968a2011-09-18 11:19:35 +030024 *
25 */
26
Samuel Ortiz52858b52011-12-14 16:43:05 +010027#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080028
Dave Jones8a70e7f2012-07-12 19:17:34 +020029#include <linux/module.h>
Ilan Elias6a2968a2011-09-18 11:19:35 +030030#include <linux/types.h>
31#include <linux/workqueue.h>
32#include <linux/completion.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040033#include <linux/export.h>
Ilan Elias6a2968a2011-09-18 11:19:35 +030034#include <linux/sched.h>
35#include <linux/bitops.h>
36#include <linux/skbuff.h>
37
38#include "../nfc.h"
39#include <net/nfc/nci.h>
40#include <net/nfc/nci_core.h>
41#include <linux/nfc.h>
42
43static void nci_cmd_work(struct work_struct *work);
44static void nci_rx_work(struct work_struct *work);
45static void nci_tx_work(struct work_struct *work);
46
47/* ---- NCI requests ---- */
48
49void nci_req_complete(struct nci_dev *ndev, int result)
50{
51 if (ndev->req_status == NCI_REQ_PEND) {
52 ndev->req_result = result;
53 ndev->req_status = NCI_REQ_DONE;
54 complete(&ndev->req_completion);
55 }
56}
57
58static void nci_req_cancel(struct nci_dev *ndev, int err)
59{
60 if (ndev->req_status == NCI_REQ_PEND) {
61 ndev->req_result = err;
62 ndev->req_status = NCI_REQ_CANCELED;
63 complete(&ndev->req_completion);
64 }
65}
66
67/* Execute request and wait for completion. */
68static int __nci_request(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010069 void (*req)(struct nci_dev *ndev, unsigned long opt),
70 unsigned long opt, __u32 timeout)
Ilan Elias6a2968a2011-09-18 11:19:35 +030071{
72 int rc = 0;
Dan Carpenterf8c141c2011-12-09 09:35:39 +030073 long completion_rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +030074
75 ndev->req_status = NCI_REQ_PEND;
76
77 init_completion(&ndev->req_completion);
78 req(ndev, opt);
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010079 completion_rc =
80 wait_for_completion_interruptible_timeout(&ndev->req_completion,
81 timeout);
Ilan Elias6a2968a2011-09-18 11:19:35 +030082
Joe Perches20c239c2011-11-29 11:37:33 -080083 pr_debug("wait_for_completion return %ld\n", completion_rc);
Ilan Elias6a2968a2011-09-18 11:19:35 +030084
85 if (completion_rc > 0) {
86 switch (ndev->req_status) {
87 case NCI_REQ_DONE:
88 rc = nci_to_errno(ndev->req_result);
89 break;
90
91 case NCI_REQ_CANCELED:
92 rc = -ndev->req_result;
93 break;
94
95 default:
96 rc = -ETIMEDOUT;
97 break;
98 }
99 } else {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800100 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
101 completion_rc);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300102
103 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
104 }
105
106 ndev->req_status = ndev->req_result = 0;
107
108 return rc;
109}
110
111static inline int nci_request(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100112 void (*req)(struct nci_dev *ndev,
113 unsigned long opt),
114 unsigned long opt, __u32 timeout)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300115{
116 int rc;
117
118 if (!test_bit(NCI_UP, &ndev->flags))
119 return -ENETDOWN;
120
121 /* Serialize all requests */
122 mutex_lock(&ndev->req_lock);
123 rc = __nci_request(ndev, req, opt, timeout);
124 mutex_unlock(&ndev->req_lock);
125
126 return rc;
127}
128
129static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
130{
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200131 struct nci_core_reset_cmd cmd;
132
133 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
134 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300135}
136
137static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
138{
139 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
140}
141
142static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
143{
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300144 struct nci_rf_disc_map_cmd cmd;
145 struct disc_map_config *cfg = cmd.mapping_configs;
146 __u8 *num = &cmd.num_mapping_configs;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300147 int i;
148
Ilan Elias6a2968a2011-09-18 11:19:35 +0300149 /* set rf mapping configurations */
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300150 *num = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300151
152 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
153 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
154 if (ndev->supported_rf_interfaces[i] ==
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100155 NCI_RF_INTERFACE_ISO_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300156 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200157 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
158 NCI_DISC_MAP_MODE_LISTEN;
159 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300160 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300161 } else if (ndev->supported_rf_interfaces[i] ==
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100162 NCI_RF_INTERFACE_NFC_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300163 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
Ilan Elias637d85a2011-12-20 16:57:40 +0200164 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
165 NCI_DISC_MAP_MODE_LISTEN;
166 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300167 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300168 }
169
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300170 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300171 break;
172 }
173
174 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100175 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300176}
177
Ilan Elias7e035232012-08-15 11:46:22 +0300178struct nci_set_config_param {
179 __u8 id;
180 size_t len;
181 __u8 *val;
182};
183
184static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
185{
186 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
187 struct nci_core_set_config_cmd cmd;
188
189 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
190
191 cmd.num_params = 1;
192 cmd.param.id = param->id;
193 cmd.param.len = param->len;
194 memcpy(cmd.param.val, param->val, param->len);
195
196 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
197}
198
Ilan Elias6a2968a2011-09-18 11:19:35 +0300199static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
200{
201 struct nci_rf_disc_cmd cmd;
202 __u32 protocols = opt;
203
204 cmd.num_disc_configs = 0;
205
206 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Szymon Janc874934f2012-10-04 15:15:51 +0200207 (protocols & NFC_PROTO_JEWEL_MASK ||
208 protocols & NFC_PROTO_MIFARE_MASK ||
209 protocols & NFC_PROTO_ISO14443_MASK ||
210 protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200211 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100212 NCI_NFC_A_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300213 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
214 cmd.num_disc_configs++;
215 }
216
217 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200218 (protocols & NFC_PROTO_ISO14443_B_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200219 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100220 NCI_NFC_B_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300221 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
222 cmd.num_disc_configs++;
223 }
224
225 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
Szymon Janc874934f2012-10-04 15:15:51 +0200226 (protocols & NFC_PROTO_FELICA_MASK ||
227 protocols & NFC_PROTO_NFC_DEP_MASK)) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200228 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100229 NCI_NFC_F_PASSIVE_POLL_MODE;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300230 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
231 cmd.num_disc_configs++;
232 }
233
234 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100235 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
236 &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300237}
238
Ilan Elias019c4fb2012-01-18 13:16:14 +0200239struct nci_rf_discover_select_param {
240 __u8 rf_discovery_id;
241 __u8 rf_protocol;
242};
243
244static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
245{
246 struct nci_rf_discover_select_param *param =
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100247 (struct nci_rf_discover_select_param *)opt;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200248 struct nci_rf_discover_select_cmd cmd;
249
250 cmd.rf_discovery_id = param->rf_discovery_id;
251 cmd.rf_protocol = param->rf_protocol;
252
253 switch (cmd.rf_protocol) {
254 case NCI_RF_PROTOCOL_ISO_DEP:
255 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
256 break;
257
258 case NCI_RF_PROTOCOL_NFC_DEP:
259 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
260 break;
261
262 default:
263 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
264 break;
265 }
266
267 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100268 sizeof(struct nci_rf_discover_select_cmd), &cmd);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200269}
270
Ilan Elias6a2968a2011-09-18 11:19:35 +0300271static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
272{
273 struct nci_rf_deactivate_cmd cmd;
274
275 cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
276
277 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100278 sizeof(struct nci_rf_deactivate_cmd), &cmd);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300279}
280
281static int nci_open_device(struct nci_dev *ndev)
282{
283 int rc = 0;
284
285 mutex_lock(&ndev->req_lock);
286
287 if (test_bit(NCI_UP, &ndev->flags)) {
288 rc = -EALREADY;
289 goto done;
290 }
291
292 if (ndev->ops->open(ndev)) {
293 rc = -EIO;
294 goto done;
295 }
296
297 atomic_set(&ndev->cmd_cnt, 1);
298
299 set_bit(NCI_INIT, &ndev->flags);
300
301 rc = __nci_request(ndev, nci_reset_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100302 msecs_to_jiffies(NCI_RESET_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300303
Amitkumar Karwar44a589c2014-02-06 11:28:31 -0800304 if (ndev->ops->setup)
Amitkumar Karwar86e85862014-01-06 12:58:17 -0800305 ndev->ops->setup(ndev);
306
Ilan Elias6a2968a2011-09-18 11:19:35 +0300307 if (!rc) {
308 rc = __nci_request(ndev, nci_init_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100309 msecs_to_jiffies(NCI_INIT_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300310 }
311
312 if (!rc) {
313 rc = __nci_request(ndev, nci_init_complete_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100314 msecs_to_jiffies(NCI_INIT_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300315 }
316
317 clear_bit(NCI_INIT, &ndev->flags);
318
319 if (!rc) {
320 set_bit(NCI_UP, &ndev->flags);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200321 nci_clear_target_list(ndev);
Ilan Elias8939e472012-01-18 13:16:12 +0200322 atomic_set(&ndev->state, NCI_IDLE);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300323 } else {
324 /* Init failed, cleanup */
325 skb_queue_purge(&ndev->cmd_q);
326 skb_queue_purge(&ndev->rx_q);
327 skb_queue_purge(&ndev->tx_q);
328
329 ndev->ops->close(ndev);
330 ndev->flags = 0;
331 }
332
333done:
334 mutex_unlock(&ndev->req_lock);
335 return rc;
336}
337
338static int nci_close_device(struct nci_dev *ndev)
339{
340 nci_req_cancel(ndev, ENODEV);
341 mutex_lock(&ndev->req_lock);
342
343 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
344 del_timer_sync(&ndev->cmd_timer);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200345 del_timer_sync(&ndev->data_timer);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300346 mutex_unlock(&ndev->req_lock);
347 return 0;
348 }
349
350 /* Drop RX and TX queues */
351 skb_queue_purge(&ndev->rx_q);
352 skb_queue_purge(&ndev->tx_q);
353
354 /* Flush RX and TX wq */
355 flush_workqueue(ndev->rx_wq);
356 flush_workqueue(ndev->tx_wq);
357
358 /* Reset device */
359 skb_queue_purge(&ndev->cmd_q);
360 atomic_set(&ndev->cmd_cnt, 1);
361
362 set_bit(NCI_INIT, &ndev->flags);
363 __nci_request(ndev, nci_reset_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100364 msecs_to_jiffies(NCI_RESET_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300365 clear_bit(NCI_INIT, &ndev->flags);
366
Amitkumar Karwarfa9be5f2013-12-23 14:15:13 -0800367 del_timer_sync(&ndev->cmd_timer);
368
Ilan Elias6a2968a2011-09-18 11:19:35 +0300369 /* Flush cmd wq */
370 flush_workqueue(ndev->cmd_wq);
371
372 /* After this point our queues are empty
373 * and no works are scheduled. */
374 ndev->ops->close(ndev);
375
376 /* Clear flags */
377 ndev->flags = 0;
378
379 mutex_unlock(&ndev->req_lock);
380
381 return 0;
382}
383
384/* NCI command timer function */
385static void nci_cmd_timer(unsigned long arg)
386{
387 struct nci_dev *ndev = (void *) arg;
388
Ilan Elias6a2968a2011-09-18 11:19:35 +0300389 atomic_set(&ndev->cmd_cnt, 1);
390 queue_work(ndev->cmd_wq, &ndev->cmd_work);
391}
392
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200393/* NCI data exchange timer function */
394static void nci_data_timer(unsigned long arg)
395{
396 struct nci_dev *ndev = (void *) arg;
397
398 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
399 queue_work(ndev->rx_wq, &ndev->rx_work);
400}
401
Ilan Elias6a2968a2011-09-18 11:19:35 +0300402static int nci_dev_up(struct nfc_dev *nfc_dev)
403{
404 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
405
Ilan Elias6a2968a2011-09-18 11:19:35 +0300406 return nci_open_device(ndev);
407}
408
409static int nci_dev_down(struct nfc_dev *nfc_dev)
410{
411 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
412
Ilan Elias6a2968a2011-09-18 11:19:35 +0300413 return nci_close_device(ndev);
414}
415
Amitkumar Karwar22c15bf2014-01-06 12:58:18 -0800416int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
417{
418 struct nci_set_config_param param;
419
420 if (!val || !len)
421 return 0;
422
423 param.id = id;
424 param.len = len;
425 param.val = val;
426
427 return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
428 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
429}
430EXPORT_SYMBOL(nci_set_config);
431
Ilan Elias7e035232012-08-15 11:46:22 +0300432static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
433{
434 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
435 struct nci_set_config_param param;
Ilan Elias7e035232012-08-15 11:46:22 +0300436
437 param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
438 if ((param.val == NULL) || (param.len == 0))
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200439 return 0;
Ilan Elias7e035232012-08-15 11:46:22 +0300440
Szymon Janc460d8f92012-10-04 15:15:45 +0200441 if (param.len > NFC_MAX_GT_LEN)
Ilan Elias7e035232012-08-15 11:46:22 +0300442 return -EINVAL;
443
Ilan Elias7e035232012-08-15 11:46:22 +0300444 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
Ilan Elias7e035232012-08-15 11:46:22 +0300445
Szymon Jancf9fc36f2012-10-04 15:15:46 +0200446 return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
447 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
Ilan Elias7e035232012-08-15 11:46:22 +0300448}
449
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200450static int nci_start_poll(struct nfc_dev *nfc_dev,
451 __u32 im_protocols, __u32 tm_protocols)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300452{
453 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
454 int rc;
455
Ilan Elias019c4fb2012-01-18 13:16:14 +0200456 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100457 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800458 pr_err("unable to start poll, since poll is already active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300459 return -EBUSY;
460 }
461
Ilan Eliasde054792011-09-22 11:13:01 +0300462 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800463 pr_err("there is an active target\n");
Ilan Eliasde054792011-09-22 11:13:01 +0300464 return -EBUSY;
465 }
466
Ilan Elias019c4fb2012-01-18 13:16:14 +0200467 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100468 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200469 pr_debug("target active or w4 select, implicitly deactivate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300470
471 rc = nci_request(ndev, nci_rf_deactivate_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100472 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300473 if (rc)
474 return -EBUSY;
475 }
476
Ilan Elias7e035232012-08-15 11:46:22 +0300477 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
478 rc = nci_set_local_general_bytes(nfc_dev);
479 if (rc) {
480 pr_err("failed to set local general bytes\n");
481 return rc;
482 }
483 }
484
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200485 rc = nci_request(ndev, nci_rf_discover_req, im_protocols,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100486 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300487
488 if (!rc)
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200489 ndev->poll_prots = im_protocols;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300490
491 return rc;
492}
493
494static void nci_stop_poll(struct nfc_dev *nfc_dev)
495{
496 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
497
Ilan Elias019c4fb2012-01-18 13:16:14 +0200498 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100499 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800500 pr_err("unable to stop poll, since poll is not active\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300501 return;
502 }
503
504 nci_request(ndev, nci_rf_deactivate_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100505 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300506}
507
Eric Lapuyade90099432012-05-07 12:31:13 +0200508static int nci_activate_target(struct nfc_dev *nfc_dev,
509 struct nfc_target *target, __u32 protocol)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300510{
511 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200512 struct nci_rf_discover_select_param param;
Eric Lapuyade90099432012-05-07 12:31:13 +0200513 struct nfc_target *nci_target = NULL;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200514 int i;
515 int rc = 0;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300516
Eric Lapuyade90099432012-05-07 12:31:13 +0200517 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300518
Ilan Elias019c4fb2012-01-18 13:16:14 +0200519 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100520 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800521 pr_err("there is no available target to activate\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300522 return -EINVAL;
523 }
524
525 if (ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800526 pr_err("there is already an active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300527 return -EBUSY;
528 }
529
Ilan Elias019c4fb2012-01-18 13:16:14 +0200530 for (i = 0; i < ndev->n_targets; i++) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200531 if (ndev->targets[i].idx == target->idx) {
532 nci_target = &ndev->targets[i];
Ilan Elias019c4fb2012-01-18 13:16:14 +0200533 break;
534 }
535 }
536
Eric Lapuyade90099432012-05-07 12:31:13 +0200537 if (!nci_target) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200538 pr_err("unable to find the selected target\n");
539 return -EINVAL;
540 }
541
Eric Lapuyade90099432012-05-07 12:31:13 +0200542 if (!(nci_target->supported_protocols & (1 << protocol))) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800543 pr_err("target does not support the requested protocol 0x%x\n",
544 protocol);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300545 return -EINVAL;
546 }
547
Ilan Elias019c4fb2012-01-18 13:16:14 +0200548 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
Eric Lapuyade90099432012-05-07 12:31:13 +0200549 param.rf_discovery_id = nci_target->logical_idx;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300550
Ilan Elias019c4fb2012-01-18 13:16:14 +0200551 if (protocol == NFC_PROTO_JEWEL)
552 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
553 else if (protocol == NFC_PROTO_MIFARE)
554 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
555 else if (protocol == NFC_PROTO_FELICA)
556 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200557 else if (protocol == NFC_PROTO_ISO14443 ||
558 protocol == NFC_PROTO_ISO14443_B)
Ilan Elias019c4fb2012-01-18 13:16:14 +0200559 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
560 else
561 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
562
563 rc = nci_request(ndev, nci_rf_discover_select_req,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100564 (unsigned long)&param,
565 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
Ilan Elias019c4fb2012-01-18 13:16:14 +0200566 }
567
568 if (!rc)
569 ndev->target_active_prot = protocol;
570
571 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300572}
573
Eric Lapuyade90099432012-05-07 12:31:13 +0200574static void nci_deactivate_target(struct nfc_dev *nfc_dev,
575 struct nfc_target *target)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300576{
577 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
578
Ilan Elias767f19a2012-08-15 11:46:24 +0300579 pr_debug("entry\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300580
581 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800582 pr_err("unable to deactivate target, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300583 return;
584 }
585
586 ndev->target_active_prot = 0;
587
Ilan Elias8939e472012-01-18 13:16:12 +0200588 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
Ilan Elias6a2968a2011-09-18 11:19:35 +0300589 nci_request(ndev, nci_rf_deactivate_req, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100590 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300591 }
592}
593
Ilan Elias767f19a2012-08-15 11:46:24 +0300594static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
595 __u8 comm_mode, __u8 *gb, size_t gb_len)
596{
597 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
598 int rc;
599
600 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
601
602 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
603 if (rc)
604 return rc;
605
606 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
607 ndev->remote_gb_len);
608 if (!rc)
609 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
610 NFC_RF_INITIATOR);
611
612 return rc;
613}
614
615static int nci_dep_link_down(struct nfc_dev *nfc_dev)
616{
617 pr_debug("entry\n");
618
619 nci_deactivate_target(nfc_dev, NULL);
620
621 return 0;
622}
623
624
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200625static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
626 struct sk_buff *skb,
627 data_exchange_cb_t cb, void *cb_context)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300628{
629 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
Ilan Elias38f04c62011-09-22 11:36:19 +0300630 int rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300631
Eric Lapuyade90099432012-05-07 12:31:13 +0200632 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300633
634 if (!ndev->target_active_prot) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800635 pr_err("unable to exchange data, no active target\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300636 return -EINVAL;
637 }
638
Ilan Elias38f04c62011-09-22 11:36:19 +0300639 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
640 return -EBUSY;
641
Ilan Elias6a2968a2011-09-18 11:19:35 +0300642 /* store cb and context to be used on receiving data */
643 ndev->data_exchange_cb = cb;
644 ndev->data_exchange_cb_context = cb_context;
645
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200646 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
Ilan Elias38f04c62011-09-22 11:36:19 +0300647 if (rc)
648 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
649
650 return rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300651}
652
Samuel Ortiz0a946302013-05-10 11:57:06 +0200653static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
654{
655 return 0;
656}
657
658static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
659{
660 return 0;
661}
662
663static int nci_discover_se(struct nfc_dev *nfc_dev)
664{
665 return 0;
666}
667
Ilan Elias6a2968a2011-09-18 11:19:35 +0300668static struct nfc_ops nci_nfc_ops = {
669 .dev_up = nci_dev_up,
670 .dev_down = nci_dev_down,
671 .start_poll = nci_start_poll,
672 .stop_poll = nci_stop_poll,
Ilan Elias767f19a2012-08-15 11:46:24 +0300673 .dep_link_up = nci_dep_link_up,
674 .dep_link_down = nci_dep_link_down,
Ilan Elias6a2968a2011-09-18 11:19:35 +0300675 .activate_target = nci_activate_target,
676 .deactivate_target = nci_deactivate_target,
Samuel Ortizbe9ae4c2012-05-16 15:55:48 +0200677 .im_transceive = nci_transceive,
Samuel Ortiz0a946302013-05-10 11:57:06 +0200678 .enable_se = nci_enable_se,
679 .disable_se = nci_disable_se,
680 .discover_se = nci_discover_se,
Ilan Elias6a2968a2011-09-18 11:19:35 +0300681};
682
683/* ---- Interface to NCI drivers ---- */
684
685/**
686 * nci_allocate_device - allocate a new nci device
687 *
688 * @ops: device operations
689 * @supported_protocols: NFC protocols supported by the device
690 */
691struct nci_dev *nci_allocate_device(struct nci_ops *ops,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100692 __u32 supported_protocols,
693 int tx_headroom, int tx_tailroom)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300694{
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300695 struct nci_dev *ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300696
Joe Perches24bf3302011-11-29 11:37:35 -0800697 pr_debug("supported_protocols 0x%x\n", supported_protocols);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300698
699 if (!ops->open || !ops->close || !ops->send)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300700 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300701
702 if (!supported_protocols)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300703 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300704
705 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
706 if (!ndev)
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300707 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300708
709 ndev->ops = ops;
710 ndev->tx_headroom = tx_headroom;
711 ndev->tx_tailroom = tx_tailroom;
712
713 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100714 supported_protocols,
715 tx_headroom + NCI_DATA_HDR_SIZE,
716 tx_tailroom);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300717 if (!ndev->nfc_dev)
718 goto free_exit;
719
720 nfc_set_drvdata(ndev->nfc_dev, ndev);
721
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300722 return ndev;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300723
724free_exit:
725 kfree(ndev);
Dan Carpenter8ebafde2011-09-23 09:14:35 +0300726 return NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300727}
728EXPORT_SYMBOL(nci_allocate_device);
729
730/**
731 * nci_free_device - deallocate nci device
732 *
733 * @ndev: The nci device to deallocate
734 */
735void nci_free_device(struct nci_dev *ndev)
736{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300737 nfc_free_device(ndev->nfc_dev);
738 kfree(ndev);
739}
740EXPORT_SYMBOL(nci_free_device);
741
742/**
743 * nci_register_device - register a nci device in the nfc subsystem
744 *
745 * @dev: The nci device to register
746 */
747int nci_register_device(struct nci_dev *ndev)
748{
749 int rc;
750 struct device *dev = &ndev->nfc_dev->dev;
751 char name[32];
752
Ilan Elias6a2968a2011-09-18 11:19:35 +0300753 rc = nfc_register_device(ndev->nfc_dev);
754 if (rc)
755 goto exit;
756
757 ndev->flags = 0;
758
759 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
760 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
761 ndev->cmd_wq = create_singlethread_workqueue(name);
762 if (!ndev->cmd_wq) {
763 rc = -ENOMEM;
764 goto unreg_exit;
765 }
766
767 INIT_WORK(&ndev->rx_work, nci_rx_work);
768 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
769 ndev->rx_wq = create_singlethread_workqueue(name);
770 if (!ndev->rx_wq) {
771 rc = -ENOMEM;
772 goto destroy_cmd_wq_exit;
773 }
774
775 INIT_WORK(&ndev->tx_work, nci_tx_work);
776 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
777 ndev->tx_wq = create_singlethread_workqueue(name);
778 if (!ndev->tx_wq) {
779 rc = -ENOMEM;
780 goto destroy_rx_wq_exit;
781 }
782
783 skb_queue_head_init(&ndev->cmd_q);
784 skb_queue_head_init(&ndev->rx_q);
785 skb_queue_head_init(&ndev->tx_q);
786
787 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100788 (unsigned long) ndev);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200789 setup_timer(&ndev->data_timer, nci_data_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100790 (unsigned long) ndev);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300791
792 mutex_init(&ndev->req_lock);
793
794 goto exit;
795
796destroy_rx_wq_exit:
797 destroy_workqueue(ndev->rx_wq);
798
799destroy_cmd_wq_exit:
800 destroy_workqueue(ndev->cmd_wq);
801
802unreg_exit:
803 nfc_unregister_device(ndev->nfc_dev);
804
805exit:
806 return rc;
807}
808EXPORT_SYMBOL(nci_register_device);
809
810/**
811 * nci_unregister_device - unregister a nci device in the nfc subsystem
812 *
813 * @dev: The nci device to unregister
814 */
815void nci_unregister_device(struct nci_dev *ndev)
816{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300817 nci_close_device(ndev);
818
819 destroy_workqueue(ndev->cmd_wq);
820 destroy_workqueue(ndev->rx_wq);
821 destroy_workqueue(ndev->tx_wq);
822
823 nfc_unregister_device(ndev->nfc_dev);
824}
825EXPORT_SYMBOL(nci_unregister_device);
826
827/**
828 * nci_recv_frame - receive frame from NCI drivers
829 *
Frederic Danis1095e692013-05-22 11:36:17 +0200830 * @ndev: The nci device
Ilan Elias6a2968a2011-09-18 11:19:35 +0300831 * @skb: The sk_buff to receive
832 */
Frederic Danis1095e692013-05-22 11:36:17 +0200833int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300834{
Joe Perches24bf3302011-11-29 11:37:35 -0800835 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300836
Szymon Janc874934f2012-10-04 15:15:51 +0200837 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
838 !test_bit(NCI_INIT, &ndev->flags))) {
Ilan Elias6a2968a2011-09-18 11:19:35 +0300839 kfree_skb(skb);
840 return -ENXIO;
841 }
842
843 /* Queue frame for rx worker thread */
844 skb_queue_tail(&ndev->rx_q, skb);
845 queue_work(ndev->rx_wq, &ndev->rx_work);
846
847 return 0;
848}
849EXPORT_SYMBOL(nci_recv_frame);
850
Frederic Danis1095e692013-05-22 11:36:17 +0200851static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300852{
Joe Perches24bf3302011-11-29 11:37:35 -0800853 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300854
855 if (!ndev) {
856 kfree_skb(skb);
857 return -ENODEV;
858 }
859
860 /* Get rid of skb owner, prior to sending to the driver. */
861 skb_orphan(skb);
862
Frederic Danis1095e692013-05-22 11:36:17 +0200863 return ndev->ops->send(ndev, skb);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300864}
865
866/* Send NCI command */
867int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
868{
869 struct nci_ctrl_hdr *hdr;
870 struct sk_buff *skb;
871
Joe Perches24bf3302011-11-29 11:37:35 -0800872 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300873
874 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
875 if (!skb) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800876 pr_err("no memory for command\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300877 return -ENOMEM;
878 }
879
880 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
881 hdr->gid = nci_opcode_gid(opcode);
882 hdr->oid = nci_opcode_oid(opcode);
883 hdr->plen = plen;
884
885 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
886 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
887
888 if (plen)
889 memcpy(skb_put(skb, plen), payload, plen);
890
Ilan Elias6a2968a2011-09-18 11:19:35 +0300891 skb_queue_tail(&ndev->cmd_q, skb);
892 queue_work(ndev->cmd_wq, &ndev->cmd_work);
893
894 return 0;
895}
896
897/* ---- NCI TX Data worker thread ---- */
898
899static void nci_tx_work(struct work_struct *work)
900{
901 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
902 struct sk_buff *skb;
903
Joe Perches24bf3302011-11-29 11:37:35 -0800904 pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300905
906 /* Send queued tx data */
907 while (atomic_read(&ndev->credits_cnt)) {
908 skb = skb_dequeue(&ndev->tx_q);
909 if (!skb)
910 return;
911
Ilan Eliasdb98c822011-11-09 12:09:16 +0200912 /* Check if data flow control is used */
913 if (atomic_read(&ndev->credits_cnt) !=
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100914 NCI_DATA_FLOW_CONTROL_NOT_USED)
Ilan Eliasdb98c822011-11-09 12:09:16 +0200915 atomic_dec(&ndev->credits_cnt);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300916
Joe Perches20c239c2011-11-29 11:37:33 -0800917 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
918 nci_pbf(skb->data),
919 nci_conn_id(skb->data),
920 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300921
Frederic Danis1095e692013-05-22 11:36:17 +0200922 nci_send_frame(ndev, skb);
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200923
924 mod_timer(&ndev->data_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100925 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300926 }
927}
928
929/* ----- NCI RX worker thread (data & control) ----- */
930
931static void nci_rx_work(struct work_struct *work)
932{
933 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
934 struct sk_buff *skb;
935
936 while ((skb = skb_dequeue(&ndev->rx_q))) {
937 /* Process frame */
938 switch (nci_mt(skb->data)) {
939 case NCI_MT_RSP_PKT:
940 nci_rsp_packet(ndev, skb);
941 break;
942
943 case NCI_MT_NTF_PKT:
944 nci_ntf_packet(ndev, skb);
945 break;
946
947 case NCI_MT_DATA_PKT:
948 nci_rx_data_packet(ndev, skb);
949 break;
950
951 default:
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800952 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300953 kfree_skb(skb);
954 break;
955 }
956 }
Ilan Eliasc4bf98b2012-01-17 12:03:50 +0200957
958 /* check if a data exchange timout has occurred */
959 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
960 /* complete the data exchange transaction, if exists */
961 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
962 nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT);
963
964 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
965 }
Ilan Elias6a2968a2011-09-18 11:19:35 +0300966}
967
968/* ----- NCI TX CMD worker thread ----- */
969
970static void nci_cmd_work(struct work_struct *work)
971{
972 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
973 struct sk_buff *skb;
974
Joe Perches24bf3302011-11-29 11:37:35 -0800975 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300976
977 /* Send queued command */
978 if (atomic_read(&ndev->cmd_cnt)) {
979 skb = skb_dequeue(&ndev->cmd_q);
980 if (!skb)
981 return;
982
983 atomic_dec(&ndev->cmd_cnt);
984
Joe Perches20c239c2011-11-29 11:37:33 -0800985 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
986 nci_pbf(skb->data),
987 nci_opcode_gid(nci_opcode(skb->data)),
988 nci_opcode_oid(nci_opcode(skb->data)),
989 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300990
Frederic Danis1095e692013-05-22 11:36:17 +0200991 nci_send_frame(ndev, skb);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300992
993 mod_timer(&ndev->cmd_timer,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100994 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300995 }
996}
Dave Jones8a70e7f2012-07-12 19:17:34 +0200997
998MODULE_LICENSE("GPL");