public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Bin Meng <bmeng.cn@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 3/4] dm: spi: Check cs number before accessing slaves
Date: Mon,  9 Sep 2019 06:00:02 -0700	[thread overview]
Message-ID: <1568034003-14675-3-git-send-email-bmeng.cn@gmail.com> (raw)
In-Reply-To: <1568034003-14675-1-git-send-email-bmeng.cn@gmail.com>

Add chip select number check in spi_find_chip_select().

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>

---

Changes in v2:
- move the chip select number check to spi_find_chip_select()

 drivers/spi/spi-uclass.c | 45 ++++++++++++++++++++++++++-------------------
 include/spi.h            |  3 ++-
 2 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/drivers/spi/spi-uclass.c b/drivers/spi/spi-uclass.c
index 24de0b5..cdeceb5 100644
--- a/drivers/spi/spi-uclass.c
+++ b/drivers/spi/spi-uclass.c
@@ -179,7 +179,32 @@ int spi_chip_select(struct udevice *dev)
 
 int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
 {
+	struct dm_spi_ops *ops;
+	struct spi_cs_info info;
 	struct udevice *dev;
+	int ret;
+
+	/*
+	 * Ask the driver. For the moment we don't have CS info.
+	 * When we do we could provide the driver with a helper function
+	 * to figure out what chip selects are valid, or just handle the
+	 * request.
+	 */
+	ops = spi_get_ops(bus);
+	if (ops->cs_info) {
+		ret = ops->cs_info(bus, cs, &info);
+	} else {
+		/*
+		 * We could assume there is at least one valid chip select.
+		 * The driver didn't care enough to tell us.
+		 */
+		ret = 0;
+	}
+
+	if (ret) {
+		printf("Invalid cs %d (err=%d)\n", cs, ret);
+		return ret;
+	}
 
 	for (device_find_first_child(bus, &dev); dev;
 	     device_find_next_child(&dev)) {
@@ -214,7 +239,6 @@ int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
 int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
 {
 	struct spi_cs_info local_info;
-	struct dm_spi_ops *ops;
 	int ret;
 
 	if (!info)
@@ -223,24 +247,7 @@ int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
 	/* If there is a device attached, return it */
 	info->dev = NULL;
 	ret = spi_find_chip_select(bus, cs, &info->dev);
-	if (!ret)
-		return 0;
-
-	/*
-	 * Otherwise ask the driver. For the moment we don't have CS info.
-	 * When we do we could provide the driver with a helper function
-	 * to figure out what chip selects are valid, or just handle the
-	 * request.
-	 */
-	ops = spi_get_ops(bus);
-	if (ops->cs_info)
-		return ops->cs_info(bus, cs, info);
-
-	/*
-	 * We could assume there is at least one valid chip select.
-	 * The driver didn't care enough to tell us.
-	 */
-	return 0;
+	return ret == -ENODEV ? 0 : ret;
 }
 
 int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
diff --git a/include/spi.h b/include/spi.h
index cc344de..6b144bc 100644
--- a/include/spi.h
+++ b/include/spi.h
@@ -528,7 +528,8 @@ int spi_chip_select(struct udevice *slave);
  * @bus:	SPI bus to search
  * @cs:		Chip select to look for
  * @devp:	Returns the slave device if found
- * @return 0 if found, -ENODEV on error
+ * @return 0 if found, -EINVAL if cs is invalid, -ENODEV if no device attached,
+ *	   other -ve value on error
  */
 int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);
 
-- 
2.7.4

  parent reply	other threads:[~2019-09-09 13:00 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-09 13:00 [U-Boot] [PATCH v2 1/4] dm: spi: Return 0 if driver does not implement ops->cs_info Bin Meng
2019-09-09 13:00 ` [U-Boot] [PATCH v2 2/4] dm: spi: Change cs_info op to return -EINVAL for invalid cs num Bin Meng
2019-09-09 13:00 ` Bin Meng [this message]
2019-10-16 14:05   ` [U-Boot] [PATCH v2 3/4] dm: spi: Check cs number before accessing slaves Jagan Teki
2019-10-16 15:21   ` Jagan Teki
2019-10-29 10:15     ` Bin Meng
2019-11-18  7:15       ` Bin Meng
2020-01-09 13:47         ` Bin Meng
2020-01-09 13:48           ` Jagan Teki
2019-09-09 13:00 ` [U-Boot] [PATCH v2 4/4] test: dm: spi: Fix sandbox dm_test_spi_find() Bin Meng
2019-10-16 14:07   ` Jagan Teki
2019-09-29  8:04 ` [U-Boot] [PATCH v2 1/4] dm: spi: Return 0 if driver does not implement ops->cs_info Bin Meng
2019-10-08 12:59   ` Bin Meng
2019-10-14  2:33     ` Bin Meng
2019-10-16 10:14   ` Jagan Teki
2019-10-16 13:58 ` Jagan Teki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1568034003-14675-3-git-send-email-bmeng.cn@gmail.com \
    --to=bmeng.cn@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox