Skip to contentSkip to conversation

platform/x86: firmware_attributes_class: Create helper file for handling firmware-attributes class registration events

PATCH In review Mark Pearson 3 participants 7 messages May 30, 2021 – Jun 9, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 platform/x86: firmware_attributes_class: Create helper file for handling firmware-attributes class registration events

This will be used by the Dell and Lenovo WMI management drivers to prevent both drivers being active.

---
Changes in v2:
- This is a new file requested as part of the review of the proposed
think_lmi.c driver. Labeling as V2 to keep series consistent

Changes in v3:
- Set default in Kconfig, and removed help text
- Allow multiple modules to register with module. Change API names to better reflect this.

Changes in v4:
- version bump for consistency in series

Changes in v5:
- Fix issue reported by kernel test robot. Add header file to includes

 drivers/platform/x86/Kconfig                  |  4 ++
 drivers/platform/x86/Makefile                 |  1 +
 .../platform/x86/firmware_attributes_class.c  | 54 +++++++++++++++++++
 .../platform/x86/firmware_attributes_class.h  | 13 +++++
 4 files changed, 72 insertions(+)
 create mode 100644 drivers/platform/x86/firmware_attributes_class.c
 create mode 100644 drivers/platform/x86/firmware_attributes_class.h
drivers/platform/x86/Kconfig +4 -0
index 2714f7c38..57da8352d 100644
@@ -1076,6 +1076,10 @@ config TOUCHSCREEN_DMI
 	  the OS-image for the device. This option supplies the missing info.
 	  Enable this for x86 tablets with Silead or Chipone touchscreens.
 
+config FW_ATTR_CLASS
+	tristate "Firmware attributes class helper module"
+	default n
+
 config INTEL_IMR
 	bool "Intel Isolated Memory Region support"
 	depends on X86_INTEL_QUARK && IOSF_MBI
drivers/platform/x86/Makefile +1 -0
index dcc8cdb95..147573f69 100644
@@ -115,6 +115,7 @@ obj-$(CONFIG_TOPSTAR_LAPTOP)	+= topstar-laptop.o
 obj-$(CONFIG_I2C_MULTI_INSTANTIATE)	+= i2c-multi-instantiate.o
 obj-$(CONFIG_MLX_PLATFORM)		+= mlx-platform.o
 obj-$(CONFIG_TOUCHSCREEN_DMI)		+= touchscreen_dmi.o
+obj-$(CONFIG_FW_ATTR_CLASS)             += firmware_attributes_class.o
 
 # Intel uncore drivers
 obj-$(CONFIG_INTEL_IPS)				+= intel_ips.o
drivers/platform/x86/firmware_attributes_class.c new file +54 -0

54 lines not on this page · raw message

drivers/platform/x86/firmware_attributes_class.h new file +13 -0
new file mode 100644
index 000000000..802f12b45
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/* Firmware attributes class helper module */
+
+#ifndef FW_ATTR_CLASS_H
+#define FW_ATTR_CLASS_H
+
+int fw_attributes_class_get(struct class **fw_attr_class);
+int fw_attributes_class_put(void);
+
+#endif /* FW_ATTR_CLASS_H */
+
+
Signature · 1 line
2.31.1
#1 lore raw

PATCH v5 2/3 platform/x86: dell-wmi-sysman: Use firmware_attributes_class helper

Update Dell WMI sysman driver to use newly implemented helper module.

---
Changes in v2:
- firmare_attributes_class.c added as reqested during review of the proposed think_lmi.c driver. Labeling as V2 to keep series consistent
- Note, compile tested only due to lack of HW to test on. Matching changes were made in the think-lmi.c driver and worked. Please let me know if any problems!

Changes in v3:
- Updated to use new firmware attribute class API names

Changes in v4, v5:
- version bump for consistency in series

 drivers/platform/x86/dell/Kconfig              |  1 +
 .../platform/x86/dell/dell-wmi-sysman/sysman.c | 18 ++++++++----------
 2 files changed, 9 insertions(+), 10 deletions(-)
drivers/platform/x86/dell/Kconfig +1 -0
index e0a55337f..931f616ff 100644
@@ -197,6 +197,7 @@ config DELL_WMI_SYSMAN
 	depends on ACPI_WMI
 	depends on DMI
 	select NLS
+	select FW_ATTR_CLASS
 	help
 	  This driver allows changing BIOS settings on many Dell machines from
 	  2018 and newer without the use of any additional software.
drivers/platform/x86/dell/dell-wmi-sysman/sysman.c +8 -10
index c8d276d78..06b3fe7da 100644
@@ -13,14 +13,11 @@
 #include <linux/kernel.h>
 #include <linux/wmi.h>
 #include "dell-wmi-sysman.h"
+#include "../../firmware_attributes_class.h"
 
 #define MAX_TYPES  4
 #include <linux/nls.h>
 
-static struct class firmware_attributes_class = {
-	.name = "firmware-attributes",
-};
-
 struct wmi_sysman_priv wmi_priv = {
 	.mutex = __MUTEX_INITIALIZER(wmi_priv.mutex),
 };
@@ -28,6 +25,7 @@ struct wmi_sysman_priv wmi_priv = {
 /* reset bios to defaults */
 static const char * const reset_types[] = {"builtinsafe", "lastknowngood", "factory", "custom"};
 static int reset_option = -1;
+struct class *fw_attr_class;
 
 
 /**
@@ -541,11 +539,11 @@ static int __init sysman_init(void)
 		goto err_exit_bios_attr_pass_interface;
 	}
 
-	ret = class_register(&firmware_attributes_class);
+	ret = fw_attributes_class_get(&fw_attr_class);
 	if (ret)
 		goto err_exit_bios_attr_pass_interface;
 
-	wmi_priv.class_dev = device_create(&firmware_attributes_class, NULL, MKDEV(0, 0),
+	wmi_priv.class_dev = device_create(fw_attr_class, NULL, MKDEV(0, 0),
 				  NULL, "%s", DRIVER_NAME);
 	if (IS_ERR(wmi_priv.class_dev)) {
 		ret = PTR_ERR(wmi_priv.class_dev);
@@ -602,10 +600,10 @@ static int __init sysman_init(void)
 	release_attributes_data();
 
 err_destroy_classdev:
-	device_destroy(&firmware_attributes_class, MKDEV(0, 0));
+	device_destroy(fw_attr_class, MKDEV(0, 0));
 
 err_unregister_class:
-	class_unregister(&firmware_attributes_class);
+	fw_attributes_class_put();
 
 err_exit_bios_attr_pass_interface:
 	exit_bios_attr_pass_interface();
@@ -619,8 +617,8 @@ static int __init sysman_init(void)
 static void __exit sysman_exit(void)
 {
 	release_attributes_data();
-	device_destroy(&firmware_attributes_class, MKDEV(0, 0));
-	class_unregister(&firmware_attributes_class);
+	device_destroy(fw_attr_class, MKDEV(0, 0));
+	fw_attributes_class_put();
 	exit_bios_attr_set_interface();
 	exit_bios_attr_pass_interface();
 }
Signature · 1 line
2.31.1
#2 lore raw

PATCH v5 3/3 platform/x86: think-lmi: Add WMI interface support on Lenovo platforms

In reply to Mark Pearson

For Lenovo platforms that support a WMI interface to the BIOS add support, using the firmware-attributes class, to allow users to access and modify various BIOS related settings.

---
Changes in V2:
- Improved error handling
- updated to use kobj's better (thanks Hans for pointers). Makes code simpler and cleaner
- Removed some dead code from previous implementation that I'd missed
- Moved think-lmi.h into platform/x86 directory
- Use firmware_attributes_class helper
- Corrected some bad indexing when storing an attribute
- improved encoding options to limit selection
- Added details on encoding and kbdlang to sysfs-class-firmware-attributes documentation

Changes in v3:
- Clean up is_enabled display
- Use new firmware-attributes class API names
- Correct release of kobj objects
- Change pwd_type to const char
- Add role field to pwd_settings
- Improved checking when setting new password

Changes in v4:
- Updated documentation with password update specifics
- Implement tlmi_errs to make error string scanning cleaner
- Clean up pointer check handling
- Clean up pointer free handling
- Clean up pwd string length handling as recommended
- Use kasprintf where appropriate
- use strreplace where appropriate
- Cosmetic cleanups as recommended

Changes in v5:
- Pick up corrections from Hans review
- Add details to MAINTAINERS file
- Clean up code based on review feedback
- Use strscpy instead of memcpy and sprintf
- Use strchrnul instead of strreplace

 .../testing/sysfs-class-firmware-attributes   |  18 +-
 MAINTAINERS                                   |   7 +
 drivers/platform/x86/Kconfig                  |  13 +-
 drivers/platform/x86/Makefile                 |   3 +-
 drivers/platform/x86/think-lmi.c              | 891 ++++++++++++++++++
 drivers/platform/x86/think-lmi.h              |  81 ++
 6 files changed, 1010 insertions(+), 3 deletions(-)
 create mode 100644 drivers/platform/x86/think-lmi.c
 create mode 100644 drivers/platform/x86/think-lmi.h
Documentation/ABI/testing/sysfs-class-firmware-attributes +17 -1
index 8ea59fea4..3348bf80a 100644
@@ -197,8 +197,24 @@ Description:
 		Drivers may emit a CHANGE uevent when a password is set or unset
 		userspace may check it again.
 
-		On Dell systems, if Admin password is set, then all BIOS attributes
+		On Dell and Lenovo systems, if Admin password is set, then all BIOS attributes
 		require password validation.
+		On Lenovo systems if you change the Admin password the new password is not active until
+		the next boot.
+
+		Lenovo specific class extensions
+		------------------------------
+
+		On Lenovo systems the following additional settings are available:
+
+		lenovo_encoding:
+					The encoding method that is used. This can be either "ascii"
+					or "scancode". Default is set to "ascii"
+
+		lenovo_kbdlang:
+					The keyboard language method that is used. This is generally a
+					two char code (e.g. "us", "fr", "gr") and may vary per platform.
+					Default is set to "us"
 
 What:		/sys/class/firmware-attributes/*/attributes/pending_reboot
 Date:		February 2021
MAINTAINERS +7 -0
index e845f052a..82854dbff 100644
@@ -18160,6 +18160,13 @@ W:	http://thinkwiki.org/wiki/Ibm-acpi
 T:	git git://repo.or.cz/linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git
 F:	drivers/platform/x86/thinkpad_acpi.c
 
+THINKPAD LMI DRIVER
+M:	Mark Pearson <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/ABI/testing/sysfs-class-firmware-attributes
+F:	drivers/platform/x86/think-lmi.c
+
 THUNDERBOLT DMA TRAFFIC TEST DRIVER
 M:	Isaac Hazan <[email protected]>
 L:	[email protected]
drivers/platform/x86/Kconfig +12 -1
index 57da8352d..09d2f54e2 100644
@@ -639,6 +639,17 @@ config THINKPAD_ACPI_HOTKEY_POLL
 	  If you are not sure, say Y here.  The driver enables polling only if
 	  it is strictly necessary to do so.
 
+config THINKPAD_LMI
+	tristate "Lenovo WMI-based systems management driver"
+	depends on ACPI_WMI
+	select FW_ATTR_CLASS
+	help
+	  This driver allows changing BIOS settings on Lenovo machines whose
+	  BIOS support the WMI interface.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called think-lmi.
+
 config INTEL_ATOMISP2_LED
 	tristate "Intel AtomISP2 camera LED driver"
 	depends on GPIOLIB && LEDS_GPIO
@@ -1077,7 +1088,7 @@ config TOUCHSCREEN_DMI
 	  Enable this for x86 tablets with Silead or Chipone touchscreens.
 
 config FW_ATTR_CLASS
-	tristate "Firmware attributes class helper module"
+	tristate
 	default n
 
 config INTEL_IMR
drivers/platform/x86/Makefile +2 -1
index 147573f69..78997301b 100644
@@ -64,6 +64,7 @@ obj-$(CONFIG_IBM_RTL)		+= ibm_rtl.o
 obj-$(CONFIG_IDEAPAD_LAPTOP)	+= ideapad-laptop.o
 obj-$(CONFIG_SENSORS_HDAPS)	+= hdaps.o
 obj-$(CONFIG_THINKPAD_ACPI)	+= thinkpad_acpi.o
+obj-$(CONFIG_THINKPAD_LMI)	+= think-lmi.o
 
 # Intel
 obj-$(CONFIG_INTEL_ATOMISP2_LED)	+= intel_atomisp2_led.o
@@ -112,10 +113,10 @@ obj-$(CONFIG_SYSTEM76_ACPI)	+= system76_acpi.o
 obj-$(CONFIG_TOPSTAR_LAPTOP)	+= topstar-laptop.o
 
 # Platform drivers
+obj-$(CONFIG_FW_ATTR_CLASS)		+= firmware_attributes_class.o
 obj-$(CONFIG_I2C_MULTI_INSTANTIATE)	+= i2c-multi-instantiate.o
 obj-$(CONFIG_MLX_PLATFORM)		+= mlx-platform.o
 obj-$(CONFIG_TOUCHSCREEN_DMI)		+= touchscreen_dmi.o
-obj-$(CONFIG_FW_ATTR_CLASS)             += firmware_attributes_class.o
 
 # Intel uncore drivers
 obj-$(CONFIG_INTEL_IPS)				+= intel_ips.o
drivers/platform/x86/think-lmi.c new file +891 -0

891 lines not on this page · raw message

drivers/platform/x86/think-lmi.h new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.31.1
#3 lore raw
In reply to Mark Pearson

Hi,

Quoted 15 lines · Mark Pearson

This will be used by the Dell and Lenovo WMI management drivers to prevent both drivers being active.

---
Changes in v2:
- This is a new file requested as part of the review of the proposed
think_lmi.c driver. Labeling as V2 to keep series consistent

Changes in v3:
- Set default in Kconfig, and removed help text
- Allow multiple modules to register with module. Change API names to better reflect this.

Quoted 5 lines

Changes in v4:
- version bump for consistency in series

Changes in v5:
- Fix issue reported by kernel test robot. Add header file to includes

Thanks Mark,

Unfortunately you squashed the Kconfig and Makefile changes which I made to v4 when fixing it up during merging into 3/3 instead of having them in v5 of this patch.

No worries, since this was the only problem which I could see I've fixed this up in my review-hans branch while merging v5 of this series there (replacing v4).

I did notice a bit of dead code while reviewing the changes which you made to 3/3 in response to Andy's review. I'll send a follow-up patch fixing that.

I'll leave this sit in my review-hans branch for a bit to give Andy a chance to give his Reviewed-by and then I'll push this to for-next.

Regards,

Hans

Quoted 116 lines
 drivers/platform/x86/Kconfig                  |  4 ++
 drivers/platform/x86/Makefile                 |  1 +
 .../platform/x86/firmware_attributes_class.c  | 54 +++++++++++++++++++
 .../platform/x86/firmware_attributes_class.h  | 13 +++++
 4 files changed, 72 insertions(+)
 create mode 100644 drivers/platform/x86/firmware_attributes_class.c
 create mode 100644 drivers/platform/x86/firmware_attributes_class.h
drivers/platform/x86/Kconfig +4 -0
index 2714f7c38..57da8352d 100644
@@ -1076,6 +1076,10 @@ config TOUCHSCREEN_DMI
 	  the OS-image for the device. This option supplies the missing info.
 	  Enable this for x86 tablets with Silead or Chipone touchscreens.
 
+config FW_ATTR_CLASS
+	tristate "Firmware attributes class helper module"
+	default n
+
 config INTEL_IMR
 	bool "Intel Isolated Memory Region support"
 	depends on X86_INTEL_QUARK && IOSF_MBI
drivers/platform/x86/Makefile +1 -0
index dcc8cdb95..147573f69 100644
@@ -115,6 +115,7 @@ obj-$(CONFIG_TOPSTAR_LAPTOP)	+= topstar-laptop.o
 obj-$(CONFIG_I2C_MULTI_INSTANTIATE)	+= i2c-multi-instantiate.o
 obj-$(CONFIG_MLX_PLATFORM)		+= mlx-platform.o
 obj-$(CONFIG_TOUCHSCREEN_DMI)		+= touchscreen_dmi.o
+obj-$(CONFIG_FW_ATTR_CLASS)             += firmware_attributes_class.o
 
 # Intel uncore drivers
 obj-$(CONFIG_INTEL_IPS)				+= intel_ips.o
drivers/platform/x86/firmware_attributes_class.c new file +54 -0
new file mode 100644
index 000000000..b407880f0
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/* Firmware attributes class helper module */
+
+#include <linux/mutex.h>
+#include <linux/device/class.h>
+#include <linux/module.h>
+#include "firmware_attributes_class.h"
+
+static DEFINE_MUTEX(fw_attr_lock);
+int fw_attr_inuse;
+
+static struct class firmware_attributes_class = {
+	.name = "firmware-attributes",
+};
+
+int fw_attributes_class_get(struct class **fw_attr_class)
+{
+	int err;
+
+	mutex_lock(&fw_attr_lock);
+	if (!fw_attr_inuse) { /*first time class is being used*/
+		err = class_register(&firmware_attributes_class);
+		if (err) {
+			mutex_unlock(&fw_attr_lock);
+			return err;
+		}
+	}
+	fw_attr_inuse++;
+	*fw_attr_class = &firmware_attributes_class;
+	mutex_unlock(&fw_attr_lock);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(fw_attributes_class_get);
+
+int fw_attributes_class_put(void)
+{
+	mutex_lock(&fw_attr_lock);
+	if (!fw_attr_inuse) {
+		mutex_unlock(&fw_attr_lock);
+		return -EINVAL;
+	}
+	fw_attr_inuse--;
+	if (!fw_attr_inuse) /* No more consumers */
+		class_unregister(&firmware_attributes_class);
+	mutex_unlock(&fw_attr_lock);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(fw_attributes_class_put);
+
+MODULE_AUTHOR("Mark Pearson <[email protected]>");
+MODULE_LICENSE("GPL");
+
+
drivers/platform/x86/firmware_attributes_class.h new file +13 -0
new file mode 100644
index 000000000..802f12b45
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/* Firmware attributes class helper module */
+
+#ifndef FW_ATTR_CLASS_H
+#define FW_ATTR_CLASS_H
+
+int fw_attributes_class_get(struct class **fw_attr_class);
+int fw_attributes_class_put(void);
+
+#endif /* FW_ATTR_CLASS_H */
+
+
#4 lore raw
Quoted 21 lines · Hans de Goede

Hi,

Quoted 18 lines · Mark Pearson

This will be used by the Dell and Lenovo WMI management drivers to prevent both drivers being active.

---
Changes in v2:
- This is a new file requested as part of the review of the proposed
think_lmi.c driver. Labeling as V2 to keep series consistent

Changes in v3:
- Set default in Kconfig, and removed help text
- Allow multiple modules to register with module. Change API names to better reflect this.

Changes in v4:
- version bump for consistency in series

Quoted 8 lines
Quoted 2 lines

Changes in v5:
- Fix issue reported by kernel test robot. Add header file to includes

Thanks Mark,

Unfortunately you squashed the Kconfig and Makefile changes which I made to v4 when fixing it up during merging into 3/3 instead of having them in v5 of this patch.

Oh - apologies; I tried to be careful and make sure I picked up the fixes you'd made as well. I must have missed them here somehow :(

Quoted 8 lines

No worries, since this was the only problem which I could see I've fixed this up in my review-hans branch while merging v5 of this series there (replacing v4).

I did notice a bit of dead code while reviewing the changes which you made to 3/3 in response to Andy's review. I'll send a follow-up patch fixing that.

Sounds good. I'm intrigued what I've missed.

Quoted 3 lines

I'll leave this sit in my review-hans branch for a bit to give Andy a chance to give his Reviewed-by and then I'll push this to for-next.

Sounds good. Thank you!

Mark

#5 lore raw
In reply to Hans de Goede
Quoted 38 lines · Hans de Goede

Hi,

Quoted 15 lines · Mark Pearson

This will be used by the Dell and Lenovo WMI management drivers to prevent both drivers being active.

---
Changes in v2:
- This is a new file requested as part of the review of the proposed
think_lmi.c driver. Labeling as V2 to keep series consistent

Changes in v3:
- Set default in Kconfig, and removed help text
- Allow multiple modules to register with module. Change API names to better reflect this.

Quoted 5 lines

Changes in v4:
- version bump for consistency in series

Changes in v5:
- Fix issue reported by kernel test robot. Add header file to includes

Thanks Mark,

Unfortunately you squashed the Kconfig and Makefile changes which I made to v4 when fixing it up during merging into 3/3 instead of having them in v5 of this patch.

No worries, since this was the only problem which I could see I've fixed this up in my review-hans branch while merging v5 of this series there (replacing v4).

I did notice a bit of dead code while reviewing the changes which you made to 3/3 in response to Andy's review. I'll send a follow-up patch fixing that.

Quoted 6 lines

I'll leave this sit in my review-hans branch for a bit to give Andy a chance to give his Reviewed-by and then I'll push this to for-next.

Regards,

Hans

It looks like this series causes allyesconfig to break on linux-next:

https://github.com/ClangBuiltLinux/continuous-integration2/runs/2773158286?check_suite_focus=true

$ make -skj"$(nproc)" allyesconfig all
ld: drivers/platform/x86/think-lmi.o:(.bss+0x0): multiple definition of `fw_attr_class'; drivers/platform/x86/dell/dell-wmi-sysman/sysman.o:(.bss+0x0): first defined here

Cheers,
Nathan

#6 lore raw

Hi,

Quoted 44 lines · Nathan Chancellor
Quoted 43 lines · Hans de Goede

Hi,

Quoted 15 lines · Mark Pearson

This will be used by the Dell and Lenovo WMI management drivers to prevent both drivers being active.

---
Changes in v2:
- This is a new file requested as part of the review of the proposed
think_lmi.c driver. Labeling as V2 to keep series consistent

Changes in v3:
- Set default in Kconfig, and removed help text
- Allow multiple modules to register with module. Change API names to better reflect this.

Quoted 5 lines

Changes in v4:
- version bump for consistency in series

Changes in v5:
- Fix issue reported by kernel test robot. Add header file to includes

Thanks Mark,

Unfortunately you squashed the Kconfig and Makefile changes which I made to v4 when fixing it up during merging into 3/3 instead of having them in v5 of this patch.

No worries, since this was the only problem which I could see I've fixed this up in my review-hans branch while merging v5 of this series there (replacing v4).

I did notice a bit of dead code while reviewing the changes which you made to 3/3 in response to Andy's review. I'll send a follow-up patch fixing that.

I'll leave this sit in my review-hans branch for a bit to give Andy a chance to give his Reviewed-by and then I'll push this to for-next.

Regards,

Quoted 8 lines
Quoted 1 line

Hans

It looks like this series causes allyesconfig to break on linux-next:

https://github.com/ClangBuiltLinux/continuous-integration2/runs/2773158286?check_suite_focus=true

$ make -skj"$(nproc)" allyesconfig all
ld: drivers/platform/x86/think-lmi.o:(.bss+0x0): multiple definition of `fw_attr_class'; drivers/platform/x86/dell/dell-wmi-sysman/sysman.o:(.bss+0x0): first defined here

Thank you for reporting this. This is caused by both these files:

drivers/platform/x86/dell/dell-wmi-sysman/sysman.c drivers/platform/x86/think-lmi.c

having a global struct class *fw_attr_class variable, which should be static in both files. I'll send a patch fixing this (and merge the patch into the pdx86/for-next branch).

Regards,

Hans