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);
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, 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(unsigned long arg)
612 struct nci_dev *ndev = (void *) arg;
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(unsigned long arg)
621 struct nci_dev *ndev = (void *) arg;
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 (protocol >= NFC_PROTO_MAX) {
910 pr_err("the requested nfc protocol is invalid\n");
914 if (!(nci_target->supported_protocols & (1 << protocol))) {
915 pr_err("target does not support the requested protocol 0x%x\n",
920 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
921 param.rf_discovery_id = nci_target->logical_idx;
923 if (protocol == NFC_PROTO_JEWEL)
924 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
925 else if (protocol == NFC_PROTO_MIFARE)
926 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
927 else if (protocol == NFC_PROTO_FELICA)
928 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
929 else if (protocol == NFC_PROTO_ISO14443 ||
930 protocol == NFC_PROTO_ISO14443_B)
931 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
933 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
935 rc = nci_request(ndev, nci_rf_discover_select_req,
936 (unsigned long)¶m,
937 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
941 ndev->target_active_prot = protocol;
946 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
947 struct nfc_target *target,
950 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
951 u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
955 if (!ndev->target_active_prot) {
956 pr_err("unable to deactivate target, no active target\n");
960 ndev->target_active_prot = 0;
963 case NFC_TARGET_MODE_SLEEP:
964 nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
968 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
969 nci_request(ndev, nci_rf_deactivate_req, nci_mode,
970 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
974 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
975 __u8 comm_mode, __u8 *gb, size_t gb_len)
977 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
980 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
982 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
986 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
987 ndev->remote_gb_len);
989 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
995 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
997 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1000 pr_debug("entry\n");
1002 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
1003 nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
1005 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
1006 atomic_read(&ndev->state) == NCI_DISCOVERY) {
1007 nci_request(ndev, nci_rf_deactivate_req, 0,
1008 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
1011 rc = nfc_tm_deactivated(nfc_dev);
1013 pr_err("error when signaling tm deactivation\n");
1020 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1021 struct sk_buff *skb,
1022 data_exchange_cb_t cb, void *cb_context)
1024 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1026 struct nci_conn_info *conn_info;
1028 conn_info = ndev->rf_conn_info;
1032 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
1034 if (!ndev->target_active_prot) {
1035 pr_err("unable to exchange data, no active target\n");
1039 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1042 /* store cb and context to be used on receiving data */
1043 conn_info->data_exchange_cb = cb;
1044 conn_info->data_exchange_cb_context = cb_context;
1046 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1048 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1053 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1055 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1058 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1060 pr_err("unable to send data\n");
1065 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1067 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1069 if (ndev->ops->enable_se)
1070 return ndev->ops->enable_se(ndev, se_idx);
1075 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1077 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1079 if (ndev->ops->disable_se)
1080 return ndev->ops->disable_se(ndev, se_idx);
1085 static int nci_discover_se(struct nfc_dev *nfc_dev)
1088 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1090 if (ndev->ops->discover_se) {
1091 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1092 if (r != NCI_STATUS_OK)
1095 return ndev->ops->discover_se(ndev);
1101 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1102 u8 *apdu, size_t apdu_length,
1103 se_io_cb_t cb, void *cb_context)
1105 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1107 if (ndev->ops->se_io)
1108 return ndev->ops->se_io(ndev, se_idx, apdu,
1109 apdu_length, cb, cb_context);
1114 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1116 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1118 if (!ndev->ops->fw_download)
1121 return ndev->ops->fw_download(ndev, firmware_name);
1124 static struct nfc_ops nci_nfc_ops = {
1125 .dev_up = nci_dev_up,
1126 .dev_down = nci_dev_down,
1127 .start_poll = nci_start_poll,
1128 .stop_poll = nci_stop_poll,
1129 .dep_link_up = nci_dep_link_up,
1130 .dep_link_down = nci_dep_link_down,
1131 .activate_target = nci_activate_target,
1132 .deactivate_target = nci_deactivate_target,
1133 .im_transceive = nci_transceive,
1134 .tm_send = nci_tm_send,
1135 .enable_se = nci_enable_se,
1136 .disable_se = nci_disable_se,
1137 .discover_se = nci_discover_se,
1139 .fw_download = nci_fw_download,
1142 /* ---- Interface to NCI drivers ---- */
1144 * nci_allocate_device - allocate a new nci device
1146 * @ops: device operations
1147 * @supported_protocols: NFC protocols supported by the device
1149 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
1150 __u32 supported_protocols,
1151 int tx_headroom, int tx_tailroom)
1153 struct nci_dev *ndev;
1155 pr_debug("supported_protocols 0x%x\n", supported_protocols);
1157 if (!ops->open || !ops->close || !ops->send)
1160 if (!supported_protocols)
1163 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1169 if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1170 pr_err("Too many proprietary commands: %zd\n",
1172 ops->prop_ops = NULL;
1173 ops->n_prop_ops = 0;
1176 ndev->tx_headroom = tx_headroom;
1177 ndev->tx_tailroom = tx_tailroom;
1178 init_completion(&ndev->req_completion);
1180 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1181 supported_protocols,
1182 tx_headroom + NCI_DATA_HDR_SIZE,
1187 ndev->hci_dev = nci_hci_allocate(ndev);
1191 nfc_set_drvdata(ndev->nfc_dev, ndev);
1196 nfc_free_device(ndev->nfc_dev);
1201 EXPORT_SYMBOL(nci_allocate_device);
1204 * nci_free_device - deallocate nci device
1206 * @ndev: The nci device to deallocate
1208 void nci_free_device(struct nci_dev *ndev)
1210 nfc_free_device(ndev->nfc_dev);
1211 nci_hci_deallocate(ndev);
1214 EXPORT_SYMBOL(nci_free_device);
1217 * nci_register_device - register a nci device in the nfc subsystem
1219 * @dev: The nci device to register
1221 int nci_register_device(struct nci_dev *ndev)
1224 struct device *dev = &ndev->nfc_dev->dev;
1229 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1230 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1231 ndev->cmd_wq = create_singlethread_workqueue(name);
1232 if (!ndev->cmd_wq) {
1237 INIT_WORK(&ndev->rx_work, nci_rx_work);
1238 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1239 ndev->rx_wq = create_singlethread_workqueue(name);
1242 goto destroy_cmd_wq_exit;
1245 INIT_WORK(&ndev->tx_work, nci_tx_work);
1246 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1247 ndev->tx_wq = create_singlethread_workqueue(name);
1250 goto destroy_rx_wq_exit;
1253 skb_queue_head_init(&ndev->cmd_q);
1254 skb_queue_head_init(&ndev->rx_q);
1255 skb_queue_head_init(&ndev->tx_q);
1257 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
1258 (unsigned long) ndev);
1259 setup_timer(&ndev->data_timer, nci_data_timer,
1260 (unsigned long) ndev);
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_rx_wq_exit;
1272 destroy_workqueue(ndev->rx_wq);
1274 destroy_cmd_wq_exit:
1275 destroy_workqueue(ndev->cmd_wq);
1280 EXPORT_SYMBOL(nci_register_device);
1283 * nci_unregister_device - unregister a nci device in the nfc subsystem
1285 * @dev: The nci device to unregister
1287 void nci_unregister_device(struct nci_dev *ndev)
1289 struct nci_conn_info *conn_info, *n;
1291 /* This set_bit is not protected with specialized barrier,
1292 * However, it is fine because the mutex_lock(&ndev->req_lock);
1293 * in nci_close_device() will help to emit one.
1295 set_bit(NCI_UNREG, &ndev->flags);
1297 nci_close_device(ndev);
1299 destroy_workqueue(ndev->cmd_wq);
1300 destroy_workqueue(ndev->rx_wq);
1301 destroy_workqueue(ndev->tx_wq);
1303 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1304 list_del(&conn_info->list);
1305 /* conn_info is allocated with devm_kzalloc */
1308 nfc_unregister_device(ndev->nfc_dev);
1310 EXPORT_SYMBOL(nci_unregister_device);
1313 * nci_recv_frame - receive frame from NCI drivers
1315 * @ndev: The nci device
1316 * @skb: The sk_buff to receive
1318 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1320 pr_debug("len %d\n", skb->len);
1322 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1323 !test_bit(NCI_INIT, &ndev->flags))) {
1328 /* Queue frame for rx worker thread */
1329 skb_queue_tail(&ndev->rx_q, skb);
1330 queue_work(ndev->rx_wq, &ndev->rx_work);
1334 EXPORT_SYMBOL(nci_recv_frame);
1336 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1338 pr_debug("len %d\n", skb->len);
1345 /* Get rid of skb owner, prior to sending to the driver. */
1348 /* Send copy to sniffer */
1349 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1350 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1352 return ndev->ops->send(ndev, skb);
1354 EXPORT_SYMBOL(nci_send_frame);
1356 /* Send NCI command */
1357 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1359 struct nci_ctrl_hdr *hdr;
1360 struct sk_buff *skb;
1362 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1364 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1366 pr_err("no memory for command\n");
1370 hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
1371 hdr->gid = nci_opcode_gid(opcode);
1372 hdr->oid = nci_opcode_oid(opcode);
1375 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1376 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1379 skb_put_data(skb, payload, plen);
1381 skb_queue_tail(&ndev->cmd_q, skb);
1382 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1386 EXPORT_SYMBOL(nci_send_cmd);
1388 /* Proprietary commands API */
1389 static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
1394 struct nci_driver_ops *op;
1399 for (i = 0; i < n_ops; i++) {
1401 if (op->opcode == opcode)
1408 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1409 struct sk_buff *skb, struct nci_driver_ops *ops,
1412 struct nci_driver_ops *op;
1414 op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1415 if (!op || !op->rsp)
1418 return op->rsp(ndev, skb);
1421 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1422 struct sk_buff *skb, struct nci_driver_ops *ops,
1425 struct nci_driver_ops *op;
1427 op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1428 if (!op || !op->ntf)
1431 return op->ntf(ndev, skb);
1434 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1435 struct sk_buff *skb)
1437 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1438 ndev->ops->n_prop_ops);
1441 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1442 struct sk_buff *skb)
1444 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1445 ndev->ops->n_prop_ops);
1448 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1449 struct sk_buff *skb)
1451 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1452 ndev->ops->n_core_ops);
1455 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1456 struct sk_buff *skb)
1458 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1459 ndev->ops->n_core_ops);
1462 /* ---- NCI TX Data worker thread ---- */
1464 static void nci_tx_work(struct work_struct *work)
1466 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1467 struct nci_conn_info *conn_info;
1468 struct sk_buff *skb;
1470 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1474 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1476 /* Send queued tx data */
1477 while (atomic_read(&conn_info->credits_cnt)) {
1478 skb = skb_dequeue(&ndev->tx_q);
1482 /* Check if data flow control is used */
1483 if (atomic_read(&conn_info->credits_cnt) !=
1484 NCI_DATA_FLOW_CONTROL_NOT_USED)
1485 atomic_dec(&conn_info->credits_cnt);
1487 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1489 nci_conn_id(skb->data),
1490 nci_plen(skb->data));
1492 nci_send_frame(ndev, skb);
1494 mod_timer(&ndev->data_timer,
1495 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1499 /* ----- NCI RX worker thread (data & control) ----- */
1501 static void nci_rx_work(struct work_struct *work)
1503 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1504 struct sk_buff *skb;
1506 while ((skb = skb_dequeue(&ndev->rx_q))) {
1508 /* Send copy to sniffer */
1509 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1510 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1513 switch (nci_mt(skb->data)) {
1514 case NCI_MT_RSP_PKT:
1515 nci_rsp_packet(ndev, skb);
1518 case NCI_MT_NTF_PKT:
1519 nci_ntf_packet(ndev, skb);
1522 case NCI_MT_DATA_PKT:
1523 nci_rx_data_packet(ndev, skb);
1527 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1533 /* check if a data exchange timout has occurred */
1534 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1535 /* complete the data exchange transaction, if exists */
1536 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1537 nci_data_exchange_complete(ndev, NULL,
1541 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1545 /* ----- NCI TX CMD worker thread ----- */
1547 static void nci_cmd_work(struct work_struct *work)
1549 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1550 struct sk_buff *skb;
1552 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1554 /* Send queued command */
1555 if (atomic_read(&ndev->cmd_cnt)) {
1556 skb = skb_dequeue(&ndev->cmd_q);
1560 atomic_dec(&ndev->cmd_cnt);
1562 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1564 nci_opcode_gid(nci_opcode(skb->data)),
1565 nci_opcode_oid(nci_opcode(skb->data)),
1566 nci_plen(skb->data));
1568 nci_send_frame(ndev, skb);
1570 mod_timer(&ndev->cmd_timer,
1571 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1575 MODULE_LICENSE("GPL");