* [PATCH v2] gpio: vt8500: memory cleanup missing
@ 2013-01-10 19:09 Tony Prisk
2013-01-14 14:14 ` Grant Likely
0 siblings, 1 reply; 4+ messages in thread
From: Tony Prisk @ 2013-01-10 19:09 UTC (permalink / raw)
To: linux-arm-kernel
This driver is missing a .remove callback, and the fail path on
probe is incomplete.
If an error occurs in vt8500_add_chips, gpio_base is not unmapped.
The driver is also ignoring the return value from this function so
if a chip fails to register it completes as successful.
Replaced pr_err with dev_err in vt8500_add_chips since the device is
available.
There is also no .remove callback defined. To allow removing the
registered chips, I have moved *vtchip to be a static global.
Signed-off-by: Tony Prisk <linux@prisktech.co.nz>
---
v2:
Remove global variable and use platform_set_drvdata instead.
drivers/gpio/gpio-vt8500.c | 51 +++++++++++++++++++++++++++++++++++---------
1 file changed, 41 insertions(+), 10 deletions(-)
diff --git a/drivers/gpio/gpio-vt8500.c b/drivers/gpio/gpio-vt8500.c
index b53320a..87e59b5 100644
--- a/drivers/gpio/gpio-vt8500.c
+++ b/drivers/gpio/gpio-vt8500.c
@@ -233,10 +233,12 @@ static int vt8500_add_chips(struct platform_device *pdev, void __iomem *base,
sizeof(struct vt8500_gpio_chip) * data->num_banks,
GFP_KERNEL);
if (!vtchip) {
- pr_err("%s: failed to allocate chip memory\n", __func__);
+ dev_err(&pdev->dev, "failed to allocate chip memory\n");
return -ENOMEM;
}
+ platform_set_drvdata(pdev, vtchip);
+
for (i = 0; i < data->num_banks; i++) {
vtchip[i].base = base;
vtchip[i].regs = &data->banks[i];
@@ -261,6 +263,7 @@ static int vt8500_add_chips(struct platform_device *pdev, void __iomem *base,
gpiochip_add(chip);
}
+
return 0;
}
@@ -273,36 +276,64 @@ static struct of_device_id vt8500_gpio_dt_ids[] = {
static int vt8500_gpio_probe(struct platform_device *pdev)
{
+ int ret;
void __iomem *gpio_base;
- struct device_node *np;
+ struct device_node *np = pdev->dev.of_node;
const struct of_device_id *of_id =
of_match_device(vt8500_gpio_dt_ids, &pdev->dev);
- if (!of_id) {
- dev_err(&pdev->dev, "Failed to find gpio controller\n");
+ if (!np) {
+ dev_err(&pdev->dev, "GPIO node missing in devicetree\n");
return -ENODEV;
}
- np = pdev->dev.of_node;
- if (!np) {
- dev_err(&pdev->dev, "Missing GPIO description in devicetree\n");
- return -EFAULT;
+ if (!of_id) {
+ dev_err(&pdev->dev, "No matching driver data\n");
+ return -ENODEV;
}
gpio_base = of_iomap(np, 0);
if (!gpio_base) {
dev_err(&pdev->dev, "Unable to map GPIO registers\n");
- of_node_put(np);
return -ENOMEM;
}
- vt8500_add_chips(pdev, gpio_base, of_id->data);
+ ret = vt8500_add_chips(pdev, gpio_base, of_id->data);
+ if (ret) {
+ iounmap(gpio_base);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int vt8500_gpio_remove(struct platform_device *pdev)
+{
+ int i;
+ int ret;
+ const struct vt8500_gpio_data *data;
+ struct vt8500_gpio_chip *vtchip = platform_get_drvdata(pdev);
+ void __iomem *gpio_base = vtchip[0].base;
+ const struct of_device_id *of_id =
+ of_match_device(vt8500_gpio_dt_ids, &pdev->dev);
+
+ data = of_id->data;
+
+ for (i = 0; i < data->num_banks; i++) {
+ ret = gpiochip_remove(&vtchip[i].chip);
+ if (ret)
+ dev_warn(&pdev->dev, "gpiochip_remove returned %d\n",
+ ret);
+ }
+
+ iounmap(gpio_base);
return 0;
}
static struct platform_driver vt8500_gpio_driver = {
.probe = vt8500_gpio_probe,
+ .remove = vt8500_gpio_remove,
.driver = {
.name = "vt8500-gpio",
.owner = THIS_MODULE,
--
1.7.9.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2] gpio: vt8500: memory cleanup missing
2013-01-10 19:09 Tony Prisk
@ 2013-01-14 14:14 ` Grant Likely
0 siblings, 0 replies; 4+ messages in thread
From: Grant Likely @ 2013-01-14 14:14 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, 11 Jan 2013 08:09:46 +1300, Tony Prisk <linux@prisktech.co.nz> wrote:
> This driver is missing a .remove callback, and the fail path on
> probe is incomplete.
>
> If an error occurs in vt8500_add_chips, gpio_base is not unmapped.
> The driver is also ignoring the return value from this function so
> if a chip fails to register it completes as successful.
>
> Replaced pr_err with dev_err in vt8500_add_chips since the device is
> available.
>
> There is also no .remove callback defined. To allow removing the
> registered chips, I have moved *vtchip to be a static global.
>
> Signed-off-by: Tony Prisk <linux@prisktech.co.nz>
> ---
> v2:
> Remove global variable and use platform_set_drvdata instead.
>
> drivers/gpio/gpio-vt8500.c | 51 +++++++++++++++++++++++++++++++++++---------
> 1 file changed, 41 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/gpio/gpio-vt8500.c b/drivers/gpio/gpio-vt8500.c
> index b53320a..87e59b5 100644
> --- a/drivers/gpio/gpio-vt8500.c
> +++ b/drivers/gpio/gpio-vt8500.c
> @@ -233,10 +233,12 @@ static int vt8500_add_chips(struct platform_device *pdev, void __iomem *base,
> sizeof(struct vt8500_gpio_chip) * data->num_banks,
> GFP_KERNEL);
> if (!vtchip) {
> - pr_err("%s: failed to allocate chip memory\n", __func__);
> + dev_err(&pdev->dev, "failed to allocate chip memory\n");
> return -ENOMEM;
> }
>
> + platform_set_drvdata(pdev, vtchip);
> +
> for (i = 0; i < data->num_banks; i++) {
> vtchip[i].base = base;
> vtchip[i].regs = &data->banks[i];
> @@ -261,6 +263,7 @@ static int vt8500_add_chips(struct platform_device *pdev, void __iomem *base,
>
> gpiochip_add(chip);
> }
> +
> return 0;
> }
>
Watch out; irrelevant whitespace change here. From a maintainer point of
voiew, I'm less confident about a patch when I see unrelated whitespace
changes because it suggests that there are things in the patch that you
don't intend. It helps me out a lot if this stuff gets scrubbed before I
see it.
> @@ -273,36 +276,64 @@ static struct of_device_id vt8500_gpio_dt_ids[] = {
>
> static int vt8500_gpio_probe(struct platform_device *pdev)
> {
> + int ret;
> void __iomem *gpio_base;
> - struct device_node *np;
> + struct device_node *np = pdev->dev.of_node;
> const struct of_device_id *of_id =
> of_match_device(vt8500_gpio_dt_ids, &pdev->dev);
>
> - if (!of_id) {
> - dev_err(&pdev->dev, "Failed to find gpio controller\n");
> + if (!np) {
> + dev_err(&pdev->dev, "GPIO node missing in devicetree\n");
> return -ENODEV;
> }
>
> - np = pdev->dev.of_node;
> - if (!np) {
> - dev_err(&pdev->dev, "Missing GPIO description in devicetree\n");
> - return -EFAULT;
> + if (!of_id) {
> + dev_err(&pdev->dev, "No matching driver data\n");
> + return -ENODEV;
> }
Why is this flipped around? I don't see any functional reason for this
change.
In actual fact, since the driver needs both it only needs to test for
the of_id. If there is no node, then of_id will never be set.
>
> gpio_base = of_iomap(np, 0);
> if (!gpio_base) {
> dev_err(&pdev->dev, "Unable to map GPIO registers\n");
> - of_node_put(np);
> return -ENOMEM;
> }
>
> - vt8500_add_chips(pdev, gpio_base, of_id->data);
> + ret = vt8500_add_chips(pdev, gpio_base, of_id->data);
> + if (ret) {
> + iounmap(gpio_base);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int vt8500_gpio_remove(struct platform_device *pdev)
> +{
> + int i;
> + int ret;
> + const struct vt8500_gpio_data *data;
> + struct vt8500_gpio_chip *vtchip = platform_get_drvdata(pdev);
> + void __iomem *gpio_base = vtchip[0].base;
> + const struct of_device_id *of_id =
> + of_match_device(vt8500_gpio_dt_ids, &pdev->dev);
> +
> + data = of_id->data;
> +
> + for (i = 0; i < data->num_banks; i++) {
It would make for simpler code all around if num_banks was cached in the
vt8500_gpio_chip structure during the .probe() routine. It looks wrong
to be calling of_match_device() in the remove hook.
Otherwise this iteration looks much better.
g.
> + ret = gpiochip_remove(&vtchip[i].chip);
> + if (ret)
> + dev_warn(&pdev->dev, "gpiochip_remove returned %d\n",
> + ret);
> + }
> +
> + iounmap(gpio_base);
>
> return 0;
> }
>
> static struct platform_driver vt8500_gpio_driver = {
> .probe = vt8500_gpio_probe,
> + .remove = vt8500_gpio_remove,
> .driver = {
> .name = "vt8500-gpio",
> .owner = THIS_MODULE,
> --
> 1.7.9.5
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo at vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
--
Grant Likely, B.Sc, P.Eng.
Secret Lab Technologies, Ltd.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] gpio: vt8500: memory cleanup missing
@ 2013-01-14 18:37 Tony Prisk
2013-01-15 5:20 ` Tony Prisk
0 siblings, 1 reply; 4+ messages in thread
From: Tony Prisk @ 2013-01-14 18:37 UTC (permalink / raw)
To: linux-arm-kernel
This driver is missing a .remove callback, and the fail path on
probe is incomplete.
If an error occurs in vt8500_add_chips, gpio_base is not unmapped.
The driver is also ignoring the return value from this function so
if a chip fails to register it completes as successful.
Replaced pr_err with dev_err in vt8500_add_chips since the device is
available.
There is also no .remove callback defined. To allow removing the
registered chips, I have moved *vtchip to be a static global.
Signed-off-by: Tony Prisk <linux@prisktech.co.nz>
---
Hi Grant,
Let me know what you think of these changes.
v2:
Removed unnecessary whitespace change.
Removed test against pdev->dev.of_node (np). Replaced code with a
devm_request_and_ioremap so np is now unneccessary. This also removes the need
for cleanup in the fail path.
Move struct vt8500_gpio_chip within vt8500_data and store the iobase and
num_banks in vt8500_data.
drivers/gpio/gpio-vt8500.c | 61 +++++++++++++++++++++++++++++++++++---------
1 file changed, 49 insertions(+), 12 deletions(-)
diff --git a/drivers/gpio/gpio-vt8500.c b/drivers/gpio/gpio-vt8500.c
index b53320a..5c8cd7c 100644
--- a/drivers/gpio/gpio-vt8500.c
+++ b/drivers/gpio/gpio-vt8500.c
@@ -127,6 +127,12 @@ struct vt8500_gpio_chip {
void __iomem *base;
};
+struct vt8500_data {
+ struct vt8500_gpio_chip *chip;
+ void __iomem *iobase;
+ int num_banks;
+};
+
#define to_vt8500(__chip) container_of(__chip, struct vt8500_gpio_chip, chip)
@@ -224,19 +230,32 @@ static int vt8500_of_xlate(struct gpio_chip *gc,
static int vt8500_add_chips(struct platform_device *pdev, void __iomem *base,
const struct vt8500_gpio_data *data)
{
+ struct vt8500_data *priv;
struct vt8500_gpio_chip *vtchip;
struct gpio_chip *chip;
int i;
int pin_cnt = 0;
- vtchip = devm_kzalloc(&pdev->dev,
+ priv = devm_kzalloc(&pdev->dev, sizeof(struct vt8500_data), GFP_KERNEL);
+ if (!priv) {
+ dev_err(&pdev->dev, "failed to allocate memory\n");
+ return -ENOMEM;
+ }
+
+ priv->chip = devm_kzalloc(&pdev->dev,
sizeof(struct vt8500_gpio_chip) * data->num_banks,
GFP_KERNEL);
- if (!vtchip) {
- pr_err("%s: failed to allocate chip memory\n", __func__);
+ if (!priv->chip) {
+ dev_err(&pdev->dev, "failed to allocate chip memory\n");
return -ENOMEM;
}
+ priv->iobase = base;
+ priv->num_banks = data->num_banks;
+ platform_set_drvdata(pdev, priv);
+
+ vtchip = priv->chip;
+
for (i = 0; i < data->num_banks; i++) {
vtchip[i].base = base;
vtchip[i].regs = &data->banks[i];
@@ -273,36 +292,54 @@ static struct of_device_id vt8500_gpio_dt_ids[] = {
static int vt8500_gpio_probe(struct platform_device *pdev)
{
+ int ret;
void __iomem *gpio_base;
- struct device_node *np;
+ struct resource *res;
const struct of_device_id *of_id =
of_match_device(vt8500_gpio_dt_ids, &pdev->dev);
if (!of_id) {
- dev_err(&pdev->dev, "Failed to find gpio controller\n");
+ dev_err(&pdev->dev, "No matching driver data\n");
return -ENODEV;
}
- np = pdev->dev.of_node;
- if (!np) {
- dev_err(&pdev->dev, "Missing GPIO description in devicetree\n");
- return -EFAULT;
+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if (!res) {
+ dev_err(&pdev->dev, "Unable to get IO resource\n");
+ return -ENODEV;
}
- gpio_base = of_iomap(np, 0);
+ gpio_base = devm_request_and_ioremap(&pdev->dev, res);
if (!gpio_base) {
dev_err(&pdev->dev, "Unable to map GPIO registers\n");
- of_node_put(np);
return -ENOMEM;
}
- vt8500_add_chips(pdev, gpio_base, of_id->data);
+ ret = vt8500_add_chips(pdev, gpio_base, of_id->data);
+
+ return ret;
+}
+
+static int vt8500_gpio_remove(struct platform_device *pdev)
+{
+ int i;
+ int ret;
+ struct vt8500_data *priv = platform_get_drvdata(pdev);
+ struct vt8500_gpio_chip *vtchip = priv->chip;
+
+ for (i = 0; i < priv->num_banks; i++) {
+ ret = gpiochip_remove(&vtchip[i].chip);
+ if (ret)
+ dev_warn(&pdev->dev, "gpiochip_remove returned %d\n",
+ ret);
+ }
return 0;
}
static struct platform_driver vt8500_gpio_driver = {
.probe = vt8500_gpio_probe,
+ .remove = vt8500_gpio_remove,
.driver = {
.name = "vt8500-gpio",
.owner = THIS_MODULE,
--
1.7.9.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2] gpio: vt8500: memory cleanup missing
2013-01-14 18:37 [PATCH v2] gpio: vt8500: memory cleanup missing Tony Prisk
@ 2013-01-15 5:20 ` Tony Prisk
0 siblings, 0 replies; 4+ messages in thread
From: Tony Prisk @ 2013-01-15 5:20 UTC (permalink / raw)
To: linux-arm-kernel
On Tue, 2013-01-15 at 07:37 +1300, Tony Prisk wrote:
> This driver is missing a .remove callback, and the fail path on
> probe is incomplete.
>
> If an error occurs in vt8500_add_chips, gpio_base is not unmapped.
> The driver is also ignoring the return value from this function so
> if a chip fails to register it completes as successful.
>
> Replaced pr_err with dev_err in vt8500_add_chips since the device is
> available.
>
> There is also no .remove callback defined. To allow removing the
> registered chips, I have moved *vtchip to be a static global.
>
> Signed-off-by: Tony Prisk <linux@prisktech.co.nz>
> ---
> Hi Grant,
>
> Let me know what you think of these changes.
>
> v2:
> Removed unnecessary whitespace change.
> Removed test against pdev->dev.of_node (np). Replaced code with a
> devm_request_and_ioremap so np is now unneccessary. This also removes the need
> for cleanup in the fail path.
> Move struct vt8500_gpio_chip within vt8500_data and store the iobase and
> num_banks in vt8500_data.
>
Grant,
If there are no further changes for this patch, would you mind
correcting the commit message when you merge it?
-There is also no .remove callback defined. To allow removing the
-Registered chips, I have moved *vtchip to be a static global.
+ There is also no .remove callback defined.
Otherwise, I will fix it along with whatever other comments come in.
Regards
Tony P
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-01-15 5:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-14 18:37 [PATCH v2] gpio: vt8500: memory cleanup missing Tony Prisk
2013-01-15 5:20 ` Tony Prisk
-- strict thread matches above, loose matches on Subject: below --
2013-01-10 19:09 Tony Prisk
2013-01-14 14:14 ` Grant Likely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).