Skip to content

Commit 766bfe7

Browse files
gmarullhenrikbrixandersen
authored andcommitted
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 <[email protected]>
1 parent f44a301 commit 766bfe7

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

include/zephyr/device.h

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

452452
/** @} */
453453

454+
/** Device operations */
455+
struct device_ops {
456+
/** Initialization function */
457+
int (*init)(const struct device *dev);
458+
};
459+
454460
/**
455461
* @brief Runtime device structure (in ROM) per driver instance
456462
*/
@@ -465,8 +471,8 @@ struct device {
465471
struct device_state *state;
466472
/** Address of the device instance private data */
467473
void *data;
468-
/** Initialization function (optional) */
469-
int (*init_fn)(const struct device *);
474+
/** Device operations */
475+
struct device_ops ops;
470476
/** Device flags */
471477
device_flags_t flags;
472478
#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
@@ -1090,7 +1096,7 @@ device_get_dt_nodelabels(const struct device *dev)
10901096
.api = (api_), \
10911097
.state = (state_), \
10921098
.data = (data_), \
1093-
.init_fn = (init_fn_), \
1099+
.ops = { .init = (init_fn_) }, \
10941100
.flags = (flags_), \
10951101
IF_ENABLED(CONFIG_DEVICE_DEPS, (.deps = (deps_),)) /**/ \
10961102
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
*/

tests/lib/devicetree/devices/src/main.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,16 @@ ZTEST(devicetree_devices, test_init_get)
8686
DEVICE_DT_GET(TEST_NOLABEL), NULL);
8787

8888
/* Check init functions */
89-
zassert_equal(DEVICE_DT_GET(TEST_GPIO)->init_fn, dev_init);
90-
zassert_equal(DEVICE_DT_GET(TEST_I2C)->init_fn, dev_init);
91-
zassert_equal(DEVICE_DT_GET(TEST_DEVA)->init_fn, dev_init);
92-
zassert_equal(DEVICE_DT_GET(TEST_DEVB)->init_fn, dev_init);
93-
zassert_equal(DEVICE_DT_GET(TEST_GPIOX)->init_fn, dev_init);
94-
zassert_equal(DEVICE_DT_GET(TEST_DEVC)->init_fn, dev_init);
95-
zassert_equal(DEVICE_DT_GET(TEST_PARTITION)->init_fn, dev_init);
96-
zassert_equal(DEVICE_DT_GET(TEST_GPIO_INJECTED)->init_fn, dev_init);
97-
zassert_equal(DEVICE_GET(manual_dev)->init_fn, dev_init);
98-
zassert_equal(DEVICE_DT_GET(TEST_NOLABEL)->init_fn, dev_init);
89+
zassert_equal(DEVICE_DT_GET(TEST_GPIO)->ops.init, dev_init);
90+
zassert_equal(DEVICE_DT_GET(TEST_I2C)->ops.init, dev_init);
91+
zassert_equal(DEVICE_DT_GET(TEST_DEVA)->ops.init, dev_init);
92+
zassert_equal(DEVICE_DT_GET(TEST_DEVB)->ops.init, dev_init);
93+
zassert_equal(DEVICE_DT_GET(TEST_GPIOX)->ops.init, dev_init);
94+
zassert_equal(DEVICE_DT_GET(TEST_DEVC)->ops.init, dev_init);
95+
zassert_equal(DEVICE_DT_GET(TEST_PARTITION)->ops.init, dev_init);
96+
zassert_equal(DEVICE_DT_GET(TEST_GPIO_INJECTED)->ops.init, dev_init);
97+
zassert_equal(DEVICE_GET(manual_dev)->ops.init, dev_init);
98+
zassert_equal(DEVICE_DT_GET(TEST_NOLABEL)->ops.init, dev_init);
9999
}
100100

101101
ZTEST(devicetree_devices, test_init_order)

0 commit comments

Comments
 (0)