linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Linus Walleij <linus.walleij@linaro.org>,
	linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 2/3] Input: gpio_keys - convert to use devm_*
Date: Fri, 25 Apr 2014 21:43:46 -0700	[thread overview]
Message-ID: <20140426044346.GB27425@core.coreip.homeip.net> (raw)
In-Reply-To: <1398442937-15309-3-git-send-email-andriy.shevchenko@linux.intel.com>

On Fri, Apr 25, 2014 at 07:22:16PM +0300, Andy Shevchenko wrote:
> This makes the error handling much more simpler than open-coding everything and
> in addition makes the probe function smaller an tidier.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Acked-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  drivers/input/keyboard/gpio_keys.c | 75 +++++++++++++-------------------------
>  1 file changed, 25 insertions(+), 50 deletions(-)
> 
> diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
> index 209d4c6..8791d94 100644
> --- a/drivers/input/keyboard/gpio_keys.c
> +++ b/drivers/input/keyboard/gpio_keys.c
> @@ -47,7 +47,7 @@ struct gpio_keys_drvdata {
>  	const struct gpio_keys_platform_data *pdata;
>  	struct input_dev *input;
>  	struct mutex disable_lock;
> -	struct gpio_button_data data[0];
> +	struct gpio_button_data *data;
>  };
>  
>  /*
> @@ -577,25 +577,22 @@ gpio_keys_get_devtree_pdata(struct device *dev)
>  	int i;
>  
>  	node = dev->of_node;
> -	if (!node) {
> -		error = -ENODEV;
> -		goto err_out;
> -	}
> +	if (!node)
> +		return ERR_PTR(-ENODEV);
>  
>  	nbuttons = of_get_child_count(node);
> -	if (nbuttons == 0) {
> -		error = -ENODEV;
> -		goto err_out;
> -	}
> +	if (nbuttons == 0)
> +		return ERR_PTR(-ENODEV);
>  
> -	pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
> -			GFP_KERNEL);
> -	if (!pdata) {
> -		error = -ENOMEM;
> -		goto err_out;
> -	}
> +	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
> +	if (!pdata)
> +		return ERR_PTR(-ENOMEM);
> +
> +	pdata->buttons = devm_kcalloc(dev, nbuttons, sizeof (*button),
> +				      GFP_KERNEL);
> +	if (!pdata->buttons)
> +		return ERR_PTR(-ENOMEM);

Why are we splitting the allocation in 2?

>  
> -	pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
>  	pdata->nbuttons = nbuttons;
>  
>  	pdata->rep = !!of_get_property(node, "autorepeat", NULL);
> @@ -618,7 +615,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
>  				dev_err(dev,
>  					"Failed to get gpio flags, error: %d\n",
>  					error);
> -			goto err_free_pdata;
> +			return ERR_PTR(error);
>  		}
>  
>  		button = &pdata->buttons[i++];
> @@ -629,8 +626,7 @@ gpio_keys_get_devtree_pdata(struct device *dev)
>  		if (of_property_read_u32(pp, "linux,code", &button->code)) {
>  			dev_err(dev, "Button without keycode: 0x%x\n",
>  				button->gpio);
> -			error = -EINVAL;
> -			goto err_free_pdata;
> +			return ERR_PTR(-EINVAL);
>  		}
>  
>  		button->desc = of_get_property(pp, "label", NULL);
> @@ -645,17 +641,10 @@ gpio_keys_get_devtree_pdata(struct device *dev)
>  			button->debounce_interval = 5;
>  	}
>  
> -	if (pdata->nbuttons == 0) {
> -		error = -EINVAL;
> -		goto err_free_pdata;
> -	}
> +	if (pdata->nbuttons == 0)
> +		return ERR_PTR(-EINVAL);
>  
>  	return pdata;
> -
> -err_free_pdata:
> -	kfree(pdata);
> -err_out:
> -	return ERR_PTR(error);
>  }
>  
>  static struct of_device_id gpio_keys_of_match[] = {
> @@ -699,16 +688,18 @@ static int gpio_keys_probe(struct platform_device *pdev)
>  			return PTR_ERR(pdata);
>  	}
>  
> -	ddata = kzalloc(sizeof(struct gpio_keys_drvdata) +
> -			pdata->nbuttons * sizeof(struct gpio_button_data),
> -			GFP_KERNEL);
> -	input = input_allocate_device();
> +	ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
> +	input = devm_input_allocate_device(dev);
>  	if (!ddata || !input) {
>  		dev_err(dev, "failed to allocate state\n");
> -		error = -ENOMEM;
> -		goto fail1;
> +		return -ENOMEM;
>  	}
>  
> +	ddata->data = devm_kcalloc(dev, pdata->nbuttons,  sizeof(*ddata->data),
> +				   GFP_KERNEL);
> +	if (!ddata->data)
> +		return -ENOMEM;
> +

Same here. I dropped the separate allocations and applied.

Thanks.

-- 
Dmitry

  reply	other threads:[~2014-04-26  4:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-25 16:22 [PATCH v3 0/3] Input: gpio-keys - clean up series Andy Shevchenko
2014-04-25 16:22 ` [PATCH v3 1/3] Input: gpio_keys - use dev instead of pdev in gpio_keys_setup_key() Andy Shevchenko
2014-04-25 21:21   ` Dmitry Torokhov
2014-04-28  9:42     ` Andy Shevchenko
2014-04-25 16:22 ` [PATCH v3 2/3] Input: gpio_keys - convert to use devm_* Andy Shevchenko
2014-04-26  4:43   ` Dmitry Torokhov [this message]
2014-04-28  9:40     ` Andy Shevchenko
2014-04-25 16:22 ` [PATCH v3 3/3] Input: gpio_keys - convert struct descriptions to kernel-doc Andy Shevchenko
2014-04-26  4:41   ` Dmitry Torokhov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140426044346.GB27425@core.coreip.homeip.net \
    --to=dmitry.torokhov@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).