]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/nfc/nci/core.c
NFC: NCI: Cancel cmd_timer in nci_close_device()
[mirror_ubuntu-artful-kernel.git] / net / nfc / nci / core.c
CommitLineData
6a2968aa
IE
1/*
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
98b32dec 23 * along with this program; if not, see <http://www.gnu.org/licenses/>.
6a2968aa
IE
24 *
25 */
26
52858b51 27#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
ed1e0ad8 28
8a70e7f8 29#include <linux/module.h>
6a2968aa
IE
30#include <linux/types.h>
31#include <linux/workqueue.h>
32#include <linux/completion.h>
bc3b2d7f 33#include <linux/export.h>
6a2968aa
IE
34#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,
eb9bc6e9
SO
69 void (*req)(struct nci_dev *ndev, unsigned long opt),
70 unsigned long opt, __u32 timeout)
6a2968aa
IE
71{
72 int rc = 0;
f8c141c3 73 long completion_rc;
6a2968aa
IE
74
75 ndev->req_status = NCI_REQ_PEND;
76
77 init_completion(&ndev->req_completion);
78 req(ndev, opt);
eb9bc6e9
SO
79 completion_rc =
80 wait_for_completion_interruptible_timeout(&ndev->req_completion,
81 timeout);
6a2968aa 82
20c239c1 83 pr_debug("wait_for_completion return %ld\n", completion_rc);
6a2968aa
IE
84
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 {
ed1e0ad8
JP
100 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
101 completion_rc);
6a2968aa
IE
102
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,
eb9bc6e9
SO
112 void (*req)(struct nci_dev *ndev,
113 unsigned long opt),
114 unsigned long opt, __u32 timeout)
6a2968aa
IE
115{
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{
e8c0dacd
IE
131 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);
6a2968aa
IE
135}
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{
2eb1dc10
IE
144 struct nci_rf_disc_map_cmd cmd;
145 struct disc_map_config *cfg = cmd.mapping_configs;
146 __u8 *num = &cmd.num_mapping_configs;
6a2968aa
IE
147 int i;
148
6a2968aa 149 /* set rf mapping configurations */
2eb1dc10 150 *num = 0;
6a2968aa
IE
151
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] ==
eb9bc6e9 155 NCI_RF_INTERFACE_ISO_DEP) {
2eb1dc10 156 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
637d85a7
IE
157 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
158 NCI_DISC_MAP_MODE_LISTEN;
159 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
2eb1dc10 160 (*num)++;
6a2968aa 161 } else if (ndev->supported_rf_interfaces[i] ==
eb9bc6e9 162 NCI_RF_INTERFACE_NFC_DEP) {
2eb1dc10 163 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
637d85a7
IE
164 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
165 NCI_DISC_MAP_MODE_LISTEN;
166 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
2eb1dc10 167 (*num)++;
6a2968aa
IE
168 }
169
2eb1dc10 170 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
6a2968aa
IE
171 break;
172 }
173
174 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
eb9bc6e9 175 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
6a2968aa
IE
176}
177
7e035230
IE
178struct 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
6a2968aa
IE
199static 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) &&
874934f4
SJ
207 (protocols & NFC_PROTO_JEWEL_MASK ||
208 protocols & NFC_PROTO_MIFARE_MASK ||
209 protocols & NFC_PROTO_ISO14443_MASK ||
210 protocols & NFC_PROTO_NFC_DEP_MASK)) {
637d85a7 211 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
eb9bc6e9 212 NCI_NFC_A_PASSIVE_POLL_MODE;
6a2968aa
IE
213 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) &&
01d719a2 218 (protocols & NFC_PROTO_ISO14443_B_MASK)) {
637d85a7 219 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
eb9bc6e9 220 NCI_NFC_B_PASSIVE_POLL_MODE;
6a2968aa
IE
221 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) &&
874934f4
SJ
226 (protocols & NFC_PROTO_FELICA_MASK ||
227 protocols & NFC_PROTO_NFC_DEP_MASK)) {
637d85a7 228 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
eb9bc6e9 229 NCI_NFC_F_PASSIVE_POLL_MODE;
6a2968aa
IE
230 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,
eb9bc6e9
SO
235 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
236 &cmd);
6a2968aa
IE
237}
238
019c4fba
IE
239struct 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 =
eb9bc6e9 247 (struct nci_rf_discover_select_param *)opt;
019c4fba
IE
248 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,
eb9bc6e9 268 sizeof(struct nci_rf_discover_select_cmd), &cmd);
019c4fba
IE
269}
270
6a2968aa
IE
271static 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,
eb9bc6e9 278 sizeof(struct nci_rf_deactivate_cmd), &cmd);
6a2968aa
IE
279}
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,
eb9bc6e9 302 msecs_to_jiffies(NCI_RESET_TIMEOUT));
6a2968aa
IE
303
304 if (!rc) {
305 rc = __nci_request(ndev, nci_init_req, 0,
eb9bc6e9 306 msecs_to_jiffies(NCI_INIT_TIMEOUT));
6a2968aa
IE
307 }
308
309 if (!rc) {
310 rc = __nci_request(ndev, nci_init_complete_req, 0,
eb9bc6e9 311 msecs_to_jiffies(NCI_INIT_TIMEOUT));
6a2968aa
IE
312 }
313
314 clear_bit(NCI_INIT, &ndev->flags);
315
316 if (!rc) {
317 set_bit(NCI_UP, &ndev->flags);
019c4fba 318 nci_clear_target_list(ndev);
8939e47f 319 atomic_set(&ndev->state, NCI_IDLE);
6a2968aa
IE
320 } else {
321 /* Init failed, cleanup */
322 skb_queue_purge(&ndev->cmd_q);
323 skb_queue_purge(&ndev->rx_q);
324 skb_queue_purge(&ndev->tx_q);
325
326 ndev->ops->close(ndev);
327 ndev->flags = 0;
328 }
329
330done:
331 mutex_unlock(&ndev->req_lock);
332 return rc;
333}
334
335static int nci_close_device(struct nci_dev *ndev)
336{
337 nci_req_cancel(ndev, ENODEV);
338 mutex_lock(&ndev->req_lock);
339
340 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
341 del_timer_sync(&ndev->cmd_timer);
c4bf98b2 342 del_timer_sync(&ndev->data_timer);
6a2968aa
IE
343 mutex_unlock(&ndev->req_lock);
344 return 0;
345 }
346
347 /* Drop RX and TX queues */
348 skb_queue_purge(&ndev->rx_q);
349 skb_queue_purge(&ndev->tx_q);
350
351 /* Flush RX and TX wq */
352 flush_workqueue(ndev->rx_wq);
353 flush_workqueue(ndev->tx_wq);
354
355 /* Reset device */
356 skb_queue_purge(&ndev->cmd_q);
357 atomic_set(&ndev->cmd_cnt, 1);
358
359 set_bit(NCI_INIT, &ndev->flags);
360 __nci_request(ndev, nci_reset_req, 0,
eb9bc6e9 361 msecs_to_jiffies(NCI_RESET_TIMEOUT));
6a2968aa
IE
362 clear_bit(NCI_INIT, &ndev->flags);
363
fa9be5f0
AK
364 del_timer_sync(&ndev->cmd_timer);
365
6a2968aa
IE
366 /* Flush cmd wq */
367 flush_workqueue(ndev->cmd_wq);
368
369 /* After this point our queues are empty
370 * and no works are scheduled. */
371 ndev->ops->close(ndev);
372
373 /* Clear flags */
374 ndev->flags = 0;
375
376 mutex_unlock(&ndev->req_lock);
377
378 return 0;
379}
380
381/* NCI command timer function */
382static void nci_cmd_timer(unsigned long arg)
383{
384 struct nci_dev *ndev = (void *) arg;
385
6a2968aa
IE
386 atomic_set(&ndev->cmd_cnt, 1);
387 queue_work(ndev->cmd_wq, &ndev->cmd_work);
388}
389
c4bf98b2
IE
390/* NCI data exchange timer function */
391static void nci_data_timer(unsigned long arg)
392{
393 struct nci_dev *ndev = (void *) arg;
394
395 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
396 queue_work(ndev->rx_wq, &ndev->rx_work);
397}
398
6a2968aa
IE
399static int nci_dev_up(struct nfc_dev *nfc_dev)
400{
401 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
402
6a2968aa
IE
403 return nci_open_device(ndev);
404}
405
406static int nci_dev_down(struct nfc_dev *nfc_dev)
407{
408 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
409
6a2968aa
IE
410 return nci_close_device(ndev);
411}
412
7e035230
IE
413static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
414{
415 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
416 struct nci_set_config_param param;
417 __u8 local_gb[NFC_MAX_GT_LEN];
f9fc36f4 418 int i;
7e035230
IE
419
420 param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
421 if ((param.val == NULL) || (param.len == 0))
f9fc36f4 422 return 0;
7e035230 423
460d8f97 424 if (param.len > NFC_MAX_GT_LEN)
7e035230
IE
425 return -EINVAL;
426
427 for (i = 0; i < param.len; i++)
428 local_gb[param.len-1-i] = param.val[i];
429
430 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
431 param.val = local_gb;
432
f9fc36f4
SJ
433 return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
434 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
7e035230
IE
435}
436
fe7c5800
SO
437static int nci_start_poll(struct nfc_dev *nfc_dev,
438 __u32 im_protocols, __u32 tm_protocols)
6a2968aa
IE
439{
440 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
441 int rc;
442
019c4fba 443 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
eb9bc6e9 444 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
ed1e0ad8 445 pr_err("unable to start poll, since poll is already active\n");
6a2968aa
IE
446 return -EBUSY;
447 }
448
de054799 449 if (ndev->target_active_prot) {
ed1e0ad8 450 pr_err("there is an active target\n");
de054799
IE
451 return -EBUSY;
452 }
453
019c4fba 454 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
eb9bc6e9 455 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
019c4fba 456 pr_debug("target active or w4 select, implicitly deactivate\n");
6a2968aa
IE
457
458 rc = nci_request(ndev, nci_rf_deactivate_req, 0,
eb9bc6e9 459 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
6a2968aa
IE
460 if (rc)
461 return -EBUSY;
462 }
463
7e035230
IE
464 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
465 rc = nci_set_local_general_bytes(nfc_dev);
466 if (rc) {
467 pr_err("failed to set local general bytes\n");
468 return rc;
469 }
470 }
471
fe7c5800 472 rc = nci_request(ndev, nci_rf_discover_req, im_protocols,
eb9bc6e9 473 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
6a2968aa
IE
474
475 if (!rc)
fe7c5800 476 ndev->poll_prots = im_protocols;
6a2968aa
IE
477
478 return rc;
479}
480
481static void nci_stop_poll(struct nfc_dev *nfc_dev)
482{
483 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
484
019c4fba 485 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
eb9bc6e9 486 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
ed1e0ad8 487 pr_err("unable to stop poll, since poll is not active\n");
6a2968aa
IE
488 return;
489 }
490
491 nci_request(ndev, nci_rf_deactivate_req, 0,
eb9bc6e9 492 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
6a2968aa
IE
493}
494
90099433
EL
495static int nci_activate_target(struct nfc_dev *nfc_dev,
496 struct nfc_target *target, __u32 protocol)
6a2968aa
IE
497{
498 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
019c4fba 499 struct nci_rf_discover_select_param param;
90099433 500 struct nfc_target *nci_target = NULL;
019c4fba
IE
501 int i;
502 int rc = 0;
6a2968aa 503
90099433 504 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
6a2968aa 505
019c4fba 506 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
eb9bc6e9 507 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
ed1e0ad8 508 pr_err("there is no available target to activate\n");
6a2968aa
IE
509 return -EINVAL;
510 }
511
512 if (ndev->target_active_prot) {
ed1e0ad8 513 pr_err("there is already an active target\n");
6a2968aa
IE
514 return -EBUSY;
515 }
516
019c4fba 517 for (i = 0; i < ndev->n_targets; i++) {
90099433
EL
518 if (ndev->targets[i].idx == target->idx) {
519 nci_target = &ndev->targets[i];
019c4fba
IE
520 break;
521 }
522 }
523
90099433 524 if (!nci_target) {
019c4fba
IE
525 pr_err("unable to find the selected target\n");
526 return -EINVAL;
527 }
528
90099433 529 if (!(nci_target->supported_protocols & (1 << protocol))) {
ed1e0ad8
JP
530 pr_err("target does not support the requested protocol 0x%x\n",
531 protocol);
6a2968aa
IE
532 return -EINVAL;
533 }
534
019c4fba 535 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
90099433 536 param.rf_discovery_id = nci_target->logical_idx;
019c4fba
IE
537
538 if (protocol == NFC_PROTO_JEWEL)
539 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
540 else if (protocol == NFC_PROTO_MIFARE)
541 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
542 else if (protocol == NFC_PROTO_FELICA)
543 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
01d719a2
SO
544 else if (protocol == NFC_PROTO_ISO14443 ||
545 protocol == NFC_PROTO_ISO14443_B)
019c4fba
IE
546 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
547 else
548 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
549
550 rc = nci_request(ndev, nci_rf_discover_select_req,
eb9bc6e9
SO
551 (unsigned long)&param,
552 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
019c4fba 553 }
6a2968aa 554
019c4fba
IE
555 if (!rc)
556 ndev->target_active_prot = protocol;
557
558 return rc;
6a2968aa
IE
559}
560
90099433
EL
561static void nci_deactivate_target(struct nfc_dev *nfc_dev,
562 struct nfc_target *target)
6a2968aa
IE
563{
564 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
565
767f19ae 566 pr_debug("entry\n");
6a2968aa
IE
567
568 if (!ndev->target_active_prot) {
ed1e0ad8 569 pr_err("unable to deactivate target, no active target\n");
6a2968aa
IE
570 return;
571 }
572
573 ndev->target_active_prot = 0;
574
8939e47f 575 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
6a2968aa 576 nci_request(ndev, nci_rf_deactivate_req, 0,
eb9bc6e9 577 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
6a2968aa
IE
578 }
579}
580
767f19ae
IE
581static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
582 __u8 comm_mode, __u8 *gb, size_t gb_len)
583{
584 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
585 int rc;
586
587 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
588
589 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
590 if (rc)
591 return rc;
592
593 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
594 ndev->remote_gb_len);
595 if (!rc)
596 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
597 NFC_RF_INITIATOR);
598
599 return rc;
600}
601
602static int nci_dep_link_down(struct nfc_dev *nfc_dev)
603{
604 pr_debug("entry\n");
605
606 nci_deactivate_target(nfc_dev, NULL);
607
608 return 0;
609}
610
611
be9ae4ce
SO
612static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
613 struct sk_buff *skb,
614 data_exchange_cb_t cb, void *cb_context)
6a2968aa
IE
615{
616 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
38f04c6b 617 int rc;
6a2968aa 618
90099433 619 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
6a2968aa
IE
620
621 if (!ndev->target_active_prot) {
ed1e0ad8 622 pr_err("unable to exchange data, no active target\n");
6a2968aa
IE
623 return -EINVAL;
624 }
625
38f04c6b
IE
626 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
627 return -EBUSY;
628
6a2968aa
IE
629 /* store cb and context to be used on receiving data */
630 ndev->data_exchange_cb = cb;
631 ndev->data_exchange_cb_context = cb_context;
632
e8c0dacd 633 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
38f04c6b
IE
634 if (rc)
635 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
636
637 return rc;
6a2968aa
IE
638}
639
0a946301
SO
640static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
641{
642 return 0;
643}
644
645static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
646{
647 return 0;
648}
649
650static int nci_discover_se(struct nfc_dev *nfc_dev)
651{
652 return 0;
653}
654
6a2968aa
IE
655static struct nfc_ops nci_nfc_ops = {
656 .dev_up = nci_dev_up,
657 .dev_down = nci_dev_down,
658 .start_poll = nci_start_poll,
659 .stop_poll = nci_stop_poll,
767f19ae
IE
660 .dep_link_up = nci_dep_link_up,
661 .dep_link_down = nci_dep_link_down,
6a2968aa
IE
662 .activate_target = nci_activate_target,
663 .deactivate_target = nci_deactivate_target,
be9ae4ce 664 .im_transceive = nci_transceive,
0a946301
SO
665 .enable_se = nci_enable_se,
666 .disable_se = nci_disable_se,
667 .discover_se = nci_discover_se,
6a2968aa
IE
668};
669
670/* ---- Interface to NCI drivers ---- */
671
672/**
673 * nci_allocate_device - allocate a new nci device
674 *
675 * @ops: device operations
676 * @supported_protocols: NFC protocols supported by the device
677 */
678struct nci_dev *nci_allocate_device(struct nci_ops *ops,
eb9bc6e9
SO
679 __u32 supported_protocols,
680 int tx_headroom, int tx_tailroom)
6a2968aa 681{
8ebafde0 682 struct nci_dev *ndev;
6a2968aa 683
24bf3304 684 pr_debug("supported_protocols 0x%x\n", supported_protocols);
6a2968aa
IE
685
686 if (!ops->open || !ops->close || !ops->send)
8ebafde0 687 return NULL;
6a2968aa
IE
688
689 if (!supported_protocols)
8ebafde0 690 return NULL;
6a2968aa
IE
691
692 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
693 if (!ndev)
8ebafde0 694 return NULL;
6a2968aa
IE
695
696 ndev->ops = ops;
697 ndev->tx_headroom = tx_headroom;
698 ndev->tx_tailroom = tx_tailroom;
699
700 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
eb9bc6e9
SO
701 supported_protocols,
702 tx_headroom + NCI_DATA_HDR_SIZE,
703 tx_tailroom);
6a2968aa
IE
704 if (!ndev->nfc_dev)
705 goto free_exit;
706
707 nfc_set_drvdata(ndev->nfc_dev, ndev);
708
8ebafde0 709 return ndev;
6a2968aa
IE
710
711free_exit:
712 kfree(ndev);
8ebafde0 713 return NULL;
6a2968aa
IE
714}
715EXPORT_SYMBOL(nci_allocate_device);
716
717/**
718 * nci_free_device - deallocate nci device
719 *
720 * @ndev: The nci device to deallocate
721 */
722void nci_free_device(struct nci_dev *ndev)
723{
6a2968aa
IE
724 nfc_free_device(ndev->nfc_dev);
725 kfree(ndev);
726}
727EXPORT_SYMBOL(nci_free_device);
728
729/**
730 * nci_register_device - register a nci device in the nfc subsystem
731 *
732 * @dev: The nci device to register
733 */
734int nci_register_device(struct nci_dev *ndev)
735{
736 int rc;
737 struct device *dev = &ndev->nfc_dev->dev;
738 char name[32];
739
6a2968aa
IE
740 rc = nfc_register_device(ndev->nfc_dev);
741 if (rc)
742 goto exit;
743
744 ndev->flags = 0;
745
746 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
747 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
748 ndev->cmd_wq = create_singlethread_workqueue(name);
749 if (!ndev->cmd_wq) {
750 rc = -ENOMEM;
751 goto unreg_exit;
752 }
753
754 INIT_WORK(&ndev->rx_work, nci_rx_work);
755 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
756 ndev->rx_wq = create_singlethread_workqueue(name);
757 if (!ndev->rx_wq) {
758 rc = -ENOMEM;
759 goto destroy_cmd_wq_exit;
760 }
761
762 INIT_WORK(&ndev->tx_work, nci_tx_work);
763 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
764 ndev->tx_wq = create_singlethread_workqueue(name);
765 if (!ndev->tx_wq) {
766 rc = -ENOMEM;
767 goto destroy_rx_wq_exit;
768 }
769
770 skb_queue_head_init(&ndev->cmd_q);
771 skb_queue_head_init(&ndev->rx_q);
772 skb_queue_head_init(&ndev->tx_q);
773
774 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
eb9bc6e9 775 (unsigned long) ndev);
c4bf98b2 776 setup_timer(&ndev->data_timer, nci_data_timer,
eb9bc6e9 777 (unsigned long) ndev);
6a2968aa
IE
778
779 mutex_init(&ndev->req_lock);
780
781 goto exit;
782
783destroy_rx_wq_exit:
784 destroy_workqueue(ndev->rx_wq);
785
786destroy_cmd_wq_exit:
787 destroy_workqueue(ndev->cmd_wq);
788
789unreg_exit:
790 nfc_unregister_device(ndev->nfc_dev);
791
792exit:
793 return rc;
794}
795EXPORT_SYMBOL(nci_register_device);
796
797/**
798 * nci_unregister_device - unregister a nci device in the nfc subsystem
799 *
800 * @dev: The nci device to unregister
801 */
802void nci_unregister_device(struct nci_dev *ndev)
803{
6a2968aa
IE
804 nci_close_device(ndev);
805
806 destroy_workqueue(ndev->cmd_wq);
807 destroy_workqueue(ndev->rx_wq);
808 destroy_workqueue(ndev->tx_wq);
809
810 nfc_unregister_device(ndev->nfc_dev);
811}
812EXPORT_SYMBOL(nci_unregister_device);
813
814/**
815 * nci_recv_frame - receive frame from NCI drivers
816 *
1095e69f 817 * @ndev: The nci device
6a2968aa
IE
818 * @skb: The sk_buff to receive
819 */
1095e69f 820int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
6a2968aa 821{
24bf3304 822 pr_debug("len %d\n", skb->len);
6a2968aa 823
874934f4
SJ
824 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
825 !test_bit(NCI_INIT, &ndev->flags))) {
6a2968aa
IE
826 kfree_skb(skb);
827 return -ENXIO;
828 }
829
830 /* Queue frame for rx worker thread */
831 skb_queue_tail(&ndev->rx_q, skb);
832 queue_work(ndev->rx_wq, &ndev->rx_work);
833
834 return 0;
835}
836EXPORT_SYMBOL(nci_recv_frame);
837
1095e69f 838static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
6a2968aa 839{
24bf3304 840 pr_debug("len %d\n", skb->len);
6a2968aa
IE
841
842 if (!ndev) {
843 kfree_skb(skb);
844 return -ENODEV;
845 }
846
847 /* Get rid of skb owner, prior to sending to the driver. */
848 skb_orphan(skb);
849
1095e69f 850 return ndev->ops->send(ndev, skb);
6a2968aa
IE
851}
852
853/* Send NCI command */
854int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
855{
856 struct nci_ctrl_hdr *hdr;
857 struct sk_buff *skb;
858
24bf3304 859 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
6a2968aa
IE
860
861 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
862 if (!skb) {
ed1e0ad8 863 pr_err("no memory for command\n");
6a2968aa
IE
864 return -ENOMEM;
865 }
866
867 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
868 hdr->gid = nci_opcode_gid(opcode);
869 hdr->oid = nci_opcode_oid(opcode);
870 hdr->plen = plen;
871
872 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
873 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
874
875 if (plen)
876 memcpy(skb_put(skb, plen), payload, plen);
877
6a2968aa
IE
878 skb_queue_tail(&ndev->cmd_q, skb);
879 queue_work(ndev->cmd_wq, &ndev->cmd_work);
880
881 return 0;
882}
883
884/* ---- NCI TX Data worker thread ---- */
885
886static void nci_tx_work(struct work_struct *work)
887{
888 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
889 struct sk_buff *skb;
890
24bf3304 891 pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt));
6a2968aa
IE
892
893 /* Send queued tx data */
894 while (atomic_read(&ndev->credits_cnt)) {
895 skb = skb_dequeue(&ndev->tx_q);
896 if (!skb)
897 return;
898
db98c829
IE
899 /* Check if data flow control is used */
900 if (atomic_read(&ndev->credits_cnt) !=
eb9bc6e9 901 NCI_DATA_FLOW_CONTROL_NOT_USED)
db98c829 902 atomic_dec(&ndev->credits_cnt);
6a2968aa 903
20c239c1
JP
904 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
905 nci_pbf(skb->data),
906 nci_conn_id(skb->data),
907 nci_plen(skb->data));
6a2968aa 908
1095e69f 909 nci_send_frame(ndev, skb);
c4bf98b2
IE
910
911 mod_timer(&ndev->data_timer,
eb9bc6e9 912 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
6a2968aa
IE
913 }
914}
915
916/* ----- NCI RX worker thread (data & control) ----- */
917
918static void nci_rx_work(struct work_struct *work)
919{
920 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
921 struct sk_buff *skb;
922
923 while ((skb = skb_dequeue(&ndev->rx_q))) {
924 /* Process frame */
925 switch (nci_mt(skb->data)) {
926 case NCI_MT_RSP_PKT:
927 nci_rsp_packet(ndev, skb);
928 break;
929
930 case NCI_MT_NTF_PKT:
931 nci_ntf_packet(ndev, skb);
932 break;
933
934 case NCI_MT_DATA_PKT:
935 nci_rx_data_packet(ndev, skb);
936 break;
937
938 default:
ed1e0ad8 939 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
6a2968aa
IE
940 kfree_skb(skb);
941 break;
942 }
943 }
c4bf98b2
IE
944
945 /* check if a data exchange timout has occurred */
946 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
947 /* complete the data exchange transaction, if exists */
948 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
949 nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT);
950
951 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
952 }
6a2968aa
IE
953}
954
955/* ----- NCI TX CMD worker thread ----- */
956
957static void nci_cmd_work(struct work_struct *work)
958{
959 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
960 struct sk_buff *skb;
961
24bf3304 962 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
6a2968aa
IE
963
964 /* Send queued command */
965 if (atomic_read(&ndev->cmd_cnt)) {
966 skb = skb_dequeue(&ndev->cmd_q);
967 if (!skb)
968 return;
969
970 atomic_dec(&ndev->cmd_cnt);
971
20c239c1
JP
972 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
973 nci_pbf(skb->data),
974 nci_opcode_gid(nci_opcode(skb->data)),
975 nci_opcode_oid(nci_opcode(skb->data)),
976 nci_plen(skb->data));
6a2968aa 977
1095e69f 978 nci_send_frame(ndev, skb);
6a2968aa
IE
979
980 mod_timer(&ndev->cmd_timer,
eb9bc6e9 981 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
6a2968aa
IE
982 }
983}
8a70e7f8
DJ
984
985MODULE_LICENSE("GPL");