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