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_dest_type_params(struct nci_dev *ndev, u8 dest_type,
68 struct dest_spec_params *params)
70 struct nci_conn_info *conn_info;
72 list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
73 if (conn_info->dest_type == dest_type) {
75 return conn_info->conn_id;
77 if (params->id == conn_info->dest_params->id &&
78 params->protocol == conn_info->dest_params->protocol)
79 return conn_info->conn_id;
85 EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params);
87 /* ---- NCI requests ---- */
89 void nci_req_complete(struct nci_dev *ndev, int result)
91 if (ndev->req_status == NCI_REQ_PEND) {
92 ndev->req_result = result;
93 ndev->req_status = NCI_REQ_DONE;
94 complete(&ndev->req_completion);
97 EXPORT_SYMBOL(nci_req_complete);
99 static void nci_req_cancel(struct nci_dev *ndev, int err)
101 if (ndev->req_status == NCI_REQ_PEND) {
102 ndev->req_result = err;
103 ndev->req_status = NCI_REQ_CANCELED;
104 complete(&ndev->req_completion);
108 /* Execute request and wait for completion. */
109 static int __nci_request(struct nci_dev *ndev,
110 void (*req)(struct nci_dev *ndev, unsigned long opt),
111 unsigned long opt, __u32 timeout)
116 ndev->req_status = NCI_REQ_PEND;
118 reinit_completion(&ndev->req_completion);
121 wait_for_completion_interruptible_timeout(&ndev->req_completion,
124 pr_debug("wait_for_completion return %ld\n", completion_rc);
126 if (completion_rc > 0) {
127 switch (ndev->req_status) {
129 rc = nci_to_errno(ndev->req_result);
132 case NCI_REQ_CANCELED:
133 rc = -ndev->req_result;
141 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
144 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
147 ndev->req_status = ndev->req_result = 0;
152 inline int nci_request(struct nci_dev *ndev,
153 void (*req)(struct nci_dev *ndev,
155 unsigned long opt, __u32 timeout)
159 /* Serialize all requests */
160 mutex_lock(&ndev->req_lock);
161 /* check the state after obtaing the lock against any races
162 * from nci_close_device when the device gets removed.
164 if (test_bit(NCI_UP, &ndev->flags))
165 rc = __nci_request(ndev, req, opt, timeout);
168 mutex_unlock(&ndev->req_lock);
173 static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
175 struct nci_core_reset_cmd cmd;
177 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
178 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
181 static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
183 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
186 static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
188 struct nci_rf_disc_map_cmd cmd;
189 struct disc_map_config *cfg = cmd.mapping_configs;
190 __u8 *num = &cmd.num_mapping_configs;
193 /* set rf mapping configurations */
196 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
197 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
198 if (ndev->supported_rf_interfaces[i] ==
199 NCI_RF_INTERFACE_ISO_DEP) {
200 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
201 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
202 NCI_DISC_MAP_MODE_LISTEN;
203 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
205 } else if (ndev->supported_rf_interfaces[i] ==
206 NCI_RF_INTERFACE_NFC_DEP) {
207 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
208 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
209 NCI_DISC_MAP_MODE_LISTEN;
210 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
214 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
218 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
219 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
222 struct nci_set_config_param {
228 static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
230 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
231 struct nci_core_set_config_cmd cmd;
233 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
236 cmd.param.id = param->id;
237 cmd.param.len = param->len;
238 memcpy(cmd.param.val, param->val, param->len);
240 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
243 struct nci_rf_discover_param {
248 static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
250 struct nci_rf_discover_param *param =
251 (struct nci_rf_discover_param *)opt;
252 struct nci_rf_disc_cmd cmd;
254 cmd.num_disc_configs = 0;
256 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
257 (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
258 param->im_protocols & NFC_PROTO_MIFARE_MASK ||
259 param->im_protocols & NFC_PROTO_ISO14443_MASK ||
260 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
261 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
262 NCI_NFC_A_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_ISO14443_B_MASK)) {
269 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
270 NCI_NFC_B_PASSIVE_POLL_MODE;
271 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
272 cmd.num_disc_configs++;
275 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
276 (param->im_protocols & NFC_PROTO_FELICA_MASK ||
277 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
278 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
279 NCI_NFC_F_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) &&
285 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
286 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
287 NCI_NFC_V_PASSIVE_POLL_MODE;
288 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
289 cmd.num_disc_configs++;
292 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
293 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
294 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
295 NCI_NFC_A_PASSIVE_LISTEN_MODE;
296 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
297 cmd.num_disc_configs++;
298 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
299 NCI_NFC_F_PASSIVE_LISTEN_MODE;
300 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
301 cmd.num_disc_configs++;
304 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
305 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
309 struct nci_rf_discover_select_param {
310 __u8 rf_discovery_id;
314 static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
316 struct nci_rf_discover_select_param *param =
317 (struct nci_rf_discover_select_param *)opt;
318 struct nci_rf_discover_select_cmd cmd;
320 cmd.rf_discovery_id = param->rf_discovery_id;
321 cmd.rf_protocol = param->rf_protocol;
323 switch (cmd.rf_protocol) {
324 case NCI_RF_PROTOCOL_ISO_DEP:
325 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
328 case NCI_RF_PROTOCOL_NFC_DEP:
329 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
333 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
337 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
338 sizeof(struct nci_rf_discover_select_cmd), &cmd);
341 static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
343 struct nci_rf_deactivate_cmd cmd;
347 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
348 sizeof(struct nci_rf_deactivate_cmd), &cmd);
351 struct nci_cmd_param {
357 static void nci_generic_req(struct nci_dev *ndev, unsigned long opt)
359 struct nci_cmd_param *param =
360 (struct nci_cmd_param *)opt;
362 nci_send_cmd(ndev, param->opcode, param->len, param->payload);
365 int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, __u8 *payload)
367 struct nci_cmd_param param;
369 param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
371 param.payload = payload;
373 return __nci_request(ndev, nci_generic_req, (unsigned long)¶m,
374 msecs_to_jiffies(NCI_CMD_TIMEOUT));
376 EXPORT_SYMBOL(nci_prop_cmd);
378 int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len, __u8 *payload)
380 struct nci_cmd_param param;
382 param.opcode = opcode;
384 param.payload = payload;
386 return __nci_request(ndev, nci_generic_req, (unsigned long)¶m,
387 msecs_to_jiffies(NCI_CMD_TIMEOUT));
389 EXPORT_SYMBOL(nci_core_cmd);
391 int nci_core_reset(struct nci_dev *ndev)
393 return __nci_request(ndev, nci_reset_req, 0,
394 msecs_to_jiffies(NCI_RESET_TIMEOUT));
396 EXPORT_SYMBOL(nci_core_reset);
398 int nci_core_init(struct nci_dev *ndev)
400 return __nci_request(ndev, nci_init_req, 0,
401 msecs_to_jiffies(NCI_INIT_TIMEOUT));
403 EXPORT_SYMBOL(nci_core_init);
405 struct nci_loopback_data {
407 struct sk_buff *data;
410 static void nci_send_data_req(struct nci_dev *ndev, unsigned long opt)
412 struct nci_loopback_data *data = (struct nci_loopback_data *)opt;
414 nci_send_data(ndev, data->conn_id, data->data);
417 static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
419 struct nci_dev *ndev = (struct nci_dev *)context;
420 struct nci_conn_info *conn_info;
422 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
424 nci_req_complete(ndev, NCI_STATUS_REJECTED);
428 conn_info->rx_skb = skb;
430 nci_req_complete(ndev, NCI_STATUS_OK);
433 int nci_nfcc_loopback(struct nci_dev *ndev, void *data, size_t data_len,
434 struct sk_buff **resp)
437 struct nci_loopback_data loopback_data;
438 struct nci_conn_info *conn_info;
440 int conn_id = nci_get_conn_info_by_dest_type_params(ndev,
441 NCI_DESTINATION_NFCC_LOOPBACK, NULL);
444 r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCC_LOOPBACK,
446 if (r != NCI_STATUS_OK)
449 conn_id = nci_get_conn_info_by_dest_type_params(ndev,
450 NCI_DESTINATION_NFCC_LOOPBACK,
454 conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
458 /* store cb and context to be used on receiving data */
459 conn_info->data_exchange_cb = nci_nfcc_loopback_cb;
460 conn_info->data_exchange_cb_context = ndev;
462 skb = nci_skb_alloc(ndev, NCI_DATA_HDR_SIZE + data_len, GFP_KERNEL);
466 skb_reserve(skb, NCI_DATA_HDR_SIZE);
467 skb_put_data(skb, data, data_len);
469 loopback_data.conn_id = conn_id;
470 loopback_data.data = skb;
472 ndev->cur_conn_id = conn_id;
473 r = nci_request(ndev, nci_send_data_req, (unsigned long)&loopback_data,
474 msecs_to_jiffies(NCI_DATA_TIMEOUT));
475 if (r == NCI_STATUS_OK && resp)
476 *resp = conn_info->rx_skb;
480 EXPORT_SYMBOL(nci_nfcc_loopback);
482 static int nci_open_device(struct nci_dev *ndev)
486 mutex_lock(&ndev->req_lock);
488 if (test_bit(NCI_UNREG, &ndev->flags)) {
493 if (test_bit(NCI_UP, &ndev->flags)) {
498 if (ndev->ops->open(ndev)) {
503 atomic_set(&ndev->cmd_cnt, 1);
505 set_bit(NCI_INIT, &ndev->flags);
508 rc = ndev->ops->init(ndev);
511 rc = __nci_request(ndev, nci_reset_req, 0,
512 msecs_to_jiffies(NCI_RESET_TIMEOUT));
515 if (!rc && ndev->ops->setup) {
516 rc = ndev->ops->setup(ndev);
520 rc = __nci_request(ndev, nci_init_req, 0,
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, 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);
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, 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);
596 del_timer_sync(&ndev->cmd_timer);
599 flush_workqueue(ndev->cmd_wq);
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, __u8 *val)
643 struct nci_set_config_param param;
652 return __nci_request(ndev, nci_set_config_req, (unsigned long)¶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, unsigned long opt)
659 struct nci_nfcee_discover_cmd cmd;
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 return __nci_request(ndev, nci_nfcee_discover_req, action,
670 msecs_to_jiffies(NCI_CMD_TIMEOUT));
672 EXPORT_SYMBOL(nci_nfcee_discover);
674 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
676 struct nci_nfcee_mode_set_cmd *cmd =
677 (struct nci_nfcee_mode_set_cmd *)opt;
679 nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
680 sizeof(struct nci_nfcee_mode_set_cmd), cmd);
683 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
685 struct nci_nfcee_mode_set_cmd cmd;
687 cmd.nfcee_id = nfcee_id;
688 cmd.nfcee_mode = nfcee_mode;
690 return __nci_request(ndev, nci_nfcee_mode_set_req,
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, unsigned long opt)
698 struct core_conn_create_data *data =
699 (struct core_conn_create_data *)opt;
701 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
704 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
705 u8 number_destination_params,
707 struct core_conn_create_dest_spec_params *params)
710 struct nci_core_conn_create_cmd *cmd;
711 struct core_conn_create_data data;
713 data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
714 cmd = kzalloc(data.length, GFP_KERNEL);
718 cmd->destination_type = destination_type;
719 cmd->number_destination_params = number_destination_params;
724 memcpy(cmd->params, params, params_len);
725 if (params->length > 0)
726 memcpy(&ndev->cur_params,
727 ¶ms->value[DEST_SPEC_PARAMS_ID_INDEX],
728 sizeof(struct dest_spec_params));
730 ndev->cur_params.id = 0;
732 ndev->cur_params.id = 0;
734 ndev->cur_dest_type = destination_type;
736 r = __nci_request(ndev, nci_core_conn_create_req, (unsigned long)&data,
737 msecs_to_jiffies(NCI_CMD_TIMEOUT));
741 EXPORT_SYMBOL(nci_core_conn_create);
743 static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
747 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
750 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
752 ndev->cur_conn_id = conn_id;
753 return __nci_request(ndev, nci_core_conn_close_req, conn_id,
754 msecs_to_jiffies(NCI_CMD_TIMEOUT));
756 EXPORT_SYMBOL(nci_core_conn_close);
758 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
760 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
761 struct nci_set_config_param param;
764 param.val = nfc_get_local_general_bytes(nfc_dev, ¶m.len);
765 if ((param.val == NULL) || (param.len == 0))
768 if (param.len > NFC_MAX_GT_LEN)
771 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
773 rc = nci_request(ndev, nci_set_config_req, (unsigned long)¶m,
774 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
778 param.id = NCI_LN_ATR_RES_GEN_BYTES;
780 return nci_request(ndev, nci_set_config_req, (unsigned long)¶m,
781 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
784 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
786 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
790 val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
792 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
796 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
798 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
802 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
804 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
807 static int nci_start_poll(struct nfc_dev *nfc_dev,
808 __u32 im_protocols, __u32 tm_protocols)
810 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
811 struct nci_rf_discover_param param;
814 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
815 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
816 pr_err("unable to start poll, since poll is already active\n");
820 if (ndev->target_active_prot) {
821 pr_err("there is an active target\n");
825 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
826 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
827 pr_debug("target active or w4 select, implicitly deactivate\n");
829 rc = nci_request(ndev, nci_rf_deactivate_req,
830 NCI_DEACTIVATE_TYPE_IDLE_MODE,
831 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
836 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
837 rc = nci_set_local_general_bytes(nfc_dev);
839 pr_err("failed to set local general bytes\n");
844 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
845 rc = nci_set_listen_parameters(nfc_dev);
847 pr_err("failed to set listen parameters\n");
850 param.im_protocols = im_protocols;
851 param.tm_protocols = tm_protocols;
852 rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)¶m,
853 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
856 ndev->poll_prots = im_protocols;
861 static void nci_stop_poll(struct nfc_dev *nfc_dev)
863 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
865 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
866 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
867 pr_err("unable to stop poll, since poll is not active\n");
871 nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
872 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
875 static int nci_activate_target(struct nfc_dev *nfc_dev,
876 struct nfc_target *target, __u32 protocol)
878 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
879 struct nci_rf_discover_select_param param;
880 struct nfc_target *nci_target = NULL;
884 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
886 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
887 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
888 pr_err("there is no available target to activate\n");
892 if (ndev->target_active_prot) {
893 pr_err("there is already an active target\n");
897 for (i = 0; i < ndev->n_targets; i++) {
898 if (ndev->targets[i].idx == target->idx) {
899 nci_target = &ndev->targets[i];
905 pr_err("unable to find the selected target\n");
909 if (!(nci_target->supported_protocols & (1 << protocol))) {
910 pr_err("target does not support the requested protocol 0x%x\n",
915 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
916 param.rf_discovery_id = nci_target->logical_idx;
918 if (protocol == NFC_PROTO_JEWEL)
919 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
920 else if (protocol == NFC_PROTO_MIFARE)
921 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
922 else if (protocol == NFC_PROTO_FELICA)
923 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
924 else if (protocol == NFC_PROTO_ISO14443 ||
925 protocol == NFC_PROTO_ISO14443_B)
926 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
928 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
930 rc = nci_request(ndev, nci_rf_discover_select_req,
931 (unsigned long)¶m,
932 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
936 ndev->target_active_prot = protocol;
941 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
942 struct nfc_target *target,
945 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
946 u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
950 if (!ndev->target_active_prot) {
951 pr_err("unable to deactivate target, no active target\n");
955 ndev->target_active_prot = 0;
958 case NFC_TARGET_MODE_SLEEP:
959 nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
963 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
964 nci_request(ndev, nci_rf_deactivate_req, nci_mode,
965 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
969 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
970 __u8 comm_mode, __u8 *gb, size_t gb_len)
972 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
975 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
977 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
981 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
982 ndev->remote_gb_len);
984 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
990 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
992 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
997 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
998 nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
1000 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
1001 atomic_read(&ndev->state) == NCI_DISCOVERY) {
1002 nci_request(ndev, nci_rf_deactivate_req, 0,
1003 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
1006 rc = nfc_tm_deactivated(nfc_dev);
1008 pr_err("error when signaling tm deactivation\n");
1015 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1016 struct sk_buff *skb,
1017 data_exchange_cb_t cb, void *cb_context)
1019 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1021 struct nci_conn_info *conn_info;
1023 conn_info = ndev->rf_conn_info;
1027 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
1029 if (!ndev->target_active_prot) {
1030 pr_err("unable to exchange data, no active target\n");
1034 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1037 /* store cb and context to be used on receiving data */
1038 conn_info->data_exchange_cb = cb;
1039 conn_info->data_exchange_cb_context = cb_context;
1041 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1043 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1048 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1050 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1053 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1055 pr_err("unable to send data\n");
1060 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1062 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1064 if (ndev->ops->enable_se)
1065 return ndev->ops->enable_se(ndev, se_idx);
1070 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1072 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1074 if (ndev->ops->disable_se)
1075 return ndev->ops->disable_se(ndev, se_idx);
1080 static int nci_discover_se(struct nfc_dev *nfc_dev)
1083 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1085 if (ndev->ops->discover_se) {
1086 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1087 if (r != NCI_STATUS_OK)
1090 return ndev->ops->discover_se(ndev);
1096 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1097 u8 *apdu, size_t apdu_length,
1098 se_io_cb_t cb, void *cb_context)
1100 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1102 if (ndev->ops->se_io)
1103 return ndev->ops->se_io(ndev, se_idx, apdu,
1104 apdu_length, cb, cb_context);
1109 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1111 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1113 if (!ndev->ops->fw_download)
1116 return ndev->ops->fw_download(ndev, firmware_name);
1119 static struct nfc_ops nci_nfc_ops = {
1120 .dev_up = nci_dev_up,
1121 .dev_down = nci_dev_down,
1122 .start_poll = nci_start_poll,
1123 .stop_poll = nci_stop_poll,
1124 .dep_link_up = nci_dep_link_up,
1125 .dep_link_down = nci_dep_link_down,
1126 .activate_target = nci_activate_target,
1127 .deactivate_target = nci_deactivate_target,
1128 .im_transceive = nci_transceive,
1129 .tm_send = nci_tm_send,
1130 .enable_se = nci_enable_se,
1131 .disable_se = nci_disable_se,
1132 .discover_se = nci_discover_se,
1134 .fw_download = nci_fw_download,
1137 /* ---- Interface to NCI drivers ---- */
1139 * nci_allocate_device - allocate a new nci device
1141 * @ops: device operations
1142 * @supported_protocols: NFC protocols supported by the device
1144 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
1145 __u32 supported_protocols,
1146 int tx_headroom, int tx_tailroom)
1148 struct nci_dev *ndev;
1150 pr_debug("supported_protocols 0x%x\n", supported_protocols);
1152 if (!ops->open || !ops->close || !ops->send)
1155 if (!supported_protocols)
1158 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1164 if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1165 pr_err("Too many proprietary commands: %zd\n",
1167 ops->prop_ops = NULL;
1168 ops->n_prop_ops = 0;
1171 ndev->tx_headroom = tx_headroom;
1172 ndev->tx_tailroom = tx_tailroom;
1173 init_completion(&ndev->req_completion);
1175 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1176 supported_protocols,
1177 tx_headroom + NCI_DATA_HDR_SIZE,
1182 ndev->hci_dev = nci_hci_allocate(ndev);
1186 nfc_set_drvdata(ndev->nfc_dev, ndev);
1191 nfc_free_device(ndev->nfc_dev);
1196 EXPORT_SYMBOL(nci_allocate_device);
1199 * nci_free_device - deallocate nci device
1201 * @ndev: The nci device to deallocate
1203 void nci_free_device(struct nci_dev *ndev)
1205 nfc_free_device(ndev->nfc_dev);
1206 nci_hci_deallocate(ndev);
1209 EXPORT_SYMBOL(nci_free_device);
1212 * nci_register_device - register a nci device in the nfc subsystem
1214 * @dev: The nci device to register
1216 int nci_register_device(struct nci_dev *ndev)
1219 struct device *dev = &ndev->nfc_dev->dev;
1224 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1225 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1226 ndev->cmd_wq = create_singlethread_workqueue(name);
1227 if (!ndev->cmd_wq) {
1232 INIT_WORK(&ndev->rx_work, nci_rx_work);
1233 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1234 ndev->rx_wq = create_singlethread_workqueue(name);
1237 goto destroy_cmd_wq_exit;
1240 INIT_WORK(&ndev->tx_work, nci_tx_work);
1241 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1242 ndev->tx_wq = create_singlethread_workqueue(name);
1245 goto destroy_rx_wq_exit;
1248 skb_queue_head_init(&ndev->cmd_q);
1249 skb_queue_head_init(&ndev->rx_q);
1250 skb_queue_head_init(&ndev->tx_q);
1252 timer_setup(&ndev->cmd_timer, nci_cmd_timer, 0);
1253 timer_setup(&ndev->data_timer, nci_data_timer, 0);
1255 mutex_init(&ndev->req_lock);
1256 INIT_LIST_HEAD(&ndev->conn_info_list);
1258 rc = nfc_register_device(ndev->nfc_dev);
1260 goto destroy_rx_wq_exit;
1265 destroy_workqueue(ndev->rx_wq);
1267 destroy_cmd_wq_exit:
1268 destroy_workqueue(ndev->cmd_wq);
1273 EXPORT_SYMBOL(nci_register_device);
1276 * nci_unregister_device - unregister a nci device in the nfc subsystem
1278 * @dev: The nci device to unregister
1280 void nci_unregister_device(struct nci_dev *ndev)
1282 struct nci_conn_info *conn_info, *n;
1284 /* This set_bit is not protected with specialized barrier,
1285 * However, it is fine because the mutex_lock(&ndev->req_lock);
1286 * in nci_close_device() will help to emit one.
1288 set_bit(NCI_UNREG, &ndev->flags);
1290 nci_close_device(ndev);
1292 destroy_workqueue(ndev->cmd_wq);
1293 destroy_workqueue(ndev->rx_wq);
1294 destroy_workqueue(ndev->tx_wq);
1296 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1297 list_del(&conn_info->list);
1298 /* conn_info is allocated with devm_kzalloc */
1301 nfc_unregister_device(ndev->nfc_dev);
1303 EXPORT_SYMBOL(nci_unregister_device);
1306 * nci_recv_frame - receive frame from NCI drivers
1308 * @ndev: The nci device
1309 * @skb: The sk_buff to receive
1311 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1313 pr_debug("len %d\n", skb->len);
1315 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1316 !test_bit(NCI_INIT, &ndev->flags))) {
1321 /* Queue frame for rx worker thread */
1322 skb_queue_tail(&ndev->rx_q, skb);
1323 queue_work(ndev->rx_wq, &ndev->rx_work);
1327 EXPORT_SYMBOL(nci_recv_frame);
1329 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1331 pr_debug("len %d\n", skb->len);
1338 /* Get rid of skb owner, prior to sending to the driver. */
1341 /* Send copy to sniffer */
1342 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1343 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1345 return ndev->ops->send(ndev, skb);
1347 EXPORT_SYMBOL(nci_send_frame);
1349 /* Send NCI command */
1350 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1352 struct nci_ctrl_hdr *hdr;
1353 struct sk_buff *skb;
1355 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1357 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1359 pr_err("no memory for command\n");
1363 hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
1364 hdr->gid = nci_opcode_gid(opcode);
1365 hdr->oid = nci_opcode_oid(opcode);
1368 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1369 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1372 skb_put_data(skb, payload, plen);
1374 skb_queue_tail(&ndev->cmd_q, skb);
1375 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1379 EXPORT_SYMBOL(nci_send_cmd);
1381 /* Proprietary commands API */
1382 static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
1387 struct nci_driver_ops *op;
1392 for (i = 0; i < n_ops; i++) {
1394 if (op->opcode == opcode)
1401 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1402 struct sk_buff *skb, struct nci_driver_ops *ops,
1405 struct nci_driver_ops *op;
1407 op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1408 if (!op || !op->rsp)
1411 return op->rsp(ndev, skb);
1414 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1415 struct sk_buff *skb, struct nci_driver_ops *ops,
1418 struct nci_driver_ops *op;
1420 op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1421 if (!op || !op->ntf)
1424 return op->ntf(ndev, skb);
1427 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1428 struct sk_buff *skb)
1430 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1431 ndev->ops->n_prop_ops);
1434 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1435 struct sk_buff *skb)
1437 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1438 ndev->ops->n_prop_ops);
1441 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1442 struct sk_buff *skb)
1444 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1445 ndev->ops->n_core_ops);
1448 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1449 struct sk_buff *skb)
1451 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1452 ndev->ops->n_core_ops);
1455 /* ---- NCI TX Data worker thread ---- */
1457 static void nci_tx_work(struct work_struct *work)
1459 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1460 struct nci_conn_info *conn_info;
1461 struct sk_buff *skb;
1463 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1467 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1469 /* Send queued tx data */
1470 while (atomic_read(&conn_info->credits_cnt)) {
1471 skb = skb_dequeue(&ndev->tx_q);
1475 /* Check if data flow control is used */
1476 if (atomic_read(&conn_info->credits_cnt) !=
1477 NCI_DATA_FLOW_CONTROL_NOT_USED)
1478 atomic_dec(&conn_info->credits_cnt);
1480 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1482 nci_conn_id(skb->data),
1483 nci_plen(skb->data));
1485 nci_send_frame(ndev, skb);
1487 mod_timer(&ndev->data_timer,
1488 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1492 /* ----- NCI RX worker thread (data & control) ----- */
1494 static void nci_rx_work(struct work_struct *work)
1496 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1497 struct sk_buff *skb;
1499 while ((skb = skb_dequeue(&ndev->rx_q))) {
1501 /* Send copy to sniffer */
1502 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1503 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1506 switch (nci_mt(skb->data)) {
1507 case NCI_MT_RSP_PKT:
1508 nci_rsp_packet(ndev, skb);
1511 case NCI_MT_NTF_PKT:
1512 nci_ntf_packet(ndev, skb);
1515 case NCI_MT_DATA_PKT:
1516 nci_rx_data_packet(ndev, skb);
1520 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1526 /* check if a data exchange timout has occurred */
1527 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1528 /* complete the data exchange transaction, if exists */
1529 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1530 nci_data_exchange_complete(ndev, NULL,
1534 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1538 /* ----- NCI TX CMD worker thread ----- */
1540 static void nci_cmd_work(struct work_struct *work)
1542 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1543 struct sk_buff *skb;
1545 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1547 /* Send queued command */
1548 if (atomic_read(&ndev->cmd_cnt)) {
1549 skb = skb_dequeue(&ndev->cmd_q);
1553 atomic_dec(&ndev->cmd_cnt);
1555 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1557 nci_opcode_gid(nci_opcode(skb->data)),
1558 nci_opcode_oid(nci_opcode(skb->data)),
1559 nci_plen(skb->data));
1561 nci_send_frame(ndev, skb);
1563 mod_timer(&ndev->cmd_timer,
1564 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1568 MODULE_LICENSE("GPL");