Common core of every hardware device: controllers, buses and the peripherals on them.
More...
|
| #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.
|
| |
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 {
uint8_t addr;
};
static int prv_foo_init(
const struct pbl_device *dev) {
return prv_probe(foo->addr) ? 0 : -ENODEV;
}
static const struct foo s_foo = {
.addr = 0x18,
};
#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
◆ 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
| Data Fields |
|
const char * |
name |
Name. |
◆ 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. |
◆ 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
-
| sym | Name to give. |
| inst | Symbol of the instance. |
| type | Type of the instance. |
| class_type | Type of the class object, which has to be its first member. |
| member | Name 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
-
| sym | Symbol of the class. |
| _name | Name. |
◆ PBL_DEVICE_CLASS_FOREACH
| #define PBL_DEVICE_CLASS_FOREACH |
( |
|
_cls, |
|
|
|
_dev |
|
) |
| |
Value:
: 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
-
| _cls | Class. |
| _dev | Loop variable, a const struct pbl_device pointer, NULL after the loop. |
◆ PBL_DEVICE_DEPENDS
| #define PBL_DEVICE_DEPENDS |
( |
|
sym, |
|
|
|
_dev, |
|
|
|
... |
|
) |
| |
Value:
}
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
-
| sym | Symbol of the device instance. |
| _dev | Pointer 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
-
| sym | Symbol of the device instance, with its state defined by PBL_DEVICE_STATE_DEFINE(). |
| _name | Name. |
| _init | Init function, or NULL. |
| _parent | Parent 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
-
| sym | Symbol of the device instance, with its state defined by PBL_DEVICE_STATE_DEFINE(). |
| _name | Name. |
| _init | Init function, or NULL. |
| _parent | Parent device, or NULL. |
| _cls | Class. |
◆ 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
-
| sym | Symbol of the instance. |
| type | Type of the instance. |
| class_type | Type of the class object, which has to be its first member. |
| member | Name 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
-
| sym | Symbol of the instance. |
| type | Type of the instance. |
◆ PBL_DEVICE_REGISTER
| #define PBL_DEVICE_REGISTER |
( |
|
sym, |
|
|
|
dev |
|
) |
| |
◆ 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
-
| sym | Symbol 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
-
| _base | Base address. |
| _size | Size in bytes. |
◆ 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.
|
◆ 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
-
- 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
-
- 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
-
- 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
-
- Returns
- Its name, or "?" without
CONFIG_DEVICE_NAMES.