Skip to contentSkip to conversation

spi: Allow to have all native CSs in use along with GPIOs

PATCH In review Andy Shevchenko 2 participants 3 messages Apr 20, 2021 – Apr 21, 2021 SPI

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 spi: Allow to have all native CSs in use along with GPIOs

The commit 7d93aecdb58d ("spi: Add generic support for unused native cs with cs-gpios") excludes the valid case for the controllers that doesn't need to switch native CS in order to perform the transfer, i.e. when

0 native
... ...
<n> - 1 native
<n> GPIO
<n> + 1 GPIO
... ...

where <n> defines maximum of native CSs supported by the controller.

To allow this, bail out from spi_get_gpio_descs() conditionally for the controllers which explicitly marked with SPI_MASTER_GPIO_SS.

Fixes 7d93aecdb58d ("spi: Add generic support for unused native cs with cs-gpios")

---
v2: no changes

 drivers/spi/spi.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/spi/spi.c +3 -2
index 36c46feab6d4..9c3730a9f7d5 100644
@@ -2610,8 +2610,9 @@ static int spi_get_gpio_descs(struct spi_controller *ctlr)
 	}
 
 	ctlr->unused_native_cs = ffz(native_cs_mask);
-	if (num_cs_gpios && ctlr->max_native_cs &&
-	    ctlr->unused_native_cs >= ctlr->max_native_cs) {
+
+	if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios &&
+	    ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
 		dev_err(dev, "No unused native chip select available\n");
 		return -EINVAL;
 	}
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 2/2 spi: Avoid undefined behaviour when counting unused native CSs

ffz(), that has been used to count unused native CSs, might cause undefined behaviour when called against ~0U. To fix that, open code it with ffs(~value) - 1.

Fixes 7d93aecdb58d ("spi: Add generic support for unused native cs with cs-gpios")

---
v2: decoded UB abbreviation (Mark)

 drivers/spi/spi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi.c +1 -1
index 9c3730a9f7d5..01f95bee2ac8 100644
@@ -2609,7 +2609,7 @@ static int spi_get_gpio_descs(struct spi_controller *ctlr)
 		native_cs_mask |= BIT(i);
 	}
 
-	ctlr->unused_native_cs = ffz(native_cs_mask);
+	ctlr->unused_native_cs = ffs(~native_cs_mask) - 1;
 
 	if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios &&
 	    ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
Signature · 1 line
2.30.2
maintainer
#2 lore raw
In reply to Andy Shevchenko
Quoted 4 lines · Andy Shevchenko

The commit 7d93aecdb58d ("spi: Add generic support for unused native cs with cs-gpios") excludes the valid case for the controllers that doesn't need to switch native CS in order to perform the transfer, i.e. when

Quoted 8 lines

0 native
... ...
<n> - 1 native
<n> GPIO
<n> + 1 GPIO
... ...

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/2] spi: Allow to have all native CSs in use along with GPIOs

      commit: dbaca8e56ea3f23fa215f48c2d46dd03ede06e02
[2/2] spi: Avoid undefined behaviour when counting unused native CSs
      commit: f60d7270c8a3d2beb1c23ae0da42497afa3584c2

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark