Re: [PATCH v5 6/9] platform/x86: serial-multi-instantiate: Reorganize I2C functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Jan 21, 2022 at 3:33 PM Stefan Binding
<sbinding@xxxxxxxxxxxxxxxxxxxxx> wrote:
>
> From: Lucas Tanure <tanureal@xxxxxxxxxxxxxxxxxxxxx>
>
> Reorganize I2C functions to accommodate SPI support
> Split the probe and factor out parts of the code
> that will be used in the SPI support
>
> Signed-off-by: Lucas Tanure <tanureal@xxxxxxxxxxxxxxxxxxxxx>
> Signed-off-by: Stefan Binding <sbinding@xxxxxxxxxxxxxxxxxxxxx>
> ---
>  .../platform/x86/serial-multi-instantiate.c   | 145 +++++++++++-------
>  1 file changed, 90 insertions(+), 55 deletions(-)
>
> diff --git a/drivers/platform/x86/serial-multi-instantiate.c b/drivers/platform/x86/serial-multi-instantiate.c
> index 32fb3f904547..162ec20a861a 100644
> --- a/drivers/platform/x86/serial-multi-instantiate.c
> +++ b/drivers/platform/x86/serial-multi-instantiate.c
> @@ -29,96 +29,131 @@ struct smi_instance {
>
>  struct smi {
>         int i2c_num;
> -       struct i2c_client *i2c_devs[];
> +       struct i2c_client **i2c_devs;
>  };
>
> -static int smi_probe(struct platform_device *pdev)
> +static int smi_get_irq(struct platform_device *pdev, struct acpi_device *adev,
> +                      const struct smi_instance *inst)
> +{
> +       int ret;
> +
> +       switch (inst->flags & IRQ_RESOURCE_TYPE) {
> +       case IRQ_RESOURCE_GPIO:
> +               ret = acpi_dev_gpio_irq_get(adev, inst->irq_idx);
> +               break;
> +       case IRQ_RESOURCE_APIC:
> +               ret = platform_get_irq(pdev, inst->irq_idx);
> +               break;
> +       default:
> +               ret = 0;
> +               break;

return 0;

would be slightly more efficient here

> +       }
> +
> +       if (ret < 0)
> +               dev_err_probe(&pdev->dev, ret, "Error requesting irq at index %d: %d\n",
> +                             inst->irq_idx, ret);
> +
> +       return ret;
> +}
> +
> +static void smi_devs_unregister(struct smi *smi)
> +{
> +       while (smi->i2c_num > 0)
> +               i2c_unregister_device(smi->i2c_devs[--smi->i2c_num]);
> +}
> +
> +/**
> + * smi_i2c_probe - Instantiate multiple I2C devices from inst array
> + * @pdev:      Platform device
> + * @adev:      ACPI device
> + * @smi:       Internal struct for Serial multi instantiate driver
> + * @inst:      Array of instances to probe

This is called inst_array below.

> + *
> + * Returns the number of I2C devices instantiate, Zero if none is found or a negative error code.
> + */
> +static int smi_i2c_probe(struct platform_device *pdev, struct acpi_device *adev, struct smi *smi,
> +                        const struct smi_instance *inst_array)
>  {
>         struct i2c_board_info board_info = {};
> -       const struct smi_instance *inst;
>         struct device *dev = &pdev->dev;
> -       struct acpi_device *adev;
> -       struct smi *smi;
>         char name[32];
> -       int i, ret;
> +       int i, ret, count;
>
> -       inst = device_get_match_data(dev);
> -       if (!inst) {
> -               dev_err(dev, "Error ACPI match data is missing\n");
> -               return -ENODEV;
> -       }
> -
> -       adev = ACPI_COMPANION(dev);
> -
> -       /* Count number of clients to instantiate */
>         ret = i2c_acpi_client_count(adev);
> -       if (ret < 0)
> -               return ret;
> +       if (ret <= 0)
> +               return ret == 0 ? -ENODEV : ret;

I would prefer

if ret < 0)
        return ret;
else if (!ret)
        return -ENODEV;

>
> -       smi = devm_kmalloc(dev, struct_size(smi, i2c_devs, ret), GFP_KERNEL);
> -       if (!smi)
> -               return -ENOMEM;
> +       count = ret;
>
> -       smi->i2c_num = ret;
> +       smi->i2c_devs = devm_kcalloc(dev, count, sizeof(*smi->i2c_devs), GFP_KERNEL);
> +       if (!smi->i2c_devs)
> +               return -ENOMEM;
>
> -       for (i = 0; i < smi->i2c_num && inst[i].type; i++) {
> +       for (i = 0; i < count && inst_array[i].type; i++) {
>                 memset(&board_info, 0, sizeof(board_info));
> -               strlcpy(board_info.type, inst[i].type, I2C_NAME_SIZE);
> -               snprintf(name, sizeof(name), "%s-%s.%d", dev_name(dev), inst[i].type, i);
> +               strscpy(board_info.type, inst_array[i].type, I2C_NAME_SIZE);

The switch-over from strlcpy() to strscpy() should be mentioned in the
changelog.

> +               snprintf(name, sizeof(name), "%s-%s.%d", dev_name(dev), inst_array[i].type, i);
>                 board_info.dev_name = name;
> -               switch (inst[i].flags & IRQ_RESOURCE_TYPE) {
> -               case IRQ_RESOURCE_GPIO:
> -                       ret = acpi_dev_gpio_irq_get(adev, inst[i].irq_idx);
> -                       if (ret < 0) {
> -                               dev_err(dev, "Error requesting irq at index %d: %d\n",
> -                                               inst[i].irq_idx, ret);
> -                               goto error;
> -                       }
> -                       board_info.irq = ret;
> -                       break;
> -               case IRQ_RESOURCE_APIC:
> -                       ret = platform_get_irq(pdev, inst[i].irq_idx);
> -                       if (ret < 0) {
> -                               dev_dbg(dev, "Error requesting irq at index %d: %d\n",
> -                                       inst[i].irq_idx, ret);
> -                               goto error;
> -                       }
> -                       board_info.irq = ret;
> -                       break;
> -               default:
> -                       board_info.irq = 0;
> -                       break;
> -               }
> +
> +               ret = smi_get_irq(pdev, adev, &inst_array[i]);
> +               if (ret < 0)
> +                       goto error;
> +               board_info.irq = ret;
> +
>                 smi->i2c_devs[i] = i2c_acpi_new_device(dev, i, &board_info);
>                 if (IS_ERR(smi->i2c_devs[i])) {
>                         ret = dev_err_probe(dev, PTR_ERR(smi->i2c_devs[i]),
>                                             "Error creating i2c-client, idx %d\n", i);
>                         goto error;
>                 }
> +               smi->i2c_num++;
>         }
> -       if (i < smi->i2c_num) {
> +       if (smi->i2c_num < count) {
>                 dev_err(dev, "Error finding driver, idx %d\n", i);

That's not particularly informative and so not particularly useful for
a casual receiver of this message.  I'd make it dev_dbg().

>                 ret = -ENODEV;
>                 goto error;
>         }
>
> -       platform_set_drvdata(pdev, smi);
> -       return 0;
> +       dev_info(dev, "Instantiated %d I2C devices.\n", smi->i2c_num);
>
> +       return 0;
>  error:
> -       while (--i >= 0)
> -               i2c_unregister_device(smi->i2c_devs[i]);
> +       smi_devs_unregister(smi);
>
>         return ret;
>  }
>
> +static int smi_probe(struct platform_device *pdev)
> +{
> +       const struct smi_instance *inst_array;
> +       struct device *dev = &pdev->dev;
> +       struct acpi_device *adev;
> +       struct smi *smi;
> +
> +       inst_array = device_get_match_data(dev);
> +       if (!inst_array) {
> +               dev_err(dev, "Error ACPI match data is missing\n");

Again, this isn't very informative to someone reading this message on
a random system, so I'd change the log level.

> +               return -ENODEV;
> +       }
> +
> +       adev = ACPI_COMPANION(dev);
> +       if (!adev)
> +               return -ENODEV;

Since you're at it anyway, I'd check the companion's presence upfront,
because you can't do much without it in any case.

> +
> +       smi = devm_kzalloc(dev, sizeof(*smi), GFP_KERNEL);
> +       if (!smi)
> +               return -ENOMEM;
> +
> +       platform_set_drvdata(pdev, smi);
> +
> +       return smi_i2c_probe(pdev, adev, smi, inst_array);
> +}
> +
>  static int smi_remove(struct platform_device *pdev)
>  {
>         struct smi *smi = platform_get_drvdata(pdev);
> -       int i;
>
> -       for (i = 0; i < smi->i2c_num; i++)
> -               i2c_unregister_device(smi->i2c_devs[i]);
> +       smi_devs_unregister(smi);
>
>         return 0;
>  }
> --
> 2.25.1
>



[Index of Archives]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux