Skip to contentSkip to conversation

platform: add samsung-wmi driver for newer Samsung laptops

PATCH In review [email protected] 3 participants 7 messages Apr 7, 2015 – Apr 16, 2015 1 message not in the archive x86 platform drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform: add samsung-wmi driver for newer Samsung laptops

From: Gavin Li <[email protected]>

This driver adds support for coarse (on or off) fan control and the keyboard backlight on newer Samsung laptops.

---
 drivers/platform/x86/Kconfig       |  10 ++
 drivers/platform/x86/Makefile      |   1 +
 drivers/platform/x86/samsung-wmi.c | 221 +++++++++++++++++++++++++++++++++++++
 3 files changed, 232 insertions(+)
 create mode 100644 drivers/platform/x86/samsung-wmi.c
drivers/platform/x86/Kconfig +10 -0
index 9752761..96f4620 100644
@@ -813,6 +813,16 @@ config SAMSUNG_LAPTOP
 	  To compile this driver as a module, choose M here: the module
 	  will be called samsung-laptop.
 
+config SAMSUNG_WMI
+	tristate "Samsung WMI driver (for newer Samsung laptops)"
+	depends on ACPI
+	depends on ACPI_WMI
+	select LEDS_CLASS
+	select NEW_LEDS
+	  ---help---
+	  This driver adds support for coarse (on or off) fan control
+	  and the keyboard backlight for newer Samsung laptops.
+
 config MXM_WMI
        tristate "WMI support for MXM Laptop Graphics"
        depends on ACPI_WMI
drivers/platform/x86/Makefile +1 -0
index f82232b..4c65e51 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_XO1_RFKILL)	+= xo1-rfkill.o
 obj-$(CONFIG_XO15_EBOOK)	+= xo15-ebook.o
 obj-$(CONFIG_IBM_RTL)		+= ibm_rtl.o
 obj-$(CONFIG_SAMSUNG_LAPTOP)	+= samsung-laptop.o
+obj-$(CONFIG_SAMSUNG_WMI)	+= samsung-wmi.o
 obj-$(CONFIG_MXM_WMI)		+= mxm-wmi.o
 obj-$(CONFIG_INTEL_MID_POWER_BUTTON)	+= intel_mid_powerbtn.o
 obj-$(CONFIG_INTEL_OAKTRAIL)	+= intel_oaktrail.o
drivers/platform/x86/samsung-wmi.c new file +221 -0

221 lines not on this page · raw message

Signature · 1 line
2.3.5
#1 lore raw

Hi Gavin Li,

Quoted 97 lines · On Tue, 7 Apr 2015, [email protected] wrote:

From: Gavin Li <[email protected]>

This driver adds support for coarse (on or off) fan control and the keyboard backlight on newer Samsung laptops.

---
 drivers/platform/x86/Kconfig       |  10 ++
 drivers/platform/x86/Makefile      |   1 +
 drivers/platform/x86/samsung-wmi.c | 221 +++++++++++++++++++++++++++++++++++++
 3 files changed, 232 insertions(+)
 create mode 100644 drivers/platform/x86/samsung-wmi.c
drivers/platform/x86/Kconfig +10 -0
index 9752761..96f4620 100644
@@ -813,6 +813,16 @@ config SAMSUNG_LAPTOP
 	  To compile this driver as a module, choose M here: the module
 	  will be called samsung-laptop.
 
+config SAMSUNG_WMI
+	tristate "Samsung WMI driver (for newer Samsung laptops)"
+	depends on ACPI
+	depends on ACPI_WMI
+	select LEDS_CLASS
+	select NEW_LEDS
+	  ---help---
+	  This driver adds support for coarse (on or off) fan control
+	  and the keyboard backlight for newer Samsung laptops.
+
 config MXM_WMI
        tristate "WMI support for MXM Laptop Graphics"
        depends on ACPI_WMI
drivers/platform/x86/Makefile +1 -0
index f82232b..4c65e51 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_XO1_RFKILL)	+= xo1-rfkill.o
 obj-$(CONFIG_XO15_EBOOK)	+= xo15-ebook.o
 obj-$(CONFIG_IBM_RTL)		+= ibm_rtl.o
 obj-$(CONFIG_SAMSUNG_LAPTOP)	+= samsung-laptop.o
+obj-$(CONFIG_SAMSUNG_WMI)	+= samsung-wmi.o
 obj-$(CONFIG_MXM_WMI)		+= mxm-wmi.o
 obj-$(CONFIG_INTEL_MID_POWER_BUTTON)	+= intel_mid_powerbtn.o
 obj-$(CONFIG_INTEL_OAKTRAIL)	+= intel_oaktrail.o
drivers/platform/x86/samsung-wmi.c new file +47 -0
new file mode 100644
index 0000000..c93cfe4
@@ -0,0 +1,221 @@
+/*
+ *  Samsung WMI driver
+ *
+ *  Copyright (C) 2015 Gavin Li <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/acpi.h>
+#include <linux/leds.h>
+#include <linux/platform_device.h>
+
+#define DRIVER_NAME "samsung-wmi"
+#define WMI_GUID "C16C47BA-50E3-444A-AF3A-B1C348380001"
+
+struct samsung_wmi_packet {
+	uint16_t magic;
+	uint16_t opcode;
+	uint8_t reqres;
+	uint8_t data[16];
+} __packed;
+
+static struct platform_device *samsung_wmi_device;
+
+static inline bool string_matches(const char *str, const char *kwd) {
+	size_t len = strlen(kwd);
+	if (strncmp(str, kwd, len) != 0)
+		return false;
+	return str[len] == '\0' || str[len] == '\n';
+}
+
+static int samsung_wmi_method_call(uint16_t opcode, void *data, size_t len) {
+	struct samsung_wmi_packet inpkt = {

drivers/platform/x86/samsung-wmi.c line 57

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+		.magic = 0x5843,
+		.opcode = opcode,
+	};
+	struct acpi_buffer inbuf = { sizeof(inpkt), &inpkt };
+	struct acpi_buffer outbuf = { ACPI_ALLOCATE_BUFFER, NULL };
+	union acpi_object *outobj;
+	struct samsung_wmi_packet *outpkt;
+	acpi_status st;
+	int ret = -EIO;
+	/********/

Comment is not needed

Quoted 8 lines
drivers/platform/x86/samsung-wmi.c new file +8 -0
+	if (len > 16) {
+		ret = -EINVAL;
+		goto err0;
+	}
+	memcpy(inpkt.data, data, len);
+	st = wmi_evaluate_method(WMI_GUID, 1, 0, &inbuf, &outbuf);
+	if (ACPI_FAILURE(st)) {
+		dev_err(&samsung_wmi_device->dev, "opcode 0x%x failed: %s\n",

drivers/platform/x86/samsung-wmi.c line 75

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+				opcode, acpi_format_exception(st));
+		goto err0;
+	}
+	outobj = outbuf.pointer;
+	if (outobj->type != ACPI_TYPE_BUFFER) {
+		goto err1;
+	}
+	if (outobj->buffer.length < sizeof(outpkt)) {
+		goto err1;
+	}

Don't need {} here.

drivers/platform/x86/samsung-wmi.c line 82

Quoted 7 lines
drivers/platform/x86/samsung-wmi.c new file +7 -0
+	outpkt = (struct samsung_wmi_packet *)outobj->buffer.pointer;
+	memcpy(data, outpkt->data, len);
+	ret = 0;
+err1:
+	kfree(outobj);
+err0:
+	return ret;

Could really improve the labels

drivers/platform/x86/samsung-wmi.c line 90

Quoted 8 lines
drivers/platform/x86/samsung-wmi.c new file +8 -0
+}
+
+static int samsung_wmi_method_call_with_unlock(
+		uint16_t unlock_opcode,
+		uint16_t opcode, void *data, size_t len) {
+	unsigned int unlock_data = 0xAABB;
+	int ret;
+	/********/

Comment is not needed

drivers/platform/x86/samsung-wmi.c line 101

Quoted 11 lines
drivers/platform/x86/samsung-wmi.c new file +11 -0
+	ret = samsung_wmi_method_call(unlock_opcode, &unlock_data, sizeof(unlock_data));
+	if (ret)
+		goto err0;
+	if (unlock_data != 0xCCDD) {
+		dev_err(&samsung_wmi_device->dev, "incorrect unlock response!\n");
+		ret = -EIO;
+		goto err0;
+	}
+	ret = samsung_wmi_method_call(opcode, data, len);
+err0:
+	return ret;

Could improve the label

Quoted 28 lines
drivers/platform/x86/samsung-wmi.c new file +28 -0
+}
+
+static ssize_t samsung_wmi_fan_mode_show(struct device *dev, struct device_attribute *attr,
+		char *buf) {
+	uint32_t data = 0;
+	if (samsung_wmi_method_call_with_unlock(0x31, 0x31, &data, sizeof(data)))
+		return -EIO;
+	if (data)
+		strcpy(buf, "[auto off] on\n");
+	else
+		strcpy(buf, "auto off [on]\n");
+	return strlen(buf);
+}
+
+static ssize_t samsung_wmi_fan_mode_store(struct device *dev, struct device_attribute *attr,
+		const char *buf, size_t count) {
+	uint32_t data;
+	if (string_matches(buf, "auto"))
+		data = 0x810001;
+	else if (string_matches(buf, "on"))
+		data = 0;
+	else if (string_matches(buf, "off"))
+		data = 0x800001;
+	else
+		return -EINVAL;
+	if (samsung_wmi_method_call_with_unlock(0x31, 0x32, &data, sizeof(data)))
+		return -EIO;
+	else

drivers/platform/x86/samsung-wmi.c line 139

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+		return count;
+}
+
+static void samsung_wmi_kbd_backlight_led_brightness_set(
+		struct led_classdev *cdev, enum led_brightness brightness) {
+	unsigned int data = brightness;
+	if (data > 4)
+		data = 4;
+	data = (data << 8) | 0x82;
+	/********/

This comment is not needed

drivers/platform/x86/samsung-wmi.c line 149

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+	samsung_wmi_method_call_with_unlock(0x78, 0x78, &data, sizeof(data));
+}
+
+static DEVICE_ATTR(fan_mode, S_IRUGO | S_IWUSR, samsung_wmi_fan_mode_show, samsung_wmi_fan_mode_store);
+
+static struct platform_driver samsung_wmi_driver = {
+	.driver = {
+		.name = DRIVER_NAME,
+		.owner = THIS_MODULE,
+	},

If I remember correctly you don't need to set .owner to THIS_MODULE anymore

Quoted 13 lines
drivers/platform/x86/samsung-wmi.c new file +13 -0
+};
+
+static struct led_classdev samsung_wmi_kbd_backlight_led = {
+	.name = DRIVER_NAME "::kbd_backlight",
+	.max_brightness = 4,
+	.brightness_set = samsung_wmi_kbd_backlight_led_brightness_set,
+};
+
+static int kb_default_brightness = -1;
+module_param(kb_default_brightness, int, S_IRUGO);
+MODULE_PARM_DESC(kb_default_brightness,
+		"Default keyboard backlight brightness right after initialization");
+

drivers/platform/x86/samsung-wmi.c line 172

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+static int samsung_wmi_init(void) {
+	int ret;
+	// check whether method is present
+	if (!wmi_has_guid(WMI_GUID))
+		return -ENODEV;
+	// we're good to go
+	ret = platform_driver_register(&samsung_wmi_driver);
+	if (ret) {
+		goto err0;
+	}

{} are not needed if there is only one statement

drivers/platform/x86/samsung-wmi.c line 181

Quoted 9 lines
drivers/platform/x86/samsung-wmi.c new file +9 -0
+	samsung_wmi_device = platform_device_alloc(DRIVER_NAME, -1);
+	if (!samsung_wmi_device) {
+		ret = -ENOMEM;
+		goto err1;
+	}
+	ret = platform_device_add(samsung_wmi_device);
+	if (ret) {
+		goto err2;
+	}

Same

drivers/platform/x86/samsung-wmi.c line 185

Quoted 4 lines
drivers/platform/x86/samsung-wmi.c new file +4 -0
+	ret = device_create_file(&samsung_wmi_device->dev, &dev_attr_fan_mode);
+	if (ret) {
+		goto err3;
+	}

Same

drivers/platform/x86/samsung-wmi.c line 190

Quoted 5 lines
drivers/platform/x86/samsung-wmi.c new file +5 -0
+	ret = led_classdev_register(&samsung_wmi_device->dev,
+			&samsung_wmi_kbd_backlight_led);
+	if (ret) {
+		goto err4;
+	}

Same

drivers/platform/x86/samsung-wmi.c line 193

Quoted 3 lines
drivers/platform/x86/samsung-wmi.c new file +3 -0
+	if (kb_default_brightness >= 0) {
+		led_set_brightness(&samsung_wmi_kbd_backlight_led, kb_default_brightness);
+	}

Same

drivers/platform/x86/samsung-wmi.c line 206

Quoted 13 lines
drivers/platform/x86/samsung-wmi.c new file +13 -0
+	return 0;
+//err5:
+	led_classdev_unregister(&samsung_wmi_kbd_backlight_led);
+err4:
+	device_remove_file(&samsung_wmi_device->dev, &dev_attr_fan_mode);
+err3:
+	platform_device_del(samsung_wmi_device);
+err2:
+	platform_device_put(samsung_wmi_device);
+err1:
+	platform_driver_unregister(&samsung_wmi_driver);
+err0:
+	return ret;

err5 is commented here so why not remove it? Also, you could think of better label names. This is a good essay/rant on them:
https://plus.google.com/106378716002406849458/posts/dnanfhQ4mHQ

Quoted 23 lines
drivers/platform/x86/samsung-wmi.c new file +15 -0
+}
+
+static void samsung_wmi_exit(void) {
+	led_classdev_unregister(&samsung_wmi_kbd_backlight_led);
+	device_remove_file(&samsung_wmi_device->dev, &dev_attr_fan_mode);
+	platform_device_unregister(samsung_wmi_device);
+	platform_driver_unregister(&samsung_wmi_driver);
+}
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Gavin Li");
+MODULE_DESCRIPTION("WMI driver for some Samsung laptops.");
+MODULE_ALIAS("wmi:" WMI_GUID);
+module_init(samsung_wmi_init);
+module_exit(samsung_wmi_exit);
Signature · 6 lines
2.3.5

--
To unsubscribe from this list: send the line "unsubscribe platform-driver-x86" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Su pagarba / Regards,
Giedrius

#2 lore raw

PATCH 1/1 platform: add samsung-wmi driver for newer Samsung laptops

From: Gavin Li <[email protected]>

This driver adds support for coarse (on or off) fan control and the keyboard backlight on newer Samsung laptops.

---
 drivers/platform/x86/Kconfig       |  10 ++
 drivers/platform/x86/Makefile      |   1 +
 drivers/platform/x86/samsung-wmi.c | 217 +++++++++++++++++++++++++++++++++++++
 3 files changed, 228 insertions(+)
 create mode 100644 drivers/platform/x86/samsung-wmi.c
drivers/platform/x86/Kconfig +10 -0
index 9752761..96f4620 100644
@@ -813,6 +813,16 @@ config SAMSUNG_LAPTOP
 	  To compile this driver as a module, choose M here: the module
 	  will be called samsung-laptop.
 
+config SAMSUNG_WMI
+	tristate "Samsung WMI driver (for newer Samsung laptops)"
+	depends on ACPI
+	depends on ACPI_WMI
+	select LEDS_CLASS
+	select NEW_LEDS
+	  ---help---
+	  This driver adds support for coarse (on or off) fan control
+	  and the keyboard backlight for newer Samsung laptops.
+
 config MXM_WMI
        tristate "WMI support for MXM Laptop Graphics"
        depends on ACPI_WMI
drivers/platform/x86/Makefile +1 -0
index f82232b..4c65e51 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_XO1_RFKILL)	+= xo1-rfkill.o
 obj-$(CONFIG_XO15_EBOOK)	+= xo15-ebook.o
 obj-$(CONFIG_IBM_RTL)		+= ibm_rtl.o
 obj-$(CONFIG_SAMSUNG_LAPTOP)	+= samsung-laptop.o
+obj-$(CONFIG_SAMSUNG_WMI)	+= samsung-wmi.o
 obj-$(CONFIG_MXM_WMI)		+= mxm-wmi.o
 obj-$(CONFIG_INTEL_MID_POWER_BUTTON)	+= intel_mid_powerbtn.o
 obj-$(CONFIG_INTEL_OAKTRAIL)	+= intel_oaktrail.o
drivers/platform/x86/samsung-wmi.c new file +217 -0

217 lines not on this page · raw message

Signature · 1 line
2.3.5
#4 lore raw

PATCH 1/1 platform: add samsung-wmi driver for newer Samsung laptops

From: Gavin Li <[email protected]>

This driver adds support for coarse (on or off) fan control and the keyboard backlight on newer Samsung laptops.

---
 drivers/platform/x86/Kconfig       |  10 ++
 drivers/platform/x86/Makefile      |   1 +
 drivers/platform/x86/samsung-wmi.c | 217 +++++++++++++++++++++++++++++++++++++
 3 files changed, 228 insertions(+)
 create mode 100644 drivers/platform/x86/samsung-wmi.c
drivers/platform/x86/Kconfig +10 -0
index 9752761..96f4620 100644
@@ -813,6 +813,16 @@ config SAMSUNG_LAPTOP
 	  To compile this driver as a module, choose M here: the module
 	  will be called samsung-laptop.
 
+config SAMSUNG_WMI
+	tristate "Samsung WMI driver (for newer Samsung laptops)"
+	depends on ACPI
+	depends on ACPI_WMI
+	select LEDS_CLASS
+	select NEW_LEDS
+	  ---help---
+	  This driver adds support for coarse (on or off) fan control
+	  and the keyboard backlight for newer Samsung laptops.
+
 config MXM_WMI
        tristate "WMI support for MXM Laptop Graphics"
        depends on ACPI_WMI
drivers/platform/x86/Makefile +1 -0
index f82232b..4c65e51 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_XO1_RFKILL)	+= xo1-rfkill.o
 obj-$(CONFIG_XO15_EBOOK)	+= xo15-ebook.o
 obj-$(CONFIG_IBM_RTL)		+= ibm_rtl.o
 obj-$(CONFIG_SAMSUNG_LAPTOP)	+= samsung-laptop.o
+obj-$(CONFIG_SAMSUNG_WMI)	+= samsung-wmi.o
 obj-$(CONFIG_MXM_WMI)		+= mxm-wmi.o
 obj-$(CONFIG_INTEL_MID_POWER_BUTTON)	+= intel_mid_powerbtn.o
 obj-$(CONFIG_INTEL_OAKTRAIL)	+= intel_oaktrail.o
drivers/platform/x86/samsung-wmi.c new file +217 -0

217 lines not on this page · raw message

Signature · 1 line
2.3.5
#5 lore raw
In reply to [email protected]
Quoted 88 lines · On Tue, Apr 7, 2015 at 6:53 PM, <[email protected]> wrote:

From: Gavin Li <[email protected]>

This driver adds support for coarse (on or off) fan control and the keyboard backlight on newer Samsung laptops.

---
 drivers/platform/x86/Kconfig       |  10 ++
 drivers/platform/x86/Makefile      |   1 +
 drivers/platform/x86/samsung-wmi.c | 221 +++++++++++++++++++++++++++++++++++++
 3 files changed, 232 insertions(+)
 create mode 100644 drivers/platform/x86/samsung-wmi.c
drivers/platform/x86/Kconfig +10 -0
index 9752761..96f4620 100644
@@ -813,6 +813,16 @@ config SAMSUNG_LAPTOP
          To compile this driver as a module, choose M here: the module
          will be called samsung-laptop.
 
+config SAMSUNG_WMI
+       tristate "Samsung WMI driver (for newer Samsung laptops)"
+       depends on ACPI
+       depends on ACPI_WMI
+       select LEDS_CLASS
+       select NEW_LEDS
+         ---help---
+         This driver adds support for coarse (on or off) fan control
+         and the keyboard backlight for newer Samsung laptops.
+
 config MXM_WMI
        tristate "WMI support for MXM Laptop Graphics"
        depends on ACPI_WMI
drivers/platform/x86/Makefile +1 -0
index f82232b..4c65e51 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_XO1_RFKILL)      += xo1-rfkill.o
 obj-$(CONFIG_XO15_EBOOK)       += xo15-ebook.o
 obj-$(CONFIG_IBM_RTL)          += ibm_rtl.o
 obj-$(CONFIG_SAMSUNG_LAPTOP)   += samsung-laptop.o
+obj-$(CONFIG_SAMSUNG_WMI)      += samsung-wmi.o
 obj-$(CONFIG_MXM_WMI)          += mxm-wmi.o
 obj-$(CONFIG_INTEL_MID_POWER_BUTTON)   += intel_mid_powerbtn.o
 obj-$(CONFIG_INTEL_OAKTRAIL)   += intel_oaktrail.o
drivers/platform/x86/samsung-wmi.c new file +38 -0
new file mode 100644
index 0000000..c93cfe4
@@ -0,0 +1,221 @@
+/*
+ *  Samsung WMI driver
+ *
+ *  Copyright (C) 2015 Gavin Li <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/acpi.h>
+#include <linux/leds.h>
+#include <linux/platform_device.h>
+
+#define DRIVER_NAME "samsung-wmi"
+#define WMI_GUID "C16C47BA-50E3-444A-AF3A-B1C348380001"
+
+struct samsung_wmi_packet {
+       uint16_t magic;
+       uint16_t opcode;
+       uint8_t reqres;
+       uint8_t data[16];
+} __packed;
+
+static struct platform_device *samsung_wmi_device;
+

drivers/platform/x86/samsung-wmi.c line 48

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+static inline bool string_matches(const char *str, const char *kwd) {
+       size_t len = strlen(kwd);
+       if (strncmp(str, kwd, len) != 0)
+               return false;
+       return str[len] == '\0' || str[len] == '\n';
+}
+
+static int samsung_wmi_method_call(uint16_t opcode, void *data, size_t len) {
+       struct samsung_wmi_packet inpkt = {
+               .magic = 0x5843,

Please use a constant instead of a magic number here.

drivers/platform/x86/samsung-wmi.c line 58

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+               .opcode = opcode,
+       };
+       struct acpi_buffer inbuf = { sizeof(inpkt), &inpkt };
+       struct acpi_buffer outbuf = { ACPI_ALLOCATE_BUFFER, NULL };
+       union acpi_object *outobj;
+       struct samsung_wmi_packet *outpkt;
+       acpi_status st;
+       int ret = -EIO;
+       /********/
+       if (len > 16) {

Please use a constant instead of 16 here.

Quoted 20 lines
drivers/platform/x86/samsung-wmi.c new file +20 -0
+               ret = -EINVAL;
+               goto err0;
+       }
+       memcpy(inpkt.data, data, len);
+       st = wmi_evaluate_method(WMI_GUID, 1, 0, &inbuf, &outbuf);
+       if (ACPI_FAILURE(st)) {
+               dev_err(&samsung_wmi_device->dev, "opcode 0x%x failed: %s\n",
+                               opcode, acpi_format_exception(st));
+               goto err0;
+       }
+       outobj = outbuf.pointer;
+       if (outobj->type != ACPI_TYPE_BUFFER) {
+               goto err1;
+       }
+       if (outobj->buffer.length < sizeof(outpkt)) {
+               goto err1;
+       }
+       outpkt = (struct samsung_wmi_packet *)outobj->buffer.pointer;
+       memcpy(data, outpkt->data, len);
+       ret = 0;

drivers/platform/x86/samsung-wmi.c line 88

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+err1:
+       kfree(outobj);
+err0:
+       return ret;
+}
+
+static int samsung_wmi_method_call_with_unlock(
+               uint16_t unlock_opcode,
+               uint16_t opcode, void *data, size_t len) {
+       unsigned int unlock_data = 0xAABB;

Same

Quoted 22 lines
drivers/platform/x86/samsung-wmi.c new file +22 -0
+       int ret;
+       /********/
+       ret = samsung_wmi_method_call(unlock_opcode, &unlock_data, sizeof(unlock_data));
+       if (ret)
+               goto err0;
+       if (unlock_data != 0xCCDD) {
+               dev_err(&samsung_wmi_device->dev, "incorrect unlock response!\n");
+               ret = -EIO;
+               goto err0;
+       }
+       ret = samsung_wmi_method_call(opcode, data, len);
+err0:
+       return ret;
+}
+
+static ssize_t samsung_wmi_fan_mode_show(struct device *dev, struct device_attribute *attr,
+               char *buf) {
+       uint32_t data = 0;
+       if (samsung_wmi_method_call_with_unlock(0x31, 0x31, &data, sizeof(data)))
+               return -EIO;
+       if (data)
+               strcpy(buf, "[auto off] on\n");

drivers/platform/x86/samsung-wmi.c line 120

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+       else
+               strcpy(buf, "auto off [on]\n");
+       return strlen(buf);
+}
+
+static ssize_t samsung_wmi_fan_mode_store(struct device *dev, struct device_attribute *attr,
+               const char *buf, size_t count) {
+       uint32_t data;
+       if (string_matches(buf, "auto"))
+               data = 0x810001;

Same, and other places

Quoted 5 lines
drivers/platform/x86/samsung-wmi.c new file +5 -0
+       else if (string_matches(buf, "on"))
+               data = 0;
+       else if (string_matches(buf, "off"))
+               data = 0x800001;
+       else

drivers/platform/x86/samsung-wmi.c line 135

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+               return -EINVAL;
+       if (samsung_wmi_method_call_with_unlock(0x31, 0x32, &data, sizeof(data)))
+               return -EIO;
+       else
+               return count;
+}
+
+static void samsung_wmi_kbd_backlight_led_brightness_set(
+               struct led_classdev *cdev, enum led_brightness brightness) {
+       unsigned int data = brightness;

Why sometime uint32_t and sometime unsigned int ?

Quoted 52 lines
drivers/platform/x86/samsung-wmi.c new file +52 -0
+       if (data > 4)
+               data = 4;
+       data = (data << 8) | 0x82;
+       /********/
+       samsung_wmi_method_call_with_unlock(0x78, 0x78, &data, sizeof(data));
+}
+
+static DEVICE_ATTR(fan_mode, S_IRUGO | S_IWUSR, samsung_wmi_fan_mode_show, samsung_wmi_fan_mode_store);
+
+static struct platform_driver samsung_wmi_driver = {
+       .driver = {
+               .name = DRIVER_NAME,
+               .owner = THIS_MODULE,
+       },
+};
+
+static struct led_classdev samsung_wmi_kbd_backlight_led = {
+       .name = DRIVER_NAME "::kbd_backlight",
+       .max_brightness = 4,
+       .brightness_set = samsung_wmi_kbd_backlight_led_brightness_set,
+};
+
+static int kb_default_brightness = -1;
+module_param(kb_default_brightness, int, S_IRUGO);
+MODULE_PARM_DESC(kb_default_brightness,
+               "Default keyboard backlight brightness right after initialization");
+
+static int samsung_wmi_init(void) {
+       int ret;
+       // check whether method is present
+       if (!wmi_has_guid(WMI_GUID))
+               return -ENODEV;
+       // we're good to go
+       ret = platform_driver_register(&samsung_wmi_driver);
+       if (ret) {
+               goto err0;
+       }
+       samsung_wmi_device = platform_device_alloc(DRIVER_NAME, -1);
+       if (!samsung_wmi_device) {
+               ret = -ENOMEM;
+               goto err1;
+       }
+       ret = platform_device_add(samsung_wmi_device);
+       if (ret) {
+               goto err2;
+       }
+       ret = device_create_file(&samsung_wmi_device->dev, &dev_attr_fan_mode);
+       if (ret) {
+               goto err3;
+       }
+       ret = led_classdev_register(&samsung_wmi_device->dev,
+                       &samsung_wmi_kbd_backlight_led);

drivers/platform/x86/samsung-wmi.c line 197

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+       if (ret) {
+               goto err4;
+       }
+       if (kb_default_brightness >= 0) {
+               led_set_brightness(&samsung_wmi_kbd_backlight_led, kb_default_brightness);
+       }
+       return 0;
+//err5:
+       led_classdev_unregister(&samsung_wmi_kbd_backlight_led);
+err4:

Instead of 'errX' please give a name related to what happen after the label (or what failed just before)

Quoted 31 lines
drivers/platform/x86/samsung-wmi.c new file +24 -1
+       device_remove_file(&samsung_wmi_device->dev, &dev_attr_fan_mode);
+err3:
+       platform_device_del(samsung_wmi_device);
+err2:
+       platform_device_put(samsung_wmi_device);
+err1:
+       platform_driver_unregister(&samsung_wmi_driver);
+err0:
+       return ret;
+}
+
+static void samsung_wmi_exit(void) {
+       led_classdev_unregister(&samsung_wmi_kbd_backlight_led);
+       device_remove_file(&samsung_wmi_device->dev, &dev_attr_fan_mode);
+       platform_device_unregister(samsung_wmi_device);
+       platform_driver_unregister(&samsung_wmi_driver);
+}
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Gavin Li");
+MODULE_DESCRIPTION("WMI driver for some Samsung laptops.");
+MODULE_ALIAS("wmi:" WMI_GUID);
+module_init(samsung_wmi_init);
+module_exit(samsung_wmi_exit);
--

2.3.5

--
To unsubscribe from this list: send the line "unsubscribe platform-driver-x86" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Corentin Chary
http://xf.iksaif.net

A message that is not in the archive (sent off-list or not yet received): CA+GxvY4t-DBeaYzzhWqV8OexikkYUdCaCwFc=z_1gjnj5bXUfg@mail.gmail.com

author
#7 lore raw

Actually there is an issue that affects the functionality of the driver; I will have to work on this a bit more first. Thanks for the feedback.

Gavin

Quoted 338 lines · Gavin Li

Actually there is an issue that affects the functionality of the driver; I will have to work on this a bit more first. Thanks for the feedback.

Gavin

Quoted 329 lines · Corentin Chary
Quoted 104 lines · On Tue, Apr 7, 2015 at 6:53 PM, <[email protected]> wrote:

From: Gavin Li <[email protected]>

This driver adds support for coarse (on or off) fan control and the keyboard backlight on newer Samsung laptops.

---
 drivers/platform/x86/Kconfig       |  10 ++
 drivers/platform/x86/Makefile      |   1 +
 drivers/platform/x86/samsung-wmi.c | 221
+++++++++++++++++++++++++++++++++++++
 3 files changed, 232 insertions(+)
 create mode 100644 drivers/platform/x86/samsung-wmi.c
drivers/platform/x86/Kconfig +10 -0
index 9752761..96f4620 100644
@@ -813,6 +813,16 @@ config SAMSUNG_LAPTOP
          To compile this driver as a module, choose M here: the module
          will be called samsung-laptop.
 
+config SAMSUNG_WMI
+       tristate "Samsung WMI driver (for newer Samsung laptops)"
+       depends on ACPI
+       depends on ACPI_WMI
+       select LEDS_CLASS
+       select NEW_LEDS
+         ---help---
+         This driver adds support for coarse (on or off) fan control
+         and the keyboard backlight for newer Samsung laptops.
+
 config MXM_WMI
        tristate "WMI support for MXM Laptop Graphics"
        depends on ACPI_WMI
drivers/platform/x86/Makefile +1 -0
index f82232b..4c65e51 100644
@@ -48,6 +48,7 @@ obj-$(CONFIG_XO1_RFKILL)      += xo1-rfkill.o
 obj-$(CONFIG_XO15_EBOOK)       += xo15-ebook.o
 obj-$(CONFIG_IBM_RTL)          += ibm_rtl.o
 obj-$(CONFIG_SAMSUNG_LAPTOP)   += samsung-laptop.o
+obj-$(CONFIG_SAMSUNG_WMI)      += samsung-wmi.o
 obj-$(CONFIG_MXM_WMI)          += mxm-wmi.o
 obj-$(CONFIG_INTEL_MID_POWER_BUTTON)   += intel_mid_powerbtn.o
 obj-$(CONFIG_INTEL_OAKTRAIL)   += intel_oaktrail.o
drivers/platform/x86/samsung-wmi.c new file +6 -0
new file mode 100644
index 0000000..c93cfe4
@@ -0,0 +1,221 @@
+/*
+ *  Samsung WMI driver
+ *
+ *  Copyright (C) 2015 Gavin Li <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or

modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *

+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.

+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */

drivers/platform/x86/samsung-wmi.c new file +27 -0
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/acpi.h>
+#include <linux/leds.h>
+#include <linux/platform_device.h>
+
+#define DRIVER_NAME "samsung-wmi"
+#define WMI_GUID "C16C47BA-50E3-444A-AF3A-B1C348380001"
+
+struct samsung_wmi_packet {
+       uint16_t magic;
+       uint16_t opcode;
+       uint8_t reqres;
+       uint8_t data[16];
+} __packed;
+
+static struct platform_device *samsung_wmi_device;
+
+static inline bool string_matches(const char *str, const char *kwd) {
+       size_t len = strlen(kwd);
+       if (strncmp(str, kwd, len) != 0)
+               return false;
+       return str[len] == '\0' || str[len] == '\n';
+}
+
+static int samsung_wmi_method_call(uint16_t opcode, void *data, size_t

len) {

drivers/platform/x86/samsung-wmi.c line 48

Quoted 2 lines
drivers/platform/x86/samsung-wmi.c new file +2 -0
+       struct samsung_wmi_packet inpkt = {
+               .magic = 0x5843,

Please use a constant instead of a magic number here.

drivers/platform/x86/samsung-wmi.c line 58

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+               .opcode = opcode,
+       };
+       struct acpi_buffer inbuf = { sizeof(inpkt), &inpkt };
+       struct acpi_buffer outbuf = { ACPI_ALLOCATE_BUFFER, NULL };
+       union acpi_object *outobj;
+       struct samsung_wmi_packet *outpkt;
+       acpi_status st;
+       int ret = -EIO;
+       /********/
+       if (len > 16) {

Please use a constant instead of 16 here.

Quoted 21 lines
drivers/platform/x86/samsung-wmi.c new file +7 -0
+               ret = -EINVAL;
+               goto err0;
+       }
+       memcpy(inpkt.data, data, len);
+       st = wmi_evaluate_method(WMI_GUID, 1, 0, &inbuf, &outbuf);
+       if (ACPI_FAILURE(st)) {
+               dev_err(&samsung_wmi_device->dev, "opcode 0x%x failed:

%s\n",

drivers/platform/x86/samsung-wmi.c new file +13 -0
+                               opcode, acpi_format_exception(st));
+               goto err0;
+       }
+       outobj = outbuf.pointer;
+       if (outobj->type != ACPI_TYPE_BUFFER) {
+               goto err1;
+       }
+       if (outobj->buffer.length < sizeof(outpkt)) {
+               goto err1;
+       }
+       outpkt = (struct samsung_wmi_packet *)outobj->buffer.pointer;
+       memcpy(data, outpkt->data, len);
+       ret = 0;

drivers/platform/x86/samsung-wmi.c line 88

Quoted 10 lines
drivers/platform/x86/samsung-wmi.c new file +10 -0
+err1:
+       kfree(outobj);
+err0:
+       return ret;
+}
+
+static int samsung_wmi_method_call_with_unlock(
+               uint16_t unlock_opcode,
+               uint16_t opcode, void *data, size_t len) {
+       unsigned int unlock_data = 0xAABB;

Same

Quoted 33 lines
drivers/platform/x86/samsung-wmi.c new file +3 -0
+       int ret;
+       /********/
+       ret = samsung_wmi_method_call(unlock_opcode, &unlock_data,

sizeof(unlock_data));

drivers/platform/x86/samsung-wmi.c new file +4 -0
+       if (ret)
+               goto err0;
+       if (unlock_data != 0xCCDD) {
+               dev_err(&samsung_wmi_device->dev, "incorrect unlock

response!\n");

drivers/platform/x86/samsung-wmi.c new file +9 -0
+               ret = -EIO;
+               goto err0;
+       }
+       ret = samsung_wmi_method_call(opcode, data, len);
+err0:
+       return ret;
+}
+
+static ssize_t samsung_wmi_fan_mode_show(struct device *dev, struct

device_attribute *attr,

drivers/platform/x86/samsung-wmi.c new file +3 -0
+               char *buf) {
+       uint32_t data = 0;
+       if (samsung_wmi_method_call_with_unlock(0x31, 0x31, &data,

sizeof(data)))

drivers/platform/x86/samsung-wmi.c new file +9 -0
+               return -EIO;
+       if (data)
+               strcpy(buf, "[auto off] on\n");
+       else
+               strcpy(buf, "auto off [on]\n");
+       return strlen(buf);
+}
+
+static ssize_t samsung_wmi_fan_mode_store(struct device *dev, struct

device_attribute *attr,

drivers/platform/x86/samsung-wmi.c line 120

Quoted 4 lines
drivers/platform/x86/samsung-wmi.c new file +4 -0
+               const char *buf, size_t count) {
+       uint32_t data;
+       if (string_matches(buf, "auto"))
+               data = 0x810001;

Same, and other places

Quoted 15 lines
drivers/platform/x86/samsung-wmi.c new file +7 -0
+       else if (string_matches(buf, "on"))
+               data = 0;
+       else if (string_matches(buf, "off"))
+               data = 0x800001;
+       else
+               return -EINVAL;
+       if (samsung_wmi_method_call_with_unlock(0x31, 0x32, &data,

sizeof(data)))

drivers/platform/x86/samsung-wmi.c new file +7 -0
+               return -EIO;
+       else
+               return count;
+}
+
+static void samsung_wmi_kbd_backlight_led_brightness_set(
+               struct led_classdev *cdev, enum led_brightness
Quoted 2 lines

brightness) {
+ unsigned int data = brightness;

Why sometime uint32_t and sometime unsigned int ?

Quoted 62 lines
drivers/platform/x86/samsung-wmi.c new file +5 -0
+       if (data > 4)
+               data = 4;
+       data = (data << 8) | 0x82;
+       /********/
+       samsung_wmi_method_call_with_unlock(0x78, 0x78, &data,

sizeof(data));

drivers/platform/x86/samsung-wmi.c new file +3 -0
+}
+
+static DEVICE_ATTR(fan_mode, S_IRUGO | S_IWUSR,

samsung_wmi_fan_mode_show, samsung_wmi_fan_mode_store);

drivers/platform/x86/samsung-wmi.c new file +18 -0
+
+static struct platform_driver samsung_wmi_driver = {
+       .driver = {
+               .name = DRIVER_NAME,
+               .owner = THIS_MODULE,
+       },
+};
+
+static struct led_classdev samsung_wmi_kbd_backlight_led = {
+       .name = DRIVER_NAME "::kbd_backlight",
+       .max_brightness = 4,
+       .brightness_set = samsung_wmi_kbd_backlight_led_brightness_set,
+};
+
+static int kb_default_brightness = -1;
+module_param(kb_default_brightness, int, S_IRUGO);
+MODULE_PARM_DESC(kb_default_brightness,
+               "Default keyboard backlight brightness right after

initialization");

drivers/platform/x86/samsung-wmi.c new file +21 -0
+
+static int samsung_wmi_init(void) {
+       int ret;
+       // check whether method is present
+       if (!wmi_has_guid(WMI_GUID))
+               return -ENODEV;
+       // we're good to go
+       ret = platform_driver_register(&samsung_wmi_driver);
+       if (ret) {
+               goto err0;
+       }
+       samsung_wmi_device = platform_device_alloc(DRIVER_NAME, -1);
+       if (!samsung_wmi_device) {
+               ret = -ENOMEM;
+               goto err1;
+       }
+       ret = platform_device_add(samsung_wmi_device);
+       if (ret) {
+               goto err2;
+       }
+       ret = device_create_file(&samsung_wmi_device->dev,

&dev_attr_fan_mode);

drivers/platform/x86/samsung-wmi.c new file +10 -0
+       if (ret) {
+               goto err3;
+       }
+       ret = led_classdev_register(&samsung_wmi_device->dev,
+                       &samsung_wmi_kbd_backlight_led);
+       if (ret) {
+               goto err4;
+       }
+       if (kb_default_brightness >= 0) {
+               led_set_brightness(&samsung_wmi_kbd_backlight_led,

kb_default_brightness);

drivers/platform/x86/samsung-wmi.c line 197

Quoted 5 lines
drivers/platform/x86/samsung-wmi.c new file +5 -0
+       }
+       return 0;
+//err5:
+       led_classdev_unregister(&samsung_wmi_kbd_backlight_led);
+err4:

Instead of 'errX' please give a name related to what happen after the label (or what failed just before)

Quoted 29 lines

+ device_remove_file(&samsung_wmi_device->dev,
&dev_attr_fan_mode);

drivers/platform/x86/samsung-wmi.c new file +13 -0
+err3:
+       platform_device_del(samsung_wmi_device);
+err2:
+       platform_device_put(samsung_wmi_device);
+err1:
+       platform_driver_unregister(&samsung_wmi_driver);
+err0:
+       return ret;
+}
+
+static void samsung_wmi_exit(void) {
+       led_classdev_unregister(&samsung_wmi_kbd_backlight_led);
+       device_remove_file(&samsung_wmi_device->dev,

&dev_attr_fan_mode);

drivers/platform/x86/samsung-wmi.c new file +10 -1
+       platform_device_unregister(samsung_wmi_device);
+       platform_driver_unregister(&samsung_wmi_driver);
+}
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Gavin Li");
+MODULE_DESCRIPTION("WMI driver for some Samsung laptops.");
+MODULE_ALIAS("wmi:" WMI_GUID);
+module_init(samsung_wmi_init);
+module_exit(samsung_wmi_exit);
--

2.3.5

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe platform-driver-x86" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
Corentin Chary
http://xf.iksaif.net