Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 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. |
Julien Lefrique | 122c195 | 2014-10-21 16:52:49 +0200 | [diff] [blame] | 6 | * Copyright (C) 2014 Marvell International Ltd. |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 7 | * |
| 8 | * Written by Ilan Elias <ilane@ti.com> |
| 9 | * |
| 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 |
| 13 | * |
| 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. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
Jeff Kirsher | 98b32de | 2013-12-06 08:56:16 -0800 | [diff] [blame] | 20 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
Samuel Ortiz | 52858b5 | 2011-12-14 16:43:05 +0100 | [diff] [blame] | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 25 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 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 */ |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 38 | void nci_data_exchange_complete(struct nci_dev *ndev, struct sk_buff *skb, |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 39 | __u8 conn_id, int err) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 40 | { |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 41 | struct nci_conn_info *conn_info; |
| 42 | data_exchange_cb_t cb; |
| 43 | void *cb_context; |
| 44 | |
| 45 | conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id); |
| 46 | if (!conn_info) { |
| 47 | kfree_skb(skb); |
| 48 | goto exit; |
| 49 | } |
| 50 | |
| 51 | cb = conn_info->data_exchange_cb; |
| 52 | cb_context = conn_info->data_exchange_cb_context; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 53 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 54 | pr_debug("len %d, err %d\n", skb ? skb->len : 0, err); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 55 | |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 56 | /* data exchange is complete, stop the data timer */ |
| 57 | del_timer_sync(&ndev->data_timer); |
| 58 | clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); |
| 59 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 60 | if (cb) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 61 | /* forward skb to nfc core */ |
| 62 | cb(cb_context, skb, err); |
| 63 | } else if (skb) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 64 | pr_err("no rx callback, dropping rx data...\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 65 | |
| 66 | /* no waiting callback, free skb */ |
| 67 | kfree_skb(skb); |
| 68 | } |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 69 | |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 70 | exit: |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 71 | clear_bit(NCI_DATA_EXCHANGE, &ndev->flags); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* ----------------- NCI TX Data ----------------- */ |
| 75 | |
| 76 | static inline void nci_push_data_hdr(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 77 | __u8 conn_id, |
| 78 | struct sk_buff *skb, |
| 79 | __u8 pbf) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 80 | { |
| 81 | struct nci_data_hdr *hdr; |
| 82 | int plen = skb->len; |
| 83 | |
| 84 | hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE); |
| 85 | hdr->conn_id = conn_id; |
| 86 | hdr->rfu = 0; |
| 87 | hdr->plen = plen; |
| 88 | |
| 89 | nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT); |
| 90 | nci_pbf_set((__u8 *)hdr, pbf); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static int nci_queue_tx_data_frags(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 94 | __u8 conn_id, |
| 95 | struct sk_buff *skb) { |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 96 | struct nci_conn_info *conn_info; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 97 | int total_len = skb->len; |
| 98 | unsigned char *data = skb->data; |
| 99 | unsigned long flags; |
| 100 | struct sk_buff_head frags_q; |
| 101 | struct sk_buff *skb_frag; |
| 102 | int frag_len; |
| 103 | int rc = 0; |
| 104 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 105 | pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 106 | |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 107 | conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id); |
| 108 | if (!conn_info) { |
| 109 | rc = -EPROTO; |
| 110 | goto free_exit; |
| 111 | } |
| 112 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 113 | __skb_queue_head_init(&frags_q); |
| 114 | |
| 115 | while (total_len) { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 116 | frag_len = |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 117 | min_t(int, total_len, conn_info->max_pkt_payload_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 118 | |
| 119 | skb_frag = nci_skb_alloc(ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 120 | (NCI_DATA_HDR_SIZE + frag_len), |
| 121 | GFP_KERNEL); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 122 | if (skb_frag == NULL) { |
| 123 | rc = -ENOMEM; |
| 124 | goto free_exit; |
| 125 | } |
| 126 | skb_reserve(skb_frag, NCI_DATA_HDR_SIZE); |
| 127 | |
| 128 | /* first, copy the data */ |
| 129 | memcpy(skb_put(skb_frag, frag_len), data, frag_len); |
| 130 | |
| 131 | /* second, set the header */ |
| 132 | nci_push_data_hdr(ndev, conn_id, skb_frag, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 133 | ((total_len == frag_len) ? |
| 134 | (NCI_PBF_LAST) : (NCI_PBF_CONT))); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 135 | |
| 136 | __skb_queue_tail(&frags_q, skb_frag); |
| 137 | |
| 138 | data += frag_len; |
| 139 | total_len -= frag_len; |
| 140 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 141 | pr_debug("frag_len %d, remaining total_len %d\n", |
| 142 | frag_len, total_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* queue all fragments atomically */ |
| 146 | spin_lock_irqsave(&ndev->tx_q.lock, flags); |
| 147 | |
| 148 | while ((skb_frag = __skb_dequeue(&frags_q)) != NULL) |
| 149 | __skb_queue_tail(&ndev->tx_q, skb_frag); |
| 150 | |
| 151 | spin_unlock_irqrestore(&ndev->tx_q.lock, flags); |
| 152 | |
| 153 | /* free the original skb */ |
| 154 | kfree_skb(skb); |
| 155 | |
| 156 | goto exit; |
| 157 | |
| 158 | free_exit: |
| 159 | while ((skb_frag = __skb_dequeue(&frags_q)) != NULL) |
| 160 | kfree_skb(skb_frag); |
| 161 | |
| 162 | exit: |
| 163 | return rc; |
| 164 | } |
| 165 | |
| 166 | /* Send NCI data */ |
| 167 | int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb) |
| 168 | { |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 169 | struct nci_conn_info *conn_info; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 170 | int rc = 0; |
| 171 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 172 | pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 173 | |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 174 | conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id); |
| 175 | if (!conn_info) { |
| 176 | rc = -EPROTO; |
| 177 | goto free_exit; |
| 178 | } |
| 179 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 180 | /* check if the packet need to be fragmented */ |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 181 | if (skb->len <= conn_info->max_pkt_payload_len) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 182 | /* no need to fragment packet */ |
| 183 | nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST); |
| 184 | |
| 185 | skb_queue_tail(&ndev->tx_q, skb); |
| 186 | } else { |
| 187 | /* fragment packet and queue the fragments */ |
| 188 | rc = nci_queue_tx_data_frags(ndev, conn_id, skb); |
| 189 | if (rc) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 190 | pr_err("failed to fragment tx data packet\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 191 | goto free_exit; |
| 192 | } |
| 193 | } |
| 194 | |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 195 | ndev->cur_conn_id = conn_id; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 196 | queue_work(ndev->tx_wq, &ndev->tx_work); |
| 197 | |
| 198 | goto exit; |
| 199 | |
| 200 | free_exit: |
| 201 | kfree_skb(skb); |
| 202 | |
| 203 | exit: |
| 204 | return rc; |
| 205 | } |
| 206 | |
| 207 | /* ----------------- NCI RX Data ----------------- */ |
| 208 | |
| 209 | static void nci_add_rx_data_frag(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 210 | struct sk_buff *skb, |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 211 | __u8 pbf, __u8 conn_id, __u8 status) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 212 | { |
| 213 | int reassembly_len; |
| 214 | int err = 0; |
| 215 | |
Christophe Ricard | 98ff416 | 2014-12-02 21:27:47 +0100 | [diff] [blame] | 216 | if (status) { |
| 217 | err = status; |
| 218 | goto exit; |
| 219 | } |
| 220 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 221 | if (ndev->rx_data_reassembly) { |
| 222 | reassembly_len = ndev->rx_data_reassembly->len; |
| 223 | |
| 224 | /* first, make enough room for the already accumulated data */ |
| 225 | if (skb_cow_head(skb, reassembly_len)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 226 | pr_err("error adding room for accumulated rx data\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 227 | |
| 228 | kfree_skb(skb); |
H Hartley Sweeten | 040487f | 2012-05-07 12:31:24 +0200 | [diff] [blame] | 229 | skb = NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 230 | |
| 231 | kfree_skb(ndev->rx_data_reassembly); |
H Hartley Sweeten | 040487f | 2012-05-07 12:31:24 +0200 | [diff] [blame] | 232 | ndev->rx_data_reassembly = NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 233 | |
| 234 | err = -ENOMEM; |
| 235 | goto exit; |
| 236 | } |
| 237 | |
| 238 | /* second, combine the two fragments */ |
| 239 | memcpy(skb_push(skb, reassembly_len), |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 240 | ndev->rx_data_reassembly->data, |
| 241 | reassembly_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 242 | |
| 243 | /* third, free old reassembly */ |
| 244 | kfree_skb(ndev->rx_data_reassembly); |
H Hartley Sweeten | 040487f | 2012-05-07 12:31:24 +0200 | [diff] [blame] | 245 | ndev->rx_data_reassembly = NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | if (pbf == NCI_PBF_CONT) { |
| 249 | /* need to wait for next fragment, store skb and exit */ |
| 250 | ndev->rx_data_reassembly = skb; |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | exit: |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 255 | if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) { |
Julien Lefrique | 122c195 | 2014-10-21 16:52:49 +0200 | [diff] [blame] | 256 | /* Data received in Target mode, forward to nfc core */ |
| 257 | err = nfc_tm_data_received(ndev->nfc_dev, skb); |
| 258 | if (err) |
| 259 | pr_err("unable to handle received data\n"); |
| 260 | } else { |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 261 | nci_data_exchange_complete(ndev, skb, conn_id, err); |
Julien Lefrique | 122c195 | 2014-10-21 16:52:49 +0200 | [diff] [blame] | 262 | } |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* Rx Data packet */ |
| 266 | void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb) |
| 267 | { |
| 268 | __u8 pbf = nci_pbf(skb->data); |
Christophe Ricard | 98ff416 | 2014-12-02 21:27:47 +0100 | [diff] [blame] | 269 | __u8 status = 0; |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 270 | __u8 conn_id = nci_conn_id(skb->data); |
| 271 | struct nci_conn_info *conn_info; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 272 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 273 | pr_debug("len %d\n", skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 274 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 275 | pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n", |
| 276 | nci_pbf(skb->data), |
| 277 | nci_conn_id(skb->data), |
| 278 | nci_plen(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 279 | |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 280 | conn_info = nci_get_conn_info_by_conn_id(ndev, nci_conn_id(skb->data)); |
| 281 | if (!conn_info) |
| 282 | return; |
| 283 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 284 | /* strip the nci data header */ |
| 285 | skb_pull(skb, NCI_DATA_HDR_SIZE); |
| 286 | |
Vincent Cuissard | 83724c3 | 2014-07-22 19:48:40 +0200 | [diff] [blame] | 287 | if (ndev->target_active_prot == NFC_PROTO_MIFARE || |
| 288 | ndev->target_active_prot == NFC_PROTO_JEWEL || |
| 289 | ndev->target_active_prot == NFC_PROTO_FELICA || |
| 290 | ndev->target_active_prot == NFC_PROTO_ISO15693) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 291 | /* frame I/F => remove the status byte */ |
Vincent Cuissard | 83724c3 | 2014-07-22 19:48:40 +0200 | [diff] [blame] | 292 | pr_debug("frame I/F => remove the status byte\n"); |
Christophe Ricard | 98ff416 | 2014-12-02 21:27:47 +0100 | [diff] [blame] | 293 | status = skb->data[skb->len - 1]; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 294 | skb_trim(skb, (skb->len - 1)); |
| 295 | } |
| 296 | |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 297 | nci_add_rx_data_frag(ndev, skb, pbf, conn_id, nci_to_errno(status)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 298 | } |