1 /* 2 * Copyright (C) 2011 Instituto Nokia de Tecnologia 3 * 4 * Authors: 5 * Lauro Ramos Venancio <lauro.venancio@openbossa.org> 6 * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the 20 * Free Software Foundation, Inc., 21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 22 */ 23 24 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ 25 26 #include <linux/init.h> 27 #include <linux/kernel.h> 28 #include <linux/module.h> 29 #include <linux/slab.h> 30 #include <linux/nfc.h> 31 32 #include "nfc.h" 33 34 #define VERSION "0.1" 35 36 #define NFC_CHECK_PRES_FREQ_MS 2000 37 38 int nfc_devlist_generation; 39 DEFINE_MUTEX(nfc_devlist_mutex); 40 41 /** 42 * nfc_dev_up - turn on the NFC device 43 * 44 * @dev: The nfc device to be turned on 45 * 46 * The device remains up until the nfc_dev_down function is called. 47 */ 48 int nfc_dev_up(struct nfc_dev *dev) 49 { 50 int rc = 0; 51 52 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 53 54 device_lock(&dev->dev); 55 56 if (!device_is_registered(&dev->dev)) { 57 rc = -ENODEV; 58 goto error; 59 } 60 61 if (dev->dev_up) { 62 rc = -EALREADY; 63 goto error; 64 } 65 66 if (dev->ops->dev_up) 67 rc = dev->ops->dev_up(dev); 68 69 if (!rc) 70 dev->dev_up = true; 71 72 error: 73 device_unlock(&dev->dev); 74 return rc; 75 } 76 77 /** 78 * nfc_dev_down - turn off the NFC device 79 * 80 * @dev: The nfc device to be turned off 81 */ 82 int nfc_dev_down(struct nfc_dev *dev) 83 { 84 int rc = 0; 85 86 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 87 88 device_lock(&dev->dev); 89 90 if (!device_is_registered(&dev->dev)) { 91 rc = -ENODEV; 92 goto error; 93 } 94 95 if (!dev->dev_up) { 96 rc = -EALREADY; 97 goto error; 98 } 99 100 if (dev->polling || dev->activated_target_idx != NFC_TARGET_IDX_NONE) { 101 rc = -EBUSY; 102 goto error; 103 } 104 105 if (dev->ops->dev_down) 106 dev->ops->dev_down(dev); 107 108 dev->dev_up = false; 109 110 error: 111 device_unlock(&dev->dev); 112 return rc; 113 } 114 115 /** 116 * nfc_start_poll - start polling for nfc targets 117 * 118 * @dev: The nfc device that must start polling 119 * @protocols: bitset of nfc protocols that must be used for polling 120 * 121 * The device remains polling for targets until a target is found or 122 * the nfc_stop_poll function is called. 123 */ 124 int nfc_start_poll(struct nfc_dev *dev, u32 protocols) 125 { 126 int rc; 127 128 pr_debug("dev_name=%s protocols=0x%x\n", 129 dev_name(&dev->dev), protocols); 130 131 if (!protocols) 132 return -EINVAL; 133 134 device_lock(&dev->dev); 135 136 if (!device_is_registered(&dev->dev)) { 137 rc = -ENODEV; 138 goto error; 139 } 140 141 if (dev->polling) { 142 rc = -EBUSY; 143 goto error; 144 } 145 146 rc = dev->ops->start_poll(dev, protocols); 147 if (!rc) 148 dev->polling = true; 149 150 error: 151 device_unlock(&dev->dev); 152 return rc; 153 } 154 155 /** 156 * nfc_stop_poll - stop polling for nfc targets 157 * 158 * @dev: The nfc device that must stop polling 159 */ 160 int nfc_stop_poll(struct nfc_dev *dev) 161 { 162 int rc = 0; 163 164 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 165 166 device_lock(&dev->dev); 167 168 if (!device_is_registered(&dev->dev)) { 169 rc = -ENODEV; 170 goto error; 171 } 172 173 if (!dev->polling) { 174 rc = -EINVAL; 175 goto error; 176 } 177 178 dev->ops->stop_poll(dev); 179 dev->polling = false; 180 181 error: 182 device_unlock(&dev->dev); 183 return rc; 184 } 185 186 int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode) 187 { 188 int rc = 0; 189 u8 *gb; 190 size_t gb_len; 191 192 pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode); 193 194 if (!dev->ops->dep_link_up) 195 return -EOPNOTSUPP; 196 197 device_lock(&dev->dev); 198 199 if (!device_is_registered(&dev->dev)) { 200 rc = -ENODEV; 201 goto error; 202 } 203 204 if (dev->dep_link_up == true) { 205 rc = -EALREADY; 206 goto error; 207 } 208 209 gb = nfc_llcp_general_bytes(dev, &gb_len); 210 if (gb_len > NFC_MAX_GT_LEN) { 211 rc = -EINVAL; 212 goto error; 213 } 214 215 rc = dev->ops->dep_link_up(dev, target_index, comm_mode, gb, gb_len); 216 if (!rc) 217 dev->activated_target_idx = target_index; 218 219 error: 220 device_unlock(&dev->dev); 221 return rc; 222 } 223 224 int nfc_dep_link_down(struct nfc_dev *dev) 225 { 226 int rc = 0; 227 228 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 229 230 if (!dev->ops->dep_link_down) 231 return -EOPNOTSUPP; 232 233 device_lock(&dev->dev); 234 235 if (!device_is_registered(&dev->dev)) { 236 rc = -ENODEV; 237 goto error; 238 } 239 240 if (dev->dep_link_up == false) { 241 rc = -EALREADY; 242 goto error; 243 } 244 245 if (dev->dep_rf_mode == NFC_RF_TARGET) { 246 rc = -EOPNOTSUPP; 247 goto error; 248 } 249 250 rc = dev->ops->dep_link_down(dev); 251 if (!rc) { 252 dev->dep_link_up = false; 253 dev->activated_target_idx = NFC_TARGET_IDX_NONE; 254 nfc_llcp_mac_is_down(dev); 255 nfc_genl_dep_link_down_event(dev); 256 } 257 258 error: 259 device_unlock(&dev->dev); 260 return rc; 261 } 262 263 int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx, 264 u8 comm_mode, u8 rf_mode) 265 { 266 dev->dep_link_up = true; 267 dev->dep_rf_mode = rf_mode; 268 269 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode); 270 271 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode); 272 } 273 EXPORT_SYMBOL(nfc_dep_link_is_up); 274 275 /** 276 * nfc_activate_target - prepare the target for data exchange 277 * 278 * @dev: The nfc device that found the target 279 * @target_idx: index of the target that must be activated 280 * @protocol: nfc protocol that will be used for data exchange 281 */ 282 int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol) 283 { 284 int rc; 285 286 pr_debug("dev_name=%s target_idx=%u protocol=%u\n", 287 dev_name(&dev->dev), target_idx, protocol); 288 289 device_lock(&dev->dev); 290 291 if (!device_is_registered(&dev->dev)) { 292 rc = -ENODEV; 293 goto error; 294 } 295 296 rc = dev->ops->activate_target(dev, target_idx, protocol); 297 if (!rc) { 298 dev->activated_target_idx = target_idx; 299 300 if (dev->ops->check_presence) 301 mod_timer(&dev->check_pres_timer, jiffies + 302 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); 303 } 304 305 error: 306 device_unlock(&dev->dev); 307 return rc; 308 } 309 310 /** 311 * nfc_deactivate_target - deactivate a nfc target 312 * 313 * @dev: The nfc device that found the target 314 * @target_idx: index of the target that must be deactivated 315 */ 316 int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx) 317 { 318 int rc = 0; 319 320 pr_debug("dev_name=%s target_idx=%u\n", 321 dev_name(&dev->dev), target_idx); 322 323 device_lock(&dev->dev); 324 325 if (!device_is_registered(&dev->dev)) { 326 rc = -ENODEV; 327 goto error; 328 } 329 330 if (dev->ops->check_presence) 331 del_timer_sync(&dev->check_pres_timer); 332 333 dev->ops->deactivate_target(dev, target_idx); 334 dev->activated_target_idx = NFC_TARGET_IDX_NONE; 335 336 error: 337 device_unlock(&dev->dev); 338 return rc; 339 } 340 341 /** 342 * nfc_data_exchange - transceive data 343 * 344 * @dev: The nfc device that found the target 345 * @target_idx: index of the target 346 * @skb: data to be sent 347 * @cb: callback called when the response is received 348 * @cb_context: parameter for the callback function 349 * 350 * The user must wait for the callback before calling this function again. 351 */ 352 int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb, 353 data_exchange_cb_t cb, void *cb_context) 354 { 355 int rc; 356 357 pr_debug("dev_name=%s target_idx=%u skb->len=%u\n", 358 dev_name(&dev->dev), target_idx, skb->len); 359 360 device_lock(&dev->dev); 361 362 if (!device_is_registered(&dev->dev)) { 363 rc = -ENODEV; 364 kfree_skb(skb); 365 goto error; 366 } 367 368 if (dev->activated_target_idx == NFC_TARGET_IDX_NONE) { 369 rc = -ENOTCONN; 370 kfree_skb(skb); 371 goto error; 372 } 373 374 if (target_idx != dev->activated_target_idx) { 375 rc = -EADDRNOTAVAIL; 376 kfree_skb(skb); 377 goto error; 378 } 379 380 if (dev->ops->check_presence) 381 del_timer_sync(&dev->check_pres_timer); 382 383 rc = dev->ops->data_exchange(dev, target_idx, skb, cb, cb_context); 384 385 if (!rc && dev->ops->check_presence) 386 mod_timer(&dev->check_pres_timer, jiffies + 387 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); 388 389 error: 390 device_unlock(&dev->dev); 391 return rc; 392 } 393 394 int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len) 395 { 396 pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len); 397 398 if (gb_len > NFC_MAX_GT_LEN) 399 return -EINVAL; 400 401 return nfc_llcp_set_remote_gb(dev, gb, gb_len); 402 } 403 EXPORT_SYMBOL(nfc_set_remote_general_bytes); 404 405 /** 406 * nfc_alloc_send_skb - allocate a skb for data exchange responses 407 * 408 * @size: size to allocate 409 * @gfp: gfp flags 410 */ 411 struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk, 412 unsigned int flags, unsigned int size, 413 unsigned int *err) 414 { 415 struct sk_buff *skb; 416 unsigned int total_size; 417 418 total_size = size + 419 dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; 420 421 skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err); 422 if (skb) 423 skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); 424 425 return skb; 426 } 427 428 /** 429 * nfc_alloc_recv_skb - allocate a skb for data exchange responses 430 * 431 * @size: size to allocate 432 * @gfp: gfp flags 433 */ 434 struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp) 435 { 436 struct sk_buff *skb; 437 unsigned int total_size; 438 439 total_size = size + 1; 440 skb = alloc_skb(total_size, gfp); 441 442 if (skb) 443 skb_reserve(skb, 1); 444 445 return skb; 446 } 447 EXPORT_SYMBOL(nfc_alloc_recv_skb); 448 449 /** 450 * nfc_targets_found - inform that targets were found 451 * 452 * @dev: The nfc device that found the targets 453 * @targets: array of nfc targets found 454 * @ntargets: targets array size 455 * 456 * The device driver must call this function when one or many nfc targets 457 * are found. After calling this function, the device driver must stop 458 * polling for targets. 459 */ 460 int nfc_targets_found(struct nfc_dev *dev, 461 struct nfc_target *targets, int n_targets) 462 { 463 int i; 464 465 pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets); 466 467 dev->polling = false; 468 469 for (i = 0; i < n_targets; i++) 470 targets[i].idx = dev->target_next_idx++; 471 472 spin_lock_bh(&dev->targets_lock); 473 474 dev->targets_generation++; 475 476 kfree(dev->targets); 477 dev->targets = kmemdup(targets, n_targets * sizeof(struct nfc_target), 478 GFP_ATOMIC); 479 480 if (!dev->targets) { 481 dev->n_targets = 0; 482 spin_unlock_bh(&dev->targets_lock); 483 return -ENOMEM; 484 } 485 486 dev->n_targets = n_targets; 487 spin_unlock_bh(&dev->targets_lock); 488 489 nfc_genl_targets_found(dev); 490 491 return 0; 492 } 493 EXPORT_SYMBOL(nfc_targets_found); 494 495 int nfc_target_lost(struct nfc_dev *dev, u32 target_idx) 496 { 497 struct nfc_target *tg; 498 int i; 499 500 pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx); 501 502 spin_lock_bh(&dev->targets_lock); 503 504 for (i = 0; i < dev->n_targets; i++) { 505 tg = &dev->targets[i]; 506 if (tg->idx == target_idx) 507 break; 508 } 509 510 if (i == dev->n_targets) { 511 spin_unlock_bh(&dev->targets_lock); 512 return -EINVAL; 513 } 514 515 dev->targets_generation++; 516 dev->n_targets--; 517 dev->activated_target_idx = NFC_TARGET_IDX_NONE; 518 519 if (dev->n_targets) { 520 memcpy(&dev->targets[i], &dev->targets[i + 1], 521 (dev->n_targets - i) * sizeof(struct nfc_target)); 522 } else { 523 kfree(dev->targets); 524 dev->targets = NULL; 525 } 526 527 spin_unlock_bh(&dev->targets_lock); 528 529 nfc_genl_target_lost(dev, target_idx); 530 531 return 0; 532 } 533 EXPORT_SYMBOL(nfc_target_lost); 534 535 static void nfc_release(struct device *d) 536 { 537 struct nfc_dev *dev = to_nfc_dev(d); 538 539 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 540 541 if (dev->ops->check_presence) { 542 del_timer_sync(&dev->check_pres_timer); 543 destroy_workqueue(dev->check_pres_wq); 544 } 545 546 nfc_genl_data_exit(&dev->genl_data); 547 kfree(dev->targets); 548 kfree(dev); 549 } 550 551 static void nfc_check_pres_work(struct work_struct *work) 552 { 553 struct nfc_dev *dev = container_of(work, struct nfc_dev, 554 check_pres_work); 555 int rc; 556 557 device_lock(&dev->dev); 558 559 if (dev->activated_target_idx != NFC_TARGET_IDX_NONE && 560 timer_pending(&dev->check_pres_timer) == 0) { 561 rc = dev->ops->check_presence(dev, dev->activated_target_idx); 562 if (!rc) { 563 mod_timer(&dev->check_pres_timer, jiffies + 564 msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS)); 565 } else { 566 nfc_target_lost(dev, dev->activated_target_idx); 567 dev->activated_target_idx = NFC_TARGET_IDX_NONE; 568 } 569 } 570 571 device_unlock(&dev->dev); 572 } 573 574 static void nfc_check_pres_timeout(unsigned long data) 575 { 576 struct nfc_dev *dev = (struct nfc_dev *)data; 577 578 queue_work(dev->check_pres_wq, &dev->check_pres_work); 579 } 580 581 struct class nfc_class = { 582 .name = "nfc", 583 .dev_release = nfc_release, 584 }; 585 EXPORT_SYMBOL(nfc_class); 586 587 static int match_idx(struct device *d, void *data) 588 { 589 struct nfc_dev *dev = to_nfc_dev(d); 590 unsigned *idx = data; 591 592 return dev->idx == *idx; 593 } 594 595 struct nfc_dev *nfc_get_device(unsigned idx) 596 { 597 struct device *d; 598 599 d = class_find_device(&nfc_class, NULL, &idx, match_idx); 600 if (!d) 601 return NULL; 602 603 return to_nfc_dev(d); 604 } 605 606 /** 607 * nfc_allocate_device - allocate a new nfc device 608 * 609 * @ops: device operations 610 * @supported_protocols: NFC protocols supported by the device 611 */ 612 struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops, 613 u32 supported_protocols, 614 int tx_headroom, int tx_tailroom) 615 { 616 static atomic_t dev_no = ATOMIC_INIT(0); 617 struct nfc_dev *dev; 618 619 if (!ops->start_poll || !ops->stop_poll || !ops->activate_target || 620 !ops->deactivate_target || !ops->data_exchange) 621 return NULL; 622 623 if (!supported_protocols) 624 return NULL; 625 626 dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL); 627 if (!dev) 628 return NULL; 629 630 dev->dev.class = &nfc_class; 631 dev->idx = atomic_inc_return(&dev_no) - 1; 632 dev_set_name(&dev->dev, "nfc%d", dev->idx); 633 device_initialize(&dev->dev); 634 635 dev->ops = ops; 636 dev->supported_protocols = supported_protocols; 637 dev->tx_headroom = tx_headroom; 638 dev->tx_tailroom = tx_tailroom; 639 640 spin_lock_init(&dev->targets_lock); 641 nfc_genl_data_init(&dev->genl_data); 642 643 /* first generation must not be 0 */ 644 dev->targets_generation = 1; 645 646 dev->activated_target_idx = NFC_TARGET_IDX_NONE; 647 648 if (ops->check_presence) { 649 char name[32]; 650 init_timer(&dev->check_pres_timer); 651 dev->check_pres_timer.data = (unsigned long)dev; 652 dev->check_pres_timer.function = nfc_check_pres_timeout; 653 654 INIT_WORK(&dev->check_pres_work, nfc_check_pres_work); 655 snprintf(name, sizeof(name), "nfc%d_check_pres_wq", dev->idx); 656 dev->check_pres_wq = alloc_workqueue(name, WQ_NON_REENTRANT | 657 WQ_UNBOUND | 658 WQ_MEM_RECLAIM, 1); 659 if (dev->check_pres_wq == NULL) { 660 kfree(dev); 661 return NULL; 662 } 663 } 664 665 666 return dev; 667 } 668 EXPORT_SYMBOL(nfc_allocate_device); 669 670 /** 671 * nfc_register_device - register a nfc device in the nfc subsystem 672 * 673 * @dev: The nfc device to register 674 */ 675 int nfc_register_device(struct nfc_dev *dev) 676 { 677 int rc; 678 679 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 680 681 mutex_lock(&nfc_devlist_mutex); 682 nfc_devlist_generation++; 683 rc = device_add(&dev->dev); 684 mutex_unlock(&nfc_devlist_mutex); 685 686 if (rc < 0) 687 return rc; 688 689 rc = nfc_llcp_register_device(dev); 690 if (rc) 691 pr_err("Could not register llcp device\n"); 692 693 rc = nfc_genl_device_added(dev); 694 if (rc) 695 pr_debug("The userspace won't be notified that the device %s was added\n", 696 dev_name(&dev->dev)); 697 698 return 0; 699 } 700 EXPORT_SYMBOL(nfc_register_device); 701 702 /** 703 * nfc_unregister_device - unregister a nfc device in the nfc subsystem 704 * 705 * @dev: The nfc device to unregister 706 */ 707 void nfc_unregister_device(struct nfc_dev *dev) 708 { 709 int rc; 710 711 pr_debug("dev_name=%s\n", dev_name(&dev->dev)); 712 713 mutex_lock(&nfc_devlist_mutex); 714 nfc_devlist_generation++; 715 716 /* lock to avoid unregistering a device while an operation 717 is in progress */ 718 device_lock(&dev->dev); 719 device_del(&dev->dev); 720 device_unlock(&dev->dev); 721 722 mutex_unlock(&nfc_devlist_mutex); 723 724 nfc_llcp_unregister_device(dev); 725 726 rc = nfc_genl_device_removed(dev); 727 if (rc) 728 pr_debug("The userspace won't be notified that the device %s was removed\n", 729 dev_name(&dev->dev)); 730 731 } 732 EXPORT_SYMBOL(nfc_unregister_device); 733 734 static int __init nfc_init(void) 735 { 736 int rc; 737 738 pr_info("NFC Core ver %s\n", VERSION); 739 740 rc = class_register(&nfc_class); 741 if (rc) 742 return rc; 743 744 rc = nfc_genl_init(); 745 if (rc) 746 goto err_genl; 747 748 /* the first generation must not be 0 */ 749 nfc_devlist_generation = 1; 750 751 rc = rawsock_init(); 752 if (rc) 753 goto err_rawsock; 754 755 rc = nfc_llcp_init(); 756 if (rc) 757 goto err_llcp_sock; 758 759 rc = af_nfc_init(); 760 if (rc) 761 goto err_af_nfc; 762 763 return 0; 764 765 err_af_nfc: 766 nfc_llcp_exit(); 767 err_llcp_sock: 768 rawsock_exit(); 769 err_rawsock: 770 nfc_genl_exit(); 771 err_genl: 772 class_unregister(&nfc_class); 773 return rc; 774 } 775 776 static void __exit nfc_exit(void) 777 { 778 af_nfc_exit(); 779 nfc_llcp_exit(); 780 rawsock_exit(); 781 nfc_genl_exit(); 782 class_unregister(&nfc_class); 783 } 784 785 subsys_initcall(nfc_init); 786 module_exit(nfc_exit); 787 788 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>"); 789 MODULE_DESCRIPTION("NFC Core ver " VERSION); 790 MODULE_VERSION(VERSION); 791 MODULE_LICENSE("GPL"); 792