Skip to content

Commit e39de0e

Browse files
nashifcfriedt
authored andcommitted
device: move device syscalls to device.c
Move device model syscalls to device.c and decouple kernel header from device related routines. Cleanup init to have only what is needed. Signed-off-by: Anas Nashif <[email protected]>
1 parent 7aa3269 commit e39de0e

File tree

3 files changed

+79
-64
lines changed

3 files changed

+79
-64
lines changed

kernel/device.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <zephyr/sys/kobject.h>
1414
#include <zephyr/internal/syscall_handler.h>
1515
#include <zephyr/toolchain.h>
16+
#include <zephyr/pm/device_runtime.h>
1617

1718
/**
1819
* @brief Initialize state for all static devices.
@@ -27,6 +28,71 @@ void z_device_state_init(void)
2728
}
2829
}
2930

31+
int do_device_init(const struct device *dev)
32+
{
33+
int rc = 0;
34+
35+
if (dev->ops.init != NULL) {
36+
rc = dev->ops.init(dev);
37+
/* If initialization failed, record in dev->state->init_res
38+
* the POSITIVE value of the resulting errno
39+
*/
40+
if (rc != 0) {
41+
/* device's init function should return:
42+
* 0 on success
43+
* a negative value on failure (-errno)
44+
* errno value maps to an uint8_t range as of now.
45+
*/
46+
__ASSERT(rc >= -UINT8_MAX && rc < 0, "device %s init: invalid error (%d)",
47+
dev->name, rc);
48+
49+
if (rc < 0) {
50+
rc = -rc;
51+
}
52+
/* handle error value overflow in production
53+
* this is likely a bug in the device's init function. Signals it
54+
*/
55+
if (rc > UINT8_MAX) {
56+
rc = UINT8_MAX;
57+
}
58+
dev->state->init_res = rc;
59+
}
60+
}
61+
62+
/* device initialization has been invoked */
63+
dev->state->initialized = true;
64+
65+
if (rc == 0) {
66+
/* Run automatic device runtime enablement */
67+
(void)pm_device_runtime_auto_enable(dev);
68+
}
69+
70+
/* here, the value of rc is either 0 or +errno
71+
* flip the sign to return a negative value on failure as expected
72+
*/
73+
return -rc;
74+
}
75+
76+
int z_impl_device_init(const struct device *dev)
77+
{
78+
if (dev->state->initialized) {
79+
return -EALREADY;
80+
}
81+
82+
return do_device_init(dev);
83+
}
84+
85+
#ifdef CONFIG_USERSPACE
86+
static inline int z_vrfy_device_init(const struct device *dev)
87+
{
88+
K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY));
89+
90+
return z_impl_device_init(dev);
91+
}
92+
#include <zephyr/syscalls/device_init_mrsh.c>
93+
#endif
94+
95+
3096
const struct device *z_impl_device_get_binding(const char *name)
3197
{
3298
/* A null string identifies no device. So does an empty

kernel/include/kernel_internal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ void z_init_thread_base(struct _thread_base *thread_base, int priority,
3434

3535
FUNC_NORETURN void z_cstart(void);
3636

37-
void z_device_state_init(void);
38-
3937
extern FUNC_NORETURN void z_thread_entry(k_thread_entry_t entry,
4038
void *p1, void *p2, void *p3);
4139

kernel/init.c

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include <kswap.h>
3939
#include <zephyr/timing/timing.h>
4040
#include <zephyr/logging/log.h>
41-
#include <zephyr/pm/device_runtime.h>
4241
#include <zephyr/internal/syscall_handler.h>
4342
#include <zephyr/arch/common/init.h>
4443

@@ -193,49 +192,20 @@ extern volatile uintptr_t __stack_chk_guard;
193192
__pinned_bss
194193
bool z_sys_post_kernel;
195194

196-
static int do_device_init(const struct device *dev)
197-
{
198-
int rc = 0;
199-
200-
if (dev->ops.init != NULL) {
201-
rc = dev->ops.init(dev);
202-
/* If initialization failed, record in dev->state->init_res
203-
* the POSITIVE value of the resulting errno
204-
*/
205-
if (rc != 0) {
206-
/* device's init function should return:
207-
* 0 on success
208-
* a negative value on failure (-errno)
209-
* errno value maps to an uint8_t range as of now.
210-
*/
211-
__ASSERT(rc >= -UINT8_MAX && rc < 0, "device %s init: invalid error (%d)",
212-
dev->name, rc);
213-
214-
if (rc < 0) {
215-
rc = -rc;
216-
}
217-
/* handle error value overflow in production
218-
* this is likely a bug in the device's init function. Signals it
219-
*/
220-
if (rc > UINT8_MAX) {
221-
rc = UINT8_MAX;
222-
}
223-
dev->state->init_res = rc;
224-
}
225-
}
226-
227-
/* device initialization has been invoked */
228-
dev->state->initialized = true;
195+
/* defined in device.c */
196+
extern int do_device_init(const struct device *dev);
229197

230-
if (rc == 0) {
231-
/* Run automatic device runtime enablement */
232-
(void)pm_device_runtime_auto_enable(dev);
198+
/**
199+
* @brief Initialize state for all static devices.
200+
*
201+
* The state object is always zero-initialized, but this may not be
202+
* sufficient.
203+
*/
204+
static void z_device_state_init(void)
205+
{
206+
STRUCT_SECTION_FOREACH(device, dev) {
207+
k_object_init(dev);
233208
}
234-
235-
/* here, the value of rc is either 0 or +errno
236-
* flip the sign to return a negative value on failure as expected
237-
*/
238-
return -rc;
239209
}
240210

241211
/**
@@ -281,26 +251,7 @@ static void z_sys_init_run_level(enum init_level level)
281251
}
282252
}
283253

284-
285-
int z_impl_device_init(const struct device *dev)
286-
{
287-
if (dev->state->initialized) {
288-
return -EALREADY;
289-
}
290-
291-
return do_device_init(dev);
292-
}
293-
294-
#ifdef CONFIG_USERSPACE
295-
static inline int z_vrfy_device_init(const struct device *dev)
296-
{
297-
K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY));
298-
299-
return z_impl_device_init(dev);
300-
}
301-
#include <zephyr/syscalls/device_init_mrsh.c>
302-
#endif
303-
254+
/* defined in banner.c */
304255
extern void boot_banner(void);
305256

306257

0 commit comments

Comments
 (0)