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