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