device_get_binding() returns NULL if driver_api is not set

This changes the behavior of device_get_binding() so that
it returns NULL if driver_api is not set. This provides
a way for driver to state that it has not been initialized
properly, and prevents app from using it since no reference
to the device struct will be returned.

This implements the idea specified in [1]. The idea is to
reuse an existing resource by piggy-backing onto driver_api,
thus avoiding an extra "device state" variable in the device
struct. This differs from the code specified in the mailing
list by checking driver_api for NULL first. This avoids
the unnecessary strcmp() if driver_api is NULL.

[1] https://lists.zephyrproject.org/archives/list/devel@lists.zephyrproject.org/message/MZB5PYBSRHV3NIEHJYXYQVLTPFIIHPB3/

Change-Id: I978b1a6683cd56c8a72532d6368c47e67515c82d
Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2016-04-05 13:40:37 -07:00 committed by Anas Nashif
commit dd5e90ec6c

View file

@ -71,14 +71,14 @@ void _sys_device_do_config_level(int level)
*
* @param name device name to search for.
*
* @return pointer to device structure, or NULL if not found.
* @return pointer to device structure; NULL if not found or cannot be used.
*/
struct device *device_get_binding(char *name)
{
struct device *info;
for (info = __device_init_start; info != __device_init_end; info++) {
if (!strcmp(name, info->config->name)) {
if (info->driver_api && !strcmp(name, info->config->name)) {
return info;
}
}