Add linux v2.6.20-rc2, as most patches were merged in
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / releases / upstream / 2.6.20-rc2 / 0011-ACPI-ibm-acpi-cleanup-fan_write.txt
blob5e566ca1b1f9c9ff4c9b46ffde71674712bf7661
1 From 18ad7996e17649d81c15a2c9dae03c75050a05a8 Mon Sep 17 00:00:00 2001
2 From: Henrique de Moraes Holschuh <hmh@hmh.eng.br>
3 Date: Fri, 24 Nov 2006 11:47:11 -0200
4 Subject: [PATCH 11/28] ACPI: ibm-acpi: cleanup fan_write
6 This patch cleans up fan_write so that it is much easier to read and
7 extend.  It separates the proc api handling from the operations themselves.
9 Signed-off-by: Henrique de Moraes Holschuh <hmh@hmh.eng.br>
10 ---
11  drivers/acpi/ibm_acpi.c |  147 ++++++++++++++++++++++++++++++++++++++++-------
12  1 files changed, 125 insertions(+), 22 deletions(-)
14 diff --git a/drivers/acpi/ibm_acpi.c b/drivers/acpi/ibm_acpi.c
15 index 9275dfc..187af1b 100644
16 --- a/drivers/acpi/ibm_acpi.c
17 +++ b/drivers/acpi/ibm_acpi.c
18 @@ -1843,40 +1843,143 @@ static int fan_read(char *p)
19         return len;
20  }
22 -static int fan_write(char *buf)
23 +static int fan_set_level(int level)
24  {
25 -       char *cmd;
26 -       int level, speed;
28 -       while ((cmd = next_cmd(&buf))) {
29 -               if (sfan_handle &&
30 -                   sscanf(cmd, "level %d", &level) == 1 &&
31 -                   level >= 0 && level <= 7) {
32 -                       /* 570, 770x-JL */
33 +       switch (fan_control_access_mode) {
34 +       case IBMACPI_FAN_WR_ACPI_SFAN:
35 +               if (level >= 0 && level <= 7) {
36                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
37                                 return -EIO;
38 -               } else if (!gfan_handle && strlencmp(cmd, "enable") == 0) {
39 -                       /* all except 570, 600e/x, 770e, 770x */
40 -                       if (!acpi_ec_write(fan_status_offset, 0x80))
41 -                               return -EIO;
42 -               } else if (!gfan_handle && strlencmp(cmd, "disable") == 0) {
43 -                       /* all except 570, 600e/x, 770e, 770x */
44 -                       if (!acpi_ec_write(fan_status_offset, 0x00))
45 -                               return -EIO;
46 -               } else if (fans_handle &&
47 -                          sscanf(cmd, "speed %d", &speed) == 1 &&
48 -                          speed >= 0 && speed <= 65535) {
49 -                       /* X31, X40 */
50 +               } else
51 +                       return -EINVAL;
52 +               break;
54 +       default:
55 +               return -ENXIO;
56 +       }
57 +       return 0;
60 +static int fan_set_enable(void)
62 +       switch (fan_control_access_mode) {
63 +       case IBMACPI_FAN_WR_ACPI_FANS:
64 +       case IBMACPI_FAN_WR_TPEC:
65 +               if (!acpi_ec_write(fan_status_offset, 0x80))
66 +                       return -EIO;
67 +               break;
69 +       default:
70 +               return -ENXIO;
71 +       }
72 +       return 0;
75 +static int fan_set_disable(void)
77 +       switch (fan_control_access_mode) {
78 +       case IBMACPI_FAN_WR_ACPI_FANS:
79 +       case IBMACPI_FAN_WR_TPEC:
80 +               if (!acpi_ec_write(fan_status_offset, 0x00))
81 +                       return -EIO;
82 +               break;
84 +       default:
85 +               return -ENXIO;
86 +       }
87 +       return 0;
90 +static int fan_set_speed(int speed)
92 +       switch (fan_control_access_mode) {
93 +       case IBMACPI_FAN_WR_ACPI_FANS:
94 +               if (speed >= 0 && speed <= 65535) {
95                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
96                                         speed, speed, speed))
97                                 return -EIO;
98                 } else
99                         return -EINVAL;
100 -       }
101 +               break;
103 +       default:
104 +               return -ENXIO;
105 +       }
106         return 0;
109 +static int fan_write_cmd_level(const char *cmd, int *rc)
111 +       int level;
113 +       if (sscanf(cmd, "level %d", &level) != 1)
114 +               return 0;
116 +       if ((*rc = fan_set_level(level)) == -ENXIO)
117 +               printk(IBM_ERR "level command accepted for unsupported "
118 +                      "access mode %d", fan_control_access_mode);
120 +       return 1;
123 +static int fan_write_cmd_enable(const char *cmd, int *rc)
125 +       if (strlencmp(cmd, "enable") != 0)
126 +               return 0;
128 +       if ((*rc = fan_set_enable()) == -ENXIO)
129 +               printk(IBM_ERR "enable command accepted for unsupported "
130 +                      "access mode %d", fan_control_access_mode);
132 +       return 1;
135 +static int fan_write_cmd_disable(const char *cmd, int *rc)
137 +       if (strlencmp(cmd, "disable") != 0)
138 +               return 0;
140 +       if ((*rc = fan_set_disable()) == -ENXIO)
141 +               printk(IBM_ERR "disable command accepted for unsupported "
142 +                      "access mode %d", fan_control_access_mode);
144 +       return 1;
147 +static int fan_write_cmd_speed(const char *cmd, int *rc)
149 +       int speed;
151 +       if (sscanf(cmd, "speed %d", &speed) != 1)
152 +               return 0;
154 +       if ((*rc = fan_set_speed(speed)) == -ENXIO)
155 +               printk(IBM_ERR "speed command accepted for unsupported "
156 +                      "access mode %d", fan_control_access_mode);
158 +       return 1;
161 +static int fan_write(char *buf)
163 +       char *cmd;
164 +       int rc = 0;
166 +       while (!rc && (cmd = next_cmd(&buf))) {
167 +               if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
168 +                     fan_write_cmd_level(cmd, &rc)) &&
169 +                   !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
170 +                     (fan_write_cmd_enable(cmd, &rc) ||
171 +                      fan_write_cmd_disable(cmd, &rc))) &&
172 +                   !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
173 +                     fan_write_cmd_speed(cmd, &rc))
174 +                   )
175 +                       rc = -EINVAL;
176 +       }
178 +       return rc;
181  static struct ibm_struct ibms[] = {
182         {
183          .name = "driver",
184 -- 
185 1.4.4.2