1 /**
2 * @file drivers/entropy.h
3 *
4 * @brief Public APIs for the entropy driver.
5 */
6
7 /*
8 * Copyright (c) 2016 ARM Ltd.
9 * Copyright (c) 2017 Intel Corporation
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 */
13 #ifndef ZEPHYR_INCLUDE_DRIVERS_ENTROPY_H_
14 #define ZEPHYR_INCLUDE_DRIVERS_ENTROPY_H_
15
16 /**
17 * @brief Entropy Interface
18 * @defgroup entropy_interface Entropy Interface
19 * @ingroup io_interfaces
20 * @{
21 */
22
23 #include <errno.h>
24
25 #include <zephyr/types.h>
26 #include <zephyr/device.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /**
33 * @typedef entropy_get_entropy_t
34 * @brief Callback API to get entropy.
35 *
36 * See entropy_get_entropy() for argument description
37 */
38 typedef int (*entropy_get_entropy_t)(const struct device *dev,
39 uint8_t *buffer,
40 uint16_t length);
41 /**
42 * @typedef entropy_get_entropy_isr_t
43 * @brief Callback API to get entropy from an ISR.
44 *
45 * See entropy_get_entropy_isr() for argument description
46 */
47 typedef int (*entropy_get_entropy_isr_t)(const struct device *dev,
48 uint8_t *buffer,
49 uint16_t length,
50 uint32_t flags);
51 __subsystem struct entropy_driver_api {
52 entropy_get_entropy_t get_entropy;
53 entropy_get_entropy_isr_t get_entropy_isr;
54 };
55
56 /**
57 * @brief Fills a buffer with entropy. Blocks if required in order to
58 * generate the necessary random data.
59 *
60 * @param dev Pointer to the entropy device.
61 * @param buffer Buffer to fill with entropy.
62 * @param length Buffer length.
63 * @retval 0 on success.
64 * @retval -ERRNO errno code on error.
65 */
66 __syscall int entropy_get_entropy(const struct device *dev,
67 uint8_t *buffer,
68 uint16_t length);
69
z_impl_entropy_get_entropy(const struct device * dev,uint8_t * buffer,uint16_t length)70 static inline int z_impl_entropy_get_entropy(const struct device *dev,
71 uint8_t *buffer,
72 uint16_t length)
73 {
74 const struct entropy_driver_api *api =
75 (const struct entropy_driver_api *)dev->api;
76
77 __ASSERT(api->get_entropy != NULL,
78 "Callback pointer should not be NULL");
79 return api->get_entropy(dev, buffer, length);
80 }
81
82 /* Busy-wait for random data to be ready */
83 #define ENTROPY_BUSYWAIT BIT(0)
84
85 /**
86 * @brief Fills a buffer with entropy in a non-blocking or busy-wait manner.
87 * Callable from ISRs.
88 *
89 * @param dev Pointer to the device structure.
90 * @param buffer Buffer to fill with entropy.
91 * @param length Buffer length.
92 * @param flags Flags to modify the behavior of the call.
93 * @retval number of bytes filled with entropy or -error.
94 */
entropy_get_entropy_isr(const struct device * dev,uint8_t * buffer,uint16_t length,uint32_t flags)95 static inline int entropy_get_entropy_isr(const struct device *dev,
96 uint8_t *buffer,
97 uint16_t length,
98 uint32_t flags)
99 {
100 const struct entropy_driver_api *api =
101 (const struct entropy_driver_api *)dev->api;
102
103 if (unlikely(!api->get_entropy_isr)) {
104 return -ENOTSUP;
105 }
106
107 return api->get_entropy_isr(dev, buffer, length, flags);
108 }
109
110
111 #ifdef __cplusplus
112 }
113 #endif
114
115 /**
116 * @}
117 */
118
119 #include <syscalls/entropy.h>
120
121 #endif /* ZEPHYR_INCLUDE_DRIVERS_ENTROPY_H_ */
122