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