[ltp] [patch 06/11] ibm-acpi: cleanup fan_write

Henrique de Moraes Holschuh linux-thinkpad@linux-thinkpad.org
Mon, 09 Oct 2006 09:41:37 -0300


This patch cleans up fan_write so that it is much easier to read and extend.

Signed-off-by: Henrique de Moraes Holschuh <hmh@hmh.eng.br>
---
 drivers/acpi/ibm_acpi.c |  128 +++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 104 insertions(+), 24 deletions(-)

Index: 2.6.18/drivers/acpi/ibm_acpi.c
===================================================================
--- 2.6.18.orig/drivers/acpi/ibm_acpi.c
+++ 2.6.18/drivers/acpi/ibm_acpi.c
@@ -1735,38 +1735,118 @@ static int fan_read(char *p)
 	return len;
 }
 
-static int fan_write(char *buf)
+static int fan_write_cmd_level(const char *cmd, int *rc)
 {
-	char *cmd;
-	int level, speed;
+	int level;
 
-	while ((cmd = next_cmd(&buf))) {
-		if (sfan_handle &&
-		    sscanf(cmd, "level %d", &level) == 1 &&
-		    level >= 0 && level <= 7) {
-			/* 570, 770x-JL */
+	if (sscanf(cmd, "level %d", &level) != 1)
+		return 0;
+
+	switch (fan_control_access_mode) {
+	case IBMACPI_FAN_WR_ACPI_SFAN:
+		if (level >= 0 && level <= 7) {
 			if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
-				return -EIO;
-		} else if (!gfan_handle && strlencmp(cmd, "enable") == 0) {
-			/* all except 570, 600e/x, 770e, 770x */
-			if (!acpi_ec_write(fan_status_offset, 0x80))
-				return -EIO;
-		} else if (!gfan_handle && strlencmp(cmd, "disable") == 0) {
-			/* all except 570, 600e/x, 770e, 770x */
-			if (!acpi_ec_write(fan_status_offset, 0x00))
-				return -EIO;
-		} else if (fans_handle &&
-			   sscanf(cmd, "speed %d", &speed) == 1 &&
-			   speed >= 0 && speed <= 65535) {
-			/* X31, X40 */
+				*rc = -EIO;
+		} else
+			*rc = -EINVAL;
+		break;
+
+	default:
+		printk(IBM_ERR "level command accepted for unsupported "
+			"access mode %d", fan_control_access_mode);
+		*rc = -EINVAL;
+	}
+	return 1;
+}
+
+static int fan_write_cmd_enable(const char *cmd, int *rc)
+{
+	if (strlencmp(cmd, "enable") != 0)
+		return 0;
+
+	switch (fan_control_access_mode) {
+	case IBMACPI_FAN_WR_ACPI_FANS:
+	case IBMACPI_FAN_WR_TPEC:
+		if (!acpi_ec_write(fan_status_offset, 0x80))
+			*rc = -EIO;
+		break;
+
+	default:
+		printk(IBM_ERR "enable command accepted for unsupported "
+			"access mode %d", fan_control_access_mode);
+		*rc = -EINVAL;
+	}
+	return 1;
+}
+
+static int fan_write_cmd_disable(const char *cmd, int *rc)
+{
+	if (strlencmp(cmd, "disable") != 0)
+		return 0;
+
+	switch (fan_control_access_mode) {
+	case IBMACPI_FAN_WR_ACPI_FANS:
+	case IBMACPI_FAN_WR_TPEC:
+		if (!acpi_ec_write(fan_status_offset, 0x00))
+			*rc = -EIO;
+		break;
+
+	default:
+		printk(IBM_ERR "disable command accepted for unsupported "
+			"access mode %d", fan_control_access_mode);
+		*rc = -EINVAL;
+	}
+	return 1;
+}
+
+static int fan_write_cmd_speed(const char *cmd, int *rc)
+{
+	int speed;
+
+	if (sscanf(cmd, "speed %d", &speed) != 1)
+		return 0;
+
+	switch (fan_control_access_mode) {
+	case IBMACPI_FAN_WR_ACPI_FANS:
+		if (speed >= 0 && speed <= 65535) {
 			if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
 					speed, speed, speed))
-				return -EIO;
+				*rc= -EIO;
 		} else
-			return -EINVAL;
+			*rc = -EINVAL;
+		break;
+
+	default:
+		printk(IBM_ERR "speed command accepted for unsupported "
+			"access mode %d", fan_control_access_mode);
+		*rc = -EINVAL;
 	}
+	return 1;
+}
 
-	return 0;
+static int fan_write(char *buf)
+{
+	char *cmd;
+	int rc = 0;
+
+	while ( !rc && (cmd = next_cmd(&buf))) {
+		if ((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
+				fan_write_cmd_level(cmd, &rc))
+		    	continue;
+		else if ((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
+				fan_write_cmd_enable(cmd, &rc))
+		    	continue;
+		else if ((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
+				fan_write_cmd_disable(cmd, &rc))
+		    	continue;
+		else if ((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
+				fan_write_cmd_speed(cmd, &rc))
+			continue;
+		else
+			rc = -EINVAL;
+	}
+
+	return rc;
 }
 
 static struct ibm_struct ibms[] = {

--
  "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