2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Copyright (C) 2014 Marvell International Ltd.
8 * Written by Ilan Elias <ilane@ti.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
26 #include <linux/types.h>
27 #include <linux/interrupt.h>
28 #include <linux/wait.h>
29 #include <linux/bitops.h>
30 #include <linux/skbuff.h>
33 #include <net/nfc/nci.h>
34 #include <net/nfc/nci_core.h>
35 #include <linux/nfc.h>
37 /* Complete data exchange transaction and forward skb to nfc core */
38 void nci_data_exchange_complete(struct nci_dev *ndev, struct sk_buff *skb,
41 data_exchange_cb_t cb = ndev->data_exchange_cb;
42 void *cb_context = ndev->data_exchange_cb_context;
44 pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
46 /* data exchange is complete, stop the data timer */
47 del_timer_sync(&ndev->data_timer);
48 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
51 ndev->data_exchange_cb = NULL;
52 ndev->data_exchange_cb_context = NULL;
54 /* forward skb to nfc core */
55 cb(cb_context, skb, err);
57 pr_err("no rx callback, dropping rx data...\n");
59 /* no waiting callback, free skb */
63 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
66 /* ----------------- NCI TX Data ----------------- */
68 static inline void nci_push_data_hdr(struct nci_dev *ndev,
73 struct nci_data_hdr *hdr;
76 hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE);
77 hdr->conn_id = conn_id;
81 nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
82 nci_pbf_set((__u8 *)hdr, pbf);
85 static int nci_queue_tx_data_frags(struct nci_dev *ndev,
87 struct sk_buff *skb) {
88 int total_len = skb->len;
89 unsigned char *data = skb->data;
91 struct sk_buff_head frags_q;
92 struct sk_buff *skb_frag;
96 pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
98 __skb_queue_head_init(&frags_q);
102 min_t(int, total_len, ndev->max_data_pkt_payload_size);
104 skb_frag = nci_skb_alloc(ndev,
105 (NCI_DATA_HDR_SIZE + frag_len),
107 if (skb_frag == NULL) {
111 skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
113 /* first, copy the data */
114 memcpy(skb_put(skb_frag, frag_len), data, frag_len);
116 /* second, set the header */
117 nci_push_data_hdr(ndev, conn_id, skb_frag,
118 ((total_len == frag_len) ?
119 (NCI_PBF_LAST) : (NCI_PBF_CONT)));
121 __skb_queue_tail(&frags_q, skb_frag);
124 total_len -= frag_len;
126 pr_debug("frag_len %d, remaining total_len %d\n",
127 frag_len, total_len);
130 /* queue all fragments atomically */
131 spin_lock_irqsave(&ndev->tx_q.lock, flags);
133 while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
134 __skb_queue_tail(&ndev->tx_q, skb_frag);
136 spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
138 /* free the original skb */
144 while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
152 int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
156 pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
158 /* check if the packet need to be fragmented */
159 if (skb->len <= ndev->max_data_pkt_payload_size) {
160 /* no need to fragment packet */
161 nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
163 skb_queue_tail(&ndev->tx_q, skb);
165 /* fragment packet and queue the fragments */
166 rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
168 pr_err("failed to fragment tx data packet\n");
173 queue_work(ndev->tx_wq, &ndev->tx_work);
184 /* ----------------- NCI RX Data ----------------- */
186 static void nci_add_rx_data_frag(struct nci_dev *ndev,
188 __u8 pbf, __u8 status)
198 if (ndev->rx_data_reassembly) {
199 reassembly_len = ndev->rx_data_reassembly->len;
201 /* first, make enough room for the already accumulated data */
202 if (skb_cow_head(skb, reassembly_len)) {
203 pr_err("error adding room for accumulated rx data\n");
208 kfree_skb(ndev->rx_data_reassembly);
209 ndev->rx_data_reassembly = NULL;
215 /* second, combine the two fragments */
216 memcpy(skb_push(skb, reassembly_len),
217 ndev->rx_data_reassembly->data,
220 /* third, free old reassembly */
221 kfree_skb(ndev->rx_data_reassembly);
222 ndev->rx_data_reassembly = NULL;
225 if (pbf == NCI_PBF_CONT) {
226 /* need to wait for next fragment, store skb and exit */
227 ndev->rx_data_reassembly = skb;
232 if (ndev->nfc_dev->rf_mode == NFC_RF_INITIATOR) {
233 nci_data_exchange_complete(ndev, skb, err);
234 } else if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) {
235 /* Data received in Target mode, forward to nfc core */
236 err = nfc_tm_data_received(ndev->nfc_dev, skb);
238 pr_err("unable to handle received data\n");
240 pr_err("rf mode unknown\n");
246 void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
248 __u8 pbf = nci_pbf(skb->data);
251 pr_debug("len %d\n", skb->len);
253 pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
255 nci_conn_id(skb->data),
256 nci_plen(skb->data));
258 /* strip the nci data header */
259 skb_pull(skb, NCI_DATA_HDR_SIZE);
261 if (ndev->target_active_prot == NFC_PROTO_MIFARE ||
262 ndev->target_active_prot == NFC_PROTO_JEWEL ||
263 ndev->target_active_prot == NFC_PROTO_FELICA ||
264 ndev->target_active_prot == NFC_PROTO_ISO15693) {
265 /* frame I/F => remove the status byte */
266 pr_debug("frame I/F => remove the status byte\n");
267 status = skb->data[skb->len - 1];
268 skb_trim(skb, (skb->len - 1));
271 nci_add_rx_data_frag(ndev, skb, pbf, nci_to_errno(status));