Skip to contentSkip to conversation

platform/x86: Add new msi-ec driver

PATCH Superseded Nikita Kravets 3 participants 8 messages Mar 11, 2023 – Mar 20, 2023

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Nikita Kravets

author maintainer
#0 lore raw

PATCH v2 1/1 platform/x86: Add new msi-ec driver

Add a new driver to allow various MSI laptops' functionalities to be controlled from userspace. This includes such features as power profiles (aka shift modes), fan speed, charge thresholds, LEDs, etc.

This driver contains EC memory configurations for different firmware versions and exports battery charge thresholds to userspace (note, that start and end thresholds control the same EC parameter and are always 10% apart).

Discussion: https://github.com/BeardOverflow/msi-ec/pull/13

Cc Aakash Singh <[email protected]>
Cc Jose Angel Pastrana <[email protected]>

---
Changes in v2:
- fix checkpatch issues:
- update MAINTAINERS
- fix SPDX license identifier
- use __initconst for const init definitions
- minor formatting
- omit commas after sentinel entries
- use pr_fmt macro to automatically prefix messages
- remove a redundant ACPI check
- in ec_read_seq(): define the counter in the for loop header, make i and len the same type
- better error handling in msi_battery_add()
- use sysfs_emit() instead of sprintf()
- catch up with the main driver repo:
- add new configurations
- add compatible EC FW versions to existing configurations
- allow fan_mode parameter to have a custom set of modes
- add MSI_EC_ADDR_UNKNOWN and MSI_EC_ADDR_UNSUPP = 0xff01 to specify unknown addresses and unsupported parameters. When converted to u8, they evaluate to 0x01, which is a readonly address in MSI EC, so in case of bugs writing to them should be safe
- use sentinel entries for modes arrays

 MAINTAINERS                   |   7 +
 drivers/platform/x86/Kconfig  |   7 +
 drivers/platform/x86/Makefile |   1 +
 drivers/platform/x86/msi-ec.c | 878 ++++++++++++++++++++++++++++++++++
 drivers/platform/x86/msi-ec.h | 122 +++++
 5 files changed, 1015 insertions(+)
 create mode 100644 drivers/platform/x86/msi-ec.c
 create mode 100644 drivers/platform/x86/msi-ec.h
MAINTAINERS +7 -0
index fb1471cb5ed3..651578a14360 100644
@@ -14191,6 +14191,13 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/net/ieee802154/mrf24j40.txt
 F:	drivers/net/ieee802154/mrf24j40.c
 
+MSI EC DRIVER
+M:	Nikita Kravets <[email protected]>
+L:	[email protected]
+S:	Maintained
+W:	https://github.com/BeardOverflow/msi-ec
+F:	drivers/platform/x86/msi-ec.*
+
 MSI LAPTOP SUPPORT
 M:	"Lee, Chun-Yi" <[email protected]>
 L:	[email protected]
drivers/platform/x86/Kconfig +7 -0
index 5692385e2d26..4534d11f9ca5 100644
@@ -644,6 +644,13 @@ config THINKPAD_LMI
 
 source "drivers/platform/x86/intel/Kconfig"
 
+config MSI_EC
+	tristate "MSI EC Extras"
+	depends on ACPI
+	help
+	  This driver allows various MSI laptops' functionalities to be
+	  controlled from userspace, including battery charge threshold.
+
 config MSI_LAPTOP
 	tristate "MSI Laptop Extras"
 	depends on ACPI
drivers/platform/x86/Makefile +1 -0
index 1d3d1b02541b..7cc2beca8208 100644
@@ -71,6 +71,7 @@ obj-$(CONFIG_THINKPAD_LMI)	+= think-lmi.o
 obj-y				+= intel/
 
 # MSI
+obj-$(CONFIG_MSI_EC)		+= msi-ec.o
 obj-$(CONFIG_MSI_LAPTOP)	+= msi-laptop.o
 obj-$(CONFIG_MSI_WMI)		+= msi-wmi.o
 
drivers/platform/x86/msi-ec.c new file +878 -0

878 lines not on this page · raw message

drivers/platform/x86/msi-ec.h new file +122 -0

122 lines not on this page · raw message

Signature · 1 line
2.39.2
#1 lore raw

Hi

2023. március 11., szombat 15:40 keltezéssel, Nikita Kravets <[email protected]> írta:

Quoted 7 lines

[...]

drivers/platform/x86/msi-ec.c new file +6 -0
+static ssize_t
+charge_control_start_threshold_show(struct device *device,
+				    struct device_attribute *attr, char *buf)
+{
+	return charge_control_threshold_show(conf.charge_control.offset_start,
+					     device, attr, buf);

drivers/platform/x86/msi-ec.c line 772

Quoted 10 lines
drivers/platform/x86/msi-ec.c new file +10 -0
+}
+
+static ssize_t
+charge_control_start_threshold_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	return charge_control_threshold_store(conf.charge_control.offset_start,
+					      dev, attr, buf, count);
+}

The above two functions are inconsistent with the rest of the file because they have the return type in a separate line.

Quoted 40 lines
drivers/platform/x86/msi-ec.c new file +40 -0
+
+static ssize_t charge_control_end_threshold_show(struct device *device,
+						 struct device_attribute *attr,
+						 char *buf)
+{
+	return charge_control_threshold_show(conf.charge_control.offset_end,
+					     device, attr, buf);
+}
+
+static ssize_t charge_control_end_threshold_store(struct device *dev,
+						  struct device_attribute *attr,
+						  const char *buf, size_t count)
+{
+	return charge_control_threshold_store(conf.charge_control.offset_end,
+					      dev, attr, buf, count);
+}
+
+static DEVICE_ATTR_RW(charge_control_start_threshold);
+static DEVICE_ATTR_RW(charge_control_end_threshold);
+
+static struct attribute *msi_battery_attrs[] = {
+	&dev_attr_charge_control_start_threshold.attr,
+	&dev_attr_charge_control_end_threshold.attr,
+	NULL
+};
+
+ATTRIBUTE_GROUPS(msi_battery);
+
+static int msi_battery_add(struct power_supply *battery,
+			   struct acpi_battery_hook *hook)
+{
+	return device_add_groups(&battery->dev, msi_battery_groups);
+}
+
+static int msi_battery_remove(struct power_supply *battery,
+			      struct acpi_battery_hook *hook)
+{
+	device_remove_groups(&battery->dev, msi_battery_groups);
+	return 0;
+}

drivers/platform/x86/msi-ec.c line 822

Quoted 10 lines
drivers/platform/x86/msi-ec.c new file +10 -0
+
+static struct acpi_battery_hook battery_hook = {
+	.add_battery = msi_battery_add,
+	.remove_battery = msi_battery_remove,
+	.name = MSI_EC_DRIVER_NAME,
+};
+
+// ============================================================ //
+// Module load/unload
+// ============================================================ //

It's a small thing, but usually /* ... */ comments are preferred. Hans will tell you if you need to change it.

Quoted 6 lines
drivers/platform/x86/msi-ec.c new file +6 -0
+
+static int __init load_configuration(void)
+{
+	int result;
+
+	u8 fw_version[MSI_EC_FW_VERSION_LENGTH + 1];

drivers/platform/x86/msi-ec.c line 838

Quoted 10 lines
drivers/platform/x86/msi-ec.c new file +10 -0
+
+	// get firmware version
+	result = ec_get_firmware_version(fw_version);
+	if (result < 0)
+		return result;
+
+	// load the suitable configuration, if exists
+	for (int i = 0; CONFIGURATIONS[i]; i++) {
+		for (int j = 0; CONFIGURATIONS[i]->allowed_fw[j]; j++) {
+			if (strcmp(CONFIGURATIONS[i]->allowed_fw[j], fw_version) == 0) {

Previously you said match_string() works here. Has something changed?

drivers/platform/x86/msi-ec.c line 839

Quoted 1 line
drivers/platform/x86/msi-ec.c new file +1 -0
+				memcpy(&conf, CONFIGURATIONS[i], sizeof(struct msi_ec_conf));

Why not simply

  conf = *CONFIGURATIONS[i];

?

drivers/platform/x86/msi-ec.c line 848

Quoted 10 lines
drivers/platform/x86/msi-ec.c new file +9 -0
+				conf.allowed_fw = NULL;
+				return 0;
+			}
+		}
+	}
+
+	pr_err("Your firmware version is not supported!\n");
+	return -EOPNOTSUPP;
+}
[...]

Regards,
Barnabás Pőcze

author maintainer
#2 lore raw

Hi,

Quoted 2 lines

The above two functions are inconsistent with the rest of the file because they have the return type in a separate line.

I believe it was originally made to not exceed the limit of 80 columns

Quoted 2 lines

It's a small thing, but usually /* ... */ comments are preferred. Hans will tell you if you need to change it.

Okay

Quoted 1 line

Previously you said match_string() works here. Has something changed?

No, I implemented it in the main repo but forgot to do it in the kernel patch.. I'll do it in the v3 after we're finished reviewing v2

Quoted 3 lines

Why not simply

  conf = *CONFIGURATIONS[i];

Noted

Regards,
Nikita

#3 lore raw

Hi,

Quoted 11 lines · Nikita Kravets

Hi,

Quoted 2 lines

The above two functions are inconsistent with the rest of the file because they have the return type in a separate line.

I believe it was originally made to not exceed the limit of 80 columns

Quoted 2 lines

It's a small thing, but usually /* ... */ comments are preferred. Hans will tell you if you need to change it.

Okay

Yes I noticed these too during my review, but I'm fine with them as long as a single driver uses a single comment style consistently.

Note most drivers/platform/x86 drivers do indeed use /* ... */, so I would not mind if you convert the comments, but this is not a blocker for merging the driver.

Quoted 4 lines
Quoted 1 line

Previously you said match_string() works here. Has something changed?

No, I implemented it in the main repo but forgot to do it in the kernel patch.. I'll do it in the v3 after we're finished reviewing v2

Thanks, sounds good.

Only remark which I have is that the driver still has no modaliases so if this gets enabled as a module by distros, which is what most distros will do it will never load.

I think you should add a dmi_system_id table together with MODULE_DEVICE_TABLE() e.g. something like this:

static const struct dmi_system_id msi_dmi_table[] __initconst = {
	{
		matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "MICRO-STAR INT"),
		}
	},
	{
		matches = {
			DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International"),
		}
	},
	{}
};

MODULE_DEVICE_TABLE(dmi, msi_dmi_table);

To make the module auto-load. But I guess it might be better to put that on the TODO list somewhere and do it once the module has had some more testing.

E.g. I should really test this on my desktop's MSI B550M PRO-VDH board and see what it does there (hopefully nothing).

Regards,

Hans

author maintainer
#4 lore raw

Hi,

Quoted 1 line

Note most drivers/platform/x86 drivers do indeed use /* ... */, so I would not mind if you convert the comments

Okay, I'll do it then

Quoted 3 lines

But I guess it might be better to put that on the TODO list somewhere and do it once the module has had some more testing.

E.g. I should really test this on my desktop's MSI B550M PRO-VDH board and see what it does there (hopefully nothing).

By design it should fail to load on your motherboard since ec_read() will return with
ENODEV if EC isn't present, or read an incompatible version if it is. Some testing would be good, so we can add a DMI table in the next set of patches.

I have a question about naming. We have the fn_super_swap configuration parameter,
but I noticed that in the kernel code the Super key is often called Meta. Should I also rename it to fn_meta_swap or stick with 'Super'?

Regards,
Nikita

#5 lore raw

Hi,

Quoted 5 lines · Nikita Kravets

Hi,

Quoted 1 line

Note most drivers/platform/x86 drivers do indeed use /* ... */, so I would not mind if you convert the comments

Okay, I'll do it then

Thank you.

Quoted 9 lines
Quoted 3 lines

But I guess it might be better to put that on the TODO list somewhere and do it once the module has had some more testing.

E.g. I should really test this on my desktop's MSI B550M PRO-VDH board and see what it does there (hopefully nothing).

By design it should fail to load on your motherboard since ec_read() will return with
ENODEV if EC isn't present, or read an incompatible version if it is. Some testing would be good, so we can add a DMI table in the next set of patches.

Ok I have tested this on the 2 MSI devices I have access to:

MSI B550M PRO-VDH desktop motherboard:
-Does not have an ACPI EC device, so the first ec_read()
fails with -ENODEV

MSI S270 (ancient) laptop
-To my surprise this one does have an ACPI EC device,
load_configuration() fails with:
"msi_ec: Your firmware version is not supported!"
Note:
1) It would be good to log the not supported version.
2) Since hitting this is expected on unsupported models this should IMHO be changed from a pr_err to a pr_warn
3) This laptop's EC is currently supported by the old msi-laptop kernel driver and this driver is necessary for backlight control at least.

Quoted 4 lines

I have a question about naming. We have the fn_super_swap configuration parameter,
but I noticed that in the kernel code the Super key is often called Meta. Should I also rename it to fn_meta_swap or stick with 'Super'?

Either way is fine meta/super are both used in various places of the stack. Probably best to keep it as is to keep compatibility for existing msi-ec users.

Regards,

Hans

author maintainer
#6 lore raw

Hi,

v3 is ready, but now that you've tested the driver on your devices, should I add modaliases before sending it?

Regards,
Nikita

#7 lore raw

Hi,

Quoted 4 lines · Nikita Kravets

Hi,

v3 is ready, but now that you've tested the driver on your devices, should I add modaliases before sending it?

Yes please.

Regards,

Hans