From af54fb8d9b0971bb349635f791ef197f2d5fef99 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Fri, 24 Jul 2020 12:09:28 +0200 Subject: [PATCH 01/23] Basic uniwill rgb keyboard backlight control through tuxedo-cc-wmi --- src/uniwill_keyboard.h | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index d3b8be6..525ce42 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -36,6 +36,27 @@ #define UNIWILL_OSD_TOUCHPADWORKAROUND 0xFFF +union uw_ec_read_return { + u32 dword; + struct { + u8 data_low; + u8 data_high; + } bytes; +}; + +union uw_ec_write_return { + u32 dword; + struct { + u8 addr_low; + u8 addr_high; + u8 data_low; + u8 data_high; + } bytes; +}; + +extern u32 uniwill_wmi_ec_read(u8, u8, union uw_ec_read_return *); +extern u32 uniwill_wmi_ec_write(u8, u8, u8, u8, union uw_ec_write_return *); + struct tuxedo_keyboard_driver uniwill_keyboard_driver; static struct key_entry uniwill_wmi_keymap[] = { @@ -95,6 +116,32 @@ static struct notifier_block keyboard_notifier_block = { .notifier_call = keyboard_notifier_callb }; +static void write_keyb_rgb(u8 red, u8 green, u8 blue) +{ + union uw_ec_read_return reg_read_return; + union uw_ec_write_return reg_write_return; + + u32 (*__uniwill_wmi_ec_read)(u8, u8, union uw_ec_read_return *); + u32 (*__uniwill_wmi_ec_write)(u8, u8, u8, u8, union uw_ec_write_return *); + + __uniwill_wmi_ec_read = symbol_get(uniwill_wmi_ec_read); + __uniwill_wmi_ec_write = symbol_get(uniwill_wmi_ec_write); + + if (__uniwill_wmi_ec_read && __uniwill_wmi_ec_write) { + __uniwill_wmi_ec_read(0x03, 0x18, ®_read_return); + __uniwill_wmi_ec_write(0x03, 0x18, red, reg_read_return.bytes.data_high, ®_write_return); + __uniwill_wmi_ec_read(0x05, 0x18, ®_read_return); + __uniwill_wmi_ec_write(0x05, 0x18, green, reg_read_return.bytes.data_high, ®_write_return); + __uniwill_wmi_ec_read(0x08, 0x18, ®_read_return); + __uniwill_wmi_ec_write(0x08, 0x18, blue, reg_read_return.bytes.data_high, ®_write_return); + } else { + TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); + } + + if (__uniwill_wmi_ec_read) symbol_put(uniwill_wmi_ec_read); + if (__uniwill_wmi_ec_write) symbol_put(uniwill_wmi_ec_write); +} + static void uniwill_wmi_handle_event(u32 value, void *context, u32 guid_nr) { struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; @@ -127,6 +174,25 @@ static void uniwill_wmi_handle_event(u32 value, void *context, u32 guid_nr) input_report_key(current_driver->input_device, KEY_LEFTMETA, 0); input_sync(current_driver->input_device); } + + // Keyboard backlight brightness toggle + switch (code) { + case 0x3b: + write_keyb_rgb(0x00, 0x00, 0x00); + break; + case 0x3c: + write_keyb_rgb(0x40, 0x40, 0x40); + break; + case 0x3d: + write_keyb_rgb(0x80, 0x80, 0x80); + break; + case 0x3e: + write_keyb_rgb(0xa0, 0xa0, 0xa0); + break; + case 0x3f: + write_keyb_rgb(0xc8, 0xc8, 0xc8); + break; + } } kfree(obj); From 5f2db68a9bebf7c8a07e88f54d881d287550ef13 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Mon, 27 Jul 2020 11:34:29 +0200 Subject: [PATCH 02/23] Move static color definitions for use by multiple drivers --- src/clevo_keyboard.h | 24 ------------------------ src/tuxedo_keyboard_common.h | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/clevo_keyboard.h b/src/clevo_keyboard.h index 7793ba2..41002d4 100644 --- a/src/clevo_keyboard.h +++ b/src/clevo_keyboard.h @@ -105,16 +105,6 @@ static struct key_entry clevo_wmi_keymap[] = { #define BRIGHTNESS_STEP 25 -struct color_t { - u32 code; - char* name; -}; - -struct color_list_t { - uint size; - struct color_t colors[]; -}; - // Keyboard struct struct kbd_led_state_t { u8 has_extra; @@ -196,20 +186,6 @@ static struct kbd_led_state_t kbd_led_state = { .whole_kbd_color = 7 }; -static struct color_list_t color_list = { - .size = 8, - .colors = { - { .name = "BLACK", .code = 0x000000 }, // 0 - { .name = "RED", .code = 0xFF0000 }, // 1 - { .name = "GREEN", .code = 0x00FF00 }, // 2 - { .name = "BLUE", .code = 0x0000FF }, // 3 - { .name = "YELLOW", .code = 0xFFFF00 }, // 4 - { .name = "MAGENTA", .code = 0xFF00FF }, // 5 - { .name = "CYAN", .code = 0x00FFFF }, // 6 - { .name = "WHITE", .code = 0xFFFFFF }, // 7 - } -}; - static struct blinking_pattern_t blinking_patterns[] = { { .key = 0,.value = 0,.name = "CUSTOM"}, { .key = 1,.value = 0x1002a000,.name = "BREATHE"}, diff --git a/src/tuxedo_keyboard_common.h b/src/tuxedo_keyboard_common.h index 950db5f..e77457a 100644 --- a/src/tuxedo_keyboard_common.h +++ b/src/tuxedo_keyboard_common.h @@ -68,4 +68,31 @@ bool sparse_keymap_report_known_event(struct input_dev *dev, unsigned int code, return false; } +struct color_t { + u32 code; + char* name; +}; + +struct color_list_t { + uint size; + struct color_t colors[]; +}; + +/** + * Commonly used standard colors + */ +static struct color_list_t color_list = { + .size = 8, + .colors = { + { .name = "BLACK", .code = 0x000000 }, // 0 + { .name = "RED", .code = 0xFF0000 }, // 1 + { .name = "GREEN", .code = 0x00FF00 }, // 2 + { .name = "BLUE", .code = 0x0000FF }, // 3 + { .name = "YELLOW", .code = 0xFFFF00 }, // 4 + { .name = "MAGENTA", .code = 0xFF00FF }, // 5 + { .name = "CYAN", .code = 0x00FFFF }, // 6 + { .name = "WHITE", .code = 0xFFFFFF }, // 7 + } +}; + #endif \ No newline at end of file From a85ccf9a4e2939b7b5351af2dc02409013385b50 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Mon, 27 Jul 2020 16:31:38 +0200 Subject: [PATCH 03/23] Prepare uw state data structure and lookup for color strings --- src/tuxedo_keyboard_common.h | 18 ++++++++++++++++++ src/uniwill_keyboard.h | 13 ++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/tuxedo_keyboard_common.h b/src/tuxedo_keyboard_common.h index e77457a..8ba4c00 100644 --- a/src/tuxedo_keyboard_common.h +++ b/src/tuxedo_keyboard_common.h @@ -95,4 +95,22 @@ static struct color_list_t color_list = { } }; +/** + * Looks up a color in the color_list + * + * Returns found color, or -1 if string did not match + */ +static u32 color_lookup(const struct color_list_t *color_list, const char *color_name) +{ + u32 found_color = -1; + int i; + for (i = 0; i < color_list->size; ++i) { + if (strcmp(color_list->colors[i].name, color_name) == 0) { + found_color = color_list->colors[i].code; + } + } + + return found_color; +} + #endif \ No newline at end of file diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 525ce42..b9dba34 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -34,7 +34,10 @@ #define UNIWILL_OSD_RADIOON 0x01A #define UNIWILL_OSD_RADIOOFF 0x01B -#define UNIWILL_OSD_TOUCHPADWORKAROUND 0xFFF +#define UNIWILL_OSD_TOUCHPADWORKAROUND 0xFFF + +#define UNIWILL_BRIGHTNESS_DEFAULT 0xc8 * 0.75 // Note: values 0x00 - 0xc8 +#define UNIWILL_COLOR_DEFAULT 0xffffff union uw_ec_read_return { u32 dword; @@ -59,6 +62,14 @@ extern u32 uniwill_wmi_ec_write(u8, u8, u8, u8, union uw_ec_write_return *); struct tuxedo_keyboard_driver uniwill_keyboard_driver; +struct kbd_led_state_uw_t { + u8 brightness; + u32 color; +} kbd_led_state_cl = { + .brightness = UNIWILL_BRIGHTNESS_DEFAULT, + .color = UNIWILL_COLOR_DEFAULT, +}; + static struct key_entry uniwill_wmi_keymap[] = { // { KE_KEY, UNIWILL_OSD_RADIOON, { KEY_RFKILL } }, // { KE_KEY, UNIWILL_OSD_RADIOOFF, { KEY_RFKILL } }, From f90c513aadd6914ee236a818059434c6f8356686 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Tue, 28 Jul 2020 11:25:29 +0200 Subject: [PATCH 04/23] Move brightness parameter in preparation for common use --- src/clevo_keyboard.h | 30 +++--------------------------- src/tuxedo_keyboard.c | 7 ------- src/tuxedo_keyboard_common.h | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/clevo_keyboard.h b/src/clevo_keyboard.h index 41002d4..91d938a 100644 --- a/src/clevo_keyboard.h +++ b/src/clevo_keyboard.h @@ -34,19 +34,7 @@ #define KEYBOARD_BRIGHTNESS 0xF4000000 -/* All these COLOR_* macros are never used in the code, don't know why they are - here, maybe for documentation purposes. So won't delete for now */ -#define COLOR_BLACK 0x000000 -#define COLOR_RED 0xFF0000 -#define COLOR_GREEN 0x00FF00 -#define COLOR_BLUE 0x0000FF -#define COLOR_YELLOW 0xFFFF00 -#define COLOR_MAGENTA 0xFF00FF -#define COLOR_CYAN 0x00FFFF -#define COLOR_WHITE 0xFFFFFF - - -#define KB_COLOR_DEFAULT COLOR_WHITE // Default Color: White +#define KB_COLOR_DEFAULT 0xFFFFFF // White #define DEFAULT_BLINKING_PATTERN 0 // Submethod IDs for the CLEVO_GET WMI method @@ -128,7 +116,7 @@ struct blinking_pattern_t { const char *const name; }; -// Param Validators + static int blinking_pattern_id_validator(const char *value, const struct kernel_param *blinking_pattern_param); static const struct kernel_param_ops param_ops_mode_ops = { @@ -136,14 +124,6 @@ static const struct kernel_param_ops param_ops_mode_ops = { .get = param_get_int, }; -static int brightness_validator(const char *val, - const struct kernel_param *brightness_param); -static const struct kernel_param_ops param_ops_brightness_ops = { - .set = brightness_validator, - .get = param_get_int, -}; - -// Module Parameters static uint param_color_left = KB_COLOR_DEFAULT; module_param_named(color_left, param_color_left, uint, S_IRUSR); MODULE_PARM_DESC(color_left, "Color for the Left Region"); @@ -164,11 +144,6 @@ static ushort param_blinking_pattern = DEFAULT_BLINKING_PATTERN; module_param_cb(mode, ¶m_ops_mode_ops, ¶m_blinking_pattern, S_IRUSR); MODULE_PARM_DESC(mode, "Set the keyboard backlight blinking pattern"); -static ushort param_brightness = BRIGHTNESS_DEFAULT; -module_param_cb(brightness, ¶m_ops_brightness_ops, ¶m_brightness, - S_IRUSR); -MODULE_PARM_DESC(brightness, "Set the Keyboard Brightness"); - static bool param_state = true; module_param_named(state, param_state, bool, S_IRUSR); MODULE_PARM_DESC(state, @@ -714,6 +689,7 @@ static int clevo_keyboard_probe(struct platform_device *dev) set_color(REGION_RIGHT, param_color_right); set_blinking_pattern(param_blinking_pattern); + if (param_brightness > BRIGHTNESS_MAX) param_brightness = BRIGHTNESS_DEFAULT; set_brightness(param_brightness); set_enabled(param_state); diff --git a/src/tuxedo_keyboard.c b/src/tuxedo_keyboard.c index 1513cf6..4016539 100644 --- a/src/tuxedo_keyboard.c +++ b/src/tuxedo_keyboard.c @@ -20,13 +20,6 @@ #define pr_fmt(fmt) "tuxedo_keyboard" ": " fmt -#include -#include -#include -#include -#include -#include -#include #include "tuxedo_keyboard_common.h" #include "clevo_keyboard.h" #include "uniwill_keyboard.h" diff --git a/src/tuxedo_keyboard_common.h b/src/tuxedo_keyboard_common.h index 8ba4c00..d19729d 100644 --- a/src/tuxedo_keyboard_common.h +++ b/src/tuxedo_keyboard_common.h @@ -21,6 +21,11 @@ #define TUXEDO_KEYBOARD_COMMON_H #include +#include +#include +#include +#include +#include #include /* :::: Module specific Constants and simple Macros :::: */ @@ -113,4 +118,18 @@ static u32 color_lookup(const struct color_list_t *color_list, const char *color return found_color; } +// Common parameters + +static int brightness_validator(const char *val, + const struct kernel_param *brightness_param); +static const struct kernel_param_ops param_ops_brightness_ops = { + .set = brightness_validator, + .get = param_get_int, +}; + +static ushort param_brightness = 0xffff; // Default unset value (higher than max) +module_param_cb(brightness, ¶m_ops_brightness_ops, ¶m_brightness, + S_IRUSR); +MODULE_PARM_DESC(brightness, "Set the Keyboard Brightness"); + #endif \ No newline at end of file From 06492b22ef49d7c8201197ad69ad33c6cdc89d4b Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Tue, 28 Jul 2020 13:25:08 +0200 Subject: [PATCH 05/23] Color and brightness param + scaling of current set color --- src/tuxedo_keyboard_common.h | 5 ++++ src/uniwill_keyboard.h | 45 ++++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/tuxedo_keyboard_common.h b/src/tuxedo_keyboard_common.h index d19729d..f613f39 100644 --- a/src/tuxedo_keyboard_common.h +++ b/src/tuxedo_keyboard_common.h @@ -132,4 +132,9 @@ module_param_cb(brightness, ¶m_ops_brightness_ops, ¶m_brightness, S_IRUSR); MODULE_PARM_DESC(brightness, "Set the Keyboard Brightness"); +#define COLOR_STRING_LEN 20 +static char param_color[COLOR_STRING_LEN]; +module_param_string(color, param_color, COLOR_STRING_LEN, S_IRUSR); +MODULE_PARM_DESC(color, "Preset color for the keyboard backlight as string"); + #endif \ No newline at end of file diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index b9dba34..f637ef4 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -36,8 +36,11 @@ #define UNIWILL_OSD_TOUCHPADWORKAROUND 0xFFF -#define UNIWILL_BRIGHTNESS_DEFAULT 0xc8 * 0.75 // Note: values 0x00 - 0xc8 +#define UNIWILL_BRIGHTNESS_MIN 0x00 +#define UNIWILL_BRIGHTNESS_MAX 0xc8 +#define UNIWILL_BRIGHTNESS_DEFAULT UNIWILL_BRIGHTNESS_MAX * 0.75 #define UNIWILL_COLOR_DEFAULT 0xffffff +#define UNIWILL_COLOR_STRING_DEFAULT "white" union uw_ec_read_return { u32 dword; @@ -65,9 +68,11 @@ struct tuxedo_keyboard_driver uniwill_keyboard_driver; struct kbd_led_state_uw_t { u8 brightness; u32 color; -} kbd_led_state_cl = { + char color_string[COLOR_STRING_LEN]; +} kbd_led_state_uw = { .brightness = UNIWILL_BRIGHTNESS_DEFAULT, .color = UNIWILL_COLOR_DEFAULT, + .color_string = "white" }; static struct key_entry uniwill_wmi_keymap[] = { @@ -153,6 +158,21 @@ static void write_keyb_rgb(u8 red, u8 green, u8 blue) if (__uniwill_wmi_ec_write) symbol_put(uniwill_wmi_ec_write); } +static void uniwill_write_kdb_led_state(void) { + // Get single colors from state + u32 color_red = (kbd_led_state_uw.color >> 0x10) && 0xff; + u32 color_green = (kbd_led_state_uw.color >> 0x08) && 0xff; + u32 color_blue = (kbd_led_state_uw.color >> 0x00) && 0xff; + + // Scale the respective values + u32 brightness_percentage = (kbd_led_state_uw.brightness * 100) / UNIWILL_BRIGHTNESS_MAX; + color_red = (color_red * brightness_percentage) / 100; + color_green = (color_green * brightness_percentage) / 100; + color_blue = (color_blue * brightness_percentage) / 100; + + write_keyb_rgb(color_red, color_green, color_blue); +} + static void uniwill_wmi_handle_event(u32 value, void *context, u32 guid_nr) { struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; @@ -189,21 +209,23 @@ static void uniwill_wmi_handle_event(u32 value, void *context, u32 guid_nr) // Keyboard backlight brightness toggle switch (code) { case 0x3b: - write_keyb_rgb(0x00, 0x00, 0x00); + kbd_led_state_uw.brightness = 0x00; break; case 0x3c: - write_keyb_rgb(0x40, 0x40, 0x40); + kbd_led_state_uw.brightness = 0x10; break; case 0x3d: - write_keyb_rgb(0x80, 0x80, 0x80); + kbd_led_state_uw.brightness = 0x30; break; case 0x3e: - write_keyb_rgb(0xa0, 0xa0, 0xa0); + kbd_led_state_uw.brightness = 0x80; break; case 0x3f: - write_keyb_rgb(0xc8, 0xc8, 0xc8); + kbd_led_state_uw.brightness = 0xc8; break; } + + uniwill_write_kdb_led_state(); } kfree(obj); @@ -263,6 +285,15 @@ static int uniwill_keyboard_probe(struct platform_device *dev) status = register_keyboard_notifier(&keyboard_notifier_block); + // Initialize keyboard backlight driver state according to parameters + if (param_brightness > UNIWILL_BRIGHTNESS_MAX) param_brightness = UNIWILL_BRIGHTNESS_DEFAULT; + kbd_led_state_uw.brightness = param_brightness; + if (color_lookup(&color_list, param_color) != -1) kbd_led_state_uw.color = color_lookup(&color_list, param_color); + else kbd_led_state_uw.color = UNIWILL_COLOR_DEFAULT; + + // Update keyboard according to the current state + uniwill_write_kdb_led_state(); + return 0; err_remove_notifiers: From f1ffca50c457d84f7ec432392285d68f19ec83da Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Tue, 28 Jul 2020 14:30:08 +0200 Subject: [PATCH 06/23] Logical fixes + color scaling fix + brightness level tweaks --- src/uniwill_keyboard.h | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index f637ef4..b69d3e4 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -66,7 +66,7 @@ extern u32 uniwill_wmi_ec_write(u8, u8, u8, u8, union uw_ec_write_return *); struct tuxedo_keyboard_driver uniwill_keyboard_driver; struct kbd_led_state_uw_t { - u8 brightness; + u32 brightness; u32 color; char color_string[COLOR_STRING_LEN]; } kbd_led_state_uw = { @@ -160,12 +160,18 @@ static void write_keyb_rgb(u8 red, u8 green, u8 blue) static void uniwill_write_kdb_led_state(void) { // Get single colors from state - u32 color_red = (kbd_led_state_uw.color >> 0x10) && 0xff; - u32 color_green = (kbd_led_state_uw.color >> 0x08) && 0xff; - u32 color_blue = (kbd_led_state_uw.color >> 0x00) && 0xff; + u32 color_red = ((kbd_led_state_uw.color >> 0x10) & 0xff); + u32 color_green = (kbd_led_state_uw.color >> 0x08) & 0xff; + u32 color_blue = (kbd_led_state_uw.color >> 0x00) & 0xff; + + u32 brightness_percentage = (kbd_led_state_uw.brightness * 100) / UNIWILL_BRIGHTNESS_MAX; + + // Scale color values + color_red = (color_red * 0xc8) / 0xff; + color_green = (color_green * 0xc8) / 0xff; + color_blue = (color_blue * 0xc8) / 0xff; // Scale the respective values - u32 brightness_percentage = (kbd_led_state_uw.brightness * 100) / UNIWILL_BRIGHTNESS_MAX; color_red = (color_red * brightness_percentage) / 100; color_green = (color_green * brightness_percentage) / 100; color_blue = (color_blue * brightness_percentage) / 100; @@ -210,22 +216,25 @@ static void uniwill_wmi_handle_event(u32 value, void *context, u32 guid_nr) switch (code) { case 0x3b: kbd_led_state_uw.brightness = 0x00; + uniwill_write_kdb_led_state(); break; case 0x3c: - kbd_led_state_uw.brightness = 0x10; + kbd_led_state_uw.brightness = 0x20; + uniwill_write_kdb_led_state(); break; case 0x3d: - kbd_led_state_uw.brightness = 0x30; + kbd_led_state_uw.brightness = 0x50; + uniwill_write_kdb_led_state(); break; case 0x3e: kbd_led_state_uw.brightness = 0x80; + uniwill_write_kdb_led_state(); break; case 0x3f: kbd_led_state_uw.brightness = 0xc8; + uniwill_write_kdb_led_state(); break; } - - uniwill_write_kdb_led_state(); } kfree(obj); From 17595676b5e76babe29ea93460d39ca99ee02064 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Thu, 30 Jul 2020 10:20:56 +0200 Subject: [PATCH 07/23] Change order of reads and writes for uw keyb backlight --- src/uniwill_keyboard.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index b69d3e4..27670f8 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -135,6 +135,7 @@ static struct notifier_block keyboard_notifier_block = { static void write_keyb_rgb(u8 red, u8 green, u8 blue) { union uw_ec_read_return reg_read_return; + u8 high_byte_red_reg, high_byte_green_reg, high_byte_blue_reg; union uw_ec_write_return reg_write_return; u32 (*__uniwill_wmi_ec_read)(u8, u8, union uw_ec_read_return *); @@ -144,12 +145,22 @@ static void write_keyb_rgb(u8 red, u8 green, u8 blue) __uniwill_wmi_ec_write = symbol_get(uniwill_wmi_ec_write); if (__uniwill_wmi_ec_read && __uniwill_wmi_ec_write) { + // Read high bytes to put the same values back __uniwill_wmi_ec_read(0x03, 0x18, ®_read_return); - __uniwill_wmi_ec_write(0x03, 0x18, red, reg_read_return.bytes.data_high, ®_write_return); + high_byte_red_reg = reg_read_return.bytes.data_high; __uniwill_wmi_ec_read(0x05, 0x18, ®_read_return); - __uniwill_wmi_ec_write(0x05, 0x18, green, reg_read_return.bytes.data_high, ®_write_return); + high_byte_green_reg = reg_read_return.bytes.data_high; __uniwill_wmi_ec_read(0x08, 0x18, ®_read_return); - __uniwill_wmi_ec_write(0x08, 0x18, blue, reg_read_return.bytes.data_high, ®_write_return); + high_byte_blue_reg = reg_read_return.bytes.data_high; + + // Write the colors + // Note: Writing is done separately because of less delay than by reading + // thereby making the transition smoother + //__uniwill_wmi_ec_write(0x01, 0x18, 0x01, 0x00, ®_write_return); + __uniwill_wmi_ec_write(0x03, 0x18, red, high_byte_red_reg, ®_write_return); + __uniwill_wmi_ec_write(0x05, 0x18, green, high_byte_green_reg, ®_write_return); + __uniwill_wmi_ec_write(0x08, 0x18, blue, high_byte_blue_reg, ®_write_return); + //__uniwill_wmi_ec_write(0x01, 0x18, 0xc8, 0x00, ®_write_return); } else { TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); } From afbe4cde64e94ff619b12a2f1914bd6074759d29 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Tue, 4 Aug 2020 11:40:17 +0200 Subject: [PATCH 08/23] Change color lookup return value --- src/tuxedo_keyboard_common.h | 4 ++-- src/uniwill_keyboard.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tuxedo_keyboard_common.h b/src/tuxedo_keyboard_common.h index f613f39..3a52b2a 100644 --- a/src/tuxedo_keyboard_common.h +++ b/src/tuxedo_keyboard_common.h @@ -103,11 +103,11 @@ static struct color_list_t color_list = { /** * Looks up a color in the color_list * - * Returns found color, or -1 if string did not match + * Returns found color value, or 0xffffffff if string did not match */ static u32 color_lookup(const struct color_list_t *color_list, const char *color_name) { - u32 found_color = -1; + u32 found_color = 0xffffffff; int i; for (i = 0; i < color_list->size; ++i) { if (strcmp(color_list->colors[i].name, color_name) == 0) { diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 27670f8..77d8e25 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -308,7 +308,7 @@ static int uniwill_keyboard_probe(struct platform_device *dev) // Initialize keyboard backlight driver state according to parameters if (param_brightness > UNIWILL_BRIGHTNESS_MAX) param_brightness = UNIWILL_BRIGHTNESS_DEFAULT; kbd_led_state_uw.brightness = param_brightness; - if (color_lookup(&color_list, param_color) != -1) kbd_led_state_uw.color = color_lookup(&color_list, param_color); + if (color_lookup(&color_list, param_color) <= (u32) 0xffffff) kbd_led_state_uw.color = color_lookup(&color_list, param_color); else kbd_led_state_uw.color = UNIWILL_COLOR_DEFAULT; // Update keyboard according to the current state From d29b1352c7cdce903c2665253ce47cfcab4776a6 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Tue, 4 Aug 2020 11:47:51 +0200 Subject: [PATCH 09/23] Fix typo, rename uw kbd methods --- src/uniwill_keyboard.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 77d8e25..86a767b 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -132,7 +132,7 @@ static struct notifier_block keyboard_notifier_block = { .notifier_call = keyboard_notifier_callb }; -static void write_keyb_rgb(u8 red, u8 green, u8 blue) +static void uniwill_write_kbd_bl_rgb(u8 red, u8 green, u8 blue) { union uw_ec_read_return reg_read_return; u8 high_byte_red_reg, high_byte_green_reg, high_byte_blue_reg; @@ -169,7 +169,7 @@ static void write_keyb_rgb(u8 red, u8 green, u8 blue) if (__uniwill_wmi_ec_write) symbol_put(uniwill_wmi_ec_write); } -static void uniwill_write_kdb_led_state(void) { +static void uniwill_write_kbd_bl_state(void) { // Get single colors from state u32 color_red = ((kbd_led_state_uw.color >> 0x10) & 0xff); u32 color_green = (kbd_led_state_uw.color >> 0x08) & 0xff; @@ -187,7 +187,7 @@ static void uniwill_write_kdb_led_state(void) { color_green = (color_green * brightness_percentage) / 100; color_blue = (color_blue * brightness_percentage) / 100; - write_keyb_rgb(color_red, color_green, color_blue); + uniwill_write_kbd_bl_rgb(color_red, color_green, color_blue); } static void uniwill_wmi_handle_event(u32 value, void *context, u32 guid_nr) @@ -227,23 +227,23 @@ static void uniwill_wmi_handle_event(u32 value, void *context, u32 guid_nr) switch (code) { case 0x3b: kbd_led_state_uw.brightness = 0x00; - uniwill_write_kdb_led_state(); + uniwill_write_kbd_bl_state(); break; case 0x3c: kbd_led_state_uw.brightness = 0x20; - uniwill_write_kdb_led_state(); + uniwill_write_kbd_bl_state(); break; case 0x3d: kbd_led_state_uw.brightness = 0x50; - uniwill_write_kdb_led_state(); + uniwill_write_kbd_bl_state(); break; case 0x3e: kbd_led_state_uw.brightness = 0x80; - uniwill_write_kdb_led_state(); + uniwill_write_kbd_bl_state(); break; case 0x3f: kbd_led_state_uw.brightness = 0xc8; - uniwill_write_kdb_led_state(); + uniwill_write_kbd_bl_state(); break; } } @@ -312,7 +312,7 @@ static int uniwill_keyboard_probe(struct platform_device *dev) else kbd_led_state_uw.color = UNIWILL_COLOR_DEFAULT; // Update keyboard according to the current state - uniwill_write_kdb_led_state(); + uniwill_write_kbd_bl_state(); return 0; From f666df45696c7385333722959a88837a8b910202 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Tue, 4 Aug 2020 11:48:46 +0200 Subject: [PATCH 10/23] Write uw kbd bl state on resume --- src/uniwill_keyboard.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 86a767b..18b54a7 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -341,6 +341,7 @@ static int uniwill_keyboard_suspend(struct platform_device *dev, pm_message_t st static int uniwill_keyboard_resume(struct platform_device *dev) { + uniwill_write_kbd_bl_state(); return 0; } From 97c6da3e5b355d071230138a5aa37a0c0c8f2de9 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Wed, 5 Aug 2020 12:32:10 +0200 Subject: [PATCH 11/23] Add uw kbd bl enable set/get methods --- src/uniwill_keyboard.h | 51 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 18b54a7..e130fab 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -132,6 +132,55 @@ static struct notifier_block keyboard_notifier_block = { .notifier_call = keyboard_notifier_callb }; +static u8 uniwill_read_kbd_bl_enable(void) +{ + union uw_ec_read_return reg_read_return; + + u32 (*__uniwill_wmi_ec_read)(u8, u8, union uw_ec_read_return *); + + u8 enabled = 0xff; + + __uniwill_wmi_ec_read = symbol_get(uniwill_wmi_ec_read); + + if (__uniwill_wmi_ec_read) { + __uniwill_wmi_ec_read(0x07, 0x8c, ®_read_return); + enabled = (reg_read_return.bytes.data_low >> 1) & 0x01; + } else { + TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); + } + + if (__uniwill_wmi_ec_read) symbol_put(uniwill_wmi_ec_read); + + return enabled; +} + +static void uniwill_write_kbd_bl_enable(u8 enable) +{ + union uw_ec_read_return reg_read_return; + union uw_ec_write_return reg_write_return; + + u32 (*__uniwill_wmi_ec_read)(u8, u8, union uw_ec_read_return *); + u32 (*__uniwill_wmi_ec_write)(u8, u8, u8, u8, union uw_ec_write_return *); + + u8 write_value = 0; + enable = enable & 0x01; + + __uniwill_wmi_ec_read = symbol_get(uniwill_wmi_ec_read); + __uniwill_wmi_ec_write = symbol_get(uniwill_wmi_ec_write); + + if (__uniwill_wmi_ec_read && __uniwill_wmi_ec_write) { + __uniwill_wmi_ec_read(0x07, 0x8c, ®_read_return); + write_value = reg_read_return.bytes.data_low & (0 << 1); + write_value |= (enable << 1); + __uniwill_wmi_ec_write(0x07, 0x8c, write_value, reg_read_return.bytes.data_high, ®_write_return); + } else { + TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); + } + + if (__uniwill_wmi_ec_read) symbol_put(uniwill_wmi_ec_read); + if (__uniwill_wmi_ec_write) symbol_put(uniwill_wmi_ec_write); +} + static void uniwill_write_kbd_bl_rgb(u8 red, u8 green, u8 blue) { union uw_ec_read_return reg_read_return; @@ -156,11 +205,9 @@ static void uniwill_write_kbd_bl_rgb(u8 red, u8 green, u8 blue) // Write the colors // Note: Writing is done separately because of less delay than by reading // thereby making the transition smoother - //__uniwill_wmi_ec_write(0x01, 0x18, 0x01, 0x00, ®_write_return); __uniwill_wmi_ec_write(0x03, 0x18, red, high_byte_red_reg, ®_write_return); __uniwill_wmi_ec_write(0x05, 0x18, green, high_byte_green_reg, ®_write_return); __uniwill_wmi_ec_write(0x08, 0x18, blue, high_byte_blue_reg, ®_write_return); - //__uniwill_wmi_ec_write(0x01, 0x18, 0xc8, 0x00, ®_write_return); } else { TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); } From f98573aa213886d59a01947d69c2e828b79d611b Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Wed, 5 Aug 2020 14:55:49 +0200 Subject: [PATCH 12/23] Fix uw kbd bl enable and set/restore enabled state --- src/uniwill_keyboard.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index e130fab..f2d2d57 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -87,6 +87,8 @@ static struct key_entry uniwill_wmi_keymap[] = { { KE_END, 0 } }; +static u8 uniwill_kbd_bl_enable_state_on_start; + static void key_event_work(struct work_struct *work) { sparse_keymap_report_known_event( @@ -132,7 +134,7 @@ static struct notifier_block keyboard_notifier_block = { .notifier_call = keyboard_notifier_callb }; -static u8 uniwill_read_kbd_bl_enable(void) +static u8 uniwill_read_kbd_bl_enabled(void) { union uw_ec_read_return reg_read_return; @@ -143,8 +145,9 @@ static u8 uniwill_read_kbd_bl_enable(void) __uniwill_wmi_ec_read = symbol_get(uniwill_wmi_ec_read); if (__uniwill_wmi_ec_read) { - __uniwill_wmi_ec_read(0x07, 0x8c, ®_read_return); + __uniwill_wmi_ec_read(0x8c, 0x07, ®_read_return); enabled = (reg_read_return.bytes.data_low >> 1) & 0x01; + enabled = !enabled; } else { TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); } @@ -169,10 +172,10 @@ static void uniwill_write_kbd_bl_enable(u8 enable) __uniwill_wmi_ec_write = symbol_get(uniwill_wmi_ec_write); if (__uniwill_wmi_ec_read && __uniwill_wmi_ec_write) { - __uniwill_wmi_ec_read(0x07, 0x8c, ®_read_return); - write_value = reg_read_return.bytes.data_low & (0 << 1); - write_value |= (enable << 1); - __uniwill_wmi_ec_write(0x07, 0x8c, write_value, reg_read_return.bytes.data_high, ®_write_return); + __uniwill_wmi_ec_read(0x8c, 0x07, ®_read_return); + write_value = reg_read_return.bytes.data_low & ~(1 << 1); + write_value |= (!enable << 1); + __uniwill_wmi_ec_write(0x8c, 0x07, write_value, reg_read_return.bytes.data_high, ®_write_return); } else { TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); } @@ -352,12 +355,18 @@ static int uniwill_keyboard_probe(struct platform_device *dev) status = register_keyboard_notifier(&keyboard_notifier_block); + // Save previous enable state + uniwill_kbd_bl_enable_state_on_start = uniwill_read_kbd_bl_enabled(); + // Initialize keyboard backlight driver state according to parameters if (param_brightness > UNIWILL_BRIGHTNESS_MAX) param_brightness = UNIWILL_BRIGHTNESS_DEFAULT; kbd_led_state_uw.brightness = param_brightness; if (color_lookup(&color_list, param_color) <= (u32) 0xffffff) kbd_led_state_uw.color = color_lookup(&color_list, param_color); else kbd_led_state_uw.color = UNIWILL_COLOR_DEFAULT; + // Enable keyboard backlight + uniwill_write_kbd_bl_enable(1); + // Update keyboard according to the current state uniwill_write_kbd_bl_state(); @@ -373,6 +382,11 @@ err_remove_notifiers: static int uniwill_keyboard_remove(struct platform_device *dev) { + // Restore previous backlight enable state + if (uniwill_kbd_bl_enable_state_on_start != 0xff) { + uniwill_write_kbd_bl_enable(uniwill_kbd_bl_enable_state_on_start); + } + unregister_keyboard_notifier(&keyboard_notifier_block); wmi_remove_notify_handler(UNIWILL_WMI_EVENT_GUID_0); wmi_remove_notify_handler(UNIWILL_WMI_EVENT_GUID_1); @@ -383,11 +397,13 @@ static int uniwill_keyboard_remove(struct platform_device *dev) static int uniwill_keyboard_suspend(struct platform_device *dev, pm_message_t state) { + uniwill_write_kbd_bl_enable(0); return 0; } static int uniwill_keyboard_resume(struct platform_device *dev) { + uniwill_write_kbd_bl_enable(1); uniwill_write_kbd_bl_state(); return 0; } From 857d5f6beac2457cb43eb277ca7616b23831518a Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Wed, 5 Aug 2020 17:00:08 +0200 Subject: [PATCH 13/23] Fix uw kbd bl default values and keep disabled during init --- src/uniwill_keyboard.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index f2d2d57..33c0d65 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -40,7 +40,7 @@ #define UNIWILL_BRIGHTNESS_MAX 0xc8 #define UNIWILL_BRIGHTNESS_DEFAULT UNIWILL_BRIGHTNESS_MAX * 0.75 #define UNIWILL_COLOR_DEFAULT 0xffffff -#define UNIWILL_COLOR_STRING_DEFAULT "white" +#define UNIWILL_COLOR_STRING_DEFAULT "WHITE" union uw_ec_read_return { u32 dword; @@ -72,7 +72,7 @@ struct kbd_led_state_uw_t { } kbd_led_state_uw = { .brightness = UNIWILL_BRIGHTNESS_DEFAULT, .color = UNIWILL_COLOR_DEFAULT, - .color_string = "white" + .color_string = UNIWILL_COLOR_STRING_DEFAULT }; static struct key_entry uniwill_wmi_keymap[] = { @@ -358,18 +358,21 @@ static int uniwill_keyboard_probe(struct platform_device *dev) // Save previous enable state uniwill_kbd_bl_enable_state_on_start = uniwill_read_kbd_bl_enabled(); + // Disable backlight while initializing + uniwill_write_kbd_bl_enable(0); + // Initialize keyboard backlight driver state according to parameters if (param_brightness > UNIWILL_BRIGHTNESS_MAX) param_brightness = UNIWILL_BRIGHTNESS_DEFAULT; kbd_led_state_uw.brightness = param_brightness; if (color_lookup(&color_list, param_color) <= (u32) 0xffffff) kbd_led_state_uw.color = color_lookup(&color_list, param_color); else kbd_led_state_uw.color = UNIWILL_COLOR_DEFAULT; - // Enable keyboard backlight - uniwill_write_kbd_bl_enable(1); - // Update keyboard according to the current state uniwill_write_kbd_bl_state(); + // Enable keyboard backlight + uniwill_write_kbd_bl_enable(1); + return 0; err_remove_notifiers: @@ -403,8 +406,8 @@ static int uniwill_keyboard_suspend(struct platform_device *dev, pm_message_t st static int uniwill_keyboard_resume(struct platform_device *dev) { - uniwill_write_kbd_bl_enable(1); uniwill_write_kbd_bl_state(); + uniwill_write_kbd_bl_enable(1); return 0; } From 4a272bb7151447c1e921cb04e7ea04fb6272ddd0 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Wed, 12 Aug 2020 10:41:58 +0200 Subject: [PATCH 14/23] Add soft dependency on tuxedo-cc-wmi --- src/tuxedo_keyboard.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tuxedo_keyboard.c b/src/tuxedo_keyboard.c index 4016539..0ea1433 100644 --- a/src/tuxedo_keyboard.c +++ b/src/tuxedo_keyboard.c @@ -36,6 +36,8 @@ MODULE_ALIAS("wmi:" UNIWILL_WMI_EVENT_GUID_0); MODULE_ALIAS("wmi:" UNIWILL_WMI_EVENT_GUID_1); MODULE_ALIAS("wmi:" UNIWILL_WMI_EVENT_GUID_2); +MODULE_SOFTDEP("pre: tuxedo-cc-wmi"); + static struct tuxedo_keyboard_driver *driver_list[] = { &clevo_keyboard_driver, &uniwill_keyboard_driver From edb5c08ad314a7909681c6e3cab7e1f874447eea Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Mon, 17 Aug 2020 13:02:44 +0200 Subject: [PATCH 15/23] Extend uw event catching (debug) --- src/uniwill_keyboard.h | 80 ++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 33c0d65..bf283ce 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -255,46 +255,50 @@ static void uniwill_wmi_handle_event(u32 value, void *context, u32 guid_nr) } obj = (union acpi_object *) response.pointer; - if (obj && obj->type == ACPI_TYPE_INTEGER) { - code = obj->integer.value; - if (!sparse_keymap_report_known_event(current_driver->input_device, code, 1, true)) { - TUXEDO_DEBUG("[Ev %d] Unknown key - %d (%0#6x)\n", guid_nr, code, code); - } + if (obj) { + if (obj->type == ACPI_TYPE_INTEGER) { + code = obj->integer.value; + if (!sparse_keymap_report_known_event(current_driver->input_device, code, 1, true)) { + TUXEDO_DEBUG("[Ev %d] Unknown key - %d (%0#6x)\n", guid_nr, code, code); + } - // Special key combination when mode change key is pressed - if (code == 0xb0) { - input_report_key(current_driver->input_device, KEY_LEFTMETA, 1); - input_report_key(current_driver->input_device, KEY_LEFTALT, 1); - input_report_key(current_driver->input_device, KEY_F6, 1); - input_sync(current_driver->input_device); - input_report_key(current_driver->input_device, KEY_F6, 0); - input_report_key(current_driver->input_device, KEY_LEFTALT, 0); - input_report_key(current_driver->input_device, KEY_LEFTMETA, 0); - input_sync(current_driver->input_device); - } + // Special key combination when mode change key is pressed + if (code == 0xb0) { + input_report_key(current_driver->input_device, KEY_LEFTMETA, 1); + input_report_key(current_driver->input_device, KEY_LEFTALT, 1); + input_report_key(current_driver->input_device, KEY_F6, 1); + input_sync(current_driver->input_device); + input_report_key(current_driver->input_device, KEY_F6, 0); + input_report_key(current_driver->input_device, KEY_LEFTALT, 0); + input_report_key(current_driver->input_device, KEY_LEFTMETA, 0); + input_sync(current_driver->input_device); + } - // Keyboard backlight brightness toggle - switch (code) { - case 0x3b: - kbd_led_state_uw.brightness = 0x00; - uniwill_write_kbd_bl_state(); - break; - case 0x3c: - kbd_led_state_uw.brightness = 0x20; - uniwill_write_kbd_bl_state(); - break; - case 0x3d: - kbd_led_state_uw.brightness = 0x50; - uniwill_write_kbd_bl_state(); - break; - case 0x3e: - kbd_led_state_uw.brightness = 0x80; - uniwill_write_kbd_bl_state(); - break; - case 0x3f: - kbd_led_state_uw.brightness = 0xc8; - uniwill_write_kbd_bl_state(); - break; + // Keyboard backlight brightness toggle + switch (code) { + case 0x3b: + kbd_led_state_uw.brightness = 0x00; + uniwill_write_kbd_bl_state(); + break; + case 0x3c: + kbd_led_state_uw.brightness = 0x20; + uniwill_write_kbd_bl_state(); + break; + case 0x3d: + kbd_led_state_uw.brightness = 0x50; + uniwill_write_kbd_bl_state(); + break; + case 0x3e: + kbd_led_state_uw.brightness = 0x80; + uniwill_write_kbd_bl_state(); + break; + case 0x3f: + kbd_led_state_uw.brightness = 0xc8; + uniwill_write_kbd_bl_state(); + break; + } + } else { + TUXEDO_DEBUG("[Ev %d] Unknown event type - %d (%0#6x)\n", guid_nr, obj->type, obj->type); } } From d976cddc81ba2afd478c26384594ec92469845ef Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Mon, 17 Aug 2020 13:07:55 +0200 Subject: [PATCH 16/23] Remove reads used to write previous high bytes --- src/uniwill_keyboard.h | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index bf283ce..4ae8cfd 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -186,8 +186,6 @@ static void uniwill_write_kbd_bl_enable(u8 enable) static void uniwill_write_kbd_bl_rgb(u8 red, u8 green, u8 blue) { - union uw_ec_read_return reg_read_return; - u8 high_byte_red_reg, high_byte_green_reg, high_byte_blue_reg; union uw_ec_write_return reg_write_return; u32 (*__uniwill_wmi_ec_read)(u8, u8, union uw_ec_read_return *); @@ -197,20 +195,13 @@ static void uniwill_write_kbd_bl_rgb(u8 red, u8 green, u8 blue) __uniwill_wmi_ec_write = symbol_get(uniwill_wmi_ec_write); if (__uniwill_wmi_ec_read && __uniwill_wmi_ec_write) { - // Read high bytes to put the same values back - __uniwill_wmi_ec_read(0x03, 0x18, ®_read_return); - high_byte_red_reg = reg_read_return.bytes.data_high; - __uniwill_wmi_ec_read(0x05, 0x18, ®_read_return); - high_byte_green_reg = reg_read_return.bytes.data_high; - __uniwill_wmi_ec_read(0x08, 0x18, ®_read_return); - high_byte_blue_reg = reg_read_return.bytes.data_high; - // Write the colors - // Note: Writing is done separately because of less delay than by reading - // thereby making the transition smoother - __uniwill_wmi_ec_write(0x03, 0x18, red, high_byte_red_reg, ®_write_return); - __uniwill_wmi_ec_write(0x05, 0x18, green, high_byte_green_reg, ®_write_return); - __uniwill_wmi_ec_write(0x08, 0x18, blue, high_byte_blue_reg, ®_write_return); + if (red > 0xc8) red = 0xc8; + if (green > 0xc8) green = 0xc8; + if (blue > 0xc8) blue = 0xc8; + __uniwill_wmi_ec_write(0x03, 0x18, red, 0x00, ®_write_return); + __uniwill_wmi_ec_write(0x05, 0x18, green, 0x00, ®_write_return); + __uniwill_wmi_ec_write(0x08, 0x18, blue, 0x00, ®_write_return); } else { TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); } From 9fc2f16482061a9ce62bbfb75f18b4631b6f6ee8 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Wed, 19 Aug 2020 13:27:06 +0200 Subject: [PATCH 17/23] Add device check to uw kbd bl driver --- src/uniwill_keyboard.h | 75 +++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 4ae8cfd..bd5cb5e 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -75,6 +75,9 @@ struct kbd_led_state_uw_t { .color_string = UNIWILL_COLOR_STRING_DEFAULT }; +static u8 uniwill_kbd_bl_enable_state_on_start; +static bool uniwill_kbd_bl_type_rgb_single_color = true; + static struct key_entry uniwill_wmi_keymap[] = { // { KE_KEY, UNIWILL_OSD_RADIOON, { KEY_RFKILL } }, // { KE_KEY, UNIWILL_OSD_RADIOOFF, { KEY_RFKILL } }, @@ -87,8 +90,6 @@ static struct key_entry uniwill_wmi_keymap[] = { { KE_END, 0 } }; -static u8 uniwill_kbd_bl_enable_state_on_start; - static void key_event_work(struct work_struct *work) { sparse_keymap_report_known_event( @@ -266,27 +267,29 @@ static void uniwill_wmi_handle_event(u32 value, void *context, u32 guid_nr) } // Keyboard backlight brightness toggle - switch (code) { - case 0x3b: - kbd_led_state_uw.brightness = 0x00; - uniwill_write_kbd_bl_state(); - break; - case 0x3c: - kbd_led_state_uw.brightness = 0x20; - uniwill_write_kbd_bl_state(); - break; - case 0x3d: - kbd_led_state_uw.brightness = 0x50; - uniwill_write_kbd_bl_state(); - break; - case 0x3e: - kbd_led_state_uw.brightness = 0x80; - uniwill_write_kbd_bl_state(); - break; - case 0x3f: - kbd_led_state_uw.brightness = 0xc8; - uniwill_write_kbd_bl_state(); - break; + if (uniwill_kbd_bl_type_rgb_single_color) { + switch (code) { + case 0x3b: + kbd_led_state_uw.brightness = 0x00; + uniwill_write_kbd_bl_state(); + break; + case 0x3c: + kbd_led_state_uw.brightness = 0x20; + uniwill_write_kbd_bl_state(); + break; + case 0x3d: + kbd_led_state_uw.brightness = 0x50; + uniwill_write_kbd_bl_state(); + break; + case 0x3e: + kbd_led_state_uw.brightness = 0x80; + uniwill_write_kbd_bl_state(); + break; + case 0x3f: + kbd_led_state_uw.brightness = 0xc8; + uniwill_write_kbd_bl_state(); + break; + } } } else { TUXEDO_DEBUG("[Ev %d] Unknown event type - %d (%0#6x)\n", guid_nr, obj->type, obj->type); @@ -350,20 +353,24 @@ static int uniwill_keyboard_probe(struct platform_device *dev) status = register_keyboard_notifier(&keyboard_notifier_block); + uniwill_kbd_bl_type_rgb_single_color = dmi_match(DMI_BOARD_NAME, "Polaris15I01"); + // Save previous enable state uniwill_kbd_bl_enable_state_on_start = uniwill_read_kbd_bl_enabled(); - // Disable backlight while initializing - uniwill_write_kbd_bl_enable(0); + if (uniwill_kbd_bl_type_rgb_single_color) { + // Disable backlight while initializing + uniwill_write_kbd_bl_enable(0); - // Initialize keyboard backlight driver state according to parameters - if (param_brightness > UNIWILL_BRIGHTNESS_MAX) param_brightness = UNIWILL_BRIGHTNESS_DEFAULT; - kbd_led_state_uw.brightness = param_brightness; - if (color_lookup(&color_list, param_color) <= (u32) 0xffffff) kbd_led_state_uw.color = color_lookup(&color_list, param_color); - else kbd_led_state_uw.color = UNIWILL_COLOR_DEFAULT; + // Initialize keyboard backlight driver state according to parameters + if (param_brightness > UNIWILL_BRIGHTNESS_MAX) param_brightness = UNIWILL_BRIGHTNESS_DEFAULT; + kbd_led_state_uw.brightness = param_brightness; + if (color_lookup(&color_list, param_color) <= (u32) 0xffffff) kbd_led_state_uw.color = color_lookup(&color_list, param_color); + else kbd_led_state_uw.color = UNIWILL_COLOR_DEFAULT; - // Update keyboard according to the current state - uniwill_write_kbd_bl_state(); + // Update keyboard backlight according to the current state + uniwill_write_kbd_bl_state(); + } // Enable keyboard backlight uniwill_write_kbd_bl_enable(1); @@ -401,7 +408,9 @@ static int uniwill_keyboard_suspend(struct platform_device *dev, pm_message_t st static int uniwill_keyboard_resume(struct platform_device *dev) { - uniwill_write_kbd_bl_state(); + if (uniwill_kbd_bl_type_rgb_single_color) { + uniwill_write_kbd_bl_state(); + } uniwill_write_kbd_bl_enable(1); return 0; } From e9e3c8b8e53cfc7ddad966e370f797949a253999 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Thu, 20 Aug 2020 15:51:16 +0200 Subject: [PATCH 18/23] Add sysfs interface for uw kbd bl color --- src/uniwill_keyboard.h | 85 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index bd5cb5e..0ba7707 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -38,9 +38,8 @@ #define UNIWILL_BRIGHTNESS_MIN 0x00 #define UNIWILL_BRIGHTNESS_MAX 0xc8 -#define UNIWILL_BRIGHTNESS_DEFAULT UNIWILL_BRIGHTNESS_MAX * 0.75 +#define UNIWILL_BRIGHTNESS_DEFAULT UNIWILL_BRIGHTNESS_MAX * 0.30 #define UNIWILL_COLOR_DEFAULT 0xffffff -#define UNIWILL_COLOR_STRING_DEFAULT "WHITE" union uw_ec_read_return { u32 dword; @@ -68,11 +67,9 @@ struct tuxedo_keyboard_driver uniwill_keyboard_driver; struct kbd_led_state_uw_t { u32 brightness; u32 color; - char color_string[COLOR_STRING_LEN]; } kbd_led_state_uw = { .brightness = UNIWILL_BRIGHTNESS_DEFAULT, .color = UNIWILL_COLOR_DEFAULT, - .color_string = UNIWILL_COLOR_STRING_DEFAULT }; static u8 uniwill_kbd_bl_enable_state_on_start; @@ -219,12 +216,12 @@ static void uniwill_write_kbd_bl_state(void) { u32 brightness_percentage = (kbd_led_state_uw.brightness * 100) / UNIWILL_BRIGHTNESS_MAX; - // Scale color values + // Scale color values to valid range color_red = (color_red * 0xc8) / 0xff; color_green = (color_green * 0xc8) / 0xff; color_blue = (color_blue * 0xc8) / 0xff; - // Scale the respective values + // Scale the respective color values with brightness color_red = (color_red * brightness_percentage) / 100; color_green = (color_green * brightness_percentage) / 100; color_blue = (color_blue * brightness_percentage) / 100; @@ -314,6 +311,77 @@ static void uniwill_wmi_notify2(u32 value, void *context) uniwill_wmi_handle_event(value, context, 2); } +static ssize_t uw_brightness_show(struct device *child, + struct device_attribute *attr, char *buffer) +{ + return sprintf(buffer, "%d\n", kbd_led_state_uw.brightness); +} + +static ssize_t uw_brightness_store(struct device *child, + struct device_attribute *attr, + const char *buffer, size_t size) +{ + u32 brightness_input; + int err = kstrtouint(buffer, 0, &brightness_input); + if (err) return err; + if (brightness_input > UNIWILL_BRIGHTNESS_MAX) return -EINVAL; + kbd_led_state_uw.brightness = (u8)brightness_input; + uniwill_write_kbd_bl_state(); + return size; +} + +static ssize_t uw_color_string_show(struct device *child, + struct device_attribute *attr, char *buffer) +{ + int i; + sprintf(buffer, "Color values:"); + for (i = 0; i < color_list.size; ++i) { + sprintf(buffer + strlen(buffer), " %s", + color_list.colors[i].name); + } + sprintf(buffer + strlen(buffer), "\n"); + return strlen(buffer); +} + +static ssize_t uw_color_string_store(struct device *child, + struct device_attribute *attr, + const char *buffer, size_t size) +{ + u32 color_value; + char *buffer_copy; + + buffer_copy = kmalloc(size + 1, GFP_KERNEL); + strcpy(buffer_copy, buffer); + color_value = color_lookup(&color_list, strstrip(buffer_copy)); + kfree(buffer_copy); + + if (color_value > 0xffffff) return -EINVAL; + kbd_led_state_uw.color = color_value; + uniwill_write_kbd_bl_state(); + return size; +} + +// Device attributes used by uw kbd +struct uw_kbd_dev_attrs_t { + struct device_attribute brightness; + struct device_attribute color_string; +} uw_kbd_dev_attrs = { + .brightness = __ATTR(brightness, 0644, uw_brightness_show, uw_brightness_store), + .color_string = __ATTR(color_string, 0644, uw_color_string_show, uw_color_string_store) +}; + +// Device attributes used for uw_kbd_bl_color +static struct attribute *uw_kbd_bl_color_attrs[] = { + &uw_kbd_dev_attrs.brightness.attr, + &uw_kbd_dev_attrs.color_string.attr, + NULL +}; + +static struct attribute_group uw_kbd_bl_color_attr_group = { + .name = "uw_kbd_bl_color", + .attrs = uw_kbd_bl_color_attrs +}; + static int uniwill_keyboard_probe(struct platform_device *dev) { int status; @@ -370,6 +438,10 @@ static int uniwill_keyboard_probe(struct platform_device *dev) // Update keyboard backlight according to the current state uniwill_write_kbd_bl_state(); + + // Init sysfs bl attributes group + status = sysfs_create_group(&dev->dev.kobj, &uw_kbd_bl_color_attr_group); + if (status) TUXEDO_ERROR("Failed to create sysfs group\n"); } // Enable keyboard backlight @@ -389,6 +461,7 @@ static int uniwill_keyboard_remove(struct platform_device *dev) { // Restore previous backlight enable state if (uniwill_kbd_bl_enable_state_on_start != 0xff) { + sysfs_remove_group(&dev->dev.kobj, &uw_kbd_bl_color_attr_group); uniwill_write_kbd_bl_enable(uniwill_kbd_bl_enable_state_on_start); } From e25b657fa13bac73965402af3d883fee7a1afd96 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Fri, 21 Aug 2020 09:06:29 +0200 Subject: [PATCH 19/23] Remove remaining unnecessary highreg value for clarity --- src/uniwill_keyboard.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 0ba7707..fe56a47 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -173,7 +173,7 @@ static void uniwill_write_kbd_bl_enable(u8 enable) __uniwill_wmi_ec_read(0x8c, 0x07, ®_read_return); write_value = reg_read_return.bytes.data_low & ~(1 << 1); write_value |= (!enable << 1); - __uniwill_wmi_ec_write(0x8c, 0x07, write_value, reg_read_return.bytes.data_high, ®_write_return); + __uniwill_wmi_ec_write(0x8c, 0x07, write_value, 0x00, ®_write_return); } else { TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); } From 726af039b9bcabf8da389845272da9c3f35d0aff Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Tue, 25 Aug 2020 12:09:56 +0200 Subject: [PATCH 20/23] Rename (new tccwmi interface) and clean up uw kbd references --- src/uniwill_keyboard.h | 55 ++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index fe56a47..74a062b 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -59,8 +59,11 @@ union uw_ec_write_return { } bytes; }; -extern u32 uniwill_wmi_ec_read(u8, u8, union uw_ec_read_return *); -extern u32 uniwill_wmi_ec_write(u8, u8, u8, u8, union uw_ec_write_return *); +typedef u32 (uw_ec_read_func)(u8, u8, union uw_ec_read_return *); +typedef u32 (uw_ec_write_func)(u8, u8, u8, u8, union uw_ec_write_return *); + +extern uw_ec_read_func uw_ec_read_addr; +extern uw_ec_write_func uw_ec_write_addr; struct tuxedo_keyboard_driver uniwill_keyboard_driver; @@ -136,21 +139,21 @@ static u8 uniwill_read_kbd_bl_enabled(void) { union uw_ec_read_return reg_read_return; - u32 (*__uniwill_wmi_ec_read)(u8, u8, union uw_ec_read_return *); + uw_ec_read_func *__uw_ec_read_addr; u8 enabled = 0xff; - __uniwill_wmi_ec_read = symbol_get(uniwill_wmi_ec_read); + __uw_ec_read_addr = symbol_get(uw_ec_read_addr); - if (__uniwill_wmi_ec_read) { - __uniwill_wmi_ec_read(0x8c, 0x07, ®_read_return); + if (__uw_ec_read_addr) { + __uw_ec_read_addr(0x8c, 0x07, ®_read_return); enabled = (reg_read_return.bytes.data_low >> 1) & 0x01; enabled = !enabled; } else { TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); } - if (__uniwill_wmi_ec_read) symbol_put(uniwill_wmi_ec_read); + if (__uw_ec_read_addr) symbol_put(uw_ec_read_addr); return enabled; } @@ -160,52 +163,52 @@ static void uniwill_write_kbd_bl_enable(u8 enable) union uw_ec_read_return reg_read_return; union uw_ec_write_return reg_write_return; - u32 (*__uniwill_wmi_ec_read)(u8, u8, union uw_ec_read_return *); - u32 (*__uniwill_wmi_ec_write)(u8, u8, u8, u8, union uw_ec_write_return *); + uw_ec_read_func *__uw_ec_read_addr; + uw_ec_write_func *__uw_ec_write_addr; u8 write_value = 0; enable = enable & 0x01; - __uniwill_wmi_ec_read = symbol_get(uniwill_wmi_ec_read); - __uniwill_wmi_ec_write = symbol_get(uniwill_wmi_ec_write); + __uw_ec_read_addr = symbol_get(uw_ec_read_addr); + __uw_ec_write_addr = symbol_get(uw_ec_write_addr); - if (__uniwill_wmi_ec_read && __uniwill_wmi_ec_write) { - __uniwill_wmi_ec_read(0x8c, 0x07, ®_read_return); + if (__uw_ec_read_addr && __uw_ec_write_addr) { + __uw_ec_read_addr(0x8c, 0x07, ®_read_return); write_value = reg_read_return.bytes.data_low & ~(1 << 1); write_value |= (!enable << 1); - __uniwill_wmi_ec_write(0x8c, 0x07, write_value, 0x00, ®_write_return); + __uw_ec_write_addr(0x8c, 0x07, write_value, 0x00, ®_write_return); } else { TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); } - if (__uniwill_wmi_ec_read) symbol_put(uniwill_wmi_ec_read); - if (__uniwill_wmi_ec_write) symbol_put(uniwill_wmi_ec_write); + if (__uw_ec_read_addr) symbol_put(uw_ec_read_addr); + if (__uw_ec_write_addr) symbol_put(uw_ec_write_addr); } static void uniwill_write_kbd_bl_rgb(u8 red, u8 green, u8 blue) { union uw_ec_write_return reg_write_return; - u32 (*__uniwill_wmi_ec_read)(u8, u8, union uw_ec_read_return *); - u32 (*__uniwill_wmi_ec_write)(u8, u8, u8, u8, union uw_ec_write_return *); + uw_ec_read_func *__uw_ec_read_addr; + uw_ec_write_func *__uw_ec_write_addr; - __uniwill_wmi_ec_read = symbol_get(uniwill_wmi_ec_read); - __uniwill_wmi_ec_write = symbol_get(uniwill_wmi_ec_write); + __uw_ec_read_addr = symbol_get(uw_ec_read_addr); + __uw_ec_write_addr = symbol_get(uw_ec_write_addr); - if (__uniwill_wmi_ec_read && __uniwill_wmi_ec_write) { + if (__uw_ec_read_addr && __uw_ec_write_addr) { // Write the colors if (red > 0xc8) red = 0xc8; if (green > 0xc8) green = 0xc8; if (blue > 0xc8) blue = 0xc8; - __uniwill_wmi_ec_write(0x03, 0x18, red, 0x00, ®_write_return); - __uniwill_wmi_ec_write(0x05, 0x18, green, 0x00, ®_write_return); - __uniwill_wmi_ec_write(0x08, 0x18, blue, 0x00, ®_write_return); + __uw_ec_write_addr(0x03, 0x18, red, 0x00, ®_write_return); + __uw_ec_write_addr(0x05, 0x18, green, 0x00, ®_write_return); + __uw_ec_write_addr(0x08, 0x18, blue, 0x00, ®_write_return); } else { TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); } - if (__uniwill_wmi_ec_read) symbol_put(uniwill_wmi_ec_read); - if (__uniwill_wmi_ec_write) symbol_put(uniwill_wmi_ec_write); + if (__uw_ec_read_addr) symbol_put(uw_ec_read_addr); + if (__uw_ec_write_addr) symbol_put(uw_ec_write_addr); } static void uniwill_write_kbd_bl_state(void) { From a21f854e0353cef7e2b5c8bdb8e3bc5ddb526130 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Wed, 26 Aug 2020 10:43:09 +0200 Subject: [PATCH 21/23] Implement delayed init set after boot animation --- src/uniwill_keyboard.h | 173 +++++++++++++++++++++++++++++++++++------ 1 file changed, 148 insertions(+), 25 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 74a062b..59ed626 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -22,6 +22,7 @@ #include #include #include +#include #define UNIWILL_WMI_MGMT_GUID_BA "ABBC0F6D-8EA1-11D1-00A0-C90629100000" #define UNIWILL_WMI_MGMT_GUID_BB "ABBC0F6E-8EA1-11D1-00A0-C90629100000" @@ -185,6 +186,54 @@ static void uniwill_write_kbd_bl_enable(u8 enable) if (__uw_ec_write_addr) symbol_put(uw_ec_write_addr); } +static u32 uniwill_read_kbd_bl_br_state(u8 *brightness_state) +{ + union uw_ec_read_return reg_read_return; + u32 result; + + uw_ec_read_func *__uw_ec_read_addr; + + __uw_ec_read_addr = symbol_get(uw_ec_read_addr); + + if (__uw_ec_read_addr) { + __uw_ec_read_addr(0x8c, 0x07, ®_read_return); + *brightness_state = (reg_read_return.bytes.data_low & 0xf0) >> 4; + result = 0; + } else { + TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); + result = -EIO; + } + + return result; +} + +static u32 uniwill_read_kbd_bl_rgb(u8 *red, u8 *green, u8 *blue) +{ + union uw_ec_read_return reg_read_return; + u32 result; + + uw_ec_read_func *__uw_ec_read_addr; + + __uw_ec_read_addr = symbol_get(uw_ec_read_addr); + + if (__uw_ec_read_addr) { + __uw_ec_read_addr(0x03, 0x18, ®_read_return); + *red = reg_read_return.bytes.data_low; + __uw_ec_read_addr(0x05, 0x18, ®_read_return); + *green = reg_read_return.bytes.data_low; + __uw_ec_read_addr(0x08, 0x18, ®_read_return); + *blue = reg_read_return.bytes.data_low; + result = 0; + } else { + TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); + result = -EIO; + } + + if (__uw_ec_read_addr) symbol_put(uw_ec_read_addr); + + return result; +} + static void uniwill_write_kbd_bl_rgb(u8 red, u8 green, u8 blue) { union uw_ec_write_return reg_write_return; @@ -203,6 +252,7 @@ static void uniwill_write_kbd_bl_rgb(u8 red, u8 green, u8 blue) __uw_ec_write_addr(0x03, 0x18, red, 0x00, ®_write_return); __uw_ec_write_addr(0x05, 0x18, green, 0x00, ®_write_return); __uw_ec_write_addr(0x08, 0x18, blue, 0x00, ®_write_return); + TUXEDO_DEBUG("Wrote color [%0#4x, %0#4x, %0#4x]\n", red, green, blue); } else { TUXEDO_DEBUG("tuxedo-cc-wmi symbols not found\n"); } @@ -385,6 +435,101 @@ static struct attribute_group uw_kbd_bl_color_attr_group = { .attrs = uw_kbd_bl_color_attrs }; +static void uw_kbd_bl_init_set(void) +{ + if (uniwill_kbd_bl_type_rgb_single_color) { + // Disable backlight while initializing + // uniwill_write_kbd_bl_enable(0); + + // Update keyboard backlight according to the current state + uniwill_write_kbd_bl_state(); + } + + // Enable keyboard backlight + uniwill_write_kbd_bl_enable(1); +} + +// Keep track of previous colors on start, init array with different non-colors +static u32 uw_prev_colors[] = {0x01000000, 0x02000000, 0x03000000}; +static u32 uw_prev_colors_size = 3; +static u32 uw_prev_colors_index = 0; + +// Timer for checking animation colors +static struct timer_list uw_kbd_bl_init_timer; +static volatile int uw_kbd_bl_check_count = 40; +static int uw_kbd_bl_init_check_interval_ms = 500; + +static void uw_kbd_bl_init_ready_check_work_func(struct work_struct *work) +{ + u8 uw_cur_red, uw_cur_green, uw_cur_blue; + int i; + bool prev_colors_same; + uniwill_read_kbd_bl_rgb(&uw_cur_red, &uw_cur_green, &uw_cur_blue); + uw_prev_colors[uw_prev_colors_index] = (uw_cur_red << 0x10) | (uw_cur_green << 0x08) | uw_cur_blue; + uw_prev_colors_index = (uw_prev_colors_index + 1) % uw_prev_colors_size; + + TUXEDO_DEBUG("uw kbd bl check count %d\n", uw_kbd_bl_check_count); + + prev_colors_same = true; + for (i = 1; i < uw_prev_colors_size; ++i) { + if (uw_prev_colors[i-1] != uw_prev_colors[i]) prev_colors_same = false; + } + + if (prev_colors_same) { + TUXEDO_DEBUG("uw kbd bl init\n"); + uw_kbd_bl_init_set(); + del_timer(&uw_kbd_bl_init_timer); + } else { + if (uw_kbd_bl_check_count != 0) { + mod_timer(&uw_kbd_bl_init_timer, jiffies + msecs_to_jiffies(uw_kbd_bl_init_check_interval_ms)); + } else { + TUXEDO_DEBUG("uw kbd bl init check timeout\n"); + del_timer(&uw_kbd_bl_init_timer); + } + } + + uw_kbd_bl_check_count -= 1; +} + +static DECLARE_WORK(uw_kbd_bl_init_ready_check_work, uw_kbd_bl_init_ready_check_work_func); + +static void uw_kbd_bl_init_ready_check(struct timer_list *t) +{ + schedule_work(&uw_kbd_bl_init_ready_check_work); +} + +static int uw_kbd_bl_init(struct platform_device *dev) +{ + int status = 0; + + uniwill_kbd_bl_type_rgb_single_color = dmi_match(DMI_BOARD_NAME, "Polaris15I01"); + + // Save previous enable state + uniwill_kbd_bl_enable_state_on_start = uniwill_read_kbd_bl_enabled(); + + if (uniwill_kbd_bl_type_rgb_single_color) { + // Initialize keyboard backlight driver state according to parameters + if (param_brightness > UNIWILL_BRIGHTNESS_MAX) param_brightness = UNIWILL_BRIGHTNESS_DEFAULT; + kbd_led_state_uw.brightness = param_brightness; + if (color_lookup(&color_list, param_color) <= (u32) 0xffffff) kbd_led_state_uw.color = color_lookup(&color_list, param_color); + else kbd_led_state_uw.color = UNIWILL_COLOR_DEFAULT; + + // Init sysfs bl attributes group + status = sysfs_create_group(&dev->dev.kobj, &uw_kbd_bl_color_attr_group); + if (status) TUXEDO_ERROR("Failed to create sysfs group\n"); + + // Start periodic checking of animation, set and enable bl when done + timer_setup(&uw_kbd_bl_init_timer, uw_kbd_bl_init_ready_check, 0); + mod_timer(&uw_kbd_bl_init_timer, jiffies + msecs_to_jiffies(uw_kbd_bl_init_check_interval_ms)); + } else { + // For non-RGB versions + // Enable keyboard backlight immediately (should it be disabled) + uniwill_write_kbd_bl_enable(1); + } + + return status; +} + static int uniwill_keyboard_probe(struct platform_device *dev) { int status; @@ -424,31 +569,7 @@ static int uniwill_keyboard_probe(struct platform_device *dev) status = register_keyboard_notifier(&keyboard_notifier_block); - uniwill_kbd_bl_type_rgb_single_color = dmi_match(DMI_BOARD_NAME, "Polaris15I01"); - - // Save previous enable state - uniwill_kbd_bl_enable_state_on_start = uniwill_read_kbd_bl_enabled(); - - if (uniwill_kbd_bl_type_rgb_single_color) { - // Disable backlight while initializing - uniwill_write_kbd_bl_enable(0); - - // Initialize keyboard backlight driver state according to parameters - if (param_brightness > UNIWILL_BRIGHTNESS_MAX) param_brightness = UNIWILL_BRIGHTNESS_DEFAULT; - kbd_led_state_uw.brightness = param_brightness; - if (color_lookup(&color_list, param_color) <= (u32) 0xffffff) kbd_led_state_uw.color = color_lookup(&color_list, param_color); - else kbd_led_state_uw.color = UNIWILL_COLOR_DEFAULT; - - // Update keyboard backlight according to the current state - uniwill_write_kbd_bl_state(); - - // Init sysfs bl attributes group - status = sysfs_create_group(&dev->dev.kobj, &uw_kbd_bl_color_attr_group); - if (status) TUXEDO_ERROR("Failed to create sysfs group\n"); - } - - // Enable keyboard backlight - uniwill_write_kbd_bl_enable(1); + uw_kbd_bl_init(dev); return 0; @@ -473,6 +594,8 @@ static int uniwill_keyboard_remove(struct platform_device *dev) wmi_remove_notify_handler(UNIWILL_WMI_EVENT_GUID_1); wmi_remove_notify_handler(UNIWILL_WMI_EVENT_GUID_2); + del_timer(&uw_kbd_bl_init_timer); + return 0; } From 038c5d7602cc08c1da99745e148b046cfb5714fa Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Wed, 26 Aug 2020 10:52:44 +0200 Subject: [PATCH 22/23] Comment out uw bl read br state --- src/uniwill_keyboard.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 59ed626..4760a69 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -186,7 +186,7 @@ static void uniwill_write_kbd_bl_enable(u8 enable) if (__uw_ec_write_addr) symbol_put(uw_ec_write_addr); } -static u32 uniwill_read_kbd_bl_br_state(u8 *brightness_state) +/*static u32 uniwill_read_kbd_bl_br_state(u8 *brightness_state) { union uw_ec_read_return reg_read_return; u32 result; @@ -205,7 +205,7 @@ static u32 uniwill_read_kbd_bl_br_state(u8 *brightness_state) } return result; -} +}*/ static u32 uniwill_read_kbd_bl_rgb(u8 *red, u8 *green, u8 *blue) { From 06ea622d59ba5e922dcacb40696d48c8897d3435 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Wed, 26 Aug 2020 11:11:21 +0200 Subject: [PATCH 23/23] Lessen debug out and make sure init timeout is seen in any case --- src/uniwill_keyboard.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/uniwill_keyboard.h b/src/uniwill_keyboard.h index 4760a69..24d3238 100644 --- a/src/uniwill_keyboard.h +++ b/src/uniwill_keyboard.h @@ -468,22 +468,19 @@ static void uw_kbd_bl_init_ready_check_work_func(struct work_struct *work) uw_prev_colors[uw_prev_colors_index] = (uw_cur_red << 0x10) | (uw_cur_green << 0x08) | uw_cur_blue; uw_prev_colors_index = (uw_prev_colors_index + 1) % uw_prev_colors_size; - TUXEDO_DEBUG("uw kbd bl check count %d\n", uw_kbd_bl_check_count); - prev_colors_same = true; for (i = 1; i < uw_prev_colors_size; ++i) { if (uw_prev_colors[i-1] != uw_prev_colors[i]) prev_colors_same = false; } if (prev_colors_same) { - TUXEDO_DEBUG("uw kbd bl init\n"); uw_kbd_bl_init_set(); del_timer(&uw_kbd_bl_init_timer); } else { if (uw_kbd_bl_check_count != 0) { mod_timer(&uw_kbd_bl_init_timer, jiffies + msecs_to_jiffies(uw_kbd_bl_init_check_interval_ms)); } else { - TUXEDO_DEBUG("uw kbd bl init check timeout\n"); + TUXEDO_INFO("uw kbd init timeout, failed to detect end of boot animation\n"); del_timer(&uw_kbd_bl_init_timer); } }