Introduce devm_hwrng_register and convert a few dirvers
Subsystems (4)
PATCH 1/6 hwrng: add devm_* interfaces
In reply to Dmitry TorokhovThis change adds devm_hwrng_register and devm_hwrng_unregister which use can simplify error unwinding and unbinding code paths in device drivers.
--- drivers/char/hw_random/core.c | 42 ++++++++++++++++++++++++++++++++++++++++++ include/linux/hw_random.h | 4 ++++ 2 files changed, 46 insertions(+)
drivers/char/hw_random/core.c +42 -0
@@ -536,6 +536,48 @@ void hwrng_unregister(struct hwrng *rng) }EXPORT_SYMBOL_GPL (hwrng_unregister); +static void devm_hwrng_release (struct device *dev,void *res) +{ +hwrng_unregister (*(struct hwrng **)res); +} + +static int devm_hwrng_match (struct device *dev,void *res,void *data) +{ +struct hwrng **r = res; + +if (WARN_ON (!r || !*r)) +return 0 ; + +return *r == data; +} + +int devm_hwrng_register (struct device *dev,struct hwrng *rng) +{ +struct hwrng **ptr; +int error; + + ptr =devres_alloc (devm_hwrng_release,sizeof (*ptr),GFP_KERNEL ); +if (!ptr) +return -ENOMEM ; + + error =hwrng_register (rng); +if (error) { +devres_free (ptr); +return error; + } + + *ptr = rng; +devres_add (dev, ptr); +return 0 ; +} +EXPORT_SYMBOL_GPL (devm_hwrng_register); + +void devm_hwrng_unregister (struct device *dev,struct hwrng *rng) +{ +devres_release (dev, devm_hwrng_release, devm_hwrng_match, rng); +} +EXPORT_SYMBOL_GPL (devm_hwrng_unregister); +static int __init hwrng_modinit (void ) {return register_miscdev ();
include/linux/hw_random.h +4 -0
@@ -50,10 +50,14 @@ struct hwrng {struct completion cleanup_done; }; +struct device ; +/** Register a new Hardware Random Number Generator driver. */ extern int hwrng_register (struct hwrng *rng); +extern int devm_hwrng_register (struct device *dev,struct hwrng *rng);/** Unregister a Hardware Random Number Generator driver. */ extern void hwrng_unregister (struct hwrng *rng); +extern void devm_hwrng_unregister (struct device *dve,struct hwrng *rng);/** Feed random bits into the pool. */ extern void add_hwgenerator_randomness (const char *buffer,size_t count,size_t entropy);
Signature · 1 line
2.2.0.rc0.207.ga3a616c
PATCH 2/6 hwrng: bcm63xx - make use of devm_hwrng_register
In reply to Dmitry TorokhovThis change converts bcm63xx-rng to use devm* API for managing all resources, which allows us to dispense with the rest of error handling path and remove() function. Also we combine hwern and driver-private data into a single allocation, use clk_prepare_enable() instead of "naked" clk_enable() and move clock enabling/disabling into hwrnd inti(0 and cleanup() methods so the clock stays off until rng is used.
--- drivers/char/hw_random/bcm63xx-rng.c | 87 +++++++++++++----------------------- 1 file changed, 31 insertions(+), 56 deletions(-)
drivers/char/hw_random/bcm63xx-rng.c +31 -56
@@ -24,16 +24,22 @@#define RNG_MASK 0x10struct bcm63xx_rng_priv { +struct hwrng rng;struct clk *clk;void __iomem *regs; }; -#define to_rng_priv (rng) ((struct bcm63xx_rng_priv *)rng->priv) +#define to_rng_priv (rng) container_of(rng, struct bcm63xx_rng_priv, rng)static int bcm63xx_rng_init (struct hwrng *rng) {struct bcm63xx_rng_priv *priv =to_rng_priv (rng);u32 val; +int error; + + error =clk_prepare_enable (priv->clk); +if (error) +return error; val =__raw_readl (priv->regs +RNG_CTRL ); val |=RNG_EN ;
@@ -50,6 +56,8 @@ static void bcm63xx_rng_cleanup(struct hwrng *rng) val =__raw_readl (priv->regs +RNG_CTRL ); val &= ~RNG_EN ;__raw_writel (val, priv->regs +RNG_CTRL ); + +clk_didsable_unprepare (prov->clk); }static int bcm63xx_rng_data_present (struct hwrng *rng,int wait)
@@ -79,86 +87,53 @@ static int bcm63xx_rng_probe(struct platform_device *pdev) r =platform_get_resource (pdev,IORESOURCE_MEM ,0 );if (!r) {dev_err (&pdev->dev,"no iomem resource\n" ); - ret = -ENXIO ; - goto out; +return -ENXIO ; } priv =devm_kzalloc (&pdev->dev,sizeof (*priv),GFP_KERNEL ); -if (!priv) { - ret = -ENOMEM ; - goto out; - } - - rng =devm_kzalloc (&pdev->dev,sizeof (*rng),GFP_KERNEL ); -if (!rng) { - ret = -ENOMEM ; - goto out; +if (!priv) +return -ENOMEM ; + + priv->rng.name = pdev->name; + priv->rng.init = bcm63xx_rng_init; + priv->rng.cleanup = bcm63xx_rng_cleanup; + prov->rng.data_present = bcm63xx_rng_data_present; + priv->rng.data_read = bcm63xx_rng_data_read; + + priv->clk =devm_clk_get (&pdev->dev,"ipsec" ); +if (IS_ERR (priv->clk)) { + error =PTR_ERR (priv->clk); +dev_err (&pdev->dev,"no clock for device: %d\n" , error); +return error; } -platform_set_drvdata (pdev, rng); - rng->priv = (unsigned long )priv; - rng->name = pdev->name; - rng->init = bcm63xx_rng_init; - rng->cleanup = bcm63xx_rng_cleanup; - rng->data_present = bcm63xx_rng_data_present; - rng->data_read = bcm63xx_rng_data_read; - - clk =clk_get (&pdev->dev,"ipsec" ); -if (IS_ERR (clk)) { -dev_err (&pdev->dev,"no clock for device\n" ); - ret =PTR_ERR (clk); - goto out; - } - - priv->clk = clk; -if (!devm_request_mem_region (&pdev->dev, r->start,resource_size (r), pdev->name)) {dev_err (&pdev->dev,"request mem failed" ); - ret = -ENOMEM ; - goto out; +return -EBUSY ; } priv->regs =devm_ioremap_nocache (&pdev->dev, r->start,resource_size (r));if (!priv->regs) {dev_err (&pdev->dev,"ioremap failed" ); - ret = -ENOMEM ; - goto out; +return -ENOMEM ; } -clk_enable (clk); - - ret =hwrng_register (rng); -if (ret) { -dev_err (&pdev->dev,"failed to register rng device\n" ); - goto out_clk_disable; + error =devm_hwrng_register (&pdev->dev, &priv->rng); +if (error) { +dev_err (&pdev->dev,"failed to register rng device: %d\n" , + error); +return error; }dev_info (&pdev->dev,"registered RNG driver\n" );return 0 ; - -out_clk_disable: -clk_disable (clk); -out: -return ret; -} - -static int bcm63xx_rng_remove (struct platform_device *pdev) -{ -struct hwrng *rng =platform_get_drvdata (pdev); -struct bcm63xx_rng_priv *priv =to_rng_priv (rng); - -hwrng_unregister (rng); -clk_disable (priv->clk); - -return 0 ; }static struct platform_driver bcm63xx_rng_driver = { .probe = bcm63xx_rng_probe, - .remove = bcm63xx_rng_remove, .driver = { .name ="bcm63xx-rng" , },
Signature · 1 line
2.2.0.rc0.207.ga3a616c
PATCH 3/6 hwrng: exynos - make use of devm_hwrng_register
In reply to Dmitry TorokhovThis allows us to get rid of remove() method.
--- drivers/char/hw_random/exynos-rng.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/char/hw_random/exynos-rng.c +1 -11
@@ -131,16 +131,7 @@ static int exynos_rng_probe(struct platform_device *pdev)pm_runtime_use_autosuspend (&pdev->dev);pm_runtime_enable (&pdev->dev); -return hwrng_register (&exynos_rng->rng); -} - -static int exynos_rng_remove (struct platform_device *pdev) -{ -struct exynos_rng *exynos_rng =platform_get_drvdata (pdev); - -hwrng_unregister (&exynos_rng->rng); - -return 0 ; +return devm_hwrng_register (&pdev->dev, &exynos_rng->rng); }#ifdef CONFIG_PM
@@ -172,7 +163,6 @@ static struct platform_driver exynos_rng_driver = { .pm = &exynos_rng_pm_ops, }, .probe = exynos_rng_probe, - .remove = exynos_rng_remove, };module_platform_driver (exynos_rng_driver);
Signature · 1 line
2.2.0.rc0.207.ga3a616c
PATCH 4/6 hwrng: msm - make use of devm_hwrng_register
In reply to Dmitry TorokhovThis allows us to get rid of remove() method.
--- drivers/char/hw_random/msm-rng.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/char/hw_random/msm-rng.c +1 -10
@@ -157,7 +157,7 @@ static int msm_rng_probe(struct platform_device *pdev) rng->hwrng.cleanup = msm_rng_cleanup, rng->hwrng.read = msm_rng_read, - ret =hwrng_register (&rng->hwrng); + ret =devm_hwrng_register (&pdev->dev, &rng->hwrng);if (ret) {dev_err (&pdev->dev,"failed to register hwrng\n" );return ret;
@@ -166,14 +166,6 @@ static int msm_rng_probe(struct platform_device *pdev)return 0 ; } -static int msm_rng_remove (struct platform_device *pdev) -{ -struct msm_rng *rng =platform_get_drvdata (pdev); - -hwrng_unregister (&rng->hwrng); -return 0 ; -} -static const struct of_device_id msm_rng_of_match[] = { { .compatible ="qcom,prng" , }, {}
@@ -182,7 +174,6 @@ MODULE_DEVICE_TABLE(of, msm_rng_of_match);static struct platform_driver msm_rng_driver = { .probe = msm_rng_probe, - .remove = msm_rng_remove, .driver = { .name =KBUILD_MODNAME , .of_match_table =of_match_ptr (msm_rng_of_match),
Signature · 1 line
2.2.0.rc0.207.ga3a616c
PATCH 5/6 hwrng: iproc-rng200 - do not use static structure
In reply to Dmitry TorokhovInstead of using static hwrng structure that is reused between binds/unbinds of the device let's embed it into driver's private structure that we allocate. This way we are guaranteed not to stumble onto something left from previous bind attempt.
--- drivers/char/hw_random/iproc-rng200.c | 44 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 23 deletions(-)
drivers/char/hw_random/iproc-rng200.c +21 -23
@@ -48,9 +48,12 @@#define RNG_FIFO_COUNT_RNG_FIFO_COUNT_MASK 0x000000FFstruct iproc_rng200_dev { -void __iomem *base; +struct hwrng rng; +void __iomem *base; }; +#define to_rng_priv (rng) container_of(rng, struct iproc_rng200_dev, rng) +static void iproc_rng200_restart (void __iomem *rng_base) {uint32_t val;
@@ -89,11 +92,11 @@ static void iproc_rng200_restart(void __iomem *rng_base) }static int iproc_rng200_read (struct hwrng *rng,void *buf,size_t max, -bool wait) +bool wait) { -uint32_t status; +struct iproc_rng200_dev *priv =to_rng_priv (rng);uint32_t num_remaining = max; -struct iproc_rng200_dev *priv = (struct iproc_rng200_dev *)rng->priv; +uint32_t status;#define MAX_RESETS_PER_READ 1uint32_t num_resets =0 ;
@@ -151,10 +154,8 @@ static int iproc_rng200_read(struct hwrng *rng, void *buf, size_t max,static int iproc_rng200_init (struct hwrng *rng) { +struct iproc_rng200_dev *priv =to_rng_priv (rng);uint32_t val; -struct iproc_rng200_dev *priv; - - priv = (struct iproc_rng200_dev *)rng->priv;/* Setup RNG. */ val =ioread32 (priv->base +RNG_CTRL_OFFSET );
@@ -167,10 +168,8 @@ static int iproc_rng200_init(struct hwrng *rng)static void iproc_rng200_cleanup (struct hwrng *rng) { +struct iproc_rng200_dev *priv =to_rng_priv (rng);uint32_t val; -struct iproc_rng200_dev *priv; - - priv = (struct iproc_rng200_dev *)rng->priv;/* Disable RNG hardware */ val =ioread32 (priv->base +RNG_CTRL_OFFSET );
@@ -179,13 +178,6 @@ static void iproc_rng200_cleanup(struct hwrng *rng)iowrite32 (val, priv->base +RNG_CTRL_OFFSET ); } -static struct hwrng iproc_rng200_ops = { - .name ="iproc-rng200" , - .read = iproc_rng200_read, - .init = iproc_rng200_init, - .cleanup = iproc_rng200_cleanup, -}; -static int iproc_rng200_probe (struct platform_device *pdev) {struct iproc_rng200_dev *priv;
@@ -193,13 +185,10 @@ static int iproc_rng200_probe(struct platform_device *pdev)struct device *dev = &pdev->dev;int ret; - priv =devm_kzalloc (dev,sizeof (struct iproc_rng200_dev ),GFP_KERNEL ); + priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL );if (!priv)return -ENOMEM ; - iproc_rng200_ops.priv = (unsigned long )priv; -platform_set_drvdata (pdev, priv); -/* Map peripheral */ res =platform_get_resource (pdev,IORESOURCE_MEM ,0 );if (!res) {
@@ -213,13 +202,20 @@ static int iproc_rng200_probe(struct platform_device *pdev)return PTR_ERR (priv->base); } + priv->rng.name ="iproc-rng200" , + priv->rng.read = iproc_rng200_read, + priv->rng.init = iproc_rng200_init, + priv->rng.cleanup = iproc_rng200_cleanup, +/* Register driver */ - ret =hwrng_register (&iproc_rng200_ops); + ret =hwrng_register (&priv->rng);if (ret) {dev_err (dev,"hwrng registration failed\n" );return ret; } +platform_set_drvdata (pdev, priv); +dev_info (dev,"hwrng registered\n" );return 0 ;
@@ -227,8 +223,10 @@ static int iproc_rng200_probe(struct platform_device *pdev)static int iproc_rng200_remove (struct platform_device *pdev) { +struct iproc_rng200_dev *priv =platform_get_drvdata (pdev); +/* Unregister driver */ -hwrng_unregister (&iproc_rng200_ops); +hwrng_unregister (&priv->rng);return 0 ; }
Signature · 1 line
2.2.0.rc0.207.ga3a616c
cover Introduce devm_hwrng_register and convert a few dirvers
A few drivers can benefit from devm-style interface for hwrng since it is quite often the last thing that isn't automatically managed. Using devm_hwrng_register() in such drivers allows get rid of manual error unwinding and drivers' remove() methods.
I tested changes to iproc-rng200, the rest are compiled only.
Thanks.
Signature · 1 line
Dmitry
PATCH 6/6 hwrng: iproc-rng200 - make use of devm_hwrng_register
This allows us to get rid of driver's remove() method.
--- drivers/char/hw_random/iproc-rng200.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
drivers/char/hw_random/iproc-rng200.c +1 -14
@@ -208,29 +208,17 @@ static int iproc_rng200_probe(struct platform_device *pdev) priv->rng.cleanup = iproc_rng200_cleanup,/* Register driver */ - ret =hwrng_register (&priv->rng); + ret =devm_hwrng_register (dev, &priv->rng);if (ret) {dev_err (dev,"hwrng registration failed\n" );return ret; } -platform_set_drvdata (pdev, priv); -dev_info (dev,"hwrng registered\n" );return 0 ; } -static int iproc_rng200_remove (struct platform_device *pdev) -{ -struct iproc_rng200_dev *priv =platform_get_drvdata (pdev); - -/* Unregister driver */ -hwrng_unregister (&priv->rng); - -return 0 ; -} -static const struct of_device_id iproc_rng200_of_match[] = { { .compatible ="brcm,iproc-rng200" , }, {},
@@ -243,7 +231,6 @@ static struct platform_driver iproc_rng200_driver = { .of_match_table = iproc_rng200_of_match, }, .probe = iproc_rng200_probe, - .remove = iproc_rng200_remove, };module_platform_driver (iproc_rng200_driver);
Signature · 1 line
2.2.0.rc0.207.ga3a616c
Herbert Xu
maintainerQuoted 6 lines · Dmitry Torokhov
A few drivers can benefit from devm-style interface for hwrng since it is quite often the last thing that isn't automatically managed. Using
devm_hwrng_register()in such drivers allows get rid of manual error unwinding and drivers'remove()methods.I tested changes to iproc-rng200, the rest are compiled only.
All applied. Thanks!
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt