1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26a2968aaSIlan Elias /*
36a2968aaSIlan Elias * The NFC Controller Interface is the communication protocol between an
46a2968aaSIlan Elias * NFC Controller (NFCC) and a Device Host (DH).
56a2968aaSIlan Elias *
66a2968aaSIlan Elias * Copyright (C) 2011 Texas Instruments, Inc.
7122c1958SJulien Lefrique * Copyright (C) 2014 Marvell International Ltd.
86a2968aaSIlan Elias *
96a2968aaSIlan Elias * Written by Ilan Elias <ilane@ti.com>
106a2968aaSIlan Elias */
116a2968aaSIlan Elias
1252858b51SSamuel Ortiz #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
13ed1e0ad8SJoe Perches
146a2968aaSIlan Elias #include <linux/types.h>
156a2968aaSIlan Elias #include <linux/interrupt.h>
166a2968aaSIlan Elias #include <linux/wait.h>
176a2968aaSIlan Elias #include <linux/bitops.h>
186a2968aaSIlan Elias #include <linux/skbuff.h>
196a2968aaSIlan Elias
206a2968aaSIlan Elias #include "../nfc.h"
216a2968aaSIlan Elias #include <net/nfc/nci.h>
226a2968aaSIlan Elias #include <net/nfc/nci_core.h>
236a2968aaSIlan Elias #include <linux/nfc.h>
246a2968aaSIlan Elias
256a2968aaSIlan Elias /* Complete data exchange transaction and forward skb to nfc core */
nci_data_exchange_complete(struct nci_dev * ndev,struct sk_buff * skb,__u8 conn_id,int err)26eb9bc6e9SSamuel Ortiz void nci_data_exchange_complete(struct nci_dev *ndev, struct sk_buff *skb,
274aeee687SChristophe Ricard __u8 conn_id, int err)
286a2968aaSIlan Elias {
29ddecf555SKrzysztof Kozlowski const struct nci_conn_info *conn_info;
304aeee687SChristophe Ricard data_exchange_cb_t cb;
314aeee687SChristophe Ricard void *cb_context;
324aeee687SChristophe Ricard
334aeee687SChristophe Ricard conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
344aeee687SChristophe Ricard if (!conn_info) {
354aeee687SChristophe Ricard kfree_skb(skb);
364aeee687SChristophe Ricard goto exit;
374aeee687SChristophe Ricard }
384aeee687SChristophe Ricard
394aeee687SChristophe Ricard cb = conn_info->data_exchange_cb;
404aeee687SChristophe Ricard cb_context = conn_info->data_exchange_cb_context;
416a2968aaSIlan Elias
4224bf3304SJoe Perches pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
436a2968aaSIlan Elias
44c4bf98b2SIlan Elias /* data exchange is complete, stop the data timer */
45c4bf98b2SIlan Elias del_timer_sync(&ndev->data_timer);
46c4bf98b2SIlan Elias clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
47c4bf98b2SIlan Elias
486a2968aaSIlan Elias if (cb) {
496a2968aaSIlan Elias /* forward skb to nfc core */
506a2968aaSIlan Elias cb(cb_context, skb, err);
516a2968aaSIlan Elias } else if (skb) {
52ed1e0ad8SJoe Perches pr_err("no rx callback, dropping rx data...\n");
536a2968aaSIlan Elias
546a2968aaSIlan Elias /* no waiting callback, free skb */
556a2968aaSIlan Elias kfree_skb(skb);
566a2968aaSIlan Elias }
5738f04c6bSIlan Elias
584aeee687SChristophe Ricard exit:
5938f04c6bSIlan Elias clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
606a2968aaSIlan Elias }
616a2968aaSIlan Elias
626a2968aaSIlan Elias /* ----------------- NCI TX Data ----------------- */
636a2968aaSIlan Elias
nci_push_data_hdr(struct nci_dev * ndev,__u8 conn_id,struct sk_buff * skb,__u8 pbf)646a2968aaSIlan Elias static inline void nci_push_data_hdr(struct nci_dev *ndev,
656a2968aaSIlan Elias __u8 conn_id,
666a2968aaSIlan Elias struct sk_buff *skb,
676a2968aaSIlan Elias __u8 pbf)
686a2968aaSIlan Elias {
696a2968aaSIlan Elias struct nci_data_hdr *hdr;
706a2968aaSIlan Elias int plen = skb->len;
716a2968aaSIlan Elias
72d58ff351SJohannes Berg hdr = skb_push(skb, NCI_DATA_HDR_SIZE);
736a2968aaSIlan Elias hdr->conn_id = conn_id;
746a2968aaSIlan Elias hdr->rfu = 0;
756a2968aaSIlan Elias hdr->plen = plen;
766a2968aaSIlan Elias
776a2968aaSIlan Elias nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
786a2968aaSIlan Elias nci_pbf_set((__u8 *)hdr, pbf);
796a2968aaSIlan Elias }
806a2968aaSIlan Elias
nci_conn_max_data_pkt_payload_size(struct nci_dev * ndev,__u8 conn_id)812663589cSRobert Dolca int nci_conn_max_data_pkt_payload_size(struct nci_dev *ndev, __u8 conn_id)
822663589cSRobert Dolca {
83ddecf555SKrzysztof Kozlowski const struct nci_conn_info *conn_info;
842663589cSRobert Dolca
852663589cSRobert Dolca conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
862663589cSRobert Dolca if (!conn_info)
872663589cSRobert Dolca return -EPROTO;
882663589cSRobert Dolca
892663589cSRobert Dolca return conn_info->max_pkt_payload_len;
902663589cSRobert Dolca }
912663589cSRobert Dolca EXPORT_SYMBOL(nci_conn_max_data_pkt_payload_size);
922663589cSRobert Dolca
nci_queue_tx_data_frags(struct nci_dev * ndev,__u8 conn_id,struct sk_buff * skb)936a2968aaSIlan Elias static int nci_queue_tx_data_frags(struct nci_dev *ndev,
946a2968aaSIlan Elias __u8 conn_id,
956a2968aaSIlan Elias struct sk_buff *skb) {
96ddecf555SKrzysztof Kozlowski const struct nci_conn_info *conn_info;
976a2968aaSIlan Elias int total_len = skb->len;
98ddecf555SKrzysztof Kozlowski const 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;
110dd006fc4SYang Wei goto 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),
12123dd4581SDuoming Zhou GFP_ATOMIC);
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 */
12959ae1d12SJohannes Berg skb_put_data(skb_frag, 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 */
nci_send_data(struct nci_dev * ndev,__u8 conn_id,struct sk_buff * skb)1676a2968aaSIlan Elias int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
1686a2968aaSIlan Elias {
169ddecf555SKrzysztof Kozlowski const 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 }
206ea785c09SRobert Dolca EXPORT_SYMBOL(nci_send_data);
2076a2968aaSIlan Elias
2086a2968aaSIlan Elias /* ----------------- NCI RX Data ----------------- */
2096a2968aaSIlan Elias
nci_add_rx_data_frag(struct nci_dev * ndev,struct sk_buff * skb,__u8 pbf,__u8 conn_id,__u8 status)2106a2968aaSIlan Elias static void nci_add_rx_data_frag(struct nci_dev *ndev,
2116a2968aaSIlan Elias struct sk_buff *skb,
2124aeee687SChristophe Ricard __u8 pbf, __u8 conn_id, __u8 status)
2136a2968aaSIlan Elias {
2146a2968aaSIlan Elias int reassembly_len;
2156a2968aaSIlan Elias int err = 0;
2166a2968aaSIlan Elias
21798ff416fSChristophe Ricard if (status) {
21898ff416fSChristophe Ricard err = status;
21998ff416fSChristophe Ricard goto exit;
22098ff416fSChristophe Ricard }
22198ff416fSChristophe Ricard
2226a2968aaSIlan Elias if (ndev->rx_data_reassembly) {
2236a2968aaSIlan Elias reassembly_len = ndev->rx_data_reassembly->len;
2246a2968aaSIlan Elias
2256a2968aaSIlan Elias /* first, make enough room for the already accumulated data */
2266a2968aaSIlan Elias if (skb_cow_head(skb, reassembly_len)) {
227ed1e0ad8SJoe Perches pr_err("error adding room for accumulated rx data\n");
2286a2968aaSIlan Elias
2296a2968aaSIlan Elias kfree_skb(skb);
230040487f3SH Hartley Sweeten skb = NULL;
2316a2968aaSIlan Elias
2326a2968aaSIlan Elias kfree_skb(ndev->rx_data_reassembly);
233040487f3SH Hartley Sweeten ndev->rx_data_reassembly = NULL;
2346a2968aaSIlan Elias
2356a2968aaSIlan Elias err = -ENOMEM;
2366a2968aaSIlan Elias goto exit;
2376a2968aaSIlan Elias }
2386a2968aaSIlan Elias
2396a2968aaSIlan Elias /* second, combine the two fragments */
2406a2968aaSIlan Elias memcpy(skb_push(skb, reassembly_len),
2416a2968aaSIlan Elias ndev->rx_data_reassembly->data,
2426a2968aaSIlan Elias reassembly_len);
2436a2968aaSIlan Elias
2446a2968aaSIlan Elias /* third, free old reassembly */
2456a2968aaSIlan Elias kfree_skb(ndev->rx_data_reassembly);
246040487f3SH Hartley Sweeten ndev->rx_data_reassembly = NULL;
2476a2968aaSIlan Elias }
2486a2968aaSIlan Elias
2496a2968aaSIlan Elias if (pbf == NCI_PBF_CONT) {
2506a2968aaSIlan Elias /* need to wait for next fragment, store skb and exit */
2516a2968aaSIlan Elias ndev->rx_data_reassembly = skb;
2526a2968aaSIlan Elias return;
2536a2968aaSIlan Elias }
2546a2968aaSIlan Elias
2556a2968aaSIlan Elias exit:
2564aeee687SChristophe Ricard if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) {
257122c1958SJulien Lefrique /* Data received in Target mode, forward to nfc core */
258122c1958SJulien Lefrique err = nfc_tm_data_received(ndev->nfc_dev, skb);
259122c1958SJulien Lefrique if (err)
260122c1958SJulien Lefrique pr_err("unable to handle received data\n");
261122c1958SJulien Lefrique } else {
2624aeee687SChristophe Ricard nci_data_exchange_complete(ndev, skb, conn_id, err);
263122c1958SJulien Lefrique }
2646a2968aaSIlan Elias }
2656a2968aaSIlan Elias
2666a2968aaSIlan Elias /* Rx Data packet */
nci_rx_data_packet(struct nci_dev * ndev,struct sk_buff * skb)2676a2968aaSIlan Elias void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
2686a2968aaSIlan Elias {
2696a2968aaSIlan Elias __u8 pbf = nci_pbf(skb->data);
27098ff416fSChristophe Ricard __u8 status = 0;
2714aeee687SChristophe Ricard __u8 conn_id = nci_conn_id(skb->data);
272ddecf555SKrzysztof Kozlowski const struct nci_conn_info *conn_info;
2736a2968aaSIlan Elias
27424bf3304SJoe Perches pr_debug("len %d\n", skb->len);
2756a2968aaSIlan Elias
27620c239c1SJoe Perches pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
2776a2968aaSIlan Elias nci_pbf(skb->data),
2786a2968aaSIlan Elias nci_conn_id(skb->data),
2796a2968aaSIlan Elias nci_plen(skb->data));
2806a2968aaSIlan Elias
2814aeee687SChristophe Ricard conn_info = nci_get_conn_info_by_conn_id(ndev, nci_conn_id(skb->data));
282*53270fb0SLiu Shixin if (!conn_info) {
283*53270fb0SLiu Shixin kfree_skb(skb);
2844aeee687SChristophe Ricard return;
285*53270fb0SLiu Shixin }
2864aeee687SChristophe Ricard
2876a2968aaSIlan Elias /* strip the nci data header */
2886a2968aaSIlan Elias skb_pull(skb, NCI_DATA_HDR_SIZE);
2896a2968aaSIlan Elias
29083724c33SVincent Cuissard if (ndev->target_active_prot == NFC_PROTO_MIFARE ||
29183724c33SVincent Cuissard ndev->target_active_prot == NFC_PROTO_JEWEL ||
29283724c33SVincent Cuissard ndev->target_active_prot == NFC_PROTO_FELICA ||
29383724c33SVincent Cuissard ndev->target_active_prot == NFC_PROTO_ISO15693) {
2946a2968aaSIlan Elias /* frame I/F => remove the status byte */
29583724c33SVincent Cuissard pr_debug("frame I/F => remove the status byte\n");
29698ff416fSChristophe Ricard status = skb->data[skb->len - 1];
2976a2968aaSIlan Elias skb_trim(skb, (skb->len - 1));
2986a2968aaSIlan Elias }
2996a2968aaSIlan Elias
3004aeee687SChristophe Ricard nci_add_rx_data_frag(ndev, skb, pbf, conn_id, nci_to_errno(status));
3016a2968aaSIlan Elias }
302