2019-06-03 12:44:50 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2011-09-18 15:19:35 +07:00
|
|
|
/*
|
|
|
|
* The NFC Controller Interface is the communication protocol between an
|
|
|
|
* NFC Controller (NFCC) and a Device Host (DH).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Texas Instruments, Inc.
|
2014-10-21 21:52:49 +07:00
|
|
|
* Copyright (C) 2014 Marvell International Ltd.
|
2011-09-18 15:19:35 +07:00
|
|
|
*
|
|
|
|
* Written by Ilan Elias <ilane@ti.com>
|
|
|
|
*/
|
|
|
|
|
2011-12-14 22:43:05 +07:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
|
2011-11-30 02:37:32 +07:00
|
|
|
|
2011-09-18 15:19:35 +07:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/wait.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>
|
|
|
|
|
|
|
|
/* Complete data exchange transaction and forward skb to nfc core */
|
2012-03-05 07:03:54 +07:00
|
|
|
void nci_data_exchange_complete(struct nci_dev *ndev, struct sk_buff *skb,
|
2015-02-02 04:26:08 +07:00
|
|
|
__u8 conn_id, int err)
|
2011-09-18 15:19:35 +07:00
|
|
|
{
|
2015-02-02 04:26:08 +07:00
|
|
|
struct nci_conn_info *conn_info;
|
|
|
|
data_exchange_cb_t cb;
|
|
|
|
void *cb_context;
|
|
|
|
|
|
|
|
conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
|
|
|
|
if (!conn_info) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb = conn_info->data_exchange_cb;
|
|
|
|
cb_context = conn_info->data_exchange_cb_context;
|
2011-09-18 15:19:35 +07:00
|
|
|
|
2011-11-30 02:37:35 +07:00
|
|
|
pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
|
2011-09-18 15:19:35 +07:00
|
|
|
|
2012-01-17 17:03:50 +07:00
|
|
|
/* data exchange is complete, stop the data timer */
|
|
|
|
del_timer_sync(&ndev->data_timer);
|
|
|
|
clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
|
|
|
|
|
2011-09-18 15:19:35 +07:00
|
|
|
if (cb) {
|
|
|
|
/* forward skb to nfc core */
|
|
|
|
cb(cb_context, skb, err);
|
|
|
|
} else if (skb) {
|
2011-11-30 02:37:32 +07:00
|
|
|
pr_err("no rx callback, dropping rx data...\n");
|
2011-09-18 15:19:35 +07:00
|
|
|
|
|
|
|
/* no waiting callback, free skb */
|
|
|
|
kfree_skb(skb);
|
|
|
|
}
|
2011-09-22 15:36:19 +07:00
|
|
|
|
2015-02-02 04:26:08 +07:00
|
|
|
exit:
|
2011-09-22 15:36:19 +07:00
|
|
|
clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
|
2011-09-18 15:19:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------- NCI TX Data ----------------- */
|
|
|
|
|
|
|
|
static inline void nci_push_data_hdr(struct nci_dev *ndev,
|
2012-03-05 07:03:54 +07:00
|
|
|
__u8 conn_id,
|
|
|
|
struct sk_buff *skb,
|
|
|
|
__u8 pbf)
|
2011-09-18 15:19:35 +07:00
|
|
|
{
|
|
|
|
struct nci_data_hdr *hdr;
|
|
|
|
int plen = skb->len;
|
|
|
|
|
networking: make skb_push & __skb_push return void pointers
It seems like a historic accident that these return unsigned char *,
and in many places that means casts are required, more often than not.
Make these functions return void * and remove all the casts across
the tree, adding a (u8 *) cast only where the unsigned char pointer
was used directly, all done with the following spatch:
@@
expression SKB, LEN;
typedef u8;
identifier fn = { skb_push, __skb_push, skb_push_rcsum };
@@
- *(fn(SKB, LEN))
+ *(u8 *)fn(SKB, LEN)
@@
expression E, SKB, LEN;
identifier fn = { skb_push, __skb_push, skb_push_rcsum };
type T;
@@
- E = ((T *)(fn(SKB, LEN)))
+ E = fn(SKB, LEN)
@@
expression SKB, LEN;
identifier fn = { skb_push, __skb_push, skb_push_rcsum };
@@
- fn(SKB, LEN)[0]
+ *(u8 *)fn(SKB, LEN)
Note that the last part there converts from push(...)[0] to the
more idiomatic *(u8 *)push(...).
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-06-16 19:29:23 +07:00
|
|
|
hdr = skb_push(skb, NCI_DATA_HDR_SIZE);
|
2011-09-18 15:19:35 +07:00
|
|
|
hdr->conn_id = conn_id;
|
|
|
|
hdr->rfu = 0;
|
|
|
|
hdr->plen = plen;
|
|
|
|
|
|
|
|
nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
|
|
|
|
nci_pbf_set((__u8 *)hdr, pbf);
|
|
|
|
}
|
|
|
|
|
2015-10-22 16:11:34 +07:00
|
|
|
int nci_conn_max_data_pkt_payload_size(struct nci_dev *ndev, __u8 conn_id)
|
|
|
|
{
|
|
|
|
struct nci_conn_info *conn_info;
|
|
|
|
|
|
|
|
conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
|
|
|
|
if (!conn_info)
|
|
|
|
return -EPROTO;
|
|
|
|
|
|
|
|
return conn_info->max_pkt_payload_len;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(nci_conn_max_data_pkt_payload_size);
|
|
|
|
|
2011-09-18 15:19:35 +07:00
|
|
|
static int nci_queue_tx_data_frags(struct nci_dev *ndev,
|
2012-03-05 07:03:54 +07:00
|
|
|
__u8 conn_id,
|
|
|
|
struct sk_buff *skb) {
|
2015-02-02 04:26:08 +07:00
|
|
|
struct nci_conn_info *conn_info;
|
2011-09-18 15:19:35 +07:00
|
|
|
int total_len = skb->len;
|
|
|
|
unsigned char *data = skb->data;
|
|
|
|
unsigned long flags;
|
|
|
|
struct sk_buff_head frags_q;
|
|
|
|
struct sk_buff *skb_frag;
|
|
|
|
int frag_len;
|
|
|
|
int rc = 0;
|
|
|
|
|
2011-11-30 02:37:35 +07:00
|
|
|
pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
|
2011-09-18 15:19:35 +07:00
|
|
|
|
2015-02-02 04:26:08 +07:00
|
|
|
conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
|
|
|
|
if (!conn_info) {
|
|
|
|
rc = -EPROTO;
|
2019-07-08 21:57:39 +07:00
|
|
|
goto exit;
|
2015-02-02 04:26:08 +07:00
|
|
|
}
|
|
|
|
|
2011-09-18 15:19:35 +07:00
|
|
|
__skb_queue_head_init(&frags_q);
|
|
|
|
|
|
|
|
while (total_len) {
|
2011-11-09 17:09:14 +07:00
|
|
|
frag_len =
|
2015-02-02 04:26:08 +07:00
|
|
|
min_t(int, total_len, conn_info->max_pkt_payload_len);
|
2011-09-18 15:19:35 +07:00
|
|
|
|
|
|
|
skb_frag = nci_skb_alloc(ndev,
|
2012-03-05 07:03:54 +07:00
|
|
|
(NCI_DATA_HDR_SIZE + frag_len),
|
|
|
|
GFP_KERNEL);
|
2011-09-18 15:19:35 +07:00
|
|
|
if (skb_frag == NULL) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto free_exit;
|
|
|
|
}
|
|
|
|
skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
|
|
|
|
|
|
|
|
/* first, copy the data */
|
networking: introduce and use skb_put_data()
A common pattern with skb_put() is to just want to memcpy()
some data into the new space, introduce skb_put_data() for
this.
An spatch similar to the one for skb_put_zero() converts many
of the places using it:
@@
identifier p, p2;
expression len, skb, data;
type t, t2;
@@
(
-p = skb_put(skb, len);
+p = skb_put_data(skb, data, len);
|
-p = (t)skb_put(skb, len);
+p = skb_put_data(skb, data, len);
)
(
p2 = (t2)p;
-memcpy(p2, data, len);
|
-memcpy(p, data, len);
)
@@
type t, t2;
identifier p, p2;
expression skb, data;
@@
t *p;
...
(
-p = skb_put(skb, sizeof(t));
+p = skb_put_data(skb, data, sizeof(t));
|
-p = (t *)skb_put(skb, sizeof(t));
+p = skb_put_data(skb, data, sizeof(t));
)
(
p2 = (t2)p;
-memcpy(p2, data, sizeof(*p));
|
-memcpy(p, data, sizeof(*p));
)
@@
expression skb, len, data;
@@
-memcpy(skb_put(skb, len), data, len);
+skb_put_data(skb, data, len);
(again, manually post-processed to retain some comments)
Reviewed-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-06-16 19:29:20 +07:00
|
|
|
skb_put_data(skb_frag, data, frag_len);
|
2011-09-18 15:19:35 +07:00
|
|
|
|
|
|
|
/* second, set the header */
|
|
|
|
nci_push_data_hdr(ndev, conn_id, skb_frag,
|
2012-03-05 07:03:54 +07:00
|
|
|
((total_len == frag_len) ?
|
|
|
|
(NCI_PBF_LAST) : (NCI_PBF_CONT)));
|
2011-09-18 15:19:35 +07:00
|
|
|
|
|
|
|
__skb_queue_tail(&frags_q, skb_frag);
|
|
|
|
|
|
|
|
data += frag_len;
|
|
|
|
total_len -= frag_len;
|
|
|
|
|
2011-11-30 02:37:33 +07:00
|
|
|
pr_debug("frag_len %d, remaining total_len %d\n",
|
|
|
|
frag_len, total_len);
|
2011-09-18 15:19:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* queue all fragments atomically */
|
|
|
|
spin_lock_irqsave(&ndev->tx_q.lock, flags);
|
|
|
|
|
|
|
|
while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
|
|
|
|
__skb_queue_tail(&ndev->tx_q, skb_frag);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
|
|
|
|
|
|
|
|
/* free the original skb */
|
|
|
|
kfree_skb(skb);
|
|
|
|
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
free_exit:
|
|
|
|
while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
|
|
|
|
kfree_skb(skb_frag);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send NCI data */
|
|
|
|
int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
|
|
|
|
{
|
2015-02-02 04:26:08 +07:00
|
|
|
struct nci_conn_info *conn_info;
|
2011-09-18 15:19:35 +07:00
|
|
|
int rc = 0;
|
|
|
|
|
2011-11-30 02:37:35 +07:00
|
|
|
pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
|
2011-09-18 15:19:35 +07:00
|
|
|
|
2015-02-02 04:26:08 +07:00
|
|
|
conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
|
|
|
|
if (!conn_info) {
|
|
|
|
rc = -EPROTO;
|
|
|
|
goto free_exit;
|
|
|
|
}
|
|
|
|
|
2011-09-18 15:19:35 +07:00
|
|
|
/* check if the packet need to be fragmented */
|
2015-02-02 04:26:08 +07:00
|
|
|
if (skb->len <= conn_info->max_pkt_payload_len) {
|
2011-09-18 15:19:35 +07:00
|
|
|
/* no need to fragment packet */
|
|
|
|
nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
|
|
|
|
|
|
|
|
skb_queue_tail(&ndev->tx_q, skb);
|
|
|
|
} else {
|
|
|
|
/* fragment packet and queue the fragments */
|
|
|
|
rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
|
|
|
|
if (rc) {
|
2011-11-30 02:37:32 +07:00
|
|
|
pr_err("failed to fragment tx data packet\n");
|
2011-09-18 15:19:35 +07:00
|
|
|
goto free_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 04:26:08 +07:00
|
|
|
ndev->cur_conn_id = conn_id;
|
2011-09-18 15:19:35 +07:00
|
|
|
queue_work(ndev->tx_wq, &ndev->tx_work);
|
|
|
|
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
free_exit:
|
|
|
|
kfree_skb(skb);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return rc;
|
|
|
|
}
|
2015-10-22 16:11:33 +07:00
|
|
|
EXPORT_SYMBOL(nci_send_data);
|
2011-09-18 15:19:35 +07:00
|
|
|
|
|
|
|
/* ----------------- NCI RX Data ----------------- */
|
|
|
|
|
|
|
|
static void nci_add_rx_data_frag(struct nci_dev *ndev,
|
2012-03-05 07:03:54 +07:00
|
|
|
struct sk_buff *skb,
|
2015-02-02 04:26:08 +07:00
|
|
|
__u8 pbf, __u8 conn_id, __u8 status)
|
2011-09-18 15:19:35 +07:00
|
|
|
{
|
|
|
|
int reassembly_len;
|
|
|
|
int err = 0;
|
|
|
|
|
2014-12-03 03:27:47 +07:00
|
|
|
if (status) {
|
|
|
|
err = status;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2011-09-18 15:19:35 +07:00
|
|
|
if (ndev->rx_data_reassembly) {
|
|
|
|
reassembly_len = ndev->rx_data_reassembly->len;
|
|
|
|
|
|
|
|
/* first, make enough room for the already accumulated data */
|
|
|
|
if (skb_cow_head(skb, reassembly_len)) {
|
2011-11-30 02:37:32 +07:00
|
|
|
pr_err("error adding room for accumulated rx data\n");
|
2011-09-18 15:19:35 +07:00
|
|
|
|
|
|
|
kfree_skb(skb);
|
2012-05-07 17:31:24 +07:00
|
|
|
skb = NULL;
|
2011-09-18 15:19:35 +07:00
|
|
|
|
|
|
|
kfree_skb(ndev->rx_data_reassembly);
|
2012-05-07 17:31:24 +07:00
|
|
|
ndev->rx_data_reassembly = NULL;
|
2011-09-18 15:19:35 +07:00
|
|
|
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* second, combine the two fragments */
|
|
|
|
memcpy(skb_push(skb, reassembly_len),
|
2012-03-05 07:03:54 +07:00
|
|
|
ndev->rx_data_reassembly->data,
|
|
|
|
reassembly_len);
|
2011-09-18 15:19:35 +07:00
|
|
|
|
|
|
|
/* third, free old reassembly */
|
|
|
|
kfree_skb(ndev->rx_data_reassembly);
|
2012-05-07 17:31:24 +07:00
|
|
|
ndev->rx_data_reassembly = NULL;
|
2011-09-18 15:19:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pbf == NCI_PBF_CONT) {
|
|
|
|
/* need to wait for next fragment, store skb and exit */
|
|
|
|
ndev->rx_data_reassembly = skb;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2015-02-02 04:26:08 +07:00
|
|
|
if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) {
|
2014-10-21 21:52:49 +07:00
|
|
|
/* Data received in Target mode, forward to nfc core */
|
|
|
|
err = nfc_tm_data_received(ndev->nfc_dev, skb);
|
|
|
|
if (err)
|
|
|
|
pr_err("unable to handle received data\n");
|
|
|
|
} else {
|
2015-02-02 04:26:08 +07:00
|
|
|
nci_data_exchange_complete(ndev, skb, conn_id, err);
|
2014-10-21 21:52:49 +07:00
|
|
|
}
|
2011-09-18 15:19:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Rx Data packet */
|
|
|
|
void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
__u8 pbf = nci_pbf(skb->data);
|
2014-12-03 03:27:47 +07:00
|
|
|
__u8 status = 0;
|
2015-02-02 04:26:08 +07:00
|
|
|
__u8 conn_id = nci_conn_id(skb->data);
|
|
|
|
struct nci_conn_info *conn_info;
|
2011-09-18 15:19:35 +07:00
|
|
|
|
2011-11-30 02:37:35 +07:00
|
|
|
pr_debug("len %d\n", skb->len);
|
2011-09-18 15:19:35 +07:00
|
|
|
|
2011-11-30 02:37:33 +07:00
|
|
|
pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
|
|
|
|
nci_pbf(skb->data),
|
|
|
|
nci_conn_id(skb->data),
|
|
|
|
nci_plen(skb->data));
|
2011-09-18 15:19:35 +07:00
|
|
|
|
2015-02-02 04:26:08 +07:00
|
|
|
conn_info = nci_get_conn_info_by_conn_id(ndev, nci_conn_id(skb->data));
|
|
|
|
if (!conn_info)
|
|
|
|
return;
|
|
|
|
|
2011-09-18 15:19:35 +07:00
|
|
|
/* strip the nci data header */
|
|
|
|
skb_pull(skb, NCI_DATA_HDR_SIZE);
|
|
|
|
|
2014-07-23 00:48:40 +07:00
|
|
|
if (ndev->target_active_prot == NFC_PROTO_MIFARE ||
|
|
|
|
ndev->target_active_prot == NFC_PROTO_JEWEL ||
|
|
|
|
ndev->target_active_prot == NFC_PROTO_FELICA ||
|
|
|
|
ndev->target_active_prot == NFC_PROTO_ISO15693) {
|
2011-09-18 15:19:35 +07:00
|
|
|
/* frame I/F => remove the status byte */
|
2014-07-23 00:48:40 +07:00
|
|
|
pr_debug("frame I/F => remove the status byte\n");
|
2014-12-03 03:27:47 +07:00
|
|
|
status = skb->data[skb->len - 1];
|
2011-09-18 15:19:35 +07:00
|
|
|
skb_trim(skb, (skb->len - 1));
|
|
|
|
}
|
|
|
|
|
2015-02-02 04:26:08 +07:00
|
|
|
nci_add_rx_data_frag(ndev, skb, pbf, conn_id, nci_to_errno(status));
|
2011-09-18 15:19:35 +07:00
|
|
|
}
|