From: Lucas Tanure <tanureal@opensource.cirrus.com>
To: Mark Brown <broonie@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J . Wysocki" <rafael@kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <patches@opensource.cirrus.com>,
Lucas Tanure <tanureal@opensource.cirrus.com>
Subject: [PATCH v3 2/2] regmap: spi: Check raw_[read|write] against max message size
Date: Wed, 15 Sep 2021 13:09:51 +0100 [thread overview]
Message-ID: <20210915120951.29907-2-tanureal@opensource.cirrus.com> (raw)
In-Reply-To: <20210915120951.29907-1-tanureal@opensource.cirrus.com>
regmap-spi will split data and address between two transfers
in the same message, so max_[read|write] must include space
for the address and padding
Signed-off-by: Lucas Tanure <tanureal@opensource.cirrus.com>
---
Changes v3:
New series
drivers/base/regmap/regmap-spi.c | 4 ++++
drivers/base/regmap/regmap.c | 15 +++++++++++++++
include/linux/regmap.h | 3 +++
3 files changed, 22 insertions(+)
diff --git a/drivers/base/regmap/regmap-spi.c b/drivers/base/regmap/regmap-spi.c
index 0e6552e57ecf..1434c502e340 100644
--- a/drivers/base/regmap/regmap-spi.c
+++ b/drivers/base/regmap/regmap-spi.c
@@ -123,6 +123,10 @@ static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi,
bus->free_on_exit = true;
bus->max_raw_read = max_size;
bus->max_raw_write = max_size;
+
+ if (spi_max_message_size(spi) != SIZE_MAX)
+ bus->max_combined_rw = spi_max_message_size(spi);
+
return bus;
}
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 21a0c2562ec0..a99152f010f8 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -735,6 +735,7 @@ struct regmap *__regmap_init(struct device *dev,
struct regmap *map;
int ret = -EINVAL;
enum regmap_endian reg_endian, val_endian;
+ size_t reg_pad_size;
int i, j;
if (!config)
@@ -840,6 +841,20 @@ struct regmap *__regmap_init(struct device *dev,
if (bus) {
map->max_raw_read = bus->max_raw_read;
map->max_raw_write = bus->max_raw_write;
+ if (bus->max_combined_rw) {
+ reg_pad_size = map->format.reg_bytes + map->format.pad_bytes;
+
+ if (map->max_raw_read + reg_pad_size > bus->max_combined_rw)
+ map->max_raw_read -= reg_pad_size;
+ if (map->max_raw_write + reg_pad_size > bus->max_combined_rw)
+ map->max_raw_write -= reg_pad_size;
+
+ if (map->max_raw_read < map->format.buf_size ||
+ map->max_raw_write < map->format.buf_size) {
+ ret = -EINVAL;
+ goto err_hwlock;
+ }
+ }
}
map->dev = dev;
map->bus = bus;
diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index e3c9a25a853a..a720f578b8e6 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -506,6 +506,8 @@ typedef void (*regmap_hw_free_context)(void *context);
* @max_raw_read: Max raw read size that can be used on the bus.
* @max_raw_write: Max raw write size that can be used on the bus.
* @free_on_exit: kfree this on exit of regmap
+ * @max_combined_rw: Max size for raw_read + raw_write, when they are issued
+ * together as part of the same message
*/
struct regmap_bus {
bool fast_io;
@@ -523,6 +525,7 @@ struct regmap_bus {
enum regmap_endian val_format_endian_default;
size_t max_raw_read;
size_t max_raw_write;
+ size_t max_combined_rw;
bool free_on_exit;
};
--
2.33.0
next prev parent reply other threads:[~2021-09-15 12:10 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-15 12:09 [PATCH v3 1/2] regmap: spi: Set regmap max raw r/w from max_transfer_size Lucas Tanure
2021-09-15 12:09 ` Lucas Tanure [this message]
2021-09-15 12:51 ` [PATCH v3 2/2] regmap: spi: Check raw_[read|write] against max message size Mark Brown
2021-09-15 12:34 ` [PATCH v3 1/2] regmap: spi: Set regmap max raw r/w from max_transfer_size Mark Brown
2021-09-15 12:37 ` Lucas tanure
2021-09-15 12:57 ` Mark Brown
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=20210915120951.29907-2-tanureal@opensource.cirrus.com \
--to=tanureal@opensource.cirrus.com \
--cc=broonie@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=patches@opensource.cirrus.com \
--cc=rafael@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®