// SPDX-License-Identifier: GPL-2.0-only
/*
* The NFC Controller Interface is the communication protocol between an
* NFC Controller (NFCC) and a Device Host (DH).
*
* Copyright (C) 2014 Marvell International Ltd.
* Copyright (C) 2011 Texas Instruments, Inc.
*
* Written by Ilan Elias <ilane@ti.com>
*
* Acknowledgements:
* This file is based on hci_event.c, which was written
* by Maxim Krasnyansky.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/bitops.h>
#include <linux/skbuff.h>
#include "../nfc.h"
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
#include <linux/nfc.h>
/* Handle NCI Notification packets */
static int nci_core_reset_ntf_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
/* Handle NCI 2.x core reset notification */
const struct nci_core_reset_ntf *ntf;
if (skb->len < sizeof(struct nci_core_reset_ntf))
return -EINVAL;
ntf = (struct nci_core_reset_ntf *)skb->data;
ndev->nci_ver = ntf->nci_ver;
pr_debug("nci_ver 0x%x, config_status 0x%x\n",
ntf->nci_ver, ntf->config_status);
ndev->manufact_id = ntf->manufact_id;
ndev->manufact_specific_info =
__le32_to_cpu(ntf->manufact_specific_info);
nci_req_complete(ndev, NCI_STATUS_OK);
return 0;
}
static int nci_core_conn_credits_ntf_packet(struct nci_dev *ndev,
struct sk_buff *skb)
{
struct nci_core_conn_credit_ntf *ntf;
struct nci_conn_info *conn_info;
int i;
if (skb->len < sizeof(struct nci_core_conn_credit_ntf))
return -EINVAL;
ntf = (struct nci_core_conn_credit_ntf *)skb->data;
pr_debug("num_entries %d\n", ntf->num_entries);
if (ntf->num_entries > NCI_MAX_NUM_CONN)
ntf->num_entries = NCI_MAX_NUM_CONN;
/* update the credits */
for (i = 0; i < ntf->num_entries; i++) {
ntf->conn_entries[i].conn_id =
nci_conn_id(&ntf->conn_entries[i].conn_id);
pr_debug("entry[%d]: conn_id %d, credits %d\n",
i, ntf->conn_entries[i].conn_id,
ntf->conn_entries[i].credits);
conn_info = nci_get_conn_info_by_conn_id(ndev,
ntf->conn_entries[i].conn_id);
if (!conn_info)
return 0;
atomic_add(ntf->conn_entries[i].credits,
&conn_info->credits_cnt);
}
/* trigger the next tx */
if (!skb_queue_empty(&ndev->tx_q))
queue_work(ndev->tx_wq, &ndev->tx_work);
return 0;
}
static int nci_core_generic_error_ntf_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
__u8 status;
if (skb->len < 1)
return -EINVAL;
status = skb->data[0];
pr_debug("status 0x%x\n", status);
if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
/* Activation failed, so complete the request
(the state remains the same) */
nci_req_complete(ndev, status);
}
return 0;
}
static int nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev,
struct sk_buff *skb)
{
struct nci_core_intf_error_ntf *ntf;
if (skb->len < sizeof(struct nci_core_intf_error_ntf))
return -EINVAL;
ntf = (struct nci_core_intf_error_ntf *)skb->data;
ntf->conn_id = nci_conn_id(&ntf->conn_id);
pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id);
/* complete the data exchange transaction, if exists */
if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
nci_data_exchange_complete(ndev, NULL, ntf->conn_id, -EIO);
return 0;
}
static const __u8 *
nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev,
struct rf_tech_specific_params_nfca_poll *nfca_poll,
const __u8 *data)
{
nfca_poll->sens_res = __le16_to_cpu(*((__le16 *)data));
data += 2;
nfca_poll->nfcid1_len = min_t(__u8, *data++, NFC_NFCID1_MAXSIZE);
pr_debug("sens_res 0x%x, nfcid1_len %d\n",
nfca_poll->sens_res, nfca_poll->nfcid1_len);
memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len);
data += nfca_poll->nfcid1_len;
nfca_poll->sel_res_len = *data++;
if (nfca_poll->sel_res_len != 0)
nfca_poll->sel_res = *data++;
pr_debug("sel_res_len %d, sel_res 0x%x\n",
nfca_poll->sel_res_len,
nfca_poll->sel_res);
return data;
}
static const __u8 *
nci_extract_rf_params_nfcb_passive_poll(struct nci_dev *ndev,
struct rf_tech_specific_params_nfcb_poll *nfcb_poll,
const __u8