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 57 if (ntf->conn_entries[i].conn_id == NCI_STATIC_RF_CONN_ID) { 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 __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev, 70 struct nci_rf_intf_activated_ntf *ntf, __u8 *data) 71 { 72 struct rf_tech_specific_params_nfca_poll *nfca_poll; 73 74 nfca_poll = &ntf->rf_tech_specific_params.nfca_poll; 75 76 nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data)); 77 data += 2; 78 79 nfca_poll->nfcid1_len = *data++; 80 81 nfc_dbg("sens_res 0x%x, nfcid1_len %d", 82 nfca_poll->sens_res, 83 nfca_poll->nfcid1_len); 84 85 memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len); 86 data += nfca_poll->nfcid1_len; 87 88 nfca_poll->sel_res_len = *data++; 89 90 if (nfca_poll->sel_res_len != 0) 91 nfca_poll->sel_res = *data++; 92 93 nfc_dbg("sel_res_len %d, sel_res 0x%x", 94 nfca_poll->sel_res_len, 95 nfca_poll->sel_res); 96 97 return data; 98 } 99 100 static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev, 101 struct nci_rf_intf_activated_ntf *ntf, __u8 *data) 102 { 103 struct activation_params_nfca_poll_iso_dep *nfca_poll; 104 105 switch (ntf->activation_rf_tech_and_mode) { 106 case NCI_NFC_A_PASSIVE_POLL_MODE: 107 nfca_poll = &ntf->activation_params.nfca_poll_iso_dep; 108 nfca_poll->rats_res_len = *data++; 109 if (nfca_poll->rats_res_len > 0) { 110 memcpy(nfca_poll->rats_res, 111 data, 112 nfca_poll->rats_res_len); 113 } 114 break; 115 116 default: 117 nfc_err("unsupported activation_rf_tech_and_mode 0x%x", 118 ntf->activation_rf_tech_and_mode); 119 return -EPROTO; 120 } 121 122 return 0; 123 } 124 125 static void nci_target_found(struct nci_dev *ndev, 126 struct nci_rf_intf_activated_ntf *ntf) 127 { 128 struct nfc_target nfc_tgt; 129 130 if (ntf->rf_protocol == NCI_RF_PROTOCOL_T2T) /* T2T MifareUL */ 131 nfc_tgt.supported_protocols = NFC_PROTO_MIFARE_MASK; 132 else if (ntf->rf_protocol == NCI_RF_PROTOCOL_ISO_DEP) /* 4A */ 133 nfc_tgt.supported_protocols = NFC_PROTO_ISO14443_MASK; 134 else 135 nfc_tgt.supported_protocols = 0; 136 137 nfc_tgt.sens_res = ntf->rf_tech_specific_params.nfca_poll.sens_res; 138 nfc_tgt.sel_res = ntf->rf_tech_specific_params.nfca_poll.sel_res; 139 140 if (!(nfc_tgt.supported_protocols & ndev->poll_prots)) { 141 nfc_dbg("the target found does not have the desired protocol"); 142 return; 143 } 144 145 nfc_dbg("new target found, supported_protocols 0x%x", 146 nfc_tgt.supported_protocols); 147 148 ndev->target_available_prots = nfc_tgt.supported_protocols; 149 150 nfc_targets_found(ndev->nfc_dev, &nfc_tgt, 1); 151 } 152 153 static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev, 154 struct sk_buff *skb) 155 { 156 struct nci_rf_intf_activated_ntf ntf; 157 __u8 *data = skb->data; 158 int err = 0; 159 160 clear_bit(NCI_DISCOVERY, &ndev->flags); 161 set_bit(NCI_POLL_ACTIVE, &ndev->flags); 162 163 ntf.rf_discovery_id = *data++; 164 ntf.rf_interface_type = *data++; 165 ntf.rf_protocol = *data++; 166 ntf.activation_rf_tech_and_mode = *data++; 167 ntf.rf_tech_specific_params_len = *data++; 168 169 nfc_dbg("rf_discovery_id %d", ntf.rf_discovery_id); 170 nfc_dbg("rf_interface_type 0x%x", ntf.rf_interface_type); 171 nfc_dbg("rf_protocol 0x%x", ntf.rf_protocol); 172 nfc_dbg("activation_rf_tech_and_mode 0x%x", 173 ntf.activation_rf_tech_and_mode); 174 nfc_dbg("rf_tech_specific_params_len %d", 175 ntf.rf_tech_specific_params_len); 176 177 if (ntf.rf_tech_specific_params_len > 0) { 178 switch (ntf.activation_rf_tech_and_mode) { 179 case NCI_NFC_A_PASSIVE_POLL_MODE: 180 data = nci_extract_rf_params_nfca_passive_poll(ndev, 181 &ntf, data); 182 break; 183 184 default: 185 nfc_err("unsupported activation_rf_tech_and_mode 0x%x", 186 ntf.activation_rf_tech_and_mode); 187 return; 188 } 189 } 190 191 ntf.data_exch_rf_tech_and_mode = *data++; 192 ntf.data_exch_tx_bit_rate = *data++; 193 ntf.data_exch_rx_bit_rate = *data++; 194 ntf.activation_params_len = *data++; 195 196 nfc_dbg("data_exch_rf_tech_and_mode 0x%x", 197 ntf.data_exch_rf_tech_and_mode); 198 nfc_dbg("data_exch_tx_bit_rate 0x%x", 199 ntf.data_exch_tx_bit_rate); 200 nfc_dbg("data_exch_rx_bit_rate 0x%x", 201 ntf.data_exch_rx_bit_rate); 202 nfc_dbg("activation_params_len %d", 203 ntf.activation_params_len); 204 205 if (ntf.activation_params_len > 0) { 206 switch (ntf.rf_interface_type) { 207 case NCI_RF_INTERFACE_ISO_DEP: 208 err = nci_extract_activation_params_iso_dep(ndev, 209 &ntf, data); 210 break; 211 212 case NCI_RF_INTERFACE_FRAME: 213 /* no activation params */ 214 break; 215 216 default: 217 nfc_err("unsupported rf_interface_type 0x%x", 218 ntf.rf_interface_type); 219 return; 220 } 221 } 222 223 if (!err) 224 nci_target_found(ndev, &ntf); 225 } 226 227 static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev, 228 struct sk_buff *skb) 229 { 230 struct nci_rf_deactivate_ntf *ntf = (void *) skb->data; 231 232 nfc_dbg("entry, type 0x%x, reason 0x%x", ntf->type, ntf->reason); 233 234 clear_bit(NCI_POLL_ACTIVE, &ndev->flags); 235 ndev->target_active_prot = 0; 236 237 /* drop tx data queue */ 238 skb_queue_purge(&ndev->tx_q); 239 240 /* drop partial rx data packet */ 241 if (ndev->rx_data_reassembly) { 242 kfree_skb(ndev->rx_data_reassembly); 243 ndev->rx_data_reassembly = 0; 244 } 245 246 /* set the available credits to initial value */ 247 atomic_set(&ndev->credits_cnt, ndev->initial_num_credits); 248 249 /* complete the data exchange transaction, if exists */ 250 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) 251 nci_data_exchange_complete(ndev, NULL, -EIO); 252 } 253 254 void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb) 255 { 256 __u16 ntf_opcode = nci_opcode(skb->data); 257 258 nfc_dbg("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d", 259 nci_pbf(skb->data), 260 nci_opcode_gid(ntf_opcode), 261 nci_opcode_oid(ntf_opcode), 262 nci_plen(skb->data)); 263 264 /* strip the nci control header */ 265 skb_pull(skb, NCI_CTRL_HDR_SIZE); 266 267 switch (ntf_opcode) { 268 case NCI_OP_CORE_CONN_CREDITS_NTF: 269 nci_core_conn_credits_ntf_packet(ndev, skb); 270 break; 271 272 case NCI_OP_RF_INTF_ACTIVATED_NTF: 273 nci_rf_intf_activated_ntf_packet(ndev, skb); 274 break; 275 276 case NCI_OP_RF_DEACTIVATE_NTF: 277 nci_rf_deactivate_ntf_packet(ndev, skb); 278 break; 279 280 default: 281 nfc_err("unknown ntf opcode 0x%x", ntf_opcode); 282 break; 283 } 284 285 kfree_skb(skb); 286 } 287