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/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>
40 #include <net/nfc/nci.h>
41 #include <net/nfc/nci_core.h>
42 #include <linux/nfc.h>
44 static void nci_cmd_work(struct work_struct
*work
);
45 static void nci_rx_work(struct work_struct
*work
);
46 static void nci_tx_work(struct work_struct
*work
);
48 struct nci_conn_info
*nci_get_conn_info_by_conn_id(struct nci_dev
*ndev
,
51 struct nci_conn_info
*conn_info
;
53 list_for_each_entry(conn_info
, &ndev
->conn_info_list
, list
) {
54 if (conn_info
->conn_id
== conn_id
)
61 /* ---- NCI requests ---- */
63 void nci_req_complete(struct nci_dev
*ndev
, int result
)
65 if (ndev
->req_status
== NCI_REQ_PEND
) {
66 ndev
->req_result
= result
;
67 ndev
->req_status
= NCI_REQ_DONE
;
68 complete(&ndev
->req_completion
);
72 static void nci_req_cancel(struct nci_dev
*ndev
, int err
)
74 if (ndev
->req_status
== NCI_REQ_PEND
) {
75 ndev
->req_result
= err
;
76 ndev
->req_status
= NCI_REQ_CANCELED
;
77 complete(&ndev
->req_completion
);
81 /* Execute request and wait for completion. */
82 static int __nci_request(struct nci_dev
*ndev
,
83 void (*req
)(struct nci_dev
*ndev
, unsigned long opt
),
84 unsigned long opt
, __u32 timeout
)
89 ndev
->req_status
= NCI_REQ_PEND
;
91 reinit_completion(&ndev
->req_completion
);
94 wait_for_completion_interruptible_timeout(&ndev
->req_completion
,
97 pr_debug("wait_for_completion return %ld\n", completion_rc
);
99 if (completion_rc
> 0) {
100 switch (ndev
->req_status
) {
102 rc
= nci_to_errno(ndev
->req_result
);
105 case NCI_REQ_CANCELED
:
106 rc
= -ndev
->req_result
;
114 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
117 rc
= ((completion_rc
== 0) ? (-ETIMEDOUT
) : (completion_rc
));
120 ndev
->req_status
= ndev
->req_result
= 0;
125 static inline int nci_request(struct nci_dev
*ndev
,
126 void (*req
)(struct nci_dev
*ndev
,
128 unsigned long opt
, __u32 timeout
)
132 if (!test_bit(NCI_UP
, &ndev
->flags
))
135 /* Serialize all requests */
136 mutex_lock(&ndev
->req_lock
);
137 rc
= __nci_request(ndev
, req
, opt
, timeout
);
138 mutex_unlock(&ndev
->req_lock
);
143 static void nci_reset_req(struct nci_dev
*ndev
, unsigned long opt
)
145 struct nci_core_reset_cmd cmd
;
147 cmd
.reset_type
= NCI_RESET_TYPE_RESET_CONFIG
;
148 nci_send_cmd(ndev
, NCI_OP_CORE_RESET_CMD
, 1, &cmd
);
151 static void nci_init_req(struct nci_dev
*ndev
, unsigned long opt
)
153 nci_send_cmd(ndev
, NCI_OP_CORE_INIT_CMD
, 0, NULL
);
156 static void nci_init_complete_req(struct nci_dev
*ndev
, unsigned long opt
)
158 struct nci_rf_disc_map_cmd cmd
;
159 struct disc_map_config
*cfg
= cmd
.mapping_configs
;
160 __u8
*num
= &cmd
.num_mapping_configs
;
163 /* set rf mapping configurations */
166 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
167 for (i
= 0; i
< ndev
->num_supported_rf_interfaces
; i
++) {
168 if (ndev
->supported_rf_interfaces
[i
] ==
169 NCI_RF_INTERFACE_ISO_DEP
) {
170 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
171 cfg
[*num
].mode
= NCI_DISC_MAP_MODE_POLL
|
172 NCI_DISC_MAP_MODE_LISTEN
;
173 cfg
[*num
].rf_interface
= NCI_RF_INTERFACE_ISO_DEP
;
175 } else if (ndev
->supported_rf_interfaces
[i
] ==
176 NCI_RF_INTERFACE_NFC_DEP
) {
177 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
178 cfg
[*num
].mode
= NCI_DISC_MAP_MODE_POLL
|
179 NCI_DISC_MAP_MODE_LISTEN
;
180 cfg
[*num
].rf_interface
= NCI_RF_INTERFACE_NFC_DEP
;
184 if (*num
== NCI_MAX_NUM_MAPPING_CONFIGS
)
188 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_MAP_CMD
,
189 (1 + ((*num
) * sizeof(struct disc_map_config
))), &cmd
);
192 struct nci_set_config_param
{
198 static void nci_set_config_req(struct nci_dev
*ndev
, unsigned long opt
)
200 struct nci_set_config_param
*param
= (struct nci_set_config_param
*)opt
;
201 struct nci_core_set_config_cmd cmd
;
203 BUG_ON(param
->len
> NCI_MAX_PARAM_LEN
);
206 cmd
.param
.id
= param
->id
;
207 cmd
.param
.len
= param
->len
;
208 memcpy(cmd
.param
.val
, param
->val
, param
->len
);
210 nci_send_cmd(ndev
, NCI_OP_CORE_SET_CONFIG_CMD
, (3 + param
->len
), &cmd
);
213 struct nci_rf_discover_param
{
218 static void nci_rf_discover_req(struct nci_dev
*ndev
, unsigned long opt
)
220 struct nci_rf_discover_param
*param
=
221 (struct nci_rf_discover_param
*)opt
;
222 struct nci_rf_disc_cmd cmd
;
224 cmd
.num_disc_configs
= 0;
226 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
227 (param
->im_protocols
& NFC_PROTO_JEWEL_MASK
||
228 param
->im_protocols
& NFC_PROTO_MIFARE_MASK
||
229 param
->im_protocols
& NFC_PROTO_ISO14443_MASK
||
230 param
->im_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
231 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
232 NCI_NFC_A_PASSIVE_POLL_MODE
;
233 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
234 cmd
.num_disc_configs
++;
237 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
238 (param
->im_protocols
& NFC_PROTO_ISO14443_B_MASK
)) {
239 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
240 NCI_NFC_B_PASSIVE_POLL_MODE
;
241 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
242 cmd
.num_disc_configs
++;
245 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
246 (param
->im_protocols
& NFC_PROTO_FELICA_MASK
||
247 param
->im_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
248 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
249 NCI_NFC_F_PASSIVE_POLL_MODE
;
250 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
251 cmd
.num_disc_configs
++;
254 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
255 (param
->im_protocols
& NFC_PROTO_ISO15693_MASK
)) {
256 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
257 NCI_NFC_V_PASSIVE_POLL_MODE
;
258 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
259 cmd
.num_disc_configs
++;
262 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
- 1) &&
263 (param
->tm_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
264 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
265 NCI_NFC_A_PASSIVE_LISTEN_MODE
;
266 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
267 cmd
.num_disc_configs
++;
268 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
269 NCI_NFC_F_PASSIVE_LISTEN_MODE
;
270 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
271 cmd
.num_disc_configs
++;
274 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_CMD
,
275 (1 + (cmd
.num_disc_configs
* sizeof(struct disc_config
))),
279 struct nci_rf_discover_select_param
{
280 __u8 rf_discovery_id
;
284 static void nci_rf_discover_select_req(struct nci_dev
*ndev
, unsigned long opt
)
286 struct nci_rf_discover_select_param
*param
=
287 (struct nci_rf_discover_select_param
*)opt
;
288 struct nci_rf_discover_select_cmd cmd
;
290 cmd
.rf_discovery_id
= param
->rf_discovery_id
;
291 cmd
.rf_protocol
= param
->rf_protocol
;
293 switch (cmd
.rf_protocol
) {
294 case NCI_RF_PROTOCOL_ISO_DEP
:
295 cmd
.rf_interface
= NCI_RF_INTERFACE_ISO_DEP
;
298 case NCI_RF_PROTOCOL_NFC_DEP
:
299 cmd
.rf_interface
= NCI_RF_INTERFACE_NFC_DEP
;
303 cmd
.rf_interface
= NCI_RF_INTERFACE_FRAME
;
307 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_SELECT_CMD
,
308 sizeof(struct nci_rf_discover_select_cmd
), &cmd
);
311 static void nci_rf_deactivate_req(struct nci_dev
*ndev
, unsigned long opt
)
313 struct nci_rf_deactivate_cmd cmd
;
317 nci_send_cmd(ndev
, NCI_OP_RF_DEACTIVATE_CMD
,
318 sizeof(struct nci_rf_deactivate_cmd
), &cmd
);
321 static int nci_open_device(struct nci_dev
*ndev
)
325 mutex_lock(&ndev
->req_lock
);
327 if (test_bit(NCI_UP
, &ndev
->flags
)) {
332 if (ndev
->ops
->open(ndev
)) {
337 atomic_set(&ndev
->cmd_cnt
, 1);
339 set_bit(NCI_INIT
, &ndev
->flags
);
341 rc
= __nci_request(ndev
, nci_reset_req
, 0,
342 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
344 if (ndev
->ops
->setup
)
345 ndev
->ops
->setup(ndev
);
348 rc
= __nci_request(ndev
, nci_init_req
, 0,
349 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
353 rc
= __nci_request(ndev
, nci_init_complete_req
, 0,
354 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
357 clear_bit(NCI_INIT
, &ndev
->flags
);
360 set_bit(NCI_UP
, &ndev
->flags
);
361 nci_clear_target_list(ndev
);
362 atomic_set(&ndev
->state
, NCI_IDLE
);
364 /* Init failed, cleanup */
365 skb_queue_purge(&ndev
->cmd_q
);
366 skb_queue_purge(&ndev
->rx_q
);
367 skb_queue_purge(&ndev
->tx_q
);
369 ndev
->ops
->close(ndev
);
374 mutex_unlock(&ndev
->req_lock
);
378 static int nci_close_device(struct nci_dev
*ndev
)
380 nci_req_cancel(ndev
, ENODEV
);
381 mutex_lock(&ndev
->req_lock
);
383 if (!test_and_clear_bit(NCI_UP
, &ndev
->flags
)) {
384 del_timer_sync(&ndev
->cmd_timer
);
385 del_timer_sync(&ndev
->data_timer
);
386 mutex_unlock(&ndev
->req_lock
);
390 /* Drop RX and TX queues */
391 skb_queue_purge(&ndev
->rx_q
);
392 skb_queue_purge(&ndev
->tx_q
);
394 /* Flush RX and TX wq */
395 flush_workqueue(ndev
->rx_wq
);
396 flush_workqueue(ndev
->tx_wq
);
399 skb_queue_purge(&ndev
->cmd_q
);
400 atomic_set(&ndev
->cmd_cnt
, 1);
402 set_bit(NCI_INIT
, &ndev
->flags
);
403 __nci_request(ndev
, nci_reset_req
, 0,
404 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
405 clear_bit(NCI_INIT
, &ndev
->flags
);
407 del_timer_sync(&ndev
->cmd_timer
);
410 flush_workqueue(ndev
->cmd_wq
);
412 /* After this point our queues are empty
413 * and no works are scheduled. */
414 ndev
->ops
->close(ndev
);
419 mutex_unlock(&ndev
->req_lock
);
424 /* NCI command timer function */
425 static void nci_cmd_timer(unsigned long arg
)
427 struct nci_dev
*ndev
= (void *) arg
;
429 atomic_set(&ndev
->cmd_cnt
, 1);
430 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
433 /* NCI data exchange timer function */
434 static void nci_data_timer(unsigned long arg
)
436 struct nci_dev
*ndev
= (void *) arg
;
438 set_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
);
439 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
442 static int nci_dev_up(struct nfc_dev
*nfc_dev
)
444 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
446 return nci_open_device(ndev
);
449 static int nci_dev_down(struct nfc_dev
*nfc_dev
)
451 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
453 return nci_close_device(ndev
);
456 int nci_set_config(struct nci_dev
*ndev
, __u8 id
, size_t len
, __u8
*val
)
458 struct nci_set_config_param param
;
467 return __nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
468 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
470 EXPORT_SYMBOL(nci_set_config
);
472 static void nci_nfcee_discover_req(struct nci_dev
*ndev
, unsigned long opt
)
474 struct nci_nfcee_discover_cmd cmd
;
477 cmd
.discovery_action
= action
;
479 nci_send_cmd(ndev
, NCI_OP_NFCEE_DISCOVER_CMD
, 1, &cmd
);
482 int nci_nfcee_discover(struct nci_dev
*ndev
, u8 action
)
484 return nci_request(ndev
, nci_nfcee_discover_req
, action
,
485 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
487 EXPORT_SYMBOL(nci_nfcee_discover
);
489 static void nci_nfcee_mode_set_req(struct nci_dev
*ndev
, unsigned long opt
)
491 struct nci_nfcee_mode_set_cmd
*cmd
=
492 (struct nci_nfcee_mode_set_cmd
*)opt
;
494 nci_send_cmd(ndev
, NCI_OP_NFCEE_MODE_SET_CMD
,
495 sizeof(struct nci_nfcee_mode_set_cmd
), cmd
);
498 int nci_nfcee_mode_set(struct nci_dev
*ndev
, u8 nfcee_id
, u8 nfcee_mode
)
500 struct nci_nfcee_mode_set_cmd cmd
;
502 cmd
.nfcee_id
= nfcee_id
;
503 cmd
.nfcee_mode
= nfcee_mode
;
505 return nci_request(ndev
, nci_nfcee_mode_set_req
, (unsigned long)&cmd
,
506 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
508 EXPORT_SYMBOL(nci_nfcee_mode_set
);
510 static void nci_core_conn_create_req(struct nci_dev
*ndev
, unsigned long opt
)
512 struct nci_core_conn_create_cmd cmd
;
513 struct core_conn_create_dest_spec_params
*params
=
514 (struct core_conn_create_dest_spec_params
*)opt
;
516 cmd
.destination_type
= NCI_DESTINATION_NFCEE
;
517 cmd
.number_destination_params
= 1;
518 memcpy(&cmd
.params
.type
, params
,
519 sizeof(struct core_conn_create_dest_spec_params
));
520 nci_send_cmd(ndev
, NCI_OP_CORE_CONN_CREATE_CMD
,
521 sizeof(struct nci_core_conn_create_cmd
), &cmd
);
524 int nci_core_conn_create(struct nci_dev
*ndev
,
525 struct core_conn_create_dest_spec_params
*params
)
527 ndev
->cur_id
= params
->value
.id
;
528 return nci_request(ndev
, nci_core_conn_create_req
,
529 (unsigned long)params
,
530 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
532 EXPORT_SYMBOL(nci_core_conn_create
);
534 static void nci_core_conn_close_req(struct nci_dev
*ndev
, unsigned long opt
)
538 nci_send_cmd(ndev
, NCI_OP_CORE_CONN_CLOSE_CMD
, 1, &conn_id
);
541 int nci_core_conn_close(struct nci_dev
*ndev
, u8 conn_id
)
543 return nci_request(ndev
, nci_core_conn_close_req
, conn_id
,
544 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
546 EXPORT_SYMBOL(nci_core_conn_close
);
548 static int nci_set_local_general_bytes(struct nfc_dev
*nfc_dev
)
550 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
551 struct nci_set_config_param param
;
554 param
.val
= nfc_get_local_general_bytes(nfc_dev
, ¶m
.len
);
555 if ((param
.val
== NULL
) || (param
.len
== 0))
558 if (param
.len
> NFC_MAX_GT_LEN
)
561 param
.id
= NCI_PN_ATR_REQ_GEN_BYTES
;
563 rc
= nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
564 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
568 param
.id
= NCI_LN_ATR_RES_GEN_BYTES
;
570 return nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
571 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
574 static int nci_set_listen_parameters(struct nfc_dev
*nfc_dev
)
576 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
580 val
= NCI_LA_SEL_INFO_NFC_DEP_MASK
;
582 rc
= nci_set_config(ndev
, NCI_LA_SEL_INFO
, 1, &val
);
586 val
= NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK
;
588 rc
= nci_set_config(ndev
, NCI_LF_PROTOCOL_TYPE
, 1, &val
);
592 val
= NCI_LF_CON_BITR_F_212
| NCI_LF_CON_BITR_F_424
;
594 return nci_set_config(ndev
, NCI_LF_CON_BITR_F
, 1, &val
);
597 static int nci_start_poll(struct nfc_dev
*nfc_dev
,
598 __u32 im_protocols
, __u32 tm_protocols
)
600 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
601 struct nci_rf_discover_param param
;
604 if ((atomic_read(&ndev
->state
) == NCI_DISCOVERY
) ||
605 (atomic_read(&ndev
->state
) == NCI_W4_ALL_DISCOVERIES
)) {
606 pr_err("unable to start poll, since poll is already active\n");
610 if (ndev
->target_active_prot
) {
611 pr_err("there is an active target\n");
615 if ((atomic_read(&ndev
->state
) == NCI_W4_HOST_SELECT
) ||
616 (atomic_read(&ndev
->state
) == NCI_POLL_ACTIVE
)) {
617 pr_debug("target active or w4 select, implicitly deactivate\n");
619 rc
= nci_request(ndev
, nci_rf_deactivate_req
,
620 NCI_DEACTIVATE_TYPE_IDLE_MODE
,
621 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
626 if ((im_protocols
| tm_protocols
) & NFC_PROTO_NFC_DEP_MASK
) {
627 rc
= nci_set_local_general_bytes(nfc_dev
);
629 pr_err("failed to set local general bytes\n");
634 if (tm_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
635 rc
= nci_set_listen_parameters(nfc_dev
);
637 pr_err("failed to set listen parameters\n");
640 param
.im_protocols
= im_protocols
;
641 param
.tm_protocols
= tm_protocols
;
642 rc
= nci_request(ndev
, nci_rf_discover_req
, (unsigned long)¶m
,
643 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT
));
646 ndev
->poll_prots
= im_protocols
;
651 static void nci_stop_poll(struct nfc_dev
*nfc_dev
)
653 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
655 if ((atomic_read(&ndev
->state
) != NCI_DISCOVERY
) &&
656 (atomic_read(&ndev
->state
) != NCI_W4_ALL_DISCOVERIES
)) {
657 pr_err("unable to stop poll, since poll is not active\n");
661 nci_request(ndev
, nci_rf_deactivate_req
, NCI_DEACTIVATE_TYPE_IDLE_MODE
,
662 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
665 static int nci_activate_target(struct nfc_dev
*nfc_dev
,
666 struct nfc_target
*target
, __u32 protocol
)
668 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
669 struct nci_rf_discover_select_param param
;
670 struct nfc_target
*nci_target
= NULL
;
674 pr_debug("target_idx %d, protocol 0x%x\n", target
->idx
, protocol
);
676 if ((atomic_read(&ndev
->state
) != NCI_W4_HOST_SELECT
) &&
677 (atomic_read(&ndev
->state
) != NCI_POLL_ACTIVE
)) {
678 pr_err("there is no available target to activate\n");
682 if (ndev
->target_active_prot
) {
683 pr_err("there is already an active target\n");
687 for (i
= 0; i
< ndev
->n_targets
; i
++) {
688 if (ndev
->targets
[i
].idx
== target
->idx
) {
689 nci_target
= &ndev
->targets
[i
];
695 pr_err("unable to find the selected target\n");
699 if (!(nci_target
->supported_protocols
& (1 << protocol
))) {
700 pr_err("target does not support the requested protocol 0x%x\n",
705 if (atomic_read(&ndev
->state
) == NCI_W4_HOST_SELECT
) {
706 param
.rf_discovery_id
= nci_target
->logical_idx
;
708 if (protocol
== NFC_PROTO_JEWEL
)
709 param
.rf_protocol
= NCI_RF_PROTOCOL_T1T
;
710 else if (protocol
== NFC_PROTO_MIFARE
)
711 param
.rf_protocol
= NCI_RF_PROTOCOL_T2T
;
712 else if (protocol
== NFC_PROTO_FELICA
)
713 param
.rf_protocol
= NCI_RF_PROTOCOL_T3T
;
714 else if (protocol
== NFC_PROTO_ISO14443
||
715 protocol
== NFC_PROTO_ISO14443_B
)
716 param
.rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
718 param
.rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
720 rc
= nci_request(ndev
, nci_rf_discover_select_req
,
721 (unsigned long)¶m
,
722 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT
));
726 ndev
->target_active_prot
= protocol
;
731 static void nci_deactivate_target(struct nfc_dev
*nfc_dev
,
732 struct nfc_target
*target
)
734 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
738 if (!ndev
->target_active_prot
) {
739 pr_err("unable to deactivate target, no active target\n");
743 ndev
->target_active_prot
= 0;
745 if (atomic_read(&ndev
->state
) == NCI_POLL_ACTIVE
) {
746 nci_request(ndev
, nci_rf_deactivate_req
,
747 NCI_DEACTIVATE_TYPE_SLEEP_MODE
,
748 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
752 static int nci_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
753 __u8 comm_mode
, __u8
*gb
, size_t gb_len
)
755 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
758 pr_debug("target_idx %d, comm_mode %d\n", target
->idx
, comm_mode
);
760 rc
= nci_activate_target(nfc_dev
, target
, NFC_PROTO_NFC_DEP
);
764 rc
= nfc_set_remote_general_bytes(nfc_dev
, ndev
->remote_gb
,
765 ndev
->remote_gb_len
);
767 rc
= nfc_dep_link_is_up(nfc_dev
, target
->idx
, NFC_COMM_PASSIVE
,
773 static int nci_dep_link_down(struct nfc_dev
*nfc_dev
)
775 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
780 if (nfc_dev
->rf_mode
== NFC_RF_INITIATOR
) {
781 nci_deactivate_target(nfc_dev
, NULL
);
783 if (atomic_read(&ndev
->state
) == NCI_LISTEN_ACTIVE
||
784 atomic_read(&ndev
->state
) == NCI_DISCOVERY
) {
785 nci_request(ndev
, nci_rf_deactivate_req
, 0,
786 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
789 rc
= nfc_tm_deactivated(nfc_dev
);
791 pr_err("error when signaling tm deactivation\n");
798 static int nci_transceive(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
800 data_exchange_cb_t cb
, void *cb_context
)
802 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
804 struct nci_conn_info
*conn_info
;
806 conn_info
= nci_get_conn_info_by_conn_id(ndev
, NCI_STATIC_RF_CONN_ID
);
810 pr_debug("target_idx %d, len %d\n", target
->idx
, skb
->len
);
812 if (!ndev
->target_active_prot
) {
813 pr_err("unable to exchange data, no active target\n");
817 if (test_and_set_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
820 /* store cb and context to be used on receiving data */
821 conn_info
->data_exchange_cb
= cb
;
822 conn_info
->data_exchange_cb_context
= cb_context
;
824 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
826 clear_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
);
831 static int nci_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
833 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
836 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
838 pr_err("unable to send data\n");
843 static int nci_enable_se(struct nfc_dev
*nfc_dev
, u32 se_idx
)
845 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
847 if (ndev
->ops
->enable_se
)
848 return ndev
->ops
->enable_se(ndev
, se_idx
);
853 static int nci_disable_se(struct nfc_dev
*nfc_dev
, u32 se_idx
)
855 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
857 if (ndev
->ops
->disable_se
)
858 return ndev
->ops
->disable_se(ndev
, se_idx
);
863 static int nci_discover_se(struct nfc_dev
*nfc_dev
)
865 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
867 if (ndev
->ops
->discover_se
)
868 return ndev
->ops
->discover_se(ndev
);
873 static int nci_se_io(struct nfc_dev
*nfc_dev
, u32 se_idx
,
874 u8
*apdu
, size_t apdu_length
,
875 se_io_cb_t cb
, void *cb_context
)
877 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
879 if (ndev
->ops
->se_io
)
880 return ndev
->ops
->se_io(ndev
, se_idx
, apdu
,
881 apdu_length
, cb
, cb_context
);
886 static struct nfc_ops nci_nfc_ops
= {
887 .dev_up
= nci_dev_up
,
888 .dev_down
= nci_dev_down
,
889 .start_poll
= nci_start_poll
,
890 .stop_poll
= nci_stop_poll
,
891 .dep_link_up
= nci_dep_link_up
,
892 .dep_link_down
= nci_dep_link_down
,
893 .activate_target
= nci_activate_target
,
894 .deactivate_target
= nci_deactivate_target
,
895 .im_transceive
= nci_transceive
,
896 .tm_send
= nci_tm_send
,
897 .enable_se
= nci_enable_se
,
898 .disable_se
= nci_disable_se
,
899 .discover_se
= nci_discover_se
,
903 /* ---- Interface to NCI drivers ---- */
906 * nci_allocate_device - allocate a new nci device
908 * @ops: device operations
909 * @supported_protocols: NFC protocols supported by the device
911 struct nci_dev
*nci_allocate_device(struct nci_ops
*ops
,
912 __u32 supported_protocols
,
913 int tx_headroom
, int tx_tailroom
)
915 struct nci_dev
*ndev
;
917 pr_debug("supported_protocols 0x%x\n", supported_protocols
);
919 if (!ops
->open
|| !ops
->close
|| !ops
->send
)
922 if (!supported_protocols
)
925 ndev
= kzalloc(sizeof(struct nci_dev
), GFP_KERNEL
);
930 ndev
->tx_headroom
= tx_headroom
;
931 ndev
->tx_tailroom
= tx_tailroom
;
932 init_completion(&ndev
->req_completion
);
934 ndev
->nfc_dev
= nfc_allocate_device(&nci_nfc_ops
,
936 tx_headroom
+ NCI_DATA_HDR_SIZE
,
941 nfc_set_drvdata(ndev
->nfc_dev
, ndev
);
949 EXPORT_SYMBOL(nci_allocate_device
);
952 * nci_free_device - deallocate nci device
954 * @ndev: The nci device to deallocate
956 void nci_free_device(struct nci_dev
*ndev
)
958 nfc_free_device(ndev
->nfc_dev
);
961 EXPORT_SYMBOL(nci_free_device
);
964 * nci_register_device - register a nci device in the nfc subsystem
966 * @dev: The nci device to register
968 int nci_register_device(struct nci_dev
*ndev
)
971 struct device
*dev
= &ndev
->nfc_dev
->dev
;
976 INIT_WORK(&ndev
->cmd_work
, nci_cmd_work
);
977 snprintf(name
, sizeof(name
), "%s_nci_cmd_wq", dev_name(dev
));
978 ndev
->cmd_wq
= create_singlethread_workqueue(name
);
984 INIT_WORK(&ndev
->rx_work
, nci_rx_work
);
985 snprintf(name
, sizeof(name
), "%s_nci_rx_wq", dev_name(dev
));
986 ndev
->rx_wq
= create_singlethread_workqueue(name
);
989 goto destroy_cmd_wq_exit
;
992 INIT_WORK(&ndev
->tx_work
, nci_tx_work
);
993 snprintf(name
, sizeof(name
), "%s_nci_tx_wq", dev_name(dev
));
994 ndev
->tx_wq
= create_singlethread_workqueue(name
);
997 goto destroy_rx_wq_exit
;
1000 skb_queue_head_init(&ndev
->cmd_q
);
1001 skb_queue_head_init(&ndev
->rx_q
);
1002 skb_queue_head_init(&ndev
->tx_q
);
1004 setup_timer(&ndev
->cmd_timer
, nci_cmd_timer
,
1005 (unsigned long) ndev
);
1006 setup_timer(&ndev
->data_timer
, nci_data_timer
,
1007 (unsigned long) ndev
);
1009 mutex_init(&ndev
->req_lock
);
1010 INIT_LIST_HEAD(&ndev
->conn_info_list
);
1012 rc
= nfc_register_device(ndev
->nfc_dev
);
1014 goto destroy_rx_wq_exit
;
1019 destroy_workqueue(ndev
->rx_wq
);
1021 destroy_cmd_wq_exit
:
1022 destroy_workqueue(ndev
->cmd_wq
);
1027 EXPORT_SYMBOL(nci_register_device
);
1030 * nci_unregister_device - unregister a nci device in the nfc subsystem
1032 * @dev: The nci device to unregister
1034 void nci_unregister_device(struct nci_dev
*ndev
)
1036 struct nci_conn_info
*conn_info
, *n
;
1038 nci_close_device(ndev
);
1040 destroy_workqueue(ndev
->cmd_wq
);
1041 destroy_workqueue(ndev
->rx_wq
);
1042 destroy_workqueue(ndev
->tx_wq
);
1044 list_for_each_entry_safe(conn_info
, n
, &ndev
->conn_info_list
, list
) {
1045 list_del(&conn_info
->list
);
1046 /* conn_info is allocated with devm_kzalloc */
1049 nfc_unregister_device(ndev
->nfc_dev
);
1051 EXPORT_SYMBOL(nci_unregister_device
);
1054 * nci_recv_frame - receive frame from NCI drivers
1056 * @ndev: The nci device
1057 * @skb: The sk_buff to receive
1059 int nci_recv_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
1061 pr_debug("len %d\n", skb
->len
);
1063 if (!ndev
|| (!test_bit(NCI_UP
, &ndev
->flags
) &&
1064 !test_bit(NCI_INIT
, &ndev
->flags
))) {
1069 /* Queue frame for rx worker thread */
1070 skb_queue_tail(&ndev
->rx_q
, skb
);
1071 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
1075 EXPORT_SYMBOL(nci_recv_frame
);
1077 static int nci_send_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
1079 pr_debug("len %d\n", skb
->len
);
1086 /* Get rid of skb owner, prior to sending to the driver. */
1089 /* Send copy to sniffer */
1090 nfc_send_to_raw_sock(ndev
->nfc_dev
, skb
,
1091 RAW_PAYLOAD_NCI
, NFC_DIRECTION_TX
);
1093 return ndev
->ops
->send(ndev
, skb
);
1096 /* Send NCI command */
1097 int nci_send_cmd(struct nci_dev
*ndev
, __u16 opcode
, __u8 plen
, void *payload
)
1099 struct nci_ctrl_hdr
*hdr
;
1100 struct sk_buff
*skb
;
1102 pr_debug("opcode 0x%x, plen %d\n", opcode
, plen
);
1104 skb
= nci_skb_alloc(ndev
, (NCI_CTRL_HDR_SIZE
+ plen
), GFP_KERNEL
);
1106 pr_err("no memory for command\n");
1110 hdr
= (struct nci_ctrl_hdr
*) skb_put(skb
, NCI_CTRL_HDR_SIZE
);
1111 hdr
->gid
= nci_opcode_gid(opcode
);
1112 hdr
->oid
= nci_opcode_oid(opcode
);
1115 nci_mt_set((__u8
*)hdr
, NCI_MT_CMD_PKT
);
1116 nci_pbf_set((__u8
*)hdr
, NCI_PBF_LAST
);
1119 memcpy(skb_put(skb
, plen
), payload
, plen
);
1121 skb_queue_tail(&ndev
->cmd_q
, skb
);
1122 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
1127 /* ---- NCI TX Data worker thread ---- */
1129 static void nci_tx_work(struct work_struct
*work
)
1131 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, tx_work
);
1132 struct nci_conn_info
*conn_info
;
1133 struct sk_buff
*skb
;
1135 conn_info
= nci_get_conn_info_by_conn_id(ndev
, ndev
->cur_conn_id
);
1139 pr_debug("credits_cnt %d\n", atomic_read(&conn_info
->credits_cnt
));
1141 /* Send queued tx data */
1142 while (atomic_read(&conn_info
->credits_cnt
)) {
1143 skb
= skb_dequeue(&ndev
->tx_q
);
1147 /* Check if data flow control is used */
1148 if (atomic_read(&conn_info
->credits_cnt
) !=
1149 NCI_DATA_FLOW_CONTROL_NOT_USED
)
1150 atomic_dec(&conn_info
->credits_cnt
);
1152 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1154 nci_conn_id(skb
->data
),
1155 nci_plen(skb
->data
));
1157 nci_send_frame(ndev
, skb
);
1159 mod_timer(&ndev
->data_timer
,
1160 jiffies
+ msecs_to_jiffies(NCI_DATA_TIMEOUT
));
1164 /* ----- NCI RX worker thread (data & control) ----- */
1166 static void nci_rx_work(struct work_struct
*work
)
1168 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, rx_work
);
1169 struct sk_buff
*skb
;
1171 while ((skb
= skb_dequeue(&ndev
->rx_q
))) {
1173 /* Send copy to sniffer */
1174 nfc_send_to_raw_sock(ndev
->nfc_dev
, skb
,
1175 RAW_PAYLOAD_NCI
, NFC_DIRECTION_RX
);
1178 switch (nci_mt(skb
->data
)) {
1179 case NCI_MT_RSP_PKT
:
1180 nci_rsp_packet(ndev
, skb
);
1183 case NCI_MT_NTF_PKT
:
1184 nci_ntf_packet(ndev
, skb
);
1187 case NCI_MT_DATA_PKT
:
1188 nci_rx_data_packet(ndev
, skb
);
1192 pr_err("unknown MT 0x%x\n", nci_mt(skb
->data
));
1198 /* check if a data exchange timout has occurred */
1199 if (test_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
)) {
1200 /* complete the data exchange transaction, if exists */
1201 if (test_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
1202 nci_data_exchange_complete(ndev
, NULL
,
1206 clear_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
);
1210 /* ----- NCI TX CMD worker thread ----- */
1212 static void nci_cmd_work(struct work_struct
*work
)
1214 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, cmd_work
);
1215 struct sk_buff
*skb
;
1217 pr_debug("cmd_cnt %d\n", atomic_read(&ndev
->cmd_cnt
));
1219 /* Send queued command */
1220 if (atomic_read(&ndev
->cmd_cnt
)) {
1221 skb
= skb_dequeue(&ndev
->cmd_q
);
1225 atomic_dec(&ndev
->cmd_cnt
);
1227 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1229 nci_opcode_gid(nci_opcode(skb
->data
)),
1230 nci_opcode_oid(nci_opcode(skb
->data
)),
1231 nci_plen(skb
->data
));
1233 nci_send_frame(ndev
, skb
);
1235 mod_timer(&ndev
->cmd_timer
,
1236 jiffies
+ msecs_to_jiffies(NCI_CMD_TIMEOUT
));
1240 MODULE_LICENSE("GPL");