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. |
| 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 | #include <linux/types.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/wait.h> |
| 27 | #include <linux/bitops.h> |
| 28 | #include <linux/skbuff.h> |
| 29 | |
| 30 | #include "../nfc.h" |
| 31 | #include <net/nfc/nci.h> |
| 32 | #include <net/nfc/nci_core.h> |
| 33 | #include <linux/nfc.h> |
| 34 | |
| 35 | /* Complete data exchange transaction and forward skb to nfc core */ |
| 36 | void nci_data_exchange_complete(struct nci_dev *ndev, |
| 37 | struct sk_buff *skb, |
| 38 | int err) |
| 39 | { |
| 40 | data_exchange_cb_t cb = ndev->data_exchange_cb; |
| 41 | void *cb_context = ndev->data_exchange_cb_context; |
| 42 | |
| 43 | nfc_dbg("entry, len %d, err %d", ((skb) ? (skb->len) : (0)), err); |
| 44 | |
| 45 | if (cb) { |
| 46 | ndev->data_exchange_cb = NULL; |
| 47 | ndev->data_exchange_cb_context = 0; |
| 48 | |
| 49 | /* forward skb to nfc core */ |
| 50 | cb(cb_context, skb, err); |
| 51 | } else if (skb) { |
| 52 | nfc_err("no rx callback, dropping rx data..."); |
| 53 | |
| 54 | /* no waiting callback, free skb */ |
| 55 | kfree_skb(skb); |
| 56 | } |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 57 | |
| 58 | clear_bit(NCI_DATA_EXCHANGE, &ndev->flags); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* ----------------- NCI TX Data ----------------- */ |
| 62 | |
| 63 | static inline void nci_push_data_hdr(struct nci_dev *ndev, |
| 64 | __u8 conn_id, |
| 65 | struct sk_buff *skb, |
| 66 | __u8 pbf) |
| 67 | { |
| 68 | struct nci_data_hdr *hdr; |
| 69 | int plen = skb->len; |
| 70 | |
| 71 | hdr = (struct nci_data_hdr *) skb_push(skb, NCI_DATA_HDR_SIZE); |
| 72 | hdr->conn_id = conn_id; |
| 73 | hdr->rfu = 0; |
| 74 | hdr->plen = plen; |
| 75 | |
| 76 | nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT); |
| 77 | nci_pbf_set((__u8 *)hdr, pbf); |
| 78 | |
| 79 | skb->dev = (void *) ndev; |
| 80 | } |
| 81 | |
| 82 | static int nci_queue_tx_data_frags(struct nci_dev *ndev, |
| 83 | __u8 conn_id, |
| 84 | struct sk_buff *skb) { |
| 85 | int total_len = skb->len; |
| 86 | unsigned char *data = skb->data; |
| 87 | unsigned long flags; |
| 88 | struct sk_buff_head frags_q; |
| 89 | struct sk_buff *skb_frag; |
| 90 | int frag_len; |
| 91 | int rc = 0; |
| 92 | |
| 93 | nfc_dbg("entry, conn_id 0x%x, total_len %d", conn_id, total_len); |
| 94 | |
| 95 | __skb_queue_head_init(&frags_q); |
| 96 | |
| 97 | while (total_len) { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 98 | frag_len = |
| 99 | min_t(int, total_len, ndev->max_data_pkt_payload_size); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 100 | |
| 101 | skb_frag = nci_skb_alloc(ndev, |
| 102 | (NCI_DATA_HDR_SIZE + frag_len), |
| 103 | GFP_KERNEL); |
| 104 | if (skb_frag == NULL) { |
| 105 | rc = -ENOMEM; |
| 106 | goto free_exit; |
| 107 | } |
| 108 | skb_reserve(skb_frag, NCI_DATA_HDR_SIZE); |
| 109 | |
| 110 | /* first, copy the data */ |
| 111 | memcpy(skb_put(skb_frag, frag_len), data, frag_len); |
| 112 | |
| 113 | /* second, set the header */ |
| 114 | nci_push_data_hdr(ndev, conn_id, skb_frag, |
| 115 | ((total_len == frag_len) ? (NCI_PBF_LAST) : (NCI_PBF_CONT))); |
| 116 | |
| 117 | __skb_queue_tail(&frags_q, skb_frag); |
| 118 | |
| 119 | data += frag_len; |
| 120 | total_len -= frag_len; |
| 121 | |
| 122 | nfc_dbg("frag_len %d, remaining total_len %d", |
| 123 | frag_len, total_len); |
| 124 | } |
| 125 | |
| 126 | /* queue all fragments atomically */ |
| 127 | spin_lock_irqsave(&ndev->tx_q.lock, flags); |
| 128 | |
| 129 | while ((skb_frag = __skb_dequeue(&frags_q)) != NULL) |
| 130 | __skb_queue_tail(&ndev->tx_q, skb_frag); |
| 131 | |
| 132 | spin_unlock_irqrestore(&ndev->tx_q.lock, flags); |
| 133 | |
| 134 | /* free the original skb */ |
| 135 | kfree_skb(skb); |
| 136 | |
| 137 | goto exit; |
| 138 | |
| 139 | free_exit: |
| 140 | while ((skb_frag = __skb_dequeue(&frags_q)) != NULL) |
| 141 | kfree_skb(skb_frag); |
| 142 | |
| 143 | exit: |
| 144 | return rc; |
| 145 | } |
| 146 | |
| 147 | /* Send NCI data */ |
| 148 | int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb) |
| 149 | { |
| 150 | int rc = 0; |
| 151 | |
| 152 | nfc_dbg("entry, conn_id 0x%x, plen %d", conn_id, skb->len); |
| 153 | |
| 154 | /* check if the packet need to be fragmented */ |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 155 | if (skb->len <= ndev->max_data_pkt_payload_size) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 156 | /* no need to fragment packet */ |
| 157 | nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST); |
| 158 | |
| 159 | skb_queue_tail(&ndev->tx_q, skb); |
| 160 | } else { |
| 161 | /* fragment packet and queue the fragments */ |
| 162 | rc = nci_queue_tx_data_frags(ndev, conn_id, skb); |
| 163 | if (rc) { |
| 164 | nfc_err("failed to fragment tx data packet"); |
| 165 | goto free_exit; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | queue_work(ndev->tx_wq, &ndev->tx_work); |
| 170 | |
| 171 | goto exit; |
| 172 | |
| 173 | free_exit: |
| 174 | kfree_skb(skb); |
| 175 | |
| 176 | exit: |
| 177 | return rc; |
| 178 | } |
| 179 | |
| 180 | /* ----------------- NCI RX Data ----------------- */ |
| 181 | |
| 182 | static void nci_add_rx_data_frag(struct nci_dev *ndev, |
| 183 | struct sk_buff *skb, |
| 184 | __u8 pbf) |
| 185 | { |
| 186 | int reassembly_len; |
| 187 | int err = 0; |
| 188 | |
| 189 | if (ndev->rx_data_reassembly) { |
| 190 | reassembly_len = ndev->rx_data_reassembly->len; |
| 191 | |
| 192 | /* first, make enough room for the already accumulated data */ |
| 193 | if (skb_cow_head(skb, reassembly_len)) { |
| 194 | nfc_err("error adding room for accumulated rx data"); |
| 195 | |
| 196 | kfree_skb(skb); |
| 197 | skb = 0; |
| 198 | |
| 199 | kfree_skb(ndev->rx_data_reassembly); |
| 200 | ndev->rx_data_reassembly = 0; |
| 201 | |
| 202 | err = -ENOMEM; |
| 203 | goto exit; |
| 204 | } |
| 205 | |
| 206 | /* second, combine the two fragments */ |
| 207 | memcpy(skb_push(skb, reassembly_len), |
| 208 | ndev->rx_data_reassembly->data, |
| 209 | reassembly_len); |
| 210 | |
| 211 | /* third, free old reassembly */ |
| 212 | kfree_skb(ndev->rx_data_reassembly); |
| 213 | ndev->rx_data_reassembly = 0; |
| 214 | } |
| 215 | |
| 216 | if (pbf == NCI_PBF_CONT) { |
| 217 | /* need to wait for next fragment, store skb and exit */ |
| 218 | ndev->rx_data_reassembly = skb; |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | exit: |
| 223 | nci_data_exchange_complete(ndev, skb, err); |
| 224 | } |
| 225 | |
| 226 | /* Rx Data packet */ |
| 227 | void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb) |
| 228 | { |
| 229 | __u8 pbf = nci_pbf(skb->data); |
| 230 | |
| 231 | nfc_dbg("entry, len %d", skb->len); |
| 232 | |
| 233 | nfc_dbg("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d", |
| 234 | nci_pbf(skb->data), |
| 235 | nci_conn_id(skb->data), |
| 236 | nci_plen(skb->data)); |
| 237 | |
| 238 | /* strip the nci data header */ |
| 239 | skb_pull(skb, NCI_DATA_HDR_SIZE); |
| 240 | |
| 241 | if (ndev->target_active_prot == NFC_PROTO_MIFARE) { |
| 242 | /* frame I/F => remove the status byte */ |
| 243 | nfc_dbg("NFC_PROTO_MIFARE => remove the status byte"); |
| 244 | skb_trim(skb, (skb->len - 1)); |
| 245 | } |
| 246 | |
| 247 | nci_add_rx_data_frag(ndev, skb, pbf); |
| 248 | } |