xref: /openbmc/linux/net/nfc/nci/data.c (revision 98ff416f)
16a2968aaSIlan Elias /*
26a2968aaSIlan Elias  *  The NFC Controller Interface is the communication protocol between an
36a2968aaSIlan Elias  *  NFC Controller (NFCC) and a Device Host (DH).
46a2968aaSIlan Elias  *
56a2968aaSIlan Elias  *  Copyright (C) 2011 Texas Instruments, Inc.
6122c1958SJulien Lefrique  *  Copyright (C) 2014 Marvell International Ltd.
76a2968aaSIlan Elias  *
86a2968aaSIlan Elias  *  Written by Ilan Elias <ilane@ti.com>
96a2968aaSIlan Elias  *
106a2968aaSIlan Elias  *  This program is free software; you can redistribute it and/or modify
116a2968aaSIlan Elias  *  it under the terms of the GNU General Public License version 2
126a2968aaSIlan Elias  *  as published by the Free Software Foundation
136a2968aaSIlan Elias  *
146a2968aaSIlan Elias  *  This program is distributed in the hope that it will be useful,
156a2968aaSIlan Elias  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
166a2968aaSIlan Elias  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
176a2968aaSIlan Elias  *  GNU General Public License for more details.
186a2968aaSIlan Elias  *
196a2968aaSIlan Elias  *  You should have received a copy of the GNU General Public License
2098b32decSJeff Kirsher  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
216a2968aaSIlan Elias  *
226a2968aaSIlan Elias  */
236a2968aaSIlan Elias 
2452858b51SSamuel Ortiz #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
25ed1e0ad8SJoe Perches 
266a2968aaSIlan Elias #include <linux/types.h>
276a2968aaSIlan Elias #include <linux/interrupt.h>
286a2968aaSIlan Elias #include <linux/wait.h>
296a2968aaSIlan Elias #include <linux/bitops.h>
306a2968aaSIlan Elias #include <linux/skbuff.h>
316a2968aaSIlan Elias 
326a2968aaSIlan Elias #include "../nfc.h"
336a2968aaSIlan Elias #include <net/nfc/nci.h>
346a2968aaSIlan Elias #include <net/nfc/nci_core.h>
356a2968aaSIlan Elias #include <linux/nfc.h>
366a2968aaSIlan Elias 
376a2968aaSIlan Elias /* Complete data exchange transaction and forward skb to nfc core */
38eb9bc6e9SSamuel Ortiz void nci_data_exchange_complete(struct nci_dev *ndev, struct sk_buff *skb,
396a2968aaSIlan Elias 				int err)
406a2968aaSIlan Elias {
416a2968aaSIlan Elias 	data_exchange_cb_t cb = ndev->data_exchange_cb;
426a2968aaSIlan Elias 	void *cb_context = ndev->data_exchange_cb_context;
436a2968aaSIlan Elias 
4424bf3304SJoe Perches 	pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
456a2968aaSIlan Elias 
46c4bf98b2SIlan Elias 	/* data exchange is complete, stop the data timer */
47c4bf98b2SIlan Elias 	del_timer_sync(&ndev->data_timer);
48c4bf98b2SIlan Elias 	clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
49c4bf98b2SIlan Elias 
506a2968aaSIlan Elias 	if (cb) {
516a2968aaSIlan Elias 		ndev->data_exchange_cb = NULL;
52040487f3SH Hartley Sweeten 		ndev->data_exchange_cb_context = NULL;
536a2968aaSIlan Elias 
546a2968aaSIlan Elias 		/* forward skb to nfc core */
556a2968aaSIlan Elias 		cb(cb_context, skb, err);
566a2968aaSIlan Elias 	} else if (skb) {
57ed1e0ad8SJoe Perches 		pr_err("no rx callback, dropping rx data...\n");
586a2968aaSIlan Elias 
596a2968aaSIlan Elias 		/* no waiting callback, free skb */
606a2968aaSIlan Elias 		kfree_skb(skb);
616a2968aaSIlan Elias 	}
6238f04c6bSIlan Elias 
6338f04c6bSIlan Elias 	clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
646a2968aaSIlan Elias }
656a2968aaSIlan Elias 
666a2968aaSIlan Elias /* ----------------- NCI TX Data ----------------- */
676a2968aaSIlan Elias 
686a2968aaSIlan Elias static inline void nci_push_data_hdr(struct nci_dev *ndev,
696a2968aaSIlan Elias 				     __u8 conn_id,
706a2968aaSIlan Elias 				     struct sk_buff *skb,
716a2968aaSIlan Elias 				     __u8 pbf)
726a2968aaSIlan Elias {
736a2968aaSIlan Elias 	struct nci_data_hdr *hdr;
746a2968aaSIlan Elias 	int plen = skb->len;
756a2968aaSIlan Elias 
766a2968aaSIlan Elias 	hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE);
776a2968aaSIlan Elias 	hdr->conn_id = conn_id;
786a2968aaSIlan Elias 	hdr->rfu = 0;
796a2968aaSIlan Elias 	hdr->plen = plen;
806a2968aaSIlan Elias 
816a2968aaSIlan Elias 	nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
826a2968aaSIlan Elias 	nci_pbf_set((__u8 *)hdr, pbf);
836a2968aaSIlan Elias }
846a2968aaSIlan Elias 
856a2968aaSIlan Elias static int nci_queue_tx_data_frags(struct nci_dev *ndev,
866a2968aaSIlan Elias 				   __u8 conn_id,
876a2968aaSIlan Elias 				   struct sk_buff *skb) {
886a2968aaSIlan Elias 	int total_len = skb->len;
896a2968aaSIlan Elias 	unsigned char *data = skb->data;
906a2968aaSIlan Elias 	unsigned long flags;
916a2968aaSIlan Elias 	struct sk_buff_head frags_q;
926a2968aaSIlan Elias 	struct sk_buff *skb_frag;
936a2968aaSIlan Elias 	int frag_len;
946a2968aaSIlan Elias 	int rc = 0;
956a2968aaSIlan Elias 
9624bf3304SJoe Perches 	pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
976a2968aaSIlan Elias 
986a2968aaSIlan Elias 	__skb_queue_head_init(&frags_q);
996a2968aaSIlan Elias 
1006a2968aaSIlan Elias 	while (total_len) {
101e8c0dacdSIlan Elias 		frag_len =
102e8c0dacdSIlan Elias 			min_t(int, total_len, ndev->max_data_pkt_payload_size);
1036a2968aaSIlan Elias 
1046a2968aaSIlan Elias 		skb_frag = nci_skb_alloc(ndev,
1056a2968aaSIlan Elias 					 (NCI_DATA_HDR_SIZE + frag_len),
1066a2968aaSIlan Elias 					 GFP_KERNEL);
1076a2968aaSIlan Elias 		if (skb_frag == NULL) {
1086a2968aaSIlan Elias 			rc = -ENOMEM;
1096a2968aaSIlan Elias 			goto free_exit;
1106a2968aaSIlan Elias 		}
1116a2968aaSIlan Elias 		skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
1126a2968aaSIlan Elias 
1136a2968aaSIlan Elias 		/* first, copy the data */
1146a2968aaSIlan Elias 		memcpy(skb_put(skb_frag, frag_len), data, frag_len);
1156a2968aaSIlan Elias 
1166a2968aaSIlan Elias 		/* second, set the header */
1176a2968aaSIlan Elias 		nci_push_data_hdr(ndev, conn_id, skb_frag,
118eb9bc6e9SSamuel Ortiz 				  ((total_len == frag_len) ?
119eb9bc6e9SSamuel Ortiz 				   (NCI_PBF_LAST) : (NCI_PBF_CONT)));
1206a2968aaSIlan Elias 
1216a2968aaSIlan Elias 		__skb_queue_tail(&frags_q, skb_frag);
1226a2968aaSIlan Elias 
1236a2968aaSIlan Elias 		data += frag_len;
1246a2968aaSIlan Elias 		total_len -= frag_len;
1256a2968aaSIlan Elias 
12620c239c1SJoe Perches 		pr_debug("frag_len %d, remaining total_len %d\n",
1276a2968aaSIlan Elias 			 frag_len, total_len);
1286a2968aaSIlan Elias 	}
1296a2968aaSIlan Elias 
1306a2968aaSIlan Elias 	/* queue all fragments atomically */
1316a2968aaSIlan Elias 	spin_lock_irqsave(&ndev->tx_q.lock, flags);
1326a2968aaSIlan Elias 
1336a2968aaSIlan Elias 	while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
1346a2968aaSIlan Elias 		__skb_queue_tail(&ndev->tx_q, skb_frag);
1356a2968aaSIlan Elias 
1366a2968aaSIlan Elias 	spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
1376a2968aaSIlan Elias 
1386a2968aaSIlan Elias 	/* free the original skb */
1396a2968aaSIlan Elias 	kfree_skb(skb);
1406a2968aaSIlan Elias 
1416a2968aaSIlan Elias 	goto exit;
1426a2968aaSIlan Elias 
1436a2968aaSIlan Elias free_exit:
1446a2968aaSIlan Elias 	while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
1456a2968aaSIlan Elias 		kfree_skb(skb_frag);
1466a2968aaSIlan Elias 
1476a2968aaSIlan Elias exit:
1486a2968aaSIlan Elias 	return rc;
1496a2968aaSIlan Elias }
1506a2968aaSIlan Elias 
1516a2968aaSIlan Elias /* Send NCI data */
1526a2968aaSIlan Elias int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
1536a2968aaSIlan Elias {
1546a2968aaSIlan Elias 	int rc = 0;
1556a2968aaSIlan Elias 
15624bf3304SJoe Perches 	pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
1576a2968aaSIlan Elias 
1586a2968aaSIlan Elias 	/* check if the packet need to be fragmented */
159e8c0dacdSIlan Elias 	if (skb->len <= ndev->max_data_pkt_payload_size) {
1606a2968aaSIlan Elias 		/* no need to fragment packet */
1616a2968aaSIlan Elias 		nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
1626a2968aaSIlan Elias 
1636a2968aaSIlan Elias 		skb_queue_tail(&ndev->tx_q, skb);
1646a2968aaSIlan Elias 	} else {
1656a2968aaSIlan Elias 		/* fragment packet and queue the fragments */
1666a2968aaSIlan Elias 		rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
1676a2968aaSIlan Elias 		if (rc) {
168ed1e0ad8SJoe Perches 			pr_err("failed to fragment tx data packet\n");
1696a2968aaSIlan Elias 			goto free_exit;
1706a2968aaSIlan Elias 		}
1716a2968aaSIlan Elias 	}
1726a2968aaSIlan Elias 
1736a2968aaSIlan Elias 	queue_work(ndev->tx_wq, &ndev->tx_work);
1746a2968aaSIlan Elias 
1756a2968aaSIlan Elias 	goto exit;
1766a2968aaSIlan Elias 
1776a2968aaSIlan Elias free_exit:
1786a2968aaSIlan Elias 	kfree_skb(skb);
1796a2968aaSIlan Elias 
1806a2968aaSIlan Elias exit:
1816a2968aaSIlan Elias 	return rc;
1826a2968aaSIlan Elias }
1836a2968aaSIlan Elias 
1846a2968aaSIlan Elias /* ----------------- NCI RX Data ----------------- */
1856a2968aaSIlan Elias 
1866a2968aaSIlan Elias static void nci_add_rx_data_frag(struct nci_dev *ndev,
1876a2968aaSIlan Elias 				 struct sk_buff *skb,
18898ff416fSChristophe Ricard 				 __u8 pbf, __u8 status)
1896a2968aaSIlan Elias {
1906a2968aaSIlan Elias 	int reassembly_len;
1916a2968aaSIlan Elias 	int err = 0;
1926a2968aaSIlan Elias 
19398ff416fSChristophe Ricard 	if (status) {
19498ff416fSChristophe Ricard 		err = status;
19598ff416fSChristophe Ricard 		goto exit;
19698ff416fSChristophe Ricard 	}
19798ff416fSChristophe Ricard 
1986a2968aaSIlan Elias 	if (ndev->rx_data_reassembly) {
1996a2968aaSIlan Elias 		reassembly_len = ndev->rx_data_reassembly->len;
2006a2968aaSIlan Elias 
2016a2968aaSIlan Elias 		/* first, make enough room for the already accumulated data */
2026a2968aaSIlan Elias 		if (skb_cow_head(skb, reassembly_len)) {
203ed1e0ad8SJoe Perches 			pr_err("error adding room for accumulated rx data\n");
2046a2968aaSIlan Elias 
2056a2968aaSIlan Elias 			kfree_skb(skb);
206040487f3SH Hartley Sweeten 			skb = NULL;
2076a2968aaSIlan Elias 
2086a2968aaSIlan Elias 			kfree_skb(ndev->rx_data_reassembly);
209040487f3SH Hartley Sweeten 			ndev->rx_data_reassembly = NULL;
2106a2968aaSIlan Elias 
2116a2968aaSIlan Elias 			err = -ENOMEM;
2126a2968aaSIlan Elias 			goto exit;
2136a2968aaSIlan Elias 		}
2146a2968aaSIlan Elias 
2156a2968aaSIlan Elias 		/* second, combine the two fragments */
2166a2968aaSIlan Elias 		memcpy(skb_push(skb, reassembly_len),
2176a2968aaSIlan Elias 		       ndev->rx_data_reassembly->data,
2186a2968aaSIlan Elias 		       reassembly_len);
2196a2968aaSIlan Elias 
2206a2968aaSIlan Elias 		/* third, free old reassembly */
2216a2968aaSIlan Elias 		kfree_skb(ndev->rx_data_reassembly);
222040487f3SH Hartley Sweeten 		ndev->rx_data_reassembly = NULL;
2236a2968aaSIlan Elias 	}
2246a2968aaSIlan Elias 
2256a2968aaSIlan Elias 	if (pbf == NCI_PBF_CONT) {
2266a2968aaSIlan Elias 		/* need to wait for next fragment, store skb and exit */
2276a2968aaSIlan Elias 		ndev->rx_data_reassembly = skb;
2286a2968aaSIlan Elias 		return;
2296a2968aaSIlan Elias 	}
2306a2968aaSIlan Elias 
2316a2968aaSIlan Elias exit:
232122c1958SJulien Lefrique 	if (ndev->nfc_dev->rf_mode == NFC_RF_INITIATOR) {
2336a2968aaSIlan Elias 		nci_data_exchange_complete(ndev, skb, err);
234122c1958SJulien Lefrique 	} else if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) {
235122c1958SJulien Lefrique 		/* Data received in Target mode, forward to nfc core */
236122c1958SJulien Lefrique 		err = nfc_tm_data_received(ndev->nfc_dev, skb);
237122c1958SJulien Lefrique 		if (err)
238122c1958SJulien Lefrique 			pr_err("unable to handle received data\n");
239122c1958SJulien Lefrique 	} else {
240122c1958SJulien Lefrique 		pr_err("rf mode unknown\n");
241122c1958SJulien Lefrique 		kfree_skb(skb);
242122c1958SJulien Lefrique 	}
2436a2968aaSIlan Elias }
2446a2968aaSIlan Elias 
2456a2968aaSIlan Elias /* Rx Data packet */
2466a2968aaSIlan Elias void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
2476a2968aaSIlan Elias {
2486a2968aaSIlan Elias 	__u8 pbf = nci_pbf(skb->data);
24998ff416fSChristophe Ricard 	__u8 status = 0;
2506a2968aaSIlan Elias 
25124bf3304SJoe Perches 	pr_debug("len %d\n", skb->len);
2526a2968aaSIlan Elias 
25320c239c1SJoe Perches 	pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
2546a2968aaSIlan Elias 		 nci_pbf(skb->data),
2556a2968aaSIlan Elias 		 nci_conn_id(skb->data),
2566a2968aaSIlan Elias 		 nci_plen(skb->data));
2576a2968aaSIlan Elias 
2586a2968aaSIlan Elias 	/* strip the nci data header */
2596a2968aaSIlan Elias 	skb_pull(skb, NCI_DATA_HDR_SIZE);
2606a2968aaSIlan Elias 
26183724c33SVincent Cuissard 	if (ndev->target_active_prot == NFC_PROTO_MIFARE ||
26283724c33SVincent Cuissard 	    ndev->target_active_prot == NFC_PROTO_JEWEL ||
26383724c33SVincent Cuissard 	    ndev->target_active_prot == NFC_PROTO_FELICA ||
26483724c33SVincent Cuissard 	    ndev->target_active_prot == NFC_PROTO_ISO15693) {
2656a2968aaSIlan Elias 		/* frame I/F => remove the status byte */
26683724c33SVincent Cuissard 		pr_debug("frame I/F => remove the status byte\n");
26798ff416fSChristophe Ricard 		status = skb->data[skb->len - 1];
2686a2968aaSIlan Elias 		skb_trim(skb, (skb->len - 1));
2696a2968aaSIlan Elias 	}
2706a2968aaSIlan Elias 
27198ff416fSChristophe Ricard 	nci_add_rx_data_frag(ndev, skb, pbf, nci_to_errno(status));
2726a2968aaSIlan Elias }
273