From: Lode Willems <me@lodewillems.com>
To: linux-input@vger.kernel.org
Cc: jikos@kernel.org, bentiss@kernel.org,
linux-kernel@vger.kernel.org, Lode Willems <me@lodewillems.com>
Subject: [PATCH 3/4] HID: Kysona: check battery status every 5s using a workqueue
Date: Sat, 5 Oct 2024 13:57:05 +0200 [thread overview]
Message-ID: <20241005120256.13847-4-me@lodewillems.com> (raw)
In-Reply-To: <20241005120256.13847-1-me@lodewillems.com>
Use a workqueue to periodically check the battery status
Signed-off-by: Lode Willems <me@lodewillems.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
next prev parent reply other threads:[~2024-10-05 12:03 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-05 11:57 [PATCH 0/4] HID: add Kysona driver Lode Willems
2024-10-05 11:57 ` [PATCH 1/4] HID: Add IDs for Kysona Lode Willems
2024-10-05 11:57 ` [PATCH 2/4] HID: Kysona: Add basic battery reporting for Kysona M600 Lode Willems
2024-10-05 11:57 ` Lode Willems [this message]
2024-10-05 11:57 ` [PATCH 4/4] HID: Kysona: add basic online status Lode Willems
2024-10-18 10:29 ` [PATCH 0/4] HID: add Kysona driver Jiri Kosina
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=20241005120256.13847-4-me@lodewillems.com \
--to=me@lodewillems.com \
--cc=bentiss@kernel.org \
--cc=jikos@kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.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
all inboxes | Powered by JetHome®