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 /* ---- NCI requests ---- */
50 void nci_req_complete(struct nci_dev
*ndev
, int result
)
52 if (ndev
->req_status
== NCI_REQ_PEND
) {
53 ndev
->req_result
= result
;
54 ndev
->req_status
= NCI_REQ_DONE
;
55 complete(&ndev
->req_completion
);
59 static void nci_req_cancel(struct nci_dev
*ndev
, int err
)
61 if (ndev
->req_status
== NCI_REQ_PEND
) {
62 ndev
->req_result
= err
;
63 ndev
->req_status
= NCI_REQ_CANCELED
;
64 complete(&ndev
->req_completion
);
68 /* Execute request and wait for completion. */
69 static int __nci_request(struct nci_dev
*ndev
,
70 void (*req
)(struct nci_dev
*ndev
, unsigned long opt
),
71 unsigned long opt
, __u32 timeout
)
76 ndev
->req_status
= NCI_REQ_PEND
;
78 reinit_completion(&ndev
->req_completion
);
81 wait_for_completion_interruptible_timeout(&ndev
->req_completion
,
84 pr_debug("wait_for_completion return %ld\n", completion_rc
);
86 if (completion_rc
> 0) {
87 switch (ndev
->req_status
) {
89 rc
= nci_to_errno(ndev
->req_result
);
92 case NCI_REQ_CANCELED
:
93 rc
= -ndev
->req_result
;
101 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
104 rc
= ((completion_rc
== 0) ? (-ETIMEDOUT
) : (completion_rc
));
107 ndev
->req_status
= ndev
->req_result
= 0;
112 static inline int nci_request(struct nci_dev
*ndev
,
113 void (*req
)(struct nci_dev
*ndev
,
115 unsigned long opt
, __u32 timeout
)
119 if (!test_bit(NCI_UP
, &ndev
->flags
))
122 /* Serialize all requests */
123 mutex_lock(&ndev
->req_lock
);
124 rc
= __nci_request(ndev
, req
, opt
, timeout
);
125 mutex_unlock(&ndev
->req_lock
);
130 static void nci_reset_req(struct nci_dev
*ndev
, unsigned long opt
)
132 struct nci_core_reset_cmd cmd
;
134 cmd
.reset_type
= NCI_RESET_TYPE_RESET_CONFIG
;
135 nci_send_cmd(ndev
, NCI_OP_CORE_RESET_CMD
, 1, &cmd
);
138 static void nci_init_req(struct nci_dev
*ndev
, unsigned long opt
)
140 nci_send_cmd(ndev
, NCI_OP_CORE_INIT_CMD
, 0, NULL
);
143 static void nci_init_complete_req(struct nci_dev
*ndev
, unsigned long opt
)
145 struct nci_rf_disc_map_cmd cmd
;
146 struct disc_map_config
*cfg
= cmd
.mapping_configs
;
147 __u8
*num
= &cmd
.num_mapping_configs
;
150 /* set rf mapping configurations */
153 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
154 for (i
= 0; i
< ndev
->num_supported_rf_interfaces
; i
++) {
155 if (ndev
->supported_rf_interfaces
[i
] ==
156 NCI_RF_INTERFACE_ISO_DEP
) {
157 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
158 cfg
[*num
].mode
= NCI_DISC_MAP_MODE_POLL
|
159 NCI_DISC_MAP_MODE_LISTEN
;
160 cfg
[*num
].rf_interface
= NCI_RF_INTERFACE_ISO_DEP
;
162 } else if (ndev
->supported_rf_interfaces
[i
] ==
163 NCI_RF_INTERFACE_NFC_DEP
) {
164 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
165 cfg
[*num
].mode
= NCI_DISC_MAP_MODE_POLL
|
166 NCI_DISC_MAP_MODE_LISTEN
;
167 cfg
[*num
].rf_interface
= NCI_RF_INTERFACE_NFC_DEP
;
171 if (*num
== NCI_MAX_NUM_MAPPING_CONFIGS
)
175 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_MAP_CMD
,
176 (1 + ((*num
) * sizeof(struct disc_map_config
))), &cmd
);
179 struct nci_set_config_param
{
185 static void nci_set_config_req(struct nci_dev
*ndev
, unsigned long opt
)
187 struct nci_set_config_param
*param
= (struct nci_set_config_param
*)opt
;
188 struct nci_core_set_config_cmd cmd
;
190 BUG_ON(param
->len
> NCI_MAX_PARAM_LEN
);
193 cmd
.param
.id
= param
->id
;
194 cmd
.param
.len
= param
->len
;
195 memcpy(cmd
.param
.val
, param
->val
, param
->len
);
197 nci_send_cmd(ndev
, NCI_OP_CORE_SET_CONFIG_CMD
, (3 + param
->len
), &cmd
);
200 struct nci_rf_discover_param
{
205 static void nci_rf_discover_req(struct nci_dev
*ndev
, unsigned long opt
)
207 struct nci_rf_discover_param
*param
=
208 (struct nci_rf_discover_param
*)opt
;
209 struct nci_rf_disc_cmd cmd
;
211 cmd
.num_disc_configs
= 0;
213 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
214 (param
->im_protocols
& NFC_PROTO_JEWEL_MASK
||
215 param
->im_protocols
& NFC_PROTO_MIFARE_MASK
||
216 param
->im_protocols
& NFC_PROTO_ISO14443_MASK
||
217 param
->im_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
218 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
219 NCI_NFC_A_PASSIVE_POLL_MODE
;
220 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
221 cmd
.num_disc_configs
++;
224 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
225 (param
->im_protocols
& NFC_PROTO_ISO14443_B_MASK
)) {
226 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
227 NCI_NFC_B_PASSIVE_POLL_MODE
;
228 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
229 cmd
.num_disc_configs
++;
232 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
233 (param
->im_protocols
& NFC_PROTO_FELICA_MASK
||
234 param
->im_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
235 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
236 NCI_NFC_F_PASSIVE_POLL_MODE
;
237 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
238 cmd
.num_disc_configs
++;
241 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
242 (param
->im_protocols
& NFC_PROTO_ISO15693_MASK
)) {
243 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
244 NCI_NFC_V_PASSIVE_POLL_MODE
;
245 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
246 cmd
.num_disc_configs
++;
249 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
- 1) &&
250 (param
->tm_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
251 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
252 NCI_NFC_A_PASSIVE_LISTEN_MODE
;
253 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
254 cmd
.num_disc_configs
++;
255 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
256 NCI_NFC_F_PASSIVE_LISTEN_MODE
;
257 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
258 cmd
.num_disc_configs
++;
261 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_CMD
,
262 (1 + (cmd
.num_disc_configs
* sizeof(struct disc_config
))),
266 struct nci_rf_discover_select_param
{
267 __u8 rf_discovery_id
;
271 static void nci_rf_discover_select_req(struct nci_dev
*ndev
, unsigned long opt
)
273 struct nci_rf_discover_select_param
*param
=
274 (struct nci_rf_discover_select_param
*)opt
;
275 struct nci_rf_discover_select_cmd cmd
;
277 cmd
.rf_discovery_id
= param
->rf_discovery_id
;
278 cmd
.rf_protocol
= param
->rf_protocol
;
280 switch (cmd
.rf_protocol
) {
281 case NCI_RF_PROTOCOL_ISO_DEP
:
282 cmd
.rf_interface
= NCI_RF_INTERFACE_ISO_DEP
;
285 case NCI_RF_PROTOCOL_NFC_DEP
:
286 cmd
.rf_interface
= NCI_RF_INTERFACE_NFC_DEP
;
290 cmd
.rf_interface
= NCI_RF_INTERFACE_FRAME
;
294 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_SELECT_CMD
,
295 sizeof(struct nci_rf_discover_select_cmd
), &cmd
);
298 static void nci_rf_deactivate_req(struct nci_dev
*ndev
, unsigned long opt
)
300 struct nci_rf_deactivate_cmd cmd
;
302 cmd
.type
= NCI_DEACTIVATE_TYPE_IDLE_MODE
;
304 nci_send_cmd(ndev
, NCI_OP_RF_DEACTIVATE_CMD
,
305 sizeof(struct nci_rf_deactivate_cmd
), &cmd
);
308 static int nci_open_device(struct nci_dev
*ndev
)
312 mutex_lock(&ndev
->req_lock
);
314 if (test_bit(NCI_UP
, &ndev
->flags
)) {
319 if (ndev
->ops
->open(ndev
)) {
324 atomic_set(&ndev
->cmd_cnt
, 1);
326 set_bit(NCI_INIT
, &ndev
->flags
);
328 rc
= __nci_request(ndev
, nci_reset_req
, 0,
329 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
331 if (ndev
->ops
->setup
)
332 ndev
->ops
->setup(ndev
);
335 rc
= __nci_request(ndev
, nci_init_req
, 0,
336 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
340 rc
= __nci_request(ndev
, nci_init_complete_req
, 0,
341 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
344 clear_bit(NCI_INIT
, &ndev
->flags
);
347 set_bit(NCI_UP
, &ndev
->flags
);
348 nci_clear_target_list(ndev
);
349 atomic_set(&ndev
->state
, NCI_IDLE
);
351 /* Init failed, cleanup */
352 skb_queue_purge(&ndev
->cmd_q
);
353 skb_queue_purge(&ndev
->rx_q
);
354 skb_queue_purge(&ndev
->tx_q
);
356 ndev
->ops
->close(ndev
);
361 mutex_unlock(&ndev
->req_lock
);
365 static int nci_close_device(struct nci_dev
*ndev
)
367 nci_req_cancel(ndev
, ENODEV
);
368 mutex_lock(&ndev
->req_lock
);
370 if (!test_and_clear_bit(NCI_UP
, &ndev
->flags
)) {
371 del_timer_sync(&ndev
->cmd_timer
);
372 del_timer_sync(&ndev
->data_timer
);
373 mutex_unlock(&ndev
->req_lock
);
377 /* Drop RX and TX queues */
378 skb_queue_purge(&ndev
->rx_q
);
379 skb_queue_purge(&ndev
->tx_q
);
381 /* Flush RX and TX wq */
382 flush_workqueue(ndev
->rx_wq
);
383 flush_workqueue(ndev
->tx_wq
);
386 skb_queue_purge(&ndev
->cmd_q
);
387 atomic_set(&ndev
->cmd_cnt
, 1);
389 set_bit(NCI_INIT
, &ndev
->flags
);
390 __nci_request(ndev
, nci_reset_req
, 0,
391 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
392 clear_bit(NCI_INIT
, &ndev
->flags
);
394 del_timer_sync(&ndev
->cmd_timer
);
397 flush_workqueue(ndev
->cmd_wq
);
399 /* After this point our queues are empty
400 * and no works are scheduled. */
401 ndev
->ops
->close(ndev
);
406 mutex_unlock(&ndev
->req_lock
);
411 /* NCI command timer function */
412 static void nci_cmd_timer(unsigned long arg
)
414 struct nci_dev
*ndev
= (void *) arg
;
416 atomic_set(&ndev
->cmd_cnt
, 1);
417 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
420 /* NCI data exchange timer function */
421 static void nci_data_timer(unsigned long arg
)
423 struct nci_dev
*ndev
= (void *) arg
;
425 set_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
);
426 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
429 static int nci_dev_up(struct nfc_dev
*nfc_dev
)
431 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
433 return nci_open_device(ndev
);
436 static int nci_dev_down(struct nfc_dev
*nfc_dev
)
438 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
440 return nci_close_device(ndev
);
443 int nci_set_config(struct nci_dev
*ndev
, __u8 id
, size_t len
, __u8
*val
)
445 struct nci_set_config_param param
;
454 return __nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
455 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
457 EXPORT_SYMBOL(nci_set_config
);
459 static int nci_set_local_general_bytes(struct nfc_dev
*nfc_dev
)
461 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
462 struct nci_set_config_param param
;
465 param
.val
= nfc_get_local_general_bytes(nfc_dev
, ¶m
.len
);
466 if ((param
.val
== NULL
) || (param
.len
== 0))
469 if (param
.len
> NFC_MAX_GT_LEN
)
472 param
.id
= NCI_PN_ATR_REQ_GEN_BYTES
;
474 rc
= nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
475 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
479 param
.id
= NCI_LN_ATR_RES_GEN_BYTES
;
481 return nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
482 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
485 static int nci_set_listen_parameters(struct nfc_dev
*nfc_dev
)
487 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
491 val
= NCI_LA_SEL_INFO_NFC_DEP_MASK
;
493 rc
= nci_set_config(ndev
, NCI_LA_SEL_INFO
, 1, &val
);
497 val
= NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK
;
499 rc
= nci_set_config(ndev
, NCI_LF_PROTOCOL_TYPE
, 1, &val
);
503 val
= NCI_LF_CON_BITR_F_212
| NCI_LF_CON_BITR_F_424
;
505 return nci_set_config(ndev
, NCI_LF_CON_BITR_F
, 1, &val
);
508 static int nci_start_poll(struct nfc_dev
*nfc_dev
,
509 __u32 im_protocols
, __u32 tm_protocols
)
511 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
512 struct nci_rf_discover_param param
;
515 if ((atomic_read(&ndev
->state
) == NCI_DISCOVERY
) ||
516 (atomic_read(&ndev
->state
) == NCI_W4_ALL_DISCOVERIES
)) {
517 pr_err("unable to start poll, since poll is already active\n");
521 if (ndev
->target_active_prot
) {
522 pr_err("there is an active target\n");
526 if ((atomic_read(&ndev
->state
) == NCI_W4_HOST_SELECT
) ||
527 (atomic_read(&ndev
->state
) == NCI_POLL_ACTIVE
)) {
528 pr_debug("target active or w4 select, implicitly deactivate\n");
530 rc
= nci_request(ndev
, nci_rf_deactivate_req
, 0,
531 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
536 if ((im_protocols
| tm_protocols
) & NFC_PROTO_NFC_DEP_MASK
) {
537 rc
= nci_set_local_general_bytes(nfc_dev
);
539 pr_err("failed to set local general bytes\n");
544 if (tm_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
545 rc
= nci_set_listen_parameters(nfc_dev
);
547 pr_err("failed to set listen parameters\n");
550 param
.im_protocols
= im_protocols
;
551 param
.tm_protocols
= tm_protocols
;
552 rc
= nci_request(ndev
, nci_rf_discover_req
, (unsigned long)¶m
,
553 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT
));
556 ndev
->poll_prots
= im_protocols
;
561 static void nci_stop_poll(struct nfc_dev
*nfc_dev
)
563 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
565 if ((atomic_read(&ndev
->state
) != NCI_DISCOVERY
) &&
566 (atomic_read(&ndev
->state
) != NCI_W4_ALL_DISCOVERIES
)) {
567 pr_err("unable to stop poll, since poll is not active\n");
571 nci_request(ndev
, nci_rf_deactivate_req
, 0,
572 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
575 static int nci_activate_target(struct nfc_dev
*nfc_dev
,
576 struct nfc_target
*target
, __u32 protocol
)
578 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
579 struct nci_rf_discover_select_param param
;
580 struct nfc_target
*nci_target
= NULL
;
584 pr_debug("target_idx %d, protocol 0x%x\n", target
->idx
, protocol
);
586 if ((atomic_read(&ndev
->state
) != NCI_W4_HOST_SELECT
) &&
587 (atomic_read(&ndev
->state
) != NCI_POLL_ACTIVE
)) {
588 pr_err("there is no available target to activate\n");
592 if (ndev
->target_active_prot
) {
593 pr_err("there is already an active target\n");
597 for (i
= 0; i
< ndev
->n_targets
; i
++) {
598 if (ndev
->targets
[i
].idx
== target
->idx
) {
599 nci_target
= &ndev
->targets
[i
];
605 pr_err("unable to find the selected target\n");
609 if (!(nci_target
->supported_protocols
& (1 << protocol
))) {
610 pr_err("target does not support the requested protocol 0x%x\n",
615 if (atomic_read(&ndev
->state
) == NCI_W4_HOST_SELECT
) {
616 param
.rf_discovery_id
= nci_target
->logical_idx
;
618 if (protocol
== NFC_PROTO_JEWEL
)
619 param
.rf_protocol
= NCI_RF_PROTOCOL_T1T
;
620 else if (protocol
== NFC_PROTO_MIFARE
)
621 param
.rf_protocol
= NCI_RF_PROTOCOL_T2T
;
622 else if (protocol
== NFC_PROTO_FELICA
)
623 param
.rf_protocol
= NCI_RF_PROTOCOL_T3T
;
624 else if (protocol
== NFC_PROTO_ISO14443
||
625 protocol
== NFC_PROTO_ISO14443_B
)
626 param
.rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
628 param
.rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
630 rc
= nci_request(ndev
, nci_rf_discover_select_req
,
631 (unsigned long)¶m
,
632 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT
));
636 ndev
->target_active_prot
= protocol
;
641 static void nci_deactivate_target(struct nfc_dev
*nfc_dev
,
642 struct nfc_target
*target
)
644 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
648 if (!ndev
->target_active_prot
) {
649 pr_err("unable to deactivate target, no active target\n");
653 ndev
->target_active_prot
= 0;
655 if (atomic_read(&ndev
->state
) == NCI_POLL_ACTIVE
) {
656 nci_request(ndev
, nci_rf_deactivate_req
, 0,
657 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
661 static int nci_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
662 __u8 comm_mode
, __u8
*gb
, size_t gb_len
)
664 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
667 pr_debug("target_idx %d, comm_mode %d\n", target
->idx
, comm_mode
);
669 rc
= nci_activate_target(nfc_dev
, target
, NFC_PROTO_NFC_DEP
);
673 rc
= nfc_set_remote_general_bytes(nfc_dev
, ndev
->remote_gb
,
674 ndev
->remote_gb_len
);
676 rc
= nfc_dep_link_is_up(nfc_dev
, target
->idx
, NFC_COMM_PASSIVE
,
682 static int nci_dep_link_down(struct nfc_dev
*nfc_dev
)
686 nci_deactivate_target(nfc_dev
, NULL
);
692 static int nci_transceive(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
694 data_exchange_cb_t cb
, void *cb_context
)
696 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
699 pr_debug("target_idx %d, len %d\n", target
->idx
, skb
->len
);
701 if (!ndev
->target_active_prot
) {
702 pr_err("unable to exchange data, no active target\n");
706 if (test_and_set_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
709 /* store cb and context to be used on receiving data */
710 ndev
->data_exchange_cb
= cb
;
711 ndev
->data_exchange_cb_context
= cb_context
;
713 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
715 clear_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
);
720 static int nci_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
722 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
725 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
727 pr_err("unable to send data\n");
732 static int nci_enable_se(struct nfc_dev
*nfc_dev
, u32 se_idx
)
737 static int nci_disable_se(struct nfc_dev
*nfc_dev
, u32 se_idx
)
742 static int nci_discover_se(struct nfc_dev
*nfc_dev
)
747 static struct nfc_ops nci_nfc_ops
= {
748 .dev_up
= nci_dev_up
,
749 .dev_down
= nci_dev_down
,
750 .start_poll
= nci_start_poll
,
751 .stop_poll
= nci_stop_poll
,
752 .dep_link_up
= nci_dep_link_up
,
753 .dep_link_down
= nci_dep_link_down
,
754 .activate_target
= nci_activate_target
,
755 .deactivate_target
= nci_deactivate_target
,
756 .im_transceive
= nci_transceive
,
757 .tm_send
= nci_tm_send
,
758 .enable_se
= nci_enable_se
,
759 .disable_se
= nci_disable_se
,
760 .discover_se
= nci_discover_se
,
763 /* ---- Interface to NCI drivers ---- */
766 * nci_allocate_device - allocate a new nci device
768 * @ops: device operations
769 * @supported_protocols: NFC protocols supported by the device
771 struct nci_dev
*nci_allocate_device(struct nci_ops
*ops
,
772 __u32 supported_protocols
,
773 int tx_headroom
, int tx_tailroom
)
775 struct nci_dev
*ndev
;
777 pr_debug("supported_protocols 0x%x\n", supported_protocols
);
779 if (!ops
->open
|| !ops
->close
|| !ops
->send
)
782 if (!supported_protocols
)
785 ndev
= kzalloc(sizeof(struct nci_dev
), GFP_KERNEL
);
790 ndev
->tx_headroom
= tx_headroom
;
791 ndev
->tx_tailroom
= tx_tailroom
;
792 init_completion(&ndev
->req_completion
);
794 ndev
->nfc_dev
= nfc_allocate_device(&nci_nfc_ops
,
796 tx_headroom
+ NCI_DATA_HDR_SIZE
,
801 nfc_set_drvdata(ndev
->nfc_dev
, ndev
);
809 EXPORT_SYMBOL(nci_allocate_device
);
812 * nci_free_device - deallocate nci device
814 * @ndev: The nci device to deallocate
816 void nci_free_device(struct nci_dev
*ndev
)
818 nfc_free_device(ndev
->nfc_dev
);
821 EXPORT_SYMBOL(nci_free_device
);
824 * nci_register_device - register a nci device in the nfc subsystem
826 * @dev: The nci device to register
828 int nci_register_device(struct nci_dev
*ndev
)
831 struct device
*dev
= &ndev
->nfc_dev
->dev
;
836 INIT_WORK(&ndev
->cmd_work
, nci_cmd_work
);
837 snprintf(name
, sizeof(name
), "%s_nci_cmd_wq", dev_name(dev
));
838 ndev
->cmd_wq
= create_singlethread_workqueue(name
);
844 INIT_WORK(&ndev
->rx_work
, nci_rx_work
);
845 snprintf(name
, sizeof(name
), "%s_nci_rx_wq", dev_name(dev
));
846 ndev
->rx_wq
= create_singlethread_workqueue(name
);
849 goto destroy_cmd_wq_exit
;
852 INIT_WORK(&ndev
->tx_work
, nci_tx_work
);
853 snprintf(name
, sizeof(name
), "%s_nci_tx_wq", dev_name(dev
));
854 ndev
->tx_wq
= create_singlethread_workqueue(name
);
857 goto destroy_rx_wq_exit
;
860 skb_queue_head_init(&ndev
->cmd_q
);
861 skb_queue_head_init(&ndev
->rx_q
);
862 skb_queue_head_init(&ndev
->tx_q
);
864 setup_timer(&ndev
->cmd_timer
, nci_cmd_timer
,
865 (unsigned long) ndev
);
866 setup_timer(&ndev
->data_timer
, nci_data_timer
,
867 (unsigned long) ndev
);
869 mutex_init(&ndev
->req_lock
);
871 rc
= nfc_register_device(ndev
->nfc_dev
);
873 goto destroy_rx_wq_exit
;
878 destroy_workqueue(ndev
->rx_wq
);
881 destroy_workqueue(ndev
->cmd_wq
);
886 EXPORT_SYMBOL(nci_register_device
);
889 * nci_unregister_device - unregister a nci device in the nfc subsystem
891 * @dev: The nci device to unregister
893 void nci_unregister_device(struct nci_dev
*ndev
)
895 nci_close_device(ndev
);
897 destroy_workqueue(ndev
->cmd_wq
);
898 destroy_workqueue(ndev
->rx_wq
);
899 destroy_workqueue(ndev
->tx_wq
);
901 nfc_unregister_device(ndev
->nfc_dev
);
903 EXPORT_SYMBOL(nci_unregister_device
);
906 * nci_recv_frame - receive frame from NCI drivers
908 * @ndev: The nci device
909 * @skb: The sk_buff to receive
911 int nci_recv_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
913 pr_debug("len %d\n", skb
->len
);
915 if (!ndev
|| (!test_bit(NCI_UP
, &ndev
->flags
) &&
916 !test_bit(NCI_INIT
, &ndev
->flags
))) {
921 /* Queue frame for rx worker thread */
922 skb_queue_tail(&ndev
->rx_q
, skb
);
923 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
927 EXPORT_SYMBOL(nci_recv_frame
);
929 static int nci_send_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
931 pr_debug("len %d\n", skb
->len
);
938 /* Get rid of skb owner, prior to sending to the driver. */
941 /* Send copy to sniffer */
942 nfc_send_to_raw_sock(ndev
->nfc_dev
, skb
,
943 RAW_PAYLOAD_NCI
, NFC_DIRECTION_TX
);
945 return ndev
->ops
->send(ndev
, skb
);
948 /* Send NCI command */
949 int nci_send_cmd(struct nci_dev
*ndev
, __u16 opcode
, __u8 plen
, void *payload
)
951 struct nci_ctrl_hdr
*hdr
;
954 pr_debug("opcode 0x%x, plen %d\n", opcode
, plen
);
956 skb
= nci_skb_alloc(ndev
, (NCI_CTRL_HDR_SIZE
+ plen
), GFP_KERNEL
);
958 pr_err("no memory for command\n");
962 hdr
= (struct nci_ctrl_hdr
*) skb_put(skb
, NCI_CTRL_HDR_SIZE
);
963 hdr
->gid
= nci_opcode_gid(opcode
);
964 hdr
->oid
= nci_opcode_oid(opcode
);
967 nci_mt_set((__u8
*)hdr
, NCI_MT_CMD_PKT
);
968 nci_pbf_set((__u8
*)hdr
, NCI_PBF_LAST
);
971 memcpy(skb_put(skb
, plen
), payload
, plen
);
973 skb_queue_tail(&ndev
->cmd_q
, skb
);
974 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
979 /* ---- NCI TX Data worker thread ---- */
981 static void nci_tx_work(struct work_struct
*work
)
983 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, tx_work
);
986 pr_debug("credits_cnt %d\n", atomic_read(&ndev
->credits_cnt
));
988 /* Send queued tx data */
989 while (atomic_read(&ndev
->credits_cnt
)) {
990 skb
= skb_dequeue(&ndev
->tx_q
);
994 /* Check if data flow control is used */
995 if (atomic_read(&ndev
->credits_cnt
) !=
996 NCI_DATA_FLOW_CONTROL_NOT_USED
)
997 atomic_dec(&ndev
->credits_cnt
);
999 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1001 nci_conn_id(skb
->data
),
1002 nci_plen(skb
->data
));
1004 nci_send_frame(ndev
, skb
);
1006 mod_timer(&ndev
->data_timer
,
1007 jiffies
+ msecs_to_jiffies(NCI_DATA_TIMEOUT
));
1011 /* ----- NCI RX worker thread (data & control) ----- */
1013 static void nci_rx_work(struct work_struct
*work
)
1015 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, rx_work
);
1016 struct sk_buff
*skb
;
1018 while ((skb
= skb_dequeue(&ndev
->rx_q
))) {
1020 /* Send copy to sniffer */
1021 nfc_send_to_raw_sock(ndev
->nfc_dev
, skb
,
1022 RAW_PAYLOAD_NCI
, NFC_DIRECTION_RX
);
1025 switch (nci_mt(skb
->data
)) {
1026 case NCI_MT_RSP_PKT
:
1027 nci_rsp_packet(ndev
, skb
);
1030 case NCI_MT_NTF_PKT
:
1031 nci_ntf_packet(ndev
, skb
);
1034 case NCI_MT_DATA_PKT
:
1035 nci_rx_data_packet(ndev
, skb
);
1039 pr_err("unknown MT 0x%x\n", nci_mt(skb
->data
));
1045 /* check if a data exchange timout has occurred */
1046 if (test_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
)) {
1047 /* complete the data exchange transaction, if exists */
1048 if (test_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
1049 nci_data_exchange_complete(ndev
, NULL
, -ETIMEDOUT
);
1051 clear_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
);
1055 /* ----- NCI TX CMD worker thread ----- */
1057 static void nci_cmd_work(struct work_struct
*work
)
1059 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, cmd_work
);
1060 struct sk_buff
*skb
;
1062 pr_debug("cmd_cnt %d\n", atomic_read(&ndev
->cmd_cnt
));
1064 /* Send queued command */
1065 if (atomic_read(&ndev
->cmd_cnt
)) {
1066 skb
= skb_dequeue(&ndev
->cmd_q
);
1070 atomic_dec(&ndev
->cmd_cnt
);
1072 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1074 nci_opcode_gid(nci_opcode(skb
->data
)),
1075 nci_opcode_oid(nci_opcode(skb
->data
)),
1076 nci_plen(skb
->data
));
1078 nci_send_frame(ndev
, skb
);
1080 mod_timer(&ndev
->cmd_timer
,
1081 jiffies
+ msecs_to_jiffies(NCI_CMD_TIMEOUT
));
1085 MODULE_LICENSE("GPL");