blob: 2a9399dd6c685566928054b16e2734984d737c94 [file] [log] [blame]
Ilan Elias6a2968a2011-09-18 11:19:35 +03001/*
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
Samuel Ortiz52858b52011-12-14 16:43:05 +010024#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080025
Ilan Elias6a2968a2011-09-18 11:19:35 +030026#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 */
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010038void nci_data_exchange_complete(struct nci_dev *ndev, struct sk_buff *skb,
Ilan Elias6a2968a2011-09-18 11:19:35 +030039 int err)
40{
41 data_exchange_cb_t cb = ndev->data_exchange_cb;
42 void *cb_context = ndev->data_exchange_cb_context;
43
Joe Perches24bf3302011-11-29 11:37:35 -080044 pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
Ilan Elias6a2968a2011-09-18 11:19:35 +030045
Ilan Eliasc4bf98b2012-01-17 12:03:50 +020046 /* data exchange is complete, stop the data timer */
47 del_timer_sync(&ndev->data_timer);
48 clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
49
Ilan Elias6a2968a2011-09-18 11:19:35 +030050 if (cb) {
51 ndev->data_exchange_cb = NULL;
H Hartley Sweeten040487f2012-05-07 12:31:24 +020052 ndev->data_exchange_cb_context = NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +030053
54 /* forward skb to nfc core */
55 cb(cb_context, skb, err);
56 } else if (skb) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -080057 pr_err("no rx callback, dropping rx data...\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +030058
59 /* no waiting callback, free skb */
60 kfree_skb(skb);
61 }
Ilan Elias38f04c62011-09-22 11:36:19 +030062
63 clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
Ilan Elias6a2968a2011-09-18 11:19:35 +030064}
65
66/* ----------------- NCI TX Data ----------------- */
67
68static inline void nci_push_data_hdr(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010069 __u8 conn_id,
70 struct sk_buff *skb,
71 __u8 pbf)
Ilan Elias6a2968a2011-09-18 11:19:35 +030072{
73 struct nci_data_hdr *hdr;
74 int plen = skb->len;
75
76 hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE);
77 hdr->conn_id = conn_id;
78 hdr->rfu = 0;
79 hdr->plen = plen;
80
81 nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
82 nci_pbf_set((__u8 *)hdr, pbf);
Ilan Elias6a2968a2011-09-18 11:19:35 +030083}
84
85static int nci_queue_tx_data_frags(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010086 __u8 conn_id,
87 struct sk_buff *skb) {
Ilan Elias6a2968a2011-09-18 11:19:35 +030088 int total_len = skb->len;
89 unsigned char *data = skb->data;
90 unsigned long flags;
91 struct sk_buff_head frags_q;
92 struct sk_buff *skb_frag;
93 int frag_len;
94 int rc = 0;
95
Joe Perches24bf3302011-11-29 11:37:35 -080096 pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
Ilan Elias6a2968a2011-09-18 11:19:35 +030097
98 __skb_queue_head_init(&frags_q);
99
100 while (total_len) {
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200101 frag_len =
102 min_t(int, total_len, ndev->max_data_pkt_payload_size);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300103
104 skb_frag = nci_skb_alloc(ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100105 (NCI_DATA_HDR_SIZE + frag_len),
106 GFP_KERNEL);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300107 if (skb_frag == NULL) {
108 rc = -ENOMEM;
109 goto free_exit;
110 }
111 skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
112
113 /* first, copy the data */
114 memcpy(skb_put(skb_frag, frag_len), data, frag_len);
115
116 /* second, set the header */
117 nci_push_data_hdr(ndev, conn_id, skb_frag,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100118 ((total_len == frag_len) ?
119 (NCI_PBF_LAST) : (NCI_PBF_CONT)));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300120
121 __skb_queue_tail(&frags_q, skb_frag);
122
123 data += frag_len;
124 total_len -= frag_len;
125
Joe Perches20c239c2011-11-29 11:37:33 -0800126 pr_debug("frag_len %d, remaining total_len %d\n",
127 frag_len, total_len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300128 }
129
130 /* queue all fragments atomically */
131 spin_lock_irqsave(&ndev->tx_q.lock, flags);
132
133 while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
134 __skb_queue_tail(&ndev->tx_q, skb_frag);
135
136 spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
137
138 /* free the original skb */
139 kfree_skb(skb);
140
141 goto exit;
142
143free_exit:
144 while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
145 kfree_skb(skb_frag);
146
147exit:
148 return rc;
149}
150
151/* Send NCI data */
152int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
153{
154 int rc = 0;
155
Joe Perches24bf3302011-11-29 11:37:35 -0800156 pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300157
158 /* check if the packet need to be fragmented */
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200159 if (skb->len <= ndev->max_data_pkt_payload_size) {
Ilan Elias6a2968a2011-09-18 11:19:35 +0300160 /* no need to fragment packet */
161 nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
162
163 skb_queue_tail(&ndev->tx_q, skb);
164 } else {
165 /* fragment packet and queue the fragments */
166 rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
167 if (rc) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800168 pr_err("failed to fragment tx data packet\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300169 goto free_exit;
170 }
171 }
172
173 queue_work(ndev->tx_wq, &ndev->tx_work);
174
175 goto exit;
176
177free_exit:
178 kfree_skb(skb);
179
180exit:
181 return rc;
182}
183
184/* ----------------- NCI RX Data ----------------- */
185
186static void nci_add_rx_data_frag(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100187 struct sk_buff *skb,
188 __u8 pbf)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300189{
190 int reassembly_len;
191 int err = 0;
192
193 if (ndev->rx_data_reassembly) {
194 reassembly_len = ndev->rx_data_reassembly->len;
195
196 /* first, make enough room for the already accumulated data */
197 if (skb_cow_head(skb, reassembly_len)) {
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800198 pr_err("error adding room for accumulated rx data\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300199
200 kfree_skb(skb);
H Hartley Sweeten040487f2012-05-07 12:31:24 +0200201 skb = NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300202
203 kfree_skb(ndev->rx_data_reassembly);
H Hartley Sweeten040487f2012-05-07 12:31:24 +0200204 ndev->rx_data_reassembly = NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300205
206 err = -ENOMEM;
207 goto exit;
208 }
209
210 /* second, combine the two fragments */
211 memcpy(skb_push(skb, reassembly_len),
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100212 ndev->rx_data_reassembly->data,
213 reassembly_len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300214
215 /* third, free old reassembly */
216 kfree_skb(ndev->rx_data_reassembly);
H Hartley Sweeten040487f2012-05-07 12:31:24 +0200217 ndev->rx_data_reassembly = NULL;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300218 }
219
220 if (pbf == NCI_PBF_CONT) {
221 /* need to wait for next fragment, store skb and exit */
222 ndev->rx_data_reassembly = skb;
223 return;
224 }
225
226exit:
227 nci_data_exchange_complete(ndev, skb, err);
228}
229
230/* Rx Data packet */
231void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
232{
233 __u8 pbf = nci_pbf(skb->data);
234
Joe Perches24bf3302011-11-29 11:37:35 -0800235 pr_debug("len %d\n", skb->len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300236
Joe Perches20c239c2011-11-29 11:37:33 -0800237 pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
238 nci_pbf(skb->data),
239 nci_conn_id(skb->data),
240 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300241
242 /* strip the nci data header */
243 skb_pull(skb, NCI_DATA_HDR_SIZE);
244
245 if (ndev->target_active_prot == NFC_PROTO_MIFARE) {
246 /* frame I/F => remove the status byte */
Joe Perches20c239c2011-11-29 11:37:33 -0800247 pr_debug("NFC_PROTO_MIFARE => remove the status byte\n");
Ilan Elias6a2968a2011-09-18 11:19:35 +0300248 skb_trim(skb, (skb->len - 1));
249 }
250
251 nci_add_rx_data_frag(ndev, skb, pbf);
252}