Skip to content

Commit 5d0cdb2

Browse files
committed
device: re-implement device_is_ready
Use device_get() instead. Also discourage its usage in favor of device_get(). Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
1 parent e3c8c09 commit 5d0cdb2

File tree

2 files changed

+29
-44
lines changed

2 files changed

+29
-44
lines changed

include/zephyr/device.h

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ typedef int16_t device_handle_t;
209209
* device object can be obtained with `DEVICE_DT_GET(node_id)` from any source
210210
* file that includes `<zephyr/device.h>` (even from extensions, when
211211
* @kconfig{CONFIG_LLEXT_EXPORT_DEVICES} is enabled). Before using the
212-
* pointer, the referenced object should be checked using device_is_ready().
212+
* pointer, its usage should be registered using device_get().
213213
*
214214
* @param node_id The devicetree node identifier.
215215
* @param init_fn Pointer to the device's initialization function, which will be
@@ -302,8 +302,8 @@ typedef int16_t device_handle_t;
302302
*
303303
* If there are multiple, this returns an arbitrary one.
304304
*
305-
* If this returns non-NULL, the device must be checked for readiness
306-
* before use, e.g. with device_is_ready().
305+
* If this returns non-NULL, device usage must be notified using
306+
* device_get().
307307
*
308308
* @param compat lowercase-and-underscores devicetree compatible
309309
* @return a pointer to a device, or NULL
@@ -323,8 +323,8 @@ typedef int16_t device_handle_t;
323323
*
324324
* If there are multiple, this returns an arbitrary one.
325325
*
326-
* If this returns non-NULL, the device must be checked for readiness before
327-
* use, e.g. with device_is_ready().
326+
* If this returns non-NULL, device usage must be notified using
327+
* device_get().
328328
*
329329
* @param compat lowercase-and-underscores devicetree compatible
330330
* @return a pointer to a device
@@ -817,6 +817,21 @@ __syscall const struct device *device_get_binding(const char *name);
817817
*/
818818
size_t z_device_get_all_static(const struct device **devices);
819819

820+
/**
821+
* Get a device.
822+
*
823+
* When getting a device, its usage count will be increased and, if not yet
824+
* initialized, its initialization routine will be called. This function will
825+
* not return until device initialization has finished.
826+
*
827+
* @param dev Device instance
828+
*
829+
* @retval -errno Device failed to initialize.
830+
* @retval >=0 Device was successfully initialized and can be used. Values > 0
831+
* indicate the current reference count.
832+
*/
833+
__syscall int device_get(const struct device *dev);
834+
820835
/**
821836
* @brief Verify that a device is ready for use.
822837
*
@@ -829,26 +844,17 @@ size_t z_device_get_all_static(const struct device **devices);
829844
*
830845
* @param dev pointer to the device in question.
831846
*
847+
* @note Usage of device_get() if preferred, this function will be deprecated
848+
* in the future.
849+
*
832850
* @retval true If the device is ready for use.
833851
* @retval false If the device is not ready for use or if a NULL device pointer
834852
* is passed as argument.
835853
*/
836-
__syscall bool device_is_ready(const struct device *dev);
837-
838-
/**
839-
* Get a device.
840-
*
841-
* When getting a device, its usage count will be increased and, if not yet
842-
* initialized, its initialization routine will be called. This function will
843-
* not return until device initialization has finished.
844-
*
845-
* @param dev Device instance
846-
*
847-
* @retval -errno Device failed to initialize.
848-
* @retval >=0 Device was successfully initialized and can be used. Values > 0
849-
* indicate the current reference count.
850-
*/
851-
__syscall int device_get(const struct device *dev);
854+
static inline bool device_is_ready(const struct device *dev)
855+
{
856+
return device_get(dev) >= 0;
857+
}
852858

853859
/**
854860
* @brief Initialize a device.

kernel/device.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const struct device *z_impl_device_get_binding(const char *name)
3838
/* Return NULL if the device matching 'name' is not ready. */
3939
STRUCT_SECTION_FOREACH(device, dev) {
4040
if ((dev->name == name) || (strcmp(name, dev->name) == 0)) {
41-
return z_impl_device_is_ready(dev) ? dev : NULL;
41+
return z_impl_device_get(dev) >= 0 ? dev : NULL;
4242
}
4343
}
4444

@@ -58,14 +58,6 @@ static inline const struct device *z_vrfy_device_get_binding(const char *name)
5858
return z_impl_device_get_binding(name_copy);
5959
}
6060
#include <zephyr/syscalls/device_get_binding_mrsh.c>
61-
62-
static inline bool z_vrfy_device_is_ready(const struct device *dev)
63-
{
64-
K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY));
65-
66-
return z_impl_device_is_ready(dev);
67-
}
68-
#include <zephyr/syscalls/device_is_ready_mrsh.c>
6961
#endif /* CONFIG_USERSPACE */
7062

7163
#ifdef CONFIG_DEVICE_DT_METADATA
@@ -88,7 +80,7 @@ const struct device *z_impl_device_get_by_dt_nodelabel(const char *nodelabel)
8880
STRUCT_SECTION_FOREACH(device, dev) {
8981
const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev);
9082

91-
if (!z_impl_device_is_ready(dev) || nl == NULL) {
83+
if ((z_impl_device_get(dev) < 0) || nl == NULL) {
9284
continue;
9385
}
9486

@@ -129,19 +121,6 @@ size_t z_device_get_all_static(struct device const **devices)
129121
return cnt;
130122
}
131123

132-
bool z_impl_device_is_ready(const struct device *dev)
133-
{
134-
/*
135-
* if an invalid device pointer is passed as argument, this call
136-
* reports the `device` as not ready for usage.
137-
*/
138-
if (dev == NULL) {
139-
return false;
140-
}
141-
142-
return dev->state->initialized && (dev->state->init_res == 0U);
143-
}
144-
145124
int z_impl_device_get(const struct device *dev)
146125
{
147126
int ret = 0;

0 commit comments

Comments
 (0)