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 | * Acknowledgements: |
| 10 | * This file is based on hci_event.c, which was written |
| 11 | * by Maxim Krasnyansky. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 |
| 15 | * as published by the Free Software Foundation |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | */ |
| 27 | |
Samuel Ortiz | 52858b5 | 2011-12-14 16:43:05 +0100 | [diff] [blame^] | 28 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 29 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 30 | #include <linux/types.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/bitops.h> |
| 33 | #include <linux/skbuff.h> |
| 34 | |
| 35 | #include "../nfc.h" |
| 36 | #include <net/nfc/nci.h> |
| 37 | #include <net/nfc/nci_core.h> |
| 38 | #include <linux/nfc.h> |
| 39 | |
| 40 | /* Handle NCI Notification packets */ |
| 41 | |
| 42 | static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev, |
| 43 | struct sk_buff *skb) |
| 44 | { |
| 45 | struct nci_core_conn_credit_ntf *ntf = (void *) skb->data; |
| 46 | int i; |
| 47 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 48 | pr_debug("num_entries %d\n", ntf->num_entries); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 49 | |
| 50 | if (ntf->num_entries > NCI_MAX_NUM_CONN) |
| 51 | ntf->num_entries = NCI_MAX_NUM_CONN; |
| 52 | |
| 53 | /* update the credits */ |
| 54 | for (i = 0; i < ntf->num_entries; i++) { |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 55 | pr_debug("entry[%d]: conn_id %d, credits %d\n", |
| 56 | i, ntf->conn_entries[i].conn_id, |
| 57 | ntf->conn_entries[i].credits); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 58 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 59 | if (ntf->conn_entries[i].conn_id == NCI_STATIC_RF_CONN_ID) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 60 | /* found static rf connection */ |
| 61 | atomic_add(ntf->conn_entries[i].credits, |
| 62 | &ndev->credits_cnt); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* trigger the next tx */ |
| 67 | if (!skb_queue_empty(&ndev->tx_q)) |
| 68 | queue_work(ndev->tx_wq, &ndev->tx_work); |
| 69 | } |
| 70 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 71 | static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev, |
| 72 | struct nci_rf_intf_activated_ntf *ntf, __u8 *data) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 73 | { |
| 74 | struct rf_tech_specific_params_nfca_poll *nfca_poll; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 75 | |
| 76 | nfca_poll = &ntf->rf_tech_specific_params.nfca_poll; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 77 | |
| 78 | nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data)); |
| 79 | data += 2; |
| 80 | |
| 81 | nfca_poll->nfcid1_len = *data++; |
| 82 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 83 | pr_debug("sens_res 0x%x, nfcid1_len %d\n", |
| 84 | nfca_poll->sens_res, nfca_poll->nfcid1_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 85 | |
| 86 | memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len); |
| 87 | data += nfca_poll->nfcid1_len; |
| 88 | |
| 89 | nfca_poll->sel_res_len = *data++; |
| 90 | |
| 91 | if (nfca_poll->sel_res_len != 0) |
| 92 | nfca_poll->sel_res = *data++; |
| 93 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 94 | pr_debug("sel_res_len %d, sel_res 0x%x\n", |
| 95 | nfca_poll->sel_res_len, |
| 96 | nfca_poll->sel_res); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 97 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 98 | return data; |
| 99 | } |
| 100 | |
| 101 | static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev, |
| 102 | struct nci_rf_intf_activated_ntf *ntf, __u8 *data) |
| 103 | { |
| 104 | struct activation_params_nfca_poll_iso_dep *nfca_poll; |
| 105 | |
| 106 | switch (ntf->activation_rf_tech_and_mode) { |
| 107 | case NCI_NFC_A_PASSIVE_POLL_MODE: |
| 108 | nfca_poll = &ntf->activation_params.nfca_poll_iso_dep; |
| 109 | nfca_poll->rats_res_len = *data++; |
| 110 | if (nfca_poll->rats_res_len > 0) { |
| 111 | memcpy(nfca_poll->rats_res, |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 112 | data, |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 113 | nfca_poll->rats_res_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 114 | } |
| 115 | break; |
| 116 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 117 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 118 | pr_err("unsupported activation_rf_tech_and_mode 0x%x\n", |
| 119 | ntf->activation_rf_tech_and_mode); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 120 | return -EPROTO; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static void nci_target_found(struct nci_dev *ndev, |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 127 | struct nci_rf_intf_activated_ntf *ntf) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 128 | { |
| 129 | struct nfc_target nfc_tgt; |
| 130 | |
| 131 | if (ntf->rf_protocol == NCI_RF_PROTOCOL_T2T) /* T2T MifareUL */ |
| 132 | nfc_tgt.supported_protocols = NFC_PROTO_MIFARE_MASK; |
| 133 | else if (ntf->rf_protocol == NCI_RF_PROTOCOL_ISO_DEP) /* 4A */ |
| 134 | nfc_tgt.supported_protocols = NFC_PROTO_ISO14443_MASK; |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 135 | else |
| 136 | nfc_tgt.supported_protocols = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 137 | |
| 138 | nfc_tgt.sens_res = ntf->rf_tech_specific_params.nfca_poll.sens_res; |
| 139 | nfc_tgt.sel_res = ntf->rf_tech_specific_params.nfca_poll.sel_res; |
| 140 | |
| 141 | if (!(nfc_tgt.supported_protocols & ndev->poll_prots)) { |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 142 | pr_debug("the target found does not have the desired protocol\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 146 | pr_debug("new target found, supported_protocols 0x%x\n", |
| 147 | nfc_tgt.supported_protocols); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 148 | |
| 149 | ndev->target_available_prots = nfc_tgt.supported_protocols; |
| 150 | |
| 151 | nfc_targets_found(ndev->nfc_dev, &nfc_tgt, 1); |
| 152 | } |
| 153 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 154 | static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev, |
| 155 | struct sk_buff *skb) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 156 | { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 157 | struct nci_rf_intf_activated_ntf ntf; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 158 | __u8 *data = skb->data; |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 159 | int err = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 160 | |
| 161 | clear_bit(NCI_DISCOVERY, &ndev->flags); |
| 162 | set_bit(NCI_POLL_ACTIVE, &ndev->flags); |
| 163 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 164 | ntf.rf_discovery_id = *data++; |
| 165 | ntf.rf_interface_type = *data++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 166 | ntf.rf_protocol = *data++; |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 167 | ntf.activation_rf_tech_and_mode = *data++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 168 | ntf.rf_tech_specific_params_len = *data++; |
| 169 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 170 | pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id); |
| 171 | pr_debug("rf_interface_type 0x%x\n", ntf.rf_interface_type); |
| 172 | pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol); |
| 173 | pr_debug("activation_rf_tech_and_mode 0x%x\n", |
| 174 | ntf.activation_rf_tech_and_mode); |
| 175 | pr_debug("rf_tech_specific_params_len %d\n", |
| 176 | ntf.rf_tech_specific_params_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 177 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 178 | if (ntf.rf_tech_specific_params_len > 0) { |
| 179 | switch (ntf.activation_rf_tech_and_mode) { |
| 180 | case NCI_NFC_A_PASSIVE_POLL_MODE: |
| 181 | data = nci_extract_rf_params_nfca_passive_poll(ndev, |
| 182 | &ntf, data); |
| 183 | break; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 184 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 185 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 186 | pr_err("unsupported activation_rf_tech_and_mode 0x%x\n", |
| 187 | ntf.activation_rf_tech_and_mode); |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 188 | return; |
| 189 | } |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 190 | } |
| 191 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 192 | ntf.data_exch_rf_tech_and_mode = *data++; |
| 193 | ntf.data_exch_tx_bit_rate = *data++; |
| 194 | ntf.data_exch_rx_bit_rate = *data++; |
| 195 | ntf.activation_params_len = *data++; |
| 196 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 197 | pr_debug("data_exch_rf_tech_and_mode 0x%x\n", |
| 198 | ntf.data_exch_rf_tech_and_mode); |
| 199 | pr_debug("data_exch_tx_bit_rate 0x%x\n", |
| 200 | ntf.data_exch_tx_bit_rate); |
| 201 | pr_debug("data_exch_rx_bit_rate 0x%x\n", |
| 202 | ntf.data_exch_rx_bit_rate); |
| 203 | pr_debug("activation_params_len %d\n", |
| 204 | ntf.activation_params_len); |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 205 | |
| 206 | if (ntf.activation_params_len > 0) { |
| 207 | switch (ntf.rf_interface_type) { |
| 208 | case NCI_RF_INTERFACE_ISO_DEP: |
| 209 | err = nci_extract_activation_params_iso_dep(ndev, |
| 210 | &ntf, data); |
| 211 | break; |
| 212 | |
| 213 | case NCI_RF_INTERFACE_FRAME: |
| 214 | /* no activation params */ |
| 215 | break; |
| 216 | |
| 217 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 218 | pr_err("unsupported rf_interface_type 0x%x\n", |
| 219 | ntf.rf_interface_type); |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (!err) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 225 | nci_target_found(ndev, &ntf); |
| 226 | } |
| 227 | |
| 228 | static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev, |
| 229 | struct sk_buff *skb) |
| 230 | { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 231 | struct nci_rf_deactivate_ntf *ntf = (void *) skb->data; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 232 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 233 | pr_debug("entry, type 0x%x, reason 0x%x\n", ntf->type, ntf->reason); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 234 | |
| 235 | clear_bit(NCI_POLL_ACTIVE, &ndev->flags); |
| 236 | ndev->target_active_prot = 0; |
| 237 | |
| 238 | /* drop tx data queue */ |
| 239 | skb_queue_purge(&ndev->tx_q); |
| 240 | |
| 241 | /* drop partial rx data packet */ |
| 242 | if (ndev->rx_data_reassembly) { |
| 243 | kfree_skb(ndev->rx_data_reassembly); |
| 244 | ndev->rx_data_reassembly = 0; |
| 245 | } |
| 246 | |
Ilan Elias | ee4c64f | 2011-11-09 12:09:15 +0200 | [diff] [blame] | 247 | /* set the available credits to initial value */ |
| 248 | atomic_set(&ndev->credits_cnt, ndev->initial_num_credits); |
| 249 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 250 | /* complete the data exchange transaction, if exists */ |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 251 | if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 252 | nci_data_exchange_complete(ndev, NULL, -EIO); |
| 253 | } |
| 254 | |
| 255 | void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb) |
| 256 | { |
| 257 | __u16 ntf_opcode = nci_opcode(skb->data); |
| 258 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 259 | pr_debug("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n", |
| 260 | nci_pbf(skb->data), |
| 261 | nci_opcode_gid(ntf_opcode), |
| 262 | nci_opcode_oid(ntf_opcode), |
| 263 | nci_plen(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 264 | |
| 265 | /* strip the nci control header */ |
| 266 | skb_pull(skb, NCI_CTRL_HDR_SIZE); |
| 267 | |
| 268 | switch (ntf_opcode) { |
| 269 | case NCI_OP_CORE_CONN_CREDITS_NTF: |
| 270 | nci_core_conn_credits_ntf_packet(ndev, skb); |
| 271 | break; |
| 272 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 273 | case NCI_OP_RF_INTF_ACTIVATED_NTF: |
| 274 | nci_rf_intf_activated_ntf_packet(ndev, skb); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 275 | break; |
| 276 | |
| 277 | case NCI_OP_RF_DEACTIVATE_NTF: |
| 278 | nci_rf_deactivate_ntf_packet(ndev, skb); |
| 279 | break; |
| 280 | |
| 281 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 282 | pr_err("unknown ntf opcode 0x%x\n", ntf_opcode); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 283 | break; |
| 284 | } |
| 285 | |
| 286 | kfree_skb(skb); |
| 287 | } |