PebbleOS v4.47.0-1075-g013613c8a
Loading...
Searching...
No Matches
Device model

Common core of every hardware device: controllers, buses and the peripherals on them. More...

Data Structures

struct  pbl_device_state
 Runtime state of a device. More...
 
struct  pbl_device_class
 A device class. More...
 
struct  pbl_device
 A hardware device. More...
 
struct  pbl_mem_region
 Memory region reserved for a device, what a devicetree memory-region entry points to (a reserved-memory node). More...
 
struct  pbl_device_depends
 Devices that device dev needs, on top of its parent. More...
 

Macros

#define PBL_DEVICE_CLASS_DEFINE(sym, _name)   const struct pbl_device_class sym = {.name = (_name)}
 Define a device class.
 
#define PBL_MEM_REGION(_base, _size)   {.base = (_base), .size = (_size)}
 Initializer of a struct pbl_mem_region.
 
#define PBL_DEVICE_DEPS(...)   ((const struct pbl_device *const[]){__VA_ARGS__, nullptr})
 Static NULL-terminated list of devices.
 
#define PBL_DEVICE_STATE_DEFINE(sym)   static struct pbl_device_state sym##_device_state
 Define the runtime state of device instance sym.
 
#define PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, _cls)
 Initializer for the struct pbl_device of device instance sym, in class _cls.
 
#define PBL_DEVICE_INIT(sym, _name, _init, _parent)    PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, nullptr)
 Initializer for the struct pbl_device of device instance sym.
 
#define PBL_DEVICE_REGISTER(sym, dev)
 Add a device to the table pbl_device_init_all() walks.
 
#define PBL_DEVICE_DEPENDS(sym, _dev, ...)
 List devices a device needs, without touching its definition.
 
#define PBL_DEVICE_ALIAS(sym, inst, type, class_type, member)
 Give an instance a second name with the type of its class, for code that does not know the driver.
 
#define PBL_DEVICE_INSTANCE(sym, type, class_type, member)
 Start the definition of a registered instance, followed by its initializer.
 
#define PBL_DEVICE_INSTANCE_DEV(sym, type)
 Start the definition of a registered instance of a device in no class, followed by its initializer.
 
#define PBL_DEVICE_CLASS_FOREACH(_cls, _dev)
 Iterate over the registered devices of a class.
 

Enumerations

enum  pbl_device_status { PBL_DEVICE_UNINIT , PBL_DEVICE_INITIALIZING , PBL_DEVICE_READY , PBL_DEVICE_FAILED }
 Device status. More...
 

Functions

int pbl_device_init (const struct pbl_device *dev)
 Initialize a device, its parent and the devices of its PBL_DEVICE_DEPENDS() first.
 
bool pbl_device_is_ready (const struct pbl_device *dev)
 Check whether a device is initialized.
 
int pbl_device_init_all (void)
 Initialize every registered device.
 
int pbl_device_init_children (const struct pbl_device *parent)
 Initialize every registered device whose parent is parent.
 
static const char * pbl_device_name (const struct pbl_device *dev)
 Get the name of a device.
 

Detailed Description

Common core of every hardware device: controllers, buses and the peripherals on them.

A device class embeds struct pbl_device, a driver embeds the class struct, and PBL_CONTAINER_OF() walks back out. Devices are const and live in flash; the only RAM is the state they point to.

Instances are defined with the PBL_<DRIVER>_DEFINE() macro of their driver, built from PBL_DEVICE_STATE_DEFINE(), PBL_DEVICE_INIT() and PBL_DEVICE_REGISTER(). The kernel brings every registered device up before main(), dependencies first, whatever their order in the table: a device's parent is initialized before it, as are the devices listed next to it with PBL_DEVICE_DEPENDS(), which the devicetree writes for what a node refers to. A driver's init calls pbl_device_init() on any other device it uses.

An init returning -ENODEV means the device is not present: that is not logged as an error.

A device class (GPIO port, clock controller, ...) defines a struct pbl_device_class and tags its devices with it through its init macro, so tooling can list them or find one by name with PBL_DEVICE_CLASS_FOREACH() and pbl_device_find().

struct foo {
struct pbl_device dev;
uint8_t addr;
};
static int prv_foo_init(const struct pbl_device *dev) {
const struct foo *foo = PBL_CONTAINER_OF(dev, const struct foo, dev);
return prv_probe(foo->addr) ? 0 : -ENODEV;
}
static const struct foo s_foo = {
.dev = PBL_DEVICE_INIT(s_foo, "foo", prv_foo_init, &s_bus.dev),
.addr = 0x18,
};
PBL_DEVICE_REGISTER(s_foo, &s_foo.dev);
PBL_DEVICE_DEPENDS(s_foo, &s_foo.dev, &s_ldo.dev);
#define PBL_DEVICE_INIT(sym, _name, _init, _parent)
Initializer for the struct pbl_device of device instance sym.
Definition device.h:169
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132
#define PBL_DEVICE_DEPENDS(sym, _dev,...)
List devices a device needs, without touching its definition.
Definition device.h:200
#define PBL_DEVICE_REGISTER(sym, dev)
Add a device to the table pbl_device_init_all() walks.
Definition device.h:178
#define PBL_CONTAINER_OF(ptr, type, member)
Get the structure that contains a member.
Definition misc.h:44
A hardware device.
Definition device.h:90

Data Structure Documentation

◆ pbl_device_state

struct pbl_device_state

Runtime state of a device.

Data Fields
int16_t res Result of the initialization, 0 or a negative errno.
uint8_t status One of pbl_device_status.

◆ pbl_device_class

struct pbl_device_class

A device class.

Data Fields
const char * name Name.

◆ pbl_mem_region

struct pbl_mem_region

Memory region reserved for a device, what a devicetree memory-region entry points to (a reserved-memory node).

Data Fields
uintptr_t base Base address.
size_t size Size in bytes.

◆ pbl_device_depends

struct pbl_device_depends

Devices that device dev needs, on top of its parent.

Data Fields
const struct pbl_device *const * deps NULL-terminated.

Devices initialized first.

const struct pbl_device * dev Device.

Macro Definition Documentation

◆ PBL_DEVICE_ALIAS

#define PBL_DEVICE_ALIAS (   sym,
  inst,
  type,
  class_type,
  member 
)
Value:
static_assert(offsetof(type, member) == 0, #member " has to come first"); \
extern const class_type sym __attribute__((alias(#inst)))

Give an instance a second name with the type of its class, for code that does not know the driver.

It is the same object, and usable in constant initializers.

The name may only be passed to the API of its class: code that needs the type of the driver uses the instance, or an accessor of the driver.

Parameters
symName to give.
instSymbol of the instance.
typeType of the instance.
class_typeType of the class object, which has to be its first member.
memberName of the class object.

◆ PBL_DEVICE_CLASS_DEFINE

#define PBL_DEVICE_CLASS_DEFINE (   sym,
  _name 
)    const struct pbl_device_class sym = {.name = (_name)}

Define a device class.

Parameters
symSymbol of the class.
_nameName.

◆ PBL_DEVICE_CLASS_FOREACH

#define PBL_DEVICE_CLASS_FOREACH (   _cls,
  _dev 
)
Value:
for (const struct pbl_device *const *pbl_device__it = PBL_ITERABLE_SECTION_START(devices); \
((_dev) = (pbl_device__it < PBL_ITERABLE_SECTION_END(devices)) ? *pbl_device__it \
: nullptr) != nullptr; \
pbl_device__it++) \
if ((_dev)->cls != (_cls)) { \
} else
#define PBL_ITERABLE_SECTION_END(tag)
End of an iterable section declared with PBL_ITERABLE_SECTION_DECLARE.
Definition section.h:94
#define PBL_ITERABLE_SECTION_START(tag)
First entry of an iterable section declared with PBL_ITERABLE_SECTION_DECLARE.
Definition section.h:86

Iterate over the registered devices of a class.

Parameters
_clsClass.
_devLoop variable, a const struct pbl_device pointer, NULL after the loop.

◆ PBL_DEVICE_DEPENDS

#define PBL_DEVICE_DEPENDS (   sym,
  _dev,
  ... 
)
Value:
static const struct pbl_device_depends sym##_device_depends PBL_ITERABLE_SECTION( \
const struct pbl_device_depends, dev_deps, #sym) = { \
.dev = (_dev), \
.deps = PBL_DEVICE_DEPS(__VA_ARGS__), \
}
const struct pbl_device *const * deps
NULL-terminated.
Definition device.h:187
const struct pbl_device * dev
Device.
Definition device.h:185
Devices that device dev needs, on top of its parent.
Definition device.h:183
#define PBL_DEVICE_DEPS(...)
Static NULL-terminated list of devices.
Definition device.h:125
#define PBL_ITERABLE_SECTION(type, tag, key)
Add the object to an iterable section, a table gathered at link time from any file.
Definition section.h:124

List devices a device needs, without touching its definition.

pbl_device_init() initializes them first. The list is constant and takes no RAM. The instances of a devicetree get theirs from what their node refers to.

Parameters
symSymbol of the device instance.
_devPointer to its struct pbl_device.
...Pointers to the struct pbl_device of each device it needs.

◆ PBL_DEVICE_DEPS

#define PBL_DEVICE_DEPS (   ...)    ((const struct pbl_device *const[]){__VA_ARGS__, nullptr})

Static NULL-terminated list of devices.

◆ PBL_DEVICE_INIT

#define PBL_DEVICE_INIT (   sym,
  _name,
  _init,
  _parent 
)     PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, nullptr)

Initializer for the struct pbl_device of device instance sym.

Parameters
symSymbol of the device instance, with its state defined by PBL_DEVICE_STATE_DEFINE().
_nameName.
_initInit function, or NULL.
_parentParent device, or NULL.

◆ PBL_DEVICE_INIT_CLASS

#define PBL_DEVICE_INIT_CLASS (   sym,
  _name,
  _init,
  _parent,
  _cls 
)
Value:
{ \
PBL_DEVICE__NAME(_name).init = (_init), \
.parent = (_parent), \
.cls = (_cls), \
.state = &sym##_device_state, \
}

Initializer for the struct pbl_device of device instance sym, in class _cls.

For the init macros of device classes.

Parameters
symSymbol of the device instance, with its state defined by PBL_DEVICE_STATE_DEFINE().
_nameName.
_initInit function, or NULL.
_parentParent device, or NULL.
_clsClass.

◆ PBL_DEVICE_INSTANCE

#define PBL_DEVICE_INSTANCE (   sym,
  type,
  class_type,
  member 
)
Value:
extern const type sym##_inst; \
PBL_DEVICE_REGISTER(sym##_inst, &sym##_inst.member.dev); \
PBL_DEVICE_ALIAS(sym, sym##_inst, type, class_type, member); \
const type sym##_inst =

Start the definition of a registered instance, followed by its initializer.

The instance is sym of the type of its class, for code that does not know the driver, and sym ## _inst of its own type, for the driver.

Parameters
symSymbol of the instance.
typeType of the instance.
class_typeType of the class object, which has to be its first member.
memberName of the class object, holding the struct pbl_device as dev.

◆ PBL_DEVICE_INSTANCE_DEV

#define PBL_DEVICE_INSTANCE_DEV (   sym,
  type 
)
Value:
extern const type sym##_inst; \
PBL_DEVICE_REGISTER(sym##_inst, &sym##_inst.dev); \
PBL_DEVICE_ALIAS(sym, sym##_inst, type, struct pbl_device, dev); \
const type sym##_inst =

Start the definition of a registered instance of a device in no class, followed by its initializer.

As PBL_DEVICE_INSTANCE(), for a driver struct whose first member is its struct pbl_device, dev: sym is that struct pbl_device.

Parameters
symSymbol of the instance.
typeType of the instance.

◆ PBL_DEVICE_REGISTER

#define PBL_DEVICE_REGISTER (   sym,
  dev 
)
Value:
static const struct pbl_device *const sym##_device_entry PBL_ITERABLE_SECTION( \
const struct pbl_device *const, devices, #sym) = (dev)

Add a device to the table pbl_device_init_all() walks.

Parameters
symSymbol of the device instance.
devPointer to its struct pbl_device.

◆ PBL_DEVICE_STATE_DEFINE

#define PBL_DEVICE_STATE_DEFINE (   sym)    static struct pbl_device_state sym##_device_state

Define the runtime state of device instance sym.

Parameters
symSymbol of the device instance.

◆ PBL_MEM_REGION

#define PBL_MEM_REGION (   _base,
  _size 
)    {.base = (_base), .size = (_size)}

Initializer of a struct pbl_mem_region.

Parameters
_baseBase address.
_sizeSize in bytes.

Enumeration Type Documentation

◆ pbl_device_status

Device status.

Enumerator
PBL_DEVICE_UNINIT 

Not initialized yet.

PBL_DEVICE_INITIALIZING 

Initialization in progress.

PBL_DEVICE_READY 

Initialized.

PBL_DEVICE_FAILED 

Initialization failed, or the device is not present.

Function Documentation

◆ pbl_device_init()

int pbl_device_init ( const struct pbl_device *  dev)

Initialize a device, its parent and the devices of its PBL_DEVICE_DEPENDS() first.

A no-op once the device is ready or has failed. A dependency cycle asserts.

Parameters
devDevice.
Returns
0, the error its init returned, or -ENODEV if its parent or a dep failed.

◆ pbl_device_init_all()

int pbl_device_init_all ( void  )

Initialize every registered device.

Called by the kernel on the main thread, before main().

Returns
Number of devices that failed or are not present.

◆ pbl_device_init_children()

int pbl_device_init_children ( const struct pbl_device *  parent)

Initialize every registered device whose parent is parent.

For a multi-function device to bring its functions up from its own init.

Parameters
parentParent device.
Returns
Number of devices that failed or are not present.

◆ pbl_device_is_ready()

bool pbl_device_is_ready ( const struct pbl_device *  dev)

Check whether a device is initialized.

Parameters
devDevice.
Returns
True if the device is ready.

◆ pbl_device_name()

static const char * pbl_device_name ( const struct pbl_device *  dev)
inlinestatic

Get the name of a device.

Parameters
devDevice.
Returns
Its name, or "?" without CONFIG_DEVICE_NAMES.