Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux-2.6] / drivers / acpi / ibm_acpi.c
1 /*
2  *  ibm_acpi.c - IBM ThinkPad ACPI Extras
3  *
4  *
5  *  Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #define IBM_VERSION "0.12a"
23
24 /*
25  *  Changelog:
26  *  
27  *  2005-08-17  0.12    fix compilation on 2.6.13-rc kernels
28  *  2005-03-17  0.11    support for 600e, 770x
29  *                          thanks to Jamie Lentin <lentinj@dial.pipex.com>
30  *                      support for 770e, G41
31  *                      G40 and G41 don't have a thinklight
32  *                      temperatures no longer experimental
33  *                      experimental brightness control
34  *                      experimental volume control
35  *                      experimental fan enable/disable
36  *  2005-01-16  0.10    fix module loading on R30, R31 
37  *  2005-01-16  0.9     support for 570, R30, R31
38  *                      ultrabay support on A22p, A3x
39  *                      limit arg for cmos, led, beep, drop experimental status
40  *                      more capable led control on A21e, A22p, T20-22, X20
41  *                      experimental temperatures and fan speed
42  *                      experimental embedded controller register dump
43  *                      mark more functions as __init, drop incorrect __exit
44  *                      use MODULE_VERSION
45  *                          thanks to Henrik Brix Andersen <brix@gentoo.org>
46  *                      fix parameter passing on module loading
47  *                          thanks to Rusty Russell <rusty@rustcorp.com.au>
48  *                          thanks to Jim Radford <radford@blackbean.org>
49  *  2004-11-08  0.8     fix init error case, don't return from a macro
50  *                          thanks to Chris Wright <chrisw@osdl.org>
51  *  2004-10-23  0.7     fix module loading on A21e, A22p, T20, T21, X20
52  *                      fix led control on A21e
53  *  2004-10-19  0.6     use acpi_bus_register_driver() to claim HKEY device
54  *  2004-10-18  0.5     thinklight support on A21e, G40, R32, T20, T21, X20
55  *                      proc file format changed
56  *                      video_switch command
57  *                      experimental cmos control
58  *                      experimental led control
59  *                      experimental acpi sounds
60  *  2004-09-16  0.4     support for module parameters
61  *                      hotkey mask can be prefixed by 0x
62  *                      video output switching
63  *                      video expansion control
64  *                      ultrabay eject support
65  *                      removed lcd brightness/on/off control, didn't work
66  *  2004-08-17  0.3     support for R40
67  *                      lcd off, brightness control
68  *                      thinklight on/off
69  *  2004-08-14  0.2     support for T series, X20
70  *                      bluetooth enable/disable
71  *                      hotkey events disabled by default
72  *                      removed fan control, currently useless
73  *  2004-08-09  0.1     initial release, support for X series
74  */
75
76 #include <linux/kernel.h>
77 #include <linux/module.h>
78 #include <linux/init.h>
79 #include <linux/types.h>
80 #include <linux/proc_fs.h>
81 #include <asm/uaccess.h>
82
83 #include <acpi/acpi_drivers.h>
84 #include <acpi/acnamesp.h>
85
86 #define IBM_NAME "ibm"
87 #define IBM_DESC "IBM ThinkPad ACPI Extras"
88 #define IBM_FILE "ibm_acpi"
89 #define IBM_URL "http://ibm-acpi.sf.net/"
90
91 MODULE_AUTHOR("Borislav Deianov");
92 MODULE_DESCRIPTION(IBM_DESC);
93 MODULE_VERSION(IBM_VERSION);
94 MODULE_LICENSE("GPL");
95
96 #define IBM_DIR IBM_NAME
97
98 #define IBM_LOG IBM_FILE ": "
99 #define IBM_ERR    KERN_ERR    IBM_LOG
100 #define IBM_NOTICE KERN_NOTICE IBM_LOG
101 #define IBM_INFO   KERN_INFO   IBM_LOG
102 #define IBM_DEBUG  KERN_DEBUG  IBM_LOG
103
104 #define IBM_MAX_ACPI_ARGS 3
105
106 #define __unused __attribute__ ((unused))
107
108 static int experimental;
109 module_param(experimental, int, 0);
110
111 static acpi_handle root_handle = NULL;
112
113 #define IBM_HANDLE(object, parent, paths...)                    \
114         static acpi_handle  object##_handle;                    \
115         static acpi_handle *object##_parent = &parent##_handle; \
116         static char        *object##_path;                      \
117         static char        *object##_paths[] = { paths }
118
119 /*
120  * The following models are supported to various degrees:
121  *
122  * 570, 600e, 600x, 770e, 770x
123  * A20m, A21e, A21m, A21p, A22p, A30, A30p, A31, A31p
124  * G40, G41
125  * R30, R31, R32, R40, R40e, R50, R50e, R50p, R51
126  * T20, T21, T22, T23, T30, T40, T40p, T41, T41p, T42, T42p, T43
127  * X20, X21, X22, X23, X24, X30, X31, X40
128  *
129  * The following models have no supported features:
130  *
131  * 240, 240x, i1400
132  *
133  * Still missing DSDTs for the following models:
134  *
135  * A20p, A22e, A22m
136  * R52
137  * S31
138  * T43p
139  */
140
141 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0",      /* 240, 240x */
142            "\\_SB.PCI.ISA.EC",  /* 570 */
143            "\\_SB.PCI0.ISA0.EC0",       /* 600e/x, 770e, 770x */
144            "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
145            "\\_SB.PCI0.AD4S.EC0",       /* i1400, R30 */
146            "\\_SB.PCI0.ICH3.EC0",       /* R31 */
147            "\\_SB.PCI0.LPC.EC", /* all others */
148     );
149
150 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA",      /* 570 */
151            "\\_SB.PCI0.AGP0.VID0",      /* 600e/x, 770x */
152            "\\_SB.PCI0.VID0",   /* 770e */
153            "\\_SB.PCI0.VID",    /* A21e, G4x, R50e, X30, X40 */
154            "\\_SB.PCI0.AGP.VID",        /* all others */
155     );                          /* R30, R31 */
156
157 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID");  /* G41 */
158
159 IBM_HANDLE(cmos, root, "\\UCMS",        /* R50, R50e, R50p, R51, T4x, X31, X40 */
160            "\\CMOS",            /* A3x, G4x, R32, T23, T30, X22-24, X30 */
161            "\\CMS",             /* R40, R40e */
162     );                          /* all others */
163 #ifdef CONFIG_ACPI_IBM_DOCK
164 IBM_HANDLE(dock, root, "\\_SB.GDCK",    /* X30, X31, X40 */
165            "\\_SB.PCI0.DOCK",   /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
166            "\\_SB.PCI0.PCI1.DOCK",      /* all others */
167            "\\_SB.PCI.ISA.SLCE",        /* 570 */
168     );                          /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
169 #endif
170 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST",        /* 570 */
171            "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
172            "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
173     );                          /* A21e, R30, R31 */
174
175 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
176            "_EJ0",              /* all others */
177     );                          /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
178
179 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV",     /* A3x, R32 */
180            "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
181     );                          /* all others */
182
183 IBM_HANDLE(bay2_ej, bay2, "_EJ3",       /* 600e/x, 770e, A3x */
184            "_EJ0",              /* 770x */
185     );                          /* all others */
186
187 /* don't list other alternatives as we install a notify handler on the 570 */
188 IBM_HANDLE(pci, root, "\\_SB.PCI");     /* 570 */
189
190 IBM_HANDLE(hkey, ec, "\\_SB.HKEY",      /* 600e/x, 770e, 770x */
191            "^HKEY",             /* R30, R31 */
192            "HKEY",              /* all others */
193     );                          /* 570 */
194
195 IBM_HANDLE(lght, root, "\\LGHT");       /* A21e, A2xm/p, T20-22, X20-21 */
196 IBM_HANDLE(ledb, ec, "LEDB");   /* G4x */
197
198 IBM_HANDLE(led, ec, "SLED",     /* 570 */
199            "SYSL",              /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
200            "LED",               /* all others */
201     );                          /* R30, R31 */
202
203 IBM_HANDLE(beep, ec, "BEEP");   /* all except R30, R31 */
204 IBM_HANDLE(ecrd, ec, "ECRD");   /* 570 */
205 IBM_HANDLE(ecwr, ec, "ECWR");   /* 570 */
206 IBM_HANDLE(fans, ec, "FANS");   /* X31, X40 */
207
208 IBM_HANDLE(gfan, ec, "GFAN",    /* 570 */
209            "\\FSPD",            /* 600e/x, 770e, 770x */
210     );                          /* all others */
211
212 IBM_HANDLE(sfan, ec, "SFAN",    /* 570 */
213            "JFNS",              /* 770x-JL */
214     );                          /* all others */
215
216 #define IBM_HKEY_HID    "IBM0068"
217 #define IBM_PCI_HID     "PNP0A03"
218
219 struct ibm_struct {
220         char *name;
221         char param[32];
222
223         char *hid;
224         struct acpi_driver *driver;
225
226         int (*init) (void);
227         int (*read) (char *);
228         int (*write) (char *);
229         void (*exit) (void);
230
231         void (*notify) (struct ibm_struct *, u32);
232         acpi_handle *handle;
233         int type;
234         struct acpi_device *device;
235
236         int driver_registered;
237         int proc_created;
238         int init_called;
239         int notify_installed;
240
241         int experimental;
242 };
243
244 static struct proc_dir_entry *proc_dir = NULL;
245
246 #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
247 #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
248 #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
249
250 static int acpi_evalf(acpi_handle handle,
251                       void *res, char *method, char *fmt, ...)
252 {
253         char *fmt0 = fmt;
254         struct acpi_object_list params;
255         union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
256         struct acpi_buffer result, *resultp;
257         union acpi_object out_obj;
258         acpi_status status;
259         va_list ap;
260         char res_type;
261         int success;
262         int quiet;
263
264         if (!*fmt) {
265                 printk(IBM_ERR "acpi_evalf() called with empty format\n");
266                 return 0;
267         }
268
269         if (*fmt == 'q') {
270                 quiet = 1;
271                 fmt++;
272         } else
273                 quiet = 0;
274
275         res_type = *(fmt++);
276
277         params.count = 0;
278         params.pointer = &in_objs[0];
279
280         va_start(ap, fmt);
281         while (*fmt) {
282                 char c = *(fmt++);
283                 switch (c) {
284                 case 'd':       /* int */
285                         in_objs[params.count].integer.value = va_arg(ap, int);
286                         in_objs[params.count++].type = ACPI_TYPE_INTEGER;
287                         break;
288                         /* add more types as needed */
289                 default:
290                         printk(IBM_ERR "acpi_evalf() called "
291                                "with invalid format character '%c'\n", c);
292                         return 0;
293                 }
294         }
295         va_end(ap);
296
297         if (res_type != 'v') {
298                 result.length = sizeof(out_obj);
299                 result.pointer = &out_obj;
300                 resultp = &result;
301         } else
302                 resultp = NULL;
303
304         status = acpi_evaluate_object(handle, method, &params, resultp);
305
306         switch (res_type) {
307         case 'd':               /* int */
308                 if (res)
309                         *(int *)res = out_obj.integer.value;
310                 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
311                 break;
312         case 'v':               /* void */
313                 success = status == AE_OK;
314                 break;
315                 /* add more types as needed */
316         default:
317                 printk(IBM_ERR "acpi_evalf() called "
318                        "with invalid format character '%c'\n", res_type);
319                 return 0;
320         }
321
322         if (!success && !quiet)
323                 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
324                        method, fmt0, status);
325
326         return success;
327 }
328
329 static void __unused acpi_print_int(acpi_handle handle, char *method)
330 {
331         int i;
332
333         if (acpi_evalf(handle, &i, method, "d"))
334                 printk(IBM_INFO "%s = 0x%x\n", method, i);
335         else
336                 printk(IBM_ERR "error calling %s\n", method);
337 }
338
339 static char *next_cmd(char **cmds)
340 {
341         char *start = *cmds;
342         char *end;
343
344         while ((end = strchr(start, ',')) && end == start)
345                 start = end + 1;
346
347         if (!end)
348                 return NULL;
349
350         *end = 0;
351         *cmds = end + 1;
352         return start;
353 }
354
355 static int driver_init(void)
356 {
357         printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
358         printk(IBM_INFO "%s\n", IBM_URL);
359
360         return 0;
361 }
362
363 static int driver_read(char *p)
364 {
365         int len = 0;
366
367         len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
368         len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
369
370         return len;
371 }
372
373 static int hotkey_supported;
374 static int hotkey_mask_supported;
375 static int hotkey_orig_status;
376 static int hotkey_orig_mask;
377
378 static int hotkey_get(int *status, int *mask)
379 {
380         if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
381                 return 0;
382
383         if (hotkey_mask_supported)
384                 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
385                         return 0;
386
387         return 1;
388 }
389
390 static int hotkey_set(int status, int mask)
391 {
392         int i;
393
394         if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
395                 return 0;
396
397         if (hotkey_mask_supported)
398                 for (i = 0; i < 32; i++) {
399                         int bit = ((1 << i) & mask) != 0;
400                         if (!acpi_evalf(hkey_handle,
401                                         NULL, "MHKM", "vdd", i + 1, bit))
402                                 return 0;
403                 }
404
405         return 1;
406 }
407
408 static int hotkey_init(void)
409 {
410         /* hotkey not supported on 570 */
411         hotkey_supported = hkey_handle != NULL;
412
413         if (hotkey_supported) {
414                 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
415                    A30, R30, R31, T20-22, X20-21, X22-24 */
416                 hotkey_mask_supported =
417                     acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
418
419                 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
420                         return -ENODEV;
421         }
422
423         return 0;
424 }
425
426 static int hotkey_read(char *p)
427 {
428         int status, mask;
429         int len = 0;
430
431         if (!hotkey_supported) {
432                 len += sprintf(p + len, "status:\t\tnot supported\n");
433                 return len;
434         }
435
436         if (!hotkey_get(&status, &mask))
437                 return -EIO;
438
439         len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
440         if (hotkey_mask_supported) {
441                 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
442                 len += sprintf(p + len,
443                                "commands:\tenable, disable, reset, <mask>\n");
444         } else {
445                 len += sprintf(p + len, "mask:\t\tnot supported\n");
446                 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
447         }
448
449         return len;
450 }
451
452 static int hotkey_write(char *buf)
453 {
454         int status, mask;
455         char *cmd;
456         int do_cmd = 0;
457
458         if (!hotkey_supported)
459                 return -ENODEV;
460
461         if (!hotkey_get(&status, &mask))
462                 return -EIO;
463
464         while ((cmd = next_cmd(&buf))) {
465                 if (strlencmp(cmd, "enable") == 0) {
466                         status = 1;
467                 } else if (strlencmp(cmd, "disable") == 0) {
468                         status = 0;
469                 } else if (strlencmp(cmd, "reset") == 0) {
470                         status = hotkey_orig_status;
471                         mask = hotkey_orig_mask;
472                 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
473                         /* mask set */
474                 } else if (sscanf(cmd, "%x", &mask) == 1) {
475                         /* mask set */
476                 } else
477                         return -EINVAL;
478                 do_cmd = 1;
479         }
480
481         if (do_cmd && !hotkey_set(status, mask))
482                 return -EIO;
483
484         return 0;
485 }
486
487 static void hotkey_exit(void)
488 {
489         if (hotkey_supported)
490                 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
491 }
492
493 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
494 {
495         int hkey;
496
497         if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
498                 acpi_bus_generate_event(ibm->device, event, hkey);
499         else {
500                 printk(IBM_ERR "unknown hotkey event %d\n", event);
501                 acpi_bus_generate_event(ibm->device, event, 0);
502         }
503 }
504
505 static int bluetooth_supported;
506
507 static int bluetooth_init(void)
508 {
509         /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
510            G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
511         bluetooth_supported = hkey_handle &&
512             acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
513
514         return 0;
515 }
516
517 static int bluetooth_status(void)
518 {
519         int status;
520
521         if (!bluetooth_supported ||
522             !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
523                 status = 0;
524
525         return status;
526 }
527
528 static int bluetooth_read(char *p)
529 {
530         int len = 0;
531         int status = bluetooth_status();
532
533         if (!bluetooth_supported)
534                 len += sprintf(p + len, "status:\t\tnot supported\n");
535         else if (!(status & 1))
536                 len += sprintf(p + len, "status:\t\tnot installed\n");
537         else {
538                 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
539                 len += sprintf(p + len, "commands:\tenable, disable\n");
540         }
541
542         return len;
543 }
544
545 static int bluetooth_write(char *buf)
546 {
547         int status = bluetooth_status();
548         char *cmd;
549         int do_cmd = 0;
550
551         if (!bluetooth_supported)
552                 return -ENODEV;
553
554         while ((cmd = next_cmd(&buf))) {
555                 if (strlencmp(cmd, "enable") == 0) {
556                         status |= 2;
557                 } else if (strlencmp(cmd, "disable") == 0) {
558                         status &= ~2;
559                 } else
560                         return -EINVAL;
561                 do_cmd = 1;
562         }
563
564         if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
565                 return -EIO;
566
567         return 0;
568 }
569
570 static int video_supported;
571 static int video_orig_autosw;
572
573 #define VIDEO_570 1
574 #define VIDEO_770 2
575 #define VIDEO_NEW 3
576
577 static int video_init(void)
578 {
579         int ivga;
580
581         if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
582                 /* G41, assume IVGA doesn't change */
583                 vid_handle = vid2_handle;
584
585         if (!vid_handle)
586                 /* video switching not supported on R30, R31 */
587                 video_supported = 0;
588         else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
589                 /* 570 */
590                 video_supported = VIDEO_570;
591         else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
592                 /* 600e/x, 770e, 770x */
593                 video_supported = VIDEO_770;
594         else
595                 /* all others */
596                 video_supported = VIDEO_NEW;
597
598         return 0;
599 }
600
601 static int video_status(void)
602 {
603         int status = 0;
604         int i;
605
606         if (video_supported == VIDEO_570) {
607                 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
608                         status = i & 3;
609         } else if (video_supported == VIDEO_770) {
610                 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
611                         status |= 0x01 * i;
612                 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
613                         status |= 0x02 * i;
614         } else if (video_supported == VIDEO_NEW) {
615                 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
616                 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
617                         status |= 0x02 * i;
618
619                 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
620                 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
621                         status |= 0x01 * i;
622                 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
623                         status |= 0x08 * i;
624         }
625
626         return status;
627 }
628
629 static int video_autosw(void)
630 {
631         int autosw = 0;
632
633         if (video_supported == VIDEO_570)
634                 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
635         else if (video_supported == VIDEO_770 || video_supported == VIDEO_NEW)
636                 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
637
638         return autosw & 1;
639 }
640
641 static int video_read(char *p)
642 {
643         int status = video_status();
644         int autosw = video_autosw();
645         int len = 0;
646
647         if (!video_supported) {
648                 len += sprintf(p + len, "status:\t\tnot supported\n");
649                 return len;
650         }
651
652         len += sprintf(p + len, "status:\t\tsupported\n");
653         len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
654         len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
655         if (video_supported == VIDEO_NEW)
656                 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
657         len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
658         len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
659         len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
660         if (video_supported == VIDEO_NEW)
661                 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
662         len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
663         len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
664
665         return len;
666 }
667
668 static int video_switch(void)
669 {
670         int autosw = video_autosw();
671         int ret;
672
673         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
674                 return -EIO;
675         ret = video_supported == VIDEO_570 ?
676             acpi_evalf(ec_handle, NULL, "_Q16", "v") :
677             acpi_evalf(vid_handle, NULL, "VSWT", "v");
678         acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
679
680         return ret;
681 }
682
683 static int video_expand(void)
684 {
685         if (video_supported == VIDEO_570)
686                 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
687         else if (video_supported == VIDEO_770)
688                 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
689         else
690                 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
691 }
692
693 static int video_switch2(int status)
694 {
695         int ret;
696
697         if (video_supported == VIDEO_570) {
698                 ret = acpi_evalf(NULL, NULL,
699                                  "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
700         } else if (video_supported == VIDEO_770) {
701                 int autosw = video_autosw();
702                 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
703                         return -EIO;
704
705                 ret = acpi_evalf(vid_handle, NULL,
706                                  "ASWT", "vdd", status * 0x100, 0);
707
708                 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
709         } else {
710                 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
711                     acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
712         }
713
714         return ret;
715 }
716
717 static int video_write(char *buf)
718 {
719         char *cmd;
720         int enable, disable, status;
721
722         if (!video_supported)
723                 return -ENODEV;
724
725         enable = disable = 0;
726
727         while ((cmd = next_cmd(&buf))) {
728                 if (strlencmp(cmd, "lcd_enable") == 0) {
729                         enable |= 0x01;
730                 } else if (strlencmp(cmd, "lcd_disable") == 0) {
731                         disable |= 0x01;
732                 } else if (strlencmp(cmd, "crt_enable") == 0) {
733                         enable |= 0x02;
734                 } else if (strlencmp(cmd, "crt_disable") == 0) {
735                         disable |= 0x02;
736                 } else if (video_supported == VIDEO_NEW &&
737                            strlencmp(cmd, "dvi_enable") == 0) {
738                         enable |= 0x08;
739                 } else if (video_supported == VIDEO_NEW &&
740                            strlencmp(cmd, "dvi_disable") == 0) {
741                         disable |= 0x08;
742                 } else if (strlencmp(cmd, "auto_enable") == 0) {
743                         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
744                                 return -EIO;
745                 } else if (strlencmp(cmd, "auto_disable") == 0) {
746                         if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
747                                 return -EIO;
748                 } else if (strlencmp(cmd, "video_switch") == 0) {
749                         if (!video_switch())
750                                 return -EIO;
751                 } else if (strlencmp(cmd, "expand_toggle") == 0) {
752                         if (!video_expand())
753                                 return -EIO;
754                 } else
755                         return -EINVAL;
756         }
757
758         if (enable || disable) {
759                 status = (video_status() & 0x0f & ~disable) | enable;
760                 if (!video_switch2(status))
761                         return -EIO;
762         }
763
764         return 0;
765 }
766
767 static void video_exit(void)
768 {
769         acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
770 }
771
772 static int light_supported;
773 static int light_status_supported;
774
775 static int light_init(void)
776 {
777         /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
778         light_supported = (cmos_handle || lght_handle) && !ledb_handle;
779
780         if (light_supported)
781                 /* light status not supported on
782                    570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
783                 light_status_supported = acpi_evalf(ec_handle, NULL,
784                                                     "KBLT", "qv");
785
786         return 0;
787 }
788
789 static int light_read(char *p)
790 {
791         int len = 0;
792         int status = 0;
793
794         if (!light_supported) {
795                 len += sprintf(p + len, "status:\t\tnot supported\n");
796         } else if (!light_status_supported) {
797                 len += sprintf(p + len, "status:\t\tunknown\n");
798                 len += sprintf(p + len, "commands:\ton, off\n");
799         } else {
800                 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
801                         return -EIO;
802                 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
803                 len += sprintf(p + len, "commands:\ton, off\n");
804         }
805
806         return len;
807 }
808
809 static int light_write(char *buf)
810 {
811         int cmos_cmd, lght_cmd;
812         char *cmd;
813         int success;
814
815         if (!light_supported)
816                 return -ENODEV;
817
818         while ((cmd = next_cmd(&buf))) {
819                 if (strlencmp(cmd, "on") == 0) {
820                         cmos_cmd = 0x0c;
821                         lght_cmd = 1;
822                 } else if (strlencmp(cmd, "off") == 0) {
823                         cmos_cmd = 0x0d;
824                         lght_cmd = 0;
825                 } else
826                         return -EINVAL;
827
828                 success = cmos_handle ?
829                     acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
830                     acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
831                 if (!success)
832                         return -EIO;
833         }
834
835         return 0;
836 }
837
838 static int _sta(acpi_handle handle)
839 {
840         int status;
841
842         if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
843                 status = 0;
844
845         return status;
846 }
847 #ifdef CONFIG_ACPI_IBM_DOCK
848 #define dock_docked() (_sta(dock_handle) & 1)
849
850 static int dock_read(char *p)
851 {
852         int len = 0;
853         int docked = dock_docked();
854
855         if (!dock_handle)
856                 len += sprintf(p + len, "status:\t\tnot supported\n");
857         else if (!docked)
858                 len += sprintf(p + len, "status:\t\tundocked\n");
859         else {
860                 len += sprintf(p + len, "status:\t\tdocked\n");
861                 len += sprintf(p + len, "commands:\tdock, undock\n");
862         }
863
864         return len;
865 }
866
867 static int dock_write(char *buf)
868 {
869         char *cmd;
870
871         if (!dock_docked())
872                 return -ENODEV;
873
874         while ((cmd = next_cmd(&buf))) {
875                 if (strlencmp(cmd, "undock") == 0) {
876                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
877                             !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
878                                 return -EIO;
879                 } else if (strlencmp(cmd, "dock") == 0) {
880                         if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
881                                 return -EIO;
882                 } else
883                         return -EINVAL;
884         }
885
886         return 0;
887 }
888
889 static void dock_notify(struct ibm_struct *ibm, u32 event)
890 {
891         int docked = dock_docked();
892         int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
893
894         if (event == 1 && !pci) /* 570 */
895                 acpi_bus_generate_event(ibm->device, event, 1); /* button */
896         else if (event == 1 && pci)     /* 570 */
897                 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
898         else if (event == 3 && docked)
899                 acpi_bus_generate_event(ibm->device, event, 1); /* button */
900         else if (event == 3 && !docked)
901                 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
902         else if (event == 0 && docked)
903                 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
904         else {
905                 printk(IBM_ERR "unknown dock event %d, status %d\n",
906                        event, _sta(dock_handle));
907                 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
908         }
909 }
910 #endif
911
912 static int bay_status_supported;
913 static int bay_status2_supported;
914 static int bay_eject_supported;
915 static int bay_eject2_supported;
916
917 static int bay_init(void)
918 {
919         bay_status_supported = bay_handle &&
920             acpi_evalf(bay_handle, NULL, "_STA", "qv");
921         bay_status2_supported = bay2_handle &&
922             acpi_evalf(bay2_handle, NULL, "_STA", "qv");
923
924         bay_eject_supported = bay_handle && bay_ej_handle &&
925             (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
926         bay_eject2_supported = bay2_handle && bay2_ej_handle &&
927             (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
928
929         return 0;
930 }
931
932 #define bay_occupied(b) (_sta(b##_handle) & 1)
933
934 static int bay_read(char *p)
935 {
936         int len = 0;
937         int occupied = bay_occupied(bay);
938         int occupied2 = bay_occupied(bay2);
939         int eject, eject2;
940
941         len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
942                        (occupied ? "occupied" : "unoccupied") :
943                        "not supported");
944         if (bay_status2_supported)
945                 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
946                                "occupied" : "unoccupied");
947
948         eject = bay_eject_supported && occupied;
949         eject2 = bay_eject2_supported && occupied2;
950
951         if (eject && eject2)
952                 len += sprintf(p + len, "commands:\teject, eject2\n");
953         else if (eject)
954                 len += sprintf(p + len, "commands:\teject\n");
955         else if (eject2)
956                 len += sprintf(p + len, "commands:\teject2\n");
957
958         return len;
959 }
960
961 static int bay_write(char *buf)
962 {
963         char *cmd;
964
965         if (!bay_eject_supported && !bay_eject2_supported)
966                 return -ENODEV;
967
968         while ((cmd = next_cmd(&buf))) {
969                 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
970                         if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
971                                 return -EIO;
972                 } else if (bay_eject2_supported &&
973                            strlencmp(cmd, "eject2") == 0) {
974                         if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
975                                 return -EIO;
976                 } else
977                         return -EINVAL;
978         }
979
980         return 0;
981 }
982
983 static void bay_notify(struct ibm_struct *ibm, u32 event)
984 {
985         acpi_bus_generate_event(ibm->device, event, 0);
986 }
987
988 static int cmos_read(char *p)
989 {
990         int len = 0;
991
992         /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
993            R30, R31, T20-22, X20-21 */
994         if (!cmos_handle)
995                 len += sprintf(p + len, "status:\t\tnot supported\n");
996         else {
997                 len += sprintf(p + len, "status:\t\tsupported\n");
998                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
999         }
1000
1001         return len;
1002 }
1003
1004 static int cmos_eval(int cmos_cmd)
1005 {
1006         if (cmos_handle)
1007                 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1008         else
1009                 return 1;
1010 }
1011
1012 static int cmos_write(char *buf)
1013 {
1014         char *cmd;
1015         int cmos_cmd;
1016
1017         if (!cmos_handle)
1018                 return -EINVAL;
1019
1020         while ((cmd = next_cmd(&buf))) {
1021                 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1022                     cmos_cmd >= 0 && cmos_cmd <= 21) {
1023                         /* cmos_cmd set */
1024                 } else
1025                         return -EINVAL;
1026
1027                 if (!cmos_eval(cmos_cmd))
1028                         return -EIO;
1029         }
1030
1031         return 0;
1032 }
1033
1034 static int led_supported;
1035
1036 #define LED_570 1
1037 #define LED_OLD 2
1038 #define LED_NEW 3
1039
1040 static int led_init(void)
1041 {
1042         if (!led_handle)
1043                 /* led not supported on R30, R31 */
1044                 led_supported = 0;
1045         else if (strlencmp(led_path, "SLED") == 0)
1046                 /* 570 */
1047                 led_supported = LED_570;
1048         else if (strlencmp(led_path, "SYSL") == 0)
1049                 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1050                 led_supported = LED_OLD;
1051         else
1052                 /* all others */
1053                 led_supported = LED_NEW;
1054
1055         return 0;
1056 }
1057
1058 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1059
1060 static int led_read(char *p)
1061 {
1062         int len = 0;
1063
1064         if (!led_supported) {
1065                 len += sprintf(p + len, "status:\t\tnot supported\n");
1066                 return len;
1067         }
1068         len += sprintf(p + len, "status:\t\tsupported\n");
1069
1070         if (led_supported == LED_570) {
1071                 /* 570 */
1072                 int i, status;
1073                 for (i = 0; i < 8; i++) {
1074                         if (!acpi_evalf(ec_handle,
1075                                         &status, "GLED", "dd", 1 << i))
1076                                 return -EIO;
1077                         len += sprintf(p + len, "%d:\t\t%s\n",
1078                                        i, led_status(status));
1079                 }
1080         }
1081
1082         len += sprintf(p + len, "commands:\t"
1083                        "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1084
1085         return len;
1086 }
1087
1088 /* off, on, blink */
1089 static const int led_sled_arg1[] = { 0, 1, 3 };
1090 static const int led_exp_hlbl[] = { 0, 0, 1 };  /* led# * */
1091 static const int led_exp_hlcl[] = { 0, 1, 1 };  /* led# * */
1092 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1093
1094 #define EC_HLCL 0x0c
1095 #define EC_HLBL 0x0d
1096 #define EC_HLMS 0x0e
1097
1098 static int led_write(char *buf)
1099 {
1100         char *cmd;
1101         int led, ind, ret;
1102
1103         if (!led_supported)
1104                 return -ENODEV;
1105
1106         while ((cmd = next_cmd(&buf))) {
1107                 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1108                         return -EINVAL;
1109
1110                 if (strstr(cmd, "off")) {
1111                         ind = 0;
1112                 } else if (strstr(cmd, "on")) {
1113                         ind = 1;
1114                 } else if (strstr(cmd, "blink")) {
1115                         ind = 2;
1116                 } else
1117                         return -EINVAL;
1118
1119                 if (led_supported == LED_570) {
1120                         /* 570 */
1121                         led = 1 << led;
1122                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1123                                         led, led_sled_arg1[ind]))
1124                                 return -EIO;
1125                 } else if (led_supported == LED_OLD) {
1126                         /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1127                         led = 1 << led;
1128                         ret = ec_write(EC_HLMS, led);
1129                         if (ret >= 0)
1130                                 ret =
1131                                     ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1132                         if (ret >= 0)
1133                                 ret =
1134                                     ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1135                         if (ret < 0)
1136                                 return ret;
1137                 } else {
1138                         /* all others */
1139                         if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1140                                         led, led_led_arg1[ind]))
1141                                 return -EIO;
1142                 }
1143         }
1144
1145         return 0;
1146 }
1147
1148 static int beep_read(char *p)
1149 {
1150         int len = 0;
1151
1152         if (!beep_handle)
1153                 len += sprintf(p + len, "status:\t\tnot supported\n");
1154         else {
1155                 len += sprintf(p + len, "status:\t\tsupported\n");
1156                 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1157         }
1158
1159         return len;
1160 }
1161
1162 static int beep_write(char *buf)
1163 {
1164         char *cmd;
1165         int beep_cmd;
1166
1167         if (!beep_handle)
1168                 return -ENODEV;
1169
1170         while ((cmd = next_cmd(&buf))) {
1171                 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1172                     beep_cmd >= 0 && beep_cmd <= 17) {
1173                         /* beep_cmd set */
1174                 } else
1175                         return -EINVAL;
1176                 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1177                         return -EIO;
1178         }
1179
1180         return 0;
1181 }
1182
1183 static int acpi_ec_read(int i, u8 * p)
1184 {
1185         int v;
1186
1187         if (ecrd_handle) {
1188                 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1189                         return 0;
1190                 *p = v;
1191         } else {
1192                 if (ec_read(i, p) < 0)
1193                         return 0;
1194         }
1195
1196         return 1;
1197 }
1198
1199 static int acpi_ec_write(int i, u8 v)
1200 {
1201         if (ecwr_handle) {
1202                 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1203                         return 0;
1204         } else {
1205                 if (ec_write(i, v) < 0)
1206                         return 0;
1207         }
1208
1209         return 1;
1210 }
1211
1212 static int thermal_tmp_supported;
1213 static int thermal_updt_supported;
1214
1215 static int thermal_init(void)
1216 {
1217         /* temperatures not supported on 570, G4x, R30, R31, R32 */
1218         thermal_tmp_supported = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1219
1220         /* 600e/x, 770e, 770x */
1221         thermal_updt_supported = acpi_evalf(ec_handle, NULL, "UPDT", "qv");
1222
1223         return 0;
1224 }
1225
1226 static int thermal_read(char *p)
1227 {
1228         int len = 0;
1229
1230         if (!thermal_tmp_supported)
1231                 len += sprintf(p + len, "temperatures:\tnot supported\n");
1232         else {
1233                 int i, t;
1234                 char tmpi[] = "TMPi";
1235                 s8 tmp[8];
1236
1237                 if (thermal_updt_supported)
1238                         if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1239                                 return -EIO;
1240
1241                 for (i = 0; i < 8; i++) {
1242                         tmpi[3] = '0' + i;
1243                         if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1244                                 return -EIO;
1245                         if (thermal_updt_supported)
1246                                 tmp[i] = (t - 2732 + 5) / 10;
1247                         else
1248                                 tmp[i] = t;
1249                 }
1250
1251                 len += sprintf(p + len,
1252                                "temperatures:\t%d %d %d %d %d %d %d %d\n",
1253                                tmp[0], tmp[1], tmp[2], tmp[3],
1254                                tmp[4], tmp[5], tmp[6], tmp[7]);
1255         }
1256
1257         return len;
1258 }
1259
1260 static u8 ecdump_regs[256];
1261
1262 static int ecdump_read(char *p)
1263 {
1264         int len = 0;
1265         int i, j;
1266         u8 v;
1267
1268         len += sprintf(p + len, "EC      "
1269                        " +00 +01 +02 +03 +04 +05 +06 +07"
1270                        " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1271         for (i = 0; i < 256; i += 16) {
1272                 len += sprintf(p + len, "EC 0x%02x:", i);
1273                 for (j = 0; j < 16; j++) {
1274                         if (!acpi_ec_read(i + j, &v))
1275                                 break;
1276                         if (v != ecdump_regs[i + j])
1277                                 len += sprintf(p + len, " *%02x", v);
1278                         else
1279                                 len += sprintf(p + len, "  %02x", v);
1280                         ecdump_regs[i + j] = v;
1281                 }
1282                 len += sprintf(p + len, "\n");
1283                 if (j != 16)
1284                         break;
1285         }
1286
1287         /* These are way too dangerous to advertise openly... */
1288 #if 0
1289         len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1290                        " (<offset> is 00-ff, <value> is 00-ff)\n");
1291         len += sprintf(p + len, "commands:\t0x<offset> <value>  "
1292                        " (<offset> is 00-ff, <value> is 0-255)\n");
1293 #endif
1294         return len;
1295 }
1296
1297 static int ecdump_write(char *buf)
1298 {
1299         char *cmd;
1300         int i, v;
1301
1302         while ((cmd = next_cmd(&buf))) {
1303                 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1304                         /* i and v set */
1305                 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1306                         /* i and v set */
1307                 } else
1308                         return -EINVAL;
1309                 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1310                         if (!acpi_ec_write(i, v))
1311                                 return -EIO;
1312                 } else
1313                         return -EINVAL;
1314         }
1315
1316         return 0;
1317 }
1318
1319 static int brightness_offset = 0x31;
1320
1321 static int brightness_read(char *p)
1322 {
1323         int len = 0;
1324         u8 level;
1325
1326         if (!acpi_ec_read(brightness_offset, &level)) {
1327                 len += sprintf(p + len, "level:\t\tunreadable\n");
1328         } else {
1329                 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1330                 len += sprintf(p + len, "commands:\tup, down\n");
1331                 len += sprintf(p + len, "commands:\tlevel <level>"
1332                                " (<level> is 0-7)\n");
1333         }
1334
1335         return len;
1336 }
1337
1338 #define BRIGHTNESS_UP   4
1339 #define BRIGHTNESS_DOWN 5
1340
1341 static int brightness_write(char *buf)
1342 {
1343         int cmos_cmd, inc, i;
1344         u8 level;
1345         int new_level;
1346         char *cmd;
1347
1348         while ((cmd = next_cmd(&buf))) {
1349                 if (!acpi_ec_read(brightness_offset, &level))
1350                         return -EIO;
1351                 level &= 7;
1352
1353                 if (strlencmp(cmd, "up") == 0) {
1354                         new_level = level == 7 ? 7 : level + 1;
1355                 } else if (strlencmp(cmd, "down") == 0) {
1356                         new_level = level == 0 ? 0 : level - 1;
1357                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1358                            new_level >= 0 && new_level <= 7) {
1359                         /* new_level set */
1360                 } else
1361                         return -EINVAL;
1362
1363                 cmos_cmd = new_level > level ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1364                 inc = new_level > level ? 1 : -1;
1365                 for (i = level; i != new_level; i += inc) {
1366                         if (!cmos_eval(cmos_cmd))
1367                                 return -EIO;
1368                         if (!acpi_ec_write(brightness_offset, i + inc))
1369                                 return -EIO;
1370                 }
1371         }
1372
1373         return 0;
1374 }
1375
1376 static int volume_offset = 0x30;
1377
1378 static int volume_read(char *p)
1379 {
1380         int len = 0;
1381         u8 level;
1382
1383         if (!acpi_ec_read(volume_offset, &level)) {
1384                 len += sprintf(p + len, "level:\t\tunreadable\n");
1385         } else {
1386                 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1387                 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1388                 len += sprintf(p + len, "commands:\tup, down, mute\n");
1389                 len += sprintf(p + len, "commands:\tlevel <level>"
1390                                " (<level> is 0-15)\n");
1391         }
1392
1393         return len;
1394 }
1395
1396 #define VOLUME_DOWN     0
1397 #define VOLUME_UP       1
1398 #define VOLUME_MUTE     2
1399
1400 static int volume_write(char *buf)
1401 {
1402         int cmos_cmd, inc, i;
1403         u8 level, mute;
1404         int new_level, new_mute;
1405         char *cmd;
1406
1407         while ((cmd = next_cmd(&buf))) {
1408                 if (!acpi_ec_read(volume_offset, &level))
1409                         return -EIO;
1410                 new_mute = mute = level & 0x40;
1411                 new_level = level = level & 0xf;
1412
1413                 if (strlencmp(cmd, "up") == 0) {
1414                         if (mute)
1415                                 new_mute = 0;
1416                         else
1417                                 new_level = level == 15 ? 15 : level + 1;
1418                 } else if (strlencmp(cmd, "down") == 0) {
1419                         if (mute)
1420                                 new_mute = 0;
1421                         else
1422                                 new_level = level == 0 ? 0 : level - 1;
1423                 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1424                            new_level >= 0 && new_level <= 15) {
1425                         /* new_level set */
1426                 } else if (strlencmp(cmd, "mute") == 0) {
1427                         new_mute = 0x40;
1428                 } else
1429                         return -EINVAL;
1430
1431                 if (new_level != level) {       /* mute doesn't change */
1432                         cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1433                         inc = new_level > level ? 1 : -1;
1434
1435                         if (mute && (!cmos_eval(cmos_cmd) ||
1436                                      !acpi_ec_write(volume_offset, level)))
1437                                 return -EIO;
1438
1439                         for (i = level; i != new_level; i += inc)
1440                                 if (!cmos_eval(cmos_cmd) ||
1441                                     !acpi_ec_write(volume_offset, i + inc))
1442                                         return -EIO;
1443
1444                         if (mute && (!cmos_eval(VOLUME_MUTE) ||
1445                                      !acpi_ec_write(volume_offset,
1446                                                     new_level + mute)))
1447                                 return -EIO;
1448                 }
1449
1450                 if (new_mute != mute) { /* level doesn't change */
1451                         cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1452
1453                         if (!cmos_eval(cmos_cmd) ||
1454                             !acpi_ec_write(volume_offset, level + new_mute))
1455                                 return -EIO;
1456                 }
1457         }
1458
1459         return 0;
1460 }
1461
1462 static int fan_status_offset = 0x2f;
1463 static int fan_rpm_offset = 0x84;
1464
1465 static int fan_read(char *p)
1466 {
1467         int len = 0;
1468         int s;
1469         u8 lo, hi, status;
1470
1471         if (gfan_handle) {
1472                 /* 570, 600e/x, 770e, 770x */
1473                 if (!acpi_evalf(gfan_handle, &s, NULL, "d"))
1474                         return -EIO;
1475
1476                 len += sprintf(p + len, "level:\t\t%d\n", s);
1477         } else {
1478                 /* all except 570, 600e/x, 770e, 770x */
1479                 if (!acpi_ec_read(fan_status_offset, &status))
1480                         len += sprintf(p + len, "status:\t\tunreadable\n");
1481                 else
1482                         len += sprintf(p + len, "status:\t\t%s\n",
1483                                        enabled(status, 7));
1484
1485                 if (!acpi_ec_read(fan_rpm_offset, &lo) ||
1486                     !acpi_ec_read(fan_rpm_offset + 1, &hi))
1487                         len += sprintf(p + len, "speed:\t\tunreadable\n");
1488                 else
1489                         len += sprintf(p + len, "speed:\t\t%d\n",
1490                                        (hi << 8) + lo);
1491         }
1492
1493         if (sfan_handle)
1494                 /* 570, 770x-JL */
1495                 len += sprintf(p + len, "commands:\tlevel <level>"
1496                                " (<level> is 0-7)\n");
1497         if (!gfan_handle)
1498                 /* all except 570, 600e/x, 770e, 770x */
1499                 len += sprintf(p + len, "commands:\tenable, disable\n");
1500         if (fans_handle)
1501                 /* X31, X40 */
1502                 len += sprintf(p + len, "commands:\tspeed <speed>"
1503                                " (<speed> is 0-65535)\n");
1504
1505         return len;
1506 }
1507
1508 static int fan_write(char *buf)
1509 {
1510         char *cmd;
1511         int level, speed;
1512
1513         while ((cmd = next_cmd(&buf))) {
1514                 if (sfan_handle &&
1515                     sscanf(cmd, "level %d", &level) == 1 &&
1516                     level >= 0 && level <= 7) {
1517                         /* 570, 770x-JL */
1518                         if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
1519                                 return -EIO;
1520                 } else if (!gfan_handle && strlencmp(cmd, "enable") == 0) {
1521                         /* all except 570, 600e/x, 770e, 770x */
1522                         if (!acpi_ec_write(fan_status_offset, 0x80))
1523                                 return -EIO;
1524                 } else if (!gfan_handle && strlencmp(cmd, "disable") == 0) {
1525                         /* all except 570, 600e/x, 770e, 770x */
1526                         if (!acpi_ec_write(fan_status_offset, 0x00))
1527                                 return -EIO;
1528                 } else if (fans_handle &&
1529                            sscanf(cmd, "speed %d", &speed) == 1 &&
1530                            speed >= 0 && speed <= 65535) {
1531                         /* X31, X40 */
1532                         if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
1533                                         speed, speed, speed))
1534                                 return -EIO;
1535                 } else
1536                         return -EINVAL;
1537         }
1538
1539         return 0;
1540 }
1541
1542 static struct ibm_struct ibms[] = {
1543         {
1544          .name = "driver",
1545          .init = driver_init,
1546          .read = driver_read,
1547          },
1548         {
1549          .name = "hotkey",
1550          .hid = IBM_HKEY_HID,
1551          .init = hotkey_init,
1552          .read = hotkey_read,
1553          .write = hotkey_write,
1554          .exit = hotkey_exit,
1555          .notify = hotkey_notify,
1556          .handle = &hkey_handle,
1557          .type = ACPI_DEVICE_NOTIFY,
1558          },
1559         {
1560          .name = "bluetooth",
1561          .init = bluetooth_init,
1562          .read = bluetooth_read,
1563          .write = bluetooth_write,
1564          },
1565         {
1566          .name = "video",
1567          .init = video_init,
1568          .read = video_read,
1569          .write = video_write,
1570          .exit = video_exit,
1571          },
1572         {
1573          .name = "light",
1574          .init = light_init,
1575          .read = light_read,
1576          .write = light_write,
1577          },
1578 #ifdef CONFIG_ACPI_IBM_DOCK
1579         {
1580          .name = "dock",
1581          .read = dock_read,
1582          .write = dock_write,
1583          .notify = dock_notify,
1584          .handle = &dock_handle,
1585          .type = ACPI_SYSTEM_NOTIFY,
1586          },
1587         {
1588          .name = "dock",
1589          .hid = IBM_PCI_HID,
1590          .notify = dock_notify,
1591          .handle = &pci_handle,
1592          .type = ACPI_SYSTEM_NOTIFY,
1593          },
1594 #endif
1595         {
1596          .name = "bay",
1597          .init = bay_init,
1598          .read = bay_read,
1599          .write = bay_write,
1600          .notify = bay_notify,
1601          .handle = &bay_handle,
1602          .type = ACPI_SYSTEM_NOTIFY,
1603          },
1604         {
1605          .name = "cmos",
1606          .read = cmos_read,
1607          .write = cmos_write,
1608          },
1609         {
1610          .name = "led",
1611          .init = led_init,
1612          .read = led_read,
1613          .write = led_write,
1614          },
1615         {
1616          .name = "beep",
1617          .read = beep_read,
1618          .write = beep_write,
1619          },
1620         {
1621          .name = "thermal",
1622          .init = thermal_init,
1623          .read = thermal_read,
1624          },
1625         {
1626          .name = "ecdump",
1627          .read = ecdump_read,
1628          .write = ecdump_write,
1629          .experimental = 1,
1630          },
1631         {
1632          .name = "brightness",
1633          .read = brightness_read,
1634          .write = brightness_write,
1635          .experimental = 1,
1636          },
1637         {
1638          .name = "volume",
1639          .read = volume_read,
1640          .write = volume_write,
1641          .experimental = 1,
1642          },
1643         {
1644          .name = "fan",
1645          .read = fan_read,
1646          .write = fan_write,
1647          .experimental = 1,
1648          },
1649 };
1650
1651 static int dispatch_read(char *page, char **start, off_t off, int count,
1652                          int *eof, void *data)
1653 {
1654         struct ibm_struct *ibm = (struct ibm_struct *)data;
1655         int len;
1656
1657         if (!ibm || !ibm->read)
1658                 return -EINVAL;
1659
1660         len = ibm->read(page);
1661         if (len < 0)
1662                 return len;
1663
1664         if (len <= off + count)
1665                 *eof = 1;
1666         *start = page + off;
1667         len -= off;
1668         if (len > count)
1669                 len = count;
1670         if (len < 0)
1671                 len = 0;
1672
1673         return len;
1674 }
1675
1676 static int dispatch_write(struct file *file, const char __user * userbuf,
1677                           unsigned long count, void *data)
1678 {
1679         struct ibm_struct *ibm = (struct ibm_struct *)data;
1680         char *kernbuf;
1681         int ret;
1682
1683         if (!ibm || !ibm->write)
1684                 return -EINVAL;
1685
1686         kernbuf = kmalloc(count + 2, GFP_KERNEL);
1687         if (!kernbuf)
1688                 return -ENOMEM;
1689
1690         if (copy_from_user(kernbuf, userbuf, count)) {
1691                 kfree(kernbuf);
1692                 return -EFAULT;
1693         }
1694
1695         kernbuf[count] = 0;
1696         strcat(kernbuf, ",");
1697         ret = ibm->write(kernbuf);
1698         if (ret == 0)
1699                 ret = count;
1700
1701         kfree(kernbuf);
1702
1703         return ret;
1704 }
1705
1706 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
1707 {
1708         struct ibm_struct *ibm = (struct ibm_struct *)data;
1709
1710         if (!ibm || !ibm->notify)
1711                 return;
1712
1713         ibm->notify(ibm, event);
1714 }
1715
1716 static int __init setup_notify(struct ibm_struct *ibm)
1717 {
1718         acpi_status status;
1719         int ret;
1720
1721         if (!*ibm->handle)
1722                 return 0;
1723
1724         ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
1725         if (ret < 0) {
1726                 printk(IBM_ERR "%s device not present\n", ibm->name);
1727                 return 0;
1728         }
1729
1730         acpi_driver_data(ibm->device) = ibm;
1731         sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
1732
1733         status = acpi_install_notify_handler(*ibm->handle, ibm->type,
1734                                              dispatch_notify, ibm);
1735         if (ACPI_FAILURE(status)) {
1736                 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
1737                        ibm->name, status);
1738                 return -ENODEV;
1739         }
1740
1741         return 0;
1742 }
1743
1744 static int __init ibm_device_add(struct acpi_device *device)
1745 {
1746         return 0;
1747 }
1748
1749 static int __init register_driver(struct ibm_struct *ibm)
1750 {
1751         int ret;
1752
1753         ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
1754         if (!ibm->driver) {
1755                 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
1756                 return -1;
1757         }
1758
1759         memset(ibm->driver, 0, sizeof(struct acpi_driver));
1760         sprintf(ibm->driver->name, "%s/%s", IBM_NAME, ibm->name);
1761         ibm->driver->ids = ibm->hid;
1762         ibm->driver->ops.add = &ibm_device_add;
1763
1764         ret = acpi_bus_register_driver(ibm->driver);
1765         if (ret < 0) {
1766                 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
1767                        ibm->hid, ret);
1768                 kfree(ibm->driver);
1769         }
1770
1771         return ret;
1772 }
1773
1774 static int __init ibm_init(struct ibm_struct *ibm)
1775 {
1776         int ret;
1777         struct proc_dir_entry *entry;
1778
1779         if (ibm->experimental && !experimental)
1780                 return 0;
1781
1782         if (ibm->hid) {
1783                 ret = register_driver(ibm);
1784                 if (ret < 0)
1785                         return ret;
1786                 ibm->driver_registered = 1;
1787         }
1788
1789         if (ibm->init) {
1790                 ret = ibm->init();
1791                 if (ret != 0)
1792                         return ret;
1793                 ibm->init_called = 1;
1794         }
1795
1796         if (ibm->read) {
1797                 entry = create_proc_entry(ibm->name,
1798                                           S_IFREG | S_IRUGO | S_IWUSR,
1799                                           proc_dir);
1800                 if (!entry) {
1801                         printk(IBM_ERR "unable to create proc entry %s\n",
1802                                ibm->name);
1803                         return -ENODEV;
1804                 }
1805                 entry->owner = THIS_MODULE;
1806                 entry->data = ibm;
1807                 entry->read_proc = &dispatch_read;
1808                 if (ibm->write)
1809                         entry->write_proc = &dispatch_write;
1810                 ibm->proc_created = 1;
1811         }
1812
1813         if (ibm->notify) {
1814                 ret = setup_notify(ibm);
1815                 if (ret < 0)
1816                         return ret;
1817                 ibm->notify_installed = 1;
1818         }
1819
1820         return 0;
1821 }
1822
1823 static void ibm_exit(struct ibm_struct *ibm)
1824 {
1825         if (ibm->notify_installed)
1826                 acpi_remove_notify_handler(*ibm->handle, ibm->type,
1827                                            dispatch_notify);
1828
1829         if (ibm->proc_created)
1830                 remove_proc_entry(ibm->name, proc_dir);
1831
1832         if (ibm->init_called && ibm->exit)
1833                 ibm->exit();
1834
1835         if (ibm->driver_registered) {
1836                 acpi_bus_unregister_driver(ibm->driver);
1837                 kfree(ibm->driver);
1838         }
1839 }
1840
1841 static void __init ibm_handle_init(char *name,
1842                                    acpi_handle * handle, acpi_handle parent,
1843                                    char **paths, int num_paths, char **path)
1844 {
1845         int i;
1846         acpi_status status;
1847
1848         for (i = 0; i < num_paths; i++) {
1849                 status = acpi_get_handle(parent, paths[i], handle);
1850                 if (ACPI_SUCCESS(status)) {
1851                         *path = paths[i];
1852                         return;
1853                 }
1854         }
1855
1856         *handle = NULL;
1857 }
1858
1859 #define IBM_HANDLE_INIT(object)                                         \
1860         ibm_handle_init(#object, &object##_handle, *object##_parent,    \
1861                 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
1862
1863 static int set_ibm_param(const char *val, struct kernel_param *kp)
1864 {
1865         unsigned int i;
1866
1867         for (i = 0; i < ARRAY_SIZE(ibms); i++)
1868                 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
1869                         if (strlen(val) > sizeof(ibms[i].param) - 2)
1870                                 return -ENOSPC;
1871                         strcpy(ibms[i].param, val);
1872                         strcat(ibms[i].param, ",");
1873                         return 0;
1874                 }
1875
1876         return -EINVAL;
1877 }
1878
1879 #define IBM_PARAM(feature) \
1880         module_param_call(feature, set_ibm_param, NULL, NULL, 0)
1881
1882 IBM_PARAM(hotkey);
1883 IBM_PARAM(bluetooth);
1884 IBM_PARAM(video);
1885 IBM_PARAM(light);
1886 #ifdef CONFIG_ACPI_IBM_DOCK
1887 IBM_PARAM(dock);
1888 #endif
1889 IBM_PARAM(bay);
1890 IBM_PARAM(cmos);
1891 IBM_PARAM(led);
1892 IBM_PARAM(beep);
1893 IBM_PARAM(ecdump);
1894 IBM_PARAM(brightness);
1895 IBM_PARAM(volume);
1896 IBM_PARAM(fan);
1897
1898 static void acpi_ibm_exit(void)
1899 {
1900         int i;
1901
1902         for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
1903                 ibm_exit(&ibms[i]);
1904
1905         remove_proc_entry(IBM_DIR, acpi_root_dir);
1906 }
1907
1908 static int __init acpi_ibm_init(void)
1909 {
1910         int ret, i;
1911
1912         if (acpi_disabled)
1913                 return -ENODEV;
1914
1915         if (!acpi_specific_hotkey_enabled) {
1916                 printk(IBM_ERR "using generic hotkey driver\n");
1917                 return -ENODEV;
1918         }
1919
1920         /* ec is required because many other handles are relative to it */
1921         IBM_HANDLE_INIT(ec);
1922         if (!ec_handle) {
1923                 printk(IBM_ERR "ec object not found\n");
1924                 return -ENODEV;
1925         }
1926
1927         /* these handles are not required */
1928         IBM_HANDLE_INIT(vid);
1929         IBM_HANDLE_INIT(vid2);
1930         IBM_HANDLE_INIT(ledb);
1931         IBM_HANDLE_INIT(led);
1932         IBM_HANDLE_INIT(hkey);
1933         IBM_HANDLE_INIT(lght);
1934         IBM_HANDLE_INIT(cmos);
1935 #ifdef CONFIG_ACPI_IBM_DOCK
1936         IBM_HANDLE_INIT(dock);
1937 #endif
1938         IBM_HANDLE_INIT(pci);
1939         IBM_HANDLE_INIT(bay);
1940         if (bay_handle)
1941                 IBM_HANDLE_INIT(bay_ej);
1942         IBM_HANDLE_INIT(bay2);
1943         if (bay2_handle)
1944                 IBM_HANDLE_INIT(bay2_ej);
1945         IBM_HANDLE_INIT(beep);
1946         IBM_HANDLE_INIT(ecrd);
1947         IBM_HANDLE_INIT(ecwr);
1948         IBM_HANDLE_INIT(fans);
1949         IBM_HANDLE_INIT(gfan);
1950         IBM_HANDLE_INIT(sfan);
1951
1952         proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
1953         if (!proc_dir) {
1954                 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
1955                 return -ENODEV;
1956         }
1957         proc_dir->owner = THIS_MODULE;
1958
1959         for (i = 0; i < ARRAY_SIZE(ibms); i++) {
1960                 ret = ibm_init(&ibms[i]);
1961                 if (ret >= 0 && *ibms[i].param)
1962                         ret = ibms[i].write(ibms[i].param);
1963                 if (ret < 0) {
1964                         acpi_ibm_exit();
1965                         return ret;
1966                 }
1967         }
1968
1969         return 0;
1970 }
1971
1972 module_init(acpi_ibm_init);
1973 module_exit(acpi_ibm_exit);