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++) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 55 | ntf->conn_entries[i].conn_id = |
| 56 | nci_conn_id(&ntf->conn_entries[i].conn_id); |
| 57 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 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); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 61 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 62 | if (ntf->conn_entries[i].conn_id == NCI_STATIC_RF_CONN_ID) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 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 | |
Ilan Elias | 004161c | 2011-12-20 16:57:41 +0200 | [diff] [blame] | 74 | static void nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev, |
| 75 | struct sk_buff *skb) |
| 76 | { |
| 77 | struct nci_core_intf_error_ntf *ntf = (void *) skb->data; |
| 78 | |
| 79 | ntf->conn_id = nci_conn_id(&ntf->conn_id); |
| 80 | |
| 81 | pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id); |
| 82 | |
| 83 | /* complete the data exchange transaction, if exists */ |
| 84 | if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) |
| 85 | nci_data_exchange_complete(ndev, NULL, -EIO); |
| 86 | } |
| 87 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 88 | static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev, |
| 89 | struct nci_rf_intf_activated_ntf *ntf, __u8 *data) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 90 | { |
| 91 | struct rf_tech_specific_params_nfca_poll *nfca_poll; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 92 | |
| 93 | nfca_poll = &ntf->rf_tech_specific_params.nfca_poll; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 94 | |
| 95 | nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data)); |
| 96 | data += 2; |
| 97 | |
| 98 | nfca_poll->nfcid1_len = *data++; |
| 99 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 100 | pr_debug("sens_res 0x%x, nfcid1_len %d\n", |
| 101 | nfca_poll->sens_res, nfca_poll->nfcid1_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 102 | |
| 103 | memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len); |
| 104 | data += nfca_poll->nfcid1_len; |
| 105 | |
| 106 | nfca_poll->sel_res_len = *data++; |
| 107 | |
| 108 | if (nfca_poll->sel_res_len != 0) |
| 109 | nfca_poll->sel_res = *data++; |
| 110 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 111 | pr_debug("sel_res_len %d, sel_res 0x%x\n", |
| 112 | nfca_poll->sel_res_len, |
| 113 | nfca_poll->sel_res); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 114 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 115 | return data; |
| 116 | } |
| 117 | |
| 118 | static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev, |
| 119 | struct nci_rf_intf_activated_ntf *ntf, __u8 *data) |
| 120 | { |
| 121 | struct activation_params_nfca_poll_iso_dep *nfca_poll; |
| 122 | |
| 123 | switch (ntf->activation_rf_tech_and_mode) { |
| 124 | case NCI_NFC_A_PASSIVE_POLL_MODE: |
| 125 | nfca_poll = &ntf->activation_params.nfca_poll_iso_dep; |
| 126 | nfca_poll->rats_res_len = *data++; |
| 127 | if (nfca_poll->rats_res_len > 0) { |
| 128 | memcpy(nfca_poll->rats_res, |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 129 | data, |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 130 | nfca_poll->rats_res_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 131 | } |
| 132 | break; |
| 133 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 134 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 135 | pr_err("unsupported activation_rf_tech_and_mode 0x%x\n", |
| 136 | ntf->activation_rf_tech_and_mode); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 137 | return -EPROTO; |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static void nci_target_found(struct nci_dev *ndev, |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 144 | struct nci_rf_intf_activated_ntf *ntf) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 145 | { |
| 146 | struct nfc_target nfc_tgt; |
| 147 | |
| 148 | if (ntf->rf_protocol == NCI_RF_PROTOCOL_T2T) /* T2T MifareUL */ |
| 149 | nfc_tgt.supported_protocols = NFC_PROTO_MIFARE_MASK; |
| 150 | else if (ntf->rf_protocol == NCI_RF_PROTOCOL_ISO_DEP) /* 4A */ |
| 151 | nfc_tgt.supported_protocols = NFC_PROTO_ISO14443_MASK; |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 152 | else |
| 153 | nfc_tgt.supported_protocols = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 154 | |
| 155 | nfc_tgt.sens_res = ntf->rf_tech_specific_params.nfca_poll.sens_res; |
| 156 | nfc_tgt.sel_res = ntf->rf_tech_specific_params.nfca_poll.sel_res; |
Ilan Elias | 288e071 | 2011-12-22 11:51:54 +0200 | [diff] [blame] | 157 | nfc_tgt.nfcid1_len = ntf->rf_tech_specific_params.nfca_poll.nfcid1_len; |
| 158 | if (nfc_tgt.nfcid1_len > 0) { |
| 159 | memcpy(nfc_tgt.nfcid1, |
| 160 | ntf->rf_tech_specific_params.nfca_poll.nfcid1, |
| 161 | nfc_tgt.nfcid1_len); |
| 162 | } |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 163 | |
| 164 | if (!(nfc_tgt.supported_protocols & ndev->poll_prots)) { |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 165 | pr_debug("the target found does not have the desired protocol\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 169 | pr_debug("new target found, supported_protocols 0x%x\n", |
| 170 | nfc_tgt.supported_protocols); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 171 | |
| 172 | ndev->target_available_prots = nfc_tgt.supported_protocols; |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 173 | ndev->max_data_pkt_payload_size = ntf->max_data_pkt_payload_size; |
| 174 | ndev->initial_num_credits = ntf->initial_num_credits; |
| 175 | |
| 176 | /* set the available credits to initial value */ |
| 177 | atomic_set(&ndev->credits_cnt, ndev->initial_num_credits); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 178 | |
| 179 | nfc_targets_found(ndev->nfc_dev, &nfc_tgt, 1); |
| 180 | } |
| 181 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 182 | static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev, |
| 183 | struct sk_buff *skb) |
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 | struct nci_rf_intf_activated_ntf ntf; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 186 | __u8 *data = skb->data; |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 187 | int err = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 188 | |
| 189 | clear_bit(NCI_DISCOVERY, &ndev->flags); |
| 190 | set_bit(NCI_POLL_ACTIVE, &ndev->flags); |
| 191 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 192 | ntf.rf_discovery_id = *data++; |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 193 | ntf.rf_interface = *data++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 194 | ntf.rf_protocol = *data++; |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 195 | ntf.activation_rf_tech_and_mode = *data++; |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 196 | ntf.max_data_pkt_payload_size = *data++; |
| 197 | ntf.initial_num_credits = *data++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 198 | ntf.rf_tech_specific_params_len = *data++; |
| 199 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 200 | pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id); |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 201 | pr_debug("rf_interface 0x%x\n", ntf.rf_interface); |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 202 | pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol); |
| 203 | pr_debug("activation_rf_tech_and_mode 0x%x\n", |
| 204 | ntf.activation_rf_tech_and_mode); |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 205 | pr_debug("max_data_pkt_payload_size 0x%x\n", |
| 206 | ntf.max_data_pkt_payload_size); |
| 207 | pr_debug("initial_num_credits 0x%x\n", ntf.initial_num_credits); |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 208 | pr_debug("rf_tech_specific_params_len %d\n", |
| 209 | ntf.rf_tech_specific_params_len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 210 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 211 | if (ntf.rf_tech_specific_params_len > 0) { |
| 212 | switch (ntf.activation_rf_tech_and_mode) { |
| 213 | case NCI_NFC_A_PASSIVE_POLL_MODE: |
| 214 | data = nci_extract_rf_params_nfca_passive_poll(ndev, |
| 215 | &ntf, data); |
| 216 | break; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 217 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 218 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 219 | pr_err("unsupported activation_rf_tech_and_mode 0x%x\n", |
| 220 | ntf.activation_rf_tech_and_mode); |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 221 | return; |
| 222 | } |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 223 | } |
| 224 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 225 | ntf.data_exch_rf_tech_and_mode = *data++; |
| 226 | ntf.data_exch_tx_bit_rate = *data++; |
| 227 | ntf.data_exch_rx_bit_rate = *data++; |
| 228 | ntf.activation_params_len = *data++; |
| 229 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 230 | pr_debug("data_exch_rf_tech_and_mode 0x%x\n", |
| 231 | ntf.data_exch_rf_tech_and_mode); |
| 232 | pr_debug("data_exch_tx_bit_rate 0x%x\n", |
| 233 | ntf.data_exch_tx_bit_rate); |
| 234 | pr_debug("data_exch_rx_bit_rate 0x%x\n", |
| 235 | ntf.data_exch_rx_bit_rate); |
| 236 | pr_debug("activation_params_len %d\n", |
| 237 | ntf.activation_params_len); |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 238 | |
| 239 | if (ntf.activation_params_len > 0) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 240 | switch (ntf.rf_interface) { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 241 | case NCI_RF_INTERFACE_ISO_DEP: |
| 242 | err = nci_extract_activation_params_iso_dep(ndev, |
| 243 | &ntf, data); |
| 244 | break; |
| 245 | |
| 246 | case NCI_RF_INTERFACE_FRAME: |
| 247 | /* no activation params */ |
| 248 | break; |
| 249 | |
| 250 | default: |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 251 | pr_err("unsupported rf_interface 0x%x\n", |
| 252 | ntf.rf_interface); |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (!err) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 258 | nci_target_found(ndev, &ntf); |
| 259 | } |
| 260 | |
| 261 | static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev, |
| 262 | struct sk_buff *skb) |
| 263 | { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 264 | struct nci_rf_deactivate_ntf *ntf = (void *) skb->data; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 265 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 266 | 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] | 267 | |
| 268 | clear_bit(NCI_POLL_ACTIVE, &ndev->flags); |
| 269 | ndev->target_active_prot = 0; |
| 270 | |
| 271 | /* drop tx data queue */ |
| 272 | skb_queue_purge(&ndev->tx_q); |
| 273 | |
| 274 | /* drop partial rx data packet */ |
| 275 | if (ndev->rx_data_reassembly) { |
| 276 | kfree_skb(ndev->rx_data_reassembly); |
| 277 | ndev->rx_data_reassembly = 0; |
| 278 | } |
| 279 | |
| 280 | /* complete the data exchange transaction, if exists */ |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 281 | if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 282 | nci_data_exchange_complete(ndev, NULL, -EIO); |
| 283 | } |
| 284 | |
| 285 | void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb) |
| 286 | { |
| 287 | __u16 ntf_opcode = nci_opcode(skb->data); |
| 288 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 289 | pr_debug("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n", |
| 290 | nci_pbf(skb->data), |
| 291 | nci_opcode_gid(ntf_opcode), |
| 292 | nci_opcode_oid(ntf_opcode), |
| 293 | nci_plen(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 294 | |
| 295 | /* strip the nci control header */ |
| 296 | skb_pull(skb, NCI_CTRL_HDR_SIZE); |
| 297 | |
| 298 | switch (ntf_opcode) { |
| 299 | case NCI_OP_CORE_CONN_CREDITS_NTF: |
| 300 | nci_core_conn_credits_ntf_packet(ndev, skb); |
| 301 | break; |
| 302 | |
Ilan Elias | 004161c | 2011-12-20 16:57:41 +0200 | [diff] [blame] | 303 | case NCI_OP_CORE_INTF_ERROR_NTF: |
| 304 | nci_core_conn_intf_error_ntf_packet(ndev, skb); |
| 305 | break; |
| 306 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 307 | case NCI_OP_RF_INTF_ACTIVATED_NTF: |
| 308 | nci_rf_intf_activated_ntf_packet(ndev, skb); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 309 | break; |
| 310 | |
| 311 | case NCI_OP_RF_DEACTIVATE_NTF: |
| 312 | nci_rf_deactivate_ntf_packet(ndev, skb); |
| 313 | break; |
| 314 | |
| 315 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 316 | pr_err("unknown ntf opcode 0x%x\n", ntf_opcode); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | |
| 320 | kfree_skb(skb); |
| 321 | } |