zephyr/drivers/entropy/entropy_b91_trng.c
Flavio Ceolin 5133ac8af4 entropy: b91_trng: Fix callback return
get_entropy_isr() has to return the number of bytes copied or a negative
value for error. Since this driver is assuming that it will always
(????) get the number of requested bytes, change the function to return it
instead of 0.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-10-09 10:16:19 +02:00

72 lines
1.5 KiB
C

/*
* Copyright (c) 2021 Telink Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT telink_b91_trng
#include <trng.h>
#include <zephyr/drivers/entropy.h>
#include <string.h>
/* API implementation: driver initialization */
static int entropy_b91_trng_init(const struct device *dev)
{
ARG_UNUSED(dev);
trng_init();
return 0;
}
/* API implementation: get_entropy */
static int entropy_b91_trng_get_entropy(const struct device *dev,
uint8_t *buffer, uint16_t length)
{
ARG_UNUSED(dev);
uint32_t value = 0;
while (length) {
value = trng_rand();
if (length >= sizeof(value)) {
memcpy(buffer, &value, sizeof(value));
buffer += sizeof(value);
length -= sizeof(value);
} else {
memcpy(buffer, &value, length);
break;
}
}
return 0;
}
/* API implementation: get_entropy_isr */
static int entropy_b91_trng_get_entropy_isr(const struct device *dev,
uint8_t *buffer, uint16_t length,
uint32_t flags)
{
ARG_UNUSED(flags);
/* No specific handling in case of running from ISR, just call standard API */
entropy_b91_trng_get_entropy(dev, buffer, length);
return length;
}
/* Entropy driver APIs structure */
static const struct entropy_driver_api entropy_b91_trng_api = {
.get_entropy = entropy_b91_trng_get_entropy,
.get_entropy_isr = entropy_b91_trng_get_entropy_isr
};
/* Entropy driver registration */
DEVICE_DT_INST_DEFINE(0, entropy_b91_trng_init,
NULL, NULL, NULL,
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
&entropy_b91_trng_api);