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