[U-Boot] [PATCH 1/2] dm: core: Add uclass_first_device_err() to return a valid device
Bin Meng
bmeng.cn at gmail.com
Tue Feb 16 10:31:17 CET 2016
Hi Simon,
On Fri, Feb 12, 2016 at 4:23 AM, Simon Glass <sjg at chromium.org> wrote:
> 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 at chromium.org>
> ---
>
> drivers/core/uclass.c | 13 +++++++++++++
> include/dm/uclass.h | 15 +++++++++++++--
> 2 files changed, 26 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
> index 12095e7..1141ce1 100644
> --- a/drivers/core/uclass.c
> +++ b/drivers/core/uclass.c
> @@ -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)
Or maybe another way is to change uclass_first_device() behavior to
return -ENODEV if device is not found? (move the return value test
logic into uclass_first_device)
> +{
> + 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;
> diff --git a/include/dm/uclass.h b/include/dm/uclass.h
> index bfbd27a..fd368b6 100644
> --- a/include/dm/uclass.h
> +++ b/include/dm/uclass.h
> @@ -200,18 +200,29 @@ int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
> *
> * @id: Uclass ID to look up
> * @devp: Returns pointer to the first device in that uclass, or NULL if none
> - * @return 0 if OK (found or not found), -1 on error
> + * @return 0 if OK (found or not found), other -ve on error
> */
> int uclass_first_device(enum uclass_id id, struct udevice **devp);
>
> /**
> + * uclass_first_device_err() - Get the first device in a uclass
> + *
> + * The device returned is probed if necessary, and ready for use
> + *
> + * @id: Uclass ID to look up
> + * @devp: Returns pointer to the first device in that uclass, or NULL if none
> + * @return 0 if found, -ENODEV if not found, other -ve on error
> + */
> +int uclass_first_device_err(enum uclass_id id, struct udevice **devp);
> +
> +/**
> * uclass_next_device() - Get the next device in a uclass
> *
> * The device returned is probed if necessary, and ready for use
> *
> * @devp: On entry, pointer to device to lookup. On exit, returns pointer
> * to the next device in the same uclass, or NULL if none
> - * @return 0 if OK (found or not found), -1 on error
> + * @return 0 if OK (found or not found), other -ve on error
> */
> int uclass_next_device(struct udevice **devp);
>
> --
Regards,
Bin
More information about the U-Boot
mailing list