mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: "Alexander Shiyan" <shc_work@mail.ru>
To: "Jean-Christophe PLAGNIOL-VILLARD" <plagnioj@jcrosoft.com>
Cc: barebox@lists.infradead.org
Subject: Re[2]: [PATCH 1/2] GPIO: Check for "gpio_is_valid" first
Date: Fri, 10 May 2013 17:20:06 +0400	[thread overview]
Message-ID: <1368192006.902657520@f345.mail.ru> (raw)
In-Reply-To: <20130510124125.GC19265@game.jcrosoft.org>

> HI,
> 
> 	do as in linux
> 
> 	introduce
> 
> 	static struct gpio_desc *gpio_to_desc(unsigned gpio)
> 
> 	and we need to check the gpio is valid before calling _free/ser/get
> 	etc..

Indeed.

> > Signed-off-by: Alexander Shiyan <shc_work@mail.ru>
> > ---
> >  drivers/gpio/gpiolib.c | 181 ++++++++++++++++++++++++++-----------------------
> >  1 file changed, 95 insertions(+), 86 deletions(-)
> > 
> > diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> > index 6398268..1662dcd 100644
> > --- a/drivers/gpio/gpiolib.c
> > +++ b/drivers/gpio/gpiolib.c
> > @@ -34,117 +34,126 @@ static int gpio_ensure_requested(struct gpio_info *gi, int gpio)
> >  
> >  int gpio_request(unsigned gpio, const char *label)
> >  {
> > -	struct gpio_info *gi = &gpio_desc[gpio];
> > -	struct gpio_chip *chip = gi->chip;
> > -	int ret;
> > -
> > -	if (!gpio_is_valid(gpio))
> > -		return -EINVAL;
> > -	if (!chip)
> > -		return -EINVAL;
> > -	if (gi->requested)
> > -		return -EBUSY;
> > -	if (chip->ops->request) {
> > -		ret = chip->ops->request(chip, gpio - chip->base);
> > -		if (ret)
> > -			return ret;
> > -	}
> > +	if (gpio_is_valid(gpio)) {
> > +		struct gpio_info *gi = &gpio_desc[gpio];
> > +		struct gpio_chip *chip = gi->chip;
> > +		int ret;
> > +
> > +		if (!chip)
> > +			return -EINVAL;
> > +		if (gi->requested)
> > +			return -EBUSY;
> > +		if (chip->ops->request) {
> > +			ret = chip->ops->request(chip, gpio - chip->base);
> > +			if (ret)
> > +				return ret;
> > +		}
> >  
> > -	gi->requested = true;
> > -	gi->label = xstrdup(label);
> > +		gi->requested = true;
> > +		gi->label = xstrdup(label);
> >  
> > -	return 0;
> > +		return 0;
> > +	}
> > +
> > +	return -EINVAL;
> >  }
> >  
> >  void gpio_free(unsigned gpio)
> >  {
> > -	struct gpio_info *gi = &gpio_desc[gpio];
> > -	struct gpio_chip *chip = gi->chip;
> > -
> > -	if (!gpio_is_valid(gpio))
> > -		return;
> > -	if (!chip)
> > -		return;
> > -	if (!gi->requested)
> > -		return;
> > -	if (chip->ops->free)
> > -		chip->ops->free(chip, gpio - chip->base);
> > -
> > -	gi->requested = false;
> > -	free(gi->label);
> > +	if (gpio_is_valid(gpio)) {
> > +		struct gpio_info *gi = &gpio_desc[gpio];
> > +		struct gpio_chip *chip = gi->chip;
> > +
> > +		if (!chip)
> > +			return;
> > +		if (!gi->requested)
> > +			return;
> > +		if (chip->ops->free)
> > +			chip->ops->free(chip, gpio - chip->base);
> > +
> > +		gi->requested = false;
> > +		free(gi->label);
> > +	}
> >  }
> >  
> >  void gpio_set_value(unsigned gpio, int value)
> >  {
> > -	struct gpio_info *gi = &gpio_desc[gpio];
> > -	struct gpio_chip *chip = gi->chip;
> > -
> > -	if (!gpio_is_valid(gpio))
> > -		return;
> > -	if (!chip)
> > -		return;
> > -	if (gpio_ensure_requested(gi, gpio))
> > -		return;
> > -	if (!chip->ops->set)
> > -		return;
> > -	chip->ops->set(chip, gpio - chip->base, value);
> > +	if (gpio_is_valid(gpio)) {
> > +		struct gpio_info *gi = &gpio_desc[gpio];
> > +		struct gpio_chip *chip = gi->chip;
> > +
> > +		if (!chip)
> > +			return;
> > +		if (gpio_ensure_requested(gi, gpio))
> > +			return;
> > +		if (!chip->ops->set)
> > +			return;
> > +		chip->ops->set(chip, gpio - chip->base, value);
> > +	}
> >  }
> >  EXPORT_SYMBOL(gpio_set_value);
> >  
> >  int gpio_get_value(unsigned gpio)
> >  {
> > -	struct gpio_info *gi = &gpio_desc[gpio];
> > -	struct gpio_chip *chip = gi->chip;
> > -	int ret;
> > -
> > -	if (!gpio_is_valid(gpio))
> > -		return -EINVAL;
> > -	if (!chip)
> > -		return -ENODEV;
> > -	ret = gpio_ensure_requested(gi, gpio);
> > -	if (ret)
> > -		return ret;
> > -	if (!chip->ops->get)
> > -		return -ENOSYS;
> > -	return chip->ops->get(chip, gpio - chip->base);
> > +	if (gpio_is_valid(gpio)) {
> > +		struct gpio_info *gi = &gpio_desc[gpio];
> > +		struct gpio_chip *chip = gi->chip;
> > +		int ret;
> > +
> > +		if (!chip)
> > +			return -ENODEV;
> > +		ret = gpio_ensure_requested(gi, gpio);
> > +		if (ret)
> > +			return ret;
> > +		if (!chip->ops->get)
> > +			return -ENOSYS;
> > +		return chip->ops->get(chip, gpio - chip->base);
> > +	}
> > +
> > +	return -EINVAL;
> >  }
> >  EXPORT_SYMBOL(gpio_get_value);
> >  
> >  int gpio_direction_output(unsigned gpio, int value)
> >  {
> > -	struct gpio_info *gi = &gpio_desc[gpio];
> > -	struct gpio_chip *chip = gi->chip;
> > -	int ret;
> > -
> > -	if (!gpio_is_valid(gpio))
> > -		return -EINVAL;
> > -	if (!chip)
> > -		return -ENODEV;
> > -	ret = gpio_ensure_requested(gi, gpio);
> > -	if (ret)
> > -		return ret;
> > -	if (!chip->ops->direction_output)
> > -		return -ENOSYS;
> > -	return chip->ops->direction_output(chip, gpio - chip->base, value);
> > +	if (gpio_is_valid(gpio)) {
> > +		struct gpio_info *gi = &gpio_desc[gpio];
> > +		struct gpio_chip *chip = gi->chip;
> > +		int ret;
> > +
> > +		if (!chip)
> > +			return -ENODEV;
> > +		ret = gpio_ensure_requested(gi, gpio);
> > +		if (ret)
> > +			return ret;
> > +		if (!chip->ops->direction_output)
> > +			return -ENOSYS;
> > +		return chip->ops->direction_output(chip, gpio - chip->base,
> > +						   value);
> > +	}
> > +
> > +	return -EINVAL;
> >  }
> >  EXPORT_SYMBOL(gpio_direction_output);
> >  
> >  int gpio_direction_input(unsigned gpio)
> >  {
> > -	struct gpio_info *gi = &gpio_desc[gpio];
> > -	struct gpio_chip *chip = gi->chip;
> > -	int ret;
> > -
> > -	if (!gpio_is_valid(gpio))
> > -		return -EINVAL;
> > -	if (!chip)
> > -		return -ENODEV;
> > -	ret = gpio_ensure_requested(gi, gpio);
> > -	if (ret)
> > -		return ret;
> > -	if (!chip->ops->direction_input)
> > -		return -ENOSYS;
> > -	return chip->ops->direction_input(chip, gpio - chip->base);
> > +	if (gpio_is_valid(gpio)) {
> > +		struct gpio_info *gi = &gpio_desc[gpio];
> > +		struct gpio_chip *chip = gi->chip;
> > +		int ret;
> > +
> > +		if (!chip)
> > +			return -ENODEV;
> > +		ret = gpio_ensure_requested(gi, gpio);
> > +		if (ret)
> > +			return ret;
> > +		if (!chip->ops->direction_input)
> > +			return -ENOSYS;
> > +		return chip->ops->direction_input(chip, gpio - chip->base);
> > +	}
> > +
> > +	return -EINVAL;
> >  }
> >  EXPORT_SYMBOL(gpio_direction_input);

---
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

      reply	other threads:[~2013-05-10 13:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-10  7:27 Alexander Shiyan
2013-05-10  7:27 ` [PATCH 2/2] GPIO: Remove unneeded "gpio_is_valid" calls Alexander Shiyan
2013-05-10 12:37 ` [PATCH 1/2] GPIO: Check for "gpio_is_valid" first Jean-Christophe PLAGNIOL-VILLARD
2013-05-10 12:41 ` Jean-Christophe PLAGNIOL-VILLARD
2013-05-10 13:20   ` Alexander Shiyan [this message]

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=1368192006.902657520@f345.mail.ru \
    --to=shc_work@mail.ru \
    --cc=barebox@lists.infradead.org \
    --cc=plagnioj@jcrosoft.com \
    /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