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 | |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/bitops.h> |
| 31 | #include <linux/skbuff.h> |
| 32 | |
| 33 | #include "../nfc.h" |
| 34 | #include <net/nfc/nci.h> |
| 35 | #include <net/nfc/nci_core.h> |
| 36 | #include <linux/nfc.h> |
| 37 | |
| 38 | /* Handle NCI Notification packets */ |
| 39 | |
| 40 | static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev, |
| 41 | struct sk_buff *skb) |
| 42 | { |
| 43 | struct nci_core_conn_credit_ntf *ntf = (void *) skb->data; |
| 44 | int i; |
| 45 | |
| 46 | nfc_dbg("entry, num_entries %d", ntf->num_entries); |
| 47 | |
| 48 | if (ntf->num_entries > NCI_MAX_NUM_CONN) |
| 49 | ntf->num_entries = NCI_MAX_NUM_CONN; |
| 50 | |
| 51 | /* update the credits */ |
| 52 | for (i = 0; i < ntf->num_entries; i++) { |
| 53 | nfc_dbg("entry[%d]: conn_id %d, credits %d", i, |
| 54 | ntf->conn_entries[i].conn_id, |
| 55 | ntf->conn_entries[i].credits); |
| 56 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 57 | if (ntf->conn_entries[i].conn_id == NCI_STATIC_RF_CONN_ID) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 58 | /* found static rf connection */ |
| 59 | atomic_add(ntf->conn_entries[i].credits, |
| 60 | &ndev->credits_cnt); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /* trigger the next tx */ |
| 65 | if (!skb_queue_empty(&ndev->tx_q)) |
| 66 | queue_work(ndev->tx_wq, &ndev->tx_work); |
| 67 | } |
| 68 | |
| 69 | static void nci_rf_field_info_ntf_packet(struct nci_dev *ndev, |
| 70 | struct sk_buff *skb) |
| 71 | { |
| 72 | struct nci_rf_field_info_ntf *ntf = (void *) skb->data; |
| 73 | |
| 74 | nfc_dbg("entry, rf_field_status %d", ntf->rf_field_status); |
| 75 | } |
| 76 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 77 | static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev, |
| 78 | struct nci_rf_intf_activated_ntf *ntf, __u8 *data) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 79 | { |
| 80 | struct rf_tech_specific_params_nfca_poll *nfca_poll; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 81 | |
| 82 | nfca_poll = &ntf->rf_tech_specific_params.nfca_poll; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 83 | |
| 84 | nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data)); |
| 85 | data += 2; |
| 86 | |
| 87 | nfca_poll->nfcid1_len = *data++; |
| 88 | |
| 89 | nfc_dbg("sens_res 0x%x, nfcid1_len %d", |
| 90 | nfca_poll->sens_res, |
| 91 | nfca_poll->nfcid1_len); |
| 92 | |
| 93 | memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len); |
| 94 | data += nfca_poll->nfcid1_len; |
| 95 | |
| 96 | nfca_poll->sel_res_len = *data++; |
| 97 | |
| 98 | if (nfca_poll->sel_res_len != 0) |
| 99 | nfca_poll->sel_res = *data++; |
| 100 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 101 | nfc_dbg("sel_res_len %d, sel_res 0x%x", |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 102 | nfca_poll->sel_res_len, |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 103 | nfca_poll->sel_res); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 104 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 105 | return data; |
| 106 | } |
| 107 | |
| 108 | static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev, |
| 109 | struct nci_rf_intf_activated_ntf *ntf, __u8 *data) |
| 110 | { |
| 111 | struct activation_params_nfca_poll_iso_dep *nfca_poll; |
| 112 | |
| 113 | switch (ntf->activation_rf_tech_and_mode) { |
| 114 | case NCI_NFC_A_PASSIVE_POLL_MODE: |
| 115 | nfca_poll = &ntf->activation_params.nfca_poll_iso_dep; |
| 116 | nfca_poll->rats_res_len = *data++; |
| 117 | if (nfca_poll->rats_res_len > 0) { |
| 118 | memcpy(nfca_poll->rats_res, |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 119 | data, |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 120 | nfca_poll->rats_res_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 121 | } |
| 122 | break; |
| 123 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 124 | default: |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 125 | nfc_err("unsupported activation_rf_tech_and_mode 0x%x", |
| 126 | ntf->activation_rf_tech_and_mode); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 127 | return -EPROTO; |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static void nci_target_found(struct nci_dev *ndev, |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 134 | struct nci_rf_intf_activated_ntf *ntf) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 135 | { |
| 136 | struct nfc_target nfc_tgt; |
| 137 | |
| 138 | if (ntf->rf_protocol == NCI_RF_PROTOCOL_T2T) /* T2T MifareUL */ |
| 139 | nfc_tgt.supported_protocols = NFC_PROTO_MIFARE_MASK; |
| 140 | else if (ntf->rf_protocol == NCI_RF_PROTOCOL_ISO_DEP) /* 4A */ |
| 141 | nfc_tgt.supported_protocols = NFC_PROTO_ISO14443_MASK; |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 142 | else |
| 143 | nfc_tgt.supported_protocols = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 144 | |
| 145 | nfc_tgt.sens_res = ntf->rf_tech_specific_params.nfca_poll.sens_res; |
| 146 | nfc_tgt.sel_res = ntf->rf_tech_specific_params.nfca_poll.sel_res; |
| 147 | |
| 148 | if (!(nfc_tgt.supported_protocols & ndev->poll_prots)) { |
| 149 | nfc_dbg("the target found does not have the desired protocol"); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | nfc_dbg("new target found, supported_protocols 0x%x", |
| 154 | nfc_tgt.supported_protocols); |
| 155 | |
| 156 | ndev->target_available_prots = nfc_tgt.supported_protocols; |
| 157 | |
| 158 | nfc_targets_found(ndev->nfc_dev, &nfc_tgt, 1); |
| 159 | } |
| 160 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 161 | static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev, |
| 162 | struct sk_buff *skb) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 163 | { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 164 | struct nci_rf_intf_activated_ntf ntf; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 165 | __u8 *data = skb->data; |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 166 | int err = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 167 | |
| 168 | clear_bit(NCI_DISCOVERY, &ndev->flags); |
| 169 | set_bit(NCI_POLL_ACTIVE, &ndev->flags); |
| 170 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 171 | ntf.rf_discovery_id = *data++; |
| 172 | ntf.rf_interface_type = *data++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 173 | ntf.rf_protocol = *data++; |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 174 | ntf.activation_rf_tech_and_mode = *data++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 175 | ntf.rf_tech_specific_params_len = *data++; |
| 176 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 177 | nfc_dbg("rf_discovery_id %d", ntf.rf_discovery_id); |
| 178 | nfc_dbg("rf_interface_type 0x%x", ntf.rf_interface_type); |
| 179 | nfc_dbg("rf_protocol 0x%x", ntf.rf_protocol); |
| 180 | nfc_dbg("activation_rf_tech_and_mode 0x%x", |
| 181 | ntf.activation_rf_tech_and_mode); |
| 182 | nfc_dbg("rf_tech_specific_params_len %d", |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 183 | ntf.rf_tech_specific_params_len); |
| 184 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 185 | if (ntf.rf_tech_specific_params_len > 0) { |
| 186 | switch (ntf.activation_rf_tech_and_mode) { |
| 187 | case NCI_NFC_A_PASSIVE_POLL_MODE: |
| 188 | data = nci_extract_rf_params_nfca_passive_poll(ndev, |
| 189 | &ntf, data); |
| 190 | break; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 191 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 192 | default: |
| 193 | nfc_err("unsupported activation_rf_tech_and_mode 0x%x", |
| 194 | ntf.activation_rf_tech_and_mode); |
| 195 | return; |
| 196 | } |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 197 | } |
| 198 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 199 | ntf.data_exch_rf_tech_and_mode = *data++; |
| 200 | ntf.data_exch_tx_bit_rate = *data++; |
| 201 | ntf.data_exch_rx_bit_rate = *data++; |
| 202 | ntf.activation_params_len = *data++; |
| 203 | |
| 204 | nfc_dbg("data_exch_rf_tech_and_mode 0x%x", |
| 205 | ntf.data_exch_rf_tech_and_mode); |
| 206 | nfc_dbg("data_exch_tx_bit_rate 0x%x", |
| 207 | ntf.data_exch_tx_bit_rate); |
| 208 | nfc_dbg("data_exch_rx_bit_rate 0x%x", |
| 209 | ntf.data_exch_rx_bit_rate); |
| 210 | nfc_dbg("activation_params_len %d", |
| 211 | ntf.activation_params_len); |
| 212 | |
| 213 | if (ntf.activation_params_len > 0) { |
| 214 | switch (ntf.rf_interface_type) { |
| 215 | case NCI_RF_INTERFACE_ISO_DEP: |
| 216 | err = nci_extract_activation_params_iso_dep(ndev, |
| 217 | &ntf, data); |
| 218 | break; |
| 219 | |
| 220 | case NCI_RF_INTERFACE_FRAME: |
| 221 | /* no activation params */ |
| 222 | break; |
| 223 | |
| 224 | default: |
| 225 | nfc_err("unsupported rf_interface_type 0x%x", |
| 226 | ntf.rf_interface_type); |
| 227 | return; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if (!err) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 232 | nci_target_found(ndev, &ntf); |
| 233 | } |
| 234 | |
| 235 | static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev, |
| 236 | struct sk_buff *skb) |
| 237 | { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 238 | struct nci_rf_deactivate_ntf *ntf = (void *) skb->data; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 239 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 240 | nfc_dbg("entry, type 0x%x, reason 0x%x", ntf->type, ntf->reason); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 241 | |
| 242 | clear_bit(NCI_POLL_ACTIVE, &ndev->flags); |
| 243 | ndev->target_active_prot = 0; |
| 244 | |
| 245 | /* drop tx data queue */ |
| 246 | skb_queue_purge(&ndev->tx_q); |
| 247 | |
| 248 | /* drop partial rx data packet */ |
| 249 | if (ndev->rx_data_reassembly) { |
| 250 | kfree_skb(ndev->rx_data_reassembly); |
| 251 | ndev->rx_data_reassembly = 0; |
| 252 | } |
| 253 | |
| 254 | /* complete the data exchange transaction, if exists */ |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 255 | if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 256 | nci_data_exchange_complete(ndev, NULL, -EIO); |
| 257 | } |
| 258 | |
| 259 | void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb) |
| 260 | { |
| 261 | __u16 ntf_opcode = nci_opcode(skb->data); |
| 262 | |
| 263 | nfc_dbg("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d", |
| 264 | nci_pbf(skb->data), |
| 265 | nci_opcode_gid(ntf_opcode), |
| 266 | nci_opcode_oid(ntf_opcode), |
| 267 | nci_plen(skb->data)); |
| 268 | |
| 269 | /* strip the nci control header */ |
| 270 | skb_pull(skb, NCI_CTRL_HDR_SIZE); |
| 271 | |
| 272 | switch (ntf_opcode) { |
| 273 | case NCI_OP_CORE_CONN_CREDITS_NTF: |
| 274 | nci_core_conn_credits_ntf_packet(ndev, skb); |
| 275 | break; |
| 276 | |
| 277 | case NCI_OP_RF_FIELD_INFO_NTF: |
| 278 | nci_rf_field_info_ntf_packet(ndev, skb); |
| 279 | break; |
| 280 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame^] | 281 | case NCI_OP_RF_INTF_ACTIVATED_NTF: |
| 282 | nci_rf_intf_activated_ntf_packet(ndev, skb); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 283 | break; |
| 284 | |
| 285 | case NCI_OP_RF_DEACTIVATE_NTF: |
| 286 | nci_rf_deactivate_ntf_packet(ndev, skb); |
| 287 | break; |
| 288 | |
| 289 | default: |
| 290 | nfc_err("unknown ntf opcode 0x%x", ntf_opcode); |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | kfree_skb(skb); |
| 295 | } |