xref: /openbmc/linux/net/nfc/nci/data.c (revision 4aeee687)
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,
394aeee687SChristophe Ricard 				__u8 conn_id, int err)
406a2968aaSIlan Elias {
414aeee687SChristophe Ricard 	struct nci_conn_info    *conn_info;
424aeee687SChristophe Ricard 	data_exchange_cb_t cb;
434aeee687SChristophe Ricard 	void *cb_context;
444aeee687SChristophe Ricard 
454aeee687SChristophe Ricard 	conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
464aeee687SChristophe Ricard 	if (!conn_info) {
474aeee687SChristophe Ricard 		kfree_skb(skb);
484aeee687SChristophe Ricard 		goto exit;
494aeee687SChristophe Ricard 	}
504aeee687SChristophe Ricard 
514aeee687SChristophe Ricard 	cb = conn_info->data_exchange_cb;
524aeee687SChristophe Ricard 	cb_context = conn_info->data_exchange_cb_context;
536a2968aaSIlan Elias 
5424bf3304SJoe Perches 	pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
556a2968aaSIlan Elias 
56c4bf98b2SIlan Elias 	/* data exchange is complete, stop the data timer */
57c4bf98b2SIlan Elias 	del_timer_sync(&ndev->data_timer);
58c4bf98b2SIlan Elias 	clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
59c4bf98b2SIlan Elias 
606a2968aaSIlan Elias 	if (cb) {
616a2968aaSIlan Elias 		/* forward skb to nfc core */
626a2968aaSIlan Elias 		cb(cb_context, skb, err);
636a2968aaSIlan Elias 	} else if (skb) {
64ed1e0ad8SJoe Perches 		pr_err("no rx callback, dropping rx data...\n");
656a2968aaSIlan Elias 
666a2968aaSIlan Elias 		/* no waiting callback, free skb */
676a2968aaSIlan Elias 		kfree_skb(skb);
686a2968aaSIlan Elias 	}
6938f04c6bSIlan Elias 
704aeee687SChristophe Ricard exit:
7138f04c6bSIlan Elias 	clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
726a2968aaSIlan Elias }
736a2968aaSIlan Elias 
746a2968aaSIlan Elias /* ----------------- NCI TX Data ----------------- */
756a2968aaSIlan Elias 
766a2968aaSIlan Elias static inline void nci_push_data_hdr(struct nci_dev *ndev,
776a2968aaSIlan Elias 				     __u8 conn_id,
786a2968aaSIlan Elias 				     struct sk_buff *skb,
796a2968aaSIlan Elias 				     __u8 pbf)
806a2968aaSIlan Elias {
816a2968aaSIlan Elias 	struct nci_data_hdr *hdr;
826a2968aaSIlan Elias 	int plen = skb->len;
836a2968aaSIlan Elias 
846a2968aaSIlan Elias 	hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE);
856a2968aaSIlan Elias 	hdr->conn_id = conn_id;
866a2968aaSIlan Elias 	hdr->rfu = 0;
876a2968aaSIlan Elias 	hdr->plen = plen;
886a2968aaSIlan Elias 
896a2968aaSIlan Elias 	nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
906a2968aaSIlan Elias 	nci_pbf_set((__u8 *)hdr, pbf);
916a2968aaSIlan Elias }
926a2968aaSIlan Elias 
936a2968aaSIlan Elias static int nci_queue_tx_data_frags(struct nci_dev *ndev,
946a2968aaSIlan Elias 				   __u8 conn_id,
956a2968aaSIlan Elias 				   struct sk_buff *skb) {
964aeee687SChristophe Ricard 	struct nci_conn_info    *conn_info;
976a2968aaSIlan Elias 	int total_len = skb->len;
986a2968aaSIlan Elias 	unsigned char *data = skb->data;
996a2968aaSIlan Elias 	unsigned long flags;
1006a2968aaSIlan Elias 	struct sk_buff_head frags_q;
1016a2968aaSIlan Elias 	struct sk_buff *skb_frag;
1026a2968aaSIlan Elias 	int frag_len;
1036a2968aaSIlan Elias 	int rc = 0;
1046a2968aaSIlan Elias 
10524bf3304SJoe Perches 	pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
1066a2968aaSIlan Elias 
1074aeee687SChristophe Ricard 	conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
1084aeee687SChristophe Ricard 	if (!conn_info) {
1094aeee687SChristophe Ricard 		rc = -EPROTO;
1104aeee687SChristophe Ricard 		goto free_exit;
1114aeee687SChristophe Ricard 	}
1124aeee687SChristophe Ricard 
1136a2968aaSIlan Elias 	__skb_queue_head_init(&frags_q);
1146a2968aaSIlan Elias 
1156a2968aaSIlan Elias 	while (total_len) {
116e8c0dacdSIlan Elias 		frag_len =
1174aeee687SChristophe Ricard 			min_t(int, total_len, conn_info->max_pkt_payload_len);
1186a2968aaSIlan Elias 
1196a2968aaSIlan Elias 		skb_frag = nci_skb_alloc(ndev,
1206a2968aaSIlan Elias 					 (NCI_DATA_HDR_SIZE + frag_len),
1216a2968aaSIlan Elias 					 GFP_KERNEL);
1226a2968aaSIlan Elias 		if (skb_frag == NULL) {
1236a2968aaSIlan Elias 			rc = -ENOMEM;
1246a2968aaSIlan Elias 			goto free_exit;
1256a2968aaSIlan Elias 		}
1266a2968aaSIlan Elias 		skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
1276a2968aaSIlan Elias 
1286a2968aaSIlan Elias 		/* first, copy the data */
1296a2968aaSIlan Elias 		memcpy(skb_put(skb_frag, frag_len), data, frag_len);
1306a2968aaSIlan Elias 
1316a2968aaSIlan Elias 		/* second, set the header */
1326a2968aaSIlan Elias 		nci_push_data_hdr(ndev, conn_id, skb_frag,
133eb9bc6e9SSamuel Ortiz 				  ((total_len == frag_len) ?
134eb9bc6e9SSamuel Ortiz 				   (NCI_PBF_LAST) : (NCI_PBF_CONT)));
1356a2968aaSIlan Elias 
1366a2968aaSIlan Elias 		__skb_queue_tail(&frags_q, skb_frag);
1376a2968aaSIlan Elias 
1386a2968aaSIlan Elias 		data += frag_len;
1396a2968aaSIlan Elias 		total_len -= frag_len;
1406a2968aaSIlan Elias 
14120c239c1SJoe Perches 		pr_debug("frag_len %d, remaining total_len %d\n",
1426a2968aaSIlan Elias 			 frag_len, total_len);
1436a2968aaSIlan Elias 	}
1446a2968aaSIlan Elias 
1456a2968aaSIlan Elias 	/* queue all fragments atomically */
1466a2968aaSIlan Elias 	spin_lock_irqsave(&ndev->tx_q.lock, flags);
1476a2968aaSIlan Elias 
1486a2968aaSIlan Elias 	while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
1496a2968aaSIlan Elias 		__skb_queue_tail(&ndev->tx_q, skb_frag);
1506a2968aaSIlan Elias 
1516a2968aaSIlan Elias 	spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
1526a2968aaSIlan Elias 
1536a2968aaSIlan Elias 	/* free the original skb */
1546a2968aaSIlan Elias 	kfree_skb(skb);
1556a2968aaSIlan Elias 
1566a2968aaSIlan Elias 	goto exit;
1576a2968aaSIlan Elias 
1586a2968aaSIlan Elias free_exit:
1596a2968aaSIlan Elias 	while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
1606a2968aaSIlan Elias 		kfree_skb(skb_frag);
1616a2968aaSIlan Elias 
1626a2968aaSIlan Elias exit:
1636a2968aaSIlan Elias 	return rc;
1646a2968aaSIlan Elias }
1656a2968aaSIlan Elias 
1666a2968aaSIlan Elias /* Send NCI data */
1676a2968aaSIlan Elias int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
1686a2968aaSIlan Elias {
1694aeee687SChristophe Ricard 	struct nci_conn_info    *conn_info;
1706a2968aaSIlan Elias 	int rc = 0;
1716a2968aaSIlan Elias 
17224bf3304SJoe Perches 	pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
1736a2968aaSIlan Elias 
1744aeee687SChristophe Ricard 	conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
1754aeee687SChristophe Ricard 	if (!conn_info) {
1764aeee687SChristophe Ricard 		rc = -EPROTO;
1774aeee687SChristophe Ricard 		goto free_exit;
1784aeee687SChristophe Ricard 	}
1794aeee687SChristophe Ricard 
1806a2968aaSIlan Elias 	/* check if the packet need to be fragmented */
1814aeee687SChristophe Ricard 	if (skb->len <= conn_info->max_pkt_payload_len) {
1826a2968aaSIlan Elias 		/* no need to fragment packet */
1836a2968aaSIlan Elias 		nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
1846a2968aaSIlan Elias 
1856a2968aaSIlan Elias 		skb_queue_tail(&ndev->tx_q, skb);
1866a2968aaSIlan Elias 	} else {
1876a2968aaSIlan Elias 		/* fragment packet and queue the fragments */
1886a2968aaSIlan Elias 		rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
1896a2968aaSIlan Elias 		if (rc) {
190ed1e0ad8SJoe Perches 			pr_err("failed to fragment tx data packet\n");
1916a2968aaSIlan Elias 			goto free_exit;
1926a2968aaSIlan Elias 		}
1936a2968aaSIlan Elias 	}
1946a2968aaSIlan Elias 
1954aeee687SChristophe Ricard 	ndev->cur_conn_id = conn_id;
1966a2968aaSIlan Elias 	queue_work(ndev->tx_wq, &ndev->tx_work);
1976a2968aaSIlan Elias 
1986a2968aaSIlan Elias 	goto exit;
1996a2968aaSIlan Elias 
2006a2968aaSIlan Elias free_exit:
2016a2968aaSIlan Elias 	kfree_skb(skb);
2026a2968aaSIlan Elias 
2036a2968aaSIlan Elias exit:
2046a2968aaSIlan Elias 	return rc;
2056a2968aaSIlan Elias }
2066a2968aaSIlan Elias 
2076a2968aaSIlan Elias /* ----------------- NCI RX Data ----------------- */
2086a2968aaSIlan Elias 
2096a2968aaSIlan Elias static void nci_add_rx_data_frag(struct nci_dev *ndev,
2106a2968aaSIlan Elias 				 struct sk_buff *skb,
2114aeee687SChristophe Ricard 				 __u8 pbf, __u8 conn_id, __u8 status)
2126a2968aaSIlan Elias {
2136a2968aaSIlan Elias 	int reassembly_len;
2146a2968aaSIlan Elias 	int err = 0;
2156a2968aaSIlan Elias 
21698ff416fSChristophe Ricard 	if (status) {
21798ff416fSChristophe Ricard 		err = status;
21898ff416fSChristophe Ricard 		goto exit;
21998ff416fSChristophe Ricard 	}
22098ff416fSChristophe Ricard 
2216a2968aaSIlan Elias 	if (ndev->rx_data_reassembly) {
2226a2968aaSIlan Elias 		reassembly_len = ndev->rx_data_reassembly->len;
2236a2968aaSIlan Elias 
2246a2968aaSIlan Elias 		/* first, make enough room for the already accumulated data */
2256a2968aaSIlan Elias 		if (skb_cow_head(skb, reassembly_len)) {
226ed1e0ad8SJoe Perches 			pr_err("error adding room for accumulated rx data\n");
2276a2968aaSIlan Elias 
2286a2968aaSIlan Elias 			kfree_skb(skb);
229040487f3SH Hartley Sweeten 			skb = NULL;
2306a2968aaSIlan Elias 
2316a2968aaSIlan Elias 			kfree_skb(ndev->rx_data_reassembly);
232040487f3SH Hartley Sweeten 			ndev->rx_data_reassembly = NULL;
2336a2968aaSIlan Elias 
2346a2968aaSIlan Elias 			err = -ENOMEM;
2356a2968aaSIlan Elias 			goto exit;
2366a2968aaSIlan Elias 		}
2376a2968aaSIlan Elias 
2386a2968aaSIlan Elias 		/* second, combine the two fragments */
2396a2968aaSIlan Elias 		memcpy(skb_push(skb, reassembly_len),
2406a2968aaSIlan Elias 		       ndev->rx_data_reassembly->data,
2416a2968aaSIlan Elias 		       reassembly_len);
2426a2968aaSIlan Elias 
2436a2968aaSIlan Elias 		/* third, free old reassembly */
2446a2968aaSIlan Elias 		kfree_skb(ndev->rx_data_reassembly);
245040487f3SH Hartley Sweeten 		ndev->rx_data_reassembly = NULL;
2466a2968aaSIlan Elias 	}
2476a2968aaSIlan Elias 
2486a2968aaSIlan Elias 	if (pbf == NCI_PBF_CONT) {
2496a2968aaSIlan Elias 		/* need to wait for next fragment, store skb and exit */
2506a2968aaSIlan Elias 		ndev->rx_data_reassembly = skb;
2516a2968aaSIlan Elias 		return;
2526a2968aaSIlan Elias 	}
2536a2968aaSIlan Elias 
2546a2968aaSIlan Elias exit:
2554aeee687SChristophe Ricard 	if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) {
256122c1958SJulien Lefrique 		/* Data received in Target mode, forward to nfc core */
257122c1958SJulien Lefrique 		err = nfc_tm_data_received(ndev->nfc_dev, skb);
258122c1958SJulien Lefrique 		if (err)
259122c1958SJulien Lefrique 			pr_err("unable to handle received data\n");
260122c1958SJulien Lefrique 	} else {
2614aeee687SChristophe Ricard 		nci_data_exchange_complete(ndev, skb, conn_id, err);
262122c1958SJulien Lefrique 	}
2636a2968aaSIlan Elias }
2646a2968aaSIlan Elias 
2656a2968aaSIlan Elias /* Rx Data packet */
2666a2968aaSIlan Elias void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
2676a2968aaSIlan Elias {
2686a2968aaSIlan Elias 	__u8 pbf = nci_pbf(skb->data);
26998ff416fSChristophe Ricard 	__u8 status = 0;
2704aeee687SChristophe Ricard 	__u8 conn_id = nci_conn_id(skb->data);
2714aeee687SChristophe Ricard 	struct nci_conn_info    *conn_info;
2726a2968aaSIlan Elias 
27324bf3304SJoe Perches 	pr_debug("len %d\n", skb->len);
2746a2968aaSIlan Elias 
27520c239c1SJoe Perches 	pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
2766a2968aaSIlan Elias 		 nci_pbf(skb->data),
2776a2968aaSIlan Elias 		 nci_conn_id(skb->data),
2786a2968aaSIlan Elias 		 nci_plen(skb->data));
2796a2968aaSIlan Elias 
2804aeee687SChristophe Ricard 	conn_info = nci_get_conn_info_by_conn_id(ndev, nci_conn_id(skb->data));
2814aeee687SChristophe Ricard 	if (!conn_info)
2824aeee687SChristophe Ricard 		return;
2834aeee687SChristophe Ricard 
2846a2968aaSIlan Elias 	/* strip the nci data header */
2856a2968aaSIlan Elias 	skb_pull(skb, NCI_DATA_HDR_SIZE);
2866a2968aaSIlan Elias 
28783724c33SVincent Cuissard 	if (ndev->target_active_prot == NFC_PROTO_MIFARE ||
28883724c33SVincent Cuissard 	    ndev->target_active_prot == NFC_PROTO_JEWEL ||
28983724c33SVincent Cuissard 	    ndev->target_active_prot == NFC_PROTO_FELICA ||
29083724c33SVincent Cuissard 	    ndev->target_active_prot == NFC_PROTO_ISO15693) {
2916a2968aaSIlan Elias 		/* frame I/F => remove the status byte */
29283724c33SVincent Cuissard 		pr_debug("frame I/F => remove the status byte\n");
29398ff416fSChristophe Ricard 		status = skb->data[skb->len - 1];
2946a2968aaSIlan Elias 		skb_trim(skb, (skb->len - 1));
2956a2968aaSIlan Elias 	}
2966a2968aaSIlan Elias 
2974aeee687SChristophe Ricard 	nci_add_rx_data_frag(ndev, skb, pbf, conn_id, nci_to_errno(status));
2986a2968aaSIlan Elias }
299