LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Bastian Germann <bastiangermann@fishpost.de>,
	Luca Tettamanti <kronos.it@gmail.com>
Cc: Jean Delvare <jdelvare@suse.com>,
	linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] hwmon: (asus_atk0110) Replace deprecated device register call
Date: Thu, 31 May 2018 07:27:21 -0700	[thread overview]
Message-ID: <f885fcb0-d9f3-7702-13ad-5af2913131f0@roeck-us.net> (raw)
In-Reply-To: <20180531140136.13969-1-bastiangermann@fishpost.de>

Hi Bastian,

On 05/31/2018 07:01 AM, Bastian Germann wrote:
> Make the asus_atk0110 driver use hwmon_device_register_with_groups instead
> of the deprecated hwmon_device_register.
> Construct the expected attribute_group array from the sensor list which
> contains all needed attributes.
> 
> Signed-off-by: Bastian Germann <bastiangermann@fishpost.de>
> ---
>   drivers/hwmon/asus_atk0110.c | 71 +++++++++++++++++++++++++++++-------
>   1 file changed, 58 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/hwmon/asus_atk0110.c b/drivers/hwmon/asus_atk0110.c
> index 975c43d446f8..8a35fb27ff50 100644
> --- a/drivers/hwmon/asus_atk0110.c
> +++ b/drivers/hwmon/asus_atk0110.c
> @@ -125,6 +125,7 @@ struct atk_data {
>   	int temperature_count;
>   	int fan_count;
>   	struct list_head sensor_list;
> +	const struct attribute_group **attr_groups;
>   
>   	struct {
>   		struct dentry *root;
> @@ -1242,27 +1243,67 @@ static void atk_free_sensors(struct atk_data *data)
>   	}
>   }
>   
> +static int atk_init_attribute_groups(struct atk_data *data)
> +{
> +	struct atk_sensor_data *s;
> +	struct attribute **attrs;
> +	struct attribute_group *group;
> +	const struct attribute_group **groups;
> +	int i = 0;
> +	int len = (data->voltage_count + data->temperature_count
> +			+ data->fan_count) * 4 + 1;
> +
> +	attrs = kcalloc(len, sizeof(struct attribute *), GFP_KERNEL);

Please use devm_kcalloc().

> +	if (!attrs)
> +		return -ENOMEM;
> +
> +	list_for_each_entry(s, &data->sensor_list, list) {
> +		attrs[i++] = &s->input_attr.attr;
> +		attrs[i++] = &s->label_attr.attr;
> +		attrs[i++] = &s->limit1_attr.attr;
> +		attrs[i++] = &s->limit2_attr.attr;
> +	}
> +	attrs[i] = NULL;
> +
> +	group = kzalloc(sizeof(*group), GFP_KERNEL);

devm_kzalloc()

> +	if (!group)
> +		goto cleanup;
> +	group->attrs = attrs;
> +
> +	groups = kcalloc(2, sizeof(struct attribute_group *), GFP_KERNEL);

devm_kcalloc()

> +	if (!groups) {
> +		kfree(group);
> +		goto cleanup;
> +	}
> +	groups[0] = group;
> +	groups[1] = NULL;

kcalloc() clears the allocated memory, so this is unnecessary. However,
I would suggest to make group and groups part of struct atk_sensor_data.
The size is static, and the fields will always be needed, so dynamic
allocation doesn't buy anything except overhead.

> +	data->attr_groups = groups;
> + > +	return 0;
> +cleanup:
> +	kfree(attrs);

then you won't need the kfree() calls.

The same should be done with the existing calls of kzalloc(),
but that should be a separate patch.

> +	return -ENOMEM;
> +}
> +
> +static void atk_free_attribute_groups(struct atk_data *data)
> +{
> +	kfree(data->attr_groups[0]->attrs);
> +	kfree(data->attr_groups[0]);
> +	kfree(data->attr_groups);
> +}
> +
>   static int atk_register_hwmon(struct atk_data *data)
>   {
>   	struct device *dev = &data->acpi_dev->dev;
> -	int err;
>   
>   	dev_dbg(dev, "registering hwmon device\n");
> -	data->hwmon_dev = hwmon_device_register(dev);
> +	data->hwmon_dev = hwmon_device_register_with_groups(dev, "atk0110",
> +							    data,
> +							    data->attr_groups);
>   	if (IS_ERR(data->hwmon_dev))
>   		return PTR_ERR(data->hwmon_dev);
>   
> -	dev_dbg(dev, "populating sysfs directory\n");
> -	err = atk_create_files(data);
> -	if (err)
> -		goto remove;
> -
>   	return 0;
> -remove:
> -	/* Cleanup the registered files */
> -	atk_remove_files(data);
> -	hwmon_device_unregister(data->hwmon_dev);
> -	return err;
>   }
>   
>   static int atk_probe_if(struct atk_data *data)
> @@ -1397,6 +1438,9 @@ static int atk_add(struct acpi_device *device)
>   		goto out;
>   	}
>   
> +	err = atk_init_attribute_groups(data);
> +	if (err)
> +		goto out;
>   	err = atk_register_hwmon(data);
>   	if (err)
>   		goto cleanup;
> @@ -1407,6 +1451,7 @@ static int atk_add(struct acpi_device *device)
>   	return 0;
>   cleanup:
>   	atk_free_sensors(data);
> +	atk_free_attribute_groups(data);
>   out:
>   	if (data->disable_ec)
>   		atk_ec_ctl(data, 0);
> @@ -1423,9 +1468,9 @@ static int atk_remove(struct acpi_device *device)
>   
>   	atk_debugfs_cleanup(data);
>   
> -	atk_remove_files(data);

You should also remove the no longer needed functions as well as the
name attribute.

>   	atk_free_sensors(data);
>   	hwmon_device_unregister(data->hwmon_dev);
> +	atk_free_attribute_groups(data);
>   
>   	if (data->disable_ec) {
>   		if (atk_ec_ctl(data, 0))
> 

  parent reply	other threads:[~2018-05-31 14:27 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-31 14:01 Bastian Germann
2018-05-31 14:01 ` [PATCH 2/2] hwmon: (asus_atk0110) Remove unused manual sysfs file management Bastian Germann
2018-05-31 14:28   ` Guenter Roeck
2018-05-31 14:27 ` Guenter Roeck [this message]
2018-05-31 22:56   ` [PATCH 1/2] hwmon: (asus_atk0110) Replace deprecated device register call Bastian Germann
2018-05-31 22:57     ` [PATCH 2/2] hwmon: (asus_atk0110) Make use of device managed memory Bastian Germann
2018-06-01  9:54       ` Andy Shevchenko
2018-06-01 11:07         ` [PATCH 1/2] hwmon: (asus_atk0110) Replace deprecated device register call Bastian Germann
2018-06-01 11:07           ` [PATCH 2/2] hwmon: (asus_atk0110) Make use of device managed memory Bastian Germann
2018-06-01 13:28           ` [PATCH 1/2] hwmon: (asus_atk0110) Replace deprecated device register call Guenter Roeck
2018-06-01 15:14             ` asus_atk0110: Use non-deprecated registration and managed memory Bastian Germann
2018-06-01 15:14               ` [PATCH v4 1/2] hwmon: (asus_atk0110) Replace deprecated device register call Bastian Germann
2018-06-01 16:35                 ` Guenter Roeck
2018-06-04 20:35                 ` Luca Tettamanti
2018-06-01 15:14               ` [PATCH v4 2/2] hwmon: (asus_atk0110) Make use of device managed memory Bastian Germann
2018-06-01 16:40                 ` Guenter Roeck

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=f885fcb0-d9f3-7702-13ad-5af2913131f0@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=bastiangermann@fishpost.de \
    --cc=jdelvare@suse.com \
    --cc=kronos.it@gmail.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: [PATCH 1/2] hwmon: (asus_atk0110) Replace deprecated device register call' \
    /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

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).