lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240608-power-supply-extensions-v2-5-2dcd35b012ad@weissschuh.net>
Date: Sat, 08 Jun 2024 21:19:41 +0200
From: Thomas Weißschuh <linux@...ssschuh.net>
To: Sebastian Reichel <sre@...nel.org>, Armin Wolf <W_Armin@....de>
Cc: linux-pm@...r.kernel.org, linux-kernel@...r.kernel.org, 
 Thomas Weißschuh <linux@...ssschuh.net>
Subject: [PATCH RFC v2 5/5] platform/x86: system76: Use power_supply
 extension API

Signed-off-by: Thomas Weißschuh <linux@...ssschuh.net>
---
 drivers/platform/x86/system76_acpi.c | 93 ++++++++++++++++++++----------------
 1 file changed, 51 insertions(+), 42 deletions(-)

diff --git a/drivers/platform/x86/system76_acpi.c b/drivers/platform/x86/system76_acpi.c
index 3da753b3d00d..d9bc5cae2dda 100644
--- a/drivers/platform/x86/system76_acpi.c
+++ b/drivers/platform/x86/system76_acpi.c
@@ -162,7 +162,7 @@ enum {
 	THRESHOLD_END,
 };
 
-static ssize_t battery_get_threshold(int which, char *buf)
+static int battery_get_threshold(int which, int *val)
 {
 	struct acpi_object_list input;
 	union acpi_object param;
@@ -186,29 +186,21 @@ static ssize_t battery_get_threshold(int which, char *buf)
 	if (ret == BATTERY_THRESHOLD_INVALID)
 		return -EINVAL;
 
-	return sysfs_emit(buf, "%d\n", (int)ret);
+	*val = ret;
+	return 0;
 }
 
-static ssize_t battery_set_threshold(int which, const char *buf, size_t count)
+static int battery_set_threshold(int which, unsigned int value)
 {
 	struct acpi_object_list input;
 	union acpi_object params[2];
 	acpi_handle handle;
 	acpi_status status;
-	unsigned int value;
-	int ret;
 
 	handle = ec_get_handle();
 	if (!handle)
 		return -ENODEV;
 
-	ret = kstrtouint(buf, 10, &value);
-	if (ret)
-		return ret;
-
-	if (value > 100)
-		return -EINVAL;
-
 	input.count = 2;
 	input.pointer = params;
 	// Start/stop selection
@@ -222,52 +214,69 @@ static ssize_t battery_set_threshold(int which, const char *buf, size_t count)
 	if (ACPI_FAILURE(status))
 		return -EIO;
 
-	return count;
-}
-
-static ssize_t charge_control_start_threshold_show(struct device *dev,
-	struct device_attribute *attr, char *buf)
-{
-	return battery_get_threshold(THRESHOLD_START, buf);
-}
-
-static ssize_t charge_control_start_threshold_store(struct device *dev,
-	struct device_attribute *attr, const char *buf, size_t count)
-{
-	return battery_set_threshold(THRESHOLD_START, buf, count);
+	return 0;
 }
 
-static DEVICE_ATTR_RW(charge_control_start_threshold);
+static const enum power_supply_property system76_battery_properties[] = {
+	POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD,
+	POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD,
+};
 
-static ssize_t charge_control_end_threshold_show(struct device *dev,
-	struct device_attribute *attr, char *buf)
+static int system76_property_is_writeable(struct power_supply *psy,
+					  const struct power_supply_ext *ext,
+					  enum power_supply_property psp)
 {
-	return battery_get_threshold(THRESHOLD_END, buf);
+	return true;
 }
 
-static ssize_t charge_control_end_threshold_store(struct device *dev,
-	struct device_attribute *attr, const char *buf, size_t count)
+static int system76_get_property(struct power_supply *psy,
+				 const struct power_supply_ext *ext,
+				 enum power_supply_property psp,
+				 union power_supply_propval *val)
 {
-	return battery_set_threshold(THRESHOLD_END, buf, count);
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD:
+		return battery_get_threshold(THRESHOLD_START, &val->intval);
+	case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
+		return battery_get_threshold(THRESHOLD_END, &val->intval);
+	default:
+		return -EINVAL;
+	};
+}
+
+static int system76_set_property(struct power_supply *psy, const struct power_supply_ext *ext,
+				 enum power_supply_property psp,
+				 const union power_supply_propval *val)
+{
+	switch (psp) {
+	case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD:
+		if (val->intval < 0 || val->intval > 100)
+			return -EINVAL;
+		return battery_set_threshold(THRESHOLD_START, val->intval);
+	case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD:
+		if (val->intval < 0 || val->intval > 100)
+			return -EINVAL;
+		return battery_set_threshold(THRESHOLD_END, val->intval);
+	default:
+		return -EINVAL;
+	};
 }
 
-static DEVICE_ATTR_RW(charge_control_end_threshold);
-
-static struct attribute *system76_battery_attrs[] = {
-	&dev_attr_charge_control_start_threshold.attr,
-	&dev_attr_charge_control_end_threshold.attr,
-	NULL,
+static const struct power_supply_ext system76_power_supply_ext = {
+	.properties            = system76_battery_properties,
+	.num_properties        = ARRAY_SIZE(system76_battery_properties),
+	.property_is_writeable = system76_property_is_writeable,
+	.get_property          = system76_get_property,
+	.set_property          = system76_set_property,
 };
 
-ATTRIBUTE_GROUPS(system76_battery);
-
 static int system76_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
 {
 	// System76 EC only supports 1 battery
 	if (strcmp(battery->desc->name, "BAT0") != 0)
 		return -ENODEV;
 
-	if (device_add_groups(&battery->dev, system76_battery_groups))
+	if (power_supply_register_extension(battery, &system76_power_supply_ext))
 		return -ENODEV;
 
 	return 0;
@@ -275,7 +284,7 @@ static int system76_battery_add(struct power_supply *battery, struct acpi_batter
 
 static int system76_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
 {
-	device_remove_groups(&battery->dev, system76_battery_groups);
+	power_supply_unregister_extension(battery, &system76_power_supply_ext);
 	return 0;
 }
 

-- 
2.45.2


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ