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