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_core.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 |
Jeff Kirsher | 98b32de | 2013-12-06 08:56:16 -0800 | [diff] [blame] | 23 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 24 | * |
| 25 | */ |
| 26 | |
Samuel Ortiz | 52858b5 | 2011-12-14 16:43:05 +0100 | [diff] [blame] | 27 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 28 | |
Dave Jones | 8a70e7f | 2012-07-12 19:17:34 +0200 | [diff] [blame] | 29 | #include <linux/module.h> |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 30 | #include <linux/types.h> |
| 31 | #include <linux/workqueue.h> |
| 32 | #include <linux/completion.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 33 | #include <linux/export.h> |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 34 | #include <linux/sched.h> |
| 35 | #include <linux/bitops.h> |
| 36 | #include <linux/skbuff.h> |
| 37 | |
| 38 | #include "../nfc.h" |
| 39 | #include <net/nfc/nci.h> |
| 40 | #include <net/nfc/nci_core.h> |
| 41 | #include <linux/nfc.h> |
| 42 | |
| 43 | static void nci_cmd_work(struct work_struct *work); |
| 44 | static void nci_rx_work(struct work_struct *work); |
| 45 | static void nci_tx_work(struct work_struct *work); |
| 46 | |
| 47 | /* ---- NCI requests ---- */ |
| 48 | |
| 49 | void nci_req_complete(struct nci_dev *ndev, int result) |
| 50 | { |
| 51 | if (ndev->req_status == NCI_REQ_PEND) { |
| 52 | ndev->req_result = result; |
| 53 | ndev->req_status = NCI_REQ_DONE; |
| 54 | complete(&ndev->req_completion); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void nci_req_cancel(struct nci_dev *ndev, int err) |
| 59 | { |
| 60 | if (ndev->req_status == NCI_REQ_PEND) { |
| 61 | ndev->req_result = err; |
| 62 | ndev->req_status = NCI_REQ_CANCELED; |
| 63 | complete(&ndev->req_completion); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* Execute request and wait for completion. */ |
| 68 | static int __nci_request(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 69 | void (*req)(struct nci_dev *ndev, unsigned long opt), |
| 70 | unsigned long opt, __u32 timeout) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 71 | { |
| 72 | int rc = 0; |
Dan Carpenter | f8c141c | 2011-12-09 09:35:39 +0300 | [diff] [blame] | 73 | long completion_rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 74 | |
| 75 | ndev->req_status = NCI_REQ_PEND; |
| 76 | |
| 77 | init_completion(&ndev->req_completion); |
| 78 | req(ndev, opt); |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 79 | completion_rc = |
| 80 | wait_for_completion_interruptible_timeout(&ndev->req_completion, |
| 81 | timeout); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 82 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 83 | pr_debug("wait_for_completion return %ld\n", completion_rc); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 84 | |
| 85 | if (completion_rc > 0) { |
| 86 | switch (ndev->req_status) { |
| 87 | case NCI_REQ_DONE: |
| 88 | rc = nci_to_errno(ndev->req_result); |
| 89 | break; |
| 90 | |
| 91 | case NCI_REQ_CANCELED: |
| 92 | rc = -ndev->req_result; |
| 93 | break; |
| 94 | |
| 95 | default: |
| 96 | rc = -ETIMEDOUT; |
| 97 | break; |
| 98 | } |
| 99 | } else { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 100 | pr_err("wait_for_completion_interruptible_timeout failed %ld\n", |
| 101 | completion_rc); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 102 | |
| 103 | rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc)); |
| 104 | } |
| 105 | |
| 106 | ndev->req_status = ndev->req_result = 0; |
| 107 | |
| 108 | return rc; |
| 109 | } |
| 110 | |
| 111 | static inline int nci_request(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 112 | void (*req)(struct nci_dev *ndev, |
| 113 | unsigned long opt), |
| 114 | unsigned long opt, __u32 timeout) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 115 | { |
| 116 | int rc; |
| 117 | |
| 118 | if (!test_bit(NCI_UP, &ndev->flags)) |
| 119 | return -ENETDOWN; |
| 120 | |
| 121 | /* Serialize all requests */ |
| 122 | mutex_lock(&ndev->req_lock); |
| 123 | rc = __nci_request(ndev, req, opt, timeout); |
| 124 | mutex_unlock(&ndev->req_lock); |
| 125 | |
| 126 | return rc; |
| 127 | } |
| 128 | |
| 129 | static void nci_reset_req(struct nci_dev *ndev, unsigned long opt) |
| 130 | { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 131 | struct nci_core_reset_cmd cmd; |
| 132 | |
| 133 | cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG; |
| 134 | nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void nci_init_req(struct nci_dev *ndev, unsigned long opt) |
| 138 | { |
| 139 | nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL); |
| 140 | } |
| 141 | |
| 142 | static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt) |
| 143 | { |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 144 | struct nci_rf_disc_map_cmd cmd; |
| 145 | struct disc_map_config *cfg = cmd.mapping_configs; |
| 146 | __u8 *num = &cmd.num_mapping_configs; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 147 | int i; |
| 148 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 149 | /* set rf mapping configurations */ |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 150 | *num = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 151 | |
| 152 | /* by default mapping is set to NCI_RF_INTERFACE_FRAME */ |
| 153 | for (i = 0; i < ndev->num_supported_rf_interfaces; i++) { |
| 154 | if (ndev->supported_rf_interfaces[i] == |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 155 | NCI_RF_INTERFACE_ISO_DEP) { |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 156 | cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP; |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 157 | cfg[*num].mode = NCI_DISC_MAP_MODE_POLL | |
| 158 | NCI_DISC_MAP_MODE_LISTEN; |
| 159 | cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP; |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 160 | (*num)++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 161 | } else if (ndev->supported_rf_interfaces[i] == |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 162 | NCI_RF_INTERFACE_NFC_DEP) { |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 163 | cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP; |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 164 | cfg[*num].mode = NCI_DISC_MAP_MODE_POLL | |
| 165 | NCI_DISC_MAP_MODE_LISTEN; |
| 166 | cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP; |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 167 | (*num)++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 168 | } |
| 169 | |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 170 | if (*num == NCI_MAX_NUM_MAPPING_CONFIGS) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 171 | break; |
| 172 | } |
| 173 | |
| 174 | nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 175 | (1 + ((*num) * sizeof(struct disc_map_config))), &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 176 | } |
| 177 | |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 178 | struct nci_set_config_param { |
| 179 | __u8 id; |
| 180 | size_t len; |
| 181 | __u8 *val; |
| 182 | }; |
| 183 | |
| 184 | static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt) |
| 185 | { |
| 186 | struct nci_set_config_param *param = (struct nci_set_config_param *)opt; |
| 187 | struct nci_core_set_config_cmd cmd; |
| 188 | |
| 189 | BUG_ON(param->len > NCI_MAX_PARAM_LEN); |
| 190 | |
| 191 | cmd.num_params = 1; |
| 192 | cmd.param.id = param->id; |
| 193 | cmd.param.len = param->len; |
| 194 | memcpy(cmd.param.val, param->val, param->len); |
| 195 | |
| 196 | nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd); |
| 197 | } |
| 198 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 199 | static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt) |
| 200 | { |
| 201 | struct nci_rf_disc_cmd cmd; |
| 202 | __u32 protocols = opt; |
| 203 | |
| 204 | cmd.num_disc_configs = 0; |
| 205 | |
| 206 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && |
Szymon Janc | 874934f | 2012-10-04 15:15:51 +0200 | [diff] [blame] | 207 | (protocols & NFC_PROTO_JEWEL_MASK || |
| 208 | protocols & NFC_PROTO_MIFARE_MASK || |
| 209 | protocols & NFC_PROTO_ISO14443_MASK || |
| 210 | protocols & NFC_PROTO_NFC_DEP_MASK)) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 211 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 212 | NCI_NFC_A_PASSIVE_POLL_MODE; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 213 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 214 | cmd.num_disc_configs++; |
| 215 | } |
| 216 | |
| 217 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 218 | (protocols & NFC_PROTO_ISO14443_B_MASK)) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 219 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 220 | NCI_NFC_B_PASSIVE_POLL_MODE; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 221 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 222 | cmd.num_disc_configs++; |
| 223 | } |
| 224 | |
| 225 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && |
Szymon Janc | 874934f | 2012-10-04 15:15:51 +0200 | [diff] [blame] | 226 | (protocols & NFC_PROTO_FELICA_MASK || |
| 227 | protocols & NFC_PROTO_NFC_DEP_MASK)) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 228 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 229 | NCI_NFC_F_PASSIVE_POLL_MODE; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 230 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 231 | cmd.num_disc_configs++; |
| 232 | } |
| 233 | |
| 234 | nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 235 | (1 + (cmd.num_disc_configs * sizeof(struct disc_config))), |
| 236 | &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 237 | } |
| 238 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 239 | struct nci_rf_discover_select_param { |
| 240 | __u8 rf_discovery_id; |
| 241 | __u8 rf_protocol; |
| 242 | }; |
| 243 | |
| 244 | static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt) |
| 245 | { |
| 246 | struct nci_rf_discover_select_param *param = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 247 | (struct nci_rf_discover_select_param *)opt; |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 248 | struct nci_rf_discover_select_cmd cmd; |
| 249 | |
| 250 | cmd.rf_discovery_id = param->rf_discovery_id; |
| 251 | cmd.rf_protocol = param->rf_protocol; |
| 252 | |
| 253 | switch (cmd.rf_protocol) { |
| 254 | case NCI_RF_PROTOCOL_ISO_DEP: |
| 255 | cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP; |
| 256 | break; |
| 257 | |
| 258 | case NCI_RF_PROTOCOL_NFC_DEP: |
| 259 | cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP; |
| 260 | break; |
| 261 | |
| 262 | default: |
| 263 | cmd.rf_interface = NCI_RF_INTERFACE_FRAME; |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 268 | sizeof(struct nci_rf_discover_select_cmd), &cmd); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 271 | static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt) |
| 272 | { |
| 273 | struct nci_rf_deactivate_cmd cmd; |
| 274 | |
| 275 | cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE; |
| 276 | |
| 277 | nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 278 | sizeof(struct nci_rf_deactivate_cmd), &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static int nci_open_device(struct nci_dev *ndev) |
| 282 | { |
| 283 | int rc = 0; |
| 284 | |
| 285 | mutex_lock(&ndev->req_lock); |
| 286 | |
| 287 | if (test_bit(NCI_UP, &ndev->flags)) { |
| 288 | rc = -EALREADY; |
| 289 | goto done; |
| 290 | } |
| 291 | |
| 292 | if (ndev->ops->open(ndev)) { |
| 293 | rc = -EIO; |
| 294 | goto done; |
| 295 | } |
| 296 | |
| 297 | atomic_set(&ndev->cmd_cnt, 1); |
| 298 | |
| 299 | set_bit(NCI_INIT, &ndev->flags); |
| 300 | |
| 301 | rc = __nci_request(ndev, nci_reset_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 302 | msecs_to_jiffies(NCI_RESET_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 303 | |
| 304 | if (!rc) { |
| 305 | rc = __nci_request(ndev, nci_init_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 306 | msecs_to_jiffies(NCI_INIT_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (!rc) { |
| 310 | rc = __nci_request(ndev, nci_init_complete_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 311 | msecs_to_jiffies(NCI_INIT_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | clear_bit(NCI_INIT, &ndev->flags); |
| 315 | |
| 316 | if (!rc) { |
| 317 | set_bit(NCI_UP, &ndev->flags); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 318 | nci_clear_target_list(ndev); |
Ilan Elias | 8939e47 | 2012-01-18 13:16:12 +0200 | [diff] [blame] | 319 | atomic_set(&ndev->state, NCI_IDLE); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 320 | } else { |
| 321 | /* Init failed, cleanup */ |
| 322 | skb_queue_purge(&ndev->cmd_q); |
| 323 | skb_queue_purge(&ndev->rx_q); |
| 324 | skb_queue_purge(&ndev->tx_q); |
| 325 | |
| 326 | ndev->ops->close(ndev); |
| 327 | ndev->flags = 0; |
| 328 | } |
| 329 | |
| 330 | done: |
| 331 | mutex_unlock(&ndev->req_lock); |
| 332 | return rc; |
| 333 | } |
| 334 | |
| 335 | static int nci_close_device(struct nci_dev *ndev) |
| 336 | { |
| 337 | nci_req_cancel(ndev, ENODEV); |
| 338 | mutex_lock(&ndev->req_lock); |
| 339 | |
| 340 | if (!test_and_clear_bit(NCI_UP, &ndev->flags)) { |
| 341 | del_timer_sync(&ndev->cmd_timer); |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 342 | del_timer_sync(&ndev->data_timer); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 343 | mutex_unlock(&ndev->req_lock); |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* Drop RX and TX queues */ |
| 348 | skb_queue_purge(&ndev->rx_q); |
| 349 | skb_queue_purge(&ndev->tx_q); |
| 350 | |
| 351 | /* Flush RX and TX wq */ |
| 352 | flush_workqueue(ndev->rx_wq); |
| 353 | flush_workqueue(ndev->tx_wq); |
| 354 | |
| 355 | /* Reset device */ |
| 356 | skb_queue_purge(&ndev->cmd_q); |
| 357 | atomic_set(&ndev->cmd_cnt, 1); |
| 358 | |
| 359 | set_bit(NCI_INIT, &ndev->flags); |
| 360 | __nci_request(ndev, nci_reset_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 361 | msecs_to_jiffies(NCI_RESET_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 362 | clear_bit(NCI_INIT, &ndev->flags); |
| 363 | |
Amitkumar Karwar | fa9be5f | 2013-12-23 14:15:13 -0800 | [diff] [blame] | 364 | del_timer_sync(&ndev->cmd_timer); |
| 365 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 366 | /* Flush cmd wq */ |
| 367 | flush_workqueue(ndev->cmd_wq); |
| 368 | |
| 369 | /* After this point our queues are empty |
| 370 | * and no works are scheduled. */ |
| 371 | ndev->ops->close(ndev); |
| 372 | |
| 373 | /* Clear flags */ |
| 374 | ndev->flags = 0; |
| 375 | |
| 376 | mutex_unlock(&ndev->req_lock); |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /* NCI command timer function */ |
| 382 | static void nci_cmd_timer(unsigned long arg) |
| 383 | { |
| 384 | struct nci_dev *ndev = (void *) arg; |
| 385 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 386 | atomic_set(&ndev->cmd_cnt, 1); |
| 387 | queue_work(ndev->cmd_wq, &ndev->cmd_work); |
| 388 | } |
| 389 | |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 390 | /* NCI data exchange timer function */ |
| 391 | static void nci_data_timer(unsigned long arg) |
| 392 | { |
| 393 | struct nci_dev *ndev = (void *) arg; |
| 394 | |
| 395 | set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); |
| 396 | queue_work(ndev->rx_wq, &ndev->rx_work); |
| 397 | } |
| 398 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 399 | static int nci_dev_up(struct nfc_dev *nfc_dev) |
| 400 | { |
| 401 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 402 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 403 | return nci_open_device(ndev); |
| 404 | } |
| 405 | |
| 406 | static int nci_dev_down(struct nfc_dev *nfc_dev) |
| 407 | { |
| 408 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 409 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 410 | return nci_close_device(ndev); |
| 411 | } |
| 412 | |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 413 | static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev) |
| 414 | { |
| 415 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 416 | struct nci_set_config_param param; |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 417 | |
| 418 | param.val = nfc_get_local_general_bytes(nfc_dev, ¶m.len); |
| 419 | if ((param.val == NULL) || (param.len == 0)) |
Szymon Janc | f9fc36f | 2012-10-04 15:15:46 +0200 | [diff] [blame] | 420 | return 0; |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 421 | |
Szymon Janc | 460d8f9 | 2012-10-04 15:15:45 +0200 | [diff] [blame] | 422 | if (param.len > NFC_MAX_GT_LEN) |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 423 | return -EINVAL; |
| 424 | |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 425 | param.id = NCI_PN_ATR_REQ_GEN_BYTES; |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 426 | |
Szymon Janc | f9fc36f | 2012-10-04 15:15:46 +0200 | [diff] [blame] | 427 | return nci_request(ndev, nci_set_config_req, (unsigned long)¶m, |
| 428 | msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT)); |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 429 | } |
| 430 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 431 | static int nci_start_poll(struct nfc_dev *nfc_dev, |
| 432 | __u32 im_protocols, __u32 tm_protocols) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 433 | { |
| 434 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 435 | int rc; |
| 436 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 437 | if ((atomic_read(&ndev->state) == NCI_DISCOVERY) || |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 438 | (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 439 | pr_err("unable to start poll, since poll is already active\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 440 | return -EBUSY; |
| 441 | } |
| 442 | |
Ilan Elias | de05479 | 2011-09-22 11:13:01 +0300 | [diff] [blame] | 443 | if (ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 444 | pr_err("there is an active target\n"); |
Ilan Elias | de05479 | 2011-09-22 11:13:01 +0300 | [diff] [blame] | 445 | return -EBUSY; |
| 446 | } |
| 447 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 448 | if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) || |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 449 | (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) { |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 450 | pr_debug("target active or w4 select, implicitly deactivate\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 451 | |
| 452 | rc = nci_request(ndev, nci_rf_deactivate_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 453 | msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 454 | if (rc) |
| 455 | return -EBUSY; |
| 456 | } |
| 457 | |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 458 | if (im_protocols & NFC_PROTO_NFC_DEP_MASK) { |
| 459 | rc = nci_set_local_general_bytes(nfc_dev); |
| 460 | if (rc) { |
| 461 | pr_err("failed to set local general bytes\n"); |
| 462 | return rc; |
| 463 | } |
| 464 | } |
| 465 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 466 | rc = nci_request(ndev, nci_rf_discover_req, im_protocols, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 467 | msecs_to_jiffies(NCI_RF_DISC_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 468 | |
| 469 | if (!rc) |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 470 | ndev->poll_prots = im_protocols; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 471 | |
| 472 | return rc; |
| 473 | } |
| 474 | |
| 475 | static void nci_stop_poll(struct nfc_dev *nfc_dev) |
| 476 | { |
| 477 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 478 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 479 | if ((atomic_read(&ndev->state) != NCI_DISCOVERY) && |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 480 | (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 481 | pr_err("unable to stop poll, since poll is not active\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 482 | return; |
| 483 | } |
| 484 | |
| 485 | nci_request(ndev, nci_rf_deactivate_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 486 | msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 487 | } |
| 488 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 489 | static int nci_activate_target(struct nfc_dev *nfc_dev, |
| 490 | struct nfc_target *target, __u32 protocol) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 491 | { |
| 492 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 493 | struct nci_rf_discover_select_param param; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 494 | struct nfc_target *nci_target = NULL; |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 495 | int i; |
| 496 | int rc = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 497 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 498 | pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 499 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 500 | if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) && |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 501 | (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 502 | pr_err("there is no available target to activate\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 503 | return -EINVAL; |
| 504 | } |
| 505 | |
| 506 | if (ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 507 | pr_err("there is already an active target\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 508 | return -EBUSY; |
| 509 | } |
| 510 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 511 | for (i = 0; i < ndev->n_targets; i++) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 512 | if (ndev->targets[i].idx == target->idx) { |
| 513 | nci_target = &ndev->targets[i]; |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 514 | break; |
| 515 | } |
| 516 | } |
| 517 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 518 | if (!nci_target) { |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 519 | pr_err("unable to find the selected target\n"); |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 523 | if (!(nci_target->supported_protocols & (1 << protocol))) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 524 | pr_err("target does not support the requested protocol 0x%x\n", |
| 525 | protocol); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 526 | return -EINVAL; |
| 527 | } |
| 528 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 529 | if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 530 | param.rf_discovery_id = nci_target->logical_idx; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 531 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 532 | if (protocol == NFC_PROTO_JEWEL) |
| 533 | param.rf_protocol = NCI_RF_PROTOCOL_T1T; |
| 534 | else if (protocol == NFC_PROTO_MIFARE) |
| 535 | param.rf_protocol = NCI_RF_PROTOCOL_T2T; |
| 536 | else if (protocol == NFC_PROTO_FELICA) |
| 537 | param.rf_protocol = NCI_RF_PROTOCOL_T3T; |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 538 | else if (protocol == NFC_PROTO_ISO14443 || |
| 539 | protocol == NFC_PROTO_ISO14443_B) |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 540 | param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP; |
| 541 | else |
| 542 | param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP; |
| 543 | |
| 544 | rc = nci_request(ndev, nci_rf_discover_select_req, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 545 | (unsigned long)¶m, |
| 546 | msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT)); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | if (!rc) |
| 550 | ndev->target_active_prot = protocol; |
| 551 | |
| 552 | return rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 553 | } |
| 554 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 555 | static void nci_deactivate_target(struct nfc_dev *nfc_dev, |
| 556 | struct nfc_target *target) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 557 | { |
| 558 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 559 | |
Ilan Elias | 767f19a | 2012-08-15 11:46:24 +0300 | [diff] [blame] | 560 | pr_debug("entry\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 561 | |
| 562 | if (!ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 563 | pr_err("unable to deactivate target, no active target\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 564 | return; |
| 565 | } |
| 566 | |
| 567 | ndev->target_active_prot = 0; |
| 568 | |
Ilan Elias | 8939e47 | 2012-01-18 13:16:12 +0200 | [diff] [blame] | 569 | if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 570 | nci_request(ndev, nci_rf_deactivate_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 571 | msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Ilan Elias | 767f19a | 2012-08-15 11:46:24 +0300 | [diff] [blame] | 575 | static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target, |
| 576 | __u8 comm_mode, __u8 *gb, size_t gb_len) |
| 577 | { |
| 578 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 579 | int rc; |
| 580 | |
| 581 | pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode); |
| 582 | |
| 583 | rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP); |
| 584 | if (rc) |
| 585 | return rc; |
| 586 | |
| 587 | rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb, |
| 588 | ndev->remote_gb_len); |
| 589 | if (!rc) |
| 590 | rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE, |
| 591 | NFC_RF_INITIATOR); |
| 592 | |
| 593 | return rc; |
| 594 | } |
| 595 | |
| 596 | static int nci_dep_link_down(struct nfc_dev *nfc_dev) |
| 597 | { |
| 598 | pr_debug("entry\n"); |
| 599 | |
| 600 | nci_deactivate_target(nfc_dev, NULL); |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 606 | static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target, |
| 607 | struct sk_buff *skb, |
| 608 | data_exchange_cb_t cb, void *cb_context) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 609 | { |
| 610 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 611 | int rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 612 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 613 | pr_debug("target_idx %d, len %d\n", target->idx, skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 614 | |
| 615 | if (!ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 616 | pr_err("unable to exchange data, no active target\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 617 | return -EINVAL; |
| 618 | } |
| 619 | |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 620 | if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags)) |
| 621 | return -EBUSY; |
| 622 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 623 | /* store cb and context to be used on receiving data */ |
| 624 | ndev->data_exchange_cb = cb; |
| 625 | ndev->data_exchange_cb_context = cb_context; |
| 626 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 627 | rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb); |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 628 | if (rc) |
| 629 | clear_bit(NCI_DATA_EXCHANGE, &ndev->flags); |
| 630 | |
| 631 | return rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 632 | } |
| 633 | |
Samuel Ortiz | 0a94630 | 2013-05-10 11:57:06 +0200 | [diff] [blame] | 634 | static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx) |
| 635 | { |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx) |
| 640 | { |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int nci_discover_se(struct nfc_dev *nfc_dev) |
| 645 | { |
| 646 | return 0; |
| 647 | } |
| 648 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 649 | static struct nfc_ops nci_nfc_ops = { |
| 650 | .dev_up = nci_dev_up, |
| 651 | .dev_down = nci_dev_down, |
| 652 | .start_poll = nci_start_poll, |
| 653 | .stop_poll = nci_stop_poll, |
Ilan Elias | 767f19a | 2012-08-15 11:46:24 +0300 | [diff] [blame] | 654 | .dep_link_up = nci_dep_link_up, |
| 655 | .dep_link_down = nci_dep_link_down, |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 656 | .activate_target = nci_activate_target, |
| 657 | .deactivate_target = nci_deactivate_target, |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 658 | .im_transceive = nci_transceive, |
Samuel Ortiz | 0a94630 | 2013-05-10 11:57:06 +0200 | [diff] [blame] | 659 | .enable_se = nci_enable_se, |
| 660 | .disable_se = nci_disable_se, |
| 661 | .discover_se = nci_discover_se, |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 662 | }; |
| 663 | |
| 664 | /* ---- Interface to NCI drivers ---- */ |
| 665 | |
| 666 | /** |
| 667 | * nci_allocate_device - allocate a new nci device |
| 668 | * |
| 669 | * @ops: device operations |
| 670 | * @supported_protocols: NFC protocols supported by the device |
| 671 | */ |
| 672 | struct nci_dev *nci_allocate_device(struct nci_ops *ops, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 673 | __u32 supported_protocols, |
| 674 | int tx_headroom, int tx_tailroom) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 675 | { |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 676 | struct nci_dev *ndev; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 677 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 678 | pr_debug("supported_protocols 0x%x\n", supported_protocols); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 679 | |
| 680 | if (!ops->open || !ops->close || !ops->send) |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 681 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 682 | |
| 683 | if (!supported_protocols) |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 684 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 685 | |
| 686 | ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL); |
| 687 | if (!ndev) |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 688 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 689 | |
| 690 | ndev->ops = ops; |
| 691 | ndev->tx_headroom = tx_headroom; |
| 692 | ndev->tx_tailroom = tx_tailroom; |
| 693 | |
| 694 | ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 695 | supported_protocols, |
| 696 | tx_headroom + NCI_DATA_HDR_SIZE, |
| 697 | tx_tailroom); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 698 | if (!ndev->nfc_dev) |
| 699 | goto free_exit; |
| 700 | |
| 701 | nfc_set_drvdata(ndev->nfc_dev, ndev); |
| 702 | |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 703 | return ndev; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 704 | |
| 705 | free_exit: |
| 706 | kfree(ndev); |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 707 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 708 | } |
| 709 | EXPORT_SYMBOL(nci_allocate_device); |
| 710 | |
| 711 | /** |
| 712 | * nci_free_device - deallocate nci device |
| 713 | * |
| 714 | * @ndev: The nci device to deallocate |
| 715 | */ |
| 716 | void nci_free_device(struct nci_dev *ndev) |
| 717 | { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 718 | nfc_free_device(ndev->nfc_dev); |
| 719 | kfree(ndev); |
| 720 | } |
| 721 | EXPORT_SYMBOL(nci_free_device); |
| 722 | |
| 723 | /** |
| 724 | * nci_register_device - register a nci device in the nfc subsystem |
| 725 | * |
| 726 | * @dev: The nci device to register |
| 727 | */ |
| 728 | int nci_register_device(struct nci_dev *ndev) |
| 729 | { |
| 730 | int rc; |
| 731 | struct device *dev = &ndev->nfc_dev->dev; |
| 732 | char name[32]; |
| 733 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 734 | rc = nfc_register_device(ndev->nfc_dev); |
| 735 | if (rc) |
| 736 | goto exit; |
| 737 | |
| 738 | ndev->flags = 0; |
| 739 | |
| 740 | INIT_WORK(&ndev->cmd_work, nci_cmd_work); |
| 741 | snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev)); |
| 742 | ndev->cmd_wq = create_singlethread_workqueue(name); |
| 743 | if (!ndev->cmd_wq) { |
| 744 | rc = -ENOMEM; |
| 745 | goto unreg_exit; |
| 746 | } |
| 747 | |
| 748 | INIT_WORK(&ndev->rx_work, nci_rx_work); |
| 749 | snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev)); |
| 750 | ndev->rx_wq = create_singlethread_workqueue(name); |
| 751 | if (!ndev->rx_wq) { |
| 752 | rc = -ENOMEM; |
| 753 | goto destroy_cmd_wq_exit; |
| 754 | } |
| 755 | |
| 756 | INIT_WORK(&ndev->tx_work, nci_tx_work); |
| 757 | snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev)); |
| 758 | ndev->tx_wq = create_singlethread_workqueue(name); |
| 759 | if (!ndev->tx_wq) { |
| 760 | rc = -ENOMEM; |
| 761 | goto destroy_rx_wq_exit; |
| 762 | } |
| 763 | |
| 764 | skb_queue_head_init(&ndev->cmd_q); |
| 765 | skb_queue_head_init(&ndev->rx_q); |
| 766 | skb_queue_head_init(&ndev->tx_q); |
| 767 | |
| 768 | setup_timer(&ndev->cmd_timer, nci_cmd_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 769 | (unsigned long) ndev); |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 770 | setup_timer(&ndev->data_timer, nci_data_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 771 | (unsigned long) ndev); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 772 | |
| 773 | mutex_init(&ndev->req_lock); |
| 774 | |
| 775 | goto exit; |
| 776 | |
| 777 | destroy_rx_wq_exit: |
| 778 | destroy_workqueue(ndev->rx_wq); |
| 779 | |
| 780 | destroy_cmd_wq_exit: |
| 781 | destroy_workqueue(ndev->cmd_wq); |
| 782 | |
| 783 | unreg_exit: |
| 784 | nfc_unregister_device(ndev->nfc_dev); |
| 785 | |
| 786 | exit: |
| 787 | return rc; |
| 788 | } |
| 789 | EXPORT_SYMBOL(nci_register_device); |
| 790 | |
| 791 | /** |
| 792 | * nci_unregister_device - unregister a nci device in the nfc subsystem |
| 793 | * |
| 794 | * @dev: The nci device to unregister |
| 795 | */ |
| 796 | void nci_unregister_device(struct nci_dev *ndev) |
| 797 | { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 798 | nci_close_device(ndev); |
| 799 | |
| 800 | destroy_workqueue(ndev->cmd_wq); |
| 801 | destroy_workqueue(ndev->rx_wq); |
| 802 | destroy_workqueue(ndev->tx_wq); |
| 803 | |
| 804 | nfc_unregister_device(ndev->nfc_dev); |
| 805 | } |
| 806 | EXPORT_SYMBOL(nci_unregister_device); |
| 807 | |
| 808 | /** |
| 809 | * nci_recv_frame - receive frame from NCI drivers |
| 810 | * |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 811 | * @ndev: The nci device |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 812 | * @skb: The sk_buff to receive |
| 813 | */ |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 814 | int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 815 | { |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 816 | pr_debug("len %d\n", skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 817 | |
Szymon Janc | 874934f | 2012-10-04 15:15:51 +0200 | [diff] [blame] | 818 | if (!ndev || (!test_bit(NCI_UP, &ndev->flags) && |
| 819 | !test_bit(NCI_INIT, &ndev->flags))) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 820 | kfree_skb(skb); |
| 821 | return -ENXIO; |
| 822 | } |
| 823 | |
| 824 | /* Queue frame for rx worker thread */ |
| 825 | skb_queue_tail(&ndev->rx_q, skb); |
| 826 | queue_work(ndev->rx_wq, &ndev->rx_work); |
| 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | EXPORT_SYMBOL(nci_recv_frame); |
| 831 | |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 832 | static int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 833 | { |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 834 | pr_debug("len %d\n", skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 835 | |
| 836 | if (!ndev) { |
| 837 | kfree_skb(skb); |
| 838 | return -ENODEV; |
| 839 | } |
| 840 | |
| 841 | /* Get rid of skb owner, prior to sending to the driver. */ |
| 842 | skb_orphan(skb); |
| 843 | |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 844 | return ndev->ops->send(ndev, skb); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | /* Send NCI command */ |
| 848 | int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload) |
| 849 | { |
| 850 | struct nci_ctrl_hdr *hdr; |
| 851 | struct sk_buff *skb; |
| 852 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 853 | pr_debug("opcode 0x%x, plen %d\n", opcode, plen); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 854 | |
| 855 | skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL); |
| 856 | if (!skb) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 857 | pr_err("no memory for command\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 858 | return -ENOMEM; |
| 859 | } |
| 860 | |
| 861 | hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE); |
| 862 | hdr->gid = nci_opcode_gid(opcode); |
| 863 | hdr->oid = nci_opcode_oid(opcode); |
| 864 | hdr->plen = plen; |
| 865 | |
| 866 | nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT); |
| 867 | nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST); |
| 868 | |
| 869 | if (plen) |
| 870 | memcpy(skb_put(skb, plen), payload, plen); |
| 871 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 872 | skb_queue_tail(&ndev->cmd_q, skb); |
| 873 | queue_work(ndev->cmd_wq, &ndev->cmd_work); |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | /* ---- NCI TX Data worker thread ---- */ |
| 879 | |
| 880 | static void nci_tx_work(struct work_struct *work) |
| 881 | { |
| 882 | struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work); |
| 883 | struct sk_buff *skb; |
| 884 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 885 | pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 886 | |
| 887 | /* Send queued tx data */ |
| 888 | while (atomic_read(&ndev->credits_cnt)) { |
| 889 | skb = skb_dequeue(&ndev->tx_q); |
| 890 | if (!skb) |
| 891 | return; |
| 892 | |
Ilan Elias | db98c82 | 2011-11-09 12:09:16 +0200 | [diff] [blame] | 893 | /* Check if data flow control is used */ |
| 894 | if (atomic_read(&ndev->credits_cnt) != |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 895 | NCI_DATA_FLOW_CONTROL_NOT_USED) |
Ilan Elias | db98c82 | 2011-11-09 12:09:16 +0200 | [diff] [blame] | 896 | atomic_dec(&ndev->credits_cnt); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 897 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 898 | pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n", |
| 899 | nci_pbf(skb->data), |
| 900 | nci_conn_id(skb->data), |
| 901 | nci_plen(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 902 | |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 903 | nci_send_frame(ndev, skb); |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 904 | |
| 905 | mod_timer(&ndev->data_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 906 | jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | /* ----- NCI RX worker thread (data & control) ----- */ |
| 911 | |
| 912 | static void nci_rx_work(struct work_struct *work) |
| 913 | { |
| 914 | struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work); |
| 915 | struct sk_buff *skb; |
| 916 | |
| 917 | while ((skb = skb_dequeue(&ndev->rx_q))) { |
| 918 | /* Process frame */ |
| 919 | switch (nci_mt(skb->data)) { |
| 920 | case NCI_MT_RSP_PKT: |
| 921 | nci_rsp_packet(ndev, skb); |
| 922 | break; |
| 923 | |
| 924 | case NCI_MT_NTF_PKT: |
| 925 | nci_ntf_packet(ndev, skb); |
| 926 | break; |
| 927 | |
| 928 | case NCI_MT_DATA_PKT: |
| 929 | nci_rx_data_packet(ndev, skb); |
| 930 | break; |
| 931 | |
| 932 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 933 | pr_err("unknown MT 0x%x\n", nci_mt(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 934 | kfree_skb(skb); |
| 935 | break; |
| 936 | } |
| 937 | } |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 938 | |
| 939 | /* check if a data exchange timout has occurred */ |
| 940 | if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) { |
| 941 | /* complete the data exchange transaction, if exists */ |
| 942 | if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) |
| 943 | nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT); |
| 944 | |
| 945 | clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); |
| 946 | } |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /* ----- NCI TX CMD worker thread ----- */ |
| 950 | |
| 951 | static void nci_cmd_work(struct work_struct *work) |
| 952 | { |
| 953 | struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work); |
| 954 | struct sk_buff *skb; |
| 955 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 956 | pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 957 | |
| 958 | /* Send queued command */ |
| 959 | if (atomic_read(&ndev->cmd_cnt)) { |
| 960 | skb = skb_dequeue(&ndev->cmd_q); |
| 961 | if (!skb) |
| 962 | return; |
| 963 | |
| 964 | atomic_dec(&ndev->cmd_cnt); |
| 965 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 966 | pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n", |
| 967 | nci_pbf(skb->data), |
| 968 | nci_opcode_gid(nci_opcode(skb->data)), |
| 969 | nci_opcode_oid(nci_opcode(skb->data)), |
| 970 | nci_plen(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 971 | |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 972 | nci_send_frame(ndev, skb); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 973 | |
| 974 | mod_timer(&ndev->cmd_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 975 | jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 976 | } |
| 977 | } |
Dave Jones | 8a70e7f | 2012-07-12 19:17:34 +0200 | [diff] [blame] | 978 | |
| 979 | MODULE_LICENSE("GPL"); |