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>
29 #include <net/nfc/nci.h>
30 #include <net/nfc/nci_core.h>
31 #include <linux/nfc.h>
33 struct core_conn_create_data
{
35 struct nci_core_conn_create_cmd
*cmd
;
38 static void nci_cmd_work(struct work_struct
*work
);
39 static void nci_rx_work(struct work_struct
*work
);
40 static void nci_tx_work(struct work_struct
*work
);
42 struct nci_conn_info
*nci_get_conn_info_by_conn_id(struct nci_dev
*ndev
,
45 struct nci_conn_info
*conn_info
;
47 list_for_each_entry(conn_info
, &ndev
->conn_info_list
, list
) {
48 if (conn_info
->conn_id
== conn_id
)
55 int nci_get_conn_info_by_dest_type_params(struct nci_dev
*ndev
, u8 dest_type
,
56 struct dest_spec_params
*params
)
58 struct nci_conn_info
*conn_info
;
60 list_for_each_entry(conn_info
, &ndev
->conn_info_list
, list
) {
61 if (conn_info
->dest_type
== dest_type
) {
63 return conn_info
->conn_id
;
65 if (params
->id
== conn_info
->dest_params
->id
&&
66 params
->protocol
== conn_info
->dest_params
->protocol
)
67 return conn_info
->conn_id
;
73 EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params
);
75 /* ---- NCI requests ---- */
77 void nci_req_complete(struct nci_dev
*ndev
, int result
)
79 if (ndev
->req_status
== NCI_REQ_PEND
) {
80 ndev
->req_result
= result
;
81 ndev
->req_status
= NCI_REQ_DONE
;
82 complete(&ndev
->req_completion
);
85 EXPORT_SYMBOL(nci_req_complete
);
87 static void nci_req_cancel(struct nci_dev
*ndev
, int err
)
89 if (ndev
->req_status
== NCI_REQ_PEND
) {
90 ndev
->req_result
= err
;
91 ndev
->req_status
= NCI_REQ_CANCELED
;
92 complete(&ndev
->req_completion
);
96 /* Execute request and wait for completion. */
97 static int __nci_request(struct nci_dev
*ndev
,
98 void (*req
)(struct nci_dev
*ndev
, unsigned long opt
),
99 unsigned long opt
, __u32 timeout
)
104 ndev
->req_status
= NCI_REQ_PEND
;
106 reinit_completion(&ndev
->req_completion
);
109 wait_for_completion_interruptible_timeout(&ndev
->req_completion
,
112 pr_debug("wait_for_completion return %ld\n", completion_rc
);
114 if (completion_rc
> 0) {
115 switch (ndev
->req_status
) {
117 rc
= nci_to_errno(ndev
->req_result
);
120 case NCI_REQ_CANCELED
:
121 rc
= -ndev
->req_result
;
129 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
132 rc
= ((completion_rc
== 0) ? (-ETIMEDOUT
) : (completion_rc
));
135 ndev
->req_status
= ndev
->req_result
= 0;
140 inline int nci_request(struct nci_dev
*ndev
,
141 void (*req
)(struct nci_dev
*ndev
,
143 unsigned long opt
, __u32 timeout
)
147 /* Serialize all requests */
148 mutex_lock(&ndev
->req_lock
);
149 /* check the state after obtaing the lock against any races
150 * from nci_close_device when the device gets removed.
152 if (test_bit(NCI_UP
, &ndev
->flags
))
153 rc
= __nci_request(ndev
, req
, opt
, timeout
);
156 mutex_unlock(&ndev
->req_lock
);
161 static void nci_reset_req(struct nci_dev
*ndev
, unsigned long opt
)
163 struct nci_core_reset_cmd cmd
;
165 cmd
.reset_type
= NCI_RESET_TYPE_RESET_CONFIG
;
166 nci_send_cmd(ndev
, NCI_OP_CORE_RESET_CMD
, 1, &cmd
);
169 static void nci_init_req(struct nci_dev
*ndev
, unsigned long opt
)
171 nci_send_cmd(ndev
, NCI_OP_CORE_INIT_CMD
, 0, NULL
);
174 static void nci_init_complete_req(struct nci_dev
*ndev
, unsigned long opt
)
176 struct nci_rf_disc_map_cmd cmd
;
177 struct disc_map_config
*cfg
= cmd
.mapping_configs
;
178 __u8
*num
= &cmd
.num_mapping_configs
;
181 /* set rf mapping configurations */
184 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
185 for (i
= 0; i
< ndev
->num_supported_rf_interfaces
; i
++) {
186 if (ndev
->supported_rf_interfaces
[i
] ==
187 NCI_RF_INTERFACE_ISO_DEP
) {
188 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
189 cfg
[*num
].mode
= NCI_DISC_MAP_MODE_POLL
|
190 NCI_DISC_MAP_MODE_LISTEN
;
191 cfg
[*num
].rf_interface
= NCI_RF_INTERFACE_ISO_DEP
;
193 } else if (ndev
->supported_rf_interfaces
[i
] ==
194 NCI_RF_INTERFACE_NFC_DEP
) {
195 cfg
[*num
].rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
196 cfg
[*num
].mode
= NCI_DISC_MAP_MODE_POLL
|
197 NCI_DISC_MAP_MODE_LISTEN
;
198 cfg
[*num
].rf_interface
= NCI_RF_INTERFACE_NFC_DEP
;
202 if (*num
== NCI_MAX_NUM_MAPPING_CONFIGS
)
206 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_MAP_CMD
,
207 (1 + ((*num
) * sizeof(struct disc_map_config
))), &cmd
);
210 struct nci_set_config_param
{
216 static void nci_set_config_req(struct nci_dev
*ndev
, unsigned long opt
)
218 struct nci_set_config_param
*param
= (struct nci_set_config_param
*)opt
;
219 struct nci_core_set_config_cmd cmd
;
221 BUG_ON(param
->len
> NCI_MAX_PARAM_LEN
);
224 cmd
.param
.id
= param
->id
;
225 cmd
.param
.len
= param
->len
;
226 memcpy(cmd
.param
.val
, param
->val
, param
->len
);
228 nci_send_cmd(ndev
, NCI_OP_CORE_SET_CONFIG_CMD
, (3 + param
->len
), &cmd
);
231 struct nci_rf_discover_param
{
236 static void nci_rf_discover_req(struct nci_dev
*ndev
, unsigned long opt
)
238 struct nci_rf_discover_param
*param
=
239 (struct nci_rf_discover_param
*)opt
;
240 struct nci_rf_disc_cmd cmd
;
242 cmd
.num_disc_configs
= 0;
244 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
245 (param
->im_protocols
& NFC_PROTO_JEWEL_MASK
||
246 param
->im_protocols
& NFC_PROTO_MIFARE_MASK
||
247 param
->im_protocols
& NFC_PROTO_ISO14443_MASK
||
248 param
->im_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
249 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
250 NCI_NFC_A_PASSIVE_POLL_MODE
;
251 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
252 cmd
.num_disc_configs
++;
255 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
256 (param
->im_protocols
& NFC_PROTO_ISO14443_B_MASK
)) {
257 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
258 NCI_NFC_B_PASSIVE_POLL_MODE
;
259 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
260 cmd
.num_disc_configs
++;
263 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
264 (param
->im_protocols
& NFC_PROTO_FELICA_MASK
||
265 param
->im_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
266 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
267 NCI_NFC_F_PASSIVE_POLL_MODE
;
268 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
269 cmd
.num_disc_configs
++;
272 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
) &&
273 (param
->im_protocols
& NFC_PROTO_ISO15693_MASK
)) {
274 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
275 NCI_NFC_V_PASSIVE_POLL_MODE
;
276 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
277 cmd
.num_disc_configs
++;
280 if ((cmd
.num_disc_configs
< NCI_MAX_NUM_RF_CONFIGS
- 1) &&
281 (param
->tm_protocols
& NFC_PROTO_NFC_DEP_MASK
)) {
282 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
283 NCI_NFC_A_PASSIVE_LISTEN_MODE
;
284 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
285 cmd
.num_disc_configs
++;
286 cmd
.disc_configs
[cmd
.num_disc_configs
].rf_tech_and_mode
=
287 NCI_NFC_F_PASSIVE_LISTEN_MODE
;
288 cmd
.disc_configs
[cmd
.num_disc_configs
].frequency
= 1;
289 cmd
.num_disc_configs
++;
292 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_CMD
,
293 (1 + (cmd
.num_disc_configs
* sizeof(struct disc_config
))),
297 struct nci_rf_discover_select_param
{
298 __u8 rf_discovery_id
;
302 static void nci_rf_discover_select_req(struct nci_dev
*ndev
, unsigned long opt
)
304 struct nci_rf_discover_select_param
*param
=
305 (struct nci_rf_discover_select_param
*)opt
;
306 struct nci_rf_discover_select_cmd cmd
;
308 cmd
.rf_discovery_id
= param
->rf_discovery_id
;
309 cmd
.rf_protocol
= param
->rf_protocol
;
311 switch (cmd
.rf_protocol
) {
312 case NCI_RF_PROTOCOL_ISO_DEP
:
313 cmd
.rf_interface
= NCI_RF_INTERFACE_ISO_DEP
;
316 case NCI_RF_PROTOCOL_NFC_DEP
:
317 cmd
.rf_interface
= NCI_RF_INTERFACE_NFC_DEP
;
321 cmd
.rf_interface
= NCI_RF_INTERFACE_FRAME
;
325 nci_send_cmd(ndev
, NCI_OP_RF_DISCOVER_SELECT_CMD
,
326 sizeof(struct nci_rf_discover_select_cmd
), &cmd
);
329 static void nci_rf_deactivate_req(struct nci_dev
*ndev
, unsigned long opt
)
331 struct nci_rf_deactivate_cmd cmd
;
335 nci_send_cmd(ndev
, NCI_OP_RF_DEACTIVATE_CMD
,
336 sizeof(struct nci_rf_deactivate_cmd
), &cmd
);
339 struct nci_cmd_param
{
345 static void nci_generic_req(struct nci_dev
*ndev
, unsigned long opt
)
347 struct nci_cmd_param
*param
=
348 (struct nci_cmd_param
*)opt
;
350 nci_send_cmd(ndev
, param
->opcode
, param
->len
, param
->payload
);
353 int nci_prop_cmd(struct nci_dev
*ndev
, __u8 oid
, size_t len
, __u8
*payload
)
355 struct nci_cmd_param param
;
357 param
.opcode
= nci_opcode_pack(NCI_GID_PROPRIETARY
, oid
);
359 param
.payload
= payload
;
361 return __nci_request(ndev
, nci_generic_req
, (unsigned long)¶m
,
362 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
364 EXPORT_SYMBOL(nci_prop_cmd
);
366 int nci_core_cmd(struct nci_dev
*ndev
, __u16 opcode
, size_t len
, __u8
*payload
)
368 struct nci_cmd_param param
;
370 param
.opcode
= opcode
;
372 param
.payload
= payload
;
374 return __nci_request(ndev
, nci_generic_req
, (unsigned long)¶m
,
375 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
377 EXPORT_SYMBOL(nci_core_cmd
);
379 int nci_core_reset(struct nci_dev
*ndev
)
381 return __nci_request(ndev
, nci_reset_req
, 0,
382 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
384 EXPORT_SYMBOL(nci_core_reset
);
386 int nci_core_init(struct nci_dev
*ndev
)
388 return __nci_request(ndev
, nci_init_req
, 0,
389 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
391 EXPORT_SYMBOL(nci_core_init
);
393 struct nci_loopback_data
{
395 struct sk_buff
*data
;
398 static void nci_send_data_req(struct nci_dev
*ndev
, unsigned long opt
)
400 struct nci_loopback_data
*data
= (struct nci_loopback_data
*)opt
;
402 nci_send_data(ndev
, data
->conn_id
, data
->data
);
405 static void nci_nfcc_loopback_cb(void *context
, struct sk_buff
*skb
, int err
)
407 struct nci_dev
*ndev
= (struct nci_dev
*)context
;
408 struct nci_conn_info
*conn_info
;
410 conn_info
= nci_get_conn_info_by_conn_id(ndev
, ndev
->cur_conn_id
);
412 nci_req_complete(ndev
, NCI_STATUS_REJECTED
);
416 conn_info
->rx_skb
= skb
;
418 nci_req_complete(ndev
, NCI_STATUS_OK
);
421 int nci_nfcc_loopback(struct nci_dev
*ndev
, void *data
, size_t data_len
,
422 struct sk_buff
**resp
)
425 struct nci_loopback_data loopback_data
;
426 struct nci_conn_info
*conn_info
;
428 int conn_id
= nci_get_conn_info_by_dest_type_params(ndev
,
429 NCI_DESTINATION_NFCC_LOOPBACK
, NULL
);
432 r
= nci_core_conn_create(ndev
, NCI_DESTINATION_NFCC_LOOPBACK
,
434 if (r
!= NCI_STATUS_OK
)
437 conn_id
= nci_get_conn_info_by_dest_type_params(ndev
,
438 NCI_DESTINATION_NFCC_LOOPBACK
,
442 conn_info
= nci_get_conn_info_by_conn_id(ndev
, conn_id
);
446 /* store cb and context to be used on receiving data */
447 conn_info
->data_exchange_cb
= nci_nfcc_loopback_cb
;
448 conn_info
->data_exchange_cb_context
= ndev
;
450 skb
= nci_skb_alloc(ndev
, NCI_DATA_HDR_SIZE
+ data_len
, GFP_KERNEL
);
454 skb_reserve(skb
, NCI_DATA_HDR_SIZE
);
455 skb_put_data(skb
, data
, data_len
);
457 loopback_data
.conn_id
= conn_id
;
458 loopback_data
.data
= skb
;
460 ndev
->cur_conn_id
= conn_id
;
461 r
= nci_request(ndev
, nci_send_data_req
, (unsigned long)&loopback_data
,
462 msecs_to_jiffies(NCI_DATA_TIMEOUT
));
463 if (r
== NCI_STATUS_OK
&& resp
)
464 *resp
= conn_info
->rx_skb
;
468 EXPORT_SYMBOL(nci_nfcc_loopback
);
470 static int nci_open_device(struct nci_dev
*ndev
)
474 mutex_lock(&ndev
->req_lock
);
476 if (test_bit(NCI_UP
, &ndev
->flags
)) {
481 if (ndev
->ops
->open(ndev
)) {
486 atomic_set(&ndev
->cmd_cnt
, 1);
488 set_bit(NCI_INIT
, &ndev
->flags
);
491 rc
= ndev
->ops
->init(ndev
);
494 rc
= __nci_request(ndev
, nci_reset_req
, 0,
495 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
498 if (!rc
&& ndev
->ops
->setup
) {
499 rc
= ndev
->ops
->setup(ndev
);
503 rc
= __nci_request(ndev
, nci_init_req
, 0,
504 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
507 if (!rc
&& ndev
->ops
->post_setup
)
508 rc
= ndev
->ops
->post_setup(ndev
);
511 rc
= __nci_request(ndev
, nci_init_complete_req
, 0,
512 msecs_to_jiffies(NCI_INIT_TIMEOUT
));
515 clear_bit(NCI_INIT
, &ndev
->flags
);
518 set_bit(NCI_UP
, &ndev
->flags
);
519 nci_clear_target_list(ndev
);
520 atomic_set(&ndev
->state
, NCI_IDLE
);
522 /* Init failed, cleanup */
523 skb_queue_purge(&ndev
->cmd_q
);
524 skb_queue_purge(&ndev
->rx_q
);
525 skb_queue_purge(&ndev
->tx_q
);
527 ndev
->ops
->close(ndev
);
532 mutex_unlock(&ndev
->req_lock
);
536 static int nci_close_device(struct nci_dev
*ndev
)
538 nci_req_cancel(ndev
, ENODEV
);
539 mutex_lock(&ndev
->req_lock
);
541 if (!test_and_clear_bit(NCI_UP
, &ndev
->flags
)) {
542 del_timer_sync(&ndev
->cmd_timer
);
543 del_timer_sync(&ndev
->data_timer
);
544 mutex_unlock(&ndev
->req_lock
);
548 /* Drop RX and TX queues */
549 skb_queue_purge(&ndev
->rx_q
);
550 skb_queue_purge(&ndev
->tx_q
);
552 /* Flush RX and TX wq */
553 flush_workqueue(ndev
->rx_wq
);
554 flush_workqueue(ndev
->tx_wq
);
557 skb_queue_purge(&ndev
->cmd_q
);
558 atomic_set(&ndev
->cmd_cnt
, 1);
560 set_bit(NCI_INIT
, &ndev
->flags
);
561 __nci_request(ndev
, nci_reset_req
, 0,
562 msecs_to_jiffies(NCI_RESET_TIMEOUT
));
564 /* After this point our queues are empty
565 * and no works are scheduled.
567 ndev
->ops
->close(ndev
);
569 clear_bit(NCI_INIT
, &ndev
->flags
);
571 del_timer_sync(&ndev
->cmd_timer
);
574 flush_workqueue(ndev
->cmd_wq
);
579 mutex_unlock(&ndev
->req_lock
);
584 /* NCI command timer function */
585 static void nci_cmd_timer(struct timer_list
*t
)
587 struct nci_dev
*ndev
= from_timer(ndev
, t
, cmd_timer
);
589 atomic_set(&ndev
->cmd_cnt
, 1);
590 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
593 /* NCI data exchange timer function */
594 static void nci_data_timer(struct timer_list
*t
)
596 struct nci_dev
*ndev
= from_timer(ndev
, t
, data_timer
);
598 set_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
);
599 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
602 static int nci_dev_up(struct nfc_dev
*nfc_dev
)
604 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
606 return nci_open_device(ndev
);
609 static int nci_dev_down(struct nfc_dev
*nfc_dev
)
611 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
613 return nci_close_device(ndev
);
616 int nci_set_config(struct nci_dev
*ndev
, __u8 id
, size_t len
, __u8
*val
)
618 struct nci_set_config_param param
;
627 return __nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
628 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
630 EXPORT_SYMBOL(nci_set_config
);
632 static void nci_nfcee_discover_req(struct nci_dev
*ndev
, unsigned long opt
)
634 struct nci_nfcee_discover_cmd cmd
;
637 cmd
.discovery_action
= action
;
639 nci_send_cmd(ndev
, NCI_OP_NFCEE_DISCOVER_CMD
, 1, &cmd
);
642 int nci_nfcee_discover(struct nci_dev
*ndev
, u8 action
)
644 return __nci_request(ndev
, nci_nfcee_discover_req
, action
,
645 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
647 EXPORT_SYMBOL(nci_nfcee_discover
);
649 static void nci_nfcee_mode_set_req(struct nci_dev
*ndev
, unsigned long opt
)
651 struct nci_nfcee_mode_set_cmd
*cmd
=
652 (struct nci_nfcee_mode_set_cmd
*)opt
;
654 nci_send_cmd(ndev
, NCI_OP_NFCEE_MODE_SET_CMD
,
655 sizeof(struct nci_nfcee_mode_set_cmd
), cmd
);
658 int nci_nfcee_mode_set(struct nci_dev
*ndev
, u8 nfcee_id
, u8 nfcee_mode
)
660 struct nci_nfcee_mode_set_cmd cmd
;
662 cmd
.nfcee_id
= nfcee_id
;
663 cmd
.nfcee_mode
= nfcee_mode
;
665 return __nci_request(ndev
, nci_nfcee_mode_set_req
,
667 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
669 EXPORT_SYMBOL(nci_nfcee_mode_set
);
671 static void nci_core_conn_create_req(struct nci_dev
*ndev
, unsigned long opt
)
673 struct core_conn_create_data
*data
=
674 (struct core_conn_create_data
*)opt
;
676 nci_send_cmd(ndev
, NCI_OP_CORE_CONN_CREATE_CMD
, data
->length
, data
->cmd
);
679 int nci_core_conn_create(struct nci_dev
*ndev
, u8 destination_type
,
680 u8 number_destination_params
,
682 struct core_conn_create_dest_spec_params
*params
)
685 struct nci_core_conn_create_cmd
*cmd
;
686 struct core_conn_create_data data
;
688 data
.length
= params_len
+ sizeof(struct nci_core_conn_create_cmd
);
689 cmd
= kzalloc(data
.length
, GFP_KERNEL
);
693 cmd
->destination_type
= destination_type
;
694 cmd
->number_destination_params
= number_destination_params
;
699 memcpy(cmd
->params
, params
, params_len
);
700 if (params
->length
> 0)
701 memcpy(&ndev
->cur_params
,
702 ¶ms
->value
[DEST_SPEC_PARAMS_ID_INDEX
],
703 sizeof(struct dest_spec_params
));
705 ndev
->cur_params
.id
= 0;
707 ndev
->cur_params
.id
= 0;
709 ndev
->cur_dest_type
= destination_type
;
711 r
= __nci_request(ndev
, nci_core_conn_create_req
, (unsigned long)&data
,
712 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
716 EXPORT_SYMBOL(nci_core_conn_create
);
718 static void nci_core_conn_close_req(struct nci_dev
*ndev
, unsigned long opt
)
722 nci_send_cmd(ndev
, NCI_OP_CORE_CONN_CLOSE_CMD
, 1, &conn_id
);
725 int nci_core_conn_close(struct nci_dev
*ndev
, u8 conn_id
)
727 ndev
->cur_conn_id
= conn_id
;
728 return __nci_request(ndev
, nci_core_conn_close_req
, conn_id
,
729 msecs_to_jiffies(NCI_CMD_TIMEOUT
));
731 EXPORT_SYMBOL(nci_core_conn_close
);
733 static int nci_set_local_general_bytes(struct nfc_dev
*nfc_dev
)
735 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
736 struct nci_set_config_param param
;
739 param
.val
= nfc_get_local_general_bytes(nfc_dev
, ¶m
.len
);
740 if ((param
.val
== NULL
) || (param
.len
== 0))
743 if (param
.len
> NFC_MAX_GT_LEN
)
746 param
.id
= NCI_PN_ATR_REQ_GEN_BYTES
;
748 rc
= nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
749 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
753 param
.id
= NCI_LN_ATR_RES_GEN_BYTES
;
755 return nci_request(ndev
, nci_set_config_req
, (unsigned long)¶m
,
756 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT
));
759 static int nci_set_listen_parameters(struct nfc_dev
*nfc_dev
)
761 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
765 val
= NCI_LA_SEL_INFO_NFC_DEP_MASK
;
767 rc
= nci_set_config(ndev
, NCI_LA_SEL_INFO
, 1, &val
);
771 val
= NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK
;
773 rc
= nci_set_config(ndev
, NCI_LF_PROTOCOL_TYPE
, 1, &val
);
777 val
= NCI_LF_CON_BITR_F_212
| NCI_LF_CON_BITR_F_424
;
779 return nci_set_config(ndev
, NCI_LF_CON_BITR_F
, 1, &val
);
782 static int nci_start_poll(struct nfc_dev
*nfc_dev
,
783 __u32 im_protocols
, __u32 tm_protocols
)
785 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
786 struct nci_rf_discover_param param
;
789 if ((atomic_read(&ndev
->state
) == NCI_DISCOVERY
) ||
790 (atomic_read(&ndev
->state
) == NCI_W4_ALL_DISCOVERIES
)) {
791 pr_err("unable to start poll, since poll is already active\n");
795 if (ndev
->target_active_prot
) {
796 pr_err("there is an active target\n");
800 if ((atomic_read(&ndev
->state
) == NCI_W4_HOST_SELECT
) ||
801 (atomic_read(&ndev
->state
) == NCI_POLL_ACTIVE
)) {
802 pr_debug("target active or w4 select, implicitly deactivate\n");
804 rc
= nci_request(ndev
, nci_rf_deactivate_req
,
805 NCI_DEACTIVATE_TYPE_IDLE_MODE
,
806 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
811 if ((im_protocols
| tm_protocols
) & NFC_PROTO_NFC_DEP_MASK
) {
812 rc
= nci_set_local_general_bytes(nfc_dev
);
814 pr_err("failed to set local general bytes\n");
819 if (tm_protocols
& NFC_PROTO_NFC_DEP_MASK
) {
820 rc
= nci_set_listen_parameters(nfc_dev
);
822 pr_err("failed to set listen parameters\n");
825 param
.im_protocols
= im_protocols
;
826 param
.tm_protocols
= tm_protocols
;
827 rc
= nci_request(ndev
, nci_rf_discover_req
, (unsigned long)¶m
,
828 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT
));
831 ndev
->poll_prots
= im_protocols
;
836 static void nci_stop_poll(struct nfc_dev
*nfc_dev
)
838 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
840 if ((atomic_read(&ndev
->state
) != NCI_DISCOVERY
) &&
841 (atomic_read(&ndev
->state
) != NCI_W4_ALL_DISCOVERIES
)) {
842 pr_err("unable to stop poll, since poll is not active\n");
846 nci_request(ndev
, nci_rf_deactivate_req
, NCI_DEACTIVATE_TYPE_IDLE_MODE
,
847 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
850 static int nci_activate_target(struct nfc_dev
*nfc_dev
,
851 struct nfc_target
*target
, __u32 protocol
)
853 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
854 struct nci_rf_discover_select_param param
;
855 struct nfc_target
*nci_target
= NULL
;
859 pr_debug("target_idx %d, protocol 0x%x\n", target
->idx
, protocol
);
861 if ((atomic_read(&ndev
->state
) != NCI_W4_HOST_SELECT
) &&
862 (atomic_read(&ndev
->state
) != NCI_POLL_ACTIVE
)) {
863 pr_err("there is no available target to activate\n");
867 if (ndev
->target_active_prot
) {
868 pr_err("there is already an active target\n");
872 for (i
= 0; i
< ndev
->n_targets
; i
++) {
873 if (ndev
->targets
[i
].idx
== target
->idx
) {
874 nci_target
= &ndev
->targets
[i
];
880 pr_err("unable to find the selected target\n");
884 if (!(nci_target
->supported_protocols
& (1 << protocol
))) {
885 pr_err("target does not support the requested protocol 0x%x\n",
890 if (atomic_read(&ndev
->state
) == NCI_W4_HOST_SELECT
) {
891 param
.rf_discovery_id
= nci_target
->logical_idx
;
893 if (protocol
== NFC_PROTO_JEWEL
)
894 param
.rf_protocol
= NCI_RF_PROTOCOL_T1T
;
895 else if (protocol
== NFC_PROTO_MIFARE
)
896 param
.rf_protocol
= NCI_RF_PROTOCOL_T2T
;
897 else if (protocol
== NFC_PROTO_FELICA
)
898 param
.rf_protocol
= NCI_RF_PROTOCOL_T3T
;
899 else if (protocol
== NFC_PROTO_ISO14443
||
900 protocol
== NFC_PROTO_ISO14443_B
)
901 param
.rf_protocol
= NCI_RF_PROTOCOL_ISO_DEP
;
903 param
.rf_protocol
= NCI_RF_PROTOCOL_NFC_DEP
;
905 rc
= nci_request(ndev
, nci_rf_discover_select_req
,
906 (unsigned long)¶m
,
907 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT
));
911 ndev
->target_active_prot
= protocol
;
916 static void nci_deactivate_target(struct nfc_dev
*nfc_dev
,
917 struct nfc_target
*target
,
920 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
921 u8 nci_mode
= NCI_DEACTIVATE_TYPE_IDLE_MODE
;
925 if (!ndev
->target_active_prot
) {
926 pr_err("unable to deactivate target, no active target\n");
930 ndev
->target_active_prot
= 0;
933 case NFC_TARGET_MODE_SLEEP
:
934 nci_mode
= NCI_DEACTIVATE_TYPE_SLEEP_MODE
;
938 if (atomic_read(&ndev
->state
) == NCI_POLL_ACTIVE
) {
939 nci_request(ndev
, nci_rf_deactivate_req
, nci_mode
,
940 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
944 static int nci_dep_link_up(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
945 __u8 comm_mode
, __u8
*gb
, size_t gb_len
)
947 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
950 pr_debug("target_idx %d, comm_mode %d\n", target
->idx
, comm_mode
);
952 rc
= nci_activate_target(nfc_dev
, target
, NFC_PROTO_NFC_DEP
);
956 rc
= nfc_set_remote_general_bytes(nfc_dev
, ndev
->remote_gb
,
957 ndev
->remote_gb_len
);
959 rc
= nfc_dep_link_is_up(nfc_dev
, target
->idx
, NFC_COMM_PASSIVE
,
965 static int nci_dep_link_down(struct nfc_dev
*nfc_dev
)
967 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
972 if (nfc_dev
->rf_mode
== NFC_RF_INITIATOR
) {
973 nci_deactivate_target(nfc_dev
, NULL
, NCI_DEACTIVATE_TYPE_IDLE_MODE
);
975 if (atomic_read(&ndev
->state
) == NCI_LISTEN_ACTIVE
||
976 atomic_read(&ndev
->state
) == NCI_DISCOVERY
) {
977 nci_request(ndev
, nci_rf_deactivate_req
, 0,
978 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT
));
981 rc
= nfc_tm_deactivated(nfc_dev
);
983 pr_err("error when signaling tm deactivation\n");
990 static int nci_transceive(struct nfc_dev
*nfc_dev
, struct nfc_target
*target
,
992 data_exchange_cb_t cb
, void *cb_context
)
994 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
996 struct nci_conn_info
*conn_info
;
998 conn_info
= ndev
->rf_conn_info
;
1002 pr_debug("target_idx %d, len %d\n", target
->idx
, skb
->len
);
1004 if (!ndev
->target_active_prot
) {
1005 pr_err("unable to exchange data, no active target\n");
1009 if (test_and_set_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
1012 /* store cb and context to be used on receiving data */
1013 conn_info
->data_exchange_cb
= cb
;
1014 conn_info
->data_exchange_cb_context
= cb_context
;
1016 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
1018 clear_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
);
1023 static int nci_tm_send(struct nfc_dev
*nfc_dev
, struct sk_buff
*skb
)
1025 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1028 rc
= nci_send_data(ndev
, NCI_STATIC_RF_CONN_ID
, skb
);
1030 pr_err("unable to send data\n");
1035 static int nci_enable_se(struct nfc_dev
*nfc_dev
, u32 se_idx
)
1037 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1039 if (ndev
->ops
->enable_se
)
1040 return ndev
->ops
->enable_se(ndev
, se_idx
);
1045 static int nci_disable_se(struct nfc_dev
*nfc_dev
, u32 se_idx
)
1047 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1049 if (ndev
->ops
->disable_se
)
1050 return ndev
->ops
->disable_se(ndev
, se_idx
);
1055 static int nci_discover_se(struct nfc_dev
*nfc_dev
)
1058 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1060 if (ndev
->ops
->discover_se
) {
1061 r
= nci_nfcee_discover(ndev
, NCI_NFCEE_DISCOVERY_ACTION_ENABLE
);
1062 if (r
!= NCI_STATUS_OK
)
1065 return ndev
->ops
->discover_se(ndev
);
1071 static int nci_se_io(struct nfc_dev
*nfc_dev
, u32 se_idx
,
1072 u8
*apdu
, size_t apdu_length
,
1073 se_io_cb_t cb
, void *cb_context
)
1075 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1077 if (ndev
->ops
->se_io
)
1078 return ndev
->ops
->se_io(ndev
, se_idx
, apdu
,
1079 apdu_length
, cb
, cb_context
);
1084 static int nci_fw_download(struct nfc_dev
*nfc_dev
, const char *firmware_name
)
1086 struct nci_dev
*ndev
= nfc_get_drvdata(nfc_dev
);
1088 if (!ndev
->ops
->fw_download
)
1091 return ndev
->ops
->fw_download(ndev
, firmware_name
);
1094 static struct nfc_ops nci_nfc_ops
= {
1095 .dev_up
= nci_dev_up
,
1096 .dev_down
= nci_dev_down
,
1097 .start_poll
= nci_start_poll
,
1098 .stop_poll
= nci_stop_poll
,
1099 .dep_link_up
= nci_dep_link_up
,
1100 .dep_link_down
= nci_dep_link_down
,
1101 .activate_target
= nci_activate_target
,
1102 .deactivate_target
= nci_deactivate_target
,
1103 .im_transceive
= nci_transceive
,
1104 .tm_send
= nci_tm_send
,
1105 .enable_se
= nci_enable_se
,
1106 .disable_se
= nci_disable_se
,
1107 .discover_se
= nci_discover_se
,
1109 .fw_download
= nci_fw_download
,
1112 /* ---- Interface to NCI drivers ---- */
1114 * nci_allocate_device - allocate a new nci device
1116 * @ops: device operations
1117 * @supported_protocols: NFC protocols supported by the device
1119 struct nci_dev
*nci_allocate_device(struct nci_ops
*ops
,
1120 __u32 supported_protocols
,
1121 int tx_headroom
, int tx_tailroom
)
1123 struct nci_dev
*ndev
;
1125 pr_debug("supported_protocols 0x%x\n", supported_protocols
);
1127 if (!ops
->open
|| !ops
->close
|| !ops
->send
)
1130 if (!supported_protocols
)
1133 ndev
= kzalloc(sizeof(struct nci_dev
), GFP_KERNEL
);
1139 if (ops
->n_prop_ops
> NCI_MAX_PROPRIETARY_CMD
) {
1140 pr_err("Too many proprietary commands: %zd\n",
1142 ops
->prop_ops
= NULL
;
1143 ops
->n_prop_ops
= 0;
1146 ndev
->tx_headroom
= tx_headroom
;
1147 ndev
->tx_tailroom
= tx_tailroom
;
1148 init_completion(&ndev
->req_completion
);
1150 ndev
->nfc_dev
= nfc_allocate_device(&nci_nfc_ops
,
1151 supported_protocols
,
1152 tx_headroom
+ NCI_DATA_HDR_SIZE
,
1157 ndev
->hci_dev
= nci_hci_allocate(ndev
);
1161 nfc_set_drvdata(ndev
->nfc_dev
, ndev
);
1166 nfc_free_device(ndev
->nfc_dev
);
1171 EXPORT_SYMBOL(nci_allocate_device
);
1174 * nci_free_device - deallocate nci device
1176 * @ndev: The nci device to deallocate
1178 void nci_free_device(struct nci_dev
*ndev
)
1180 nfc_free_device(ndev
->nfc_dev
);
1181 nci_hci_deallocate(ndev
);
1184 EXPORT_SYMBOL(nci_free_device
);
1187 * nci_register_device - register a nci device in the nfc subsystem
1189 * @dev: The nci device to register
1191 int nci_register_device(struct nci_dev
*ndev
)
1194 struct device
*dev
= &ndev
->nfc_dev
->dev
;
1199 INIT_WORK(&ndev
->cmd_work
, nci_cmd_work
);
1200 snprintf(name
, sizeof(name
), "%s_nci_cmd_wq", dev_name(dev
));
1201 ndev
->cmd_wq
= create_singlethread_workqueue(name
);
1202 if (!ndev
->cmd_wq
) {
1207 INIT_WORK(&ndev
->rx_work
, nci_rx_work
);
1208 snprintf(name
, sizeof(name
), "%s_nci_rx_wq", dev_name(dev
));
1209 ndev
->rx_wq
= create_singlethread_workqueue(name
);
1212 goto destroy_cmd_wq_exit
;
1215 INIT_WORK(&ndev
->tx_work
, nci_tx_work
);
1216 snprintf(name
, sizeof(name
), "%s_nci_tx_wq", dev_name(dev
));
1217 ndev
->tx_wq
= create_singlethread_workqueue(name
);
1220 goto destroy_rx_wq_exit
;
1223 skb_queue_head_init(&ndev
->cmd_q
);
1224 skb_queue_head_init(&ndev
->rx_q
);
1225 skb_queue_head_init(&ndev
->tx_q
);
1227 timer_setup(&ndev
->cmd_timer
, nci_cmd_timer
, 0);
1228 timer_setup(&ndev
->data_timer
, nci_data_timer
, 0);
1230 mutex_init(&ndev
->req_lock
);
1231 INIT_LIST_HEAD(&ndev
->conn_info_list
);
1233 rc
= nfc_register_device(ndev
->nfc_dev
);
1235 goto destroy_rx_wq_exit
;
1240 destroy_workqueue(ndev
->rx_wq
);
1242 destroy_cmd_wq_exit
:
1243 destroy_workqueue(ndev
->cmd_wq
);
1248 EXPORT_SYMBOL(nci_register_device
);
1251 * nci_unregister_device - unregister a nci device in the nfc subsystem
1253 * @dev: The nci device to unregister
1255 void nci_unregister_device(struct nci_dev
*ndev
)
1257 struct nci_conn_info
*conn_info
, *n
;
1259 nci_close_device(ndev
);
1261 destroy_workqueue(ndev
->cmd_wq
);
1262 destroy_workqueue(ndev
->rx_wq
);
1263 destroy_workqueue(ndev
->tx_wq
);
1265 list_for_each_entry_safe(conn_info
, n
, &ndev
->conn_info_list
, list
) {
1266 list_del(&conn_info
->list
);
1267 /* conn_info is allocated with devm_kzalloc */
1270 nfc_unregister_device(ndev
->nfc_dev
);
1272 EXPORT_SYMBOL(nci_unregister_device
);
1275 * nci_recv_frame - receive frame from NCI drivers
1277 * @ndev: The nci device
1278 * @skb: The sk_buff to receive
1280 int nci_recv_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
1282 pr_debug("len %d\n", skb
->len
);
1284 if (!ndev
|| (!test_bit(NCI_UP
, &ndev
->flags
) &&
1285 !test_bit(NCI_INIT
, &ndev
->flags
))) {
1290 /* Queue frame for rx worker thread */
1291 skb_queue_tail(&ndev
->rx_q
, skb
);
1292 queue_work(ndev
->rx_wq
, &ndev
->rx_work
);
1296 EXPORT_SYMBOL(nci_recv_frame
);
1298 int nci_send_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
1300 pr_debug("len %d\n", skb
->len
);
1307 /* Get rid of skb owner, prior to sending to the driver. */
1310 /* Send copy to sniffer */
1311 nfc_send_to_raw_sock(ndev
->nfc_dev
, skb
,
1312 RAW_PAYLOAD_NCI
, NFC_DIRECTION_TX
);
1314 return ndev
->ops
->send(ndev
, skb
);
1316 EXPORT_SYMBOL(nci_send_frame
);
1318 /* Send NCI command */
1319 int nci_send_cmd(struct nci_dev
*ndev
, __u16 opcode
, __u8 plen
, void *payload
)
1321 struct nci_ctrl_hdr
*hdr
;
1322 struct sk_buff
*skb
;
1324 pr_debug("opcode 0x%x, plen %d\n", opcode
, plen
);
1326 skb
= nci_skb_alloc(ndev
, (NCI_CTRL_HDR_SIZE
+ plen
), GFP_KERNEL
);
1328 pr_err("no memory for command\n");
1332 hdr
= skb_put(skb
, NCI_CTRL_HDR_SIZE
);
1333 hdr
->gid
= nci_opcode_gid(opcode
);
1334 hdr
->oid
= nci_opcode_oid(opcode
);
1337 nci_mt_set((__u8
*)hdr
, NCI_MT_CMD_PKT
);
1338 nci_pbf_set((__u8
*)hdr
, NCI_PBF_LAST
);
1341 skb_put_data(skb
, payload
, plen
);
1343 skb_queue_tail(&ndev
->cmd_q
, skb
);
1344 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);
1348 EXPORT_SYMBOL(nci_send_cmd
);
1350 /* Proprietary commands API */
1351 static struct nci_driver_ops
*ops_cmd_lookup(struct nci_driver_ops
*ops
,
1356 struct nci_driver_ops
*op
;
1361 for (i
= 0; i
< n_ops
; i
++) {
1363 if (op
->opcode
== opcode
)
1370 static int nci_op_rsp_packet(struct nci_dev
*ndev
, __u16 rsp_opcode
,
1371 struct sk_buff
*skb
, struct nci_driver_ops
*ops
,
1374 struct nci_driver_ops
*op
;
1376 op
= ops_cmd_lookup(ops
, n_ops
, rsp_opcode
);
1377 if (!op
|| !op
->rsp
)
1380 return op
->rsp(ndev
, skb
);
1383 static int nci_op_ntf_packet(struct nci_dev
*ndev
, __u16 ntf_opcode
,
1384 struct sk_buff
*skb
, struct nci_driver_ops
*ops
,
1387 struct nci_driver_ops
*op
;
1389 op
= ops_cmd_lookup(ops
, n_ops
, ntf_opcode
);
1390 if (!op
|| !op
->ntf
)
1393 return op
->ntf(ndev
, skb
);
1396 int nci_prop_rsp_packet(struct nci_dev
*ndev
, __u16 opcode
,
1397 struct sk_buff
*skb
)
1399 return nci_op_rsp_packet(ndev
, opcode
, skb
, ndev
->ops
->prop_ops
,
1400 ndev
->ops
->n_prop_ops
);
1403 int nci_prop_ntf_packet(struct nci_dev
*ndev
, __u16 opcode
,
1404 struct sk_buff
*skb
)
1406 return nci_op_ntf_packet(ndev
, opcode
, skb
, ndev
->ops
->prop_ops
,
1407 ndev
->ops
->n_prop_ops
);
1410 int nci_core_rsp_packet(struct nci_dev
*ndev
, __u16 opcode
,
1411 struct sk_buff
*skb
)
1413 return nci_op_rsp_packet(ndev
, opcode
, skb
, ndev
->ops
->core_ops
,
1414 ndev
->ops
->n_core_ops
);
1417 int nci_core_ntf_packet(struct nci_dev
*ndev
, __u16 opcode
,
1418 struct sk_buff
*skb
)
1420 return nci_op_ntf_packet(ndev
, opcode
, skb
, ndev
->ops
->core_ops
,
1421 ndev
->ops
->n_core_ops
);
1424 /* ---- NCI TX Data worker thread ---- */
1426 static void nci_tx_work(struct work_struct
*work
)
1428 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, tx_work
);
1429 struct nci_conn_info
*conn_info
;
1430 struct sk_buff
*skb
;
1432 conn_info
= nci_get_conn_info_by_conn_id(ndev
, ndev
->cur_conn_id
);
1436 pr_debug("credits_cnt %d\n", atomic_read(&conn_info
->credits_cnt
));
1438 /* Send queued tx data */
1439 while (atomic_read(&conn_info
->credits_cnt
)) {
1440 skb
= skb_dequeue(&ndev
->tx_q
);
1444 /* Check if data flow control is used */
1445 if (atomic_read(&conn_info
->credits_cnt
) !=
1446 NCI_DATA_FLOW_CONTROL_NOT_USED
)
1447 atomic_dec(&conn_info
->credits_cnt
);
1449 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1451 nci_conn_id(skb
->data
),
1452 nci_plen(skb
->data
));
1454 nci_send_frame(ndev
, skb
);
1456 mod_timer(&ndev
->data_timer
,
1457 jiffies
+ msecs_to_jiffies(NCI_DATA_TIMEOUT
));
1461 /* ----- NCI RX worker thread (data & control) ----- */
1463 static void nci_rx_work(struct work_struct
*work
)
1465 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, rx_work
);
1466 struct sk_buff
*skb
;
1468 while ((skb
= skb_dequeue(&ndev
->rx_q
))) {
1470 /* Send copy to sniffer */
1471 nfc_send_to_raw_sock(ndev
->nfc_dev
, skb
,
1472 RAW_PAYLOAD_NCI
, NFC_DIRECTION_RX
);
1475 switch (nci_mt(skb
->data
)) {
1476 case NCI_MT_RSP_PKT
:
1477 nci_rsp_packet(ndev
, skb
);
1480 case NCI_MT_NTF_PKT
:
1481 nci_ntf_packet(ndev
, skb
);
1484 case NCI_MT_DATA_PKT
:
1485 nci_rx_data_packet(ndev
, skb
);
1489 pr_err("unknown MT 0x%x\n", nci_mt(skb
->data
));
1495 /* check if a data exchange timout has occurred */
1496 if (test_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
)) {
1497 /* complete the data exchange transaction, if exists */
1498 if (test_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
1499 nci_data_exchange_complete(ndev
, NULL
,
1503 clear_bit(NCI_DATA_EXCHANGE_TO
, &ndev
->flags
);
1507 /* ----- NCI TX CMD worker thread ----- */
1509 static void nci_cmd_work(struct work_struct
*work
)
1511 struct nci_dev
*ndev
= container_of(work
, struct nci_dev
, cmd_work
);
1512 struct sk_buff
*skb
;
1514 pr_debug("cmd_cnt %d\n", atomic_read(&ndev
->cmd_cnt
));
1516 /* Send queued command */
1517 if (atomic_read(&ndev
->cmd_cnt
)) {
1518 skb
= skb_dequeue(&ndev
->cmd_q
);
1522 atomic_dec(&ndev
->cmd_cnt
);
1524 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1526 nci_opcode_gid(nci_opcode(skb
->data
)),
1527 nci_opcode_oid(nci_opcode(skb
->data
)),
1528 nci_plen(skb
->data
));
1530 nci_send_frame(ndev
, skb
);
1532 mod_timer(&ndev
->cmd_timer
,
1533 jiffies
+ msecs_to_jiffies(NCI_CMD_TIMEOUT
));
1537 MODULE_LICENSE("GPL");