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 /* ---- NCI requests ---- */
69 void nci_req_complete(struct nci_dev
*ndev
, int result
)
71 if (ndev
->req_status
== NCI_REQ_PEND
) {
72 ndev
->req_result
= result
;
73 ndev
->req_status
= NCI_REQ_DONE
;
74 complete(&ndev
->req_completion
);
77 EXPORT_SYMBOL(nci_req_complete
);
79 static void nci_req_cancel(struct nci_dev
*ndev
, int err
)
81 if (ndev
->req_status
== NCI_REQ_PEND
) {
82 ndev
->req_result
= err
;
83 ndev
->req_status
= NCI_REQ_CANCELED
;
84 complete(&ndev
->req_completion
);
88 /* Execute request and wait for completion. */
89 static int __nci_request(struct nci_dev
*ndev
,
90 void (*req
)(struct nci_dev
*ndev
, unsigned long opt
),
91 unsigned long opt
, __u32 timeout
)
96 ndev
->req_status
= NCI_REQ_PEND
;
98 reinit_completion(&ndev
->req_completion
);
101 wait_for_completion_interruptible_timeout(&ndev
->req_completion
,
104 pr_debug("wait_for_completion return %ld\n", completion_rc
);
106 if (completion_rc
> 0) {
107 switch (ndev
->req_status
) {
109 rc
= nci_to_errno(ndev
->req_result
);
112 case NCI_REQ_CANCELED
:
113 rc
= -ndev
->req_result
;
121 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
124 rc
= ((completion_rc
== 0) ? (-ETIMEDOUT
) : (completion_rc
));
127 ndev
->req_status
= ndev
->req_result
= 0;
132 inline int nci_request(struct nci_dev
*ndev
,
133 void (*req
)(struct nci_dev
*ndev
,
135 unsigned long opt
, __u32 timeout
)
139 if (!test_bit(NCI_UP
, &ndev
->flags
))
142 /* Serialize all requests */
143 mutex_lock(&ndev
->req_lock
);
144 rc
= __nci_request(ndev
, req
, opt
, timeout
);
145 mutex_unlock(&ndev
->req_lock
);
150 static void nci_reset_req(struct nci_dev
*ndev
, unsigned long opt
)
152 struct nci_core_reset_cmd cmd
;
154 cmd
.reset_type
= NCI_RESET_TYPE_RESET_CONFIG
;
155 nci_send_cmd(ndev
, NCI_OP_CORE_RESET_CMD
, 1, &cmd
);
158 static void nci_init_req(struct nci_dev
*ndev
, unsigned long opt
)
160 nci_send_cmd(ndev
, NCI_OP_CORE_INIT_CMD
, 0, NULL
);
163 static void nci_init_complete_req(struct nci_dev
*ndev
, unsigned long opt
)
165 struct nci_rf_disc_map_cmd cmd
;
166 struct disc_map_config
*cfg
= cmd
.mapping_configs
;
167 __u8
*num
= &cmd
.num_mapping_configs
;
170 /* set rf mapping configurations */
173 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
174 for (i
= 0; i
< ndev
->num_supported_rf_interfaces
; i
++) {
175 if (ndev
->supported_rf_interfaces
[i
] ==
176 NCI_RF_INTERFACE_ISO_DEP
) {
177 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
178 cfg
[*num
].mode
= NCI_DISC_MAP_MODE_POLL
|
179 NCI_DISC_MAP_MODE_LISTEN
;
180 cfg
[*num
].rf_interface
= NCI_RF_INTERFACE_ISO_DEP
;
182 } else if (ndev
->supported_rf_interfaces
[i
] ==
183 NCI_RF_INTERFACE_NFC_DEP
) {
184 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
185 cfg
[*num
].mode
= NCI_DISC_MAP_MODE_POLL
|
186 NCI_DISC_MAP_MODE_LISTEN
;
187 cfg
[*num
].rf_interface
= NCI_RF_INTERFACE_NFC_DEP
;
191 if (*num
== NCI_MAX_NUM_MAPPING_CONFIGS
)
195 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_MAP_CMD
,
196 (1 + ((*num
) * sizeof(struct disc_map_config
))), &cmd
);
199 struct nci_set_config_param
{
205 static void nci_set_config_req(struct nci_dev
*ndev
, unsigned long opt
)
207 struct nci_set_config_param
*param
= (struct nci_set_config_param
*)opt
;
208 struct nci_core_set_config_cmd cmd
;
210 BUG_ON(param
->len
> NCI_MAX_PARAM_LEN
);
213 cmd
.param
.id
= param
->id
;
214 cmd
.param
.len
= param
->len
;
215 memcpy(cmd
.param
.val
, param
->val
, param
->len
);
217 nci_send_cmd(ndev
, NCI_OP_CORE_SET_CONFIG_CMD
, (3 + param
->len
), &cmd
);
220 struct nci_rf_discover_param
{
225 static void nci_rf_discover_req(struct nci_dev
*ndev
, unsigned long opt
)
227 struct nci_rf_discover_param
*param
=
228 (struct nci_rf_discover_param
*)opt
;
229 struct nci_rf_disc_cmd cmd
;
231 cmd
.num_disc_configs
= 0;
233 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
234 (param
->im_protocols
& NFC_PROTO_JEWEL_MASK
||
235 param
->im_protocols
& NFC_PROTO_MIFARE_MASK
||
236 param
->im_protocols
& NFC_PROTO_ISO14443_MASK
||
237 param
->im_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
238 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
239 NCI_NFC_A_PASSIVE_POLL_MODE
;
240 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
241 cmd
.num_disc_configs
++;
244 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
245 (param
->im_protocols
& NFC_PROTO_ISO14443_B_MASK
)) {
246 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
247 NCI_NFC_B_PASSIVE_POLL_MODE
;
248 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
249 cmd
.num_disc_configs
++;
252 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
253 (param
->im_protocols
& NFC_PROTO_FELICA_MASK
||
254 param
->im_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
255 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
256 NCI_NFC_F_PASSIVE_POLL_MODE
;
257 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
258 cmd
.num_disc_configs
++;
261 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
262 (param
->im_protocols
& NFC_PROTO_ISO15693_MASK
)) {
263 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
264 NCI_NFC_V_PASSIVE_POLL_MODE
;
265 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
266 cmd
.num_disc_configs
++;
269 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
- 1) &&
270 (param
->tm_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
271 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
272 NCI_NFC_A_PASSIVE_LISTEN_MODE
;
273 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
274 cmd
.num_disc_configs
++;
275 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
276 NCI_NFC_F_PASSIVE_LISTEN_MODE
;
277 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
278 cmd
.num_disc_configs
++;
281 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_CMD
,
282 (1 + (cmd
.num_disc_configs
* sizeof(struct disc_config
))),
286 struct nci_rf_discover_select_param
{
287 __u8 rf_discovery_id
;
291 static void nci_rf_discover_select_req(struct nci_dev
*ndev
, unsigned long opt
)
293 struct nci_rf_discover_select_param
*param
=
294 (struct nci_rf_discover_select_param
*)opt
;
295 struct nci_rf_discover_select_cmd cmd
;
297 cmd
.rf_discovery_id
= param
->rf_discovery_id
;
298 cmd
.rf_protocol
= param
->rf_protocol
;
300 switch (cmd
.rf_protocol
) {
301 case NCI_RF_PROTOCOL_ISO_DEP
:
302 cmd
.rf_interface
= NCI_RF_INTERFACE_ISO_DEP
;
305 case NCI_RF_PROTOCOL_NFC_DEP
:
306 cmd
.rf_interface
= NCI_RF_INTERFACE_NFC_DEP
;
310 cmd
.rf_interface
= NCI_RF_INTERFACE_FRAME
;
314 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_SELECT_CMD
,
315 sizeof(struct nci_rf_discover_select_cmd
), &cmd
);
318 static void nci_rf_deactivate_req(struct nci_dev
*ndev
, unsigned long opt
)
320 struct nci_rf_deactivate_cmd cmd
;
324 nci_send_cmd(ndev
, NCI_OP_RF_DEACTIVATE_CMD
,
325 sizeof(struct nci_rf_deactivate_cmd
), &cmd
);
328 struct nci_prop_cmd_param
{
334 static void nci_prop_cmd_req(struct nci_dev
*ndev
, unsigned long opt
)
336 struct nci_prop_cmd_param
*param
= (struct nci_prop_cmd_param
*)opt
;
338 nci_send_cmd(ndev
, param
->opcode
, param
->len
, param
->payload
);
341 int nci_prop_cmd(struct nci_dev
*ndev
, __u8 oid
, size_t len
, __u8
*payload
)
343 struct nci_prop_cmd_param param
;
345 param
.opcode
= nci_opcode_pack(NCI_GID_PROPRIETARY
, oid
);
347 param
.payload
= payload
;
349 return __nci_request(ndev
, nci_prop_cmd_req
, (unsigned long)¶m
,
350 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
352 EXPORT_SYMBOL(nci_prop_cmd
);
354 int nci_core_reset(struct nci_dev
*ndev
)
356 return __nci_request(ndev
, nci_reset_req
, 0,
357 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
359 EXPORT_SYMBOL(nci_core_reset
);
361 int nci_core_init(struct nci_dev
*ndev
)
363 return __nci_request(ndev
, nci_init_req
, 0,
364 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
366 EXPORT_SYMBOL(nci_core_init
);
368 static int nci_open_device(struct nci_dev
*ndev
)
372 mutex_lock(&ndev
->req_lock
);
374 if (test_bit(NCI_UP
, &ndev
->flags
)) {
379 if (ndev
->ops
->open(ndev
)) {
384 atomic_set(&ndev
->cmd_cnt
, 1);
386 set_bit(NCI_INIT
, &ndev
->flags
);
389 rc
= ndev
->ops
->init(ndev
);
392 rc
= __nci_request(ndev
, nci_reset_req
, 0,
393 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
396 if (!rc
&& ndev
->ops
->setup
) {
397 rc
= ndev
->ops
->setup(ndev
);
401 rc
= __nci_request(ndev
, nci_init_req
, 0,
402 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
405 if (!rc
&& ndev
->ops
->post_setup
)
406 rc
= ndev
->ops
->post_setup(ndev
);
409 rc
= __nci_request(ndev
, nci_init_complete_req
, 0,
410 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
413 clear_bit(NCI_INIT
, &ndev
->flags
);
416 set_bit(NCI_UP
, &ndev
->flags
);
417 nci_clear_target_list(ndev
);
418 atomic_set(&ndev
->state
, NCI_IDLE
);
420 /* Init failed, cleanup */
421 skb_queue_purge(&ndev
->cmd_q
);
422 skb_queue_purge(&ndev
->rx_q
);
423 skb_queue_purge(&ndev
->tx_q
);
425 ndev
->ops
->close(ndev
);
430 mutex_unlock(&ndev
->req_lock
);
434 static int nci_close_device(struct nci_dev
*ndev
)
436 nci_req_cancel(ndev
, ENODEV
);
437 mutex_lock(&ndev
->req_lock
);
439 if (!test_and_clear_bit(NCI_UP
, &ndev
->flags
)) {
440 del_timer_sync(&ndev
->cmd_timer
);
441 del_timer_sync(&ndev
->data_timer
);
442 mutex_unlock(&ndev
->req_lock
);
446 /* Drop RX and TX queues */
447 skb_queue_purge(&ndev
->rx_q
);
448 skb_queue_purge(&ndev
->tx_q
);
450 /* Flush RX and TX wq */
451 flush_workqueue(ndev
->rx_wq
);
452 flush_workqueue(ndev
->tx_wq
);
455 skb_queue_purge(&ndev
->cmd_q
);
456 atomic_set(&ndev
->cmd_cnt
, 1);
458 set_bit(NCI_INIT
, &ndev
->flags
);
459 __nci_request(ndev
, nci_reset_req
, 0,
460 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
462 /* After this point our queues are empty
463 * and no works are scheduled.
465 ndev
->ops
->close(ndev
);
467 clear_bit(NCI_INIT
, &ndev
->flags
);
469 del_timer_sync(&ndev
->cmd_timer
);
472 flush_workqueue(ndev
->cmd_wq
);
477 mutex_unlock(&ndev
->req_lock
);
482 /* NCI command timer function */
483 static void nci_cmd_timer(unsigned long arg
)
485 struct nci_dev
*ndev
= (void *) arg
;
487 atomic_set(&ndev
->cmd_cnt
, 1);
488 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
491 /* NCI data exchange timer function */
492 static void nci_data_timer(unsigned long arg
)
494 struct nci_dev
*ndev
= (void *) arg
;
496 set_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
);
497 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
500 static int nci_dev_up(struct nfc_dev
*nfc_dev
)
502 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
504 return nci_open_device(ndev
);
507 static int nci_dev_down(struct nfc_dev
*nfc_dev
)
509 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
511 return nci_close_device(ndev
);
514 int nci_set_config(struct nci_dev
*ndev
, __u8 id
, size_t len
, __u8
*val
)
516 struct nci_set_config_param param
;
525 return __nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
526 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
528 EXPORT_SYMBOL(nci_set_config
);
530 static void nci_nfcee_discover_req(struct nci_dev
*ndev
, unsigned long opt
)
532 struct nci_nfcee_discover_cmd cmd
;
535 cmd
.discovery_action
= action
;
537 nci_send_cmd(ndev
, NCI_OP_NFCEE_DISCOVER_CMD
, 1, &cmd
);
540 int nci_nfcee_discover(struct nci_dev
*ndev
, u8 action
)
542 return __nci_request(ndev
, nci_nfcee_discover_req
, action
,
543 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
545 EXPORT_SYMBOL(nci_nfcee_discover
);
547 static void nci_nfcee_mode_set_req(struct nci_dev
*ndev
, unsigned long opt
)
549 struct nci_nfcee_mode_set_cmd
*cmd
=
550 (struct nci_nfcee_mode_set_cmd
*)opt
;
552 nci_send_cmd(ndev
, NCI_OP_NFCEE_MODE_SET_CMD
,
553 sizeof(struct nci_nfcee_mode_set_cmd
), cmd
);
556 int nci_nfcee_mode_set(struct nci_dev
*ndev
, u8 nfcee_id
, u8 nfcee_mode
)
558 struct nci_nfcee_mode_set_cmd cmd
;
560 cmd
.nfcee_id
= nfcee_id
;
561 cmd
.nfcee_mode
= nfcee_mode
;
563 return __nci_request(ndev
, nci_nfcee_mode_set_req
,
565 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
567 EXPORT_SYMBOL(nci_nfcee_mode_set
);
569 static void nci_core_conn_create_req(struct nci_dev
*ndev
, unsigned long opt
)
571 struct core_conn_create_data
*data
=
572 (struct core_conn_create_data
*)opt
;
574 nci_send_cmd(ndev
, NCI_OP_CORE_CONN_CREATE_CMD
, data
->length
, data
->cmd
);
577 int nci_core_conn_create(struct nci_dev
*ndev
, u8 destination_type
,
578 u8 number_destination_params
,
580 struct core_conn_create_dest_spec_params
*params
)
583 struct nci_core_conn_create_cmd
*cmd
;
584 struct core_conn_create_data data
;
586 data
.length
= params_len
+ sizeof(struct nci_core_conn_create_cmd
);
587 cmd
= kzalloc(data
.length
, GFP_KERNEL
);
591 cmd
->destination_type
= destination_type
;
592 cmd
->number_destination_params
= number_destination_params
;
593 memcpy(cmd
->params
, params
, params_len
);
596 ndev
->cur_id
= params
->value
[DEST_SPEC_PARAMS_ID_INDEX
];
598 r
= __nci_request(ndev
, nci_core_conn_create_req
,
599 (unsigned long)&data
,
600 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
604 EXPORT_SYMBOL(nci_core_conn_create
);
606 static void nci_core_conn_close_req(struct nci_dev
*ndev
, unsigned long opt
)
610 nci_send_cmd(ndev
, NCI_OP_CORE_CONN_CLOSE_CMD
, 1, &conn_id
);
613 int nci_core_conn_close(struct nci_dev
*ndev
, u8 conn_id
)
615 return __nci_request(ndev
, nci_core_conn_close_req
, conn_id
,
616 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
618 EXPORT_SYMBOL(nci_core_conn_close
);
620 static int nci_set_local_general_bytes(struct nfc_dev
*nfc_dev
)
622 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
623 struct nci_set_config_param param
;
626 param
.val
= nfc_get_local_general_bytes(nfc_dev
, ¶m
.len
);
627 if ((param
.val
== NULL
) || (param
.len
== 0))
630 if (param
.len
> NFC_MAX_GT_LEN
)
633 param
.id
= NCI_PN_ATR_REQ_GEN_BYTES
;
635 rc
= nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
636 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
640 param
.id
= NCI_LN_ATR_RES_GEN_BYTES
;
642 return nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
643 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
646 static int nci_set_listen_parameters(struct nfc_dev
*nfc_dev
)
648 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
652 val
= NCI_LA_SEL_INFO_NFC_DEP_MASK
;
654 rc
= nci_set_config(ndev
, NCI_LA_SEL_INFO
, 1, &val
);
658 val
= NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK
;
660 rc
= nci_set_config(ndev
, NCI_LF_PROTOCOL_TYPE
, 1, &val
);
664 val
= NCI_LF_CON_BITR_F_212
| NCI_LF_CON_BITR_F_424
;
666 return nci_set_config(ndev
, NCI_LF_CON_BITR_F
, 1, &val
);
669 static int nci_start_poll(struct nfc_dev
*nfc_dev
,
670 __u32 im_protocols
, __u32 tm_protocols
)
672 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
673 struct nci_rf_discover_param param
;
676 if ((atomic_read(&ndev
->state
) == NCI_DISCOVERY
) ||
677 (atomic_read(&ndev
->state
) == NCI_W4_ALL_DISCOVERIES
)) {
678 pr_err("unable to start poll, since poll is already active\n");
682 if (ndev
->target_active_prot
) {
683 pr_err("there is an active target\n");
687 if ((atomic_read(&ndev
->state
) == NCI_W4_HOST_SELECT
) ||
688 (atomic_read(&ndev
->state
) == NCI_POLL_ACTIVE
)) {
689 pr_debug("target active or w4 select, implicitly deactivate\n");
691 rc
= nci_request(ndev
, nci_rf_deactivate_req
,
692 NCI_DEACTIVATE_TYPE_IDLE_MODE
,
693 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
698 if ((im_protocols
| tm_protocols
) & NFC_PROTO_NFC_DEP_MASK
) {
699 rc
= nci_set_local_general_bytes(nfc_dev
);
701 pr_err("failed to set local general bytes\n");
706 if (tm_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
707 rc
= nci_set_listen_parameters(nfc_dev
);
709 pr_err("failed to set listen parameters\n");
712 param
.im_protocols
= im_protocols
;
713 param
.tm_protocols
= tm_protocols
;
714 rc
= nci_request(ndev
, nci_rf_discover_req
, (unsigned long)¶m
,
715 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT
));
718 ndev
->poll_prots
= im_protocols
;
723 static void nci_stop_poll(struct nfc_dev
*nfc_dev
)
725 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
727 if ((atomic_read(&ndev
->state
) != NCI_DISCOVERY
) &&
728 (atomic_read(&ndev
->state
) != NCI_W4_ALL_DISCOVERIES
)) {
729 pr_err("unable to stop poll, since poll is not active\n");
733 nci_request(ndev
, nci_rf_deactivate_req
, NCI_DEACTIVATE_TYPE_IDLE_MODE
,
734 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
737 static int nci_activate_target(struct nfc_dev
*nfc_dev
,
738 struct nfc_target
*target
, __u32 protocol
)
740 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
741 struct nci_rf_discover_select_param param
;
742 struct nfc_target
*nci_target
= NULL
;
746 pr_debug("target_idx %d, protocol 0x%x\n", target
->idx
, protocol
);
748 if ((atomic_read(&ndev
->state
) != NCI_W4_HOST_SELECT
) &&
749 (atomic_read(&ndev
->state
) != NCI_POLL_ACTIVE
)) {
750 pr_err("there is no available target to activate\n");
754 if (ndev
->target_active_prot
) {
755 pr_err("there is already an active target\n");
759 for (i
= 0; i
< ndev
->n_targets
; i
++) {
760 if (ndev
->targets
[i
].idx
== target
->idx
) {
761 nci_target
= &ndev
->targets
[i
];
767 pr_err("unable to find the selected target\n");
771 if (!(nci_target
->supported_protocols
& (1 << protocol
))) {
772 pr_err("target does not support the requested protocol 0x%x\n",
777 if (atomic_read(&ndev
->state
) == NCI_W4_HOST_SELECT
) {
778 param
.rf_discovery_id
= nci_target
->logical_idx
;
780 if (protocol
== NFC_PROTO_JEWEL
)
781 param
.rf_protocol
= NCI_RF_PROTOCOL_T1T
;
782 else if (protocol
== NFC_PROTO_MIFARE
)
783 param
.rf_protocol
= NCI_RF_PROTOCOL_T2T
;
784 else if (protocol
== NFC_PROTO_FELICA
)
785 param
.rf_protocol
= NCI_RF_PROTOCOL_T3T
;
786 else if (protocol
== NFC_PROTO_ISO14443
||
787 protocol
== NFC_PROTO_ISO14443_B
)
788 param
.rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
790 param
.rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
792 rc
= nci_request(ndev
, nci_rf_discover_select_req
,
793 (unsigned long)¶m
,
794 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT
));
798 ndev
->target_active_prot
= protocol
;
803 static void nci_deactivate_target(struct nfc_dev
*nfc_dev
,
804 struct nfc_target
*target
)
806 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
810 if (!ndev
->target_active_prot
) {
811 pr_err("unable to deactivate target, no active target\n");
815 ndev
->target_active_prot
= 0;
817 if (atomic_read(&ndev
->state
) == NCI_POLL_ACTIVE
) {
818 nci_request(ndev
, nci_rf_deactivate_req
,
819 NCI_DEACTIVATE_TYPE_IDLE_MODE
,
820 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
824 static int nci_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
825 __u8 comm_mode
, __u8
*gb
, size_t gb_len
)
827 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
830 pr_debug("target_idx %d, comm_mode %d\n", target
->idx
, comm_mode
);
832 rc
= nci_activate_target(nfc_dev
, target
, NFC_PROTO_NFC_DEP
);
836 rc
= nfc_set_remote_general_bytes(nfc_dev
, ndev
->remote_gb
,
837 ndev
->remote_gb_len
);
839 rc
= nfc_dep_link_is_up(nfc_dev
, target
->idx
, NFC_COMM_PASSIVE
,
845 static int nci_dep_link_down(struct nfc_dev
*nfc_dev
)
847 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
852 if (nfc_dev
->rf_mode
== NFC_RF_INITIATOR
) {
853 nci_deactivate_target(nfc_dev
, NULL
);
855 if (atomic_read(&ndev
->state
) == NCI_LISTEN_ACTIVE
||
856 atomic_read(&ndev
->state
) == NCI_DISCOVERY
) {
857 nci_request(ndev
, nci_rf_deactivate_req
, 0,
858 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
861 rc
= nfc_tm_deactivated(nfc_dev
);
863 pr_err("error when signaling tm deactivation\n");
870 static int nci_transceive(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
872 data_exchange_cb_t cb
, void *cb_context
)
874 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
876 struct nci_conn_info
*conn_info
;
878 conn_info
= ndev
->rf_conn_info
;
882 pr_debug("target_idx %d, len %d\n", target
->idx
, skb
->len
);
884 if (!ndev
->target_active_prot
) {
885 pr_err("unable to exchange data, no active target\n");
889 if (test_and_set_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
892 /* store cb and context to be used on receiving data */
893 conn_info
->data_exchange_cb
= cb
;
894 conn_info
->data_exchange_cb_context
= cb_context
;
896 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
898 clear_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
);
903 static int nci_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
905 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
908 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
910 pr_err("unable to send data\n");
915 static int nci_enable_se(struct nfc_dev
*nfc_dev
, u32 se_idx
)
917 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
919 if (ndev
->ops
->enable_se
)
920 return ndev
->ops
->enable_se(ndev
, se_idx
);
925 static int nci_disable_se(struct nfc_dev
*nfc_dev
, u32 se_idx
)
927 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
929 if (ndev
->ops
->disable_se
)
930 return ndev
->ops
->disable_se(ndev
, se_idx
);
935 static int nci_discover_se(struct nfc_dev
*nfc_dev
)
938 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
940 if (ndev
->ops
->discover_se
) {
941 r
= nci_nfcee_discover(ndev
, NCI_NFCEE_DISCOVERY_ACTION_ENABLE
);
942 if (r
!= NCI_STATUS_OK
)
945 return ndev
->ops
->discover_se(ndev
);
951 static int nci_se_io(struct nfc_dev
*nfc_dev
, u32 se_idx
,
952 u8
*apdu
, size_t apdu_length
,
953 se_io_cb_t cb
, void *cb_context
)
955 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
957 if (ndev
->ops
->se_io
)
958 return ndev
->ops
->se_io(ndev
, se_idx
, apdu
,
959 apdu_length
, cb
, cb_context
);
964 static int nci_fw_download(struct nfc_dev
*nfc_dev
, const char *firmware_name
)
966 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
968 if (!ndev
->ops
->fw_download
)
971 return ndev
->ops
->fw_download(ndev
, firmware_name
);
974 static struct nfc_ops nci_nfc_ops
= {
975 .dev_up
= nci_dev_up
,
976 .dev_down
= nci_dev_down
,
977 .start_poll
= nci_start_poll
,
978 .stop_poll
= nci_stop_poll
,
979 .dep_link_up
= nci_dep_link_up
,
980 .dep_link_down
= nci_dep_link_down
,
981 .activate_target
= nci_activate_target
,
982 .deactivate_target
= nci_deactivate_target
,
983 .im_transceive
= nci_transceive
,
984 .tm_send
= nci_tm_send
,
985 .enable_se
= nci_enable_se
,
986 .disable_se
= nci_disable_se
,
987 .discover_se
= nci_discover_se
,
989 .fw_download
= nci_fw_download
,
992 /* ---- Interface to NCI drivers ---- */
994 * nci_allocate_device - allocate a new nci device
996 * @ops: device operations
997 * @supported_protocols: NFC protocols supported by the device
999 struct nci_dev
*nci_allocate_device(struct nci_ops
*ops
,
1000 __u32 supported_protocols
,
1001 int tx_headroom
, int tx_tailroom
)
1003 struct nci_dev
*ndev
;
1005 pr_debug("supported_protocols 0x%x\n", supported_protocols
);
1007 if (!ops
->open
|| !ops
->close
|| !ops
->send
)
1010 if (!supported_protocols
)
1013 ndev
= kzalloc(sizeof(struct nci_dev
), GFP_KERNEL
);
1019 if (ops
->n_prop_ops
> NCI_MAX_PROPRIETARY_CMD
) {
1020 pr_err("Too many proprietary commands: %zd\n",
1022 ops
->prop_ops
= NULL
;
1023 ops
->n_prop_ops
= 0;
1026 ndev
->tx_headroom
= tx_headroom
;
1027 ndev
->tx_tailroom
= tx_tailroom
;
1028 init_completion(&ndev
->req_completion
);
1030 ndev
->nfc_dev
= nfc_allocate_device(&nci_nfc_ops
,
1031 supported_protocols
,
1032 tx_headroom
+ NCI_DATA_HDR_SIZE
,
1037 ndev
->hci_dev
= nci_hci_allocate(ndev
);
1041 nfc_set_drvdata(ndev
->nfc_dev
, ndev
);
1046 kfree(ndev
->nfc_dev
);
1052 EXPORT_SYMBOL(nci_allocate_device
);
1055 * nci_free_device - deallocate nci device
1057 * @ndev: The nci device to deallocate
1059 void nci_free_device(struct nci_dev
*ndev
)
1061 nfc_free_device(ndev
->nfc_dev
);
1064 EXPORT_SYMBOL(nci_free_device
);
1067 * nci_register_device - register a nci device in the nfc subsystem
1069 * @dev: The nci device to register
1071 int nci_register_device(struct nci_dev
*ndev
)
1074 struct device
*dev
= &ndev
->nfc_dev
->dev
;
1079 INIT_WORK(&ndev
->cmd_work
, nci_cmd_work
);
1080 snprintf(name
, sizeof(name
), "%s_nci_cmd_wq", dev_name(dev
));
1081 ndev
->cmd_wq
= create_singlethread_workqueue(name
);
1082 if (!ndev
->cmd_wq
) {
1087 INIT_WORK(&ndev
->rx_work
, nci_rx_work
);
1088 snprintf(name
, sizeof(name
), "%s_nci_rx_wq", dev_name(dev
));
1089 ndev
->rx_wq
= create_singlethread_workqueue(name
);
1092 goto destroy_cmd_wq_exit
;
1095 INIT_WORK(&ndev
->tx_work
, nci_tx_work
);
1096 snprintf(name
, sizeof(name
), "%s_nci_tx_wq", dev_name(dev
));
1097 ndev
->tx_wq
= create_singlethread_workqueue(name
);
1100 goto destroy_rx_wq_exit
;
1103 skb_queue_head_init(&ndev
->cmd_q
);
1104 skb_queue_head_init(&ndev
->rx_q
);
1105 skb_queue_head_init(&ndev
->tx_q
);
1107 setup_timer(&ndev
->cmd_timer
, nci_cmd_timer
,
1108 (unsigned long) ndev
);
1109 setup_timer(&ndev
->data_timer
, nci_data_timer
,
1110 (unsigned long) ndev
);
1112 mutex_init(&ndev
->req_lock
);
1113 INIT_LIST_HEAD(&ndev
->conn_info_list
);
1115 rc
= nfc_register_device(ndev
->nfc_dev
);
1117 goto destroy_rx_wq_exit
;
1122 destroy_workqueue(ndev
->rx_wq
);
1124 destroy_cmd_wq_exit
:
1125 destroy_workqueue(ndev
->cmd_wq
);
1130 EXPORT_SYMBOL(nci_register_device
);
1133 * nci_unregister_device - unregister a nci device in the nfc subsystem
1135 * @dev: The nci device to unregister
1137 void nci_unregister_device(struct nci_dev
*ndev
)
1139 struct nci_conn_info
*conn_info
, *n
;
1141 nci_close_device(ndev
);
1143 destroy_workqueue(ndev
->cmd_wq
);
1144 destroy_workqueue(ndev
->rx_wq
);
1145 destroy_workqueue(ndev
->tx_wq
);
1147 list_for_each_entry_safe(conn_info
, n
, &ndev
->conn_info_list
, list
) {
1148 list_del(&conn_info
->list
);
1149 /* conn_info is allocated with devm_kzalloc */
1152 nfc_unregister_device(ndev
->nfc_dev
);
1154 EXPORT_SYMBOL(nci_unregister_device
);
1157 * nci_recv_frame - receive frame from NCI drivers
1159 * @ndev: The nci device
1160 * @skb: The sk_buff to receive
1162 int nci_recv_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
1164 pr_debug("len %d\n", skb
->len
);
1166 if (!ndev
|| (!test_bit(NCI_UP
, &ndev
->flags
) &&
1167 !test_bit(NCI_INIT
, &ndev
->flags
))) {
1172 /* Queue frame for rx worker thread */
1173 skb_queue_tail(&ndev
->rx_q
, skb
);
1174 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
1178 EXPORT_SYMBOL(nci_recv_frame
);
1180 static int nci_send_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
1182 pr_debug("len %d\n", skb
->len
);
1189 /* Get rid of skb owner, prior to sending to the driver. */
1192 /* Send copy to sniffer */
1193 nfc_send_to_raw_sock(ndev
->nfc_dev
, skb
,
1194 RAW_PAYLOAD_NCI
, NFC_DIRECTION_TX
);
1196 return ndev
->ops
->send(ndev
, skb
);
1199 /* Send NCI command */
1200 int nci_send_cmd(struct nci_dev
*ndev
, __u16 opcode
, __u8 plen
, void *payload
)
1202 struct nci_ctrl_hdr
*hdr
;
1203 struct sk_buff
*skb
;
1205 pr_debug("opcode 0x%x, plen %d\n", opcode
, plen
);
1207 skb
= nci_skb_alloc(ndev
, (NCI_CTRL_HDR_SIZE
+ plen
), GFP_KERNEL
);
1209 pr_err("no memory for command\n");
1213 hdr
= (struct nci_ctrl_hdr
*) skb_put(skb
, NCI_CTRL_HDR_SIZE
);
1214 hdr
->gid
= nci_opcode_gid(opcode
);
1215 hdr
->oid
= nci_opcode_oid(opcode
);
1218 nci_mt_set((__u8
*)hdr
, NCI_MT_CMD_PKT
);
1219 nci_pbf_set((__u8
*)hdr
, NCI_PBF_LAST
);
1222 memcpy(skb_put(skb
, plen
), payload
, plen
);
1224 skb_queue_tail(&ndev
->cmd_q
, skb
);
1225 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
1230 /* Proprietary commands API */
1231 static struct nci_prop_ops
*prop_cmd_lookup(struct nci_dev
*ndev
,
1235 struct nci_prop_ops
*prop_op
;
1237 if (!ndev
->ops
->prop_ops
|| !ndev
->ops
->n_prop_ops
)
1240 for (i
= 0; i
< ndev
->ops
->n_prop_ops
; i
++) {
1241 prop_op
= &ndev
->ops
->prop_ops
[i
];
1242 if (prop_op
->opcode
== opcode
)
1249 int nci_prop_rsp_packet(struct nci_dev
*ndev
, __u16 rsp_opcode
,
1250 struct sk_buff
*skb
)
1252 struct nci_prop_ops
*prop_op
;
1254 prop_op
= prop_cmd_lookup(ndev
, rsp_opcode
);
1255 if (!prop_op
|| !prop_op
->rsp
)
1258 return prop_op
->rsp(ndev
, skb
);
1261 int nci_prop_ntf_packet(struct nci_dev
*ndev
, __u16 ntf_opcode
,
1262 struct sk_buff
*skb
)
1264 struct nci_prop_ops
*prop_op
;
1266 prop_op
= prop_cmd_lookup(ndev
, ntf_opcode
);
1267 if (!prop_op
|| !prop_op
->ntf
)
1270 return prop_op
->ntf(ndev
, skb
);
1273 /* ---- NCI TX Data worker thread ---- */
1275 static void nci_tx_work(struct work_struct
*work
)
1277 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, tx_work
);
1278 struct nci_conn_info
*conn_info
;
1279 struct sk_buff
*skb
;
1281 conn_info
= nci_get_conn_info_by_conn_id(ndev
, ndev
->cur_conn_id
);
1285 pr_debug("credits_cnt %d\n", atomic_read(&conn_info
->credits_cnt
));
1287 /* Send queued tx data */
1288 while (atomic_read(&conn_info
->credits_cnt
)) {
1289 skb
= skb_dequeue(&ndev
->tx_q
);
1293 /* Check if data flow control is used */
1294 if (atomic_read(&conn_info
->credits_cnt
) !=
1295 NCI_DATA_FLOW_CONTROL_NOT_USED
)
1296 atomic_dec(&conn_info
->credits_cnt
);
1298 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1300 nci_conn_id(skb
->data
),
1301 nci_plen(skb
->data
));
1303 nci_send_frame(ndev
, skb
);
1305 mod_timer(&ndev
->data_timer
,
1306 jiffies
+ msecs_to_jiffies(NCI_DATA_TIMEOUT
));
1310 /* ----- NCI RX worker thread (data & control) ----- */
1312 static void nci_rx_work(struct work_struct
*work
)
1314 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, rx_work
);
1315 struct sk_buff
*skb
;
1317 while ((skb
= skb_dequeue(&ndev
->rx_q
))) {
1319 /* Send copy to sniffer */
1320 nfc_send_to_raw_sock(ndev
->nfc_dev
, skb
,
1321 RAW_PAYLOAD_NCI
, NFC_DIRECTION_RX
);
1324 switch (nci_mt(skb
->data
)) {
1325 case NCI_MT_RSP_PKT
:
1326 nci_rsp_packet(ndev
, skb
);
1329 case NCI_MT_NTF_PKT
:
1330 nci_ntf_packet(ndev
, skb
);
1333 case NCI_MT_DATA_PKT
:
1334 nci_rx_data_packet(ndev
, skb
);
1338 pr_err("unknown MT 0x%x\n", nci_mt(skb
->data
));
1344 /* check if a data exchange timout has occurred */
1345 if (test_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
)) {
1346 /* complete the data exchange transaction, if exists */
1347 if (test_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
1348 nci_data_exchange_complete(ndev
, NULL
,
1352 clear_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
);
1356 /* ----- NCI TX CMD worker thread ----- */
1358 static void nci_cmd_work(struct work_struct
*work
)
1360 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, cmd_work
);
1361 struct sk_buff
*skb
;
1363 pr_debug("cmd_cnt %d\n", atomic_read(&ndev
->cmd_cnt
));
1365 /* Send queued command */
1366 if (atomic_read(&ndev
->cmd_cnt
)) {
1367 skb
= skb_dequeue(&ndev
->cmd_q
);
1371 atomic_dec(&ndev
->cmd_cnt
);
1373 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1375 nci_opcode_gid(nci_opcode(skb
->data
)),
1376 nci_opcode_oid(nci_opcode(skb
->data
)),
1377 nci_plen(skb
->data
));
1379 nci_send_frame(ndev
, skb
);
1381 mod_timer(&ndev
->cmd_timer
,
1382 jiffies
+ msecs_to_jiffies(NCI_CMD_TIMEOUT
));
1386 MODULE_LICENSE("GPL");