ACPI: ibm-acpi: document fan control
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / ibm_acpi.c
1 /*
2  *  ibm_acpi.c - IBM ThinkPad ACPI Extras
3  *
4  *
5  *  Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #define IBM_VERSION "0.12a"
23
24 /*
25  *  Changelog:
26  *  
27  *  2005-08-17  0.12    fix compilation on 2.6.13-rc kernels
28  *  2005-03-17  0.11    support for 600e, 770x
29  *                          thanks to Jamie Lentin <lentinj@dial.pipex.com>
30  *                      support for 770e, G41
31  *                      G40 and G41 don't have a thinklight
32  *                      temperatures no longer experimental
33  *                      experimental brightness control
34  *                      experimental volume control
35  *                      experimental fan enable/disable
36  *  2005-01-16  0.10    fix module loading on R30, R31 
37  *  2005-01-16  0.9     support for 570, R30, R31
38  *                      ultrabay support on A22p, A3x
39  *                      limit arg for cmos, led, beep, drop experimental status
40  *                      more capable led control on A21e, A22p, T20-22, X20
41  *                      experimental temperatures and fan speed
42  *                      experimental embedded controller register dump
43  *                      mark more functions as __init, drop incorrect __exit
44  *                      use MODULE_VERSION
45  *                          thanks to Henrik Brix Andersen <brix@gentoo.org>
46  *                      fix parameter passing on module loading
47  *                          thanks to Rusty Russell <rusty@rustcorp.com.au>
48  *                          thanks to Jim Radford <radford@blackbean.org>
49  *  2004-11-08  0.8     fix init error case, don't return from a macro
50  *                          thanks to Chris Wright <chrisw@osdl.org>
51  *  2004-10-23  0.7     fix module loading on A21e, A22p, T20, T21, X20
52  *                      fix led control on A21e
53  *  2004-10-19  0.6     use acpi_bus_register_driver() to claim HKEY device
54  *  2004-10-18  0.5     thinklight support on A21e, G40, R32, T20, T21, X20
55  *                      proc file format changed
56  *                      video_switch command
57  *                      experimental cmos control
58  *                      experimental led control
59  *                      experimental acpi sounds
60  *  2004-09-16  0.4     support for module parameters
61  *                      hotkey mask can be prefixed by 0x
62  *                      video output switching
63  *                      video expansion control
64  *                      ultrabay eject support
65  *                      removed lcd brightness/on/off control, didn't work
66  *  2004-08-17  0.3     support for R40
67  *                      lcd off, brightness control
68  *                      thinklight on/off
69  *  2004-08-14  0.2     support for T series, X20
70  *                      bluetooth enable/disable
71  *                      hotkey events disabled by default
72  *                      removed fan control, currently useless
73  *  2004-08-09  0.1     initial release, support for X series
74  */
75
76 #include <linux/kernel.h>
77 #include <linux/module.h>
78 #include <linux/init.h>
79 #include <linux/types.h>
80 #include <linux/proc_fs.h>
81 #include <linux/backlight.h>
82 #include <asm/uaccess.h>
83 #include <linux/dmi.h>
84
85 #include <acpi/acpi_drivers.h>
86 #include <acpi/acnamesp.h>
87
88 #define IBM_NAME "ibm"
89 #define IBM_DESC "IBM ThinkPad ACPI Extras"
90 #define IBM_FILE "ibm_acpi"
91 #define IBM_URL "http://ibm-acpi.sf.net/"
92
93 MODULE_AUTHOR("Borislav Deianov");
94 MODULE_DESCRIPTION(IBM_DESC);
95 MODULE_VERSION(IBM_VERSION);
96 MODULE_LICENSE("GPL");
97
98 #define IBM_DIR IBM_NAME
99
100 #define IBM_LOG IBM_FILE ": "
101 #define IBM_ERR    KERN_ERR    IBM_LOG
102 #define IBM_NOTICE KERN_NOTICE IBM_LOG
103 #define IBM_INFO   KERN_INFO   IBM_LOG
104 #define IBM_DEBUG  KERN_DEBUG  IBM_LOG
105
106 #define IBM_MAX_ACPI_ARGS 3
107
108 #define __unused __attribute__ ((unused))
109
110 static int experimental;
111 module_param(experimental, int, 0);
112
113 static acpi_handle root_handle = NULL;
114
115 #define IBM_HANDLE(object, parent, paths...)                    \
116         static acpi_handle  object##_handle;                    \
117         static acpi_handle *object##_parent = &parent##_handle; \
118         static char        *object##_path;                      \
119         static char        *object##_paths[] = { paths }
120
121 /*
122  * The following models are supported to various degrees:
123  *
124  * 570, 600e, 600x, 770e, 770x
125  * A20m, A21e, A21m, A21p, A22p, A30, A30p, A31, A31p
126  * G40, G41
127  * R30, R31, R32, R40, R40e, R50, R50e, R50p, R51
128  * T20, T21, T22, T23, T30, T40, T40p, T41, T41p, T42, T42p, T43
129  * X20, X21, X22, X23, X24, X30, X31, X40
130  *
131  * The following models have no supported features:
132  *
133  * 240, 240x, i1400
134  *
135  * Still missing DSDTs for the following models:
136  *
137  * A20p, A22e, A22m
138  * R52
139  * S31
140  * T43p
141  */
142
143 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",      /* 240, 240x */
144            "\\_SB.PCI.ISA.EC",  /* 570 */
145            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
146            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
147            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
148            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
149            "\\_SB.PCI0.LPC.EC", /* all others */
150     );
151
152 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",      /* 570 */
153            "\\_SB.PCI0.AGP0.VID0",      /* 600e/x, 770x */
154            "\\_SB.PCI0.VID0",   /* 770e */
155            "\\_SB.PCI0.VID",    /* A21e, G4x, R50e, X30, X40 */
156            "\\_SB.PCI0.AGP.VID",        /* all others */
157     );                          /* R30, R31 */
158
159 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");  /* G41 */
160
161 IBM_HANDLE(cmos, root, "\\UCMS",        /* R50, R50e, R50p, R51, T4x, X31, X40 */
162            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
163            "\\CMS",             /* R40, R40e */
164     );                          /* all others */
165 #ifdef CONFIG_ACPI_IBM_DOCK
166 IBM_HANDLE(dock, root, "\\_SB.GDCK",    /* X30, X31, X40 */
167            "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
168            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
169            "\\_SB.PCI.ISA.SLCE",        /* 570 */
170     );                          /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
171 #endif
172 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST",        /* 570 */
173            "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
174            "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
175     );                          /* A21e, R30, R31 */
176
177 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
178            "_EJ0",              /* all others */
179     );                          /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
180
181 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV",     /* A3x, R32 */
182            "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
183     );                          /* all others */
184
185 IBM_HANDLE(bay2_ej, bay2, "_EJ3",       /* 600e/x, 770e, A3x */
186            "_EJ0",              /* 770x */
187     );                          /* all others */
188
189 /* don't list other alternatives as we install a notify handler on the 570 */
190 IBM_HANDLE(pci, root, "\\_SB.PCI");     /* 570 */
191
192 IBM_HANDLE(hkey, ec, "\\_SB.HKEY",      /* 600e/x, 770e, 770x */
193            "^HKEY",             /* R30, R31 */
194            "HKEY",              /* all others */
195     );                          /* 570 */
196
197 IBM_HANDLE(lght, root, "\\LGHT");       /* A21e, A2xm/p, T20-22, X20-21 */
198 IBM_HANDLE(ledb, ec, "LEDB");   /* G4x */
199
200 IBM_HANDLE(led, ec, "SLED",     /* 570 */
201            "SYSL",              /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
202            "LED",               /* all others */
203     );                          /* R30, R31 */
204
205 IBM_HANDLE(beep, ec, "BEEP");   /* all except R30, R31 */
206 IBM_HANDLE(ecrd, ec, "ECRD");   /* 570 */
207 IBM_HANDLE(ecwr, ec, "ECWR");   /* 570 */
208 IBM_HANDLE(fans, ec, "FANS");   /* X31, X40, X41 */
209
210 IBM_HANDLE(gfan, ec, "GFAN",    /* 570 */
211            "\\FSPD",            /* 600e/x, 770e, 770x */
212     );                          /* all others */
213
214 IBM_HANDLE(sfan, ec, "SFAN",    /* 570 */
215            "JFNS",              /* 770x-JL */
216     );                          /* all others */
217
218 #define IBM_HKEY_HID    "IBM0068"
219 #define IBM_PCI_HID     "PNP0A03"
220
221 enum thermal_access_mode {
222         IBMACPI_THERMAL_NONE = 0,       /* No thermal support */
223         IBMACPI_THERMAL_ACPI_TMP07,     /* Use ACPI TMP0-7 */
224         IBMACPI_THERMAL_ACPI_UPDT,      /* Use ACPI TMP0-7 with UPDT */
225         IBMACPI_THERMAL_TPEC_8,         /* Use ACPI EC regs, 8 sensors */
226         IBMACPI_THERMAL_TPEC_16,        /* Use ACPI EC regs, 16 sensors */
227 };
228
229 #define IBMACPI_MAX_THERMAL_SENSORS 16  /* Max thermal sensors supported */
230 struct ibm_thermal_sensors_struct {
231         s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
232 };
233
234 /*
235  * FAN ACCESS MODES
236  *
237  * IBMACPI_FAN_RD_ACPI_GFAN:
238  *      ACPI GFAN method: returns fan level
239  *
240  *      see IBMACPI_FAN_WR_ACPI_SFAN
241  *      EC 0x2f not available if GFAN exists
242  *
243  * IBMACPI_FAN_WR_ACPI_SFAN:
244  *      ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
245  *
246  *      EC 0x2f might be available *for reading*, but never for writing.
247  *
248  * IBMACPI_FAN_WR_TPEC:
249  *      ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
250  *      on almost all ThinkPads
251  *
252  *      Fan speed changes of any sort (including those caused by the
253  *      disengaged mode) are usually done slowly by the firmware as the
254  *      maximum ammount of fan duty cycle change per second seems to be
255  *      limited.
256  *
257  *      Reading is not available if GFAN exists.
258  *      Writing is not available if SFAN exists.
259  *
260  *      Bits
261  *       7      automatic mode engaged;
262  *              (default operation mode of the ThinkPad)
263  *              fan level is ignored in this mode.
264  *       6      disengage mode (takes precedence over bit 7);
265  *              not available on all thinkpads.  May disable
266  *              the tachometer, and speeds up fan to 100% duty-cycle,
267  *              which speeds it up far above the standard RPM
268  *              levels.  It is not impossible that it could cause
269  *              hardware damage.
270  *      5-3     unused in some models.  Extra bits for fan level
271  *              in others, but still useless as all values above
272  *              7 map to the same speed as level 7 in these models.
273  *      2-0     fan level (0..7 usually)
274  *                      0x00 = stop
275  *                      0x07 = max (set when temperatures critical)
276  *              Some ThinkPads may have other levels, see
277  *              IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
278  *
279  *      FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
280  *      boot. Apparently the EC does not intialize it, so unless ACPI DSDT
281  *      does so, its initial value is meaningless (0x07).
282  *
283  *      For firmware bugs, refer to:
284  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
285  *
286  *      ----
287  *
288  *      ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
289  *      Main fan tachometer reading (in RPM)
290  *
291  *      This register is present on all ThinkPads with a new-style EC, and
292  *      it is known not to be present on the A21m/e, and T22, as there is
293  *      something else in offset 0x84 according to the ACPI DSDT.  Other
294  *      ThinkPads from this same time period (and earlier) probably lack the
295  *      tachometer as well.
296  *
297  *      Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
298  *      was never fixed by IBM to report the EC firmware version string
299  *      probably support the tachometer (like the early X models), so
300  *      detecting it is quite hard.  We need more data to know for sure.
301  *
302  *      FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
303  *      might result.
304  *
305  *      FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
306  *      register is not invalidated in ThinkPads that disable tachometer
307  *      readings.  Thus, the tachometer readings go stale.
308  *
309  *      For firmware bugs, refer to:
310  *      http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
311  *
312  * IBMACPI_FAN_WR_ACPI_FANS:
313  *      ThinkPad X31, X40, X41.  Not available in the X60.
314  *
315  *      FANS ACPI handle: takes three arguments: low speed, medium speed,
316  *      high speed.  ACPI DSDT seems to map these three speeds to levels
317  *      as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
318  *      (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
319  *
320  *      The speeds are stored on handles
321  *      (FANA:FAN9), (FANC:FANB), (FANE:FAND).
322  *
323  *      There are three default speed sets, acessible as handles:
324  *      FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
325  *
326  *      ACPI DSDT switches which set is in use depending on various
327  *      factors.
328  *
329  *      IBMACPI_FAN_WR_TPEC is also available and should be used to
330  *      command the fan.  The X31/X40/X41 seems to have 8 fan levels,
331  *      but the ACPI tables just mention level 7.
332  */
333
334 enum fan_status_access_mode {
335         IBMACPI_FAN_NONE = 0,           /* No fan status or control */
336         IBMACPI_FAN_RD_ACPI_GFAN,       /* Use ACPI GFAN */
337         IBMACPI_FAN_RD_TPEC,            /* Use ACPI EC regs 0x2f, 0x84-0x85 */
338 };
339
340 enum fan_control_access_mode {
341         IBMACPI_FAN_WR_NONE = 0,        /* No fan control */
342         IBMACPI_FAN_WR_ACPI_SFAN,       /* Use ACPI SFAN */
343         IBMACPI_FAN_WR_TPEC,            /* Use ACPI EC reg 0x2f */
344         IBMACPI_FAN_WR_ACPI_FANS,       /* Use ACPI FANS and EC reg 0x2f */
345 };
346
347 enum fan_control_commands {
348         IBMACPI_FAN_CMD_SPEED   = 0x0001,       /* speed command */
349         IBMACPI_FAN_CMD_LEVEL   = 0x0002,       /* level command  */
350         IBMACPI_FAN_CMD_ENABLE  = 0x0004,       /* enable/disable cmd */
351 };
352
353 enum {                                  /* Fan control constants */
354         fan_status_offset = 0x2f,       /* EC register 0x2f */
355         fan_rpm_offset = 0x84,          /* EC register 0x84: LSB, 0x85 MSB (RPM)
356                                          * 0x84 must be read before 0x85 */
357 };
358
359 static int ibm_thinkpad_ec_found;
360
361 struct ibm_struct {
362         char *name;
363         char param[32];
364
365         char *hid;
366         struct acpi_driver *driver;
367
368         int (*init) (void);
369         int (*read) (char *);
370         int (*write) (char *);
371         void (*exit) (void);
372
373         void (*notify) (struct ibm_struct *, u32);
374         acpi_handle *handle;
375         int type;
376         struct acpi_device *device;
377
378         int driver_registered;
379         int proc_created;
380         int init_called;
381         int notify_installed;
382
383         int experimental;
384 };
385
386 static struct proc_dir_entry *proc_dir = NULL;
387
388 static struct backlight_device *ibm_backlight_device;
389
390 #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
391 #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
392 #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
393
394 static int acpi_evalf(acpi_handle handle,
395                       void *res, char *method, char *fmt, ...)
396 {
397         char *fmt0 = fmt;
398         struct acpi_object_list params;
399         union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
400         struct acpi_buffer result, *resultp;
401         union acpi_object out_obj;
402         acpi_status status;
403         va_list ap;
404         char res_type;
405         int success;
406         int quiet;
407
408         if (!*fmt) {
409                 printk(IBM_ERR "acpi_evalf() called with empty format\n");
410                 return 0;
411         }
412
413         if (*fmt == 'q') {
414                 quiet = 1;
415                 fmt++;
416         } else
417                 quiet = 0;
418
419         res_type = *(fmt++);
420
421         params.count = 0;
422         params.pointer = &in_objs[0];
423
424         va_start(ap, fmt);
425         while (*fmt) {
426                 char c = *(fmt++);
427                 switch (c) {
428                 case 'd':       /* int */
429                         in_objs[params.count].integer.value = va_arg(ap, int);
430                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
431                         break;
432                         /* add more types as needed */
433                 default:
434                         printk(IBM_ERR "acpi_evalf() called "
435                                "with invalid format character '%c'\n", c);
436                         return 0;
437                 }
438         }
439         va_end(ap);
440
441         if (res_type != 'v') {
442                 result.length = sizeof(out_obj);
443                 result.pointer = &out_obj;
444                 resultp = &result;
445         } else
446                 resultp = NULL;
447
448         status = acpi_evaluate_object(handle, method, &params, resultp);
449
450         switch (res_type) {
451         case 'd':               /* int */
452                 if (res)
453                         *(int *)res = out_obj.integer.value;
454                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
455                 break;
456         case 'v':               /* void */
457                 success = status == AE_OK;
458                 break;
459                 /* add more types as needed */
460         default:
461                 printk(IBM_ERR "acpi_evalf() called "
462                        "with invalid format character '%c'\n", res_type);
463                 return 0;
464         }
465
466         if (!success && !quiet)
467                 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
468                        method, fmt0, status);
469
470         return success;
471 }
472
473 static void __unused acpi_print_int(acpi_handle handle, char *method)
474 {
475         int i;
476
477         if (acpi_evalf(handle, &i, method, "d"))
478                 printk(IBM_INFO "%s = 0x%x\n", method, i);
479         else
480                 printk(IBM_ERR "error calling %s\n", method);
481 }
482
483 static char *next_cmd(char **cmds)
484 {
485         char *start = *cmds;
486         char *end;
487
488         while ((end = strchr(start, ',')) && end == start)
489                 start = end + 1;
490
491         if (!end)
492                 return NULL;
493
494         *end = 0;
495         *cmds = end + 1;
496         return start;
497 }
498
499 static int driver_init(void)
500 {
501         printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
502         printk(IBM_INFO "%s\n", IBM_URL);
503
504         return 0;
505 }
506
507 static int driver_read(char *p)
508 {
509         int len = 0;
510
511         len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
512         len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
513
514         return len;
515 }
516
517 static int hotkey_supported;
518 static int hotkey_mask_supported;
519 static int hotkey_orig_status;
520 static int hotkey_orig_mask;
521
522 static int hotkey_get(int *status, int *mask)
523 {
524         if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
525                 return 0;
526
527         if (hotkey_mask_supported)
528                 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
529                         return 0;
530
531         return 1;
532 }
533
534 static int hotkey_set(int status, int mask)
535 {
536         int i;
537
538         if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
539                 return 0;
540
541         if (hotkey_mask_supported)
542                 for (i = 0; i < 32; i++) {
543                         int bit = ((1 << i) & mask) != 0;
544                         if (!acpi_evalf(hkey_handle,
545                                         NULL, "MHKM", "vdd", i + 1, bit))
546                                 return 0;
547                 }
548
549         return 1;
550 }
551
552 static int hotkey_init(void)
553 {
554         /* hotkey not supported on 570 */
555         hotkey_supported = hkey_handle != NULL;
556
557         if (hotkey_supported) {
558                 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
559                    A30, R30, R31, T20-22, X20-21, X22-24 */
560                 hotkey_mask_supported =
561                     acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
562
563                 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
564                         return -ENODEV;
565         }
566
567         return 0;
568 }
569
570 static int hotkey_read(char *p)
571 {
572         int status, mask;
573         int len = 0;
574
575         if (!hotkey_supported) {
576                 len += sprintf(p + len, "status:\t\tnot supported\n");
577                 return len;
578         }
579
580         if (!hotkey_get(&status, &mask))
581                 return -EIO;
582
583         len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
584         if (hotkey_mask_supported) {
585                 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
586                 len += sprintf(p + len,
587                                "commands:\tenable, disable, reset, <mask>\n");
588         } else {
589                 len += sprintf(p + len, "mask:\t\tnot supported\n");
590                 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
591         }
592
593         return len;
594 }
595
596 static int hotkey_write(char *buf)
597 {
598         int status, mask;
599         char *cmd;
600         int do_cmd = 0;
601
602         if (!hotkey_supported)
603                 return -ENODEV;
604
605         if (!hotkey_get(&status, &mask))
606                 return -EIO;
607
608         while ((cmd = next_cmd(&buf))) {
609                 if (strlencmp(cmd, "enable") == 0) {
610                         status = 1;
611                 } else if (strlencmp(cmd, "disable") == 0) {
612                         status = 0;
613                 } else if (strlencmp(cmd, "reset") == 0) {
614                         status = hotkey_orig_status;
615                         mask = hotkey_orig_mask;
616                 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
617                         /* mask set */
618                 } else if (sscanf(cmd, "%x", &mask) == 1) {
619                         /* mask set */
620                 } else
621                         return -EINVAL;
622                 do_cmd = 1;
623         }
624
625         if (do_cmd && !hotkey_set(status, mask))
626                 return -EIO;
627
628         return 0;
629 }
630
631 static void hotkey_exit(void)
632 {
633         if (hotkey_supported)
634                 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
635 }
636
637 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
638 {
639         int hkey;
640
641         if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
642                 acpi_bus_generate_event(ibm->device, event, hkey);
643         else {
644                 printk(IBM_ERR "unknown hotkey event %d\n", event);
645                 acpi_bus_generate_event(ibm->device, event, 0);
646         }
647 }
648
649 static int bluetooth_supported;
650
651 static int bluetooth_init(void)
652 {
653         /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
654            G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
655         bluetooth_supported = hkey_handle &&
656             acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
657
658         return 0;
659 }
660
661 static int bluetooth_status(void)
662 {
663         int status;
664
665         if (!bluetooth_supported ||
666             !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
667                 status = 0;
668
669         return status;
670 }
671
672 static int bluetooth_read(char *p)
673 {
674         int len = 0;
675         int status = bluetooth_status();
676
677         if (!bluetooth_supported)
678                 len += sprintf(p + len, "status:\t\tnot supported\n");
679         else if (!(status & 1))
680                 len += sprintf(p + len, "status:\t\tnot installed\n");
681         else {
682                 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
683                 len += sprintf(p + len, "commands:\tenable, disable\n");
684         }
685
686         return len;
687 }
688
689 static int bluetooth_write(char *buf)
690 {
691         int status = bluetooth_status();
692         char *cmd;
693         int do_cmd = 0;
694
695         if (!bluetooth_supported)
696                 return -ENODEV;
697
698         while ((cmd = next_cmd(&buf))) {
699                 if (strlencmp(cmd, "enable") == 0) {
700                         status |= 2;
701                 } else if (strlencmp(cmd, "disable") == 0) {
702                         status &= ~2;
703                 } else
704                         return -EINVAL;
705                 do_cmd = 1;
706         }
707
708         if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
709                 return -EIO;
710
711         return 0;
712 }
713
714 static int wan_supported;
715
716 static int wan_init(void)
717 {
718         wan_supported = hkey_handle &&
719             acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
720
721         return 0;
722 }
723
724 static int wan_status(void)
725 {
726         int status;
727
728         if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
729                 status = 0;
730
731         return status;
732 }
733
734 static int wan_read(char *p)
735 {
736         int len = 0;
737         int status = wan_status();
738
739         if (!wan_supported)
740                 len += sprintf(p + len, "status:\t\tnot supported\n");
741         else if (!(status & 1))
742                 len += sprintf(p + len, "status:\t\tnot installed\n");
743         else {
744                 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
745                 len += sprintf(p + len, "commands:\tenable, disable\n");
746         }
747
748         return len;
749 }
750
751 static int wan_write(char *buf)
752 {
753         int status = wan_status();
754         char *cmd;
755         int do_cmd = 0;
756
757         if (!wan_supported)
758                 return -ENODEV;
759
760         while ((cmd = next_cmd(&buf))) {
761                 if (strlencmp(cmd, "enable") == 0) {
762                         status |= 2;
763                 } else if (strlencmp(cmd, "disable") == 0) {
764                         status &= ~2;
765                 } else
766                         return -EINVAL;
767                 do_cmd = 1;
768         }
769
770         if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
771                 return -EIO;
772
773         return 0;
774 }
775
776 static int video_supported;
777 static int video_orig_autosw;
778
779 #define VIDEO_570 1
780 #define VIDEO_770 2
781 #define VIDEO_NEW 3
782
783 static int video_init(void)
784 {
785         int ivga;
786
787         if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
788                 /* G41, assume IVGA doesn't change */
789                 vid_handle = vid2_handle;
790
791         if (!vid_handle)
792                 /* video switching not supported on R30, R31 */
793                 video_supported = 0;
794         else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
795                 /* 570 */
796                 video_supported = VIDEO_570;
797         else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
798                 /* 600e/x, 770e, 770x */
799                 video_supported = VIDEO_770;
800         else
801                 /* all others */
802                 video_supported = VIDEO_NEW;
803
804         return 0;
805 }
806
807 static int video_status(void)
808 {
809         int status = 0;
810         int i;
811
812         if (video_supported == VIDEO_570) {
813                 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
814                         status = i & 3;
815         } else if (video_supported == VIDEO_770) {
816                 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
817                         status |= 0x01 * i;
818                 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
819                         status |= 0x02 * i;
820         } else if (video_supported == VIDEO_NEW) {
821                 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
822                 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
823                         status |= 0x02 * i;
824
825                 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
826                 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
827                         status |= 0x01 * i;
828                 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
829                         status |= 0x08 * i;
830         }
831
832         return status;
833 }
834
835 static int video_autosw(void)
836 {
837         int autosw = 0;
838
839         if (video_supported == VIDEO_570)
840                 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
841         else if (video_supported == VIDEO_770 || video_supported == VIDEO_NEW)
842                 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
843
844         return autosw & 1;
845 }
846
847 static int video_read(char *p)
848 {
849         int status = video_status();
850         int autosw = video_autosw();
851         int len = 0;
852
853         if (!video_supported) {
854                 len += sprintf(p + len, "status:\t\tnot supported\n");
855                 return len;
856         }
857
858         len += sprintf(p + len, "status:\t\tsupported\n");
859         len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
860         len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
861         if (video_supported == VIDEO_NEW)
862                 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
863         len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
864         len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
865         len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
866         if (video_supported == VIDEO_NEW)
867                 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
868         len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
869         len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
870
871         return len;
872 }
873
874 static int video_switch(void)
875 {
876         int autosw = video_autosw();
877         int ret;
878
879         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
880                 return -EIO;
881         ret = video_supported == VIDEO_570 ?
882             acpi_evalf(ec_handle, NULL, "_Q16", "v") :
883             acpi_evalf(vid_handle, NULL, "VSWT", "v");
884         acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
885
886         return ret;
887 }
888
889 static int video_expand(void)
890 {
891         if (video_supported == VIDEO_570)
892                 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
893         else if (video_supported == VIDEO_770)
894                 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
895         else
896                 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
897 }
898
899 static int video_switch2(int status)
900 {
901         int ret;
902
903         if (video_supported == VIDEO_570) {
904                 ret = acpi_evalf(NULL, NULL,
905                                  "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
906         } else if (video_supported == VIDEO_770) {
907                 int autosw = video_autosw();
908                 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
909                         return -EIO;
910
911                 ret = acpi_evalf(vid_handle, NULL,
912                                  "ASWT", "vdd", status * 0x100, 0);
913
914                 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
915         } else {
916                 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
917                     acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
918         }
919
920         return ret;
921 }
922
923 static int video_write(char *buf)
924 {
925         char *cmd;
926         int enable, disable, status;
927
928         if (!video_supported)
929                 return -ENODEV;
930
931         enable = disable = 0;
932
933         while ((cmd = next_cmd(&buf))) {
934                 if (strlencmp(cmd, "lcd_enable") == 0) {
935                         enable |= 0x01;
936                 } else if (strlencmp(cmd, "lcd_disable") == 0) {
937                         disable |= 0x01;
938                 } else if (strlencmp(cmd, "crt_enable") == 0) {
939                         enable |= 0x02;
940                 } else if (strlencmp(cmd, "crt_disable") == 0) {
941                         disable |= 0x02;
942                 } else if (video_supported == VIDEO_NEW &&
943                            strlencmp(cmd, "dvi_enable") == 0) {
944                         enable |= 0x08;
945                 } else if (video_supported == VIDEO_NEW &&
946                            strlencmp(cmd, "dvi_disable") == 0) {
947                         disable |= 0x08;
948                 } else if (strlencmp(cmd, "auto_enable") == 0) {
949                         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
950                                 return -EIO;
951                 } else if (strlencmp(cmd, "auto_disable") == 0) {
952                         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
953                                 return -EIO;
954                 } else if (strlencmp(cmd, "video_switch") == 0) {
955                         if (!video_switch())
956                                 return -EIO;
957                 } else if (strlencmp(cmd, "expand_toggle") == 0) {
958                         if (!video_expand())
959                                 return -EIO;
960                 } else
961                         return -EINVAL;
962         }
963
964         if (enable || disable) {
965                 status = (video_status() & 0x0f & ~disable) | enable;
966                 if (!video_switch2(status))
967                         return -EIO;
968         }
969
970         return 0;
971 }
972
973 static void video_exit(void)
974 {
975         acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
976 }
977
978 static int light_supported;
979 static int light_status_supported;
980
981 static int light_init(void)
982 {
983         /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
984         light_supported = (cmos_handle || lght_handle) && !ledb_handle;
985
986         if (light_supported)
987                 /* light status not supported on
988                    570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
989                 light_status_supported = acpi_evalf(ec_handle, NULL,
990                                                     "KBLT", "qv");
991
992         return 0;
993 }
994
995 static int light_read(char *p)
996 {
997         int len = 0;
998         int status = 0;
999
1000         if (!light_supported) {
1001                 len += sprintf(p + len, "status:\t\tnot supported\n");
1002         } else if (!light_status_supported) {
1003                 len += sprintf(p + len, "status:\t\tunknown\n");
1004                 len += sprintf(p + len, "commands:\ton, off\n");
1005         } else {
1006                 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1007                         return -EIO;
1008                 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1009                 len += sprintf(p + len, "commands:\ton, off\n");
1010         }
1011
1012         return len;
1013 }
1014
1015 static int light_write(char *buf)
1016 {
1017         int cmos_cmd, lght_cmd;
1018         char *cmd;
1019         int success;
1020
1021         if (!light_supported)
1022                 return -ENODEV;
1023
1024         while ((cmd = next_cmd(&buf))) {
1025                 if (strlencmp(cmd, "on") == 0) {
1026                         cmos_cmd = 0x0c;
1027                         lght_cmd = 1;
1028                 } else if (strlencmp(cmd, "off") == 0) {
1029                         cmos_cmd = 0x0d;
1030                         lght_cmd = 0;
1031                 } else
1032                         return -EINVAL;
1033
1034                 success = cmos_handle ?
1035                     acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1036                     acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1037                 if (!success)
1038                         return -EIO;
1039         }
1040
1041         return 0;
1042 }
1043
1044 static int _sta(acpi_handle handle)
1045 {
1046         int status;
1047
1048         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1049                 status = 0;
1050
1051         return status;
1052 }
1053
1054 #ifdef CONFIG_ACPI_IBM_DOCK
1055 #define dock_docked() (_sta(dock_handle) & 1)
1056
1057 static int dock_read(char *p)
1058 {
1059         int len = 0;
1060         int docked = dock_docked();
1061
1062         if (!dock_handle)
1063                 len += sprintf(p + len, "status:\t\tnot supported\n");
1064         else if (!docked)
1065                 len += sprintf(p + len, "status:\t\tundocked\n");
1066         else {
1067                 len += sprintf(p + len, "status:\t\tdocked\n");
1068                 len += sprintf(p + len, "commands:\tdock, undock\n");
1069         }
1070
1071         return len;
1072 }
1073
1074 static int dock_write(char *buf)
1075 {
1076         char *cmd;
1077
1078         if (!dock_docked())
1079                 return -ENODEV;
1080
1081         while ((cmd = next_cmd(&buf))) {
1082                 if (strlencmp(cmd, "undock") == 0) {
1083                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1084                             !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1085                                 return -EIO;
1086                 } else if (strlencmp(cmd, "dock") == 0) {
1087                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1088                                 return -EIO;
1089                 } else
1090                         return -EINVAL;
1091         }
1092
1093         return 0;
1094 }
1095
1096 static void dock_notify(struct ibm_struct *ibm, u32 event)
1097 {
1098         int docked = dock_docked();
1099         int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1100
1101         if (event == 1 && !pci) /* 570 */
1102                 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1103         else if (event == 1 && pci)     /* 570 */
1104                 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1105         else if (event == 3 && docked)
1106                 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1107         else if (event == 3 && !docked)
1108                 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1109         else if (event == 0 && docked)
1110                 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1111         else {
1112                 printk(IBM_ERR "unknown dock event %d, status %d\n",
1113                        event, _sta(dock_handle));
1114                 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1115         }
1116 }
1117 #endif
1118
1119 static int bay_status_supported;
1120 static int bay_status2_supported;
1121 static int bay_eject_supported;
1122 static int bay_eject2_supported;
1123
1124 static int bay_init(void)
1125 {
1126         bay_status_supported = bay_handle &&
1127             acpi_evalf(bay_handle, NULL, "_STA", "qv");
1128         bay_status2_supported = bay2_handle &&
1129             acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1130
1131         bay_eject_supported = bay_handle && bay_ej_handle &&
1132             (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1133         bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1134             (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1135
1136         return 0;
1137 }
1138
1139 #define bay_occupied(b) (_sta(b##_handle) & 1)
1140
1141 static int bay_read(char *p)
1142 {
1143         int len = 0;
1144         int occupied = bay_occupied(bay);
1145         int occupied2 = bay_occupied(bay2);
1146         int eject, eject2;
1147
1148         len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1149                        (occupied ? "occupied" : "unoccupied") :
1150                        "not supported");
1151         if (bay_status2_supported)
1152                 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1153                                "occupied" : "unoccupied");
1154
1155         eject = bay_eject_supported && occupied;
1156         eject2 = bay_eject2_supported && occupied2;
1157
1158         if (eject && eject2)
1159                 len += sprintf(p + len, "commands:\teject, eject2\n");
1160         else if (eject)
1161                 len += sprintf(p + len, "commands:\teject\n");
1162         else if (eject2)
1163                 len += sprintf(p + len, "commands:\teject2\n");
1164
1165         return len;
1166 }
1167
1168 static int bay_write(char *buf)
1169 {
1170         char *cmd;
1171
1172         if (!bay_eject_supported && !bay_eject2_supported)
1173                 return -ENODEV;
1174
1175         while ((cmd = next_cmd(&buf))) {
1176                 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1177                         if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1178                                 return -EIO;
1179                 } else if (bay_eject2_supported &&
1180                            strlencmp(cmd, "eject2") == 0) {
1181                         if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1182                                 return -EIO;
1183                 } else
1184                         return -EINVAL;
1185         }
1186
1187         return 0;
1188 }
1189
1190 static void bay_notify(struct ibm_struct *ibm, u32 event)
1191 {
1192         acpi_bus_generate_event(ibm->device, event, 0);
1193 }
1194
1195 static int cmos_read(char *p)
1196 {
1197         int len = 0;
1198
1199         /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1200            R30, R31, T20-22, X20-21 */
1201         if (!cmos_handle)
1202                 len += sprintf(p + len, "status:\t\tnot supported\n");
1203         else {
1204                 len += sprintf(p + len, "status:\t\tsupported\n");
1205                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1206         }
1207
1208         return len;
1209 }
1210
1211 static int cmos_eval(int cmos_cmd)
1212 {
1213         if (cmos_handle)
1214                 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1215         else
1216                 return 1;
1217 }
1218
1219 static int cmos_write(char *buf)
1220 {
1221         char *cmd;
1222         int cmos_cmd;
1223
1224         if (!cmos_handle)
1225                 return -EINVAL;
1226
1227         while ((cmd = next_cmd(&buf))) {
1228                 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1229                     cmos_cmd >= 0 && cmos_cmd <= 21) {
1230                         /* cmos_cmd set */
1231                 } else
1232                         return -EINVAL;
1233
1234                 if (!cmos_eval(cmos_cmd))
1235                         return -EIO;
1236         }
1237
1238         return 0;
1239 }
1240
1241 static int led_supported;
1242
1243 #define LED_570 1
1244 #define LED_OLD 2
1245 #define LED_NEW 3
1246
1247 static int led_init(void)
1248 {
1249         if (!led_handle)
1250                 /* led not supported on R30, R31 */
1251                 led_supported = 0;
1252         else if (strlencmp(led_path, "SLED") == 0)
1253                 /* 570 */
1254                 led_supported = LED_570;
1255         else if (strlencmp(led_path, "SYSL") == 0)
1256                 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1257                 led_supported = LED_OLD;
1258         else
1259                 /* all others */
1260                 led_supported = LED_NEW;
1261
1262         return 0;
1263 }
1264
1265 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1266
1267 static int led_read(char *p)
1268 {
1269         int len = 0;
1270
1271         if (!led_supported) {
1272                 len += sprintf(p + len, "status:\t\tnot supported\n");
1273                 return len;
1274         }
1275         len += sprintf(p + len, "status:\t\tsupported\n");
1276
1277         if (led_supported == LED_570) {
1278                 /* 570 */
1279                 int i, status;
1280                 for (i = 0; i < 8; i++) {
1281                         if (!acpi_evalf(ec_handle,
1282                                         &status, "GLED", "dd", 1 << i))
1283                                 return -EIO;
1284                         len += sprintf(p + len, "%d:\t\t%s\n",
1285                                        i, led_status(status));
1286                 }
1287         }
1288
1289         len += sprintf(p + len, "commands:\t"
1290                        "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1291
1292         return len;
1293 }
1294
1295 /* off, on, blink */
1296 static const int led_sled_arg1[] = { 0, 1, 3 };
1297 static const int led_exp_hlbl[] = { 0, 0, 1 };  /* led# * */
1298 static const int led_exp_hlcl[] = { 0, 1, 1 };  /* led# * */
1299 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1300
1301 #define EC_HLCL 0x0c
1302 #define EC_HLBL 0x0d
1303 #define EC_HLMS 0x0e
1304
1305 static int led_write(char *buf)
1306 {
1307         char *cmd;
1308         int led, ind, ret;
1309
1310         if (!led_supported)
1311                 return -ENODEV;
1312
1313         while ((cmd = next_cmd(&buf))) {
1314                 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1315                         return -EINVAL;
1316
1317                 if (strstr(cmd, "off")) {
1318                         ind = 0;
1319                 } else if (strstr(cmd, "on")) {
1320                         ind = 1;
1321                 } else if (strstr(cmd, "blink")) {
1322                         ind = 2;
1323                 } else
1324                         return -EINVAL;
1325
1326                 if (led_supported == LED_570) {
1327                         /* 570 */
1328                         led = 1 << led;
1329                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1330                                         led, led_sled_arg1[ind]))
1331                                 return -EIO;
1332                 } else if (led_supported == LED_OLD) {
1333                         /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1334                         led = 1 << led;
1335                         ret = ec_write(EC_HLMS, led);
1336                         if (ret >= 0)
1337                                 ret =
1338                                     ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1339                         if (ret >= 0)
1340                                 ret =
1341                                     ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1342                         if (ret < 0)
1343                                 return ret;
1344                 } else {
1345                         /* all others */
1346                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1347                                         led, led_led_arg1[ind]))
1348                                 return -EIO;
1349                 }
1350         }
1351
1352         return 0;
1353 }
1354
1355 static int beep_read(char *p)
1356 {
1357         int len = 0;
1358
1359         if (!beep_handle)
1360                 len += sprintf(p + len, "status:\t\tnot supported\n");
1361         else {
1362                 len += sprintf(p + len, "status:\t\tsupported\n");
1363                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1364         }
1365
1366         return len;
1367 }
1368
1369 static int beep_write(char *buf)
1370 {
1371         char *cmd;
1372         int beep_cmd;
1373
1374         if (!beep_handle)
1375                 return -ENODEV;
1376
1377         while ((cmd = next_cmd(&buf))) {
1378                 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1379                     beep_cmd >= 0 && beep_cmd <= 17) {
1380                         /* beep_cmd set */
1381                 } else
1382                         return -EINVAL;
1383                 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1384                         return -EIO;
1385         }
1386
1387         return 0;
1388 }
1389
1390 static int acpi_ec_read(int i, u8 * p)
1391 {
1392         int v;
1393
1394         if (ecrd_handle) {
1395                 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1396                         return 0;
1397                 *p = v;
1398         } else {
1399                 if (ec_read(i, p) < 0)
1400                         return 0;
1401         }
1402
1403         return 1;
1404 }
1405
1406 static int acpi_ec_write(int i, u8 v)
1407 {
1408         if (ecwr_handle) {
1409                 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1410                         return 0;
1411         } else {
1412                 if (ec_write(i, v) < 0)
1413                         return 0;
1414         }
1415
1416         return 1;
1417 }
1418
1419 static enum thermal_access_mode thermal_read_mode;
1420
1421 static int thermal_init(void)
1422 {
1423         u8 t, ta1, ta2;
1424         int i;
1425         int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1426
1427         if (ibm_thinkpad_ec_found && experimental) {
1428                 /*
1429                  * Direct EC access mode: sensors at registers
1430                  * 0x78-0x7F, 0xC0-0xC7.  Registers return 0x00 for
1431                  * non-implemented, thermal sensors return 0x80 when
1432                  * not available
1433                  */
1434
1435                 ta1 = ta2 = 0;
1436                 for (i = 0; i < 8; i++) {
1437                         if (likely(acpi_ec_read(0x78 + i, &t))) {
1438                                 ta1 |= t;
1439                         } else {
1440                                 ta1 = 0;
1441                                 break;
1442                         }
1443                         if (likely(acpi_ec_read(0xC0 + i, &t))) {
1444                                 ta2 |= t;
1445                         } else {
1446                                 ta1 = 0;
1447                                 break;
1448                         }
1449                 }
1450                 if (ta1 == 0) {
1451                         /* This is sheer paranoia, but we handle it anyway */
1452                         if (acpi_tmp7) {
1453                                 printk(IBM_ERR
1454                                        "ThinkPad ACPI EC access misbehaving, "
1455                                        "falling back to ACPI TMPx access mode\n");
1456                                 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1457                         } else {
1458                                 printk(IBM_ERR
1459                                        "ThinkPad ACPI EC access misbehaving, "
1460                                        "disabling thermal sensors access\n");
1461                                 thermal_read_mode = IBMACPI_THERMAL_NONE;
1462                         }
1463                 } else {
1464                         thermal_read_mode =
1465                             (ta2 != 0) ?
1466                             IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1467                 }
1468         } else if (acpi_tmp7) {
1469                 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1470                         /* 600e/x, 770e, 770x */
1471                         thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1472                 } else {
1473                         /* Standard ACPI TMPx access, max 8 sensors */
1474                         thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1475                 }
1476         } else {
1477                 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1478                 thermal_read_mode = IBMACPI_THERMAL_NONE;
1479         }
1480
1481         return 0;
1482 }
1483
1484 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1485 {
1486         int i, t;
1487         s8 tmp;
1488         char tmpi[] = "TMPi";
1489
1490         if (!s)
1491                 return -EINVAL;
1492
1493         switch (thermal_read_mode) {
1494 #if IBMACPI_MAX_THERMAL_SENSORS >= 16
1495         case IBMACPI_THERMAL_TPEC_16:
1496                 for (i = 0; i < 8; i++) {
1497                         if (!acpi_ec_read(0xC0 + i, &tmp))
1498                                 return -EIO;
1499                         s->temp[i + 8] = tmp * 1000;
1500                 }
1501                 /* fallthrough */
1502 #endif
1503         case IBMACPI_THERMAL_TPEC_8:
1504                 for (i = 0; i < 8; i++) {
1505                         if (!acpi_ec_read(0x78 + i, &tmp))
1506                                 return -EIO;
1507                         s->temp[i] = tmp * 1000;
1508                 }
1509                 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1510
1511         case IBMACPI_THERMAL_ACPI_UPDT:
1512                 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1513                         return -EIO;
1514                 for (i = 0; i < 8; i++) {
1515                         tmpi[3] = '0' + i;
1516                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1517                                 return -EIO;
1518                         s->temp[i] = (t - 2732) * 100;
1519                 }
1520                 return 8;
1521
1522         case IBMACPI_THERMAL_ACPI_TMP07:
1523                 for (i = 0; i < 8; i++) {
1524                         tmpi[3] = '0' + i;
1525                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1526                                 return -EIO;
1527                         s->temp[i] = t * 1000;
1528                 }
1529                 return 8;
1530
1531         case IBMACPI_THERMAL_NONE:
1532         default:
1533                 return 0;
1534         }
1535 }
1536
1537 static int thermal_read(char *p)
1538 {
1539         int len = 0;
1540         int n, i;
1541         struct ibm_thermal_sensors_struct t;
1542
1543         n = thermal_get_sensors(&t);
1544         if (unlikely(n < 0))
1545                 return n;
1546
1547         len += sprintf(p + len, "temperatures:\t");
1548
1549         if (n > 0) {
1550                 for (i = 0; i < (n - 1); i++)
1551                         len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1552                 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1553         } else
1554                 len += sprintf(p + len, "not supported\n");
1555
1556         return len;
1557 }
1558
1559 static u8 ecdump_regs[256];
1560
1561 static int ecdump_read(char *p)
1562 {
1563         int len = 0;
1564         int i, j;
1565         u8 v;
1566
1567         len += sprintf(p + len, "EC      "
1568                        " +00 +01 +02 +03 +04 +05 +06 +07"
1569                        " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1570         for (i = 0; i < 256; i += 16) {
1571                 len += sprintf(p + len, "EC 0x%02x:", i);
1572                 for (j = 0; j < 16; j++) {
1573                         if (!acpi_ec_read(i + j, &v))
1574                                 break;
1575                         if (v != ecdump_regs[i + j])
1576                                 len += sprintf(p + len, " *%02x", v);
1577                         else
1578                                 len += sprintf(p + len, "  %02x", v);
1579                         ecdump_regs[i + j] = v;
1580                 }
1581                 len += sprintf(p + len, "\n");
1582                 if (j != 16)
1583                         break;
1584         }
1585
1586         /* These are way too dangerous to advertise openly... */
1587 #if 0
1588         len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1589                        " (<offset> is 00-ff, <value> is 00-ff)\n");
1590         len += sprintf(p + len, "commands:\t0x<offset> <value>  "
1591                        " (<offset> is 00-ff, <value> is 0-255)\n");
1592 #endif
1593         return len;
1594 }
1595
1596 static int ecdump_write(char *buf)
1597 {
1598         char *cmd;
1599         int i, v;
1600
1601         while ((cmd = next_cmd(&buf))) {
1602                 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1603                         /* i and v set */
1604                 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1605                         /* i and v set */
1606                 } else
1607                         return -EINVAL;
1608                 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1609                         if (!acpi_ec_write(i, v))
1610                                 return -EIO;
1611                 } else
1612                         return -EINVAL;
1613         }
1614
1615         return 0;
1616 }
1617
1618 static int brightness_offset = 0x31;
1619
1620 static int brightness_get(struct backlight_device *bd)
1621 {
1622         u8 level;
1623         if (!acpi_ec_read(brightness_offset, &level))
1624                 return -EIO;
1625
1626         level &= 0x7;
1627         return level;
1628 }
1629
1630 static int brightness_read(char *p)
1631 {
1632         int len = 0;
1633         int level;
1634
1635         if ((level = brightness_get(NULL)) < 0) {
1636                 len += sprintf(p + len, "level:\t\tunreadable\n");
1637         } else {
1638                 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1639                 len += sprintf(p + len, "commands:\tup, down\n");
1640                 len += sprintf(p + len, "commands:\tlevel <level>"
1641                                " (<level> is 0-7)\n");
1642         }
1643
1644         return len;
1645 }
1646
1647 #define BRIGHTNESS_UP   4
1648 #define BRIGHTNESS_DOWN 5
1649
1650 static int brightness_set(int value)
1651 {
1652         int cmos_cmd, inc, i;
1653         int current_value = brightness_get(NULL);
1654
1655         value &= 7;
1656
1657         cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1658         inc = value > current_value ? 1 : -1;
1659         for (i = current_value; i != value; i += inc) {
1660                 if (!cmos_eval(cmos_cmd))
1661                         return -EIO;
1662                 if (!acpi_ec_write(brightness_offset, i + inc))
1663                         return -EIO;
1664         }
1665
1666         return 0;
1667 }
1668
1669 static int brightness_write(char *buf)
1670 {
1671         int level;
1672         int new_level;
1673         char *cmd;
1674
1675         while ((cmd = next_cmd(&buf))) {
1676                 if ((level = brightness_get(NULL)) < 0)
1677                         return level;
1678                 level &= 7;
1679
1680                 if (strlencmp(cmd, "up") == 0) {
1681                         new_level = level == 7 ? 7 : level + 1;
1682                 } else if (strlencmp(cmd, "down") == 0) {
1683                         new_level = level == 0 ? 0 : level - 1;
1684                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1685                            new_level >= 0 && new_level <= 7) {
1686                         /* new_level set */
1687                 } else
1688                         return -EINVAL;
1689
1690                 brightness_set(new_level);
1691         }
1692
1693         return 0;
1694 }
1695
1696 static int brightness_update_status(struct backlight_device *bd)
1697 {
1698         return brightness_set(bd->props->brightness);
1699 }
1700
1701 static int volume_offset = 0x30;
1702
1703 static int volume_read(char *p)
1704 {
1705         int len = 0;
1706         u8 level;
1707
1708         if (!acpi_ec_read(volume_offset, &level)) {
1709                 len += sprintf(p + len, "level:\t\tunreadable\n");
1710         } else {
1711                 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1712                 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1713                 len += sprintf(p + len, "commands:\tup, down, mute\n");
1714                 len += sprintf(p + len, "commands:\tlevel <level>"
1715                                " (<level> is 0-15)\n");
1716         }
1717
1718         return len;
1719 }
1720
1721 #define VOLUME_DOWN     0
1722 #define VOLUME_UP       1
1723 #define VOLUME_MUTE     2
1724
1725 static int volume_write(char *buf)
1726 {
1727         int cmos_cmd, inc, i;
1728         u8 level, mute;
1729         int new_level, new_mute;
1730         char *cmd;
1731
1732         while ((cmd = next_cmd(&buf))) {
1733                 if (!acpi_ec_read(volume_offset, &level))
1734                         return -EIO;
1735                 new_mute = mute = level & 0x40;
1736                 new_level = level = level & 0xf;
1737
1738                 if (strlencmp(cmd, "up") == 0) {
1739                         if (mute)
1740                                 new_mute = 0;
1741                         else
1742                                 new_level = level == 15 ? 15 : level + 1;
1743                 } else if (strlencmp(cmd, "down") == 0) {
1744                         if (mute)
1745                                 new_mute = 0;
1746                         else
1747                                 new_level = level == 0 ? 0 : level - 1;
1748                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1749                            new_level >= 0 && new_level <= 15) {
1750                         /* new_level set */
1751                 } else if (strlencmp(cmd, "mute") == 0) {
1752                         new_mute = 0x40;
1753                 } else
1754                         return -EINVAL;
1755
1756                 if (new_level != level) {       /* mute doesn't change */
1757                         cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1758                         inc = new_level > level ? 1 : -1;
1759
1760                         if (mute && (!cmos_eval(cmos_cmd) ||
1761                                      !acpi_ec_write(volume_offset, level)))
1762                                 return -EIO;
1763
1764                         for (i = level; i != new_level; i += inc)
1765                                 if (!cmos_eval(cmos_cmd) ||
1766                                     !acpi_ec_write(volume_offset, i + inc))
1767                                         return -EIO;
1768
1769                         if (mute && (!cmos_eval(VOLUME_MUTE) ||
1770                                      !acpi_ec_write(volume_offset,
1771                                                     new_level + mute)))
1772                                 return -EIO;
1773                 }
1774
1775                 if (new_mute != mute) { /* level doesn't change */
1776                         cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1777
1778                         if (!cmos_eval(cmos_cmd) ||
1779                             !acpi_ec_write(volume_offset, level + new_mute))
1780                                 return -EIO;
1781                 }
1782         }
1783
1784         return 0;
1785 }
1786
1787 static enum fan_status_access_mode fan_status_access_mode;
1788 static enum fan_control_access_mode fan_control_access_mode;
1789 static enum fan_control_commands fan_control_commands;
1790
1791 static int fan_init(void)
1792 {
1793         u8 status;
1794
1795         fan_status_access_mode = IBMACPI_FAN_NONE;
1796         fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1797         fan_control_commands = 0;
1798
1799         if (gfan_handle) {
1800                 /* 570, 600e/x, 770e, 770x */
1801                 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1802         } else {
1803                 /* all other ThinkPads: note that even old-style
1804                  * ThinkPad ECs supports the fan control register */
1805                 if (likely(acpi_ec_read(fan_status_offset, &status))) {
1806                         fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1807                 } else {
1808                         printk(IBM_ERR
1809                                "ThinkPad ACPI EC access misbehaving, "
1810                                "fan status and control unavailable\n");
1811                         return 0;
1812                 }
1813         }
1814
1815         if (sfan_handle) {
1816                 /* 570, 770x-JL */
1817                 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
1818                 fan_control_commands |= IBMACPI_FAN_CMD_LEVEL;
1819         } else {
1820                 if (!gfan_handle) {
1821                         /* gfan without sfan means no fan control */
1822                         /* all other models implement TP EC 0x2f control */
1823
1824                         if (fans_handle) {
1825                                 /* X31, X40, X41 */
1826                                 fan_control_access_mode =
1827                                     IBMACPI_FAN_WR_ACPI_FANS;
1828                                 fan_control_commands |=
1829                                     IBMACPI_FAN_CMD_SPEED |
1830                                     IBMACPI_FAN_CMD_ENABLE;
1831                         } else {
1832                                 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
1833                                 fan_control_commands |= IBMACPI_FAN_CMD_ENABLE;
1834                         }
1835                 }
1836         }
1837
1838         return 0;
1839 }
1840
1841 static int fan_get_status(u8 *status)
1842 {
1843         u8 s;
1844
1845         /* TODO:
1846          * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1847
1848         switch (fan_status_access_mode) {
1849         case IBMACPI_FAN_RD_ACPI_GFAN:
1850                 /* 570, 600e/x, 770e, 770x */
1851
1852                 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
1853                         return -EIO;
1854
1855                 if (likely(status))
1856                         *status = s & 0x07;
1857
1858                 break;
1859
1860         case IBMACPI_FAN_RD_TPEC:
1861                 /* all except 570, 600e/x, 770e, 770x */
1862                 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1863                         return -EIO;
1864
1865                 if (likely(status))
1866                         *status = s;
1867
1868                 break;
1869
1870         default:
1871                 return -ENXIO;
1872         }
1873
1874         return 0;
1875 }
1876
1877 static int fan_get_speed(unsigned int *speed)
1878 {
1879         u8 hi, lo;
1880
1881         switch (fan_status_access_mode) {
1882         case IBMACPI_FAN_RD_TPEC:
1883                 /* all except 570, 600e/x, 770e, 770x */
1884                 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1885                              !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1886                         return -EIO;
1887
1888                 if (likely(speed))
1889                         *speed = (hi << 8) | lo;
1890
1891                 break;
1892
1893         default:
1894                 return -ENXIO;
1895         }
1896
1897         return 0;
1898 }
1899
1900 static int fan_read(char *p)
1901 {
1902         int len = 0;
1903         int rc;
1904         u8 status;
1905         unsigned int speed = 0;
1906
1907         switch (fan_status_access_mode) {
1908         case IBMACPI_FAN_RD_ACPI_GFAN:
1909                 /* 570, 600e/x, 770e, 770x */
1910                 if ((rc = fan_get_status(&status)) < 0)
1911                         return rc;
1912
1913                 len += sprintf(p + len, "level:\t\t%d\n", status);
1914
1915                 break;
1916
1917         case IBMACPI_FAN_RD_TPEC:
1918                 /* all except 570, 600e/x, 770e, 770x */
1919                 if ((rc = fan_get_status(&status)) < 0)
1920                         return rc;
1921
1922                 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 7));
1923
1924                 if ((rc = fan_get_speed(&speed)) < 0)
1925                         return rc;
1926
1927                 len += sprintf(p + len, "speed:\t\t%d\n", speed);
1928                 break;
1929
1930         case IBMACPI_FAN_NONE:
1931         default:
1932                 len += sprintf(p + len, "status:\t\tnot supported\n");
1933         }
1934
1935         if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL)
1936                 len += sprintf(p + len, "commands:\tlevel <level>"
1937                                " (<level> is 0-7)\n");
1938
1939         if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
1940                 len += sprintf(p + len, "commands:\tenable, disable\n");
1941
1942         if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
1943                 len += sprintf(p + len, "commands:\tspeed <speed>"
1944                                " (<speed> is 0-65535)\n");
1945
1946         return len;
1947 }
1948
1949 static int fan_set_level(int level)
1950 {
1951         switch (fan_control_access_mode) {
1952         case IBMACPI_FAN_WR_ACPI_SFAN:
1953                 if (level >= 0 && level <= 7) {
1954                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
1955                                 return -EIO;
1956                 } else
1957                         return -EINVAL;
1958                 break;
1959
1960         default:
1961                 return -ENXIO;
1962         }
1963         return 0;
1964 }
1965
1966 static int fan_set_enable(void)
1967 {
1968         switch (fan_control_access_mode) {
1969         case IBMACPI_FAN_WR_ACPI_FANS:
1970         case IBMACPI_FAN_WR_TPEC:
1971                 if (!acpi_ec_write(fan_status_offset, 0x80))
1972                         return -EIO;
1973                 break;
1974
1975         default:
1976                 return -ENXIO;
1977         }
1978         return 0;
1979 }
1980
1981 static int fan_set_disable(void)
1982 {
1983         switch (fan_control_access_mode) {
1984         case IBMACPI_FAN_WR_ACPI_FANS:
1985         case IBMACPI_FAN_WR_TPEC:
1986                 if (!acpi_ec_write(fan_status_offset, 0x00))
1987                         return -EIO;
1988                 break;
1989
1990         default:
1991                 return -ENXIO;
1992         }
1993         return 0;
1994 }
1995
1996 static int fan_set_speed(int speed)
1997 {
1998         switch (fan_control_access_mode) {
1999         case IBMACPI_FAN_WR_ACPI_FANS:
2000                 if (speed >= 0 && speed <= 65535) {
2001                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2002                                         speed, speed, speed))
2003                                 return -EIO;
2004                 } else
2005                         return -EINVAL;
2006                 break;
2007
2008         default:
2009                 return -ENXIO;
2010         }
2011         return 0;
2012 }
2013
2014 static int fan_write_cmd_level(const char *cmd, int *rc)
2015 {
2016         int level;
2017
2018         if (sscanf(cmd, "level %d", &level) != 1)
2019                 return 0;
2020
2021         if ((*rc = fan_set_level(level)) == -ENXIO)
2022                 printk(IBM_ERR "level command accepted for unsupported "
2023                        "access mode %d", fan_control_access_mode);
2024
2025         return 1;
2026 }
2027
2028 static int fan_write_cmd_enable(const char *cmd, int *rc)
2029 {
2030         if (strlencmp(cmd, "enable") != 0)
2031                 return 0;
2032
2033         if ((*rc = fan_set_enable()) == -ENXIO)
2034                 printk(IBM_ERR "enable command accepted for unsupported "
2035                        "access mode %d", fan_control_access_mode);
2036
2037         return 1;
2038 }
2039
2040 static int fan_write_cmd_disable(const char *cmd, int *rc)
2041 {
2042         if (strlencmp(cmd, "disable") != 0)
2043                 return 0;
2044
2045         if ((*rc = fan_set_disable()) == -ENXIO)
2046                 printk(IBM_ERR "disable command accepted for unsupported "
2047                        "access mode %d", fan_control_access_mode);
2048
2049         return 1;
2050 }
2051
2052 static int fan_write_cmd_speed(const char *cmd, int *rc)
2053 {
2054         int speed;
2055
2056         /* TODO:
2057          * Support speed <low> <medium> <high> ? */
2058
2059         if (sscanf(cmd, "speed %d", &speed) != 1)
2060                 return 0;
2061
2062         if ((*rc = fan_set_speed(speed)) == -ENXIO)
2063                 printk(IBM_ERR "speed command accepted for unsupported "
2064                        "access mode %d", fan_control_access_mode);
2065
2066         return 1;
2067 }
2068
2069 static int fan_write(char *buf)
2070 {
2071         char *cmd;
2072         int rc = 0;
2073
2074         while (!rc && (cmd = next_cmd(&buf))) {
2075                 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2076                       fan_write_cmd_level(cmd, &rc)) &&
2077                     !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2078                       (fan_write_cmd_enable(cmd, &rc) ||
2079                        fan_write_cmd_disable(cmd, &rc))) &&
2080                     !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2081                       fan_write_cmd_speed(cmd, &rc))
2082                     )
2083                         rc = -EINVAL;
2084         }
2085
2086         return rc;
2087 }
2088
2089 static struct ibm_struct ibms[] = {
2090         {
2091          .name = "driver",
2092          .init = driver_init,
2093          .read = driver_read,
2094          },
2095         {
2096          .name = "hotkey",
2097          .hid = IBM_HKEY_HID,
2098          .init = hotkey_init,
2099          .read = hotkey_read,
2100          .write = hotkey_write,
2101          .exit = hotkey_exit,
2102          .notify = hotkey_notify,
2103          .handle = &hkey_handle,
2104          .type = ACPI_DEVICE_NOTIFY,
2105          },
2106         {
2107          .name = "bluetooth",
2108          .init = bluetooth_init,
2109          .read = bluetooth_read,
2110          .write = bluetooth_write,
2111          },
2112         {
2113          .name = "wan",
2114          .init = wan_init,
2115          .read = wan_read,
2116          .write = wan_write,
2117          .experimental = 1,
2118          },
2119         {
2120          .name = "video",
2121          .init = video_init,
2122          .read = video_read,
2123          .write = video_write,
2124          .exit = video_exit,
2125          },
2126         {
2127          .name = "light",
2128          .init = light_init,
2129          .read = light_read,
2130          .write = light_write,
2131          },
2132 #ifdef CONFIG_ACPI_IBM_DOCK
2133         {
2134          .name = "dock",
2135          .read = dock_read,
2136          .write = dock_write,
2137          .notify = dock_notify,
2138          .handle = &dock_handle,
2139          .type = ACPI_SYSTEM_NOTIFY,
2140          },
2141         {
2142          .name = "dock",
2143          .hid = IBM_PCI_HID,
2144          .notify = dock_notify,
2145          .handle = &pci_handle,
2146          .type = ACPI_SYSTEM_NOTIFY,
2147          },
2148 #endif
2149         {
2150          .name = "bay",
2151          .init = bay_init,
2152          .read = bay_read,
2153          .write = bay_write,
2154          .notify = bay_notify,
2155          .handle = &bay_handle,
2156          .type = ACPI_SYSTEM_NOTIFY,
2157          },
2158         {
2159          .name = "cmos",
2160          .read = cmos_read,
2161          .write = cmos_write,
2162          },
2163         {
2164          .name = "led",
2165          .init = led_init,
2166          .read = led_read,
2167          .write = led_write,
2168          },
2169         {
2170          .name = "beep",
2171          .read = beep_read,
2172          .write = beep_write,
2173          },
2174         {
2175          .name = "thermal",
2176          .init = thermal_init,
2177          .read = thermal_read,
2178          },
2179         {
2180          .name = "ecdump",
2181          .read = ecdump_read,
2182          .write = ecdump_write,
2183          .experimental = 1,
2184          },
2185         {
2186          .name = "brightness",
2187          .read = brightness_read,
2188          .write = brightness_write,
2189          },
2190         {
2191          .name = "volume",
2192          .read = volume_read,
2193          .write = volume_write,
2194          },
2195         {
2196          .name = "fan",
2197          .read = fan_read,
2198          .write = fan_write,
2199          .init = fan_init,
2200          .experimental = 1,
2201          },
2202 };
2203
2204 static int dispatch_read(char *page, char **start, off_t off, int count,
2205                          int *eof, void *data)
2206 {
2207         struct ibm_struct *ibm = (struct ibm_struct *)data;
2208         int len;
2209
2210         if (!ibm || !ibm->read)
2211                 return -EINVAL;
2212
2213         len = ibm->read(page);
2214         if (len < 0)
2215                 return len;
2216
2217         if (len <= off + count)
2218                 *eof = 1;
2219         *start = page + off;
2220         len -= off;
2221         if (len > count)
2222                 len = count;
2223         if (len < 0)
2224                 len = 0;
2225
2226         return len;
2227 }
2228
2229 static int dispatch_write(struct file *file, const char __user * userbuf,
2230                           unsigned long count, void *data)
2231 {
2232         struct ibm_struct *ibm = (struct ibm_struct *)data;
2233         char *kernbuf;
2234         int ret;
2235
2236         if (!ibm || !ibm->write)
2237                 return -EINVAL;
2238
2239         kernbuf = kmalloc(count + 2, GFP_KERNEL);
2240         if (!kernbuf)
2241                 return -ENOMEM;
2242
2243         if (copy_from_user(kernbuf, userbuf, count)) {
2244                 kfree(kernbuf);
2245                 return -EFAULT;
2246         }
2247
2248         kernbuf[count] = 0;
2249         strcat(kernbuf, ",");
2250         ret = ibm->write(kernbuf);
2251         if (ret == 0)
2252                 ret = count;
2253
2254         kfree(kernbuf);
2255
2256         return ret;
2257 }
2258
2259 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2260 {
2261         struct ibm_struct *ibm = (struct ibm_struct *)data;
2262
2263         if (!ibm || !ibm->notify)
2264                 return;
2265
2266         ibm->notify(ibm, event);
2267 }
2268
2269 static int __init setup_notify(struct ibm_struct *ibm)
2270 {
2271         acpi_status status;
2272         int ret;
2273
2274         if (!*ibm->handle)
2275                 return 0;
2276
2277         ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2278         if (ret < 0) {
2279                 printk(IBM_ERR "%s device not present\n", ibm->name);
2280                 return 0;
2281         }
2282
2283         acpi_driver_data(ibm->device) = ibm;
2284         sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2285
2286         status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2287                                              dispatch_notify, ibm);
2288         if (ACPI_FAILURE(status)) {
2289                 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2290                        ibm->name, status);
2291                 return -ENODEV;
2292         }
2293
2294         return 0;
2295 }
2296
2297 static int __init ibm_device_add(struct acpi_device *device)
2298 {
2299         return 0;
2300 }
2301
2302 static int __init register_driver(struct ibm_struct *ibm)
2303 {
2304         int ret;
2305
2306         ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2307         if (!ibm->driver) {
2308                 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2309                 return -1;
2310         }
2311
2312         memset(ibm->driver, 0, sizeof(struct acpi_driver));
2313         sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
2314         ibm->driver->ids = ibm->hid;
2315         ibm->driver->ops.add = &ibm_device_add;
2316
2317         ret = acpi_bus_register_driver(ibm->driver);
2318         if (ret < 0) {
2319                 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2320                        ibm->hid, ret);
2321                 kfree(ibm->driver);
2322         }
2323
2324         return ret;
2325 }
2326
2327 static int __init ibm_init(struct ibm_struct *ibm)
2328 {
2329         int ret;
2330         struct proc_dir_entry *entry;
2331
2332         if (ibm->experimental && !experimental)
2333                 return 0;
2334
2335         if (ibm->hid) {
2336                 ret = register_driver(ibm);
2337                 if (ret < 0)
2338                         return ret;
2339                 ibm->driver_registered = 1;
2340         }
2341
2342         if (ibm->init) {
2343                 ret = ibm->init();
2344                 if (ret != 0)
2345                         return ret;
2346                 ibm->init_called = 1;
2347         }
2348
2349         if (ibm->read) {
2350                 entry = create_proc_entry(ibm->name,
2351                                           S_IFREG | S_IRUGO | S_IWUSR,
2352                                           proc_dir);
2353                 if (!entry) {
2354                         printk(IBM_ERR "unable to create proc entry %s\n",
2355                                ibm->name);
2356                         return -ENODEV;
2357                 }
2358                 entry->owner = THIS_MODULE;
2359                 entry->data = ibm;
2360                 entry->read_proc = &dispatch_read;
2361                 if (ibm->write)
2362                         entry->write_proc = &dispatch_write;
2363                 ibm->proc_created = 1;
2364         }
2365
2366         if (ibm->notify) {
2367                 ret = setup_notify(ibm);
2368                 if (ret < 0)
2369                         return ret;
2370                 ibm->notify_installed = 1;
2371         }
2372
2373         return 0;
2374 }
2375
2376 static void ibm_exit(struct ibm_struct *ibm)
2377 {
2378         if (ibm->notify_installed)
2379                 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2380                                            dispatch_notify);
2381
2382         if (ibm->proc_created)
2383                 remove_proc_entry(ibm->name, proc_dir);
2384
2385         if (ibm->init_called && ibm->exit)
2386                 ibm->exit();
2387
2388         if (ibm->driver_registered) {
2389                 acpi_bus_unregister_driver(ibm->driver);
2390                 kfree(ibm->driver);
2391         }
2392 }
2393
2394 static void __init ibm_handle_init(char *name,
2395                                    acpi_handle * handle, acpi_handle parent,
2396                                    char **paths, int num_paths, char **path)
2397 {
2398         int i;
2399         acpi_status status;
2400
2401         for (i = 0; i < num_paths; i++) {
2402                 status = acpi_get_handle(parent, paths[i], handle);
2403                 if (ACPI_SUCCESS(status)) {
2404                         *path = paths[i];
2405                         return;
2406                 }
2407         }
2408
2409         *handle = NULL;
2410 }
2411
2412 #define IBM_HANDLE_INIT(object)                                         \
2413         ibm_handle_init(#object, &object##_handle, *object##_parent,    \
2414                 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
2415
2416 static int set_ibm_param(const char *val, struct kernel_param *kp)
2417 {
2418         unsigned int i;
2419
2420         for (i = 0; i < ARRAY_SIZE(ibms); i++)
2421                 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2422                         if (strlen(val) > sizeof(ibms[i].param) - 2)
2423                                 return -ENOSPC;
2424                         strcpy(ibms[i].param, val);
2425                         strcat(ibms[i].param, ",");
2426                         return 0;
2427                 }
2428
2429         return -EINVAL;
2430 }
2431
2432 #define IBM_PARAM(feature) \
2433         module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2434
2435 IBM_PARAM(hotkey);
2436 IBM_PARAM(bluetooth);
2437 IBM_PARAM(video);
2438 IBM_PARAM(light);
2439 #ifdef CONFIG_ACPI_IBM_DOCK
2440 IBM_PARAM(dock);
2441 #endif
2442 IBM_PARAM(bay);
2443 IBM_PARAM(cmos);
2444 IBM_PARAM(led);
2445 IBM_PARAM(beep);
2446 IBM_PARAM(ecdump);
2447 IBM_PARAM(brightness);
2448 IBM_PARAM(volume);
2449 IBM_PARAM(fan);
2450
2451 static struct backlight_properties ibm_backlight_data = {
2452         .owner = THIS_MODULE,
2453         .get_brightness = brightness_get,
2454         .update_status = brightness_update_status,
2455         .max_brightness = 7,
2456 };
2457
2458 static void acpi_ibm_exit(void)
2459 {
2460         int i;
2461
2462         if (ibm_backlight_device)
2463                 backlight_device_unregister(ibm_backlight_device);
2464
2465         for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2466                 ibm_exit(&ibms[i]);
2467
2468         remove_proc_entry(IBM_DIR, acpi_root_dir);
2469 }
2470
2471 static int __init check_dmi_for_ec(void)
2472 {
2473         struct dmi_device *dev = NULL;
2474
2475         /*
2476          * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2477          * X32 or newer, all Z series;  Some models must have an
2478          * up-to-date BIOS or they will not be detected.
2479          *
2480          * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2481          */
2482         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2483                 if (strstr(dev->name, "IBM ThinkPad Embedded Controller"))
2484                         return 1;
2485         }
2486         return 0;
2487 }
2488
2489 static int __init acpi_ibm_init(void)
2490 {
2491         int ret, i;
2492
2493         if (acpi_disabled)
2494                 return -ENODEV;
2495
2496         if (!acpi_specific_hotkey_enabled) {
2497                 printk(IBM_ERR "using generic hotkey driver\n");
2498                 return -ENODEV;
2499         }
2500
2501         /* ec is required because many other handles are relative to it */
2502         IBM_HANDLE_INIT(ec);
2503         if (!ec_handle) {
2504                 printk(IBM_ERR "ec object not found\n");
2505                 return -ENODEV;
2506         }
2507
2508         /* Models with newer firmware report the EC in DMI */
2509         ibm_thinkpad_ec_found = check_dmi_for_ec();
2510
2511         /* these handles are not required */
2512         IBM_HANDLE_INIT(vid);
2513         IBM_HANDLE_INIT(vid2);
2514         IBM_HANDLE_INIT(ledb);
2515         IBM_HANDLE_INIT(led);
2516         IBM_HANDLE_INIT(hkey);
2517         IBM_HANDLE_INIT(lght);
2518         IBM_HANDLE_INIT(cmos);
2519 #ifdef CONFIG_ACPI_IBM_DOCK
2520         IBM_HANDLE_INIT(dock);
2521 #endif
2522         IBM_HANDLE_INIT(pci);
2523         IBM_HANDLE_INIT(bay);
2524         if (bay_handle)
2525                 IBM_HANDLE_INIT(bay_ej);
2526         IBM_HANDLE_INIT(bay2);
2527         if (bay2_handle)
2528                 IBM_HANDLE_INIT(bay2_ej);
2529         IBM_HANDLE_INIT(beep);
2530         IBM_HANDLE_INIT(ecrd);
2531         IBM_HANDLE_INIT(ecwr);
2532         IBM_HANDLE_INIT(fans);
2533         IBM_HANDLE_INIT(gfan);
2534         IBM_HANDLE_INIT(sfan);
2535
2536         proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2537         if (!proc_dir) {
2538                 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2539                 return -ENODEV;
2540         }
2541         proc_dir->owner = THIS_MODULE;
2542
2543         for (i = 0; i < ARRAY_SIZE(ibms); i++) {
2544                 ret = ibm_init(&ibms[i]);
2545                 if (ret >= 0 && *ibms[i].param)
2546                         ret = ibms[i].write(ibms[i].param);
2547                 if (ret < 0) {
2548                         acpi_ibm_exit();
2549                         return ret;
2550                 }
2551         }
2552
2553         ibm_backlight_device = backlight_device_register("ibm", NULL,
2554                                                          &ibm_backlight_data);
2555         if (IS_ERR(ibm_backlight_device)) {
2556                 printk(IBM_ERR "Could not register ibm backlight device\n");
2557                 ibm_backlight_device = NULL;
2558                 acpi_ibm_exit();
2559         }
2560
2561         return 0;
2562 }
2563
2564 module_init(acpi_ibm_init);
2565 module_exit(acpi_ibm_exit);