1 // SPDX-License-Identifier: GPL-2.0-only
3 * The NFC Controller Interface is the communication protocol between an
4 * NFC Controller (NFCC) and a Device Host (DH).
6 * Copyright (C) 2011 Texas Instruments, Inc.
7 * Copyright (C) 2014 Marvell International Ltd.
9 * Written by Ilan Elias <ilane@ti.com>
12 * This file is based on hci_core.c, which was written
13 * by Maxim Krasnyansky.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/workqueue.h>
22 #include <linux/completion.h>
23 #include <linux/export.h>
24 #include <linux/sched.h>
25 #include <linux/bitops.h>
26 #include <linux/skbuff.h>
29 #include <net/nfc/nci.h>
30 #include <net/nfc/nci_core.h>
31 #include <linux/nfc.h>
33 struct core_conn_create_data {
35 struct nci_core_conn_create_cmd *cmd;
38 static void nci_cmd_work(struct work_struct *work);
39 static void nci_rx_work(struct work_struct *work);
40 static void nci_tx_work(struct work_struct *work);
42 struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
45 struct nci_conn_info *conn_info;
47 list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
48 if (conn_info->conn_id == conn_id)
55 int nci_get_conn_info_by_dest_type_params(struct nci_dev *ndev, u8 dest_type,
56 const struct dest_spec_params *params)
58 const struct nci_conn_info *conn_info;
60 list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
61 if (conn_info->dest_type == dest_type) {
63 return conn_info->conn_id;
65 if (params->id == conn_info->dest_params->id &&
66 params->protocol == conn_info->dest_params->protocol)
67 return conn_info->conn_id;
73 EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params);
75 /* ---- NCI requests ---- */
77 void nci_req_complete(struct nci_dev *ndev, int result)
79 if (ndev->req_status == NCI_REQ_PEND) {
80 ndev->req_result = result;
81 ndev->req_status = NCI_REQ_DONE;
82 complete(&ndev->req_completion);
85 EXPORT_SYMBOL(nci_req_complete);
87 static void nci_req_cancel(struct nci_dev *ndev, int err)
89 if (ndev->req_status == NCI_REQ_PEND) {
90 ndev->req_result = err;
91 ndev->req_status = NCI_REQ_CANCELED;
92 complete(&ndev->req_completion);
96 /* Execute request and wait for completion. */
97 static int __nci_request(struct nci_dev *ndev,
98 void (*req)(struct nci_dev *ndev, const void *opt),
99 const void *opt, __u32 timeout)
104 ndev->req_status = NCI_REQ_PEND;
106 reinit_completion(&ndev->req_completion);
109 wait_for_completion_interruptible_timeout(&ndev->req_completion,
112 pr_debug("wait_for_completion return %ld\n", completion_rc);
114 if (completion_rc > 0) {
115 switch (ndev->req_status) {
117 rc = nci_to_errno(ndev->req_result);
120 case NCI_REQ_CANCELED:
121 rc = -ndev->req_result;
129 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
132 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
135 ndev->req_status = ndev->req_result = 0;
140 inline int nci_request(struct nci_dev *ndev,
141 void (*req)(struct nci_dev *ndev,
143 const void *opt, __u32 timeout)
147 /* Serialize all requests */
148 mutex_lock(&ndev->req_lock);
149 /* check the state after obtaing the lock against any races
150 * from nci_close_device when the device gets removed.
152 if (test_bit(NCI_UP, &ndev->flags))
153 rc = __nci_request(ndev, req, opt, timeout);
156 mutex_unlock(&ndev->req_lock);
161 static void nci_reset_req(struct nci_dev *ndev, const void *opt)
163 struct nci_core_reset_cmd cmd;
165 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
166 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
169 static void nci_init_req(struct nci_dev *ndev, const void *opt)
174 plen = sizeof(struct nci_core_init_v2_cmd);
176 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, plen, opt);
179 static void nci_init_complete_req(struct nci_dev *ndev, const void *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, const void *opt)
223 const struct nci_set_config_param *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, const void *opt)
243 const struct nci_rf_discover_param *param = opt;
244 struct nci_rf_disc_cmd cmd;
246 cmd.num_disc_configs = 0;
248 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
249 (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
250 param->im_protocols & NFC_PROTO_MIFARE_MASK ||
251 param->im_protocols & NFC_PROTO_ISO14443_MASK ||
252 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
253 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
254 NCI_NFC_A_PASSIVE_POLL_MODE;
255 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
256 cmd.num_disc_configs++;
259 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
260 (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
261 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
262 NCI_NFC_B_PASSIVE_POLL_MODE;
263 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
264 cmd.num_disc_configs++;
267 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
268 (param->im_protocols & NFC_PROTO_FELICA_MASK ||
269 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
270 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
271 NCI_NFC_F_PASSIVE_POLL_MODE;
272 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
273 cmd.num_disc_configs++;
276 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
277 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
278 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
279 NCI_NFC_V_PASSIVE_POLL_MODE;
280 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
281 cmd.num_disc_configs++;
284 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
285 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
286 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
287 NCI_NFC_A_PASSIVE_LISTEN_MODE;
288 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
289 cmd.num_disc_configs++;
290 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
291 NCI_NFC_F_PASSIVE_LISTEN_MODE;
292 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
293 cmd.num_disc_configs++;
296 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
297 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
301 struct nci_rf_discover_select_param {
302 __u8 rf_discovery_id;
306 static void nci_rf_discover_select_req(struct nci_dev *ndev, const void *opt)
308 const struct nci_rf_discover_select_param *param = opt;
309 struct nci_rf_discover_select_cmd cmd;
311 cmd.rf_discovery_id = param->rf_discovery_id;
312 cmd.rf_protocol = param->rf_protocol;
314 switch (cmd.rf_protocol) {
315 case NCI_RF_PROTOCOL_ISO_DEP:
316 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
319 case NCI_RF_PROTOCOL_NFC_DEP:
320 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
324 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
328 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
329 sizeof(struct nci_rf_discover_select_cmd), &cmd);
332 static void nci_rf_deactivate_req(struct nci_dev *ndev, const void *opt)
334 struct nci_rf_deactivate_cmd cmd;
336 cmd.type = (unsigned long)opt;
338 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
339 sizeof(struct nci_rf_deactivate_cmd), &cmd);
342 struct nci_cmd_param {
348 static void nci_generic_req(struct nci_dev *ndev, const void *opt)
350 const struct nci_cmd_param *param = opt;
352 nci_send_cmd(ndev, param->opcode, param->len, param->payload);
355 int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, const __u8 *payload)
357 struct nci_cmd_param param;
359 param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
361 param.payload = payload;
363 return __nci_request(ndev, nci_generic_req, ¶m,
364 msecs_to_jiffies(NCI_CMD_TIMEOUT));
366 EXPORT_SYMBOL(nci_prop_cmd);
368 int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len,
371 struct nci_cmd_param param;
373 param.opcode = opcode;
375 param.payload = payload;
377 return __nci_request(ndev, nci_generic_req, ¶m,
378 msecs_to_jiffies(NCI_CMD_TIMEOUT));
380 EXPORT_SYMBOL(nci_core_cmd);
382 int nci_core_reset(struct nci_dev *ndev)
384 return __nci_request(ndev, nci_reset_req, (void *)0,
385 msecs_to_jiffies(NCI_RESET_TIMEOUT));
387 EXPORT_SYMBOL(nci_core_reset);
389 int nci_core_init(struct nci_dev *ndev)
391 return __nci_request(ndev, nci_init_req, (void *)0,
392 msecs_to_jiffies(NCI_INIT_TIMEOUT));
394 EXPORT_SYMBOL(nci_core_init);
396 struct nci_loopback_data {
398 struct sk_buff *data;
401 static void nci_send_data_req(struct nci_dev *ndev, const void *opt)
403 const struct nci_loopback_data *data = opt;
405 nci_send_data(ndev, data->conn_id, data->data);
408 static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
410 struct nci_dev *ndev = (struct nci_dev *)context;
411 struct nci_conn_info *conn_info;
413 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
415 nci_req_complete(ndev, NCI_STATUS_REJECTED);
419 conn_info->rx_skb = skb;
421 nci_req_complete(ndev, NCI_STATUS_OK);
424 int nci_nfcc_loopback(struct nci_dev *ndev, const void *data, size_t data_len,
425 struct sk_buff **resp)
428 struct nci_loopback_data loopback_data;
429 struct nci_conn_info *conn_info;
431 int conn_id = nci_get_conn_info_by_dest_type_params(ndev,
432 NCI_DESTINATION_NFCC_LOOPBACK, NULL);
435 r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCC_LOOPBACK,
437 if (r != NCI_STATUS_OK)
440 conn_id = nci_get_conn_info_by_dest_type_params(ndev,
441 NCI_DESTINATION_NFCC_LOOPBACK,
445 conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
449 /* store cb and context to be used on receiving data */
450 conn_info->data_exchange_cb = nci_nfcc_loopback_cb;
451 conn_info->data_exchange_cb_context = ndev;
453 skb = nci_skb_alloc(ndev, NCI_DATA_HDR_SIZE + data_len, GFP_KERNEL);
457 skb_reserve(skb, NCI_DATA_HDR_SIZE);
458 skb_put_data(skb, data, data_len);
460 loopback_data.conn_id = conn_id;
461 loopback_data.data = skb;
463 ndev->cur_conn_id = conn_id;
464 r = nci_request(ndev, nci_send_data_req, &loopback_data,
465 msecs_to_jiffies(NCI_DATA_TIMEOUT));
466 if (r == NCI_STATUS_OK && resp)
467 *resp = conn_info->rx_skb;
471 EXPORT_SYMBOL(nci_nfcc_loopback);
473 static int nci_open_device(struct nci_dev *ndev)
477 mutex_lock(&ndev->req_lock);
479 if (test_bit(NCI_UNREG, &ndev->flags)) {
484 if (test_bit(NCI_UP, &ndev->flags)) {
489 if (ndev->ops->open(ndev)) {
494 atomic_set(&ndev->cmd_cnt, 1);
496 set_bit(NCI_INIT, &ndev->flags);
499 rc = ndev->ops->init(ndev);
502 rc = __nci_request(ndev, nci_reset_req, (void *)0,
503 msecs_to_jiffies(NCI_RESET_TIMEOUT));
506 if (!rc && ndev->ops->setup) {
507 rc = ndev->ops->setup(ndev);
511 struct nci_core_init_v2_cmd nci_init_v2_cmd = {
512 .feature1 = NCI_FEATURE_DISABLE,
513 .feature2 = NCI_FEATURE_DISABLE
515 const void *opt = NULL;
517 if (ndev->nci_ver & NCI_VER_2_MASK)
518 opt = &nci_init_v2_cmd;
520 rc = __nci_request(ndev, nci_init_req, opt,
521 msecs_to_jiffies(NCI_INIT_TIMEOUT));
524 if (!rc && ndev->ops->post_setup)
525 rc = ndev->ops->post_setup(ndev);
528 rc = __nci_request(ndev, nci_init_complete_req, (void *)0,
529 msecs_to_jiffies(NCI_INIT_TIMEOUT));
532 clear_bit(NCI_INIT, &ndev->flags);
535 set_bit(NCI_UP, &ndev->flags);
536 nci_clear_target_list(ndev);
537 atomic_set(&ndev->state, NCI_IDLE);
539 /* Init failed, cleanup */
540 skb_queue_purge(&ndev->cmd_q);
541 skb_queue_purge(&ndev->rx_q);
542 skb_queue_purge(&ndev->tx_q);
544 ndev->ops->close(ndev);
545 ndev->flags &= BIT(NCI_UNREG);
549 mutex_unlock(&ndev->req_lock);
553 static int nci_close_device(struct nci_dev *ndev)
555 nci_req_cancel(ndev, ENODEV);
557 /* This mutex needs to be held as a barrier for
558 * caller nci_unregister_device
560 mutex_lock(&ndev->req_lock);
562 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
563 /* Need to flush the cmd wq in case
564 * there is a queued/running cmd_work
566 flush_workqueue(ndev->cmd_wq);
567 del_timer_sync(&ndev->cmd_timer);
568 del_timer_sync(&ndev->data_timer);
569 mutex_unlock(&ndev->req_lock);
573 /* Drop RX and TX queues */
574 skb_queue_purge(&ndev->rx_q);
575 skb_queue_purge(&ndev->tx_q);
577 /* Flush RX and TX wq */
578 flush_workqueue(ndev->rx_wq);
579 flush_workqueue(ndev->tx_wq);
582 skb_queue_purge(&ndev->cmd_q);
583 atomic_set(&ndev->cmd_cnt, 1);
585 set_bit(NCI_INIT, &ndev->flags);
586 __nci_request(ndev, nci_reset_req, (void *)0,
587 msecs_to_jiffies(NCI_RESET_TIMEOUT));
589 /* After this point our queues are empty
590 * and no works are scheduled.
592 ndev->ops->close(ndev);
594 clear_bit(NCI_INIT, &ndev->flags);
597 flush_workqueue(ndev->cmd_wq);
599 del_timer_sync(&ndev->cmd_timer);
601 /* Clear flags except NCI_UNREG */
602 ndev->flags &= BIT(NCI_UNREG);
604 mutex_unlock(&ndev->req_lock);
609 /* NCI command timer function */
610 static void nci_cmd_timer(struct timer_list *t)
612 struct nci_dev *ndev = from_timer(ndev, t, cmd_timer);
614 atomic_set(&ndev->cmd_cnt, 1);
615 queue_work(ndev->cmd_wq, &ndev->cmd_work);
618 /* NCI data exchange timer function */
619 static void nci_data_timer(struct timer_list *t)
621 struct nci_dev *ndev = from_timer(ndev, t, data_timer);
623 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
624 queue_work(ndev->rx_wq, &ndev->rx_work);
627 static int nci_dev_up(struct nfc_dev *nfc_dev)
629 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
631 return nci_open_device(ndev);
634 static int nci_dev_down(struct nfc_dev *nfc_dev)
636 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
638 return nci_close_device(ndev);
641 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, const __u8 *val)
643 struct nci_set_config_param param;
652 return __nci_request(ndev, nci_set_config_req, ¶m,
653 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
655 EXPORT_SYMBOL(nci_set_config);
657 static void nci_nfcee_discover_req(struct nci_dev *ndev, const void *opt)
659 struct nci_nfcee_discover_cmd cmd;
660 __u8 action = (unsigned long)opt;
662 cmd.discovery_action = action;
664 nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
667 int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
669 unsigned long opt = action;
671 return __nci_request(ndev, nci_nfcee_discover_req, (void *)opt,
672 msecs_to_jiffies(NCI_CMD_TIMEOUT));
674 EXPORT_SYMBOL(nci_nfcee_discover);
676 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, const void *opt)
678 const struct nci_nfcee_mode_set_cmd *cmd = opt;
680 nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
681 sizeof(struct nci_nfcee_mode_set_cmd), cmd);
684 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
686 struct nci_nfcee_mode_set_cmd cmd;
688 cmd.nfcee_id = nfcee_id;
689 cmd.nfcee_mode = nfcee_mode;
691 return __nci_request(ndev, nci_nfcee_mode_set_req, &cmd,
692 msecs_to_jiffies(NCI_CMD_TIMEOUT));
694 EXPORT_SYMBOL(nci_nfcee_mode_set);
696 static void nci_core_conn_create_req(struct nci_dev *ndev, const void *opt)
698 const struct core_conn_create_data *data = opt;
700 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
703 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
704 u8 number_destination_params,
706 const struct core_conn_create_dest_spec_params *params)
709 struct nci_core_conn_create_cmd *cmd;
710 struct core_conn_create_data data;
712 data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
713 cmd = kzalloc(data.length, GFP_KERNEL);
717 cmd->destination_type = destination_type;
718 cmd->number_destination_params = number_destination_params;
723 memcpy(cmd->params, params, params_len);
724 if (params->length > 0)
725 memcpy(&ndev->cur_params,
726 ¶ms->value[DEST_SPEC_PARAMS_ID_INDEX],
727 sizeof(struct dest_spec_params));
729 ndev->cur_params.id = 0;
731 ndev->cur_params.id = 0;
733 ndev->cur_dest_type = destination_type;
735 r = __nci_request(ndev, nci_core_conn_create_req, &data,
736 msecs_to_jiffies(NCI_CMD_TIMEOUT));
740 EXPORT_SYMBOL(nci_core_conn_create);
742 static void nci_core_conn_close_req(struct nci_dev *ndev, const void *opt)
744 __u8 conn_id = (unsigned long)opt;
746 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
749 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
751 unsigned long opt = conn_id;
753 ndev->cur_conn_id = conn_id;
754 return __nci_request(ndev, nci_core_conn_close_req, (void *)opt,
755 msecs_to_jiffies(NCI_CMD_TIMEOUT));
757 EXPORT_SYMBOL(nci_core_conn_close);
759 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
761 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
762 struct nci_set_config_param param;
765 param.val = nfc_get_local_general_bytes(nfc_dev, ¶m.len);
766 if ((param.val == NULL) || (param.len == 0))
769 if (param.len > NFC_MAX_GT_LEN)
772 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
774 rc = nci_request(ndev, nci_set_config_req, ¶m,
775 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
779 param.id = NCI_LN_ATR_RES_GEN_BYTES;
781 return nci_request(ndev, nci_set_config_req, ¶m,
782 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
785 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
787 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
791 val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
793 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
797 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
799 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
803 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
805 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
808 static int nci_start_poll(struct nfc_dev *nfc_dev,
809 __u32 im_protocols, __u32 tm_protocols)
811 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
812 struct nci_rf_discover_param param;
815 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
816 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
817 pr_err("unable to start poll, since poll is already active\n");
821 if (ndev->target_active_prot) {
822 pr_err("there is an active target\n");
826 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
827 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
828 pr_debug("target active or w4 select, implicitly deactivate\n");
830 rc = nci_request(ndev, nci_rf_deactivate_req,
831 (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE,
832 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
837 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
838 rc = nci_set_local_general_bytes(nfc_dev);
840 pr_err("failed to set local general bytes\n");
845 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
846 rc = nci_set_listen_parameters(nfc_dev);
848 pr_err("failed to set listen parameters\n");
851 param.im_protocols = im_protocols;
852 param.tm_protocols = tm_protocols;
853 rc = nci_request(ndev, nci_rf_discover_req, ¶m,
854 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
857 ndev->poll_prots = im_protocols;
862 static void nci_stop_poll(struct nfc_dev *nfc_dev)
864 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
866 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
867 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
868 pr_err("unable to stop poll, since poll is not active\n");
872 nci_request(ndev, nci_rf_deactivate_req,
873 (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE,
874 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
877 static int nci_activate_target(struct nfc_dev *nfc_dev,
878 struct nfc_target *target, __u32 protocol)
880 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
881 struct nci_rf_discover_select_param param;
882 const struct nfc_target *nci_target = NULL;
886 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
888 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
889 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
890 pr_err("there is no available target to activate\n");
894 if (ndev->target_active_prot) {
895 pr_err("there is already an active target\n");
899 for (i = 0; i < ndev->n_targets; i++) {
900 if (ndev->targets[i].idx == target->idx) {
901 nci_target = &ndev->targets[i];
907 pr_err("unable to find the selected target\n");
911 if (protocol >= NFC_PROTO_MAX) {
912 pr_err("the requested nfc protocol is invalid\n");
916 if (!(nci_target->supported_protocols & (1 << protocol))) {
917 pr_err("target does not support the requested protocol 0x%x\n",
922 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
923 param.rf_discovery_id = nci_target->logical_idx;
925 if (protocol == NFC_PROTO_JEWEL)
926 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
927 else if (protocol == NFC_PROTO_MIFARE)
928 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
929 else if (protocol == NFC_PROTO_FELICA)
930 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
931 else if (protocol == NFC_PROTO_ISO14443 ||
932 protocol == NFC_PROTO_ISO14443_B)
933 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
935 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
937 rc = nci_request(ndev, nci_rf_discover_select_req, ¶m,
938 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
942 ndev->target_active_prot = protocol;
947 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
948 struct nfc_target *target,
951 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
952 unsigned long nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
956 if (!ndev->target_active_prot) {
957 pr_err("unable to deactivate target, no active target\n");
961 ndev->target_active_prot = 0;
964 case NFC_TARGET_MODE_SLEEP:
965 nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
969 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
970 nci_request(ndev, nci_rf_deactivate_req, (void *)nci_mode,
971 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
975 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
976 __u8 comm_mode, __u8 *gb, size_t gb_len)
978 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
981 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
983 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
987 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
988 ndev->remote_gb_len);
990 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
996 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
998 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1001 pr_debug("entry\n");
1003 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
1004 nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
1006 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
1007 atomic_read(&ndev->state) == NCI_DISCOVERY) {
1008 nci_request(ndev, nci_rf_deactivate_req, (void *)0,
1009 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
1012 rc = nfc_tm_deactivated(nfc_dev);
1014 pr_err("error when signaling tm deactivation\n");
1021 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1022 struct sk_buff *skb,
1023 data_exchange_cb_t cb, void *cb_context)
1025 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1027 struct nci_conn_info *conn_info;
1029 conn_info = ndev->rf_conn_info;
1033 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
1035 if (!ndev->target_active_prot) {
1036 pr_err("unable to exchange data, no active target\n");
1040 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1043 /* store cb and context to be used on receiving data */
1044 conn_info->data_exchange_cb = cb;
1045 conn_info->data_exchange_cb_context = cb_context;
1047 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1049 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1054 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1056 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1059 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1061 pr_err("unable to send data\n");
1066 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1068 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1070 if (ndev->ops->enable_se)
1071 return ndev->ops->enable_se(ndev, se_idx);
1076 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1078 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1080 if (ndev->ops->disable_se)
1081 return ndev->ops->disable_se(ndev, se_idx);
1086 static int nci_discover_se(struct nfc_dev *nfc_dev)
1089 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1091 if (ndev->ops->discover_se) {
1092 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1093 if (r != NCI_STATUS_OK)
1096 return ndev->ops->discover_se(ndev);
1102 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1103 u8 *apdu, size_t apdu_length,
1104 se_io_cb_t cb, void *cb_context)
1106 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1108 if (ndev->ops->se_io)
1109 return ndev->ops->se_io(ndev, se_idx, apdu,
1110 apdu_length, cb, cb_context);
1115 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1117 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1119 if (!ndev->ops->fw_download)
1122 return ndev->ops->fw_download(ndev, firmware_name);
1125 static const struct nfc_ops nci_nfc_ops = {
1126 .dev_up = nci_dev_up,
1127 .dev_down = nci_dev_down,
1128 .start_poll = nci_start_poll,
1129 .stop_poll = nci_stop_poll,
1130 .dep_link_up = nci_dep_link_up,
1131 .dep_link_down = nci_dep_link_down,
1132 .activate_target = nci_activate_target,
1133 .deactivate_target = nci_deactivate_target,
1134 .im_transceive = nci_transceive,
1135 .tm_send = nci_tm_send,
1136 .enable_se = nci_enable_se,
1137 .disable_se = nci_disable_se,
1138 .discover_se = nci_discover_se,
1140 .fw_download = nci_fw_download,
1143 /* ---- Interface to NCI drivers ---- */
1145 * nci_allocate_device - allocate a new nci device
1147 * @ops: device operations
1148 * @supported_protocols: NFC protocols supported by the device
1149 * @tx_headroom: Reserved space at beginning of skb
1150 * @tx_tailroom: Reserved space at end of skb
1152 struct nci_dev *nci_allocate_device(const struct nci_ops *ops,
1153 __u32 supported_protocols,
1154 int tx_headroom, int tx_tailroom)
1156 struct nci_dev *ndev;
1158 pr_debug("supported_protocols 0x%x\n", supported_protocols);
1160 if (!ops->open || !ops->close || !ops->send)
1163 if (!supported_protocols)
1166 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1172 if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1173 pr_err("Too many proprietary commands: %zd\n",
1178 ndev->tx_headroom = tx_headroom;
1179 ndev->tx_tailroom = tx_tailroom;
1180 init_completion(&ndev->req_completion);
1182 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1183 supported_protocols,
1184 tx_headroom + NCI_DATA_HDR_SIZE,
1189 ndev->hci_dev = nci_hci_allocate(ndev);
1193 nfc_set_drvdata(ndev->nfc_dev, ndev);
1198 nfc_free_device(ndev->nfc_dev);
1203 EXPORT_SYMBOL(nci_allocate_device);
1206 * nci_free_device - deallocate nci device
1208 * @ndev: The nci device to deallocate
1210 void nci_free_device(struct nci_dev *ndev)
1212 nfc_free_device(ndev->nfc_dev);
1213 nci_hci_deallocate(ndev);
1216 EXPORT_SYMBOL(nci_free_device);
1219 * nci_register_device - register a nci device in the nfc subsystem
1221 * @ndev: The nci device to register
1223 int nci_register_device(struct nci_dev *ndev)
1226 struct device *dev = &ndev->nfc_dev->dev;
1231 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1232 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1233 ndev->cmd_wq = create_singlethread_workqueue(name);
1234 if (!ndev->cmd_wq) {
1239 INIT_WORK(&ndev->rx_work, nci_rx_work);
1240 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1241 ndev->rx_wq = create_singlethread_workqueue(name);
1244 goto destroy_cmd_wq_exit;
1247 INIT_WORK(&ndev->tx_work, nci_tx_work);
1248 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1249 ndev->tx_wq = create_singlethread_workqueue(name);
1252 goto destroy_rx_wq_exit;
1255 skb_queue_head_init(&ndev->cmd_q);
1256 skb_queue_head_init(&ndev->rx_q);
1257 skb_queue_head_init(&ndev->tx_q);
1259 timer_setup(&ndev->cmd_timer, nci_cmd_timer, 0);
1260 timer_setup(&ndev->data_timer, nci_data_timer, 0);
1262 mutex_init(&ndev->req_lock);
1263 INIT_LIST_HEAD(&ndev->conn_info_list);
1265 rc = nfc_register_device(ndev->nfc_dev);
1267 goto destroy_tx_wq_exit;
1272 destroy_workqueue(ndev->tx_wq);
1275 destroy_workqueue(ndev->rx_wq);
1277 destroy_cmd_wq_exit:
1278 destroy_workqueue(ndev->cmd_wq);
1283 EXPORT_SYMBOL(nci_register_device);
1286 * nci_unregister_device - unregister a nci device in the nfc subsystem
1288 * @ndev: The nci device to unregister
1290 void nci_unregister_device(struct nci_dev *ndev)
1292 struct nci_conn_info *conn_info, *n;
1294 /* This set_bit is not protected with specialized barrier,
1295 * However, it is fine because the mutex_lock(&ndev->req_lock);
1296 * in nci_close_device() will help to emit one.
1298 set_bit(NCI_UNREG, &ndev->flags);
1300 nci_close_device(ndev);
1302 destroy_workqueue(ndev->cmd_wq);
1303 destroy_workqueue(ndev->rx_wq);
1304 destroy_workqueue(ndev->tx_wq);
1306 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1307 list_del(&conn_info->list);
1308 /* conn_info is allocated with devm_kzalloc */
1311 nfc_unregister_device(ndev->nfc_dev);
1313 EXPORT_SYMBOL(nci_unregister_device);
1316 * nci_recv_frame - receive frame from NCI drivers
1318 * @ndev: The nci device
1319 * @skb: The sk_buff to receive
1321 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1323 pr_debug("len %d\n", skb->len);
1325 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1326 !test_bit(NCI_INIT, &ndev->flags))) {
1331 /* Queue frame for rx worker thread */
1332 skb_queue_tail(&ndev->rx_q, skb);
1333 queue_work(ndev->rx_wq, &ndev->rx_work);
1337 EXPORT_SYMBOL(nci_recv_frame);
1339 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1341 pr_debug("len %d\n", skb->len);
1348 /* Get rid of skb owner, prior to sending to the driver. */
1351 /* Send copy to sniffer */
1352 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1353 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1355 return ndev->ops->send(ndev, skb);
1357 EXPORT_SYMBOL(nci_send_frame);
1359 /* Send NCI command */
1360 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, const void *payload)
1362 struct nci_ctrl_hdr *hdr;
1363 struct sk_buff *skb;
1365 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1367 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1369 pr_err("no memory for command\n");
1373 hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
1374 hdr->gid = nci_opcode_gid(opcode);
1375 hdr->oid = nci_opcode_oid(opcode);
1378 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1379 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1382 skb_put_data(skb, payload, plen);
1384 skb_queue_tail(&ndev->cmd_q, skb);
1385 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1389 EXPORT_SYMBOL(nci_send_cmd);
1391 /* Proprietary commands API */
1392 static const struct nci_driver_ops *ops_cmd_lookup(const struct nci_driver_ops *ops,
1397 const struct nci_driver_ops *op;
1402 for (i = 0; i < n_ops; i++) {
1404 if (op->opcode == opcode)
1411 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1412 struct sk_buff *skb, const struct nci_driver_ops *ops,
1415 const struct nci_driver_ops *op;
1417 op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1418 if (!op || !op->rsp)
1421 return op->rsp(ndev, skb);
1424 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1425 struct sk_buff *skb, const struct nci_driver_ops *ops,
1428 const struct nci_driver_ops *op;
1430 op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1431 if (!op || !op->ntf)
1434 return op->ntf(ndev, skb);
1437 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1438 struct sk_buff *skb)
1440 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1441 ndev->ops->n_prop_ops);
1444 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1445 struct sk_buff *skb)
1447 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1448 ndev->ops->n_prop_ops);
1451 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1452 struct sk_buff *skb)
1454 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1455 ndev->ops->n_core_ops);
1458 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1459 struct sk_buff *skb)
1461 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1462 ndev->ops->n_core_ops);
1465 /* ---- NCI TX Data worker thread ---- */
1467 static void nci_tx_work(struct work_struct *work)
1469 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1470 struct nci_conn_info *conn_info;
1471 struct sk_buff *skb;
1473 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1477 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1479 /* Send queued tx data */
1480 while (atomic_read(&conn_info->credits_cnt)) {
1481 skb = skb_dequeue(&ndev->tx_q);
1485 /* Check if data flow control is used */
1486 if (atomic_read(&conn_info->credits_cnt) !=
1487 NCI_DATA_FLOW_CONTROL_NOT_USED)
1488 atomic_dec(&conn_info->credits_cnt);
1490 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1492 nci_conn_id(skb->data),
1493 nci_plen(skb->data));
1495 nci_send_frame(ndev, skb);
1497 mod_timer(&ndev->data_timer,
1498 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1502 /* ----- NCI RX worker thread (data & control) ----- */
1504 static void nci_rx_work(struct work_struct *work)
1506 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1507 struct sk_buff *skb;
1509 while ((skb = skb_dequeue(&ndev->rx_q))) {
1511 /* Send copy to sniffer */
1512 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1513 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1516 switch (nci_mt(skb->data)) {
1517 case NCI_MT_RSP_PKT:
1518 nci_rsp_packet(ndev, skb);
1521 case NCI_MT_NTF_PKT:
1522 nci_ntf_packet(ndev, skb);
1525 case NCI_MT_DATA_PKT:
1526 nci_rx_data_packet(ndev, skb);
1530 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1536 /* check if a data exchange timeout has occurred */
1537 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1538 /* complete the data exchange transaction, if exists */
1539 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1540 nci_data_exchange_complete(ndev, NULL,
1544 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1548 /* ----- NCI TX CMD worker thread ----- */
1550 static void nci_cmd_work(struct work_struct *work)
1552 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1553 struct sk_buff *skb;
1555 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1557 /* Send queued command */
1558 if (atomic_read(&ndev->cmd_cnt)) {
1559 skb = skb_dequeue(&ndev->cmd_q);
1563 atomic_dec(&ndev->cmd_cnt);
1565 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1567 nci_opcode_gid(nci_opcode(skb->data)),
1568 nci_opcode_oid(nci_opcode(skb->data)),
1569 nci_plen(skb->data));
1571 nci_send_frame(ndev, skb);
1573 mod_timer(&ndev->cmd_timer,
1574 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1578 MODULE_LICENSE("GPL");