ACPI: ibm-acpi: make non-generic bay support optional
[powerpc.git] / drivers / acpi / ibm_acpi.c
1 /*
2  *  ibm_acpi.c - IBM ThinkPad ACPI Extras
3  *
4  *
5  *  Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #define IBM_VERSION "0.12a"
23
24 /*
25  *  Changelog:
26  *  
27  *  2005-08-17  0.12    fix compilation on 2.6.13-rc kernels
28  *  2005-03-17  0.11    support for 600e, 770x
29  *                          thanks to Jamie Lentin <lentinj@dial.pipex.com>
30  *                      support for 770e, G41
31  *                      G40 and G41 don't have a thinklight
32  *                      temperatures no longer experimental
33  *                      experimental brightness control
34  *                      experimental volume control
35  *                      experimental fan enable/disable
36  *  2005-01-16  0.10    fix module loading on R30, R31 
37  *  2005-01-16  0.9     support for 570, R30, R31
38  *                      ultrabay support on A22p, A3x
39  *                      limit arg for cmos, led, beep, drop experimental status
40  *                      more capable led control on A21e, A22p, T20-22, X20
41  *                      experimental temperatures and fan speed
42  *                      experimental embedded controller register dump
43  *                      mark more functions as __init, drop incorrect __exit
44  *                      use MODULE_VERSION
45  *                          thanks to Henrik Brix Andersen <brix@gentoo.org>
46  *                      fix parameter passing on module loading
47  *                          thanks to Rusty Russell <rusty@rustcorp.com.au>
48  *                          thanks to Jim Radford <radford@blackbean.org>
49  *  2004-11-08  0.8     fix init error case, don't return from a macro
50  *                          thanks to Chris Wright <chrisw@osdl.org>
51  *  2004-10-23  0.7     fix module loading on A21e, A22p, T20, T21, X20
52  *                      fix led control on A21e
53  *  2004-10-19  0.6     use acpi_bus_register_driver() to claim HKEY device
54  *  2004-10-18  0.5     thinklight support on A21e, G40, R32, T20, T21, X20
55  *                      proc file format changed
56  *                      video_switch command
57  *                      experimental cmos control
58  *                      experimental led control
59  *                      experimental acpi sounds
60  *  2004-09-16  0.4     support for module parameters
61  *                      hotkey mask can be prefixed by 0x
62  *                      video output switching
63  *                      video expansion control
64  *                      ultrabay eject support
65  *                      removed lcd brightness/on/off control, didn't work
66  *  2004-08-17  0.3     support for R40
67  *                      lcd off, brightness control
68  *                      thinklight on/off
69  *  2004-08-14  0.2     support for T series, X20
70  *                      bluetooth enable/disable
71  *                      hotkey events disabled by default
72  *                      removed fan control, currently useless
73  *  2004-08-09  0.1     initial release, support for X series
74  */
75
76 #include <linux/kernel.h>
77 #include <linux/module.h>
78 #include <linux/init.h>
79 #include <linux/types.h>
80 #include <linux/string.h>
81 #include <linux/proc_fs.h>
82 #include <linux/backlight.h>
83 #include <asm/uaccess.h>
84 #include <linux/dmi.h>
85 #include <linux/jiffies.h>
86 #include <linux/workqueue.h>
87
88 #include <acpi/acpi_drivers.h>
89 #include <acpi/acnamesp.h>
90
91 #define IBM_NAME "ibm"
92 #define IBM_DESC "IBM ThinkPad ACPI Extras"
93 #define IBM_FILE "ibm_acpi"
94 #define IBM_URL "http://ibm-acpi.sf.net/"
95
96 MODULE_AUTHOR("Borislav Deianov");
97 MODULE_DESCRIPTION(IBM_DESC);
98 MODULE_VERSION(IBM_VERSION);
99 MODULE_LICENSE("GPL");
100
101 #define IBM_DIR IBM_NAME
102
103 #define IBM_LOG IBM_FILE ": "
104 #define IBM_ERR    KERN_ERR    IBM_LOG
105 #define IBM_NOTICE KERN_NOTICE IBM_LOG
106 #define IBM_INFO   KERN_INFO   IBM_LOG
107 #define IBM_DEBUG  KERN_DEBUG  IBM_LOG
108
109 #define IBM_MAX_ACPI_ARGS 3
110
111 #define __unused __attribute__ ((unused))
112
113 static int experimental;
114 module_param(experimental, int, 0);
115
116 static acpi_handle root_handle = NULL;
117
118 #define IBM_HANDLE(object, parent, paths...)                    \
119         static acpi_handle  object##_handle;                    \
120         static acpi_handle *object##_parent = &parent##_handle; \
121         static char        *object##_path;                      \
122         static char        *object##_paths[] = { paths }
123
124 /*
125  * The following models are supported to various degrees:
126  *
127  * 570, 600e, 600x, 770e, 770x
128  * A20m, A21e, A21m, A21p, A22p, A30, A30p, A31, A31p
129  * G40, G41
130  * R30, R31, R32, R40, R40e, R50, R50e, R50p, R51
131  * T20, T21, T22, T23, T30, T40, T40p, T41, T41p, T42, T42p, T43
132  * X20, X21, X22, X23, X24, X30, X31, X40
133  *
134  * The following models have no supported features:
135  *
136  * 240, 240x, i1400
137  *
138  * Still missing DSDTs for the following models:
139  *
140  * A20p, A22e, A22m
141  * R52
142  * S31
143  * T43p
144  */
145
146 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",      /* 240, 240x */
147            "\\_SB.PCI.ISA.EC",  /* 570 */
148            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
149            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
150            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
151            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
152            "\\_SB.PCI0.LPC.EC", /* all others */
153     );
154
155 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",      /* 570 */
156            "\\_SB.PCI0.AGP0.VID0",      /* 600e/x, 770x */
157            "\\_SB.PCI0.VID0",   /* 770e */
158            "\\_SB.PCI0.VID",    /* A21e, G4x, R50e, X30, X40 */
159            "\\_SB.PCI0.AGP.VID",        /* all others */
160     );                          /* R30, R31 */
161
162 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");  /* G41 */
163
164 IBM_HANDLE(cmos, root, "\\UCMS",        /* R50, R50e, R50p, R51, T4x, X31, X40 */
165            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
166            "\\CMS",             /* R40, R40e */
167     );                          /* all others */
168 #ifdef CONFIG_ACPI_IBM_DOCK
169 IBM_HANDLE(dock, root, "\\_SB.GDCK",    /* X30, X31, X40 */
170            "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
171            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
172            "\\_SB.PCI.ISA.SLCE",        /* 570 */
173     );                          /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
174 #endif
175 #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;
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         return level;
1643 }
1644
1645 static int brightness_read(char *p)
1646 {
1647         int len = 0;
1648         int level;
1649
1650         if ((level = brightness_get(NULL)) < 0) {
1651                 len += sprintf(p + len, "level:\t\tunreadable\n");
1652         } else {
1653                 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1654                 len += sprintf(p + len, "commands:\tup, down\n");
1655                 len += sprintf(p + len, "commands:\tlevel <level>"
1656                                " (<level> is 0-7)\n");
1657         }
1658
1659         return len;
1660 }
1661
1662 #define BRIGHTNESS_UP   4
1663 #define BRIGHTNESS_DOWN 5
1664
1665 static int brightness_set(int value)
1666 {
1667         int cmos_cmd, inc, i;
1668         int current_value = brightness_get(NULL);
1669
1670         value &= 7;
1671
1672         cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1673         inc = value > current_value ? 1 : -1;
1674         for (i = current_value; i != value; i += inc) {
1675                 if (!cmos_eval(cmos_cmd))
1676                         return -EIO;
1677                 if (!acpi_ec_write(brightness_offset, i + inc))
1678                         return -EIO;
1679         }
1680
1681         return 0;
1682 }
1683
1684 static int brightness_write(char *buf)
1685 {
1686         int level;
1687         int new_level;
1688         char *cmd;
1689
1690         while ((cmd = next_cmd(&buf))) {
1691                 if ((level = brightness_get(NULL)) < 0)
1692                         return level;
1693                 level &= 7;
1694
1695                 if (strlencmp(cmd, "up") == 0) {
1696                         new_level = level == 7 ? 7 : level + 1;
1697                 } else if (strlencmp(cmd, "down") == 0) {
1698                         new_level = level == 0 ? 0 : level - 1;
1699                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1700                            new_level >= 0 && new_level <= 7) {
1701                         /* new_level set */
1702                 } else
1703                         return -EINVAL;
1704
1705                 brightness_set(new_level);
1706         }
1707
1708         return 0;
1709 }
1710
1711 static int brightness_update_status(struct backlight_device *bd)
1712 {
1713         return brightness_set(bd->props->brightness);
1714 }
1715
1716 static int volume_offset = 0x30;
1717
1718 static int volume_read(char *p)
1719 {
1720         int len = 0;
1721         u8 level;
1722
1723         if (!acpi_ec_read(volume_offset, &level)) {
1724                 len += sprintf(p + len, "level:\t\tunreadable\n");
1725         } else {
1726                 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1727                 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1728                 len += sprintf(p + len, "commands:\tup, down, mute\n");
1729                 len += sprintf(p + len, "commands:\tlevel <level>"
1730                                " (<level> is 0-15)\n");
1731         }
1732
1733         return len;
1734 }
1735
1736 #define VOLUME_DOWN     0
1737 #define VOLUME_UP       1
1738 #define VOLUME_MUTE     2
1739
1740 static int volume_write(char *buf)
1741 {
1742         int cmos_cmd, inc, i;
1743         u8 level, mute;
1744         int new_level, new_mute;
1745         char *cmd;
1746
1747         while ((cmd = next_cmd(&buf))) {
1748                 if (!acpi_ec_read(volume_offset, &level))
1749                         return -EIO;
1750                 new_mute = mute = level & 0x40;
1751                 new_level = level = level & 0xf;
1752
1753                 if (strlencmp(cmd, "up") == 0) {
1754                         if (mute)
1755                                 new_mute = 0;
1756                         else
1757                                 new_level = level == 15 ? 15 : level + 1;
1758                 } else if (strlencmp(cmd, "down") == 0) {
1759                         if (mute)
1760                                 new_mute = 0;
1761                         else
1762                                 new_level = level == 0 ? 0 : level - 1;
1763                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1764                            new_level >= 0 && new_level <= 15) {
1765                         /* new_level set */
1766                 } else if (strlencmp(cmd, "mute") == 0) {
1767                         new_mute = 0x40;
1768                 } else
1769                         return -EINVAL;
1770
1771                 if (new_level != level) {       /* mute doesn't change */
1772                         cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1773                         inc = new_level > level ? 1 : -1;
1774
1775                         if (mute && (!cmos_eval(cmos_cmd) ||
1776                                      !acpi_ec_write(volume_offset, level)))
1777                                 return -EIO;
1778
1779                         for (i = level; i != new_level; i += inc)
1780                                 if (!cmos_eval(cmos_cmd) ||
1781                                     !acpi_ec_write(volume_offset, i + inc))
1782                                         return -EIO;
1783
1784                         if (mute && (!cmos_eval(VOLUME_MUTE) ||
1785                                      !acpi_ec_write(volume_offset,
1786                                                     new_level + mute)))
1787                                 return -EIO;
1788                 }
1789
1790                 if (new_mute != mute) { /* level doesn't change */
1791                         cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1792
1793                         if (!cmos_eval(cmos_cmd) ||
1794                             !acpi_ec_write(volume_offset, level + new_mute))
1795                                 return -EIO;
1796                 }
1797         }
1798
1799         return 0;
1800 }
1801
1802 static enum fan_status_access_mode fan_status_access_mode;
1803 static enum fan_control_access_mode fan_control_access_mode;
1804 static enum fan_control_commands fan_control_commands;
1805
1806 static int fan_control_status_known;
1807 static u8 fan_control_initial_status;
1808
1809 static void fan_watchdog_fire(void *ignored);
1810 static int fan_watchdog_maxinterval;
1811 static DECLARE_WORK(fan_watchdog_task, fan_watchdog_fire, NULL);
1812
1813 static int fan_init(void)
1814 {
1815         fan_status_access_mode = IBMACPI_FAN_NONE;
1816         fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1817         fan_control_commands = 0;
1818         fan_control_status_known = 1;
1819         fan_watchdog_maxinterval = 0;
1820
1821         if (gfan_handle) {
1822                 /* 570, 600e/x, 770e, 770x */
1823                 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1824         } else {
1825                 /* all other ThinkPads: note that even old-style
1826                  * ThinkPad ECs supports the fan control register */
1827                 if (likely(acpi_ec_read(fan_status_offset,
1828                                         &fan_control_initial_status))) {
1829                         fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1830
1831                         /* In some ThinkPads, neither the EC nor the ACPI
1832                          * DSDT initialize the fan status, and it ends up
1833                          * being set to 0x07 when it *could* be either
1834                          * 0x07 or 0x80.
1835                          *
1836                          * Enable for TP-1Y (T43), TP-78 (R51e),
1837                          * TP-76 (R52), TP-70 (T43, R52), which are known
1838                          * to be buggy. */
1839                         if (fan_control_initial_status == 0x07 &&
1840                             ibm_thinkpad_ec_found &&
1841                             ((ibm_thinkpad_ec_found[0] == '1' &&
1842                               ibm_thinkpad_ec_found[1] == 'Y') ||
1843                              (ibm_thinkpad_ec_found[0] == '7' &&
1844                               (ibm_thinkpad_ec_found[1] == '6' ||
1845                                ibm_thinkpad_ec_found[1] == '8' ||
1846                                ibm_thinkpad_ec_found[1] == '0'))
1847                             )) {
1848                                 printk(IBM_NOTICE
1849                                        "fan_init: initial fan status is "
1850                                        "unknown, assuming it is in auto "
1851                                        "mode\n");
1852                                 fan_control_status_known = 0;
1853                         }
1854                 } else {
1855                         printk(IBM_ERR
1856                                "ThinkPad ACPI EC access misbehaving, "
1857                                "fan status and control unavailable\n");
1858                         return 0;
1859                 }
1860         }
1861
1862         if (sfan_handle) {
1863                 /* 570, 770x-JL */
1864                 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
1865                 fan_control_commands |=
1866                     IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
1867         } else {
1868                 if (!gfan_handle) {
1869                         /* gfan without sfan means no fan control */
1870                         /* all other models implement TP EC 0x2f control */
1871
1872                         if (fans_handle) {
1873                                 /* X31, X40, X41 */
1874                                 fan_control_access_mode =
1875                                     IBMACPI_FAN_WR_ACPI_FANS;
1876                                 fan_control_commands |=
1877                                     IBMACPI_FAN_CMD_SPEED |
1878                                     IBMACPI_FAN_CMD_LEVEL |
1879                                     IBMACPI_FAN_CMD_ENABLE;
1880                         } else {
1881                                 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
1882                                 fan_control_commands |=
1883                                     IBMACPI_FAN_CMD_LEVEL |
1884                                     IBMACPI_FAN_CMD_ENABLE;
1885                         }
1886                 }
1887         }
1888
1889         return 0;
1890 }
1891
1892 static int fan_get_status(u8 *status)
1893 {
1894         u8 s;
1895
1896         /* TODO:
1897          * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1898
1899         switch (fan_status_access_mode) {
1900         case IBMACPI_FAN_RD_ACPI_GFAN:
1901                 /* 570, 600e/x, 770e, 770x */
1902
1903                 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
1904                         return -EIO;
1905
1906                 if (likely(status))
1907                         *status = s & 0x07;
1908
1909                 break;
1910
1911         case IBMACPI_FAN_RD_TPEC:
1912                 /* all except 570, 600e/x, 770e, 770x */
1913                 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1914                         return -EIO;
1915
1916                 if (likely(status))
1917                         *status = s;
1918
1919                 break;
1920
1921         default:
1922                 return -ENXIO;
1923         }
1924
1925         return 0;
1926 }
1927
1928 static int fan_get_speed(unsigned int *speed)
1929 {
1930         u8 hi, lo;
1931
1932         switch (fan_status_access_mode) {
1933         case IBMACPI_FAN_RD_TPEC:
1934                 /* all except 570, 600e/x, 770e, 770x */
1935                 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1936                              !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1937                         return -EIO;
1938
1939                 if (likely(speed))
1940                         *speed = (hi << 8) | lo;
1941
1942                 break;
1943
1944         default:
1945                 return -ENXIO;
1946         }
1947
1948         return 0;
1949 }
1950
1951 static void fan_exit(void)
1952 {
1953         cancel_delayed_work(&fan_watchdog_task);
1954         flush_scheduled_work();
1955 }
1956
1957 static void fan_watchdog_reset(void)
1958 {
1959         static int fan_watchdog_active = 0;
1960
1961         if (fan_watchdog_active)
1962                 cancel_delayed_work(&fan_watchdog_task);
1963
1964         if (fan_watchdog_maxinterval > 0) {
1965                 fan_watchdog_active = 1;
1966                 if (!schedule_delayed_work(&fan_watchdog_task,
1967                                 msecs_to_jiffies(fan_watchdog_maxinterval
1968                                                  * 1000))) {
1969                         printk(IBM_ERR "failed to schedule the fan watchdog, "
1970                                "watchdog will not trigger\n");
1971                 }
1972         } else
1973                 fan_watchdog_active = 0;
1974 }
1975
1976 static int fan_read(char *p)
1977 {
1978         int len = 0;
1979         int rc;
1980         u8 status;
1981         unsigned int speed = 0;
1982
1983         switch (fan_status_access_mode) {
1984         case IBMACPI_FAN_RD_ACPI_GFAN:
1985                 /* 570, 600e/x, 770e, 770x */
1986                 if ((rc = fan_get_status(&status)) < 0)
1987                         return rc;
1988
1989                 len += sprintf(p + len, "status:\t\t%s\n"
1990                                "level:\t\t%d\n",
1991                                (status != 0) ? "enabled" : "disabled", status);
1992                 break;
1993
1994         case IBMACPI_FAN_RD_TPEC:
1995                 /* all except 570, 600e/x, 770e, 770x */
1996                 if ((rc = fan_get_status(&status)) < 0)
1997                         return rc;
1998
1999                 if (unlikely(!fan_control_status_known)) {
2000                         if (status != fan_control_initial_status)
2001                                 fan_control_status_known = 1;
2002                         else
2003                                 /* Return most likely status. In fact, it
2004                                  * might be the only possible status */
2005                                 status = IBMACPI_FAN_EC_AUTO;
2006                 }
2007
2008                 len += sprintf(p + len, "status:\t\t%s\n",
2009                                (status != 0) ? "enabled" : "disabled");
2010
2011                 /* No ThinkPad boots on disengaged mode, we can safely
2012                  * assume the tachometer is online if fan control status
2013                  * was unknown */
2014                 if ((rc = fan_get_speed(&speed)) < 0)
2015                         return rc;
2016
2017                 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2018
2019                 if (status & IBMACPI_FAN_EC_DISENGAGED)
2020                         /* Disengaged mode takes precedence */
2021                         len += sprintf(p + len, "level:\t\tdisengaged\n");
2022                 else if (status & IBMACPI_FAN_EC_AUTO)
2023                         len += sprintf(p + len, "level:\t\tauto\n");
2024                 else
2025                         len += sprintf(p + len, "level:\t\t%d\n", status);
2026                 break;
2027
2028         case IBMACPI_FAN_NONE:
2029         default:
2030                 len += sprintf(p + len, "status:\t\tnot supported\n");
2031         }
2032
2033         if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2034                 len += sprintf(p + len, "commands:\tlevel <level>");
2035
2036                 switch (fan_control_access_mode) {
2037                 case IBMACPI_FAN_WR_ACPI_SFAN:
2038                         len += sprintf(p + len, " (<level> is 0-7)\n");
2039                         break;
2040
2041                 default:
2042                         len += sprintf(p + len, " (<level> is 0-7, "
2043                                        "auto, disengaged)\n");
2044                         break;
2045                 }
2046         }
2047
2048         if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
2049                 len += sprintf(p + len, "commands:\tenable, disable\n"
2050                                "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2051                                "1-120 (seconds))\n");
2052
2053         if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
2054                 len += sprintf(p + len, "commands:\tspeed <speed>"
2055                                " (<speed> is 0-65535)\n");
2056
2057         return len;
2058 }
2059
2060 static int fan_set_level(int level)
2061 {
2062         switch (fan_control_access_mode) {
2063         case IBMACPI_FAN_WR_ACPI_SFAN:
2064                 if (level >= 0 && level <= 7) {
2065                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2066                                 return -EIO;
2067                 } else
2068                         return -EINVAL;
2069                 break;
2070
2071         case IBMACPI_FAN_WR_ACPI_FANS:
2072         case IBMACPI_FAN_WR_TPEC:
2073                 if ((level != IBMACPI_FAN_EC_AUTO) &&
2074                     (level != IBMACPI_FAN_EC_DISENGAGED) &&
2075                     ((level < 0) || (level > 7)))
2076                         return -EINVAL;
2077
2078                 if (!acpi_ec_write(fan_status_offset, level))
2079                         return -EIO;
2080                 else
2081                         fan_control_status_known = 1;
2082                 break;
2083
2084         default:
2085                 return -ENXIO;
2086         }
2087         return 0;
2088 }
2089
2090 static int fan_set_enable(void)
2091 {
2092         u8 s;
2093         int rc;
2094
2095         switch (fan_control_access_mode) {
2096         case IBMACPI_FAN_WR_ACPI_FANS:
2097         case IBMACPI_FAN_WR_TPEC:
2098                 if ((rc = fan_get_status(&s)) < 0)
2099                         return rc;
2100
2101                 /* Don't go out of emergency fan mode */
2102                 if (s != 7)
2103                         s = IBMACPI_FAN_EC_AUTO;
2104
2105                 if (!acpi_ec_write(fan_status_offset, s))
2106                         return -EIO;
2107                 else
2108                         fan_control_status_known = 1;
2109                 break;
2110
2111         case IBMACPI_FAN_WR_ACPI_SFAN:
2112                 if ((rc = fan_get_status(&s)) < 0)
2113                         return rc;
2114
2115                 s &= 0x07;
2116
2117                 /* Set fan to at least level 4 */
2118                 if (s < 4)
2119                         s = 4;
2120
2121                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2122                         return -EIO;
2123                 break;
2124
2125         default:
2126                 return -ENXIO;
2127         }
2128         return 0;
2129 }
2130
2131 static int fan_set_disable(void)
2132 {
2133         switch (fan_control_access_mode) {
2134         case IBMACPI_FAN_WR_ACPI_FANS:
2135         case IBMACPI_FAN_WR_TPEC:
2136                 if (!acpi_ec_write(fan_status_offset, 0x00))
2137                         return -EIO;
2138                 else
2139                         fan_control_status_known = 1;
2140                 break;
2141
2142         case IBMACPI_FAN_WR_ACPI_SFAN:
2143                 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2144                         return -EIO;
2145                 break;
2146
2147         default:
2148                 return -ENXIO;
2149         }
2150         return 0;
2151 }
2152
2153 static int fan_set_speed(int speed)
2154 {
2155         switch (fan_control_access_mode) {
2156         case IBMACPI_FAN_WR_ACPI_FANS:
2157                 if (speed >= 0 && speed <= 65535) {
2158                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2159                                         speed, speed, speed))
2160                                 return -EIO;
2161                 } else
2162                         return -EINVAL;
2163                 break;
2164
2165         default:
2166                 return -ENXIO;
2167         }
2168         return 0;
2169 }
2170
2171 static int fan_write_cmd_level(const char *cmd, int *rc)
2172 {
2173         int level;
2174
2175         if (strlencmp(cmd, "level auto") == 0)
2176                 level = IBMACPI_FAN_EC_AUTO;
2177         else if (strlencmp(cmd, "level disengaged") == 0)
2178                 level = IBMACPI_FAN_EC_DISENGAGED;
2179         else if (sscanf(cmd, "level %d", &level) != 1)
2180                 return 0;
2181
2182         if ((*rc = fan_set_level(level)) == -ENXIO)
2183                 printk(IBM_ERR "level command accepted for unsupported "
2184                        "access mode %d", fan_control_access_mode);
2185
2186         return 1;
2187 }
2188
2189 static int fan_write_cmd_enable(const char *cmd, int *rc)
2190 {
2191         if (strlencmp(cmd, "enable") != 0)
2192                 return 0;
2193
2194         if ((*rc = fan_set_enable()) == -ENXIO)
2195                 printk(IBM_ERR "enable command accepted for unsupported "
2196                        "access mode %d", fan_control_access_mode);
2197
2198         return 1;
2199 }
2200
2201 static int fan_write_cmd_disable(const char *cmd, int *rc)
2202 {
2203         if (strlencmp(cmd, "disable") != 0)
2204                 return 0;
2205
2206         if ((*rc = fan_set_disable()) == -ENXIO)
2207                 printk(IBM_ERR "disable command accepted for unsupported "
2208                        "access mode %d", fan_control_access_mode);
2209
2210         return 1;
2211 }
2212
2213 static int fan_write_cmd_speed(const char *cmd, int *rc)
2214 {
2215         int speed;
2216
2217         /* TODO:
2218          * Support speed <low> <medium> <high> ? */
2219
2220         if (sscanf(cmd, "speed %d", &speed) != 1)
2221                 return 0;
2222
2223         if ((*rc = fan_set_speed(speed)) == -ENXIO)
2224                 printk(IBM_ERR "speed command accepted for unsupported "
2225                        "access mode %d", fan_control_access_mode);
2226
2227         return 1;
2228 }
2229
2230 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2231 {
2232         int interval;
2233
2234         if (sscanf(cmd, "watchdog %d", &interval) != 1)
2235                 return 0;
2236
2237         if (interval < 0 || interval > 120)
2238                 *rc = -EINVAL;
2239         else
2240                 fan_watchdog_maxinterval = interval;
2241
2242         return 1;
2243 }
2244
2245 static int fan_write(char *buf)
2246 {
2247         char *cmd;
2248         int rc = 0;
2249
2250         while (!rc && (cmd = next_cmd(&buf))) {
2251                 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2252                       fan_write_cmd_level(cmd, &rc)) &&
2253                     !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2254                       (fan_write_cmd_enable(cmd, &rc) ||
2255                        fan_write_cmd_disable(cmd, &rc) ||
2256                        fan_write_cmd_watchdog(cmd, &rc))) &&
2257                     !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2258                       fan_write_cmd_speed(cmd, &rc))
2259                     )
2260                         rc = -EINVAL;
2261                 else if (!rc)
2262                         fan_watchdog_reset();
2263         }
2264
2265         return rc;
2266 }
2267
2268 static void fan_watchdog_fire(void *ignored)
2269 {
2270         printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2271         if (fan_set_enable()) {
2272                 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2273                 /* reschedule for later */
2274                 fan_watchdog_reset();
2275         }
2276 }
2277
2278 static struct ibm_struct ibms[] = {
2279         {
2280          .name = "driver",
2281          .init = driver_init,
2282          .read = driver_read,
2283          },
2284         {
2285          .name = "hotkey",
2286          .hid = IBM_HKEY_HID,
2287          .init = hotkey_init,
2288          .read = hotkey_read,
2289          .write = hotkey_write,
2290          .exit = hotkey_exit,
2291          .notify = hotkey_notify,
2292          .handle = &hkey_handle,
2293          .type = ACPI_DEVICE_NOTIFY,
2294          },
2295         {
2296          .name = "bluetooth",
2297          .init = bluetooth_init,
2298          .read = bluetooth_read,
2299          .write = bluetooth_write,
2300          },
2301         {
2302          .name = "wan",
2303          .init = wan_init,
2304          .read = wan_read,
2305          .write = wan_write,
2306          .experimental = 1,
2307          },
2308         {
2309          .name = "video",
2310          .init = video_init,
2311          .read = video_read,
2312          .write = video_write,
2313          .exit = video_exit,
2314          },
2315         {
2316          .name = "light",
2317          .init = light_init,
2318          .read = light_read,
2319          .write = light_write,
2320          },
2321 #ifdef CONFIG_ACPI_IBM_DOCK
2322         {
2323          .name = "dock",
2324          .read = dock_read,
2325          .write = dock_write,
2326          .notify = dock_notify,
2327          .handle = &dock_handle,
2328          .type = ACPI_SYSTEM_NOTIFY,
2329          },
2330         {
2331          .name = "dock",
2332          .hid = IBM_PCI_HID,
2333          .notify = dock_notify,
2334          .handle = &pci_handle,
2335          .type = ACPI_SYSTEM_NOTIFY,
2336          },
2337 #endif
2338 #ifdef CONFIG_ACPI_IBM_BAY
2339         {
2340          .name = "bay",
2341          .init = bay_init,
2342          .read = bay_read,
2343          .write = bay_write,
2344          .notify = bay_notify,
2345          .handle = &bay_handle,
2346          .type = ACPI_SYSTEM_NOTIFY,
2347          },
2348 #endif
2349         {
2350          .name = "cmos",
2351          .read = cmos_read,
2352          .write = cmos_write,
2353          },
2354         {
2355          .name = "led",
2356          .init = led_init,
2357          .read = led_read,
2358          .write = led_write,
2359          },
2360         {
2361          .name = "beep",
2362          .read = beep_read,
2363          .write = beep_write,
2364          },
2365         {
2366          .name = "thermal",
2367          .init = thermal_init,
2368          .read = thermal_read,
2369          },
2370         {
2371          .name = "ecdump",
2372          .read = ecdump_read,
2373          .write = ecdump_write,
2374          .experimental = 1,
2375          },
2376         {
2377          .name = "brightness",
2378          .read = brightness_read,
2379          .write = brightness_write,
2380          },
2381         {
2382          .name = "volume",
2383          .read = volume_read,
2384          .write = volume_write,
2385          },
2386         {
2387          .name = "fan",
2388          .read = fan_read,
2389          .write = fan_write,
2390          .init = fan_init,
2391          .exit = fan_exit,
2392          .experimental = 1,
2393          },
2394 };
2395
2396 static int dispatch_read(char *page, char **start, off_t off, int count,
2397                          int *eof, void *data)
2398 {
2399         struct ibm_struct *ibm = (struct ibm_struct *)data;
2400         int len;
2401
2402         if (!ibm || !ibm->read)
2403                 return -EINVAL;
2404
2405         len = ibm->read(page);
2406         if (len < 0)
2407                 return len;
2408
2409         if (len <= off + count)
2410                 *eof = 1;
2411         *start = page + off;
2412         len -= off;
2413         if (len > count)
2414                 len = count;
2415         if (len < 0)
2416                 len = 0;
2417
2418         return len;
2419 }
2420
2421 static int dispatch_write(struct file *file, const char __user * userbuf,
2422                           unsigned long count, void *data)
2423 {
2424         struct ibm_struct *ibm = (struct ibm_struct *)data;
2425         char *kernbuf;
2426         int ret;
2427
2428         if (!ibm || !ibm->write)
2429                 return -EINVAL;
2430
2431         kernbuf = kmalloc(count + 2, GFP_KERNEL);
2432         if (!kernbuf)
2433                 return -ENOMEM;
2434
2435         if (copy_from_user(kernbuf, userbuf, count)) {
2436                 kfree(kernbuf);
2437                 return -EFAULT;
2438         }
2439
2440         kernbuf[count] = 0;
2441         strcat(kernbuf, ",");
2442         ret = ibm->write(kernbuf);
2443         if (ret == 0)
2444                 ret = count;
2445
2446         kfree(kernbuf);
2447
2448         return ret;
2449 }
2450
2451 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2452 {
2453         struct ibm_struct *ibm = (struct ibm_struct *)data;
2454
2455         if (!ibm || !ibm->notify)
2456                 return;
2457
2458         ibm->notify(ibm, event);
2459 }
2460
2461 static int __init setup_notify(struct ibm_struct *ibm)
2462 {
2463         acpi_status status;
2464         int ret;
2465
2466         if (!*ibm->handle)
2467                 return 0;
2468
2469         ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2470         if (ret < 0) {
2471                 printk(IBM_ERR "%s device not present\n", ibm->name);
2472                 return 0;
2473         }
2474
2475         acpi_driver_data(ibm->device) = ibm;
2476         sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2477
2478         status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2479                                              dispatch_notify, ibm);
2480         if (ACPI_FAILURE(status)) {
2481                 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2482                        ibm->name, status);
2483                 return -ENODEV;
2484         }
2485
2486         return 0;
2487 }
2488
2489 static int __init ibm_device_add(struct acpi_device *device)
2490 {
2491         return 0;
2492 }
2493
2494 static int __init register_driver(struct ibm_struct *ibm)
2495 {
2496         int ret;
2497
2498         ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2499         if (!ibm->driver) {
2500                 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2501                 return -1;
2502         }
2503
2504         memset(ibm->driver, 0, sizeof(struct acpi_driver));
2505         sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
2506         ibm->driver->ids = ibm->hid;
2507         ibm->driver->ops.add = &ibm_device_add;
2508
2509         ret = acpi_bus_register_driver(ibm->driver);
2510         if (ret < 0) {
2511                 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2512                        ibm->hid, ret);
2513                 kfree(ibm->driver);
2514         }
2515
2516         return ret;
2517 }
2518
2519 static int __init ibm_init(struct ibm_struct *ibm)
2520 {
2521         int ret;
2522         struct proc_dir_entry *entry;
2523
2524         if (ibm->experimental && !experimental)
2525                 return 0;
2526
2527         if (ibm->hid) {
2528                 ret = register_driver(ibm);
2529                 if (ret < 0)
2530                         return ret;
2531                 ibm->driver_registered = 1;
2532         }
2533
2534         if (ibm->init) {
2535                 ret = ibm->init();
2536                 if (ret != 0)
2537                         return ret;
2538                 ibm->init_called = 1;
2539         }
2540
2541         if (ibm->read) {
2542                 entry = create_proc_entry(ibm->name,
2543                                           S_IFREG | S_IRUGO | S_IWUSR,
2544                                           proc_dir);
2545                 if (!entry) {
2546                         printk(IBM_ERR "unable to create proc entry %s\n",
2547                                ibm->name);
2548                         return -ENODEV;
2549                 }
2550                 entry->owner = THIS_MODULE;
2551                 entry->data = ibm;
2552                 entry->read_proc = &dispatch_read;
2553                 if (ibm->write)
2554                         entry->write_proc = &dispatch_write;
2555                 ibm->proc_created = 1;
2556         }
2557
2558         if (ibm->notify) {
2559                 ret = setup_notify(ibm);
2560                 if (ret < 0)
2561                         return ret;
2562                 ibm->notify_installed = 1;
2563         }
2564
2565         return 0;
2566 }
2567
2568 static void ibm_exit(struct ibm_struct *ibm)
2569 {
2570         if (ibm->notify_installed)
2571                 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2572                                            dispatch_notify);
2573
2574         if (ibm->proc_created)
2575                 remove_proc_entry(ibm->name, proc_dir);
2576
2577         if (ibm->init_called && ibm->exit)
2578                 ibm->exit();
2579
2580         if (ibm->driver_registered) {
2581                 acpi_bus_unregister_driver(ibm->driver);
2582                 kfree(ibm->driver);
2583         }
2584 }
2585
2586 static void __init ibm_handle_init(char *name,
2587                                    acpi_handle * handle, acpi_handle parent,
2588                                    char **paths, int num_paths, char **path)
2589 {
2590         int i;
2591         acpi_status status;
2592
2593         for (i = 0; i < num_paths; i++) {
2594                 status = acpi_get_handle(parent, paths[i], handle);
2595                 if (ACPI_SUCCESS(status)) {
2596                         *path = paths[i];
2597                         return;
2598                 }
2599         }
2600
2601         *handle = NULL;
2602 }
2603
2604 #define IBM_HANDLE_INIT(object)                                         \
2605         ibm_handle_init(#object, &object##_handle, *object##_parent,    \
2606                 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
2607
2608 static int set_ibm_param(const char *val, struct kernel_param *kp)
2609 {
2610         unsigned int i;
2611
2612         for (i = 0; i < ARRAY_SIZE(ibms); i++)
2613                 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2614                         if (strlen(val) > sizeof(ibms[i].param) - 2)
2615                                 return -ENOSPC;
2616                         strcpy(ibms[i].param, val);
2617                         strcat(ibms[i].param, ",");
2618                         return 0;
2619                 }
2620
2621         return -EINVAL;
2622 }
2623
2624 #define IBM_PARAM(feature) \
2625         module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2626
2627 IBM_PARAM(hotkey);
2628 IBM_PARAM(bluetooth);
2629 IBM_PARAM(video);
2630 IBM_PARAM(light);
2631 #ifdef CONFIG_ACPI_IBM_DOCK
2632 IBM_PARAM(dock);
2633 #endif
2634 #ifdef CONFIG_ACPI_IBM_BAY
2635 IBM_PARAM(bay);
2636 #endif
2637 IBM_PARAM(cmos);
2638 IBM_PARAM(led);
2639 IBM_PARAM(beep);
2640 IBM_PARAM(ecdump);
2641 IBM_PARAM(brightness);
2642 IBM_PARAM(volume);
2643 IBM_PARAM(fan);
2644
2645 static struct backlight_properties ibm_backlight_data = {
2646         .owner = THIS_MODULE,
2647         .get_brightness = brightness_get,
2648         .update_status = brightness_update_status,
2649         .max_brightness = 7,
2650 };
2651
2652 static void acpi_ibm_exit(void)
2653 {
2654         int i;
2655
2656         if (ibm_backlight_device)
2657                 backlight_device_unregister(ibm_backlight_device);
2658
2659         for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2660                 ibm_exit(&ibms[i]);
2661
2662         remove_proc_entry(IBM_DIR, acpi_root_dir);
2663
2664         if (ibm_thinkpad_ec_found)
2665                 kfree(ibm_thinkpad_ec_found);
2666 }
2667
2668 static char* __init check_dmi_for_ec(void)
2669 {
2670         struct dmi_device *dev = NULL;
2671         char ec_fw_string[18];
2672
2673         /*
2674          * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2675          * X32 or newer, all Z series;  Some models must have an
2676          * up-to-date BIOS or they will not be detected.
2677          *
2678          * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2679          */
2680         while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2681                 if (sscanf(dev->name,
2682                            "IBM ThinkPad Embedded Controller -[%17c",
2683                            ec_fw_string) == 1) {
2684                         ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2685                         ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2686                         return kstrdup(ec_fw_string, GFP_KERNEL);
2687                 }
2688         }
2689         return NULL;
2690 }
2691
2692 static int __init acpi_ibm_init(void)
2693 {
2694         int ret, i;
2695
2696         if (acpi_disabled)
2697                 return -ENODEV;
2698
2699         if (!acpi_specific_hotkey_enabled) {
2700                 printk(IBM_ERR "using generic hotkey driver\n");
2701                 return -ENODEV;
2702         }
2703
2704         /* ec is required because many other handles are relative to it */
2705         IBM_HANDLE_INIT(ec);
2706         if (!ec_handle) {
2707                 printk(IBM_ERR "ec object not found\n");
2708                 return -ENODEV;
2709         }
2710
2711         /* Models with newer firmware report the EC in DMI */
2712         ibm_thinkpad_ec_found = check_dmi_for_ec();
2713         if (ibm_thinkpad_ec_found)
2714                 printk(IBM_INFO "ThinkPad EC firmware %s\n",
2715                        ibm_thinkpad_ec_found);
2716
2717         /* these handles are not required */
2718         IBM_HANDLE_INIT(vid);
2719         IBM_HANDLE_INIT(vid2);
2720         IBM_HANDLE_INIT(ledb);
2721         IBM_HANDLE_INIT(led);
2722         IBM_HANDLE_INIT(hkey);
2723         IBM_HANDLE_INIT(lght);
2724         IBM_HANDLE_INIT(cmos);
2725 #ifdef CONFIG_ACPI_IBM_DOCK
2726         IBM_HANDLE_INIT(dock);
2727 #endif
2728         IBM_HANDLE_INIT(pci);
2729 #ifdef CONFIG_ACPI_IBM_BAY
2730         IBM_HANDLE_INIT(bay);
2731         if (bay_handle)
2732                 IBM_HANDLE_INIT(bay_ej);
2733         IBM_HANDLE_INIT(bay2);
2734         if (bay2_handle)
2735                 IBM_HANDLE_INIT(bay2_ej);
2736 #endif
2737         IBM_HANDLE_INIT(beep);
2738         IBM_HANDLE_INIT(ecrd);
2739         IBM_HANDLE_INIT(ecwr);
2740         IBM_HANDLE_INIT(fans);
2741         IBM_HANDLE_INIT(gfan);
2742         IBM_HANDLE_INIT(sfan);
2743
2744         proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2745         if (!proc_dir) {
2746                 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2747                 return -ENODEV;
2748         }
2749         proc_dir->owner = THIS_MODULE;
2750
2751         for (i = 0; i < ARRAY_SIZE(ibms); i++) {
2752                 ret = ibm_init(&ibms[i]);
2753                 if (ret >= 0 && *ibms[i].param)
2754                         ret = ibms[i].write(ibms[i].param);
2755                 if (ret < 0) {
2756                         acpi_ibm_exit();
2757                         return ret;
2758                 }
2759         }
2760
2761         ibm_backlight_device = backlight_device_register("ibm", NULL,
2762                                                          &ibm_backlight_data);
2763         if (IS_ERR(ibm_backlight_device)) {
2764                 printk(IBM_ERR "Could not register ibm backlight device\n");
2765                 ibm_backlight_device = NULL;
2766                 acpi_ibm_exit();
2767         }
2768
2769         return 0;
2770 }
2771
2772 module_init(acpi_ibm_init);
2773 module_exit(acpi_ibm_exit);