ACPI: ibm-acpi: extend fan status functions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / ibm_acpi.c
blobb6ad2ed5fd6ce1dd76d75212abcc484a860da513
1 /*
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"
25 * Changelog:
27 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
28 * 2005-03-17 0.11 support for 600e, 770x
29 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
30 * support for 770e, G41
31 * G40 and G41 don't have a thinklight
32 * temperatures no longer experimental
33 * experimental brightness control
34 * experimental volume control
35 * experimental fan enable/disable
36 * 2005-01-16 0.10 fix module loading on R30, R31
37 * 2005-01-16 0.9 support for 570, R30, R31
38 * ultrabay support on A22p, A3x
39 * limit arg for cmos, led, beep, drop experimental status
40 * more capable led control on A21e, A22p, T20-22, X20
41 * experimental temperatures and fan speed
42 * experimental embedded controller register dump
43 * mark more functions as __init, drop incorrect __exit
44 * use MODULE_VERSION
45 * thanks to Henrik Brix Andersen <brix@gentoo.org>
46 * fix parameter passing on module loading
47 * thanks to Rusty Russell <rusty@rustcorp.com.au>
48 * thanks to Jim Radford <radford@blackbean.org>
49 * 2004-11-08 0.8 fix init error case, don't return from a macro
50 * thanks to Chris Wright <chrisw@osdl.org>
51 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
52 * fix led control on A21e
53 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
54 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
55 * proc file format changed
56 * video_switch command
57 * experimental cmos control
58 * experimental led control
59 * experimental acpi sounds
60 * 2004-09-16 0.4 support for module parameters
61 * hotkey mask can be prefixed by 0x
62 * video output switching
63 * video expansion control
64 * ultrabay eject support
65 * removed lcd brightness/on/off control, didn't work
66 * 2004-08-17 0.3 support for R40
67 * lcd off, brightness control
68 * thinklight on/off
69 * 2004-08-14 0.2 support for T series, X20
70 * bluetooth enable/disable
71 * hotkey events disabled by default
72 * removed fan control, currently useless
73 * 2004-08-09 0.1 initial release, support for X series
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
126 * G40, G41
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:
133 * 240, 240x, i1400
135 * Still missing DSDTs for the following models:
137 * A20p, A22e, A22m
138 * R52
139 * S31
140 * T43p
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 */
157 ); /* R30, R31 */
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 */
164 ); /* all others */
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 */
171 #endif
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 */
183 ); /* all others */
185 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
186 "_EJ0", /* 770x */
187 ); /* all others */
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 */
195 ); /* 570 */
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 */
203 ); /* R30, R31 */
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 */
212 ); /* all others */
214 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
215 "JFNS", /* 770x-JL */
216 ); /* all others */
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];
235 * FAN ACCESS MODES
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
255 * limited.
257 * Reading is not available if GFAN exists.
258 * Writing is not available if SFAN exists.
260 * Bits
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
269 * hardware damage.
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)
274 * 0x00 = stop
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
286 * ----
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
303 * might result.
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
327 * factors.
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 */
358 IBMACPI_FAN_EC_DISENGAGED = 0x40, /* EC mode: tachometer
359 * disengaged */
360 IBMACPI_FAN_EC_AUTO = 0x80, /* EC mode: auto fan
361 * control */
364 static int ibm_thinkpad_ec_found;
366 struct ibm_struct {
367 char *name;
368 char param[32];
370 char *hid;
371 struct acpi_driver *driver;
373 int (*init) (void);
374 int (*read) (char *);
375 int (*write) (char *);
376 void (*exit) (void);
378 void (*notify) (struct ibm_struct *, u32);
379 acpi_handle *handle;
380 int type;
381 struct acpi_device *device;
383 int driver_registered;
384 int proc_created;
385 int init_called;
386 int notify_installed;
388 int experimental;
391 static struct proc_dir_entry *proc_dir = NULL;
393 static struct backlight_device *ibm_backlight_device;
395 #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
396 #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
397 #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
399 static int acpi_evalf(acpi_handle handle,
400 void *res, char *method, char *fmt, ...)
402 char *fmt0 = fmt;
403 struct acpi_object_list params;
404 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
405 struct acpi_buffer result, *resultp;
406 union acpi_object out_obj;
407 acpi_status status;
408 va_list ap;
409 char res_type;
410 int success;
411 int quiet;
413 if (!*fmt) {
414 printk(IBM_ERR "acpi_evalf() called with empty format\n");
415 return 0;
418 if (*fmt == 'q') {
419 quiet = 1;
420 fmt++;
421 } else
422 quiet = 0;
424 res_type = *(fmt++);
426 params.count = 0;
427 params.pointer = &in_objs[0];
429 va_start(ap, fmt);
430 while (*fmt) {
431 char c = *(fmt++);
432 switch (c) {
433 case 'd': /* int */
434 in_objs[params.count].integer.value = va_arg(ap, int);
435 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
436 break;
437 /* add more types as needed */
438 default:
439 printk(IBM_ERR "acpi_evalf() called "
440 "with invalid format character '%c'\n", c);
441 return 0;
444 va_end(ap);
446 if (res_type != 'v') {
447 result.length = sizeof(out_obj);
448 result.pointer = &out_obj;
449 resultp = &result;
450 } else
451 resultp = NULL;
453 status = acpi_evaluate_object(handle, method, &params, resultp);
455 switch (res_type) {
456 case 'd': /* int */
457 if (res)
458 *(int *)res = out_obj.integer.value;
459 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
460 break;
461 case 'v': /* void */
462 success = status == AE_OK;
463 break;
464 /* add more types as needed */
465 default:
466 printk(IBM_ERR "acpi_evalf() called "
467 "with invalid format character '%c'\n", res_type);
468 return 0;
471 if (!success && !quiet)
472 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
473 method, fmt0, status);
475 return success;
478 static void __unused acpi_print_int(acpi_handle handle, char *method)
480 int i;
482 if (acpi_evalf(handle, &i, method, "d"))
483 printk(IBM_INFO "%s = 0x%x\n", method, i);
484 else
485 printk(IBM_ERR "error calling %s\n", method);
488 static char *next_cmd(char **cmds)
490 char *start = *cmds;
491 char *end;
493 while ((end = strchr(start, ',')) && end == start)
494 start = end + 1;
496 if (!end)
497 return NULL;
499 *end = 0;
500 *cmds = end + 1;
501 return start;
504 static int driver_init(void)
506 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
507 printk(IBM_INFO "%s\n", IBM_URL);
509 return 0;
512 static int driver_read(char *p)
514 int len = 0;
516 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
517 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
519 return len;
522 static int hotkey_supported;
523 static int hotkey_mask_supported;
524 static int hotkey_orig_status;
525 static int hotkey_orig_mask;
527 static int hotkey_get(int *status, int *mask)
529 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
530 return 0;
532 if (hotkey_mask_supported)
533 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
534 return 0;
536 return 1;
539 static int hotkey_set(int status, int mask)
541 int i;
543 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
544 return 0;
546 if (hotkey_mask_supported)
547 for (i = 0; i < 32; i++) {
548 int bit = ((1 << i) & mask) != 0;
549 if (!acpi_evalf(hkey_handle,
550 NULL, "MHKM", "vdd", i + 1, bit))
551 return 0;
554 return 1;
557 static int hotkey_init(void)
559 /* hotkey not supported on 570 */
560 hotkey_supported = hkey_handle != NULL;
562 if (hotkey_supported) {
563 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
564 A30, R30, R31, T20-22, X20-21, X22-24 */
565 hotkey_mask_supported =
566 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
568 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
569 return -ENODEV;
572 return 0;
575 static int hotkey_read(char *p)
577 int status, mask;
578 int len = 0;
580 if (!hotkey_supported) {
581 len += sprintf(p + len, "status:\t\tnot supported\n");
582 return len;
585 if (!hotkey_get(&status, &mask))
586 return -EIO;
588 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
589 if (hotkey_mask_supported) {
590 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
591 len += sprintf(p + len,
592 "commands:\tenable, disable, reset, <mask>\n");
593 } else {
594 len += sprintf(p + len, "mask:\t\tnot supported\n");
595 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
598 return len;
601 static int hotkey_write(char *buf)
603 int status, mask;
604 char *cmd;
605 int do_cmd = 0;
607 if (!hotkey_supported)
608 return -ENODEV;
610 if (!hotkey_get(&status, &mask))
611 return -EIO;
613 while ((cmd = next_cmd(&buf))) {
614 if (strlencmp(cmd, "enable") == 0) {
615 status = 1;
616 } else if (strlencmp(cmd, "disable") == 0) {
617 status = 0;
618 } else if (strlencmp(cmd, "reset") == 0) {
619 status = hotkey_orig_status;
620 mask = hotkey_orig_mask;
621 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
622 /* mask set */
623 } else if (sscanf(cmd, "%x", &mask) == 1) {
624 /* mask set */
625 } else
626 return -EINVAL;
627 do_cmd = 1;
630 if (do_cmd && !hotkey_set(status, mask))
631 return -EIO;
633 return 0;
636 static void hotkey_exit(void)
638 if (hotkey_supported)
639 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
642 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
644 int hkey;
646 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
647 acpi_bus_generate_event(ibm->device, event, hkey);
648 else {
649 printk(IBM_ERR "unknown hotkey event %d\n", event);
650 acpi_bus_generate_event(ibm->device, event, 0);
654 static int bluetooth_supported;
656 static int bluetooth_init(void)
658 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
659 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
660 bluetooth_supported = hkey_handle &&
661 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
663 return 0;
666 static int bluetooth_status(void)
668 int status;
670 if (!bluetooth_supported ||
671 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
672 status = 0;
674 return status;
677 static int bluetooth_read(char *p)
679 int len = 0;
680 int status = bluetooth_status();
682 if (!bluetooth_supported)
683 len += sprintf(p + len, "status:\t\tnot supported\n");
684 else if (!(status & 1))
685 len += sprintf(p + len, "status:\t\tnot installed\n");
686 else {
687 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
688 len += sprintf(p + len, "commands:\tenable, disable\n");
691 return len;
694 static int bluetooth_write(char *buf)
696 int status = bluetooth_status();
697 char *cmd;
698 int do_cmd = 0;
700 if (!bluetooth_supported)
701 return -ENODEV;
703 while ((cmd = next_cmd(&buf))) {
704 if (strlencmp(cmd, "enable") == 0) {
705 status |= 2;
706 } else if (strlencmp(cmd, "disable") == 0) {
707 status &= ~2;
708 } else
709 return -EINVAL;
710 do_cmd = 1;
713 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
714 return -EIO;
716 return 0;
719 static int wan_supported;
721 static int wan_init(void)
723 wan_supported = hkey_handle &&
724 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
726 return 0;
729 static int wan_status(void)
731 int status;
733 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
734 status = 0;
736 return status;
739 static int wan_read(char *p)
741 int len = 0;
742 int status = wan_status();
744 if (!wan_supported)
745 len += sprintf(p + len, "status:\t\tnot supported\n");
746 else if (!(status & 1))
747 len += sprintf(p + len, "status:\t\tnot installed\n");
748 else {
749 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
750 len += sprintf(p + len, "commands:\tenable, disable\n");
753 return len;
756 static int wan_write(char *buf)
758 int status = wan_status();
759 char *cmd;
760 int do_cmd = 0;
762 if (!wan_supported)
763 return -ENODEV;
765 while ((cmd = next_cmd(&buf))) {
766 if (strlencmp(cmd, "enable") == 0) {
767 status |= 2;
768 } else if (strlencmp(cmd, "disable") == 0) {
769 status &= ~2;
770 } else
771 return -EINVAL;
772 do_cmd = 1;
775 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
776 return -EIO;
778 return 0;
781 static int video_supported;
782 static int video_orig_autosw;
784 #define VIDEO_570 1
785 #define VIDEO_770 2
786 #define VIDEO_NEW 3
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 = 0;
799 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
800 /* 570 */
801 video_supported = VIDEO_570;
802 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
803 /* 600e/x, 770e, 770x */
804 video_supported = VIDEO_770;
805 else
806 /* all others */
807 video_supported = VIDEO_NEW;
809 return 0;
812 static int video_status(void)
814 int status = 0;
815 int i;
817 if (video_supported == VIDEO_570) {
818 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
819 status = i & 3;
820 } else if (video_supported == 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 == 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 == VIDEO_570)
845 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
846 else if (video_supported == VIDEO_770 || video_supported == VIDEO_NEW)
847 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
849 return autosw & 1;
852 static int video_read(char *p)
854 int status = video_status();
855 int autosw = video_autosw();
856 int len = 0;
858 if (!video_supported) {
859 len += sprintf(p + len, "status:\t\tnot supported\n");
860 return len;
863 len += sprintf(p + len, "status:\t\tsupported\n");
864 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
865 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
866 if (video_supported == VIDEO_NEW)
867 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
868 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
869 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
870 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
871 if (video_supported == VIDEO_NEW)
872 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
873 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
874 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
876 return len;
879 static int video_switch(void)
881 int autosw = video_autosw();
882 int ret;
884 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
885 return -EIO;
886 ret = video_supported == VIDEO_570 ?
887 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
888 acpi_evalf(vid_handle, NULL, "VSWT", "v");
889 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
891 return ret;
894 static int video_expand(void)
896 if (video_supported == VIDEO_570)
897 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
898 else if (video_supported == VIDEO_770)
899 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
900 else
901 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
904 static int video_switch2(int status)
906 int ret;
908 if (video_supported == VIDEO_570) {
909 ret = acpi_evalf(NULL, NULL,
910 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
911 } else if (video_supported == VIDEO_770) {
912 int autosw = video_autosw();
913 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
914 return -EIO;
916 ret = acpi_evalf(vid_handle, NULL,
917 "ASWT", "vdd", status * 0x100, 0);
919 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
920 } else {
921 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
922 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
925 return ret;
928 static int video_write(char *buf)
930 char *cmd;
931 int enable, disable, status;
933 if (!video_supported)
934 return -ENODEV;
936 enable = disable = 0;
938 while ((cmd = next_cmd(&buf))) {
939 if (strlencmp(cmd, "lcd_enable") == 0) {
940 enable |= 0x01;
941 } else if (strlencmp(cmd, "lcd_disable") == 0) {
942 disable |= 0x01;
943 } else if (strlencmp(cmd, "crt_enable") == 0) {
944 enable |= 0x02;
945 } else if (strlencmp(cmd, "crt_disable") == 0) {
946 disable |= 0x02;
947 } else if (video_supported == VIDEO_NEW &&
948 strlencmp(cmd, "dvi_enable") == 0) {
949 enable |= 0x08;
950 } else if (video_supported == VIDEO_NEW &&
951 strlencmp(cmd, "dvi_disable") == 0) {
952 disable |= 0x08;
953 } else if (strlencmp(cmd, "auto_enable") == 0) {
954 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
955 return -EIO;
956 } else if (strlencmp(cmd, "auto_disable") == 0) {
957 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
958 return -EIO;
959 } else if (strlencmp(cmd, "video_switch") == 0) {
960 if (!video_switch())
961 return -EIO;
962 } else if (strlencmp(cmd, "expand_toggle") == 0) {
963 if (!video_expand())
964 return -EIO;
965 } else
966 return -EINVAL;
969 if (enable || disable) {
970 status = (video_status() & 0x0f & ~disable) | enable;
971 if (!video_switch2(status))
972 return -EIO;
975 return 0;
978 static void video_exit(void)
980 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
983 static int light_supported;
984 static int light_status_supported;
986 static int light_init(void)
988 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
989 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
991 if (light_supported)
992 /* light status not supported on
993 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
994 light_status_supported = acpi_evalf(ec_handle, NULL,
995 "KBLT", "qv");
997 return 0;
1000 static int light_read(char *p)
1002 int len = 0;
1003 int status = 0;
1005 if (!light_supported) {
1006 len += sprintf(p + len, "status:\t\tnot supported\n");
1007 } else if (!light_status_supported) {
1008 len += sprintf(p + len, "status:\t\tunknown\n");
1009 len += sprintf(p + len, "commands:\ton, off\n");
1010 } else {
1011 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1012 return -EIO;
1013 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1014 len += sprintf(p + len, "commands:\ton, off\n");
1017 return len;
1020 static int light_write(char *buf)
1022 int cmos_cmd, lght_cmd;
1023 char *cmd;
1024 int success;
1026 if (!light_supported)
1027 return -ENODEV;
1029 while ((cmd = next_cmd(&buf))) {
1030 if (strlencmp(cmd, "on") == 0) {
1031 cmos_cmd = 0x0c;
1032 lght_cmd = 1;
1033 } else if (strlencmp(cmd, "off") == 0) {
1034 cmos_cmd = 0x0d;
1035 lght_cmd = 0;
1036 } else
1037 return -EINVAL;
1039 success = cmos_handle ?
1040 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1041 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1042 if (!success)
1043 return -EIO;
1046 return 0;
1049 static int _sta(acpi_handle handle)
1051 int status;
1053 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
1054 status = 0;
1056 return status;
1059 #ifdef CONFIG_ACPI_IBM_DOCK
1060 #define dock_docked() (_sta(dock_handle) & 1)
1062 static int dock_read(char *p)
1064 int len = 0;
1065 int docked = dock_docked();
1067 if (!dock_handle)
1068 len += sprintf(p + len, "status:\t\tnot supported\n");
1069 else if (!docked)
1070 len += sprintf(p + len, "status:\t\tundocked\n");
1071 else {
1072 len += sprintf(p + len, "status:\t\tdocked\n");
1073 len += sprintf(p + len, "commands:\tdock, undock\n");
1076 return len;
1079 static int dock_write(char *buf)
1081 char *cmd;
1083 if (!dock_docked())
1084 return -ENODEV;
1086 while ((cmd = next_cmd(&buf))) {
1087 if (strlencmp(cmd, "undock") == 0) {
1088 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1089 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1090 return -EIO;
1091 } else if (strlencmp(cmd, "dock") == 0) {
1092 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1093 return -EIO;
1094 } else
1095 return -EINVAL;
1098 return 0;
1101 static void dock_notify(struct ibm_struct *ibm, u32 event)
1103 int docked = dock_docked();
1104 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1106 if (event == 1 && !pci) /* 570 */
1107 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1108 else if (event == 1 && pci) /* 570 */
1109 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1110 else if (event == 3 && docked)
1111 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1112 else if (event == 3 && !docked)
1113 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1114 else if (event == 0 && docked)
1115 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1116 else {
1117 printk(IBM_ERR "unknown dock event %d, status %d\n",
1118 event, _sta(dock_handle));
1119 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1122 #endif
1124 static int bay_status_supported;
1125 static int bay_status2_supported;
1126 static int bay_eject_supported;
1127 static int bay_eject2_supported;
1129 static int bay_init(void)
1131 bay_status_supported = bay_handle &&
1132 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1133 bay_status2_supported = bay2_handle &&
1134 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1136 bay_eject_supported = bay_handle && bay_ej_handle &&
1137 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1138 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1139 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1141 return 0;
1144 #define bay_occupied(b) (_sta(b##_handle) & 1)
1146 static int bay_read(char *p)
1148 int len = 0;
1149 int occupied = bay_occupied(bay);
1150 int occupied2 = bay_occupied(bay2);
1151 int eject, eject2;
1153 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1154 (occupied ? "occupied" : "unoccupied") :
1155 "not supported");
1156 if (bay_status2_supported)
1157 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1158 "occupied" : "unoccupied");
1160 eject = bay_eject_supported && occupied;
1161 eject2 = bay_eject2_supported && occupied2;
1163 if (eject && eject2)
1164 len += sprintf(p + len, "commands:\teject, eject2\n");
1165 else if (eject)
1166 len += sprintf(p + len, "commands:\teject\n");
1167 else if (eject2)
1168 len += sprintf(p + len, "commands:\teject2\n");
1170 return len;
1173 static int bay_write(char *buf)
1175 char *cmd;
1177 if (!bay_eject_supported && !bay_eject2_supported)
1178 return -ENODEV;
1180 while ((cmd = next_cmd(&buf))) {
1181 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1182 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1183 return -EIO;
1184 } else if (bay_eject2_supported &&
1185 strlencmp(cmd, "eject2") == 0) {
1186 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1187 return -EIO;
1188 } else
1189 return -EINVAL;
1192 return 0;
1195 static void bay_notify(struct ibm_struct *ibm, u32 event)
1197 acpi_bus_generate_event(ibm->device, event, 0);
1200 static int cmos_read(char *p)
1202 int len = 0;
1204 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1205 R30, R31, T20-22, X20-21 */
1206 if (!cmos_handle)
1207 len += sprintf(p + len, "status:\t\tnot supported\n");
1208 else {
1209 len += sprintf(p + len, "status:\t\tsupported\n");
1210 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1213 return len;
1216 static int cmos_eval(int cmos_cmd)
1218 if (cmos_handle)
1219 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1220 else
1221 return 1;
1224 static int cmos_write(char *buf)
1226 char *cmd;
1227 int cmos_cmd;
1229 if (!cmos_handle)
1230 return -EINVAL;
1232 while ((cmd = next_cmd(&buf))) {
1233 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1234 cmos_cmd >= 0 && cmos_cmd <= 21) {
1235 /* cmos_cmd set */
1236 } else
1237 return -EINVAL;
1239 if (!cmos_eval(cmos_cmd))
1240 return -EIO;
1243 return 0;
1246 static int led_supported;
1248 #define LED_570 1
1249 #define LED_OLD 2
1250 #define LED_NEW 3
1252 static int led_init(void)
1254 if (!led_handle)
1255 /* led not supported on R30, R31 */
1256 led_supported = 0;
1257 else if (strlencmp(led_path, "SLED") == 0)
1258 /* 570 */
1259 led_supported = LED_570;
1260 else if (strlencmp(led_path, "SYSL") == 0)
1261 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1262 led_supported = LED_OLD;
1263 else
1264 /* all others */
1265 led_supported = LED_NEW;
1267 return 0;
1270 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1272 static int led_read(char *p)
1274 int len = 0;
1276 if (!led_supported) {
1277 len += sprintf(p + len, "status:\t\tnot supported\n");
1278 return len;
1280 len += sprintf(p + len, "status:\t\tsupported\n");
1282 if (led_supported == LED_570) {
1283 /* 570 */
1284 int i, status;
1285 for (i = 0; i < 8; i++) {
1286 if (!acpi_evalf(ec_handle,
1287 &status, "GLED", "dd", 1 << i))
1288 return -EIO;
1289 len += sprintf(p + len, "%d:\t\t%s\n",
1290 i, led_status(status));
1294 len += sprintf(p + len, "commands:\t"
1295 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1297 return len;
1300 /* off, on, blink */
1301 static const int led_sled_arg1[] = { 0, 1, 3 };
1302 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1303 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1304 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1306 #define EC_HLCL 0x0c
1307 #define EC_HLBL 0x0d
1308 #define EC_HLMS 0x0e
1310 static int led_write(char *buf)
1312 char *cmd;
1313 int led, ind, ret;
1315 if (!led_supported)
1316 return -ENODEV;
1318 while ((cmd = next_cmd(&buf))) {
1319 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1320 return -EINVAL;
1322 if (strstr(cmd, "off")) {
1323 ind = 0;
1324 } else if (strstr(cmd, "on")) {
1325 ind = 1;
1326 } else if (strstr(cmd, "blink")) {
1327 ind = 2;
1328 } else
1329 return -EINVAL;
1331 if (led_supported == LED_570) {
1332 /* 570 */
1333 led = 1 << led;
1334 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1335 led, led_sled_arg1[ind]))
1336 return -EIO;
1337 } else if (led_supported == LED_OLD) {
1338 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1339 led = 1 << led;
1340 ret = ec_write(EC_HLMS, led);
1341 if (ret >= 0)
1342 ret =
1343 ec_write(EC_HLBL, led * led_exp_hlbl[ind]);
1344 if (ret >= 0)
1345 ret =
1346 ec_write(EC_HLCL, led * led_exp_hlcl[ind]);
1347 if (ret < 0)
1348 return ret;
1349 } else {
1350 /* all others */
1351 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1352 led, led_led_arg1[ind]))
1353 return -EIO;
1357 return 0;
1360 static int beep_read(char *p)
1362 int len = 0;
1364 if (!beep_handle)
1365 len += sprintf(p + len, "status:\t\tnot supported\n");
1366 else {
1367 len += sprintf(p + len, "status:\t\tsupported\n");
1368 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1371 return len;
1374 static int beep_write(char *buf)
1376 char *cmd;
1377 int beep_cmd;
1379 if (!beep_handle)
1380 return -ENODEV;
1382 while ((cmd = next_cmd(&buf))) {
1383 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1384 beep_cmd >= 0 && beep_cmd <= 17) {
1385 /* beep_cmd set */
1386 } else
1387 return -EINVAL;
1388 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1389 return -EIO;
1392 return 0;
1395 static int acpi_ec_read(int i, u8 * p)
1397 int v;
1399 if (ecrd_handle) {
1400 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
1401 return 0;
1402 *p = v;
1403 } else {
1404 if (ec_read(i, p) < 0)
1405 return 0;
1408 return 1;
1411 static int acpi_ec_write(int i, u8 v)
1413 if (ecwr_handle) {
1414 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
1415 return 0;
1416 } else {
1417 if (ec_write(i, v) < 0)
1418 return 0;
1421 return 1;
1424 static enum thermal_access_mode thermal_read_mode;
1426 static int thermal_init(void)
1428 u8 t, ta1, ta2;
1429 int i;
1430 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1432 if (ibm_thinkpad_ec_found && experimental) {
1434 * Direct EC access mode: sensors at registers
1435 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1436 * non-implemented, thermal sensors return 0x80 when
1437 * not available
1440 ta1 = ta2 = 0;
1441 for (i = 0; i < 8; i++) {
1442 if (likely(acpi_ec_read(0x78 + i, &t))) {
1443 ta1 |= t;
1444 } else {
1445 ta1 = 0;
1446 break;
1448 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1449 ta2 |= t;
1450 } else {
1451 ta1 = 0;
1452 break;
1455 if (ta1 == 0) {
1456 /* This is sheer paranoia, but we handle it anyway */
1457 if (acpi_tmp7) {
1458 printk(IBM_ERR
1459 "ThinkPad ACPI EC access misbehaving, "
1460 "falling back to ACPI TMPx access mode\n");
1461 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1462 } else {
1463 printk(IBM_ERR
1464 "ThinkPad ACPI EC access misbehaving, "
1465 "disabling thermal sensors access\n");
1466 thermal_read_mode = IBMACPI_THERMAL_NONE;
1468 } else {
1469 thermal_read_mode =
1470 (ta2 != 0) ?
1471 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1473 } else if (acpi_tmp7) {
1474 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1475 /* 600e/x, 770e, 770x */
1476 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1477 } else {
1478 /* Standard ACPI TMPx access, max 8 sensors */
1479 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1481 } else {
1482 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1483 thermal_read_mode = IBMACPI_THERMAL_NONE;
1486 return 0;
1489 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1491 int i, t;
1492 s8 tmp;
1493 char tmpi[] = "TMPi";
1495 if (!s)
1496 return -EINVAL;
1498 switch (thermal_read_mode) {
1499 #if IBMACPI_MAX_THERMAL_SENSORS >= 16
1500 case IBMACPI_THERMAL_TPEC_16:
1501 for (i = 0; i < 8; i++) {
1502 if (!acpi_ec_read(0xC0 + i, &tmp))
1503 return -EIO;
1504 s->temp[i + 8] = tmp * 1000;
1506 /* fallthrough */
1507 #endif
1508 case IBMACPI_THERMAL_TPEC_8:
1509 for (i = 0; i < 8; i++) {
1510 if (!acpi_ec_read(0x78 + i, &tmp))
1511 return -EIO;
1512 s->temp[i] = tmp * 1000;
1514 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1516 case IBMACPI_THERMAL_ACPI_UPDT:
1517 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1518 return -EIO;
1519 for (i = 0; i < 8; i++) {
1520 tmpi[3] = '0' + i;
1521 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1522 return -EIO;
1523 s->temp[i] = (t - 2732) * 100;
1525 return 8;
1527 case IBMACPI_THERMAL_ACPI_TMP07:
1528 for (i = 0; i < 8; i++) {
1529 tmpi[3] = '0' + i;
1530 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1531 return -EIO;
1532 s->temp[i] = t * 1000;
1534 return 8;
1536 case IBMACPI_THERMAL_NONE:
1537 default:
1538 return 0;
1542 static int thermal_read(char *p)
1544 int len = 0;
1545 int n, i;
1546 struct ibm_thermal_sensors_struct t;
1548 n = thermal_get_sensors(&t);
1549 if (unlikely(n < 0))
1550 return n;
1552 len += sprintf(p + len, "temperatures:\t");
1554 if (n > 0) {
1555 for (i = 0; i < (n - 1); i++)
1556 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1557 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1558 } else
1559 len += sprintf(p + len, "not supported\n");
1561 return len;
1564 static u8 ecdump_regs[256];
1566 static int ecdump_read(char *p)
1568 int len = 0;
1569 int i, j;
1570 u8 v;
1572 len += sprintf(p + len, "EC "
1573 " +00 +01 +02 +03 +04 +05 +06 +07"
1574 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1575 for (i = 0; i < 256; i += 16) {
1576 len += sprintf(p + len, "EC 0x%02x:", i);
1577 for (j = 0; j < 16; j++) {
1578 if (!acpi_ec_read(i + j, &v))
1579 break;
1580 if (v != ecdump_regs[i + j])
1581 len += sprintf(p + len, " *%02x", v);
1582 else
1583 len += sprintf(p + len, " %02x", v);
1584 ecdump_regs[i + j] = v;
1586 len += sprintf(p + len, "\n");
1587 if (j != 16)
1588 break;
1591 /* These are way too dangerous to advertise openly... */
1592 #if 0
1593 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1594 " (<offset> is 00-ff, <value> is 00-ff)\n");
1595 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1596 " (<offset> is 00-ff, <value> is 0-255)\n");
1597 #endif
1598 return len;
1601 static int ecdump_write(char *buf)
1603 char *cmd;
1604 int i, v;
1606 while ((cmd = next_cmd(&buf))) {
1607 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1608 /* i and v set */
1609 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1610 /* i and v set */
1611 } else
1612 return -EINVAL;
1613 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1614 if (!acpi_ec_write(i, v))
1615 return -EIO;
1616 } else
1617 return -EINVAL;
1620 return 0;
1623 static int brightness_offset = 0x31;
1625 static int brightness_get(struct backlight_device *bd)
1627 u8 level;
1628 if (!acpi_ec_read(brightness_offset, &level))
1629 return -EIO;
1631 level &= 0x7;
1632 return level;
1635 static int brightness_read(char *p)
1637 int len = 0;
1638 int level;
1640 if ((level = brightness_get(NULL)) < 0) {
1641 len += sprintf(p + len, "level:\t\tunreadable\n");
1642 } else {
1643 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1644 len += sprintf(p + len, "commands:\tup, down\n");
1645 len += sprintf(p + len, "commands:\tlevel <level>"
1646 " (<level> is 0-7)\n");
1649 return len;
1652 #define BRIGHTNESS_UP 4
1653 #define BRIGHTNESS_DOWN 5
1655 static int brightness_set(int value)
1657 int cmos_cmd, inc, i;
1658 int current_value = brightness_get(NULL);
1660 value &= 7;
1662 cmos_cmd = value > current_value ? BRIGHTNESS_UP : BRIGHTNESS_DOWN;
1663 inc = value > current_value ? 1 : -1;
1664 for (i = current_value; i != value; i += inc) {
1665 if (!cmos_eval(cmos_cmd))
1666 return -EIO;
1667 if (!acpi_ec_write(brightness_offset, i + inc))
1668 return -EIO;
1671 return 0;
1674 static int brightness_write(char *buf)
1676 int level;
1677 int new_level;
1678 char *cmd;
1680 while ((cmd = next_cmd(&buf))) {
1681 if ((level = brightness_get(NULL)) < 0)
1682 return level;
1683 level &= 7;
1685 if (strlencmp(cmd, "up") == 0) {
1686 new_level = level == 7 ? 7 : level + 1;
1687 } else if (strlencmp(cmd, "down") == 0) {
1688 new_level = level == 0 ? 0 : level - 1;
1689 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1690 new_level >= 0 && new_level <= 7) {
1691 /* new_level set */
1692 } else
1693 return -EINVAL;
1695 brightness_set(new_level);
1698 return 0;
1701 static int brightness_update_status(struct backlight_device *bd)
1703 return brightness_set(bd->props->brightness);
1706 static int volume_offset = 0x30;
1708 static int volume_read(char *p)
1710 int len = 0;
1711 u8 level;
1713 if (!acpi_ec_read(volume_offset, &level)) {
1714 len += sprintf(p + len, "level:\t\tunreadable\n");
1715 } else {
1716 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1717 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1718 len += sprintf(p + len, "commands:\tup, down, mute\n");
1719 len += sprintf(p + len, "commands:\tlevel <level>"
1720 " (<level> is 0-15)\n");
1723 return len;
1726 #define VOLUME_DOWN 0
1727 #define VOLUME_UP 1
1728 #define VOLUME_MUTE 2
1730 static int volume_write(char *buf)
1732 int cmos_cmd, inc, i;
1733 u8 level, mute;
1734 int new_level, new_mute;
1735 char *cmd;
1737 while ((cmd = next_cmd(&buf))) {
1738 if (!acpi_ec_read(volume_offset, &level))
1739 return -EIO;
1740 new_mute = mute = level & 0x40;
1741 new_level = level = level & 0xf;
1743 if (strlencmp(cmd, "up") == 0) {
1744 if (mute)
1745 new_mute = 0;
1746 else
1747 new_level = level == 15 ? 15 : level + 1;
1748 } else if (strlencmp(cmd, "down") == 0) {
1749 if (mute)
1750 new_mute = 0;
1751 else
1752 new_level = level == 0 ? 0 : level - 1;
1753 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1754 new_level >= 0 && new_level <= 15) {
1755 /* new_level set */
1756 } else if (strlencmp(cmd, "mute") == 0) {
1757 new_mute = 0x40;
1758 } else
1759 return -EINVAL;
1761 if (new_level != level) { /* mute doesn't change */
1762 cmos_cmd = new_level > level ? VOLUME_UP : VOLUME_DOWN;
1763 inc = new_level > level ? 1 : -1;
1765 if (mute && (!cmos_eval(cmos_cmd) ||
1766 !acpi_ec_write(volume_offset, level)))
1767 return -EIO;
1769 for (i = level; i != new_level; i += inc)
1770 if (!cmos_eval(cmos_cmd) ||
1771 !acpi_ec_write(volume_offset, i + inc))
1772 return -EIO;
1774 if (mute && (!cmos_eval(VOLUME_MUTE) ||
1775 !acpi_ec_write(volume_offset,
1776 new_level + mute)))
1777 return -EIO;
1780 if (new_mute != mute) { /* level doesn't change */
1781 cmos_cmd = new_mute ? VOLUME_MUTE : VOLUME_UP;
1783 if (!cmos_eval(cmos_cmd) ||
1784 !acpi_ec_write(volume_offset, level + new_mute))
1785 return -EIO;
1789 return 0;
1792 static enum fan_status_access_mode fan_status_access_mode;
1793 static enum fan_control_access_mode fan_control_access_mode;
1794 static enum fan_control_commands fan_control_commands;
1796 static int fan_init(void)
1798 u8 status;
1800 fan_status_access_mode = IBMACPI_FAN_NONE;
1801 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1802 fan_control_commands = 0;
1804 if (gfan_handle) {
1805 /* 570, 600e/x, 770e, 770x */
1806 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1807 } else {
1808 /* all other ThinkPads: note that even old-style
1809 * ThinkPad ECs supports the fan control register */
1810 if (likely(acpi_ec_read(fan_status_offset, &status))) {
1811 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1812 } else {
1813 printk(IBM_ERR
1814 "ThinkPad ACPI EC access misbehaving, "
1815 "fan status and control unavailable\n");
1816 return 0;
1820 if (sfan_handle) {
1821 /* 570, 770x-JL */
1822 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
1823 fan_control_commands |= IBMACPI_FAN_CMD_LEVEL;
1824 } else {
1825 if (!gfan_handle) {
1826 /* gfan without sfan means no fan control */
1827 /* all other models implement TP EC 0x2f control */
1829 if (fans_handle) {
1830 /* X31, X40, X41 */
1831 fan_control_access_mode =
1832 IBMACPI_FAN_WR_ACPI_FANS;
1833 fan_control_commands |=
1834 IBMACPI_FAN_CMD_SPEED |
1835 IBMACPI_FAN_CMD_ENABLE;
1836 } else {
1837 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
1838 fan_control_commands |= IBMACPI_FAN_CMD_ENABLE;
1843 return 0;
1846 static int fan_get_status(u8 *status)
1848 u8 s;
1850 /* TODO:
1851 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
1853 switch (fan_status_access_mode) {
1854 case IBMACPI_FAN_RD_ACPI_GFAN:
1855 /* 570, 600e/x, 770e, 770x */
1857 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
1858 return -EIO;
1860 if (likely(status))
1861 *status = s & 0x07;
1863 break;
1865 case IBMACPI_FAN_RD_TPEC:
1866 /* all except 570, 600e/x, 770e, 770x */
1867 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
1868 return -EIO;
1870 if (likely(status))
1871 *status = s;
1873 break;
1875 default:
1876 return -ENXIO;
1879 return 0;
1882 static int fan_get_speed(unsigned int *speed)
1884 u8 hi, lo;
1886 switch (fan_status_access_mode) {
1887 case IBMACPI_FAN_RD_TPEC:
1888 /* all except 570, 600e/x, 770e, 770x */
1889 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
1890 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
1891 return -EIO;
1893 if (likely(speed))
1894 *speed = (hi << 8) | lo;
1896 break;
1898 default:
1899 return -ENXIO;
1902 return 0;
1905 static int fan_read(char *p)
1907 int len = 0;
1908 int rc;
1909 u8 status;
1910 unsigned int speed = 0;
1912 switch (fan_status_access_mode) {
1913 case IBMACPI_FAN_RD_ACPI_GFAN:
1914 /* 570, 600e/x, 770e, 770x */
1915 if ((rc = fan_get_status(&status)) < 0)
1916 return rc;
1918 len += sprintf(p + len, "status:\t\t%s\n"
1919 "level:\t\t%d\n",
1920 (status != 0) ? "enabled" : "disabled", status);
1921 break;
1923 case IBMACPI_FAN_RD_TPEC:
1924 /* all except 570, 600e/x, 770e, 770x */
1925 if ((rc = fan_get_status(&status)) < 0)
1926 return rc;
1928 len += sprintf(p + len, "status:\t\t%s\n",
1929 (status != 0) ? "enabled" : "disabled");
1931 if ((rc = fan_get_speed(&speed)) < 0)
1932 return rc;
1934 len += sprintf(p + len, "speed:\t\t%d\n", speed);
1936 if (status & IBMACPI_FAN_EC_DISENGAGED)
1937 /* Disengaged mode takes precedence */
1938 len += sprintf(p + len, "level:\t\tdisengaged\n");
1939 else if (status & IBMACPI_FAN_EC_AUTO)
1940 len += sprintf(p + len, "level:\t\tauto\n");
1941 else
1942 len += sprintf(p + len, "level:\t\t%d\n", status);
1943 break;
1945 case IBMACPI_FAN_NONE:
1946 default:
1947 len += sprintf(p + len, "status:\t\tnot supported\n");
1950 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL)
1951 len += sprintf(p + len, "commands:\tlevel <level>"
1952 " (<level> is 0-7)\n");
1954 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
1955 len += sprintf(p + len, "commands:\tenable, disable\n");
1957 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
1958 len += sprintf(p + len, "commands:\tspeed <speed>"
1959 " (<speed> is 0-65535)\n");
1961 return len;
1964 static int fan_set_level(int level)
1966 switch (fan_control_access_mode) {
1967 case IBMACPI_FAN_WR_ACPI_SFAN:
1968 if (level >= 0 && level <= 7) {
1969 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
1970 return -EIO;
1971 } else
1972 return -EINVAL;
1973 break;
1975 default:
1976 return -ENXIO;
1978 return 0;
1981 static int fan_set_enable(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, 0x80))
1987 return -EIO;
1988 break;
1990 default:
1991 return -ENXIO;
1993 return 0;
1996 static int fan_set_disable(void)
1998 switch (fan_control_access_mode) {
1999 case IBMACPI_FAN_WR_ACPI_FANS:
2000 case IBMACPI_FAN_WR_TPEC:
2001 if (!acpi_ec_write(fan_status_offset, 0x00))
2002 return -EIO;
2003 break;
2005 default:
2006 return -ENXIO;
2008 return 0;
2011 static int fan_set_speed(int speed)
2013 switch (fan_control_access_mode) {
2014 case IBMACPI_FAN_WR_ACPI_FANS:
2015 if (speed >= 0 && speed <= 65535) {
2016 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2017 speed, speed, speed))
2018 return -EIO;
2019 } else
2020 return -EINVAL;
2021 break;
2023 default:
2024 return -ENXIO;
2026 return 0;
2029 static int fan_write_cmd_level(const char *cmd, int *rc)
2031 int level;
2033 if (sscanf(cmd, "level %d", &level) != 1)
2034 return 0;
2036 if ((*rc = fan_set_level(level)) == -ENXIO)
2037 printk(IBM_ERR "level command accepted for unsupported "
2038 "access mode %d", fan_control_access_mode);
2040 return 1;
2043 static int fan_write_cmd_enable(const char *cmd, int *rc)
2045 if (strlencmp(cmd, "enable") != 0)
2046 return 0;
2048 if ((*rc = fan_set_enable()) == -ENXIO)
2049 printk(IBM_ERR "enable command accepted for unsupported "
2050 "access mode %d", fan_control_access_mode);
2052 return 1;
2055 static int fan_write_cmd_disable(const char *cmd, int *rc)
2057 if (strlencmp(cmd, "disable") != 0)
2058 return 0;
2060 if ((*rc = fan_set_disable()) == -ENXIO)
2061 printk(IBM_ERR "disable command accepted for unsupported "
2062 "access mode %d", fan_control_access_mode);
2064 return 1;
2067 static int fan_write_cmd_speed(const char *cmd, int *rc)
2069 int speed;
2071 /* TODO:
2072 * Support speed <low> <medium> <high> ? */
2074 if (sscanf(cmd, "speed %d", &speed) != 1)
2075 return 0;
2077 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2078 printk(IBM_ERR "speed command accepted for unsupported "
2079 "access mode %d", fan_control_access_mode);
2081 return 1;
2084 static int fan_write(char *buf)
2086 char *cmd;
2087 int rc = 0;
2089 while (!rc && (cmd = next_cmd(&buf))) {
2090 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2091 fan_write_cmd_level(cmd, &rc)) &&
2092 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2093 (fan_write_cmd_enable(cmd, &rc) ||
2094 fan_write_cmd_disable(cmd, &rc))) &&
2095 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2096 fan_write_cmd_speed(cmd, &rc))
2098 rc = -EINVAL;
2101 return rc;
2104 static struct ibm_struct ibms[] = {
2106 .name = "driver",
2107 .init = driver_init,
2108 .read = driver_read,
2111 .name = "hotkey",
2112 .hid = IBM_HKEY_HID,
2113 .init = hotkey_init,
2114 .read = hotkey_read,
2115 .write = hotkey_write,
2116 .exit = hotkey_exit,
2117 .notify = hotkey_notify,
2118 .handle = &hkey_handle,
2119 .type = ACPI_DEVICE_NOTIFY,
2122 .name = "bluetooth",
2123 .init = bluetooth_init,
2124 .read = bluetooth_read,
2125 .write = bluetooth_write,
2128 .name = "wan",
2129 .init = wan_init,
2130 .read = wan_read,
2131 .write = wan_write,
2132 .experimental = 1,
2135 .name = "video",
2136 .init = video_init,
2137 .read = video_read,
2138 .write = video_write,
2139 .exit = video_exit,
2142 .name = "light",
2143 .init = light_init,
2144 .read = light_read,
2145 .write = light_write,
2147 #ifdef CONFIG_ACPI_IBM_DOCK
2149 .name = "dock",
2150 .read = dock_read,
2151 .write = dock_write,
2152 .notify = dock_notify,
2153 .handle = &dock_handle,
2154 .type = ACPI_SYSTEM_NOTIFY,
2157 .name = "dock",
2158 .hid = IBM_PCI_HID,
2159 .notify = dock_notify,
2160 .handle = &pci_handle,
2161 .type = ACPI_SYSTEM_NOTIFY,
2163 #endif
2165 .name = "bay",
2166 .init = bay_init,
2167 .read = bay_read,
2168 .write = bay_write,
2169 .notify = bay_notify,
2170 .handle = &bay_handle,
2171 .type = ACPI_SYSTEM_NOTIFY,
2174 .name = "cmos",
2175 .read = cmos_read,
2176 .write = cmos_write,
2179 .name = "led",
2180 .init = led_init,
2181 .read = led_read,
2182 .write = led_write,
2185 .name = "beep",
2186 .read = beep_read,
2187 .write = beep_write,
2190 .name = "thermal",
2191 .init = thermal_init,
2192 .read = thermal_read,
2195 .name = "ecdump",
2196 .read = ecdump_read,
2197 .write = ecdump_write,
2198 .experimental = 1,
2201 .name = "brightness",
2202 .read = brightness_read,
2203 .write = brightness_write,
2206 .name = "volume",
2207 .read = volume_read,
2208 .write = volume_write,
2211 .name = "fan",
2212 .read = fan_read,
2213 .write = fan_write,
2214 .init = fan_init,
2215 .experimental = 1,
2219 static int dispatch_read(char *page, char **start, off_t off, int count,
2220 int *eof, void *data)
2222 struct ibm_struct *ibm = (struct ibm_struct *)data;
2223 int len;
2225 if (!ibm || !ibm->read)
2226 return -EINVAL;
2228 len = ibm->read(page);
2229 if (len < 0)
2230 return len;
2232 if (len <= off + count)
2233 *eof = 1;
2234 *start = page + off;
2235 len -= off;
2236 if (len > count)
2237 len = count;
2238 if (len < 0)
2239 len = 0;
2241 return len;
2244 static int dispatch_write(struct file *file, const char __user * userbuf,
2245 unsigned long count, void *data)
2247 struct ibm_struct *ibm = (struct ibm_struct *)data;
2248 char *kernbuf;
2249 int ret;
2251 if (!ibm || !ibm->write)
2252 return -EINVAL;
2254 kernbuf = kmalloc(count + 2, GFP_KERNEL);
2255 if (!kernbuf)
2256 return -ENOMEM;
2258 if (copy_from_user(kernbuf, userbuf, count)) {
2259 kfree(kernbuf);
2260 return -EFAULT;
2263 kernbuf[count] = 0;
2264 strcat(kernbuf, ",");
2265 ret = ibm->write(kernbuf);
2266 if (ret == 0)
2267 ret = count;
2269 kfree(kernbuf);
2271 return ret;
2274 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
2276 struct ibm_struct *ibm = (struct ibm_struct *)data;
2278 if (!ibm || !ibm->notify)
2279 return;
2281 ibm->notify(ibm, event);
2284 static int __init setup_notify(struct ibm_struct *ibm)
2286 acpi_status status;
2287 int ret;
2289 if (!*ibm->handle)
2290 return 0;
2292 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
2293 if (ret < 0) {
2294 printk(IBM_ERR "%s device not present\n", ibm->name);
2295 return 0;
2298 acpi_driver_data(ibm->device) = ibm;
2299 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
2301 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
2302 dispatch_notify, ibm);
2303 if (ACPI_FAILURE(status)) {
2304 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
2305 ibm->name, status);
2306 return -ENODEV;
2309 return 0;
2312 static int __init ibm_device_add(struct acpi_device *device)
2314 return 0;
2317 static int __init register_driver(struct ibm_struct *ibm)
2319 int ret;
2321 ibm->driver = kmalloc(sizeof(struct acpi_driver), GFP_KERNEL);
2322 if (!ibm->driver) {
2323 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
2324 return -1;
2327 memset(ibm->driver, 0, sizeof(struct acpi_driver));
2328 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
2329 ibm->driver->ids = ibm->hid;
2330 ibm->driver->ops.add = &ibm_device_add;
2332 ret = acpi_bus_register_driver(ibm->driver);
2333 if (ret < 0) {
2334 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
2335 ibm->hid, ret);
2336 kfree(ibm->driver);
2339 return ret;
2342 static int __init ibm_init(struct ibm_struct *ibm)
2344 int ret;
2345 struct proc_dir_entry *entry;
2347 if (ibm->experimental && !experimental)
2348 return 0;
2350 if (ibm->hid) {
2351 ret = register_driver(ibm);
2352 if (ret < 0)
2353 return ret;
2354 ibm->driver_registered = 1;
2357 if (ibm->init) {
2358 ret = ibm->init();
2359 if (ret != 0)
2360 return ret;
2361 ibm->init_called = 1;
2364 if (ibm->read) {
2365 entry = create_proc_entry(ibm->name,
2366 S_IFREG | S_IRUGO | S_IWUSR,
2367 proc_dir);
2368 if (!entry) {
2369 printk(IBM_ERR "unable to create proc entry %s\n",
2370 ibm->name);
2371 return -ENODEV;
2373 entry->owner = THIS_MODULE;
2374 entry->data = ibm;
2375 entry->read_proc = &dispatch_read;
2376 if (ibm->write)
2377 entry->write_proc = &dispatch_write;
2378 ibm->proc_created = 1;
2381 if (ibm->notify) {
2382 ret = setup_notify(ibm);
2383 if (ret < 0)
2384 return ret;
2385 ibm->notify_installed = 1;
2388 return 0;
2391 static void ibm_exit(struct ibm_struct *ibm)
2393 if (ibm->notify_installed)
2394 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2395 dispatch_notify);
2397 if (ibm->proc_created)
2398 remove_proc_entry(ibm->name, proc_dir);
2400 if (ibm->init_called && ibm->exit)
2401 ibm->exit();
2403 if (ibm->driver_registered) {
2404 acpi_bus_unregister_driver(ibm->driver);
2405 kfree(ibm->driver);
2409 static void __init ibm_handle_init(char *name,
2410 acpi_handle * handle, acpi_handle parent,
2411 char **paths, int num_paths, char **path)
2413 int i;
2414 acpi_status status;
2416 for (i = 0; i < num_paths; i++) {
2417 status = acpi_get_handle(parent, paths[i], handle);
2418 if (ACPI_SUCCESS(status)) {
2419 *path = paths[i];
2420 return;
2424 *handle = NULL;
2427 #define IBM_HANDLE_INIT(object) \
2428 ibm_handle_init(#object, &object##_handle, *object##_parent, \
2429 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
2431 static int set_ibm_param(const char *val, struct kernel_param *kp)
2433 unsigned int i;
2435 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2436 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2437 if (strlen(val) > sizeof(ibms[i].param) - 2)
2438 return -ENOSPC;
2439 strcpy(ibms[i].param, val);
2440 strcat(ibms[i].param, ",");
2441 return 0;
2444 return -EINVAL;
2447 #define IBM_PARAM(feature) \
2448 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2450 IBM_PARAM(hotkey);
2451 IBM_PARAM(bluetooth);
2452 IBM_PARAM(video);
2453 IBM_PARAM(light);
2454 #ifdef CONFIG_ACPI_IBM_DOCK
2455 IBM_PARAM(dock);
2456 #endif
2457 IBM_PARAM(bay);
2458 IBM_PARAM(cmos);
2459 IBM_PARAM(led);
2460 IBM_PARAM(beep);
2461 IBM_PARAM(ecdump);
2462 IBM_PARAM(brightness);
2463 IBM_PARAM(volume);
2464 IBM_PARAM(fan);
2466 static struct backlight_properties ibm_backlight_data = {
2467 .owner = THIS_MODULE,
2468 .get_brightness = brightness_get,
2469 .update_status = brightness_update_status,
2470 .max_brightness = 7,
2473 static void acpi_ibm_exit(void)
2475 int i;
2477 if (ibm_backlight_device)
2478 backlight_device_unregister(ibm_backlight_device);
2480 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2481 ibm_exit(&ibms[i]);
2483 remove_proc_entry(IBM_DIR, acpi_root_dir);
2486 static int __init check_dmi_for_ec(void)
2488 struct dmi_device *dev = NULL;
2491 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2492 * X32 or newer, all Z series; Some models must have an
2493 * up-to-date BIOS or they will not be detected.
2495 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2497 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2498 if (strstr(dev->name, "IBM ThinkPad Embedded Controller"))
2499 return 1;
2501 return 0;
2504 static int __init acpi_ibm_init(void)
2506 int ret, i;
2508 if (acpi_disabled)
2509 return -ENODEV;
2511 if (!acpi_specific_hotkey_enabled) {
2512 printk(IBM_ERR "using generic hotkey driver\n");
2513 return -ENODEV;
2516 /* ec is required because many other handles are relative to it */
2517 IBM_HANDLE_INIT(ec);
2518 if (!ec_handle) {
2519 printk(IBM_ERR "ec object not found\n");
2520 return -ENODEV;
2523 /* Models with newer firmware report the EC in DMI */
2524 ibm_thinkpad_ec_found = check_dmi_for_ec();
2526 /* these handles are not required */
2527 IBM_HANDLE_INIT(vid);
2528 IBM_HANDLE_INIT(vid2);
2529 IBM_HANDLE_INIT(ledb);
2530 IBM_HANDLE_INIT(led);
2531 IBM_HANDLE_INIT(hkey);
2532 IBM_HANDLE_INIT(lght);
2533 IBM_HANDLE_INIT(cmos);
2534 #ifdef CONFIG_ACPI_IBM_DOCK
2535 IBM_HANDLE_INIT(dock);
2536 #endif
2537 IBM_HANDLE_INIT(pci);
2538 IBM_HANDLE_INIT(bay);
2539 if (bay_handle)
2540 IBM_HANDLE_INIT(bay_ej);
2541 IBM_HANDLE_INIT(bay2);
2542 if (bay2_handle)
2543 IBM_HANDLE_INIT(bay2_ej);
2544 IBM_HANDLE_INIT(beep);
2545 IBM_HANDLE_INIT(ecrd);
2546 IBM_HANDLE_INIT(ecwr);
2547 IBM_HANDLE_INIT(fans);
2548 IBM_HANDLE_INIT(gfan);
2549 IBM_HANDLE_INIT(sfan);
2551 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2552 if (!proc_dir) {
2553 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2554 return -ENODEV;
2556 proc_dir->owner = THIS_MODULE;
2558 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
2559 ret = ibm_init(&ibms[i]);
2560 if (ret >= 0 && *ibms[i].param)
2561 ret = ibms[i].write(ibms[i].param);
2562 if (ret < 0) {
2563 acpi_ibm_exit();
2564 return ret;
2568 ibm_backlight_device = backlight_device_register("ibm", NULL,
2569 &ibm_backlight_data);
2570 if (IS_ERR(ibm_backlight_device)) {
2571 printk(IBM_ERR "Could not register ibm backlight device\n");
2572 ibm_backlight_device = NULL;
2573 acpi_ibm_exit();
2576 return 0;
2579 module_init(acpi_ibm_init);
2580 module_exit(acpi_ibm_exit);