ACPI: ibm-acpi: store embedded controller firmware version for matching
[pandora-kernel.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_init(void)
1798 {
1799         u8 status;
1800
1801         fan_status_access_mode = IBMACPI_FAN_NONE;
1802         fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1803         fan_control_commands = 0;
1804
1805         if (gfan_handle) {
1806                 /* 570, 600e/x, 770e, 770x */
1807                 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1808         } else {
1809                 /* all other ThinkPads: note that even old-style
1810                  * ThinkPad ECs supports the fan control register */
1811                 if (likely(acpi_ec_read(fan_status_offset, &status))) {
1812                         fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1813                 } else {
1814                         printk(IBM_ERR
1815                                "ThinkPad ACPI EC access misbehaving, "
1816                                "fan status and control unavailable\n");
1817                         return 0;
1818                 }
1819         }
1820
1821         if (sfan_handle) {
1822                 /* 570, 770x-JL */
1823                 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
1824                 fan_control_commands |=
1825                     IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
1826         } else {
1827                 if (!gfan_handle) {
1828                         /* gfan without sfan means no fan control */
1829                         /* all other models implement TP EC 0x2f control */
1830
1831                         if (fans_handle) {
1832                                 /* X31, X40, X41 */
1833                                 fan_control_access_mode =
1834                                     IBMACPI_FAN_WR_ACPI_FANS;
1835                                 fan_control_commands |=
1836                                     IBMACPI_FAN_CMD_SPEED |
1837                                     IBMACPI_FAN_CMD_LEVEL |
1838                                     IBMACPI_FAN_CMD_ENABLE;
1839                         } else {
1840                                 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
1841                                 fan_control_commands |=
1842                                     IBMACPI_FAN_CMD_LEVEL |
1843                                     IBMACPI_FAN_CMD_ENABLE;
1844                         }
1845                 }
1846         }
1847
1848         return 0;
1849 }
1850
1851 static int fan_get_status(u8 *status)
1852 {
1853         u8 s;
1854
1855         /* TODO:
1856          * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1857
1858         switch (fan_status_access_mode) {
1859         case IBMACPI_FAN_RD_ACPI_GFAN:
1860                 /* 570, 600e/x, 770e, 770x */
1861
1862                 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
1863                         return -EIO;
1864
1865                 if (likely(status))
1866                         *status = s & 0x07;
1867
1868                 break;
1869
1870         case IBMACPI_FAN_RD_TPEC:
1871                 /* all except 570, 600e/x, 770e, 770x */
1872                 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1873                         return -EIO;
1874
1875                 if (likely(status))
1876                         *status = s;
1877
1878                 break;
1879
1880         default:
1881                 return -ENXIO;
1882         }
1883
1884         return 0;
1885 }
1886
1887 static int fan_get_speed(unsigned int *speed)
1888 {
1889         u8 hi, lo;
1890
1891         switch (fan_status_access_mode) {
1892         case IBMACPI_FAN_RD_TPEC:
1893                 /* all except 570, 600e/x, 770e, 770x */
1894                 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1895                              !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1896                         return -EIO;
1897
1898                 if (likely(speed))
1899                         *speed = (hi << 8) | lo;
1900
1901                 break;
1902
1903         default:
1904                 return -ENXIO;
1905         }
1906
1907         return 0;
1908 }
1909
1910 static int fan_read(char *p)
1911 {
1912         int len = 0;
1913         int rc;
1914         u8 status;
1915         unsigned int speed = 0;
1916
1917         switch (fan_status_access_mode) {
1918         case IBMACPI_FAN_RD_ACPI_GFAN:
1919                 /* 570, 600e/x, 770e, 770x */
1920                 if ((rc = fan_get_status(&status)) < 0)
1921                         return rc;
1922
1923                 len += sprintf(p + len, "status:\t\t%s\n"
1924                                "level:\t\t%d\n",
1925                                (status != 0) ? "enabled" : "disabled", status);
1926                 break;
1927
1928         case IBMACPI_FAN_RD_TPEC:
1929                 /* all except 570, 600e/x, 770e, 770x */
1930                 if ((rc = fan_get_status(&status)) < 0)
1931                         return rc;
1932
1933                 len += sprintf(p + len, "status:\t\t%s\n",
1934                                (status != 0) ? "enabled" : "disabled");
1935
1936                 if ((rc = fan_get_speed(&speed)) < 0)
1937                         return rc;
1938
1939                 len += sprintf(p + len, "speed:\t\t%d\n", speed);
1940
1941                 if (status & IBMACPI_FAN_EC_DISENGAGED)
1942                         /* Disengaged mode takes precedence */
1943                         len += sprintf(p + len, "level:\t\tdisengaged\n");
1944                 else if (status & IBMACPI_FAN_EC_AUTO)
1945                         len += sprintf(p + len, "level:\t\tauto\n");
1946                 else
1947                         len += sprintf(p + len, "level:\t\t%d\n", status);
1948                 break;
1949
1950         case IBMACPI_FAN_NONE:
1951         default:
1952                 len += sprintf(p + len, "status:\t\tnot supported\n");
1953         }
1954
1955         if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
1956                 len += sprintf(p + len, "commands:\tlevel <level>");
1957
1958                 switch (fan_control_access_mode) {
1959                 case IBMACPI_FAN_WR_ACPI_SFAN:
1960                         len += sprintf(p + len, " (<level> is 0-7)\n");
1961                         break;
1962
1963                 default:
1964                         len += sprintf(p + len, " (<level> is 0-7, "
1965                                        "auto, disengaged)\n");
1966                         break;
1967                 }
1968         }
1969
1970         if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
1971                 len += sprintf(p + len, "commands:\tenable, disable\n");
1972
1973         if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
1974                 len += sprintf(p + len, "commands:\tspeed <speed>"
1975                                " (<speed> is 0-65535)\n");
1976
1977         return len;
1978 }
1979
1980 static int fan_set_level(int level)
1981 {
1982         switch (fan_control_access_mode) {
1983         case IBMACPI_FAN_WR_ACPI_SFAN:
1984                 if (level >= 0 && level <= 7) {
1985                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
1986                                 return -EIO;
1987                 } else
1988                         return -EINVAL;
1989                 break;
1990
1991         case IBMACPI_FAN_WR_ACPI_FANS:
1992         case IBMACPI_FAN_WR_TPEC:
1993                 if ((level != IBMACPI_FAN_EC_AUTO) &&
1994                     (level != IBMACPI_FAN_EC_DISENGAGED) &&
1995                     ((level < 0) || (level > 7)))
1996                         return -EINVAL;
1997
1998                 if (!acpi_ec_write(fan_status_offset, level))
1999                         return -EIO;
2000                 break;
2001
2002         default:
2003                 return -ENXIO;
2004         }
2005         return 0;
2006 }
2007
2008 static int fan_set_enable(void)
2009 {
2010         u8 s;
2011         int rc;
2012
2013         switch (fan_control_access_mode) {
2014         case IBMACPI_FAN_WR_ACPI_FANS:
2015         case IBMACPI_FAN_WR_TPEC:
2016                 if ((rc = fan_get_status(&s)) < 0)
2017                         return rc;
2018
2019                 /* Don't go out of emergency fan mode */
2020                 if (s != 7)
2021                         s = IBMACPI_FAN_EC_AUTO;
2022
2023                 if (!acpi_ec_write(fan_status_offset, s))
2024                         return -EIO;
2025                 break;
2026
2027         case IBMACPI_FAN_WR_ACPI_SFAN:
2028                 if ((rc = fan_get_status(&s)) < 0)
2029                         return rc;
2030
2031                 s &= 0x07;
2032
2033                 /* Set fan to at least level 4 */
2034                 if (s < 4)
2035                         s = 4;
2036
2037                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2038                         return -EIO;
2039                 break;
2040
2041         default:
2042                 return -ENXIO;
2043         }
2044         return 0;
2045 }
2046
2047 static int fan_set_disable(void)
2048 {
2049         switch (fan_control_access_mode) {
2050         case IBMACPI_FAN_WR_ACPI_FANS:
2051         case IBMACPI_FAN_WR_TPEC:
2052                 if (!acpi_ec_write(fan_status_offset, 0x00))
2053                         return -EIO;
2054                 break;
2055
2056         case IBMACPI_FAN_WR_ACPI_SFAN:
2057                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2058                         return -EIO;
2059                 break;
2060
2061         default:
2062                 return -ENXIO;
2063         }
2064         return 0;
2065 }
2066
2067 static int fan_set_speed(int speed)
2068 {
2069         switch (fan_control_access_mode) {
2070         case IBMACPI_FAN_WR_ACPI_FANS:
2071                 if (speed >= 0 && speed <= 65535) {
2072                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2073                                         speed, speed, speed))
2074                                 return -EIO;
2075                 } else
2076                         return -EINVAL;
2077                 break;
2078
2079         default:
2080                 return -ENXIO;
2081         }
2082         return 0;
2083 }
2084
2085 static int fan_write_cmd_level(const char *cmd, int *rc)
2086 {
2087         int level;
2088
2089         if (strlencmp(cmd, "level auto") == 0)
2090                 level = IBMACPI_FAN_EC_AUTO;
2091         else if (strlencmp(cmd, "level disengaged") == 0)
2092                 level = IBMACPI_FAN_EC_DISENGAGED;
2093         else if (sscanf(cmd, "level %d", &level) != 1)
2094                 return 0;
2095
2096         if ((*rc = fan_set_level(level)) == -ENXIO)
2097                 printk(IBM_ERR "level command accepted for unsupported "
2098                        "access mode %d", fan_control_access_mode);
2099
2100         return 1;
2101 }
2102
2103 static int fan_write_cmd_enable(const char *cmd, int *rc)
2104 {
2105         if (strlencmp(cmd, "enable") != 0)
2106                 return 0;
2107
2108         if ((*rc = fan_set_enable()) == -ENXIO)
2109                 printk(IBM_ERR "enable command accepted for unsupported "
2110                        "access mode %d", fan_control_access_mode);
2111
2112         return 1;
2113 }
2114
2115 static int fan_write_cmd_disable(const char *cmd, int *rc)
2116 {
2117         if (strlencmp(cmd, "disable") != 0)
2118                 return 0;
2119
2120         if ((*rc = fan_set_disable()) == -ENXIO)
2121                 printk(IBM_ERR "disable command accepted for unsupported "
2122                        "access mode %d", fan_control_access_mode);
2123
2124         return 1;
2125 }
2126
2127 static int fan_write_cmd_speed(const char *cmd, int *rc)
2128 {
2129         int speed;
2130
2131         /* TODO:
2132          * Support speed <low> <medium> <high> ? */
2133
2134         if (sscanf(cmd, "speed %d", &speed) != 1)
2135                 return 0;
2136
2137         if ((*rc = fan_set_speed(speed)) == -ENXIO)
2138                 printk(IBM_ERR "speed command accepted for unsupported "
2139                        "access mode %d", fan_control_access_mode);
2140
2141         return 1;
2142 }
2143
2144 static int fan_write(char *buf)
2145 {
2146         char *cmd;
2147         int rc = 0;
2148
2149         while (!rc && (cmd = next_cmd(&buf))) {
2150                 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2151                       fan_write_cmd_level(cmd, &rc)) &&
2152                     !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2153                       (fan_write_cmd_enable(cmd, &rc) ||
2154                        fan_write_cmd_disable(cmd, &rc))) &&
2155                     !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2156                       fan_write_cmd_speed(cmd, &rc))
2157                     )
2158                         rc = -EINVAL;
2159         }
2160
2161         return rc;
2162 }
2163
2164 static struct ibm_struct ibms[] = {
2165         {
2166          .name = "driver",
2167          .init = driver_init,
2168          .read = driver_read,
2169          },
2170         {
2171          .name = "hotkey",
2172          .hid = IBM_HKEY_HID,
2173          .init = hotkey_init,
2174          .read = hotkey_read,
2175          .write = hotkey_write,
2176          .exit = hotkey_exit,
2177          .notify = hotkey_notify,
2178          .handle = &hkey_handle,
2179          .type = ACPI_DEVICE_NOTIFY,
2180          },
2181         {
2182          .name = "bluetooth",
2183          .init = bluetooth_init,
2184          .read = bluetooth_read,
2185          .write = bluetooth_write,
2186          },
2187         {
2188          .name = "wan",
2189          .init = wan_init,
2190          .read = wan_read,
2191          .write = wan_write,
2192          .experimental = 1,
2193          },
2194         {
2195          .name = "video",
2196          .init = video_init,
2197          .read = video_read,
2198          .write = video_write,
2199          .exit = video_exit,
2200          },
2201         {
2202          .name = "light",
2203          .init = light_init,
2204          .read = light_read,
2205          .write = light_write,
2206          },
2207 #ifdef CONFIG_ACPI_IBM_DOCK
2208         {
2209          .name = "dock",
2210          .read = dock_read,
2211          .write = dock_write,
2212          .notify = dock_notify,
2213          .handle = &dock_handle,
2214          .type = ACPI_SYSTEM_NOTIFY,
2215          },
2216         {
2217          .name = "dock",
2218          .hid = IBM_PCI_HID,
2219          .notify = dock_notify,
2220          .handle = &pci_handle,
2221          .type = ACPI_SYSTEM_NOTIFY,
2222          },
2223 #endif
2224         {
2225          .name = "bay",
2226          .init = bay_init,
2227          .read = bay_read,
2228          .write = bay_write,
2229          .notify = bay_notify,
2230          .handle = &bay_handle,
2231          .type = ACPI_SYSTEM_NOTIFY,
2232          },
2233         {
2234          .name = "cmos",
2235          .read = cmos_read,
2236          .write = cmos_write,
2237          },
2238         {
2239          .name = "led",
2240          .init = led_init,
2241          .read = led_read,
2242          .write = led_write,
2243          },
2244         {
2245          .name = "beep",
2246          .read = beep_read,
2247          .write = beep_write,
2248          },
2249         {
2250          .name = "thermal",
2251          .init = thermal_init,
2252          .read = thermal_read,
2253          },
2254         {
2255          .name = "ecdump",
2256          .read = ecdump_read,
2257          .write = ecdump_write,
2258          .experimental = 1,
2259          },
2260         {
2261          .name = "brightness",
2262          .read = brightness_read,
2263          .write = brightness_write,
2264          },
2265         {
2266          .name = "volume",
2267          .read = volume_read,
2268          .write = volume_write,
2269          },
2270         {
2271          .name = "fan",
2272          .read = fan_read,
2273          .write = fan_write,
2274          .init = fan_init,
2275          .experimental = 1,
2276          },
2277 };
2278
2279 static int dispatch_read(char *page, char **start, off_t off, int count,
2280                          int *eof, void *data)
2281 {
2282         struct ibm_struct *ibm = (struct ibm_struct *)data;
2283         int len;
2284
2285         if (!ibm || !ibm->read)
2286                 return -EINVAL;
2287
2288         len = ibm->read(page);
2289         if (len < 0)
2290                 return len;
2291
2292         if (len <= off + count)
2293                 *eof = 1;
2294         *start = page + off;
2295         len -= off;
2296         if (len > count)
2297                 len = count;
2298         if (len < 0)
2299                 len = 0;
2300
2301         return len;
2302 }
2303
2304 static int dispatch_write(struct file *file, const char __user * userbuf,
2305                           unsigned long count, void *data)
2306 {
2307         struct ibm_struct *ibm = (struct ibm_struct *)data;
2308         char *kernbuf;
2309         int ret;
2310
2311         if (!ibm || !ibm->write)
2312                 return -EINVAL;
2313
2314         kernbuf = kmalloc(count + 2, GFP_KERNEL);
2315         if (!kernbuf)
2316                 return -ENOMEM;
2317
2318         if (copy_from_user(kernbuf, userbuf, count)) {
2319                 kfree(kernbuf);
2320                 return -EFAULT;
2321         }
2322
2323         kernbuf[count] = 0;
2324         strcat(kernbuf, ",");
2325         ret = ibm->write(kernbuf);
2326         if (ret == 0)
2327                 ret = count;
2328
2329         kfree(kernbuf);
2330
2331         return ret;
2332 }
2333
2334 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2335 {
2336         struct ibm_struct *ibm = (struct ibm_struct *)data;
2337
2338         if (!ibm || !ibm->notify)
2339                 return;
2340
2341         ibm->notify(ibm, event);
2342 }
2343
2344 static int __init setup_notify(struct ibm_struct *ibm)
2345 {
2346         acpi_status status;
2347         int ret;
2348
2349         if (!*ibm->handle)
2350                 return 0;
2351
2352         ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2353         if (ret < 0) {
2354                 printk(IBM_ERR "%s device not present\n", ibm->name);
2355                 return 0;
2356         }
2357
2358         acpi_driver_data(ibm->device) = ibm;
2359         sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2360
2361         status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2362                                              dispatch_notify, ibm);
2363         if (ACPI_FAILURE(status)) {
2364                 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2365                        ibm->name, status);
2366                 return -ENODEV;
2367         }
2368
2369         return 0;
2370 }
2371
2372 static int __init ibm_device_add(struct acpi_device *device)
2373 {
2374         return 0;
2375 }
2376
2377 static int __init register_driver(struct ibm_struct *ibm)
2378 {
2379         int ret;
2380
2381         ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2382         if (!ibm->driver) {
2383                 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2384                 return -1;
2385         }
2386
2387         memset(ibm->driver, 0, sizeof(struct acpi_driver));
2388         sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
2389         ibm->driver->ids = ibm->hid;
2390         ibm->driver->ops.add = &ibm_device_add;
2391
2392         ret = acpi_bus_register_driver(ibm->driver);
2393         if (ret < 0) {
2394                 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2395                        ibm->hid, ret);
2396                 kfree(ibm->driver);
2397         }
2398
2399         return ret;
2400 }
2401
2402 static int __init ibm_init(struct ibm_struct *ibm)
2403 {
2404         int ret;
2405         struct proc_dir_entry *entry;
2406
2407         if (ibm->experimental && !experimental)
2408                 return 0;
2409
2410         if (ibm->hid) {
2411                 ret = register_driver(ibm);
2412                 if (ret < 0)
2413                         return ret;
2414                 ibm->driver_registered = 1;
2415         }
2416
2417         if (ibm->init) {
2418                 ret = ibm->init();
2419                 if (ret != 0)
2420                         return ret;
2421                 ibm->init_called = 1;
2422         }
2423
2424         if (ibm->read) {
2425                 entry = create_proc_entry(ibm->name,
2426                                           S_IFREG | S_IRUGO | S_IWUSR,
2427                                           proc_dir);
2428                 if (!entry) {
2429                         printk(IBM_ERR "unable to create proc entry %s\n",
2430                                ibm->name);
2431                         return -ENODEV;
2432                 }
2433                 entry->owner = THIS_MODULE;
2434                 entry->data = ibm;
2435                 entry->read_proc = &dispatch_read;
2436                 if (ibm->write)
2437                         entry->write_proc = &dispatch_write;
2438                 ibm->proc_created = 1;
2439         }
2440
2441         if (ibm->notify) {
2442                 ret = setup_notify(ibm);
2443                 if (ret < 0)
2444                         return ret;
2445                 ibm->notify_installed = 1;
2446         }
2447
2448         return 0;
2449 }
2450
2451 static void ibm_exit(struct ibm_struct *ibm)
2452 {
2453         if (ibm->notify_installed)
2454                 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2455                                            dispatch_notify);
2456
2457         if (ibm->proc_created)
2458                 remove_proc_entry(ibm->name, proc_dir);
2459
2460         if (ibm->init_called && ibm->exit)
2461                 ibm->exit();
2462
2463         if (ibm->driver_registered) {
2464                 acpi_bus_unregister_driver(ibm->driver);
2465                 kfree(ibm->driver);
2466         }
2467 }
2468
2469 static void __init ibm_handle_init(char *name,
2470                                    acpi_handle * handle, acpi_handle parent,
2471                                    char **paths, int num_paths, char **path)
2472 {
2473         int i;
2474         acpi_status status;
2475
2476         for (i = 0; i < num_paths; i++) {
2477                 status = acpi_get_handle(parent, paths[i], handle);
2478                 if (ACPI_SUCCESS(status)) {
2479                         *path = paths[i];
2480                         return;
2481                 }
2482         }
2483
2484         *handle = NULL;
2485 }
2486
2487 #define IBM_HANDLE_INIT(object)                                         \
2488         ibm_handle_init(#object, &object##_handle, *object##_parent,    \
2489                 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
2490
2491 static int set_ibm_param(const char *val, struct kernel_param *kp)
2492 {
2493         unsigned int i;
2494
2495         for (i = 0; i < ARRAY_SIZE(ibms); i++)
2496                 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2497                         if (strlen(val) > sizeof(ibms[i].param) - 2)
2498                                 return -ENOSPC;
2499                         strcpy(ibms[i].param, val);
2500                         strcat(ibms[i].param, ",");
2501                         return 0;
2502                 }
2503
2504         return -EINVAL;
2505 }
2506
2507 #define IBM_PARAM(feature) \
2508         module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2509
2510 IBM_PARAM(hotkey);
2511 IBM_PARAM(bluetooth);
2512 IBM_PARAM(video);
2513 IBM_PARAM(light);
2514 #ifdef CONFIG_ACPI_IBM_DOCK
2515 IBM_PARAM(dock);
2516 #endif
2517 IBM_PARAM(bay);
2518 IBM_PARAM(cmos);
2519 IBM_PARAM(led);
2520 IBM_PARAM(beep);
2521 IBM_PARAM(ecdump);
2522 IBM_PARAM(brightness);
2523 IBM_PARAM(volume);
2524 IBM_PARAM(fan);
2525
2526 static struct backlight_properties ibm_backlight_data = {
2527         .owner = THIS_MODULE,
2528         .get_brightness = brightness_get,
2529         .update_status = brightness_update_status,
2530         .max_brightness = 7,
2531 };
2532
2533 static void acpi_ibm_exit(void)
2534 {
2535         int i;
2536
2537         if (ibm_backlight_device)
2538                 backlight_device_unregister(ibm_backlight_device);
2539
2540         for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2541                 ibm_exit(&ibms[i]);
2542
2543         remove_proc_entry(IBM_DIR, acpi_root_dir);
2544
2545         if (ibm_thinkpad_ec_found)
2546                 kfree(ibm_thinkpad_ec_found);
2547 }
2548
2549 static char* __init check_dmi_for_ec(void)
2550 {
2551         struct dmi_device *dev = NULL;
2552         char ec_fw_string[18];
2553
2554         /*
2555          * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2556          * X32 or newer, all Z series;  Some models must have an
2557          * up-to-date BIOS or they will not be detected.
2558          *
2559          * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2560          */
2561         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2562                 if (sscanf(dev->name,
2563                            "IBM ThinkPad Embedded Controller -[%17c",
2564                            ec_fw_string) == 1) {
2565                         ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2566                         ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2567                         return kstrdup(ec_fw_string, GFP_KERNEL);
2568                 }
2569         }
2570         return NULL;
2571 }
2572
2573 static int __init acpi_ibm_init(void)
2574 {
2575         int ret, i;
2576
2577         if (acpi_disabled)
2578                 return -ENODEV;
2579
2580         if (!acpi_specific_hotkey_enabled) {
2581                 printk(IBM_ERR "using generic hotkey driver\n");
2582                 return -ENODEV;
2583         }
2584
2585         /* ec is required because many other handles are relative to it */
2586         IBM_HANDLE_INIT(ec);
2587         if (!ec_handle) {
2588                 printk(IBM_ERR "ec object not found\n");
2589                 return -ENODEV;
2590         }
2591
2592         /* Models with newer firmware report the EC in DMI */
2593         ibm_thinkpad_ec_found = check_dmi_for_ec();
2594         if (ibm_thinkpad_ec_found)
2595                 printk(IBM_INFO "ThinkPad EC firmware %s\n",
2596                        ibm_thinkpad_ec_found);
2597
2598         /* these handles are not required */
2599         IBM_HANDLE_INIT(vid);
2600         IBM_HANDLE_INIT(vid2);
2601         IBM_HANDLE_INIT(ledb);
2602         IBM_HANDLE_INIT(led);
2603         IBM_HANDLE_INIT(hkey);
2604         IBM_HANDLE_INIT(lght);
2605         IBM_HANDLE_INIT(cmos);
2606 #ifdef CONFIG_ACPI_IBM_DOCK
2607         IBM_HANDLE_INIT(dock);
2608 #endif
2609         IBM_HANDLE_INIT(pci);
2610         IBM_HANDLE_INIT(bay);
2611         if (bay_handle)
2612                 IBM_HANDLE_INIT(bay_ej);
2613         IBM_HANDLE_INIT(bay2);
2614         if (bay2_handle)
2615                 IBM_HANDLE_INIT(bay2_ej);
2616         IBM_HANDLE_INIT(beep);
2617         IBM_HANDLE_INIT(ecrd);
2618         IBM_HANDLE_INIT(ecwr);
2619         IBM_HANDLE_INIT(fans);
2620         IBM_HANDLE_INIT(gfan);
2621         IBM_HANDLE_INIT(sfan);
2622
2623         proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2624         if (!proc_dir) {
2625                 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2626                 return -ENODEV;
2627         }
2628         proc_dir->owner = THIS_MODULE;
2629
2630         for (i = 0; i < ARRAY_SIZE(ibms); i++) {
2631                 ret = ibm_init(&ibms[i]);
2632                 if (ret >= 0 && *ibms[i].param)
2633                         ret = ibms[i].write(ibms[i].param);
2634                 if (ret < 0) {
2635                         acpi_ibm_exit();
2636                         return ret;
2637                 }
2638         }
2639
2640         ibm_backlight_device = backlight_device_register("ibm", NULL,
2641                                                          &ibm_backlight_data);
2642         if (IS_ERR(ibm_backlight_device)) {
2643                 printk(IBM_ERR "Could not register ibm backlight device\n");
2644                 ibm_backlight_device = NULL;
2645                 acpi_ibm_exit();
2646         }
2647
2648         return 0;
2649 }
2650
2651 module_init(acpi_ibm_init);
2652 module_exit(acpi_ibm_exit);