1 // SPDX-License-Identifier: GPL-2.0-only
3 * The NFC Controller Interface is the communication protocol between an
4 * NFC Controller (NFCC) and a Device Host (DH).
6 * Copyright (C) 2011 Texas Instruments, Inc.
7 * Copyright (C) 2014 Marvell International Ltd.
9 * Written by Ilan Elias <ilane@ti.com>
12 * This file is based on hci_core.c, which was written
13 * by Maxim Krasnyansky.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/workqueue.h>
22 #include <linux/completion.h>
23 #include <linux/export.h>
24 #include <linux/sched.h>
25 #include <linux/bitops.h>
26 #include <linux/skbuff.h>
27 #include <linux/kcov.h>
30 #include <net/nfc/nci.h>
31 #include <net/nfc/nci_core.h>
32 #include <linux/nfc.h>
34 struct core_conn_create_data
{
36 struct nci_core_conn_create_cmd
*cmd
;
39 static void nci_cmd_work(struct work_struct
*work
);
40 static void nci_rx_work(struct work_struct
*work
);
41 static void nci_tx_work(struct work_struct
*work
);
43 struct nci_conn_info
*nci_get_conn_info_by_conn_id(struct nci_dev
*ndev
,
46 struct nci_conn_info
*conn_info
;
48 list_for_each_entry(conn_info
, &ndev
->conn_info_list
, list
) {
49 if (conn_info
->conn_id
== conn_id
)
56 int nci_get_conn_info_by_dest_type_params(struct nci_dev
*ndev
, u8 dest_type
,
57 const struct dest_spec_params
*params
)
59 const struct nci_conn_info
*conn_info
;
61 list_for_each_entry(conn_info
, &ndev
->conn_info_list
, list
) {
62 if (conn_info
->dest_type
== dest_type
) {
64 return conn_info
->conn_id
;
66 if (params
->id
== conn_info
->dest_params
->id
&&
67 params
->protocol
== conn_info
->dest_params
->protocol
)
68 return conn_info
->conn_id
;
74 EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params
);
76 /* ---- NCI requests ---- */
78 void nci_req_complete(struct nci_dev
*ndev
, int result
)
80 if (ndev
->req_status
== NCI_REQ_PEND
) {
81 ndev
->req_result
= result
;
82 ndev
->req_status
= NCI_REQ_DONE
;
83 complete(&ndev
->req_completion
);
86 EXPORT_SYMBOL(nci_req_complete
);
88 static void nci_req_cancel(struct nci_dev
*ndev
, int err
)
90 if (ndev
->req_status
== NCI_REQ_PEND
) {
91 ndev
->req_result
= err
;
92 ndev
->req_status
= NCI_REQ_CANCELED
;
93 complete(&ndev
->req_completion
);
97 /* Execute request and wait for completion. */
98 static int __nci_request(struct nci_dev
*ndev
,
99 void (*req
)(struct nci_dev
*ndev
, const void *opt
),
100 const void *opt
, __u32 timeout
)
105 ndev
->req_status
= NCI_REQ_PEND
;
107 reinit_completion(&ndev
->req_completion
);
110 wait_for_completion_interruptible_timeout(&ndev
->req_completion
,
113 pr_debug("wait_for_completion return %ld\n", completion_rc
);
115 if (completion_rc
> 0) {
116 switch (ndev
->req_status
) {
118 rc
= nci_to_errno(ndev
->req_result
);
121 case NCI_REQ_CANCELED
:
122 rc
= -ndev
->req_result
;
130 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
133 rc
= ((completion_rc
== 0) ? (-ETIMEDOUT
) : (completion_rc
));
136 ndev
->req_status
= ndev
->req_result
= 0;
141 inline int nci_request(struct nci_dev
*ndev
,
142 void (*req
)(struct nci_dev
*ndev
,
144 const void *opt
, __u32 timeout
)
148 /* Serialize all requests */
149 mutex_lock(&ndev
->req_lock
);
150 /* check the state after obtaing the lock against any races
151 * from nci_close_device when the device gets removed.
153 if (test_bit(NCI_UP
, &ndev
->flags
))
154 rc
= __nci_request(ndev
, req
, opt
, timeout
);
157 mutex_unlock(&ndev
->req_lock
);
162 static void nci_reset_req(struct nci_dev
*ndev
, const void *opt
)
164 struct nci_core_reset_cmd cmd
;
166 cmd
.reset_type
= NCI_RESET_TYPE_RESET_CONFIG
;
167 nci_send_cmd(ndev
, NCI_OP_CORE_RESET_CMD
, 1, &cmd
);
170 static void nci_init_req(struct nci_dev
*ndev
, const void *opt
)
175 plen
= sizeof(struct nci_core_init_v2_cmd
);
177 nci_send_cmd(ndev
, NCI_OP_CORE_INIT_CMD
, plen
, opt
);
180 static void nci_init_complete_req(struct nci_dev
*ndev
, const void *opt
)
182 struct nci_rf_disc_map_cmd cmd
;
183 struct disc_map_config
*cfg
= cmd
.mapping_configs
;
184 __u8
*num
= &cmd
.num_mapping_configs
;
187 /* set rf mapping configurations */
190 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
191 for (i
= 0; i
< ndev
->num_supported_rf_interfaces
; i
++) {
192 if (ndev
->supported_rf_interfaces
[i
] ==
193 NCI_RF_INTERFACE_ISO_DEP
) {
194 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
195 cfg
[*num
].mode
= NCI_DISC_MAP_MODE_POLL
|
196 NCI_DISC_MAP_MODE_LISTEN
;
197 cfg
[*num
].rf_interface
= NCI_RF_INTERFACE_ISO_DEP
;
199 } else if (ndev
->supported_rf_interfaces
[i
] ==
200 NCI_RF_INTERFACE_NFC_DEP
) {
201 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
202 cfg
[*num
].mode
= NCI_DISC_MAP_MODE_POLL
|
203 NCI_DISC_MAP_MODE_LISTEN
;
204 cfg
[*num
].rf_interface
= NCI_RF_INTERFACE_NFC_DEP
;
208 if (*num
== NCI_MAX_NUM_MAPPING_CONFIGS
)
212 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_MAP_CMD
,
213 (1 + ((*num
) * sizeof(struct disc_map_config
))), &cmd
);
216 struct nci_set_config_param
{
222 static void nci_set_config_req(struct nci_dev
*ndev
, const void *opt
)
224 const struct nci_set_config_param
*param
= opt
;
225 struct nci_core_set_config_cmd cmd
;
227 BUG_ON(param
->len
> NCI_MAX_PARAM_LEN
);
230 cmd
.param
.id
= param
->id
;
231 cmd
.param
.len
= param
->len
;
232 memcpy(cmd
.param
.val
, param
->val
, param
->len
);
234 nci_send_cmd(ndev
, NCI_OP_CORE_SET_CONFIG_CMD
, (3 + param
->len
), &cmd
);
237 struct nci_rf_discover_param
{
242 static void nci_rf_discover_req(struct nci_dev
*ndev
, const void *opt
)
244 const struct nci_rf_discover_param
*param
= opt
;
245 struct nci_rf_disc_cmd cmd
;
247 cmd
.num_disc_configs
= 0;
249 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
250 (param
->im_protocols
& NFC_PROTO_JEWEL_MASK
||
251 param
->im_protocols
& NFC_PROTO_MIFARE_MASK
||
252 param
->im_protocols
& NFC_PROTO_ISO14443_MASK
||
253 param
->im_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
254 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
255 NCI_NFC_A_PASSIVE_POLL_MODE
;
256 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
257 cmd
.num_disc_configs
++;
260 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
261 (param
->im_protocols
& NFC_PROTO_ISO14443_B_MASK
)) {
262 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
263 NCI_NFC_B_PASSIVE_POLL_MODE
;
264 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
265 cmd
.num_disc_configs
++;
268 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
269 (param
->im_protocols
& NFC_PROTO_FELICA_MASK
||
270 param
->im_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
271 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
272 NCI_NFC_F_PASSIVE_POLL_MODE
;
273 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
274 cmd
.num_disc_configs
++;
277 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
278 (param
->im_protocols
& NFC_PROTO_ISO15693_MASK
)) {
279 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
280 NCI_NFC_V_PASSIVE_POLL_MODE
;
281 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
282 cmd
.num_disc_configs
++;
285 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
- 1) &&
286 (param
->tm_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
287 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
288 NCI_NFC_A_PASSIVE_LISTEN_MODE
;
289 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
290 cmd
.num_disc_configs
++;
291 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
292 NCI_NFC_F_PASSIVE_LISTEN_MODE
;
293 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
294 cmd
.num_disc_configs
++;
297 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_CMD
,
298 (1 + (cmd
.num_disc_configs
* sizeof(struct disc_config
))),
302 struct nci_rf_discover_select_param
{
303 __u8 rf_discovery_id
;
307 static void nci_rf_discover_select_req(struct nci_dev
*ndev
, const void *opt
)
309 const struct nci_rf_discover_select_param
*param
= opt
;
310 struct nci_rf_discover_select_cmd cmd
;
312 cmd
.rf_discovery_id
= param
->rf_discovery_id
;
313 cmd
.rf_protocol
= param
->rf_protocol
;
315 switch (cmd
.rf_protocol
) {
316 case NCI_RF_PROTOCOL_ISO_DEP
:
317 cmd
.rf_interface
= NCI_RF_INTERFACE_ISO_DEP
;
320 case NCI_RF_PROTOCOL_NFC_DEP
:
321 cmd
.rf_interface
= NCI_RF_INTERFACE_NFC_DEP
;
325 cmd
.rf_interface
= NCI_RF_INTERFACE_FRAME
;
329 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_SELECT_CMD
,
330 sizeof(struct nci_rf_discover_select_cmd
), &cmd
);
333 static void nci_rf_deactivate_req(struct nci_dev
*ndev
, const void *opt
)
335 struct nci_rf_deactivate_cmd cmd
;
337 cmd
.type
= (unsigned long)opt
;
339 nci_send_cmd(ndev
, NCI_OP_RF_DEACTIVATE_CMD
,
340 sizeof(struct nci_rf_deactivate_cmd
), &cmd
);
343 struct nci_cmd_param
{
349 static void nci_generic_req(struct nci_dev
*ndev
, const void *opt
)
351 const struct nci_cmd_param
*param
= opt
;
353 nci_send_cmd(ndev
, param
->opcode
, param
->len
, param
->payload
);
356 int nci_prop_cmd(struct nci_dev
*ndev
, __u8 oid
, size_t len
, const __u8
*payload
)
358 struct nci_cmd_param param
;
360 param
.opcode
= nci_opcode_pack(NCI_GID_PROPRIETARY
, oid
);
362 param
.payload
= payload
;
364 return __nci_request(ndev
, nci_generic_req
, ¶m
,
365 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
367 EXPORT_SYMBOL(nci_prop_cmd
);
369 int nci_core_cmd(struct nci_dev
*ndev
, __u16 opcode
, size_t len
,
372 struct nci_cmd_param param
;
374 param
.opcode
= opcode
;
376 param
.payload
= payload
;
378 return __nci_request(ndev
, nci_generic_req
, ¶m
,
379 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
381 EXPORT_SYMBOL(nci_core_cmd
);
383 int nci_core_reset(struct nci_dev
*ndev
)
385 return __nci_request(ndev
, nci_reset_req
, (void *)0,
386 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
388 EXPORT_SYMBOL(nci_core_reset
);
390 int nci_core_init(struct nci_dev
*ndev
)
392 return __nci_request(ndev
, nci_init_req
, (void *)0,
393 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
395 EXPORT_SYMBOL(nci_core_init
);
397 struct nci_loopback_data
{
399 struct sk_buff
*data
;
402 static void nci_send_data_req(struct nci_dev
*ndev
, const void *opt
)
404 const struct nci_loopback_data
*data
= opt
;
406 nci_send_data(ndev
, data
->conn_id
, data
->data
);
409 static void nci_nfcc_loopback_cb(void *context
, struct sk_buff
*skb
, int err
)
411 struct nci_dev
*ndev
= (struct nci_dev
*)context
;
412 struct nci_conn_info
*conn_info
;
414 conn_info
= nci_get_conn_info_by_conn_id(ndev
, ndev
->cur_conn_id
);
416 nci_req_complete(ndev
, NCI_STATUS_REJECTED
);
420 conn_info
->rx_skb
= skb
;
422 nci_req_complete(ndev
, NCI_STATUS_OK
);
425 int nci_nfcc_loopback(struct nci_dev
*ndev
, const void *data
, size_t data_len
,
426 struct sk_buff
**resp
)
429 struct nci_loopback_data loopback_data
;
430 struct nci_conn_info
*conn_info
;
432 int conn_id
= nci_get_conn_info_by_dest_type_params(ndev
,
433 NCI_DESTINATION_NFCC_LOOPBACK
, NULL
);
436 r
= nci_core_conn_create(ndev
, NCI_DESTINATION_NFCC_LOOPBACK
,
438 if (r
!= NCI_STATUS_OK
)
441 conn_id
= nci_get_conn_info_by_dest_type_params(ndev
,
442 NCI_DESTINATION_NFCC_LOOPBACK
,
446 conn_info
= nci_get_conn_info_by_conn_id(ndev
, conn_id
);
450 /* store cb and context to be used on receiving data */
451 conn_info
->data_exchange_cb
= nci_nfcc_loopback_cb
;
452 conn_info
->data_exchange_cb_context
= ndev
;
454 skb
= nci_skb_alloc(ndev
, NCI_DATA_HDR_SIZE
+ data_len
, GFP_KERNEL
);
458 skb_reserve(skb
, NCI_DATA_HDR_SIZE
);
459 skb_put_data(skb
, data
, data_len
);
461 loopback_data
.conn_id
= conn_id
;
462 loopback_data
.data
= skb
;
464 ndev
->cur_conn_id
= conn_id
;
465 r
= nci_request(ndev
, nci_send_data_req
, &loopback_data
,
466 msecs_to_jiffies(NCI_DATA_TIMEOUT
));
467 if (r
== NCI_STATUS_OK
&& resp
)
468 *resp
= conn_info
->rx_skb
;
472 EXPORT_SYMBOL(nci_nfcc_loopback
);
474 static int nci_open_device(struct nci_dev
*ndev
)
478 mutex_lock(&ndev
->req_lock
);
480 if (test_bit(NCI_UNREG
, &ndev
->flags
)) {
485 if (test_bit(NCI_UP
, &ndev
->flags
)) {
490 if (ndev
->ops
->open(ndev
)) {
495 atomic_set(&ndev
->cmd_cnt
, 1);
497 set_bit(NCI_INIT
, &ndev
->flags
);
500 rc
= ndev
->ops
->init(ndev
);
503 rc
= __nci_request(ndev
, nci_reset_req
, (void *)0,
504 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
507 if (!rc
&& ndev
->ops
->setup
) {
508 rc
= ndev
->ops
->setup(ndev
);
512 struct nci_core_init_v2_cmd nci_init_v2_cmd
= {
513 .feature1
= NCI_FEATURE_DISABLE
,
514 .feature2
= NCI_FEATURE_DISABLE
516 const void *opt
= NULL
;
518 if (ndev
->nci_ver
& NCI_VER_2_MASK
)
519 opt
= &nci_init_v2_cmd
;
521 rc
= __nci_request(ndev
, nci_init_req
, opt
,
522 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
525 if (!rc
&& ndev
->ops
->post_setup
)
526 rc
= ndev
->ops
->post_setup(ndev
);
529 rc
= __nci_request(ndev
, nci_init_complete_req
, (void *)0,
530 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
533 clear_bit(NCI_INIT
, &ndev
->flags
);
536 set_bit(NCI_UP
, &ndev
->flags
);
537 nci_clear_target_list(ndev
);
538 atomic_set(&ndev
->state
, NCI_IDLE
);
540 /* Init failed, cleanup */
541 skb_queue_purge(&ndev
->cmd_q
);
542 skb_queue_purge(&ndev
->rx_q
);
543 skb_queue_purge(&ndev
->tx_q
);
545 ndev
->ops
->close(ndev
);
546 ndev
->flags
&= BIT(NCI_UNREG
);
550 mutex_unlock(&ndev
->req_lock
);
554 static int nci_close_device(struct nci_dev
*ndev
)
556 nci_req_cancel(ndev
, ENODEV
);
558 /* This mutex needs to be held as a barrier for
559 * caller nci_unregister_device
561 mutex_lock(&ndev
->req_lock
);
563 if (!test_and_clear_bit(NCI_UP
, &ndev
->flags
)) {
564 /* Need to flush the cmd wq in case
565 * there is a queued/running cmd_work
567 flush_workqueue(ndev
->cmd_wq
);
568 del_timer_sync(&ndev
->cmd_timer
);
569 del_timer_sync(&ndev
->data_timer
);
570 mutex_unlock(&ndev
->req_lock
);
574 /* Drop RX and TX queues */
575 skb_queue_purge(&ndev
->rx_q
);
576 skb_queue_purge(&ndev
->tx_q
);
578 /* Flush RX and TX wq */
579 flush_workqueue(ndev
->rx_wq
);
580 flush_workqueue(ndev
->tx_wq
);
583 skb_queue_purge(&ndev
->cmd_q
);
584 atomic_set(&ndev
->cmd_cnt
, 1);
586 set_bit(NCI_INIT
, &ndev
->flags
);
587 __nci_request(ndev
, nci_reset_req
, (void *)0,
588 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
590 /* After this point our queues are empty
591 * and no works are scheduled.
593 ndev
->ops
->close(ndev
);
595 clear_bit(NCI_INIT
, &ndev
->flags
);
598 flush_workqueue(ndev
->cmd_wq
);
600 del_timer_sync(&ndev
->cmd_timer
);
602 /* Clear flags except NCI_UNREG */
603 ndev
->flags
&= BIT(NCI_UNREG
);
605 mutex_unlock(&ndev
->req_lock
);
610 /* NCI command timer function */
611 static void nci_cmd_timer(struct timer_list
*t
)
613 struct nci_dev
*ndev
= from_timer(ndev
, t
, cmd_timer
);
615 atomic_set(&ndev
->cmd_cnt
, 1);
616 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
619 /* NCI data exchange timer function */
620 static void nci_data_timer(struct timer_list
*t
)
622 struct nci_dev
*ndev
= from_timer(ndev
, t
, data_timer
);
624 set_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
);
625 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
628 static int nci_dev_up(struct nfc_dev
*nfc_dev
)
630 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
632 return nci_open_device(ndev
);
635 static int nci_dev_down(struct nfc_dev
*nfc_dev
)
637 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
639 return nci_close_device(ndev
);
642 int nci_set_config(struct nci_dev
*ndev
, __u8 id
, size_t len
, const __u8
*val
)
644 struct nci_set_config_param param
;
653 return __nci_request(ndev
, nci_set_config_req
, ¶m
,
654 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
656 EXPORT_SYMBOL(nci_set_config
);
658 static void nci_nfcee_discover_req(struct nci_dev
*ndev
, const void *opt
)
660 struct nci_nfcee_discover_cmd cmd
;
661 __u8 action
= (unsigned long)opt
;
663 cmd
.discovery_action
= action
;
665 nci_send_cmd(ndev
, NCI_OP_NFCEE_DISCOVER_CMD
, 1, &cmd
);
668 int nci_nfcee_discover(struct nci_dev
*ndev
, u8 action
)
670 unsigned long opt
= action
;
672 return __nci_request(ndev
, nci_nfcee_discover_req
, (void *)opt
,
673 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
675 EXPORT_SYMBOL(nci_nfcee_discover
);
677 static void nci_nfcee_mode_set_req(struct nci_dev
*ndev
, const void *opt
)
679 const struct nci_nfcee_mode_set_cmd
*cmd
= opt
;
681 nci_send_cmd(ndev
, NCI_OP_NFCEE_MODE_SET_CMD
,
682 sizeof(struct nci_nfcee_mode_set_cmd
), cmd
);
685 int nci_nfcee_mode_set(struct nci_dev
*ndev
, u8 nfcee_id
, u8 nfcee_mode
)
687 struct nci_nfcee_mode_set_cmd cmd
;
689 cmd
.nfcee_id
= nfcee_id
;
690 cmd
.nfcee_mode
= nfcee_mode
;
692 return __nci_request(ndev
, nci_nfcee_mode_set_req
, &cmd
,
693 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
695 EXPORT_SYMBOL(nci_nfcee_mode_set
);
697 static void nci_core_conn_create_req(struct nci_dev
*ndev
, const void *opt
)
699 const struct core_conn_create_data
*data
= opt
;
701 nci_send_cmd(ndev
, NCI_OP_CORE_CONN_CREATE_CMD
, data
->length
, data
->cmd
);
704 int nci_core_conn_create(struct nci_dev
*ndev
, u8 destination_type
,
705 u8 number_destination_params
,
707 const struct core_conn_create_dest_spec_params
*params
)
710 struct nci_core_conn_create_cmd
*cmd
;
711 struct core_conn_create_data data
;
713 data
.length
= params_len
+ sizeof(struct nci_core_conn_create_cmd
);
714 cmd
= kzalloc(data
.length
, GFP_KERNEL
);
718 cmd
->destination_type
= destination_type
;
719 cmd
->number_destination_params
= number_destination_params
;
724 memcpy(cmd
->params
, params
, params_len
);
725 if (params
->length
> 0)
726 memcpy(&ndev
->cur_params
,
727 ¶ms
->value
[DEST_SPEC_PARAMS_ID_INDEX
],
728 sizeof(struct dest_spec_params
));
730 ndev
->cur_params
.id
= 0;
732 ndev
->cur_params
.id
= 0;
734 ndev
->cur_dest_type
= destination_type
;
736 r
= __nci_request(ndev
, nci_core_conn_create_req
, &data
,
737 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
741 EXPORT_SYMBOL(nci_core_conn_create
);
743 static void nci_core_conn_close_req(struct nci_dev
*ndev
, const void *opt
)
745 __u8 conn_id
= (unsigned long)opt
;
747 nci_send_cmd(ndev
, NCI_OP_CORE_CONN_CLOSE_CMD
, 1, &conn_id
);
750 int nci_core_conn_close(struct nci_dev
*ndev
, u8 conn_id
)
752 unsigned long opt
= conn_id
;
754 ndev
->cur_conn_id
= conn_id
;
755 return __nci_request(ndev
, nci_core_conn_close_req
, (void *)opt
,
756 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
758 EXPORT_SYMBOL(nci_core_conn_close
);
760 static void nci_set_target_ats(struct nfc_target
*target
, struct nci_dev
*ndev
)
762 if (ndev
->target_ats_len
> 0) {
763 target
->ats_len
= ndev
->target_ats_len
;
764 memcpy(target
->ats
, ndev
->target_ats
, target
->ats_len
);
768 static int nci_set_local_general_bytes(struct nfc_dev
*nfc_dev
)
770 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
771 struct nci_set_config_param param
;
774 param
.val
= nfc_get_local_general_bytes(nfc_dev
, ¶m
.len
);
775 if ((param
.val
== NULL
) || (param
.len
== 0))
778 if (param
.len
> NFC_MAX_GT_LEN
)
781 param
.id
= NCI_PN_ATR_REQ_GEN_BYTES
;
783 rc
= nci_request(ndev
, nci_set_config_req
, ¶m
,
784 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
788 param
.id
= NCI_LN_ATR_RES_GEN_BYTES
;
790 return nci_request(ndev
, nci_set_config_req
, ¶m
,
791 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
794 static int nci_set_listen_parameters(struct nfc_dev
*nfc_dev
)
796 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
800 val
= NCI_LA_SEL_INFO_NFC_DEP_MASK
;
802 rc
= nci_set_config(ndev
, NCI_LA_SEL_INFO
, 1, &val
);
806 val
= NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK
;
808 rc
= nci_set_config(ndev
, NCI_LF_PROTOCOL_TYPE
, 1, &val
);
812 val
= NCI_LF_CON_BITR_F_212
| NCI_LF_CON_BITR_F_424
;
814 return nci_set_config(ndev
, NCI_LF_CON_BITR_F
, 1, &val
);
817 static int nci_start_poll(struct nfc_dev
*nfc_dev
,
818 __u32 im_protocols
, __u32 tm_protocols
)
820 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
821 struct nci_rf_discover_param param
;
824 if ((atomic_read(&ndev
->state
) == NCI_DISCOVERY
) ||
825 (atomic_read(&ndev
->state
) == NCI_W4_ALL_DISCOVERIES
)) {
826 pr_err("unable to start poll, since poll is already active\n");
830 if (ndev
->target_active_prot
) {
831 pr_err("there is an active target\n");
835 if ((atomic_read(&ndev
->state
) == NCI_W4_HOST_SELECT
) ||
836 (atomic_read(&ndev
->state
) == NCI_POLL_ACTIVE
)) {
837 pr_debug("target active or w4 select, implicitly deactivate\n");
839 rc
= nci_request(ndev
, nci_rf_deactivate_req
,
840 (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE
,
841 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
846 if ((im_protocols
| tm_protocols
) & NFC_PROTO_NFC_DEP_MASK
) {
847 rc
= nci_set_local_general_bytes(nfc_dev
);
849 pr_err("failed to set local general bytes\n");
854 if (tm_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
855 rc
= nci_set_listen_parameters(nfc_dev
);
857 pr_err("failed to set listen parameters\n");
860 param
.im_protocols
= im_protocols
;
861 param
.tm_protocols
= tm_protocols
;
862 rc
= nci_request(ndev
, nci_rf_discover_req
, ¶m
,
863 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT
));
866 ndev
->poll_prots
= im_protocols
;
871 static void nci_stop_poll(struct nfc_dev
*nfc_dev
)
873 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
875 if ((atomic_read(&ndev
->state
) != NCI_DISCOVERY
) &&
876 (atomic_read(&ndev
->state
) != NCI_W4_ALL_DISCOVERIES
)) {
877 pr_err("unable to stop poll, since poll is not active\n");
881 nci_request(ndev
, nci_rf_deactivate_req
,
882 (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE
,
883 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
886 static int nci_activate_target(struct nfc_dev
*nfc_dev
,
887 struct nfc_target
*target
, __u32 protocol
)
889 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
890 struct nci_rf_discover_select_param param
;
891 const struct nfc_target
*nci_target
= NULL
;
895 pr_debug("target_idx %d, protocol 0x%x\n", target
->idx
, protocol
);
897 if ((atomic_read(&ndev
->state
) != NCI_W4_HOST_SELECT
) &&
898 (atomic_read(&ndev
->state
) != NCI_POLL_ACTIVE
)) {
899 pr_err("there is no available target to activate\n");
903 if (ndev
->target_active_prot
) {
904 pr_err("there is already an active target\n");
908 for (i
= 0; i
< ndev
->n_targets
; i
++) {
909 if (ndev
->targets
[i
].idx
== target
->idx
) {
910 nci_target
= &ndev
->targets
[i
];
916 pr_err("unable to find the selected target\n");
920 if (protocol
>= NFC_PROTO_MAX
) {
921 pr_err("the requested nfc protocol is invalid\n");
925 if (!(nci_target
->supported_protocols
& (1 << protocol
))) {
926 pr_err("target does not support the requested protocol 0x%x\n",
931 if (atomic_read(&ndev
->state
) == NCI_W4_HOST_SELECT
) {
932 param
.rf_discovery_id
= nci_target
->logical_idx
;
934 if (protocol
== NFC_PROTO_JEWEL
)
935 param
.rf_protocol
= NCI_RF_PROTOCOL_T1T
;
936 else if (protocol
== NFC_PROTO_MIFARE
)
937 param
.rf_protocol
= NCI_RF_PROTOCOL_T2T
;
938 else if (protocol
== NFC_PROTO_FELICA
)
939 param
.rf_protocol
= NCI_RF_PROTOCOL_T3T
;
940 else if (protocol
== NFC_PROTO_ISO14443
||
941 protocol
== NFC_PROTO_ISO14443_B
)
942 param
.rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
944 param
.rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
946 rc
= nci_request(ndev
, nci_rf_discover_select_req
, ¶m
,
947 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT
));
951 ndev
->target_active_prot
= protocol
;
952 if (protocol
== NFC_PROTO_ISO14443
)
953 nci_set_target_ats(target
, ndev
);
959 static void nci_deactivate_target(struct nfc_dev
*nfc_dev
,
960 struct nfc_target
*target
,
963 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
964 unsigned long nci_mode
= NCI_DEACTIVATE_TYPE_IDLE_MODE
;
966 if (!ndev
->target_active_prot
) {
967 pr_err("unable to deactivate target, no active target\n");
971 ndev
->target_active_prot
= 0;
974 case NFC_TARGET_MODE_SLEEP
:
975 nci_mode
= NCI_DEACTIVATE_TYPE_SLEEP_MODE
;
979 if (atomic_read(&ndev
->state
) == NCI_POLL_ACTIVE
) {
980 nci_request(ndev
, nci_rf_deactivate_req
, (void *)nci_mode
,
981 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
985 static int nci_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
986 __u8 comm_mode
, __u8
*gb
, size_t gb_len
)
988 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
991 pr_debug("target_idx %d, comm_mode %d\n", target
->idx
, comm_mode
);
993 rc
= nci_activate_target(nfc_dev
, target
, NFC_PROTO_NFC_DEP
);
997 rc
= nfc_set_remote_general_bytes(nfc_dev
, ndev
->remote_gb
,
998 ndev
->remote_gb_len
);
1000 rc
= nfc_dep_link_is_up(nfc_dev
, target
->idx
, NFC_COMM_PASSIVE
,
1006 static int nci_dep_link_down(struct nfc_dev
*nfc_dev
)
1008 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1011 if (nfc_dev
->rf_mode
== NFC_RF_INITIATOR
) {
1012 nci_deactivate_target(nfc_dev
, NULL
, NCI_DEACTIVATE_TYPE_IDLE_MODE
);
1014 if (atomic_read(&ndev
->state
) == NCI_LISTEN_ACTIVE
||
1015 atomic_read(&ndev
->state
) == NCI_DISCOVERY
) {
1016 nci_request(ndev
, nci_rf_deactivate_req
, (void *)0,
1017 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
1020 rc
= nfc_tm_deactivated(nfc_dev
);
1022 pr_err("error when signaling tm deactivation\n");
1029 static int nci_transceive(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
1030 struct sk_buff
*skb
,
1031 data_exchange_cb_t cb
, void *cb_context
)
1033 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1035 struct nci_conn_info
*conn_info
;
1037 conn_info
= ndev
->rf_conn_info
;
1041 pr_debug("target_idx %d, len %d\n", target
->idx
, skb
->len
);
1043 if (!ndev
->target_active_prot
) {
1044 pr_err("unable to exchange data, no active target\n");
1048 if (test_and_set_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
1051 /* store cb and context to be used on receiving data */
1052 conn_info
->data_exchange_cb
= cb
;
1053 conn_info
->data_exchange_cb_context
= cb_context
;
1055 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
1057 clear_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
);
1062 static int nci_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
1064 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1067 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
1069 pr_err("unable to send data\n");
1074 static int nci_enable_se(struct nfc_dev
*nfc_dev
, u32 se_idx
)
1076 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1078 if (ndev
->ops
->enable_se
)
1079 return ndev
->ops
->enable_se(ndev
, se_idx
);
1084 static int nci_disable_se(struct nfc_dev
*nfc_dev
, u32 se_idx
)
1086 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1088 if (ndev
->ops
->disable_se
)
1089 return ndev
->ops
->disable_se(ndev
, se_idx
);
1094 static int nci_discover_se(struct nfc_dev
*nfc_dev
)
1097 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1099 if (ndev
->ops
->discover_se
) {
1100 r
= nci_nfcee_discover(ndev
, NCI_NFCEE_DISCOVERY_ACTION_ENABLE
);
1101 if (r
!= NCI_STATUS_OK
)
1104 return ndev
->ops
->discover_se(ndev
);
1110 static int nci_se_io(struct nfc_dev
*nfc_dev
, u32 se_idx
,
1111 u8
*apdu
, size_t apdu_length
,
1112 se_io_cb_t cb
, void *cb_context
)
1114 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1116 if (ndev
->ops
->se_io
)
1117 return ndev
->ops
->se_io(ndev
, se_idx
, apdu
,
1118 apdu_length
, cb
, cb_context
);
1123 static int nci_fw_download(struct nfc_dev
*nfc_dev
, const char *firmware_name
)
1125 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1127 if (!ndev
->ops
->fw_download
)
1130 return ndev
->ops
->fw_download(ndev
, firmware_name
);
1133 static const struct nfc_ops nci_nfc_ops
= {
1134 .dev_up
= nci_dev_up
,
1135 .dev_down
= nci_dev_down
,
1136 .start_poll
= nci_start_poll
,
1137 .stop_poll
= nci_stop_poll
,
1138 .dep_link_up
= nci_dep_link_up
,
1139 .dep_link_down
= nci_dep_link_down
,
1140 .activate_target
= nci_activate_target
,
1141 .deactivate_target
= nci_deactivate_target
,
1142 .im_transceive
= nci_transceive
,
1143 .tm_send
= nci_tm_send
,
1144 .enable_se
= nci_enable_se
,
1145 .disable_se
= nci_disable_se
,
1146 .discover_se
= nci_discover_se
,
1148 .fw_download
= nci_fw_download
,
1151 /* ---- Interface to NCI drivers ---- */
1153 * nci_allocate_device - allocate a new nci device
1155 * @ops: device operations
1156 * @supported_protocols: NFC protocols supported by the device
1157 * @tx_headroom: Reserved space at beginning of skb
1158 * @tx_tailroom: Reserved space at end of skb
1160 struct nci_dev
*nci_allocate_device(const struct nci_ops
*ops
,
1161 __u32 supported_protocols
,
1162 int tx_headroom
, int tx_tailroom
)
1164 struct nci_dev
*ndev
;
1166 pr_debug("supported_protocols 0x%x\n", supported_protocols
);
1168 if (!ops
->open
|| !ops
->close
|| !ops
->send
)
1171 if (!supported_protocols
)
1174 ndev
= kzalloc(sizeof(struct nci_dev
), GFP_KERNEL
);
1180 if (ops
->n_prop_ops
> NCI_MAX_PROPRIETARY_CMD
) {
1181 pr_err("Too many proprietary commands: %zd\n",
1186 ndev
->tx_headroom
= tx_headroom
;
1187 ndev
->tx_tailroom
= tx_tailroom
;
1188 init_completion(&ndev
->req_completion
);
1190 ndev
->nfc_dev
= nfc_allocate_device(&nci_nfc_ops
,
1191 supported_protocols
,
1192 tx_headroom
+ NCI_DATA_HDR_SIZE
,
1197 ndev
->hci_dev
= nci_hci_allocate(ndev
);
1201 nfc_set_drvdata(ndev
->nfc_dev
, ndev
);
1206 nfc_free_device(ndev
->nfc_dev
);
1211 EXPORT_SYMBOL(nci_allocate_device
);
1214 * nci_free_device - deallocate nci device
1216 * @ndev: The nci device to deallocate
1218 void nci_free_device(struct nci_dev
*ndev
)
1220 nfc_free_device(ndev
->nfc_dev
);
1221 nci_hci_deallocate(ndev
);
1223 /* drop partial rx data packet if present */
1224 if (ndev
->rx_data_reassembly
)
1225 kfree_skb(ndev
->rx_data_reassembly
);
1228 EXPORT_SYMBOL(nci_free_device
);
1231 * nci_register_device - register a nci device in the nfc subsystem
1233 * @ndev: The nci device to register
1235 int nci_register_device(struct nci_dev
*ndev
)
1238 struct device
*dev
= &ndev
->nfc_dev
->dev
;
1243 INIT_WORK(&ndev
->cmd_work
, nci_cmd_work
);
1244 snprintf(name
, sizeof(name
), "%s_nci_cmd_wq", dev_name(dev
));
1245 ndev
->cmd_wq
= create_singlethread_workqueue(name
);
1246 if (!ndev
->cmd_wq
) {
1251 INIT_WORK(&ndev
->rx_work
, nci_rx_work
);
1252 snprintf(name
, sizeof(name
), "%s_nci_rx_wq", dev_name(dev
));
1253 ndev
->rx_wq
= create_singlethread_workqueue(name
);
1256 goto destroy_cmd_wq_exit
;
1259 INIT_WORK(&ndev
->tx_work
, nci_tx_work
);
1260 snprintf(name
, sizeof(name
), "%s_nci_tx_wq", dev_name(dev
));
1261 ndev
->tx_wq
= create_singlethread_workqueue(name
);
1264 goto destroy_rx_wq_exit
;
1267 skb_queue_head_init(&ndev
->cmd_q
);
1268 skb_queue_head_init(&ndev
->rx_q
);
1269 skb_queue_head_init(&ndev
->tx_q
);
1271 timer_setup(&ndev
->cmd_timer
, nci_cmd_timer
, 0);
1272 timer_setup(&ndev
->data_timer
, nci_data_timer
, 0);
1274 mutex_init(&ndev
->req_lock
);
1275 INIT_LIST_HEAD(&ndev
->conn_info_list
);
1277 rc
= nfc_register_device(ndev
->nfc_dev
);
1279 goto destroy_tx_wq_exit
;
1284 destroy_workqueue(ndev
->tx_wq
);
1287 destroy_workqueue(ndev
->rx_wq
);
1289 destroy_cmd_wq_exit
:
1290 destroy_workqueue(ndev
->cmd_wq
);
1295 EXPORT_SYMBOL(nci_register_device
);
1298 * nci_unregister_device - unregister a nci device in the nfc subsystem
1300 * @ndev: The nci device to unregister
1302 void nci_unregister_device(struct nci_dev
*ndev
)
1304 struct nci_conn_info
*conn_info
, *n
;
1306 /* This set_bit is not protected with specialized barrier,
1307 * However, it is fine because the mutex_lock(&ndev->req_lock);
1308 * in nci_close_device() will help to emit one.
1310 set_bit(NCI_UNREG
, &ndev
->flags
);
1312 nci_close_device(ndev
);
1314 destroy_workqueue(ndev
->cmd_wq
);
1315 destroy_workqueue(ndev
->rx_wq
);
1316 destroy_workqueue(ndev
->tx_wq
);
1318 list_for_each_entry_safe(conn_info
, n
, &ndev
->conn_info_list
, list
) {
1319 list_del(&conn_info
->list
);
1320 /* conn_info is allocated with devm_kzalloc */
1323 nfc_unregister_device(ndev
->nfc_dev
);
1325 EXPORT_SYMBOL(nci_unregister_device
);
1328 * nci_recv_frame - receive frame from NCI drivers
1330 * @ndev: The nci device
1331 * @skb: The sk_buff to receive
1333 int nci_recv_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
1335 pr_debug("len %d\n", skb
->len
);
1337 if (!ndev
|| (!test_bit(NCI_UP
, &ndev
->flags
) &&
1338 !test_bit(NCI_INIT
, &ndev
->flags
))) {
1343 /* Queue frame for rx worker thread */
1344 skb_queue_tail(&ndev
->rx_q
, skb
);
1345 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
1349 EXPORT_SYMBOL(nci_recv_frame
);
1351 int nci_send_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
1353 pr_debug("len %d\n", skb
->len
);
1360 /* Get rid of skb owner, prior to sending to the driver. */
1363 /* Send copy to sniffer */
1364 nfc_send_to_raw_sock(ndev
->nfc_dev
, skb
,
1365 RAW_PAYLOAD_NCI
, NFC_DIRECTION_TX
);
1367 return ndev
->ops
->send(ndev
, skb
);
1369 EXPORT_SYMBOL(nci_send_frame
);
1371 /* Send NCI command */
1372 int nci_send_cmd(struct nci_dev
*ndev
, __u16 opcode
, __u8 plen
, const void *payload
)
1374 struct nci_ctrl_hdr
*hdr
;
1375 struct sk_buff
*skb
;
1377 pr_debug("opcode 0x%x, plen %d\n", opcode
, plen
);
1379 skb
= nci_skb_alloc(ndev
, (NCI_CTRL_HDR_SIZE
+ plen
), GFP_KERNEL
);
1381 pr_err("no memory for command\n");
1385 hdr
= skb_put(skb
, NCI_CTRL_HDR_SIZE
);
1386 hdr
->gid
= nci_opcode_gid(opcode
);
1387 hdr
->oid
= nci_opcode_oid(opcode
);
1390 nci_mt_set((__u8
*)hdr
, NCI_MT_CMD_PKT
);
1391 nci_pbf_set((__u8
*)hdr
, NCI_PBF_LAST
);
1394 skb_put_data(skb
, payload
, plen
);
1396 skb_queue_tail(&ndev
->cmd_q
, skb
);
1397 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
1401 EXPORT_SYMBOL(nci_send_cmd
);
1403 /* Proprietary commands API */
1404 static const struct nci_driver_ops
*ops_cmd_lookup(const struct nci_driver_ops
*ops
,
1409 const struct nci_driver_ops
*op
;
1414 for (i
= 0; i
< n_ops
; i
++) {
1416 if (op
->opcode
== opcode
)
1423 static int nci_op_rsp_packet(struct nci_dev
*ndev
, __u16 rsp_opcode
,
1424 struct sk_buff
*skb
, const struct nci_driver_ops
*ops
,
1427 const struct nci_driver_ops
*op
;
1429 op
= ops_cmd_lookup(ops
, n_ops
, rsp_opcode
);
1430 if (!op
|| !op
->rsp
)
1433 return op
->rsp(ndev
, skb
);
1436 static int nci_op_ntf_packet(struct nci_dev
*ndev
, __u16 ntf_opcode
,
1437 struct sk_buff
*skb
, const struct nci_driver_ops
*ops
,
1440 const struct nci_driver_ops
*op
;
1442 op
= ops_cmd_lookup(ops
, n_ops
, ntf_opcode
);
1443 if (!op
|| !op
->ntf
)
1446 return op
->ntf(ndev
, skb
);
1449 int nci_prop_rsp_packet(struct nci_dev
*ndev
, __u16 opcode
,
1450 struct sk_buff
*skb
)
1452 return nci_op_rsp_packet(ndev
, opcode
, skb
, ndev
->ops
->prop_ops
,
1453 ndev
->ops
->n_prop_ops
);
1456 int nci_prop_ntf_packet(struct nci_dev
*ndev
, __u16 opcode
,
1457 struct sk_buff
*skb
)
1459 return nci_op_ntf_packet(ndev
, opcode
, skb
, ndev
->ops
->prop_ops
,
1460 ndev
->ops
->n_prop_ops
);
1463 int nci_core_rsp_packet(struct nci_dev
*ndev
, __u16 opcode
,
1464 struct sk_buff
*skb
)
1466 return nci_op_rsp_packet(ndev
, opcode
, skb
, ndev
->ops
->core_ops
,
1467 ndev
->ops
->n_core_ops
);
1470 int nci_core_ntf_packet(struct nci_dev
*ndev
, __u16 opcode
,
1471 struct sk_buff
*skb
)
1473 return nci_op_ntf_packet(ndev
, opcode
, skb
, ndev
->ops
->core_ops
,
1474 ndev
->ops
->n_core_ops
);
1477 static bool nci_valid_size(struct sk_buff
*skb
)
1479 BUILD_BUG_ON(NCI_CTRL_HDR_SIZE
!= NCI_DATA_HDR_SIZE
);
1480 unsigned int hdr_size
= NCI_CTRL_HDR_SIZE
;
1482 if (skb
->len
< hdr_size
||
1483 !nci_plen(skb
->data
) ||
1484 skb
->len
< hdr_size
+ nci_plen(skb
->data
)) {
1490 /* ---- NCI TX Data worker thread ---- */
1492 static void nci_tx_work(struct work_struct
*work
)
1494 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, tx_work
);
1495 struct nci_conn_info
*conn_info
;
1496 struct sk_buff
*skb
;
1498 conn_info
= nci_get_conn_info_by_conn_id(ndev
, ndev
->cur_conn_id
);
1502 pr_debug("credits_cnt %d\n", atomic_read(&conn_info
->credits_cnt
));
1504 /* Send queued tx data */
1505 while (atomic_read(&conn_info
->credits_cnt
)) {
1506 skb
= skb_dequeue(&ndev
->tx_q
);
1509 kcov_remote_start_common(skb_get_kcov_handle(skb
));
1511 /* Check if data flow control is used */
1512 if (atomic_read(&conn_info
->credits_cnt
) !=
1513 NCI_DATA_FLOW_CONTROL_NOT_USED
)
1514 atomic_dec(&conn_info
->credits_cnt
);
1516 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1518 nci_conn_id(skb
->data
),
1519 nci_plen(skb
->data
));
1521 nci_send_frame(ndev
, skb
);
1523 mod_timer(&ndev
->data_timer
,
1524 jiffies
+ msecs_to_jiffies(NCI_DATA_TIMEOUT
));
1529 /* ----- NCI RX worker thread (data & control) ----- */
1531 static void nci_rx_work(struct work_struct
*work
)
1533 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, rx_work
);
1534 struct sk_buff
*skb
;
1536 for (; (skb
= skb_dequeue(&ndev
->rx_q
)); kcov_remote_stop()) {
1537 kcov_remote_start_common(skb_get_kcov_handle(skb
));
1539 /* Send copy to sniffer */
1540 nfc_send_to_raw_sock(ndev
->nfc_dev
, skb
,
1541 RAW_PAYLOAD_NCI
, NFC_DIRECTION_RX
);
1543 if (!nci_valid_size(skb
)) {
1549 switch (nci_mt(skb
->data
)) {
1550 case NCI_MT_RSP_PKT
:
1551 nci_rsp_packet(ndev
, skb
);
1554 case NCI_MT_NTF_PKT
:
1555 nci_ntf_packet(ndev
, skb
);
1558 case NCI_MT_DATA_PKT
:
1559 nci_rx_data_packet(ndev
, skb
);
1563 pr_err("unknown MT 0x%x\n", nci_mt(skb
->data
));
1569 /* check if a data exchange timeout has occurred */
1570 if (test_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
)) {
1571 /* complete the data exchange transaction, if exists */
1572 if (test_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
1573 nci_data_exchange_complete(ndev
, NULL
,
1577 clear_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
);
1581 /* ----- NCI TX CMD worker thread ----- */
1583 static void nci_cmd_work(struct work_struct
*work
)
1585 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, cmd_work
);
1586 struct sk_buff
*skb
;
1588 pr_debug("cmd_cnt %d\n", atomic_read(&ndev
->cmd_cnt
));
1590 /* Send queued command */
1591 if (atomic_read(&ndev
->cmd_cnt
)) {
1592 skb
= skb_dequeue(&ndev
->cmd_q
);
1596 kcov_remote_start_common(skb_get_kcov_handle(skb
));
1597 atomic_dec(&ndev
->cmd_cnt
);
1599 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1601 nci_opcode_gid(nci_opcode(skb
->data
)),
1602 nci_opcode_oid(nci_opcode(skb
->data
)),
1603 nci_plen(skb
->data
));
1605 nci_send_frame(ndev
, skb
);
1607 mod_timer(&ndev
->cmd_timer
,
1608 jiffies
+ msecs_to_jiffies(NCI_CMD_TIMEOUT
));
1613 MODULE_DESCRIPTION("NFC Controller Interface");
1614 MODULE_LICENSE("GPL");