Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] mtd: rawnand: r852: preserve card changes during detection work
@ 2026-11-09  0:22 James Hilliard
  2026-12-04 21:14 ` Miquel Raynal
  2026-12-04 21:33 ` Miquel Raynal
  0 siblings, 2 replies; 3+ messages in thread
From: James Hilliard @ 2026-11-09  0:22 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Maxim Levitsky
  Cc: linux-mtd, linux-kernel, stable, James Hilliard

r852_update_card_detect() reads and writes the card interrupt enable
register without holding irqlock. A card interrupt can mask detection
interrupts and mark the card unstable between those accesses, only for
the worker to overwrite the mask with its saved value.

The worker also calls this helper before and after registering or
unregistering a card. An interrupt between these calls starts a new
debounce interval, but the second call clears card_unstable and enables
interrupts again before that interval has elapsed. The interrupt can
also change card_detected between the worker's registration checks and
its media-property update.

Hold irqlock across the interrupt mask update and leave the mask alone
while the card is unstable. Move the transition to stable state into
the locked card sample, and return that sample for both of the worker's
registration decisions. Sample the write-protect and media-type fields
there as well when a present card is not yet registered, so a later IRQ
cannot make registration use properties left over from a previous card.
Keep the properties of an already registered card unchanged.

Keep registration failure from overwriting a newer interrupt's card
state, so the next delayed work invocation can process that change.

Fixes: 67e054e91924 ("mtd: nand: Add driver for Ricoh xD/SmartMedia reader")
Cc: stable@vger.kernel.org
Closes: https://lore.kernel.org/r/sashiko-outbox-204068@kernel.org/
Closes: https://lore.kernel.org/r/sashiko-outbox-204135@kernel.org/
Link: https://lore.kernel.org/r/sashiko-outbox-204169@kernel.org/
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
Changes in v3:
- Sample media properties with card presence under irqlock.
- Keep registered-card properties unchanged and log the sampled decision.
- Link to v2: https://patch.msgid.link/20261108-submit-r852-card-detect-lock-v1-v2-1-e3251bc3680a@gmail.com

Changes in v2:
- Keep new debounce events pending until the next card-state sample.
- Use one presence sample for the worker's registration decisions.
- Protect the registration-failure update from newer IRQ events.
- Link to v1: https://patch.msgid.link/20261108-submit-r852-card-detect-lock-v1-v1-1-5514e7543209@gmail.com
---
 drivers/mtd/nand/raw/r852.c | 78 ++++++++++++++++++++++-----------------------
 1 file changed, 38 insertions(+), 40 deletions(-)

diff --git a/drivers/mtd/nand/raw/r852.c b/drivers/mtd/nand/raw/r852.c
index 92c47d351c27..dbee043e6d8d 100644
--- a/drivers/mtd/nand/raw/r852.c
+++ b/drivers/mtd/nand/raw/r852.c
@@ -554,18 +554,28 @@ static void r852_engine_disable(struct r852_device *dev)
 }
 
 /*
- * Test if card is present
+ * Sample card presence and the properties needed for registration
  */
 
-static void r852_card_update_present(struct r852_device *dev)
+static bool r852_card_update_present(struct r852_device *dev)
 {
 	unsigned long flags;
 	uint8_t reg;
+	bool present;
 
 	spin_lock_irqsave(&dev->irqlock, flags);
 	reg = r852_read_reg(dev, R852_CARD_STA);
-	dev->card_detected = !!(reg & R852_CARD_STA_PRESENT);
+	present = !!(reg & R852_CARD_STA_PRESENT);
+	dev->card_detected = present;
+	dev->card_unstable = 0;
+	if (present && !dev->card_registered) {
+		dev->readonly = reg & R852_CARD_STA_RO;
+		reg = r852_read_reg(dev, R852_DMA_CAP);
+		dev->sm = (reg & (R852_DMA1 | R852_DMA2)) && (reg & R852_SMBIT);
+	}
 	spin_unlock_irqrestore(&dev->irqlock, flags);
+
+	return present;
 }
 
 /*
@@ -574,8 +584,14 @@ static void r852_card_update_present(struct r852_device *dev)
  */
 static void r852_update_card_detect(struct r852_device *dev)
 {
-	int card_detect_reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
-	dev->card_unstable = 0;
+	unsigned long flags;
+	int card_detect_reg;
+
+	spin_lock_irqsave(&dev->irqlock, flags);
+	if (dev->card_unstable)
+		goto out;
+
+	card_detect_reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
 
 	card_detect_reg &= ~(R852_CARD_IRQ_REMOVE | R852_CARD_IRQ_INSERT);
 	card_detect_reg |= R852_CARD_IRQ_GENABLE;
@@ -584,6 +600,8 @@ static void r852_update_card_detect(struct r852_device *dev)
 		R852_CARD_IRQ_REMOVE : R852_CARD_IRQ_INSERT;
 
 	r852_write_reg(dev, R852_CARD_IRQ_ENABLE, card_detect_reg);
+out:
+	spin_unlock_irqrestore(&dev->irqlock, flags);
 }
 
 static ssize_t media_type_show(struct device *sys_dev,
@@ -599,32 +617,6 @@ static ssize_t media_type_show(struct device *sys_dev,
 static DEVICE_ATTR_RO(media_type);
 
 
-/* Detect properties of card in slot */
-static void r852_update_media_status(struct r852_device *dev)
-{
-	uint8_t reg;
-	unsigned long flags;
-	int readonly;
-
-	spin_lock_irqsave(&dev->irqlock, flags);
-	if (!dev->card_detected) {
-		message("card removed");
-		spin_unlock_irqrestore(&dev->irqlock, flags);
-		return ;
-	}
-
-	readonly  = r852_read_reg(dev, R852_CARD_STA) & R852_CARD_STA_RO;
-	reg = r852_read_reg(dev, R852_DMA_CAP);
-	dev->sm = (reg & (R852_DMA1 | R852_DMA2)) && (reg & R852_SMBIT);
-
-	message("detected %s %s card in slot",
-		dev->sm ? "SmartMedia" : "xD",
-		readonly ? "readonly" : "writeable");
-
-	dev->readonly = readonly;
-	spin_unlock_irqrestore(&dev->irqlock, flags);
-}
-
 /*
  * Register the nand device
  * Called when the card is detected
@@ -632,6 +624,7 @@ static void r852_update_media_status(struct r852_device *dev)
 static int r852_register_nand_device(struct r852_device *dev)
 {
 	struct mtd_info *mtd = nand_to_mtd(dev->chip);
+	unsigned long flags;
 
 	WARN_ON(dev->card_registered);
 
@@ -657,7 +650,10 @@ static int r852_register_nand_device(struct r852_device *dev)
 	nand_cleanup(dev->chip);
 error1:
 	/* Force card redetect */
-	dev->card_detected = 0;
+	spin_lock_irqsave(&dev->irqlock, flags);
+	if (!dev->card_unstable)
+		dev->card_detected = 0;
+	spin_unlock_irqrestore(&dev->irqlock, flags);
 	return -1;
 }
 
@@ -684,23 +680,25 @@ static void r852_card_detect_work(struct work_struct *work)
 {
 	struct r852_device *dev =
 		container_of(work, struct r852_device, card_detect_work.work);
+	bool present;
 
-	r852_card_update_present(dev);
+	present = r852_card_update_present(dev);
 	r852_update_card_detect(dev);
-	dev->card_unstable = 0;
 
 	/* False alarm */
-	if (dev->card_detected == dev->card_registered)
+	if (present == dev->card_registered)
 		goto exit;
 
-	/* Read media properties */
-	r852_update_media_status(dev);
-
 	/* Register the card */
-	if (dev->card_detected)
+	if (present) {
+		message("detected %s %s card in slot",
+			dev->sm ? "SmartMedia" : "xD",
+			dev->readonly ? "readonly" : "writeable");
 		r852_register_nand_device(dev);
-	else
+	} else {
+		message("card removed");
 		r852_unregister_nand_device(dev);
+	}
 exit:
 	r852_update_card_detect(dev);
 }

---
base-commit: 30daaf3e7897b4a8d9b72e5b0c86dd6e2a4d7d85
change-id: 20261108-submit-r852-card-detect-lock-v1-713bf5cd926b

Best regards,
--  
James Hilliard <james.hilliard1@gmail.com>


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] mtd: rawnand: r852: preserve card changes during detection work
  2026-11-09  0:22 [PATCH v3] mtd: rawnand: r852: preserve card changes during detection work James Hilliard
@ 2026-12-04 21:14 ` Miquel Raynal
  2026-12-04 21:33 ` Miquel Raynal
  1 sibling, 0 replies; 3+ messages in thread
From: Miquel Raynal @ 2026-12-04 21:14 UTC (permalink / raw)
  To: James Hilliard
  Cc: Richard Weinberger, Vignesh Raghavendra, Maxim Levitsky,
	linux-mtd, linux-kernel, stable

On 08/11/2026 at 17:22:18 -07, James Hilliard <james.hilliard1@gmail.com> wrote:

> r852_update_card_detect() reads and writes the card interrupt enable
> register without holding irqlock. A card interrupt can mask detection
> interrupts and mark the card unstable between those accesses, only for
> the worker to overwrite the mask with its saved value.
>
> The worker also calls this helper before and after registering or
> unregistering a card. An interrupt between these calls starts a new
> debounce interval, but the second call clears card_unstable and enables
> interrupts again before that interval has elapsed. The interrupt can
> also change card_detected between the worker's registration checks and
> its media-property update.
>
> Hold irqlock across the interrupt mask update and leave the mask alone
> while the card is unstable. Move the transition to stable state into
> the locked card sample, and return that sample for both of the worker's
> registration decisions. Sample the write-protect and media-type fields
> there as well when a present card is not yet registered, so a later IRQ
> cannot make registration use properties left over from a previous card.
> Keep the properties of an already registered card unchanged.
>
> Keep registration failure from overwriting a newer interrupt's card
> state, so the next delayed work invocation can process that change.
>
> Fixes: 67e054e91924 ("mtd: nand: Add driver for Ricoh xD/SmartMedia reader")
> Cc: stable@vger.kernel.org
> Closes: https://lore.kernel.org/r/sashiko-outbox-204068@kernel.org/
> Closes: https://lore.kernel.org/r/sashiko-outbox-204135@kernel.org/
> Link: https://lore.kernel.org/r/sashiko-outbox-204169@kernel.org/
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>

Same, after applying the first patch, these do not apply. Please make a
series for all the r852 fixes.

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] mtd: rawnand: r852: preserve card changes during detection work
  2026-11-09  0:22 [PATCH v3] mtd: rawnand: r852: preserve card changes during detection work James Hilliard
  2026-12-04 21:14 ` Miquel Raynal
@ 2026-12-04 21:33 ` Miquel Raynal
  1 sibling, 0 replies; 3+ messages in thread
From: Miquel Raynal @ 2026-12-04 21:33 UTC (permalink / raw)
  To: Richard Weinberger, Vignesh Raghavendra, Maxim Levitsky,
	James Hilliard
  Cc: linux-mtd, linux-kernel, stable

On Sun, 08 Nov 2026 17:22:18 -0700, James Hilliard wrote:
> r852_update_card_detect() reads and writes the card interrupt enable
> register without holding irqlock. A card interrupt can mask detection
> interrupts and mark the card unstable between those accesses, only for
> the worker to overwrite the mask with its saved value.
> 
> The worker also calls this helper before and after registering or
> unregistering a card. An interrupt between these calls starts a new
> debounce interval, but the second call clears card_unstable and enables
> interrupts again before that interval has elapsed. The interrupt can
> also change card_detected between the worker's registration checks and
> its media-property update.
> 
> [...]

Applied to nand/next, thanks!

[1/1] mtd: rawnand: r852: preserve card changes during detection work
      commit: 167c909d97d9cc740615a78154d72839e661fbd5

Patche(s) should be available on mtd/linux.git and will be
part of the next PR (provided that no robot complains by then).

Kind regards,
Miquèl


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04 21:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-09  0:22 [PATCH v3] mtd: rawnand: r852: preserve card changes during detection work James Hilliard
2026-12-04 21:14 ` Miquel Raynal
2026-12-04 21:33 ` Miquel Raynal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox