Skip to contentSkip to conversation

thinkpad_acpi: Factor out get/set adaptive kbd mode

PATCH In review Bastien Nocera 3 participants 3 messages Mar 2, 2015

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

PATCH 2/4 thinkpad_acpi: Factor out get/set adaptive kbd mode

Move the getting/setting of the adaptive keyboard mode to separate functions, so that we can reuse them later through sysfs attributes.

---
 drivers/platform/x86/thinkpad_acpi.c | 61 ++++++++++++++++++++++--------------
 1 file changed, 38 insertions(+), 23 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +38 -23
index 763aef0..5dea4c2 100644
@@ -3483,6 +3483,32 @@ static const int adaptive_keyboard_modes[] = {
 static bool adaptive_keyboard_mode_is_saved;
 static int adaptive_keyboard_prev_mode;
 
+static int adaptive_keyboard_get_mode(void)
+{
+	int mode = 0;
+
+	if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
+		pr_err("Cannot read adaptive keyboard mode\n");
+		return -EIO;
+	}
+
+	return mode;
+}
+
+static int adaptive_keyboard_set_mode(int new_mode)
+{
+	if (new_mode < 0 ||
+		new_mode > LAYFLAT_MODE)
+		return -EINVAL;
+
+	if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
+		pr_err("Cannot set adaptive keyboard mode\n");
+		return -EIO;
+	}
+
+	return 0;
+}
+
 static int adaptive_keyboard_get_next_mode(int mode)
 {
 	size_t i;
@@ -3512,39 +3538,28 @@ static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
 			new_mode = adaptive_keyboard_prev_mode;
 			adaptive_keyboard_mode_is_saved = false;
 		} else {
-			if (!acpi_evalf(
-					hkey_handle, &current_mode,
-					"GTRW", "dd", 0)) {
-				pr_err("Cannot read adaptive keyboard mode\n");
+			current_mode = adaptive_keyboard_get_mode();
+			if (current_mode < 0)
 				return false;
-			} else {
-				new_mode = adaptive_keyboard_get_next_mode(
-						current_mode);
-			}
+			new_mode = adaptive_keyboard_get_next_mode(
+					current_mode);
 		}
 
-		if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
-			pr_err("Cannot set adaptive keyboard mode\n");
+		if (adaptive_keyboard_set_mode(new_mode) < 0)
 			return false;
-		}
 
 		return true;
 
 	case DFR_SHOW_QUICKVIEW_ROW:
-		if (!acpi_evalf(hkey_handle,
-				&adaptive_keyboard_prev_mode,
-				"GTRW", "dd", 0)) {
-			pr_err("Cannot read adaptive keyboard mode\n");
+		current_mode = adaptive_keyboard_get_mode();
+		if (current_mode < 0)
 			return false;
-		} else {
-			adaptive_keyboard_mode_is_saved = true;
 
-			if (!acpi_evalf(hkey_handle,
-					NULL, "STRW", "vd", FUNCTION_MODE)) {
-				pr_err("Cannot set adaptive keyboard mode\n");
-				return false;
-			}
-		}
+		adaptive_keyboard_prev_mode = current_mode;
+		adaptive_keyboard_mode_is_saved = true;
+
+		if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
+			return false;
 		return true;
 
 	default:
Signature · 1 line
2.1.0
Acked-by Henrique de Moraes Holschuh on v1 2/4
#1 lore raw
Quoted 4 lines · Bastien Nocera

Move the getting/setting of the adaptive keyboard mode to separate functions, so that we can reuse them later through sysfs attributes.

Quoted 105 lines
---
 drivers/platform/x86/thinkpad_acpi.c | 61
 ++++++++++++++++++++++--------------
 1 file changed, 38 insertions(+), 23 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +28 -6
index 763aef0..5dea4c2 100644
@@ -3483,6 +3483,32 @@ static const int adaptive_keyboard_modes[] = {
 static bool adaptive_keyboard_mode_is_saved;
 static int adaptive_keyboard_prev_mode;
 
+static int adaptive_keyboard_get_mode(void)
+{
+       int mode = 0;
+
+       if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
+               pr_err("Cannot read adaptive keyboard mode\n");
+               return -EIO;
+       }
+
+       return mode;
+}
+
+static int adaptive_keyboard_set_mode(int new_mode)
+{
+       if (new_mode < 0 ||
+               new_mode > LAYFLAT_MODE)
+               return -EINVAL;
+
+       if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
+               pr_err("Cannot set adaptive keyboard mode\n");
+               return -EIO;
+       }
+
+       return 0;
+}
+
 static int adaptive_keyboard_get_next_mode(int mode)
 {
 	size_t i;
@@ -3512,39 +3538,28 @@ static bool
 adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
 			new_mode = adaptive_keyboard_prev_mode;
 			adaptive_keyboard_mode_is_saved = false;
 		} else {
-                       if (!acpi_evalf(
-                                       hkey_handle, &current_mode,
-                                       "GTRW", "dd", 0)) {
-                               pr_err("Cannot read adaptive keyboard
 mode\n");
+                       current_mode = adaptive_keyboard_get_mode();
+                       if (current_mode < 0)
 				return false;
-                       } else {
-                               new_mode =

adaptive_keyboard_get_next_mode(

drivers/platform/x86/thinkpad_acpi.c +2 -3
-                                               current_mode);
-                       }
+                       new_mode = adaptive_keyboard_get_next_mode(
+                                       current_mode);
 		}
 
-               if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",

new_mode)) {

drivers/platform/x86/thinkpad_acpi.c +3 -10
-                       pr_err("Cannot set adaptive keyboard mode\n");
+               if (adaptive_keyboard_set_mode(new_mode) < 0)
 			return false;
-               }
 
 		return true;
 
 	case DFR_SHOW_QUICKVIEW_ROW:
-               if (!acpi_evalf(hkey_handle,
-                               &adaptive_keyboard_prev_mode,
-                               "GTRW", "dd", 0)) {
-                       pr_err("Cannot read adaptive keyboard mode\n");
+               current_mode = adaptive_keyboard_get_mode();
+               if (current_mode < 0)
 			return false;
-               } else {
-                       adaptive_keyboard_mode_is_saved = true;
 
-                       if (!acpi_evalf(hkey_handle,
-                                       NULL, "STRW", "vd",

FUNCTION_MODE)) {
- pr_err("Cannot set adaptive keyboard
mode\n");

drivers/platform/x86/thinkpad_acpi.c +5 -3
-                               return false;
-                       }
-               }
+               adaptive_keyboard_prev_mode = current_mode;
+               adaptive_keyboard_mode_is_saved = true;
+
+               if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
+                       return false;
 		return true;
 
 	default:
Signature · 1 line
2.1.0
Acked-by on v1 2/4
#2 lore raw
In reply to Bastien Nocera
Quoted 4 lines · Bastien Nocera

Move the getting/setting of the adaptive keyboard mode to separate functions, so that we can reuse them later through sysfs attributes.

Quoted 105 lines
---
 drivers/platform/x86/thinkpad_acpi.c | 61
 ++++++++++++++++++++++--------------
 1 file changed, 38 insertions(+), 23 deletions(-)
drivers/platform/x86/thinkpad_acpi.c +28 -6
index 763aef0..5dea4c2 100644
@@ -3483,6 +3483,32 @@ static const int adaptive_keyboard_modes[] = {
 static bool adaptive_keyboard_mode_is_saved;
 static int adaptive_keyboard_prev_mode;
 
+static int adaptive_keyboard_get_mode(void)
+{
+       int mode = 0;
+
+       if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
+               pr_err("Cannot read adaptive keyboard mode\n");
+               return -EIO;
+       }
+
+       return mode;
+}
+
+static int adaptive_keyboard_set_mode(int new_mode)
+{
+       if (new_mode < 0 ||
+               new_mode > LAYFLAT_MODE)
+               return -EINVAL;
+
+       if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
+               pr_err("Cannot set adaptive keyboard mode\n");
+               return -EIO;
+       }
+
+       return 0;
+}
+
 static int adaptive_keyboard_get_next_mode(int mode)
 {
 	size_t i;
@@ -3512,39 +3538,28 @@ static bool
 adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
 			new_mode = adaptive_keyboard_prev_mode;
 			adaptive_keyboard_mode_is_saved = false;
 		} else {
-                       if (!acpi_evalf(
-                                       hkey_handle, &current_mode,
-                                       "GTRW", "dd", 0)) {
-                               pr_err("Cannot read adaptive keyboard
 mode\n");
+                       current_mode = adaptive_keyboard_get_mode();
+                       if (current_mode < 0)
 				return false;
-                       } else {
-                               new_mode =

adaptive_keyboard_get_next_mode(

drivers/platform/x86/thinkpad_acpi.c +2 -3
-                                               current_mode);
-                       }
+                       new_mode = adaptive_keyboard_get_next_mode(
+                                       current_mode);
 		}
 
-               if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",

new_mode)) {

drivers/platform/x86/thinkpad_acpi.c +3 -10
-                       pr_err("Cannot set adaptive keyboard mode\n");
+               if (adaptive_keyboard_set_mode(new_mode) < 0)
 			return false;
-               }
 
 		return true;
 
 	case DFR_SHOW_QUICKVIEW_ROW:
-               if (!acpi_evalf(hkey_handle,
-                               &adaptive_keyboard_prev_mode,
-                               "GTRW", "dd", 0)) {
-                       pr_err("Cannot read adaptive keyboard mode\n");
+               current_mode = adaptive_keyboard_get_mode();
+               if (current_mode < 0)
 			return false;
-               } else {
-                       adaptive_keyboard_mode_is_saved = true;
 
-                       if (!acpi_evalf(hkey_handle,
-                                       NULL, "STRW", "vd",

FUNCTION_MODE)) {
- pr_err("Cannot set adaptive keyboard
mode\n");

drivers/platform/x86/thinkpad_acpi.c +5 -3
-                               return false;
-                       }
-               }
+               adaptive_keyboard_prev_mode = current_mode;
+               adaptive_keyboard_mode_is_saved = true;
+
+               if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
+                       return false;
 		return true;
 
 	default:
Signature · 1 line
2.1.0
Signature · 4 lines
  "One disk to rule them all, One disk to find them. One disk to bring
  them all and in the darkness grind them. In the Land of Redmond
  where the shadows lie." -- The Silicon Valley Tarot
  Henrique Holschuh