@@ -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 */
505508int 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}
538541EXPORT_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+ */
540555int 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
0 commit comments