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