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. |
| 6 | * |
| 7 | * Written by Ilan Elias <ilane@ti.com> |
| 8 | * |
| 9 | * Acknowledgements: |
| 10 | * This file is based on hci_core.c, which was written |
| 11 | * by Maxim Krasnyansky. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 |
| 15 | * as published by the Free Software Foundation |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 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> |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 31 | #include <linux/types.h> |
| 32 | #include <linux/workqueue.h> |
| 33 | #include <linux/completion.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 34 | #include <linux/export.h> |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 35 | #include <linux/sched.h> |
| 36 | #include <linux/bitops.h> |
| 37 | #include <linux/skbuff.h> |
| 38 | |
| 39 | #include "../nfc.h" |
| 40 | #include <net/nfc/nci.h> |
| 41 | #include <net/nfc/nci_core.h> |
| 42 | #include <linux/nfc.h> |
| 43 | |
| 44 | static void nci_cmd_work(struct work_struct *work); |
| 45 | static void nci_rx_work(struct work_struct *work); |
| 46 | static void nci_tx_work(struct work_struct *work); |
| 47 | |
| 48 | /* ---- NCI requests ---- */ |
| 49 | |
| 50 | void nci_req_complete(struct nci_dev *ndev, int result) |
| 51 | { |
| 52 | if (ndev->req_status == NCI_REQ_PEND) { |
| 53 | ndev->req_result = result; |
| 54 | ndev->req_status = NCI_REQ_DONE; |
| 55 | complete(&ndev->req_completion); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static void nci_req_cancel(struct nci_dev *ndev, int err) |
| 60 | { |
| 61 | if (ndev->req_status == NCI_REQ_PEND) { |
| 62 | ndev->req_result = err; |
| 63 | ndev->req_status = NCI_REQ_CANCELED; |
| 64 | complete(&ndev->req_completion); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /* Execute request and wait for completion. */ |
| 69 | static int __nci_request(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 70 | void (*req)(struct nci_dev *ndev, unsigned long opt), |
| 71 | unsigned long opt, __u32 timeout) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 72 | { |
| 73 | int rc = 0; |
Dan Carpenter | f8c141c | 2011-12-09 09:35:39 +0300 | [diff] [blame] | 74 | long completion_rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 75 | |
| 76 | ndev->req_status = NCI_REQ_PEND; |
| 77 | |
| 78 | init_completion(&ndev->req_completion); |
| 79 | req(ndev, opt); |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 80 | completion_rc = |
| 81 | wait_for_completion_interruptible_timeout(&ndev->req_completion, |
| 82 | timeout); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 83 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 84 | pr_debug("wait_for_completion return %ld\n", completion_rc); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 85 | |
| 86 | if (completion_rc > 0) { |
| 87 | switch (ndev->req_status) { |
| 88 | case NCI_REQ_DONE: |
| 89 | rc = nci_to_errno(ndev->req_result); |
| 90 | break; |
| 91 | |
| 92 | case NCI_REQ_CANCELED: |
| 93 | rc = -ndev->req_result; |
| 94 | break; |
| 95 | |
| 96 | default: |
| 97 | rc = -ETIMEDOUT; |
| 98 | break; |
| 99 | } |
| 100 | } else { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 101 | pr_err("wait_for_completion_interruptible_timeout failed %ld\n", |
| 102 | completion_rc); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 103 | |
| 104 | rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc)); |
| 105 | } |
| 106 | |
| 107 | ndev->req_status = ndev->req_result = 0; |
| 108 | |
| 109 | return rc; |
| 110 | } |
| 111 | |
| 112 | static inline int nci_request(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 113 | void (*req)(struct nci_dev *ndev, |
| 114 | unsigned long opt), |
| 115 | unsigned long opt, __u32 timeout) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 116 | { |
| 117 | int rc; |
| 118 | |
| 119 | if (!test_bit(NCI_UP, &ndev->flags)) |
| 120 | return -ENETDOWN; |
| 121 | |
| 122 | /* Serialize all requests */ |
| 123 | mutex_lock(&ndev->req_lock); |
| 124 | rc = __nci_request(ndev, req, opt, timeout); |
| 125 | mutex_unlock(&ndev->req_lock); |
| 126 | |
| 127 | return rc; |
| 128 | } |
| 129 | |
| 130 | static void nci_reset_req(struct nci_dev *ndev, unsigned long opt) |
| 131 | { |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 132 | struct nci_core_reset_cmd cmd; |
| 133 | |
| 134 | cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG; |
| 135 | nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void nci_init_req(struct nci_dev *ndev, unsigned long opt) |
| 139 | { |
| 140 | nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL); |
| 141 | } |
| 142 | |
| 143 | static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt) |
| 144 | { |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 145 | struct nci_rf_disc_map_cmd cmd; |
| 146 | struct disc_map_config *cfg = cmd.mapping_configs; |
| 147 | __u8 *num = &cmd.num_mapping_configs; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 148 | int i; |
| 149 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 150 | /* set rf mapping configurations */ |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 151 | *num = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 152 | |
| 153 | /* by default mapping is set to NCI_RF_INTERFACE_FRAME */ |
| 154 | for (i = 0; i < ndev->num_supported_rf_interfaces; i++) { |
| 155 | if (ndev->supported_rf_interfaces[i] == |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 156 | NCI_RF_INTERFACE_ISO_DEP) { |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 157 | cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP; |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 158 | cfg[*num].mode = NCI_DISC_MAP_MODE_POLL | |
| 159 | NCI_DISC_MAP_MODE_LISTEN; |
| 160 | cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP; |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 161 | (*num)++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 162 | } else if (ndev->supported_rf_interfaces[i] == |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 163 | NCI_RF_INTERFACE_NFC_DEP) { |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 164 | cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP; |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 165 | cfg[*num].mode = NCI_DISC_MAP_MODE_POLL | |
| 166 | NCI_DISC_MAP_MODE_LISTEN; |
| 167 | cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP; |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 168 | (*num)++; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 169 | } |
| 170 | |
Ilan Elias | 2eb1dc1 | 2011-09-22 10:47:52 +0300 | [diff] [blame] | 171 | if (*num == NCI_MAX_NUM_MAPPING_CONFIGS) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | |
| 175 | nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 176 | (1 + ((*num) * sizeof(struct disc_map_config))), &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt) |
| 180 | { |
| 181 | struct nci_rf_disc_cmd cmd; |
| 182 | __u32 protocols = opt; |
| 183 | |
| 184 | cmd.num_disc_configs = 0; |
| 185 | |
| 186 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 187 | (protocols & NFC_PROTO_JEWEL_MASK |
| 188 | || protocols & NFC_PROTO_MIFARE_MASK |
| 189 | || protocols & NFC_PROTO_ISO14443_MASK |
| 190 | || protocols & NFC_PROTO_NFC_DEP_MASK)) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 191 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 192 | NCI_NFC_A_PASSIVE_POLL_MODE; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 193 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 194 | cmd.num_disc_configs++; |
| 195 | } |
| 196 | |
| 197 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 198 | (protocols & NFC_PROTO_ISO14443_B_MASK)) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 199 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 200 | NCI_NFC_B_PASSIVE_POLL_MODE; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 201 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 202 | cmd.num_disc_configs++; |
| 203 | } |
| 204 | |
| 205 | if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) && |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 206 | (protocols & NFC_PROTO_FELICA_MASK |
| 207 | || protocols & NFC_PROTO_NFC_DEP_MASK)) { |
Ilan Elias | 637d85a | 2011-12-20 16:57:40 +0200 | [diff] [blame] | 208 | cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 209 | NCI_NFC_F_PASSIVE_POLL_MODE; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 210 | cmd.disc_configs[cmd.num_disc_configs].frequency = 1; |
| 211 | cmd.num_disc_configs++; |
| 212 | } |
| 213 | |
| 214 | nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 215 | (1 + (cmd.num_disc_configs * sizeof(struct disc_config))), |
| 216 | &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 217 | } |
| 218 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 219 | struct nci_rf_discover_select_param { |
| 220 | __u8 rf_discovery_id; |
| 221 | __u8 rf_protocol; |
| 222 | }; |
| 223 | |
| 224 | static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt) |
| 225 | { |
| 226 | struct nci_rf_discover_select_param *param = |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 227 | (struct nci_rf_discover_select_param *)opt; |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 228 | struct nci_rf_discover_select_cmd cmd; |
| 229 | |
| 230 | cmd.rf_discovery_id = param->rf_discovery_id; |
| 231 | cmd.rf_protocol = param->rf_protocol; |
| 232 | |
| 233 | switch (cmd.rf_protocol) { |
| 234 | case NCI_RF_PROTOCOL_ISO_DEP: |
| 235 | cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP; |
| 236 | break; |
| 237 | |
| 238 | case NCI_RF_PROTOCOL_NFC_DEP: |
| 239 | cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP; |
| 240 | break; |
| 241 | |
| 242 | default: |
| 243 | cmd.rf_interface = NCI_RF_INTERFACE_FRAME; |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 248 | sizeof(struct nci_rf_discover_select_cmd), &cmd); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 251 | static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt) |
| 252 | { |
| 253 | struct nci_rf_deactivate_cmd cmd; |
| 254 | |
| 255 | cmd.type = NCI_DEACTIVATE_TYPE_IDLE_MODE; |
| 256 | |
| 257 | nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 258 | sizeof(struct nci_rf_deactivate_cmd), &cmd); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static int nci_open_device(struct nci_dev *ndev) |
| 262 | { |
| 263 | int rc = 0; |
| 264 | |
| 265 | mutex_lock(&ndev->req_lock); |
| 266 | |
| 267 | if (test_bit(NCI_UP, &ndev->flags)) { |
| 268 | rc = -EALREADY; |
| 269 | goto done; |
| 270 | } |
| 271 | |
| 272 | if (ndev->ops->open(ndev)) { |
| 273 | rc = -EIO; |
| 274 | goto done; |
| 275 | } |
| 276 | |
| 277 | atomic_set(&ndev->cmd_cnt, 1); |
| 278 | |
| 279 | set_bit(NCI_INIT, &ndev->flags); |
| 280 | |
| 281 | rc = __nci_request(ndev, nci_reset_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 282 | msecs_to_jiffies(NCI_RESET_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 283 | |
| 284 | if (!rc) { |
| 285 | rc = __nci_request(ndev, nci_init_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 286 | msecs_to_jiffies(NCI_INIT_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | if (!rc) { |
| 290 | rc = __nci_request(ndev, nci_init_complete_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 291 | msecs_to_jiffies(NCI_INIT_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | clear_bit(NCI_INIT, &ndev->flags); |
| 295 | |
| 296 | if (!rc) { |
| 297 | set_bit(NCI_UP, &ndev->flags); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 298 | nci_clear_target_list(ndev); |
Ilan Elias | 8939e47 | 2012-01-18 13:16:12 +0200 | [diff] [blame] | 299 | atomic_set(&ndev->state, NCI_IDLE); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 300 | } else { |
| 301 | /* Init failed, cleanup */ |
| 302 | skb_queue_purge(&ndev->cmd_q); |
| 303 | skb_queue_purge(&ndev->rx_q); |
| 304 | skb_queue_purge(&ndev->tx_q); |
| 305 | |
| 306 | ndev->ops->close(ndev); |
| 307 | ndev->flags = 0; |
| 308 | } |
| 309 | |
| 310 | done: |
| 311 | mutex_unlock(&ndev->req_lock); |
| 312 | return rc; |
| 313 | } |
| 314 | |
| 315 | static int nci_close_device(struct nci_dev *ndev) |
| 316 | { |
| 317 | nci_req_cancel(ndev, ENODEV); |
| 318 | mutex_lock(&ndev->req_lock); |
| 319 | |
| 320 | if (!test_and_clear_bit(NCI_UP, &ndev->flags)) { |
| 321 | del_timer_sync(&ndev->cmd_timer); |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 322 | del_timer_sync(&ndev->data_timer); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 323 | mutex_unlock(&ndev->req_lock); |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* Drop RX and TX queues */ |
| 328 | skb_queue_purge(&ndev->rx_q); |
| 329 | skb_queue_purge(&ndev->tx_q); |
| 330 | |
| 331 | /* Flush RX and TX wq */ |
| 332 | flush_workqueue(ndev->rx_wq); |
| 333 | flush_workqueue(ndev->tx_wq); |
| 334 | |
| 335 | /* Reset device */ |
| 336 | skb_queue_purge(&ndev->cmd_q); |
| 337 | atomic_set(&ndev->cmd_cnt, 1); |
| 338 | |
| 339 | set_bit(NCI_INIT, &ndev->flags); |
| 340 | __nci_request(ndev, nci_reset_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 341 | msecs_to_jiffies(NCI_RESET_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 342 | clear_bit(NCI_INIT, &ndev->flags); |
| 343 | |
| 344 | /* Flush cmd wq */ |
| 345 | flush_workqueue(ndev->cmd_wq); |
| 346 | |
| 347 | /* After this point our queues are empty |
| 348 | * and no works are scheduled. */ |
| 349 | ndev->ops->close(ndev); |
| 350 | |
| 351 | /* Clear flags */ |
| 352 | ndev->flags = 0; |
| 353 | |
| 354 | mutex_unlock(&ndev->req_lock); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /* NCI command timer function */ |
| 360 | static void nci_cmd_timer(unsigned long arg) |
| 361 | { |
| 362 | struct nci_dev *ndev = (void *) arg; |
| 363 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 364 | atomic_set(&ndev->cmd_cnt, 1); |
| 365 | queue_work(ndev->cmd_wq, &ndev->cmd_work); |
| 366 | } |
| 367 | |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 368 | /* NCI data exchange timer function */ |
| 369 | static void nci_data_timer(unsigned long arg) |
| 370 | { |
| 371 | struct nci_dev *ndev = (void *) arg; |
| 372 | |
| 373 | set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); |
| 374 | queue_work(ndev->rx_wq, &ndev->rx_work); |
| 375 | } |
| 376 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 377 | static int nci_dev_up(struct nfc_dev *nfc_dev) |
| 378 | { |
| 379 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 380 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 381 | return nci_open_device(ndev); |
| 382 | } |
| 383 | |
| 384 | static int nci_dev_down(struct nfc_dev *nfc_dev) |
| 385 | { |
| 386 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 387 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 388 | return nci_close_device(ndev); |
| 389 | } |
| 390 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 391 | static int nci_start_poll(struct nfc_dev *nfc_dev, |
| 392 | __u32 im_protocols, __u32 tm_protocols) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 393 | { |
| 394 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 395 | int rc; |
| 396 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 397 | if ((atomic_read(&ndev->state) == NCI_DISCOVERY) || |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 398 | (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 399 | pr_err("unable to start poll, since poll is already active\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 400 | return -EBUSY; |
| 401 | } |
| 402 | |
Ilan Elias | de05479 | 2011-09-22 11:13:01 +0300 | [diff] [blame] | 403 | if (ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 404 | pr_err("there is an active target\n"); |
Ilan Elias | de05479 | 2011-09-22 11:13:01 +0300 | [diff] [blame] | 405 | return -EBUSY; |
| 406 | } |
| 407 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 408 | if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) || |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 409 | (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) { |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 410 | pr_debug("target active or w4 select, implicitly deactivate\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 411 | |
| 412 | rc = nci_request(ndev, nci_rf_deactivate_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 413 | msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 414 | if (rc) |
| 415 | return -EBUSY; |
| 416 | } |
| 417 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 418 | rc = nci_request(ndev, nci_rf_discover_req, im_protocols, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 419 | msecs_to_jiffies(NCI_RF_DISC_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 420 | |
| 421 | if (!rc) |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 422 | ndev->poll_prots = im_protocols; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 423 | |
| 424 | return rc; |
| 425 | } |
| 426 | |
| 427 | static void nci_stop_poll(struct nfc_dev *nfc_dev) |
| 428 | { |
| 429 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 430 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 431 | if ((atomic_read(&ndev->state) != NCI_DISCOVERY) && |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 432 | (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 433 | pr_err("unable to stop poll, since poll is not active\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
| 437 | nci_request(ndev, nci_rf_deactivate_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 438 | msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 439 | } |
| 440 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 441 | static int nci_activate_target(struct nfc_dev *nfc_dev, |
| 442 | struct nfc_target *target, __u32 protocol) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 443 | { |
| 444 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 445 | struct nci_rf_discover_select_param param; |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 446 | struct nfc_target *nci_target = NULL; |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 447 | int i; |
| 448 | int rc = 0; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 449 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 450 | pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 451 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 452 | if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) && |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 453 | (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 454 | pr_err("there is no available target to activate\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 455 | return -EINVAL; |
| 456 | } |
| 457 | |
| 458 | if (ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 459 | pr_err("there is already an active target\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 460 | return -EBUSY; |
| 461 | } |
| 462 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 463 | for (i = 0; i < ndev->n_targets; i++) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 464 | if (ndev->targets[i].idx == target->idx) { |
| 465 | nci_target = &ndev->targets[i]; |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 466 | break; |
| 467 | } |
| 468 | } |
| 469 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 470 | if (!nci_target) { |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 471 | pr_err("unable to find the selected target\n"); |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 475 | if (!(nci_target->supported_protocols & (1 << protocol))) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 476 | pr_err("target does not support the requested protocol 0x%x\n", |
| 477 | protocol); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 478 | return -EINVAL; |
| 479 | } |
| 480 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 481 | if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) { |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 482 | param.rf_discovery_id = nci_target->logical_idx; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 483 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 484 | if (protocol == NFC_PROTO_JEWEL) |
| 485 | param.rf_protocol = NCI_RF_PROTOCOL_T1T; |
| 486 | else if (protocol == NFC_PROTO_MIFARE) |
| 487 | param.rf_protocol = NCI_RF_PROTOCOL_T2T; |
| 488 | else if (protocol == NFC_PROTO_FELICA) |
| 489 | param.rf_protocol = NCI_RF_PROTOCOL_T3T; |
Samuel Ortiz | 01d719a | 2012-07-04 00:14:04 +0200 | [diff] [blame] | 490 | else if (protocol == NFC_PROTO_ISO14443 || |
| 491 | protocol == NFC_PROTO_ISO14443_B) |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 492 | param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP; |
| 493 | else |
| 494 | param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP; |
| 495 | |
| 496 | rc = nci_request(ndev, nci_rf_discover_select_req, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 497 | (unsigned long)¶m, |
| 498 | msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT)); |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | if (!rc) |
| 502 | ndev->target_active_prot = protocol; |
| 503 | |
| 504 | return rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 505 | } |
| 506 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 507 | static void nci_deactivate_target(struct nfc_dev *nfc_dev, |
| 508 | struct nfc_target *target) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 509 | { |
| 510 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
| 511 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 512 | pr_debug("target_idx %d\n", target->idx); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 513 | |
| 514 | if (!ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 515 | pr_err("unable to deactivate target, no active target\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | |
| 519 | ndev->target_active_prot = 0; |
| 520 | |
Ilan Elias | 8939e47 | 2012-01-18 13:16:12 +0200 | [diff] [blame] | 521 | if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 522 | nci_request(ndev, nci_rf_deactivate_req, 0, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 523 | msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 527 | static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target, |
| 528 | struct sk_buff *skb, |
| 529 | data_exchange_cb_t cb, void *cb_context) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 530 | { |
| 531 | struct nci_dev *ndev = nfc_get_drvdata(nfc_dev); |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 532 | int rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 533 | |
Eric Lapuyade | 9009943 | 2012-05-07 12:31:13 +0200 | [diff] [blame] | 534 | pr_debug("target_idx %d, len %d\n", target->idx, skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 535 | |
| 536 | if (!ndev->target_active_prot) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 537 | pr_err("unable to exchange data, no active target\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 538 | return -EINVAL; |
| 539 | } |
| 540 | |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 541 | if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags)) |
| 542 | return -EBUSY; |
| 543 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 544 | /* store cb and context to be used on receiving data */ |
| 545 | ndev->data_exchange_cb = cb; |
| 546 | ndev->data_exchange_cb_context = cb_context; |
| 547 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 548 | rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb); |
Ilan Elias | 38f04c6 | 2011-09-22 11:36:19 +0300 | [diff] [blame] | 549 | if (rc) |
| 550 | clear_bit(NCI_DATA_EXCHANGE, &ndev->flags); |
| 551 | |
| 552 | return rc; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static struct nfc_ops nci_nfc_ops = { |
| 556 | .dev_up = nci_dev_up, |
| 557 | .dev_down = nci_dev_down, |
| 558 | .start_poll = nci_start_poll, |
| 559 | .stop_poll = nci_stop_poll, |
| 560 | .activate_target = nci_activate_target, |
| 561 | .deactivate_target = nci_deactivate_target, |
Samuel Ortiz | be9ae4c | 2012-05-16 15:55:48 +0200 | [diff] [blame] | 562 | .im_transceive = nci_transceive, |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 563 | }; |
| 564 | |
| 565 | /* ---- Interface to NCI drivers ---- */ |
| 566 | |
| 567 | /** |
| 568 | * nci_allocate_device - allocate a new nci device |
| 569 | * |
| 570 | * @ops: device operations |
| 571 | * @supported_protocols: NFC protocols supported by the device |
| 572 | */ |
| 573 | struct nci_dev *nci_allocate_device(struct nci_ops *ops, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 574 | __u32 supported_protocols, |
| 575 | int tx_headroom, int tx_tailroom) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 576 | { |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 577 | struct nci_dev *ndev; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 578 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 579 | pr_debug("supported_protocols 0x%x\n", supported_protocols); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 580 | |
| 581 | if (!ops->open || !ops->close || !ops->send) |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 582 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 583 | |
| 584 | if (!supported_protocols) |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 585 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 586 | |
| 587 | ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL); |
| 588 | if (!ndev) |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 589 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 590 | |
| 591 | ndev->ops = ops; |
| 592 | ndev->tx_headroom = tx_headroom; |
| 593 | ndev->tx_tailroom = tx_tailroom; |
| 594 | |
| 595 | ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 596 | supported_protocols, |
| 597 | tx_headroom + NCI_DATA_HDR_SIZE, |
| 598 | tx_tailroom); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 599 | if (!ndev->nfc_dev) |
| 600 | goto free_exit; |
| 601 | |
| 602 | nfc_set_drvdata(ndev->nfc_dev, ndev); |
| 603 | |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 604 | return ndev; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 605 | |
| 606 | free_exit: |
| 607 | kfree(ndev); |
Dan Carpenter | 8ebafde | 2011-09-23 09:14:35 +0300 | [diff] [blame] | 608 | return NULL; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 609 | } |
| 610 | EXPORT_SYMBOL(nci_allocate_device); |
| 611 | |
| 612 | /** |
| 613 | * nci_free_device - deallocate nci device |
| 614 | * |
| 615 | * @ndev: The nci device to deallocate |
| 616 | */ |
| 617 | void nci_free_device(struct nci_dev *ndev) |
| 618 | { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 619 | nfc_free_device(ndev->nfc_dev); |
| 620 | kfree(ndev); |
| 621 | } |
| 622 | EXPORT_SYMBOL(nci_free_device); |
| 623 | |
| 624 | /** |
| 625 | * nci_register_device - register a nci device in the nfc subsystem |
| 626 | * |
| 627 | * @dev: The nci device to register |
| 628 | */ |
| 629 | int nci_register_device(struct nci_dev *ndev) |
| 630 | { |
| 631 | int rc; |
| 632 | struct device *dev = &ndev->nfc_dev->dev; |
| 633 | char name[32]; |
| 634 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 635 | rc = nfc_register_device(ndev->nfc_dev); |
| 636 | if (rc) |
| 637 | goto exit; |
| 638 | |
| 639 | ndev->flags = 0; |
| 640 | |
| 641 | INIT_WORK(&ndev->cmd_work, nci_cmd_work); |
| 642 | snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev)); |
| 643 | ndev->cmd_wq = create_singlethread_workqueue(name); |
| 644 | if (!ndev->cmd_wq) { |
| 645 | rc = -ENOMEM; |
| 646 | goto unreg_exit; |
| 647 | } |
| 648 | |
| 649 | INIT_WORK(&ndev->rx_work, nci_rx_work); |
| 650 | snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev)); |
| 651 | ndev->rx_wq = create_singlethread_workqueue(name); |
| 652 | if (!ndev->rx_wq) { |
| 653 | rc = -ENOMEM; |
| 654 | goto destroy_cmd_wq_exit; |
| 655 | } |
| 656 | |
| 657 | INIT_WORK(&ndev->tx_work, nci_tx_work); |
| 658 | snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev)); |
| 659 | ndev->tx_wq = create_singlethread_workqueue(name); |
| 660 | if (!ndev->tx_wq) { |
| 661 | rc = -ENOMEM; |
| 662 | goto destroy_rx_wq_exit; |
| 663 | } |
| 664 | |
| 665 | skb_queue_head_init(&ndev->cmd_q); |
| 666 | skb_queue_head_init(&ndev->rx_q); |
| 667 | skb_queue_head_init(&ndev->tx_q); |
| 668 | |
| 669 | setup_timer(&ndev->cmd_timer, nci_cmd_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 670 | (unsigned long) ndev); |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 671 | setup_timer(&ndev->data_timer, nci_data_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 672 | (unsigned long) ndev); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 673 | |
| 674 | mutex_init(&ndev->req_lock); |
| 675 | |
| 676 | goto exit; |
| 677 | |
| 678 | destroy_rx_wq_exit: |
| 679 | destroy_workqueue(ndev->rx_wq); |
| 680 | |
| 681 | destroy_cmd_wq_exit: |
| 682 | destroy_workqueue(ndev->cmd_wq); |
| 683 | |
| 684 | unreg_exit: |
| 685 | nfc_unregister_device(ndev->nfc_dev); |
| 686 | |
| 687 | exit: |
| 688 | return rc; |
| 689 | } |
| 690 | EXPORT_SYMBOL(nci_register_device); |
| 691 | |
| 692 | /** |
| 693 | * nci_unregister_device - unregister a nci device in the nfc subsystem |
| 694 | * |
| 695 | * @dev: The nci device to unregister |
| 696 | */ |
| 697 | void nci_unregister_device(struct nci_dev *ndev) |
| 698 | { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 699 | nci_close_device(ndev); |
| 700 | |
| 701 | destroy_workqueue(ndev->cmd_wq); |
| 702 | destroy_workqueue(ndev->rx_wq); |
| 703 | destroy_workqueue(ndev->tx_wq); |
| 704 | |
| 705 | nfc_unregister_device(ndev->nfc_dev); |
| 706 | } |
| 707 | EXPORT_SYMBOL(nci_unregister_device); |
| 708 | |
| 709 | /** |
| 710 | * nci_recv_frame - receive frame from NCI drivers |
| 711 | * |
| 712 | * @skb: The sk_buff to receive |
| 713 | */ |
| 714 | int nci_recv_frame(struct sk_buff *skb) |
| 715 | { |
| 716 | struct nci_dev *ndev = (struct nci_dev *) skb->dev; |
| 717 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 718 | pr_debug("len %d\n", skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 719 | |
| 720 | if (!ndev || (!test_bit(NCI_UP, &ndev->flags) |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 721 | && !test_bit(NCI_INIT, &ndev->flags))) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 722 | kfree_skb(skb); |
| 723 | return -ENXIO; |
| 724 | } |
| 725 | |
| 726 | /* Queue frame for rx worker thread */ |
| 727 | skb_queue_tail(&ndev->rx_q, skb); |
| 728 | queue_work(ndev->rx_wq, &ndev->rx_work); |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | EXPORT_SYMBOL(nci_recv_frame); |
| 733 | |
| 734 | static int nci_send_frame(struct sk_buff *skb) |
| 735 | { |
| 736 | struct nci_dev *ndev = (struct nci_dev *) skb->dev; |
| 737 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 738 | pr_debug("len %d\n", skb->len); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 739 | |
| 740 | if (!ndev) { |
| 741 | kfree_skb(skb); |
| 742 | return -ENODEV; |
| 743 | } |
| 744 | |
| 745 | /* Get rid of skb owner, prior to sending to the driver. */ |
| 746 | skb_orphan(skb); |
| 747 | |
| 748 | return ndev->ops->send(skb); |
| 749 | } |
| 750 | |
| 751 | /* Send NCI command */ |
| 752 | int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload) |
| 753 | { |
| 754 | struct nci_ctrl_hdr *hdr; |
| 755 | struct sk_buff *skb; |
| 756 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 757 | pr_debug("opcode 0x%x, plen %d\n", opcode, plen); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 758 | |
| 759 | skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL); |
| 760 | if (!skb) { |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 761 | pr_err("no memory for command\n"); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 762 | return -ENOMEM; |
| 763 | } |
| 764 | |
| 765 | hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE); |
| 766 | hdr->gid = nci_opcode_gid(opcode); |
| 767 | hdr->oid = nci_opcode_oid(opcode); |
| 768 | hdr->plen = plen; |
| 769 | |
| 770 | nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT); |
| 771 | nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST); |
| 772 | |
| 773 | if (plen) |
| 774 | memcpy(skb_put(skb, plen), payload, plen); |
| 775 | |
| 776 | skb->dev = (void *) ndev; |
| 777 | |
| 778 | skb_queue_tail(&ndev->cmd_q, skb); |
| 779 | queue_work(ndev->cmd_wq, &ndev->cmd_work); |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /* ---- NCI TX Data worker thread ---- */ |
| 785 | |
| 786 | static void nci_tx_work(struct work_struct *work) |
| 787 | { |
| 788 | struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work); |
| 789 | struct sk_buff *skb; |
| 790 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 791 | pr_debug("credits_cnt %d\n", atomic_read(&ndev->credits_cnt)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 792 | |
| 793 | /* Send queued tx data */ |
| 794 | while (atomic_read(&ndev->credits_cnt)) { |
| 795 | skb = skb_dequeue(&ndev->tx_q); |
| 796 | if (!skb) |
| 797 | return; |
| 798 | |
Ilan Elias | db98c82 | 2011-11-09 12:09:16 +0200 | [diff] [blame] | 799 | /* Check if data flow control is used */ |
| 800 | if (atomic_read(&ndev->credits_cnt) != |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 801 | NCI_DATA_FLOW_CONTROL_NOT_USED) |
Ilan Elias | db98c82 | 2011-11-09 12:09:16 +0200 | [diff] [blame] | 802 | atomic_dec(&ndev->credits_cnt); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 803 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 804 | pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n", |
| 805 | nci_pbf(skb->data), |
| 806 | nci_conn_id(skb->data), |
| 807 | nci_plen(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 808 | |
| 809 | nci_send_frame(skb); |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 810 | |
| 811 | mod_timer(&ndev->data_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 812 | jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
| 816 | /* ----- NCI RX worker thread (data & control) ----- */ |
| 817 | |
| 818 | static void nci_rx_work(struct work_struct *work) |
| 819 | { |
| 820 | struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work); |
| 821 | struct sk_buff *skb; |
| 822 | |
| 823 | while ((skb = skb_dequeue(&ndev->rx_q))) { |
| 824 | /* Process frame */ |
| 825 | switch (nci_mt(skb->data)) { |
| 826 | case NCI_MT_RSP_PKT: |
| 827 | nci_rsp_packet(ndev, skb); |
| 828 | break; |
| 829 | |
| 830 | case NCI_MT_NTF_PKT: |
| 831 | nci_ntf_packet(ndev, skb); |
| 832 | break; |
| 833 | |
| 834 | case NCI_MT_DATA_PKT: |
| 835 | nci_rx_data_packet(ndev, skb); |
| 836 | break; |
| 837 | |
| 838 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 839 | pr_err("unknown MT 0x%x\n", nci_mt(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 840 | kfree_skb(skb); |
| 841 | break; |
| 842 | } |
| 843 | } |
Ilan Elias | c4bf98b | 2012-01-17 12:03:50 +0200 | [diff] [blame] | 844 | |
| 845 | /* check if a data exchange timout has occurred */ |
| 846 | if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) { |
| 847 | /* complete the data exchange transaction, if exists */ |
| 848 | if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags)) |
| 849 | nci_data_exchange_complete(ndev, NULL, -ETIMEDOUT); |
| 850 | |
| 851 | clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags); |
| 852 | } |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /* ----- NCI TX CMD worker thread ----- */ |
| 856 | |
| 857 | static void nci_cmd_work(struct work_struct *work) |
| 858 | { |
| 859 | struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work); |
| 860 | struct sk_buff *skb; |
| 861 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 862 | pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 863 | |
| 864 | /* Send queued command */ |
| 865 | if (atomic_read(&ndev->cmd_cnt)) { |
| 866 | skb = skb_dequeue(&ndev->cmd_q); |
| 867 | if (!skb) |
| 868 | return; |
| 869 | |
| 870 | atomic_dec(&ndev->cmd_cnt); |
| 871 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 872 | pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n", |
| 873 | nci_pbf(skb->data), |
| 874 | nci_opcode_gid(nci_opcode(skb->data)), |
| 875 | nci_opcode_oid(nci_opcode(skb->data)), |
| 876 | nci_plen(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 877 | |
| 878 | nci_send_frame(skb); |
| 879 | |
| 880 | mod_timer(&ndev->cmd_timer, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame] | 881 | jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 882 | } |
| 883 | } |
Dave Jones | 8a70e7f | 2012-07-12 19:17:34 +0200 | [diff] [blame] | 884 | |
| 885 | MODULE_LICENSE("GPL"); |