WIP FPC-III support
[linux/fpc-iii.git] / net / nfc / nci / core.c
blobe64727e1a72f9c2ffb830de8492b1e64d2d7963b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
11 * Acknowledgements:
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>
28 #include "../nfc.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 {
34 int length;
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,
43 int conn_id)
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)
49 return conn_info;
52 return NULL;
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) {
62 if (!params)
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;
71 return -EINVAL;
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)
101 int rc = 0;
102 long completion_rc;
104 ndev->req_status = NCI_REQ_PEND;
106 reinit_completion(&ndev->req_completion);
107 req(ndev, opt);
108 completion_rc =
109 wait_for_completion_interruptible_timeout(&ndev->req_completion,
110 timeout);
112 pr_debug("wait_for_completion return %ld\n", completion_rc);
114 if (completion_rc > 0) {
115 switch (ndev->req_status) {
116 case NCI_REQ_DONE:
117 rc = nci_to_errno(ndev->req_result);
118 break;
120 case NCI_REQ_CANCELED:
121 rc = -ndev->req_result;
122 break;
124 default:
125 rc = -ETIMEDOUT;
126 break;
128 } else {
129 pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
130 completion_rc);
132 rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
135 ndev->req_status = ndev->req_result = 0;
137 return rc;
140 inline int nci_request(struct nci_dev *ndev,
141 void (*req)(struct nci_dev *ndev,
142 unsigned long opt),
143 unsigned long opt, __u32 timeout)
145 int rc;
147 if (!test_bit(NCI_UP, &ndev->flags))
148 return -ENETDOWN;
150 /* Serialize all requests */
151 mutex_lock(&ndev->req_lock);
152 rc = __nci_request(ndev, req, opt, timeout);
153 mutex_unlock(&ndev->req_lock);
155 return rc;
158 static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
160 struct nci_core_reset_cmd cmd;
162 cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
163 nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
166 static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
168 u8 plen = 0;
170 if (opt)
171 plen = sizeof(struct nci_core_init_v2_cmd);
173 nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, plen, (void *)opt);
176 static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
178 struct nci_rf_disc_map_cmd cmd;
179 struct disc_map_config *cfg = cmd.mapping_configs;
180 __u8 *num = &cmd.num_mapping_configs;
181 int i;
183 /* set rf mapping configurations */
184 *num = 0;
186 /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
187 for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
188 if (ndev->supported_rf_interfaces[i] ==
189 NCI_RF_INTERFACE_ISO_DEP) {
190 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
191 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
192 NCI_DISC_MAP_MODE_LISTEN;
193 cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
194 (*num)++;
195 } else if (ndev->supported_rf_interfaces[i] ==
196 NCI_RF_INTERFACE_NFC_DEP) {
197 cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
198 cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
199 NCI_DISC_MAP_MODE_LISTEN;
200 cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
201 (*num)++;
204 if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
205 break;
208 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
209 (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
212 struct nci_set_config_param {
213 __u8 id;
214 size_t len;
215 __u8 *val;
218 static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
220 struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
221 struct nci_core_set_config_cmd cmd;
223 BUG_ON(param->len > NCI_MAX_PARAM_LEN);
225 cmd.num_params = 1;
226 cmd.param.id = param->id;
227 cmd.param.len = param->len;
228 memcpy(cmd.param.val, param->val, param->len);
230 nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
233 struct nci_rf_discover_param {
234 __u32 im_protocols;
235 __u32 tm_protocols;
238 static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
240 struct nci_rf_discover_param *param =
241 (struct nci_rf_discover_param *)opt;
242 struct nci_rf_disc_cmd cmd;
244 cmd.num_disc_configs = 0;
246 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
247 (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
248 param->im_protocols & NFC_PROTO_MIFARE_MASK ||
249 param->im_protocols & NFC_PROTO_ISO14443_MASK ||
250 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
251 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
252 NCI_NFC_A_PASSIVE_POLL_MODE;
253 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
254 cmd.num_disc_configs++;
257 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
258 (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
259 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
260 NCI_NFC_B_PASSIVE_POLL_MODE;
261 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
262 cmd.num_disc_configs++;
265 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
266 (param->im_protocols & NFC_PROTO_FELICA_MASK ||
267 param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
268 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
269 NCI_NFC_F_PASSIVE_POLL_MODE;
270 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
271 cmd.num_disc_configs++;
274 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
275 (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
276 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
277 NCI_NFC_V_PASSIVE_POLL_MODE;
278 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
279 cmd.num_disc_configs++;
282 if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
283 (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
284 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
285 NCI_NFC_A_PASSIVE_LISTEN_MODE;
286 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
287 cmd.num_disc_configs++;
288 cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
289 NCI_NFC_F_PASSIVE_LISTEN_MODE;
290 cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
291 cmd.num_disc_configs++;
294 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
295 (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
296 &cmd);
299 struct nci_rf_discover_select_param {
300 __u8 rf_discovery_id;
301 __u8 rf_protocol;
304 static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
306 struct nci_rf_discover_select_param *param =
307 (struct nci_rf_discover_select_param *)opt;
308 struct nci_rf_discover_select_cmd cmd;
310 cmd.rf_discovery_id = param->rf_discovery_id;
311 cmd.rf_protocol = param->rf_protocol;
313 switch (cmd.rf_protocol) {
314 case NCI_RF_PROTOCOL_ISO_DEP:
315 cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
316 break;
318 case NCI_RF_PROTOCOL_NFC_DEP:
319 cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
320 break;
322 default:
323 cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
324 break;
327 nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
328 sizeof(struct nci_rf_discover_select_cmd), &cmd);
331 static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
333 struct nci_rf_deactivate_cmd cmd;
335 cmd.type = opt;
337 nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
338 sizeof(struct nci_rf_deactivate_cmd), &cmd);
341 struct nci_cmd_param {
342 __u16 opcode;
343 size_t len;
344 __u8 *payload;
347 static void nci_generic_req(struct nci_dev *ndev, unsigned long opt)
349 struct nci_cmd_param *param =
350 (struct nci_cmd_param *)opt;
352 nci_send_cmd(ndev, param->opcode, param->len, param->payload);
355 int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, __u8 *payload)
357 struct nci_cmd_param param;
359 param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
360 param.len = len;
361 param.payload = payload;
363 return __nci_request(ndev, nci_generic_req, (unsigned long)&param,
364 msecs_to_jiffies(NCI_CMD_TIMEOUT));
366 EXPORT_SYMBOL(nci_prop_cmd);
368 int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len, __u8 *payload)
370 struct nci_cmd_param param;
372 param.opcode = opcode;
373 param.len = len;
374 param.payload = payload;
376 return __nci_request(ndev, nci_generic_req, (unsigned long)&param,
377 msecs_to_jiffies(NCI_CMD_TIMEOUT));
379 EXPORT_SYMBOL(nci_core_cmd);
381 int nci_core_reset(struct nci_dev *ndev)
383 return __nci_request(ndev, nci_reset_req, 0,
384 msecs_to_jiffies(NCI_RESET_TIMEOUT));
386 EXPORT_SYMBOL(nci_core_reset);
388 int nci_core_init(struct nci_dev *ndev)
390 return __nci_request(ndev, nci_init_req, 0,
391 msecs_to_jiffies(NCI_INIT_TIMEOUT));
393 EXPORT_SYMBOL(nci_core_init);
395 struct nci_loopback_data {
396 u8 conn_id;
397 struct sk_buff *data;
400 static void nci_send_data_req(struct nci_dev *ndev, unsigned long opt)
402 struct nci_loopback_data *data = (struct nci_loopback_data *)opt;
404 nci_send_data(ndev, data->conn_id, data->data);
407 static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
409 struct nci_dev *ndev = (struct nci_dev *)context;
410 struct nci_conn_info *conn_info;
412 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
413 if (!conn_info) {
414 nci_req_complete(ndev, NCI_STATUS_REJECTED);
415 return;
418 conn_info->rx_skb = skb;
420 nci_req_complete(ndev, NCI_STATUS_OK);
423 int nci_nfcc_loopback(struct nci_dev *ndev, void *data, size_t data_len,
424 struct sk_buff **resp)
426 int r;
427 struct nci_loopback_data loopback_data;
428 struct nci_conn_info *conn_info;
429 struct sk_buff *skb;
430 int conn_id = nci_get_conn_info_by_dest_type_params(ndev,
431 NCI_DESTINATION_NFCC_LOOPBACK, NULL);
433 if (conn_id < 0) {
434 r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCC_LOOPBACK,
435 0, 0, NULL);
436 if (r != NCI_STATUS_OK)
437 return r;
439 conn_id = nci_get_conn_info_by_dest_type_params(ndev,
440 NCI_DESTINATION_NFCC_LOOPBACK,
441 NULL);
444 conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
445 if (!conn_info)
446 return -EPROTO;
448 /* store cb and context to be used on receiving data */
449 conn_info->data_exchange_cb = nci_nfcc_loopback_cb;
450 conn_info->data_exchange_cb_context = ndev;
452 skb = nci_skb_alloc(ndev, NCI_DATA_HDR_SIZE + data_len, GFP_KERNEL);
453 if (!skb)
454 return -ENOMEM;
456 skb_reserve(skb, NCI_DATA_HDR_SIZE);
457 skb_put_data(skb, data, data_len);
459 loopback_data.conn_id = conn_id;
460 loopback_data.data = skb;
462 ndev->cur_conn_id = conn_id;
463 r = nci_request(ndev, nci_send_data_req, (unsigned long)&loopback_data,
464 msecs_to_jiffies(NCI_DATA_TIMEOUT));
465 if (r == NCI_STATUS_OK && resp)
466 *resp = conn_info->rx_skb;
468 return r;
470 EXPORT_SYMBOL(nci_nfcc_loopback);
472 static int nci_open_device(struct nci_dev *ndev)
474 int rc = 0;
476 mutex_lock(&ndev->req_lock);
478 if (test_bit(NCI_UP, &ndev->flags)) {
479 rc = -EALREADY;
480 goto done;
483 if (ndev->ops->open(ndev)) {
484 rc = -EIO;
485 goto done;
488 atomic_set(&ndev->cmd_cnt, 1);
490 set_bit(NCI_INIT, &ndev->flags);
492 if (ndev->ops->init)
493 rc = ndev->ops->init(ndev);
495 if (!rc) {
496 rc = __nci_request(ndev, nci_reset_req, 0,
497 msecs_to_jiffies(NCI_RESET_TIMEOUT));
500 if (!rc && ndev->ops->setup) {
501 rc = ndev->ops->setup(ndev);
504 if (!rc) {
505 struct nci_core_init_v2_cmd nci_init_v2_cmd = {
506 .feature1 = NCI_FEATURE_DISABLE,
507 .feature2 = NCI_FEATURE_DISABLE
509 unsigned long opt = 0;
511 if (!(ndev->nci_ver & NCI_VER_2_MASK))
512 opt = (unsigned long)&nci_init_v2_cmd;
514 rc = __nci_request(ndev, nci_init_req, opt,
515 msecs_to_jiffies(NCI_INIT_TIMEOUT));
518 if (!rc && ndev->ops->post_setup)
519 rc = ndev->ops->post_setup(ndev);
521 if (!rc) {
522 rc = __nci_request(ndev, nci_init_complete_req, 0,
523 msecs_to_jiffies(NCI_INIT_TIMEOUT));
526 clear_bit(NCI_INIT, &ndev->flags);
528 if (!rc) {
529 set_bit(NCI_UP, &ndev->flags);
530 nci_clear_target_list(ndev);
531 atomic_set(&ndev->state, NCI_IDLE);
532 } else {
533 /* Init failed, cleanup */
534 skb_queue_purge(&ndev->cmd_q);
535 skb_queue_purge(&ndev->rx_q);
536 skb_queue_purge(&ndev->tx_q);
538 ndev->ops->close(ndev);
539 ndev->flags = 0;
542 done:
543 mutex_unlock(&ndev->req_lock);
544 return rc;
547 static int nci_close_device(struct nci_dev *ndev)
549 nci_req_cancel(ndev, ENODEV);
550 mutex_lock(&ndev->req_lock);
552 if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
553 del_timer_sync(&ndev->cmd_timer);
554 del_timer_sync(&ndev->data_timer);
555 mutex_unlock(&ndev->req_lock);
556 return 0;
559 /* Drop RX and TX queues */
560 skb_queue_purge(&ndev->rx_q);
561 skb_queue_purge(&ndev->tx_q);
563 /* Flush RX and TX wq */
564 flush_workqueue(ndev->rx_wq);
565 flush_workqueue(ndev->tx_wq);
567 /* Reset device */
568 skb_queue_purge(&ndev->cmd_q);
569 atomic_set(&ndev->cmd_cnt, 1);
571 set_bit(NCI_INIT, &ndev->flags);
572 __nci_request(ndev, nci_reset_req, 0,
573 msecs_to_jiffies(NCI_RESET_TIMEOUT));
575 /* After this point our queues are empty
576 * and no works are scheduled.
578 ndev->ops->close(ndev);
580 clear_bit(NCI_INIT, &ndev->flags);
582 del_timer_sync(&ndev->cmd_timer);
584 /* Flush cmd wq */
585 flush_workqueue(ndev->cmd_wq);
587 /* Clear flags */
588 ndev->flags = 0;
590 mutex_unlock(&ndev->req_lock);
592 return 0;
595 /* NCI command timer function */
596 static void nci_cmd_timer(struct timer_list *t)
598 struct nci_dev *ndev = from_timer(ndev, t, cmd_timer);
600 atomic_set(&ndev->cmd_cnt, 1);
601 queue_work(ndev->cmd_wq, &ndev->cmd_work);
604 /* NCI data exchange timer function */
605 static void nci_data_timer(struct timer_list *t)
607 struct nci_dev *ndev = from_timer(ndev, t, data_timer);
609 set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
610 queue_work(ndev->rx_wq, &ndev->rx_work);
613 static int nci_dev_up(struct nfc_dev *nfc_dev)
615 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
617 return nci_open_device(ndev);
620 static int nci_dev_down(struct nfc_dev *nfc_dev)
622 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
624 return nci_close_device(ndev);
627 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
629 struct nci_set_config_param param;
631 if (!val || !len)
632 return 0;
634 param.id = id;
635 param.len = len;
636 param.val = val;
638 return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
639 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
641 EXPORT_SYMBOL(nci_set_config);
643 static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
645 struct nci_nfcee_discover_cmd cmd;
646 __u8 action = opt;
648 cmd.discovery_action = action;
650 nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
653 int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
655 return __nci_request(ndev, nci_nfcee_discover_req, action,
656 msecs_to_jiffies(NCI_CMD_TIMEOUT));
658 EXPORT_SYMBOL(nci_nfcee_discover);
660 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
662 struct nci_nfcee_mode_set_cmd *cmd =
663 (struct nci_nfcee_mode_set_cmd *)opt;
665 nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
666 sizeof(struct nci_nfcee_mode_set_cmd), cmd);
669 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
671 struct nci_nfcee_mode_set_cmd cmd;
673 cmd.nfcee_id = nfcee_id;
674 cmd.nfcee_mode = nfcee_mode;
676 return __nci_request(ndev, nci_nfcee_mode_set_req,
677 (unsigned long)&cmd,
678 msecs_to_jiffies(NCI_CMD_TIMEOUT));
680 EXPORT_SYMBOL(nci_nfcee_mode_set);
682 static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
684 struct core_conn_create_data *data =
685 (struct core_conn_create_data *)opt;
687 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
690 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
691 u8 number_destination_params,
692 size_t params_len,
693 struct core_conn_create_dest_spec_params *params)
695 int r;
696 struct nci_core_conn_create_cmd *cmd;
697 struct core_conn_create_data data;
699 data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
700 cmd = kzalloc(data.length, GFP_KERNEL);
701 if (!cmd)
702 return -ENOMEM;
704 cmd->destination_type = destination_type;
705 cmd->number_destination_params = number_destination_params;
707 data.cmd = cmd;
709 if (params) {
710 memcpy(cmd->params, params, params_len);
711 if (params->length > 0)
712 memcpy(&ndev->cur_params,
713 &params->value[DEST_SPEC_PARAMS_ID_INDEX],
714 sizeof(struct dest_spec_params));
715 else
716 ndev->cur_params.id = 0;
717 } else {
718 ndev->cur_params.id = 0;
720 ndev->cur_dest_type = destination_type;
722 r = __nci_request(ndev, nci_core_conn_create_req, (unsigned long)&data,
723 msecs_to_jiffies(NCI_CMD_TIMEOUT));
724 kfree(cmd);
725 return r;
727 EXPORT_SYMBOL(nci_core_conn_create);
729 static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
731 __u8 conn_id = opt;
733 nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
736 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
738 ndev->cur_conn_id = conn_id;
739 return __nci_request(ndev, nci_core_conn_close_req, conn_id,
740 msecs_to_jiffies(NCI_CMD_TIMEOUT));
742 EXPORT_SYMBOL(nci_core_conn_close);
744 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
746 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
747 struct nci_set_config_param param;
748 int rc;
750 param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
751 if ((param.val == NULL) || (param.len == 0))
752 return 0;
754 if (param.len > NFC_MAX_GT_LEN)
755 return -EINVAL;
757 param.id = NCI_PN_ATR_REQ_GEN_BYTES;
759 rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
760 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
761 if (rc)
762 return rc;
764 param.id = NCI_LN_ATR_RES_GEN_BYTES;
766 return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
767 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
770 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
772 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
773 int rc;
774 __u8 val;
776 val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
778 rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
779 if (rc)
780 return rc;
782 val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
784 rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
785 if (rc)
786 return rc;
788 val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
790 return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
793 static int nci_start_poll(struct nfc_dev *nfc_dev,
794 __u32 im_protocols, __u32 tm_protocols)
796 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
797 struct nci_rf_discover_param param;
798 int rc;
800 if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
801 (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
802 pr_err("unable to start poll, since poll is already active\n");
803 return -EBUSY;
806 if (ndev->target_active_prot) {
807 pr_err("there is an active target\n");
808 return -EBUSY;
811 if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
812 (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
813 pr_debug("target active or w4 select, implicitly deactivate\n");
815 rc = nci_request(ndev, nci_rf_deactivate_req,
816 NCI_DEACTIVATE_TYPE_IDLE_MODE,
817 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
818 if (rc)
819 return -EBUSY;
822 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
823 rc = nci_set_local_general_bytes(nfc_dev);
824 if (rc) {
825 pr_err("failed to set local general bytes\n");
826 return rc;
830 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
831 rc = nci_set_listen_parameters(nfc_dev);
832 if (rc)
833 pr_err("failed to set listen parameters\n");
836 param.im_protocols = im_protocols;
837 param.tm_protocols = tm_protocols;
838 rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
839 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
841 if (!rc)
842 ndev->poll_prots = im_protocols;
844 return rc;
847 static void nci_stop_poll(struct nfc_dev *nfc_dev)
849 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
851 if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
852 (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
853 pr_err("unable to stop poll, since poll is not active\n");
854 return;
857 nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
858 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
861 static int nci_activate_target(struct nfc_dev *nfc_dev,
862 struct nfc_target *target, __u32 protocol)
864 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
865 struct nci_rf_discover_select_param param;
866 struct nfc_target *nci_target = NULL;
867 int i;
868 int rc = 0;
870 pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
872 if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
873 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
874 pr_err("there is no available target to activate\n");
875 return -EINVAL;
878 if (ndev->target_active_prot) {
879 pr_err("there is already an active target\n");
880 return -EBUSY;
883 for (i = 0; i < ndev->n_targets; i++) {
884 if (ndev->targets[i].idx == target->idx) {
885 nci_target = &ndev->targets[i];
886 break;
890 if (!nci_target) {
891 pr_err("unable to find the selected target\n");
892 return -EINVAL;
895 if (!(nci_target->supported_protocols & (1 << protocol))) {
896 pr_err("target does not support the requested protocol 0x%x\n",
897 protocol);
898 return -EINVAL;
901 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
902 param.rf_discovery_id = nci_target->logical_idx;
904 if (protocol == NFC_PROTO_JEWEL)
905 param.rf_protocol = NCI_RF_PROTOCOL_T1T;
906 else if (protocol == NFC_PROTO_MIFARE)
907 param.rf_protocol = NCI_RF_PROTOCOL_T2T;
908 else if (protocol == NFC_PROTO_FELICA)
909 param.rf_protocol = NCI_RF_PROTOCOL_T3T;
910 else if (protocol == NFC_PROTO_ISO14443 ||
911 protocol == NFC_PROTO_ISO14443_B)
912 param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
913 else
914 param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
916 rc = nci_request(ndev, nci_rf_discover_select_req,
917 (unsigned long)&param,
918 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
921 if (!rc)
922 ndev->target_active_prot = protocol;
924 return rc;
927 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
928 struct nfc_target *target,
929 __u8 mode)
931 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
932 u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
934 pr_debug("entry\n");
936 if (!ndev->target_active_prot) {
937 pr_err("unable to deactivate target, no active target\n");
938 return;
941 ndev->target_active_prot = 0;
943 switch (mode) {
944 case NFC_TARGET_MODE_SLEEP:
945 nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
946 break;
949 if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
950 nci_request(ndev, nci_rf_deactivate_req, nci_mode,
951 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
955 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
956 __u8 comm_mode, __u8 *gb, size_t gb_len)
958 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
959 int rc;
961 pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
963 rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
964 if (rc)
965 return rc;
967 rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
968 ndev->remote_gb_len);
969 if (!rc)
970 rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
971 NFC_RF_INITIATOR);
973 return rc;
976 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
978 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
979 int rc;
981 pr_debug("entry\n");
983 if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
984 nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
985 } else {
986 if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
987 atomic_read(&ndev->state) == NCI_DISCOVERY) {
988 nci_request(ndev, nci_rf_deactivate_req, 0,
989 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
992 rc = nfc_tm_deactivated(nfc_dev);
993 if (rc)
994 pr_err("error when signaling tm deactivation\n");
997 return 0;
1001 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1002 struct sk_buff *skb,
1003 data_exchange_cb_t cb, void *cb_context)
1005 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1006 int rc;
1007 struct nci_conn_info *conn_info;
1009 conn_info = ndev->rf_conn_info;
1010 if (!conn_info)
1011 return -EPROTO;
1013 pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
1015 if (!ndev->target_active_prot) {
1016 pr_err("unable to exchange data, no active target\n");
1017 return -EINVAL;
1020 if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1021 return -EBUSY;
1023 /* store cb and context to be used on receiving data */
1024 conn_info->data_exchange_cb = cb;
1025 conn_info->data_exchange_cb_context = cb_context;
1027 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1028 if (rc)
1029 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1031 return rc;
1034 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1036 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1037 int rc;
1039 rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1040 if (rc)
1041 pr_err("unable to send data\n");
1043 return rc;
1046 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1048 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1050 if (ndev->ops->enable_se)
1051 return ndev->ops->enable_se(ndev, se_idx);
1053 return 0;
1056 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1058 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1060 if (ndev->ops->disable_se)
1061 return ndev->ops->disable_se(ndev, se_idx);
1063 return 0;
1066 static int nci_discover_se(struct nfc_dev *nfc_dev)
1068 int r;
1069 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1071 if (ndev->ops->discover_se) {
1072 r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1073 if (r != NCI_STATUS_OK)
1074 return -EPROTO;
1076 return ndev->ops->discover_se(ndev);
1079 return 0;
1082 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1083 u8 *apdu, size_t apdu_length,
1084 se_io_cb_t cb, void *cb_context)
1086 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1088 if (ndev->ops->se_io)
1089 return ndev->ops->se_io(ndev, se_idx, apdu,
1090 apdu_length, cb, cb_context);
1092 return 0;
1095 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1097 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1099 if (!ndev->ops->fw_download)
1100 return -ENOTSUPP;
1102 return ndev->ops->fw_download(ndev, firmware_name);
1105 static struct nfc_ops nci_nfc_ops = {
1106 .dev_up = nci_dev_up,
1107 .dev_down = nci_dev_down,
1108 .start_poll = nci_start_poll,
1109 .stop_poll = nci_stop_poll,
1110 .dep_link_up = nci_dep_link_up,
1111 .dep_link_down = nci_dep_link_down,
1112 .activate_target = nci_activate_target,
1113 .deactivate_target = nci_deactivate_target,
1114 .im_transceive = nci_transceive,
1115 .tm_send = nci_tm_send,
1116 .enable_se = nci_enable_se,
1117 .disable_se = nci_disable_se,
1118 .discover_se = nci_discover_se,
1119 .se_io = nci_se_io,
1120 .fw_download = nci_fw_download,
1123 /* ---- Interface to NCI drivers ---- */
1125 * nci_allocate_device - allocate a new nci device
1127 * @ops: device operations
1128 * @supported_protocols: NFC protocols supported by the device
1129 * @tx_headroom: Reserved space at beginning of skb
1130 * @tx_tailroom: Reserved space at end of skb
1132 struct nci_dev *nci_allocate_device(struct nci_ops *ops,
1133 __u32 supported_protocols,
1134 int tx_headroom, int tx_tailroom)
1136 struct nci_dev *ndev;
1138 pr_debug("supported_protocols 0x%x\n", supported_protocols);
1140 if (!ops->open || !ops->close || !ops->send)
1141 return NULL;
1143 if (!supported_protocols)
1144 return NULL;
1146 ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1147 if (!ndev)
1148 return NULL;
1150 ndev->ops = ops;
1152 if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1153 pr_err("Too many proprietary commands: %zd\n",
1154 ops->n_prop_ops);
1155 ops->prop_ops = NULL;
1156 ops->n_prop_ops = 0;
1159 ndev->tx_headroom = tx_headroom;
1160 ndev->tx_tailroom = tx_tailroom;
1161 init_completion(&ndev->req_completion);
1163 ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1164 supported_protocols,
1165 tx_headroom + NCI_DATA_HDR_SIZE,
1166 tx_tailroom);
1167 if (!ndev->nfc_dev)
1168 goto free_nci;
1170 ndev->hci_dev = nci_hci_allocate(ndev);
1171 if (!ndev->hci_dev)
1172 goto free_nfc;
1174 nfc_set_drvdata(ndev->nfc_dev, ndev);
1176 return ndev;
1178 free_nfc:
1179 nfc_free_device(ndev->nfc_dev);
1180 free_nci:
1181 kfree(ndev);
1182 return NULL;
1184 EXPORT_SYMBOL(nci_allocate_device);
1187 * nci_free_device - deallocate nci device
1189 * @ndev: The nci device to deallocate
1191 void nci_free_device(struct nci_dev *ndev)
1193 nfc_free_device(ndev->nfc_dev);
1194 kfree(ndev);
1196 EXPORT_SYMBOL(nci_free_device);
1199 * nci_register_device - register a nci device in the nfc subsystem
1201 * @ndev: The nci device to register
1203 int nci_register_device(struct nci_dev *ndev)
1205 int rc;
1206 struct device *dev = &ndev->nfc_dev->dev;
1207 char name[32];
1209 ndev->flags = 0;
1211 INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1212 snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1213 ndev->cmd_wq = create_singlethread_workqueue(name);
1214 if (!ndev->cmd_wq) {
1215 rc = -ENOMEM;
1216 goto exit;
1219 INIT_WORK(&ndev->rx_work, nci_rx_work);
1220 snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1221 ndev->rx_wq = create_singlethread_workqueue(name);
1222 if (!ndev->rx_wq) {
1223 rc = -ENOMEM;
1224 goto destroy_cmd_wq_exit;
1227 INIT_WORK(&ndev->tx_work, nci_tx_work);
1228 snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1229 ndev->tx_wq = create_singlethread_workqueue(name);
1230 if (!ndev->tx_wq) {
1231 rc = -ENOMEM;
1232 goto destroy_rx_wq_exit;
1235 skb_queue_head_init(&ndev->cmd_q);
1236 skb_queue_head_init(&ndev->rx_q);
1237 skb_queue_head_init(&ndev->tx_q);
1239 timer_setup(&ndev->cmd_timer, nci_cmd_timer, 0);
1240 timer_setup(&ndev->data_timer, nci_data_timer, 0);
1242 mutex_init(&ndev->req_lock);
1243 INIT_LIST_HEAD(&ndev->conn_info_list);
1245 rc = nfc_register_device(ndev->nfc_dev);
1246 if (rc)
1247 goto destroy_tx_wq_exit;
1249 goto exit;
1251 destroy_tx_wq_exit:
1252 destroy_workqueue(ndev->tx_wq);
1254 destroy_rx_wq_exit:
1255 destroy_workqueue(ndev->rx_wq);
1257 destroy_cmd_wq_exit:
1258 destroy_workqueue(ndev->cmd_wq);
1260 exit:
1261 return rc;
1263 EXPORT_SYMBOL(nci_register_device);
1266 * nci_unregister_device - unregister a nci device in the nfc subsystem
1268 * @ndev: The nci device to unregister
1270 void nci_unregister_device(struct nci_dev *ndev)
1272 struct nci_conn_info *conn_info, *n;
1274 nci_close_device(ndev);
1276 destroy_workqueue(ndev->cmd_wq);
1277 destroy_workqueue(ndev->rx_wq);
1278 destroy_workqueue(ndev->tx_wq);
1280 list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1281 list_del(&conn_info->list);
1282 /* conn_info is allocated with devm_kzalloc */
1285 nfc_unregister_device(ndev->nfc_dev);
1287 EXPORT_SYMBOL(nci_unregister_device);
1290 * nci_recv_frame - receive frame from NCI drivers
1292 * @ndev: The nci device
1293 * @skb: The sk_buff to receive
1295 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1297 pr_debug("len %d\n", skb->len);
1299 if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1300 !test_bit(NCI_INIT, &ndev->flags))) {
1301 kfree_skb(skb);
1302 return -ENXIO;
1305 /* Queue frame for rx worker thread */
1306 skb_queue_tail(&ndev->rx_q, skb);
1307 queue_work(ndev->rx_wq, &ndev->rx_work);
1309 return 0;
1311 EXPORT_SYMBOL(nci_recv_frame);
1313 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1315 pr_debug("len %d\n", skb->len);
1317 if (!ndev) {
1318 kfree_skb(skb);
1319 return -ENODEV;
1322 /* Get rid of skb owner, prior to sending to the driver. */
1323 skb_orphan(skb);
1325 /* Send copy to sniffer */
1326 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1327 RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1329 return ndev->ops->send(ndev, skb);
1331 EXPORT_SYMBOL(nci_send_frame);
1333 /* Send NCI command */
1334 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1336 struct nci_ctrl_hdr *hdr;
1337 struct sk_buff *skb;
1339 pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1341 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1342 if (!skb) {
1343 pr_err("no memory for command\n");
1344 return -ENOMEM;
1347 hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
1348 hdr->gid = nci_opcode_gid(opcode);
1349 hdr->oid = nci_opcode_oid(opcode);
1350 hdr->plen = plen;
1352 nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1353 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1355 if (plen)
1356 skb_put_data(skb, payload, plen);
1358 skb_queue_tail(&ndev->cmd_q, skb);
1359 queue_work(ndev->cmd_wq, &ndev->cmd_work);
1361 return 0;
1363 EXPORT_SYMBOL(nci_send_cmd);
1365 /* Proprietary commands API */
1366 static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
1367 size_t n_ops,
1368 __u16 opcode)
1370 size_t i;
1371 struct nci_driver_ops *op;
1373 if (!ops || !n_ops)
1374 return NULL;
1376 for (i = 0; i < n_ops; i++) {
1377 op = &ops[i];
1378 if (op->opcode == opcode)
1379 return op;
1382 return NULL;
1385 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1386 struct sk_buff *skb, struct nci_driver_ops *ops,
1387 size_t n_ops)
1389 struct nci_driver_ops *op;
1391 op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1392 if (!op || !op->rsp)
1393 return -ENOTSUPP;
1395 return op->rsp(ndev, skb);
1398 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1399 struct sk_buff *skb, struct nci_driver_ops *ops,
1400 size_t n_ops)
1402 struct nci_driver_ops *op;
1404 op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1405 if (!op || !op->ntf)
1406 return -ENOTSUPP;
1408 return op->ntf(ndev, skb);
1411 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1412 struct sk_buff *skb)
1414 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1415 ndev->ops->n_prop_ops);
1418 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1419 struct sk_buff *skb)
1421 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1422 ndev->ops->n_prop_ops);
1425 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1426 struct sk_buff *skb)
1428 return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1429 ndev->ops->n_core_ops);
1432 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1433 struct sk_buff *skb)
1435 return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1436 ndev->ops->n_core_ops);
1439 /* ---- NCI TX Data worker thread ---- */
1441 static void nci_tx_work(struct work_struct *work)
1443 struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1444 struct nci_conn_info *conn_info;
1445 struct sk_buff *skb;
1447 conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1448 if (!conn_info)
1449 return;
1451 pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1453 /* Send queued tx data */
1454 while (atomic_read(&conn_info->credits_cnt)) {
1455 skb = skb_dequeue(&ndev->tx_q);
1456 if (!skb)
1457 return;
1459 /* Check if data flow control is used */
1460 if (atomic_read(&conn_info->credits_cnt) !=
1461 NCI_DATA_FLOW_CONTROL_NOT_USED)
1462 atomic_dec(&conn_info->credits_cnt);
1464 pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1465 nci_pbf(skb->data),
1466 nci_conn_id(skb->data),
1467 nci_plen(skb->data));
1469 nci_send_frame(ndev, skb);
1471 mod_timer(&ndev->data_timer,
1472 jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1476 /* ----- NCI RX worker thread (data & control) ----- */
1478 static void nci_rx_work(struct work_struct *work)
1480 struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1481 struct sk_buff *skb;
1483 while ((skb = skb_dequeue(&ndev->rx_q))) {
1485 /* Send copy to sniffer */
1486 nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1487 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1489 /* Process frame */
1490 switch (nci_mt(skb->data)) {
1491 case NCI_MT_RSP_PKT:
1492 nci_rsp_packet(ndev, skb);
1493 break;
1495 case NCI_MT_NTF_PKT:
1496 nci_ntf_packet(ndev, skb);
1497 break;
1499 case NCI_MT_DATA_PKT:
1500 nci_rx_data_packet(ndev, skb);
1501 break;
1503 default:
1504 pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1505 kfree_skb(skb);
1506 break;
1510 /* check if a data exchange timout has occurred */
1511 if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1512 /* complete the data exchange transaction, if exists */
1513 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1514 nci_data_exchange_complete(ndev, NULL,
1515 ndev->cur_conn_id,
1516 -ETIMEDOUT);
1518 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1522 /* ----- NCI TX CMD worker thread ----- */
1524 static void nci_cmd_work(struct work_struct *work)
1526 struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1527 struct sk_buff *skb;
1529 pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1531 /* Send queued command */
1532 if (atomic_read(&ndev->cmd_cnt)) {
1533 skb = skb_dequeue(&ndev->cmd_q);
1534 if (!skb)
1535 return;
1537 atomic_dec(&ndev->cmd_cnt);
1539 pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1540 nci_pbf(skb->data),
1541 nci_opcode_gid(nci_opcode(skb->data)),
1542 nci_opcode_oid(nci_opcode(skb->data)),
1543 nci_plen(skb->data));
1545 nci_send_frame(ndev, skb);
1547 mod_timer(&ndev->cmd_timer,
1548 jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1552 MODULE_LICENSE("GPL");