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. 66a2968aaSIlan Elias * 76a2968aaSIlan Elias * Written by Ilan Elias <ilane@ti.com> 86a2968aaSIlan Elias * 96a2968aaSIlan Elias * This program is free software; you can redistribute it and/or modify 106a2968aaSIlan Elias * it under the terms of the GNU General Public License version 2 116a2968aaSIlan Elias * as published by the Free Software Foundation 126a2968aaSIlan Elias * 136a2968aaSIlan Elias * This program is distributed in the hope that it will be useful, 146a2968aaSIlan Elias * but WITHOUT ANY WARRANTY; without even the implied warranty of 156a2968aaSIlan Elias * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 166a2968aaSIlan Elias * GNU General Public License for more details. 176a2968aaSIlan Elias * 186a2968aaSIlan Elias * You should have received a copy of the GNU General Public License 196a2968aaSIlan Elias * along with this program; if not, write to the Free Software 206a2968aaSIlan Elias * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 216a2968aaSIlan Elias * 226a2968aaSIlan Elias */ 236a2968aaSIlan Elias 246a2968aaSIlan Elias #include <linux/types.h> 256a2968aaSIlan Elias #include <linux/interrupt.h> 266a2968aaSIlan Elias #include <linux/wait.h> 276a2968aaSIlan Elias #include <linux/bitops.h> 286a2968aaSIlan Elias #include <linux/skbuff.h> 296a2968aaSIlan Elias 306a2968aaSIlan Elias #include "../nfc.h" 316a2968aaSIlan Elias #include <net/nfc/nci.h> 326a2968aaSIlan Elias #include <net/nfc/nci_core.h> 336a2968aaSIlan Elias #include <linux/nfc.h> 346a2968aaSIlan Elias 356a2968aaSIlan Elias /* Complete data exchange transaction and forward skb to nfc core */ 366a2968aaSIlan Elias void nci_data_exchange_complete(struct nci_dev *ndev, 376a2968aaSIlan Elias struct sk_buff *skb, 386a2968aaSIlan Elias int err) 396a2968aaSIlan Elias { 406a2968aaSIlan Elias data_exchange_cb_t cb = ndev->data_exchange_cb; 416a2968aaSIlan Elias void *cb_context = ndev->data_exchange_cb_context; 426a2968aaSIlan Elias 436a2968aaSIlan Elias nfc_dbg("entry, len %d, err %d", ((skb) ? (skb->len) : (0)), err); 446a2968aaSIlan Elias 456a2968aaSIlan Elias if (cb) { 466a2968aaSIlan Elias ndev->data_exchange_cb = NULL; 476a2968aaSIlan Elias ndev->data_exchange_cb_context = 0; 486a2968aaSIlan Elias 496a2968aaSIlan Elias /* forward skb to nfc core */ 506a2968aaSIlan Elias cb(cb_context, skb, err); 516a2968aaSIlan Elias } else if (skb) { 526a2968aaSIlan Elias nfc_err("no rx callback, dropping rx data..."); 536a2968aaSIlan Elias 546a2968aaSIlan Elias /* no waiting callback, free skb */ 556a2968aaSIlan Elias kfree_skb(skb); 566a2968aaSIlan Elias } 57*38f04c6bSIlan Elias 58*38f04c6bSIlan Elias clear_bit(NCI_DATA_EXCHANGE, &ndev->flags); 596a2968aaSIlan Elias } 606a2968aaSIlan Elias 616a2968aaSIlan Elias /* ----------------- NCI TX Data ----------------- */ 626a2968aaSIlan Elias 636a2968aaSIlan Elias static inline void nci_push_data_hdr(struct nci_dev *ndev, 646a2968aaSIlan Elias __u8 conn_id, 656a2968aaSIlan Elias struct sk_buff *skb, 666a2968aaSIlan Elias __u8 pbf) 676a2968aaSIlan Elias { 686a2968aaSIlan Elias struct nci_data_hdr *hdr; 696a2968aaSIlan Elias int plen = skb->len; 706a2968aaSIlan Elias 716a2968aaSIlan Elias hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE); 726a2968aaSIlan Elias hdr->conn_id = conn_id; 736a2968aaSIlan Elias hdr->rfu = 0; 746a2968aaSIlan Elias hdr->plen = plen; 756a2968aaSIlan Elias 766a2968aaSIlan Elias nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT); 776a2968aaSIlan Elias nci_pbf_set((__u8 *)hdr, pbf); 786a2968aaSIlan Elias 796a2968aaSIlan Elias skb->dev = (void *) ndev; 806a2968aaSIlan Elias } 816a2968aaSIlan Elias 826a2968aaSIlan Elias static int nci_queue_tx_data_frags(struct nci_dev *ndev, 836a2968aaSIlan Elias __u8 conn_id, 846a2968aaSIlan Elias struct sk_buff *skb) { 856a2968aaSIlan Elias int total_len = skb->len; 866a2968aaSIlan Elias unsigned char *data = skb->data; 876a2968aaSIlan Elias unsigned long flags; 886a2968aaSIlan Elias struct sk_buff_head frags_q; 896a2968aaSIlan Elias struct sk_buff *skb_frag; 906a2968aaSIlan Elias int frag_len; 916a2968aaSIlan Elias int rc = 0; 926a2968aaSIlan Elias 936a2968aaSIlan Elias nfc_dbg("entry, conn_id 0x%x, total_len %d", conn_id, total_len); 946a2968aaSIlan Elias 956a2968aaSIlan Elias __skb_queue_head_init(&frags_q); 966a2968aaSIlan Elias 976a2968aaSIlan Elias while (total_len) { 986a2968aaSIlan Elias frag_len = min_t(int, total_len, ndev->max_pkt_payload_size); 996a2968aaSIlan Elias 1006a2968aaSIlan Elias skb_frag = nci_skb_alloc(ndev, 1016a2968aaSIlan Elias (NCI_DATA_HDR_SIZE + frag_len), 1026a2968aaSIlan Elias GFP_KERNEL); 1036a2968aaSIlan Elias if (skb_frag == NULL) { 1046a2968aaSIlan Elias rc = -ENOMEM; 1056a2968aaSIlan Elias goto free_exit; 1066a2968aaSIlan Elias } 1076a2968aaSIlan Elias skb_reserve(skb_frag, NCI_DATA_HDR_SIZE); 1086a2968aaSIlan Elias 1096a2968aaSIlan Elias /* first, copy the data */ 1106a2968aaSIlan Elias memcpy(skb_put(skb_frag, frag_len), data, frag_len); 1116a2968aaSIlan Elias 1126a2968aaSIlan Elias /* second, set the header */ 1136a2968aaSIlan Elias nci_push_data_hdr(ndev, conn_id, skb_frag, 1146a2968aaSIlan Elias ((total_len == frag_len) ? (NCI_PBF_LAST) : (NCI_PBF_CONT))); 1156a2968aaSIlan Elias 1166a2968aaSIlan Elias __skb_queue_tail(&frags_q, skb_frag); 1176a2968aaSIlan Elias 1186a2968aaSIlan Elias data += frag_len; 1196a2968aaSIlan Elias total_len -= frag_len; 1206a2968aaSIlan Elias 1216a2968aaSIlan Elias nfc_dbg("frag_len %d, remaining total_len %d", 1226a2968aaSIlan Elias frag_len, total_len); 1236a2968aaSIlan Elias } 1246a2968aaSIlan Elias 1256a2968aaSIlan Elias /* queue all fragments atomically */ 1266a2968aaSIlan Elias spin_lock_irqsave(&ndev->tx_q.lock, flags); 1276a2968aaSIlan Elias 1286a2968aaSIlan Elias while ((skb_frag = __skb_dequeue(&frags_q)) != NULL) 1296a2968aaSIlan Elias __skb_queue_tail(&ndev->tx_q, skb_frag); 1306a2968aaSIlan Elias 1316a2968aaSIlan Elias spin_unlock_irqrestore(&ndev->tx_q.lock, flags); 1326a2968aaSIlan Elias 1336a2968aaSIlan Elias /* free the original skb */ 1346a2968aaSIlan Elias kfree_skb(skb); 1356a2968aaSIlan Elias 1366a2968aaSIlan Elias goto exit; 1376a2968aaSIlan Elias 1386a2968aaSIlan Elias free_exit: 1396a2968aaSIlan Elias while ((skb_frag = __skb_dequeue(&frags_q)) != NULL) 1406a2968aaSIlan Elias kfree_skb(skb_frag); 1416a2968aaSIlan Elias 1426a2968aaSIlan Elias exit: 1436a2968aaSIlan Elias return rc; 1446a2968aaSIlan Elias } 1456a2968aaSIlan Elias 1466a2968aaSIlan Elias /* Send NCI data */ 1476a2968aaSIlan Elias int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb) 1486a2968aaSIlan Elias { 1496a2968aaSIlan Elias int rc = 0; 1506a2968aaSIlan Elias 1516a2968aaSIlan Elias nfc_dbg("entry, conn_id 0x%x, plen %d", conn_id, skb->len); 1526a2968aaSIlan Elias 1536a2968aaSIlan Elias /* check if the packet need to be fragmented */ 1546a2968aaSIlan Elias if (skb->len <= ndev->max_pkt_payload_size) { 1556a2968aaSIlan Elias /* no need to fragment packet */ 1566a2968aaSIlan Elias nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST); 1576a2968aaSIlan Elias 1586a2968aaSIlan Elias skb_queue_tail(&ndev->tx_q, skb); 1596a2968aaSIlan Elias } else { 1606a2968aaSIlan Elias /* fragment packet and queue the fragments */ 1616a2968aaSIlan Elias rc = nci_queue_tx_data_frags(ndev, conn_id, skb); 1626a2968aaSIlan Elias if (rc) { 1636a2968aaSIlan Elias nfc_err("failed to fragment tx data packet"); 1646a2968aaSIlan Elias goto free_exit; 1656a2968aaSIlan Elias } 1666a2968aaSIlan Elias } 1676a2968aaSIlan Elias 1686a2968aaSIlan Elias queue_work(ndev->tx_wq, &ndev->tx_work); 1696a2968aaSIlan Elias 1706a2968aaSIlan Elias goto exit; 1716a2968aaSIlan Elias 1726a2968aaSIlan Elias free_exit: 1736a2968aaSIlan Elias kfree_skb(skb); 1746a2968aaSIlan Elias 1756a2968aaSIlan Elias exit: 1766a2968aaSIlan Elias return rc; 1776a2968aaSIlan Elias } 1786a2968aaSIlan Elias 1796a2968aaSIlan Elias /* ----------------- NCI RX Data ----------------- */ 1806a2968aaSIlan Elias 1816a2968aaSIlan Elias static void nci_add_rx_data_frag(struct nci_dev *ndev, 1826a2968aaSIlan Elias struct sk_buff *skb, 1836a2968aaSIlan Elias __u8 pbf) 1846a2968aaSIlan Elias { 1856a2968aaSIlan Elias int reassembly_len; 1866a2968aaSIlan Elias int err = 0; 1876a2968aaSIlan Elias 1886a2968aaSIlan Elias if (ndev->rx_data_reassembly) { 1896a2968aaSIlan Elias reassembly_len = ndev->rx_data_reassembly->len; 1906a2968aaSIlan Elias 1916a2968aaSIlan Elias /* first, make enough room for the already accumulated data */ 1926a2968aaSIlan Elias if (skb_cow_head(skb, reassembly_len)) { 1936a2968aaSIlan Elias nfc_err("error adding room for accumulated rx data"); 1946a2968aaSIlan Elias 1956a2968aaSIlan Elias kfree_skb(skb); 1966a2968aaSIlan Elias skb = 0; 1976a2968aaSIlan Elias 1986a2968aaSIlan Elias kfree_skb(ndev->rx_data_reassembly); 1996a2968aaSIlan Elias ndev->rx_data_reassembly = 0; 2006a2968aaSIlan Elias 2016a2968aaSIlan Elias err = -ENOMEM; 2026a2968aaSIlan Elias goto exit; 2036a2968aaSIlan Elias } 2046a2968aaSIlan Elias 2056a2968aaSIlan Elias /* second, combine the two fragments */ 2066a2968aaSIlan Elias memcpy(skb_push(skb, reassembly_len), 2076a2968aaSIlan Elias ndev->rx_data_reassembly->data, 2086a2968aaSIlan Elias reassembly_len); 2096a2968aaSIlan Elias 2106a2968aaSIlan Elias /* third, free old reassembly */ 2116a2968aaSIlan Elias kfree_skb(ndev->rx_data_reassembly); 2126a2968aaSIlan Elias ndev->rx_data_reassembly = 0; 2136a2968aaSIlan Elias } 2146a2968aaSIlan Elias 2156a2968aaSIlan Elias if (pbf == NCI_PBF_CONT) { 2166a2968aaSIlan Elias /* need to wait for next fragment, store skb and exit */ 2176a2968aaSIlan Elias ndev->rx_data_reassembly = skb; 2186a2968aaSIlan Elias return; 2196a2968aaSIlan Elias } 2206a2968aaSIlan Elias 2216a2968aaSIlan Elias exit: 2226a2968aaSIlan Elias nci_data_exchange_complete(ndev, skb, err); 2236a2968aaSIlan Elias } 2246a2968aaSIlan Elias 2256a2968aaSIlan Elias /* Rx Data packet */ 2266a2968aaSIlan Elias void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb) 2276a2968aaSIlan Elias { 2286a2968aaSIlan Elias __u8 pbf = nci_pbf(skb->data); 2296a2968aaSIlan Elias 2306a2968aaSIlan Elias nfc_dbg("entry, len %d", skb->len); 2316a2968aaSIlan Elias 2326a2968aaSIlan Elias nfc_dbg("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d", 2336a2968aaSIlan Elias nci_pbf(skb->data), 2346a2968aaSIlan Elias nci_conn_id(skb->data), 2356a2968aaSIlan Elias nci_plen(skb->data)); 2366a2968aaSIlan Elias 2376a2968aaSIlan Elias /* strip the nci data header */ 2386a2968aaSIlan Elias skb_pull(skb, NCI_DATA_HDR_SIZE); 2396a2968aaSIlan Elias 2406a2968aaSIlan Elias if (ndev->target_active_prot == NFC_PROTO_MIFARE) { 2416a2968aaSIlan Elias /* frame I/F => remove the status byte */ 2426a2968aaSIlan Elias nfc_dbg("NFC_PROTO_MIFARE => remove the status byte"); 2436a2968aaSIlan Elias skb_trim(skb, (skb->len - 1)); 2446a2968aaSIlan Elias } 2456a2968aaSIlan Elias 2466a2968aaSIlan Elias nci_add_rx_data_frag(ndev, skb, pbf); 2476a2968aaSIlan Elias } 248