From 668e31f7dba2e149693edde068e9eb9fa326643f Mon Sep 17 00:00:00 2001 From: Carles Cufi Date: Thu, 24 May 2018 20:10:53 +0200 Subject: [PATCH] drivers: entropy: Introduce ISR-specific entropy function In order to address the requirements of the kernel boot process, an optional ISR-specific function is declared to allow entropy collection outside of thread mode and without using any kernel primitives. It also includes a parameter to optionally busy-wait for hardware entropy generation. Signed-off-by: Carles Cufi --- include/entropy.h | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/include/entropy.h b/include/entropy.h index 587e0968436..f8d919558f2 100644 --- a/include/entropy.h +++ b/include/entropy.h @@ -36,13 +36,24 @@ extern "C" { typedef int (*entropy_get_entropy_t)(struct device *dev, u8_t *buffer, u16_t length); - +/** + * @typedef entropy_get_entropy_isr_t + * @brief Callback API to get entropy from an ISR. + * + * See entropy_get_entropy_isr() for argument description + */ +typedef int (*entropy_get_entropy_isr_t)(struct device *dev, + u8_t *buffer, + u16_t length, + u32_t flags); struct entropy_driver_api { - entropy_get_entropy_t get_entropy; + entropy_get_entropy_t get_entropy; + entropy_get_entropy_isr_t get_entropy_isr; }; /** - * @brief Fills a buffer with entropy. + * @brief Fills a buffer with entropy. Blocks if required in order to + * generate the necessary random data. * * @param dev Pointer to the entropy device. * @param buffer Buffer to fill with entropy. @@ -64,6 +75,33 @@ static inline int _impl_entropy_get_entropy(struct device *dev, return api->get_entropy(dev, buffer, length); } +/* Busy-wait for random data to be ready */ +#define ENTROPY_BUSYWAIT BIT(0) + +/** + * @brief Fills a buffer with entropy in a non-blocking or busy-wait manner. + * Callable from ISRs. + * + * @param dev Pointer to the device structure. + * @param buffer Buffer to fill with entropy. + * @param length Buffer length. + * @param flags Flags to modify the behavior of the call. + * @retval number of bytes filled with entropy or -error. + */ +static inline int entropy_get_entropy_isr(struct device *dev, + u8_t *buffer, + u16_t length, + u32_t flags) +{ + const struct entropy_driver_api *api = dev->driver_api; + + if (!api->get_entropy_isr) { + return -ENOTSUP; + } + return api->get_entropy_isr(dev, buffer, length, flags); +} + + #ifdef __cplusplus } #endif