dm: core: Add uclass_first_device_err() to return a valid device

A common pattern is to call uclass_first_device() and then check if it
actually returns a device. Add a new function which does this, returning
an error if there are no devices in that uclass.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2016-02-11 13:23:25 -07:00
parent df61a74e68
commit b06750501f
2 changed files with 26 additions and 2 deletions

View File

@@ -401,6 +401,19 @@ int uclass_first_device(enum uclass_id id, struct udevice **devp)
return uclass_get_device_tail(dev, ret, devp);
}
int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
{
int ret;
ret = uclass_first_device(id, devp);
if (ret)
return ret;
else if (!*devp)
return -ENODEV;
return 0;
}
int uclass_next_device(struct udevice **devp)
{
struct udevice *dev = *devp;