From nobody Thu Nov 28 01:43:06 2024 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CCA70146592; Sat, 5 Oct 2024 12:03:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728129828; cv=none; b=ukNYd4ZJyHgUXAOdaAvFp8wmLSEvdS5WHAF6qdmPjy1gsx/XPMjzi0pCAT+6a3rSw2KPYTslG6QezTBOhesGLYgFMlqVeHqu7TKTUh0sAdEGgJUjAUaFZqeMsHDnog5Ism5MnfaKET+Xtb+o9lvVhOw5ZegfaYwvzwKZGasLbgs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728129828; c=relaxed/simple; bh=lmeD9Qaj2zD+ZD0KAIuu/2/NgmkBpVCG1Dw4SXlNrT0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nNJlQg7b3vTSFC/BMzvgsPzeUik8AyalQQHzJX1bnqHrZ/fZ3F1C30D6ABnuuF8bz+b0inNEbVlbVmJBea1gkxHzGFGynK4tzNkI+dGdtE8DivG9K0cduZYXsjdhwhzUb9om4ADbvrSBSqDuL+7C/waWlPxvXikjJIeWxGIAYoA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lodewillems.com; spf=pass smtp.mailfrom=lodewillems.com; dkim=pass (2048-bit key) header.d=lodewillems.com header.i=@lodewillems.com header.b=tkAvWn8V; arc=none smtp.client-ip=80.241.56.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lodewillems.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lodewillems.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lodewillems.com header.i=@lodewillems.com header.b="tkAvWn8V" Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4XLPFp2qyWz9tV3; Sat, 5 Oct 2024 14:03:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lodewillems.com; s=MBO0001; t=1728129822; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EfRavpUO6nQZ5ohUYARfB7BRubgVwCDpKb/oVP7T3RU=; b=tkAvWn8VBZBch4qXbaXOHGPvG6mbs3izBA1+mt19SUBUNyyshzGZaFjL2jradRLMJHAVvs ij7LEOL7lKieoPcfwsOJ4Ur/7rvUDZPY+I862NlL25HrS9uf3WvYqABGUukm9rusTMVUtr E68sUljBs8Gv4zuvpRP4spZc1aZUhYRyVgFy2YjCWY8ak4CIYxQh6zsgFVBpLTs1ljKrFU UZyhNjONf07qd85cUmuvVkeye2WZVfrKsh62gVH/+hcRGQR0c6l6fDvxJqioKlVsWJf8WZ lvCNY5cTXc0BbxOVkyImKjEv2MJ7awzKMf7mBR0TO9SbiNBbGvhyhhJyUjNFbg== From: Lode Willems To: linux-input@vger.kernel.org Cc: jikos@kernel.org, bentiss@kernel.org, linux-kernel@vger.kernel.org, Lode Willems Subject: [PATCH 1/4] HID: Add IDs for Kysona Date: Sat, 5 Oct 2024 13:57:03 +0200 Message-ID: <20241005120256.13847-2-me@lodewillems.com> In-Reply-To: <20241005120256.13847-1-me@lodewillems.com> References: <20241005120256.13847-1-me@lodewillems.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 4XLPFp2qyWz9tV3 Content-Type: text/plain; charset="utf-8" Add the vendor ID for Kysona and the product IDs for the M600 mouse (both the dongle and the mouse itself) Signed-off-by: Lode Willems --- drivers/hid/hid-ids.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h index 781c5aa29859..76052c653112 100644 --- a/drivers/hid/hid-ids.h +++ b/drivers/hid/hid-ids.h @@ -749,6 +749,10 @@ #define USB_DEVICE_ID_KYE_MOUSEPEN_I608X_V2 0x501A #define USB_DEVICE_ID_KYE_PENSKETCH_T609A 0x501B =20 +#define USB_VENDOR_ID_KYSONA 0x3554 +#define USB_DEVICE_ID_KYSONA_M600_DONGLE 0xF57C +#define USB_DEVICE_ID_KYSONA_M600_WIRED 0xF57D + #define USB_VENDOR_ID_LABTEC 0x1020 #define USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD 0x0006 #define USB_DEVICE_ID_LABTEC_ODDOR_HANDBRAKE 0x8888 --=20 2.46.1 From nobody Thu Nov 28 01:43:06 2024 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90A9C1553BC; Sat, 5 Oct 2024 12:03:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728129834; cv=none; b=smahH2rsc7f+va45qJo/v2MM3yvLgdoHv0HFJyjTqd85fLQqUrt3jes9V31ktH/wVjo3GVrqau5OTbVWYgNpWzzKwj2+TnnX4/+zCIu1oCHDmWUJuxaOPzb5ihc1qYvBO895Qs0XiYO8w/T0YLNbxybUqdeY9YnXmQ/6vesB7Ok= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728129834; c=relaxed/simple; bh=AHbl1M0PKa4qlOqRbPcd+FqsIhQGu8AVRZGUvPXC6V0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ssDWZkAMlu2Y3WuiQP061Wx5fZ7JQUUSlZKwiIddtHtEscDXvQCBac4pwhJb7WLCsTOJ2pUHHPNQctul7NEBTRrZLLl97NKLAGxuzq2HsDSv0GOVd7hNEytXFS4GvpHi3/Ikf4+2pKL4wqLUGVzhbSPmp7pcIacrGpDNbnDqk1w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lodewillems.com; spf=pass smtp.mailfrom=lodewillems.com; dkim=pass (2048-bit key) header.d=lodewillems.com header.i=@lodewillems.com header.b=smeFAYyL; arc=none smtp.client-ip=80.241.56.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lodewillems.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lodewillems.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lodewillems.com header.i=@lodewillems.com header.b="smeFAYyL" Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4XLPFq0xSYz9sd5; Sat, 5 Oct 2024 14:03:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lodewillems.com; s=MBO0001; t=1728129823; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=33jmOsnNcAO1avfPTWzl0cE9y4C5TXL327mIibaeIhg=; b=smeFAYyLyvwIuku4gPw3/Ge25eMlmin+RlgjVhBQEEInQyXhBDBUPyandPOXokfw9OCld+ I8jdv8GWIZoAYRDa23sOLB44lR5AcMhQWEIPzwBmsMsULxfwo7DnCdkTSyE42D2aacUPoz s4bXm2HE7N2qygVpzbbNJq+DB/uC9qlJa7TOsqgQw74jqthoo0/u6EFIZSUBOUNe1evG38 nxrb3Ld7qBaWWR2RCUSn5l92oKQuOsDLiA+KWRVXVe8ehjMMYAdDPXx7PN96lxzpSOYHan 9hkVkg7SzT9Jrd6euqPpYOgJkzFTw5VuwryjqfgpRrksicOLvqBgQlvLaBLU+w== From: Lode Willems To: linux-input@vger.kernel.org Cc: jikos@kernel.org, bentiss@kernel.org, linux-kernel@vger.kernel.org, Lode Willems Subject: [PATCH 2/4] HID: Kysona: Add basic battery reporting for Kysona M600 Date: Sat, 5 Oct 2024 13:57:04 +0200 Message-ID: <20241005120256.13847-3-me@lodewillems.com> In-Reply-To: <20241005120256.13847-1-me@lodewillems.com> References: <20241005120256.13847-1-me@lodewillems.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 4XLPFq0xSYz9sd5 Content-Type: text/plain; charset="utf-8" In this initial the battery is only probed once, a following patch will add periodic checking. Signed-off-by: Lode Willems --- drivers/hid/Kconfig | 9 ++ drivers/hid/Makefile | 1 + drivers/hid/hid-kysona.c | 208 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 218 insertions(+) create mode 100644 drivers/hid/hid-kysona.c diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 08446c89eff6..541dbee4fa00 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -459,6 +459,15 @@ config HID_KYE - MousePen i608X tablet - EasyPen M610X tablet =20 +config HID_KYSONA + tristate "Kysona devices" + depends on USB_HID + help + Support for Kysona mice. + + Say Y here if you have a Kysona M600 mouse + and want to be able to read its battery capacity. + config HID_UCLOGIC tristate "UC-Logic" depends on USB_HID diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index e40f1ddebbb7..c39ef074ac66 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -69,6 +69,7 @@ obj-$(CONFIG_HID_JABRA) +=3D hid-jabra.o obj-$(CONFIG_HID_KENSINGTON) +=3D hid-kensington.o obj-$(CONFIG_HID_KEYTOUCH) +=3D hid-keytouch.o obj-$(CONFIG_HID_KYE) +=3D hid-kye.o +obj-$(CONFIG_HID_KYSONA) +=3D hid-kysona.o obj-$(CONFIG_HID_LCPOWER) +=3D hid-lcpower.o obj-$(CONFIG_HID_LENOVO) +=3D hid-lenovo.o obj-$(CONFIG_HID_LETSKETCH) +=3D hid-letsketch.o diff --git a/drivers/hid/hid-kysona.c b/drivers/hid/hid-kysona.c new file mode 100644 index 000000000000..91e359c31fa1 --- /dev/null +++ b/drivers/hid/hid-kysona.c @@ -0,0 +1,208 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * USB HID driver for Kysona + * Kysona M600 mice. + * + * Copyright (c) 2024 Lode Willems + */ + +#include +#include +#include + +#include "hid-ids.h" + +#define BATTERY_REPORT_ID 4 + +struct kysona_drvdata { + struct hid_device *hdev; + struct power_supply_desc battery_desc; + struct power_supply *battery; + u8 battery_capacity; + bool battery_charging; + u16 battery_voltage; +}; + +static enum power_supply_property kysona_battery_props[] =3D { + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_SCOPE, + POWER_SUPPLY_PROP_MODEL_NAME, + POWER_SUPPLY_PROP_VOLTAGE_NOW +}; + +static int kysona_battery_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct kysona_drvdata *drv_data =3D power_supply_get_drvdata(psy); + int ret =3D 0; + + switch (psp) { + case POWER_SUPPLY_PROP_PRESENT: + val->intval =3D 1; + break; + case POWER_SUPPLY_PROP_STATUS: + // TODO: check if device is online + val->intval =3D drv_data->battery_charging ? + POWER_SUPPLY_STATUS_CHARGING : + POWER_SUPPLY_STATUS_DISCHARGING; + break; + case POWER_SUPPLY_PROP_SCOPE: + val->intval =3D POWER_SUPPLY_SCOPE_DEVICE; + break; + case POWER_SUPPLY_PROP_CAPACITY: + val->intval =3D drv_data->battery_capacity; + break; + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + /* hardware reports voltage in mV. sysfs expects uV */ + val->intval =3D drv_data->battery_voltage * 1000; + break; + case POWER_SUPPLY_PROP_MODEL_NAME: + val->strval =3D drv_data->hdev->name; + break; + default: + ret =3D -EINVAL; + break; + } + return ret; +} + +static const char kysona_battery_request[] =3D { + 0x08, BATTERY_REPORT_ID, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49 +}; + +static int kysona_m600_fetch_battery(struct hid_device *hdev) +{ + u8 *write_buf; + int ret; + + /* Request battery information */ + write_buf =3D kmemdup(kysona_battery_request, sizeof(kysona_battery_reque= st), GFP_KERNEL); + if (!write_buf) + return -ENOMEM; + + ret =3D hid_hw_raw_request(hdev, kysona_battery_request[0], + write_buf, sizeof(kysona_battery_request), + HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); + if (ret < (int)sizeof(kysona_battery_request)) { + hid_err(hdev, "hid_hw_raw_request() failed with %d\n", ret); + ret =3D -ENODATA; + } + kfree(write_buf); + return ret; +} + +static void kysona_fetch_battery(struct hid_device *hdev) +{ + int ret =3D kysona_m600_fetch_battery(hdev); + + if (ret < 0) + hid_dbg(hdev, + "Battery query failed (err: %d)\n", ret); +} + +static int kysona_battery_probe(struct hid_device *hdev) +{ + struct kysona_drvdata *drv_data =3D hid_get_drvdata(hdev); + struct power_supply_config pscfg =3D { .drv_data =3D drv_data }; + int ret =3D 0; + + drv_data->battery_capacity =3D 100; + + drv_data->battery_desc.properties =3D kysona_battery_props; + drv_data->battery_desc.num_properties =3D ARRAY_SIZE(kysona_battery_props= ); + drv_data->battery_desc.get_property =3D kysona_battery_get_property; + drv_data->battery_desc.type =3D POWER_SUPPLY_TYPE_BATTERY; + drv_data->battery_desc.use_for_apm =3D 0; + drv_data->battery_desc.name =3D devm_kasprintf(&hdev->dev, GFP_KERNEL, + "kysona-%s-battery", + strlen(hdev->uniq) ? + hdev->uniq : dev_name(&hdev->dev)); + if (!drv_data->battery_desc.name) + return -ENOMEM; + + drv_data->battery =3D devm_power_supply_register(&hdev->dev, + &drv_data->battery_desc, &pscfg); + if (IS_ERR(drv_data->battery)) { + ret =3D PTR_ERR(drv_data->battery); + drv_data->battery =3D NULL; + hid_err(hdev, "Unable to register battery device\n"); + return ret; + } + + power_supply_powers(drv_data->battery, &hdev->dev); + kysona_fetch_battery(hdev); + + return ret; +} + +static int kysona_probe(struct hid_device *hdev, const struct hid_device_i= d *id) +{ + int ret; + struct kysona_drvdata *drv_data; + struct usb_interface *usbif; + + if (!hid_is_usb(hdev)) + return -EINVAL; + + usbif =3D to_usb_interface(hdev->dev.parent); + + drv_data =3D devm_kzalloc(&hdev->dev, sizeof(*drv_data), GFP_KERNEL); + if (!drv_data) + return -ENOMEM; + + hid_set_drvdata(hdev, drv_data); + drv_data->hdev =3D hdev; + + ret =3D hid_parse(hdev); + if (ret) + return ret; + + ret =3D hid_hw_start(hdev, HID_CONNECT_DEFAULT); + if (ret) + return ret; + + if (usbif->cur_altsetting->desc.bInterfaceNumber =3D=3D 1) { + if (kysona_battery_probe(hdev) < 0) + hid_err(hdev, "Kysona hid battery_probe failed: %d\n", ret); + } + + return 0; +} + +static int kysona_raw_event(struct hid_device *hdev, + struct hid_report *report, u8 *data, int size) +{ + struct kysona_drvdata *drv_data =3D hid_get_drvdata(hdev); + + if (drv_data->battery && size =3D=3D sizeof(kysona_battery_request) && + data[0] =3D=3D 8 && data[1] =3D=3D BATTERY_REPORT_ID) { + drv_data->battery_capacity =3D data[6]; + drv_data->battery_charging =3D data[7]; + drv_data->battery_voltage =3D (data[8] << 8) | data[9]; + } + + return 0; +} + +static const struct hid_device_id kysona_devices[] =3D { + { 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) }, + { } +}; +MODULE_DEVICE_TABLE(hid, kysona_devices); + +static struct hid_driver kysona_driver =3D { + .name =3D "kysona", + .id_table =3D kysona_devices, + .probe =3D kysona_probe, + .raw_event =3D kysona_raw_event +}; +module_hid_driver(kysona_driver); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("HID driver for Kysona devices"); +MODULE_AUTHOR("Lode Willems "); --=20 2.46.1 From nobody Thu Nov 28 01:43:06 2024 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B52C14C588; Sat, 5 Oct 2024 12:03:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728129828; cv=none; b=oomoHCnxFQkSe61JDXW/GOnIF3q09V9PF26YE/IF+BAGCdKnsGZhhw+YEyjEFdQI7nkYJXTJ4DxaJPZsbD5Arptc4CUX7HfGKGAZTW1SvSO29/elCcn+iZkrQIco9BL+6sLhP4O6TecuQb4hfIGD5g83QKmt3avvRQwVZlji528= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728129828; c=relaxed/simple; bh=AoUTk7ZVTlWqnA+1WPmBYFqGmH1zgass1Hv6K/Ac7lo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uqXpuSxBQCResPo2eXuBW6ix0f5SZK3XJzruViWnWBoD2BQOBuc/mfXoSbpq24aDT5MMZqisOz4NBG5WQhMSgkFAUwVpk7RzK8hw4uqGOQ3DHOIBthI8LwAxd4itB0JSv4RgrPg1cNyIJEWZq3cpUvcf/9wndz5tzq07RYwJebM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lodewillems.com; spf=pass smtp.mailfrom=lodewillems.com; dkim=pass (2048-bit key) header.d=lodewillems.com header.i=@lodewillems.com header.b=nmohr6h0; arc=none smtp.client-ip=80.241.56.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lodewillems.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lodewillems.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lodewillems.com header.i=@lodewillems.com header.b="nmohr6h0" Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4XLPFq5s4Wz9tcl; Sat, 5 Oct 2024 14:03:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lodewillems.com; s=MBO0001; t=1728129823; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0ObizGCt7UEBiFDSHwDfQQHRbYHPcrHTfQHYppe1aSA=; b=nmohr6h0gJRq+a4XCiQDYnDLhglwquwT3ojMCtlVIiuQW8mDBXOHpzFSyLvM/RzInvsyIw 9Qzak8cvclvHaROdNy/OSyTj30N2jnkpPPFa091HaxGAIcfn3+4G3iXAQTL7iDrWHKRtWi zEGyj21kKlZTHm6ydSDKtvq2GBjAcWTwA5K4u28E/9tCUvVHuL9HzUwxSkuqCsjf7lFLDG 2Pq//eu2J+9EMOHtibP4NHnFTVLwbwckXOP/KC7WZWGVmjadtbIUG/aHwwpnWC28OyM/Qr KN07abgr45WQFHPMam+0ewYCsfdJ4s/WA3e3p+3xtHJhBhjwK6YnId6ggne6+A== From: Lode Willems To: linux-input@vger.kernel.org Cc: jikos@kernel.org, bentiss@kernel.org, linux-kernel@vger.kernel.org, Lode Willems Subject: [PATCH 3/4] HID: Kysona: check battery status every 5s using a workqueue Date: Sat, 5 Oct 2024 13:57:05 +0200 Message-ID: <20241005120256.13847-4-me@lodewillems.com> In-Reply-To: <20241005120256.13847-1-me@lodewillems.com> References: <20241005120256.13847-1-me@lodewillems.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use a workqueue to periodically check the battery status Signed-off-by: Lode Willems --- 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 @@ =20 #include "hid-ids.h" =20 +#define BATTERY_TIMEOUT_MS 5000 + #define BATTERY_REPORT_ID 4 =20 struct kysona_drvdata { @@ -21,6 +23,7 @@ struct kysona_drvdata { u8 battery_capacity; bool battery_charging; u16 battery_voltage; + struct delayed_work battery_work; }; =20 static enum power_supply_property kysona_battery_props[] =3D { @@ -104,6 +107,17 @@ static void kysona_fetch_battery(struct hid_device *hd= ev) "Battery query failed (err: %d)\n", ret); } =20 +static void kysona_battery_timer_tick(struct work_struct *work) +{ + struct kysona_drvdata *drv_data =3D container_of(work, + struct kysona_drvdata, battery_work.work); + struct hid_device *hdev =3D 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 =3D hid_get_drvdata(hdev); @@ -134,7 +148,11 @@ static int kysona_battery_probe(struct hid_device *hde= v) } =20 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)); =20 return ret; } @@ -188,6 +206,16 @@ static int kysona_raw_event(struct hid_device *hdev, return 0; } =20 +static void kysona_remove(struct hid_device *hdev) +{ + struct kysona_drvdata *drv_data =3D 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[] =3D { { 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 =3D { .name =3D "kysona", .id_table =3D kysona_devices, .probe =3D kysona_probe, - .raw_event =3D kysona_raw_event + .raw_event =3D kysona_raw_event, + .remove =3D kysona_remove }; module_hid_driver(kysona_driver); =20 --=20 2.46.1 From nobody Thu Nov 28 01:43:06 2024 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0F5214C5B0; Sat, 5 Oct 2024 12:03:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728129829; cv=none; b=QccfC9uP6pcGYBtKFrlrAZtnqaW1Dl+JL34Gsht1SNJui9+3bfDK9Z2gao4x7KgTG0plix5zy2n45GJ4hHgx3t24EMCLRNuFg3DbEt/dJbiHZ//IiDLuPMGJyZhuZkM08xOB+dEEE8n5+GwviMQqnZ1xLOp986TdkkRszQk1uJ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728129829; c=relaxed/simple; bh=soBxRlhZVFPtz2pp25QZslJmcMPycIduAafeKmQthB0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tuBIGa0x/UF8Tvbqh4hT6KXvYB5mhe1BDlL0uoM6XMyLmovb2wWvw/Ol6depXPAmKj8ExUDcfR4noV1QUqPit5+7Wej3B/TbyEI1wn0VzYhEeVrEqsUJVGorwqpfDenhJxm0RPdJCM3INE2P9iEq/P+2gz184nF//eQSny9k+EQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lodewillems.com; spf=pass smtp.mailfrom=lodewillems.com; dkim=pass (2048-bit key) header.d=lodewillems.com header.i=@lodewillems.com header.b=nmGMpM6l; arc=none smtp.client-ip=80.241.56.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lodewillems.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lodewillems.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lodewillems.com header.i=@lodewillems.com header.b="nmGMpM6l" Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4XLPFr3hh2z9tdC; Sat, 5 Oct 2024 14:03:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lodewillems.com; s=MBO0001; t=1728129824; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=evj8B3A64fh6mBbSeZ/neCXQL4H1yrwJBIr1g7QFat4=; b=nmGMpM6lBZYp7U75cTd3ZdBJxWVchSKBLdDzvau8MxB5WHCnOgOfuHV7uDgigDmbfBSrvT bFuKFWxeSm8lIt/JgWM8MDr8WI7Mkm6Xnvcv8/0mMK+9+b5Kr102W8zoQkR8wpITrNeHfK v+Dj+bC3lexKqO8b4I6FLm0zZG3oBZu1TOabCQ/DBtUIxQaEhSsRXKLTYsQQUlycYyFad5 jcLXjqpVZL04D8BRNZBFy3GXWCIvamn2LpNV13YnHdydEGhNXSDPoFnT5gsSH0h43PMd02 5YXk+0daufy59S7A8PjitvXJoiBkeGP1h74O7emuTdF455BHXeIPrAw554AgTg== From: Lode Willems To: linux-input@vger.kernel.org Cc: jikos@kernel.org, bentiss@kernel.org, linux-kernel@vger.kernel.org, Lode Willems Subject: [PATCH 4/4] HID: Kysona: add basic online status Date: Sat, 5 Oct 2024 13:57:06 +0200 Message-ID: <20241005120256.13847-5-me@lodewillems.com> In-Reply-To: <20241005120256.13847-1-me@lodewillems.com> References: <20241005120256.13847-1-me@lodewillems.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 4XLPFr3hh2z9tdC Content-Type: text/plain; charset="utf-8" Wait for a response to the battery status request to set the device as online. This prevent wrong power info when the dongle is connected but the mouse is turned off. Signed-off-by: Lode Willems --- drivers/hid/hid-kysona.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/drivers/hid/hid-kysona.c b/drivers/hid/hid-kysona.c index 403bdc4a5e12..d4c0406b3323 100644 --- a/drivers/hid/hid-kysona.c +++ b/drivers/hid/hid-kysona.c @@ -18,6 +18,8 @@ =20 struct kysona_drvdata { struct hid_device *hdev; + bool online; + struct power_supply_desc battery_desc; struct power_supply *battery; u8 battery_capacity; @@ -32,7 +34,8 @@ static enum power_supply_property kysona_battery_props[] = =3D { POWER_SUPPLY_PROP_CAPACITY, POWER_SUPPLY_PROP_SCOPE, POWER_SUPPLY_PROP_MODEL_NAME, - POWER_SUPPLY_PROP_VOLTAGE_NOW + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_ONLINE }; =20 static int kysona_battery_get_property(struct power_supply *psy, @@ -46,11 +49,16 @@ static int kysona_battery_get_property(struct power_sup= ply *psy, case POWER_SUPPLY_PROP_PRESENT: val->intval =3D 1; break; + case POWER_SUPPLY_PROP_ONLINE: + val->intval =3D drv_data->online; + break; case POWER_SUPPLY_PROP_STATUS: - // TODO: check if device is online - val->intval =3D drv_data->battery_charging ? - POWER_SUPPLY_STATUS_CHARGING : - POWER_SUPPLY_STATUS_DISCHARGING; + if (drv_data->online) + val->intval =3D drv_data->battery_charging ? + POWER_SUPPLY_STATUS_CHARGING : + POWER_SUPPLY_STATUS_DISCHARGING; + else + val->intval =3D POWER_SUPPLY_STATUS_UNKNOWN; break; case POWER_SUPPLY_PROP_SCOPE: val->intval =3D POWER_SUPPLY_SCOPE_DEVICE; @@ -124,7 +132,9 @@ static int kysona_battery_probe(struct hid_device *hdev) struct power_supply_config pscfg =3D { .drv_data =3D drv_data }; int ret =3D 0; =20 + drv_data->online =3D false; drv_data->battery_capacity =3D 100; + drv_data->battery_voltage =3D 4200; =20 drv_data->battery_desc.properties =3D kysona_battery_props; drv_data->battery_desc.num_properties =3D ARRAY_SIZE(kysona_battery_props= ); @@ -201,6 +211,7 @@ static int kysona_raw_event(struct hid_device *hdev, drv_data->battery_capacity =3D data[6]; drv_data->battery_charging =3D data[7]; drv_data->battery_voltage =3D (data[8] << 8) | data[9]; + drv_data->online =3D true; } =20 return 0; --=20 2.46.1