Skip to content

Commit f39828c

Browse files
committed
device: introduce struct device_ops
Instead of passing a single init function, create struct device_ops with the init function inside. This allows to easily extend device's capabilities in the future without too much breakage, e.g. to add a de-init call. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
1 parent 3af95fb commit f39828c

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

include/zephyr/device.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ typedef uint8_t device_flags_t;
461461

462462
/** @} */
463463

464+
/** Device operations */
465+
struct device_ops {
466+
/** Initialization function */
467+
int (*init)(const struct device *dev);
468+
};
469+
464470
/**
465471
* @brief Runtime device structure (in ROM) per driver instance
466472
*/
@@ -475,8 +481,8 @@ struct device {
475481
struct device_state *state;
476482
/** Address of the device instance private data */
477483
void *data;
478-
/** Initialization function (optional) */
479-
int (*init_fn)(const struct device *);
484+
/** Device operations */
485+
struct device_ops ops;
480486
/** Device flags */
481487
device_flags_t flags;
482488
#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
@@ -1100,7 +1106,7 @@ device_get_dt_nodelabels(const struct device *dev)
11001106
.api = (api_), \
11011107
.state = (state_), \
11021108
.data = (data_), \
1103-
.init_fn = (init_fn_), \
1109+
.ops = { .init = (init_fn_) }, \
11041110
.flags = (flags_), \
11051111
IF_ENABLED(CONFIG_DEVICE_DEPS, (.deps = (deps_),)) /**/ \
11061112
IF_ENABLED(CONFIG_PM_DEVICE, Z_DEVICE_INIT_PM_BASE(pm_)) /**/ \

kernel/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ static int do_device_init(const struct device *dev)
306306
{
307307
int rc = 0;
308308

309-
if (dev->init_fn != NULL) {
310-
rc = dev->init_fn(dev);
309+
if (dev->ops.init != NULL) {
310+
rc = dev->ops.init(dev);
311311
/* Mark device initialized. If initialization
312312
* failed, record the error condition.
313313
*/

0 commit comments

Comments
 (0)