blob: c3dfd4e13bd5c93843bfe1d691247036f96373a8 [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
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28#include <linux/types.h>
29#include <linux/workqueue.h>
30#include <linux/completion.h>
31#include <linux/sched.h>
32#include <linux/bitops.h>
33#include <linux/skbuff.h>
34
35#include "../nfc.h"
36#include <net/nfc/nci.h>
37#include <net/nfc/nci_core.h>
38#include <linux/nfc.h>
39
40static void nci_cmd_work(struct work_struct *work);
41static void nci_rx_work(struct work_struct *work);
42static void nci_tx_work(struct work_struct *work);
43
44/* ---- NCI requests ---- */
45
46void nci_req_complete(struct nci_dev *ndev, int result)
47{
48 if (ndev->req_status == NCI_REQ_PEND) {
49 ndev->req_result = result;
50 ndev->req_status = NCI_REQ_DONE;
51 complete(&ndev->req_completion);
52 }
53}
54
55static void nci_req_cancel(struct nci_dev *ndev, int err)
56{
57 if (ndev->req_status == NCI_REQ_PEND) {
58 ndev->req_result = err;
59 ndev->req_status = NCI_REQ_CANCELED;
60 complete(&ndev->req_completion);
61 }
62}
63
64/* Execute request and wait for completion. */
65static int __nci_request(struct nci_dev *ndev,
66 void (*req)(struct nci_dev *ndev, unsigned long opt),
67 unsigned long opt,
68 __u32 timeout)
69{
70 int rc = 0;
71 unsigned long completion_rc;
72
73 ndev->req_status = NCI_REQ_PEND;
74
75 init_completion(&ndev->req_completion);
76 req(ndev, opt);
77 completion_rc = wait_for_completion_interruptible_timeout(
78 &ndev->req_completion,
79 timeout);
80
81 nfc_dbg("wait_for_completion return %ld", completion_rc);
82
83 if (completion_rc > 0) {
84 switch (ndev->req_status) {
85 case NCI_REQ_DONE:
86 rc = nci_to_errno(ndev->req_result);
87 break;
88
89 case NCI_REQ_CANCELED:
90 rc = -ndev->req_result;
91 break;
92
93 default:
94 rc = -ETIMEDOUT;
95 break;
96 }
97 } else {
98 nfc_err("wait_for_completion_interruptible_timeout failed %ld",
99 completion_rc);
100
101 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
102 }
103
104 ndev->req_status = ndev->req_result = 0;
105
106 return rc;
107}
108
109static inline int nci_request(struct nci_dev *ndev,
110 void (*req)(struct nci_dev *ndev, unsigned long opt),
111 unsigned long opt, __u32 timeout)
112{
113 int rc;
114
115 if (!test_bit(NCI_UP, &ndev->flags))
116 return -ENETDOWN;
117
118 /* Serialize all requests */
119 mutex_lock(&ndev->req_lock);
120 rc = __nci_request(ndev, req, opt, timeout);
121 mutex_unlock(&ndev->req_lock);
122
123 return rc;
124}
125
126static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
127{
128 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 0, NULL);
129}
130
131static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
132{
133 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
134}
135
136static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
137{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300138 struct nci_core_conn_create_cmd conn_cmd;
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300139 struct nci_rf_disc_map_cmd cmd;
140 struct disc_map_config *cfg = cmd.mapping_configs;
141 __u8 *num = &cmd.num_mapping_configs;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300142 int i;
143
144 /* create static rf connection */
145 conn_cmd.target_handle = 0;
146 conn_cmd.num_target_specific_params = 0;
147 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, 2, &conn_cmd);
148
149 /* 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] ==
155 NCI_RF_INTERFACE_ISO_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300156 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
157 cfg[*num].mode = NCI_DISC_MAP_MODE_BOTH;
158 cfg[*num].rf_interface_type = NCI_RF_INTERFACE_ISO_DEP;
159 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300160 } else if (ndev->supported_rf_interfaces[i] ==
161 NCI_RF_INTERFACE_NFC_DEP) {
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300162 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
163 cfg[*num].mode = NCI_DISC_MAP_MODE_BOTH;
164 cfg[*num].rf_interface_type = NCI_RF_INTERFACE_NFC_DEP;
165 (*num)++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300166 }
167
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300168 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300169 break;
170 }
171
172 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
Ilan Elias2eb1dc12011-09-22 10:47:52 +0300173 (1 + ((*num)*sizeof(struct disc_map_config))),
Ilan Elias6a2968a2011-09-18 11:19:35 +0300174 &cmd);
175}
176
177static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
178{
179 struct nci_rf_disc_cmd cmd;
180 __u32 protocols = opt;
181
182 cmd.num_disc_configs = 0;
183
184 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
185 (protocols & NFC_PROTO_JEWEL_MASK
186 || protocols & NFC_PROTO_MIFARE_MASK
187 || protocols & NFC_PROTO_ISO14443_MASK
188 || protocols & NFC_PROTO_NFC_DEP_MASK)) {
189 cmd.disc_configs[cmd.num_disc_configs].type =
190 NCI_DISCOVERY_TYPE_POLL_A_PASSIVE;
191 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
192 cmd.num_disc_configs++;
193 }
194
195 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
196 (protocols & NFC_PROTO_ISO14443_MASK)) {
197 cmd.disc_configs[cmd.num_disc_configs].type =
198 NCI_DISCOVERY_TYPE_POLL_B_PASSIVE;
199 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
200 cmd.num_disc_configs++;
201 }
202
203 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
204 (protocols & NFC_PROTO_FELICA_MASK
205 || protocols & NFC_PROTO_NFC_DEP_MASK)) {
206 cmd.disc_configs[cmd.num_disc_configs].type =
207 NCI_DISCOVERY_TYPE_POLL_F_PASSIVE;
208 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
209 cmd.num_disc_configs++;
210 }
211
212 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
213 (1 + (cmd.num_disc_configs*sizeof(struct disc_config))),
214 &cmd);
215}
216
217static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
218{
219 struct nci_rf_deactivate_cmd cmd;
220
221 cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
222
223 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
224 sizeof(struct nci_rf_deactivate_cmd),
225 &cmd);
226}
227
228static int nci_open_device(struct nci_dev *ndev)
229{
230 int rc = 0;
231
232 mutex_lock(&ndev->req_lock);
233
234 if (test_bit(NCI_UP, &ndev->flags)) {
235 rc = -EALREADY;
236 goto done;
237 }
238
239 if (ndev->ops->open(ndev)) {
240 rc = -EIO;
241 goto done;
242 }
243
244 atomic_set(&ndev->cmd_cnt, 1);
245
246 set_bit(NCI_INIT, &ndev->flags);
247
248 rc = __nci_request(ndev, nci_reset_req, 0,
249 msecs_to_jiffies(NCI_RESET_TIMEOUT));
250
251 if (!rc) {
252 rc = __nci_request(ndev, nci_init_req, 0,
253 msecs_to_jiffies(NCI_INIT_TIMEOUT));
254 }
255
256 if (!rc) {
257 rc = __nci_request(ndev, nci_init_complete_req, 0,
258 msecs_to_jiffies(NCI_INIT_TIMEOUT));
259 }
260
261 clear_bit(NCI_INIT, &ndev->flags);
262
263 if (!rc) {
264 set_bit(NCI_UP, &ndev->flags);
265 } else {
266 /* Init failed, cleanup */
267 skb_queue_purge(&ndev->cmd_q);
268 skb_queue_purge(&ndev->rx_q);
269 skb_queue_purge(&ndev->tx_q);
270
271 ndev->ops->close(ndev);
272 ndev->flags = 0;
273 }
274
275done:
276 mutex_unlock(&ndev->req_lock);
277 return rc;
278}
279
280static int nci_close_device(struct nci_dev *ndev)
281{
282 nci_req_cancel(ndev, ENODEV);
283 mutex_lock(&ndev->req_lock);
284
285 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
286 del_timer_sync(&ndev->cmd_timer);
287 mutex_unlock(&ndev->req_lock);
288 return 0;
289 }
290
291 /* Drop RX and TX queues */
292 skb_queue_purge(&ndev->rx_q);
293 skb_queue_purge(&ndev->tx_q);
294
295 /* Flush RX and TX wq */
296 flush_workqueue(ndev->rx_wq);
297 flush_workqueue(ndev->tx_wq);
298
299 /* Reset device */
300 skb_queue_purge(&ndev->cmd_q);
301 atomic_set(&ndev->cmd_cnt, 1);
302
303 set_bit(NCI_INIT, &ndev->flags);
304 __nci_request(ndev, nci_reset_req, 0,
305 msecs_to_jiffies(NCI_RESET_TIMEOUT));
306 clear_bit(NCI_INIT, &ndev->flags);
307
308 /* Flush cmd wq */
309 flush_workqueue(ndev->cmd_wq);
310
311 /* After this point our queues are empty
312 * and no works are scheduled. */
313 ndev->ops->close(ndev);
314
315 /* Clear flags */
316 ndev->flags = 0;
317
318 mutex_unlock(&ndev->req_lock);
319
320 return 0;
321}
322
323/* NCI command timer function */
324static void nci_cmd_timer(unsigned long arg)
325{
326 struct nci_dev *ndev = (void *) arg;
327
328 nfc_dbg("entry");
329
330 atomic_set(&ndev->cmd_cnt, 1);
331 queue_work(ndev->cmd_wq, &ndev->cmd_work);
332}
333
334static int nci_dev_up(struct nfc_dev *nfc_dev)
335{
336 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
337
338 nfc_dbg("entry");
339
340 return nci_open_device(ndev);
341}
342
343static int nci_dev_down(struct nfc_dev *nfc_dev)
344{
345 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
346
347 nfc_dbg("entry");
348
349 return nci_close_device(ndev);
350}
351
352static int nci_start_poll(struct nfc_dev *nfc_dev, __u32 protocols)
353{
354 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
355 int rc;
356
357 nfc_dbg("entry");
358
359 if (test_bit(NCI_DISCOVERY, &ndev->flags)) {
360 nfc_err("unable to start poll, since poll is already active");
361 return -EBUSY;
362 }
363
364 if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
365 nfc_dbg("target already active, first deactivate...");
366
367 rc = nci_request(ndev, nci_rf_deactivate_req, 0,
368 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
369 if (rc)
370 return -EBUSY;
371 }
372
373 rc = nci_request(ndev, nci_rf_discover_req, protocols,
374 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
375
376 if (!rc)
377 ndev->poll_prots = protocols;
378
379 return rc;
380}
381
382static void nci_stop_poll(struct nfc_dev *nfc_dev)
383{
384 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
385
386 nfc_dbg("entry");
387
388 if (!test_bit(NCI_DISCOVERY, &ndev->flags)) {
389 nfc_err("unable to stop poll, since poll is not active");
390 return;
391 }
392
393 nci_request(ndev, nci_rf_deactivate_req, 0,
394 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
395}
396
397static int nci_activate_target(struct nfc_dev *nfc_dev, __u32 target_idx,
398 __u32 protocol)
399{
400 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
401
402 nfc_dbg("entry, target_idx %d, protocol 0x%x", target_idx, protocol);
403
404 if (!test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
405 nfc_err("there is no available target to activate");
406 return -EINVAL;
407 }
408
409 if (ndev->target_active_prot) {
410 nfc_err("there is already an active target");
411 return -EBUSY;
412 }
413
414 if (!(ndev->target_available_prots & (1 << protocol))) {
415 nfc_err("target does not support the requested protocol 0x%x",
416 protocol);
417 return -EINVAL;
418 }
419
420 ndev->target_active_prot = protocol;
421 ndev->target_available_prots = 0;
422
423 return 0;
424}
425
426static void nci_deactivate_target(struct nfc_dev *nfc_dev, __u32 target_idx)
427{
428 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
429
430 nfc_dbg("entry, target_idx %d", target_idx);
431
432 if (!ndev->target_active_prot) {
433 nfc_err("unable to deactivate target, no active target");
434 return;
435 }
436
437 ndev->target_active_prot = 0;
438
439 if (test_bit(NCI_POLL_ACTIVE, &ndev->flags)) {
440 nci_request(ndev, nci_rf_deactivate_req, 0,
441 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
442 }
443}
444
445static int nci_data_exchange(struct nfc_dev *nfc_dev, __u32 target_idx,
446 struct sk_buff *skb,
447 data_exchange_cb_t cb,
448 void *cb_context)
449{
450 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
451
452 nfc_dbg("entry, target_idx %d, len %d", target_idx, skb->len);
453
454 if (!ndev->target_active_prot) {
455 nfc_err("unable to exchange data, no active target");
456 return -EINVAL;
457 }
458
459 /* store cb and context to be used on receiving data */
460 ndev->data_exchange_cb = cb;
461 ndev->data_exchange_cb_context = cb_context;
462
463 return nci_send_data(ndev, ndev->conn_id, skb);
464}
465
466static struct nfc_ops nci_nfc_ops = {
467 .dev_up = nci_dev_up,
468 .dev_down = nci_dev_down,
469 .start_poll = nci_start_poll,
470 .stop_poll = nci_stop_poll,
471 .activate_target = nci_activate_target,
472 .deactivate_target = nci_deactivate_target,
473 .data_exchange = nci_data_exchange,
474};
475
476/* ---- Interface to NCI drivers ---- */
477
478/**
479 * nci_allocate_device - allocate a new nci device
480 *
481 * @ops: device operations
482 * @supported_protocols: NFC protocols supported by the device
483 */
484struct nci_dev *nci_allocate_device(struct nci_ops *ops,
485 __u32 supported_protocols,
486 int tx_headroom,
487 int tx_tailroom)
488{
489 struct nci_dev *ndev = NULL;
490
491 nfc_dbg("entry, supported_protocols 0x%x", supported_protocols);
492
493 if (!ops->open || !ops->close || !ops->send)
494 goto exit;
495
496 if (!supported_protocols)
497 goto exit;
498
499 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
500 if (!ndev)
501 goto exit;
502
503 ndev->ops = ops;
504 ndev->tx_headroom = tx_headroom;
505 ndev->tx_tailroom = tx_tailroom;
506
507 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
508 supported_protocols,
509 tx_headroom + NCI_DATA_HDR_SIZE,
510 tx_tailroom);
511 if (!ndev->nfc_dev)
512 goto free_exit;
513
514 nfc_set_drvdata(ndev->nfc_dev, ndev);
515
516 goto exit;
517
518free_exit:
519 kfree(ndev);
520
521exit:
522 return ndev;
523}
524EXPORT_SYMBOL(nci_allocate_device);
525
526/**
527 * nci_free_device - deallocate nci device
528 *
529 * @ndev: The nci device to deallocate
530 */
531void nci_free_device(struct nci_dev *ndev)
532{
533 nfc_dbg("entry");
534
535 nfc_free_device(ndev->nfc_dev);
536 kfree(ndev);
537}
538EXPORT_SYMBOL(nci_free_device);
539
540/**
541 * nci_register_device - register a nci device in the nfc subsystem
542 *
543 * @dev: The nci device to register
544 */
545int nci_register_device(struct nci_dev *ndev)
546{
547 int rc;
548 struct device *dev = &ndev->nfc_dev->dev;
549 char name[32];
550
551 nfc_dbg("entry");
552
553 rc = nfc_register_device(ndev->nfc_dev);
554 if (rc)
555 goto exit;
556
557 ndev->flags = 0;
558
559 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
560 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
561 ndev->cmd_wq = create_singlethread_workqueue(name);
562 if (!ndev->cmd_wq) {
563 rc = -ENOMEM;
564 goto unreg_exit;
565 }
566
567 INIT_WORK(&ndev->rx_work, nci_rx_work);
568 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
569 ndev->rx_wq = create_singlethread_workqueue(name);
570 if (!ndev->rx_wq) {
571 rc = -ENOMEM;
572 goto destroy_cmd_wq_exit;
573 }
574
575 INIT_WORK(&ndev->tx_work, nci_tx_work);
576 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
577 ndev->tx_wq = create_singlethread_workqueue(name);
578 if (!ndev->tx_wq) {
579 rc = -ENOMEM;
580 goto destroy_rx_wq_exit;
581 }
582
583 skb_queue_head_init(&ndev->cmd_q);
584 skb_queue_head_init(&ndev->rx_q);
585 skb_queue_head_init(&ndev->tx_q);
586
587 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
588 (unsigned long) ndev);
589
590 mutex_init(&ndev->req_lock);
591
592 goto exit;
593
594destroy_rx_wq_exit:
595 destroy_workqueue(ndev->rx_wq);
596
597destroy_cmd_wq_exit:
598 destroy_workqueue(ndev->cmd_wq);
599
600unreg_exit:
601 nfc_unregister_device(ndev->nfc_dev);
602
603exit:
604 return rc;
605}
606EXPORT_SYMBOL(nci_register_device);
607
608/**
609 * nci_unregister_device - unregister a nci device in the nfc subsystem
610 *
611 * @dev: The nci device to unregister
612 */
613void nci_unregister_device(struct nci_dev *ndev)
614{
615 nfc_dbg("entry");
616
617 nci_close_device(ndev);
618
619 destroy_workqueue(ndev->cmd_wq);
620 destroy_workqueue(ndev->rx_wq);
621 destroy_workqueue(ndev->tx_wq);
622
623 nfc_unregister_device(ndev->nfc_dev);
624}
625EXPORT_SYMBOL(nci_unregister_device);
626
627/**
628 * nci_recv_frame - receive frame from NCI drivers
629 *
630 * @skb: The sk_buff to receive
631 */
632int nci_recv_frame(struct sk_buff *skb)
633{
634 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
635
636 nfc_dbg("entry, len %d", skb->len);
637
638 if (!ndev || (!test_bit(NCI_UP, &ndev->flags)
639 && !test_bit(NCI_INIT, &ndev->flags))) {
640 kfree_skb(skb);
641 return -ENXIO;
642 }
643
644 /* Queue frame for rx worker thread */
645 skb_queue_tail(&ndev->rx_q, skb);
646 queue_work(ndev->rx_wq, &ndev->rx_work);
647
648 return 0;
649}
650EXPORT_SYMBOL(nci_recv_frame);
651
652static int nci_send_frame(struct sk_buff *skb)
653{
654 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
655
656 nfc_dbg("entry, len %d", skb->len);
657
658 if (!ndev) {
659 kfree_skb(skb);
660 return -ENODEV;
661 }
662
663 /* Get rid of skb owner, prior to sending to the driver. */
664 skb_orphan(skb);
665
666 return ndev->ops->send(skb);
667}
668
669/* Send NCI command */
670int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
671{
672 struct nci_ctrl_hdr *hdr;
673 struct sk_buff *skb;
674
675 nfc_dbg("entry, opcode 0x%x, plen %d", opcode, plen);
676
677 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
678 if (!skb) {
679 nfc_err("no memory for command");
680 return -ENOMEM;
681 }
682
683 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
684 hdr->gid = nci_opcode_gid(opcode);
685 hdr->oid = nci_opcode_oid(opcode);
686 hdr->plen = plen;
687
688 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
689 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
690
691 if (plen)
692 memcpy(skb_put(skb, plen), payload, plen);
693
694 skb->dev = (void *) ndev;
695
696 skb_queue_tail(&ndev->cmd_q, skb);
697 queue_work(ndev->cmd_wq, &ndev->cmd_work);
698
699 return 0;
700}
701
702/* ---- NCI TX Data worker thread ---- */
703
704static void nci_tx_work(struct work_struct *work)
705{
706 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
707 struct sk_buff *skb;
708
709 nfc_dbg("entry, credits_cnt %d", atomic_read(&ndev->credits_cnt));
710
711 /* Send queued tx data */
712 while (atomic_read(&ndev->credits_cnt)) {
713 skb = skb_dequeue(&ndev->tx_q);
714 if (!skb)
715 return;
716
717 atomic_dec(&ndev->credits_cnt);
718
719 nfc_dbg("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d",
720 nci_pbf(skb->data),
721 nci_conn_id(skb->data),
722 nci_plen(skb->data));
723
724 nci_send_frame(skb);
725 }
726}
727
728/* ----- NCI RX worker thread (data & control) ----- */
729
730static void nci_rx_work(struct work_struct *work)
731{
732 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
733 struct sk_buff *skb;
734
735 while ((skb = skb_dequeue(&ndev->rx_q))) {
736 /* Process frame */
737 switch (nci_mt(skb->data)) {
738 case NCI_MT_RSP_PKT:
739 nci_rsp_packet(ndev, skb);
740 break;
741
742 case NCI_MT_NTF_PKT:
743 nci_ntf_packet(ndev, skb);
744 break;
745
746 case NCI_MT_DATA_PKT:
747 nci_rx_data_packet(ndev, skb);
748 break;
749
750 default:
751 nfc_err("unknown MT 0x%x", nci_mt(skb->data));
752 kfree_skb(skb);
753 break;
754 }
755 }
756}
757
758/* ----- NCI TX CMD worker thread ----- */
759
760static void nci_cmd_work(struct work_struct *work)
761{
762 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
763 struct sk_buff *skb;
764
765 nfc_dbg("entry, cmd_cnt %d", atomic_read(&ndev->cmd_cnt));
766
767 /* Send queued command */
768 if (atomic_read(&ndev->cmd_cnt)) {
769 skb = skb_dequeue(&ndev->cmd_q);
770 if (!skb)
771 return;
772
773 atomic_dec(&ndev->cmd_cnt);
774
775 nfc_dbg("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d",
776 nci_pbf(skb->data),
777 nci_opcode_gid(nci_opcode(skb->data)),
778 nci_opcode_oid(nci_opcode(skb->data)),
779 nci_plen(skb->data));
780
781 nci_send_frame(skb);
782
783 mod_timer(&ndev->cmd_timer,
784 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
785 }
786}