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 84*d58ff351SJohannes Berg 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 932663589cSRobert Dolca int nci_conn_max_data_pkt_payload_size(struct nci_dev *ndev, __u8 conn_id) 942663589cSRobert Dolca { 952663589cSRobert Dolca struct nci_conn_info *conn_info; 962663589cSRobert Dolca 972663589cSRobert Dolca conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id); 982663589cSRobert Dolca if (!conn_info) 992663589cSRobert Dolca return -EPROTO; 1002663589cSRobert Dolca 1012663589cSRobert Dolca return conn_info->max_pkt_payload_len; 1022663589cSRobert Dolca } 1032663589cSRobert Dolca EXPORT_SYMBOL(nci_conn_max_data_pkt_payload_size); 1042663589cSRobert Dolca 1056a2968aaSIlan Elias static int nci_queue_tx_data_frags(struct nci_dev *ndev, 1066a2968aaSIlan Elias __u8 conn_id, 1076a2968aaSIlan Elias struct sk_buff *skb) { 1084aeee687SChristophe Ricard struct nci_conn_info *conn_info; 1096a2968aaSIlan Elias int total_len = skb->len; 1106a2968aaSIlan Elias unsigned char *data = skb->data; 1116a2968aaSIlan Elias unsigned long flags; 1126a2968aaSIlan Elias struct sk_buff_head frags_q; 1136a2968aaSIlan Elias struct sk_buff *skb_frag; 1146a2968aaSIlan Elias int frag_len; 1156a2968aaSIlan Elias int rc = 0; 1166a2968aaSIlan Elias 11724bf3304SJoe Perches pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len); 1186a2968aaSIlan Elias 1194aeee687SChristophe Ricard conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id); 1204aeee687SChristophe Ricard if (!conn_info) { 1214aeee687SChristophe Ricard rc = -EPROTO; 1224aeee687SChristophe Ricard goto free_exit; 1234aeee687SChristophe Ricard } 1244aeee687SChristophe Ricard 1256a2968aaSIlan Elias __skb_queue_head_init(&frags_q); 1266a2968aaSIlan Elias 1276a2968aaSIlan Elias while (total_len) { 128e8c0dacdSIlan Elias frag_len = 1294aeee687SChristophe Ricard min_t(int, total_len, conn_info->max_pkt_payload_len); 1306a2968aaSIlan Elias 1316a2968aaSIlan Elias skb_frag = nci_skb_alloc(ndev, 1326a2968aaSIlan Elias (NCI_DATA_HDR_SIZE + frag_len), 1336a2968aaSIlan Elias GFP_KERNEL); 1346a2968aaSIlan Elias if (skb_frag == NULL) { 1356a2968aaSIlan Elias rc = -ENOMEM; 1366a2968aaSIlan Elias goto free_exit; 1376a2968aaSIlan Elias } 1386a2968aaSIlan Elias skb_reserve(skb_frag, NCI_DATA_HDR_SIZE); 1396a2968aaSIlan Elias 1406a2968aaSIlan Elias /* first, copy the data */ 14159ae1d12SJohannes Berg skb_put_data(skb_frag, data, frag_len); 1426a2968aaSIlan Elias 1436a2968aaSIlan Elias /* second, set the header */ 1446a2968aaSIlan Elias nci_push_data_hdr(ndev, conn_id, skb_frag, 145eb9bc6e9SSamuel Ortiz ((total_len == frag_len) ? 146eb9bc6e9SSamuel Ortiz (NCI_PBF_LAST) : (NCI_PBF_CONT))); 1476a2968aaSIlan Elias 1486a2968aaSIlan Elias __skb_queue_tail(&frags_q, skb_frag); 1496a2968aaSIlan Elias 1506a2968aaSIlan Elias data += frag_len; 1516a2968aaSIlan Elias total_len -= frag_len; 1526a2968aaSIlan Elias 15320c239c1SJoe Perches pr_debug("frag_len %d, remaining total_len %d\n", 1546a2968aaSIlan Elias frag_len, total_len); 1556a2968aaSIlan Elias } 1566a2968aaSIlan Elias 1576a2968aaSIlan Elias /* queue all fragments atomically */ 1586a2968aaSIlan Elias spin_lock_irqsave(&ndev->tx_q.lock, flags); 1596a2968aaSIlan Elias 1606a2968aaSIlan Elias while ((skb_frag = __skb_dequeue(&frags_q)) != NULL) 1616a2968aaSIlan Elias __skb_queue_tail(&ndev->tx_q, skb_frag); 1626a2968aaSIlan Elias 1636a2968aaSIlan Elias spin_unlock_irqrestore(&ndev->tx_q.lock, flags); 1646a2968aaSIlan Elias 1656a2968aaSIlan Elias /* free the original skb */ 1666a2968aaSIlan Elias kfree_skb(skb); 1676a2968aaSIlan Elias 1686a2968aaSIlan Elias goto exit; 1696a2968aaSIlan Elias 1706a2968aaSIlan Elias free_exit: 1716a2968aaSIlan Elias while ((skb_frag = __skb_dequeue(&frags_q)) != NULL) 1726a2968aaSIlan Elias kfree_skb(skb_frag); 1736a2968aaSIlan Elias 1746a2968aaSIlan Elias exit: 1756a2968aaSIlan Elias return rc; 1766a2968aaSIlan Elias } 1776a2968aaSIlan Elias 1786a2968aaSIlan Elias /* Send NCI data */ 1796a2968aaSIlan Elias int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb) 1806a2968aaSIlan Elias { 1814aeee687SChristophe Ricard struct nci_conn_info *conn_info; 1826a2968aaSIlan Elias int rc = 0; 1836a2968aaSIlan Elias 18424bf3304SJoe Perches pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len); 1856a2968aaSIlan Elias 1864aeee687SChristophe Ricard conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id); 1874aeee687SChristophe Ricard if (!conn_info) { 1884aeee687SChristophe Ricard rc = -EPROTO; 1894aeee687SChristophe Ricard goto free_exit; 1904aeee687SChristophe Ricard } 1914aeee687SChristophe Ricard 1926a2968aaSIlan Elias /* check if the packet need to be fragmented */ 1934aeee687SChristophe Ricard if (skb->len <= conn_info->max_pkt_payload_len) { 1946a2968aaSIlan Elias /* no need to fragment packet */ 1956a2968aaSIlan Elias nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST); 1966a2968aaSIlan Elias 1976a2968aaSIlan Elias skb_queue_tail(&ndev->tx_q, skb); 1986a2968aaSIlan Elias } else { 1996a2968aaSIlan Elias /* fragment packet and queue the fragments */ 2006a2968aaSIlan Elias rc = nci_queue_tx_data_frags(ndev, conn_id, skb); 2016a2968aaSIlan Elias if (rc) { 202ed1e0ad8SJoe Perches pr_err("failed to fragment tx data packet\n"); 2036a2968aaSIlan Elias goto free_exit; 2046a2968aaSIlan Elias } 2056a2968aaSIlan Elias } 2066a2968aaSIlan Elias 2074aeee687SChristophe Ricard ndev->cur_conn_id = conn_id; 2086a2968aaSIlan Elias queue_work(ndev->tx_wq, &ndev->tx_work); 2096a2968aaSIlan Elias 2106a2968aaSIlan Elias goto exit; 2116a2968aaSIlan Elias 2126a2968aaSIlan Elias free_exit: 2136a2968aaSIlan Elias kfree_skb(skb); 2146a2968aaSIlan Elias 2156a2968aaSIlan Elias exit: 2166a2968aaSIlan Elias return rc; 2176a2968aaSIlan Elias } 218ea785c09SRobert Dolca EXPORT_SYMBOL(nci_send_data); 2196a2968aaSIlan Elias 2206a2968aaSIlan Elias /* ----------------- NCI RX Data ----------------- */ 2216a2968aaSIlan Elias 2226a2968aaSIlan Elias static void nci_add_rx_data_frag(struct nci_dev *ndev, 2236a2968aaSIlan Elias struct sk_buff *skb, 2244aeee687SChristophe Ricard __u8 pbf, __u8 conn_id, __u8 status) 2256a2968aaSIlan Elias { 2266a2968aaSIlan Elias int reassembly_len; 2276a2968aaSIlan Elias int err = 0; 2286a2968aaSIlan Elias 22998ff416fSChristophe Ricard if (status) { 23098ff416fSChristophe Ricard err = status; 23198ff416fSChristophe Ricard goto exit; 23298ff416fSChristophe Ricard } 23398ff416fSChristophe Ricard 2346a2968aaSIlan Elias if (ndev->rx_data_reassembly) { 2356a2968aaSIlan Elias reassembly_len = ndev->rx_data_reassembly->len; 2366a2968aaSIlan Elias 2376a2968aaSIlan Elias /* first, make enough room for the already accumulated data */ 2386a2968aaSIlan Elias if (skb_cow_head(skb, reassembly_len)) { 239ed1e0ad8SJoe Perches pr_err("error adding room for accumulated rx data\n"); 2406a2968aaSIlan Elias 2416a2968aaSIlan Elias kfree_skb(skb); 242040487f3SH Hartley Sweeten skb = NULL; 2436a2968aaSIlan Elias 2446a2968aaSIlan Elias kfree_skb(ndev->rx_data_reassembly); 245040487f3SH Hartley Sweeten ndev->rx_data_reassembly = NULL; 2466a2968aaSIlan Elias 2476a2968aaSIlan Elias err = -ENOMEM; 2486a2968aaSIlan Elias goto exit; 2496a2968aaSIlan Elias } 2506a2968aaSIlan Elias 2516a2968aaSIlan Elias /* second, combine the two fragments */ 2526a2968aaSIlan Elias memcpy(skb_push(skb, reassembly_len), 2536a2968aaSIlan Elias ndev->rx_data_reassembly->data, 2546a2968aaSIlan Elias reassembly_len); 2556a2968aaSIlan Elias 2566a2968aaSIlan Elias /* third, free old reassembly */ 2576a2968aaSIlan Elias kfree_skb(ndev->rx_data_reassembly); 258040487f3SH Hartley Sweeten ndev->rx_data_reassembly = NULL; 2596a2968aaSIlan Elias } 2606a2968aaSIlan Elias 2616a2968aaSIlan Elias if (pbf == NCI_PBF_CONT) { 2626a2968aaSIlan Elias /* need to wait for next fragment, store skb and exit */ 2636a2968aaSIlan Elias ndev->rx_data_reassembly = skb; 2646a2968aaSIlan Elias return; 2656a2968aaSIlan Elias } 2666a2968aaSIlan Elias 2676a2968aaSIlan Elias exit: 2684aeee687SChristophe Ricard if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) { 269122c1958SJulien Lefrique /* Data received in Target mode, forward to nfc core */ 270122c1958SJulien Lefrique err = nfc_tm_data_received(ndev->nfc_dev, skb); 271122c1958SJulien Lefrique if (err) 272122c1958SJulien Lefrique pr_err("unable to handle received data\n"); 273122c1958SJulien Lefrique } else { 2744aeee687SChristophe Ricard nci_data_exchange_complete(ndev, skb, conn_id, err); 275122c1958SJulien Lefrique } 2766a2968aaSIlan Elias } 2776a2968aaSIlan Elias 2786a2968aaSIlan Elias /* Rx Data packet */ 2796a2968aaSIlan Elias void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb) 2806a2968aaSIlan Elias { 2816a2968aaSIlan Elias __u8 pbf = nci_pbf(skb->data); 28298ff416fSChristophe Ricard __u8 status = 0; 2834aeee687SChristophe Ricard __u8 conn_id = nci_conn_id(skb->data); 2844aeee687SChristophe Ricard struct nci_conn_info *conn_info; 2856a2968aaSIlan Elias 28624bf3304SJoe Perches pr_debug("len %d\n", skb->len); 2876a2968aaSIlan Elias 28820c239c1SJoe Perches pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n", 2896a2968aaSIlan Elias nci_pbf(skb->data), 2906a2968aaSIlan Elias nci_conn_id(skb->data), 2916a2968aaSIlan Elias nci_plen(skb->data)); 2926a2968aaSIlan Elias 2934aeee687SChristophe Ricard conn_info = nci_get_conn_info_by_conn_id(ndev, nci_conn_id(skb->data)); 2944aeee687SChristophe Ricard if (!conn_info) 2954aeee687SChristophe Ricard return; 2964aeee687SChristophe Ricard 2976a2968aaSIlan Elias /* strip the nci data header */ 2986a2968aaSIlan Elias skb_pull(skb, NCI_DATA_HDR_SIZE); 2996a2968aaSIlan Elias 30083724c33SVincent Cuissard if (ndev->target_active_prot == NFC_PROTO_MIFARE || 30183724c33SVincent Cuissard ndev->target_active_prot == NFC_PROTO_JEWEL || 30283724c33SVincent Cuissard ndev->target_active_prot == NFC_PROTO_FELICA || 30383724c33SVincent Cuissard ndev->target_active_prot == NFC_PROTO_ISO15693) { 3046a2968aaSIlan Elias /* frame I/F => remove the status byte */ 30583724c33SVincent Cuissard pr_debug("frame I/F => remove the status byte\n"); 30698ff416fSChristophe Ricard status = skb->data[skb->len - 1]; 3076a2968aaSIlan Elias skb_trim(skb, (skb->len - 1)); 3086a2968aaSIlan Elias } 3096a2968aaSIlan Elias 3104aeee687SChristophe Ricard nci_add_rx_data_frag(ndev, skb, pbf, conn_id, nci_to_errno(status)); 3116a2968aaSIlan Elias } 312