ACPI: thinkpad-acpi: add debug mode
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob6d0cc868adeb909d044d8f601f00974df7a1adf2
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define IBM_VERSION "0.14"
27 * Changelog:
28 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
29 * drivers/misc.
31 * 2006-11-22 0.13 new maintainer
32 * changelog now lives in git commit history, and will
33 * not be updated further in-file.
35 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
36 * 2005-03-17 0.11 support for 600e, 770x
37 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
38 * support for 770e, G41
39 * G40 and G41 don't have a thinklight
40 * temperatures no longer experimental
41 * experimental brightness control
42 * experimental volume control
43 * experimental fan enable/disable
44 * 2005-01-16 0.10 fix module loading on R30, R31
45 * 2005-01-16 0.9 support for 570, R30, R31
46 * ultrabay support on A22p, A3x
47 * limit arg for cmos, led, beep, drop experimental status
48 * more capable led control on A21e, A22p, T20-22, X20
49 * experimental temperatures and fan speed
50 * experimental embedded controller register dump
51 * mark more functions as __init, drop incorrect __exit
52 * use MODULE_VERSION
53 * thanks to Henrik Brix Andersen <brix@gentoo.org>
54 * fix parameter passing on module loading
55 * thanks to Rusty Russell <rusty@rustcorp.com.au>
56 * thanks to Jim Radford <radford@blackbean.org>
57 * 2004-11-08 0.8 fix init error case, don't return from a macro
58 * thanks to Chris Wright <chrisw@osdl.org>
59 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
60 * fix led control on A21e
61 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
62 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
63 * proc file format changed
64 * video_switch command
65 * experimental cmos control
66 * experimental led control
67 * experimental acpi sounds
68 * 2004-09-16 0.4 support for module parameters
69 * hotkey mask can be prefixed by 0x
70 * video output switching
71 * video expansion control
72 * ultrabay eject support
73 * removed lcd brightness/on/off control, didn't work
74 * 2004-08-17 0.3 support for R40
75 * lcd off, brightness control
76 * thinklight on/off
77 * 2004-08-14 0.2 support for T series, X20
78 * bluetooth enable/disable
79 * hotkey events disabled by default
80 * removed fan control, currently useless
81 * 2004-08-09 0.1 initial release, support for X series
84 #include "thinkpad_acpi.h"
86 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
87 MODULE_DESCRIPTION(IBM_DESC);
88 MODULE_VERSION(IBM_VERSION);
89 MODULE_LICENSE("GPL");
91 /* Please remove this in year 2009 */
92 MODULE_ALIAS("ibm_acpi");
94 #define __unused __attribute__ ((unused))
96 /****************************************************************************
97 ****************************************************************************
99 * ACPI Helpers and device model
101 ****************************************************************************
102 ****************************************************************************/
104 /*************************************************************************
105 * ACPI basic handles
108 static acpi_handle root_handle = NULL;
110 #define IBM_HANDLE(object, parent, paths...) \
111 static acpi_handle object##_handle; \
112 static acpi_handle *object##_parent = &parent##_handle; \
113 static char *object##_path; \
114 static char *object##_paths[] = { paths }
116 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
117 "\\_SB.PCI.ISA.EC", /* 570 */
118 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
119 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
120 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
121 "\\_SB.PCI0.ICH3.EC0", /* R31 */
122 "\\_SB.PCI0.LPC.EC", /* all others */
125 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
126 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
129 /*************************************************************************
130 * Misc ACPI handles
133 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
134 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
135 "\\CMS", /* R40, R40e */
136 ); /* all others */
138 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
139 "^HKEY", /* R30, R31 */
140 "HKEY", /* all others */
141 ); /* 570 */
144 /*************************************************************************
145 * ACPI helpers
148 static int acpi_evalf(acpi_handle handle,
149 void *res, char *method, char *fmt, ...)
151 char *fmt0 = fmt;
152 struct acpi_object_list params;
153 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
154 struct acpi_buffer result, *resultp;
155 union acpi_object out_obj;
156 acpi_status status;
157 va_list ap;
158 char res_type;
159 int success;
160 int quiet;
162 if (!*fmt) {
163 printk(IBM_ERR "acpi_evalf() called with empty format\n");
164 return 0;
167 if (*fmt == 'q') {
168 quiet = 1;
169 fmt++;
170 } else
171 quiet = 0;
173 res_type = *(fmt++);
175 params.count = 0;
176 params.pointer = &in_objs[0];
178 va_start(ap, fmt);
179 while (*fmt) {
180 char c = *(fmt++);
181 switch (c) {
182 case 'd': /* int */
183 in_objs[params.count].integer.value = va_arg(ap, int);
184 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
185 break;
186 /* add more types as needed */
187 default:
188 printk(IBM_ERR "acpi_evalf() called "
189 "with invalid format character '%c'\n", c);
190 return 0;
193 va_end(ap);
195 if (res_type != 'v') {
196 result.length = sizeof(out_obj);
197 result.pointer = &out_obj;
198 resultp = &result;
199 } else
200 resultp = NULL;
202 status = acpi_evaluate_object(handle, method, &params, resultp);
204 switch (res_type) {
205 case 'd': /* int */
206 if (res)
207 *(int *)res = out_obj.integer.value;
208 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
209 break;
210 case 'v': /* void */
211 success = status == AE_OK;
212 break;
213 /* add more types as needed */
214 default:
215 printk(IBM_ERR "acpi_evalf() called "
216 "with invalid format character '%c'\n", res_type);
217 return 0;
220 if (!success && !quiet)
221 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
222 method, fmt0, status);
224 return success;
227 static void __unused acpi_print_int(acpi_handle handle, char *method)
229 int i;
231 if (acpi_evalf(handle, &i, method, "d"))
232 printk(IBM_INFO "%s = 0x%x\n", method, i);
233 else
234 printk(IBM_ERR "error calling %s\n", method);
237 static int acpi_ec_read(int i, u8 * p)
239 int v;
241 if (ecrd_handle) {
242 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
243 return 0;
244 *p = v;
245 } else {
246 if (ec_read(i, p) < 0)
247 return 0;
250 return 1;
253 static int acpi_ec_write(int i, u8 v)
255 if (ecwr_handle) {
256 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
257 return 0;
258 } else {
259 if (ec_write(i, v) < 0)
260 return 0;
263 return 1;
266 static int _sta(acpi_handle handle)
268 int status;
270 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
271 status = 0;
273 return status;
276 /*************************************************************************
277 * ACPI device model
280 static void __init ibm_handle_init(char *name,
281 acpi_handle * handle, acpi_handle parent,
282 char **paths, int num_paths, char **path)
284 int i;
285 acpi_status status;
287 for (i = 0; i < num_paths; i++) {
288 status = acpi_get_handle(parent, paths[i], handle);
289 if (ACPI_SUCCESS(status)) {
290 *path = paths[i];
291 return;
295 *handle = NULL;
298 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
300 struct ibm_struct *ibm = data;
302 if (!ibm || !ibm->notify)
303 return;
305 ibm->notify(ibm, event);
308 static int __init setup_notify(struct ibm_struct *ibm)
310 acpi_status status;
311 int ret;
313 if (!*ibm->handle)
314 return 0;
316 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
317 if (ret < 0) {
318 printk(IBM_ERR "%s device not present\n", ibm->name);
319 return -ENODEV;
322 acpi_driver_data(ibm->device) = ibm;
323 sprintf(acpi_device_class(ibm->device), "%s/%s",
324 IBM_ACPI_EVENT_PREFIX,
325 ibm->name);
327 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
328 dispatch_notify, ibm);
329 if (ACPI_FAILURE(status)) {
330 if (status == AE_ALREADY_EXISTS) {
331 printk(IBM_NOTICE "another device driver is already handling %s events\n",
332 ibm->name);
333 } else {
334 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
335 ibm->name, status);
337 return -ENODEV;
339 ibm->notify_installed = 1;
340 return 0;
343 static int __init ibm_device_add(struct acpi_device *device)
345 return 0;
348 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
350 int ret;
352 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
353 if (!ibm->driver) {
354 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
355 return -1;
358 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
359 ibm->driver->ids = ibm->hid;
360 ibm->driver->ops.add = &ibm_device_add;
362 ret = acpi_bus_register_driver(ibm->driver);
363 if (ret < 0) {
364 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
365 ibm->hid, ret);
366 kfree(ibm->driver);
369 return ret;
373 /****************************************************************************
374 ****************************************************************************
376 * Procfs Helpers
378 ****************************************************************************
379 ****************************************************************************/
381 static int dispatch_read(char *page, char **start, off_t off, int count,
382 int *eof, void *data)
384 struct ibm_struct *ibm = data;
385 int len;
387 if (!ibm || !ibm->read)
388 return -EINVAL;
390 len = ibm->read(page);
391 if (len < 0)
392 return len;
394 if (len <= off + count)
395 *eof = 1;
396 *start = page + off;
397 len -= off;
398 if (len > count)
399 len = count;
400 if (len < 0)
401 len = 0;
403 return len;
406 static int dispatch_write(struct file *file, const char __user * userbuf,
407 unsigned long count, void *data)
409 struct ibm_struct *ibm = data;
410 char *kernbuf;
411 int ret;
413 if (!ibm || !ibm->write)
414 return -EINVAL;
416 kernbuf = kmalloc(count + 2, GFP_KERNEL);
417 if (!kernbuf)
418 return -ENOMEM;
420 if (copy_from_user(kernbuf, userbuf, count)) {
421 kfree(kernbuf);
422 return -EFAULT;
425 kernbuf[count] = 0;
426 strcat(kernbuf, ",");
427 ret = ibm->write(kernbuf);
428 if (ret == 0)
429 ret = count;
431 kfree(kernbuf);
433 return ret;
436 static char *next_cmd(char **cmds)
438 char *start = *cmds;
439 char *end;
441 while ((end = strchr(start, ',')) && end == start)
442 start = end + 1;
444 if (!end)
445 return NULL;
447 *end = 0;
448 *cmds = end + 1;
449 return start;
453 /****************************************************************************
454 ****************************************************************************
456 * Subdrivers
458 ****************************************************************************
459 ****************************************************************************/
461 /*************************************************************************
462 * thinkpad-acpi init subdriver
465 static int thinkpad_acpi_driver_init(void)
467 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
468 printk(IBM_INFO "%s\n", IBM_URL);
470 if (ibm_thinkpad_ec_found)
471 printk(IBM_INFO "ThinkPad EC firmware %s\n",
472 ibm_thinkpad_ec_found);
474 return 0;
477 static int thinkpad_acpi_driver_read(char *p)
479 int len = 0;
481 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
482 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
484 return len;
487 /*************************************************************************
488 * Hotkey subdriver
491 static int hotkey_supported;
492 static int hotkey_mask_supported;
493 static int hotkey_orig_status;
494 static int hotkey_orig_mask;
496 static int hotkey_init(void)
498 /* hotkey not supported on 570 */
499 hotkey_supported = hkey_handle != NULL;
501 if (hotkey_supported) {
502 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
503 A30, R30, R31, T20-22, X20-21, X22-24 */
504 hotkey_mask_supported =
505 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
507 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
508 return -ENODEV;
511 return 0;
514 static void hotkey_exit(void)
516 if (hotkey_supported)
517 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
520 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
522 int hkey;
524 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
525 acpi_bus_generate_event(ibm->device, event, hkey);
526 else {
527 printk(IBM_ERR "unknown hotkey event %d\n", event);
528 acpi_bus_generate_event(ibm->device, event, 0);
532 static int hotkey_get(int *status, int *mask)
534 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
535 return 0;
537 if (hotkey_mask_supported)
538 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
539 return 0;
541 return 1;
544 static int hotkey_set(int status, int mask)
546 int i;
548 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
549 return 0;
551 if (hotkey_mask_supported)
552 for (i = 0; i < 32; i++) {
553 int bit = ((1 << i) & mask) != 0;
554 if (!acpi_evalf(hkey_handle,
555 NULL, "MHKM", "vdd", i + 1, bit))
556 return 0;
559 return 1;
562 static int hotkey_read(char *p)
564 int status, mask;
565 int len = 0;
567 if (!hotkey_supported) {
568 len += sprintf(p + len, "status:\t\tnot supported\n");
569 return len;
572 if (!hotkey_get(&status, &mask))
573 return -EIO;
575 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
576 if (hotkey_mask_supported) {
577 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
578 len += sprintf(p + len,
579 "commands:\tenable, disable, reset, <mask>\n");
580 } else {
581 len += sprintf(p + len, "mask:\t\tnot supported\n");
582 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
585 return len;
588 static int hotkey_write(char *buf)
590 int status, mask;
591 char *cmd;
592 int do_cmd = 0;
594 if (!hotkey_supported)
595 return -ENODEV;
597 if (!hotkey_get(&status, &mask))
598 return -EIO;
600 while ((cmd = next_cmd(&buf))) {
601 if (strlencmp(cmd, "enable") == 0) {
602 status = 1;
603 } else if (strlencmp(cmd, "disable") == 0) {
604 status = 0;
605 } else if (strlencmp(cmd, "reset") == 0) {
606 status = hotkey_orig_status;
607 mask = hotkey_orig_mask;
608 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
609 /* mask set */
610 } else if (sscanf(cmd, "%x", &mask) == 1) {
611 /* mask set */
612 } else
613 return -EINVAL;
614 do_cmd = 1;
617 if (do_cmd && !hotkey_set(status, mask))
618 return -EIO;
620 return 0;
623 /*************************************************************************
624 * Bluetooth subdriver
627 static int bluetooth_supported;
629 static int bluetooth_init(void)
631 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
632 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
633 bluetooth_supported = hkey_handle &&
634 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
636 return 0;
639 static int bluetooth_status(void)
641 int status;
643 if (!bluetooth_supported ||
644 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
645 status = 0;
647 return status;
650 static int bluetooth_read(char *p)
652 int len = 0;
653 int status = bluetooth_status();
655 if (!bluetooth_supported)
656 len += sprintf(p + len, "status:\t\tnot supported\n");
657 else if (!(status & 1))
658 len += sprintf(p + len, "status:\t\tnot installed\n");
659 else {
660 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
661 len += sprintf(p + len, "commands:\tenable, disable\n");
664 return len;
667 static int bluetooth_write(char *buf)
669 int status = bluetooth_status();
670 char *cmd;
671 int do_cmd = 0;
673 if (!bluetooth_supported)
674 return -ENODEV;
676 while ((cmd = next_cmd(&buf))) {
677 if (strlencmp(cmd, "enable") == 0) {
678 status |= 2;
679 } else if (strlencmp(cmd, "disable") == 0) {
680 status &= ~2;
681 } else
682 return -EINVAL;
683 do_cmd = 1;
686 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
687 return -EIO;
689 return 0;
692 /*************************************************************************
693 * Wan subdriver
696 static int wan_supported;
698 static int wan_init(void)
700 wan_supported = hkey_handle &&
701 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
703 return 0;
706 static int wan_status(void)
708 int status;
710 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
711 status = 0;
713 return status;
716 static int wan_read(char *p)
718 int len = 0;
719 int status = wan_status();
721 if (!wan_supported)
722 len += sprintf(p + len, "status:\t\tnot supported\n");
723 else if (!(status & 1))
724 len += sprintf(p + len, "status:\t\tnot installed\n");
725 else {
726 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
727 len += sprintf(p + len, "commands:\tenable, disable\n");
730 return len;
733 static int wan_write(char *buf)
735 int status = wan_status();
736 char *cmd;
737 int do_cmd = 0;
739 if (!wan_supported)
740 return -ENODEV;
742 while ((cmd = next_cmd(&buf))) {
743 if (strlencmp(cmd, "enable") == 0) {
744 status |= 2;
745 } else if (strlencmp(cmd, "disable") == 0) {
746 status &= ~2;
747 } else
748 return -EINVAL;
749 do_cmd = 1;
752 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
753 return -EIO;
755 return 0;
758 /*************************************************************************
759 * Video subdriver
762 static enum video_access_mode video_supported;
763 static int video_orig_autosw;
765 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
766 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
767 "\\_SB.PCI0.VID0", /* 770e */
768 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
769 "\\_SB.PCI0.AGP.VID", /* all others */
770 ); /* R30, R31 */
772 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
774 static int video_init(void)
776 int ivga;
778 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
779 /* G41, assume IVGA doesn't change */
780 vid_handle = vid2_handle;
782 if (!vid_handle)
783 /* video switching not supported on R30, R31 */
784 video_supported = TPACPI_VIDEO_NONE;
785 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
786 /* 570 */
787 video_supported = TPACPI_VIDEO_570;
788 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
789 /* 600e/x, 770e, 770x */
790 video_supported = TPACPI_VIDEO_770;
791 else
792 /* all others */
793 video_supported = TPACPI_VIDEO_NEW;
795 return 0;
798 static void video_exit(void)
800 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
803 static int video_status(void)
805 int status = 0;
806 int i;
808 if (video_supported == TPACPI_VIDEO_570) {
809 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
810 status = i & 3;
811 } else if (video_supported == TPACPI_VIDEO_770) {
812 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
813 status |= 0x01 * i;
814 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
815 status |= 0x02 * i;
816 } else if (video_supported == TPACPI_VIDEO_NEW) {
817 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
818 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
819 status |= 0x02 * i;
821 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
822 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
823 status |= 0x01 * i;
824 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
825 status |= 0x08 * i;
828 return status;
831 static int video_autosw(void)
833 int autosw = 0;
835 if (video_supported == TPACPI_VIDEO_570)
836 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
837 else if (video_supported == TPACPI_VIDEO_770 ||
838 video_supported == TPACPI_VIDEO_NEW)
839 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
841 return autosw & 1;
844 static int video_switch(void)
846 int autosw = video_autosw();
847 int ret;
849 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
850 return -EIO;
851 ret = video_supported == TPACPI_VIDEO_570 ?
852 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
853 acpi_evalf(vid_handle, NULL, "VSWT", "v");
854 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
856 return ret;
859 static int video_expand(void)
861 if (video_supported == TPACPI_VIDEO_570)
862 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
863 else if (video_supported == TPACPI_VIDEO_770)
864 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
865 else
866 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
869 static int video_switch2(int status)
871 int ret;
873 if (video_supported == TPACPI_VIDEO_570) {
874 ret = acpi_evalf(NULL, NULL,
875 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
876 } else if (video_supported == TPACPI_VIDEO_770) {
877 int autosw = video_autosw();
878 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
879 return -EIO;
881 ret = acpi_evalf(vid_handle, NULL,
882 "ASWT", "vdd", status * 0x100, 0);
884 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
885 } else {
886 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
887 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
890 return ret;
893 static int video_read(char *p)
895 int status = video_status();
896 int autosw = video_autosw();
897 int len = 0;
899 if (!video_supported) {
900 len += sprintf(p + len, "status:\t\tnot supported\n");
901 return len;
904 len += sprintf(p + len, "status:\t\tsupported\n");
905 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
906 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
907 if (video_supported == TPACPI_VIDEO_NEW)
908 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
909 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
910 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
911 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
912 if (video_supported == TPACPI_VIDEO_NEW)
913 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
914 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
915 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
917 return len;
920 static int video_write(char *buf)
922 char *cmd;
923 int enable, disable, status;
925 if (!video_supported)
926 return -ENODEV;
928 enable = disable = 0;
930 while ((cmd = next_cmd(&buf))) {
931 if (strlencmp(cmd, "lcd_enable") == 0) {
932 enable |= 0x01;
933 } else if (strlencmp(cmd, "lcd_disable") == 0) {
934 disable |= 0x01;
935 } else if (strlencmp(cmd, "crt_enable") == 0) {
936 enable |= 0x02;
937 } else if (strlencmp(cmd, "crt_disable") == 0) {
938 disable |= 0x02;
939 } else if (video_supported == TPACPI_VIDEO_NEW &&
940 strlencmp(cmd, "dvi_enable") == 0) {
941 enable |= 0x08;
942 } else if (video_supported == TPACPI_VIDEO_NEW &&
943 strlencmp(cmd, "dvi_disable") == 0) {
944 disable |= 0x08;
945 } else if (strlencmp(cmd, "auto_enable") == 0) {
946 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
947 return -EIO;
948 } else if (strlencmp(cmd, "auto_disable") == 0) {
949 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
950 return -EIO;
951 } else if (strlencmp(cmd, "video_switch") == 0) {
952 if (!video_switch())
953 return -EIO;
954 } else if (strlencmp(cmd, "expand_toggle") == 0) {
955 if (!video_expand())
956 return -EIO;
957 } else
958 return -EINVAL;
961 if (enable || disable) {
962 status = (video_status() & 0x0f & ~disable) | enable;
963 if (!video_switch2(status))
964 return -EIO;
967 return 0;
970 /*************************************************************************
971 * Light (thinklight) subdriver
974 static int light_supported;
975 static int light_status_supported;
977 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
978 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
980 static int light_init(void)
982 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
983 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
985 if (light_supported)
986 /* light status not supported on
987 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
988 light_status_supported = acpi_evalf(ec_handle, NULL,
989 "KBLT", "qv");
991 return 0;
994 static int light_read(char *p)
996 int len = 0;
997 int status = 0;
999 if (!light_supported) {
1000 len += sprintf(p + len, "status:\t\tnot supported\n");
1001 } else if (!light_status_supported) {
1002 len += sprintf(p + len, "status:\t\tunknown\n");
1003 len += sprintf(p + len, "commands:\ton, off\n");
1004 } else {
1005 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1006 return -EIO;
1007 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1008 len += sprintf(p + len, "commands:\ton, off\n");
1011 return len;
1014 static int light_write(char *buf)
1016 int cmos_cmd, lght_cmd;
1017 char *cmd;
1018 int success;
1020 if (!light_supported)
1021 return -ENODEV;
1023 while ((cmd = next_cmd(&buf))) {
1024 if (strlencmp(cmd, "on") == 0) {
1025 cmos_cmd = 0x0c;
1026 lght_cmd = 1;
1027 } else if (strlencmp(cmd, "off") == 0) {
1028 cmos_cmd = 0x0d;
1029 lght_cmd = 0;
1030 } else
1031 return -EINVAL;
1033 success = cmos_handle ?
1034 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1035 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1036 if (!success)
1037 return -EIO;
1040 return 0;
1043 /*************************************************************************
1044 * Dock subdriver
1047 /* don't list other alternatives as we install a notify handler on the 570 */
1048 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1050 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1052 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1053 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1054 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1055 "\\_SB.PCI.ISA.SLCE", /* 570 */
1056 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1058 #define dock_docked() (_sta(dock_handle) & 1)
1060 static void dock_notify(struct ibm_struct *ibm, u32 event)
1062 int docked = dock_docked();
1063 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1065 if (event == 1 && !pci) /* 570 */
1066 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1067 else if (event == 1 && pci) /* 570 */
1068 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1069 else if (event == 3 && docked)
1070 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1071 else if (event == 3 && !docked)
1072 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1073 else if (event == 0 && docked)
1074 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1075 else {
1076 printk(IBM_ERR "unknown dock event %d, status %d\n",
1077 event, _sta(dock_handle));
1078 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1082 static int dock_read(char *p)
1084 int len = 0;
1085 int docked = dock_docked();
1087 if (!dock_handle)
1088 len += sprintf(p + len, "status:\t\tnot supported\n");
1089 else if (!docked)
1090 len += sprintf(p + len, "status:\t\tundocked\n");
1091 else {
1092 len += sprintf(p + len, "status:\t\tdocked\n");
1093 len += sprintf(p + len, "commands:\tdock, undock\n");
1096 return len;
1099 static int dock_write(char *buf)
1101 char *cmd;
1103 if (!dock_docked())
1104 return -ENODEV;
1106 while ((cmd = next_cmd(&buf))) {
1107 if (strlencmp(cmd, "undock") == 0) {
1108 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1109 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1110 return -EIO;
1111 } else if (strlencmp(cmd, "dock") == 0) {
1112 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1113 return -EIO;
1114 } else
1115 return -EINVAL;
1118 return 0;
1121 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1123 /*************************************************************************
1124 * Bay subdriver
1127 #ifdef CONFIG_THINKPAD_ACPI_BAY
1128 static int bay_status_supported;
1129 static int bay_status2_supported;
1130 static int bay_eject_supported;
1131 static int bay_eject2_supported;
1133 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1134 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1135 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1136 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1137 ); /* A21e, R30, R31 */
1138 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1139 "_EJ0", /* all others */
1140 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1141 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1142 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1143 ); /* all others */
1144 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1145 "_EJ0", /* 770x */
1146 ); /* all others */
1148 static int bay_init(void)
1150 bay_status_supported = bay_handle &&
1151 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1152 bay_status2_supported = bay2_handle &&
1153 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1155 bay_eject_supported = bay_handle && bay_ej_handle &&
1156 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1157 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1158 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1160 return 0;
1163 static void bay_notify(struct ibm_struct *ibm, u32 event)
1165 acpi_bus_generate_event(ibm->device, event, 0);
1168 #define bay_occupied(b) (_sta(b##_handle) & 1)
1170 static int bay_read(char *p)
1172 int len = 0;
1173 int occupied = bay_occupied(bay);
1174 int occupied2 = bay_occupied(bay2);
1175 int eject, eject2;
1177 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1178 (occupied ? "occupied" : "unoccupied") :
1179 "not supported");
1180 if (bay_status2_supported)
1181 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1182 "occupied" : "unoccupied");
1184 eject = bay_eject_supported && occupied;
1185 eject2 = bay_eject2_supported && occupied2;
1187 if (eject && eject2)
1188 len += sprintf(p + len, "commands:\teject, eject2\n");
1189 else if (eject)
1190 len += sprintf(p + len, "commands:\teject\n");
1191 else if (eject2)
1192 len += sprintf(p + len, "commands:\teject2\n");
1194 return len;
1197 static int bay_write(char *buf)
1199 char *cmd;
1201 if (!bay_eject_supported && !bay_eject2_supported)
1202 return -ENODEV;
1204 while ((cmd = next_cmd(&buf))) {
1205 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1206 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1207 return -EIO;
1208 } else if (bay_eject2_supported &&
1209 strlencmp(cmd, "eject2") == 0) {
1210 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1211 return -EIO;
1212 } else
1213 return -EINVAL;
1216 return 0;
1218 #endif /* CONFIG_THINKPAD_ACPI_BAY */
1220 /*************************************************************************
1221 * CMOS subdriver
1224 static int cmos_eval(int cmos_cmd)
1226 if (cmos_handle)
1227 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1228 else
1229 return 1;
1232 static int cmos_read(char *p)
1234 int len = 0;
1236 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1237 R30, R31, T20-22, X20-21 */
1238 if (!cmos_handle)
1239 len += sprintf(p + len, "status:\t\tnot supported\n");
1240 else {
1241 len += sprintf(p + len, "status:\t\tsupported\n");
1242 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1245 return len;
1248 static int cmos_write(char *buf)
1250 char *cmd;
1251 int cmos_cmd;
1253 if (!cmos_handle)
1254 return -EINVAL;
1256 while ((cmd = next_cmd(&buf))) {
1257 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1258 cmos_cmd >= 0 && cmos_cmd <= 21) {
1259 /* cmos_cmd set */
1260 } else
1261 return -EINVAL;
1263 if (!cmos_eval(cmos_cmd))
1264 return -EIO;
1267 return 0;
1271 /*************************************************************************
1272 * LED subdriver
1275 static enum led_access_mode led_supported;
1277 IBM_HANDLE(led, ec, "SLED", /* 570 */
1278 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1279 "LED", /* all others */
1280 ); /* R30, R31 */
1282 static int led_init(void)
1284 if (!led_handle)
1285 /* led not supported on R30, R31 */
1286 led_supported = TPACPI_LED_NONE;
1287 else if (strlencmp(led_path, "SLED") == 0)
1288 /* 570 */
1289 led_supported = TPACPI_LED_570;
1290 else if (strlencmp(led_path, "SYSL") == 0)
1291 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1292 led_supported = TPACPI_LED_OLD;
1293 else
1294 /* all others */
1295 led_supported = TPACPI_LED_NEW;
1297 return 0;
1300 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1302 static int led_read(char *p)
1304 int len = 0;
1306 if (!led_supported) {
1307 len += sprintf(p + len, "status:\t\tnot supported\n");
1308 return len;
1310 len += sprintf(p + len, "status:\t\tsupported\n");
1312 if (led_supported == TPACPI_LED_570) {
1313 /* 570 */
1314 int i, status;
1315 for (i = 0; i < 8; i++) {
1316 if (!acpi_evalf(ec_handle,
1317 &status, "GLED", "dd", 1 << i))
1318 return -EIO;
1319 len += sprintf(p + len, "%d:\t\t%s\n",
1320 i, led_status(status));
1324 len += sprintf(p + len, "commands:\t"
1325 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1327 return len;
1330 /* off, on, blink */
1331 static const int led_sled_arg1[] = { 0, 1, 3 };
1332 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1333 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1334 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1336 static int led_write(char *buf)
1338 char *cmd;
1339 int led, ind, ret;
1341 if (!led_supported)
1342 return -ENODEV;
1344 while ((cmd = next_cmd(&buf))) {
1345 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1346 return -EINVAL;
1348 if (strstr(cmd, "off")) {
1349 ind = 0;
1350 } else if (strstr(cmd, "on")) {
1351 ind = 1;
1352 } else if (strstr(cmd, "blink")) {
1353 ind = 2;
1354 } else
1355 return -EINVAL;
1357 if (led_supported == TPACPI_LED_570) {
1358 /* 570 */
1359 led = 1 << led;
1360 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1361 led, led_sled_arg1[ind]))
1362 return -EIO;
1363 } else if (led_supported == TPACPI_LED_OLD) {
1364 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1365 led = 1 << led;
1366 ret = ec_write(TPACPI_LED_EC_HLMS, led);
1367 if (ret >= 0)
1368 ret =
1369 ec_write(TPACPI_LED_EC_HLBL,
1370 led * led_exp_hlbl[ind]);
1371 if (ret >= 0)
1372 ret =
1373 ec_write(TPACPI_LED_EC_HLCL,
1374 led * led_exp_hlcl[ind]);
1375 if (ret < 0)
1376 return ret;
1377 } else {
1378 /* all others */
1379 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1380 led, led_led_arg1[ind]))
1381 return -EIO;
1385 return 0;
1388 /*************************************************************************
1389 * Beep subdriver
1392 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1394 static int beep_read(char *p)
1396 int len = 0;
1398 if (!beep_handle)
1399 len += sprintf(p + len, "status:\t\tnot supported\n");
1400 else {
1401 len += sprintf(p + len, "status:\t\tsupported\n");
1402 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1405 return len;
1408 static int beep_write(char *buf)
1410 char *cmd;
1411 int beep_cmd;
1413 if (!beep_handle)
1414 return -ENODEV;
1416 while ((cmd = next_cmd(&buf))) {
1417 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1418 beep_cmd >= 0 && beep_cmd <= 17) {
1419 /* beep_cmd set */
1420 } else
1421 return -EINVAL;
1422 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1423 return -EIO;
1426 return 0;
1429 /*************************************************************************
1430 * Thermal subdriver
1433 static enum thermal_access_mode thermal_read_mode;
1435 static int thermal_init(void)
1437 u8 t, ta1, ta2;
1438 int i;
1439 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1441 if (ibm_thinkpad_ec_found && experimental) {
1443 * Direct EC access mode: sensors at registers
1444 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1445 * non-implemented, thermal sensors return 0x80 when
1446 * not available
1449 ta1 = ta2 = 0;
1450 for (i = 0; i < 8; i++) {
1451 if (likely(acpi_ec_read(0x78 + i, &t))) {
1452 ta1 |= t;
1453 } else {
1454 ta1 = 0;
1455 break;
1457 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1458 ta2 |= t;
1459 } else {
1460 ta1 = 0;
1461 break;
1464 if (ta1 == 0) {
1465 /* This is sheer paranoia, but we handle it anyway */
1466 if (acpi_tmp7) {
1467 printk(IBM_ERR
1468 "ThinkPad ACPI EC access misbehaving, "
1469 "falling back to ACPI TMPx access mode\n");
1470 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
1471 } else {
1472 printk(IBM_ERR
1473 "ThinkPad ACPI EC access misbehaving, "
1474 "disabling thermal sensors access\n");
1475 thermal_read_mode = TPACPI_THERMAL_NONE;
1477 } else {
1478 thermal_read_mode =
1479 (ta2 != 0) ?
1480 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
1482 } else if (acpi_tmp7) {
1483 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1484 /* 600e/x, 770e, 770x */
1485 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
1486 } else {
1487 /* Standard ACPI TMPx access, max 8 sensors */
1488 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
1490 } else {
1491 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1492 thermal_read_mode = TPACPI_THERMAL_NONE;
1495 return 0;
1498 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1500 int i, t;
1501 s8 tmp;
1502 char tmpi[] = "TMPi";
1504 if (!s)
1505 return -EINVAL;
1507 switch (thermal_read_mode) {
1508 #if TPACPI_MAX_THERMAL_SENSORS >= 16
1509 case TPACPI_THERMAL_TPEC_16:
1510 for (i = 0; i < 8; i++) {
1511 if (!acpi_ec_read(0xC0 + i, &tmp))
1512 return -EIO;
1513 s->temp[i + 8] = tmp * 1000;
1515 /* fallthrough */
1516 #endif
1517 case TPACPI_THERMAL_TPEC_8:
1518 for (i = 0; i < 8; i++) {
1519 if (!acpi_ec_read(0x78 + i, &tmp))
1520 return -EIO;
1521 s->temp[i] = tmp * 1000;
1523 return (thermal_read_mode == TPACPI_THERMAL_TPEC_16) ? 16 : 8;
1525 case TPACPI_THERMAL_ACPI_UPDT:
1526 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1527 return -EIO;
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 - 2732) * 100;
1534 return 8;
1536 case TPACPI_THERMAL_ACPI_TMP07:
1537 for (i = 0; i < 8; i++) {
1538 tmpi[3] = '0' + i;
1539 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1540 return -EIO;
1541 s->temp[i] = t * 1000;
1543 return 8;
1545 case TPACPI_THERMAL_NONE:
1546 default:
1547 return 0;
1551 static int thermal_read(char *p)
1553 int len = 0;
1554 int n, i;
1555 struct ibm_thermal_sensors_struct t;
1557 n = thermal_get_sensors(&t);
1558 if (unlikely(n < 0))
1559 return n;
1561 len += sprintf(p + len, "temperatures:\t");
1563 if (n > 0) {
1564 for (i = 0; i < (n - 1); i++)
1565 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1566 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1567 } else
1568 len += sprintf(p + len, "not supported\n");
1570 return len;
1573 /*************************************************************************
1574 * EC Dump subdriver
1577 static u8 ecdump_regs[256];
1579 static int ecdump_read(char *p)
1581 int len = 0;
1582 int i, j;
1583 u8 v;
1585 len += sprintf(p + len, "EC "
1586 " +00 +01 +02 +03 +04 +05 +06 +07"
1587 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1588 for (i = 0; i < 256; i += 16) {
1589 len += sprintf(p + len, "EC 0x%02x:", i);
1590 for (j = 0; j < 16; j++) {
1591 if (!acpi_ec_read(i + j, &v))
1592 break;
1593 if (v != ecdump_regs[i + j])
1594 len += sprintf(p + len, " *%02x", v);
1595 else
1596 len += sprintf(p + len, " %02x", v);
1597 ecdump_regs[i + j] = v;
1599 len += sprintf(p + len, "\n");
1600 if (j != 16)
1601 break;
1604 /* These are way too dangerous to advertise openly... */
1605 #if 0
1606 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1607 " (<offset> is 00-ff, <value> is 00-ff)\n");
1608 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1609 " (<offset> is 00-ff, <value> is 0-255)\n");
1610 #endif
1611 return len;
1614 static int ecdump_write(char *buf)
1616 char *cmd;
1617 int i, v;
1619 while ((cmd = next_cmd(&buf))) {
1620 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1621 /* i and v set */
1622 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1623 /* i and v set */
1624 } else
1625 return -EINVAL;
1626 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1627 if (!acpi_ec_write(i, v))
1628 return -EIO;
1629 } else
1630 return -EINVAL;
1633 return 0;
1636 /*************************************************************************
1637 * Backlight/brightness subdriver
1640 static struct backlight_device *ibm_backlight_device = NULL;
1642 static struct backlight_properties ibm_backlight_data = {
1643 .owner = THIS_MODULE,
1644 .get_brightness = brightness_get,
1645 .update_status = brightness_update_status,
1646 .max_brightness = 7,
1649 static int brightness_init(void)
1651 int b;
1653 b = brightness_get(NULL);
1654 if (b < 0)
1655 return b;
1657 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
1658 &ibm_backlight_data);
1659 if (IS_ERR(ibm_backlight_device)) {
1660 printk(IBM_ERR "Could not register backlight device\n");
1661 return PTR_ERR(ibm_backlight_device);
1664 ibm_backlight_device->props->brightness = b;
1665 brightness_update_status(ibm_backlight_device);
1667 return 0;
1670 static void brightness_exit(void)
1672 if (ibm_backlight_device) {
1673 backlight_device_unregister(ibm_backlight_device);
1674 ibm_backlight_device = NULL;
1678 static int brightness_update_status(struct backlight_device *bd)
1680 return brightness_set(
1681 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
1682 bd->props->power == FB_BLANK_UNBLANK) ?
1683 bd->props->brightness : 0);
1686 static int brightness_get(struct backlight_device *bd)
1688 u8 level;
1689 if (!acpi_ec_read(brightness_offset, &level))
1690 return -EIO;
1692 level &= 0x7;
1694 return level;
1697 static int brightness_set(int value)
1699 int cmos_cmd, inc, i;
1700 int current_value = brightness_get(NULL);
1702 value &= 7;
1704 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
1705 inc = value > current_value ? 1 : -1;
1706 for (i = current_value; i != value; i += inc) {
1707 if (!cmos_eval(cmos_cmd))
1708 return -EIO;
1709 if (!acpi_ec_write(brightness_offset, i + inc))
1710 return -EIO;
1713 return 0;
1716 static int brightness_read(char *p)
1718 int len = 0;
1719 int level;
1721 if ((level = brightness_get(NULL)) < 0) {
1722 len += sprintf(p + len, "level:\t\tunreadable\n");
1723 } else {
1724 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1725 len += sprintf(p + len, "commands:\tup, down\n");
1726 len += sprintf(p + len, "commands:\tlevel <level>"
1727 " (<level> is 0-7)\n");
1730 return len;
1733 static int brightness_write(char *buf)
1735 int level;
1736 int new_level;
1737 char *cmd;
1739 while ((cmd = next_cmd(&buf))) {
1740 if ((level = brightness_get(NULL)) < 0)
1741 return level;
1742 level &= 7;
1744 if (strlencmp(cmd, "up") == 0) {
1745 new_level = level == 7 ? 7 : level + 1;
1746 } else if (strlencmp(cmd, "down") == 0) {
1747 new_level = level == 0 ? 0 : level - 1;
1748 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1749 new_level >= 0 && new_level <= 7) {
1750 /* new_level set */
1751 } else
1752 return -EINVAL;
1754 brightness_set(new_level);
1757 return 0;
1760 /*************************************************************************
1761 * Volume subdriver
1764 static int volume_read(char *p)
1766 int len = 0;
1767 u8 level;
1769 if (!acpi_ec_read(volume_offset, &level)) {
1770 len += sprintf(p + len, "level:\t\tunreadable\n");
1771 } else {
1772 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1773 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1774 len += sprintf(p + len, "commands:\tup, down, mute\n");
1775 len += sprintf(p + len, "commands:\tlevel <level>"
1776 " (<level> is 0-15)\n");
1779 return len;
1782 static int volume_write(char *buf)
1784 int cmos_cmd, inc, i;
1785 u8 level, mute;
1786 int new_level, new_mute;
1787 char *cmd;
1789 while ((cmd = next_cmd(&buf))) {
1790 if (!acpi_ec_read(volume_offset, &level))
1791 return -EIO;
1792 new_mute = mute = level & 0x40;
1793 new_level = level = level & 0xf;
1795 if (strlencmp(cmd, "up") == 0) {
1796 if (mute)
1797 new_mute = 0;
1798 else
1799 new_level = level == 15 ? 15 : level + 1;
1800 } else if (strlencmp(cmd, "down") == 0) {
1801 if (mute)
1802 new_mute = 0;
1803 else
1804 new_level = level == 0 ? 0 : level - 1;
1805 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1806 new_level >= 0 && new_level <= 15) {
1807 /* new_level set */
1808 } else if (strlencmp(cmd, "mute") == 0) {
1809 new_mute = 0x40;
1810 } else
1811 return -EINVAL;
1813 if (new_level != level) { /* mute doesn't change */
1814 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
1815 inc = new_level > level ? 1 : -1;
1817 if (mute && (!cmos_eval(cmos_cmd) ||
1818 !acpi_ec_write(volume_offset, level)))
1819 return -EIO;
1821 for (i = level; i != new_level; i += inc)
1822 if (!cmos_eval(cmos_cmd) ||
1823 !acpi_ec_write(volume_offset, i + inc))
1824 return -EIO;
1826 if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
1827 !acpi_ec_write(volume_offset,
1828 new_level + mute)))
1829 return -EIO;
1832 if (new_mute != mute) { /* level doesn't change */
1833 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
1835 if (!cmos_eval(cmos_cmd) ||
1836 !acpi_ec_write(volume_offset, level + new_mute))
1837 return -EIO;
1841 return 0;
1845 /*************************************************************************
1846 * Fan subdriver
1850 * FAN ACCESS MODES
1852 * TPACPI_FAN_RD_ACPI_GFAN:
1853 * ACPI GFAN method: returns fan level
1855 * see TPACPI_FAN_WR_ACPI_SFAN
1856 * EC 0x2f (HFSP) not available if GFAN exists
1858 * TPACPI_FAN_WR_ACPI_SFAN:
1859 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
1861 * EC 0x2f (HFSP) might be available *for reading*, but do not use
1862 * it for writing.
1864 * TPACPI_FAN_WR_TPEC:
1865 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
1866 * Supported on almost all ThinkPads
1868 * Fan speed changes of any sort (including those caused by the
1869 * disengaged mode) are usually done slowly by the firmware as the
1870 * maximum ammount of fan duty cycle change per second seems to be
1871 * limited.
1873 * Reading is not available if GFAN exists.
1874 * Writing is not available if SFAN exists.
1876 * Bits
1877 * 7 automatic mode engaged;
1878 * (default operation mode of the ThinkPad)
1879 * fan level is ignored in this mode.
1880 * 6 full speed mode (takes precedence over bit 7);
1881 * not available on all thinkpads. May disable
1882 * the tachometer while the fan controller ramps up
1883 * the speed (which can take up to a few *minutes*).
1884 * Speeds up fan to 100% duty-cycle, which is far above
1885 * the standard RPM levels. It is not impossible that
1886 * it could cause hardware damage.
1887 * 5-3 unused in some models. Extra bits for fan level
1888 * in others, but still useless as all values above
1889 * 7 map to the same speed as level 7 in these models.
1890 * 2-0 fan level (0..7 usually)
1891 * 0x00 = stop
1892 * 0x07 = max (set when temperatures critical)
1893 * Some ThinkPads may have other levels, see
1894 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
1896 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
1897 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
1898 * does so, its initial value is meaningless (0x07).
1900 * For firmware bugs, refer to:
1901 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1903 * ----
1905 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
1906 * Main fan tachometer reading (in RPM)
1908 * This register is present on all ThinkPads with a new-style EC, and
1909 * it is known not to be present on the A21m/e, and T22, as there is
1910 * something else in offset 0x84 according to the ACPI DSDT. Other
1911 * ThinkPads from this same time period (and earlier) probably lack the
1912 * tachometer as well.
1914 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
1915 * was never fixed by IBM to report the EC firmware version string
1916 * probably support the tachometer (like the early X models), so
1917 * detecting it is quite hard. We need more data to know for sure.
1919 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
1920 * might result.
1922 * FIRMWARE BUG: may go stale while the EC is switching to full speed
1923 * mode.
1925 * For firmware bugs, refer to:
1926 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1928 * TPACPI_FAN_WR_ACPI_FANS:
1929 * ThinkPad X31, X40, X41. Not available in the X60.
1931 * FANS ACPI handle: takes three arguments: low speed, medium speed,
1932 * high speed. ACPI DSDT seems to map these three speeds to levels
1933 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
1934 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
1936 * The speeds are stored on handles
1937 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
1939 * There are three default speed sets, acessible as handles:
1940 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
1942 * ACPI DSDT switches which set is in use depending on various
1943 * factors.
1945 * TPACPI_FAN_WR_TPEC is also available and should be used to
1946 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
1947 * but the ACPI tables just mention level 7.
1950 static enum fan_status_access_mode fan_status_access_mode;
1951 static enum fan_control_access_mode fan_control_access_mode;
1952 static enum fan_control_commands fan_control_commands;
1954 static int fan_control_status_known;
1955 static u8 fan_control_initial_status;
1957 static void fan_watchdog_fire(struct work_struct *ignored);
1958 static int fan_watchdog_maxinterval;
1959 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
1961 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
1962 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
1963 "\\FSPD", /* 600e/x, 770e, 770x */
1964 ); /* all others */
1965 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
1966 "JFNS", /* 770x-JL */
1967 ); /* all others */
1969 static int fan_init(void)
1971 fan_status_access_mode = TPACPI_FAN_NONE;
1972 fan_control_access_mode = TPACPI_FAN_WR_NONE;
1973 fan_control_commands = 0;
1974 fan_control_status_known = 1;
1975 fan_watchdog_maxinterval = 0;
1977 if (gfan_handle) {
1978 /* 570, 600e/x, 770e, 770x */
1979 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
1980 } else {
1981 /* all other ThinkPads: note that even old-style
1982 * ThinkPad ECs supports the fan control register */
1983 if (likely(acpi_ec_read(fan_status_offset,
1984 &fan_control_initial_status))) {
1985 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
1987 /* In some ThinkPads, neither the EC nor the ACPI
1988 * DSDT initialize the fan status, and it ends up
1989 * being set to 0x07 when it *could* be either
1990 * 0x07 or 0x80.
1992 * Enable for TP-1Y (T43), TP-78 (R51e),
1993 * TP-76 (R52), TP-70 (T43, R52), which are known
1994 * to be buggy. */
1995 if (fan_control_initial_status == 0x07 &&
1996 ibm_thinkpad_ec_found &&
1997 ((ibm_thinkpad_ec_found[0] == '1' &&
1998 ibm_thinkpad_ec_found[1] == 'Y') ||
1999 (ibm_thinkpad_ec_found[0] == '7' &&
2000 (ibm_thinkpad_ec_found[1] == '6' ||
2001 ibm_thinkpad_ec_found[1] == '8' ||
2002 ibm_thinkpad_ec_found[1] == '0'))
2003 )) {
2004 printk(IBM_NOTICE
2005 "fan_init: initial fan status is "
2006 "unknown, assuming it is in auto "
2007 "mode\n");
2008 fan_control_status_known = 0;
2010 } else {
2011 printk(IBM_ERR
2012 "ThinkPad ACPI EC access misbehaving, "
2013 "fan status and control unavailable\n");
2014 return 0;
2018 if (sfan_handle) {
2019 /* 570, 770x-JL */
2020 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
2021 fan_control_commands |=
2022 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
2023 } else {
2024 if (!gfan_handle) {
2025 /* gfan without sfan means no fan control */
2026 /* all other models implement TP EC 0x2f control */
2028 if (fans_handle) {
2029 /* X31, X40, X41 */
2030 fan_control_access_mode =
2031 TPACPI_FAN_WR_ACPI_FANS;
2032 fan_control_commands |=
2033 TPACPI_FAN_CMD_SPEED |
2034 TPACPI_FAN_CMD_LEVEL |
2035 TPACPI_FAN_CMD_ENABLE;
2036 } else {
2037 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
2038 fan_control_commands |=
2039 TPACPI_FAN_CMD_LEVEL |
2040 TPACPI_FAN_CMD_ENABLE;
2045 return 0;
2048 static int fan_get_status(u8 *status)
2050 u8 s;
2052 /* TODO:
2053 * Add TPACPI_FAN_RD_ACPI_FANS ? */
2055 switch (fan_status_access_mode) {
2056 case TPACPI_FAN_RD_ACPI_GFAN:
2057 /* 570, 600e/x, 770e, 770x */
2059 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
2060 return -EIO;
2062 if (likely(status))
2063 *status = s & 0x07;
2065 break;
2067 case TPACPI_FAN_RD_TPEC:
2068 /* all except 570, 600e/x, 770e, 770x */
2069 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2070 return -EIO;
2072 if (likely(status))
2073 *status = s;
2075 break;
2077 default:
2078 return -ENXIO;
2081 return 0;
2084 static void fan_exit(void)
2086 cancel_delayed_work(&fan_watchdog_task);
2087 flush_scheduled_work();
2090 static int fan_get_speed(unsigned int *speed)
2092 u8 hi, lo;
2094 switch (fan_status_access_mode) {
2095 case TPACPI_FAN_RD_TPEC:
2096 /* all except 570, 600e/x, 770e, 770x */
2097 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2098 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2099 return -EIO;
2101 if (likely(speed))
2102 *speed = (hi << 8) | lo;
2104 break;
2106 default:
2107 return -ENXIO;
2110 return 0;
2113 static void fan_watchdog_fire(struct work_struct *ignored)
2115 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2116 if (fan_set_enable()) {
2117 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2118 /* reschedule for later */
2119 fan_watchdog_reset();
2123 static void fan_watchdog_reset(void)
2125 static int fan_watchdog_active = 0;
2127 if (fan_watchdog_active)
2128 cancel_delayed_work(&fan_watchdog_task);
2130 if (fan_watchdog_maxinterval > 0) {
2131 fan_watchdog_active = 1;
2132 if (!schedule_delayed_work(&fan_watchdog_task,
2133 msecs_to_jiffies(fan_watchdog_maxinterval
2134 * 1000))) {
2135 printk(IBM_ERR "failed to schedule the fan watchdog, "
2136 "watchdog will not trigger\n");
2138 } else
2139 fan_watchdog_active = 0;
2142 static int fan_set_level(int level)
2144 switch (fan_control_access_mode) {
2145 case TPACPI_FAN_WR_ACPI_SFAN:
2146 if (level >= 0 && level <= 7) {
2147 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2148 return -EIO;
2149 } else
2150 return -EINVAL;
2151 break;
2153 case TPACPI_FAN_WR_ACPI_FANS:
2154 case TPACPI_FAN_WR_TPEC:
2155 if ((level != TP_EC_FAN_AUTO) &&
2156 (level != TP_EC_FAN_FULLSPEED) &&
2157 ((level < 0) || (level > 7)))
2158 return -EINVAL;
2160 if (!acpi_ec_write(fan_status_offset, level))
2161 return -EIO;
2162 else
2163 fan_control_status_known = 1;
2164 break;
2166 default:
2167 return -ENXIO;
2169 return 0;
2172 static int fan_set_enable(void)
2174 u8 s;
2175 int rc;
2177 switch (fan_control_access_mode) {
2178 case TPACPI_FAN_WR_ACPI_FANS:
2179 case TPACPI_FAN_WR_TPEC:
2180 if ((rc = fan_get_status(&s)) < 0)
2181 return rc;
2183 /* Don't go out of emergency fan mode */
2184 if (s != 7)
2185 s = TP_EC_FAN_AUTO;
2187 if (!acpi_ec_write(fan_status_offset, s))
2188 return -EIO;
2189 else
2190 fan_control_status_known = 1;
2191 break;
2193 case TPACPI_FAN_WR_ACPI_SFAN:
2194 if ((rc = fan_get_status(&s)) < 0)
2195 return rc;
2197 s &= 0x07;
2199 /* Set fan to at least level 4 */
2200 if (s < 4)
2201 s = 4;
2203 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2204 return -EIO;
2205 break;
2207 default:
2208 return -ENXIO;
2210 return 0;
2213 static int fan_set_disable(void)
2215 switch (fan_control_access_mode) {
2216 case TPACPI_FAN_WR_ACPI_FANS:
2217 case TPACPI_FAN_WR_TPEC:
2218 if (!acpi_ec_write(fan_status_offset, 0x00))
2219 return -EIO;
2220 else
2221 fan_control_status_known = 1;
2222 break;
2224 case TPACPI_FAN_WR_ACPI_SFAN:
2225 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2226 return -EIO;
2227 break;
2229 default:
2230 return -ENXIO;
2232 return 0;
2235 static int fan_set_speed(int speed)
2237 switch (fan_control_access_mode) {
2238 case TPACPI_FAN_WR_ACPI_FANS:
2239 if (speed >= 0 && speed <= 65535) {
2240 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2241 speed, speed, speed))
2242 return -EIO;
2243 } else
2244 return -EINVAL;
2245 break;
2247 default:
2248 return -ENXIO;
2250 return 0;
2253 static int fan_read(char *p)
2255 int len = 0;
2256 int rc;
2257 u8 status;
2258 unsigned int speed = 0;
2260 switch (fan_status_access_mode) {
2261 case TPACPI_FAN_RD_ACPI_GFAN:
2262 /* 570, 600e/x, 770e, 770x */
2263 if ((rc = fan_get_status(&status)) < 0)
2264 return rc;
2266 len += sprintf(p + len, "status:\t\t%s\n"
2267 "level:\t\t%d\n",
2268 (status != 0) ? "enabled" : "disabled", status);
2269 break;
2271 case TPACPI_FAN_RD_TPEC:
2272 /* all except 570, 600e/x, 770e, 770x */
2273 if ((rc = fan_get_status(&status)) < 0)
2274 return rc;
2276 if (unlikely(!fan_control_status_known)) {
2277 if (status != fan_control_initial_status)
2278 fan_control_status_known = 1;
2279 else
2280 /* Return most likely status. In fact, it
2281 * might be the only possible status */
2282 status = TP_EC_FAN_AUTO;
2285 len += sprintf(p + len, "status:\t\t%s\n",
2286 (status != 0) ? "enabled" : "disabled");
2288 if ((rc = fan_get_speed(&speed)) < 0)
2289 return rc;
2291 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2293 if (status & TP_EC_FAN_FULLSPEED)
2294 /* Disengaged mode takes precedence */
2295 len += sprintf(p + len, "level:\t\tdisengaged\n");
2296 else if (status & TP_EC_FAN_AUTO)
2297 len += sprintf(p + len, "level:\t\tauto\n");
2298 else
2299 len += sprintf(p + len, "level:\t\t%d\n", status);
2300 break;
2302 case TPACPI_FAN_NONE:
2303 default:
2304 len += sprintf(p + len, "status:\t\tnot supported\n");
2307 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
2308 len += sprintf(p + len, "commands:\tlevel <level>");
2310 switch (fan_control_access_mode) {
2311 case TPACPI_FAN_WR_ACPI_SFAN:
2312 len += sprintf(p + len, " (<level> is 0-7)\n");
2313 break;
2315 default:
2316 len += sprintf(p + len, " (<level> is 0-7, "
2317 "auto, disengaged)\n");
2318 break;
2322 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
2323 len += sprintf(p + len, "commands:\tenable, disable\n"
2324 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2325 "1-120 (seconds))\n");
2327 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
2328 len += sprintf(p + len, "commands:\tspeed <speed>"
2329 " (<speed> is 0-65535)\n");
2331 return len;
2334 static int fan_write_cmd_level(const char *cmd, int *rc)
2336 int level;
2338 if (strlencmp(cmd, "level auto") == 0)
2339 level = TP_EC_FAN_AUTO;
2340 else if (strlencmp(cmd, "level disengaged") == 0)
2341 level = TP_EC_FAN_FULLSPEED;
2342 else if (sscanf(cmd, "level %d", &level) != 1)
2343 return 0;
2345 if ((*rc = fan_set_level(level)) == -ENXIO)
2346 printk(IBM_ERR "level command accepted for unsupported "
2347 "access mode %d", fan_control_access_mode);
2349 return 1;
2352 static int fan_write_cmd_enable(const char *cmd, int *rc)
2354 if (strlencmp(cmd, "enable") != 0)
2355 return 0;
2357 if ((*rc = fan_set_enable()) == -ENXIO)
2358 printk(IBM_ERR "enable command accepted for unsupported "
2359 "access mode %d", fan_control_access_mode);
2361 return 1;
2364 static int fan_write_cmd_disable(const char *cmd, int *rc)
2366 if (strlencmp(cmd, "disable") != 0)
2367 return 0;
2369 if ((*rc = fan_set_disable()) == -ENXIO)
2370 printk(IBM_ERR "disable command accepted for unsupported "
2371 "access mode %d", fan_control_access_mode);
2373 return 1;
2376 static int fan_write_cmd_speed(const char *cmd, int *rc)
2378 int speed;
2380 /* TODO:
2381 * Support speed <low> <medium> <high> ? */
2383 if (sscanf(cmd, "speed %d", &speed) != 1)
2384 return 0;
2386 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2387 printk(IBM_ERR "speed command accepted for unsupported "
2388 "access mode %d", fan_control_access_mode);
2390 return 1;
2393 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2395 int interval;
2397 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2398 return 0;
2400 if (interval < 0 || interval > 120)
2401 *rc = -EINVAL;
2402 else
2403 fan_watchdog_maxinterval = interval;
2405 return 1;
2408 static int fan_write(char *buf)
2410 char *cmd;
2411 int rc = 0;
2413 while (!rc && (cmd = next_cmd(&buf))) {
2414 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
2415 fan_write_cmd_level(cmd, &rc)) &&
2416 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
2417 (fan_write_cmd_enable(cmd, &rc) ||
2418 fan_write_cmd_disable(cmd, &rc) ||
2419 fan_write_cmd_watchdog(cmd, &rc))) &&
2420 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
2421 fan_write_cmd_speed(cmd, &rc))
2423 rc = -EINVAL;
2424 else if (!rc)
2425 fan_watchdog_reset();
2428 return rc;
2431 /****************************************************************************
2432 ****************************************************************************
2434 * Infrastructure
2436 ****************************************************************************
2437 ****************************************************************************/
2439 /* /proc support */
2440 static struct proc_dir_entry *proc_dir = NULL;
2442 /* Subdriver registry */
2443 static struct ibm_struct ibms[] = {
2445 .name = "driver",
2446 .init = thinkpad_acpi_driver_init,
2447 .read = thinkpad_acpi_driver_read,
2450 .name = "hotkey",
2451 .hid = IBM_HKEY_HID,
2452 .init = hotkey_init,
2453 .read = hotkey_read,
2454 .write = hotkey_write,
2455 .exit = hotkey_exit,
2456 .notify = hotkey_notify,
2457 .handle = &hkey_handle,
2458 .type = ACPI_DEVICE_NOTIFY,
2461 .name = "bluetooth",
2462 .init = bluetooth_init,
2463 .read = bluetooth_read,
2464 .write = bluetooth_write,
2467 .name = "wan",
2468 .init = wan_init,
2469 .read = wan_read,
2470 .write = wan_write,
2471 .experimental = 1,
2474 .name = "video",
2475 .init = video_init,
2476 .read = video_read,
2477 .write = video_write,
2478 .exit = video_exit,
2481 .name = "light",
2482 .init = light_init,
2483 .read = light_read,
2484 .write = light_write,
2486 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2488 .name = "dock",
2489 .read = dock_read,
2490 .write = dock_write,
2491 .notify = dock_notify,
2492 .handle = &dock_handle,
2493 .type = ACPI_SYSTEM_NOTIFY,
2496 .name = "dock",
2497 .hid = IBM_PCI_HID,
2498 .notify = dock_notify,
2499 .handle = &pci_handle,
2500 .type = ACPI_SYSTEM_NOTIFY,
2502 #endif
2503 #ifdef CONFIG_THINKPAD_ACPI_BAY
2505 .name = "bay",
2506 .init = bay_init,
2507 .read = bay_read,
2508 .write = bay_write,
2509 .notify = bay_notify,
2510 .handle = &bay_handle,
2511 .type = ACPI_SYSTEM_NOTIFY,
2513 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2515 .name = "cmos",
2516 .read = cmos_read,
2517 .write = cmos_write,
2520 .name = "led",
2521 .init = led_init,
2522 .read = led_read,
2523 .write = led_write,
2526 .name = "beep",
2527 .read = beep_read,
2528 .write = beep_write,
2531 .name = "thermal",
2532 .init = thermal_init,
2533 .read = thermal_read,
2536 .name = "ecdump",
2537 .read = ecdump_read,
2538 .write = ecdump_write,
2539 .experimental = 1,
2542 .name = "brightness",
2543 .read = brightness_read,
2544 .write = brightness_write,
2545 .init = brightness_init,
2546 .exit = brightness_exit,
2549 .name = "volume",
2550 .read = volume_read,
2551 .write = volume_write,
2554 .name = "fan",
2555 .read = fan_read,
2556 .write = fan_write,
2557 .init = fan_init,
2558 .exit = fan_exit,
2559 .experimental = 1,
2564 * Module and infrastructure proble, init and exit handling
2567 static int __init ibm_init(struct ibm_struct *ibm)
2569 int ret;
2570 struct proc_dir_entry *entry;
2572 if (ibm->experimental && !experimental)
2573 return 0;
2575 if (ibm->hid) {
2576 ret = register_tpacpi_subdriver(ibm);
2577 if (ret < 0)
2578 return ret;
2579 ibm->driver_registered = 1;
2582 if (ibm->init) {
2583 ret = ibm->init();
2584 if (ret != 0)
2585 return ret;
2586 ibm->init_called = 1;
2589 if (ibm->read) {
2590 entry = create_proc_entry(ibm->name,
2591 S_IFREG | S_IRUGO | S_IWUSR,
2592 proc_dir);
2593 if (!entry) {
2594 printk(IBM_ERR "unable to create proc entry %s\n",
2595 ibm->name);
2596 return -ENODEV;
2598 entry->owner = THIS_MODULE;
2599 entry->data = ibm;
2600 entry->read_proc = &dispatch_read;
2601 if (ibm->write)
2602 entry->write_proc = &dispatch_write;
2603 ibm->proc_created = 1;
2606 if (ibm->notify) {
2607 ret = setup_notify(ibm);
2608 if (ret == -ENODEV) {
2609 printk(IBM_NOTICE "disabling subdriver %s\n",
2610 ibm->name);
2611 ibm_exit(ibm);
2612 return 0;
2614 if (ret < 0)
2615 return ret;
2618 return 0;
2621 static void ibm_exit(struct ibm_struct *ibm)
2623 if (ibm->notify_installed)
2624 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2625 dispatch_notify);
2627 if (ibm->proc_created)
2628 remove_proc_entry(ibm->name, proc_dir);
2630 if (ibm->init_called && ibm->exit)
2631 ibm->exit();
2633 if (ibm->driver_registered) {
2634 acpi_bus_unregister_driver(ibm->driver);
2635 kfree(ibm->driver);
2639 /* Probing */
2641 static char *ibm_thinkpad_ec_found = NULL;
2643 static char* __init check_dmi_for_ec(void)
2645 struct dmi_device *dev = NULL;
2646 char ec_fw_string[18];
2649 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2650 * X32 or newer, all Z series; Some models must have an
2651 * up-to-date BIOS or they will not be detected.
2653 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2655 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2656 if (sscanf(dev->name,
2657 "IBM ThinkPad Embedded Controller -[%17c",
2658 ec_fw_string) == 1) {
2659 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2660 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2661 return kstrdup(ec_fw_string, GFP_KERNEL);
2664 return NULL;
2667 /* Module init, exit, parameters */
2669 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2671 unsigned int i;
2673 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2674 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2675 if (strlen(val) > sizeof(ibms[i].param) - 2)
2676 return -ENOSPC;
2677 strcpy(ibms[i].param, val);
2678 strcat(ibms[i].param, ",");
2679 return 0;
2682 return -EINVAL;
2685 static int experimental;
2686 module_param(experimental, int, 0);
2688 static u32 dbg_level;
2689 module_param_named(debug, dbg_level, uint, 0);
2691 #define IBM_PARAM(feature) \
2692 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2694 IBM_PARAM(hotkey);
2695 IBM_PARAM(bluetooth);
2696 IBM_PARAM(video);
2697 IBM_PARAM(light);
2698 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2699 IBM_PARAM(dock);
2700 #endif
2701 #ifdef CONFIG_THINKPAD_ACPI_BAY
2702 IBM_PARAM(bay);
2703 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2704 IBM_PARAM(cmos);
2705 IBM_PARAM(led);
2706 IBM_PARAM(beep);
2707 IBM_PARAM(ecdump);
2708 IBM_PARAM(brightness);
2709 IBM_PARAM(volume);
2710 IBM_PARAM(fan);
2712 static int __init thinkpad_acpi_module_init(void)
2714 int ret, i;
2716 if (acpi_disabled)
2717 return -ENODEV;
2719 /* ec is required because many other handles are relative to it */
2720 IBM_HANDLE_INIT(ec);
2721 if (!ec_handle) {
2722 printk(IBM_ERR "ec object not found\n");
2723 return -ENODEV;
2726 /* Models with newer firmware report the EC in DMI */
2727 ibm_thinkpad_ec_found = check_dmi_for_ec();
2729 /* these handles are not required */
2730 IBM_HANDLE_INIT(vid);
2731 IBM_HANDLE_INIT(vid2);
2732 IBM_HANDLE_INIT(ledb);
2733 IBM_HANDLE_INIT(led);
2734 IBM_HANDLE_INIT(hkey);
2735 IBM_HANDLE_INIT(lght);
2736 IBM_HANDLE_INIT(cmos);
2737 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2738 IBM_HANDLE_INIT(dock);
2739 #endif
2740 IBM_HANDLE_INIT(pci);
2741 #ifdef CONFIG_THINKPAD_ACPI_BAY
2742 IBM_HANDLE_INIT(bay);
2743 if (bay_handle)
2744 IBM_HANDLE_INIT(bay_ej);
2745 IBM_HANDLE_INIT(bay2);
2746 if (bay2_handle)
2747 IBM_HANDLE_INIT(bay2_ej);
2748 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2749 IBM_HANDLE_INIT(beep);
2750 IBM_HANDLE_INIT(ecrd);
2751 IBM_HANDLE_INIT(ecwr);
2752 IBM_HANDLE_INIT(fans);
2753 IBM_HANDLE_INIT(gfan);
2754 IBM_HANDLE_INIT(sfan);
2756 proc_dir = proc_mkdir(IBM_DIR, acpi_root_dir);
2757 if (!proc_dir) {
2758 printk(IBM_ERR "unable to create proc dir %s", IBM_DIR);
2759 thinkpad_acpi_module_exit();
2760 return -ENODEV;
2762 proc_dir->owner = THIS_MODULE;
2764 for (i = 0; i < ARRAY_SIZE(ibms); i++) {
2765 ret = ibm_init(&ibms[i]);
2766 if (ret >= 0 && *ibms[i].param)
2767 ret = ibms[i].write(ibms[i].param);
2768 if (ret < 0) {
2769 thinkpad_acpi_module_exit();
2770 return ret;
2774 return 0;
2777 static void thinkpad_acpi_module_exit(void)
2779 int i;
2781 for (i = ARRAY_SIZE(ibms) - 1; i >= 0; i--)
2782 ibm_exit(&ibms[i]);
2784 if (proc_dir)
2785 remove_proc_entry(IBM_DIR, acpi_root_dir);
2787 if (ibm_thinkpad_ec_found)
2788 kfree(ibm_thinkpad_ec_found);
2791 module_init(thinkpad_acpi_module_init);
2792 module_exit(thinkpad_acpi_module_exit);