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