KVM: Introduce gfn_to_hva_memslot_prot
[linux/fpc-iii.git] / net / nfc / nci / data.c
blob6c3aef852876c98acc09f6c8c554a60df1d7990e
1 /*
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.
7 * Written by Ilan Elias <ilane@ti.com>
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
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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
25 #include <linux/types.h>
26 #include <linux/interrupt.h>
27 #include <linux/wait.h>
28 #include <linux/bitops.h>
29 #include <linux/skbuff.h>
31 #include "../nfc.h"
32 #include <net/nfc/nci.h>
33 #include <net/nfc/nci_core.h>
34 #include <linux/nfc.h>
36 /* Complete data exchange transaction and forward skb to nfc core */
37 void nci_data_exchange_complete(struct nci_dev *ndev, struct sk_buff *skb,
38 int err)
40 data_exchange_cb_t cb = ndev->data_exchange_cb;
41 void *cb_context = ndev->data_exchange_cb_context;
43 pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
45 /* data exchange is complete, stop the data timer */
46 del_timer_sync(&ndev->data_timer);
47 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
49 if (cb) {
50 ndev->data_exchange_cb = NULL;
51 ndev->data_exchange_cb_context = NULL;
53 /* forward skb to nfc core */
54 cb(cb_context, skb, err);
55 } else if (skb) {
56 pr_err("no rx callback, dropping rx data...\n");
58 /* no waiting callback, free skb */
59 kfree_skb(skb);
62 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
65 /* ----------------- NCI TX Data ----------------- */
67 static inline void nci_push_data_hdr(struct nci_dev *ndev,
68 __u8 conn_id,
69 struct sk_buff *skb,
70 __u8 pbf)
72 struct nci_data_hdr *hdr;
73 int plen = skb->len;
75 hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE);
76 hdr->conn_id = conn_id;
77 hdr->rfu = 0;
78 hdr->plen = plen;
80 nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
81 nci_pbf_set((__u8 *)hdr, pbf);
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;
95 pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
97 __skb_queue_head_init(&frags_q);
99 while (total_len) {
100 frag_len =
101 min_t(int, total_len, ndev->max_data_pkt_payload_size);
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;
110 skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
112 /* first, copy the data */
113 memcpy(skb_put(skb_frag, frag_len), data, frag_len);
115 /* second, set the header */
116 nci_push_data_hdr(ndev, conn_id, skb_frag,
117 ((total_len == frag_len) ?
118 (NCI_PBF_LAST) : (NCI_PBF_CONT)));
120 __skb_queue_tail(&frags_q, skb_frag);
122 data += frag_len;
123 total_len -= frag_len;
125 pr_debug("frag_len %d, remaining total_len %d\n",
126 frag_len, total_len);
129 /* queue all fragments atomically */
130 spin_lock_irqsave(&ndev->tx_q.lock, flags);
132 while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
133 __skb_queue_tail(&ndev->tx_q, skb_frag);
135 spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
137 /* free the original skb */
138 kfree_skb(skb);
140 goto exit;
142 free_exit:
143 while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
144 kfree_skb(skb_frag);
146 exit:
147 return rc;
150 /* Send NCI data */
151 int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
153 int rc = 0;
155 pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
157 /* check if the packet need to be fragmented */
158 if (skb->len <= ndev->max_data_pkt_payload_size) {
159 /* no need to fragment packet */
160 nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
162 skb_queue_tail(&ndev->tx_q, skb);
163 } else {
164 /* fragment packet and queue the fragments */
165 rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
166 if (rc) {
167 pr_err("failed to fragment tx data packet\n");
168 goto free_exit;
172 queue_work(ndev->tx_wq, &ndev->tx_work);
174 goto exit;
176 free_exit:
177 kfree_skb(skb);
179 exit:
180 return rc;
183 /* ----------------- NCI RX Data ----------------- */
185 static void nci_add_rx_data_frag(struct nci_dev *ndev,
186 struct sk_buff *skb,
187 __u8 pbf)
189 int reassembly_len;
190 int err = 0;
192 if (ndev->rx_data_reassembly) {
193 reassembly_len = ndev->rx_data_reassembly->len;
195 /* first, make enough room for the already accumulated data */
196 if (skb_cow_head(skb, reassembly_len)) {
197 pr_err("error adding room for accumulated rx data\n");
199 kfree_skb(skb);
200 skb = NULL;
202 kfree_skb(ndev->rx_data_reassembly);
203 ndev->rx_data_reassembly = NULL;
205 err = -ENOMEM;
206 goto exit;
209 /* second, combine the two fragments */
210 memcpy(skb_push(skb, reassembly_len),
211 ndev->rx_data_reassembly->data,
212 reassembly_len);
214 /* third, free old reassembly */
215 kfree_skb(ndev->rx_data_reassembly);
216 ndev->rx_data_reassembly = NULL;
219 if (pbf == NCI_PBF_CONT) {
220 /* need to wait for next fragment, store skb and exit */
221 ndev->rx_data_reassembly = skb;
222 return;
225 exit:
226 nci_data_exchange_complete(ndev, skb, err);
229 /* Rx Data packet */
230 void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
232 __u8 pbf = nci_pbf(skb->data);
234 pr_debug("len %d\n", skb->len);
236 pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
237 nci_pbf(skb->data),
238 nci_conn_id(skb->data),
239 nci_plen(skb->data));
241 /* strip the nci data header */
242 skb_pull(skb, NCI_DATA_HDR_SIZE);
244 if (ndev->target_active_prot == NFC_PROTO_MIFARE) {
245 /* frame I/F => remove the status byte */
246 pr_debug("NFC_PROTO_MIFARE => remove the status byte\n");
247 skb_trim(skb, (skb->len - 1));
250 nci_add_rx_data_frag(ndev, skb, pbf);