Linux Audio

Check our new training course

Loading...
v4.10.11
 
   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, see <http://www.gnu.org/licenses/>.
  20 */
  21
  22#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  23
  24#include <linux/init.h>
  25#include <linux/kernel.h>
  26#include <linux/module.h>
  27#include <linux/slab.h>
  28#include <linux/rfkill.h>
  29#include <linux/nfc.h>
  30
  31#include <net/genetlink.h>
  32
  33#include "nfc.h"
  34
  35#define VERSION "0.1"
  36
  37#define NFC_CHECK_PRES_FREQ_MS	2000
  38
  39int nfc_devlist_generation;
  40DEFINE_MUTEX(nfc_devlist_mutex);
  41
  42/* NFC device ID bitmap */
  43static DEFINE_IDA(nfc_index_ida);
  44
  45int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
  46{
  47	int rc = 0;
  48
  49	pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
  50
  51	device_lock(&dev->dev);
  52
  53	if (!device_is_registered(&dev->dev)) {
  54		rc = -ENODEV;
  55		goto error;
  56	}
  57
  58	if (dev->dev_up) {
  59		rc = -EBUSY;
  60		goto error;
  61	}
  62
  63	if (!dev->ops->fw_download) {
  64		rc = -EOPNOTSUPP;
  65		goto error;
  66	}
  67
  68	dev->fw_download_in_progress = true;
  69	rc = dev->ops->fw_download(dev, firmware_name);
  70	if (rc)
  71		dev->fw_download_in_progress = false;
  72
  73error:
  74	device_unlock(&dev->dev);
  75	return rc;
  76}
  77
  78/**
  79 * nfc_fw_download_done - inform that a firmware download was completed
  80 *
  81 * @dev: The nfc device to which firmware was downloaded
  82 * @firmware_name: The firmware filename
  83 * @result: The positive value of a standard errno value
  84 */
  85int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
  86			 u32 result)
  87{
  88	dev->fw_download_in_progress = false;
  89
  90	return nfc_genl_fw_download_done(dev, firmware_name, result);
  91}
  92EXPORT_SYMBOL(nfc_fw_download_done);
  93
  94/**
  95 * nfc_dev_up - turn on the NFC device
  96 *
  97 * @dev: The nfc device to be turned on
  98 *
  99 * The device remains up until the nfc_dev_down function is called.
 100 */
 101int nfc_dev_up(struct nfc_dev *dev)
 102{
 103	int rc = 0;
 104
 105	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 106
 107	device_lock(&dev->dev);
 108
 109	if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
 110		rc = -ERFKILL;
 111		goto error;
 112	}
 113
 114	if (!device_is_registered(&dev->dev)) {
 115		rc = -ENODEV;
 116		goto error;
 117	}
 118
 119	if (dev->fw_download_in_progress) {
 120		rc = -EBUSY;
 121		goto error;
 122	}
 123
 124	if (dev->dev_up) {
 125		rc = -EALREADY;
 126		goto error;
 127	}
 128
 129	if (dev->ops->dev_up)
 130		rc = dev->ops->dev_up(dev);
 131
 132	if (!rc)
 133		dev->dev_up = true;
 134
 135	/* We have to enable the device before discovering SEs */
 136	if (dev->ops->discover_se && dev->ops->discover_se(dev))
 137		pr_err("SE discovery failed\n");
 138
 139error:
 140	device_unlock(&dev->dev);
 141	return rc;
 142}
 143
 144/**
 145 * nfc_dev_down - turn off the NFC device
 146 *
 147 * @dev: The nfc device to be turned off
 148 */
 149int nfc_dev_down(struct nfc_dev *dev)
 150{
 151	int rc = 0;
 152
 153	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 154
 155	device_lock(&dev->dev);
 156
 157	if (!device_is_registered(&dev->dev)) {
 158		rc = -ENODEV;
 159		goto error;
 160	}
 161
 162	if (!dev->dev_up) {
 163		rc = -EALREADY;
 164		goto error;
 165	}
 166
 167	if (dev->polling || dev->active_target) {
 168		rc = -EBUSY;
 169		goto error;
 170	}
 171
 172	if (dev->ops->dev_down)
 173		dev->ops->dev_down(dev);
 174
 175	dev->dev_up = false;
 176
 177error:
 178	device_unlock(&dev->dev);
 179	return rc;
 180}
 181
 182static int nfc_rfkill_set_block(void *data, bool blocked)
 183{
 184	struct nfc_dev *dev = data;
 185
 186	pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
 187
 188	if (!blocked)
 189		return 0;
 190
 191	nfc_dev_down(dev);
 192
 193	return 0;
 194}
 195
 196static const struct rfkill_ops nfc_rfkill_ops = {
 197	.set_block = nfc_rfkill_set_block,
 198};
 199
 200/**
 201 * nfc_start_poll - start polling for nfc targets
 202 *
 203 * @dev: The nfc device that must start polling
 204 * @protocols: bitset of nfc protocols that must be used for polling
 205 *
 206 * The device remains polling for targets until a target is found or
 207 * the nfc_stop_poll function is called.
 208 */
 209int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
 210{
 211	int rc;
 212
 213	pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
 214		 dev_name(&dev->dev), im_protocols, tm_protocols);
 215
 216	if (!im_protocols && !tm_protocols)
 217		return -EINVAL;
 218
 219	device_lock(&dev->dev);
 220
 221	if (!device_is_registered(&dev->dev)) {
 222		rc = -ENODEV;
 223		goto error;
 224	}
 225
 226	if (!dev->dev_up) {
 227		rc = -ENODEV;
 228		goto error;
 229	}
 230
 231	if (dev->polling) {
 232		rc = -EBUSY;
 233		goto error;
 234	}
 235
 236	rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
 237	if (!rc) {
 238		dev->polling = true;
 239		dev->rf_mode = NFC_RF_NONE;
 240	}
 241
 242error:
 243	device_unlock(&dev->dev);
 244	return rc;
 245}
 246
 247/**
 248 * nfc_stop_poll - stop polling for nfc targets
 249 *
 250 * @dev: The nfc device that must stop polling
 251 */
 252int nfc_stop_poll(struct nfc_dev *dev)
 253{
 254	int rc = 0;
 255
 256	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 257
 258	device_lock(&dev->dev);
 259
 260	if (!device_is_registered(&dev->dev)) {
 261		rc = -ENODEV;
 262		goto error;
 263	}
 264
 265	if (!dev->polling) {
 266		rc = -EINVAL;
 267		goto error;
 268	}
 269
 270	dev->ops->stop_poll(dev);
 271	dev->polling = false;
 272	dev->rf_mode = NFC_RF_NONE;
 273
 274error:
 275	device_unlock(&dev->dev);
 276	return rc;
 277}
 278
 279static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
 280{
 281	int i;
 282
 283	for (i = 0; i < dev->n_targets; i++) {
 284		if (dev->targets[i].idx == target_idx)
 285			return &dev->targets[i];
 286	}
 287
 288	return NULL;
 289}
 290
 291int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
 292{
 293	int rc = 0;
 294	u8 *gb;
 295	size_t gb_len;
 296	struct nfc_target *target;
 297
 298	pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
 299
 300	if (!dev->ops->dep_link_up)
 301		return -EOPNOTSUPP;
 302
 303	device_lock(&dev->dev);
 304
 305	if (!device_is_registered(&dev->dev)) {
 306		rc = -ENODEV;
 307		goto error;
 308	}
 309
 310	if (dev->dep_link_up == true) {
 311		rc = -EALREADY;
 312		goto error;
 313	}
 314
 315	gb = nfc_llcp_general_bytes(dev, &gb_len);
 316	if (gb_len > NFC_MAX_GT_LEN) {
 317		rc = -EINVAL;
 318		goto error;
 319	}
 320
 321	target = nfc_find_target(dev, target_index);
 322	if (target == NULL) {
 323		rc = -ENOTCONN;
 324		goto error;
 325	}
 326
 327	rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
 328	if (!rc) {
 329		dev->active_target = target;
 330		dev->rf_mode = NFC_RF_INITIATOR;
 331	}
 332
 333error:
 334	device_unlock(&dev->dev);
 335	return rc;
 336}
 337
 338int nfc_dep_link_down(struct nfc_dev *dev)
 339{
 340	int rc = 0;
 341
 342	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 343
 344	if (!dev->ops->dep_link_down)
 345		return -EOPNOTSUPP;
 346
 347	device_lock(&dev->dev);
 348
 349	if (!device_is_registered(&dev->dev)) {
 350		rc = -ENODEV;
 351		goto error;
 352	}
 353
 354	if (dev->dep_link_up == false) {
 355		rc = -EALREADY;
 356		goto error;
 357	}
 358
 359	rc = dev->ops->dep_link_down(dev);
 360	if (!rc) {
 361		dev->dep_link_up = false;
 362		dev->active_target = NULL;
 363		dev->rf_mode = NFC_RF_NONE;
 364		nfc_llcp_mac_is_down(dev);
 365		nfc_genl_dep_link_down_event(dev);
 366	}
 367
 368error:
 369	device_unlock(&dev->dev);
 370
 371	return rc;
 372}
 373
 374int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
 375		       u8 comm_mode, u8 rf_mode)
 376{
 377	dev->dep_link_up = true;
 378
 379	if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
 380		struct nfc_target *target;
 381
 382		target = nfc_find_target(dev, target_idx);
 383		if (target == NULL)
 384			return -ENOTCONN;
 385
 386		dev->active_target = target;
 387	}
 388
 389	dev->polling = false;
 390	dev->rf_mode = rf_mode;
 391
 392	nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
 393
 394	return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
 395}
 396EXPORT_SYMBOL(nfc_dep_link_is_up);
 397
 398/**
 399 * nfc_activate_target - prepare the target for data exchange
 400 *
 401 * @dev: The nfc device that found the target
 402 * @target_idx: index of the target that must be activated
 403 * @protocol: nfc protocol that will be used for data exchange
 404 */
 405int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
 406{
 407	int rc;
 408	struct nfc_target *target;
 409
 410	pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
 411		 dev_name(&dev->dev), target_idx, protocol);
 412
 413	device_lock(&dev->dev);
 414
 415	if (!device_is_registered(&dev->dev)) {
 416		rc = -ENODEV;
 417		goto error;
 418	}
 419
 420	if (dev->active_target) {
 421		rc = -EBUSY;
 422		goto error;
 423	}
 424
 425	target = nfc_find_target(dev, target_idx);
 426	if (target == NULL) {
 427		rc = -ENOTCONN;
 428		goto error;
 429	}
 430
 431	rc = dev->ops->activate_target(dev, target, protocol);
 432	if (!rc) {
 433		dev->active_target = target;
 434		dev->rf_mode = NFC_RF_INITIATOR;
 435
 436		if (dev->ops->check_presence && !dev->shutting_down)
 437			mod_timer(&dev->check_pres_timer, jiffies +
 438				  msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
 439	}
 440
 441error:
 442	device_unlock(&dev->dev);
 443	return rc;
 444}
 445
 446/**
 447 * nfc_deactivate_target - deactivate a nfc target
 448 *
 449 * @dev: The nfc device that found the target
 450 * @target_idx: index of the target that must be deactivated
 451 */
 452int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode)
 453{
 454	int rc = 0;
 455
 456	pr_debug("dev_name=%s target_idx=%u\n",
 457		 dev_name(&dev->dev), target_idx);
 458
 459	device_lock(&dev->dev);
 460
 461	if (!device_is_registered(&dev->dev)) {
 462		rc = -ENODEV;
 463		goto error;
 464	}
 465
 466	if (dev->active_target == NULL) {
 467		rc = -ENOTCONN;
 468		goto error;
 469	}
 470
 471	if (dev->active_target->idx != target_idx) {
 472		rc = -ENOTCONN;
 473		goto error;
 474	}
 475
 476	if (dev->ops->check_presence)
 477		del_timer_sync(&dev->check_pres_timer);
 478
 479	dev->ops->deactivate_target(dev, dev->active_target, mode);
 480	dev->active_target = NULL;
 481
 482error:
 483	device_unlock(&dev->dev);
 484	return rc;
 485}
 486
 487/**
 488 * nfc_data_exchange - transceive data
 489 *
 490 * @dev: The nfc device that found the target
 491 * @target_idx: index of the target
 492 * @skb: data to be sent
 493 * @cb: callback called when the response is received
 494 * @cb_context: parameter for the callback function
 495 *
 496 * The user must wait for the callback before calling this function again.
 497 */
 498int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
 499		      data_exchange_cb_t cb, void *cb_context)
 500{
 501	int rc;
 502
 503	pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
 504		 dev_name(&dev->dev), target_idx, skb->len);
 505
 506	device_lock(&dev->dev);
 507
 508	if (!device_is_registered(&dev->dev)) {
 509		rc = -ENODEV;
 510		kfree_skb(skb);
 511		goto error;
 512	}
 513
 514	if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
 515		if (dev->active_target->idx != target_idx) {
 516			rc = -EADDRNOTAVAIL;
 517			kfree_skb(skb);
 518			goto error;
 519		}
 520
 521		if (dev->ops->check_presence)
 522			del_timer_sync(&dev->check_pres_timer);
 523
 524		rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
 525					     cb_context);
 526
 527		if (!rc && dev->ops->check_presence && !dev->shutting_down)
 528			mod_timer(&dev->check_pres_timer, jiffies +
 529				  msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
 530	} else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
 531		rc = dev->ops->tm_send(dev, skb);
 532	} else {
 533		rc = -ENOTCONN;
 534		kfree_skb(skb);
 535		goto error;
 536	}
 537
 538
 539error:
 540	device_unlock(&dev->dev);
 541	return rc;
 542}
 543
 544struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
 545{
 546	struct nfc_se *se;
 547
 548	list_for_each_entry(se, &dev->secure_elements, list)
 549		if (se->idx == se_idx)
 550			return se;
 551
 552	return NULL;
 553}
 554EXPORT_SYMBOL(nfc_find_se);
 555
 556int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
 557{
 558	struct nfc_se *se;
 559	int rc;
 560
 561	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
 562
 563	device_lock(&dev->dev);
 564
 565	if (!device_is_registered(&dev->dev)) {
 566		rc = -ENODEV;
 567		goto error;
 568	}
 569
 570	if (!dev->dev_up) {
 571		rc = -ENODEV;
 572		goto error;
 573	}
 574
 575	if (dev->polling) {
 576		rc = -EBUSY;
 577		goto error;
 578	}
 579
 580	if (!dev->ops->enable_se || !dev->ops->disable_se) {
 581		rc = -EOPNOTSUPP;
 582		goto error;
 583	}
 584
 585	se = nfc_find_se(dev, se_idx);
 586	if (!se) {
 587		rc = -EINVAL;
 588		goto error;
 589	}
 590
 591	if (se->state == NFC_SE_ENABLED) {
 592		rc = -EALREADY;
 593		goto error;
 594	}
 595
 596	rc = dev->ops->enable_se(dev, se_idx);
 597	if (rc >= 0)
 598		se->state = NFC_SE_ENABLED;
 599
 600error:
 601	device_unlock(&dev->dev);
 602	return rc;
 603}
 604
 605int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
 606{
 607	struct nfc_se *se;
 608	int rc;
 609
 610	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
 611
 612	device_lock(&dev->dev);
 613
 614	if (!device_is_registered(&dev->dev)) {
 615		rc = -ENODEV;
 616		goto error;
 617	}
 618
 619	if (!dev->dev_up) {
 620		rc = -ENODEV;
 621		goto error;
 622	}
 623
 624	if (!dev->ops->enable_se || !dev->ops->disable_se) {
 625		rc = -EOPNOTSUPP;
 626		goto error;
 627	}
 628
 629	se = nfc_find_se(dev, se_idx);
 630	if (!se) {
 631		rc = -EINVAL;
 632		goto error;
 633	}
 634
 635	if (se->state == NFC_SE_DISABLED) {
 636		rc = -EALREADY;
 637		goto error;
 638	}
 639
 640	rc = dev->ops->disable_se(dev, se_idx);
 641	if (rc >= 0)
 642		se->state = NFC_SE_DISABLED;
 643
 644error:
 645	device_unlock(&dev->dev);
 646	return rc;
 647}
 648
 649int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
 650{
 651	pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
 652
 653	return nfc_llcp_set_remote_gb(dev, gb, gb_len);
 654}
 655EXPORT_SYMBOL(nfc_set_remote_general_bytes);
 656
 657u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
 658{
 659	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 660
 661	return nfc_llcp_general_bytes(dev, gb_len);
 662}
 663EXPORT_SYMBOL(nfc_get_local_general_bytes);
 664
 665int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
 666{
 667	/* Only LLCP target mode for now */
 668	if (dev->dep_link_up == false) {
 669		kfree_skb(skb);
 670		return -ENOLINK;
 671	}
 672
 673	return nfc_llcp_data_received(dev, skb);
 674}
 675EXPORT_SYMBOL(nfc_tm_data_received);
 676
 677int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
 678		     u8 *gb, size_t gb_len)
 679{
 680	int rc;
 681
 682	device_lock(&dev->dev);
 683
 684	dev->polling = false;
 685
 686	if (gb != NULL) {
 687		rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
 688		if (rc < 0)
 689			goto out;
 690	}
 691
 692	dev->rf_mode = NFC_RF_TARGET;
 693
 694	if (protocol == NFC_PROTO_NFC_DEP_MASK)
 695		nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
 696
 697	rc = nfc_genl_tm_activated(dev, protocol);
 698
 699out:
 700	device_unlock(&dev->dev);
 701
 702	return rc;
 703}
 704EXPORT_SYMBOL(nfc_tm_activated);
 705
 706int nfc_tm_deactivated(struct nfc_dev *dev)
 707{
 708	dev->dep_link_up = false;
 709	dev->rf_mode = NFC_RF_NONE;
 710
 711	return nfc_genl_tm_deactivated(dev);
 712}
 713EXPORT_SYMBOL(nfc_tm_deactivated);
 714
 715/**
 716 * nfc_alloc_send_skb - allocate a skb for data exchange responses
 717 *
 718 * @size: size to allocate
 719 * @gfp: gfp flags
 720 */
 721struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
 722				   unsigned int flags, unsigned int size,
 723				   unsigned int *err)
 724{
 725	struct sk_buff *skb;
 726	unsigned int total_size;
 727
 728	total_size = size +
 729		dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
 730
 731	skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
 732	if (skb)
 733		skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
 734
 735	return skb;
 736}
 737
 738/**
 739 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
 740 *
 741 * @size: size to allocate
 742 * @gfp: gfp flags
 743 */
 744struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
 745{
 746	struct sk_buff *skb;
 747	unsigned int total_size;
 748
 749	total_size = size + 1;
 750	skb = alloc_skb(total_size, gfp);
 751
 752	if (skb)
 753		skb_reserve(skb, 1);
 754
 755	return skb;
 756}
 757EXPORT_SYMBOL(nfc_alloc_recv_skb);
 758
 759/**
 760 * nfc_targets_found - inform that targets were found
 761 *
 762 * @dev: The nfc device that found the targets
 763 * @targets: array of nfc targets found
 764 * @ntargets: targets array size
 765 *
 766 * The device driver must call this function when one or many nfc targets
 767 * are found. After calling this function, the device driver must stop
 768 * polling for targets.
 769 * NOTE: This function can be called with targets=NULL and n_targets=0 to
 770 * notify a driver error, meaning that the polling operation cannot complete.
 771 * IMPORTANT: this function must not be called from an atomic context.
 772 * In addition, it must also not be called from a context that would prevent
 773 * the NFC Core to call other nfc ops entry point concurrently.
 774 */
 775int nfc_targets_found(struct nfc_dev *dev,
 776		      struct nfc_target *targets, int n_targets)
 777{
 778	int i;
 779
 780	pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
 781
 782	for (i = 0; i < n_targets; i++)
 783		targets[i].idx = dev->target_next_idx++;
 784
 785	device_lock(&dev->dev);
 786
 787	if (dev->polling == false) {
 788		device_unlock(&dev->dev);
 789		return 0;
 790	}
 791
 792	dev->polling = false;
 793
 794	dev->targets_generation++;
 795
 796	kfree(dev->targets);
 797	dev->targets = NULL;
 798
 799	if (targets) {
 800		dev->targets = kmemdup(targets,
 801				       n_targets * sizeof(struct nfc_target),
 802				       GFP_ATOMIC);
 803
 804		if (!dev->targets) {
 805			dev->n_targets = 0;
 806			device_unlock(&dev->dev);
 807			return -ENOMEM;
 808		}
 809	}
 810
 811	dev->n_targets = n_targets;
 812	device_unlock(&dev->dev);
 813
 814	nfc_genl_targets_found(dev);
 815
 816	return 0;
 817}
 818EXPORT_SYMBOL(nfc_targets_found);
 819
 820/**
 821 * nfc_target_lost - inform that an activated target went out of field
 822 *
 823 * @dev: The nfc device that had the activated target in field
 824 * @target_idx: the nfc index of the target
 825 *
 826 * The device driver must call this function when the activated target
 827 * goes out of the field.
 828 * IMPORTANT: this function must not be called from an atomic context.
 829 * In addition, it must also not be called from a context that would prevent
 830 * the NFC Core to call other nfc ops entry point concurrently.
 831 */
 832int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
 833{
 834	struct nfc_target *tg;
 835	int i;
 836
 837	pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
 838
 839	device_lock(&dev->dev);
 840
 841	for (i = 0; i < dev->n_targets; i++) {
 842		tg = &dev->targets[i];
 843		if (tg->idx == target_idx)
 844			break;
 845	}
 846
 847	if (i == dev->n_targets) {
 848		device_unlock(&dev->dev);
 849		return -EINVAL;
 850	}
 851
 852	dev->targets_generation++;
 853	dev->n_targets--;
 854	dev->active_target = NULL;
 855
 856	if (dev->n_targets) {
 857		memcpy(&dev->targets[i], &dev->targets[i + 1],
 858		       (dev->n_targets - i) * sizeof(struct nfc_target));
 859	} else {
 860		kfree(dev->targets);
 861		dev->targets = NULL;
 862	}
 863
 864	device_unlock(&dev->dev);
 865
 866	nfc_genl_target_lost(dev, target_idx);
 867
 868	return 0;
 869}
 870EXPORT_SYMBOL(nfc_target_lost);
 871
 872inline void nfc_driver_failure(struct nfc_dev *dev, int err)
 873{
 874	nfc_targets_found(dev, NULL, 0);
 875}
 876EXPORT_SYMBOL(nfc_driver_failure);
 877
 878int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
 879{
 880	struct nfc_se *se;
 881	int rc;
 882
 883	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
 884
 885	se = nfc_find_se(dev, se_idx);
 886	if (se)
 887		return -EALREADY;
 888
 889	se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
 890	if (!se)
 891		return -ENOMEM;
 892
 893	se->idx = se_idx;
 894	se->type = type;
 895	se->state = NFC_SE_DISABLED;
 896	INIT_LIST_HEAD(&se->list);
 897
 898	list_add(&se->list, &dev->secure_elements);
 899
 900	rc = nfc_genl_se_added(dev, se_idx, type);
 901	if (rc < 0) {
 902		list_del(&se->list);
 903		kfree(se);
 904
 905		return rc;
 906	}
 907
 908	return 0;
 909}
 910EXPORT_SYMBOL(nfc_add_se);
 911
 912int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
 913{
 914	struct nfc_se *se, *n;
 915	int rc;
 916
 917	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
 918
 919	list_for_each_entry_safe(se, n, &dev->secure_elements, list)
 920		if (se->idx == se_idx) {
 921			rc = nfc_genl_se_removed(dev, se_idx);
 922			if (rc < 0)
 923				return rc;
 924
 925			list_del(&se->list);
 926			kfree(se);
 927
 928			return 0;
 929		}
 930
 931	return -EINVAL;
 932}
 933EXPORT_SYMBOL(nfc_remove_se);
 934
 935int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
 936		       struct nfc_evt_transaction *evt_transaction)
 937{
 938	int rc;
 939
 940	pr_debug("transaction: %x\n", se_idx);
 941
 942	device_lock(&dev->dev);
 943
 944	if (!evt_transaction) {
 945		rc = -EPROTO;
 946		goto out;
 947	}
 948
 949	rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction);
 950out:
 951	device_unlock(&dev->dev);
 952	return rc;
 953}
 954EXPORT_SYMBOL(nfc_se_transaction);
 955
 956int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx)
 957{
 958	int rc;
 959
 960	pr_debug("connectivity: %x\n", se_idx);
 961
 962	device_lock(&dev->dev);
 963	rc = nfc_genl_se_connectivity(dev, se_idx);
 964	device_unlock(&dev->dev);
 965	return rc;
 966}
 967EXPORT_SYMBOL(nfc_se_connectivity);
 968
 969static void nfc_release(struct device *d)
 970{
 971	struct nfc_dev *dev = to_nfc_dev(d);
 972	struct nfc_se *se, *n;
 973
 974	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 975
 976	nfc_genl_data_exit(&dev->genl_data);
 977	kfree(dev->targets);
 978
 979	list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
 980			nfc_genl_se_removed(dev, se->idx);
 981			list_del(&se->list);
 982			kfree(se);
 983	}
 984
 
 
 985	kfree(dev);
 986}
 987
 988static void nfc_check_pres_work(struct work_struct *work)
 989{
 990	struct nfc_dev *dev = container_of(work, struct nfc_dev,
 991					   check_pres_work);
 992	int rc;
 993
 994	device_lock(&dev->dev);
 995
 996	if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
 997		rc = dev->ops->check_presence(dev, dev->active_target);
 998		if (rc == -EOPNOTSUPP)
 999			goto exit;
1000		if (rc) {
1001			u32 active_target_idx = dev->active_target->idx;
1002			device_unlock(&dev->dev);
1003			nfc_target_lost(dev, active_target_idx);
1004			return;
1005		}
1006
1007		if (!dev->shutting_down)
1008			mod_timer(&dev->check_pres_timer, jiffies +
1009				  msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
1010	}
1011
1012exit:
1013	device_unlock(&dev->dev);
1014}
1015
1016static void nfc_check_pres_timeout(unsigned long data)
1017{
1018	struct nfc_dev *dev = (struct nfc_dev *)data;
1019
1020	schedule_work(&dev->check_pres_work);
1021}
1022
1023struct class nfc_class = {
1024	.name = "nfc",
1025	.dev_release = nfc_release,
1026};
1027EXPORT_SYMBOL(nfc_class);
1028
1029static int match_idx(struct device *d, const void *data)
1030{
1031	struct nfc_dev *dev = to_nfc_dev(d);
1032	const unsigned int *idx = data;
1033
1034	return dev->idx == *idx;
1035}
1036
1037struct nfc_dev *nfc_get_device(unsigned int idx)
1038{
1039	struct device *d;
1040
1041	d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1042	if (!d)
1043		return NULL;
1044
1045	return to_nfc_dev(d);
1046}
1047
1048/**
1049 * nfc_allocate_device - allocate a new nfc device
1050 *
1051 * @ops: device operations
1052 * @supported_protocols: NFC protocols supported by the device
1053 */
1054struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
1055				    u32 supported_protocols,
1056				    int tx_headroom, int tx_tailroom)
1057{
1058	struct nfc_dev *dev;
 
1059
1060	if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
1061	    !ops->deactivate_target || !ops->im_transceive)
1062		return NULL;
1063
1064	if (!supported_protocols)
1065		return NULL;
1066
1067	dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1068	if (!dev)
1069		return NULL;
1070
 
 
 
 
 
 
 
 
 
1071	dev->ops = ops;
1072	dev->supported_protocols = supported_protocols;
1073	dev->tx_headroom = tx_headroom;
1074	dev->tx_tailroom = tx_tailroom;
1075	INIT_LIST_HEAD(&dev->secure_elements);
1076
1077	nfc_genl_data_init(&dev->genl_data);
1078
1079	dev->rf_mode = NFC_RF_NONE;
1080
1081	/* first generation must not be 0 */
1082	dev->targets_generation = 1;
1083
1084	if (ops->check_presence) {
1085		init_timer(&dev->check_pres_timer);
1086		dev->check_pres_timer.data = (unsigned long)dev;
1087		dev->check_pres_timer.function = nfc_check_pres_timeout;
1088
1089		INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
1090	}
1091
1092	return dev;
 
 
 
 
 
1093}
1094EXPORT_SYMBOL(nfc_allocate_device);
1095
1096/**
1097 * nfc_register_device - register a nfc device in the nfc subsystem
1098 *
1099 * @dev: The nfc device to register
1100 */
1101int nfc_register_device(struct nfc_dev *dev)
1102{
1103	int rc;
1104
1105	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1106
1107	dev->idx = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1108	if (dev->idx < 0)
1109		return dev->idx;
1110
1111	dev->dev.class = &nfc_class;
1112	dev_set_name(&dev->dev, "nfc%d", dev->idx);
1113	device_initialize(&dev->dev);
1114
1115	mutex_lock(&nfc_devlist_mutex);
1116	nfc_devlist_generation++;
1117	rc = device_add(&dev->dev);
1118	mutex_unlock(&nfc_devlist_mutex);
1119
1120	if (rc < 0)
1121		return rc;
1122
1123	rc = nfc_llcp_register_device(dev);
1124	if (rc)
1125		pr_err("Could not register llcp device\n");
1126
1127	rc = nfc_genl_device_added(dev);
1128	if (rc)
1129		pr_debug("The userspace won't be notified that the device %s was added\n",
1130			 dev_name(&dev->dev));
1131
1132	dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1133				   RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1134	if (dev->rfkill) {
1135		if (rfkill_register(dev->rfkill) < 0) {
1136			rfkill_destroy(dev->rfkill);
1137			dev->rfkill = NULL;
1138		}
1139	}
1140
1141	return 0;
1142}
1143EXPORT_SYMBOL(nfc_register_device);
1144
1145/**
1146 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1147 *
1148 * @dev: The nfc device to unregister
1149 */
1150void nfc_unregister_device(struct nfc_dev *dev)
1151{
1152	int rc, id;
1153
1154	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1155
1156	id = dev->idx;
1157
1158	if (dev->rfkill) {
1159		rfkill_unregister(dev->rfkill);
1160		rfkill_destroy(dev->rfkill);
1161	}
1162
1163	if (dev->ops->check_presence) {
1164		device_lock(&dev->dev);
1165		dev->shutting_down = true;
1166		device_unlock(&dev->dev);
1167		del_timer_sync(&dev->check_pres_timer);
1168		cancel_work_sync(&dev->check_pres_work);
1169	}
1170
1171	rc = nfc_genl_device_removed(dev);
1172	if (rc)
1173		pr_debug("The userspace won't be notified that the device %s "
1174			 "was removed\n", dev_name(&dev->dev));
1175
1176	nfc_llcp_unregister_device(dev);
1177
1178	mutex_lock(&nfc_devlist_mutex);
1179	nfc_devlist_generation++;
1180	device_del(&dev->dev);
1181	mutex_unlock(&nfc_devlist_mutex);
1182
1183	ida_simple_remove(&nfc_index_ida, id);
1184}
1185EXPORT_SYMBOL(nfc_unregister_device);
1186
1187static int __init nfc_init(void)
1188{
1189	int rc;
1190
1191	pr_info("NFC Core ver %s\n", VERSION);
1192
1193	rc = class_register(&nfc_class);
1194	if (rc)
1195		return rc;
1196
1197	rc = nfc_genl_init();
1198	if (rc)
1199		goto err_genl;
1200
1201	/* the first generation must not be 0 */
1202	nfc_devlist_generation = 1;
1203
1204	rc = rawsock_init();
1205	if (rc)
1206		goto err_rawsock;
1207
1208	rc = nfc_llcp_init();
1209	if (rc)
1210		goto err_llcp_sock;
1211
1212	rc = af_nfc_init();
1213	if (rc)
1214		goto err_af_nfc;
1215
1216	return 0;
1217
1218err_af_nfc:
1219	nfc_llcp_exit();
1220err_llcp_sock:
1221	rawsock_exit();
1222err_rawsock:
1223	nfc_genl_exit();
1224err_genl:
1225	class_unregister(&nfc_class);
1226	return rc;
1227}
1228
1229static void __exit nfc_exit(void)
1230{
1231	af_nfc_exit();
1232	nfc_llcp_exit();
1233	rawsock_exit();
1234	nfc_genl_exit();
1235	class_unregister(&nfc_class);
1236}
1237
1238subsys_initcall(nfc_init);
1239module_exit(nfc_exit);
1240
1241MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1242MODULE_DESCRIPTION("NFC Core ver " VERSION);
1243MODULE_VERSION(VERSION);
1244MODULE_LICENSE("GPL");
1245MODULE_ALIAS_NETPROTO(PF_NFC);
1246MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);
v5.4
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (C) 2011 Instituto Nokia de Tecnologia
   4 *
   5 * Authors:
   6 *    Lauro Ramos Venancio <lauro.venancio@openbossa.org>
   7 *    Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
 
 
 
 
 
 
 
 
 
 
 
 
 
   8 */
   9
  10#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  11
  12#include <linux/init.h>
  13#include <linux/kernel.h>
  14#include <linux/module.h>
  15#include <linux/slab.h>
  16#include <linux/rfkill.h>
  17#include <linux/nfc.h>
  18
  19#include <net/genetlink.h>
  20
  21#include "nfc.h"
  22
  23#define VERSION "0.1"
  24
  25#define NFC_CHECK_PRES_FREQ_MS	2000
  26
  27int nfc_devlist_generation;
  28DEFINE_MUTEX(nfc_devlist_mutex);
  29
  30/* NFC device ID bitmap */
  31static DEFINE_IDA(nfc_index_ida);
  32
  33int nfc_fw_download(struct nfc_dev *dev, const char *firmware_name)
  34{
  35	int rc = 0;
  36
  37	pr_debug("%s do firmware %s\n", dev_name(&dev->dev), firmware_name);
  38
  39	device_lock(&dev->dev);
  40
  41	if (!device_is_registered(&dev->dev)) {
  42		rc = -ENODEV;
  43		goto error;
  44	}
  45
  46	if (dev->dev_up) {
  47		rc = -EBUSY;
  48		goto error;
  49	}
  50
  51	if (!dev->ops->fw_download) {
  52		rc = -EOPNOTSUPP;
  53		goto error;
  54	}
  55
  56	dev->fw_download_in_progress = true;
  57	rc = dev->ops->fw_download(dev, firmware_name);
  58	if (rc)
  59		dev->fw_download_in_progress = false;
  60
  61error:
  62	device_unlock(&dev->dev);
  63	return rc;
  64}
  65
  66/**
  67 * nfc_fw_download_done - inform that a firmware download was completed
  68 *
  69 * @dev: The nfc device to which firmware was downloaded
  70 * @firmware_name: The firmware filename
  71 * @result: The positive value of a standard errno value
  72 */
  73int nfc_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
  74			 u32 result)
  75{
  76	dev->fw_download_in_progress = false;
  77
  78	return nfc_genl_fw_download_done(dev, firmware_name, result);
  79}
  80EXPORT_SYMBOL(nfc_fw_download_done);
  81
  82/**
  83 * nfc_dev_up - turn on the NFC device
  84 *
  85 * @dev: The nfc device to be turned on
  86 *
  87 * The device remains up until the nfc_dev_down function is called.
  88 */
  89int nfc_dev_up(struct nfc_dev *dev)
  90{
  91	int rc = 0;
  92
  93	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
  94
  95	device_lock(&dev->dev);
  96
  97	if (dev->rfkill && rfkill_blocked(dev->rfkill)) {
  98		rc = -ERFKILL;
  99		goto error;
 100	}
 101
 102	if (!device_is_registered(&dev->dev)) {
 103		rc = -ENODEV;
 104		goto error;
 105	}
 106
 107	if (dev->fw_download_in_progress) {
 108		rc = -EBUSY;
 109		goto error;
 110	}
 111
 112	if (dev->dev_up) {
 113		rc = -EALREADY;
 114		goto error;
 115	}
 116
 117	if (dev->ops->dev_up)
 118		rc = dev->ops->dev_up(dev);
 119
 120	if (!rc)
 121		dev->dev_up = true;
 122
 123	/* We have to enable the device before discovering SEs */
 124	if (dev->ops->discover_se && dev->ops->discover_se(dev))
 125		pr_err("SE discovery failed\n");
 126
 127error:
 128	device_unlock(&dev->dev);
 129	return rc;
 130}
 131
 132/**
 133 * nfc_dev_down - turn off the NFC device
 134 *
 135 * @dev: The nfc device to be turned off
 136 */
 137int nfc_dev_down(struct nfc_dev *dev)
 138{
 139	int rc = 0;
 140
 141	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 142
 143	device_lock(&dev->dev);
 144
 145	if (!device_is_registered(&dev->dev)) {
 146		rc = -ENODEV;
 147		goto error;
 148	}
 149
 150	if (!dev->dev_up) {
 151		rc = -EALREADY;
 152		goto error;
 153	}
 154
 155	if (dev->polling || dev->active_target) {
 156		rc = -EBUSY;
 157		goto error;
 158	}
 159
 160	if (dev->ops->dev_down)
 161		dev->ops->dev_down(dev);
 162
 163	dev->dev_up = false;
 164
 165error:
 166	device_unlock(&dev->dev);
 167	return rc;
 168}
 169
 170static int nfc_rfkill_set_block(void *data, bool blocked)
 171{
 172	struct nfc_dev *dev = data;
 173
 174	pr_debug("%s blocked %d", dev_name(&dev->dev), blocked);
 175
 176	if (!blocked)
 177		return 0;
 178
 179	nfc_dev_down(dev);
 180
 181	return 0;
 182}
 183
 184static const struct rfkill_ops nfc_rfkill_ops = {
 185	.set_block = nfc_rfkill_set_block,
 186};
 187
 188/**
 189 * nfc_start_poll - start polling for nfc targets
 190 *
 191 * @dev: The nfc device that must start polling
 192 * @protocols: bitset of nfc protocols that must be used for polling
 193 *
 194 * The device remains polling for targets until a target is found or
 195 * the nfc_stop_poll function is called.
 196 */
 197int nfc_start_poll(struct nfc_dev *dev, u32 im_protocols, u32 tm_protocols)
 198{
 199	int rc;
 200
 201	pr_debug("dev_name %s initiator protocols 0x%x target protocols 0x%x\n",
 202		 dev_name(&dev->dev), im_protocols, tm_protocols);
 203
 204	if (!im_protocols && !tm_protocols)
 205		return -EINVAL;
 206
 207	device_lock(&dev->dev);
 208
 209	if (!device_is_registered(&dev->dev)) {
 210		rc = -ENODEV;
 211		goto error;
 212	}
 213
 214	if (!dev->dev_up) {
 215		rc = -ENODEV;
 216		goto error;
 217	}
 218
 219	if (dev->polling) {
 220		rc = -EBUSY;
 221		goto error;
 222	}
 223
 224	rc = dev->ops->start_poll(dev, im_protocols, tm_protocols);
 225	if (!rc) {
 226		dev->polling = true;
 227		dev->rf_mode = NFC_RF_NONE;
 228	}
 229
 230error:
 231	device_unlock(&dev->dev);
 232	return rc;
 233}
 234
 235/**
 236 * nfc_stop_poll - stop polling for nfc targets
 237 *
 238 * @dev: The nfc device that must stop polling
 239 */
 240int nfc_stop_poll(struct nfc_dev *dev)
 241{
 242	int rc = 0;
 243
 244	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 245
 246	device_lock(&dev->dev);
 247
 248	if (!device_is_registered(&dev->dev)) {
 249		rc = -ENODEV;
 250		goto error;
 251	}
 252
 253	if (!dev->polling) {
 254		rc = -EINVAL;
 255		goto error;
 256	}
 257
 258	dev->ops->stop_poll(dev);
 259	dev->polling = false;
 260	dev->rf_mode = NFC_RF_NONE;
 261
 262error:
 263	device_unlock(&dev->dev);
 264	return rc;
 265}
 266
 267static struct nfc_target *nfc_find_target(struct nfc_dev *dev, u32 target_idx)
 268{
 269	int i;
 270
 271	for (i = 0; i < dev->n_targets; i++) {
 272		if (dev->targets[i].idx == target_idx)
 273			return &dev->targets[i];
 274	}
 275
 276	return NULL;
 277}
 278
 279int nfc_dep_link_up(struct nfc_dev *dev, int target_index, u8 comm_mode)
 280{
 281	int rc = 0;
 282	u8 *gb;
 283	size_t gb_len;
 284	struct nfc_target *target;
 285
 286	pr_debug("dev_name=%s comm %d\n", dev_name(&dev->dev), comm_mode);
 287
 288	if (!dev->ops->dep_link_up)
 289		return -EOPNOTSUPP;
 290
 291	device_lock(&dev->dev);
 292
 293	if (!device_is_registered(&dev->dev)) {
 294		rc = -ENODEV;
 295		goto error;
 296	}
 297
 298	if (dev->dep_link_up == true) {
 299		rc = -EALREADY;
 300		goto error;
 301	}
 302
 303	gb = nfc_llcp_general_bytes(dev, &gb_len);
 304	if (gb_len > NFC_MAX_GT_LEN) {
 305		rc = -EINVAL;
 306		goto error;
 307	}
 308
 309	target = nfc_find_target(dev, target_index);
 310	if (target == NULL) {
 311		rc = -ENOTCONN;
 312		goto error;
 313	}
 314
 315	rc = dev->ops->dep_link_up(dev, target, comm_mode, gb, gb_len);
 316	if (!rc) {
 317		dev->active_target = target;
 318		dev->rf_mode = NFC_RF_INITIATOR;
 319	}
 320
 321error:
 322	device_unlock(&dev->dev);
 323	return rc;
 324}
 325
 326int nfc_dep_link_down(struct nfc_dev *dev)
 327{
 328	int rc = 0;
 329
 330	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 331
 332	if (!dev->ops->dep_link_down)
 333		return -EOPNOTSUPP;
 334
 335	device_lock(&dev->dev);
 336
 337	if (!device_is_registered(&dev->dev)) {
 338		rc = -ENODEV;
 339		goto error;
 340	}
 341
 342	if (dev->dep_link_up == false) {
 343		rc = -EALREADY;
 344		goto error;
 345	}
 346
 347	rc = dev->ops->dep_link_down(dev);
 348	if (!rc) {
 349		dev->dep_link_up = false;
 350		dev->active_target = NULL;
 351		dev->rf_mode = NFC_RF_NONE;
 352		nfc_llcp_mac_is_down(dev);
 353		nfc_genl_dep_link_down_event(dev);
 354	}
 355
 356error:
 357	device_unlock(&dev->dev);
 358
 359	return rc;
 360}
 361
 362int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
 363		       u8 comm_mode, u8 rf_mode)
 364{
 365	dev->dep_link_up = true;
 366
 367	if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
 368		struct nfc_target *target;
 369
 370		target = nfc_find_target(dev, target_idx);
 371		if (target == NULL)
 372			return -ENOTCONN;
 373
 374		dev->active_target = target;
 375	}
 376
 377	dev->polling = false;
 378	dev->rf_mode = rf_mode;
 379
 380	nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
 381
 382	return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
 383}
 384EXPORT_SYMBOL(nfc_dep_link_is_up);
 385
 386/**
 387 * nfc_activate_target - prepare the target for data exchange
 388 *
 389 * @dev: The nfc device that found the target
 390 * @target_idx: index of the target that must be activated
 391 * @protocol: nfc protocol that will be used for data exchange
 392 */
 393int nfc_activate_target(struct nfc_dev *dev, u32 target_idx, u32 protocol)
 394{
 395	int rc;
 396	struct nfc_target *target;
 397
 398	pr_debug("dev_name=%s target_idx=%u protocol=%u\n",
 399		 dev_name(&dev->dev), target_idx, protocol);
 400
 401	device_lock(&dev->dev);
 402
 403	if (!device_is_registered(&dev->dev)) {
 404		rc = -ENODEV;
 405		goto error;
 406	}
 407
 408	if (dev->active_target) {
 409		rc = -EBUSY;
 410		goto error;
 411	}
 412
 413	target = nfc_find_target(dev, target_idx);
 414	if (target == NULL) {
 415		rc = -ENOTCONN;
 416		goto error;
 417	}
 418
 419	rc = dev->ops->activate_target(dev, target, protocol);
 420	if (!rc) {
 421		dev->active_target = target;
 422		dev->rf_mode = NFC_RF_INITIATOR;
 423
 424		if (dev->ops->check_presence && !dev->shutting_down)
 425			mod_timer(&dev->check_pres_timer, jiffies +
 426				  msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
 427	}
 428
 429error:
 430	device_unlock(&dev->dev);
 431	return rc;
 432}
 433
 434/**
 435 * nfc_deactivate_target - deactivate a nfc target
 436 *
 437 * @dev: The nfc device that found the target
 438 * @target_idx: index of the target that must be deactivated
 439 */
 440int nfc_deactivate_target(struct nfc_dev *dev, u32 target_idx, u8 mode)
 441{
 442	int rc = 0;
 443
 444	pr_debug("dev_name=%s target_idx=%u\n",
 445		 dev_name(&dev->dev), target_idx);
 446
 447	device_lock(&dev->dev);
 448
 449	if (!device_is_registered(&dev->dev)) {
 450		rc = -ENODEV;
 451		goto error;
 452	}
 453
 454	if (dev->active_target == NULL) {
 455		rc = -ENOTCONN;
 456		goto error;
 457	}
 458
 459	if (dev->active_target->idx != target_idx) {
 460		rc = -ENOTCONN;
 461		goto error;
 462	}
 463
 464	if (dev->ops->check_presence)
 465		del_timer_sync(&dev->check_pres_timer);
 466
 467	dev->ops->deactivate_target(dev, dev->active_target, mode);
 468	dev->active_target = NULL;
 469
 470error:
 471	device_unlock(&dev->dev);
 472	return rc;
 473}
 474
 475/**
 476 * nfc_data_exchange - transceive data
 477 *
 478 * @dev: The nfc device that found the target
 479 * @target_idx: index of the target
 480 * @skb: data to be sent
 481 * @cb: callback called when the response is received
 482 * @cb_context: parameter for the callback function
 483 *
 484 * The user must wait for the callback before calling this function again.
 485 */
 486int nfc_data_exchange(struct nfc_dev *dev, u32 target_idx, struct sk_buff *skb,
 487		      data_exchange_cb_t cb, void *cb_context)
 488{
 489	int rc;
 490
 491	pr_debug("dev_name=%s target_idx=%u skb->len=%u\n",
 492		 dev_name(&dev->dev), target_idx, skb->len);
 493
 494	device_lock(&dev->dev);
 495
 496	if (!device_is_registered(&dev->dev)) {
 497		rc = -ENODEV;
 498		kfree_skb(skb);
 499		goto error;
 500	}
 501
 502	if (dev->rf_mode == NFC_RF_INITIATOR && dev->active_target != NULL) {
 503		if (dev->active_target->idx != target_idx) {
 504			rc = -EADDRNOTAVAIL;
 505			kfree_skb(skb);
 506			goto error;
 507		}
 508
 509		if (dev->ops->check_presence)
 510			del_timer_sync(&dev->check_pres_timer);
 511
 512		rc = dev->ops->im_transceive(dev, dev->active_target, skb, cb,
 513					     cb_context);
 514
 515		if (!rc && dev->ops->check_presence && !dev->shutting_down)
 516			mod_timer(&dev->check_pres_timer, jiffies +
 517				  msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
 518	} else if (dev->rf_mode == NFC_RF_TARGET && dev->ops->tm_send != NULL) {
 519		rc = dev->ops->tm_send(dev, skb);
 520	} else {
 521		rc = -ENOTCONN;
 522		kfree_skb(skb);
 523		goto error;
 524	}
 525
 526
 527error:
 528	device_unlock(&dev->dev);
 529	return rc;
 530}
 531
 532struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
 533{
 534	struct nfc_se *se;
 535
 536	list_for_each_entry(se, &dev->secure_elements, list)
 537		if (se->idx == se_idx)
 538			return se;
 539
 540	return NULL;
 541}
 542EXPORT_SYMBOL(nfc_find_se);
 543
 544int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
 545{
 546	struct nfc_se *se;
 547	int rc;
 548
 549	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
 550
 551	device_lock(&dev->dev);
 552
 553	if (!device_is_registered(&dev->dev)) {
 554		rc = -ENODEV;
 555		goto error;
 556	}
 557
 558	if (!dev->dev_up) {
 559		rc = -ENODEV;
 560		goto error;
 561	}
 562
 563	if (dev->polling) {
 564		rc = -EBUSY;
 565		goto error;
 566	}
 567
 568	if (!dev->ops->enable_se || !dev->ops->disable_se) {
 569		rc = -EOPNOTSUPP;
 570		goto error;
 571	}
 572
 573	se = nfc_find_se(dev, se_idx);
 574	if (!se) {
 575		rc = -EINVAL;
 576		goto error;
 577	}
 578
 579	if (se->state == NFC_SE_ENABLED) {
 580		rc = -EALREADY;
 581		goto error;
 582	}
 583
 584	rc = dev->ops->enable_se(dev, se_idx);
 585	if (rc >= 0)
 586		se->state = NFC_SE_ENABLED;
 587
 588error:
 589	device_unlock(&dev->dev);
 590	return rc;
 591}
 592
 593int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
 594{
 595	struct nfc_se *se;
 596	int rc;
 597
 598	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
 599
 600	device_lock(&dev->dev);
 601
 602	if (!device_is_registered(&dev->dev)) {
 603		rc = -ENODEV;
 604		goto error;
 605	}
 606
 607	if (!dev->dev_up) {
 608		rc = -ENODEV;
 609		goto error;
 610	}
 611
 612	if (!dev->ops->enable_se || !dev->ops->disable_se) {
 613		rc = -EOPNOTSUPP;
 614		goto error;
 615	}
 616
 617	se = nfc_find_se(dev, se_idx);
 618	if (!se) {
 619		rc = -EINVAL;
 620		goto error;
 621	}
 622
 623	if (se->state == NFC_SE_DISABLED) {
 624		rc = -EALREADY;
 625		goto error;
 626	}
 627
 628	rc = dev->ops->disable_se(dev, se_idx);
 629	if (rc >= 0)
 630		se->state = NFC_SE_DISABLED;
 631
 632error:
 633	device_unlock(&dev->dev);
 634	return rc;
 635}
 636
 637int nfc_set_remote_general_bytes(struct nfc_dev *dev, u8 *gb, u8 gb_len)
 638{
 639	pr_debug("dev_name=%s gb_len=%d\n", dev_name(&dev->dev), gb_len);
 640
 641	return nfc_llcp_set_remote_gb(dev, gb, gb_len);
 642}
 643EXPORT_SYMBOL(nfc_set_remote_general_bytes);
 644
 645u8 *nfc_get_local_general_bytes(struct nfc_dev *dev, size_t *gb_len)
 646{
 647	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 648
 649	return nfc_llcp_general_bytes(dev, gb_len);
 650}
 651EXPORT_SYMBOL(nfc_get_local_general_bytes);
 652
 653int nfc_tm_data_received(struct nfc_dev *dev, struct sk_buff *skb)
 654{
 655	/* Only LLCP target mode for now */
 656	if (dev->dep_link_up == false) {
 657		kfree_skb(skb);
 658		return -ENOLINK;
 659	}
 660
 661	return nfc_llcp_data_received(dev, skb);
 662}
 663EXPORT_SYMBOL(nfc_tm_data_received);
 664
 665int nfc_tm_activated(struct nfc_dev *dev, u32 protocol, u8 comm_mode,
 666		     u8 *gb, size_t gb_len)
 667{
 668	int rc;
 669
 670	device_lock(&dev->dev);
 671
 672	dev->polling = false;
 673
 674	if (gb != NULL) {
 675		rc = nfc_set_remote_general_bytes(dev, gb, gb_len);
 676		if (rc < 0)
 677			goto out;
 678	}
 679
 680	dev->rf_mode = NFC_RF_TARGET;
 681
 682	if (protocol == NFC_PROTO_NFC_DEP_MASK)
 683		nfc_dep_link_is_up(dev, 0, comm_mode, NFC_RF_TARGET);
 684
 685	rc = nfc_genl_tm_activated(dev, protocol);
 686
 687out:
 688	device_unlock(&dev->dev);
 689
 690	return rc;
 691}
 692EXPORT_SYMBOL(nfc_tm_activated);
 693
 694int nfc_tm_deactivated(struct nfc_dev *dev)
 695{
 696	dev->dep_link_up = false;
 697	dev->rf_mode = NFC_RF_NONE;
 698
 699	return nfc_genl_tm_deactivated(dev);
 700}
 701EXPORT_SYMBOL(nfc_tm_deactivated);
 702
 703/**
 704 * nfc_alloc_send_skb - allocate a skb for data exchange responses
 705 *
 706 * @size: size to allocate
 707 * @gfp: gfp flags
 708 */
 709struct sk_buff *nfc_alloc_send_skb(struct nfc_dev *dev, struct sock *sk,
 710				   unsigned int flags, unsigned int size,
 711				   unsigned int *err)
 712{
 713	struct sk_buff *skb;
 714	unsigned int total_size;
 715
 716	total_size = size +
 717		dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE;
 718
 719	skb = sock_alloc_send_skb(sk, total_size, flags & MSG_DONTWAIT, err);
 720	if (skb)
 721		skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE);
 722
 723	return skb;
 724}
 725
 726/**
 727 * nfc_alloc_recv_skb - allocate a skb for data exchange responses
 728 *
 729 * @size: size to allocate
 730 * @gfp: gfp flags
 731 */
 732struct sk_buff *nfc_alloc_recv_skb(unsigned int size, gfp_t gfp)
 733{
 734	struct sk_buff *skb;
 735	unsigned int total_size;
 736
 737	total_size = size + 1;
 738	skb = alloc_skb(total_size, gfp);
 739
 740	if (skb)
 741		skb_reserve(skb, 1);
 742
 743	return skb;
 744}
 745EXPORT_SYMBOL(nfc_alloc_recv_skb);
 746
 747/**
 748 * nfc_targets_found - inform that targets were found
 749 *
 750 * @dev: The nfc device that found the targets
 751 * @targets: array of nfc targets found
 752 * @ntargets: targets array size
 753 *
 754 * The device driver must call this function when one or many nfc targets
 755 * are found. After calling this function, the device driver must stop
 756 * polling for targets.
 757 * NOTE: This function can be called with targets=NULL and n_targets=0 to
 758 * notify a driver error, meaning that the polling operation cannot complete.
 759 * IMPORTANT: this function must not be called from an atomic context.
 760 * In addition, it must also not be called from a context that would prevent
 761 * the NFC Core to call other nfc ops entry point concurrently.
 762 */
 763int nfc_targets_found(struct nfc_dev *dev,
 764		      struct nfc_target *targets, int n_targets)
 765{
 766	int i;
 767
 768	pr_debug("dev_name=%s n_targets=%d\n", dev_name(&dev->dev), n_targets);
 769
 770	for (i = 0; i < n_targets; i++)
 771		targets[i].idx = dev->target_next_idx++;
 772
 773	device_lock(&dev->dev);
 774
 775	if (dev->polling == false) {
 776		device_unlock(&dev->dev);
 777		return 0;
 778	}
 779
 780	dev->polling = false;
 781
 782	dev->targets_generation++;
 783
 784	kfree(dev->targets);
 785	dev->targets = NULL;
 786
 787	if (targets) {
 788		dev->targets = kmemdup(targets,
 789				       n_targets * sizeof(struct nfc_target),
 790				       GFP_ATOMIC);
 791
 792		if (!dev->targets) {
 793			dev->n_targets = 0;
 794			device_unlock(&dev->dev);
 795			return -ENOMEM;
 796		}
 797	}
 798
 799	dev->n_targets = n_targets;
 800	device_unlock(&dev->dev);
 801
 802	nfc_genl_targets_found(dev);
 803
 804	return 0;
 805}
 806EXPORT_SYMBOL(nfc_targets_found);
 807
 808/**
 809 * nfc_target_lost - inform that an activated target went out of field
 810 *
 811 * @dev: The nfc device that had the activated target in field
 812 * @target_idx: the nfc index of the target
 813 *
 814 * The device driver must call this function when the activated target
 815 * goes out of the field.
 816 * IMPORTANT: this function must not be called from an atomic context.
 817 * In addition, it must also not be called from a context that would prevent
 818 * the NFC Core to call other nfc ops entry point concurrently.
 819 */
 820int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
 821{
 822	struct nfc_target *tg;
 823	int i;
 824
 825	pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
 826
 827	device_lock(&dev->dev);
 828
 829	for (i = 0; i < dev->n_targets; i++) {
 830		tg = &dev->targets[i];
 831		if (tg->idx == target_idx)
 832			break;
 833	}
 834
 835	if (i == dev->n_targets) {
 836		device_unlock(&dev->dev);
 837		return -EINVAL;
 838	}
 839
 840	dev->targets_generation++;
 841	dev->n_targets--;
 842	dev->active_target = NULL;
 843
 844	if (dev->n_targets) {
 845		memcpy(&dev->targets[i], &dev->targets[i + 1],
 846		       (dev->n_targets - i) * sizeof(struct nfc_target));
 847	} else {
 848		kfree(dev->targets);
 849		dev->targets = NULL;
 850	}
 851
 852	device_unlock(&dev->dev);
 853
 854	nfc_genl_target_lost(dev, target_idx);
 855
 856	return 0;
 857}
 858EXPORT_SYMBOL(nfc_target_lost);
 859
 860inline void nfc_driver_failure(struct nfc_dev *dev, int err)
 861{
 862	nfc_targets_found(dev, NULL, 0);
 863}
 864EXPORT_SYMBOL(nfc_driver_failure);
 865
 866int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
 867{
 868	struct nfc_se *se;
 869	int rc;
 870
 871	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
 872
 873	se = nfc_find_se(dev, se_idx);
 874	if (se)
 875		return -EALREADY;
 876
 877	se = kzalloc(sizeof(struct nfc_se), GFP_KERNEL);
 878	if (!se)
 879		return -ENOMEM;
 880
 881	se->idx = se_idx;
 882	se->type = type;
 883	se->state = NFC_SE_DISABLED;
 884	INIT_LIST_HEAD(&se->list);
 885
 886	list_add(&se->list, &dev->secure_elements);
 887
 888	rc = nfc_genl_se_added(dev, se_idx, type);
 889	if (rc < 0) {
 890		list_del(&se->list);
 891		kfree(se);
 892
 893		return rc;
 894	}
 895
 896	return 0;
 897}
 898EXPORT_SYMBOL(nfc_add_se);
 899
 900int nfc_remove_se(struct nfc_dev *dev, u32 se_idx)
 901{
 902	struct nfc_se *se, *n;
 903	int rc;
 904
 905	pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
 906
 907	list_for_each_entry_safe(se, n, &dev->secure_elements, list)
 908		if (se->idx == se_idx) {
 909			rc = nfc_genl_se_removed(dev, se_idx);
 910			if (rc < 0)
 911				return rc;
 912
 913			list_del(&se->list);
 914			kfree(se);
 915
 916			return 0;
 917		}
 918
 919	return -EINVAL;
 920}
 921EXPORT_SYMBOL(nfc_remove_se);
 922
 923int nfc_se_transaction(struct nfc_dev *dev, u8 se_idx,
 924		       struct nfc_evt_transaction *evt_transaction)
 925{
 926	int rc;
 927
 928	pr_debug("transaction: %x\n", se_idx);
 929
 930	device_lock(&dev->dev);
 931
 932	if (!evt_transaction) {
 933		rc = -EPROTO;
 934		goto out;
 935	}
 936
 937	rc = nfc_genl_se_transaction(dev, se_idx, evt_transaction);
 938out:
 939	device_unlock(&dev->dev);
 940	return rc;
 941}
 942EXPORT_SYMBOL(nfc_se_transaction);
 943
 944int nfc_se_connectivity(struct nfc_dev *dev, u8 se_idx)
 945{
 946	int rc;
 947
 948	pr_debug("connectivity: %x\n", se_idx);
 949
 950	device_lock(&dev->dev);
 951	rc = nfc_genl_se_connectivity(dev, se_idx);
 952	device_unlock(&dev->dev);
 953	return rc;
 954}
 955EXPORT_SYMBOL(nfc_se_connectivity);
 956
 957static void nfc_release(struct device *d)
 958{
 959	struct nfc_dev *dev = to_nfc_dev(d);
 960	struct nfc_se *se, *n;
 961
 962	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
 963
 964	nfc_genl_data_exit(&dev->genl_data);
 965	kfree(dev->targets);
 966
 967	list_for_each_entry_safe(se, n, &dev->secure_elements, list) {
 968			nfc_genl_se_removed(dev, se->idx);
 969			list_del(&se->list);
 970			kfree(se);
 971	}
 972
 973	ida_simple_remove(&nfc_index_ida, dev->idx);
 974
 975	kfree(dev);
 976}
 977
 978static void nfc_check_pres_work(struct work_struct *work)
 979{
 980	struct nfc_dev *dev = container_of(work, struct nfc_dev,
 981					   check_pres_work);
 982	int rc;
 983
 984	device_lock(&dev->dev);
 985
 986	if (dev->active_target && timer_pending(&dev->check_pres_timer) == 0) {
 987		rc = dev->ops->check_presence(dev, dev->active_target);
 988		if (rc == -EOPNOTSUPP)
 989			goto exit;
 990		if (rc) {
 991			u32 active_target_idx = dev->active_target->idx;
 992			device_unlock(&dev->dev);
 993			nfc_target_lost(dev, active_target_idx);
 994			return;
 995		}
 996
 997		if (!dev->shutting_down)
 998			mod_timer(&dev->check_pres_timer, jiffies +
 999				  msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
1000	}
1001
1002exit:
1003	device_unlock(&dev->dev);
1004}
1005
1006static void nfc_check_pres_timeout(struct timer_list *t)
1007{
1008	struct nfc_dev *dev = from_timer(dev, t, check_pres_timer);
1009
1010	schedule_work(&dev->check_pres_work);
1011}
1012
1013struct class nfc_class = {
1014	.name = "nfc",
1015	.dev_release = nfc_release,
1016};
1017EXPORT_SYMBOL(nfc_class);
1018
1019static int match_idx(struct device *d, const void *data)
1020{
1021	struct nfc_dev *dev = to_nfc_dev(d);
1022	const unsigned int *idx = data;
1023
1024	return dev->idx == *idx;
1025}
1026
1027struct nfc_dev *nfc_get_device(unsigned int idx)
1028{
1029	struct device *d;
1030
1031	d = class_find_device(&nfc_class, NULL, &idx, match_idx);
1032	if (!d)
1033		return NULL;
1034
1035	return to_nfc_dev(d);
1036}
1037
1038/**
1039 * nfc_allocate_device - allocate a new nfc device
1040 *
1041 * @ops: device operations
1042 * @supported_protocols: NFC protocols supported by the device
1043 */
1044struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
1045				    u32 supported_protocols,
1046				    int tx_headroom, int tx_tailroom)
1047{
1048	struct nfc_dev *dev;
1049	int rc;
1050
1051	if (!ops->start_poll || !ops->stop_poll || !ops->activate_target ||
1052	    !ops->deactivate_target || !ops->im_transceive)
1053		return NULL;
1054
1055	if (!supported_protocols)
1056		return NULL;
1057
1058	dev = kzalloc(sizeof(struct nfc_dev), GFP_KERNEL);
1059	if (!dev)
1060		return NULL;
1061
1062	rc = ida_simple_get(&nfc_index_ida, 0, 0, GFP_KERNEL);
1063	if (rc < 0)
1064		goto err_free_dev;
1065	dev->idx = rc;
1066
1067	dev->dev.class = &nfc_class;
1068	dev_set_name(&dev->dev, "nfc%d", dev->idx);
1069	device_initialize(&dev->dev);
1070
1071	dev->ops = ops;
1072	dev->supported_protocols = supported_protocols;
1073	dev->tx_headroom = tx_headroom;
1074	dev->tx_tailroom = tx_tailroom;
1075	INIT_LIST_HEAD(&dev->secure_elements);
1076
1077	nfc_genl_data_init(&dev->genl_data);
1078
1079	dev->rf_mode = NFC_RF_NONE;
1080
1081	/* first generation must not be 0 */
1082	dev->targets_generation = 1;
1083
1084	if (ops->check_presence) {
1085		timer_setup(&dev->check_pres_timer, nfc_check_pres_timeout, 0);
 
 
 
1086		INIT_WORK(&dev->check_pres_work, nfc_check_pres_work);
1087	}
1088
1089	return dev;
1090
1091err_free_dev:
1092	kfree(dev);
1093
1094	return NULL;
1095}
1096EXPORT_SYMBOL(nfc_allocate_device);
1097
1098/**
1099 * nfc_register_device - register a nfc device in the nfc subsystem
1100 *
1101 * @dev: The nfc device to register
1102 */
1103int nfc_register_device(struct nfc_dev *dev)
1104{
1105	int rc;
1106
1107	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1108
 
 
 
 
 
 
 
 
1109	mutex_lock(&nfc_devlist_mutex);
1110	nfc_devlist_generation++;
1111	rc = device_add(&dev->dev);
1112	mutex_unlock(&nfc_devlist_mutex);
1113
1114	if (rc < 0)
1115		return rc;
1116
1117	rc = nfc_llcp_register_device(dev);
1118	if (rc)
1119		pr_err("Could not register llcp device\n");
1120
1121	rc = nfc_genl_device_added(dev);
1122	if (rc)
1123		pr_debug("The userspace won't be notified that the device %s was added\n",
1124			 dev_name(&dev->dev));
1125
1126	dev->rfkill = rfkill_alloc(dev_name(&dev->dev), &dev->dev,
1127				   RFKILL_TYPE_NFC, &nfc_rfkill_ops, dev);
1128	if (dev->rfkill) {
1129		if (rfkill_register(dev->rfkill) < 0) {
1130			rfkill_destroy(dev->rfkill);
1131			dev->rfkill = NULL;
1132		}
1133	}
1134
1135	return 0;
1136}
1137EXPORT_SYMBOL(nfc_register_device);
1138
1139/**
1140 * nfc_unregister_device - unregister a nfc device in the nfc subsystem
1141 *
1142 * @dev: The nfc device to unregister
1143 */
1144void nfc_unregister_device(struct nfc_dev *dev)
1145{
1146	int rc;
1147
1148	pr_debug("dev_name=%s\n", dev_name(&dev->dev));
1149
 
 
1150	if (dev->rfkill) {
1151		rfkill_unregister(dev->rfkill);
1152		rfkill_destroy(dev->rfkill);
1153	}
1154
1155	if (dev->ops->check_presence) {
1156		device_lock(&dev->dev);
1157		dev->shutting_down = true;
1158		device_unlock(&dev->dev);
1159		del_timer_sync(&dev->check_pres_timer);
1160		cancel_work_sync(&dev->check_pres_work);
1161	}
1162
1163	rc = nfc_genl_device_removed(dev);
1164	if (rc)
1165		pr_debug("The userspace won't be notified that the device %s "
1166			 "was removed\n", dev_name(&dev->dev));
1167
1168	nfc_llcp_unregister_device(dev);
1169
1170	mutex_lock(&nfc_devlist_mutex);
1171	nfc_devlist_generation++;
1172	device_del(&dev->dev);
1173	mutex_unlock(&nfc_devlist_mutex);
 
 
1174}
1175EXPORT_SYMBOL(nfc_unregister_device);
1176
1177static int __init nfc_init(void)
1178{
1179	int rc;
1180
1181	pr_info("NFC Core ver %s\n", VERSION);
1182
1183	rc = class_register(&nfc_class);
1184	if (rc)
1185		return rc;
1186
1187	rc = nfc_genl_init();
1188	if (rc)
1189		goto err_genl;
1190
1191	/* the first generation must not be 0 */
1192	nfc_devlist_generation = 1;
1193
1194	rc = rawsock_init();
1195	if (rc)
1196		goto err_rawsock;
1197
1198	rc = nfc_llcp_init();
1199	if (rc)
1200		goto err_llcp_sock;
1201
1202	rc = af_nfc_init();
1203	if (rc)
1204		goto err_af_nfc;
1205
1206	return 0;
1207
1208err_af_nfc:
1209	nfc_llcp_exit();
1210err_llcp_sock:
1211	rawsock_exit();
1212err_rawsock:
1213	nfc_genl_exit();
1214err_genl:
1215	class_unregister(&nfc_class);
1216	return rc;
1217}
1218
1219static void __exit nfc_exit(void)
1220{
1221	af_nfc_exit();
1222	nfc_llcp_exit();
1223	rawsock_exit();
1224	nfc_genl_exit();
1225	class_unregister(&nfc_class);
1226}
1227
1228subsys_initcall(nfc_init);
1229module_exit(nfc_exit);
1230
1231MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
1232MODULE_DESCRIPTION("NFC Core ver " VERSION);
1233MODULE_VERSION(VERSION);
1234MODULE_LICENSE("GPL");
1235MODULE_ALIAS_NETPROTO(PF_NFC);
1236MODULE_ALIAS_GENL_FAMILY(NFC_GENL_NAME);