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