backlight: add devm_backlight_device_{register,unregister}()
Subsystems (2)
PATCH 1/2 backlight: add devm_backlight_device_{register,unregister}()
These functions allow the driver core to automatically clean up any allocation made by backlight drivers. Thus it simplifies the error paths.
--- drivers/video/backlight/backlight.c | 75 +++++++++++++++++++++++++++++++++++ include/linux/backlight.h | 6 +++ 2 files changed, 81 insertions(+)
drivers/video/backlight/backlight.c +75 -0
@@ -370,6 +370,81 @@ void backlight_device_unregister(struct backlight_device *bd) }EXPORT_SYMBOL (backlight_device_unregister); +static void devm_backlight_device_release (struct device *dev,void *res) +{ +struct backlight_device *backlight = *(struct backlight_device **)res; + +backlight_device_unregister (backlight); +} + +static int devm_backlight_device_match (struct device *dev,void *res, +void *data) +{ +struct backlight_device **r = res; + +return *r == data; +} + +/** +* devm_backlight_device_register - resource managed backlight_device_register() +* @dev: the device to register +* @name: the name of the device +* @parent: a pointer to the parent device +* @devdata: an optional pointer to be stored for private driver use +* @ops: the backlight operations structure +* @props: the backlight properties +* +* @return a struct backlight on success, or an ERR_PTR on error +* +* Managed backlight_device_register(). The backlight_device returned +* from this function are automatically freed on driver detach. +* See backlight_device_register() for more information. +*/ +struct backlight_device *devm_backlight_device_register (struct device *dev, +const char *name,struct device *parent,void *devdata, +const struct backlight_ops *ops, +const struct backlight_properties *props) +{ +struct backlight_device **ptr, *backlight; + + ptr =devres_alloc (devm_backlight_device_release,sizeof (*ptr), +GFP_KERNEL ); +if (!ptr) +return ERR_PTR (-ENOMEM ); + + backlight =backlight_device_register (name, parent, devdata, ops, + props); +if (!IS_ERR (backlight)) { + *ptr = backlight; +devres_add (dev, ptr); + }else { +devres_free (ptr); + } + +return backlight; +} +EXPORT_SYMBOL (devm_backlight_device_register); + +/** +* devm_backlight_device_unregister - resource managed backlight_device_unregister() +* @dev: the device to unregister +* @bd: the backlight device to unregister +* +* Deallocated a backlight allocated with devm_backlight_device_register(). +* Normally this function will not need to be called and the resource management +* code will ensure that the resource is freed. +*/ +void devm_backlight_device_unregister (struct device *dev, +struct backlight_device *bd) +{ +int rc; + + rc =devres_release (dev, devm_backlight_device_release, + devm_backlight_device_match, bd); +WARN_ON (rc); +} +EXPORT_SYMBOL (devm_backlight_device_unregister); +#ifdef CONFIG_OF static int of_parent_match (struct device *dev,const void *data) {
include/linux/backlight.h +6 -0
@@ -114,7 +114,13 @@ static inline void backlight_update_status(struct backlight_device *bd)extern struct backlight_device *backlight_device_register (const char *name,struct device *dev,void *devdata,const struct backlight_ops *ops,const struct backlight_properties *props); +extern struct backlight_device *devm_backlight_device_register ( +struct device *dev,const char *name,struct device *parent, +void *devdata,const struct backlight_ops *ops, +const struct backlight_properties *props);extern void backlight_device_unregister (struct backlight_device *bd); +extern void devm_backlight_device_unregister (struct device *dev, +struct backlight_device *bd);extern void backlight_force_update (struct backlight_device *bd,enum backlight_update_reason reason);
Signature · 3 lines
1.7.10.4
�淸�{.n�+돴윯돪�+%듚�깁負쪐w��{.n�+돴쪐{깸G쳺��{ay����,j�f"톒쉹��묎�z_溫(�쉸듶줷"얎�m����G쳺�◀?솳鈺�&x�~뤳턨O뺞촾퇉v�^�m����띠�◀?뻀�Quoted 20 lines · On Thu, 16 May 2013 05:28:26 +0000 (GMT) Jingoo Han <[email protected]> wrote:
These functions allow the driver core to automatically clean up any allocation made by backlight drivers. Thus it simplifies the error paths.
...
drivers/video/backlight/backlight.c +6
-0@@ -370,6 +370,81 @@ void backlight_device_unregister(struct backlight_device *bd) }EXPORT_SYMBOL (backlight_device_unregister); +static void devm_backlight_device_release (struct device *dev,void *res) +{ +struct backlight_device *backlight = *(struct backlight_device **)res; + +backlight_device_unregister (backlight); +}
drivers/video/backlight/backlight.c void backlight_device_unregister(struct backlight_device *bd) · line 411
Quoted 10 lines
drivers/video/backlight/backlight.c +9
-0+struct backlight_device *devm_backlight_device_register (struct device *dev, +const char *name,struct device *parent,void *devdata, +const struct backlight_ops *ops, +const struct backlight_properties *props) +{ +struct backlight_device **ptr, *backlight; + + ptr =devres_alloc (devm_backlight_device_release,sizeof (*ptr), +GFP_KERNEL );
Well this is an awkward-looking thing. We allocate a silly little 4- or 8-byte object just to track the backlight_device* via the devres system.
I assume this was done so that devres could be used as a wrapper around the existing code?
Really it would be better if things like backlight_device_register() were themselves directly converted to use devm_foo(), yes?