ACPI: ibm-acpi: kill trailing whitespace
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / ibm_acpi.c
blob750ec0037aebf9bc5287cb427c9ee3167e3a9d7b
1 /*
2 * ibm_acpi.c - IBM ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define IBM_VERSION "0.13"
26 * Changelog:
28 * 2006-11-22 0.13 new maintainer
29 * changelog now lives in git commit history, and will
30 * not be updated further in-file.
32 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
33 * 2005-03-17 0.11 support for 600e, 770x
34 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
35 * support for 770e, G41
36 * G40 and G41 don't have a thinklight
37 * temperatures no longer experimental
38 * experimental brightness control
39 * experimental volume control
40 * experimental fan enable/disable
41 * 2005-01-16 0.10 fix module loading on R30, R31
42 * 2005-01-16 0.9 support for 570, R30, R31
43 * ultrabay support on A22p, A3x
44 * limit arg for cmos, led, beep, drop experimental status
45 * more capable led control on A21e, A22p, T20-22, X20
46 * experimental temperatures and fan speed
47 * experimental embedded controller register dump
48 * mark more functions as __init, drop incorrect __exit
49 * use MODULE_VERSION
50 * thanks to Henrik Brix Andersen <brix@gentoo.org>
51 * fix parameter passing on module loading
52 * thanks to Rusty Russell <rusty@rustcorp.com.au>
53 * thanks to Jim Radford <radford@blackbean.org>
54 * 2004-11-08 0.8 fix init error case, don't return from a macro
55 * thanks to Chris Wright <chrisw@osdl.org>
56 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
57 * fix led control on A21e
58 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
59 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
60 * proc file format changed
61 * video_switch command
62 * experimental cmos control
63 * experimental led control
64 * experimental acpi sounds
65 * 2004-09-16 0.4 support for module parameters
66 * hotkey mask can be prefixed by 0x
67 * video output switching
68 * video expansion control
69 * ultrabay eject support
70 * removed lcd brightness/on/off control, didn't work
71 * 2004-08-17 0.3 support for R40
72 * lcd off, brightness control
73 * thinklight on/off
74 * 2004-08-14 0.2 support for T series, X20
75 * bluetooth enable/disable
76 * hotkey events disabled by default
77 * removed fan control, currently useless
78 * 2004-08-09 0.1 initial release, support for X series
81 #include <linux/kernel.h>
82 #include <linux/module.h>
83 #include <linux/init.h>
84 #include <linux/types.h>
85 #include <linux/string.h>
87 #include <linux/proc_fs.h>
88 #include <linux/backlight.h>
89 #include <linux/fb.h>
90 #include <asm/uaccess.h>
92 #include <linux/dmi.h>
93 #include <linux/jiffies.h>
94 #include <linux/workqueue.h>
96 #include <acpi/acpi_drivers.h>
97 #include <acpi/acnamesp.h>
99 #define IBM_NAME "ibm"
100 #define IBM_DESC "IBM ThinkPad ACPI Extras"
101 #define IBM_FILE "ibm_acpi"
102 #define IBM_URL "http://ibm-acpi.sf.net/"
104 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
105 MODULE_DESCRIPTION(IBM_DESC);
106 MODULE_VERSION(IBM_VERSION);
107 MODULE_LICENSE("GPL");
109 #define IBM_DIR IBM_NAME
111 #define IBM_LOG IBM_FILE ": "
112 #define IBM_ERR KERN_ERR IBM_LOG
113 #define IBM_NOTICE KERN_NOTICE IBM_LOG
114 #define IBM_INFO KERN_INFO IBM_LOG
115 #define IBM_DEBUG KERN_DEBUG IBM_LOG
117 #define IBM_MAX_ACPI_ARGS 3
119 #define __unused __attribute__ ((unused))
121 static int experimental;
122 module_param(experimental, int, 0);
124 static acpi_handle root_handle = NULL;
126 #define IBM_HANDLE(object, parent, paths...) \
127 static acpi_handle object##_handle; \
128 static acpi_handle *object##_parent = &parent##_handle; \
129 static char *object##_path; \
130 static char *object##_paths[] = { paths }
132 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
133 "\\_SB.PCI.ISA.EC", /* 570 */
134 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
135 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
136 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
137 "\\_SB.PCI0.ICH3.EC0", /* R31 */
138 "\\_SB.PCI0.LPC.EC", /* all others */
141 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
142 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
143 "\\_SB.PCI0.VID0", /* 770e */
144 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
145 "\\_SB.PCI0.AGP.VID", /* all others */
146 ); /* R30, R31 */
148 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
150 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
151 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
152 "\\CMS", /* R40, R40e */
153 ); /* all others */
154 #ifdef CONFIG_ACPI_IBM_DOCK
155 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
156 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
157 "\\_SB.PCI0.PCI1.DOCK", /* all others */
158 "\\_SB.PCI.ISA.SLCE", /* 570 */
159 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
160 #endif
161 #ifdef CONFIG_ACPI_IBM_BAY
162 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
163 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
164 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
165 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
166 ); /* A21e, R30, R31 */
168 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
169 "_EJ0", /* all others */
170 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
172 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
173 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
174 ); /* all others */
176 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
177 "_EJ0", /* 770x */
178 ); /* all others */
179 #endif /* CONFIG_ACPI_IBM_BAY */
181 /* don't list other alternatives as we install a notify handler on the 570 */
182 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
184 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
185 "^HKEY", /* R30, R31 */
186 "HKEY", /* all others */
187 ); /* 570 */
189 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
190 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
192 IBM_HANDLE(led, ec, "SLED", /* 570 */
193 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
194 "LED", /* all others */
195 ); /* R30, R31 */
197 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
198 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
199 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
200 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
202 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
203 "\\FSPD", /* 600e/x, 770e, 770x */
204 ); /* all others */
206 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
207 "JFNS", /* 770x-JL */
208 ); /* all others */
210 #define IBM_HKEY_HID "IBM0068"
211 #define IBM_PCI_HID "PNP0A03"
213 enum thermal_access_mode {
214 IBMACPI_THERMAL_NONE = 0, /* No thermal support */
215 IBMACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
216 IBMACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
217 IBMACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
218 IBMACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
221 #define IBMACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
222 struct ibm_thermal_sensors_struct {
223 s32 temp[IBMACPI_MAX_THERMAL_SENSORS];
227 * FAN ACCESS MODES
229 * IBMACPI_FAN_RD_ACPI_GFAN:
230 * ACPI GFAN method: returns fan level
232 * see IBMACPI_FAN_WR_ACPI_SFAN
233 * EC 0x2f not available if GFAN exists
235 * IBMACPI_FAN_WR_ACPI_SFAN:
236 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
238 * EC 0x2f might be available *for reading*, but never for writing.
240 * IBMACPI_FAN_WR_TPEC:
241 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
242 * on almost all ThinkPads
244 * Fan speed changes of any sort (including those caused by the
245 * disengaged mode) are usually done slowly by the firmware as the
246 * maximum ammount of fan duty cycle change per second seems to be
247 * limited.
249 * Reading is not available if GFAN exists.
250 * Writing is not available if SFAN exists.
252 * Bits
253 * 7 automatic mode engaged;
254 * (default operation mode of the ThinkPad)
255 * fan level is ignored in this mode.
256 * 6 disengage mode (takes precedence over bit 7);
257 * not available on all thinkpads. May disable
258 * the tachometer, and speeds up fan to 100% duty-cycle,
259 * which speeds it up far above the standard RPM
260 * levels. It is not impossible that it could cause
261 * hardware damage.
262 * 5-3 unused in some models. Extra bits for fan level
263 * in others, but still useless as all values above
264 * 7 map to the same speed as level 7 in these models.
265 * 2-0 fan level (0..7 usually)
266 * 0x00 = stop
267 * 0x07 = max (set when temperatures critical)
268 * Some ThinkPads may have other levels, see
269 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
271 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
272 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
273 * does so, its initial value is meaningless (0x07).
275 * For firmware bugs, refer to:
276 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
278 * ----
280 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
281 * Main fan tachometer reading (in RPM)
283 * This register is present on all ThinkPads with a new-style EC, and
284 * it is known not to be present on the A21m/e, and T22, as there is
285 * something else in offset 0x84 according to the ACPI DSDT. Other
286 * ThinkPads from this same time period (and earlier) probably lack the
287 * tachometer as well.
289 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
290 * was never fixed by IBM to report the EC firmware version string
291 * probably support the tachometer (like the early X models), so
292 * detecting it is quite hard. We need more data to know for sure.
294 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
295 * might result.
297 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
298 * register is not invalidated in ThinkPads that disable tachometer
299 * readings. Thus, the tachometer readings go stale.
301 * For firmware bugs, refer to:
302 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
304 * IBMACPI_FAN_WR_ACPI_FANS:
305 * ThinkPad X31, X40, X41. Not available in the X60.
307 * FANS ACPI handle: takes three arguments: low speed, medium speed,
308 * high speed. ACPI DSDT seems to map these three speeds to levels
309 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
310 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
312 * The speeds are stored on handles
313 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
315 * There are three default speed sets, acessible as handles:
316 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
318 * ACPI DSDT switches which set is in use depending on various
319 * factors.
321 * IBMACPI_FAN_WR_TPEC is also available and should be used to
322 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
323 * but the ACPI tables just mention level 7.
326 enum fan_status_access_mode {
327 IBMACPI_FAN_NONE = 0, /* No fan status or control */
328 IBMACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
329 IBMACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
332 enum fan_control_access_mode {
333 IBMACPI_FAN_WR_NONE = 0, /* No fan control */
334 IBMACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
335 IBMACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
336 IBMACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
339 enum fan_control_commands {
340 IBMACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
341 IBMACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
342 IBMACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
343 * and also watchdog cmd */
346 enum { /* Fan control constants */
347 fan_status_offset = 0x2f, /* EC register 0x2f */
348 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
349 * 0x84 must be read before 0x85 */
351 IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
352 * disengaged */
353 IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
354 * control */
357 static char *ibm_thinkpad_ec_found = NULL;
359 struct ibm_struct {
360 char *name;
361 char param[32];
363 char *hid;
364 struct acpi_driver *driver;
366 int (*init) (void);
367 int (*read) (char *);
368 int (*write) (char *);
369 void (*exit) (void);
371 void (*notify) (struct ibm_struct *, u32);
372 acpi_handle *handle;
373 int type;
374 struct acpi_device *device;
376 int driver_registered;
377 int proc_created;
378 int init_called;
379 int notify_installed;
381 int experimental;
384 static struct proc_dir_entry *proc_dir = NULL;
386 static struct backlight_device *ibm_backlight_device = NULL;
388 #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
389 #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
390 #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
392 static int acpi_evalf(acpi_handle handle,
393 void *res, char *method, char *fmt, ...)
395 char *fmt0 = fmt;
396 struct acpi_object_list params;
397 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
398 struct acpi_buffer result, *resultp;
399 union acpi_object out_obj;
400 acpi_status status;
401 va_list ap;
402 char res_type;
403 int success;
404 int quiet;
406 if (!*fmt) {
407 printk(IBM_ERR "acpi_evalf() called with empty format\n");
408 return 0;
411 if (*fmt == 'q') {
412 quiet = 1;
413 fmt++;
414 } else
415 quiet = 0;
417 res_type = *(fmt++);
419 params.count = 0;
420 params.pointer = &in_objs[0];
422 va_start(ap, fmt);
423 while (*fmt) {
424 char c = *(fmt++);
425 switch (c) {
426 case 'd': /* int */
427 in_objs[params.count].integer.value = va_arg(ap, int);
428 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
429 break;
430 /* add more types as needed */
431 default:
432 printk(IBM_ERR "acpi_evalf() called "
433 "with invalid format character '%c'\n", c);
434 return 0;
437 va_end(ap);
439 if (res_type != 'v') {
440 result.length = sizeof(out_obj);
441 result.pointer = &out_obj;
442 resultp = &result;
443 } else
444 resultp = NULL;
446 status = acpi_evaluate_object(handle, method, &params, resultp);
448 switch (res_type) {
449 case 'd': /* int */
450 if (res)
451 *(int *)res = out_obj.integer.value;
452 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
453 break;
454 case 'v': /* void */
455 success = status == AE_OK;
456 break;
457 /* add more types as needed */
458 default:
459 printk(IBM_ERR "acpi_evalf() called "
460 "with invalid format character '%c'\n", res_type);
461 return 0;
464 if (!success && !quiet)
465 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
466 method, fmt0, status);
468 return success;
471 static void __unused acpi_print_int(acpi_handle handle, char *method)
473 int i;
475 if (acpi_evalf(handle, &i, method, "d"))
476 printk(IBM_INFO "%s = 0x%x\n", method, i);
477 else
478 printk(IBM_ERR "error calling %s\n", method);
481 static char *next_cmd(char **cmds)
483 char *start = *cmds;
484 char *end;
486 while ((end = strchr(start, ',')) && end == start)
487 start = end + 1;
489 if (!end)
490 return NULL;
492 *end = 0;
493 *cmds = end + 1;
494 return start;
497 static int ibm_acpi_driver_init(void)
499 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
500 printk(IBM_INFO "%s\n", IBM_URL);
502 if (ibm_thinkpad_ec_found)
503 printk(IBM_INFO "ThinkPad EC firmware %s\n",
504 ibm_thinkpad_ec_found);
506 return 0;
509 static int driver_read(char *p)
511 int len = 0;
513 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
514 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
516 return len;
519 static int hotkey_supported;
520 static int hotkey_mask_supported;
521 static int hotkey_orig_status;
522 static int hotkey_orig_mask;
524 static int hotkey_get(int *status, int *mask)
526 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
527 return 0;
529 if (hotkey_mask_supported)
530 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
531 return 0;
533 return 1;
536 static int hotkey_set(int status, int mask)
538 int i;
540 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
541 return 0;
543 if (hotkey_mask_supported)
544 for (i = 0; i < 32; i++) {
545 int bit = ((1 << i) & mask) != 0;
546 if (!acpi_evalf(hkey_handle,
547 NULL, "MHKM", "vdd", i + 1, bit))
548 return 0;
551 return 1;
554 static int hotkey_init(void)
556 /* hotkey not supported on 570 */
557 hotkey_supported = hkey_handle != NULL;
559 if (hotkey_supported) {
560 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
561 A30, R30, R31, T20-22, X20-21, X22-24 */
562 hotkey_mask_supported =
563 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
565 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
566 return -ENODEV;
569 return 0;
572 static int hotkey_read(char *p)
574 int status, mask;
575 int len = 0;
577 if (!hotkey_supported) {
578 len += sprintf(p + len, "status:\t\tnot supported\n");
579 return len;
582 if (!hotkey_get(&status, &mask))
583 return -EIO;
585 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
586 if (hotkey_mask_supported) {
587 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
588 len += sprintf(p + len,
589 "commands:\tenable, disable, reset, <mask>\n");
590 } else {
591 len += sprintf(p + len, "mask:\t\tnot supported\n");
592 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
595 return len;
598 static int hotkey_write(char *buf)
600 int status, mask;
601 char *cmd;
602 int do_cmd = 0;
604 if (!hotkey_supported)
605 return -ENODEV;
607 if (!hotkey_get(&status, &mask))
608 return -EIO;
610 while ((cmd = next_cmd(&buf))) {
611 if (strlencmp(cmd, "enable") == 0) {
612 status = 1;
613 } else if (strlencmp(cmd, "disable") == 0) {
614 status = 0;
615 } else if (strlencmp(cmd, "reset") == 0) {
616 status = hotkey_orig_status;
617 mask = hotkey_orig_mask;
618 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
619 /* mask set */
620 } else if (sscanf(cmd, "%x", &mask) == 1) {
621 /* mask set */
622 } else
623 return -EINVAL;
624 do_cmd = 1;
627 if (do_cmd && !hotkey_set(status, mask))
628 return -EIO;
630 return 0;
633 static void hotkey_exit(void)
635 if (hotkey_supported)
636 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
639 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
641 int hkey;
643 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
644 acpi_bus_generate_event(ibm->device, event, hkey);
645 else {
646 printk(IBM_ERR "unknown hotkey event %d\n", event);
647 acpi_bus_generate_event(ibm->device, event, 0);
651 static int bluetooth_supported;
653 static int bluetooth_init(void)
655 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
656 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
657 bluetooth_supported = hkey_handle &&
658 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
660 return 0;
663 static int bluetooth_status(void)
665 int status;
667 if (!bluetooth_supported ||
668 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
669 status = 0;
671 return status;
674 static int bluetooth_read(char *p)
676 int len = 0;
677 int status = bluetooth_status();
679 if (!bluetooth_supported)
680 len += sprintf(p + len, "status:\t\tnot supported\n");
681 else if (!(status & 1))
682 len += sprintf(p + len, "status:\t\tnot installed\n");
683 else {
684 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
685 len += sprintf(p + len, "commands:\tenable, disable\n");
688 return len;
691 static int bluetooth_write(char *buf)
693 int status = bluetooth_status();
694 char *cmd;
695 int do_cmd = 0;
697 if (!bluetooth_supported)
698 return -ENODEV;
700 while ((cmd = next_cmd(&buf))) {
701 if (strlencmp(cmd, "enable") == 0) {
702 status |= 2;
703 } else if (strlencmp(cmd, "disable") == 0) {
704 status &= ~2;
705 } else
706 return -EINVAL;
707 do_cmd = 1;
710 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
711 return -EIO;
713 return 0;
716 static int wan_supported;
718 static int wan_init(void)
720 wan_supported = hkey_handle &&
721 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
723 return 0;
726 static int wan_status(void)
728 int status;
730 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
731 status = 0;
733 return status;
736 static int wan_read(char *p)
738 int len = 0;
739 int status = wan_status();
741 if (!wan_supported)
742 len += sprintf(p + len, "status:\t\tnot supported\n");
743 else if (!(status & 1))
744 len += sprintf(p + len, "status:\t\tnot installed\n");
745 else {
746 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
747 len += sprintf(p + len, "commands:\tenable, disable\n");
750 return len;
753 static int wan_write(char *buf)
755 int status = wan_status();
756 char *cmd;
757 int do_cmd = 0;
759 if (!wan_supported)
760 return -ENODEV;
762 while ((cmd = next_cmd(&buf))) {
763 if (strlencmp(cmd, "enable") == 0) {
764 status |= 2;
765 } else if (strlencmp(cmd, "disable") == 0) {
766 status &= ~2;
767 } else
768 return -EINVAL;
769 do_cmd = 1;
772 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
773 return -EIO;
775 return 0;
778 enum video_access_mode {
779 IBMACPI_VIDEO_NONE = 0,
780 IBMACPI_VIDEO_570, /* 570 */
781 IBMACPI_VIDEO_770, /* 600e/x, 770e, 770x */
782 IBMACPI_VIDEO_NEW, /* all others */
785 static enum video_access_mode video_supported;
786 static int video_orig_autosw;
788 static int video_init(void)
790 int ivga;
792 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
793 /* G41, assume IVGA doesn't change */
794 vid_handle = vid2_handle;
796 if (!vid_handle)
797 /* video switching not supported on R30, R31 */
798 video_supported = IBMACPI_VIDEO_NONE;
799 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
800 /* 570 */
801 video_supported = IBMACPI_VIDEO_570;
802 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
803 /* 600e/x, 770e, 770x */
804 video_supported = IBMACPI_VIDEO_770;
805 else
806 /* all others */
807 video_supported = IBMACPI_VIDEO_NEW;
809 return 0;
812 static int video_status(void)
814 int status = 0;
815 int i;
817 if (video_supported == IBMACPI_VIDEO_570) {
818 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
819 status = i & 3;
820 } else if (video_supported == IBMACPI_VIDEO_770) {
821 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
822 status |= 0x01 * i;
823 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
824 status |= 0x02 * i;
825 } else if (video_supported == IBMACPI_VIDEO_NEW) {
826 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
827 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
828 status |= 0x02 * i;
830 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
831 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
832 status |= 0x01 * i;
833 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
834 status |= 0x08 * i;
837 return status;
840 static int video_autosw(void)
842 int autosw = 0;
844 if (video_supported == IBMACPI_VIDEO_570)
845 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
846 else if (video_supported == IBMACPI_VIDEO_770 ||
847 video_supported == IBMACPI_VIDEO_NEW)
848 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
850 return autosw & 1;
853 static int video_read(char *p)
855 int status = video_status();
856 int autosw = video_autosw();
857 int len = 0;
859 if (!video_supported) {
860 len += sprintf(p + len, "status:\t\tnot supported\n");
861 return len;
864 len += sprintf(p + len, "status:\t\tsupported\n");
865 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
866 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
867 if (video_supported == IBMACPI_VIDEO_NEW)
868 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
869 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
870 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
871 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
872 if (video_supported == IBMACPI_VIDEO_NEW)
873 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
874 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
875 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
877 return len;
880 static int video_switch(void)
882 int autosw = video_autosw();
883 int ret;
885 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
886 return -EIO;
887 ret = video_supported == IBMACPI_VIDEO_570 ?
888 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
889 acpi_evalf(vid_handle, NULL, "VSWT", "v");
890 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
892 return ret;
895 static int video_expand(void)
897 if (video_supported == IBMACPI_VIDEO_570)
898 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
899 else if (video_supported == IBMACPI_VIDEO_770)
900 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
901 else
902 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
905 static int video_switch2(int status)
907 int ret;
909 if (video_supported == IBMACPI_VIDEO_570) {
910 ret = acpi_evalf(NULL, NULL,
911 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
912 } else if (video_supported == IBMACPI_VIDEO_770) {
913 int autosw = video_autosw();
914 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
915 return -EIO;
917 ret = acpi_evalf(vid_handle, NULL,
918 "ASWT", "vdd", status * 0x100, 0);
920 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
921 } else {
922 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
923 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
926 return ret;
929 static int video_write(char *buf)
931 char *cmd;
932 int enable, disable, status;
934 if (!video_supported)
935 return -ENODEV;
937 enable = disable = 0;
939 while ((cmd = next_cmd(&buf))) {
940 if (strlencmp(cmd, "lcd_enable") == 0) {
941 enable |= 0x01;
942 } else if (strlencmp(cmd, "lcd_disable") == 0) {
943 disable |= 0x01;
944 } else if (strlencmp(cmd, "crt_enable") == 0) {
945 enable |= 0x02;
946 } else if (strlencmp(cmd, "crt_disable") == 0) {
947 disable |= 0x02;
948 } else if (video_supported == IBMACPI_VIDEO_NEW &&
949 strlencmp(cmd, "dvi_enable") == 0) {
950 enable |= 0x08;
951 } else if (video_supported == IBMACPI_VIDEO_NEW &&
952 strlencmp(cmd, "dvi_disable") == 0) {
953 disable |= 0x08;
954 } else if (strlencmp(cmd, "auto_enable") == 0) {
955 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
956 return -EIO;
957 } else if (strlencmp(cmd, "auto_disable") == 0) {
958 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
959 return -EIO;
960 } else if (strlencmp(cmd, "video_switch") == 0) {
961 if (!video_switch())
962 return -EIO;
963 } else if (strlencmp(cmd, "expand_toggle") == 0) {
964 if (!video_expand())
965 return -EIO;
966 } else
967 return -EINVAL;
970 if (enable || disable) {
971 status = (video_status() & 0x0f & ~disable) | enable;
972 if (!video_switch2(status))
973 return -EIO;
976 return 0;
979 static void video_exit(void)
981 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
984 static int light_supported;
985 static int light_status_supported;
987 static int light_init(void)
989 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
990 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
992 if (light_supported)
993 /* light status not supported on
994 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
995 light_status_supported = acpi_evalf(ec_handle, NULL,
996 "KBLT", "qv");
998 return 0;
1001 static int light_read(char *p)
1003 int len = 0;
1004 int status = 0;
1006 if (!light_supported) {
1007 len += sprintf(p + len, "status:\t\tnot supported\n");
1008 } else if (!light_status_supported) {
1009 len += sprintf(p + len, "status:\t\tunknown\n");
1010 len += sprintf(p + len, "commands:\ton, off\n");
1011 } else {
1012 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1013 return -EIO;
1014 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1015 len += sprintf(p + len, "commands:\ton, off\n");
1018 return len;
1021 static int light_write(char *buf)
1023 int cmos_cmd, lght_cmd;
1024 char *cmd;
1025 int success;
1027 if (!light_supported)
1028 return -ENODEV;
1030 while ((cmd = next_cmd(&buf))) {
1031 if (strlencmp(cmd, "on") == 0) {
1032 cmos_cmd = 0x0c;
1033 lght_cmd = 1;
1034 } else if (strlencmp(cmd, "off") == 0) {
1035 cmos_cmd = 0x0d;
1036 lght_cmd = 0;
1037 } else
1038 return -EINVAL;
1040 success = cmos_handle ?
1041 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1042 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1043 if (!success)
1044 return -EIO;
1047 return 0;
1050 #if defined(CONFIG_ACPI_IBM_DOCK) || defined(CONFIG_ACPI_IBM_BAY)
1051 static int _sta(acpi_handle handle)
1053 int status;
1055 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1056 status = 0;
1058 return status;
1060 #endif
1062 #ifdef CONFIG_ACPI_IBM_DOCK
1063 #define dock_docked() (_sta(dock_handle) & 1)
1065 static int dock_read(char *p)
1067 int len = 0;
1068 int docked = dock_docked();
1070 if (!dock_handle)
1071 len += sprintf(p + len, "status:\t\tnot supported\n");
1072 else if (!docked)
1073 len += sprintf(p + len, "status:\t\tundocked\n");
1074 else {
1075 len += sprintf(p + len, "status:\t\tdocked\n");
1076 len += sprintf(p + len, "commands:\tdock, undock\n");
1079 return len;
1082 static int dock_write(char *buf)
1084 char *cmd;
1086 if (!dock_docked())
1087 return -ENODEV;
1089 while ((cmd = next_cmd(&buf))) {
1090 if (strlencmp(cmd, "undock") == 0) {
1091 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1092 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1093 return -EIO;
1094 } else if (strlencmp(cmd, "dock") == 0) {
1095 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1096 return -EIO;
1097 } else
1098 return -EINVAL;
1101 return 0;
1104 static void dock_notify(struct ibm_struct *ibm, u32 event)
1106 int docked = dock_docked();
1107 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1109 if (event == 1 && !pci) /* 570 */
1110 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1111 else if (event == 1 && pci) /* 570 */
1112 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1113 else if (event == 3 && docked)
1114 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1115 else if (event == 3 && !docked)
1116 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1117 else if (event == 0 && docked)
1118 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1119 else {
1120 printk(IBM_ERR "unknown dock event %d, status %d\n",
1121 event, _sta(dock_handle));
1122 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1125 #endif
1127 #ifdef CONFIG_ACPI_IBM_BAY
1128 static int bay_status_supported;
1129 static int bay_status2_supported;
1130 static int bay_eject_supported;
1131 static int bay_eject2_supported;
1133 static int bay_init(void)
1135 bay_status_supported = bay_handle &&
1136 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1137 bay_status2_supported = bay2_handle &&
1138 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1140 bay_eject_supported = bay_handle && bay_ej_handle &&
1141 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1142 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1143 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1145 return 0;
1148 #define bay_occupied(b) (_sta(b##_handle) & 1)
1150 static int bay_read(char *p)
1152 int len = 0;
1153 int occupied = bay_occupied(bay);
1154 int occupied2 = bay_occupied(bay2);
1155 int eject, eject2;
1157 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1158 (occupied ? "occupied" : "unoccupied") :
1159 "not supported");
1160 if (bay_status2_supported)
1161 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1162 "occupied" : "unoccupied");
1164 eject = bay_eject_supported && occupied;
1165 eject2 = bay_eject2_supported && occupied2;
1167 if (eject && eject2)
1168 len += sprintf(p + len, "commands:\teject, eject2\n");
1169 else if (eject)
1170 len += sprintf(p + len, "commands:\teject\n");
1171 else if (eject2)
1172 len += sprintf(p + len, "commands:\teject2\n");
1174 return len;
1177 static int bay_write(char *buf)
1179 char *cmd;
1181 if (!bay_eject_supported && !bay_eject2_supported)
1182 return -ENODEV;
1184 while ((cmd = next_cmd(&buf))) {
1185 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1186 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1187 return -EIO;
1188 } else if (bay_eject2_supported &&
1189 strlencmp(cmd, "eject2") == 0) {
1190 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1191 return -EIO;
1192 } else
1193 return -EINVAL;
1196 return 0;
1199 static void bay_notify(struct ibm_struct *ibm, u32 event)
1201 acpi_bus_generate_event(ibm->device, event, 0);
1203 #endif /* CONFIG_ACPI_IBM_BAY */
1205 static int cmos_read(char *p)
1207 int len = 0;
1209 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1210 R30, R31, T20-22, X20-21 */
1211 if (!cmos_handle)
1212 len += sprintf(p + len, "status:\t\tnot supported\n");
1213 else {
1214 len += sprintf(p + len, "status:\t\tsupported\n");
1215 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1218 return len;
1221 static int cmos_eval(int cmos_cmd)
1223 if (cmos_handle)
1224 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1225 else
1226 return 1;
1229 static int cmos_write(char *buf)
1231 char *cmd;
1232 int cmos_cmd;
1234 if (!cmos_handle)
1235 return -EINVAL;
1237 while ((cmd = next_cmd(&buf))) {
1238 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1239 cmos_cmd >= 0 && cmos_cmd <= 21) {
1240 /* cmos_cmd set */
1241 } else
1242 return -EINVAL;
1244 if (!cmos_eval(cmos_cmd))
1245 return -EIO;
1248 return 0;
1251 enum led_access_mode {
1252 IBMACPI_LED_NONE = 0,
1253 IBMACPI_LED_570, /* 570 */
1254 IBMACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1255 IBMACPI_LED_NEW, /* all others */
1257 static enum led_access_mode led_supported;
1259 static int led_init(void)
1261 if (!led_handle)
1262 /* led not supported on R30, R31 */
1263 led_supported = IBMACPI_LED_NONE;
1264 else if (strlencmp(led_path, "SLED") == 0)
1265 /* 570 */
1266 led_supported = IBMACPI_LED_570;
1267 else if (strlencmp(led_path, "SYSL") == 0)
1268 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1269 led_supported = IBMACPI_LED_OLD;
1270 else
1271 /* all others */
1272 led_supported = IBMACPI_LED_NEW;
1274 return 0;
1277 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1279 static int led_read(char *p)
1281 int len = 0;
1283 if (!led_supported) {
1284 len += sprintf(p + len, "status:\t\tnot supported\n");
1285 return len;
1287 len += sprintf(p + len, "status:\t\tsupported\n");
1289 if (led_supported == IBMACPI_LED_570) {
1290 /* 570 */
1291 int i, status;
1292 for (i = 0; i < 8; i++) {
1293 if (!acpi_evalf(ec_handle,
1294 &status, "GLED", "dd", 1 << i))
1295 return -EIO;
1296 len += sprintf(p + len, "%d:\t\t%s\n",
1297 i, led_status(status));
1301 len += sprintf(p + len, "commands:\t"
1302 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1304 return len;
1307 /* off, on, blink */
1308 static const int led_sled_arg1[] = { 0, 1, 3 };
1309 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1310 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1311 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1313 #define EC_HLCL 0x0c
1314 #define EC_HLBL 0x0d
1315 #define EC_HLMS 0x0e
1317 static int led_write(char *buf)
1319 char *cmd;
1320 int led, ind, ret;
1322 if (!led_supported)
1323 return -ENODEV;
1325 while ((cmd = next_cmd(&buf))) {
1326 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1327 return -EINVAL;
1329 if (strstr(cmd, "off")) {
1330 ind = 0;
1331 } else if (strstr(cmd, "on")) {
1332 ind = 1;
1333 } else if (strstr(cmd, "blink")) {
1334 ind = 2;
1335 } else
1336 return -EINVAL;
1338 if (led_supported == IBMACPI_LED_570) {
1339 /* 570 */
1340 led = 1 << led;
1341 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1342 led, led_sled_arg1[ind]))
1343 return -EIO;
1344 } else if (led_supported == IBMACPI_LED_OLD) {
1345 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1346 led = 1 << led;
1347 ret = ec_write(EC_HLMS, led);
1348 if (ret >= 0)
1349 ret =
1350 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1351 if (ret >= 0)
1352 ret =
1353 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1354 if (ret < 0)
1355 return ret;
1356 } else {
1357 /* all others */
1358 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1359 led, led_led_arg1[ind]))
1360 return -EIO;
1364 return 0;
1367 static int beep_read(char *p)
1369 int len = 0;
1371 if (!beep_handle)
1372 len += sprintf(p + len, "status:\t\tnot supported\n");
1373 else {
1374 len += sprintf(p + len, "status:\t\tsupported\n");
1375 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1378 return len;
1381 static int beep_write(char *buf)
1383 char *cmd;
1384 int beep_cmd;
1386 if (!beep_handle)
1387 return -ENODEV;
1389 while ((cmd = next_cmd(&buf))) {
1390 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1391 beep_cmd >= 0 && beep_cmd <= 17) {
1392 /* beep_cmd set */
1393 } else
1394 return -EINVAL;
1395 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1396 return -EIO;
1399 return 0;
1402 static int acpi_ec_read(int i, u8 * p)
1404 int v;
1406 if (ecrd_handle) {
1407 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1408 return 0;
1409 *p = v;
1410 } else {
1411 if (ec_read(i, p) < 0)
1412 return 0;
1415 return 1;
1418 static int acpi_ec_write(int i, u8 v)
1420 if (ecwr_handle) {
1421 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1422 return 0;
1423 } else {
1424 if (ec_write(i, v) < 0)
1425 return 0;
1428 return 1;
1431 static enum thermal_access_mode thermal_read_mode;
1433 static int thermal_init(void)
1435 u8 t, ta1, ta2;
1436 int i;
1437 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1439 if (ibm_thinkpad_ec_found && experimental) {
1441 * Direct EC access mode: sensors at registers
1442 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1443 * non-implemented, thermal sensors return 0x80 when
1444 * not available
1447 ta1 = ta2 = 0;
1448 for (i = 0; i < 8; i++) {
1449 if (likely(acpi_ec_read(0x78 + i, &t))) {
1450 ta1 |= t;
1451 } else {
1452 ta1 = 0;
1453 break;
1455 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1456 ta2 |= t;
1457 } else {
1458 ta1 = 0;
1459 break;
1462 if (ta1 == 0) {
1463 /* This is sheer paranoia, but we handle it anyway */
1464 if (acpi_tmp7) {
1465 printk(IBM_ERR
1466 "ThinkPad ACPI EC access misbehaving, "
1467 "falling back to ACPI TMPx access mode\n");
1468 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1469 } else {
1470 printk(IBM_ERR
1471 "ThinkPad ACPI EC access misbehaving, "
1472 "disabling thermal sensors access\n");
1473 thermal_read_mode = IBMACPI_THERMAL_NONE;
1475 } else {
1476 thermal_read_mode =
1477 (ta2 != 0) ?
1478 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1480 } else if (acpi_tmp7) {
1481 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1482 /* 600e/x, 770e, 770x */
1483 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1484 } else {
1485 /* Standard ACPI TMPx access, max 8 sensors */
1486 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1488 } else {
1489 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1490 thermal_read_mode = IBMACPI_THERMAL_NONE;
1493 return 0;
1496 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1498 int i, t;
1499 s8 tmp;
1500 char tmpi[] = "TMPi";
1502 if (!s)
1503 return -EINVAL;
1505 switch (thermal_read_mode) {
1506 #if IBMACPI_MAX_THERMAL_SENSORS >= 16
1507 case IBMACPI_THERMAL_TPEC_16:
1508 for (i = 0; i < 8; i++) {
1509 if (!acpi_ec_read(0xC0 + i, &tmp))
1510 return -EIO;
1511 s->temp[i + 8] = tmp * 1000;
1513 /* fallthrough */
1514 #endif
1515 case IBMACPI_THERMAL_TPEC_8:
1516 for (i = 0; i < 8; i++) {
1517 if (!acpi_ec_read(0x78 + i, &tmp))
1518 return -EIO;
1519 s->temp[i] = tmp * 1000;
1521 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1523 case IBMACPI_THERMAL_ACPI_UPDT:
1524 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1525 return -EIO;
1526 for (i = 0; i < 8; i++) {
1527 tmpi[3] = '0' + i;
1528 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1529 return -EIO;
1530 s->temp[i] = (t - 2732) * 100;
1532 return 8;
1534 case IBMACPI_THERMAL_ACPI_TMP07:
1535 for (i = 0; i < 8; i++) {
1536 tmpi[3] = '0' + i;
1537 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1538 return -EIO;
1539 s->temp[i] = t * 1000;
1541 return 8;
1543 case IBMACPI_THERMAL_NONE:
1544 default:
1545 return 0;
1549 static int thermal_read(char *p)
1551 int len = 0;
1552 int n, i;
1553 struct ibm_thermal_sensors_struct t;
1555 n = thermal_get_sensors(&t);
1556 if (unlikely(n < 0))
1557 return n;
1559 len += sprintf(p + len, "temperatures:\t");
1561 if (n > 0) {
1562 for (i = 0; i < (n - 1); i++)
1563 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1564 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1565 } else
1566 len += sprintf(p + len, "not supported\n");
1568 return len;
1571 static u8 ecdump_regs[256];
1573 static int ecdump_read(char *p)
1575 int len = 0;
1576 int i, j;
1577 u8 v;
1579 len += sprintf(p + len, "EC "
1580 " +00 +01 +02 +03 +04 +05 +06 +07"
1581 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1582 for (i = 0; i < 256; i += 16) {
1583 len += sprintf(p + len, "EC 0x%02x:", i);
1584 for (j = 0; j < 16; j++) {
1585 if (!acpi_ec_read(i + j, &v))
1586 break;
1587 if (v != ecdump_regs[i + j])
1588 len += sprintf(p + len, " *%02x", v);
1589 else
1590 len += sprintf(p + len, " %02x", v);
1591 ecdump_regs[i + j] = v;
1593 len += sprintf(p + len, "\n");
1594 if (j != 16)
1595 break;
1598 /* These are way too dangerous to advertise openly... */
1599 #if 0
1600 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1601 " (<offset> is 00-ff, <value> is 00-ff)\n");
1602 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1603 " (<offset> is 00-ff, <value> is 0-255)\n");
1604 #endif
1605 return len;
1608 static int ecdump_write(char *buf)
1610 char *cmd;
1611 int i, v;
1613 while ((cmd = next_cmd(&buf))) {
1614 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1615 /* i and v set */
1616 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1617 /* i and v set */
1618 } else
1619 return -EINVAL;
1620 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1621 if (!acpi_ec_write(i, v))
1622 return -EIO;
1623 } else
1624 return -EINVAL;
1627 return 0;
1630 static int brightness_offset = 0x31;
1632 static int brightness_get(struct backlight_device *bd)
1634 u8 level;
1635 if (!acpi_ec_read(brightness_offset, &level))
1636 return -EIO;
1638 level &= 0x7;
1640 return level;
1643 static int brightness_read(char *p)
1645 int len = 0;
1646 int level;
1648 if ((level = brightness_get(NULL)) < 0) {
1649 len += sprintf(p + len, "level:\t\tunreadable\n");
1650 } else {
1651 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1652 len += sprintf(p + len, "commands:\tup, down\n");
1653 len += sprintf(p + len, "commands:\tlevel <level>"
1654 " (<level> is 0-7)\n");
1657 return len;
1660 #define BRIGHTNESS_UP 4
1661 #define BRIGHTNESS_DOWN 5
1663 static int brightness_set(int value)
1665 int cmos_cmd, inc, i;
1666 int current_value = brightness_get(NULL);
1668 value &= 7;
1670 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1671 inc = value > current_value ? 1 : -1;
1672 for (i = current_value; i != value; i += inc) {
1673 if (!cmos_eval(cmos_cmd))
1674 return -EIO;
1675 if (!acpi_ec_write(brightness_offset, i + inc))
1676 return -EIO;
1679 return 0;
1682 static int brightness_write(char *buf)
1684 int level;
1685 int new_level;
1686 char *cmd;
1688 while ((cmd = next_cmd(&buf))) {
1689 if ((level = brightness_get(NULL)) < 0)
1690 return level;
1691 level &= 7;
1693 if (strlencmp(cmd, "up") == 0) {
1694 new_level = level == 7 ? 7 : level + 1;
1695 } else if (strlencmp(cmd, "down") == 0) {
1696 new_level = level == 0 ? 0 : level - 1;
1697 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1698 new_level >= 0 && new_level <= 7) {
1699 /* new_level set */
1700 } else
1701 return -EINVAL;
1703 brightness_set(new_level);
1706 return 0;
1709 static int brightness_update_status(struct backlight_device *bd)
1711 return brightness_set(
1712 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
1713 bd->props->power == FB_BLANK_UNBLANK) ?
1714 bd->props->brightness : 0);
1717 static struct backlight_properties ibm_backlight_data = {
1718 .owner = THIS_MODULE,
1719 .get_brightness = brightness_get,
1720 .update_status = brightness_update_status,
1721 .max_brightness = 7,
1724 static int brightness_init(void)
1726 int b;
1728 b = brightness_get(NULL);
1729 if (b < 0)
1730 return b;
1732 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
1733 &ibm_backlight_data);
1734 if (IS_ERR(ibm_backlight_device)) {
1735 printk(IBM_ERR "Could not register backlight device\n");
1736 return PTR_ERR(ibm_backlight_device);
1739 ibm_backlight_device->props->brightness = b;
1740 brightness_update_status(ibm_backlight_device);
1742 return 0;
1745 static void brightness_exit(void)
1747 if (ibm_backlight_device) {
1748 backlight_device_unregister(ibm_backlight_device);
1749 ibm_backlight_device = NULL;
1753 static int volume_offset = 0x30;
1755 static int volume_read(char *p)
1757 int len = 0;
1758 u8 level;
1760 if (!acpi_ec_read(volume_offset, &level)) {
1761 len += sprintf(p + len, "level:\t\tunreadable\n");
1762 } else {
1763 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1764 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1765 len += sprintf(p + len, "commands:\tup, down, mute\n");
1766 len += sprintf(p + len, "commands:\tlevel <level>"
1767 " (<level> is 0-15)\n");
1770 return len;
1773 #define VOLUME_DOWN 0
1774 #define VOLUME_UP 1
1775 #define VOLUME_MUTE 2
1777 static int volume_write(char *buf)
1779 int cmos_cmd, inc, i;
1780 u8 level, mute;
1781 int new_level, new_mute;
1782 char *cmd;
1784 while ((cmd = next_cmd(&buf))) {
1785 if (!acpi_ec_read(volume_offset, &level))
1786 return -EIO;
1787 new_mute = mute = level & 0x40;
1788 new_level = level = level & 0xf;
1790 if (strlencmp(cmd, "up") == 0) {
1791 if (mute)
1792 new_mute = 0;
1793 else
1794 new_level = level == 15 ? 15 : level + 1;
1795 } else if (strlencmp(cmd, "down") == 0) {
1796 if (mute)
1797 new_mute = 0;
1798 else
1799 new_level = level == 0 ? 0 : level - 1;
1800 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1801 new_level >= 0 && new_level <= 15) {
1802 /* new_level set */
1803 } else if (strlencmp(cmd, "mute") == 0) {
1804 new_mute = 0x40;
1805 } else
1806 return -EINVAL;
1808 if (new_level != level) { /* mute doesn't change */
1809 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1810 inc = new_level > level ? 1 : -1;
1812 if (mute && (!cmos_eval(cmos_cmd) ||
1813 !acpi_ec_write(volume_offset, level)))
1814 return -EIO;
1816 for (i = level; i != new_level; i += inc)
1817 if (!cmos_eval(cmos_cmd) ||
1818 !acpi_ec_write(volume_offset, i + inc))
1819 return -EIO;
1821 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1822 !acpi_ec_write(volume_offset,
1823 new_level + mute)))
1824 return -EIO;
1827 if (new_mute != mute) { /* level doesn't change */
1828 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1830 if (!cmos_eval(cmos_cmd) ||
1831 !acpi_ec_write(volume_offset, level + new_mute))
1832 return -EIO;
1836 return 0;
1839 static enum fan_status_access_mode fan_status_access_mode;
1840 static enum fan_control_access_mode fan_control_access_mode;
1841 static enum fan_control_commands fan_control_commands;
1843 static int fan_control_status_known;
1844 static u8 fan_control_initial_status;
1846 static void fan_watchdog_fire(struct work_struct *ignored);
1847 static int fan_watchdog_maxinterval;
1848 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
1850 static int fan_init(void)
1852 fan_status_access_mode = IBMACPI_FAN_NONE;
1853 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1854 fan_control_commands = 0;
1855 fan_control_status_known = 1;
1856 fan_watchdog_maxinterval = 0;
1858 if (gfan_handle) {
1859 /* 570, 600e/x, 770e, 770x */
1860 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1861 } else {
1862 /* all other ThinkPads: note that even old-style
1863 * ThinkPad ECs supports the fan control register */
1864 if (likely(acpi_ec_read(fan_status_offset,
1865 &fan_control_initial_status))) {
1866 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1868 /* In some ThinkPads, neither the EC nor the ACPI
1869 * DSDT initialize the fan status, and it ends up
1870 * being set to 0x07 when it *could* be either
1871 * 0x07 or 0x80.
1873 * Enable for TP-1Y (T43), TP-78 (R51e),
1874 * TP-76 (R52), TP-70 (T43, R52), which are known
1875 * to be buggy. */
1876 if (fan_control_initial_status == 0x07 &&
1877 ibm_thinkpad_ec_found &&
1878 ((ibm_thinkpad_ec_found[0] == '1' &&
1879 ibm_thinkpad_ec_found[1] == 'Y') ||
1880 (ibm_thinkpad_ec_found[0] == '7' &&
1881 (ibm_thinkpad_ec_found[1] == '6' ||
1882 ibm_thinkpad_ec_found[1] == '8' ||
1883 ibm_thinkpad_ec_found[1] == '0'))
1884 )) {
1885 printk(IBM_NOTICE
1886 "fan_init: initial fan status is "
1887 "unknown, assuming it is in auto "
1888 "mode\n");
1889 fan_control_status_known = 0;
1891 } else {
1892 printk(IBM_ERR
1893 "ThinkPad ACPI EC access misbehaving, "
1894 "fan status and control unavailable\n");
1895 return 0;
1899 if (sfan_handle) {
1900 /* 570, 770x-JL */
1901 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
1902 fan_control_commands |=
1903 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
1904 } else {
1905 if (!gfan_handle) {
1906 /* gfan without sfan means no fan control */
1907 /* all other models implement TP EC 0x2f control */
1909 if (fans_handle) {
1910 /* X31, X40, X41 */
1911 fan_control_access_mode =
1912 IBMACPI_FAN_WR_ACPI_FANS;
1913 fan_control_commands |=
1914 IBMACPI_FAN_CMD_SPEED |
1915 IBMACPI_FAN_CMD_LEVEL |
1916 IBMACPI_FAN_CMD_ENABLE;
1917 } else {
1918 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
1919 fan_control_commands |=
1920 IBMACPI_FAN_CMD_LEVEL |
1921 IBMACPI_FAN_CMD_ENABLE;
1926 return 0;
1929 static int fan_get_status(u8 *status)
1931 u8 s;
1933 /* TODO:
1934 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1936 switch (fan_status_access_mode) {
1937 case IBMACPI_FAN_RD_ACPI_GFAN:
1938 /* 570, 600e/x, 770e, 770x */
1940 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
1941 return -EIO;
1943 if (likely(status))
1944 *status = s & 0x07;
1946 break;
1948 case IBMACPI_FAN_RD_TPEC:
1949 /* all except 570, 600e/x, 770e, 770x */
1950 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1951 return -EIO;
1953 if (likely(status))
1954 *status = s;
1956 break;
1958 default:
1959 return -ENXIO;
1962 return 0;
1965 static int fan_get_speed(unsigned int *speed)
1967 u8 hi, lo;
1969 switch (fan_status_access_mode) {
1970 case IBMACPI_FAN_RD_TPEC:
1971 /* all except 570, 600e/x, 770e, 770x */
1972 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1973 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1974 return -EIO;
1976 if (likely(speed))
1977 *speed = (hi << 8) | lo;
1979 break;
1981 default:
1982 return -ENXIO;
1985 return 0;
1988 static void fan_exit(void)
1990 cancel_delayed_work(&fan_watchdog_task);
1991 flush_scheduled_work();
1994 static void fan_watchdog_reset(void)
1996 static int fan_watchdog_active = 0;
1998 if (fan_watchdog_active)
1999 cancel_delayed_work(&fan_watchdog_task);
2001 if (fan_watchdog_maxinterval > 0) {
2002 fan_watchdog_active = 1;
2003 if (!schedule_delayed_work(&fan_watchdog_task,
2004 msecs_to_jiffies(fan_watchdog_maxinterval
2005 * 1000))) {
2006 printk(IBM_ERR "failed to schedule the fan watchdog, "
2007 "watchdog will not trigger\n");
2009 } else
2010 fan_watchdog_active = 0;
2013 static int fan_read(char *p)
2015 int len = 0;
2016 int rc;
2017 u8 status;
2018 unsigned int speed = 0;
2020 switch (fan_status_access_mode) {
2021 case IBMACPI_FAN_RD_ACPI_GFAN:
2022 /* 570, 600e/x, 770e, 770x */
2023 if ((rc = fan_get_status(&status)) < 0)
2024 return rc;
2026 len += sprintf(p + len, "status:\t\t%s\n"
2027 "level:\t\t%d\n",
2028 (status != 0) ? "enabled" : "disabled", status);
2029 break;
2031 case IBMACPI_FAN_RD_TPEC:
2032 /* all except 570, 600e/x, 770e, 770x */
2033 if ((rc = fan_get_status(&status)) < 0)
2034 return rc;
2036 if (unlikely(!fan_control_status_known)) {
2037 if (status != fan_control_initial_status)
2038 fan_control_status_known = 1;
2039 else
2040 /* Return most likely status. In fact, it
2041 * might be the only possible status */
2042 status = IBMACPI_FAN_EC_AUTO;
2045 len += sprintf(p + len, "status:\t\t%s\n",
2046 (status != 0) ? "enabled" : "disabled");
2048 /* No ThinkPad boots on disengaged mode, we can safely
2049 * assume the tachometer is online if fan control status
2050 * was unknown */
2051 if ((rc = fan_get_speed(&speed)) < 0)
2052 return rc;
2054 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2056 if (status & IBMACPI_FAN_EC_DISENGAGED)
2057 /* Disengaged mode takes precedence */
2058 len += sprintf(p + len, "level:\t\tdisengaged\n");
2059 else if (status & IBMACPI_FAN_EC_AUTO)
2060 len += sprintf(p + len, "level:\t\tauto\n");
2061 else
2062 len += sprintf(p + len, "level:\t\t%d\n", status);
2063 break;
2065 case IBMACPI_FAN_NONE:
2066 default:
2067 len += sprintf(p + len, "status:\t\tnot supported\n");
2070 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2071 len += sprintf(p + len, "commands:\tlevel <level>");
2073 switch (fan_control_access_mode) {
2074 case IBMACPI_FAN_WR_ACPI_SFAN:
2075 len += sprintf(p + len, " (<level> is 0-7)\n");
2076 break;
2078 default:
2079 len += sprintf(p + len, " (<level> is 0-7, "
2080 "auto, disengaged)\n");
2081 break;
2085 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
2086 len += sprintf(p + len, "commands:\tenable, disable\n"
2087 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2088 "1-120 (seconds))\n");
2090 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
2091 len += sprintf(p + len, "commands:\tspeed <speed>"
2092 " (<speed> is 0-65535)\n");
2094 return len;
2097 static int fan_set_level(int level)
2099 switch (fan_control_access_mode) {
2100 case IBMACPI_FAN_WR_ACPI_SFAN:
2101 if (level >= 0 && level <= 7) {
2102 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2103 return -EIO;
2104 } else
2105 return -EINVAL;
2106 break;
2108 case IBMACPI_FAN_WR_ACPI_FANS:
2109 case IBMACPI_FAN_WR_TPEC:
2110 if ((level != IBMACPI_FAN_EC_AUTO) &&
2111 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2112 ((level < 0) || (level > 7)))
2113 return -EINVAL;
2115 if (!acpi_ec_write(fan_status_offset, level))
2116 return -EIO;
2117 else
2118 fan_control_status_known = 1;
2119 break;
2121 default:
2122 return -ENXIO;
2124 return 0;
2127 static int fan_set_enable(void)
2129 u8 s;
2130 int rc;
2132 switch (fan_control_access_mode) {
2133 case IBMACPI_FAN_WR_ACPI_FANS:
2134 case IBMACPI_FAN_WR_TPEC:
2135 if ((rc = fan_get_status(&s)) < 0)
2136 return rc;
2138 /* Don't go out of emergency fan mode */
2139 if (s != 7)
2140 s = IBMACPI_FAN_EC_AUTO;
2142 if (!acpi_ec_write(fan_status_offset, s))
2143 return -EIO;
2144 else
2145 fan_control_status_known = 1;
2146 break;
2148 case IBMACPI_FAN_WR_ACPI_SFAN:
2149 if ((rc = fan_get_status(&s)) < 0)
2150 return rc;
2152 s &= 0x07;
2154 /* Set fan to at least level 4 */
2155 if (s < 4)
2156 s = 4;
2158 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2159 return -EIO;
2160 break;
2162 default:
2163 return -ENXIO;
2165 return 0;
2168 static int fan_set_disable(void)
2170 switch (fan_control_access_mode) {
2171 case IBMACPI_FAN_WR_ACPI_FANS:
2172 case IBMACPI_FAN_WR_TPEC:
2173 if (!acpi_ec_write(fan_status_offset, 0x00))
2174 return -EIO;
2175 else
2176 fan_control_status_known = 1;
2177 break;
2179 case IBMACPI_FAN_WR_ACPI_SFAN:
2180 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2181 return -EIO;
2182 break;
2184 default:
2185 return -ENXIO;
2187 return 0;
2190 static int fan_set_speed(int speed)
2192 switch (fan_control_access_mode) {
2193 case IBMACPI_FAN_WR_ACPI_FANS:
2194 if (speed >= 0 && speed <= 65535) {
2195 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2196 speed, speed, speed))
2197 return -EIO;
2198 } else
2199 return -EINVAL;
2200 break;
2202 default:
2203 return -ENXIO;
2205 return 0;
2208 static int fan_write_cmd_level(const char *cmd, int *rc)
2210 int level;
2212 if (strlencmp(cmd, "level auto") == 0)
2213 level = IBMACPI_FAN_EC_AUTO;
2214 else if (strlencmp(cmd, "level disengaged") == 0)
2215 level = IBMACPI_FAN_EC_DISENGAGED;
2216 else if (sscanf(cmd, "level %d", &level) != 1)
2217 return 0;
2219 if ((*rc = fan_set_level(level)) == -ENXIO)
2220 printk(IBM_ERR "level command accepted for unsupported "
2221 "access mode %d", fan_control_access_mode);
2223 return 1;
2226 static int fan_write_cmd_enable(const char *cmd, int *rc)
2228 if (strlencmp(cmd, "enable") != 0)
2229 return 0;
2231 if ((*rc = fan_set_enable()) == -ENXIO)
2232 printk(IBM_ERR "enable command accepted for unsupported "
2233 "access mode %d", fan_control_access_mode);
2235 return 1;
2238 static int fan_write_cmd_disable(const char *cmd, int *rc)
2240 if (strlencmp(cmd, "disable") != 0)
2241 return 0;
2243 if ((*rc = fan_set_disable()) == -ENXIO)
2244 printk(IBM_ERR "disable command accepted for unsupported "
2245 "access mode %d", fan_control_access_mode);
2247 return 1;
2250 static int fan_write_cmd_speed(const char *cmd, int *rc)
2252 int speed;
2254 /* TODO:
2255 * Support speed <low> <medium> <high> ? */
2257 if (sscanf(cmd, "speed %d", &speed) != 1)
2258 return 0;
2260 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2261 printk(IBM_ERR "speed command accepted for unsupported "
2262 "access mode %d", fan_control_access_mode);
2264 return 1;
2267 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2269 int interval;
2271 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2272 return 0;
2274 if (interval < 0 || interval > 120)
2275 *rc = -EINVAL;
2276 else
2277 fan_watchdog_maxinterval = interval;
2279 return 1;
2282 static int fan_write(char *buf)
2284 char *cmd;
2285 int rc = 0;
2287 while (!rc && (cmd = next_cmd(&buf))) {
2288 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2289 fan_write_cmd_level(cmd, &rc)) &&
2290 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2291 (fan_write_cmd_enable(cmd, &rc) ||
2292 fan_write_cmd_disable(cmd, &rc) ||
2293 fan_write_cmd_watchdog(cmd, &rc))) &&
2294 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2295 fan_write_cmd_speed(cmd, &rc))
2297 rc = -EINVAL;
2298 else if (!rc)
2299 fan_watchdog_reset();
2302 return rc;
2305 static void fan_watchdog_fire(struct work_struct *ignored)
2307 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2308 if (fan_set_enable()) {
2309 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2310 /* reschedule for later */
2311 fan_watchdog_reset();
2315 static struct ibm_struct ibms[] = {
2317 .name = "driver",
2318 .init = ibm_acpi_driver_init,
2319 .read = driver_read,
2322 .name = "hotkey",
2323 .hid = IBM_HKEY_HID,
2324 .init = hotkey_init,
2325 .read = hotkey_read,
2326 .write = hotkey_write,
2327 .exit = hotkey_exit,
2328 .notify = hotkey_notify,
2329 .handle = &hkey_handle,
2330 .type = ACPI_DEVICE_NOTIFY,
2333 .name = "bluetooth",
2334 .init = bluetooth_init,
2335 .read = bluetooth_read,
2336 .write = bluetooth_write,
2339 .name = "wan",
2340 .init = wan_init,
2341 .read = wan_read,
2342 .write = wan_write,
2343 .experimental = 1,
2346 .name = "video",
2347 .init = video_init,
2348 .read = video_read,
2349 .write = video_write,
2350 .exit = video_exit,
2353 .name = "light",
2354 .init = light_init,
2355 .read = light_read,
2356 .write = light_write,
2358 #ifdef CONFIG_ACPI_IBM_DOCK
2360 .name = "dock",
2361 .read = dock_read,
2362 .write = dock_write,
2363 .notify = dock_notify,
2364 .handle = &dock_handle,
2365 .type = ACPI_SYSTEM_NOTIFY,
2368 .name = "dock",
2369 .hid = IBM_PCI_HID,
2370 .notify = dock_notify,
2371 .handle = &pci_handle,
2372 .type = ACPI_SYSTEM_NOTIFY,
2374 #endif
2375 #ifdef CONFIG_ACPI_IBM_BAY
2377 .name = "bay",
2378 .init = bay_init,
2379 .read = bay_read,
2380 .write = bay_write,
2381 .notify = bay_notify,
2382 .handle = &bay_handle,
2383 .type = ACPI_SYSTEM_NOTIFY,
2385 #endif /* CONFIG_ACPI_IBM_BAY */
2387 .name = "cmos",
2388 .read = cmos_read,
2389 .write = cmos_write,
2392 .name = "led",
2393 .init = led_init,
2394 .read = led_read,
2395 .write = led_write,
2398 .name = "beep",
2399 .read = beep_read,
2400 .write = beep_write,
2403 .name = "thermal",
2404 .init = thermal_init,
2405 .read = thermal_read,
2408 .name = "ecdump",
2409 .read = ecdump_read,
2410 .write = ecdump_write,
2411 .experimental = 1,
2414 .name = "brightness",
2415 .read = brightness_read,
2416 .write = brightness_write,
2417 .init = brightness_init,
2418 .exit = brightness_exit,
2421 .name = "volume",
2422 .read = volume_read,
2423 .write = volume_write,
2426 .name = "fan",
2427 .read = fan_read,
2428 .write = fan_write,
2429 .init = fan_init,
2430 .exit = fan_exit,
2431 .experimental = 1,
2435 static int dispatch_read(char *page, char **start, off_t off, int count,
2436 int *eof, void *data)
2438 struct ibm_struct *ibm = data;
2439 int len;
2441 if (!ibm || !ibm->read)
2442 return -EINVAL;
2444 len = ibm->read(page);
2445 if (len < 0)
2446 return len;
2448 if (len <= off + count)
2449 *eof = 1;
2450 *start = page + off;
2451 len -= off;
2452 if (len > count)
2453 len = count;
2454 if (len < 0)
2455 len = 0;
2457 return len;
2460 static int dispatch_write(struct file *file, const char __user * userbuf,
2461 unsigned long count, void *data)
2463 struct ibm_struct *ibm = data;
2464 char *kernbuf;
2465 int ret;
2467 if (!ibm || !ibm->write)
2468 return -EINVAL;
2470 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2471 if (!kernbuf)
2472 return -ENOMEM;
2474 if (copy_from_user(kernbuf, userbuf, count)) {
2475 kfree(kernbuf);
2476 return -EFAULT;
2479 kernbuf[count] = 0;
2480 strcat(kernbuf, ",");
2481 ret = ibm->write(kernbuf);
2482 if (ret == 0)
2483 ret = count;
2485 kfree(kernbuf);
2487 return ret;
2490 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2492 struct ibm_struct *ibm = data;
2494 if (!ibm || !ibm->notify)
2495 return;
2497 ibm->notify(ibm, event);
2500 static int __init setup_notify(struct ibm_struct *ibm)
2502 acpi_status status;
2503 int ret;
2505 if (!*ibm->handle)
2506 return 0;
2508 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2509 if (ret < 0) {
2510 printk(IBM_ERR "%s device not present\n", ibm->name);
2511 return -ENODEV;
2514 acpi_driver_data(ibm->device) = ibm;
2515 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2517 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2518 dispatch_notify, ibm);
2519 if (ACPI_FAILURE(status)) {
2520 if (status == AE_ALREADY_EXISTS) {
2521 printk(IBM_NOTICE "another device driver is already handling %s events\n",
2522 ibm->name);
2523 } else {
2524 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2525 ibm->name, status);
2527 return -ENODEV;
2529 ibm->notify_installed = 1;
2530 return 0;
2533 static int __init ibm_device_add(struct acpi_device *device)
2535 return 0;
2538 static int __init register_driver(struct ibm_struct *ibm)
2540 int ret;
2542 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2543 if (!ibm->driver) {
2544 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2545 return -1;
2548 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
2549 ibm->driver->ids = ibm->hid;
2550 ibm->driver->ops.add = &ibm_device_add;
2552 ret = acpi_bus_register_driver(ibm->driver);
2553 if (ret < 0) {
2554 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2555 ibm->hid, ret);
2556 kfree(ibm->driver);
2559 return ret;
2562 static void ibm_exit(struct ibm_struct *ibm);
2564 static int __init ibm_init(struct ibm_struct *ibm)
2566 int ret;
2567 struct proc_dir_entry *entry;
2569 if (ibm->experimental && !experimental)
2570 return 0;
2572 if (ibm->hid) {
2573 ret = register_driver(ibm);
2574 if (ret < 0)
2575 return ret;
2576 ibm->driver_registered = 1;
2579 if (ibm->init) {
2580 ret = ibm->init();
2581 if (ret != 0)
2582 return ret;
2583 ibm->init_called = 1;
2586 if (ibm->read) {
2587 entry = create_proc_entry(ibm->name,
2588 S_IFREG | S_IRUGO | S_IWUSR,
2589 proc_dir);
2590 if (!entry) {
2591 printk(IBM_ERR "unable to create proc entry %s\n",
2592 ibm->name);
2593 return -ENODEV;
2595 entry->owner = THIS_MODULE;
2596 entry->data = ibm;
2597 entry->read_proc = &dispatch_read;
2598 if (ibm->write)
2599 entry->write_proc = &dispatch_write;
2600 ibm->proc_created = 1;
2603 if (ibm->notify) {
2604 ret = setup_notify(ibm);
2605 if (ret == -ENODEV) {
2606 printk(IBM_NOTICE "disabling subdriver %s\n",
2607 ibm->name);
2608 ibm_exit(ibm);
2609 return 0;
2611 if (ret < 0)
2612 return ret;
2615 return 0;
2618 static void ibm_exit(struct ibm_struct *ibm)
2620 if (ibm->notify_installed)
2621 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2622 dispatch_notify);
2624 if (ibm->proc_created)
2625 remove_proc_entry(ibm->name, proc_dir);
2627 if (ibm->init_called && ibm->exit)
2628 ibm->exit();
2630 if (ibm->driver_registered) {
2631 acpi_bus_unregister_driver(ibm->driver);
2632 kfree(ibm->driver);
2636 static void __init ibm_handle_init(char *name,
2637 acpi_handle * handle, acpi_handle parent,
2638 char **paths, int num_paths, char **path)
2640 int i;
2641 acpi_status status;
2643 for (i = 0; i < num_paths; i++) {
2644 status = acpi_get_handle(parent, paths[i], handle);
2645 if (ACPI_SUCCESS(status)) {
2646 *path = paths[i];
2647 return;
2651 *handle = NULL;
2654 #define IBM_HANDLE_INIT(object) \
2655 ibm_handle_init(#object, &object##_handle, *object##_parent, \
2656 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
2658 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2660 unsigned int i;
2662 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2663 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2664 if (strlen(val) > sizeof(ibms[i].param) - 2)
2665 return -ENOSPC;
2666 strcpy(ibms[i].param, val);
2667 strcat(ibms[i].param, ",");
2668 return 0;
2671 return -EINVAL;
2674 #define IBM_PARAM(feature) \
2675 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2677 IBM_PARAM(hotkey);
2678 IBM_PARAM(bluetooth);
2679 IBM_PARAM(video);
2680 IBM_PARAM(light);
2681 #ifdef CONFIG_ACPI_IBM_DOCK
2682 IBM_PARAM(dock);
2683 #endif
2684 #ifdef CONFIG_ACPI_IBM_BAY
2685 IBM_PARAM(bay);
2686 #endif /* CONFIG_ACPI_IBM_BAY */
2687 IBM_PARAM(cmos);
2688 IBM_PARAM(led);
2689 IBM_PARAM(beep);
2690 IBM_PARAM(ecdump);
2691 IBM_PARAM(brightness);
2692 IBM_PARAM(volume);
2693 IBM_PARAM(fan);
2695 static void acpi_ibm_exit(void)
2697 int i;
2699 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2700 ibm_exit(&ibms[i]);
2702 if (proc_dir)
2703 remove_proc_entry(IBM_DIR, acpi_root_dir);
2705 if (ibm_thinkpad_ec_found)
2706 kfree(ibm_thinkpad_ec_found);
2709 static char* __init check_dmi_for_ec(void)
2711 struct dmi_device *dev = NULL;
2712 char ec_fw_string[18];
2715 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2716 * X32 or newer, all Z series; Some models must have an
2717 * up-to-date BIOS or they will not be detected.
2719 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2721 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2722 if (sscanf(dev->name,
2723 "IBM ThinkPad Embedded Controller -[%17c",
2724 ec_fw_string) == 1) {
2725 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2726 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2727 return kstrdup(ec_fw_string, GFP_KERNEL);
2730 return NULL;
2733 static int __init acpi_ibm_init(void)
2735 int ret, i;
2737 if (acpi_disabled)
2738 return -ENODEV;
2740 if (!acpi_specific_hotkey_enabled) {
2741 printk(IBM_ERR "using generic hotkey driver\n");
2742 return -ENODEV;
2745 /* ec is required because many other handles are relative to it */
2746 IBM_HANDLE_INIT(ec);
2747 if (!ec_handle) {
2748 printk(IBM_ERR "ec object not found\n");
2749 return -ENODEV;
2752 /* Models with newer firmware report the EC in DMI */
2753 ibm_thinkpad_ec_found = check_dmi_for_ec();
2755 /* these handles are not required */
2756 IBM_HANDLE_INIT(vid);
2757 IBM_HANDLE_INIT(vid2);
2758 IBM_HANDLE_INIT(ledb);
2759 IBM_HANDLE_INIT(led);
2760 IBM_HANDLE_INIT(hkey);
2761 IBM_HANDLE_INIT(lght);
2762 IBM_HANDLE_INIT(cmos);
2763 #ifdef CONFIG_ACPI_IBM_DOCK
2764 IBM_HANDLE_INIT(dock);
2765 #endif
2766 IBM_HANDLE_INIT(pci);
2767 #ifdef CONFIG_ACPI_IBM_BAY
2768 IBM_HANDLE_INIT(bay);
2769 if (bay_handle)
2770 IBM_HANDLE_INIT(bay_ej);
2771 IBM_HANDLE_INIT(bay2);
2772 if (bay2_handle)
2773 IBM_HANDLE_INIT(bay2_ej);
2774 #endif /* CONFIG_ACPI_IBM_BAY */
2775 IBM_HANDLE_INIT(beep);
2776 IBM_HANDLE_INIT(ecrd);
2777 IBM_HANDLE_INIT(ecwr);
2778 IBM_HANDLE_INIT(fans);
2779 IBM_HANDLE_INIT(gfan);
2780 IBM_HANDLE_INIT(sfan);
2782 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2783 if (!proc_dir) {
2784 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2785 acpi_ibm_exit();
2786 return -ENODEV;
2788 proc_dir->owner = THIS_MODULE;
2790 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
2791 ret = ibm_init(&ibms[i]);
2792 if (ret >= 0 && *ibms[i].param)
2793 ret = ibms[i].write(ibms[i].param);
2794 if (ret < 0) {
2795 acpi_ibm_exit();
2796 return ret;
2800 return 0;
2803 module_init(acpi_ibm_init);
2804 module_exit(acpi_ibm_exit);