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 <linux/backlight.h>
82 #include <asm/uaccess.h>
83 #include <linux/dmi.h>
85 #include <acpi/acpi_drivers.h>
86 #include <acpi/acnamesp.h>
88 #define IBM_NAME "ibm"
89 #define IBM_DESC "IBM ThinkPad ACPI Extras"
90 #define IBM_FILE "ibm_acpi"
91 #define IBM_URL "http://ibm-acpi.sf.net/"
93 MODULE_AUTHOR("Borislav Deianov");
94 MODULE_DESCRIPTION(IBM_DESC);
95 MODULE_VERSION(IBM_VERSION);
96 MODULE_LICENSE("GPL");
98 #define IBM_DIR IBM_NAME
100 #define IBM_LOG IBM_FILE ": "
101 #define IBM_ERR KERN_ERR IBM_LOG
102 #define IBM_NOTICE KERN_NOTICE IBM_LOG
103 #define IBM_INFO KERN_INFO IBM_LOG
104 #define IBM_DEBUG KERN_DEBUG IBM_LOG
106 #define IBM_MAX_ACPI_ARGS 3
108 #define __unused __attribute__ ((unused))
110 static int experimental;
111 module_param(experimental, int, 0);
113 static acpi_handle root_handle = NULL;
115 #define IBM_HANDLE(object, parent, paths...) \
116 static acpi_handle object##_handle; \
117 static acpi_handle *object##_parent = &parent##_handle; \
118 static char *object##_path; \
119 static char *object##_paths[] = { paths }
122 * The following models are supported to various degrees:
124 * 570, 600e, 600x, 770e, 770x
125 * A20m, A21e, A21m, A21p, A22p, A30, A30p, A31, A31p
127 * R30, R31, R32, R40, R40e, R50, R50e, R50p, R51
128 * T20, T21, T22, T23, T30, T40, T40p, T41, T41p, T42, T42p, T43
129 * X20, X21, X22, X23, X24, X30, X31, X40
131 * The following models have no supported features:
135 * Still missing DSDTs for the following models:
143 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
144 "\\_SB.PCI.ISA.EC", /* 570 */
145 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
146 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
147 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
148 "\\_SB.PCI0.ICH3.EC0", /* R31 */
149 "\\_SB.PCI0.LPC.EC", /* all others */
152 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
153 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
154 "\\_SB.PCI0.VID0", /* 770e */
155 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
156 "\\_SB.PCI0.AGP.VID", /* all others */
159 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
161 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
162 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
163 "\\CMS", /* R40, R40e */
165 #ifdef CONFIG_ACPI_IBM_DOCK
166 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
167 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
168 "\\_SB.PCI0.PCI1.DOCK", /* all others */
169 "\\_SB.PCI.ISA.SLCE", /* 570 */
170 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
172 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
173 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
174 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
175 ); /* A21e, R30, R31 */
177 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
178 "_EJ0", /* all others */
179 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
181 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
182 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
185 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
189 /* don't list other alternatives as we install a notify handler on the 570 */
190 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
192 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
193 "^HKEY", /* R30, R31 */
194 "HKEY", /* all others */
197 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
198 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
200 IBM_HANDLE(led, ec, "SLED", /* 570 */
201 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
202 "LED", /* all others */
205 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
206 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
207 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
208 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
210 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
211 "\\FSPD", /* 600e/x, 770e, 770x */
214 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
215 "JFNS", /* 770x-JL */
218 #define IBM_HKEY_HID "IBM0068"
219 #define IBM_PCI_HID "PNP0A03"
221 enum thermal_access_mode {
222 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
223 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
224 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
225 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
226 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
229 #define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
230 struct ibm_thermal_sensors_struct {
231 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
237 * IBMACPI_FAN_RD_ACPI_GFAN:
238 * ACPI GFAN method: returns fan level
240 * see IBMACPI_FAN_WR_ACPI_SFAN
241 * EC 0x2f not available if GFAN exists
243 * IBMACPI_FAN_WR_ACPI_SFAN:
244 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
246 * EC 0x2f might be available *for reading*, but never for writing.
248 * IBMACPI_FAN_WR_TPEC:
249 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
250 * on almost all ThinkPads
252 * Fan speed changes of any sort (including those caused by the
253 * disengaged mode) are usually done slowly by the firmware as the
254 * maximum ammount of fan duty cycle change per second seems to be
257 * Reading is not available if GFAN exists.
258 * Writing is not available if SFAN exists.
261 * 7 automatic mode engaged;
262 * (default operation mode of the ThinkPad)
263 * fan level is ignored in this mode.
264 * 6 disengage mode (takes precedence over bit 7);
265 * not available on all thinkpads. May disable
266 * the tachometer, and speeds up fan to 100% duty-cycle,
267 * which speeds it up far above the standard RPM
268 * levels. It is not impossible that it could cause
270 * 5-3 unused in some models. Extra bits for fan level
271 * in others, but still useless as all values above
272 * 7 map to the same speed as level 7 in these models.
273 * 2-0 fan level (0..7 usually)
275 * 0x07 = max (set when temperatures critical)
276 * Some ThinkPads may have other levels, see
277 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
279 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
280 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
281 * does so, its initial value is meaningless (0x07).
283 * For firmware bugs, refer to:
284 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
288 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
289 * Main fan tachometer reading (in RPM)
291 * This register is present on all ThinkPads with a new-style EC, and
292 * it is known not to be present on the A21m/e, and T22, as there is
293 * something else in offset 0x84 according to the ACPI DSDT. Other
294 * ThinkPads from this same time period (and earlier) probably lack the
295 * tachometer as well.
297 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
298 * was never fixed by IBM to report the EC firmware version string
299 * probably support the tachometer (like the early X models), so
300 * detecting it is quite hard. We need more data to know for sure.
302 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
305 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
306 * register is not invalidated in ThinkPads that disable tachometer
307 * readings. Thus, the tachometer readings go stale.
309 * For firmware bugs, refer to:
310 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
312 * IBMACPI_FAN_WR_ACPI_FANS:
313 * ThinkPad X31, X40, X41. Not available in the X60.
315 * FANS ACPI handle: takes three arguments: low speed, medium speed,
316 * high speed. ACPI DSDT seems to map these three speeds to levels
317 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
318 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
320 * The speeds are stored on handles
321 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
323 * There are three default speed sets, acessible as handles:
324 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
326 * ACPI DSDT switches which set is in use depending on various
329 * IBMACPI_FAN_WR_TPEC is also available and should be used to
330 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
331 * but the ACPI tables just mention level 7.
334 enum fan_status_access_mode {
335 IBMACPI_FAN_NONE = 0, /* No fan status or control */
336 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
337 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
340 enum fan_control_access_mode {
341 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
342 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
343 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
344 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
347 enum fan_control_commands {
348 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
349 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
350 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd */
353 enum { /* Fan control constants */
354 fan_status_offset = 0x2f, /* EC register 0x2f */
355 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
356 * 0x84 must be read before 0x85 */
359 static int ibm_thinkpad_ec_found;
366 struct acpi_driver *driver;
369 int (*read) (char *);
370 int (*write) (char *);
373 void (*notify) (struct ibm_struct *, u32);
376 struct acpi_device *device;
378 int driver_registered;
381 int notify_installed;
386 static struct proc_dir_entry *proc_dir = NULL;
388 static struct backlight_device *ibm_backlight_device;
390 #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
391 #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
392 #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
394 static int acpi_evalf(acpi_handle handle,
395 void *res, char *method, char *fmt, ...)
398 struct acpi_object_list params;
399 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
400 struct acpi_buffer result, *resultp;
401 union acpi_object out_obj;
409 printk(IBM_ERR "acpi_evalf() called with empty format\n");
422 params.pointer = &in_objs[0];
429 in_objs[params.count].integer.value = va_arg(ap, int);
430 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
432 /* add more types as needed */
434 printk(IBM_ERR "acpi_evalf() called "
435 "with invalid format character '%c'\n", c);
441 if (res_type != 'v') {
442 result.length = sizeof(out_obj);
443 result.pointer = &out_obj;
448 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
453 *(int *)res = out_obj.integer.value;
454 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
457 success = status == AE_OK;
459 /* add more types as needed */
461 printk(IBM_ERR "acpi_evalf() called "
462 "with invalid format character '%c'\n", res_type);
466 if (!success && !quiet)
467 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
468 method, fmt0, status);
473 static void __unused acpi_print_int(acpi_handle handle, char *method)
477 if (acpi_evalf(handle, &i, method, "d"))
478 printk(IBM_INFO "%s = 0x%x\n", method, i);
480 printk(IBM_ERR "error calling %s\n", method);
483 static char *next_cmd(char **cmds)
488 while ((end = strchr(start, ',')) && end == start)
499 static int driver_init(void)
501 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
502 printk(IBM_INFO "%s\n", IBM_URL);
507 static int driver_read(char *p)
511 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
512 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
517 static int hotkey_supported;
518 static int hotkey_mask_supported;
519 static int hotkey_orig_status;
520 static int hotkey_orig_mask;
522 static int hotkey_get(int *status, int *mask)
524 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
527 if (hotkey_mask_supported)
528 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
534 static int hotkey_set(int status, int mask)
538 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
541 if (hotkey_mask_supported)
542 for (i = 0; i < 32; i++) {
543 int bit = ((1 << i) & mask) != 0;
544 if (!acpi_evalf(hkey_handle,
545 NULL, "MHKM", "vdd", i + 1, bit))
552 static int hotkey_init(void)
554 /* hotkey not supported on 570 */
555 hotkey_supported = hkey_handle != NULL;
557 if (hotkey_supported) {
558 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
559 A30, R30, R31, T20-22, X20-21, X22-24 */
560 hotkey_mask_supported =
561 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
563 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
570 static int hotkey_read(char *p)
575 if (!hotkey_supported) {
576 len += sprintf(p + len, "status:\t\tnot supported\n");
580 if (!hotkey_get(&status, &mask))
583 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
584 if (hotkey_mask_supported) {
585 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
586 len += sprintf(p + len,
587 "commands:\tenable, disable, reset, <mask>\n");
589 len += sprintf(p + len, "mask:\t\tnot supported\n");
590 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
596 static int hotkey_write(char *buf)
602 if (!hotkey_supported)
605 if (!hotkey_get(&status, &mask))
608 while ((cmd = next_cmd(&buf))) {
609 if (strlencmp(cmd, "enable") == 0) {
611 } else if (strlencmp(cmd, "disable") == 0) {
613 } else if (strlencmp(cmd, "reset") == 0) {
614 status = hotkey_orig_status;
615 mask = hotkey_orig_mask;
616 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
618 } else if (sscanf(cmd, "%x", &mask) == 1) {
625 if (do_cmd && !hotkey_set(status, mask))
631 static void hotkey_exit(void)
633 if (hotkey_supported)
634 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
637 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
641 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
642 acpi_bus_generate_event(ibm->device, event, hkey);
644 printk(IBM_ERR "unknown hotkey event %d\n", event);
645 acpi_bus_generate_event(ibm->device, event, 0);
649 static int bluetooth_supported;
651 static int bluetooth_init(void)
653 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
654 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
655 bluetooth_supported = hkey_handle &&
656 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
661 static int bluetooth_status(void)
665 if (!bluetooth_supported ||
666 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
672 static int bluetooth_read(char *p)
675 int status = bluetooth_status();
677 if (!bluetooth_supported)
678 len += sprintf(p + len, "status:\t\tnot supported\n");
679 else if (!(status & 1))
680 len += sprintf(p + len, "status:\t\tnot installed\n");
682 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
683 len += sprintf(p + len, "commands:\tenable, disable\n");
689 static int bluetooth_write(char *buf)
691 int status = bluetooth_status();
695 if (!bluetooth_supported)
698 while ((cmd = next_cmd(&buf))) {
699 if (strlencmp(cmd, "enable") == 0) {
701 } else if (strlencmp(cmd, "disable") == 0) {
708 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
714 static int wan_supported;
716 static int wan_init(void)
718 wan_supported = hkey_handle &&
719 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
724 static int wan_status(void)
728 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
734 static int wan_read(char *p)
737 int status = wan_status();
740 len += sprintf(p + len, "status:\t\tnot supported\n");
741 else if (!(status & 1))
742 len += sprintf(p + len, "status:\t\tnot installed\n");
744 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
745 len += sprintf(p + len, "commands:\tenable, disable\n");
751 static int wan_write(char *buf)
753 int status = wan_status();
760 while ((cmd = next_cmd(&buf))) {
761 if (strlencmp(cmd, "enable") == 0) {
763 } else if (strlencmp(cmd, "disable") == 0) {
770 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
776 static int video_supported;
777 static int video_orig_autosw;
783 static int video_init(void)
787 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
788 /* G41, assume IVGA doesn't change */
789 vid_handle = vid2_handle;
792 /* video switching not supported on R30, R31 */
794 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
796 video_supported = VIDEO_570;
797 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
798 /* 600e/x, 770e, 770x */
799 video_supported = VIDEO_770;
802 video_supported = VIDEO_NEW;
807 static int video_status(void)
812 if (video_supported == VIDEO_570) {
813 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
815 } else if (video_supported == VIDEO_770) {
816 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
818 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
820 } else if (video_supported == VIDEO_NEW) {
821 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
822 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
825 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
826 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
828 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
835 static int video_autosw(void)
839 if (video_supported == VIDEO_570)
840 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
841 else if (video_supported == VIDEO_770 || video_supported == VIDEO_NEW)
842 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
847 static int video_read(char *p)
849 int status = video_status();
850 int autosw = video_autosw();
853 if (!video_supported) {
854 len += sprintf(p + len, "status:\t\tnot supported\n");
858 len += sprintf(p + len, "status:\t\tsupported\n");
859 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
860 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
861 if (video_supported == VIDEO_NEW)
862 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
863 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
864 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
865 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
866 if (video_supported == VIDEO_NEW)
867 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
868 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
869 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
874 static int video_switch(void)
876 int autosw = video_autosw();
879 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
881 ret = video_supported == VIDEO_570 ?
882 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
883 acpi_evalf(vid_handle, NULL, "VSWT", "v");
884 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
889 static int video_expand(void)
891 if (video_supported == VIDEO_570)
892 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
893 else if (video_supported == VIDEO_770)
894 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
896 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
899 static int video_switch2(int status)
903 if (video_supported == VIDEO_570) {
904 ret = acpi_evalf(NULL, NULL,
905 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
906 } else if (video_supported == VIDEO_770) {
907 int autosw = video_autosw();
908 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
911 ret = acpi_evalf(vid_handle, NULL,
912 "ASWT", "vdd", status * 0x100, 0);
914 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
916 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
917 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
923 static int video_write(char *buf)
926 int enable, disable, status;
928 if (!video_supported)
931 enable = disable = 0;
933 while ((cmd = next_cmd(&buf))) {
934 if (strlencmp(cmd, "lcd_enable") == 0) {
936 } else if (strlencmp(cmd, "lcd_disable") == 0) {
938 } else if (strlencmp(cmd, "crt_enable") == 0) {
940 } else if (strlencmp(cmd, "crt_disable") == 0) {
942 } else if (video_supported == VIDEO_NEW &&
943 strlencmp(cmd, "dvi_enable") == 0) {
945 } else if (video_supported == VIDEO_NEW &&
946 strlencmp(cmd, "dvi_disable") == 0) {
948 } else if (strlencmp(cmd, "auto_enable") == 0) {
949 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
951 } else if (strlencmp(cmd, "auto_disable") == 0) {
952 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
954 } else if (strlencmp(cmd, "video_switch") == 0) {
957 } else if (strlencmp(cmd, "expand_toggle") == 0) {
964 if (enable || disable) {
965 status = (video_status() & 0x0f & ~disable) | enable;
966 if (!video_switch2(status))
973 static void video_exit(void)
975 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
978 static int light_supported;
979 static int light_status_supported;
981 static int light_init(void)
983 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
984 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
987 /* light status not supported on
988 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
989 light_status_supported = acpi_evalf(ec_handle, NULL,
995 static int light_read(char *p)
1000 if (!light_supported) {
1001 len += sprintf(p + len, "status:\t\tnot supported\n");
1002 } else if (!light_status_supported) {
1003 len += sprintf(p + len, "status:\t\tunknown\n");
1004 len += sprintf(p + len, "commands:\ton, off\n");
1006 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1008 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1009 len += sprintf(p + len, "commands:\ton, off\n");
1015 static int light_write(char *buf)
1017 int cmos_cmd, lght_cmd;
1021 if (!light_supported)
1024 while ((cmd = next_cmd(&buf))) {
1025 if (strlencmp(cmd, "on") == 0) {
1028 } else if (strlencmp(cmd, "off") == 0) {
1034 success = cmos_handle ?
1035 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1036 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1044 static int _sta(acpi_handle handle)
1048 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1054 #ifdef CONFIG_ACPI_IBM_DOCK
1055 #define dock_docked() (_sta(dock_handle) & 1)
1057 static int dock_read(char *p)
1060 int docked = dock_docked();
1063 len += sprintf(p + len, "status:\t\tnot supported\n");
1065 len += sprintf(p + len, "status:\t\tundocked\n");
1067 len += sprintf(p + len, "status:\t\tdocked\n");
1068 len += sprintf(p + len, "commands:\tdock, undock\n");
1074 static int dock_write(char *buf)
1081 while ((cmd = next_cmd(&buf))) {
1082 if (strlencmp(cmd, "undock") == 0) {
1083 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1084 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1086 } else if (strlencmp(cmd, "dock") == 0) {
1087 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1096 static void dock_notify(struct ibm_struct *ibm, u32 event)
1098 int docked = dock_docked();
1099 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1101 if (event == 1 && !pci) /* 570 */
1102 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1103 else if (event == 1 && pci) /* 570 */
1104 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1105 else if (event == 3 && docked)
1106 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1107 else if (event == 3 && !docked)
1108 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1109 else if (event == 0 && docked)
1110 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1112 printk(IBM_ERR "unknown dock event %d, status %d\n",
1113 event, _sta(dock_handle));
1114 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1119 static int bay_status_supported;
1120 static int bay_status2_supported;
1121 static int bay_eject_supported;
1122 static int bay_eject2_supported;
1124 static int bay_init(void)
1126 bay_status_supported = bay_handle &&
1127 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1128 bay_status2_supported = bay2_handle &&
1129 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1131 bay_eject_supported = bay_handle && bay_ej_handle &&
1132 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1133 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1134 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1139 #define bay_occupied(b) (_sta(b##_handle) & 1)
1141 static int bay_read(char *p)
1144 int occupied = bay_occupied(bay);
1145 int occupied2 = bay_occupied(bay2);
1148 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1149 (occupied ? "occupied" : "unoccupied") :
1151 if (bay_status2_supported)
1152 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1153 "occupied" : "unoccupied");
1155 eject = bay_eject_supported && occupied;
1156 eject2 = bay_eject2_supported && occupied2;
1158 if (eject && eject2)
1159 len += sprintf(p + len, "commands:\teject, eject2\n");
1161 len += sprintf(p + len, "commands:\teject\n");
1163 len += sprintf(p + len, "commands:\teject2\n");
1168 static int bay_write(char *buf)
1172 if (!bay_eject_supported && !bay_eject2_supported)
1175 while ((cmd = next_cmd(&buf))) {
1176 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1177 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1179 } else if (bay_eject2_supported &&
1180 strlencmp(cmd, "eject2") == 0) {
1181 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1190 static void bay_notify(struct ibm_struct *ibm, u32 event)
1192 acpi_bus_generate_event(ibm->device, event, 0);
1195 static int cmos_read(char *p)
1199 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1200 R30, R31, T20-22, X20-21 */
1202 len += sprintf(p + len, "status:\t\tnot supported\n");
1204 len += sprintf(p + len, "status:\t\tsupported\n");
1205 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1211 static int cmos_eval(int cmos_cmd)
1214 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1219 static int cmos_write(char *buf)
1227 while ((cmd = next_cmd(&buf))) {
1228 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1229 cmos_cmd >= 0 && cmos_cmd <= 21) {
1234 if (!cmos_eval(cmos_cmd))
1241 static int led_supported;
1247 static int led_init(void)
1250 /* led not supported on R30, R31 */
1252 else if (strlencmp(led_path, "SLED") == 0)
1254 led_supported = LED_570;
1255 else if (strlencmp(led_path, "SYSL") == 0)
1256 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1257 led_supported = LED_OLD;
1260 led_supported = LED_NEW;
1265 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1267 static int led_read(char *p)
1271 if (!led_supported) {
1272 len += sprintf(p + len, "status:\t\tnot supported\n");
1275 len += sprintf(p + len, "status:\t\tsupported\n");
1277 if (led_supported == LED_570) {
1280 for (i = 0; i < 8; i++) {
1281 if (!acpi_evalf(ec_handle,
1282 &status, "GLED", "dd", 1 << i))
1284 len += sprintf(p + len, "%d:\t\t%s\n",
1285 i, led_status(status));
1289 len += sprintf(p + len, "commands:\t"
1290 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1295 /* off, on, blink */
1296 static const int led_sled_arg1[] = { 0, 1, 3 };
1297 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1298 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1299 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1301 #define EC_HLCL 0x0c
1302 #define EC_HLBL 0x0d
1303 #define EC_HLMS 0x0e
1305 static int led_write(char *buf)
1313 while ((cmd = next_cmd(&buf))) {
1314 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1317 if (strstr(cmd, "off")) {
1319 } else if (strstr(cmd, "on")) {
1321 } else if (strstr(cmd, "blink")) {
1326 if (led_supported == LED_570) {
1329 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1330 led, led_sled_arg1[ind]))
1332 } else if (led_supported == LED_OLD) {
1333 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1335 ret = ec_write(EC_HLMS, led);
1338 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1341 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1346 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1347 led, led_led_arg1[ind]))
1355 static int beep_read(char *p)
1360 len += sprintf(p + len, "status:\t\tnot supported\n");
1362 len += sprintf(p + len, "status:\t\tsupported\n");
1363 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1369 static int beep_write(char *buf)
1377 while ((cmd = next_cmd(&buf))) {
1378 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1379 beep_cmd >= 0 && beep_cmd <= 17) {
1383 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1390 static int acpi_ec_read(int i, u8 * p)
1395 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1399 if (ec_read(i, p) < 0)
1406 static int acpi_ec_write(int i, u8 v)
1409 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1412 if (ec_write(i, v) < 0)
1419 static enum thermal_access_mode thermal_read_mode;
1421 static int thermal_init(void)
1425 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1427 if (ibm_thinkpad_ec_found && experimental) {
1429 * Direct EC access mode: sensors at registers
1430 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1431 * non-implemented, thermal sensors return 0x80 when
1436 for (i = 0; i < 8; i++) {
1437 if (likely(acpi_ec_read(0x78 + i, &t))) {
1443 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1451 /* This is sheer paranoia, but we handle it anyway */
1454 "ThinkPad ACPI EC access misbehaving, "
1455 "falling back to ACPI TMPx access mode\n");
1456 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1459 "ThinkPad ACPI EC access misbehaving, "
1460 "disabling thermal sensors access\n");
1461 thermal_read_mode = IBMACPI_THERMAL_NONE;
1466 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1468 } else if (acpi_tmp7) {
1469 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1470 /* 600e/x, 770e, 770x */
1471 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1473 /* Standard ACPI TMPx access, max 8 sensors */
1474 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1477 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1478 thermal_read_mode = IBMACPI_THERMAL_NONE;
1484 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1488 char tmpi[] = "TMPi";
1493 switch (thermal_read_mode) {
1494 #if IBMACPI_MAX_THERMAL_SENSORS >= 16
1495 case IBMACPI_THERMAL_TPEC_16:
1496 for (i = 0; i < 8; i++) {
1497 if (!acpi_ec_read(0xC0 + i, &tmp))
1499 s->temp[i + 8] = tmp * 1000;
1503 case IBMACPI_THERMAL_TPEC_8:
1504 for (i = 0; i < 8; i++) {
1505 if (!acpi_ec_read(0x78 + i, &tmp))
1507 s->temp[i] = tmp * 1000;
1509 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1511 case IBMACPI_THERMAL_ACPI_UPDT:
1512 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1514 for (i = 0; i < 8; i++) {
1516 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1518 s->temp[i] = (t - 2732) * 100;
1522 case IBMACPI_THERMAL_ACPI_TMP07:
1523 for (i = 0; i < 8; i++) {
1525 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1527 s->temp[i] = t * 1000;
1531 case IBMACPI_THERMAL_NONE:
1537 static int thermal_read(char *p)
1541 struct ibm_thermal_sensors_struct t;
1543 n = thermal_get_sensors(&t);
1544 if (unlikely(n < 0))
1547 len += sprintf(p + len, "temperatures:\t");
1550 for (i = 0; i < (n - 1); i++)
1551 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1552 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1554 len += sprintf(p + len, "not supported\n");
1559 static u8 ecdump_regs[256];
1561 static int ecdump_read(char *p)
1567 len += sprintf(p + len, "EC "
1568 " +00 +01 +02 +03 +04 +05 +06 +07"
1569 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1570 for (i = 0; i < 256; i += 16) {
1571 len += sprintf(p + len, "EC 0x%02x:", i);
1572 for (j = 0; j < 16; j++) {
1573 if (!acpi_ec_read(i + j, &v))
1575 if (v != ecdump_regs[i + j])
1576 len += sprintf(p + len, " *%02x", v);
1578 len += sprintf(p + len, " %02x", v);
1579 ecdump_regs[i + j] = v;
1581 len += sprintf(p + len, "\n");
1586 /* These are way too dangerous to advertise openly... */
1588 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1589 " (<offset> is 00-ff, <value> is 00-ff)\n");
1590 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1591 " (<offset> is 00-ff, <value> is 0-255)\n");
1596 static int ecdump_write(char *buf)
1601 while ((cmd = next_cmd(&buf))) {
1602 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1604 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1608 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1609 if (!acpi_ec_write(i, v))
1618 static int brightness_offset = 0x31;
1620 static int brightness_get(struct backlight_device *bd)
1623 if (!acpi_ec_read(brightness_offset, &level))
1630 static int brightness_read(char *p)
1635 if ((level = brightness_get(NULL)) < 0) {
1636 len += sprintf(p + len, "level:\t\tunreadable\n");
1638 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1639 len += sprintf(p + len, "commands:\tup, down\n");
1640 len += sprintf(p + len, "commands:\tlevel <level>"
1641 " (<level> is 0-7)\n");
1647 #define BRIGHTNESS_UP 4
1648 #define BRIGHTNESS_DOWN 5
1650 static int brightness_set(int value)
1652 int cmos_cmd, inc, i;
1653 int current_value = brightness_get(NULL);
1657 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1658 inc = value > current_value ? 1 : -1;
1659 for (i = current_value; i != value; i += inc) {
1660 if (!cmos_eval(cmos_cmd))
1662 if (!acpi_ec_write(brightness_offset, i + inc))
1669 static int brightness_write(char *buf)
1675 while ((cmd = next_cmd(&buf))) {
1676 if ((level = brightness_get(NULL)) < 0)
1680 if (strlencmp(cmd, "up") == 0) {
1681 new_level = level == 7 ? 7 : level + 1;
1682 } else if (strlencmp(cmd, "down") == 0) {
1683 new_level = level == 0 ? 0 : level - 1;
1684 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1685 new_level >= 0 && new_level <= 7) {
1690 brightness_set(new_level);
1696 static int brightness_update_status(struct backlight_device *bd)
1698 return brightness_set(bd->props->brightness);
1701 static int volume_offset = 0x30;
1703 static int volume_read(char *p)
1708 if (!acpi_ec_read(volume_offset, &level)) {
1709 len += sprintf(p + len, "level:\t\tunreadable\n");
1711 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1712 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1713 len += sprintf(p + len, "commands:\tup, down, mute\n");
1714 len += sprintf(p + len, "commands:\tlevel <level>"
1715 " (<level> is 0-15)\n");
1721 #define VOLUME_DOWN 0
1723 #define VOLUME_MUTE 2
1725 static int volume_write(char *buf)
1727 int cmos_cmd, inc, i;
1729 int new_level, new_mute;
1732 while ((cmd = next_cmd(&buf))) {
1733 if (!acpi_ec_read(volume_offset, &level))
1735 new_mute = mute = level & 0x40;
1736 new_level = level = level & 0xf;
1738 if (strlencmp(cmd, "up") == 0) {
1742 new_level = level == 15 ? 15 : level + 1;
1743 } else if (strlencmp(cmd, "down") == 0) {
1747 new_level = level == 0 ? 0 : level - 1;
1748 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1749 new_level >= 0 && new_level <= 15) {
1751 } else if (strlencmp(cmd, "mute") == 0) {
1756 if (new_level != level) { /* mute doesn't change */
1757 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1758 inc = new_level > level ? 1 : -1;
1760 if (mute && (!cmos_eval(cmos_cmd) ||
1761 !acpi_ec_write(volume_offset, level)))
1764 for (i = level; i != new_level; i += inc)
1765 if (!cmos_eval(cmos_cmd) ||
1766 !acpi_ec_write(volume_offset, i + inc))
1769 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1770 !acpi_ec_write(volume_offset,
1775 if (new_mute != mute) { /* level doesn't change */
1776 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1778 if (!cmos_eval(cmos_cmd) ||
1779 !acpi_ec_write(volume_offset, level + new_mute))
1787 static enum fan_status_access_mode fan_status_access_mode;
1788 static enum fan_control_access_mode fan_control_access_mode;
1789 static enum fan_control_commands fan_control_commands;
1791 static int fan_init(void)
1795 fan_status_access_mode = IBMACPI_FAN_NONE;
1796 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1797 fan_control_commands = 0;
1800 /* 570, 600e/x, 770e, 770x */
1801 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1803 /* all other ThinkPads: note that even old-style
1804 * ThinkPad ECs supports the fan control register */
1805 if (likely(acpi_ec_read(fan_status_offset, &status))) {
1806 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1809 "ThinkPad ACPI EC access misbehaving, "
1810 "fan status and control unavailable\n");
1817 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
1818 fan_control_commands |= IBMACPI_FAN_CMD_LEVEL;
1821 /* gfan without sfan means no fan control */
1822 /* all other models implement TP EC 0x2f control */
1826 fan_control_access_mode =
1827 IBMACPI_FAN_WR_ACPI_FANS;
1828 fan_control_commands |=
1829 IBMACPI_FAN_CMD_SPEED |
1830 IBMACPI_FAN_CMD_ENABLE;
1832 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
1833 fan_control_commands |= IBMACPI_FAN_CMD_ENABLE;
1841 static int fan_get_status(u8 *status)
1846 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1848 switch (fan_status_access_mode) {
1849 case IBMACPI_FAN_RD_ACPI_GFAN:
1850 /* 570, 600e/x, 770e, 770x */
1852 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
1860 case IBMACPI_FAN_RD_TPEC:
1861 /* all except 570, 600e/x, 770e, 770x */
1862 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1877 static int fan_get_speed(unsigned int *speed)
1881 switch (fan_status_access_mode) {
1882 case IBMACPI_FAN_RD_TPEC:
1883 /* all except 570, 600e/x, 770e, 770x */
1884 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1885 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1889 *speed = (hi << 8) | lo;
1900 static int fan_read(char *p)
1905 unsigned int speed = 0;
1907 switch (fan_status_access_mode) {
1908 case IBMACPI_FAN_RD_ACPI_GFAN:
1909 /* 570, 600e/x, 770e, 770x */
1910 if ((rc = fan_get_status(&status)) < 0)
1913 len += sprintf(p + len, "level:\t\t%d\n", status);
1917 case IBMACPI_FAN_RD_TPEC:
1918 /* all except 570, 600e/x, 770e, 770x */
1919 if ((rc = fan_get_status(&status)) < 0)
1922 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 7));
1924 if ((rc = fan_get_speed(&speed)) < 0)
1927 len += sprintf(p + len, "speed:\t\t%d\n", speed);
1930 case IBMACPI_FAN_NONE:
1932 len += sprintf(p + len, "status:\t\tnot supported\n");
1935 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL)
1936 len += sprintf(p + len, "commands:\tlevel <level>"
1937 " (<level> is 0-7)\n");
1939 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
1940 len += sprintf(p + len, "commands:\tenable, disable\n");
1942 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
1943 len += sprintf(p + len, "commands:\tspeed <speed>"
1944 " (<speed> is 0-65535)\n");
1949 static int fan_set_level(int level)
1951 switch (fan_control_access_mode) {
1952 case IBMACPI_FAN_WR_ACPI_SFAN:
1953 if (level >= 0 && level <= 7) {
1954 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
1966 static int fan_set_enable(void)
1968 switch (fan_control_access_mode) {
1969 case IBMACPI_FAN_WR_ACPI_FANS:
1970 case IBMACPI_FAN_WR_TPEC:
1971 if (!acpi_ec_write(fan_status_offset, 0x80))
1981 static int fan_set_disable(void)
1983 switch (fan_control_access_mode) {
1984 case IBMACPI_FAN_WR_ACPI_FANS:
1985 case IBMACPI_FAN_WR_TPEC:
1986 if (!acpi_ec_write(fan_status_offset, 0x00))
1996 static int fan_set_speed(int speed)
1998 switch (fan_control_access_mode) {
1999 case IBMACPI_FAN_WR_ACPI_FANS:
2000 if (speed >= 0 && speed <= 65535) {
2001 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2002 speed, speed, speed))
2014 static int fan_write_cmd_level(const char *cmd, int *rc)
2018 if (sscanf(cmd, "level %d", &level) != 1)
2021 if ((*rc = fan_set_level(level)) == -ENXIO)
2022 printk(IBM_ERR "level command accepted for unsupported "
2023 "access mode %d", fan_control_access_mode);
2028 static int fan_write_cmd_enable(const char *cmd, int *rc)
2030 if (strlencmp(cmd, "enable") != 0)
2033 if ((*rc = fan_set_enable()) == -ENXIO)
2034 printk(IBM_ERR "enable command accepted for unsupported "
2035 "access mode %d", fan_control_access_mode);
2040 static int fan_write_cmd_disable(const char *cmd, int *rc)
2042 if (strlencmp(cmd, "disable") != 0)
2045 if ((*rc = fan_set_disable()) == -ENXIO)
2046 printk(IBM_ERR "disable command accepted for unsupported "
2047 "access mode %d", fan_control_access_mode);
2052 static int fan_write_cmd_speed(const char *cmd, int *rc)
2057 * Support speed <low> <medium> <high> ? */
2059 if (sscanf(cmd, "speed %d", &speed) != 1)
2062 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2063 printk(IBM_ERR "speed command accepted for unsupported "
2064 "access mode %d", fan_control_access_mode);
2069 static int fan_write(char *buf)
2074 while (!rc && (cmd = next_cmd(&buf))) {
2075 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2076 fan_write_cmd_level(cmd, &rc)) &&
2077 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2078 (fan_write_cmd_enable(cmd, &rc) ||
2079 fan_write_cmd_disable(cmd, &rc))) &&
2080 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2081 fan_write_cmd_speed(cmd, &rc))
2089 static struct ibm_struct ibms[] = {
2092 .init = driver_init,
2093 .read = driver_read,
2097 .hid = IBM_HKEY_HID,
2098 .init = hotkey_init,
2099 .read = hotkey_read,
2100 .write = hotkey_write,
2101 .exit = hotkey_exit,
2102 .notify = hotkey_notify,
2103 .handle = &hkey_handle,
2104 .type = ACPI_DEVICE_NOTIFY,
2107 .name = "bluetooth",
2108 .init = bluetooth_init,
2109 .read = bluetooth_read,
2110 .write = bluetooth_write,
2123 .write = video_write,
2130 .write = light_write,
2132 #ifdef CONFIG_ACPI_IBM_DOCK
2136 .write = dock_write,
2137 .notify = dock_notify,
2138 .handle = &dock_handle,
2139 .type = ACPI_SYSTEM_NOTIFY,
2144 .notify = dock_notify,
2145 .handle = &pci_handle,
2146 .type = ACPI_SYSTEM_NOTIFY,
2154 .notify = bay_notify,
2155 .handle = &bay_handle,
2156 .type = ACPI_SYSTEM_NOTIFY,
2161 .write = cmos_write,
2172 .write = beep_write,
2176 .init = thermal_init,
2177 .read = thermal_read,
2181 .read = ecdump_read,
2182 .write = ecdump_write,
2186 .name = "brightness",
2187 .read = brightness_read,
2188 .write = brightness_write,
2192 .read = volume_read,
2193 .write = volume_write,
2204 static int dispatch_read(char *page, char **start, off_t off, int count,
2205 int *eof, void *data)
2207 struct ibm_struct *ibm = (struct ibm_struct *)data;
2210 if (!ibm || !ibm->read)
2213 len = ibm->read(page);
2217 if (len <= off + count)
2219 *start = page + off;
2229 static int dispatch_write(struct file *file, const char __user * userbuf,
2230 unsigned long count, void *data)
2232 struct ibm_struct *ibm = (struct ibm_struct *)data;
2236 if (!ibm || !ibm->write)
2239 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2243 if (copy_from_user(kernbuf, userbuf, count)) {
2249 strcat(kernbuf, ",");
2250 ret = ibm->write(kernbuf);
2259 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2261 struct ibm_struct *ibm = (struct ibm_struct *)data;
2263 if (!ibm || !ibm->notify)
2266 ibm->notify(ibm, event);
2269 static int __init setup_notify(struct ibm_struct *ibm)
2277 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2279 printk(IBM_ERR "%s device not present\n", ibm->name);
2283 acpi_driver_data(ibm->device) = ibm;
2284 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2286 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2287 dispatch_notify, ibm);
2288 if (ACPI_FAILURE(status)) {
2289 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2297 static int __init ibm_device_add(struct acpi_device *device)
2302 static int __init register_driver(struct ibm_struct *ibm)
2306 ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2308 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2312 memset(ibm->driver, 0, sizeof(struct acpi_driver));
2313 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
2314 ibm->driver->ids = ibm->hid;
2315 ibm->driver->ops.add = &ibm_device_add;
2317 ret = acpi_bus_register_driver(ibm->driver);
2319 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2327 static int __init ibm_init(struct ibm_struct *ibm)
2330 struct proc_dir_entry *entry;
2332 if (ibm->experimental && !experimental)
2336 ret = register_driver(ibm);
2339 ibm->driver_registered = 1;
2346 ibm->init_called = 1;
2350 entry = create_proc_entry(ibm->name,
2351 S_IFREG | S_IRUGO | S_IWUSR,
2354 printk(IBM_ERR "unable to create proc entry %s\n",
2358 entry->owner = THIS_MODULE;
2360 entry->read_proc = &dispatch_read;
2362 entry->write_proc = &dispatch_write;
2363 ibm->proc_created = 1;
2367 ret = setup_notify(ibm);
2370 ibm->notify_installed = 1;
2376 static void ibm_exit(struct ibm_struct *ibm)
2378 if (ibm->notify_installed)
2379 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2382 if (ibm->proc_created)
2383 remove_proc_entry(ibm->name, proc_dir);
2385 if (ibm->init_called && ibm->exit)
2388 if (ibm->driver_registered) {
2389 acpi_bus_unregister_driver(ibm->driver);
2394 static void __init ibm_handle_init(char *name,
2395 acpi_handle * handle, acpi_handle parent,
2396 char **paths, int num_paths, char **path)
2401 for (i = 0; i < num_paths; i++) {
2402 status = acpi_get_handle(parent, paths[i], handle);
2403 if (ACPI_SUCCESS(status)) {
2412 #define IBM_HANDLE_INIT(object) \
2413 ibm_handle_init(#object, &object##_handle, *object##_parent, \
2414 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
2416 static int set_ibm_param(const char *val, struct kernel_param *kp)
2420 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2421 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2422 if (strlen(val) > sizeof(ibms[i].param) - 2)
2424 strcpy(ibms[i].param, val);
2425 strcat(ibms[i].param, ",");
2432 #define IBM_PARAM(feature) \
2433 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2436 IBM_PARAM(bluetooth);
2439 #ifdef CONFIG_ACPI_IBM_DOCK
2447 IBM_PARAM(brightness);
2451 static struct backlight_properties ibm_backlight_data = {
2452 .owner = THIS_MODULE,
2453 .get_brightness = brightness_get,
2454 .update_status = brightness_update_status,
2455 .max_brightness = 7,
2458 static void acpi_ibm_exit(void)
2462 if (ibm_backlight_device)
2463 backlight_device_unregister(ibm_backlight_device);
2465 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2468 remove_proc_entry(IBM_DIR, acpi_root_dir);
2471 static int __init check_dmi_for_ec(void)
2473 struct dmi_device *dev = NULL;
2476 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2477 * X32 or newer, all Z series; Some models must have an
2478 * up-to-date BIOS or they will not be detected.
2480 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2482 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2483 if (strstr(dev->name, "IBM ThinkPad Embedded Controller"))
2489 static int __init acpi_ibm_init(void)
2496 if (!acpi_specific_hotkey_enabled) {
2497 printk(IBM_ERR "using generic hotkey driver\n");
2501 /* ec is required because many other handles are relative to it */
2502 IBM_HANDLE_INIT(ec);
2504 printk(IBM_ERR "ec object not found\n");
2508 /* Models with newer firmware report the EC in DMI */
2509 ibm_thinkpad_ec_found = check_dmi_for_ec();
2511 /* these handles are not required */
2512 IBM_HANDLE_INIT(vid);
2513 IBM_HANDLE_INIT(vid2);
2514 IBM_HANDLE_INIT(ledb);
2515 IBM_HANDLE_INIT(led);
2516 IBM_HANDLE_INIT(hkey);
2517 IBM_HANDLE_INIT(lght);
2518 IBM_HANDLE_INIT(cmos);
2519 #ifdef CONFIG_ACPI_IBM_DOCK
2520 IBM_HANDLE_INIT(dock);
2522 IBM_HANDLE_INIT(pci);
2523 IBM_HANDLE_INIT(bay);
2525 IBM_HANDLE_INIT(bay_ej);
2526 IBM_HANDLE_INIT(bay2);
2528 IBM_HANDLE_INIT(bay2_ej);
2529 IBM_HANDLE_INIT(beep);
2530 IBM_HANDLE_INIT(ecrd);
2531 IBM_HANDLE_INIT(ecwr);
2532 IBM_HANDLE_INIT(fans);
2533 IBM_HANDLE_INIT(gfan);
2534 IBM_HANDLE_INIT(sfan);
2536 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2538 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2541 proc_dir->owner = THIS_MODULE;
2543 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
2544 ret = ibm_init(&ibms[i]);
2545 if (ret >= 0 && *ibms[i].param)
2546 ret = ibms[i].write(ibms[i].param);
2553 ibm_backlight_device = backlight_device_register("ibm", NULL,
2554 &ibm_backlight_data);
2555 if (IS_ERR(ibm_backlight_device)) {
2556 printk(IBM_ERR "Could not register ibm backlight device\n");
2557 ibm_backlight_device = NULL;
2564 module_init(acpi_ibm_init);
2565 module_exit(acpi_ibm_exit);