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