2 * ibm_acpi.c - IBM ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
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.
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.
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
22 #define IBM_VERSION "0.12a"
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
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
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
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>
83 #include <acpi/acpi_drivers.h>
84 #include <acpi/acnamesp.h>
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/"
91 MODULE_AUTHOR("Borislav Deianov");
92 MODULE_DESCRIPTION(IBM_DESC);
93 MODULE_VERSION(IBM_VERSION);
94 MODULE_LICENSE("GPL");
96 #define IBM_DIR IBM_NAME
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
104 #define IBM_MAX_ACPI_ARGS 3
106 #define __unused __attribute__ ((unused))
108 static int experimental;
109 module_param(experimental, int, 0);
111 static acpi_handle root_handle = NULL;
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 }
120 * The following models are supported to various degrees:
122 * 570, 600e, 600x, 770e, 770x
123 * A20m, A21e, A21m, A21p, A22p, A30, A30p, A31, A31p
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
129 * The following models have no supported features:
133 * Still missing DSDTs for the following models:
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 */
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 */
157 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
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 */
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 */
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 */
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 */
179 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
180 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
183 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
187 /* don't list other alternatives as we install a notify handler on the 570 */
188 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
190 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
191 "^HKEY", /* R30, R31 */
192 "HKEY", /* all others */
195 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
196 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
198 IBM_HANDLE(led, ec, "SLED", /* 570 */
199 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
200 "LED", /* all others */
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 */
208 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
209 "\\FSPD", /* 600e/x, 770e, 770x */
212 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
213 "JFNS", /* 770x-JL */
216 #define IBM_HKEY_HID "IBM0068"
217 #define IBM_PCI_HID "PNP0A03"
224 struct acpi_driver *driver;
227 int (*read) (char *);
228 int (*write) (char *);
231 void (*notify) (struct ibm_struct *, u32);
234 struct acpi_device *device;
236 int driver_registered;
239 int notify_installed;
244 static struct proc_dir_entry *proc_dir = NULL;
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)))
250 static int acpi_evalf(acpi_handle handle,
251 void *res, char *method, char *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;
265 printk(IBM_ERR "acpi_evalf() called with empty format\n");
278 params.pointer = &in_objs[0];
285 in_objs[params.count].integer.value = va_arg(ap, int);
286 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
288 /* add more types as needed */
290 printk(IBM_ERR "acpi_evalf() called "
291 "with invalid format character '%c'\n", c);
297 if (res_type != 'v') {
298 result.length = sizeof(out_obj);
299 result.pointer = &out_obj;
304 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
309 *(int *)res = out_obj.integer.value;
310 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
313 success = status == AE_OK;
315 /* add more types as needed */
317 printk(IBM_ERR "acpi_evalf() called "
318 "with invalid format character '%c'\n", res_type);
322 if (!success && !quiet)
323 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
324 method, fmt0, status);
329 static void __unused acpi_print_int(acpi_handle handle, char *method)
333 if (acpi_evalf(handle, &i, method, "d"))
334 printk(IBM_INFO "%s = 0x%x\n", method, i);
336 printk(IBM_ERR "error calling %s\n", method);
339 static char *next_cmd(char **cmds)
344 while ((end = strchr(start, ',')) && end == start)
355 static int driver_init(void)
357 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
358 printk(IBM_INFO "%s\n", IBM_URL);
363 static int driver_read(char *p)
367 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
368 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
373 static int hotkey_supported;
374 static int hotkey_mask_supported;
375 static int hotkey_orig_status;
376 static int hotkey_orig_mask;
378 static int hotkey_get(int *status, int *mask)
380 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
383 if (hotkey_mask_supported)
384 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
390 static int hotkey_set(int status, int mask)
394 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
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))
408 static int hotkey_init(void)
410 /* hotkey not supported on 570 */
411 hotkey_supported = hkey_handle != NULL;
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");
419 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
426 static int hotkey_read(char *p)
431 if (!hotkey_supported) {
432 len += sprintf(p + len, "status:\t\tnot supported\n");
436 if (!hotkey_get(&status, &mask))
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");
445 len += sprintf(p + len, "mask:\t\tnot supported\n");
446 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
452 static int hotkey_write(char *buf)
458 if (!hotkey_supported)
461 if (!hotkey_get(&status, &mask))
464 while ((cmd = next_cmd(&buf))) {
465 if (strlencmp(cmd, "enable") == 0) {
467 } else if (strlencmp(cmd, "disable") == 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) {
474 } else if (sscanf(cmd, "%x", &mask) == 1) {
481 if (do_cmd && !hotkey_set(status, mask))
487 static void hotkey_exit(void)
489 if (hotkey_supported)
490 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
493 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
497 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
498 acpi_bus_generate_event(ibm->device, event, hkey);
500 printk(IBM_ERR "unknown hotkey event %d\n", event);
501 acpi_bus_generate_event(ibm->device, event, 0);
505 static int bluetooth_supported;
507 static int bluetooth_init(void)
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");
517 static int bluetooth_status(void)
521 if (!bluetooth_supported ||
522 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
528 static int bluetooth_read(char *p)
531 int status = bluetooth_status();
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");
538 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
539 len += sprintf(p + len, "commands:\tenable, disable\n");
545 static int bluetooth_write(char *buf)
547 int status = bluetooth_status();
551 if (!bluetooth_supported)
554 while ((cmd = next_cmd(&buf))) {
555 if (strlencmp(cmd, "enable") == 0) {
557 } else if (strlencmp(cmd, "disable") == 0) {
564 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
570 static int video_supported;
571 static int video_orig_autosw;
577 static int video_init(void)
581 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
582 /* G41, assume IVGA doesn't change */
583 vid_handle = vid2_handle;
586 /* video switching not supported on R30, R31 */
588 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
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;
596 video_supported = VIDEO_NEW;
601 static int video_status(void)
606 if (video_supported == VIDEO_570) {
607 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
609 } else if (video_supported == VIDEO_770) {
610 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
612 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
614 } else if (video_supported == VIDEO_NEW) {
615 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
616 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
619 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
620 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
622 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
629 static int video_autosw(void)
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");
641 static int video_read(char *p)
643 int status = video_status();
644 int autosw = video_autosw();
647 if (!video_supported) {
648 len += sprintf(p + len, "status:\t\tnot supported\n");
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");
668 static int video_switch(void)
670 int autosw = video_autosw();
673 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
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);
683 static int video_expand(void)
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");
690 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
693 static int video_switch2(int status)
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))
705 ret = acpi_evalf(vid_handle, NULL,
706 "ASWT", "vdd", status * 0x100, 0);
708 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
710 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
711 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
717 static int video_write(char *buf)
720 int enable, disable, status;
722 if (!video_supported)
725 enable = disable = 0;
727 while ((cmd = next_cmd(&buf))) {
728 if (strlencmp(cmd, "lcd_enable") == 0) {
730 } else if (strlencmp(cmd, "lcd_disable") == 0) {
732 } else if (strlencmp(cmd, "crt_enable") == 0) {
734 } else if (strlencmp(cmd, "crt_disable") == 0) {
736 } else if (video_supported == VIDEO_NEW &&
737 strlencmp(cmd, "dvi_enable") == 0) {
739 } else if (video_supported == VIDEO_NEW &&
740 strlencmp(cmd, "dvi_disable") == 0) {
742 } else if (strlencmp(cmd, "auto_enable") == 0) {
743 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
745 } else if (strlencmp(cmd, "auto_disable") == 0) {
746 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
748 } else if (strlencmp(cmd, "video_switch") == 0) {
751 } else if (strlencmp(cmd, "expand_toggle") == 0) {
758 if (enable || disable) {
759 status = (video_status() & 0x0f & ~disable) | enable;
760 if (!video_switch2(status))
767 static void video_exit(void)
769 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
772 static int light_supported;
773 static int light_status_supported;
775 static int light_init(void)
777 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
778 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
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,
789 static int light_read(char *p)
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");
800 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
802 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
803 len += sprintf(p + len, "commands:\ton, off\n");
809 static int light_write(char *buf)
811 int cmos_cmd, lght_cmd;
815 if (!light_supported)
818 while ((cmd = next_cmd(&buf))) {
819 if (strlencmp(cmd, "on") == 0) {
822 } else if (strlencmp(cmd, "off") == 0) {
828 success = cmos_handle ?
829 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
830 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
838 static int _sta(acpi_handle handle)
842 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
848 #define dock_docked() (_sta(dock_handle) & 1)
850 static int dock_read(char *p)
853 int docked = dock_docked();
856 len += sprintf(p + len, "status:\t\tnot supported\n");
858 len += sprintf(p + len, "status:\t\tundocked\n");
860 len += sprintf(p + len, "status:\t\tdocked\n");
861 len += sprintf(p + len, "commands:\tdock, undock\n");
867 static int dock_write(char *buf)
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))
879 } else if (strlencmp(cmd, "dock") == 0) {
880 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
889 static void dock_notify(struct ibm_struct *ibm, u32 event)
891 int docked = dock_docked();
892 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
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 */
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 */
911 static int bay_status_supported;
912 static int bay_status2_supported;
913 static int bay_eject_supported;
914 static int bay_eject2_supported;
916 static int bay_init(void)
918 bay_status_supported = bay_handle &&
919 acpi_evalf(bay_handle, NULL, "_STA", "qv");
920 bay_status2_supported = bay2_handle &&
921 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
923 bay_eject_supported = bay_handle && bay_ej_handle &&
924 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
925 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
926 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
931 #define bay_occupied(b) (_sta(b##_handle) & 1)
933 static int bay_read(char *p)
936 int occupied = bay_occupied(bay);
937 int occupied2 = bay_occupied(bay2);
940 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
941 (occupied ? "occupied" : "unoccupied") :
943 if (bay_status2_supported)
944 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
945 "occupied" : "unoccupied");
947 eject = bay_eject_supported && occupied;
948 eject2 = bay_eject2_supported && occupied2;
951 len += sprintf(p + len, "commands:\teject, eject2\n");
953 len += sprintf(p + len, "commands:\teject\n");
955 len += sprintf(p + len, "commands:\teject2\n");
960 static int bay_write(char *buf)
964 if (!bay_eject_supported && !bay_eject2_supported)
967 while ((cmd = next_cmd(&buf))) {
968 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
969 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
971 } else if (bay_eject2_supported &&
972 strlencmp(cmd, "eject2") == 0) {
973 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
982 static void bay_notify(struct ibm_struct *ibm, u32 event)
984 acpi_bus_generate_event(ibm->device, event, 0);
987 static int cmos_read(char *p)
991 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
992 R30, R31, T20-22, X20-21 */
994 len += sprintf(p + len, "status:\t\tnot supported\n");
996 len += sprintf(p + len, "status:\t\tsupported\n");
997 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1003 static int cmos_eval(int cmos_cmd)
1006 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1011 static int cmos_write(char *buf)
1019 while ((cmd = next_cmd(&buf))) {
1020 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1021 cmos_cmd >= 0 && cmos_cmd <= 21) {
1026 if (!cmos_eval(cmos_cmd))
1033 static int led_supported;
1039 static int led_init(void)
1042 /* led not supported on R30, R31 */
1044 else if (strlencmp(led_path, "SLED") == 0)
1046 led_supported = LED_570;
1047 else if (strlencmp(led_path, "SYSL") == 0)
1048 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1049 led_supported = LED_OLD;
1052 led_supported = LED_NEW;
1057 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1059 static int led_read(char *p)
1063 if (!led_supported) {
1064 len += sprintf(p + len, "status:\t\tnot supported\n");
1067 len += sprintf(p + len, "status:\t\tsupported\n");
1069 if (led_supported == LED_570) {
1072 for (i = 0; i < 8; i++) {
1073 if (!acpi_evalf(ec_handle,
1074 &status, "GLED", "dd", 1 << i))
1076 len += sprintf(p + len, "%d:\t\t%s\n",
1077 i, led_status(status));
1081 len += sprintf(p + len, "commands:\t"
1082 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1087 /* off, on, blink */
1088 static const int led_sled_arg1[] = { 0, 1, 3 };
1089 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1090 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1091 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1093 #define EC_HLCL 0x0c
1094 #define EC_HLBL 0x0d
1095 #define EC_HLMS 0x0e
1097 static int led_write(char *buf)
1105 while ((cmd = next_cmd(&buf))) {
1106 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1109 if (strstr(cmd, "off")) {
1111 } else if (strstr(cmd, "on")) {
1113 } else if (strstr(cmd, "blink")) {
1118 if (led_supported == LED_570) {
1121 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1122 led, led_sled_arg1[ind]))
1124 } else if (led_supported == LED_OLD) {
1125 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1127 ret = ec_write(EC_HLMS, led);
1130 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1133 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1138 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1139 led, led_led_arg1[ind]))
1147 static int beep_read(char *p)
1152 len += sprintf(p + len, "status:\t\tnot supported\n");
1154 len += sprintf(p + len, "status:\t\tsupported\n");
1155 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1161 static int beep_write(char *buf)
1169 while ((cmd = next_cmd(&buf))) {
1170 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1171 beep_cmd >= 0 && beep_cmd <= 17) {
1175 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1182 static int acpi_ec_read(int i, u8 * p)
1187 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1191 if (ec_read(i, p) < 0)
1198 static int acpi_ec_write(int i, u8 v)
1201 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1204 if (ec_write(i, v) < 0)
1211 static int thermal_tmp_supported;
1212 static int thermal_updt_supported;
1214 static int thermal_init(void)
1216 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1217 thermal_tmp_supported = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1219 /* 600e/x, 770e, 770x */
1220 thermal_updt_supported = acpi_evalf(ec_handle, NULL, "UPDT", "qv");
1225 static int thermal_read(char *p)
1229 if (!thermal_tmp_supported)
1230 len += sprintf(p + len, "temperatures:\tnot supported\n");
1233 char tmpi[] = "TMPi";
1236 if (thermal_updt_supported)
1237 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1240 for (i = 0; i < 8; i++) {
1242 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1244 if (thermal_updt_supported)
1245 tmp[i] = (t - 2732 + 5) / 10;
1250 len += sprintf(p + len,
1251 "temperatures:\t%d %d %d %d %d %d %d %d\n",
1252 tmp[0], tmp[1], tmp[2], tmp[3],
1253 tmp[4], tmp[5], tmp[6], tmp[7]);
1259 static u8 ecdump_regs[256];
1261 static int ecdump_read(char *p)
1267 len += sprintf(p + len, "EC "
1268 " +00 +01 +02 +03 +04 +05 +06 +07"
1269 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1270 for (i = 0; i < 256; i += 16) {
1271 len += sprintf(p + len, "EC 0x%02x:", i);
1272 for (j = 0; j < 16; j++) {
1273 if (!acpi_ec_read(i + j, &v))
1275 if (v != ecdump_regs[i + j])
1276 len += sprintf(p + len, " *%02x", v);
1278 len += sprintf(p + len, " %02x", v);
1279 ecdump_regs[i + j] = v;
1281 len += sprintf(p + len, "\n");
1286 /* These are way too dangerous to advertise openly... */
1288 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1289 " (<offset> is 00-ff, <value> is 00-ff)\n");
1290 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1291 " (<offset> is 00-ff, <value> is 0-255)\n");
1296 static int ecdump_write(char *buf)
1301 while ((cmd = next_cmd(&buf))) {
1302 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1304 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1308 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1309 if (!acpi_ec_write(i, v))
1318 static int brightness_offset = 0x31;
1320 static int brightness_read(char *p)
1325 if (!acpi_ec_read(brightness_offset, &level)) {
1326 len += sprintf(p + len, "level:\t\tunreadable\n");
1328 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1329 len += sprintf(p + len, "commands:\tup, down\n");
1330 len += sprintf(p + len, "commands:\tlevel <level>"
1331 " (<level> is 0-7)\n");
1337 #define BRIGHTNESS_UP 4
1338 #define BRIGHTNESS_DOWN 5
1340 static int brightness_write(char *buf)
1342 int cmos_cmd, inc, i;
1347 while ((cmd = next_cmd(&buf))) {
1348 if (!acpi_ec_read(brightness_offset, &level))
1352 if (strlencmp(cmd, "up") == 0) {
1353 new_level = level == 7 ? 7 : level + 1;
1354 } else if (strlencmp(cmd, "down") == 0) {
1355 new_level = level == 0 ? 0 : level - 1;
1356 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1357 new_level >= 0 && new_level <= 7) {
1362 cmos_cmd = new_level > level ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1363 inc = new_level > level ? 1 : -1;
1364 for (i = level; i != new_level; i += inc) {
1365 if (!cmos_eval(cmos_cmd))
1367 if (!acpi_ec_write(brightness_offset, i + inc))
1375 static int volume_offset = 0x30;
1377 static int volume_read(char *p)
1382 if (!acpi_ec_read(volume_offset, &level)) {
1383 len += sprintf(p + len, "level:\t\tunreadable\n");
1385 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1386 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1387 len += sprintf(p + len, "commands:\tup, down, mute\n");
1388 len += sprintf(p + len, "commands:\tlevel <level>"
1389 " (<level> is 0-15)\n");
1395 #define VOLUME_DOWN 0
1397 #define VOLUME_MUTE 2
1399 static int volume_write(char *buf)
1401 int cmos_cmd, inc, i;
1403 int new_level, new_mute;
1406 while ((cmd = next_cmd(&buf))) {
1407 if (!acpi_ec_read(volume_offset, &level))
1409 new_mute = mute = level & 0x40;
1410 new_level = level = level & 0xf;
1412 if (strlencmp(cmd, "up") == 0) {
1416 new_level = level == 15 ? 15 : level + 1;
1417 } else if (strlencmp(cmd, "down") == 0) {
1421 new_level = level == 0 ? 0 : level - 1;
1422 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1423 new_level >= 0 && new_level <= 15) {
1425 } else if (strlencmp(cmd, "mute") == 0) {
1430 if (new_level != level) { /* mute doesn't change */
1431 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1432 inc = new_level > level ? 1 : -1;
1434 if (mute && (!cmos_eval(cmos_cmd) ||
1435 !acpi_ec_write(volume_offset, level)))
1438 for (i = level; i != new_level; i += inc)
1439 if (!cmos_eval(cmos_cmd) ||
1440 !acpi_ec_write(volume_offset, i + inc))
1443 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1444 !acpi_ec_write(volume_offset,
1449 if (new_mute != mute) { /* level doesn't change */
1450 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1452 if (!cmos_eval(cmos_cmd) ||
1453 !acpi_ec_write(volume_offset, level + new_mute))
1461 static int fan_status_offset = 0x2f;
1462 static int fan_rpm_offset = 0x84;
1464 static int fan_read(char *p)
1471 /* 570, 600e/x, 770e, 770x */
1472 if (!acpi_evalf(gfan_handle, &s, NULL, "d"))
1475 len += sprintf(p + len, "level:\t\t%d\n", s);
1477 /* all except 570, 600e/x, 770e, 770x */
1478 if (!acpi_ec_read(fan_status_offset, &status))
1479 len += sprintf(p + len, "status:\t\tunreadable\n");
1481 len += sprintf(p + len, "status:\t\t%s\n",
1482 enabled(status, 7));
1484 if (!acpi_ec_read(fan_rpm_offset, &lo) ||
1485 !acpi_ec_read(fan_rpm_offset + 1, &hi))
1486 len += sprintf(p + len, "speed:\t\tunreadable\n");
1488 len += sprintf(p + len, "speed:\t\t%d\n",
1494 len += sprintf(p + len, "commands:\tlevel <level>"
1495 " (<level> is 0-7)\n");
1497 /* all except 570, 600e/x, 770e, 770x */
1498 len += sprintf(p + len, "commands:\tenable, disable\n");
1501 len += sprintf(p + len, "commands:\tspeed <speed>"
1502 " (<speed> is 0-65535)\n");
1507 static int fan_write(char *buf)
1512 while ((cmd = next_cmd(&buf))) {
1514 sscanf(cmd, "level %d", &level) == 1 &&
1515 level >= 0 && level <= 7) {
1517 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
1519 } else if (!gfan_handle && strlencmp(cmd, "enable") == 0) {
1520 /* all except 570, 600e/x, 770e, 770x */
1521 if (!acpi_ec_write(fan_status_offset, 0x80))
1523 } else if (!gfan_handle && strlencmp(cmd, "disable") == 0) {
1524 /* all except 570, 600e/x, 770e, 770x */
1525 if (!acpi_ec_write(fan_status_offset, 0x00))
1527 } else if (fans_handle &&
1528 sscanf(cmd, "speed %d", &speed) == 1 &&
1529 speed >= 0 && speed <= 65535) {
1531 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
1532 speed, speed, speed))
1541 static struct ibm_struct ibms[] = {
1544 .init = driver_init,
1545 .read = driver_read,
1549 .hid = IBM_HKEY_HID,
1550 .init = hotkey_init,
1551 .read = hotkey_read,
1552 .write = hotkey_write,
1553 .exit = hotkey_exit,
1554 .notify = hotkey_notify,
1555 .handle = &hkey_handle,
1556 .type = ACPI_DEVICE_NOTIFY,
1559 .name = "bluetooth",
1560 .init = bluetooth_init,
1561 .read = bluetooth_read,
1562 .write = bluetooth_write,
1568 .write = video_write,
1575 .write = light_write,
1580 .write = dock_write,
1581 .notify = dock_notify,
1582 .handle = &dock_handle,
1583 .type = ACPI_SYSTEM_NOTIFY,
1588 .notify = dock_notify,
1589 .handle = &pci_handle,
1590 .type = ACPI_SYSTEM_NOTIFY,
1597 .notify = bay_notify,
1598 .handle = &bay_handle,
1599 .type = ACPI_SYSTEM_NOTIFY,
1604 .write = cmos_write,
1615 .write = beep_write,
1619 .init = thermal_init,
1620 .read = thermal_read,
1624 .read = ecdump_read,
1625 .write = ecdump_write,
1629 .name = "brightness",
1630 .read = brightness_read,
1631 .write = brightness_write,
1636 .read = volume_read,
1637 .write = volume_write,
1648 static int dispatch_read(char *page, char **start, off_t off, int count,
1649 int *eof, void *data)
1651 struct ibm_struct *ibm = (struct ibm_struct *)data;
1654 if (!ibm || !ibm->read)
1657 len = ibm->read(page);
1661 if (len <= off + count)
1663 *start = page + off;
1673 static int dispatch_write(struct file *file, const char __user * userbuf,
1674 unsigned long count, void *data)
1676 struct ibm_struct *ibm = (struct ibm_struct *)data;
1680 if (!ibm || !ibm->write)
1683 kernbuf = kmalloc(count + 2, GFP_KERNEL);
1687 if (copy_from_user(kernbuf, userbuf, count)) {
1693 strcat(kernbuf, ",");
1694 ret = ibm->write(kernbuf);
1703 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
1705 struct ibm_struct *ibm = (struct ibm_struct *)data;
1707 if (!ibm || !ibm->notify)
1710 ibm->notify(ibm, event);
1713 static int __init setup_notify(struct ibm_struct *ibm)
1721 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
1723 printk(IBM_ERR "%s device not present\n", ibm->name);
1727 acpi_driver_data(ibm->device) = ibm;
1728 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
1730 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
1731 dispatch_notify, ibm);
1732 if (ACPI_FAILURE(status)) {
1733 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
1741 static int __init ibm_device_add(struct acpi_device *device)
1746 static int __init register_driver(struct ibm_struct *ibm)
1750 ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
1752 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
1756 memset(ibm->driver, 0, sizeof(struct acpi_driver));
1757 sprintf(ibm->driver->name, "%s/%s", IBM_NAME, ibm->name);
1758 ibm->driver->ids = ibm->hid;
1759 ibm->driver->ops.add = &ibm_device_add;
1761 ret = acpi_bus_register_driver(ibm->driver);
1763 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
1771 static int __init ibm_init(struct ibm_struct *ibm)
1774 struct proc_dir_entry *entry;
1776 if (ibm->experimental && !experimental)
1780 ret = register_driver(ibm);
1783 ibm->driver_registered = 1;
1790 ibm->init_called = 1;
1794 entry = create_proc_entry(ibm->name,
1795 S_IFREG | S_IRUGO | S_IWUSR,
1798 printk(IBM_ERR "unable to create proc entry %s\n",
1802 entry->owner = THIS_MODULE;
1804 entry->read_proc = &dispatch_read;
1806 entry->write_proc = &dispatch_write;
1807 ibm->proc_created = 1;
1811 ret = setup_notify(ibm);
1814 ibm->notify_installed = 1;
1820 static void ibm_exit(struct ibm_struct *ibm)
1822 if (ibm->notify_installed)
1823 acpi_remove_notify_handler(*ibm->handle, ibm->type,
1826 if (ibm->proc_created)
1827 remove_proc_entry(ibm->name, proc_dir);
1829 if (ibm->init_called && ibm->exit)
1832 if (ibm->driver_registered) {
1833 acpi_bus_unregister_driver(ibm->driver);
1838 static void __init ibm_handle_init(char *name,
1839 acpi_handle * handle, acpi_handle parent,
1840 char **paths, int num_paths, char **path)
1845 for (i = 0; i < num_paths; i++) {
1846 status = acpi_get_handle(parent, paths[i], handle);
1847 if (ACPI_SUCCESS(status)) {
1856 #define IBM_HANDLE_INIT(object) \
1857 ibm_handle_init(#object, &object##_handle, *object##_parent, \
1858 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
1860 static int set_ibm_param(const char *val, struct kernel_param *kp)
1864 for (i = 0; i < ARRAY_SIZE(ibms); i++)
1865 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
1866 if (strlen(val) > sizeof(ibms[i].param) - 2)
1868 strcpy(ibms[i].param, val);
1869 strcat(ibms[i].param, ",");
1876 #define IBM_PARAM(feature) \
1877 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
1880 IBM_PARAM(bluetooth);
1889 IBM_PARAM(brightness);
1893 static void acpi_ibm_exit(void)
1897 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
1900 remove_proc_entry(IBM_DIR, acpi_root_dir);
1903 static int __init acpi_ibm_init(void)
1910 if (!acpi_specific_hotkey_enabled) {
1911 printk(IBM_ERR "using generic hotkey driver\n");
1915 /* ec is required because many other handles are relative to it */
1916 IBM_HANDLE_INIT(ec);
1918 printk(IBM_ERR "ec object not found\n");
1922 /* these handles are not required */
1923 IBM_HANDLE_INIT(vid);
1924 IBM_HANDLE_INIT(vid2);
1925 IBM_HANDLE_INIT(ledb);
1926 IBM_HANDLE_INIT(led);
1927 IBM_HANDLE_INIT(hkey);
1928 IBM_HANDLE_INIT(lght);
1929 IBM_HANDLE_INIT(cmos);
1930 IBM_HANDLE_INIT(dock);
1931 IBM_HANDLE_INIT(pci);
1932 IBM_HANDLE_INIT(bay);
1934 IBM_HANDLE_INIT(bay_ej);
1935 IBM_HANDLE_INIT(bay2);
1937 IBM_HANDLE_INIT(bay2_ej);
1938 IBM_HANDLE_INIT(beep);
1939 IBM_HANDLE_INIT(ecrd);
1940 IBM_HANDLE_INIT(ecwr);
1941 IBM_HANDLE_INIT(fans);
1942 IBM_HANDLE_INIT(gfan);
1943 IBM_HANDLE_INIT(sfan);
1945 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
1947 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
1950 proc_dir->owner = THIS_MODULE;
1952 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
1953 ret = ibm_init(&ibms[i]);
1954 if (ret >= 0 && *ibms[i].param)
1955 ret = ibms[i].write(ibms[i].param);
1965 module_init(acpi_ibm_init);
1966 module_exit(acpi_ibm_exit);