ACPI: ibm-acpi: organize code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / ibm_acpi.c
blobb6b5447b6e719b21b2dc66223a9143e1b09cfbb2
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 "ibm_acpi.h"
83 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
84 MODULE_DESCRIPTION(IBM_DESC);
85 MODULE_VERSION(IBM_VERSION);
86 MODULE_LICENSE("GPL");
88 #define __unused __attribute__ ((unused))
90 /****************************************************************************
91 ****************************************************************************
93 * ACPI Helpers and device model
95 ****************************************************************************
96 ****************************************************************************/
98 /*************************************************************************
99 * ACPI basic handles
102 static acpi_handle root_handle = NULL;
104 #define IBM_HANDLE(object, parent, paths...) \
105 static acpi_handle object##_handle; \
106 static acpi_handle *object##_parent = &parent##_handle; \
107 static char *object##_path; \
108 static char *object##_paths[] = { paths }
110 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
111 "\\_SB.PCI.ISA.EC", /* 570 */
112 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
113 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
114 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
115 "\\_SB.PCI0.ICH3.EC0", /* R31 */
116 "\\_SB.PCI0.LPC.EC", /* all others */
119 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
120 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
123 /*************************************************************************
124 * Misc ACPI handles
127 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
128 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
129 "\\CMS", /* R40, R40e */
130 ); /* all others */
132 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
133 "^HKEY", /* R30, R31 */
134 "HKEY", /* all others */
135 ); /* 570 */
138 /*************************************************************************
139 * ACPI helpers
142 static int acpi_evalf(acpi_handle handle,
143 void *res, char *method, char *fmt, ...)
145 char *fmt0 = fmt;
146 struct acpi_object_list params;
147 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
148 struct acpi_buffer result, *resultp;
149 union acpi_object out_obj;
150 acpi_status status;
151 va_list ap;
152 char res_type;
153 int success;
154 int quiet;
156 if (!*fmt) {
157 printk(IBM_ERR "acpi_evalf() called with empty format\n");
158 return 0;
161 if (*fmt == 'q') {
162 quiet = 1;
163 fmt++;
164 } else
165 quiet = 0;
167 res_type = *(fmt++);
169 params.count = 0;
170 params.pointer = &in_objs[0];
172 va_start(ap, fmt);
173 while (*fmt) {
174 char c = *(fmt++);
175 switch (c) {
176 case 'd': /* int */
177 in_objs[params.count].integer.value = va_arg(ap, int);
178 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
179 break;
180 /* add more types as needed */
181 default:
182 printk(IBM_ERR "acpi_evalf() called "
183 "with invalid format character '%c'\n", c);
184 return 0;
187 va_end(ap);
189 if (res_type != 'v') {
190 result.length = sizeof(out_obj);
191 result.pointer = &out_obj;
192 resultp = &result;
193 } else
194 resultp = NULL;
196 status = acpi_evaluate_object(handle, method, &params, resultp);
198 switch (res_type) {
199 case 'd': /* int */
200 if (res)
201 *(int *)res = out_obj.integer.value;
202 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
203 break;
204 case 'v': /* void */
205 success = status == AE_OK;
206 break;
207 /* add more types as needed */
208 default:
209 printk(IBM_ERR "acpi_evalf() called "
210 "with invalid format character '%c'\n", res_type);
211 return 0;
214 if (!success && !quiet)
215 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
216 method, fmt0, status);
218 return success;
221 static void __unused acpi_print_int(acpi_handle handle, char *method)
223 int i;
225 if (acpi_evalf(handle, &i, method, "d"))
226 printk(IBM_INFO "%s = 0x%x\n", method, i);
227 else
228 printk(IBM_ERR "error calling %s\n", method);
231 static int acpi_ec_read(int i, u8 * p)
233 int v;
235 if (ecrd_handle) {
236 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
237 return 0;
238 *p = v;
239 } else {
240 if (ec_read(i, p) < 0)
241 return 0;
244 return 1;
247 static int acpi_ec_write(int i, u8 v)
249 if (ecwr_handle) {
250 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
251 return 0;
252 } else {
253 if (ec_write(i, v) < 0)
254 return 0;
257 return 1;
260 static int _sta(acpi_handle handle)
262 int status;
264 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
265 status = 0;
267 return status;
270 /*************************************************************************
271 * ACPI device model
274 static void __init ibm_handle_init(char *name,
275 acpi_handle * handle, acpi_handle parent,
276 char **paths, int num_paths, char **path)
278 int i;
279 acpi_status status;
281 for (i = 0; i < num_paths; i++) {
282 status = acpi_get_handle(parent, paths[i], handle);
283 if (ACPI_SUCCESS(status)) {
284 *path = paths[i];
285 return;
289 *handle = NULL;
292 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
294 struct ibm_struct *ibm = data;
296 if (!ibm || !ibm->notify)
297 return;
299 ibm->notify(ibm, event);
302 static int __init setup_notify(struct ibm_struct *ibm)
304 acpi_status status;
305 int ret;
307 if (!*ibm->handle)
308 return 0;
310 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
311 if (ret < 0) {
312 printk(IBM_ERR "%s device not present\n", ibm->name);
313 return -ENODEV;
316 acpi_driver_data(ibm->device) = ibm;
317 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
319 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
320 dispatch_notify, ibm);
321 if (ACPI_FAILURE(status)) {
322 if (status == AE_ALREADY_EXISTS) {
323 printk(IBM_NOTICE "another device driver is already handling %s events\n",
324 ibm->name);
325 } else {
326 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
327 ibm->name, status);
329 return -ENODEV;
331 ibm->notify_installed = 1;
332 return 0;
335 static int __init ibm_device_add(struct acpi_device *device)
337 return 0;
340 static int __init register_ibmacpi_subdriver(struct ibm_struct *ibm)
342 int ret;
344 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
345 if (!ibm->driver) {
346 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
347 return -1;
350 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
351 ibm->driver->ids = ibm->hid;
352 ibm->driver->ops.add = &ibm_device_add;
354 ret = acpi_bus_register_driver(ibm->driver);
355 if (ret < 0) {
356 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
357 ibm->hid, ret);
358 kfree(ibm->driver);
361 return ret;
365 /****************************************************************************
366 ****************************************************************************
368 * Procfs Helpers
370 ****************************************************************************
371 ****************************************************************************/
373 static int dispatch_read(char *page, char **start, off_t off, int count,
374 int *eof, void *data)
376 struct ibm_struct *ibm = data;
377 int len;
379 if (!ibm || !ibm->read)
380 return -EINVAL;
382 len = ibm->read(page);
383 if (len < 0)
384 return len;
386 if (len <= off + count)
387 *eof = 1;
388 *start = page + off;
389 len -= off;
390 if (len > count)
391 len = count;
392 if (len < 0)
393 len = 0;
395 return len;
398 static int dispatch_write(struct file *file, const char __user * userbuf,
399 unsigned long count, void *data)
401 struct ibm_struct *ibm = data;
402 char *kernbuf;
403 int ret;
405 if (!ibm || !ibm->write)
406 return -EINVAL;
408 kernbuf = kmalloc(count + 2, GFP_KERNEL);
409 if (!kernbuf)
410 return -ENOMEM;
412 if (copy_from_user(kernbuf, userbuf, count)) {
413 kfree(kernbuf);
414 return -EFAULT;
417 kernbuf[count] = 0;
418 strcat(kernbuf, ",");
419 ret = ibm->write(kernbuf);
420 if (ret == 0)
421 ret = count;
423 kfree(kernbuf);
425 return ret;
428 static char *next_cmd(char **cmds)
430 char *start = *cmds;
431 char *end;
433 while ((end = strchr(start, ',')) && end == start)
434 start = end + 1;
436 if (!end)
437 return NULL;
439 *end = 0;
440 *cmds = end + 1;
441 return start;
445 /****************************************************************************
446 ****************************************************************************
448 * Subdrivers
450 ****************************************************************************
451 ****************************************************************************/
453 /*************************************************************************
454 * ibm-acpi init subdriver
457 static int ibm_acpi_driver_init(void)
459 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
460 printk(IBM_INFO "%s\n", IBM_URL);
462 if (ibm_thinkpad_ec_found)
463 printk(IBM_INFO "ThinkPad EC firmware %s\n",
464 ibm_thinkpad_ec_found);
466 return 0;
469 static int ibm_acpi_driver_read(char *p)
471 int len = 0;
473 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
474 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
476 return len;
479 /*************************************************************************
480 * Hotkey subdriver
483 static int hotkey_supported;
484 static int hotkey_mask_supported;
485 static int hotkey_orig_status;
486 static int hotkey_orig_mask;
488 static int hotkey_init(void)
490 /* hotkey not supported on 570 */
491 hotkey_supported = hkey_handle != NULL;
493 if (hotkey_supported) {
494 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
495 A30, R30, R31, T20-22, X20-21, X22-24 */
496 hotkey_mask_supported =
497 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
499 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
500 return -ENODEV;
503 return 0;
506 static void hotkey_exit(void)
508 if (hotkey_supported)
509 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
512 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
514 int hkey;
516 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
517 acpi_bus_generate_event(ibm->device, event, hkey);
518 else {
519 printk(IBM_ERR "unknown hotkey event %d\n", event);
520 acpi_bus_generate_event(ibm->device, event, 0);
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_read(char *p)
556 int status, mask;
557 int len = 0;
559 if (!hotkey_supported) {
560 len += sprintf(p + len, "status:\t\tnot supported\n");
561 return len;
564 if (!hotkey_get(&status, &mask))
565 return -EIO;
567 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
568 if (hotkey_mask_supported) {
569 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
570 len += sprintf(p + len,
571 "commands:\tenable, disable, reset, <mask>\n");
572 } else {
573 len += sprintf(p + len, "mask:\t\tnot supported\n");
574 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
577 return len;
580 static int hotkey_write(char *buf)
582 int status, mask;
583 char *cmd;
584 int do_cmd = 0;
586 if (!hotkey_supported)
587 return -ENODEV;
589 if (!hotkey_get(&status, &mask))
590 return -EIO;
592 while ((cmd = next_cmd(&buf))) {
593 if (strlencmp(cmd, "enable") == 0) {
594 status = 1;
595 } else if (strlencmp(cmd, "disable") == 0) {
596 status = 0;
597 } else if (strlencmp(cmd, "reset") == 0) {
598 status = hotkey_orig_status;
599 mask = hotkey_orig_mask;
600 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
601 /* mask set */
602 } else if (sscanf(cmd, "%x", &mask) == 1) {
603 /* mask set */
604 } else
605 return -EINVAL;
606 do_cmd = 1;
609 if (do_cmd && !hotkey_set(status, mask))
610 return -EIO;
612 return 0;
615 /*************************************************************************
616 * Bluetooth subdriver
619 static int bluetooth_supported;
621 static int bluetooth_init(void)
623 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
624 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
625 bluetooth_supported = hkey_handle &&
626 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
628 return 0;
631 static int bluetooth_status(void)
633 int status;
635 if (!bluetooth_supported ||
636 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
637 status = 0;
639 return status;
642 static int bluetooth_read(char *p)
644 int len = 0;
645 int status = bluetooth_status();
647 if (!bluetooth_supported)
648 len += sprintf(p + len, "status:\t\tnot supported\n");
649 else if (!(status & 1))
650 len += sprintf(p + len, "status:\t\tnot installed\n");
651 else {
652 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
653 len += sprintf(p + len, "commands:\tenable, disable\n");
656 return len;
659 static int bluetooth_write(char *buf)
661 int status = bluetooth_status();
662 char *cmd;
663 int do_cmd = 0;
665 if (!bluetooth_supported)
666 return -ENODEV;
668 while ((cmd = next_cmd(&buf))) {
669 if (strlencmp(cmd, "enable") == 0) {
670 status |= 2;
671 } else if (strlencmp(cmd, "disable") == 0) {
672 status &= ~2;
673 } else
674 return -EINVAL;
675 do_cmd = 1;
678 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
679 return -EIO;
681 return 0;
684 /*************************************************************************
685 * Wan subdriver
688 static int wan_supported;
690 static int wan_init(void)
692 wan_supported = hkey_handle &&
693 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
695 return 0;
698 static int wan_status(void)
700 int status;
702 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
703 status = 0;
705 return status;
708 static int wan_read(char *p)
710 int len = 0;
711 int status = wan_status();
713 if (!wan_supported)
714 len += sprintf(p + len, "status:\t\tnot supported\n");
715 else if (!(status & 1))
716 len += sprintf(p + len, "status:\t\tnot installed\n");
717 else {
718 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
719 len += sprintf(p + len, "commands:\tenable, disable\n");
722 return len;
725 static int wan_write(char *buf)
727 int status = wan_status();
728 char *cmd;
729 int do_cmd = 0;
731 if (!wan_supported)
732 return -ENODEV;
734 while ((cmd = next_cmd(&buf))) {
735 if (strlencmp(cmd, "enable") == 0) {
736 status |= 2;
737 } else if (strlencmp(cmd, "disable") == 0) {
738 status &= ~2;
739 } else
740 return -EINVAL;
741 do_cmd = 1;
744 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
745 return -EIO;
747 return 0;
750 /*************************************************************************
751 * Video subdriver
754 static enum video_access_mode video_supported;
755 static int video_orig_autosw;
757 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
758 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
759 "\\_SB.PCI0.VID0", /* 770e */
760 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
761 "\\_SB.PCI0.AGP.VID", /* all others */
762 ); /* R30, R31 */
764 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
766 static int video_init(void)
768 int ivga;
770 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
771 /* G41, assume IVGA doesn't change */
772 vid_handle = vid2_handle;
774 if (!vid_handle)
775 /* video switching not supported on R30, R31 */
776 video_supported = IBMACPI_VIDEO_NONE;
777 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
778 /* 570 */
779 video_supported = IBMACPI_VIDEO_570;
780 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
781 /* 600e/x, 770e, 770x */
782 video_supported = IBMACPI_VIDEO_770;
783 else
784 /* all others */
785 video_supported = IBMACPI_VIDEO_NEW;
787 return 0;
790 static void video_exit(void)
792 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
795 static int video_status(void)
797 int status = 0;
798 int i;
800 if (video_supported == IBMACPI_VIDEO_570) {
801 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
802 status = i & 3;
803 } else if (video_supported == IBMACPI_VIDEO_770) {
804 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
805 status |= 0x01 * i;
806 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
807 status |= 0x02 * i;
808 } else if (video_supported == IBMACPI_VIDEO_NEW) {
809 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
810 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
811 status |= 0x02 * i;
813 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
814 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
815 status |= 0x01 * i;
816 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
817 status |= 0x08 * i;
820 return status;
823 static int video_autosw(void)
825 int autosw = 0;
827 if (video_supported == IBMACPI_VIDEO_570)
828 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
829 else if (video_supported == IBMACPI_VIDEO_770 ||
830 video_supported == IBMACPI_VIDEO_NEW)
831 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
833 return autosw & 1;
836 static int video_switch(void)
838 int autosw = video_autosw();
839 int ret;
841 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
842 return -EIO;
843 ret = video_supported == IBMACPI_VIDEO_570 ?
844 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
845 acpi_evalf(vid_handle, NULL, "VSWT", "v");
846 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
848 return ret;
851 static int video_expand(void)
853 if (video_supported == IBMACPI_VIDEO_570)
854 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
855 else if (video_supported == IBMACPI_VIDEO_770)
856 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
857 else
858 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
861 static int video_switch2(int status)
863 int ret;
865 if (video_supported == IBMACPI_VIDEO_570) {
866 ret = acpi_evalf(NULL, NULL,
867 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
868 } else if (video_supported == IBMACPI_VIDEO_770) {
869 int autosw = video_autosw();
870 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
871 return -EIO;
873 ret = acpi_evalf(vid_handle, NULL,
874 "ASWT", "vdd", status * 0x100, 0);
876 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
877 } else {
878 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
879 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
882 return ret;
885 static int video_read(char *p)
887 int status = video_status();
888 int autosw = video_autosw();
889 int len = 0;
891 if (!video_supported) {
892 len += sprintf(p + len, "status:\t\tnot supported\n");
893 return len;
896 len += sprintf(p + len, "status:\t\tsupported\n");
897 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
898 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
899 if (video_supported == IBMACPI_VIDEO_NEW)
900 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
901 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
902 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
903 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
904 if (video_supported == IBMACPI_VIDEO_NEW)
905 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
906 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
907 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
909 return len;
912 static int video_write(char *buf)
914 char *cmd;
915 int enable, disable, status;
917 if (!video_supported)
918 return -ENODEV;
920 enable = disable = 0;
922 while ((cmd = next_cmd(&buf))) {
923 if (strlencmp(cmd, "lcd_enable") == 0) {
924 enable |= 0x01;
925 } else if (strlencmp(cmd, "lcd_disable") == 0) {
926 disable |= 0x01;
927 } else if (strlencmp(cmd, "crt_enable") == 0) {
928 enable |= 0x02;
929 } else if (strlencmp(cmd, "crt_disable") == 0) {
930 disable |= 0x02;
931 } else if (video_supported == IBMACPI_VIDEO_NEW &&
932 strlencmp(cmd, "dvi_enable") == 0) {
933 enable |= 0x08;
934 } else if (video_supported == IBMACPI_VIDEO_NEW &&
935 strlencmp(cmd, "dvi_disable") == 0) {
936 disable |= 0x08;
937 } else if (strlencmp(cmd, "auto_enable") == 0) {
938 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
939 return -EIO;
940 } else if (strlencmp(cmd, "auto_disable") == 0) {
941 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
942 return -EIO;
943 } else if (strlencmp(cmd, "video_switch") == 0) {
944 if (!video_switch())
945 return -EIO;
946 } else if (strlencmp(cmd, "expand_toggle") == 0) {
947 if (!video_expand())
948 return -EIO;
949 } else
950 return -EINVAL;
953 if (enable || disable) {
954 status = (video_status() & 0x0f & ~disable) | enable;
955 if (!video_switch2(status))
956 return -EIO;
959 return 0;
962 /*************************************************************************
963 * Light (thinklight) subdriver
966 static int light_supported;
967 static int light_status_supported;
969 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
970 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
972 static int light_init(void)
974 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
975 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
977 if (light_supported)
978 /* light status not supported on
979 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
980 light_status_supported = acpi_evalf(ec_handle, NULL,
981 "KBLT", "qv");
983 return 0;
986 static int light_read(char *p)
988 int len = 0;
989 int status = 0;
991 if (!light_supported) {
992 len += sprintf(p + len, "status:\t\tnot supported\n");
993 } else if (!light_status_supported) {
994 len += sprintf(p + len, "status:\t\tunknown\n");
995 len += sprintf(p + len, "commands:\ton, off\n");
996 } else {
997 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
998 return -EIO;
999 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1000 len += sprintf(p + len, "commands:\ton, off\n");
1003 return len;
1006 static int light_write(char *buf)
1008 int cmos_cmd, lght_cmd;
1009 char *cmd;
1010 int success;
1012 if (!light_supported)
1013 return -ENODEV;
1015 while ((cmd = next_cmd(&buf))) {
1016 if (strlencmp(cmd, "on") == 0) {
1017 cmos_cmd = 0x0c;
1018 lght_cmd = 1;
1019 } else if (strlencmp(cmd, "off") == 0) {
1020 cmos_cmd = 0x0d;
1021 lght_cmd = 0;
1022 } else
1023 return -EINVAL;
1025 success = cmos_handle ?
1026 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1027 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1028 if (!success)
1029 return -EIO;
1032 return 0;
1035 /*************************************************************************
1036 * Dock subdriver
1039 /* don't list other alternatives as we install a notify handler on the 570 */
1040 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1042 #ifdef CONFIG_ACPI_IBM_DOCK
1044 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1045 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1046 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1047 "\\_SB.PCI.ISA.SLCE", /* 570 */
1048 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1050 #define dock_docked() (_sta(dock_handle) & 1)
1052 static void dock_notify(struct ibm_struct *ibm, u32 event)
1054 int docked = dock_docked();
1055 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1057 if (event == 1 && !pci) /* 570 */
1058 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1059 else if (event == 1 && pci) /* 570 */
1060 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1061 else if (event == 3 && docked)
1062 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1063 else if (event == 3 && !docked)
1064 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1065 else if (event == 0 && docked)
1066 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1067 else {
1068 printk(IBM_ERR "unknown dock event %d, status %d\n",
1069 event, _sta(dock_handle));
1070 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1074 static int dock_read(char *p)
1076 int len = 0;
1077 int docked = dock_docked();
1079 if (!dock_handle)
1080 len += sprintf(p + len, "status:\t\tnot supported\n");
1081 else if (!docked)
1082 len += sprintf(p + len, "status:\t\tundocked\n");
1083 else {
1084 len += sprintf(p + len, "status:\t\tdocked\n");
1085 len += sprintf(p + len, "commands:\tdock, undock\n");
1088 return len;
1091 static int dock_write(char *buf)
1093 char *cmd;
1095 if (!dock_docked())
1096 return -ENODEV;
1098 while ((cmd = next_cmd(&buf))) {
1099 if (strlencmp(cmd, "undock") == 0) {
1100 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1101 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1102 return -EIO;
1103 } else if (strlencmp(cmd, "dock") == 0) {
1104 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1105 return -EIO;
1106 } else
1107 return -EINVAL;
1110 return 0;
1113 #endif /* CONFIG_ACPI_IBM_DOCK */
1115 /*************************************************************************
1116 * Bay subdriver
1119 #ifdef CONFIG_ACPI_IBM_BAY
1120 static int bay_status_supported;
1121 static int bay_status2_supported;
1122 static int bay_eject_supported;
1123 static int bay_eject2_supported;
1125 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1126 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1127 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1128 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1129 ); /* A21e, R30, R31 */
1130 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1131 "_EJ0", /* all others */
1132 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1133 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1134 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1135 ); /* all others */
1136 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1137 "_EJ0", /* 770x */
1138 ); /* all others */
1140 static int bay_init(void)
1142 bay_status_supported = bay_handle &&
1143 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1144 bay_status2_supported = bay2_handle &&
1145 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1147 bay_eject_supported = bay_handle && bay_ej_handle &&
1148 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1149 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1150 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1152 return 0;
1155 static void bay_notify(struct ibm_struct *ibm, u32 event)
1157 acpi_bus_generate_event(ibm->device, event, 0);
1160 #define bay_occupied(b) (_sta(b##_handle) & 1)
1162 static int bay_read(char *p)
1164 int len = 0;
1165 int occupied = bay_occupied(bay);
1166 int occupied2 = bay_occupied(bay2);
1167 int eject, eject2;
1169 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1170 (occupied ? "occupied" : "unoccupied") :
1171 "not supported");
1172 if (bay_status2_supported)
1173 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1174 "occupied" : "unoccupied");
1176 eject = bay_eject_supported && occupied;
1177 eject2 = bay_eject2_supported && occupied2;
1179 if (eject && eject2)
1180 len += sprintf(p + len, "commands:\teject, eject2\n");
1181 else if (eject)
1182 len += sprintf(p + len, "commands:\teject\n");
1183 else if (eject2)
1184 len += sprintf(p + len, "commands:\teject2\n");
1186 return len;
1189 static int bay_write(char *buf)
1191 char *cmd;
1193 if (!bay_eject_supported && !bay_eject2_supported)
1194 return -ENODEV;
1196 while ((cmd = next_cmd(&buf))) {
1197 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1198 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1199 return -EIO;
1200 } else if (bay_eject2_supported &&
1201 strlencmp(cmd, "eject2") == 0) {
1202 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1203 return -EIO;
1204 } else
1205 return -EINVAL;
1208 return 0;
1210 #endif /* CONFIG_ACPI_IBM_BAY */
1212 /*************************************************************************
1213 * CMOS subdriver
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_read(char *p)
1226 int len = 0;
1228 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1229 R30, R31, T20-22, X20-21 */
1230 if (!cmos_handle)
1231 len += sprintf(p + len, "status:\t\tnot supported\n");
1232 else {
1233 len += sprintf(p + len, "status:\t\tsupported\n");
1234 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1237 return len;
1240 static int cmos_write(char *buf)
1242 char *cmd;
1243 int cmos_cmd;
1245 if (!cmos_handle)
1246 return -EINVAL;
1248 while ((cmd = next_cmd(&buf))) {
1249 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1250 cmos_cmd >= 0 && cmos_cmd <= 21) {
1251 /* cmos_cmd set */
1252 } else
1253 return -EINVAL;
1255 if (!cmos_eval(cmos_cmd))
1256 return -EIO;
1259 return 0;
1263 /*************************************************************************
1264 * LED subdriver
1267 static enum led_access_mode led_supported;
1269 IBM_HANDLE(led, ec, "SLED", /* 570 */
1270 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1271 "LED", /* all others */
1272 ); /* R30, R31 */
1274 static int led_init(void)
1276 if (!led_handle)
1277 /* led not supported on R30, R31 */
1278 led_supported = IBMACPI_LED_NONE;
1279 else if (strlencmp(led_path, "SLED") == 0)
1280 /* 570 */
1281 led_supported = IBMACPI_LED_570;
1282 else if (strlencmp(led_path, "SYSL") == 0)
1283 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1284 led_supported = IBMACPI_LED_OLD;
1285 else
1286 /* all others */
1287 led_supported = IBMACPI_LED_NEW;
1289 return 0;
1292 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1294 static int led_read(char *p)
1296 int len = 0;
1298 if (!led_supported) {
1299 len += sprintf(p + len, "status:\t\tnot supported\n");
1300 return len;
1302 len += sprintf(p + len, "status:\t\tsupported\n");
1304 if (led_supported == IBMACPI_LED_570) {
1305 /* 570 */
1306 int i, status;
1307 for (i = 0; i < 8; i++) {
1308 if (!acpi_evalf(ec_handle,
1309 &status, "GLED", "dd", 1 << i))
1310 return -EIO;
1311 len += sprintf(p + len, "%d:\t\t%s\n",
1312 i, led_status(status));
1316 len += sprintf(p + len, "commands:\t"
1317 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1319 return len;
1322 /* off, on, blink */
1323 static const int led_sled_arg1[] = { 0, 1, 3 };
1324 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1325 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1326 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1328 static int led_write(char *buf)
1330 char *cmd;
1331 int led, ind, ret;
1333 if (!led_supported)
1334 return -ENODEV;
1336 while ((cmd = next_cmd(&buf))) {
1337 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1338 return -EINVAL;
1340 if (strstr(cmd, "off")) {
1341 ind = 0;
1342 } else if (strstr(cmd, "on")) {
1343 ind = 1;
1344 } else if (strstr(cmd, "blink")) {
1345 ind = 2;
1346 } else
1347 return -EINVAL;
1349 if (led_supported == IBMACPI_LED_570) {
1350 /* 570 */
1351 led = 1 << led;
1352 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1353 led, led_sled_arg1[ind]))
1354 return -EIO;
1355 } else if (led_supported == IBMACPI_LED_OLD) {
1356 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1357 led = 1 << led;
1358 ret = ec_write(IBMACPI_LED_EC_HLMS, led);
1359 if (ret >= 0)
1360 ret =
1361 ec_write(IBMACPI_LED_EC_HLBL,
1362 led * led_exp_hlbl[ind]);
1363 if (ret >= 0)
1364 ret =
1365 ec_write(IBMACPI_LED_EC_HLCL,
1366 led * led_exp_hlcl[ind]);
1367 if (ret < 0)
1368 return ret;
1369 } else {
1370 /* all others */
1371 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1372 led, led_led_arg1[ind]))
1373 return -EIO;
1377 return 0;
1380 /*************************************************************************
1381 * Beep subdriver
1384 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1386 static int beep_read(char *p)
1388 int len = 0;
1390 if (!beep_handle)
1391 len += sprintf(p + len, "status:\t\tnot supported\n");
1392 else {
1393 len += sprintf(p + len, "status:\t\tsupported\n");
1394 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1397 return len;
1400 static int beep_write(char *buf)
1402 char *cmd;
1403 int beep_cmd;
1405 if (!beep_handle)
1406 return -ENODEV;
1408 while ((cmd = next_cmd(&buf))) {
1409 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1410 beep_cmd >= 0 && beep_cmd <= 17) {
1411 /* beep_cmd set */
1412 } else
1413 return -EINVAL;
1414 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1415 return -EIO;
1418 return 0;
1421 /*************************************************************************
1422 * Thermal subdriver
1425 static enum thermal_access_mode thermal_read_mode;
1427 static int thermal_init(void)
1429 u8 t, ta1, ta2;
1430 int i;
1431 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1433 if (ibm_thinkpad_ec_found && experimental) {
1435 * Direct EC access mode: sensors at registers
1436 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1437 * non-implemented, thermal sensors return 0x80 when
1438 * not available
1441 ta1 = ta2 = 0;
1442 for (i = 0; i < 8; i++) {
1443 if (likely(acpi_ec_read(0x78 + i, &t))) {
1444 ta1 |= t;
1445 } else {
1446 ta1 = 0;
1447 break;
1449 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1450 ta2 |= t;
1451 } else {
1452 ta1 = 0;
1453 break;
1456 if (ta1 == 0) {
1457 /* This is sheer paranoia, but we handle it anyway */
1458 if (acpi_tmp7) {
1459 printk(IBM_ERR
1460 "ThinkPad ACPI EC access misbehaving, "
1461 "falling back to ACPI TMPx access mode\n");
1462 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1463 } else {
1464 printk(IBM_ERR
1465 "ThinkPad ACPI EC access misbehaving, "
1466 "disabling thermal sensors access\n");
1467 thermal_read_mode = IBMACPI_THERMAL_NONE;
1469 } else {
1470 thermal_read_mode =
1471 (ta2 != 0) ?
1472 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1474 } else if (acpi_tmp7) {
1475 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1476 /* 600e/x, 770e, 770x */
1477 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1478 } else {
1479 /* Standard ACPI TMPx access, max 8 sensors */
1480 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1482 } else {
1483 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1484 thermal_read_mode = IBMACPI_THERMAL_NONE;
1487 return 0;
1490 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1492 int i, t;
1493 s8 tmp;
1494 char tmpi[] = "TMPi";
1496 if (!s)
1497 return -EINVAL;
1499 switch (thermal_read_mode) {
1500 #if IBMACPI_MAX_THERMAL_SENSORS >= 16
1501 case IBMACPI_THERMAL_TPEC_16:
1502 for (i = 0; i < 8; i++) {
1503 if (!acpi_ec_read(0xC0 + i, &tmp))
1504 return -EIO;
1505 s->temp[i + 8] = tmp * 1000;
1507 /* fallthrough */
1508 #endif
1509 case IBMACPI_THERMAL_TPEC_8:
1510 for (i = 0; i < 8; i++) {
1511 if (!acpi_ec_read(0x78 + i, &tmp))
1512 return -EIO;
1513 s->temp[i] = tmp * 1000;
1515 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1517 case IBMACPI_THERMAL_ACPI_UPDT:
1518 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1519 return -EIO;
1520 for (i = 0; i < 8; i++) {
1521 tmpi[3] = '0' + i;
1522 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1523 return -EIO;
1524 s->temp[i] = (t - 2732) * 100;
1526 return 8;
1528 case IBMACPI_THERMAL_ACPI_TMP07:
1529 for (i = 0; i < 8; i++) {
1530 tmpi[3] = '0' + i;
1531 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1532 return -EIO;
1533 s->temp[i] = t * 1000;
1535 return 8;
1537 case IBMACPI_THERMAL_NONE:
1538 default:
1539 return 0;
1543 static int thermal_read(char *p)
1545 int len = 0;
1546 int n, i;
1547 struct ibm_thermal_sensors_struct t;
1549 n = thermal_get_sensors(&t);
1550 if (unlikely(n < 0))
1551 return n;
1553 len += sprintf(p + len, "temperatures:\t");
1555 if (n > 0) {
1556 for (i = 0; i < (n - 1); i++)
1557 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1558 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1559 } else
1560 len += sprintf(p + len, "not supported\n");
1562 return len;
1565 /*************************************************************************
1566 * EC Dump subdriver
1569 static u8 ecdump_regs[256];
1571 static int ecdump_read(char *p)
1573 int len = 0;
1574 int i, j;
1575 u8 v;
1577 len += sprintf(p + len, "EC "
1578 " +00 +01 +02 +03 +04 +05 +06 +07"
1579 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1580 for (i = 0; i < 256; i += 16) {
1581 len += sprintf(p + len, "EC 0x%02x:", i);
1582 for (j = 0; j < 16; j++) {
1583 if (!acpi_ec_read(i + j, &v))
1584 break;
1585 if (v != ecdump_regs[i + j])
1586 len += sprintf(p + len, " *%02x", v);
1587 else
1588 len += sprintf(p + len, " %02x", v);
1589 ecdump_regs[i + j] = v;
1591 len += sprintf(p + len, "\n");
1592 if (j != 16)
1593 break;
1596 /* These are way too dangerous to advertise openly... */
1597 #if 0
1598 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1599 " (<offset> is 00-ff, <value> is 00-ff)\n");
1600 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1601 " (<offset> is 00-ff, <value> is 0-255)\n");
1602 #endif
1603 return len;
1606 static int ecdump_write(char *buf)
1608 char *cmd;
1609 int i, v;
1611 while ((cmd = next_cmd(&buf))) {
1612 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1613 /* i and v set */
1614 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1615 /* i and v set */
1616 } else
1617 return -EINVAL;
1618 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1619 if (!acpi_ec_write(i, v))
1620 return -EIO;
1621 } else
1622 return -EINVAL;
1625 return 0;
1628 /*************************************************************************
1629 * Backlight/brightness subdriver
1632 static struct backlight_device *ibm_backlight_device = NULL;
1634 static struct backlight_properties ibm_backlight_data = {
1635 .owner = THIS_MODULE,
1636 .get_brightness = brightness_get,
1637 .update_status = brightness_update_status,
1638 .max_brightness = 7,
1641 static int brightness_init(void)
1643 int b;
1645 b = brightness_get(NULL);
1646 if (b < 0)
1647 return b;
1649 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
1650 &ibm_backlight_data);
1651 if (IS_ERR(ibm_backlight_device)) {
1652 printk(IBM_ERR "Could not register backlight device\n");
1653 return PTR_ERR(ibm_backlight_device);
1656 ibm_backlight_device->props->brightness = b;
1657 brightness_update_status(ibm_backlight_device);
1659 return 0;
1662 static void brightness_exit(void)
1664 if (ibm_backlight_device) {
1665 backlight_device_unregister(ibm_backlight_device);
1666 ibm_backlight_device = NULL;
1670 static int brightness_update_status(struct backlight_device *bd)
1672 return brightness_set(
1673 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
1674 bd->props->power == FB_BLANK_UNBLANK) ?
1675 bd->props->brightness : 0);
1678 static int brightness_get(struct backlight_device *bd)
1680 u8 level;
1681 if (!acpi_ec_read(brightness_offset, &level))
1682 return -EIO;
1684 level &= 0x7;
1686 return level;
1689 static int brightness_set(int value)
1691 int cmos_cmd, inc, i;
1692 int current_value = brightness_get(NULL);
1694 value &= 7;
1696 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
1697 inc = value > current_value ? 1 : -1;
1698 for (i = current_value; i != value; i += inc) {
1699 if (!cmos_eval(cmos_cmd))
1700 return -EIO;
1701 if (!acpi_ec_write(brightness_offset, i + inc))
1702 return -EIO;
1705 return 0;
1708 static int brightness_read(char *p)
1710 int len = 0;
1711 int level;
1713 if ((level = brightness_get(NULL)) < 0) {
1714 len += sprintf(p + len, "level:\t\tunreadable\n");
1715 } else {
1716 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1717 len += sprintf(p + len, "commands:\tup, down\n");
1718 len += sprintf(p + len, "commands:\tlevel <level>"
1719 " (<level> is 0-7)\n");
1722 return len;
1725 static int brightness_write(char *buf)
1727 int level;
1728 int new_level;
1729 char *cmd;
1731 while ((cmd = next_cmd(&buf))) {
1732 if ((level = brightness_get(NULL)) < 0)
1733 return level;
1734 level &= 7;
1736 if (strlencmp(cmd, "up") == 0) {
1737 new_level = level == 7 ? 7 : level + 1;
1738 } else if (strlencmp(cmd, "down") == 0) {
1739 new_level = level == 0 ? 0 : level - 1;
1740 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1741 new_level >= 0 && new_level <= 7) {
1742 /* new_level set */
1743 } else
1744 return -EINVAL;
1746 brightness_set(new_level);
1749 return 0;
1752 /*************************************************************************
1753 * Volume subdriver
1756 static int volume_read(char *p)
1758 int len = 0;
1759 u8 level;
1761 if (!acpi_ec_read(volume_offset, &level)) {
1762 len += sprintf(p + len, "level:\t\tunreadable\n");
1763 } else {
1764 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1765 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1766 len += sprintf(p + len, "commands:\tup, down, mute\n");
1767 len += sprintf(p + len, "commands:\tlevel <level>"
1768 " (<level> is 0-15)\n");
1771 return len;
1774 static int volume_write(char *buf)
1776 int cmos_cmd, inc, i;
1777 u8 level, mute;
1778 int new_level, new_mute;
1779 char *cmd;
1781 while ((cmd = next_cmd(&buf))) {
1782 if (!acpi_ec_read(volume_offset, &level))
1783 return -EIO;
1784 new_mute = mute = level & 0x40;
1785 new_level = level = level & 0xf;
1787 if (strlencmp(cmd, "up") == 0) {
1788 if (mute)
1789 new_mute = 0;
1790 else
1791 new_level = level == 15 ? 15 : level + 1;
1792 } else if (strlencmp(cmd, "down") == 0) {
1793 if (mute)
1794 new_mute = 0;
1795 else
1796 new_level = level == 0 ? 0 : level - 1;
1797 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1798 new_level >= 0 && new_level <= 15) {
1799 /* new_level set */
1800 } else if (strlencmp(cmd, "mute") == 0) {
1801 new_mute = 0x40;
1802 } else
1803 return -EINVAL;
1805 if (new_level != level) { /* mute doesn't change */
1806 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
1807 inc = new_level > level ? 1 : -1;
1809 if (mute && (!cmos_eval(cmos_cmd) ||
1810 !acpi_ec_write(volume_offset, level)))
1811 return -EIO;
1813 for (i = level; i != new_level; i += inc)
1814 if (!cmos_eval(cmos_cmd) ||
1815 !acpi_ec_write(volume_offset, i + inc))
1816 return -EIO;
1818 if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
1819 !acpi_ec_write(volume_offset,
1820 new_level + mute)))
1821 return -EIO;
1824 if (new_mute != mute) { /* level doesn't change */
1825 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
1827 if (!cmos_eval(cmos_cmd) ||
1828 !acpi_ec_write(volume_offset, level + new_mute))
1829 return -EIO;
1833 return 0;
1837 /*************************************************************************
1838 * Fan subdriver
1842 * FAN ACCESS MODES
1844 * IBMACPI_FAN_RD_ACPI_GFAN:
1845 * ACPI GFAN method: returns fan level
1847 * see IBMACPI_FAN_WR_ACPI_SFAN
1848 * EC 0x2f not available if GFAN exists
1850 * IBMACPI_FAN_WR_ACPI_SFAN:
1851 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
1853 * EC 0x2f might be available *for reading*, but never for writing.
1855 * IBMACPI_FAN_WR_TPEC:
1856 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
1857 * on almost all ThinkPads
1859 * Fan speed changes of any sort (including those caused by the
1860 * disengaged mode) are usually done slowly by the firmware as the
1861 * maximum ammount of fan duty cycle change per second seems to be
1862 * limited.
1864 * Reading is not available if GFAN exists.
1865 * Writing is not available if SFAN exists.
1867 * Bits
1868 * 7 automatic mode engaged;
1869 * (default operation mode of the ThinkPad)
1870 * fan level is ignored in this mode.
1871 * 6 disengage mode (takes precedence over bit 7);
1872 * not available on all thinkpads. May disable
1873 * the tachometer, and speeds up fan to 100% duty-cycle,
1874 * which speeds it up far above the standard RPM
1875 * levels. It is not impossible that it could cause
1876 * hardware damage.
1877 * 5-3 unused in some models. Extra bits for fan level
1878 * in others, but still useless as all values above
1879 * 7 map to the same speed as level 7 in these models.
1880 * 2-0 fan level (0..7 usually)
1881 * 0x00 = stop
1882 * 0x07 = max (set when temperatures critical)
1883 * Some ThinkPads may have other levels, see
1884 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
1886 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
1887 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
1888 * does so, its initial value is meaningless (0x07).
1890 * For firmware bugs, refer to:
1891 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1893 * ----
1895 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
1896 * Main fan tachometer reading (in RPM)
1898 * This register is present on all ThinkPads with a new-style EC, and
1899 * it is known not to be present on the A21m/e, and T22, as there is
1900 * something else in offset 0x84 according to the ACPI DSDT. Other
1901 * ThinkPads from this same time period (and earlier) probably lack the
1902 * tachometer as well.
1904 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
1905 * was never fixed by IBM to report the EC firmware version string
1906 * probably support the tachometer (like the early X models), so
1907 * detecting it is quite hard. We need more data to know for sure.
1909 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
1910 * might result.
1912 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
1913 * register is not invalidated in ThinkPads that disable tachometer
1914 * readings. Thus, the tachometer readings go stale.
1916 * For firmware bugs, refer to:
1917 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1919 * IBMACPI_FAN_WR_ACPI_FANS:
1920 * ThinkPad X31, X40, X41. Not available in the X60.
1922 * FANS ACPI handle: takes three arguments: low speed, medium speed,
1923 * high speed. ACPI DSDT seems to map these three speeds to levels
1924 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
1925 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
1927 * The speeds are stored on handles
1928 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
1930 * There are three default speed sets, acessible as handles:
1931 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
1933 * ACPI DSDT switches which set is in use depending on various
1934 * factors.
1936 * IBMACPI_FAN_WR_TPEC is also available and should be used to
1937 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
1938 * but the ACPI tables just mention level 7.
1941 static enum fan_status_access_mode fan_status_access_mode;
1942 static enum fan_control_access_mode fan_control_access_mode;
1943 static enum fan_control_commands fan_control_commands;
1945 static int fan_control_status_known;
1946 static u8 fan_control_initial_status;
1948 static void fan_watchdog_fire(struct work_struct *ignored);
1949 static int fan_watchdog_maxinterval;
1950 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
1952 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
1953 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
1954 "\\FSPD", /* 600e/x, 770e, 770x */
1955 ); /* all others */
1956 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
1957 "JFNS", /* 770x-JL */
1958 ); /* all others */
1960 static int fan_init(void)
1962 fan_status_access_mode = IBMACPI_FAN_NONE;
1963 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1964 fan_control_commands = 0;
1965 fan_control_status_known = 1;
1966 fan_watchdog_maxinterval = 0;
1968 if (gfan_handle) {
1969 /* 570, 600e/x, 770e, 770x */
1970 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1971 } else {
1972 /* all other ThinkPads: note that even old-style
1973 * ThinkPad ECs supports the fan control register */
1974 if (likely(acpi_ec_read(fan_status_offset,
1975 &fan_control_initial_status))) {
1976 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1978 /* In some ThinkPads, neither the EC nor the ACPI
1979 * DSDT initialize the fan status, and it ends up
1980 * being set to 0x07 when it *could* be either
1981 * 0x07 or 0x80.
1983 * Enable for TP-1Y (T43), TP-78 (R51e),
1984 * TP-76 (R52), TP-70 (T43, R52), which are known
1985 * to be buggy. */
1986 if (fan_control_initial_status == 0x07 &&
1987 ibm_thinkpad_ec_found &&
1988 ((ibm_thinkpad_ec_found[0] == '1' &&
1989 ibm_thinkpad_ec_found[1] == 'Y') ||
1990 (ibm_thinkpad_ec_found[0] == '7' &&
1991 (ibm_thinkpad_ec_found[1] == '6' ||
1992 ibm_thinkpad_ec_found[1] == '8' ||
1993 ibm_thinkpad_ec_found[1] == '0'))
1994 )) {
1995 printk(IBM_NOTICE
1996 "fan_init: initial fan status is "
1997 "unknown, assuming it is in auto "
1998 "mode\n");
1999 fan_control_status_known = 0;
2001 } else {
2002 printk(IBM_ERR
2003 "ThinkPad ACPI EC access misbehaving, "
2004 "fan status and control unavailable\n");
2005 return 0;
2009 if (sfan_handle) {
2010 /* 570, 770x-JL */
2011 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
2012 fan_control_commands |=
2013 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
2014 } else {
2015 if (!gfan_handle) {
2016 /* gfan without sfan means no fan control */
2017 /* all other models implement TP EC 0x2f control */
2019 if (fans_handle) {
2020 /* X31, X40, X41 */
2021 fan_control_access_mode =
2022 IBMACPI_FAN_WR_ACPI_FANS;
2023 fan_control_commands |=
2024 IBMACPI_FAN_CMD_SPEED |
2025 IBMACPI_FAN_CMD_LEVEL |
2026 IBMACPI_FAN_CMD_ENABLE;
2027 } else {
2028 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
2029 fan_control_commands |=
2030 IBMACPI_FAN_CMD_LEVEL |
2031 IBMACPI_FAN_CMD_ENABLE;
2036 return 0;
2039 static int fan_get_status(u8 *status)
2041 u8 s;
2043 /* TODO:
2044 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
2046 switch (fan_status_access_mode) {
2047 case IBMACPI_FAN_RD_ACPI_GFAN:
2048 /* 570, 600e/x, 770e, 770x */
2050 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
2051 return -EIO;
2053 if (likely(status))
2054 *status = s & 0x07;
2056 break;
2058 case IBMACPI_FAN_RD_TPEC:
2059 /* all except 570, 600e/x, 770e, 770x */
2060 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2061 return -EIO;
2063 if (likely(status))
2064 *status = s;
2066 break;
2068 default:
2069 return -ENXIO;
2072 return 0;
2075 static void fan_exit(void)
2077 cancel_delayed_work(&fan_watchdog_task);
2078 flush_scheduled_work();
2081 static int fan_get_speed(unsigned int *speed)
2083 u8 hi, lo;
2085 switch (fan_status_access_mode) {
2086 case IBMACPI_FAN_RD_TPEC:
2087 /* all except 570, 600e/x, 770e, 770x */
2088 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2089 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2090 return -EIO;
2092 if (likely(speed))
2093 *speed = (hi << 8) | lo;
2095 break;
2097 default:
2098 return -ENXIO;
2101 return 0;
2104 static void fan_watchdog_fire(struct work_struct *ignored)
2106 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2107 if (fan_set_enable()) {
2108 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2109 /* reschedule for later */
2110 fan_watchdog_reset();
2114 static void fan_watchdog_reset(void)
2116 static int fan_watchdog_active = 0;
2118 if (fan_watchdog_active)
2119 cancel_delayed_work(&fan_watchdog_task);
2121 if (fan_watchdog_maxinterval > 0) {
2122 fan_watchdog_active = 1;
2123 if (!schedule_delayed_work(&fan_watchdog_task,
2124 msecs_to_jiffies(fan_watchdog_maxinterval
2125 * 1000))) {
2126 printk(IBM_ERR "failed to schedule the fan watchdog, "
2127 "watchdog will not trigger\n");
2129 } else
2130 fan_watchdog_active = 0;
2133 static int fan_set_level(int level)
2135 switch (fan_control_access_mode) {
2136 case IBMACPI_FAN_WR_ACPI_SFAN:
2137 if (level >= 0 && level <= 7) {
2138 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2139 return -EIO;
2140 } else
2141 return -EINVAL;
2142 break;
2144 case IBMACPI_FAN_WR_ACPI_FANS:
2145 case IBMACPI_FAN_WR_TPEC:
2146 if ((level != IBMACPI_FAN_EC_AUTO) &&
2147 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2148 ((level < 0) || (level > 7)))
2149 return -EINVAL;
2151 if (!acpi_ec_write(fan_status_offset, level))
2152 return -EIO;
2153 else
2154 fan_control_status_known = 1;
2155 break;
2157 default:
2158 return -ENXIO;
2160 return 0;
2163 static int fan_set_enable(void)
2165 u8 s;
2166 int rc;
2168 switch (fan_control_access_mode) {
2169 case IBMACPI_FAN_WR_ACPI_FANS:
2170 case IBMACPI_FAN_WR_TPEC:
2171 if ((rc = fan_get_status(&s)) < 0)
2172 return rc;
2174 /* Don't go out of emergency fan mode */
2175 if (s != 7)
2176 s = IBMACPI_FAN_EC_AUTO;
2178 if (!acpi_ec_write(fan_status_offset, s))
2179 return -EIO;
2180 else
2181 fan_control_status_known = 1;
2182 break;
2184 case IBMACPI_FAN_WR_ACPI_SFAN:
2185 if ((rc = fan_get_status(&s)) < 0)
2186 return rc;
2188 s &= 0x07;
2190 /* Set fan to at least level 4 */
2191 if (s < 4)
2192 s = 4;
2194 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2195 return -EIO;
2196 break;
2198 default:
2199 return -ENXIO;
2201 return 0;
2204 static int fan_set_disable(void)
2206 switch (fan_control_access_mode) {
2207 case IBMACPI_FAN_WR_ACPI_FANS:
2208 case IBMACPI_FAN_WR_TPEC:
2209 if (!acpi_ec_write(fan_status_offset, 0x00))
2210 return -EIO;
2211 else
2212 fan_control_status_known = 1;
2213 break;
2215 case IBMACPI_FAN_WR_ACPI_SFAN:
2216 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2217 return -EIO;
2218 break;
2220 default:
2221 return -ENXIO;
2223 return 0;
2226 static int fan_set_speed(int speed)
2228 switch (fan_control_access_mode) {
2229 case IBMACPI_FAN_WR_ACPI_FANS:
2230 if (speed >= 0 && speed <= 65535) {
2231 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2232 speed, speed, speed))
2233 return -EIO;
2234 } else
2235 return -EINVAL;
2236 break;
2238 default:
2239 return -ENXIO;
2241 return 0;
2244 static int fan_read(char *p)
2246 int len = 0;
2247 int rc;
2248 u8 status;
2249 unsigned int speed = 0;
2251 switch (fan_status_access_mode) {
2252 case IBMACPI_FAN_RD_ACPI_GFAN:
2253 /* 570, 600e/x, 770e, 770x */
2254 if ((rc = fan_get_status(&status)) < 0)
2255 return rc;
2257 len += sprintf(p + len, "status:\t\t%s\n"
2258 "level:\t\t%d\n",
2259 (status != 0) ? "enabled" : "disabled", status);
2260 break;
2262 case IBMACPI_FAN_RD_TPEC:
2263 /* all except 570, 600e/x, 770e, 770x */
2264 if ((rc = fan_get_status(&status)) < 0)
2265 return rc;
2267 if (unlikely(!fan_control_status_known)) {
2268 if (status != fan_control_initial_status)
2269 fan_control_status_known = 1;
2270 else
2271 /* Return most likely status. In fact, it
2272 * might be the only possible status */
2273 status = IBMACPI_FAN_EC_AUTO;
2276 len += sprintf(p + len, "status:\t\t%s\n",
2277 (status != 0) ? "enabled" : "disabled");
2279 /* No ThinkPad boots on disengaged mode, we can safely
2280 * assume the tachometer is online if fan control status
2281 * was unknown */
2282 if ((rc = fan_get_speed(&speed)) < 0)
2283 return rc;
2285 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2287 if (status & IBMACPI_FAN_EC_DISENGAGED)
2288 /* Disengaged mode takes precedence */
2289 len += sprintf(p + len, "level:\t\tdisengaged\n");
2290 else if (status & IBMACPI_FAN_EC_AUTO)
2291 len += sprintf(p + len, "level:\t\tauto\n");
2292 else
2293 len += sprintf(p + len, "level:\t\t%d\n", status);
2294 break;
2296 case IBMACPI_FAN_NONE:
2297 default:
2298 len += sprintf(p + len, "status:\t\tnot supported\n");
2301 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2302 len += sprintf(p + len, "commands:\tlevel <level>");
2304 switch (fan_control_access_mode) {
2305 case IBMACPI_FAN_WR_ACPI_SFAN:
2306 len += sprintf(p + len, " (<level> is 0-7)\n");
2307 break;
2309 default:
2310 len += sprintf(p + len, " (<level> is 0-7, "
2311 "auto, disengaged)\n");
2312 break;
2316 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
2317 len += sprintf(p + len, "commands:\tenable, disable\n"
2318 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2319 "1-120 (seconds))\n");
2321 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
2322 len += sprintf(p + len, "commands:\tspeed <speed>"
2323 " (<speed> is 0-65535)\n");
2325 return len;
2328 static int fan_write_cmd_level(const char *cmd, int *rc)
2330 int level;
2332 if (strlencmp(cmd, "level auto") == 0)
2333 level = IBMACPI_FAN_EC_AUTO;
2334 else if (strlencmp(cmd, "level disengaged") == 0)
2335 level = IBMACPI_FAN_EC_DISENGAGED;
2336 else if (sscanf(cmd, "level %d", &level) != 1)
2337 return 0;
2339 if ((*rc = fan_set_level(level)) == -ENXIO)
2340 printk(IBM_ERR "level command accepted for unsupported "
2341 "access mode %d", fan_control_access_mode);
2343 return 1;
2346 static int fan_write_cmd_enable(const char *cmd, int *rc)
2348 if (strlencmp(cmd, "enable") != 0)
2349 return 0;
2351 if ((*rc = fan_set_enable()) == -ENXIO)
2352 printk(IBM_ERR "enable command accepted for unsupported "
2353 "access mode %d", fan_control_access_mode);
2355 return 1;
2358 static int fan_write_cmd_disable(const char *cmd, int *rc)
2360 if (strlencmp(cmd, "disable") != 0)
2361 return 0;
2363 if ((*rc = fan_set_disable()) == -ENXIO)
2364 printk(IBM_ERR "disable command accepted for unsupported "
2365 "access mode %d", fan_control_access_mode);
2367 return 1;
2370 static int fan_write_cmd_speed(const char *cmd, int *rc)
2372 int speed;
2374 /* TODO:
2375 * Support speed <low> <medium> <high> ? */
2377 if (sscanf(cmd, "speed %d", &speed) != 1)
2378 return 0;
2380 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2381 printk(IBM_ERR "speed command accepted for unsupported "
2382 "access mode %d", fan_control_access_mode);
2384 return 1;
2387 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2389 int interval;
2391 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2392 return 0;
2394 if (interval < 0 || interval > 120)
2395 *rc = -EINVAL;
2396 else
2397 fan_watchdog_maxinterval = interval;
2399 return 1;
2402 static int fan_write(char *buf)
2404 char *cmd;
2405 int rc = 0;
2407 while (!rc && (cmd = next_cmd(&buf))) {
2408 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2409 fan_write_cmd_level(cmd, &rc)) &&
2410 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2411 (fan_write_cmd_enable(cmd, &rc) ||
2412 fan_write_cmd_disable(cmd, &rc) ||
2413 fan_write_cmd_watchdog(cmd, &rc))) &&
2414 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2415 fan_write_cmd_speed(cmd, &rc))
2417 rc = -EINVAL;
2418 else if (!rc)
2419 fan_watchdog_reset();
2422 return rc;
2425 /****************************************************************************
2426 ****************************************************************************
2428 * Infrastructure
2430 ****************************************************************************
2431 ****************************************************************************/
2433 /* /proc support */
2434 static struct proc_dir_entry *proc_dir = NULL;
2436 /* Subdriver registry */
2437 static struct ibm_struct ibms[] = {
2439 .name = "driver",
2440 .init = ibm_acpi_driver_init,
2441 .read = ibm_acpi_driver_read,
2444 .name = "hotkey",
2445 .hid = IBM_HKEY_HID,
2446 .init = hotkey_init,
2447 .read = hotkey_read,
2448 .write = hotkey_write,
2449 .exit = hotkey_exit,
2450 .notify = hotkey_notify,
2451 .handle = &hkey_handle,
2452 .type = ACPI_DEVICE_NOTIFY,
2455 .name = "bluetooth",
2456 .init = bluetooth_init,
2457 .read = bluetooth_read,
2458 .write = bluetooth_write,
2461 .name = "wan",
2462 .init = wan_init,
2463 .read = wan_read,
2464 .write = wan_write,
2465 .experimental = 1,
2468 .name = "video",
2469 .init = video_init,
2470 .read = video_read,
2471 .write = video_write,
2472 .exit = video_exit,
2475 .name = "light",
2476 .init = light_init,
2477 .read = light_read,
2478 .write = light_write,
2480 #ifdef CONFIG_ACPI_IBM_DOCK
2482 .name = "dock",
2483 .read = dock_read,
2484 .write = dock_write,
2485 .notify = dock_notify,
2486 .handle = &dock_handle,
2487 .type = ACPI_SYSTEM_NOTIFY,
2490 .name = "dock",
2491 .hid = IBM_PCI_HID,
2492 .notify = dock_notify,
2493 .handle = &pci_handle,
2494 .type = ACPI_SYSTEM_NOTIFY,
2496 #endif
2497 #ifdef CONFIG_ACPI_IBM_BAY
2499 .name = "bay",
2500 .init = bay_init,
2501 .read = bay_read,
2502 .write = bay_write,
2503 .notify = bay_notify,
2504 .handle = &bay_handle,
2505 .type = ACPI_SYSTEM_NOTIFY,
2507 #endif /* CONFIG_ACPI_IBM_BAY */
2509 .name = "cmos",
2510 .read = cmos_read,
2511 .write = cmos_write,
2514 .name = "led",
2515 .init = led_init,
2516 .read = led_read,
2517 .write = led_write,
2520 .name = "beep",
2521 .read = beep_read,
2522 .write = beep_write,
2525 .name = "thermal",
2526 .init = thermal_init,
2527 .read = thermal_read,
2530 .name = "ecdump",
2531 .read = ecdump_read,
2532 .write = ecdump_write,
2533 .experimental = 1,
2536 .name = "brightness",
2537 .read = brightness_read,
2538 .write = brightness_write,
2539 .init = brightness_init,
2540 .exit = brightness_exit,
2543 .name = "volume",
2544 .read = volume_read,
2545 .write = volume_write,
2548 .name = "fan",
2549 .read = fan_read,
2550 .write = fan_write,
2551 .init = fan_init,
2552 .exit = fan_exit,
2553 .experimental = 1,
2558 * Module and infrastructure proble, init and exit handling
2561 static int __init ibm_init(struct ibm_struct *ibm)
2563 int ret;
2564 struct proc_dir_entry *entry;
2566 if (ibm->experimental && !experimental)
2567 return 0;
2569 if (ibm->hid) {
2570 ret = register_ibmacpi_subdriver(ibm);
2571 if (ret < 0)
2572 return ret;
2573 ibm->driver_registered = 1;
2576 if (ibm->init) {
2577 ret = ibm->init();
2578 if (ret != 0)
2579 return ret;
2580 ibm->init_called = 1;
2583 if (ibm->read) {
2584 entry = create_proc_entry(ibm->name,
2585 S_IFREG | S_IRUGO | S_IWUSR,
2586 proc_dir);
2587 if (!entry) {
2588 printk(IBM_ERR "unable to create proc entry %s\n",
2589 ibm->name);
2590 return -ENODEV;
2592 entry->owner = THIS_MODULE;
2593 entry->data = ibm;
2594 entry->read_proc = &dispatch_read;
2595 if (ibm->write)
2596 entry->write_proc = &dispatch_write;
2597 ibm->proc_created = 1;
2600 if (ibm->notify) {
2601 ret = setup_notify(ibm);
2602 if (ret == -ENODEV) {
2603 printk(IBM_NOTICE "disabling subdriver %s\n",
2604 ibm->name);
2605 ibm_exit(ibm);
2606 return 0;
2608 if (ret < 0)
2609 return ret;
2612 return 0;
2615 static void ibm_exit(struct ibm_struct *ibm)
2617 if (ibm->notify_installed)
2618 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2619 dispatch_notify);
2621 if (ibm->proc_created)
2622 remove_proc_entry(ibm->name, proc_dir);
2624 if (ibm->init_called && ibm->exit)
2625 ibm->exit();
2627 if (ibm->driver_registered) {
2628 acpi_bus_unregister_driver(ibm->driver);
2629 kfree(ibm->driver);
2633 /* Probing */
2635 static char *ibm_thinkpad_ec_found = NULL;
2637 static char* __init check_dmi_for_ec(void)
2639 struct dmi_device *dev = NULL;
2640 char ec_fw_string[18];
2643 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2644 * X32 or newer, all Z series; Some models must have an
2645 * up-to-date BIOS or they will not be detected.
2647 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2649 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2650 if (sscanf(dev->name,
2651 "IBM ThinkPad Embedded Controller -[%17c",
2652 ec_fw_string) == 1) {
2653 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2654 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2655 return kstrdup(ec_fw_string, GFP_KERNEL);
2658 return NULL;
2661 /* Module init, exit, parameters */
2663 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2665 unsigned int i;
2667 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2668 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2669 if (strlen(val) > sizeof(ibms[i].param) - 2)
2670 return -ENOSPC;
2671 strcpy(ibms[i].param, val);
2672 strcat(ibms[i].param, ",");
2673 return 0;
2676 return -EINVAL;
2679 static int experimental;
2680 module_param(experimental, int, 0);
2682 #define IBM_PARAM(feature) \
2683 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2685 IBM_PARAM(hotkey);
2686 IBM_PARAM(bluetooth);
2687 IBM_PARAM(video);
2688 IBM_PARAM(light);
2689 #ifdef CONFIG_ACPI_IBM_DOCK
2690 IBM_PARAM(dock);
2691 #endif
2692 #ifdef CONFIG_ACPI_IBM_BAY
2693 IBM_PARAM(bay);
2694 #endif /* CONFIG_ACPI_IBM_BAY */
2695 IBM_PARAM(cmos);
2696 IBM_PARAM(led);
2697 IBM_PARAM(beep);
2698 IBM_PARAM(ecdump);
2699 IBM_PARAM(brightness);
2700 IBM_PARAM(volume);
2701 IBM_PARAM(fan);
2703 static int __init acpi_ibm_init(void)
2705 int ret, i;
2707 if (acpi_disabled)
2708 return -ENODEV;
2710 if (!acpi_specific_hotkey_enabled) {
2711 printk(IBM_ERR "using generic hotkey driver\n");
2712 return -ENODEV;
2715 /* ec is required because many other handles are relative to it */
2716 IBM_HANDLE_INIT(ec);
2717 if (!ec_handle) {
2718 printk(IBM_ERR "ec object not found\n");
2719 return -ENODEV;
2722 /* Models with newer firmware report the EC in DMI */
2723 ibm_thinkpad_ec_found = check_dmi_for_ec();
2725 /* these handles are not required */
2726 IBM_HANDLE_INIT(vid);
2727 IBM_HANDLE_INIT(vid2);
2728 IBM_HANDLE_INIT(ledb);
2729 IBM_HANDLE_INIT(led);
2730 IBM_HANDLE_INIT(hkey);
2731 IBM_HANDLE_INIT(lght);
2732 IBM_HANDLE_INIT(cmos);
2733 #ifdef CONFIG_ACPI_IBM_DOCK
2734 IBM_HANDLE_INIT(dock);
2735 #endif
2736 IBM_HANDLE_INIT(pci);
2737 #ifdef CONFIG_ACPI_IBM_BAY
2738 IBM_HANDLE_INIT(bay);
2739 if (bay_handle)
2740 IBM_HANDLE_INIT(bay_ej);
2741 IBM_HANDLE_INIT(bay2);
2742 if (bay2_handle)
2743 IBM_HANDLE_INIT(bay2_ej);
2744 #endif /* CONFIG_ACPI_IBM_BAY */
2745 IBM_HANDLE_INIT(beep);
2746 IBM_HANDLE_INIT(ecrd);
2747 IBM_HANDLE_INIT(ecwr);
2748 IBM_HANDLE_INIT(fans);
2749 IBM_HANDLE_INIT(gfan);
2750 IBM_HANDLE_INIT(sfan);
2752 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2753 if (!proc_dir) {
2754 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2755 acpi_ibm_exit();
2756 return -ENODEV;
2758 proc_dir->owner = THIS_MODULE;
2760 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
2761 ret = ibm_init(&ibms[i]);
2762 if (ret >= 0 && *ibms[i].param)
2763 ret = ibms[i].write(ibms[i].param);
2764 if (ret < 0) {
2765 acpi_ibm_exit();
2766 return ret;
2770 return 0;
2773 static void acpi_ibm_exit(void)
2775 int i;
2777 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2778 ibm_exit(&ibms[i]);
2780 if (proc_dir)
2781 remove_proc_entry(IBM_DIR, acpi_root_dir);
2783 if (ibm_thinkpad_ec_found)
2784 kfree(ibm_thinkpad_ec_found);
2787 module_init(acpi_ibm_init);
2788 module_exit(acpi_ibm_exit);