From: Sebastian Reichel <sebastian.reichel@collabora.com>
To: Sebastian Reichel <sre@kernel.org>
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Pali Rohár" <pali.rohar@gmail.com>, "Milo Kim" <milo.kim@ti.com>,
"Andreas Dannenberg" <dannenberg@ti.com>,
"Krzysztof Kozlowski" <k.kozlowski@samsung.com>,
"Hans de Goede" <hdegoede@redhat.com>,
"Liam Breck" <kernel@networkimprov.net>,
linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org,
"Sebastian Reichel" <sebastian.reichel@collabora.com>
Subject: [PATCHv1 02/14] power: supply: bq2415x: fix race-condition in sysfs registration
Date: Mon, 1 Oct 2018 00:03:37 +0200 [thread overview]
Message-ID: <20180930220349.10515-3-sebastian.reichel@collabora.com> (raw)
In-Reply-To: <20180930220349.10515-1-sebastian.reichel@collabora.com>
This registers custom sysfs properties using the native functionality
of the power-supply framework, which cleans up the code a bit and
fixes a race-condition. Before this patch the sysfs attributes were
not properly registered to udev.
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
---
drivers/power/supply/bq2415x_charger.c | 119 ++++++++++---------------
1 file changed, 49 insertions(+), 70 deletions(-)
diff --git a/drivers/power/supply/bq2415x_charger.c b/drivers/power/supply/bq2415x_charger.c
index cbec70f3e73e..6693e7aeead5 100644
--- a/drivers/power/supply/bq2415x_charger.c
+++ b/drivers/power/supply/bq2415x_charger.c
@@ -1032,54 +1032,6 @@ static int bq2415x_power_supply_get_property(struct power_supply *psy,
return 0;
}
-static int bq2415x_power_supply_init(struct bq2415x_device *bq)
-{
- int ret;
- int chip;
- char revstr[8];
- struct power_supply_config psy_cfg = {
- .drv_data = bq,
- .of_node = bq->dev->of_node,
- };
-
- bq->charger_desc.name = bq->name;
- bq->charger_desc.type = POWER_SUPPLY_TYPE_USB;
- bq->charger_desc.properties = bq2415x_power_supply_props;
- bq->charger_desc.num_properties =
- ARRAY_SIZE(bq2415x_power_supply_props);
- bq->charger_desc.get_property = bq2415x_power_supply_get_property;
-
- ret = bq2415x_detect_chip(bq);
- if (ret < 0)
- chip = BQUNKNOWN;
- else
- chip = ret;
-
- ret = bq2415x_detect_revision(bq);
- if (ret < 0)
- strcpy(revstr, "unknown");
- else
- sprintf(revstr, "1.%d", ret);
-
- bq->model = kasprintf(GFP_KERNEL,
- "chip %s, revision %s, vender code %.3d",
- bq2415x_chip_name[chip], revstr,
- bq2415x_get_vender_code(bq));
- if (!bq->model) {
- dev_err(bq->dev, "failed to allocate model name\n");
- return -ENOMEM;
- }
-
- bq->charger = power_supply_register(bq->dev, &bq->charger_desc,
- &psy_cfg);
- if (IS_ERR(bq->charger)) {
- kfree(bq->model);
- return PTR_ERR(bq->charger);
- }
-
- return 0;
-}
-
static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
{
bq->autotimer = 0;
@@ -1496,7 +1448,7 @@ static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
-static struct attribute *bq2415x_sysfs_attributes[] = {
+static struct attribute *bq2415x_sysfs_attrs[] = {
/*
* TODO: some (appropriate) of these attrs should be switched to
* use power supply class props.
@@ -1525,19 +1477,55 @@ static struct attribute *bq2415x_sysfs_attributes[] = {
NULL,
};
-static const struct attribute_group bq2415x_sysfs_attr_group = {
- .attrs = bq2415x_sysfs_attributes,
-};
+ATTRIBUTE_GROUPS(bq2415x_sysfs);
-static int bq2415x_sysfs_init(struct bq2415x_device *bq)
+static int bq2415x_power_supply_init(struct bq2415x_device *bq)
{
- return sysfs_create_group(&bq->charger->dev.kobj,
- &bq2415x_sysfs_attr_group);
-}
+ int ret;
+ int chip;
+ char revstr[8];
+ struct power_supply_config psy_cfg = {
+ .drv_data = bq,
+ .of_node = bq->dev->of_node,
+ .attr_grp = bq2415x_sysfs_groups,
+ };
-static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
-{
- sysfs_remove_group(&bq->charger->dev.kobj, &bq2415x_sysfs_attr_group);
+ bq->charger_desc.name = bq->name;
+ bq->charger_desc.type = POWER_SUPPLY_TYPE_USB;
+ bq->charger_desc.properties = bq2415x_power_supply_props;
+ bq->charger_desc.num_properties =
+ ARRAY_SIZE(bq2415x_power_supply_props);
+ bq->charger_desc.get_property = bq2415x_power_supply_get_property;
+
+ ret = bq2415x_detect_chip(bq);
+ if (ret < 0)
+ chip = BQUNKNOWN;
+ else
+ chip = ret;
+
+ ret = bq2415x_detect_revision(bq);
+ if (ret < 0)
+ strcpy(revstr, "unknown");
+ else
+ sprintf(revstr, "1.%d", ret);
+
+ bq->model = kasprintf(GFP_KERNEL,
+ "chip %s, revision %s, vender code %.3d",
+ bq2415x_chip_name[chip], revstr,
+ bq2415x_get_vender_code(bq));
+ if (!bq->model) {
+ dev_err(bq->dev, "failed to allocate model name\n");
+ return -ENOMEM;
+ }
+
+ bq->charger = power_supply_register(bq->dev, &bq->charger_desc,
+ &psy_cfg);
+ if (IS_ERR(bq->charger)) {
+ kfree(bq->model);
+ return PTR_ERR(bq->charger);
+ }
+
+ return 0;
}
/* main bq2415x probe function */
@@ -1651,16 +1639,10 @@ static int bq2415x_probe(struct i2c_client *client,
goto error_2;
}
- ret = bq2415x_sysfs_init(bq);
- if (ret) {
- dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
- goto error_3;
- }
-
ret = bq2415x_set_defaults(bq);
if (ret) {
dev_err(bq->dev, "failed to set default values: %d\n", ret);
- goto error_4;
+ goto error_3;
}
if (bq->notify_node || bq->init_data.notify_device) {
@@ -1668,7 +1650,7 @@ static int bq2415x_probe(struct i2c_client *client,
ret = power_supply_reg_notifier(&bq->nb);
if (ret) {
dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
- goto error_4;
+ goto error_3;
}
bq->automode = 1;
@@ -1707,8 +1689,6 @@ static int bq2415x_probe(struct i2c_client *client,
dev_info(bq->dev, "driver registered\n");
return 0;
-error_4:
- bq2415x_sysfs_exit(bq);
error_3:
bq2415x_power_supply_exit(bq);
error_2:
@@ -1733,7 +1713,6 @@ static int bq2415x_remove(struct i2c_client *client)
power_supply_unreg_notifier(&bq->nb);
of_node_put(bq->notify_node);
- bq2415x_sysfs_exit(bq);
bq2415x_power_supply_exit(bq);
bq2415x_reset_chip(bq);
--
2.19.0
next prev parent reply other threads:[~2018-09-30 22:04 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-30 22:03 [PATCHv1 00/14] power: supply: Fix custom sysfs attribute registration Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 01/14] power: supply: core: add support for custom sysfs attributes Sebastian Reichel
2018-09-30 22:03 ` Sebastian Reichel [this message]
2018-09-30 22:03 ` [PATCHv1 03/14] power: supply: ds2780: fix race-condition in sysfs registration Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 04/14] power: supply: ds2781: " Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 05/14] power: supply: lp8788: " Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 06/14] power: supply: bq24190_charger: " Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 07/14] power: supply: bq24257: " Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 08/14] power: supply: charger-manager: simplify generation of sysfs attribute group name Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 09/14] power: supply: charger-manager: fix race-condition in sysfs registration Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 10/14] power: supply: pcf50633: " Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 11/14] power: supply: ds2780: fix race-condition in bin attribute registration Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 12/14] power: supply: ds2781: " Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 13/14] power: supply: ds2780: switch to devm_power_supply_register Sebastian Reichel
2018-09-30 22:03 ` [PATCHv1 14/14] power: supply: ds2781: " Sebastian Reichel
2018-10-02 23:00 ` [PATCHv1 00/14] power: supply: Fix custom sysfs attribute registration Greg Kroah-Hartman
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=20180930220349.10515-3-sebastian.reichel@collabora.com \
--to=sebastian.reichel@collabora.com \
--cc=dannenberg@ti.com \
--cc=gregkh@linuxfoundation.org \
--cc=hdegoede@redhat.com \
--cc=k.kozlowski@samsung.com \
--cc=kernel@networkimprov.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=milo.kim@ti.com \
--cc=pali.rohar@gmail.com \
--cc=sre@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).