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.
6 * Copyright (C) 2014 Marvell International Ltd.
8 * Written by Ilan Elias <ilane@ti.com>
11 * This file is based on hci_core.c, which was written
12 * by Maxim Krasnyansky.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2
16 * as published by the Free Software Foundation
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/workqueue.h>
34 #include <linux/completion.h>
35 #include <linux/export.h>
36 #include <linux/sched.h>
37 #include <linux/bitops.h>
38 #include <linux/skbuff.h>
41 #include <net/nfc/nci.h>
42 #include <net/nfc/nci_core.h>
43 #include <linux/nfc.h>
45 struct core_conn_create_data {
47 struct nci_core_conn_create_cmd *cmd;
50 static void nci_cmd_work(struct work_struct *work);
51 static void nci_rx_work(struct work_struct *work);
52 static void nci_tx_work(struct work_struct *work);
54 struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
57 struct nci_conn_info *conn_info;
59 list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
60 if (conn_info->conn_id == conn_id)
67 int nci_get_conn_info_by_id(struct nci_dev *ndev, u8 id)
69 struct nci_conn_info *conn_info;
71 list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
72 if (conn_info->id == id)
73 return conn_info->conn_id;
78 EXPORT_SYMBOL(nci_get_conn_info_by_id);
80 /* ---- NCI requests ---- */
82 void nci_req_complete(struct nci_dev *ndev, int result)
84 if (ndev->req_status == NCI_REQ_PEND) {
85 ndev->req_result = result;
86 ndev->req_status = NCI_REQ_DONE;
87 complete(&ndev->req_completion);
90 EXPORT_SYMBOL(nci_req_complete);
92 static void nci_req_cancel(struct nci_dev *ndev, int err)
94 if (ndev->req_status == NCI_REQ_PEND) {
95 ndev->req_result = err;
96 ndev->req_status = NCI_REQ_CANCELED;
97 complete(&ndev->req_completion);
101 /* Execute request and wait for completion. */
102 static int __nci_request(struct nci_dev *ndev,
103 void (*req)(struct nci_dev *ndev, unsigned long opt),
104 unsigned long opt, __u32 timeout)
109 ndev->req_status = NCI_REQ_PEND;
111 reinit_completion(&ndev->req_completion);
114 wait_for_completion_interruptible_timeout(&ndev->req_completion,
117 pr_debug("wait_for_completion return %ld\n", completion_rc);
119 if (completion_rc > 0) {
120 switch (ndev->req_status) {
122 rc = nci_to_errno(ndev->req_result);
125 case NCI_REQ_CANCELED:
126 rc = -ndev->req_result;
134 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
137 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
140 ndev->req_status = ndev->req_result = 0;
145 inline int nci_request(struct nci_dev *ndev,
146 void (*req)(struct nci_dev *ndev,
148 unsigned long opt, __u32 timeout)
152 /* Serialize all requests */
153 mutex_lock(&ndev->req_lock);
154 /* check the state after obtaing the lock against any races
155 * from nci_close_device when the device gets removed.
157 if (test_bit(NCI_UP, &ndev->flags))
158 rc = __nci_request(ndev, req, opt, timeout);
161 mutex_unlock(&ndev->req_lock);
166 static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
168 struct nci_core_reset_cmd cmd;
170 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
171 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
174 static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
176 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
179 static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
181 struct nci_rf_disc_map_cmd cmd;
182 struct disc_map_config *cfg = cmd.mapping_configs;
183 __u8 *num = &cmd.num_mapping_configs;
186 /* set rf mapping configurations */
189 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
190 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
191 if (ndev->supported_rf_interfaces[i] ==
192 NCI_RF_INTERFACE_ISO_DEP) {
193 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
194 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
195 NCI_DISC_MAP_MODE_LISTEN;
196 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
198 } else if (ndev->supported_rf_interfaces[i] ==
199 NCI_RF_INTERFACE_NFC_DEP) {
200 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
201 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
202 NCI_DISC_MAP_MODE_LISTEN;
203 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
207 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
211 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
212 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
215 struct nci_set_config_param {
221 static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
223 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
224 struct nci_core_set_config_cmd cmd;
226 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
229 cmd.param.id = param->id;
230 cmd.param.len = param->len;
231 memcpy(cmd.param.val, param->val, param->len);
233 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
236 struct nci_rf_discover_param {
241 static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
243 struct nci_rf_discover_param *param =
244 (struct nci_rf_discover_param *)opt;
245 struct nci_rf_disc_cmd cmd;
247 cmd.num_disc_configs = 0;
249 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
250 (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
251 param->im_protocols & NFC_PROTO_MIFARE_MASK ||
252 param->im_protocols & NFC_PROTO_ISO14443_MASK ||
253 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
254 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
255 NCI_NFC_A_PASSIVE_POLL_MODE;
256 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
257 cmd.num_disc_configs++;
260 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
261 (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
262 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
263 NCI_NFC_B_PASSIVE_POLL_MODE;
264 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
265 cmd.num_disc_configs++;
268 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
269 (param->im_protocols & NFC_PROTO_FELICA_MASK ||
270 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
271 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
272 NCI_NFC_F_PASSIVE_POLL_MODE;
273 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
274 cmd.num_disc_configs++;
277 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
278 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
279 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
280 NCI_NFC_V_PASSIVE_POLL_MODE;
281 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
282 cmd.num_disc_configs++;
285 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
286 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
287 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
288 NCI_NFC_A_PASSIVE_LISTEN_MODE;
289 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
290 cmd.num_disc_configs++;
291 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
292 NCI_NFC_F_PASSIVE_LISTEN_MODE;
293 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
294 cmd.num_disc_configs++;
297 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
298 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
302 struct nci_rf_discover_select_param {
303 __u8 rf_discovery_id;
307 static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
309 struct nci_rf_discover_select_param *param =
310 (struct nci_rf_discover_select_param *)opt;
311 struct nci_rf_discover_select_cmd cmd;
313 cmd.rf_discovery_id = param->rf_discovery_id;
314 cmd.rf_protocol = param->rf_protocol;
316 switch (cmd.rf_protocol) {
317 case NCI_RF_PROTOCOL_ISO_DEP:
318 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
321 case NCI_RF_PROTOCOL_NFC_DEP:
322 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
326 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
330 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
331 sizeof(struct nci_rf_discover_select_cmd), &cmd);
334 static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
336 struct nci_rf_deactivate_cmd cmd;
340 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
341 sizeof(struct nci_rf_deactivate_cmd), &cmd);
344 struct nci_cmd_param {
350 static void nci_generic_req(struct nci_dev *ndev, unsigned long opt)
352 struct nci_cmd_param *param =
353 (struct nci_cmd_param *)opt;
355 nci_send_cmd(ndev, param->opcode, param->len, param->payload);
358 int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, __u8 *payload)
360 struct nci_cmd_param param;
362 param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
364 param.payload = payload;
366 return __nci_request(ndev, nci_generic_req, (unsigned long)¶m,
367 msecs_to_jiffies(NCI_CMD_TIMEOUT));
369 EXPORT_SYMBOL(nci_prop_cmd);
371 int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len, __u8 *payload)
373 struct nci_cmd_param param;
375 param.opcode = opcode;
377 param.payload = payload;
379 return __nci_request(ndev, nci_generic_req, (unsigned long)¶m,
380 msecs_to_jiffies(NCI_CMD_TIMEOUT));
382 EXPORT_SYMBOL(nci_core_cmd);
384 int nci_core_reset(struct nci_dev *ndev)
386 return __nci_request(ndev, nci_reset_req, 0,
387 msecs_to_jiffies(NCI_RESET_TIMEOUT));
389 EXPORT_SYMBOL(nci_core_reset);
391 int nci_core_init(struct nci_dev *ndev)
393 return __nci_request(ndev, nci_init_req, 0,
394 msecs_to_jiffies(NCI_INIT_TIMEOUT));
396 EXPORT_SYMBOL(nci_core_init);
398 static int nci_open_device(struct nci_dev *ndev)
402 mutex_lock(&ndev->req_lock);
404 if (test_bit(NCI_UNREG, &ndev->flags)) {
409 if (test_bit(NCI_UP, &ndev->flags)) {
414 if (ndev->ops->open(ndev)) {
419 atomic_set(&ndev->cmd_cnt, 1);
421 set_bit(NCI_INIT, &ndev->flags);
424 rc = ndev->ops->init(ndev);
427 rc = __nci_request(ndev, nci_reset_req, 0,
428 msecs_to_jiffies(NCI_RESET_TIMEOUT));
431 if (!rc && ndev->ops->setup) {
432 rc = ndev->ops->setup(ndev);
436 rc = __nci_request(ndev, nci_init_req, 0,
437 msecs_to_jiffies(NCI_INIT_TIMEOUT));
440 if (!rc && ndev->ops->post_setup)
441 rc = ndev->ops->post_setup(ndev);
444 rc = __nci_request(ndev, nci_init_complete_req, 0,
445 msecs_to_jiffies(NCI_INIT_TIMEOUT));
448 clear_bit(NCI_INIT, &ndev->flags);
451 set_bit(NCI_UP, &ndev->flags);
452 nci_clear_target_list(ndev);
453 atomic_set(&ndev->state, NCI_IDLE);
455 /* Init failed, cleanup */
456 skb_queue_purge(&ndev->cmd_q);
457 skb_queue_purge(&ndev->rx_q);
458 skb_queue_purge(&ndev->tx_q);
460 ndev->ops->close(ndev);
465 mutex_unlock(&ndev->req_lock);
469 static int nci_close_device(struct nci_dev *ndev)
471 nci_req_cancel(ndev, ENODEV);
473 /* This mutex needs to be held as a barrier for
474 * caller nci_unregister_device
476 mutex_lock(&ndev->req_lock);
478 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
479 del_timer_sync(&ndev->cmd_timer);
480 del_timer_sync(&ndev->data_timer);
481 mutex_unlock(&ndev->req_lock);
485 /* Drop RX and TX queues */
486 skb_queue_purge(&ndev->rx_q);
487 skb_queue_purge(&ndev->tx_q);
489 /* Flush RX and TX wq */
490 flush_workqueue(ndev->rx_wq);
491 flush_workqueue(ndev->tx_wq);
494 skb_queue_purge(&ndev->cmd_q);
495 atomic_set(&ndev->cmd_cnt, 1);
497 set_bit(NCI_INIT, &ndev->flags);
498 __nci_request(ndev, nci_reset_req, 0,
499 msecs_to_jiffies(NCI_RESET_TIMEOUT));
501 /* After this point our queues are empty
502 * and no works are scheduled.
504 ndev->ops->close(ndev);
506 clear_bit(NCI_INIT, &ndev->flags);
508 del_timer_sync(&ndev->cmd_timer);
511 flush_workqueue(ndev->cmd_wq);
513 /* Clear flags except NCI_UNREG */
514 ndev->flags &= BIT(NCI_UNREG);
516 mutex_unlock(&ndev->req_lock);
521 /* NCI command timer function */
522 static void nci_cmd_timer(unsigned long arg)
524 struct nci_dev *ndev = (void *) arg;
526 atomic_set(&ndev->cmd_cnt, 1);
527 queue_work(ndev->cmd_wq, &ndev->cmd_work);
530 /* NCI data exchange timer function */
531 static void nci_data_timer(unsigned long arg)
533 struct nci_dev *ndev = (void *) arg;
535 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
536 queue_work(ndev->rx_wq, &ndev->rx_work);
539 static int nci_dev_up(struct nfc_dev *nfc_dev)
541 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
543 return nci_open_device(ndev);
546 static int nci_dev_down(struct nfc_dev *nfc_dev)
548 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
550 return nci_close_device(ndev);
553 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
555 struct nci_set_config_param param;
564 return __nci_request(ndev, nci_set_config_req, (unsigned long)¶m,
565 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
567 EXPORT_SYMBOL(nci_set_config);
569 static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
571 struct nci_nfcee_discover_cmd cmd;
574 cmd.discovery_action = action;
576 nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
579 int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
581 return __nci_request(ndev, nci_nfcee_discover_req, action,
582 msecs_to_jiffies(NCI_CMD_TIMEOUT));
584 EXPORT_SYMBOL(nci_nfcee_discover);
586 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
588 struct nci_nfcee_mode_set_cmd *cmd =
589 (struct nci_nfcee_mode_set_cmd *)opt;
591 nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
592 sizeof(struct nci_nfcee_mode_set_cmd), cmd);
595 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
597 struct nci_nfcee_mode_set_cmd cmd;
599 cmd.nfcee_id = nfcee_id;
600 cmd.nfcee_mode = nfcee_mode;
602 return __nci_request(ndev, nci_nfcee_mode_set_req,
604 msecs_to_jiffies(NCI_CMD_TIMEOUT));
606 EXPORT_SYMBOL(nci_nfcee_mode_set);
608 static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
610 struct core_conn_create_data *data =
611 (struct core_conn_create_data *)opt;
613 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
616 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
617 u8 number_destination_params,
619 struct core_conn_create_dest_spec_params *params)
622 struct nci_core_conn_create_cmd *cmd;
623 struct core_conn_create_data data;
625 if (!number_destination_params)
628 data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
629 cmd = kzalloc(data.length, GFP_KERNEL);
633 cmd->destination_type = destination_type;
634 cmd->number_destination_params = number_destination_params;
635 memcpy(cmd->params, params, params_len);
639 if (params->length > 0)
640 ndev->cur_id = params->value[DEST_SPEC_PARAMS_ID_INDEX];
644 r = __nci_request(ndev, nci_core_conn_create_req,
645 (unsigned long)&data,
646 msecs_to_jiffies(NCI_CMD_TIMEOUT));
650 EXPORT_SYMBOL(nci_core_conn_create);
652 static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
656 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
659 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
661 return __nci_request(ndev, nci_core_conn_close_req, conn_id,
662 msecs_to_jiffies(NCI_CMD_TIMEOUT));
664 EXPORT_SYMBOL(nci_core_conn_close);
666 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
668 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
669 struct nci_set_config_param param;
672 param.val = nfc_get_local_general_bytes(nfc_dev, ¶m.len);
673 if ((param.val == NULL) || (param.len == 0))
676 if (param.len > NFC_MAX_GT_LEN)
679 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
681 rc = nci_request(ndev, nci_set_config_req, (unsigned long)¶m,
682 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
686 param.id = NCI_LN_ATR_RES_GEN_BYTES;
688 return nci_request(ndev, nci_set_config_req, (unsigned long)¶m,
689 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
692 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
694 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
698 val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
700 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
704 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
706 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
710 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
712 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
715 static int nci_start_poll(struct nfc_dev *nfc_dev,
716 __u32 im_protocols, __u32 tm_protocols)
718 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
719 struct nci_rf_discover_param param;
722 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
723 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
724 pr_err("unable to start poll, since poll is already active\n");
728 if (ndev->target_active_prot) {
729 pr_err("there is an active target\n");
733 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
734 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
735 pr_debug("target active or w4 select, implicitly deactivate\n");
737 rc = nci_request(ndev, nci_rf_deactivate_req,
738 NCI_DEACTIVATE_TYPE_IDLE_MODE,
739 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
744 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
745 rc = nci_set_local_general_bytes(nfc_dev);
747 pr_err("failed to set local general bytes\n");
752 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
753 rc = nci_set_listen_parameters(nfc_dev);
755 pr_err("failed to set listen parameters\n");
758 param.im_protocols = im_protocols;
759 param.tm_protocols = tm_protocols;
760 rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)¶m,
761 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
764 ndev->poll_prots = im_protocols;
769 static void nci_stop_poll(struct nfc_dev *nfc_dev)
771 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
773 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
774 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
775 pr_err("unable to stop poll, since poll is not active\n");
779 nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
780 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
783 static int nci_activate_target(struct nfc_dev *nfc_dev,
784 struct nfc_target *target, __u32 protocol)
786 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
787 struct nci_rf_discover_select_param param;
788 struct nfc_target *nci_target = NULL;
792 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
794 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
795 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
796 pr_err("there is no available target to activate\n");
800 if (ndev->target_active_prot) {
801 pr_err("there is already an active target\n");
805 for (i = 0; i < ndev->n_targets; i++) {
806 if (ndev->targets[i].idx == target->idx) {
807 nci_target = &ndev->targets[i];
813 pr_err("unable to find the selected target\n");
817 if (!(nci_target->supported_protocols & (1 << protocol))) {
818 pr_err("target does not support the requested protocol 0x%x\n",
823 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
824 param.rf_discovery_id = nci_target->logical_idx;
826 if (protocol == NFC_PROTO_JEWEL)
827 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
828 else if (protocol == NFC_PROTO_MIFARE)
829 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
830 else if (protocol == NFC_PROTO_FELICA)
831 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
832 else if (protocol == NFC_PROTO_ISO14443 ||
833 protocol == NFC_PROTO_ISO14443_B)
834 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
836 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
838 rc = nci_request(ndev, nci_rf_discover_select_req,
839 (unsigned long)¶m,
840 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
844 ndev->target_active_prot = protocol;
849 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
850 struct nfc_target *target,
853 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
854 u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
858 if (!ndev->target_active_prot) {
859 pr_err("unable to deactivate target, no active target\n");
863 ndev->target_active_prot = 0;
866 case NFC_TARGET_MODE_SLEEP:
867 nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
871 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
872 nci_request(ndev, nci_rf_deactivate_req, nci_mode,
873 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
877 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
878 __u8 comm_mode, __u8 *gb, size_t gb_len)
880 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
883 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
885 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
889 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
890 ndev->remote_gb_len);
892 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
898 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
900 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
905 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
906 nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
908 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
909 atomic_read(&ndev->state) == NCI_DISCOVERY) {
910 nci_request(ndev, nci_rf_deactivate_req, 0,
911 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
914 rc = nfc_tm_deactivated(nfc_dev);
916 pr_err("error when signaling tm deactivation\n");
923 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
925 data_exchange_cb_t cb, void *cb_context)
927 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
929 struct nci_conn_info *conn_info;
931 conn_info = ndev->rf_conn_info;
935 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
937 if (!ndev->target_active_prot) {
938 pr_err("unable to exchange data, no active target\n");
942 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
945 /* store cb and context to be used on receiving data */
946 conn_info->data_exchange_cb = cb;
947 conn_info->data_exchange_cb_context = cb_context;
949 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
951 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
956 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
958 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
961 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
963 pr_err("unable to send data\n");
968 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
970 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
972 if (ndev->ops->enable_se)
973 return ndev->ops->enable_se(ndev, se_idx);
978 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
980 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
982 if (ndev->ops->disable_se)
983 return ndev->ops->disable_se(ndev, se_idx);
988 static int nci_discover_se(struct nfc_dev *nfc_dev)
991 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
993 if (ndev->ops->discover_se) {
994 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
995 if (r != NCI_STATUS_OK)
998 return ndev->ops->discover_se(ndev);
1004 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1005 u8 *apdu, size_t apdu_length,
1006 se_io_cb_t cb, void *cb_context)
1008 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1010 if (ndev->ops->se_io)
1011 return ndev->ops->se_io(ndev, se_idx, apdu,
1012 apdu_length, cb, cb_context);
1017 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1019 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1021 if (!ndev->ops->fw_download)
1024 return ndev->ops->fw_download(ndev, firmware_name);
1027 static struct nfc_ops nci_nfc_ops = {
1028 .dev_up = nci_dev_up,
1029 .dev_down = nci_dev_down,
1030 .start_poll = nci_start_poll,
1031 .stop_poll = nci_stop_poll,
1032 .dep_link_up = nci_dep_link_up,
1033 .dep_link_down = nci_dep_link_down,
1034 .activate_target = nci_activate_target,
1035 .deactivate_target = nci_deactivate_target,
1036 .im_transceive = nci_transceive,
1037 .tm_send = nci_tm_send,
1038 .enable_se = nci_enable_se,
1039 .disable_se = nci_disable_se,
1040 .discover_se = nci_discover_se,
1042 .fw_download = nci_fw_download,
1045 /* ---- Interface to NCI drivers ---- */
1047 * nci_allocate_device - allocate a new nci device
1049 * @ops: device operations
1050 * @supported_protocols: NFC protocols supported by the device
1052 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
1053 __u32 supported_protocols,
1054 int tx_headroom, int tx_tailroom)
1056 struct nci_dev *ndev;
1058 pr_debug("supported_protocols 0x%x\n", supported_protocols);
1060 if (!ops->open || !ops->close || !ops->send)
1063 if (!supported_protocols)
1066 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1072 if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1073 pr_err("Too many proprietary commands: %zd\n",
1075 ops->prop_ops = NULL;
1076 ops->n_prop_ops = 0;
1079 ndev->tx_headroom = tx_headroom;
1080 ndev->tx_tailroom = tx_tailroom;
1081 init_completion(&ndev->req_completion);
1083 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1084 supported_protocols,
1085 tx_headroom + NCI_DATA_HDR_SIZE,
1090 ndev->hci_dev = nci_hci_allocate(ndev);
1094 nfc_set_drvdata(ndev->nfc_dev, ndev);
1099 nfc_free_device(ndev->nfc_dev);
1104 EXPORT_SYMBOL(nci_allocate_device);
1107 * nci_free_device - deallocate nci device
1109 * @ndev: The nci device to deallocate
1111 void nci_free_device(struct nci_dev *ndev)
1113 nfc_free_device(ndev->nfc_dev);
1114 nci_hci_deallocate(ndev);
1117 EXPORT_SYMBOL(nci_free_device);
1120 * nci_register_device - register a nci device in the nfc subsystem
1122 * @dev: The nci device to register
1124 int nci_register_device(struct nci_dev *ndev)
1127 struct device *dev = &ndev->nfc_dev->dev;
1132 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1133 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1134 ndev->cmd_wq = create_singlethread_workqueue(name);
1135 if (!ndev->cmd_wq) {
1140 INIT_WORK(&ndev->rx_work, nci_rx_work);
1141 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1142 ndev->rx_wq = create_singlethread_workqueue(name);
1145 goto destroy_cmd_wq_exit;
1148 INIT_WORK(&ndev->tx_work, nci_tx_work);
1149 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1150 ndev->tx_wq = create_singlethread_workqueue(name);
1153 goto destroy_rx_wq_exit;
1156 skb_queue_head_init(&ndev->cmd_q);
1157 skb_queue_head_init(&ndev->rx_q);
1158 skb_queue_head_init(&ndev->tx_q);
1160 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
1161 (unsigned long) ndev);
1162 setup_timer(&ndev->data_timer, nci_data_timer,
1163 (unsigned long) ndev);
1165 mutex_init(&ndev->req_lock);
1166 INIT_LIST_HEAD(&ndev->conn_info_list);
1168 rc = nfc_register_device(ndev->nfc_dev);
1170 goto destroy_rx_wq_exit;
1175 destroy_workqueue(ndev->rx_wq);
1177 destroy_cmd_wq_exit:
1178 destroy_workqueue(ndev->cmd_wq);
1183 EXPORT_SYMBOL(nci_register_device);
1186 * nci_unregister_device - unregister a nci device in the nfc subsystem
1188 * @dev: The nci device to unregister
1190 void nci_unregister_device(struct nci_dev *ndev)
1192 struct nci_conn_info *conn_info, *n;
1194 /* This set_bit is not protected with specialized barrier,
1195 * However, it is fine because the mutex_lock(&ndev->req_lock);
1196 * in nci_close_device() will help to emit one.
1198 set_bit(NCI_UNREG, &ndev->flags);
1200 nci_close_device(ndev);
1202 destroy_workqueue(ndev->cmd_wq);
1203 destroy_workqueue(ndev->rx_wq);
1204 destroy_workqueue(ndev->tx_wq);
1206 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1207 list_del(&conn_info->list);
1208 /* conn_info is allocated with devm_kzalloc */
1211 nfc_unregister_device(ndev->nfc_dev);
1213 EXPORT_SYMBOL(nci_unregister_device);
1216 * nci_recv_frame - receive frame from NCI drivers
1218 * @ndev: The nci device
1219 * @skb: The sk_buff to receive
1221 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1223 pr_debug("len %d\n", skb->len);
1225 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1226 !test_bit(NCI_INIT, &ndev->flags))) {
1231 /* Queue frame for rx worker thread */
1232 skb_queue_tail(&ndev->rx_q, skb);
1233 queue_work(ndev->rx_wq, &ndev->rx_work);
1237 EXPORT_SYMBOL(nci_recv_frame);
1239 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1241 pr_debug("len %d\n", skb->len);
1248 /* Get rid of skb owner, prior to sending to the driver. */
1251 /* Send copy to sniffer */
1252 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1253 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1255 return ndev->ops->send(ndev, skb);
1257 EXPORT_SYMBOL(nci_send_frame);
1259 /* Send NCI command */
1260 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1262 struct nci_ctrl_hdr *hdr;
1263 struct sk_buff *skb;
1265 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1267 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1269 pr_err("no memory for command\n");
1273 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
1274 hdr->gid = nci_opcode_gid(opcode);
1275 hdr->oid = nci_opcode_oid(opcode);
1278 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1279 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1282 memcpy(skb_put(skb, plen), payload, plen);
1284 skb_queue_tail(&ndev->cmd_q, skb);
1285 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1289 EXPORT_SYMBOL(nci_send_cmd);
1291 /* Proprietary commands API */
1292 static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
1297 struct nci_driver_ops *op;
1302 for (i = 0; i < n_ops; i++) {
1304 if (op->opcode == opcode)
1311 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1312 struct sk_buff *skb, struct nci_driver_ops *ops,
1315 struct nci_driver_ops *op;
1317 op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1318 if (!op || !op->rsp)
1321 return op->rsp(ndev, skb);
1324 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1325 struct sk_buff *skb, struct nci_driver_ops *ops,
1328 struct nci_driver_ops *op;
1330 op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1331 if (!op || !op->ntf)
1334 return op->ntf(ndev, skb);
1337 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1338 struct sk_buff *skb)
1340 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1341 ndev->ops->n_prop_ops);
1344 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1345 struct sk_buff *skb)
1347 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1348 ndev->ops->n_prop_ops);
1351 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1352 struct sk_buff *skb)
1354 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1355 ndev->ops->n_core_ops);
1358 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1359 struct sk_buff *skb)
1361 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1362 ndev->ops->n_core_ops);
1365 /* ---- NCI TX Data worker thread ---- */
1367 static void nci_tx_work(struct work_struct *work)
1369 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1370 struct nci_conn_info *conn_info;
1371 struct sk_buff *skb;
1373 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1377 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1379 /* Send queued tx data */
1380 while (atomic_read(&conn_info->credits_cnt)) {
1381 skb = skb_dequeue(&ndev->tx_q);
1385 /* Check if data flow control is used */
1386 if (atomic_read(&conn_info->credits_cnt) !=
1387 NCI_DATA_FLOW_CONTROL_NOT_USED)
1388 atomic_dec(&conn_info->credits_cnt);
1390 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1392 nci_conn_id(skb->data),
1393 nci_plen(skb->data));
1395 nci_send_frame(ndev, skb);
1397 mod_timer(&ndev->data_timer,
1398 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1402 /* ----- NCI RX worker thread (data & control) ----- */
1404 static void nci_rx_work(struct work_struct *work)
1406 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1407 struct sk_buff *skb;
1409 while ((skb = skb_dequeue(&ndev->rx_q))) {
1411 /* Send copy to sniffer */
1412 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1413 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1416 switch (nci_mt(skb->data)) {
1417 case NCI_MT_RSP_PKT:
1418 nci_rsp_packet(ndev, skb);
1421 case NCI_MT_NTF_PKT:
1422 nci_ntf_packet(ndev, skb);
1425 case NCI_MT_DATA_PKT:
1426 nci_rx_data_packet(ndev, skb);
1430 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1436 /* check if a data exchange timout has occurred */
1437 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1438 /* complete the data exchange transaction, if exists */
1439 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1440 nci_data_exchange_complete(ndev, NULL,
1444 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1448 /* ----- NCI TX CMD worker thread ----- */
1450 static void nci_cmd_work(struct work_struct *work)
1452 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1453 struct sk_buff *skb;
1455 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1457 /* Send queued command */
1458 if (atomic_read(&ndev->cmd_cnt)) {
1459 skb = skb_dequeue(&ndev->cmd_q);
1463 atomic_dec(&ndev->cmd_cnt);
1465 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1467 nci_opcode_gid(nci_opcode(skb->data)),
1468 nci_opcode_oid(nci_opcode(skb->data)),
1469 nci_plen(skb->data));
1471 nci_send_frame(ndev, skb);
1473 mod_timer(&ndev->cmd_timer,
1474 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1478 MODULE_LICENSE("GPL");