xref: /openbmc/linux/net/nfc/nci/data.c (revision 24bf3304)
1 /*
2  *  The NFC Controller Interface is the communication protocol between an
3  *  NFC Controller (NFCC) and a Device Host (DH).
4  *
5  *  Copyright (C) 2011 Texas Instruments, Inc.
6  *
7  *  Written by Ilan Elias <ilane@ti.com>
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2
11  *  as published by the Free Software Foundation
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 
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>
31 
32 #include "../nfc.h"
33 #include <net/nfc/nci.h>
34 #include <net/nfc/nci_core.h>
35 #include <linux/nfc.h>
36 
37 /* Complete data exchange transaction and forward skb to nfc core */
38 void nci_data_exchange_complete(struct nci_dev *ndev,
39 				struct sk_buff *skb,
40 				int err)
41 {
42 	data_exchange_cb_t cb = ndev->data_exchange_cb;
43 	void *cb_context = ndev->data_exchange_cb_context;
44 
45 	pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
46 
47 	if (cb) {
48 		ndev->data_exchange_cb = NULL;
49 		ndev->data_exchange_cb_context = 0;
50 
51 		/* forward skb to nfc core */
52 		cb(cb_context, skb, err);
53 	} else if (skb) {
54 		pr_err("no rx callback, dropping rx data...\n");
55 
56 		/* no waiting callback, free skb */
57 		kfree_skb(skb);
58 	}
59 
60 	clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
61 }
62 
63 /* ----------------- NCI TX Data ----------------- */
64 
65 static inline void nci_push_data_hdr(struct nci_dev *ndev,
66 					__u8 conn_id,
67 					struct sk_buff *skb,
68 					__u8 pbf)
69 {
70 	struct nci_data_hdr *hdr;
71 	int plen = skb->len;
72 
73 	hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE);
74 	hdr->conn_id = conn_id;
75 	hdr->rfu = 0;
76 	hdr->plen = plen;
77 
78 	nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
79 	nci_pbf_set((__u8 *)hdr, pbf);
80 
81 	skb->dev = (void *) ndev;
82 }
83 
84 static int nci_queue_tx_data_frags(struct nci_dev *ndev,
85 					__u8 conn_id,
86 					struct sk_buff *skb) {
87 	int total_len = skb->len;
88 	unsigned char *data = skb->data;
89 	unsigned long flags;
90 	struct sk_buff_head frags_q;
91 	struct sk_buff *skb_frag;
92 	int frag_len;
93 	int rc = 0;
94 
95 	pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
96 
97 	__skb_queue_head_init(&frags_q);
98 
99 	while (total_len) {
100 		frag_len =
101 			min_t(int, total_len, ndev->max_data_pkt_payload_size);
102 
103 		skb_frag = nci_skb_alloc(ndev,
104 					(NCI_DATA_HDR_SIZE + frag_len),
105 					GFP_KERNEL);
106 		if (skb_frag == NULL) {
107 			rc = -ENOMEM;
108 			goto free_exit;
109 		}
110 		skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
111 
112 		/* first, copy the data */
113 		memcpy(skb_put(skb_frag, frag_len), data, frag_len);
114 
115 		/* second, set the header */
116 		nci_push_data_hdr(ndev, conn_id, skb_frag,
117 		((total_len == frag_len) ? (NCI_PBF_LAST) : (NCI_PBF_CONT)));
118 
119 		__skb_queue_tail(&frags_q, skb_frag);
120 
121 		data += frag_len;
122 		total_len -= frag_len;
123 
124 		pr_debug("frag_len %d, remaining total_len %d\n",
125 			 frag_len, total_len);
126 	}
127 
128 	/* queue all fragments atomically */
129 	spin_lock_irqsave(&ndev->tx_q.lock, flags);
130 
131 	while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
132 		__skb_queue_tail(&ndev->tx_q, skb_frag);
133 
134 	spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
135 
136 	/* free the original skb */
137 	kfree_skb(skb);
138 
139 	goto exit;
140 
141 free_exit:
142 	while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
143 		kfree_skb(skb_frag);
144 
145 exit:
146 	return rc;
147 }
148 
149 /* Send NCI data */
150 int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
151 {
152 	int rc = 0;
153 
154 	pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
155 
156 	/* check if the packet need to be fragmented */
157 	if (skb->len <= ndev->max_data_pkt_payload_size) {
158 		/* no need to fragment packet */
159 		nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
160 
161 		skb_queue_tail(&ndev->tx_q, skb);
162 	} else {
163 		/* fragment packet and queue the fragments */
164 		rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
165 		if (rc) {
166 			pr_err("failed to fragment tx data packet\n");
167 			goto free_exit;
168 		}
169 	}
170 
171 	queue_work(ndev->tx_wq, &ndev->tx_work);
172 
173 	goto exit;
174 
175 free_exit:
176 	kfree_skb(skb);
177 
178 exit:
179 	return rc;
180 }
181 
182 /* ----------------- NCI RX Data ----------------- */
183 
184 static void nci_add_rx_data_frag(struct nci_dev *ndev,
185 				struct sk_buff *skb,
186 				__u8 pbf)
187 {
188 	int reassembly_len;
189 	int err = 0;
190 
191 	if (ndev->rx_data_reassembly) {
192 		reassembly_len = ndev->rx_data_reassembly->len;
193 
194 		/* first, make enough room for the already accumulated data */
195 		if (skb_cow_head(skb, reassembly_len)) {
196 			pr_err("error adding room for accumulated rx data\n");
197 
198 			kfree_skb(skb);
199 			skb = 0;
200 
201 			kfree_skb(ndev->rx_data_reassembly);
202 			ndev->rx_data_reassembly = 0;
203 
204 			err = -ENOMEM;
205 			goto exit;
206 		}
207 
208 		/* second, combine the two fragments */
209 		memcpy(skb_push(skb, reassembly_len),
210 				ndev->rx_data_reassembly->data,
211 				reassembly_len);
212 
213 		/* third, free old reassembly */
214 		kfree_skb(ndev->rx_data_reassembly);
215 		ndev->rx_data_reassembly = 0;
216 	}
217 
218 	if (pbf == NCI_PBF_CONT) {
219 		/* need to wait for next fragment, store skb and exit */
220 		ndev->rx_data_reassembly = skb;
221 		return;
222 	}
223 
224 exit:
225 	nci_data_exchange_complete(ndev, skb, err);
226 }
227 
228 /* Rx Data packet */
229 void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
230 {
231 	__u8 pbf = nci_pbf(skb->data);
232 
233 	pr_debug("len %d\n", skb->len);
234 
235 	pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
236 		 nci_pbf(skb->data),
237 		 nci_conn_id(skb->data),
238 		 nci_plen(skb->data));
239 
240 	/* strip the nci data header */
241 	skb_pull(skb, NCI_DATA_HDR_SIZE);
242 
243 	if (ndev->target_active_prot == NFC_PROTO_MIFARE) {
244 		/* frame I/F => remove the status byte */
245 		pr_debug("NFC_PROTO_MIFARE => remove the status byte\n");
246 		skb_trim(skb, (skb->len - 1));
247 	}
248 
249 	nci_add_rx_data_frag(ndev, skb, pbf);
250 }
251