Skip to content

evaluate complexity of moving immutable device data to substructure #26127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion boards/arm/thingy52_nrf52832/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct pwr_ctrl_cfg {

static int pwr_ctrl_init(struct device *dev)
{
const struct pwr_ctrl_cfg *cfg = dev->config_info;
const struct pwr_ctrl_cfg *cfg = dev->fixed->config_info;
struct device *gpio;

gpio = device_get_binding(cfg->port);
Expand Down
44 changes: 44 additions & 0 deletions devfix.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@r_base@
expression DE;
identifier ftag =~ "(?x)^
(name
|config_info
|driver_api
|driver_data
|device_pm_control
|pm
)$";
@@
DE->ftag

@r_param
extends r_base@
identifier FN;
identifier D;
@@
FN(..., struct device *D, ...) {
<...
D->
+fixed->
ftag
...>
}

@r_local
extends r_base@
identifier D;
@@
struct device *D;
<...
D->
+fixed->
ftag
...>

@r_expr
extends r_base@
expression E;
@@
(struct device *)E->
+fixed->
ftag
45 changes: 27 additions & 18 deletions doc/reference/drivers/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,28 @@ The following APIs for device drivers are provided by :file:`device.h`. The APIs
are intended for use in device drivers only and should not be used in
applications.

:c:func:`DEVICE_INIT()`
create device object and set it up for boot time initialization.
:c:func:`DEVICE_DEFINE()`
Create device object and related data structures including setting it
up for boot-time initialization.

:c:func:`DEVICE_AND_API_INIT()`
Create device object and set it up for boot time initialization.
This also takes a pointer to driver API struct for link time
pointer assignment.
Like :c:func:`DEVICE_DEFINE()` but without support for device power
management.

:c:func:`DEVICE_INIT()`
Like :c:func:`DEVICE_AND_API_INIT()` but without providing an API
pointer.

:c:func:`DEVICE_NAME_GET()`
Expands to the full name of a global device object.
Converts a device identifier to the global identifier for a device
object.

:c:func:`DEVICE_GET()`
Obtain a pointer to a device object by name.

:c:func:`DEVICE_DECLARE()`
Declare a device object.
Declare a device object. Use this when you need a forward reference
to a device that has not yet been defined.

.. _device_struct:

Expand All @@ -106,7 +112,7 @@ split into read-only and runtime-mutable parts. At a high level we have:
The ``config_info`` member is for read-only configuration data set at build time. For
example, base memory mapped IO addresses, IRQ line numbers, or other fixed
physical characteristics of the device. This is the ``config_info`` structure
passed to the ``DEVICE_*INIT()`` macros.
passed to ``DEVICE_DEFINE()`` and related macros.

The ``driver_data`` struct is kept in RAM, and is used by the driver for
per-instance runtime housekeeping. For example, it may contain reference counts,
Expand Down Expand Up @@ -140,15 +146,15 @@ A subsystem API definition typically looks like this:
{
struct subsystem_api *api;

api = (struct subsystem_api *)device->driver_api;
api = (struct subsystem_api *)device->fixed->driver_api;
return api->do_this(device, foo, bar);
}

static inline void subsystem_do_that(struct device *device, void *baz)
{
struct subsystem_api *api;

api = (struct subsystem_api *)device->driver_api;
api = (struct subsystem_api *)device->fixed->driver_api;
api->do_that(device, foo, bar);
}

Expand All @@ -173,8 +179,7 @@ of these APIs, and populate an instance of subsystem_api structure:
};

The driver would then pass ``my_driver_api_funcs`` as the ``api`` argument to
``DEVICE_AND_API_INIT()``, or manually assign it to ``device->driver_api``
in the driver init function.
``DEVICE_AND_API_INIT()``.

.. note::

Expand Down Expand Up @@ -295,7 +300,7 @@ In the implementation of the common init function:

int my_driver_init(struct device *device)
{
const struct my_driver_config *config = device->config_info;
const struct my_driver_config *config = device->fixed->config_info;

/* Do other initialization stuff */
...
Expand Down Expand Up @@ -338,10 +343,11 @@ the IRQ handler argument and the definition of the device itself.
Initialization Levels
*********************

Drivers may depend on other drivers being initialized first, or
require the use of kernel services. The DEVICE_INIT() APIs allow the user to
specify at what time during the boot sequence the init function will be
executed. Any driver will specify one of four initialization levels:
Drivers may depend on other drivers being initialized first, or require
the use of kernel services. :c:func:`DEVICE_DEFINE()` and related APIs
allow the user to specify at what time during the boot sequence the init
function will be executed. Any driver will specify one of four
initialization levels:

``PRE_KERNEL_1``
Used for devices that have no dependencies, such as those that rely
Expand Down Expand Up @@ -383,7 +389,7 @@ System Drivers
**************

In some cases you may just need to run a function at boot. Special ``SYS_*``
macros exist that map to ``DEVICE_*INIT()`` calls.
macros exist that map to ``DEVICE_DEFINE()`` calls.
For ``SYS_INIT()`` there are no config or runtime data structures and there
isn't a way
to later get a device pointer by name. The same policies for initialization
Expand All @@ -393,8 +399,11 @@ For ``SYS_DEVICE_DEFINE()`` you can obtain pointers by name, see
:ref:`power management <power_management_api>` section.

:c:func:`SYS_INIT()`
Run an initialization function at boot at specified priority.

:c:func:`SYS_DEVICE_DEFINE()`
Like :c:func:`DEVICE_DEFINE` without an API table and constructing
the device name from the init function name.

Error handling
**************
Expand Down
48 changes: 24 additions & 24 deletions drivers/adc/adc_lmp90xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ static inline uint8_t lmp90xxx_inst2_sz(size_t len)
static int lmp90xxx_read_reg(struct device *dev, uint8_t addr, uint8_t *dptr,
size_t len)
{
const struct lmp90xxx_config *config = dev->config_info;
struct lmp90xxx_data *data = dev->driver_data;
const struct lmp90xxx_config *config = dev->fixed->config_info;
struct lmp90xxx_data *data = dev->fixed->driver_data;
uint8_t ura = LMP90XXX_URA(addr);
uint8_t inst1_uab[2] = { LMP90XXX_INST1_WAB, ura };
uint8_t inst2 = LMP90XXX_INST2_R | LMP90XXX_LRA(addr);
Expand Down Expand Up @@ -249,8 +249,8 @@ static int lmp90xxx_read_reg8(struct device *dev, uint8_t addr, uint8_t *val)
static int lmp90xxx_write_reg(struct device *dev, uint8_t addr, uint8_t *dptr,
size_t len)
{
const struct lmp90xxx_config *config = dev->config_info;
struct lmp90xxx_data *data = dev->driver_data;
const struct lmp90xxx_config *config = dev->fixed->config_info;
struct lmp90xxx_data *data = dev->fixed->driver_data;
uint8_t ura = LMP90XXX_URA(addr);
uint8_t inst1_uab[2] = { LMP90XXX_INST1_WAB, ura };
uint8_t inst2 = LMP90XXX_INST2_WB | LMP90XXX_LRA(addr);
Expand Down Expand Up @@ -327,7 +327,7 @@ static int lmp90xxx_soft_reset(struct device *dev)

static inline bool lmp90xxx_has_channel(struct device *dev, uint8_t channel)
{
const struct lmp90xxx_config *config = dev->config_info;
const struct lmp90xxx_config *config = dev->fixed->config_info;

if (channel >= config->channels) {
return false;
Expand All @@ -338,7 +338,7 @@ static inline bool lmp90xxx_has_channel(struct device *dev, uint8_t channel)

static inline bool lmp90xxx_has_input(struct device *dev, uint8_t input)
{
const struct lmp90xxx_config *config = dev->config_info;
const struct lmp90xxx_config *config = dev->fixed->config_info;

if (input >= LMP90XXX_MAX_INPUTS) {
return false;
Expand Down Expand Up @@ -379,7 +379,7 @@ static inline int lmp90xxx_acq_time_to_odr(uint16_t acq_time)
static int lmp90xxx_adc_channel_setup(struct device *dev,
const struct adc_channel_cfg *channel_cfg)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
uint8_t chx_inputcn = LMP90XXX_BURNOUT_EN(0); /* No burnout currents */
uint8_t chx_config = LMP90XXX_BUF_EN(0); /* No buffer */
uint8_t payload[2];
Expand Down Expand Up @@ -496,8 +496,8 @@ static int lmp90xxx_validate_buffer_size(const struct adc_sequence *sequence)
static int lmp90xxx_adc_start_read(struct device *dev,
const struct adc_sequence *sequence)
{
const struct lmp90xxx_config *config = dev->config_info;
struct lmp90xxx_data *data = dev->driver_data;
const struct lmp90xxx_config *config = dev->fixed->config_info;
struct lmp90xxx_data *data = dev->fixed->driver_data;
int err;

if (sequence->resolution != config->resolution) {
Expand Down Expand Up @@ -528,7 +528,7 @@ static int lmp90xxx_adc_read_async(struct device *dev,
const struct adc_sequence *sequence,
struct k_poll_signal *async)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
int err;

adc_context_lock(&data->ctx, async ? true : false, async);
Expand Down Expand Up @@ -569,8 +569,8 @@ static void adc_context_update_buffer_pointer(struct adc_context *ctx,
static int lmp90xxx_adc_read_channel(struct device *dev, uint8_t channel,
int32_t *result)
{
const struct lmp90xxx_config *config = dev->config_info;
struct lmp90xxx_data *data = dev->driver_data;
const struct lmp90xxx_config *config = dev->fixed->config_info;
struct lmp90xxx_data *data = dev->fixed->driver_data;
uint8_t adc_done;
uint8_t ch_scan;
uint8_t buf[4]; /* ADC_DOUT + CRC */
Expand Down Expand Up @@ -648,7 +648,7 @@ static int lmp90xxx_adc_read_channel(struct device *dev, uint8_t channel,

static void lmp90xxx_acquisition_thread(struct device *dev)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
uint8_t bgcalcn = LMP90XXX_BGCALN(0x3); /* Default to BgCalMode3 */
int32_t result = 0;
uint8_t channel;
Expand Down Expand Up @@ -711,7 +711,7 @@ static void lmp90xxx_drdyb_callback(struct device *port,
#ifdef CONFIG_ADC_LMP90XXX_GPIO
int lmp90xxx_gpio_set_output(struct device *dev, uint8_t pin)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
int err = 0;
uint8_t tmp;

Expand All @@ -736,7 +736,7 @@ int lmp90xxx_gpio_set_output(struct device *dev, uint8_t pin)

int lmp90xxx_gpio_set_input(struct device *dev, uint8_t pin)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
int err = 0;
uint8_t tmp;

Expand All @@ -761,7 +761,7 @@ int lmp90xxx_gpio_set_input(struct device *dev, uint8_t pin)

int lmp90xxx_gpio_set_pin_value(struct device *dev, uint8_t pin, bool value)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
int err = 0;
uint8_t tmp;

Expand All @@ -788,7 +788,7 @@ int lmp90xxx_gpio_set_pin_value(struct device *dev, uint8_t pin, bool value)

int lmp90xxx_gpio_get_pin_value(struct device *dev, uint8_t pin, bool *value)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
int err = 0;
uint8_t tmp;

Expand All @@ -810,7 +810,7 @@ int lmp90xxx_gpio_get_pin_value(struct device *dev, uint8_t pin, bool *value)

int lmp90xxx_gpio_port_get_raw(struct device *dev, gpio_port_value_t *value)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
uint8_t tmp;
int err;

Expand All @@ -828,7 +828,7 @@ int lmp90xxx_gpio_port_set_masked_raw(struct device *dev,
gpio_port_pins_t mask,
gpio_port_value_t value)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
int err = 0;
uint8_t tmp;

Expand All @@ -849,7 +849,7 @@ int lmp90xxx_gpio_port_set_masked_raw(struct device *dev,

int lmp90xxx_gpio_port_set_bits_raw(struct device *dev, gpio_port_pins_t pins)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
int err = 0;
uint8_t tmp;

Expand All @@ -871,7 +871,7 @@ int lmp90xxx_gpio_port_set_bits_raw(struct device *dev, gpio_port_pins_t pins)
int lmp90xxx_gpio_port_clear_bits_raw(struct device *dev,
gpio_port_pins_t pins)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
int err = 0;
uint8_t tmp;

Expand All @@ -892,7 +892,7 @@ int lmp90xxx_gpio_port_clear_bits_raw(struct device *dev,

int lmp90xxx_gpio_port_toggle_bits(struct device *dev, gpio_port_pins_t pins)
{
struct lmp90xxx_data *data = dev->driver_data;
struct lmp90xxx_data *data = dev->fixed->driver_data;
uint8_t tmp;
int err;

Expand All @@ -913,8 +913,8 @@ int lmp90xxx_gpio_port_toggle_bits(struct device *dev, gpio_port_pins_t pins)

static int lmp90xxx_init(struct device *dev)
{
const struct lmp90xxx_config *config = dev->config_info;
struct lmp90xxx_data *data = dev->driver_data;
const struct lmp90xxx_config *config = dev->fixed->config_info;
struct lmp90xxx_data *data = dev->fixed->driver_data;
struct device *drdyb_dev;
k_tid_t tid;
int err;
Expand Down
Loading