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