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