Re: [RFC] Device: device_get_binding() returns NULL if device fails initialization

Davidoaia, Bogdan M <bogdan.m.davidoaia@...>

Hi Daniel,

I didn't catch this at first, but the patch causes a problem for the
Grove LCD driver (not sure if this problem is also present for other

The way it was written, the Grove LCD driver doesn't use the driver_api
pointer at all (there is no LCD API). As such, it is NULL by default and
the call to device_get_binding will return NULL even though the device
was correctly initialized.


On Ma, 2016-04-05 at 16:36 -0700, Daniel Leung wrote:
Problem Statement:
Currently, there is no way to know if a driver fails initialization.

Problem Description:
Zephyr currently does not provide a way to check programmatically
whether a device can be used. The device_get_binding() always
returns a valid device struct (if such device driver instance
exists). This causes a bit of headache when debugging apps as
the developer assumes the devices are ready to be used.

The solution was actually proposed in [1] a while ago. The idea
is to piggy-back onto driver_api pointer. If the pointer is NULL,
the device driver has failed in its own initialization function.
The driver_api is set to NULL (or in other words, never set to
the actual API struct). When device_get_binding() sess that
the driver_api is NULL for a particular device struct, it returns
NULL to the caller, and thus preventing its use in the app.
Since this is a binary state, instead of creating another variable
inside device struct, this uses driver_api to avoid unnecessarily
enlarging the ROM size. Since this is a simple change, a patch
has been created in [2].


Daniel Leung

Join to automatically receive all group messages.