perf build: Get rid of LIB_INCLUDE variable
[linux/fpc-iii.git] / net / nfc / nci / core.c
blob9575a1892607c5dbf18f5bc04f531ed826dfe812
1 /*
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>
10 * Acknowledgements:
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/types.h>
32 #include <linux/workqueue.h>
33 #include <linux/completion.h>
34 #include <linux/export.h>
35 #include <linux/sched.h>
36 #include <linux/bitops.h>
37 #include <linux/skbuff.h>
39 #include "../nfc.h"
40 #include <net/nfc/nci.h>
41 #include <net/nfc/nci_core.h>
42 #include <linux/nfc.h>
44 struct core_conn_create_data {
45 int length;
46 struct nci_core_conn_create_cmd *cmd;
49 static void nci_cmd_work(struct work_struct *work);
50 static void nci_rx_work(struct work_struct *work);
51 static void nci_tx_work(struct work_struct *work);
53 struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
54 int conn_id)
56 struct nci_conn_info *conn_info;
58 list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
59 if (conn_info->conn_id == conn_id)
60 return conn_info;
63 return NULL;
66 /* ---- NCI requests ---- */
68 void nci_req_complete(struct nci_dev *ndev, int result)
70 if (ndev->req_status == NCI_REQ_PEND) {
71 ndev->req_result = result;
72 ndev->req_status = NCI_REQ_DONE;
73 complete(&ndev->req_completion);
77 static void nci_req_cancel(struct nci_dev *ndev, int err)
79 if (ndev->req_status == NCI_REQ_PEND) {
80 ndev->req_result = err;
81 ndev->req_status = NCI_REQ_CANCELED;
82 complete(&ndev->req_completion);
86 /* Execute request and wait for completion. */
87 static int __nci_request(struct nci_dev *ndev,
88 void (*req)(struct nci_dev *ndev, unsigned long opt),
89 unsigned long opt, __u32 timeout)
91 int rc = 0;
92 long completion_rc;
94 ndev->req_status = NCI_REQ_PEND;
96 reinit_completion(&ndev->req_completion);
97 req(ndev, opt);
98 completion_rc =
99 wait_for_completion_interruptible_timeout(&ndev->req_completion,
100 timeout);
102 pr_debug("wait_for_completion return %ld\n", completion_rc);
104 if (completion_rc > 0) {
105 switch (ndev->req_status) {
106 case NCI_REQ_DONE:
107 rc = nci_to_errno(ndev->req_result);
108 break;
110 case NCI_REQ_CANCELED:
111 rc = -ndev->req_result;
112 break;
114 default:
115 rc = -ETIMEDOUT;
116 break;
118 } else {
119 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
120 completion_rc);
122 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
125 ndev->req_status = ndev->req_result = 0;
127 return rc;
130 inline int nci_request(struct nci_dev *ndev,
131 void (*req)(struct nci_dev *ndev,
132 unsigned long opt),
133 unsigned long opt, __u32 timeout)
135 int rc;
137 if (!test_bit(NCI_UP, &ndev->flags))
138 return -ENETDOWN;
140 /* Serialize all requests */
141 mutex_lock(&ndev->req_lock);
142 rc = __nci_request(ndev, req, opt, timeout);
143 mutex_unlock(&ndev->req_lock);
145 return rc;
148 static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
150 struct nci_core_reset_cmd cmd;
152 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
153 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
156 static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
158 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
161 static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
163 struct nci_rf_disc_map_cmd cmd;
164 struct disc_map_config *cfg = cmd.mapping_configs;
165 __u8 *num = &cmd.num_mapping_configs;
166 int i;
168 /* set rf mapping configurations */
169 *num = 0;
171 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
172 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
173 if (ndev->supported_rf_interfaces[i] ==
174 NCI_RF_INTERFACE_ISO_DEP) {
175 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
176 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
177 NCI_DISC_MAP_MODE_LISTEN;
178 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
179 (*num)++;
180 } else if (ndev->supported_rf_interfaces[i] ==
181 NCI_RF_INTERFACE_NFC_DEP) {
182 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
183 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
184 NCI_DISC_MAP_MODE_LISTEN;
185 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
186 (*num)++;
189 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
190 break;
193 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
194 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
197 struct nci_set_config_param {
198 __u8 id;
199 size_t len;
200 __u8 *val;
203 static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
205 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
206 struct nci_core_set_config_cmd cmd;
208 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
210 cmd.num_params = 1;
211 cmd.param.id = param->id;
212 cmd.param.len = param->len;
213 memcpy(cmd.param.val, param->val, param->len);
215 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
218 struct nci_rf_discover_param {
219 __u32 im_protocols;
220 __u32 tm_protocols;
223 static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
225 struct nci_rf_discover_param *param =
226 (struct nci_rf_discover_param *)opt;
227 struct nci_rf_disc_cmd cmd;
229 cmd.num_disc_configs = 0;
231 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
232 (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
233 param->im_protocols & NFC_PROTO_MIFARE_MASK ||
234 param->im_protocols & NFC_PROTO_ISO14443_MASK ||
235 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
236 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
237 NCI_NFC_A_PASSIVE_POLL_MODE;
238 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
239 cmd.num_disc_configs++;
242 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
243 (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
244 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
245 NCI_NFC_B_PASSIVE_POLL_MODE;
246 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
247 cmd.num_disc_configs++;
250 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
251 (param->im_protocols & NFC_PROTO_FELICA_MASK ||
252 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
253 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
254 NCI_NFC_F_PASSIVE_POLL_MODE;
255 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
256 cmd.num_disc_configs++;
259 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
260 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
261 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
262 NCI_NFC_V_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 - 1) &&
268 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
269 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
270 NCI_NFC_A_PASSIVE_LISTEN_MODE;
271 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
272 cmd.num_disc_configs++;
273 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
274 NCI_NFC_F_PASSIVE_LISTEN_MODE;
275 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
276 cmd.num_disc_configs++;
279 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
280 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
281 &cmd);
284 struct nci_rf_discover_select_param {
285 __u8 rf_discovery_id;
286 __u8 rf_protocol;
289 static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
291 struct nci_rf_discover_select_param *param =
292 (struct nci_rf_discover_select_param *)opt;
293 struct nci_rf_discover_select_cmd cmd;
295 cmd.rf_discovery_id = param->rf_discovery_id;
296 cmd.rf_protocol = param->rf_protocol;
298 switch (cmd.rf_protocol) {
299 case NCI_RF_PROTOCOL_ISO_DEP:
300 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
301 break;
303 case NCI_RF_PROTOCOL_NFC_DEP:
304 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
305 break;
307 default:
308 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
309 break;
312 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
313 sizeof(struct nci_rf_discover_select_cmd), &cmd);
316 static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
318 struct nci_rf_deactivate_cmd cmd;
320 cmd.type = opt;
322 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
323 sizeof(struct nci_rf_deactivate_cmd), &cmd);
326 static int nci_open_device(struct nci_dev *ndev)
328 int rc = 0;
330 mutex_lock(&ndev->req_lock);
332 if (test_bit(NCI_UP, &ndev->flags)) {
333 rc = -EALREADY;
334 goto done;
337 if (ndev->ops->open(ndev)) {
338 rc = -EIO;
339 goto done;
342 atomic_set(&ndev->cmd_cnt, 1);
344 set_bit(NCI_INIT, &ndev->flags);
346 rc = __nci_request(ndev, nci_reset_req, 0,
347 msecs_to_jiffies(NCI_RESET_TIMEOUT));
349 if (ndev->ops->setup)
350 ndev->ops->setup(ndev);
352 if (!rc) {
353 rc = __nci_request(ndev, nci_init_req, 0,
354 msecs_to_jiffies(NCI_INIT_TIMEOUT));
357 if (!rc) {
358 rc = __nci_request(ndev, nci_init_complete_req, 0,
359 msecs_to_jiffies(NCI_INIT_TIMEOUT));
362 clear_bit(NCI_INIT, &ndev->flags);
364 if (!rc) {
365 set_bit(NCI_UP, &ndev->flags);
366 nci_clear_target_list(ndev);
367 atomic_set(&ndev->state, NCI_IDLE);
368 } else {
369 /* Init failed, cleanup */
370 skb_queue_purge(&ndev->cmd_q);
371 skb_queue_purge(&ndev->rx_q);
372 skb_queue_purge(&ndev->tx_q);
374 ndev->ops->close(ndev);
375 ndev->flags = 0;
378 done:
379 mutex_unlock(&ndev->req_lock);
380 return rc;
383 static int nci_close_device(struct nci_dev *ndev)
385 nci_req_cancel(ndev, ENODEV);
386 mutex_lock(&ndev->req_lock);
388 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
389 del_timer_sync(&ndev->cmd_timer);
390 del_timer_sync(&ndev->data_timer);
391 mutex_unlock(&ndev->req_lock);
392 return 0;
395 /* Drop RX and TX queues */
396 skb_queue_purge(&ndev->rx_q);
397 skb_queue_purge(&ndev->tx_q);
399 /* Flush RX and TX wq */
400 flush_workqueue(ndev->rx_wq);
401 flush_workqueue(ndev->tx_wq);
403 /* Reset device */
404 skb_queue_purge(&ndev->cmd_q);
405 atomic_set(&ndev->cmd_cnt, 1);
407 set_bit(NCI_INIT, &ndev->flags);
408 __nci_request(ndev, nci_reset_req, 0,
409 msecs_to_jiffies(NCI_RESET_TIMEOUT));
410 clear_bit(NCI_INIT, &ndev->flags);
412 del_timer_sync(&ndev->cmd_timer);
414 /* Flush cmd wq */
415 flush_workqueue(ndev->cmd_wq);
417 /* After this point our queues are empty
418 * and no works are scheduled. */
419 ndev->ops->close(ndev);
421 /* Clear flags */
422 ndev->flags = 0;
424 mutex_unlock(&ndev->req_lock);
426 return 0;
429 /* NCI command timer function */
430 static void nci_cmd_timer(unsigned long arg)
432 struct nci_dev *ndev = (void *) arg;
434 atomic_set(&ndev->cmd_cnt, 1);
435 queue_work(ndev->cmd_wq, &ndev->cmd_work);
438 /* NCI data exchange timer function */
439 static void nci_data_timer(unsigned long arg)
441 struct nci_dev *ndev = (void *) arg;
443 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
444 queue_work(ndev->rx_wq, &ndev->rx_work);
447 static int nci_dev_up(struct nfc_dev *nfc_dev)
449 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
451 return nci_open_device(ndev);
454 static int nci_dev_down(struct nfc_dev *nfc_dev)
456 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
458 return nci_close_device(ndev);
461 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
463 struct nci_set_config_param param;
465 if (!val || !len)
466 return 0;
468 param.id = id;
469 param.len = len;
470 param.val = val;
472 return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
473 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
475 EXPORT_SYMBOL(nci_set_config);
477 static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
479 struct nci_nfcee_discover_cmd cmd;
480 __u8 action = opt;
482 cmd.discovery_action = action;
484 nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
487 int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
489 return nci_request(ndev, nci_nfcee_discover_req, action,
490 msecs_to_jiffies(NCI_CMD_TIMEOUT));
492 EXPORT_SYMBOL(nci_nfcee_discover);
494 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
496 struct nci_nfcee_mode_set_cmd *cmd =
497 (struct nci_nfcee_mode_set_cmd *)opt;
499 nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
500 sizeof(struct nci_nfcee_mode_set_cmd), cmd);
503 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
505 struct nci_nfcee_mode_set_cmd cmd;
507 cmd.nfcee_id = nfcee_id;
508 cmd.nfcee_mode = nfcee_mode;
510 return nci_request(ndev, nci_nfcee_mode_set_req, (unsigned long)&cmd,
511 msecs_to_jiffies(NCI_CMD_TIMEOUT));
513 EXPORT_SYMBOL(nci_nfcee_mode_set);
515 static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
517 struct core_conn_create_data *data =
518 (struct core_conn_create_data *)opt;
520 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
523 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
524 u8 number_destination_params,
525 size_t params_len,
526 struct core_conn_create_dest_spec_params *params)
528 int r;
529 struct nci_core_conn_create_cmd *cmd;
530 struct core_conn_create_data data;
532 data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
533 cmd = kzalloc(data.length, GFP_KERNEL);
534 if (!cmd)
535 return -ENOMEM;
537 cmd->destination_type = destination_type;
538 cmd->number_destination_params = number_destination_params;
539 memcpy(cmd->params, params, params_len);
541 data.cmd = cmd;
542 ndev->cur_id = params->value[DEST_SPEC_PARAMS_ID_INDEX];
544 r = __nci_request(ndev, nci_core_conn_create_req,
545 (unsigned long)&data,
546 msecs_to_jiffies(NCI_CMD_TIMEOUT));
547 kfree(cmd);
548 return r;
550 EXPORT_SYMBOL(nci_core_conn_create);
552 static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
554 __u8 conn_id = opt;
556 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
559 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
561 return nci_request(ndev, nci_core_conn_close_req, conn_id,
562 msecs_to_jiffies(NCI_CMD_TIMEOUT));
564 EXPORT_SYMBOL(nci_core_conn_close);
566 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
568 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
569 struct nci_set_config_param param;
570 int rc;
572 param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
573 if ((param.val == NULL) || (param.len == 0))
574 return 0;
576 if (param.len > NFC_MAX_GT_LEN)
577 return -EINVAL;
579 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
581 rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
582 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
583 if (rc)
584 return rc;
586 param.id = NCI_LN_ATR_RES_GEN_BYTES;
588 return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
589 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
592 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
594 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
595 int rc;
596 __u8 val;
598 val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
600 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
601 if (rc)
602 return rc;
604 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
606 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
607 if (rc)
608 return rc;
610 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
612 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
615 static int nci_start_poll(struct nfc_dev *nfc_dev,
616 __u32 im_protocols, __u32 tm_protocols)
618 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
619 struct nci_rf_discover_param param;
620 int rc;
622 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
623 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
624 pr_err("unable to start poll, since poll is already active\n");
625 return -EBUSY;
628 if (ndev->target_active_prot) {
629 pr_err("there is an active target\n");
630 return -EBUSY;
633 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
634 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
635 pr_debug("target active or w4 select, implicitly deactivate\n");
637 rc = nci_request(ndev, nci_rf_deactivate_req,
638 NCI_DEACTIVATE_TYPE_IDLE_MODE,
639 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
640 if (rc)
641 return -EBUSY;
644 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
645 rc = nci_set_local_general_bytes(nfc_dev);
646 if (rc) {
647 pr_err("failed to set local general bytes\n");
648 return rc;
652 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
653 rc = nci_set_listen_parameters(nfc_dev);
654 if (rc)
655 pr_err("failed to set listen parameters\n");
658 param.im_protocols = im_protocols;
659 param.tm_protocols = tm_protocols;
660 rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
661 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
663 if (!rc)
664 ndev->poll_prots = im_protocols;
666 return rc;
669 static void nci_stop_poll(struct nfc_dev *nfc_dev)
671 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
673 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
674 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
675 pr_err("unable to stop poll, since poll is not active\n");
676 return;
679 nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
680 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
683 static int nci_activate_target(struct nfc_dev *nfc_dev,
684 struct nfc_target *target, __u32 protocol)
686 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
687 struct nci_rf_discover_select_param param;
688 struct nfc_target *nci_target = NULL;
689 int i;
690 int rc = 0;
692 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
694 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
695 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
696 pr_err("there is no available target to activate\n");
697 return -EINVAL;
700 if (ndev->target_active_prot) {
701 pr_err("there is already an active target\n");
702 return -EBUSY;
705 for (i = 0; i < ndev->n_targets; i++) {
706 if (ndev->targets[i].idx == target->idx) {
707 nci_target = &ndev->targets[i];
708 break;
712 if (!nci_target) {
713 pr_err("unable to find the selected target\n");
714 return -EINVAL;
717 if (!(nci_target->supported_protocols & (1 << protocol))) {
718 pr_err("target does not support the requested protocol 0x%x\n",
719 protocol);
720 return -EINVAL;
723 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
724 param.rf_discovery_id = nci_target->logical_idx;
726 if (protocol == NFC_PROTO_JEWEL)
727 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
728 else if (protocol == NFC_PROTO_MIFARE)
729 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
730 else if (protocol == NFC_PROTO_FELICA)
731 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
732 else if (protocol == NFC_PROTO_ISO14443 ||
733 protocol == NFC_PROTO_ISO14443_B)
734 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
735 else
736 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
738 rc = nci_request(ndev, nci_rf_discover_select_req,
739 (unsigned long)&param,
740 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
743 if (!rc)
744 ndev->target_active_prot = protocol;
746 return rc;
749 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
750 struct nfc_target *target)
752 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
754 pr_debug("entry\n");
756 if (!ndev->target_active_prot) {
757 pr_err("unable to deactivate target, no active target\n");
758 return;
761 ndev->target_active_prot = 0;
763 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
764 nci_request(ndev, nci_rf_deactivate_req,
765 NCI_DEACTIVATE_TYPE_SLEEP_MODE,
766 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
770 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
771 __u8 comm_mode, __u8 *gb, size_t gb_len)
773 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
774 int rc;
776 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
778 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
779 if (rc)
780 return rc;
782 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
783 ndev->remote_gb_len);
784 if (!rc)
785 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
786 NFC_RF_INITIATOR);
788 return rc;
791 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
793 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
794 int rc;
796 pr_debug("entry\n");
798 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
799 nci_deactivate_target(nfc_dev, NULL);
800 } else {
801 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
802 atomic_read(&ndev->state) == NCI_DISCOVERY) {
803 nci_request(ndev, nci_rf_deactivate_req, 0,
804 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
807 rc = nfc_tm_deactivated(nfc_dev);
808 if (rc)
809 pr_err("error when signaling tm deactivation\n");
812 return 0;
816 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
817 struct sk_buff *skb,
818 data_exchange_cb_t cb, void *cb_context)
820 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
821 int rc;
822 struct nci_conn_info *conn_info;
824 conn_info = ndev->rf_conn_info;
825 if (!conn_info)
826 return -EPROTO;
828 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
830 if (!ndev->target_active_prot) {
831 pr_err("unable to exchange data, no active target\n");
832 return -EINVAL;
835 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
836 return -EBUSY;
838 /* store cb and context to be used on receiving data */
839 conn_info->data_exchange_cb = cb;
840 conn_info->data_exchange_cb_context = cb_context;
842 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
843 if (rc)
844 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
846 return rc;
849 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
851 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
852 int rc;
854 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
855 if (rc)
856 pr_err("unable to send data\n");
858 return rc;
861 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
863 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
865 if (ndev->ops->enable_se)
866 return ndev->ops->enable_se(ndev, se_idx);
868 return 0;
871 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
873 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
875 if (ndev->ops->disable_se)
876 return ndev->ops->disable_se(ndev, se_idx);
878 return 0;
881 static int nci_discover_se(struct nfc_dev *nfc_dev)
883 int r;
884 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
886 if (ndev->ops->discover_se) {
887 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
888 if (r != NCI_STATUS_OK)
889 return -EPROTO;
891 return ndev->ops->discover_se(ndev);
894 return 0;
897 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
898 u8 *apdu, size_t apdu_length,
899 se_io_cb_t cb, void *cb_context)
901 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
903 if (ndev->ops->se_io)
904 return ndev->ops->se_io(ndev, se_idx, apdu,
905 apdu_length, cb, cb_context);
907 return 0;
910 static struct nfc_ops nci_nfc_ops = {
911 .dev_up = nci_dev_up,
912 .dev_down = nci_dev_down,
913 .start_poll = nci_start_poll,
914 .stop_poll = nci_stop_poll,
915 .dep_link_up = nci_dep_link_up,
916 .dep_link_down = nci_dep_link_down,
917 .activate_target = nci_activate_target,
918 .deactivate_target = nci_deactivate_target,
919 .im_transceive = nci_transceive,
920 .tm_send = nci_tm_send,
921 .enable_se = nci_enable_se,
922 .disable_se = nci_disable_se,
923 .discover_se = nci_discover_se,
924 .se_io = nci_se_io,
927 /* ---- Interface to NCI drivers ---- */
929 * nci_allocate_device - allocate a new nci device
931 * @ops: device operations
932 * @supported_protocols: NFC protocols supported by the device
934 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
935 __u32 supported_protocols,
936 int tx_headroom, int tx_tailroom)
938 struct nci_dev *ndev;
940 pr_debug("supported_protocols 0x%x\n", supported_protocols);
942 if (!ops->open || !ops->close || !ops->send)
943 return NULL;
945 if (!supported_protocols)
946 return NULL;
948 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
949 if (!ndev)
950 return NULL;
952 ndev->ops = ops;
953 ndev->tx_headroom = tx_headroom;
954 ndev->tx_tailroom = tx_tailroom;
955 init_completion(&ndev->req_completion);
957 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
958 supported_protocols,
959 tx_headroom + NCI_DATA_HDR_SIZE,
960 tx_tailroom);
961 if (!ndev->nfc_dev)
962 goto free_nci;
964 ndev->hci_dev = nci_hci_allocate(ndev);
965 if (!ndev->hci_dev)
966 goto free_nfc;
968 nfc_set_drvdata(ndev->nfc_dev, ndev);
970 return ndev;
972 free_nfc:
973 kfree(ndev->nfc_dev);
975 free_nci:
976 kfree(ndev);
977 return NULL;
979 EXPORT_SYMBOL(nci_allocate_device);
982 * nci_free_device - deallocate nci device
984 * @ndev: The nci device to deallocate
986 void nci_free_device(struct nci_dev *ndev)
988 nfc_free_device(ndev->nfc_dev);
989 kfree(ndev);
991 EXPORT_SYMBOL(nci_free_device);
994 * nci_register_device - register a nci device in the nfc subsystem
996 * @dev: The nci device to register
998 int nci_register_device(struct nci_dev *ndev)
1000 int rc;
1001 struct device *dev = &ndev->nfc_dev->dev;
1002 char name[32];
1004 ndev->flags = 0;
1006 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1007 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1008 ndev->cmd_wq = create_singlethread_workqueue(name);
1009 if (!ndev->cmd_wq) {
1010 rc = -ENOMEM;
1011 goto exit;
1014 INIT_WORK(&ndev->rx_work, nci_rx_work);
1015 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1016 ndev->rx_wq = create_singlethread_workqueue(name);
1017 if (!ndev->rx_wq) {
1018 rc = -ENOMEM;
1019 goto destroy_cmd_wq_exit;
1022 INIT_WORK(&ndev->tx_work, nci_tx_work);
1023 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1024 ndev->tx_wq = create_singlethread_workqueue(name);
1025 if (!ndev->tx_wq) {
1026 rc = -ENOMEM;
1027 goto destroy_rx_wq_exit;
1030 skb_queue_head_init(&ndev->cmd_q);
1031 skb_queue_head_init(&ndev->rx_q);
1032 skb_queue_head_init(&ndev->tx_q);
1034 setup_timer(&ndev->cmd_timer, nci_cmd_timer,
1035 (unsigned long) ndev);
1036 setup_timer(&ndev->data_timer, nci_data_timer,
1037 (unsigned long) ndev);
1039 mutex_init(&ndev->req_lock);
1040 INIT_LIST_HEAD(&ndev->conn_info_list);
1042 rc = nfc_register_device(ndev->nfc_dev);
1043 if (rc)
1044 goto destroy_rx_wq_exit;
1046 goto exit;
1048 destroy_rx_wq_exit:
1049 destroy_workqueue(ndev->rx_wq);
1051 destroy_cmd_wq_exit:
1052 destroy_workqueue(ndev->cmd_wq);
1054 exit:
1055 return rc;
1057 EXPORT_SYMBOL(nci_register_device);
1060 * nci_unregister_device - unregister a nci device in the nfc subsystem
1062 * @dev: The nci device to unregister
1064 void nci_unregister_device(struct nci_dev *ndev)
1066 struct nci_conn_info *conn_info, *n;
1068 nci_close_device(ndev);
1070 destroy_workqueue(ndev->cmd_wq);
1071 destroy_workqueue(ndev->rx_wq);
1072 destroy_workqueue(ndev->tx_wq);
1074 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1075 list_del(&conn_info->list);
1076 /* conn_info is allocated with devm_kzalloc */
1079 nfc_unregister_device(ndev->nfc_dev);
1081 EXPORT_SYMBOL(nci_unregister_device);
1084 * nci_recv_frame - receive frame from NCI drivers
1086 * @ndev: The nci device
1087 * @skb: The sk_buff to receive
1089 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1091 pr_debug("len %d\n", skb->len);
1093 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1094 !test_bit(NCI_INIT, &ndev->flags))) {
1095 kfree_skb(skb);
1096 return -ENXIO;
1099 /* Queue frame for rx worker thread */
1100 skb_queue_tail(&ndev->rx_q, skb);
1101 queue_work(ndev->rx_wq, &ndev->rx_work);
1103 return 0;
1105 EXPORT_SYMBOL(nci_recv_frame);
1107 static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1109 pr_debug("len %d\n", skb->len);
1111 if (!ndev) {
1112 kfree_skb(skb);
1113 return -ENODEV;
1116 /* Get rid of skb owner, prior to sending to the driver. */
1117 skb_orphan(skb);
1119 /* Send copy to sniffer */
1120 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1121 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1123 return ndev->ops->send(ndev, skb);
1126 /* Send NCI command */
1127 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1129 struct nci_ctrl_hdr *hdr;
1130 struct sk_buff *skb;
1132 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1134 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1135 if (!skb) {
1136 pr_err("no memory for command\n");
1137 return -ENOMEM;
1140 hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
1141 hdr->gid = nci_opcode_gid(opcode);
1142 hdr->oid = nci_opcode_oid(opcode);
1143 hdr->plen = plen;
1145 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1146 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1148 if (plen)
1149 memcpy(skb_put(skb, plen), payload, plen);
1151 skb_queue_tail(&ndev->cmd_q, skb);
1152 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1154 return 0;
1157 /* ---- NCI TX Data worker thread ---- */
1159 static void nci_tx_work(struct work_struct *work)
1161 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1162 struct nci_conn_info *conn_info;
1163 struct sk_buff *skb;
1165 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1166 if (!conn_info)
1167 return;
1169 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1171 /* Send queued tx data */
1172 while (atomic_read(&conn_info->credits_cnt)) {
1173 skb = skb_dequeue(&ndev->tx_q);
1174 if (!skb)
1175 return;
1177 /* Check if data flow control is used */
1178 if (atomic_read(&conn_info->credits_cnt) !=
1179 NCI_DATA_FLOW_CONTROL_NOT_USED)
1180 atomic_dec(&conn_info->credits_cnt);
1182 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1183 nci_pbf(skb->data),
1184 nci_conn_id(skb->data),
1185 nci_plen(skb->data));
1187 nci_send_frame(ndev, skb);
1189 mod_timer(&ndev->data_timer,
1190 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1194 /* ----- NCI RX worker thread (data & control) ----- */
1196 static void nci_rx_work(struct work_struct *work)
1198 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1199 struct sk_buff *skb;
1201 while ((skb = skb_dequeue(&ndev->rx_q))) {
1203 /* Send copy to sniffer */
1204 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1205 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1207 /* Process frame */
1208 switch (nci_mt(skb->data)) {
1209 case NCI_MT_RSP_PKT:
1210 nci_rsp_packet(ndev, skb);
1211 break;
1213 case NCI_MT_NTF_PKT:
1214 nci_ntf_packet(ndev, skb);
1215 break;
1217 case NCI_MT_DATA_PKT:
1218 nci_rx_data_packet(ndev, skb);
1219 break;
1221 default:
1222 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1223 kfree_skb(skb);
1224 break;
1228 /* check if a data exchange timout has occurred */
1229 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1230 /* complete the data exchange transaction, if exists */
1231 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1232 nci_data_exchange_complete(ndev, NULL,
1233 ndev->cur_conn_id,
1234 -ETIMEDOUT);
1236 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1240 /* ----- NCI TX CMD worker thread ----- */
1242 static void nci_cmd_work(struct work_struct *work)
1244 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1245 struct sk_buff *skb;
1247 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1249 /* Send queued command */
1250 if (atomic_read(&ndev->cmd_cnt)) {
1251 skb = skb_dequeue(&ndev->cmd_q);
1252 if (!skb)
1253 return;
1255 atomic_dec(&ndev->cmd_cnt);
1257 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1258 nci_pbf(skb->data),
1259 nci_opcode_gid(nci_opcode(skb->data)),
1260 nci_opcode_oid(nci_opcode(skb->data)),
1261 nci_plen(skb->data));
1263 nci_send_frame(ndev, skb);
1265 mod_timer(&ndev->cmd_timer,
1266 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1270 MODULE_LICENSE("GPL");