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>
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/types.h>
31 #include <linux/workqueue.h>
32 #include <linux/completion.h>
33 #include <linux/export.h>
34 #include <linux/sched.h>
35 #include <linux/bitops.h>
36 #include <linux/skbuff.h>
39 #include <net/nfc/nci.h>
40 #include <net/nfc/nci_core.h>
41 #include <linux/nfc.h>
43 static void nci_cmd_work(struct work_struct
*work
);
44 static void nci_rx_work(struct work_struct
*work
);
45 static void nci_tx_work(struct work_struct
*work
);
47 /* ---- NCI requests ---- */
49 void nci_req_complete(struct nci_dev
*ndev
, int result
)
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
);
58 static void nci_req_cancel(struct nci_dev
*ndev
, int err
)
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
);
67 /* Execute request and wait for completion. */
68 static int __nci_request(struct nci_dev
*ndev
,
69 void (*req
)(struct nci_dev
*ndev
, unsigned long opt
),
76 ndev
->req_status
= NCI_REQ_PEND
;
78 init_completion(&ndev
->req_completion
);
80 completion_rc
= wait_for_completion_interruptible_timeout(
81 &ndev
->req_completion
,
84 pr_debug("wait_for_completion return %ld\n", completion_rc
);
86 if (completion_rc
> 0) {
87 switch (ndev
->req_status
) {
89 rc
= nci_to_errno(ndev
->req_result
);
92 case NCI_REQ_CANCELED
:
93 rc
= -ndev
->req_result
;
101 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
104 rc
= ((completion_rc
== 0) ? (-ETIMEDOUT
) : (completion_rc
));
107 ndev
->req_status
= ndev
->req_result
= 0;
112 static inline int nci_request(struct nci_dev
*ndev
,
113 void (*req
)(struct nci_dev
*ndev
, unsigned long opt
),
114 unsigned long opt
, __u32 timeout
)
118 if (!test_bit(NCI_UP
, &ndev
->flags
))
121 /* Serialize all requests */
122 mutex_lock(&ndev
->req_lock
);
123 rc
= __nci_request(ndev
, req
, opt
, timeout
);
124 mutex_unlock(&ndev
->req_lock
);
129 static void nci_reset_req(struct nci_dev
*ndev
, unsigned long opt
)
131 struct nci_core_reset_cmd cmd
;
133 cmd
.reset_type
= NCI_RESET_TYPE_RESET_CONFIG
;
134 nci_send_cmd(ndev
, NCI_OP_CORE_RESET_CMD
, 1, &cmd
);
137 static void nci_init_req(struct nci_dev
*ndev
, unsigned long opt
)
139 nci_send_cmd(ndev
, NCI_OP_CORE_INIT_CMD
, 0, NULL
);
142 static void nci_init_complete_req(struct nci_dev
*ndev
, unsigned long opt
)
144 struct nci_rf_disc_map_cmd cmd
;
145 struct disc_map_config
*cfg
= cmd
.mapping_configs
;
146 __u8
*num
= &cmd
.num_mapping_configs
;
149 /* set rf mapping configurations */
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
) {
156 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
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
;
161 } else if (ndev
->supported_rf_interfaces
[i
] ==
162 NCI_RF_INTERFACE_NFC_DEP
) {
163 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
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
;
170 if (*num
== NCI_MAX_NUM_MAPPING_CONFIGS
)
174 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_MAP_CMD
,
175 (1 + ((*num
)*sizeof(struct disc_map_config
))),
179 static void nci_rf_discover_req(struct nci_dev
*ndev
, unsigned long opt
)
181 struct nci_rf_disc_cmd cmd
;
182 __u32 protocols
= opt
;
184 cmd
.num_disc_configs
= 0;
186 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
187 (protocols
& NFC_PROTO_JEWEL_MASK
188 || protocols
& NFC_PROTO_MIFARE_MASK
189 || protocols
& NFC_PROTO_ISO14443_MASK
190 || protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
191 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
192 NCI_NFC_A_PASSIVE_POLL_MODE
;
193 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
194 cmd
.num_disc_configs
++;
197 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
198 (protocols
& NFC_PROTO_ISO14443_MASK
)) {
199 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
200 NCI_NFC_B_PASSIVE_POLL_MODE
;
201 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
202 cmd
.num_disc_configs
++;
205 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
206 (protocols
& NFC_PROTO_FELICA_MASK
207 || protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
208 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
209 NCI_NFC_F_PASSIVE_POLL_MODE
;
210 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
211 cmd
.num_disc_configs
++;
214 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_CMD
,
215 (1 + (cmd
.num_disc_configs
*sizeof(struct disc_config
))),
219 static void nci_rf_deactivate_req(struct nci_dev
*ndev
, unsigned long opt
)
221 struct nci_rf_deactivate_cmd cmd
;
223 cmd
.type
= NCI_DEACTIVATE_TYPE_IDLE_MODE
;
225 nci_send_cmd(ndev
, NCI_OP_RF_DEACTIVATE_CMD
,
226 sizeof(struct nci_rf_deactivate_cmd
),
230 static int nci_open_device(struct nci_dev
*ndev
)
234 mutex_lock(&ndev
->req_lock
);
236 if (test_bit(NCI_UP
, &ndev
->flags
)) {
241 if (ndev
->ops
->open(ndev
)) {
246 atomic_set(&ndev
->cmd_cnt
, 1);
248 set_bit(NCI_INIT
, &ndev
->flags
);
250 rc
= __nci_request(ndev
, nci_reset_req
, 0,
251 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
254 rc
= __nci_request(ndev
, nci_init_req
, 0,
255 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
259 rc
= __nci_request(ndev
, nci_init_complete_req
, 0,
260 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
263 clear_bit(NCI_INIT
, &ndev
->flags
);
266 set_bit(NCI_UP
, &ndev
->flags
);
268 /* Init failed, cleanup */
269 skb_queue_purge(&ndev
->cmd_q
);
270 skb_queue_purge(&ndev
->rx_q
);
271 skb_queue_purge(&ndev
->tx_q
);
273 ndev
->ops
->close(ndev
);
278 mutex_unlock(&ndev
->req_lock
);
282 static int nci_close_device(struct nci_dev
*ndev
)
284 nci_req_cancel(ndev
, ENODEV
);
285 mutex_lock(&ndev
->req_lock
);
287 if (!test_and_clear_bit(NCI_UP
, &ndev
->flags
)) {
288 del_timer_sync(&ndev
->cmd_timer
);
289 mutex_unlock(&ndev
->req_lock
);
293 /* Drop RX and TX queues */
294 skb_queue_purge(&ndev
->rx_q
);
295 skb_queue_purge(&ndev
->tx_q
);
297 /* Flush RX and TX wq */
298 flush_workqueue(ndev
->rx_wq
);
299 flush_workqueue(ndev
->tx_wq
);
302 skb_queue_purge(&ndev
->cmd_q
);
303 atomic_set(&ndev
->cmd_cnt
, 1);
305 set_bit(NCI_INIT
, &ndev
->flags
);
306 __nci_request(ndev
, nci_reset_req
, 0,
307 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
308 clear_bit(NCI_INIT
, &ndev
->flags
);
311 flush_workqueue(ndev
->cmd_wq
);
313 /* After this point our queues are empty
314 * and no works are scheduled. */
315 ndev
->ops
->close(ndev
);
320 mutex_unlock(&ndev
->req_lock
);
325 /* NCI command timer function */
326 static void nci_cmd_timer(unsigned long arg
)
328 struct nci_dev
*ndev
= (void *) arg
;
330 atomic_set(&ndev
->cmd_cnt
, 1);
331 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
334 static int nci_dev_up(struct nfc_dev
*nfc_dev
)
336 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
338 return nci_open_device(ndev
);
341 static int nci_dev_down(struct nfc_dev
*nfc_dev
)
343 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
345 return nci_close_device(ndev
);
348 static int nci_start_poll(struct nfc_dev
*nfc_dev
, __u32 protocols
)
350 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
353 if (test_bit(NCI_DISCOVERY
, &ndev
->flags
)) {
354 pr_err("unable to start poll, since poll is already active\n");
358 if (ndev
->target_active_prot
) {
359 pr_err("there is an active target\n");
363 if (test_bit(NCI_POLL_ACTIVE
, &ndev
->flags
)) {
364 pr_debug("target is active, implicitly deactivate...\n");
366 rc
= nci_request(ndev
, nci_rf_deactivate_req
, 0,
367 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
372 rc
= nci_request(ndev
, nci_rf_discover_req
, protocols
,
373 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT
));
376 ndev
->poll_prots
= protocols
;
381 static void nci_stop_poll(struct nfc_dev
*nfc_dev
)
383 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
385 if (!test_bit(NCI_DISCOVERY
, &ndev
->flags
)) {
386 pr_err("unable to stop poll, since poll is not active\n");
390 nci_request(ndev
, nci_rf_deactivate_req
, 0,
391 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
394 static int nci_activate_target(struct nfc_dev
*nfc_dev
, __u32 target_idx
,
397 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
399 pr_debug("target_idx %d, protocol 0x%x\n", target_idx
, protocol
);
401 if (!test_bit(NCI_POLL_ACTIVE
, &ndev
->flags
)) {
402 pr_err("there is no available target to activate\n");
406 if (ndev
->target_active_prot
) {
407 pr_err("there is already an active target\n");
411 if (!(ndev
->target_available_prots
& (1 << protocol
))) {
412 pr_err("target does not support the requested protocol 0x%x\n",
417 ndev
->target_active_prot
= protocol
;
418 ndev
->target_available_prots
= 0;
423 static void nci_deactivate_target(struct nfc_dev
*nfc_dev
, __u32 target_idx
)
425 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
427 pr_debug("target_idx %d\n", target_idx
);
429 if (!ndev
->target_active_prot
) {
430 pr_err("unable to deactivate target, no active target\n");
434 ndev
->target_active_prot
= 0;
436 if (test_bit(NCI_POLL_ACTIVE
, &ndev
->flags
)) {
437 nci_request(ndev
, nci_rf_deactivate_req
, 0,
438 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
442 static int nci_data_exchange(struct nfc_dev
*nfc_dev
, __u32 target_idx
,
444 data_exchange_cb_t cb
,
447 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
450 pr_debug("target_idx %d, len %d\n", target_idx
, skb
->len
);
452 if (!ndev
->target_active_prot
) {
453 pr_err("unable to exchange data, no active target\n");
457 if (test_and_set_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
460 /* store cb and context to be used on receiving data */
461 ndev
->data_exchange_cb
= cb
;
462 ndev
->data_exchange_cb_context
= cb_context
;
464 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
466 clear_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
);
471 static struct nfc_ops nci_nfc_ops
= {
472 .dev_up
= nci_dev_up
,
473 .dev_down
= nci_dev_down
,
474 .start_poll
= nci_start_poll
,
475 .stop_poll
= nci_stop_poll
,
476 .activate_target
= nci_activate_target
,
477 .deactivate_target
= nci_deactivate_target
,
478 .data_exchange
= nci_data_exchange
,
481 /* ---- Interface to NCI drivers ---- */
484 * nci_allocate_device - allocate a new nci device
486 * @ops: device operations
487 * @supported_protocols: NFC protocols supported by the device
489 struct nci_dev
*nci_allocate_device(struct nci_ops
*ops
,
490 __u32 supported_protocols
,
494 struct nci_dev
*ndev
;
496 pr_debug("supported_protocols 0x%x\n", supported_protocols
);
498 if (!ops
->open
|| !ops
->close
|| !ops
->send
)
501 if (!supported_protocols
)
504 ndev
= kzalloc(sizeof(struct nci_dev
), GFP_KERNEL
);
509 ndev
->tx_headroom
= tx_headroom
;
510 ndev
->tx_tailroom
= tx_tailroom
;
512 ndev
->nfc_dev
= nfc_allocate_device(&nci_nfc_ops
,
514 tx_headroom
+ NCI_DATA_HDR_SIZE
,
519 nfc_set_drvdata(ndev
->nfc_dev
, ndev
);
527 EXPORT_SYMBOL(nci_allocate_device
);
530 * nci_free_device - deallocate nci device
532 * @ndev: The nci device to deallocate
534 void nci_free_device(struct nci_dev
*ndev
)
536 nfc_free_device(ndev
->nfc_dev
);
539 EXPORT_SYMBOL(nci_free_device
);
542 * nci_register_device - register a nci device in the nfc subsystem
544 * @dev: The nci device to register
546 int nci_register_device(struct nci_dev
*ndev
)
549 struct device
*dev
= &ndev
->nfc_dev
->dev
;
552 rc
= nfc_register_device(ndev
->nfc_dev
);
558 INIT_WORK(&ndev
->cmd_work
, nci_cmd_work
);
559 snprintf(name
, sizeof(name
), "%s_nci_cmd_wq", dev_name(dev
));
560 ndev
->cmd_wq
= create_singlethread_workqueue(name
);
566 INIT_WORK(&ndev
->rx_work
, nci_rx_work
);
567 snprintf(name
, sizeof(name
), "%s_nci_rx_wq", dev_name(dev
));
568 ndev
->rx_wq
= create_singlethread_workqueue(name
);
571 goto destroy_cmd_wq_exit
;
574 INIT_WORK(&ndev
->tx_work
, nci_tx_work
);
575 snprintf(name
, sizeof(name
), "%s_nci_tx_wq", dev_name(dev
));
576 ndev
->tx_wq
= create_singlethread_workqueue(name
);
579 goto destroy_rx_wq_exit
;
582 skb_queue_head_init(&ndev
->cmd_q
);
583 skb_queue_head_init(&ndev
->rx_q
);
584 skb_queue_head_init(&ndev
->tx_q
);
586 setup_timer(&ndev
->cmd_timer
, nci_cmd_timer
,
587 (unsigned long) ndev
);
589 mutex_init(&ndev
->req_lock
);
594 destroy_workqueue(ndev
->rx_wq
);
597 destroy_workqueue(ndev
->cmd_wq
);
600 nfc_unregister_device(ndev
->nfc_dev
);
605 EXPORT_SYMBOL(nci_register_device
);
608 * nci_unregister_device - unregister a nci device in the nfc subsystem
610 * @dev: The nci device to unregister
612 void nci_unregister_device(struct nci_dev
*ndev
)
614 nci_close_device(ndev
);
616 destroy_workqueue(ndev
->cmd_wq
);
617 destroy_workqueue(ndev
->rx_wq
);
618 destroy_workqueue(ndev
->tx_wq
);
620 nfc_unregister_device(ndev
->nfc_dev
);
622 EXPORT_SYMBOL(nci_unregister_device
);
625 * nci_recv_frame - receive frame from NCI drivers
627 * @skb: The sk_buff to receive
629 int nci_recv_frame(struct sk_buff
*skb
)
631 struct nci_dev
*ndev
= (struct nci_dev
*) skb
->dev
;
633 pr_debug("len %d\n", skb
->len
);
635 if (!ndev
|| (!test_bit(NCI_UP
, &ndev
->flags
)
636 && !test_bit(NCI_INIT
, &ndev
->flags
))) {
641 /* Queue frame for rx worker thread */
642 skb_queue_tail(&ndev
->rx_q
, skb
);
643 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
647 EXPORT_SYMBOL(nci_recv_frame
);
649 static int nci_send_frame(struct sk_buff
*skb
)
651 struct nci_dev
*ndev
= (struct nci_dev
*) skb
->dev
;
653 pr_debug("len %d\n", skb
->len
);
660 /* Get rid of skb owner, prior to sending to the driver. */
663 return ndev
->ops
->send(skb
);
666 /* Send NCI command */
667 int nci_send_cmd(struct nci_dev
*ndev
, __u16 opcode
, __u8 plen
, void *payload
)
669 struct nci_ctrl_hdr
*hdr
;
672 pr_debug("opcode 0x%x, plen %d\n", opcode
, plen
);
674 skb
= nci_skb_alloc(ndev
, (NCI_CTRL_HDR_SIZE
+ plen
), GFP_KERNEL
);
676 pr_err("no memory for command\n");
680 hdr
= (struct nci_ctrl_hdr
*) skb_put(skb
, NCI_CTRL_HDR_SIZE
);
681 hdr
->gid
= nci_opcode_gid(opcode
);
682 hdr
->oid
= nci_opcode_oid(opcode
);
685 nci_mt_set((__u8
*)hdr
, NCI_MT_CMD_PKT
);
686 nci_pbf_set((__u8
*)hdr
, NCI_PBF_LAST
);
689 memcpy(skb_put(skb
, plen
), payload
, plen
);
691 skb
->dev
= (void *) ndev
;
693 skb_queue_tail(&ndev
->cmd_q
, skb
);
694 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
699 /* ---- NCI TX Data worker thread ---- */
701 static void nci_tx_work(struct work_struct
*work
)
703 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, tx_work
);
706 pr_debug("credits_cnt %d\n", atomic_read(&ndev
->credits_cnt
));
708 /* Send queued tx data */
709 while (atomic_read(&ndev
->credits_cnt
)) {
710 skb
= skb_dequeue(&ndev
->tx_q
);
714 /* Check if data flow control is used */
715 if (atomic_read(&ndev
->credits_cnt
) !=
716 NCI_DATA_FLOW_CONTROL_NOT_USED
)
717 atomic_dec(&ndev
->credits_cnt
);
719 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
721 nci_conn_id(skb
->data
),
722 nci_plen(skb
->data
));
728 /* ----- NCI RX worker thread (data & control) ----- */
730 static void nci_rx_work(struct work_struct
*work
)
732 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, rx_work
);
735 while ((skb
= skb_dequeue(&ndev
->rx_q
))) {
737 switch (nci_mt(skb
->data
)) {
739 nci_rsp_packet(ndev
, skb
);
743 nci_ntf_packet(ndev
, skb
);
746 case NCI_MT_DATA_PKT
:
747 nci_rx_data_packet(ndev
, skb
);
751 pr_err("unknown MT 0x%x\n", nci_mt(skb
->data
));
758 /* ----- NCI TX CMD worker thread ----- */
760 static void nci_cmd_work(struct work_struct
*work
)
762 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, cmd_work
);
765 pr_debug("cmd_cnt %d\n", atomic_read(&ndev
->cmd_cnt
));
767 /* Send queued command */
768 if (atomic_read(&ndev
->cmd_cnt
)) {
769 skb
= skb_dequeue(&ndev
->cmd_q
);
773 atomic_dec(&ndev
->cmd_cnt
);
775 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
777 nci_opcode_gid(nci_opcode(skb
->data
)),
778 nci_opcode_oid(nci_opcode(skb
->data
)),
779 nci_plen(skb
->data
));
783 mod_timer(&ndev
->cmd_timer
,
784 jiffies
+ msecs_to_jiffies(NCI_CMD_TIMEOUT
));