All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chuanhua Han <chuanhua.han@nxp.com>
To: broonie@kernel.org
Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org,
	eha@deif.com, boris.brezillon@bootlin.com,
	Chuanhua Han <chuanhua.han@nxp.com>,
	stable@vger.kernel.org
Subject: [PATCH] spi: spi-mem: Adjust op len based on message/transfer size limitations
Date: Fri, 21 Sep 2018 15:06:28 +0800	[thread overview]
Message-ID: <20180921070628.35153-3-chuanhua.han@nxp.com> (raw)
In-Reply-To: <20180921070628.35153-1-chuanhua.han@nxp.com>

We need that to adjust the len of the 2nd transfer (called data in
spi-mem) if it's too long to fit in a SPI message or SPI transfer.

Fixes: c36ff266dc82 ("spi: Extend the core to ease integration of SPI memory controllers")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chuanhua Han <chuanhua.han@nxp.com>
Reviewed-by: Boris Brezillon <boris.brezillon@bootlin.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
 drivers/spi/spi-mem.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c
index 990770dfa5cf..ec0c24e873cd 100644
--- a/drivers/spi/spi-mem.c
+++ b/drivers/spi/spi-mem.c
@@ -328,10 +328,25 @@ EXPORT_SYMBOL_GPL(spi_mem_exec_op);
 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
 {
 	struct spi_controller *ctlr = mem->spi->controller;
+	size_t len;
+
+	len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
 
 	if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
 		return ctlr->mem_ops->adjust_op_size(mem, op);
 
+	if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
+		if (len > spi_max_transfer_size(mem->spi))
+			return -EINVAL;
+
+		op->data.nbytes = min3((size_t)op->data.nbytes,
+				       spi_max_transfer_size(mem->spi),
+				       spi_max_message_size(mem->spi) -
+				       len);
+		if (!op->data.nbytes)
+			return -EINVAL;
+	}
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
-- 
2.17.1


  parent reply	other threads:[~2018-09-21  7:06 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-21  7:06 [PATCH 1/2] spi: spi-mem: Add the spi_set_xfer_bpw function Chuanhua Han
2018-09-21  7:06 ` [PATCH 2/2] spi: spi-fsl-dspi: Fix support for XSPI transport mode Chuanhua Han
2018-09-28  6:37   ` Chuanhua Han
2018-09-28  6:55   ` Boris Brezillon
2018-09-29 14:56   ` Esben Haabendal
2018-09-29 15:43     ` Boris Brezillon
2018-09-30  2:49     ` Chuanhua Han
2018-09-21  7:06 ` Chuanhua Han [this message]
2018-09-21  7:15   ` [PATCH] spi: spi-mem: Adjust op len based on message/transfer size limitations Chuanhua Han
2018-09-28  6:37 ` [PATCH 1/2] spi: spi-mem: Add the spi_set_xfer_bpw function Chuanhua Han
2018-09-28  6:44 ` Boris Brezillon
2018-09-28  6:59   ` Chuanhua Han
2018-09-28  7:18     ` Boris Brezillon
2018-09-28  7:29       ` Chuanhua Han
2018-09-28 13:26       ` Mark Brown
2018-10-09  9:52       ` Chuanhua Han
2018-10-09 10:05         ` Boris Brezillon
2018-10-09 10:24           ` Chuanhua Han
2018-10-09 11:20             ` Esben Haabendal
2018-10-10  2:42               ` Chuanhua Han
2018-10-10  6:38                 ` Esben Haabendal
2018-10-09 10:33           ` Mark Brown
2018-10-09 13:50             ` Esben Haabendal
2018-10-09 10:53         ` Esben Haabendal

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=20180921070628.35153-3-chuanhua.han@nxp.com \
    --to=chuanhua.han@nxp.com \
    --cc=boris.brezillon@bootlin.com \
    --cc=broonie@kernel.org \
    --cc=eha@deif.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.