[PATCH 2/4] crypto: xilinx-trng - Fix return value of xtrng_hwrng_trng_read()

From: Eric Biggers

Date: Sun May 31 2026 - 15:23:52 EST


Implementations of hwrng::read are expected to return the number of
bytes generated. Update xtrng_hwrng_trng_read() to match that.

Fixes: 8979744aca80 ("crypto: xilinx - Add TRNG driver for Versal")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Eric Biggers <ebiggers@xxxxxxxxxx>
---
drivers/crypto/xilinx/xilinx-trng.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/crypto/xilinx/xilinx-trng.c b/drivers/crypto/xilinx/xilinx-trng.c
index a35643baa489..a30b0b3b3685 100644
--- a/drivers/crypto/xilinx/xilinx-trng.c
+++ b/drivers/crypto/xilinx/xilinx-trng.c
@@ -237,22 +237,25 @@ static int xtrng_random_bytes_generate(struct xilinx_rng *rng, u8 *rand_buf_ptr,

static int xtrng_hwrng_trng_read(struct hwrng *hwrng, void *data, size_t max, bool wait)
{
u8 buf[TRNG_SEC_STRENGTH_BYTES];
struct xilinx_rng *rng;
- int ret = -EINVAL, i = 0;
+ int ret = 0, i = 0;

rng = container_of(hwrng, struct xilinx_rng, trng);
while (i < max) {
ret = xtrng_random_bytes_generate(rng, buf, TRNG_SEC_STRENGTH_BYTES, wait);
- if (ret < 0)
+ if (ret < 0) {
+ if (i == 0)
+ return ret;
break;
+ }

memcpy(data + i, buf, min_t(int, ret, (max - i)));
i += min_t(int, ret, (max - i));
}
- return ret;
+ return i;
}

static int xtrng_hwrng_register(struct hwrng *trng)
{
int ret;
--
2.54.0