powerpc/perf: Move BHRB code into CONFIG_PPC64 region
[linux-2.6/btrfs-unstable.git] / net / nfc / nci / core.c
blob48ada0ec749ec369bb6fc78308f12805cfa90c69
1 /*
2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
5 * Copyright (C) 2011 Texas Instruments, Inc.
7 * Written by Ilan Elias <ilane@ti.com>
9 * Acknowledgements:
10 * This file is based on hci_core.c, which was written
11 * by Maxim Krasnyansky.
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
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.
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
28 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
30 #include <linux/module.h>
31 #include <linux/types.h>
32 #include <linux/workqueue.h>
33 #include <linux/completion.h>
34 #include <linux/export.h>
35 #include <linux/sched.h>
36 #include <linux/bitops.h>
37 #include <linux/skbuff.h>
39 #include "../nfc.h"
40 #include <net/nfc/nci.h>
41 #include <net/nfc/nci_core.h>
42 #include <linux/nfc.h>
44 static void nci_cmd_work(struct work_struct *work);
45 static void nci_rx_work(struct work_struct *work);
46 static void nci_tx_work(struct work_struct *work);
48 /* ---- NCI requests ---- */
50 void nci_req_complete(struct nci_dev *ndev, int result)
52 if (ndev->req_status == NCI_REQ_PEND) {
53 ndev->req_result = result;
54 ndev->req_status = NCI_REQ_DONE;
55 complete(&ndev->req_completion);
59 static void nci_req_cancel(struct nci_dev *ndev, int err)
61 if (ndev->req_status == NCI_REQ_PEND) {
62 ndev->req_result = err;
63 ndev->req_status = NCI_REQ_CANCELED;
64 complete(&ndev->req_completion);
68 /* Execute request and wait for completion. */
69 static int __nci_request(struct nci_dev *ndev,
70 void (*req)(struct nci_dev *ndev, unsigned long opt),
71 unsigned long opt, __u32 timeout)
73 int rc = 0;
74 long completion_rc;
76 ndev->req_status = NCI_REQ_PEND;
78 init_completion(&ndev->req_completion);
79 req(ndev, opt);
80 completion_rc =
81 wait_for_completion_interruptible_timeout(&ndev->req_completion,
82 timeout);
84 pr_debug("wait_for_completion return %ld\n", completion_rc);
86 if (completion_rc > 0) {
87 switch (ndev->req_status) {
88 case NCI_REQ_DONE:
89 rc = nci_to_errno(ndev->req_result);
90 break;
92 case NCI_REQ_CANCELED:
93 rc = -ndev->req_result;
94 break;
96 default:
97 rc = -ETIMEDOUT;
98 break;
100 } else {
101 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
102 completion_rc);
104 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
107 ndev->req_status = ndev->req_result = 0;
109 return rc;
112 static inline int nci_request(struct nci_dev *ndev,
113 void (*req)(struct nci_dev *ndev,
114 unsigned long opt),
115 unsigned long opt, __u32 timeout)
117 int rc;
119 if (!test_bit(NCI_UP, &ndev->flags))
120 return -ENETDOWN;
122 /* Serialize all requests */
123 mutex_lock(&ndev->req_lock);
124 rc = __nci_request(ndev, req, opt, timeout);
125 mutex_unlock(&ndev->req_lock);
127 return rc;
130 static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
132 struct nci_core_reset_cmd cmd;
134 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
135 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
138 static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
140 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
143 static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
145 struct nci_rf_disc_map_cmd cmd;
146 struct disc_map_config *cfg = cmd.mapping_configs;
147 __u8 *num = &cmd.num_mapping_configs;
148 int i;
150 /* set rf mapping configurations */
151 *num = 0;
153 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
154 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
155 if (ndev->supported_rf_interfaces[i] ==
156 NCI_RF_INTERFACE_ISO_DEP) {
157 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
158 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
159 NCI_DISC_MAP_MODE_LISTEN;
160 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
161 (*num)++;
162 } else if (ndev->supported_rf_interfaces[i] ==
163 NCI_RF_INTERFACE_NFC_DEP) {
164 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
165 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
166 NCI_DISC_MAP_MODE_LISTEN;
167 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
168 (*num)++;
171 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
172 break;
175 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
176 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
179 struct nci_set_config_param {
180 __u8 id;
181 size_t len;
182 __u8 *val;
185 static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
187 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
188 struct nci_core_set_config_cmd cmd;
190 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
192 cmd.num_params = 1;
193 cmd.param.id = param->id;
194 cmd.param.len = param->len;
195 memcpy(cmd.param.val, param->val, param->len);
197 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
200 static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
202 struct nci_rf_disc_cmd cmd;
203 __u32 protocols = opt;
205 cmd.num_disc_configs = 0;
207 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
208 (protocols & NFC_PROTO_JEWEL_MASK ||
209 protocols & NFC_PROTO_MIFARE_MASK ||
210 protocols & NFC_PROTO_ISO14443_MASK ||
211 protocols & NFC_PROTO_NFC_DEP_MASK)) {
212 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
213 NCI_NFC_A_PASSIVE_POLL_MODE;
214 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
215 cmd.num_disc_configs++;
218 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
219 (protocols & NFC_PROTO_ISO14443_B_MASK)) {
220 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
221 NCI_NFC_B_PASSIVE_POLL_MODE;
222 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
223 cmd.num_disc_configs++;
226 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
227 (protocols & NFC_PROTO_FELICA_MASK ||
228 protocols & NFC_PROTO_NFC_DEP_MASK)) {
229 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
230 NCI_NFC_F_PASSIVE_POLL_MODE;
231 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
232 cmd.num_disc_configs++;
235 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
236 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
237 &cmd);
240 struct nci_rf_discover_select_param {
241 __u8 rf_discovery_id;
242 __u8 rf_protocol;
245 static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
247 struct nci_rf_discover_select_param *param =
248 (struct nci_rf_discover_select_param *)opt;
249 struct nci_rf_discover_select_cmd cmd;
251 cmd.rf_discovery_id = param->rf_discovery_id;
252 cmd.rf_protocol = param->rf_protocol;
254 switch (cmd.rf_protocol) {
255 case NCI_RF_PROTOCOL_ISO_DEP:
256 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
257 break;
259 case NCI_RF_PROTOCOL_NFC_DEP:
260 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
261 break;
263 default:
264 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
265 break;
268 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
269 sizeof(struct nci_rf_discover_select_cmd), &cmd);
272 static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
274 struct nci_rf_deactivate_cmd cmd;
276 cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE;
278 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
279 sizeof(struct nci_rf_deactivate_cmd), &cmd);
282 static int nci_open_device(struct nci_dev *ndev)
284 int rc = 0;
286 mutex_lock(&ndev->req_lock);
288 if (test_bit(NCI_UP, &ndev->flags)) {
289 rc = -EALREADY;
290 goto done;
293 if (ndev->ops->open(ndev)) {
294 rc = -EIO;
295 goto done;
298 atomic_set(&ndev->cmd_cnt, 1);
300 set_bit(NCI_INIT, &ndev->flags);
302 rc = __nci_request(ndev, nci_reset_req, 0,
303 msecs_to_jiffies(NCI_RESET_TIMEOUT));
305 if (!rc) {
306 rc = __nci_request(ndev, nci_init_req, 0,
307 msecs_to_jiffies(NCI_INIT_TIMEOUT));
310 if (!rc) {
311 rc = __nci_request(ndev, nci_init_complete_req, 0,
312 msecs_to_jiffies(NCI_INIT_TIMEOUT));
315 clear_bit(NCI_INIT, &ndev->flags);
317 if (!rc) {
318 set_bit(NCI_UP, &ndev->flags);
319 nci_clear_target_list(ndev);
320 atomic_set(&ndev->state, NCI_IDLE);
321 } else {
322 /* Init failed, cleanup */
323 skb_queue_purge(&ndev->cmd_q);
324 skb_queue_purge(&ndev->rx_q);
325 skb_queue_purge(&ndev->tx_q);
327 ndev->ops->close(ndev);
328 ndev->flags = 0;
331 done:
332 mutex_unlock(&ndev->req_lock);
333 return rc;
336 static int nci_close_device(struct nci_dev *ndev)
338 nci_req_cancel(ndev, ENODEV);
339 mutex_lock(&ndev->req_lock);
341 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
342 del_timer_sync(&ndev->cmd_timer);
343 del_timer_sync(&ndev->data_timer);
344 mutex_unlock(&ndev->req_lock);
345 return 0;
348 /* Drop RX and TX queues */
349 skb_queue_purge(&ndev->rx_q);
350 skb_queue_purge(&ndev->tx_q);
352 /* Flush RX and TX wq */
353 flush_workqueue(ndev->rx_wq);
354 flush_workqueue(ndev->tx_wq);
356 /* Reset device */
357 skb_queue_purge(&ndev->cmd_q);
358 atomic_set(&ndev->cmd_cnt, 1);
360 set_bit(NCI_INIT, &ndev->flags);
361 __nci_request(ndev, nci_reset_req, 0,
362 msecs_to_jiffies(NCI_RESET_TIMEOUT));
363 clear_bit(NCI_INIT, &ndev->flags);
365 /* Flush cmd wq */
366 flush_workqueue(ndev->cmd_wq);
368 /* After this point our queues are empty
369 * and no works are scheduled. */
370 ndev->ops->close(ndev);
372 /* Clear flags */
373 ndev->flags = 0;
375 mutex_unlock(&ndev->req_lock);
377 return 0;
380 /* NCI command timer function */
381 static void nci_cmd_timer(unsigned long arg)
383 struct nci_dev *ndev = (void *) arg;
385 atomic_set(&ndev->cmd_cnt, 1);
386 queue_work(ndev->cmd_wq, &ndev->cmd_work);
389 /* NCI data exchange timer function */
390 static void nci_data_timer(unsigned long arg)
392 struct nci_dev *ndev = (void *) arg;
394 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
395 queue_work(ndev->rx_wq, &ndev->rx_work);
398 static int nci_dev_up(struct nfc_dev *nfc_dev)
400 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
402 return nci_open_device(ndev);
405 static int nci_dev_down(struct nfc_dev *nfc_dev)
407 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
409 return nci_close_device(ndev);
412 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
414 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
415 struct nci_set_config_param param;
416 __u8 local_gb[NFC_MAX_GT_LEN];
417 int i;
419 param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
420 if ((param.val == NULL) || (param.len == 0))
421 return 0;
423 if (param.len > NFC_MAX_GT_LEN)
424 return -EINVAL;
426 for (i = 0; i < param.len; i++)
427 local_gb[param.len-1-i] = param.val[i];
429 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
430 param.val = local_gb;
432 return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
433 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
436 static int nci_start_poll(struct nfc_dev *nfc_dev,
437 __u32 im_protocols, __u32 tm_protocols)
439 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
440 int rc;
442 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
443 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
444 pr_err("unable to start poll, since poll is already active\n");
445 return -EBUSY;
448 if (ndev->target_active_prot) {
449 pr_err("there is an active target\n");
450 return -EBUSY;
453 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
454 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
455 pr_debug("target active or w4 select, implicitly deactivate\n");
457 rc = nci_request(ndev, nci_rf_deactivate_req, 0,
458 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
459 if (rc)
460 return -EBUSY;
463 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
464 rc = nci_set_local_general_bytes(nfc_dev);
465 if (rc) {
466 pr_err("failed to set local general bytes\n");
467 return rc;
471 rc = nci_request(ndev, nci_rf_discover_req, im_protocols,
472 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
474 if (!rc)
475 ndev->poll_prots = im_protocols;
477 return rc;
480 static void nci_stop_poll(struct nfc_dev *nfc_dev)
482 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
484 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
485 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
486 pr_err("unable to stop poll, since poll is not active\n");
487 return;
490 nci_request(ndev, nci_rf_deactivate_req, 0,
491 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
494 static int nci_activate_target(struct nfc_dev *nfc_dev,
495 struct nfc_target *target, __u32 protocol)
497 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
498 struct nci_rf_discover_select_param param;
499 struct nfc_target *nci_target = NULL;
500 int i;
501 int rc = 0;
503 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
505 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
506 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
507 pr_err("there is no available target to activate\n");
508 return -EINVAL;
511 if (ndev->target_active_prot) {
512 pr_err("there is already an active target\n");
513 return -EBUSY;
516 for (i = 0; i < ndev->n_targets; i++) {
517 if (ndev->targets[i].idx == target->idx) {
518 nci_target = &ndev->targets[i];
519 break;
523 if (!nci_target) {
524 pr_err("unable to find the selected target\n");
525 return -EINVAL;
528 if (!(nci_target->supported_protocols & (1 << protocol))) {
529 pr_err("target does not support the requested protocol 0x%x\n",
530 protocol);
531 return -EINVAL;
534 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
535 param.rf_discovery_id = nci_target->logical_idx;
537 if (protocol == NFC_PROTO_JEWEL)
538 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
539 else if (protocol == NFC_PROTO_MIFARE)
540 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
541 else if (protocol == NFC_PROTO_FELICA)
542 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
543 else if (protocol == NFC_PROTO_ISO14443 ||
544 protocol == NFC_PROTO_ISO14443_B)
545 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
546 else
547 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
549 rc = nci_request(ndev, nci_rf_discover_select_req,
550 (unsigned long)&param,
551 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
554 if (!rc)
555 ndev->target_active_prot = protocol;
557 return rc;
560 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
561 struct nfc_target *target)
563 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
565 pr_debug("entry\n");
567 if (!ndev->target_active_prot) {
568 pr_err("unable to deactivate target, no active target\n");
569 return;
572 ndev->target_active_prot = 0;
574 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
575 nci_request(ndev, nci_rf_deactivate_req, 0,
576 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
580 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
581 __u8 comm_mode, __u8 *gb, size_t gb_len)
583 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
584 int rc;
586 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
588 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
589 if (rc)
590 return rc;
592 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
593 ndev->remote_gb_len);
594 if (!rc)
595 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
596 NFC_RF_INITIATOR);
598 return rc;
601 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
603 pr_debug("entry\n");
605 nci_deactivate_target(nfc_dev, NULL);
607 return 0;
611 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
612 struct sk_buff *skb,
613 data_exchange_cb_t cb, void *cb_context)
615 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
616 int rc;
618 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
620 if (!ndev->target_active_prot) {
621 pr_err("unable to exchange data, no active target\n");
622 return -EINVAL;
625 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
626 return -EBUSY;
628 /* store cb and context to be used on receiving data */
629 ndev->data_exchange_cb = cb;
630 ndev->data_exchange_cb_context = cb_context;
632 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
633 if (rc)
634 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
636 return rc;
639 static struct nfc_ops nci_nfc_ops = {
640 .dev_up = nci_dev_up,
641 .dev_down = nci_dev_down,
642 .start_poll = nci_start_poll,
643 .stop_poll = nci_stop_poll,
644 .dep_link_up = nci_dep_link_up,
645 .dep_link_down = nci_dep_link_down,
646 .activate_target = nci_activate_target,
647 .deactivate_target = nci_deactivate_target,
648 .im_transceive = nci_transceive,
651 /* ---- Interface to NCI drivers ---- */
654 * nci_allocate_device - allocate a new nci device
656 * @ops: device operations
657 * @supported_protocols: NFC protocols supported by the device
659 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
660 __u32 supported_protocols,
661 __u32 supported_se,
662 int tx_headroom, int tx_tailroom)
664 struct nci_dev *ndev;
666 pr_debug("supported_protocols 0x%x\n", supported_protocols);
668 if (!ops->open || !ops->close || !ops->send)
669 return NULL;
671 if (!supported_protocols)
672 return NULL;
674 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
675 if (!ndev)
676 return NULL;
678 ndev->ops = ops;
679 ndev->tx_headroom = tx_headroom;
680 ndev->tx_tailroom = tx_tailroom;
682 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
683 supported_protocols,
684 supported_se,
685 tx_headroom + NCI_DATA_HDR_SIZE,
686 tx_tailroom);
687 if (!ndev->nfc_dev)
688 goto free_exit;
690 nfc_set_drvdata(ndev->nfc_dev, ndev);
692 return ndev;
694 free_exit:
695 kfree(ndev);
696 return NULL;
698 EXPORT_SYMBOL(nci_allocate_device);
701 * nci_free_device - deallocate nci device
703 * @ndev: The nci device to deallocate
705 void nci_free_device(struct nci_dev *ndev)
707 nfc_free_device(ndev->nfc_dev);
708 kfree(ndev);
710 EXPORT_SYMBOL(nci_free_device);
713 * nci_register_device - register a nci device in the nfc subsystem
715 * @dev: The nci device to register
717 int nci_register_device(struct nci_dev *ndev)
719 int rc;
720 struct device *dev = &ndev->nfc_dev->dev;
721 char name[32];
723 rc = nfc_register_device(ndev->nfc_dev);
724 if (rc)
725 goto exit;
727 ndev->flags = 0;
729 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
730 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
731 ndev->cmd_wq = create_singlethread_workqueue(name);
732 if (!ndev->cmd_wq) {
733 rc = -ENOMEM;
734 goto unreg_exit;
737 INIT_WORK(&ndev->rx_work, nci_rx_work);
738 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
739 ndev->rx_wq = create_singlethread_workqueue(name);
740 if (!ndev->rx_wq) {
741 rc = -ENOMEM;
742 goto destroy_cmd_wq_exit;
745 INIT_WORK(&ndev->tx_work, nci_tx_work);
746 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
747 ndev->tx_wq = create_singlethread_workqueue(name);
748 if (!ndev->tx_wq) {
749 rc = -ENOMEM;
750 goto destroy_rx_wq_exit;
753 skb_queue_head_init(&ndev->cmd_q);
754 skb_queue_head_init(&ndev->rx_q);
755 skb_queue_head_init(&ndev->tx_q);
757 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
758 (unsigned long) ndev);
759 setup_timer(&ndev->data_timer, nci_data_timer,
760 (unsigned long) ndev);
762 mutex_init(&ndev->req_lock);
764 goto exit;
766 destroy_rx_wq_exit:
767 destroy_workqueue(ndev->rx_wq);
769 destroy_cmd_wq_exit:
770 destroy_workqueue(ndev->cmd_wq);
772 unreg_exit:
773 nfc_unregister_device(ndev->nfc_dev);
775 exit:
776 return rc;
778 EXPORT_SYMBOL(nci_register_device);
781 * nci_unregister_device - unregister a nci device in the nfc subsystem
783 * @dev: The nci device to unregister
785 void nci_unregister_device(struct nci_dev *ndev)
787 nci_close_device(ndev);
789 destroy_workqueue(ndev->cmd_wq);
790 destroy_workqueue(ndev->rx_wq);
791 destroy_workqueue(ndev->tx_wq);
793 nfc_unregister_device(ndev->nfc_dev);
795 EXPORT_SYMBOL(nci_unregister_device);
798 * nci_recv_frame - receive frame from NCI drivers
800 * @skb: The sk_buff to receive
802 int nci_recv_frame(struct sk_buff *skb)
804 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
806 pr_debug("len %d\n", skb->len);
808 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
809 !test_bit(NCI_INIT, &ndev->flags))) {
810 kfree_skb(skb);
811 return -ENXIO;
814 /* Queue frame for rx worker thread */
815 skb_queue_tail(&ndev->rx_q, skb);
816 queue_work(ndev->rx_wq, &ndev->rx_work);
818 return 0;
820 EXPORT_SYMBOL(nci_recv_frame);
822 static int nci_send_frame(struct sk_buff *skb)
824 struct nci_dev *ndev = (struct nci_dev *) skb->dev;
826 pr_debug("len %d\n", skb->len);
828 if (!ndev) {
829 kfree_skb(skb);
830 return -ENODEV;
833 /* Get rid of skb owner, prior to sending to the driver. */
834 skb_orphan(skb);
836 return ndev->ops->send(skb);
839 /* Send NCI command */
840 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
842 struct nci_ctrl_hdr *hdr;
843 struct sk_buff *skb;
845 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
847 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
848 if (!skb) {
849 pr_err("no memory for command\n");
850 return -ENOMEM;
853 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
854 hdr->gid = nci_opcode_gid(opcode);
855 hdr->oid = nci_opcode_oid(opcode);
856 hdr->plen = plen;
858 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
859 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
861 if (plen)
862 memcpy(skb_put(skb, plen), payload, plen);
864 skb->dev = (void *) ndev;
866 skb_queue_tail(&ndev->cmd_q, skb);
867 queue_work(ndev->cmd_wq, &ndev->cmd_work);
869 return 0;
872 /* ---- NCI TX Data worker thread ---- */
874 static void nci_tx_work(struct work_struct *work)
876 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
877 struct sk_buff *skb;
879 pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt));
881 /* Send queued tx data */
882 while (atomic_read(&ndev->credits_cnt)) {
883 skb = skb_dequeue(&ndev->tx_q);
884 if (!skb)
885 return;
887 /* Check if data flow control is used */
888 if (atomic_read(&ndev->credits_cnt) !=
889 NCI_DATA_FLOW_CONTROL_NOT_USED)
890 atomic_dec(&ndev->credits_cnt);
892 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
893 nci_pbf(skb->data),
894 nci_conn_id(skb->data),
895 nci_plen(skb->data));
897 nci_send_frame(skb);
899 mod_timer(&ndev->data_timer,
900 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
904 /* ----- NCI RX worker thread (data & control) ----- */
906 static void nci_rx_work(struct work_struct *work)
908 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
909 struct sk_buff *skb;
911 while ((skb = skb_dequeue(&ndev->rx_q))) {
912 /* Process frame */
913 switch (nci_mt(skb->data)) {
914 case NCI_MT_RSP_PKT:
915 nci_rsp_packet(ndev, skb);
916 break;
918 case NCI_MT_NTF_PKT:
919 nci_ntf_packet(ndev, skb);
920 break;
922 case NCI_MT_DATA_PKT:
923 nci_rx_data_packet(ndev, skb);
924 break;
926 default:
927 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
928 kfree_skb(skb);
929 break;
933 /* check if a data exchange timout has occurred */
934 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
935 /* complete the data exchange transaction, if exists */
936 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
937 nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT);
939 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
943 /* ----- NCI TX CMD worker thread ----- */
945 static void nci_cmd_work(struct work_struct *work)
947 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
948 struct sk_buff *skb;
950 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
952 /* Send queued command */
953 if (atomic_read(&ndev->cmd_cnt)) {
954 skb = skb_dequeue(&ndev->cmd_q);
955 if (!skb)
956 return;
958 atomic_dec(&ndev->cmd_cnt);
960 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
961 nci_pbf(skb->data),
962 nci_opcode_gid(nci_opcode(skb->data)),
963 nci_opcode_oid(nci_opcode(skb->data)),
964 nci_plen(skb->data));
966 nci_send_frame(skb);
968 mod_timer(&ndev->cmd_timer,
969 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
973 MODULE_LICENSE("GPL");