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: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241005120256.13847-4-me@lodewillems.com>
Date: Sat,  5 Oct 2024 13:57:05 +0200
From: Lode Willems <me@...ewillems.com>
To: linux-input@...r.kernel.org
Cc: jikos@...nel.org,
	bentiss@...nel.org,
	linux-kernel@...r.kernel.org,
	Lode Willems <me@...ewillems.com>
Subject: [PATCH 3/4] HID: Kysona: check battery status every 5s using a workqueue

Use a workqueue to periodically check the battery status

Signed-off-by: Lode Willems <me@...ewillems.com>
---
 drivers/hid/hid-kysona.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/drivers/hid/hid-kysona.c b/drivers/hid/hid-kysona.c
index 91e359c31fa1..403bdc4a5e12 100644
--- a/drivers/hid/hid-kysona.c
+++ b/drivers/hid/hid-kysona.c
@@ -12,6 +12,8 @@
 
 #include "hid-ids.h"
 
+#define BATTERY_TIMEOUT_MS 5000
+
 #define BATTERY_REPORT_ID 4
 
 struct kysona_drvdata {
@@ -21,6 +23,7 @@ struct kysona_drvdata {
 	u8 battery_capacity;
 	bool battery_charging;
 	u16 battery_voltage;
+	struct delayed_work battery_work;
 };
 
 static enum power_supply_property kysona_battery_props[] = {
@@ -104,6 +107,17 @@ static void kysona_fetch_battery(struct hid_device *hdev)
 			"Battery query failed (err: %d)\n", ret);
 }
 
+static void kysona_battery_timer_tick(struct work_struct *work)
+{
+	struct kysona_drvdata *drv_data = container_of(work,
+		struct kysona_drvdata, battery_work.work);
+	struct hid_device *hdev = drv_data->hdev;
+
+	kysona_fetch_battery(hdev);
+	schedule_delayed_work(&drv_data->battery_work,
+			      msecs_to_jiffies(BATTERY_TIMEOUT_MS));
+}
+
 static int kysona_battery_probe(struct hid_device *hdev)
 {
 	struct kysona_drvdata *drv_data = hid_get_drvdata(hdev);
@@ -134,7 +148,11 @@ static int kysona_battery_probe(struct hid_device *hdev)
 	}
 
 	power_supply_powers(drv_data->battery, &hdev->dev);
+
+	INIT_DELAYED_WORK(&drv_data->battery_work, kysona_battery_timer_tick);
 	kysona_fetch_battery(hdev);
+	schedule_delayed_work(&drv_data->battery_work,
+			      msecs_to_jiffies(BATTERY_TIMEOUT_MS));
 
 	return ret;
 }
@@ -188,6 +206,16 @@ static int kysona_raw_event(struct hid_device *hdev,
 	return 0;
 }
 
+static void kysona_remove(struct hid_device *hdev)
+{
+	struct kysona_drvdata *drv_data = hid_get_drvdata(hdev);
+
+	if (drv_data->battery)
+		cancel_delayed_work_sync(&drv_data->battery_work);
+
+	hid_hw_stop(hdev);
+}
+
 static const struct hid_device_id kysona_devices[] = {
 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYSONA, USB_DEVICE_ID_KYSONA_M600_DONGLE) },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_KYSONA, USB_DEVICE_ID_KYSONA_M600_WIRED) },
@@ -199,7 +227,8 @@ static struct hid_driver kysona_driver = {
 	.name			= "kysona",
 	.id_table		= kysona_devices,
 	.probe			= kysona_probe,
-	.raw_event		= kysona_raw_event
+	.raw_event		= kysona_raw_event,
+	.remove			= kysona_remove
 };
 module_hid_driver(kysona_driver);
 
-- 
2.46.1


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ