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