Re: [PATCH v4 11/13] gpiolib: provide a dedicated function for setting lineinfo

From: Andy Shevchenko
Date: Wed Jan 08 2020 - 07:41:55 EST


On Tue, Dec 24, 2019 at 01:07:07PM +0100, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx>
>
> We'll soon be filling out the gpioline_info structure in multiple
> places. Add a separate function that given a gpio_desc sets all relevant
> fields.
>

Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>

> Signed-off-by: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx>
> ---
> drivers/gpio/gpiolib.c | 98 ++++++++++++++++++++++++------------------
> 1 file changed, 55 insertions(+), 43 deletions(-)
>
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index 543244355e1c..276a7068f23c 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -1148,6 +1148,60 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)
> return ret;
> }
>
> +static void gpio_desc_to_lineinfo(struct gpio_desc *desc,
> + struct gpioline_info *info)
> +{
> + struct gpio_chip *chip = desc->gdev->chip;
> + unsigned long flags;
> +
> + spin_lock_irqsave(&gpio_lock, flags);
> +
> + if (desc->name) {
> + strncpy(info->name, desc->name, sizeof(info->name));
> + info->name[sizeof(info->name) - 1] = '\0';
> + } else {
> + info->name[0] = '\0';
> + }
> +
> + if (desc->label) {
> + strncpy(info->consumer, desc->label, sizeof(info->consumer));
> + info->consumer[sizeof(info->consumer) - 1] = '\0';
> + } else {
> + info->consumer[0] = '\0';
> + }
> +
> + /*
> + * Userspace only need to know that the kernel is using this GPIO so
> + * it can't use it.
> + */
> + info->flags = 0;
> + if (test_bit(FLAG_REQUESTED, &desc->flags) ||
> + test_bit(FLAG_IS_HOGGED, &desc->flags) ||
> + test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
> + test_bit(FLAG_EXPORT, &desc->flags) ||
> + test_bit(FLAG_SYSFS, &desc->flags) ||
> + !pinctrl_gpio_can_use_line(chip->base + info->line_offset))
> + info->flags |= GPIOLINE_FLAG_KERNEL;
> + if (test_bit(FLAG_IS_OUT, &desc->flags))
> + info->flags |= GPIOLINE_FLAG_IS_OUT;
> + if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
> + info->flags |= GPIOLINE_FLAG_ACTIVE_LOW;
> + if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
> + info->flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
> + GPIOLINE_FLAG_IS_OUT);
> + if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
> + info->flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
> + GPIOLINE_FLAG_IS_OUT);
> + if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
> + info->flags |= GPIOLINE_FLAG_BIAS_DISABLE;
> + if (test_bit(FLAG_PULL_DOWN, &desc->flags))
> + info->flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
> + if (test_bit(FLAG_PULL_UP, &desc->flags))
> + info->flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
> +
> + spin_unlock_irqrestore(&gpio_lock, flags);
> +}
> +
> /*
> * gpio_ioctl() - ioctl handler for the GPIO chardev
> */
> @@ -1188,49 +1242,7 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
> if (IS_ERR(desc))
> return PTR_ERR(desc);
>
> - if (desc->name) {
> - strncpy(lineinfo.name, desc->name,
> - sizeof(lineinfo.name));
> - lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
> - } else {
> - lineinfo.name[0] = '\0';
> - }
> - if (desc->label) {
> - strncpy(lineinfo.consumer, desc->label,
> - sizeof(lineinfo.consumer));
> - lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
> - } else {
> - lineinfo.consumer[0] = '\0';
> - }
> -
> - /*
> - * Userspace only need to know that the kernel is using
> - * this GPIO so it can't use it.
> - */
> - lineinfo.flags = 0;
> - if (test_bit(FLAG_REQUESTED, &desc->flags) ||
> - test_bit(FLAG_IS_HOGGED, &desc->flags) ||
> - test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
> - test_bit(FLAG_EXPORT, &desc->flags) ||
> - test_bit(FLAG_SYSFS, &desc->flags) ||
> - !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset))
> - lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
> - if (test_bit(FLAG_IS_OUT, &desc->flags))
> - lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
> - if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
> - lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
> - if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
> - lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
> - GPIOLINE_FLAG_IS_OUT);
> - if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
> - lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
> - GPIOLINE_FLAG_IS_OUT);
> - if (test_bit(FLAG_BIAS_DISABLE, &desc->flags))
> - lineinfo.flags |= GPIOLINE_FLAG_BIAS_DISABLE;
> - if (test_bit(FLAG_PULL_DOWN, &desc->flags))
> - lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_DOWN;
> - if (test_bit(FLAG_PULL_UP, &desc->flags))
> - lineinfo.flags |= GPIOLINE_FLAG_BIAS_PULL_UP;
> + gpio_desc_to_lineinfo(desc, &lineinfo);
>
> if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
> return -EFAULT;
> --
> 2.23.0
>

--
With Best Regards,
Andy Shevchenko