Skip to content

Commit d4ccb13

Browse files
Eric Lapuyadelinvjw
authored andcommitted
NFC: Specify usage for targets found and target lost events
It is now specified that nfc_target_found() and nfc_target_lost() core functions must not be called from an atomic context. This allow us to serialize calls and protect the targets table using the nfc device lock instead of a spinlock. Signed-off-by: Eric Lapuyade <eric.lapuyade@intel.com> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com> Signed-off-by: John W. Linville <linville@tuxdriver.com>
1 parent addfabf commit d4ccb13

3 files changed

Lines changed: 28 additions & 12 deletions

File tree

‎include/net/nfc/nfc.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ struct nfc_dev {
9797
struct nfc_target *targets;
9898
int n_targets;
9999
int targets_generation;
100-
spinlock_t targets_lock;
101100
struct device dev;
102101
bool dev_up;
103102
bool polling;

‎net/nfc/core.c‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,9 @@ EXPORT_SYMBOL(nfc_alloc_recv_skb);
501501
* The device driver must call this function when one or many nfc targets
502502
* are found. After calling this function, the device driver must stop
503503
* polling for targets.
504+
* IMPORTANT: this function must not be called from an atomic context.
505+
* In addition, it must also not be called from a context that would prevent
506+
* the NFC Core to call other nfc ops entry point concurrently.
504507
*/
505508
int nfc_targets_found(struct nfc_dev *dev,
506509
struct nfc_target *targets, int n_targets)
@@ -514,7 +517,7 @@ int nfc_targets_found(struct nfc_dev *dev,
514517
for (i = 0; i < n_targets; i++)
515518
targets[i].idx = dev->target_next_idx++;
516519

517-
spin_lock_bh(&dev->targets_lock);
520+
device_lock(&dev->dev);
518521

519522
dev->targets_generation++;
520523

@@ -524,27 +527,39 @@ int nfc_targets_found(struct nfc_dev *dev,
524527

525528
if (!dev->targets) {
526529
dev->n_targets = 0;
527-
spin_unlock_bh(&dev->targets_lock);
530+
device_unlock(&dev->dev);
528531
return -ENOMEM;
529532
}
530533

531534
dev->n_targets = n_targets;
532-
spin_unlock_bh(&dev->targets_lock);
535+
device_unlock(&dev->dev);
533536

534537
nfc_genl_targets_found(dev);
535538

536539
return 0;
537540
}
538541
EXPORT_SYMBOL(nfc_targets_found);
539542

543+
/**
544+
* nfc_target_lost - inform that an activated target went out of field
545+
*
546+
* @dev: The nfc device that had the activated target in field
547+
* @target_idx: the nfc index of the target
548+
*
549+
* The device driver must call this function when the activated target
550+
* goes out of the field.
551+
* IMPORTANT: this function must not be called from an atomic context.
552+
* In addition, it must also not be called from a context that would prevent
553+
* the NFC Core to call other nfc ops entry point concurrently.
554+
*/
540555
int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
541556
{
542557
struct nfc_target *tg;
543558
int i;
544559

545560
pr_debug("dev_name %s n_target %d\n", dev_name(&dev->dev), target_idx);
546561

547-
spin_lock_bh(&dev->targets_lock);
562+
device_lock(&dev->dev);
548563

549564
for (i = 0; i < dev->n_targets; i++) {
550565
tg = &dev->targets[i];
@@ -553,7 +568,7 @@ int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
553568
}
554569

555570
if (i == dev->n_targets) {
556-
spin_unlock_bh(&dev->targets_lock);
571+
device_unlock(&dev->dev);
557572
return -EINVAL;
558573
}
559574

@@ -569,7 +584,7 @@ int nfc_target_lost(struct nfc_dev *dev, u32 target_idx)
569584
dev->targets = NULL;
570585
}
571586

572-
spin_unlock_bh(&dev->targets_lock);
587+
device_unlock(&dev->dev);
573588

574589
nfc_genl_target_lost(dev, target_idx);
575590

@@ -607,8 +622,10 @@ static void nfc_check_pres_work(struct work_struct *work)
607622
mod_timer(&dev->check_pres_timer, jiffies +
608623
msecs_to_jiffies(NFC_CHECK_PRES_FREQ_MS));
609624
} else {
610-
nfc_target_lost(dev, dev->active_target->idx);
611-
dev->active_target = NULL;
625+
u32 active_target_idx = dev->active_target->idx;
626+
device_unlock(&dev->dev);
627+
nfc_target_lost(dev, active_target_idx);
628+
return;
612629
}
613630
}
614631

@@ -681,9 +698,9 @@ struct nfc_dev *nfc_allocate_device(struct nfc_ops *ops,
681698
dev->tx_headroom = tx_headroom;
682699
dev->tx_tailroom = tx_tailroom;
683700

684-
spin_lock_init(&dev->targets_lock);
685701
nfc_genl_data_init(&dev->genl_data);
686702

703+
687704
/* first generation must not be 0 */
688705
dev->targets_generation = 1;
689706

‎net/nfc/netlink.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static int nfc_genl_dump_targets(struct sk_buff *skb,
128128
cb->args[1] = (long) dev;
129129
}
130130

131-
spin_lock_bh(&dev->targets_lock);
131+
device_lock(&dev->dev);
132132

133133
cb->seq = dev->targets_generation;
134134

@@ -141,7 +141,7 @@ static int nfc_genl_dump_targets(struct sk_buff *skb,
141141
i++;
142142
}
143143

144-
spin_unlock_bh(&dev->targets_lock);
144+
device_unlock(&dev->dev);
145145

146146
cb->args[0] = i;
147147

0 commit comments

Comments
 (0)