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;
86 EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params);
88 /* ---- NCI requests ---- */
90 void nci_req_complete(struct nci_dev *ndev, int result)
92 if (ndev->req_status == NCI_REQ_PEND) {
93 ndev->req_result = result;
94 ndev->req_status = NCI_REQ_DONE;
95 complete(&ndev->req_completion);
98 EXPORT_SYMBOL(nci_req_complete);
100 static void nci_req_cancel(struct nci_dev *ndev, int err)
102 if (ndev->req_status == NCI_REQ_PEND) {
103 ndev->req_result = err;
104 ndev->req_status = NCI_REQ_CANCELED;
105 complete(&ndev->req_completion);
109 /* Execute request and wait for completion. */
110 static int __nci_request(struct nci_dev *ndev,
111 void (*req)(struct nci_dev *ndev, unsigned long opt),
112 unsigned long opt, __u32 timeout)
117 ndev->req_status = NCI_REQ_PEND;
119 reinit_completion(&ndev->req_completion);
122 wait_for_completion_interruptible_timeout(&ndev->req_completion,
125 pr_debug("wait_for_completion return %ld\n", completion_rc);
127 if (completion_rc > 0) {
128 switch (ndev->req_status) {
130 rc = nci_to_errno(ndev->req_result);
133 case NCI_REQ_CANCELED:
134 rc = -ndev->req_result;
142 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
145 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
148 ndev->req_status = ndev->req_result = 0;
153 inline int nci_request(struct nci_dev *ndev,
154 void (*req)(struct nci_dev *ndev,
156 unsigned long opt, __u32 timeout)
160 /* Serialize all requests */
161 mutex_lock(&ndev->req_lock);
162 /* check the state after obtaing the lock against any races
163 * from nci_close_device when the device gets removed.
165 if (test_bit(NCI_UP, &ndev->flags))
166 rc = __nci_request(ndev, req, opt, timeout);
169 mutex_unlock(&ndev->req_lock);
174 static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
176 struct nci_core_reset_cmd cmd;
178 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
179 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
182 static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
184 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
187 static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
189 struct nci_rf_disc_map_cmd cmd;
190 struct disc_map_config *cfg = cmd.mapping_configs;
191 __u8 *num = &cmd.num_mapping_configs;
194 /* set rf mapping configurations */
197 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
198 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
199 if (ndev->supported_rf_interfaces[i] ==
200 NCI_RF_INTERFACE_ISO_DEP) {
201 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
202 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
203 NCI_DISC_MAP_MODE_LISTEN;
204 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
206 } else if (ndev->supported_rf_interfaces[i] ==
207 NCI_RF_INTERFACE_NFC_DEP) {
208 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
209 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
210 NCI_DISC_MAP_MODE_LISTEN;
211 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
215 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
219 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
220 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
223 struct nci_set_config_param {
229 static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
231 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
232 struct nci_core_set_config_cmd cmd;
234 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
237 cmd.param.id = param->id;
238 cmd.param.len = param->len;
239 memcpy(cmd.param.val, param->val, param->len);
241 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
244 struct nci_rf_discover_param {
249 static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
251 struct nci_rf_discover_param *param =
252 (struct nci_rf_discover_param *)opt;
253 struct nci_rf_disc_cmd cmd;
255 cmd.num_disc_configs = 0;
257 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
258 (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
259 param->im_protocols & NFC_PROTO_MIFARE_MASK ||
260 param->im_protocols & NFC_PROTO_ISO14443_MASK ||
261 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
262 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
263 NCI_NFC_A_PASSIVE_POLL_MODE;
264 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
265 cmd.num_disc_configs++;
268 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
269 (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
270 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
271 NCI_NFC_B_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_FELICA_MASK ||
278 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
279 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
280 NCI_NFC_F_PASSIVE_POLL_MODE;
281 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
282 cmd.num_disc_configs++;
285 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
286 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
287 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
288 NCI_NFC_V_PASSIVE_POLL_MODE;
289 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
290 cmd.num_disc_configs++;
293 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
294 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
295 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
296 NCI_NFC_A_PASSIVE_LISTEN_MODE;
297 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
298 cmd.num_disc_configs++;
299 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
300 NCI_NFC_F_PASSIVE_LISTEN_MODE;
301 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
302 cmd.num_disc_configs++;
305 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
306 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
310 struct nci_rf_discover_select_param {
311 __u8 rf_discovery_id;
315 static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
317 struct nci_rf_discover_select_param *param =
318 (struct nci_rf_discover_select_param *)opt;
319 struct nci_rf_discover_select_cmd cmd;
321 cmd.rf_discovery_id = param->rf_discovery_id;
322 cmd.rf_protocol = param->rf_protocol;
324 switch (cmd.rf_protocol) {
325 case NCI_RF_PROTOCOL_ISO_DEP:
326 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
329 case NCI_RF_PROTOCOL_NFC_DEP:
330 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
334 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
338 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
339 sizeof(struct nci_rf_discover_select_cmd), &cmd);
342 static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
344 struct nci_rf_deactivate_cmd cmd;
348 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
349 sizeof(struct nci_rf_deactivate_cmd), &cmd);
352 struct nci_cmd_param {
358 static void nci_generic_req(struct nci_dev *ndev, unsigned long opt)
360 struct nci_cmd_param *param =
361 (struct nci_cmd_param *)opt;
363 nci_send_cmd(ndev, param->opcode, param->len, param->payload);
366 int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, __u8 *payload)
368 struct nci_cmd_param param;
370 param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
372 param.payload = payload;
374 return __nci_request(ndev, nci_generic_req, (unsigned long)¶m,
375 msecs_to_jiffies(NCI_CMD_TIMEOUT));
377 EXPORT_SYMBOL(nci_prop_cmd);
379 int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len, __u8 *payload)
381 struct nci_cmd_param param;
383 param.opcode = opcode;
385 param.payload = payload;
387 return __nci_request(ndev, nci_generic_req, (unsigned long)¶m,
388 msecs_to_jiffies(NCI_CMD_TIMEOUT));
390 EXPORT_SYMBOL(nci_core_cmd);
392 int nci_core_reset(struct nci_dev *ndev)
394 return __nci_request(ndev, nci_reset_req, 0,
395 msecs_to_jiffies(NCI_RESET_TIMEOUT));
397 EXPORT_SYMBOL(nci_core_reset);
399 int nci_core_init(struct nci_dev *ndev)
401 return __nci_request(ndev, nci_init_req, 0,
402 msecs_to_jiffies(NCI_INIT_TIMEOUT));
404 EXPORT_SYMBOL(nci_core_init);
406 struct nci_loopback_data {
408 struct sk_buff *data;
411 static void nci_send_data_req(struct nci_dev *ndev, unsigned long opt)
413 struct nci_loopback_data *data = (struct nci_loopback_data *)opt;
415 nci_send_data(ndev, data->conn_id, data->data);
418 static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
420 struct nci_dev *ndev = (struct nci_dev *)context;
421 struct nci_conn_info *conn_info;
423 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
425 nci_req_complete(ndev, NCI_STATUS_REJECTED);
429 conn_info->rx_skb = skb;
431 nci_req_complete(ndev, NCI_STATUS_OK);
434 int nci_nfcc_loopback(struct nci_dev *ndev, void *data, size_t data_len,
435 struct sk_buff **resp)
438 struct nci_loopback_data loopback_data;
439 struct nci_conn_info *conn_info;
441 int conn_id = nci_get_conn_info_by_dest_type_params(ndev,
442 NCI_DESTINATION_NFCC_LOOPBACK, NULL);
445 r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCC_LOOPBACK,
447 if (r != NCI_STATUS_OK)
450 conn_id = nci_get_conn_info_by_dest_type_params(ndev,
451 NCI_DESTINATION_NFCC_LOOPBACK,
455 conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
459 /* store cb and context to be used on receiving data */
460 conn_info->data_exchange_cb = nci_nfcc_loopback_cb;
461 conn_info->data_exchange_cb_context = ndev;
463 skb = nci_skb_alloc(ndev, NCI_DATA_HDR_SIZE + data_len, GFP_KERNEL);
467 skb_reserve(skb, NCI_DATA_HDR_SIZE);
468 memcpy(skb_put(skb, data_len), data, data_len);
470 loopback_data.conn_id = conn_id;
471 loopback_data.data = skb;
473 ndev->cur_conn_id = conn_id;
474 r = nci_request(ndev, nci_send_data_req, (unsigned long)&loopback_data,
475 msecs_to_jiffies(NCI_DATA_TIMEOUT));
476 if (r == NCI_STATUS_OK && resp)
477 *resp = conn_info->rx_skb;
481 EXPORT_SYMBOL(nci_nfcc_loopback);
483 static int nci_open_device(struct nci_dev *ndev)
487 mutex_lock(&ndev->req_lock);
489 if (test_bit(NCI_UNREG, &ndev->flags)) {
494 if (test_bit(NCI_UP, &ndev->flags)) {
499 if (ndev->ops->open(ndev)) {
504 atomic_set(&ndev->cmd_cnt, 1);
506 set_bit(NCI_INIT, &ndev->flags);
509 rc = ndev->ops->init(ndev);
512 rc = __nci_request(ndev, nci_reset_req, 0,
513 msecs_to_jiffies(NCI_RESET_TIMEOUT));
516 if (!rc && ndev->ops->setup) {
517 rc = ndev->ops->setup(ndev);
521 rc = __nci_request(ndev, nci_init_req, 0,
522 msecs_to_jiffies(NCI_INIT_TIMEOUT));
525 if (!rc && ndev->ops->post_setup)
526 rc = ndev->ops->post_setup(ndev);
529 rc = __nci_request(ndev, nci_init_complete_req, 0,
530 msecs_to_jiffies(NCI_INIT_TIMEOUT));
533 clear_bit(NCI_INIT, &ndev->flags);
536 set_bit(NCI_UP, &ndev->flags);
537 nci_clear_target_list(ndev);
538 atomic_set(&ndev->state, NCI_IDLE);
540 /* Init failed, cleanup */
541 skb_queue_purge(&ndev->cmd_q);
542 skb_queue_purge(&ndev->rx_q);
543 skb_queue_purge(&ndev->tx_q);
545 ndev->ops->close(ndev);
550 mutex_unlock(&ndev->req_lock);
554 static int nci_close_device(struct nci_dev *ndev)
556 nci_req_cancel(ndev, ENODEV);
558 /* This mutex needs to be held as a barrier for
559 * caller nci_unregister_device
561 mutex_lock(&ndev->req_lock);
563 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
564 del_timer_sync(&ndev->cmd_timer);
565 del_timer_sync(&ndev->data_timer);
566 mutex_unlock(&ndev->req_lock);
570 /* Drop RX and TX queues */
571 skb_queue_purge(&ndev->rx_q);
572 skb_queue_purge(&ndev->tx_q);
574 /* Flush RX and TX wq */
575 flush_workqueue(ndev->rx_wq);
576 flush_workqueue(ndev->tx_wq);
579 skb_queue_purge(&ndev->cmd_q);
580 atomic_set(&ndev->cmd_cnt, 1);
582 set_bit(NCI_INIT, &ndev->flags);
583 __nci_request(ndev, nci_reset_req, 0,
584 msecs_to_jiffies(NCI_RESET_TIMEOUT));
586 /* After this point our queues are empty
587 * and no works are scheduled.
589 ndev->ops->close(ndev);
591 clear_bit(NCI_INIT, &ndev->flags);
593 del_timer_sync(&ndev->cmd_timer);
596 flush_workqueue(ndev->cmd_wq);
598 /* Clear flags except NCI_UNREG */
599 ndev->flags &= BIT(NCI_UNREG);
601 mutex_unlock(&ndev->req_lock);
606 /* NCI command timer function */
607 static void nci_cmd_timer(unsigned long arg)
609 struct nci_dev *ndev = (void *) arg;
611 atomic_set(&ndev->cmd_cnt, 1);
612 queue_work(ndev->cmd_wq, &ndev->cmd_work);
615 /* NCI data exchange timer function */
616 static void nci_data_timer(unsigned long arg)
618 struct nci_dev *ndev = (void *) arg;
620 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
621 queue_work(ndev->rx_wq, &ndev->rx_work);
624 static int nci_dev_up(struct nfc_dev *nfc_dev)
626 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
628 return nci_open_device(ndev);
631 static int nci_dev_down(struct nfc_dev *nfc_dev)
633 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
635 return nci_close_device(ndev);
638 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
640 struct nci_set_config_param param;
649 return __nci_request(ndev, nci_set_config_req, (unsigned long)¶m,
650 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
652 EXPORT_SYMBOL(nci_set_config);
654 static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
656 struct nci_nfcee_discover_cmd cmd;
659 cmd.discovery_action = action;
661 nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
664 int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
666 return __nci_request(ndev, nci_nfcee_discover_req, action,
667 msecs_to_jiffies(NCI_CMD_TIMEOUT));
669 EXPORT_SYMBOL(nci_nfcee_discover);
671 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
673 struct nci_nfcee_mode_set_cmd *cmd =
674 (struct nci_nfcee_mode_set_cmd *)opt;
676 nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
677 sizeof(struct nci_nfcee_mode_set_cmd), cmd);
680 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
682 struct nci_nfcee_mode_set_cmd cmd;
684 cmd.nfcee_id = nfcee_id;
685 cmd.nfcee_mode = nfcee_mode;
687 return __nci_request(ndev, nci_nfcee_mode_set_req,
689 msecs_to_jiffies(NCI_CMD_TIMEOUT));
691 EXPORT_SYMBOL(nci_nfcee_mode_set);
693 static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
695 struct core_conn_create_data *data =
696 (struct core_conn_create_data *)opt;
698 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
701 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
702 u8 number_destination_params,
704 struct core_conn_create_dest_spec_params *params)
707 struct nci_core_conn_create_cmd *cmd;
708 struct core_conn_create_data data;
710 data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
711 cmd = kzalloc(data.length, GFP_KERNEL);
715 cmd->destination_type = destination_type;
716 cmd->number_destination_params = number_destination_params;
721 memcpy(cmd->params, params, params_len);
722 if (params->length > 0)
723 memcpy(&ndev->cur_params,
724 ¶ms->value[DEST_SPEC_PARAMS_ID_INDEX],
725 sizeof(struct dest_spec_params));
727 ndev->cur_params.id = 0;
729 ndev->cur_params.id = 0;
731 ndev->cur_dest_type = destination_type;
733 r = __nci_request(ndev, nci_core_conn_create_req, (unsigned long)&data,
734 msecs_to_jiffies(NCI_CMD_TIMEOUT));
738 EXPORT_SYMBOL(nci_core_conn_create);
740 static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
744 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
747 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
749 ndev->cur_conn_id = conn_id;
750 return __nci_request(ndev, nci_core_conn_close_req, conn_id,
751 msecs_to_jiffies(NCI_CMD_TIMEOUT));
753 EXPORT_SYMBOL(nci_core_conn_close);
755 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
757 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
758 struct nci_set_config_param param;
761 param.val = nfc_get_local_general_bytes(nfc_dev, ¶m.len);
762 if ((param.val == NULL) || (param.len == 0))
765 if (param.len > NFC_MAX_GT_LEN)
768 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
770 rc = nci_request(ndev, nci_set_config_req, (unsigned long)¶m,
771 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
775 param.id = NCI_LN_ATR_RES_GEN_BYTES;
777 return nci_request(ndev, nci_set_config_req, (unsigned long)¶m,
778 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
781 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
783 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
787 val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
789 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
793 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
795 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
799 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
801 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
804 static int nci_start_poll(struct nfc_dev *nfc_dev,
805 __u32 im_protocols, __u32 tm_protocols)
807 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
808 struct nci_rf_discover_param param;
811 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
812 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
813 pr_err("unable to start poll, since poll is already active\n");
817 if (ndev->target_active_prot) {
818 pr_err("there is an active target\n");
822 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
823 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
824 pr_debug("target active or w4 select, implicitly deactivate\n");
826 rc = nci_request(ndev, nci_rf_deactivate_req,
827 NCI_DEACTIVATE_TYPE_IDLE_MODE,
828 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
833 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
834 rc = nci_set_local_general_bytes(nfc_dev);
836 pr_err("failed to set local general bytes\n");
841 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
842 rc = nci_set_listen_parameters(nfc_dev);
844 pr_err("failed to set listen parameters\n");
847 param.im_protocols = im_protocols;
848 param.tm_protocols = tm_protocols;
849 rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)¶m,
850 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
853 ndev->poll_prots = im_protocols;
858 static void nci_stop_poll(struct nfc_dev *nfc_dev)
860 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
862 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
863 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
864 pr_err("unable to stop poll, since poll is not active\n");
868 nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
869 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
872 static int nci_activate_target(struct nfc_dev *nfc_dev,
873 struct nfc_target *target, __u32 protocol)
875 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
876 struct nci_rf_discover_select_param param;
877 struct nfc_target *nci_target = NULL;
881 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
883 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
884 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
885 pr_err("there is no available target to activate\n");
889 if (ndev->target_active_prot) {
890 pr_err("there is already an active target\n");
894 for (i = 0; i < ndev->n_targets; i++) {
895 if (ndev->targets[i].idx == target->idx) {
896 nci_target = &ndev->targets[i];
902 pr_err("unable to find the selected target\n");
906 if (!(nci_target->supported_protocols & (1 << protocol))) {
907 pr_err("target does not support the requested protocol 0x%x\n",
912 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
913 param.rf_discovery_id = nci_target->logical_idx;
915 if (protocol == NFC_PROTO_JEWEL)
916 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
917 else if (protocol == NFC_PROTO_MIFARE)
918 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
919 else if (protocol == NFC_PROTO_FELICA)
920 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
921 else if (protocol == NFC_PROTO_ISO14443 ||
922 protocol == NFC_PROTO_ISO14443_B)
923 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
925 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
927 rc = nci_request(ndev, nci_rf_discover_select_req,
928 (unsigned long)¶m,
929 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
933 ndev->target_active_prot = protocol;
938 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
939 struct nfc_target *target,
942 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
943 u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
947 if (!ndev->target_active_prot) {
948 pr_err("unable to deactivate target, no active target\n");
952 ndev->target_active_prot = 0;
955 case NFC_TARGET_MODE_SLEEP:
956 nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
960 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
961 nci_request(ndev, nci_rf_deactivate_req, nci_mode,
962 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
966 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
967 __u8 comm_mode, __u8 *gb, size_t gb_len)
969 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
972 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
974 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
978 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
979 ndev->remote_gb_len);
981 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
987 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
989 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
994 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
995 nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
997 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
998 atomic_read(&ndev->state) == NCI_DISCOVERY) {
999 nci_request(ndev, nci_rf_deactivate_req, 0,
1000 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
1003 rc = nfc_tm_deactivated(nfc_dev);
1005 pr_err("error when signaling tm deactivation\n");
1012 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1013 struct sk_buff *skb,
1014 data_exchange_cb_t cb, void *cb_context)
1016 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1018 struct nci_conn_info *conn_info;
1020 conn_info = ndev->rf_conn_info;
1024 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
1026 if (!ndev->target_active_prot) {
1027 pr_err("unable to exchange data, no active target\n");
1031 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1034 /* store cb and context to be used on receiving data */
1035 conn_info->data_exchange_cb = cb;
1036 conn_info->data_exchange_cb_context = cb_context;
1038 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1040 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1045 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1047 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1050 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1052 pr_err("unable to send data\n");
1057 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1059 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1061 if (ndev->ops->enable_se)
1062 return ndev->ops->enable_se(ndev, se_idx);
1067 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1069 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1071 if (ndev->ops->disable_se)
1072 return ndev->ops->disable_se(ndev, se_idx);
1077 static int nci_discover_se(struct nfc_dev *nfc_dev)
1080 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1082 if (ndev->ops->discover_se) {
1083 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1084 if (r != NCI_STATUS_OK)
1087 return ndev->ops->discover_se(ndev);
1093 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1094 u8 *apdu, size_t apdu_length,
1095 se_io_cb_t cb, void *cb_context)
1097 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1099 if (ndev->ops->se_io)
1100 return ndev->ops->se_io(ndev, se_idx, apdu,
1101 apdu_length, cb, cb_context);
1106 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1108 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1110 if (!ndev->ops->fw_download)
1113 return ndev->ops->fw_download(ndev, firmware_name);
1116 static struct nfc_ops nci_nfc_ops = {
1117 .dev_up = nci_dev_up,
1118 .dev_down = nci_dev_down,
1119 .start_poll = nci_start_poll,
1120 .stop_poll = nci_stop_poll,
1121 .dep_link_up = nci_dep_link_up,
1122 .dep_link_down = nci_dep_link_down,
1123 .activate_target = nci_activate_target,
1124 .deactivate_target = nci_deactivate_target,
1125 .im_transceive = nci_transceive,
1126 .tm_send = nci_tm_send,
1127 .enable_se = nci_enable_se,
1128 .disable_se = nci_disable_se,
1129 .discover_se = nci_discover_se,
1131 .fw_download = nci_fw_download,
1134 /* ---- Interface to NCI drivers ---- */
1136 * nci_allocate_device - allocate a new nci device
1138 * @ops: device operations
1139 * @supported_protocols: NFC protocols supported by the device
1141 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
1142 __u32 supported_protocols,
1143 int tx_headroom, int tx_tailroom)
1145 struct nci_dev *ndev;
1147 pr_debug("supported_protocols 0x%x\n", supported_protocols);
1149 if (!ops->open || !ops->close || !ops->send)
1152 if (!supported_protocols)
1155 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1161 if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1162 pr_err("Too many proprietary commands: %zd\n",
1164 ops->prop_ops = NULL;
1165 ops->n_prop_ops = 0;
1168 ndev->tx_headroom = tx_headroom;
1169 ndev->tx_tailroom = tx_tailroom;
1170 init_completion(&ndev->req_completion);
1172 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1173 supported_protocols,
1174 tx_headroom + NCI_DATA_HDR_SIZE,
1179 ndev->hci_dev = nci_hci_allocate(ndev);
1183 nfc_set_drvdata(ndev->nfc_dev, ndev);
1188 nfc_free_device(ndev->nfc_dev);
1193 EXPORT_SYMBOL(nci_allocate_device);
1196 * nci_free_device - deallocate nci device
1198 * @ndev: The nci device to deallocate
1200 void nci_free_device(struct nci_dev *ndev)
1202 nfc_free_device(ndev->nfc_dev);
1203 nci_hci_deallocate(ndev);
1206 EXPORT_SYMBOL(nci_free_device);
1209 * nci_register_device - register a nci device in the nfc subsystem
1211 * @dev: The nci device to register
1213 int nci_register_device(struct nci_dev *ndev)
1216 struct device *dev = &ndev->nfc_dev->dev;
1221 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1222 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1223 ndev->cmd_wq = create_singlethread_workqueue(name);
1224 if (!ndev->cmd_wq) {
1229 INIT_WORK(&ndev->rx_work, nci_rx_work);
1230 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1231 ndev->rx_wq = create_singlethread_workqueue(name);
1234 goto destroy_cmd_wq_exit;
1237 INIT_WORK(&ndev->tx_work, nci_tx_work);
1238 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1239 ndev->tx_wq = create_singlethread_workqueue(name);
1242 goto destroy_rx_wq_exit;
1245 skb_queue_head_init(&ndev->cmd_q);
1246 skb_queue_head_init(&ndev->rx_q);
1247 skb_queue_head_init(&ndev->tx_q);
1249 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
1250 (unsigned long) ndev);
1251 setup_timer(&ndev->data_timer, nci_data_timer,
1252 (unsigned long) ndev);
1254 mutex_init(&ndev->req_lock);
1255 INIT_LIST_HEAD(&ndev->conn_info_list);
1257 rc = nfc_register_device(ndev->nfc_dev);
1259 goto destroy_rx_wq_exit;
1264 destroy_workqueue(ndev->rx_wq);
1266 destroy_cmd_wq_exit:
1267 destroy_workqueue(ndev->cmd_wq);
1272 EXPORT_SYMBOL(nci_register_device);
1275 * nci_unregister_device - unregister a nci device in the nfc subsystem
1277 * @dev: The nci device to unregister
1279 void nci_unregister_device(struct nci_dev *ndev)
1281 struct nci_conn_info *conn_info, *n;
1283 /* This set_bit is not protected with specialized barrier,
1284 * However, it is fine because the mutex_lock(&ndev->req_lock);
1285 * in nci_close_device() will help to emit one.
1287 set_bit(NCI_UNREG, &ndev->flags);
1289 nci_close_device(ndev);
1291 destroy_workqueue(ndev->cmd_wq);
1292 destroy_workqueue(ndev->rx_wq);
1293 destroy_workqueue(ndev->tx_wq);
1295 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1296 list_del(&conn_info->list);
1297 /* conn_info is allocated with devm_kzalloc */
1300 nfc_unregister_device(ndev->nfc_dev);
1302 EXPORT_SYMBOL(nci_unregister_device);
1305 * nci_recv_frame - receive frame from NCI drivers
1307 * @ndev: The nci device
1308 * @skb: The sk_buff to receive
1310 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1312 pr_debug("len %d\n", skb->len);
1314 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1315 !test_bit(NCI_INIT, &ndev->flags))) {
1320 /* Queue frame for rx worker thread */
1321 skb_queue_tail(&ndev->rx_q, skb);
1322 queue_work(ndev->rx_wq, &ndev->rx_work);
1326 EXPORT_SYMBOL(nci_recv_frame);
1328 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1330 pr_debug("len %d\n", skb->len);
1337 /* Get rid of skb owner, prior to sending to the driver. */
1340 /* Send copy to sniffer */
1341 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1342 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1344 return ndev->ops->send(ndev, skb);
1346 EXPORT_SYMBOL(nci_send_frame);
1348 /* Send NCI command */
1349 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1351 struct nci_ctrl_hdr *hdr;
1352 struct sk_buff *skb;
1354 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1356 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1358 pr_err("no memory for command\n");
1362 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
1363 hdr->gid = nci_opcode_gid(opcode);
1364 hdr->oid = nci_opcode_oid(opcode);
1367 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1368 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1371 memcpy(skb_put(skb, plen), payload, plen);
1373 skb_queue_tail(&ndev->cmd_q, skb);
1374 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1378 EXPORT_SYMBOL(nci_send_cmd);
1380 /* Proprietary commands API */
1381 static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
1386 struct nci_driver_ops *op;
1391 for (i = 0; i < n_ops; i++) {
1393 if (op->opcode == opcode)
1400 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1401 struct sk_buff *skb, struct nci_driver_ops *ops,
1404 struct nci_driver_ops *op;
1406 op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1407 if (!op || !op->rsp)
1410 return op->rsp(ndev, skb);
1413 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1414 struct sk_buff *skb, struct nci_driver_ops *ops,
1417 struct nci_driver_ops *op;
1419 op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1420 if (!op || !op->ntf)
1423 return op->ntf(ndev, skb);
1426 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1427 struct sk_buff *skb)
1429 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1430 ndev->ops->n_prop_ops);
1433 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1434 struct sk_buff *skb)
1436 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1437 ndev->ops->n_prop_ops);
1440 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1441 struct sk_buff *skb)
1443 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1444 ndev->ops->n_core_ops);
1447 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1448 struct sk_buff *skb)
1450 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1451 ndev->ops->n_core_ops);
1454 /* ---- NCI TX Data worker thread ---- */
1456 static void nci_tx_work(struct work_struct *work)
1458 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1459 struct nci_conn_info *conn_info;
1460 struct sk_buff *skb;
1462 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1466 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1468 /* Send queued tx data */
1469 while (atomic_read(&conn_info->credits_cnt)) {
1470 skb = skb_dequeue(&ndev->tx_q);
1474 /* Check if data flow control is used */
1475 if (atomic_read(&conn_info->credits_cnt) !=
1476 NCI_DATA_FLOW_CONTROL_NOT_USED)
1477 atomic_dec(&conn_info->credits_cnt);
1479 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1481 nci_conn_id(skb->data),
1482 nci_plen(skb->data));
1484 nci_send_frame(ndev, skb);
1486 mod_timer(&ndev->data_timer,
1487 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1491 /* ----- NCI RX worker thread (data & control) ----- */
1493 static void nci_rx_work(struct work_struct *work)
1495 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1496 struct sk_buff *skb;
1498 while ((skb = skb_dequeue(&ndev->rx_q))) {
1500 /* Send copy to sniffer */
1501 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1502 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1505 switch (nci_mt(skb->data)) {
1506 case NCI_MT_RSP_PKT:
1507 nci_rsp_packet(ndev, skb);
1510 case NCI_MT_NTF_PKT:
1511 nci_ntf_packet(ndev, skb);
1514 case NCI_MT_DATA_PKT:
1515 nci_rx_data_packet(ndev, skb);
1519 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1525 /* check if a data exchange timout has occurred */
1526 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1527 /* complete the data exchange transaction, if exists */
1528 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1529 nci_data_exchange_complete(ndev, NULL,
1533 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1537 /* ----- NCI TX CMD worker thread ----- */
1539 static void nci_cmd_work(struct work_struct *work)
1541 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1542 struct sk_buff *skb;
1544 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1546 /* Send queued command */
1547 if (atomic_read(&ndev->cmd_cnt)) {
1548 skb = skb_dequeue(&ndev->cmd_q);
1552 atomic_dec(&ndev->cmd_cnt);
1554 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1556 nci_opcode_gid(nci_opcode(skb->data)),
1557 nci_opcode_oid(nci_opcode(skb->data)),
1558 nci_plen(skb->data));
1560 nci_send_frame(ndev, skb);
1562 mod_timer(&ndev->cmd_timer,
1563 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1567 MODULE_LICENSE("GPL");