Linux Audio

Check our new training course

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