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. |
Julien Lefrique | 772dccf | 2014-10-21 16:52:44 +0200 | [diff] [blame] | 6 | * Copyright (C) 2014 Marvell International Ltd. |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 7 | * |
| 8 | * Written by Ilan Elias <ilane@ti.com> |
| 9 | * |
| 10 | * Acknowledgements: |
| 11 | * This file is based on hci_core.c, which was written |
| 12 | * by Maxim Krasnyansky. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 |
| 16 | * as published by the Free Software Foundation |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
Jeff Kirsher | 98b32de | 2013-12-06 08:56:16 -0800 | [diff] [blame] | 24 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 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 | |
Dave Jones | 8a70e7f | 2012-07-12 19:17:34 +0200 | [diff] [blame] | 30 | #include <linux/module.h> |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 31 | #include <linux/types.h> |
| 32 | #include <linux/workqueue.h> |
| 33 | #include <linux/completion.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 34 | #include <linux/export.h> |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 35 | #include <linux/sched.h> |
| 36 | #include <linux/bitops.h> |
| 37 | #include <linux/skbuff.h> |
| 38 | |
| 39 | #include "../nfc.h" |
| 40 | #include <net/nfc/nci.h> |
| 41 | #include <net/nfc/nci_core.h> |
| 42 | #include <linux/nfc.h> |
| 43 | |
Christophe Ricard | b16ae71 | 2015-02-03 19:48:05 +0100 | [diff] [blame] | 44 | struct core_conn_create_data { |
| 45 | int length; |
| 46 | struct nci_core_conn_create_cmd *cmd; |
| 47 | }; |
| 48 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 49 | static void nci_cmd_work(struct work_struct *work); |
| 50 | static void nci_rx_work(struct work_struct *work); |
| 51 | static void nci_tx_work(struct work_struct *work); |
| 52 | |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 53 | struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev, |
| 54 | int conn_id) |
| 55 | { |
| 56 | struct nci_conn_info *conn_info; |
| 57 | |
| 58 | list_for_each_entry(conn_info, &ndev->conn_info_list, list) { |
| 59 | if (conn_info->conn_id == conn_id) |
| 60 | return conn_info; |
| 61 | } |
| 62 | |
| 63 | return NULL; |
| 64 | } |
| 65 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 66 | /* ---- NCI requests ---- */ |
| 67 | |
| 68 | void nci_req_complete(struct nci_dev *ndev, int result) |
| 69 | { |
| 70 | if (ndev->req_status == NCI_REQ_PEND) { |
| 71 | ndev->req_result = result; |
| 72 | ndev->req_status = NCI_REQ_DONE; |
| 73 | complete(&ndev->req_completion); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static void nci_req_cancel(struct nci_dev *ndev, int err) |
| 78 | { |
| 79 | if (ndev->req_status == NCI_REQ_PEND) { |
| 80 | ndev->req_result = err; |
| 81 | ndev->req_status = NCI_REQ_CANCELED; |
| 82 | complete(&ndev->req_completion); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* Execute request and wait for completion. */ |
| 87 | static int __nci_request(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 88 | void (*req)(struct nci_dev *ndev, unsigned long opt), |
| 89 | unsigned long opt, __u32 timeout) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 90 | { |
| 91 | int rc = 0; |
Dan Carpenter | f8c141c | 2011-12-09 09:35:39 +0300 | [diff] [blame] | 92 | long completion_rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 93 | |
| 94 | ndev->req_status = NCI_REQ_PEND; |
| 95 | |
Axel Lin | 9bec44b | 2014-02-13 13:25:48 +0800 | [diff] [blame] | 96 | reinit_completion(&ndev->req_completion); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 97 | req(ndev, opt); |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 98 | completion_rc = |
| 99 | wait_for_completion_interruptible_timeout(&ndev->req_completion, |
| 100 | timeout); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 101 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 102 | pr_debug("wait_for_completion return %ld\n", completion_rc); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 103 | |
| 104 | if (completion_rc > 0) { |
| 105 | switch (ndev->req_status) { |
| 106 | case NCI_REQ_DONE: |
| 107 | rc = nci_to_errno(ndev->req_result); |
| 108 | break; |
| 109 | |
| 110 | case NCI_REQ_CANCELED: |
| 111 | rc = -ndev->req_result; |
| 112 | break; |
| 113 | |
| 114 | default: |
| 115 | rc = -ETIMEDOUT; |
| 116 | break; |
| 117 | } |
| 118 | } else { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 119 | pr_err("wait_for_completion_interruptible_timeout failed %ld\n", |
| 120 | completion_rc); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 121 | |
| 122 | rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc)); |
| 123 | } |
| 124 | |
| 125 | ndev->req_status = ndev->req_result = 0; |
| 126 | |
| 127 | return rc; |
| 128 | } |
| 129 | |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 130 | inline int nci_request(struct nci_dev *ndev, |
| 131 | void (*req)(struct nci_dev *ndev, |
| 132 | unsigned long opt), |
| 133 | unsigned long opt, __u32 timeout) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 134 | { |
| 135 | int rc; |
| 136 | |
| 137 | if (!test_bit(NCI_UP, &ndev->flags)) |
| 138 | return -ENETDOWN; |
| 139 | |
| 140 | /* Serialize all requests */ |
| 141 | mutex_lock(&ndev->req_lock); |
| 142 | rc = __nci_request(ndev, req, opt, timeout); |
| 143 | mutex_unlock(&ndev->req_lock); |
| 144 | |
| 145 | return rc; |
| 146 | } |
| 147 | |
| 148 | static void nci_reset_req(struct nci_dev *ndev, unsigned long opt) |
| 149 | { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 150 | struct nci_core_reset_cmd cmd; |
| 151 | |
| 152 | cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG; |
| 153 | nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void nci_init_req(struct nci_dev *ndev, unsigned long opt) |
| 157 | { |
| 158 | nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL); |
| 159 | } |
| 160 | |
| 161 | static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt) |
| 162 | { |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 163 | struct nci_rf_disc_map_cmd cmd; |
| 164 | struct disc_map_config *cfg = cmd.mapping_configs; |
| 165 | __u8 *num = &cmd.num_mapping_configs; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 166 | int i; |
| 167 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 168 | /* set rf mapping configurations */ |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 169 | *num = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 170 | |
| 171 | /* by default mapping is set to NCI_RF_INTERFACE_FRAME */ |
| 172 | for (i = 0; i < ndev->num_supported_rf_interfaces; i++) { |
| 173 | if (ndev->supported_rf_interfaces[i] == |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 174 | NCI_RF_INTERFACE_ISO_DEP) { |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 175 | cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP; |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 176 | cfg[*num].mode = NCI_DISC_MAP_MODE_POLL | |
| 177 | NCI_DISC_MAP_MODE_LISTEN; |
| 178 | cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP; |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 179 | (*num)++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 180 | } else if (ndev->supported_rf_interfaces[i] == |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 181 | NCI_RF_INTERFACE_NFC_DEP) { |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 182 | cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP; |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 183 | cfg[*num].mode = NCI_DISC_MAP_MODE_POLL | |
| 184 | NCI_DISC_MAP_MODE_LISTEN; |
| 185 | cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP; |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 186 | (*num)++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 187 | } |
| 188 | |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 189 | if (*num == NCI_MAX_NUM_MAPPING_CONFIGS) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | |
| 193 | nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 194 | (1 + ((*num) * sizeof(struct disc_map_config))), &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 195 | } |
| 196 | |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 197 | struct nci_set_config_param { |
| 198 | __u8 id; |
| 199 | size_t len; |
| 200 | __u8 *val; |
| 201 | }; |
| 202 | |
| 203 | static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt) |
| 204 | { |
| 205 | struct nci_set_config_param *param = (struct nci_set_config_param *)opt; |
| 206 | struct nci_core_set_config_cmd cmd; |
| 207 | |
| 208 | BUG_ON(param->len > NCI_MAX_PARAM_LEN); |
| 209 | |
| 210 | cmd.num_params = 1; |
| 211 | cmd.param.id = param->id; |
| 212 | cmd.param.len = param->len; |
| 213 | memcpy(cmd.param.val, param->val, param->len); |
| 214 | |
| 215 | nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd); |
| 216 | } |
| 217 | |
Julien Lefrique | 772dccf | 2014-10-21 16:52:44 +0200 | [diff] [blame] | 218 | struct nci_rf_discover_param { |
| 219 | __u32 im_protocols; |
| 220 | __u32 tm_protocols; |
| 221 | }; |
| 222 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 223 | static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt) |
| 224 | { |
Julien Lefrique | 772dccf | 2014-10-21 16:52:44 +0200 | [diff] [blame] | 225 | struct nci_rf_discover_param *param = |
| 226 | (struct nci_rf_discover_param *)opt; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 227 | struct nci_rf_disc_cmd cmd; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 228 | |
| 229 | cmd.num_disc_configs = 0; |
| 230 | |
| 231 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && |
Julien Lefrique | 772dccf | 2014-10-21 16:52:44 +0200 | [diff] [blame] | 232 | (param->im_protocols & NFC_PROTO_JEWEL_MASK || |
| 233 | param->im_protocols & NFC_PROTO_MIFARE_MASK || |
| 234 | param->im_protocols & NFC_PROTO_ISO14443_MASK || |
| 235 | param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 236 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 237 | NCI_NFC_A_PASSIVE_POLL_MODE; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 238 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 239 | cmd.num_disc_configs++; |
| 240 | } |
| 241 | |
| 242 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && |
Julien Lefrique | 772dccf | 2014-10-21 16:52:44 +0200 | [diff] [blame] | 243 | (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 244 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 245 | NCI_NFC_B_PASSIVE_POLL_MODE; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 246 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 247 | cmd.num_disc_configs++; |
| 248 | } |
| 249 | |
| 250 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && |
Julien Lefrique | 772dccf | 2014-10-21 16:52:44 +0200 | [diff] [blame] | 251 | (param->im_protocols & NFC_PROTO_FELICA_MASK || |
| 252 | param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 253 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 254 | NCI_NFC_F_PASSIVE_POLL_MODE; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 255 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 256 | cmd.num_disc_configs++; |
| 257 | } |
| 258 | |
Vincent Cuissard | cfdbeea | 2014-07-22 19:48:38 +0200 | [diff] [blame] | 259 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && |
Julien Lefrique | 772dccf | 2014-10-21 16:52:44 +0200 | [diff] [blame] | 260 | (param->im_protocols & NFC_PROTO_ISO15693_MASK)) { |
Vincent Cuissard | cfdbeea | 2014-07-22 19:48:38 +0200 | [diff] [blame] | 261 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
| 262 | NCI_NFC_V_PASSIVE_POLL_MODE; |
| 263 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 264 | cmd.num_disc_configs++; |
| 265 | } |
| 266 | |
Julien Lefrique | 772dccf | 2014-10-21 16:52:44 +0200 | [diff] [blame] | 267 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) && |
| 268 | (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) { |
| 269 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
| 270 | NCI_NFC_A_PASSIVE_LISTEN_MODE; |
| 271 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 272 | cmd.num_disc_configs++; |
| 273 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
| 274 | NCI_NFC_F_PASSIVE_LISTEN_MODE; |
| 275 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 276 | cmd.num_disc_configs++; |
| 277 | } |
| 278 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 279 | nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 280 | (1 + (cmd.num_disc_configs * sizeof(struct disc_config))), |
| 281 | &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 282 | } |
| 283 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 284 | struct nci_rf_discover_select_param { |
| 285 | __u8 rf_discovery_id; |
| 286 | __u8 rf_protocol; |
| 287 | }; |
| 288 | |
| 289 | static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt) |
| 290 | { |
| 291 | struct nci_rf_discover_select_param *param = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 292 | (struct nci_rf_discover_select_param *)opt; |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 293 | struct nci_rf_discover_select_cmd cmd; |
| 294 | |
| 295 | cmd.rf_discovery_id = param->rf_discovery_id; |
| 296 | cmd.rf_protocol = param->rf_protocol; |
| 297 | |
| 298 | switch (cmd.rf_protocol) { |
| 299 | case NCI_RF_PROTOCOL_ISO_DEP: |
| 300 | cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP; |
| 301 | break; |
| 302 | |
| 303 | case NCI_RF_PROTOCOL_NFC_DEP: |
| 304 | cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP; |
| 305 | break; |
| 306 | |
| 307 | default: |
| 308 | cmd.rf_interface = NCI_RF_INTERFACE_FRAME; |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 313 | sizeof(struct nci_rf_discover_select_cmd), &cmd); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 316 | static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt) |
| 317 | { |
| 318 | struct nci_rf_deactivate_cmd cmd; |
| 319 | |
Christophe Ricard | 9295b5b | 2014-12-02 21:27:49 +0100 | [diff] [blame] | 320 | cmd.type = opt; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 321 | |
| 322 | nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 323 | sizeof(struct nci_rf_deactivate_cmd), &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static int nci_open_device(struct nci_dev *ndev) |
| 327 | { |
| 328 | int rc = 0; |
| 329 | |
| 330 | mutex_lock(&ndev->req_lock); |
| 331 | |
| 332 | if (test_bit(NCI_UP, &ndev->flags)) { |
| 333 | rc = -EALREADY; |
| 334 | goto done; |
| 335 | } |
| 336 | |
| 337 | if (ndev->ops->open(ndev)) { |
| 338 | rc = -EIO; |
| 339 | goto done; |
| 340 | } |
| 341 | |
| 342 | atomic_set(&ndev->cmd_cnt, 1); |
| 343 | |
| 344 | set_bit(NCI_INIT, &ndev->flags); |
| 345 | |
| 346 | rc = __nci_request(ndev, nci_reset_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 347 | msecs_to_jiffies(NCI_RESET_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 348 | |
Amitkumar Karwar | 44a589c | 2014-02-06 11:28:31 -0800 | [diff] [blame] | 349 | if (ndev->ops->setup) |
Amitkumar Karwar | 86e8586 | 2014-01-06 12:58:17 -0800 | [diff] [blame] | 350 | ndev->ops->setup(ndev); |
| 351 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 352 | if (!rc) { |
| 353 | rc = __nci_request(ndev, nci_init_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 354 | msecs_to_jiffies(NCI_INIT_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | if (!rc) { |
| 358 | rc = __nci_request(ndev, nci_init_complete_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 359 | msecs_to_jiffies(NCI_INIT_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | clear_bit(NCI_INIT, &ndev->flags); |
| 363 | |
| 364 | if (!rc) { |
| 365 | set_bit(NCI_UP, &ndev->flags); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 366 | nci_clear_target_list(ndev); |
Ilan Elias | 8939e47 | 2012-01-18 13:16:12 +0200 | [diff] [blame] | 367 | atomic_set(&ndev->state, NCI_IDLE); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 368 | } else { |
| 369 | /* Init failed, cleanup */ |
| 370 | skb_queue_purge(&ndev->cmd_q); |
| 371 | skb_queue_purge(&ndev->rx_q); |
| 372 | skb_queue_purge(&ndev->tx_q); |
| 373 | |
| 374 | ndev->ops->close(ndev); |
| 375 | ndev->flags = 0; |
| 376 | } |
| 377 | |
| 378 | done: |
| 379 | mutex_unlock(&ndev->req_lock); |
| 380 | return rc; |
| 381 | } |
| 382 | |
| 383 | static int nci_close_device(struct nci_dev *ndev) |
| 384 | { |
| 385 | nci_req_cancel(ndev, ENODEV); |
| 386 | mutex_lock(&ndev->req_lock); |
| 387 | |
| 388 | if (!test_and_clear_bit(NCI_UP, &ndev->flags)) { |
| 389 | del_timer_sync(&ndev->cmd_timer); |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 390 | del_timer_sync(&ndev->data_timer); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 391 | mutex_unlock(&ndev->req_lock); |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | /* Drop RX and TX queues */ |
| 396 | skb_queue_purge(&ndev->rx_q); |
| 397 | skb_queue_purge(&ndev->tx_q); |
| 398 | |
| 399 | /* Flush RX and TX wq */ |
| 400 | flush_workqueue(ndev->rx_wq); |
| 401 | flush_workqueue(ndev->tx_wq); |
| 402 | |
| 403 | /* Reset device */ |
| 404 | skb_queue_purge(&ndev->cmd_q); |
| 405 | atomic_set(&ndev->cmd_cnt, 1); |
| 406 | |
| 407 | set_bit(NCI_INIT, &ndev->flags); |
| 408 | __nci_request(ndev, nci_reset_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 409 | msecs_to_jiffies(NCI_RESET_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 410 | clear_bit(NCI_INIT, &ndev->flags); |
| 411 | |
Amitkumar Karwar | fa9be5f | 2013-12-23 14:15:13 -0800 | [diff] [blame] | 412 | del_timer_sync(&ndev->cmd_timer); |
| 413 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 414 | /* Flush cmd wq */ |
| 415 | flush_workqueue(ndev->cmd_wq); |
| 416 | |
| 417 | /* After this point our queues are empty |
| 418 | * and no works are scheduled. */ |
| 419 | ndev->ops->close(ndev); |
| 420 | |
| 421 | /* Clear flags */ |
| 422 | ndev->flags = 0; |
| 423 | |
| 424 | mutex_unlock(&ndev->req_lock); |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | /* NCI command timer function */ |
| 430 | static void nci_cmd_timer(unsigned long arg) |
| 431 | { |
| 432 | struct nci_dev *ndev = (void *) arg; |
| 433 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 434 | atomic_set(&ndev->cmd_cnt, 1); |
| 435 | queue_work(ndev->cmd_wq, &ndev->cmd_work); |
| 436 | } |
| 437 | |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 438 | /* NCI data exchange timer function */ |
| 439 | static void nci_data_timer(unsigned long arg) |
| 440 | { |
| 441 | struct nci_dev *ndev = (void *) arg; |
| 442 | |
| 443 | set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); |
| 444 | queue_work(ndev->rx_wq, &ndev->rx_work); |
| 445 | } |
| 446 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 447 | static int nci_dev_up(struct nfc_dev *nfc_dev) |
| 448 | { |
| 449 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 450 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 451 | return nci_open_device(ndev); |
| 452 | } |
| 453 | |
| 454 | static int nci_dev_down(struct nfc_dev *nfc_dev) |
| 455 | { |
| 456 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 457 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 458 | return nci_close_device(ndev); |
| 459 | } |
| 460 | |
Amitkumar Karwar | 22c15bf | 2014-01-06 12:58:18 -0800 | [diff] [blame] | 461 | int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val) |
| 462 | { |
| 463 | struct nci_set_config_param param; |
| 464 | |
| 465 | if (!val || !len) |
| 466 | return 0; |
| 467 | |
| 468 | param.id = id; |
| 469 | param.len = len; |
| 470 | param.val = val; |
| 471 | |
| 472 | return __nci_request(ndev, nci_set_config_req, (unsigned long)¶m, |
| 473 | msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT)); |
| 474 | } |
| 475 | EXPORT_SYMBOL(nci_set_config); |
| 476 | |
Christophe Ricard | af9c8aa | 2015-02-01 22:26:10 +0100 | [diff] [blame] | 477 | static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt) |
| 478 | { |
| 479 | struct nci_nfcee_discover_cmd cmd; |
| 480 | __u8 action = opt; |
| 481 | |
| 482 | cmd.discovery_action = action; |
| 483 | |
| 484 | nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd); |
| 485 | } |
| 486 | |
| 487 | int nci_nfcee_discover(struct nci_dev *ndev, u8 action) |
| 488 | { |
| 489 | return nci_request(ndev, nci_nfcee_discover_req, action, |
| 490 | msecs_to_jiffies(NCI_CMD_TIMEOUT)); |
| 491 | } |
| 492 | EXPORT_SYMBOL(nci_nfcee_discover); |
| 493 | |
Christophe Ricard | f7f793f | 2015-02-01 22:26:11 +0100 | [diff] [blame] | 494 | static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt) |
| 495 | { |
| 496 | struct nci_nfcee_mode_set_cmd *cmd = |
| 497 | (struct nci_nfcee_mode_set_cmd *)opt; |
| 498 | |
| 499 | nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD, |
| 500 | sizeof(struct nci_nfcee_mode_set_cmd), cmd); |
| 501 | } |
| 502 | |
| 503 | int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode) |
| 504 | { |
| 505 | struct nci_nfcee_mode_set_cmd cmd; |
| 506 | |
| 507 | cmd.nfcee_id = nfcee_id; |
| 508 | cmd.nfcee_mode = nfcee_mode; |
| 509 | |
| 510 | return nci_request(ndev, nci_nfcee_mode_set_req, (unsigned long)&cmd, |
| 511 | msecs_to_jiffies(NCI_CMD_TIMEOUT)); |
| 512 | } |
| 513 | EXPORT_SYMBOL(nci_nfcee_mode_set); |
| 514 | |
Christophe Ricard | 736bb95 | 2015-02-01 22:26:12 +0100 | [diff] [blame] | 515 | static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt) |
| 516 | { |
Christophe Ricard | b16ae71 | 2015-02-03 19:48:05 +0100 | [diff] [blame] | 517 | struct core_conn_create_data *data = |
| 518 | (struct core_conn_create_data *)opt; |
Christophe Ricard | 736bb95 | 2015-02-01 22:26:12 +0100 | [diff] [blame] | 519 | |
Christophe Ricard | b16ae71 | 2015-02-03 19:48:05 +0100 | [diff] [blame] | 520 | nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd); |
Christophe Ricard | 736bb95 | 2015-02-01 22:26:12 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Christophe Ricard | b16ae71 | 2015-02-03 19:48:05 +0100 | [diff] [blame] | 523 | int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type, |
| 524 | u8 number_destination_params, |
| 525 | size_t params_len, |
Christophe Ricard | 736bb95 | 2015-02-01 22:26:12 +0100 | [diff] [blame] | 526 | struct core_conn_create_dest_spec_params *params) |
| 527 | { |
Christophe Ricard | b16ae71 | 2015-02-03 19:48:05 +0100 | [diff] [blame] | 528 | int r; |
| 529 | struct nci_core_conn_create_cmd *cmd; |
| 530 | struct core_conn_create_data data; |
| 531 | |
| 532 | data.length = params_len + sizeof(struct nci_core_conn_create_cmd); |
| 533 | cmd = kzalloc(data.length, GFP_KERNEL); |
| 534 | if (!cmd) |
| 535 | return -ENOMEM; |
| 536 | |
| 537 | cmd->destination_type = destination_type; |
| 538 | cmd->number_destination_params = number_destination_params; |
| 539 | memcpy(cmd->params, params, params_len); |
| 540 | |
| 541 | data.cmd = cmd; |
| 542 | ndev->cur_id = params->value[DEST_SPEC_PARAMS_ID_INDEX]; |
| 543 | |
| 544 | r = __nci_request(ndev, nci_core_conn_create_req, |
| 545 | (unsigned long)&data, |
| 546 | msecs_to_jiffies(NCI_CMD_TIMEOUT)); |
| 547 | kfree(cmd); |
| 548 | return r; |
Christophe Ricard | 736bb95 | 2015-02-01 22:26:12 +0100 | [diff] [blame] | 549 | } |
| 550 | EXPORT_SYMBOL(nci_core_conn_create); |
| 551 | |
| 552 | static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt) |
| 553 | { |
| 554 | __u8 conn_id = opt; |
| 555 | |
| 556 | nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id); |
| 557 | } |
| 558 | |
| 559 | int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id) |
| 560 | { |
| 561 | return nci_request(ndev, nci_core_conn_close_req, conn_id, |
| 562 | msecs_to_jiffies(NCI_CMD_TIMEOUT)); |
| 563 | } |
| 564 | EXPORT_SYMBOL(nci_core_conn_close); |
| 565 | |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 566 | static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev) |
| 567 | { |
| 568 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 569 | struct nci_set_config_param param; |
Julien Lefrique | 529ee06 | 2014-10-21 16:52:47 +0200 | [diff] [blame] | 570 | int rc; |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 571 | |
| 572 | param.val = nfc_get_local_general_bytes(nfc_dev, ¶m.len); |
| 573 | if ((param.val == NULL) || (param.len == 0)) |
Szymon Janc | f9fc36f | 2012-10-04 15:15:46 +0200 | [diff] [blame] | 574 | return 0; |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 575 | |
Szymon Janc | 460d8f9 | 2012-10-04 15:15:45 +0200 | [diff] [blame] | 576 | if (param.len > NFC_MAX_GT_LEN) |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 577 | return -EINVAL; |
| 578 | |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 579 | param.id = NCI_PN_ATR_REQ_GEN_BYTES; |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 580 | |
Julien Lefrique | 529ee06 | 2014-10-21 16:52:47 +0200 | [diff] [blame] | 581 | rc = nci_request(ndev, nci_set_config_req, (unsigned long)¶m, |
| 582 | msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT)); |
| 583 | if (rc) |
| 584 | return rc; |
| 585 | |
| 586 | param.id = NCI_LN_ATR_RES_GEN_BYTES; |
| 587 | |
Szymon Janc | f9fc36f | 2012-10-04 15:15:46 +0200 | [diff] [blame] | 588 | return nci_request(ndev, nci_set_config_req, (unsigned long)¶m, |
| 589 | msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT)); |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 590 | } |
| 591 | |
Julien Lefrique | 90d78c1 | 2014-10-21 16:52:45 +0200 | [diff] [blame] | 592 | static int nci_set_listen_parameters(struct nfc_dev *nfc_dev) |
| 593 | { |
| 594 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 595 | int rc; |
| 596 | __u8 val; |
| 597 | |
| 598 | val = NCI_LA_SEL_INFO_NFC_DEP_MASK; |
| 599 | |
| 600 | rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val); |
| 601 | if (rc) |
| 602 | return rc; |
| 603 | |
| 604 | val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK; |
| 605 | |
| 606 | rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val); |
| 607 | if (rc) |
| 608 | return rc; |
| 609 | |
| 610 | val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424; |
| 611 | |
| 612 | return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val); |
| 613 | } |
| 614 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 615 | static int nci_start_poll(struct nfc_dev *nfc_dev, |
| 616 | __u32 im_protocols, __u32 tm_protocols) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 617 | { |
| 618 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
Julien Lefrique | 772dccf | 2014-10-21 16:52:44 +0200 | [diff] [blame] | 619 | struct nci_rf_discover_param param; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 620 | int rc; |
| 621 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 622 | if ((atomic_read(&ndev->state) == NCI_DISCOVERY) || |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 623 | (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 624 | pr_err("unable to start poll, since poll is already active\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 625 | return -EBUSY; |
| 626 | } |
| 627 | |
Ilan Elias | de05479 | 2011-09-22 11:13:01 +0300 | [diff] [blame] | 628 | if (ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 629 | pr_err("there is an active target\n"); |
Ilan Elias | de05479 | 2011-09-22 11:13:01 +0300 | [diff] [blame] | 630 | return -EBUSY; |
| 631 | } |
| 632 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 633 | if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) || |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 634 | (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) { |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 635 | pr_debug("target active or w4 select, implicitly deactivate\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 636 | |
Christophe Ricard | 9295b5b | 2014-12-02 21:27:49 +0100 | [diff] [blame] | 637 | rc = nci_request(ndev, nci_rf_deactivate_req, |
| 638 | NCI_DEACTIVATE_TYPE_IDLE_MODE, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 639 | msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 640 | if (rc) |
| 641 | return -EBUSY; |
| 642 | } |
| 643 | |
Julien Lefrique | 529ee06 | 2014-10-21 16:52:47 +0200 | [diff] [blame] | 644 | if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) { |
Ilan Elias | 7e03523 | 2012-08-15 11:46:22 +0300 | [diff] [blame] | 645 | rc = nci_set_local_general_bytes(nfc_dev); |
| 646 | if (rc) { |
| 647 | pr_err("failed to set local general bytes\n"); |
| 648 | return rc; |
| 649 | } |
| 650 | } |
| 651 | |
Julien Lefrique | 90d78c1 | 2014-10-21 16:52:45 +0200 | [diff] [blame] | 652 | if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) { |
| 653 | rc = nci_set_listen_parameters(nfc_dev); |
| 654 | if (rc) |
| 655 | pr_err("failed to set listen parameters\n"); |
| 656 | } |
| 657 | |
Julien Lefrique | 772dccf | 2014-10-21 16:52:44 +0200 | [diff] [blame] | 658 | param.im_protocols = im_protocols; |
| 659 | param.tm_protocols = tm_protocols; |
| 660 | rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)¶m, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 661 | msecs_to_jiffies(NCI_RF_DISC_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 662 | |
| 663 | if (!rc) |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 664 | ndev->poll_prots = im_protocols; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 665 | |
| 666 | return rc; |
| 667 | } |
| 668 | |
| 669 | static void nci_stop_poll(struct nfc_dev *nfc_dev) |
| 670 | { |
| 671 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 672 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 673 | if ((atomic_read(&ndev->state) != NCI_DISCOVERY) && |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 674 | (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 675 | pr_err("unable to stop poll, since poll is not active\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 676 | return; |
| 677 | } |
| 678 | |
Christophe Ricard | 9295b5b | 2014-12-02 21:27:49 +0100 | [diff] [blame] | 679 | nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 680 | msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 681 | } |
| 682 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 683 | static int nci_activate_target(struct nfc_dev *nfc_dev, |
| 684 | struct nfc_target *target, __u32 protocol) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 685 | { |
| 686 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 687 | struct nci_rf_discover_select_param param; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 688 | struct nfc_target *nci_target = NULL; |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 689 | int i; |
| 690 | int rc = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 691 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 692 | pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 693 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 694 | if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) && |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 695 | (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 696 | pr_err("there is no available target to activate\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 697 | return -EINVAL; |
| 698 | } |
| 699 | |
| 700 | if (ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 701 | pr_err("there is already an active target\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 702 | return -EBUSY; |
| 703 | } |
| 704 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 705 | for (i = 0; i < ndev->n_targets; i++) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 706 | if (ndev->targets[i].idx == target->idx) { |
| 707 | nci_target = &ndev->targets[i]; |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 708 | break; |
| 709 | } |
| 710 | } |
| 711 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 712 | if (!nci_target) { |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 713 | pr_err("unable to find the selected target\n"); |
| 714 | return -EINVAL; |
| 715 | } |
| 716 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 717 | if (!(nci_target->supported_protocols & (1 << protocol))) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 718 | pr_err("target does not support the requested protocol 0x%x\n", |
| 719 | protocol); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 720 | return -EINVAL; |
| 721 | } |
| 722 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 723 | if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 724 | param.rf_discovery_id = nci_target->logical_idx; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 725 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 726 | if (protocol == NFC_PROTO_JEWEL) |
| 727 | param.rf_protocol = NCI_RF_PROTOCOL_T1T; |
| 728 | else if (protocol == NFC_PROTO_MIFARE) |
| 729 | param.rf_protocol = NCI_RF_PROTOCOL_T2T; |
| 730 | else if (protocol == NFC_PROTO_FELICA) |
| 731 | param.rf_protocol = NCI_RF_PROTOCOL_T3T; |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 732 | else if (protocol == NFC_PROTO_ISO14443 || |
| 733 | protocol == NFC_PROTO_ISO14443_B) |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 734 | param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP; |
| 735 | else |
| 736 | param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP; |
| 737 | |
| 738 | rc = nci_request(ndev, nci_rf_discover_select_req, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 739 | (unsigned long)¶m, |
| 740 | msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT)); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | if (!rc) |
| 744 | ndev->target_active_prot = protocol; |
| 745 | |
| 746 | return rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 747 | } |
| 748 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 749 | static void nci_deactivate_target(struct nfc_dev *nfc_dev, |
| 750 | struct nfc_target *target) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 751 | { |
| 752 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 753 | |
Ilan Elias | 767f19a | 2012-08-15 11:46:24 +0300 | [diff] [blame] | 754 | pr_debug("entry\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 755 | |
| 756 | if (!ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 757 | pr_err("unable to deactivate target, no active target\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 758 | return; |
| 759 | } |
| 760 | |
| 761 | ndev->target_active_prot = 0; |
| 762 | |
Ilan Elias | 8939e47 | 2012-01-18 13:16:12 +0200 | [diff] [blame] | 763 | if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) { |
Christophe Ricard | 9295b5b | 2014-12-02 21:27:49 +0100 | [diff] [blame] | 764 | nci_request(ndev, nci_rf_deactivate_req, |
| 765 | NCI_DEACTIVATE_TYPE_SLEEP_MODE, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 766 | msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Ilan Elias | 767f19a | 2012-08-15 11:46:24 +0300 | [diff] [blame] | 770 | static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target, |
| 771 | __u8 comm_mode, __u8 *gb, size_t gb_len) |
| 772 | { |
| 773 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 774 | int rc; |
| 775 | |
| 776 | pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode); |
| 777 | |
| 778 | rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP); |
| 779 | if (rc) |
| 780 | return rc; |
| 781 | |
| 782 | rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb, |
| 783 | ndev->remote_gb_len); |
| 784 | if (!rc) |
| 785 | rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE, |
| 786 | NFC_RF_INITIATOR); |
| 787 | |
| 788 | return rc; |
| 789 | } |
| 790 | |
| 791 | static int nci_dep_link_down(struct nfc_dev *nfc_dev) |
| 792 | { |
Julien Lefrique | d7979e1 | 2014-10-21 16:52:53 +0200 | [diff] [blame] | 793 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 794 | int rc; |
| 795 | |
Ilan Elias | 767f19a | 2012-08-15 11:46:24 +0300 | [diff] [blame] | 796 | pr_debug("entry\n"); |
| 797 | |
Julien Lefrique | d7979e1 | 2014-10-21 16:52:53 +0200 | [diff] [blame] | 798 | if (nfc_dev->rf_mode == NFC_RF_INITIATOR) { |
| 799 | nci_deactivate_target(nfc_dev, NULL); |
| 800 | } else { |
| 801 | if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE || |
| 802 | atomic_read(&ndev->state) == NCI_DISCOVERY) { |
| 803 | nci_request(ndev, nci_rf_deactivate_req, 0, |
| 804 | msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); |
| 805 | } |
| 806 | |
| 807 | rc = nfc_tm_deactivated(nfc_dev); |
| 808 | if (rc) |
| 809 | pr_err("error when signaling tm deactivation\n"); |
| 810 | } |
Ilan Elias | 767f19a | 2012-08-15 11:46:24 +0300 | [diff] [blame] | 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 816 | static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target, |
| 817 | struct sk_buff *skb, |
| 818 | data_exchange_cb_t cb, void *cb_context) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 819 | { |
| 820 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 821 | int rc; |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 822 | struct nci_conn_info *conn_info; |
| 823 | |
Christophe Ricard | 12bdf27 | 2015-02-03 19:48:04 +0100 | [diff] [blame] | 824 | conn_info = ndev->rf_conn_info; |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 825 | if (!conn_info) |
| 826 | return -EPROTO; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 827 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 828 | pr_debug("target_idx %d, len %d\n", target->idx, skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 829 | |
| 830 | if (!ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 831 | pr_err("unable to exchange data, no active target\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 832 | return -EINVAL; |
| 833 | } |
| 834 | |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 835 | if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags)) |
| 836 | return -EBUSY; |
| 837 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 838 | /* store cb and context to be used on receiving data */ |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 839 | conn_info->data_exchange_cb = cb; |
| 840 | conn_info->data_exchange_cb_context = cb_context; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 841 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 842 | rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb); |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 843 | if (rc) |
| 844 | clear_bit(NCI_DATA_EXCHANGE, &ndev->flags); |
| 845 | |
| 846 | return rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 847 | } |
| 848 | |
Julien Lefrique | 485f442 | 2014-10-21 16:52:48 +0200 | [diff] [blame] | 849 | static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb) |
| 850 | { |
| 851 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 852 | int rc; |
| 853 | |
| 854 | rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb); |
| 855 | if (rc) |
| 856 | pr_err("unable to send data\n"); |
| 857 | |
| 858 | return rc; |
| 859 | } |
| 860 | |
Samuel Ortiz | 0a94630 | 2013-05-10 11:57:06 +0200 | [diff] [blame] | 861 | static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx) |
| 862 | { |
Christophe Ricard | 93bca2b | 2014-11-13 00:30:36 +0100 | [diff] [blame] | 863 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 864 | |
| 865 | if (ndev->ops->enable_se) |
| 866 | return ndev->ops->enable_se(ndev, se_idx); |
| 867 | |
Samuel Ortiz | 0a94630 | 2013-05-10 11:57:06 +0200 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx) |
| 872 | { |
Christophe Ricard | e9ef943 | 2014-11-13 00:30:37 +0100 | [diff] [blame] | 873 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 874 | |
| 875 | if (ndev->ops->disable_se) |
| 876 | return ndev->ops->disable_se(ndev, se_idx); |
| 877 | |
Samuel Ortiz | 0a94630 | 2013-05-10 11:57:06 +0200 | [diff] [blame] | 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | static int nci_discover_se(struct nfc_dev *nfc_dev) |
| 882 | { |
Christophe Ricard | fa00e8f | 2015-02-03 19:48:08 +0100 | [diff] [blame] | 883 | int r; |
Christophe Ricard | ba4db55 | 2014-11-13 00:30:35 +0100 | [diff] [blame] | 884 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 885 | |
Christophe Ricard | fa00e8f | 2015-02-03 19:48:08 +0100 | [diff] [blame] | 886 | if (ndev->ops->discover_se) { |
| 887 | r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE); |
| 888 | if (r != NCI_STATUS_OK) |
| 889 | return -EPROTO; |
| 890 | |
Christophe Ricard | ba4db55 | 2014-11-13 00:30:35 +0100 | [diff] [blame] | 891 | return ndev->ops->discover_se(ndev); |
Christophe Ricard | fa00e8f | 2015-02-03 19:48:08 +0100 | [diff] [blame] | 892 | } |
Christophe Ricard | ba4db55 | 2014-11-13 00:30:35 +0100 | [diff] [blame] | 893 | |
Samuel Ortiz | 0a94630 | 2013-05-10 11:57:06 +0200 | [diff] [blame] | 894 | return 0; |
| 895 | } |
| 896 | |
Christophe Ricard | a688bf5 | 2014-11-13 00:30:38 +0100 | [diff] [blame] | 897 | static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx, |
| 898 | u8 *apdu, size_t apdu_length, |
| 899 | se_io_cb_t cb, void *cb_context) |
| 900 | { |
| 901 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 902 | |
| 903 | if (ndev->ops->se_io) |
| 904 | return ndev->ops->se_io(ndev, se_idx, apdu, |
| 905 | apdu_length, cb, cb_context); |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 910 | static struct nfc_ops nci_nfc_ops = { |
| 911 | .dev_up = nci_dev_up, |
| 912 | .dev_down = nci_dev_down, |
| 913 | .start_poll = nci_start_poll, |
| 914 | .stop_poll = nci_stop_poll, |
Ilan Elias | 767f19a | 2012-08-15 11:46:24 +0300 | [diff] [blame] | 915 | .dep_link_up = nci_dep_link_up, |
| 916 | .dep_link_down = nci_dep_link_down, |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 917 | .activate_target = nci_activate_target, |
| 918 | .deactivate_target = nci_deactivate_target, |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 919 | .im_transceive = nci_transceive, |
Julien Lefrique | 485f442 | 2014-10-21 16:52:48 +0200 | [diff] [blame] | 920 | .tm_send = nci_tm_send, |
Samuel Ortiz | 0a94630 | 2013-05-10 11:57:06 +0200 | [diff] [blame] | 921 | .enable_se = nci_enable_se, |
| 922 | .disable_se = nci_disable_se, |
| 923 | .discover_se = nci_discover_se, |
Christophe Ricard | a688bf5 | 2014-11-13 00:30:38 +0100 | [diff] [blame] | 924 | .se_io = nci_se_io, |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 925 | }; |
| 926 | |
| 927 | /* ---- Interface to NCI drivers ---- */ |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 928 | /** |
| 929 | * nci_allocate_device - allocate a new nci device |
| 930 | * |
| 931 | * @ops: device operations |
| 932 | * @supported_protocols: NFC protocols supported by the device |
| 933 | */ |
| 934 | struct nci_dev *nci_allocate_device(struct nci_ops *ops, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 935 | __u32 supported_protocols, |
| 936 | int tx_headroom, int tx_tailroom) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 937 | { |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 938 | struct nci_dev *ndev; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 939 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 940 | pr_debug("supported_protocols 0x%x\n", supported_protocols); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 941 | |
| 942 | if (!ops->open || !ops->close || !ops->send) |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 943 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 944 | |
| 945 | if (!supported_protocols) |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 946 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 947 | |
| 948 | ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL); |
| 949 | if (!ndev) |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 950 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 951 | |
| 952 | ndev->ops = ops; |
| 953 | ndev->tx_headroom = tx_headroom; |
| 954 | ndev->tx_tailroom = tx_tailroom; |
Axel Lin | 9bec44b | 2014-02-13 13:25:48 +0800 | [diff] [blame] | 955 | init_completion(&ndev->req_completion); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 956 | |
| 957 | ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 958 | supported_protocols, |
| 959 | tx_headroom + NCI_DATA_HDR_SIZE, |
| 960 | tx_tailroom); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 961 | if (!ndev->nfc_dev) |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 962 | goto free_nci; |
| 963 | |
| 964 | ndev->hci_dev = nci_hci_allocate(ndev); |
| 965 | if (!ndev->hci_dev) |
| 966 | goto free_nfc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 967 | |
| 968 | nfc_set_drvdata(ndev->nfc_dev, ndev); |
| 969 | |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 970 | return ndev; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 971 | |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 972 | free_nfc: |
| 973 | kfree(ndev->nfc_dev); |
| 974 | |
| 975 | free_nci: |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 976 | kfree(ndev); |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 977 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 978 | } |
| 979 | EXPORT_SYMBOL(nci_allocate_device); |
| 980 | |
| 981 | /** |
| 982 | * nci_free_device - deallocate nci device |
| 983 | * |
| 984 | * @ndev: The nci device to deallocate |
| 985 | */ |
| 986 | void nci_free_device(struct nci_dev *ndev) |
| 987 | { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 988 | nfc_free_device(ndev->nfc_dev); |
| 989 | kfree(ndev); |
| 990 | } |
| 991 | EXPORT_SYMBOL(nci_free_device); |
| 992 | |
| 993 | /** |
| 994 | * nci_register_device - register a nci device in the nfc subsystem |
| 995 | * |
| 996 | * @dev: The nci device to register |
| 997 | */ |
| 998 | int nci_register_device(struct nci_dev *ndev) |
| 999 | { |
| 1000 | int rc; |
| 1001 | struct device *dev = &ndev->nfc_dev->dev; |
| 1002 | char name[32]; |
| 1003 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1004 | ndev->flags = 0; |
| 1005 | |
| 1006 | INIT_WORK(&ndev->cmd_work, nci_cmd_work); |
| 1007 | snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev)); |
| 1008 | ndev->cmd_wq = create_singlethread_workqueue(name); |
| 1009 | if (!ndev->cmd_wq) { |
| 1010 | rc = -ENOMEM; |
Vincent Cuissard | 3c1c0f5 | 2014-07-22 19:48:39 +0200 | [diff] [blame] | 1011 | goto exit; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | INIT_WORK(&ndev->rx_work, nci_rx_work); |
| 1015 | snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev)); |
| 1016 | ndev->rx_wq = create_singlethread_workqueue(name); |
| 1017 | if (!ndev->rx_wq) { |
| 1018 | rc = -ENOMEM; |
| 1019 | goto destroy_cmd_wq_exit; |
| 1020 | } |
| 1021 | |
| 1022 | INIT_WORK(&ndev->tx_work, nci_tx_work); |
| 1023 | snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev)); |
| 1024 | ndev->tx_wq = create_singlethread_workqueue(name); |
| 1025 | if (!ndev->tx_wq) { |
| 1026 | rc = -ENOMEM; |
| 1027 | goto destroy_rx_wq_exit; |
| 1028 | } |
| 1029 | |
| 1030 | skb_queue_head_init(&ndev->cmd_q); |
| 1031 | skb_queue_head_init(&ndev->rx_q); |
| 1032 | skb_queue_head_init(&ndev->tx_q); |
| 1033 | |
| 1034 | setup_timer(&ndev->cmd_timer, nci_cmd_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 1035 | (unsigned long) ndev); |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 1036 | setup_timer(&ndev->data_timer, nci_data_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 1037 | (unsigned long) ndev); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1038 | |
| 1039 | mutex_init(&ndev->req_lock); |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 1040 | INIT_LIST_HEAD(&ndev->conn_info_list); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1041 | |
Vincent Cuissard | 3c1c0f5 | 2014-07-22 19:48:39 +0200 | [diff] [blame] | 1042 | rc = nfc_register_device(ndev->nfc_dev); |
| 1043 | if (rc) |
| 1044 | goto destroy_rx_wq_exit; |
| 1045 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1046 | goto exit; |
| 1047 | |
| 1048 | destroy_rx_wq_exit: |
| 1049 | destroy_workqueue(ndev->rx_wq); |
| 1050 | |
| 1051 | destroy_cmd_wq_exit: |
| 1052 | destroy_workqueue(ndev->cmd_wq); |
| 1053 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1054 | exit: |
| 1055 | return rc; |
| 1056 | } |
| 1057 | EXPORT_SYMBOL(nci_register_device); |
| 1058 | |
| 1059 | /** |
| 1060 | * nci_unregister_device - unregister a nci device in the nfc subsystem |
| 1061 | * |
| 1062 | * @dev: The nci device to unregister |
| 1063 | */ |
| 1064 | void nci_unregister_device(struct nci_dev *ndev) |
| 1065 | { |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 1066 | struct nci_conn_info *conn_info, *n; |
| 1067 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1068 | nci_close_device(ndev); |
| 1069 | |
| 1070 | destroy_workqueue(ndev->cmd_wq); |
| 1071 | destroy_workqueue(ndev->rx_wq); |
| 1072 | destroy_workqueue(ndev->tx_wq); |
| 1073 | |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 1074 | list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) { |
| 1075 | list_del(&conn_info->list); |
| 1076 | /* conn_info is allocated with devm_kzalloc */ |
| 1077 | } |
| 1078 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1079 | nfc_unregister_device(ndev->nfc_dev); |
| 1080 | } |
| 1081 | EXPORT_SYMBOL(nci_unregister_device); |
| 1082 | |
| 1083 | /** |
| 1084 | * nci_recv_frame - receive frame from NCI drivers |
| 1085 | * |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 1086 | * @ndev: The nci device |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1087 | * @skb: The sk_buff to receive |
| 1088 | */ |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 1089 | int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1090 | { |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 1091 | pr_debug("len %d\n", skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1092 | |
Szymon Janc | 874934f | 2012-10-04 15:15:51 +0200 | [diff] [blame] | 1093 | if (!ndev || (!test_bit(NCI_UP, &ndev->flags) && |
| 1094 | !test_bit(NCI_INIT, &ndev->flags))) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1095 | kfree_skb(skb); |
| 1096 | return -ENXIO; |
| 1097 | } |
| 1098 | |
| 1099 | /* Queue frame for rx worker thread */ |
| 1100 | skb_queue_tail(&ndev->rx_q, skb); |
| 1101 | queue_work(ndev->rx_wq, &ndev->rx_work); |
| 1102 | |
| 1103 | return 0; |
| 1104 | } |
| 1105 | EXPORT_SYMBOL(nci_recv_frame); |
| 1106 | |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 1107 | 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] | 1108 | { |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 1109 | pr_debug("len %d\n", skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1110 | |
| 1111 | if (!ndev) { |
| 1112 | kfree_skb(skb); |
| 1113 | return -ENODEV; |
| 1114 | } |
| 1115 | |
| 1116 | /* Get rid of skb owner, prior to sending to the driver. */ |
| 1117 | skb_orphan(skb); |
| 1118 | |
Hiren Tandel | 0515829 | 2014-05-05 19:52:27 +0900 | [diff] [blame] | 1119 | /* Send copy to sniffer */ |
| 1120 | nfc_send_to_raw_sock(ndev->nfc_dev, skb, |
| 1121 | RAW_PAYLOAD_NCI, NFC_DIRECTION_TX); |
| 1122 | |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 1123 | return ndev->ops->send(ndev, skb); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* Send NCI command */ |
| 1127 | int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload) |
| 1128 | { |
| 1129 | struct nci_ctrl_hdr *hdr; |
| 1130 | struct sk_buff *skb; |
| 1131 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 1132 | pr_debug("opcode 0x%x, plen %d\n", opcode, plen); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1133 | |
| 1134 | skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL); |
| 1135 | if (!skb) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 1136 | pr_err("no memory for command\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1137 | return -ENOMEM; |
| 1138 | } |
| 1139 | |
| 1140 | hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE); |
| 1141 | hdr->gid = nci_opcode_gid(opcode); |
| 1142 | hdr->oid = nci_opcode_oid(opcode); |
| 1143 | hdr->plen = plen; |
| 1144 | |
| 1145 | nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT); |
| 1146 | nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST); |
| 1147 | |
| 1148 | if (plen) |
| 1149 | memcpy(skb_put(skb, plen), payload, plen); |
| 1150 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1151 | skb_queue_tail(&ndev->cmd_q, skb); |
| 1152 | queue_work(ndev->cmd_wq, &ndev->cmd_work); |
| 1153 | |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
| 1157 | /* ---- NCI TX Data worker thread ---- */ |
| 1158 | |
| 1159 | static void nci_tx_work(struct work_struct *work) |
| 1160 | { |
| 1161 | struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work); |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 1162 | struct nci_conn_info *conn_info; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1163 | struct sk_buff *skb; |
| 1164 | |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 1165 | conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id); |
| 1166 | if (!conn_info) |
| 1167 | return; |
| 1168 | |
| 1169 | pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1170 | |
| 1171 | /* Send queued tx data */ |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 1172 | while (atomic_read(&conn_info->credits_cnt)) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1173 | skb = skb_dequeue(&ndev->tx_q); |
| 1174 | if (!skb) |
| 1175 | return; |
| 1176 | |
Ilan Elias | db98c82 | 2011-11-09 12:09:16 +0200 | [diff] [blame] | 1177 | /* Check if data flow control is used */ |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 1178 | if (atomic_read(&conn_info->credits_cnt) != |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 1179 | NCI_DATA_FLOW_CONTROL_NOT_USED) |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 1180 | atomic_dec(&conn_info->credits_cnt); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1181 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 1182 | pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n", |
| 1183 | nci_pbf(skb->data), |
| 1184 | nci_conn_id(skb->data), |
| 1185 | nci_plen(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1186 | |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 1187 | nci_send_frame(ndev, skb); |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 1188 | |
| 1189 | mod_timer(&ndev->data_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 1190 | jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /* ----- NCI RX worker thread (data & control) ----- */ |
| 1195 | |
| 1196 | static void nci_rx_work(struct work_struct *work) |
| 1197 | { |
| 1198 | struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work); |
| 1199 | struct sk_buff *skb; |
| 1200 | |
| 1201 | while ((skb = skb_dequeue(&ndev->rx_q))) { |
Hiren Tandel | 0515829 | 2014-05-05 19:52:27 +0900 | [diff] [blame] | 1202 | |
| 1203 | /* Send copy to sniffer */ |
| 1204 | nfc_send_to_raw_sock(ndev->nfc_dev, skb, |
| 1205 | RAW_PAYLOAD_NCI, NFC_DIRECTION_RX); |
| 1206 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1207 | /* Process frame */ |
| 1208 | switch (nci_mt(skb->data)) { |
| 1209 | case NCI_MT_RSP_PKT: |
| 1210 | nci_rsp_packet(ndev, skb); |
| 1211 | break; |
| 1212 | |
| 1213 | case NCI_MT_NTF_PKT: |
| 1214 | nci_ntf_packet(ndev, skb); |
| 1215 | break; |
| 1216 | |
| 1217 | case NCI_MT_DATA_PKT: |
| 1218 | nci_rx_data_packet(ndev, skb); |
| 1219 | break; |
| 1220 | |
| 1221 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 1222 | pr_err("unknown MT 0x%x\n", nci_mt(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1223 | kfree_skb(skb); |
| 1224 | break; |
| 1225 | } |
| 1226 | } |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 1227 | |
| 1228 | /* check if a data exchange timout has occurred */ |
| 1229 | if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) { |
| 1230 | /* complete the data exchange transaction, if exists */ |
| 1231 | if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) |
Christophe Ricard | 4aeee68 | 2015-02-01 22:26:08 +0100 | [diff] [blame] | 1232 | nci_data_exchange_complete(ndev, NULL, |
| 1233 | ndev->cur_conn_id, |
| 1234 | -ETIMEDOUT); |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 1235 | |
| 1236 | clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); |
| 1237 | } |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | /* ----- NCI TX CMD worker thread ----- */ |
| 1241 | |
| 1242 | static void nci_cmd_work(struct work_struct *work) |
| 1243 | { |
| 1244 | struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work); |
| 1245 | struct sk_buff *skb; |
| 1246 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 1247 | pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1248 | |
| 1249 | /* Send queued command */ |
| 1250 | if (atomic_read(&ndev->cmd_cnt)) { |
| 1251 | skb = skb_dequeue(&ndev->cmd_q); |
| 1252 | if (!skb) |
| 1253 | return; |
| 1254 | |
| 1255 | atomic_dec(&ndev->cmd_cnt); |
| 1256 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 1257 | pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n", |
| 1258 | nci_pbf(skb->data), |
| 1259 | nci_opcode_gid(nci_opcode(skb->data)), |
| 1260 | nci_opcode_oid(nci_opcode(skb->data)), |
| 1261 | nci_plen(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1262 | |
Frederic Danis | 1095e69 | 2013-05-22 11:36:17 +0200 | [diff] [blame] | 1263 | nci_send_frame(ndev, skb); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1264 | |
| 1265 | mod_timer(&ndev->cmd_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 1266 | jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1267 | } |
| 1268 | } |
Dave Jones | 8a70e7f | 2012-07-12 19:17:34 +0200 | [diff] [blame] | 1269 | |
| 1270 | MODULE_LICENSE("GPL"); |