ACPI: thinkpad-acpi: add compatibility MODULE_ALIAS entry
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob67e7fde07945895c77a191dc1dd45b17bdf2fd9d
1 /*
2 * ibm_acpi.c - IBM 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.13"
27 * Changelog:
29 * 2006-11-22 0.13 new maintainer
30 * changelog now lives in git commit history, and will
31 * not be updated further in-file.
33 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
34 * 2005-03-17 0.11 support for 600e, 770x
35 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
36 * support for 770e, G41
37 * G40 and G41 don't have a thinklight
38 * temperatures no longer experimental
39 * experimental brightness control
40 * experimental volume control
41 * experimental fan enable/disable
42 * 2005-01-16 0.10 fix module loading on R30, R31
43 * 2005-01-16 0.9 support for 570, R30, R31
44 * ultrabay support on A22p, A3x
45 * limit arg for cmos, led, beep, drop experimental status
46 * more capable led control on A21e, A22p, T20-22, X20
47 * experimental temperatures and fan speed
48 * experimental embedded controller register dump
49 * mark more functions as __init, drop incorrect __exit
50 * use MODULE_VERSION
51 * thanks to Henrik Brix Andersen <brix@gentoo.org>
52 * fix parameter passing on module loading
53 * thanks to Rusty Russell <rusty@rustcorp.com.au>
54 * thanks to Jim Radford <radford@blackbean.org>
55 * 2004-11-08 0.8 fix init error case, don't return from a macro
56 * thanks to Chris Wright <chrisw@osdl.org>
57 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
58 * fix led control on A21e
59 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
60 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
61 * proc file format changed
62 * video_switch command
63 * experimental cmos control
64 * experimental led control
65 * experimental acpi sounds
66 * 2004-09-16 0.4 support for module parameters
67 * hotkey mask can be prefixed by 0x
68 * video output switching
69 * video expansion control
70 * ultrabay eject support
71 * removed lcd brightness/on/off control, didn't work
72 * 2004-08-17 0.3 support for R40
73 * lcd off, brightness control
74 * thinklight on/off
75 * 2004-08-14 0.2 support for T series, X20
76 * bluetooth enable/disable
77 * hotkey events disabled by default
78 * removed fan control, currently useless
79 * 2004-08-09 0.1 initial release, support for X series
82 #include "thinkpad_acpi.h"
84 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
85 MODULE_DESCRIPTION(IBM_DESC);
86 MODULE_VERSION(IBM_VERSION);
87 MODULE_LICENSE("GPL");
89 /* Please remove this in year 2009 */
90 MODULE_ALIAS("ibm_acpi");
92 #define __unused __attribute__ ((unused))
94 /****************************************************************************
95 ****************************************************************************
97 * ACPI Helpers and device model
99 ****************************************************************************
100 ****************************************************************************/
102 /*************************************************************************
103 * ACPI basic handles
106 static acpi_handle root_handle = NULL;
108 #define IBM_HANDLE(object, parent, paths...) \
109 static acpi_handle object##_handle; \
110 static acpi_handle *object##_parent = &parent##_handle; \
111 static char *object##_path; \
112 static char *object##_paths[] = { paths }
114 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
115 "\\_SB.PCI.ISA.EC", /* 570 */
116 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
117 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
118 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
119 "\\_SB.PCI0.ICH3.EC0", /* R31 */
120 "\\_SB.PCI0.LPC.EC", /* all others */
123 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
124 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
127 /*************************************************************************
128 * Misc ACPI handles
131 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
132 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
133 "\\CMS", /* R40, R40e */
134 ); /* all others */
136 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
137 "^HKEY", /* R30, R31 */
138 "HKEY", /* all others */
139 ); /* 570 */
142 /*************************************************************************
143 * ACPI helpers
146 static int acpi_evalf(acpi_handle handle,
147 void *res, char *method, char *fmt, ...)
149 char *fmt0 = fmt;
150 struct acpi_object_list params;
151 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
152 struct acpi_buffer result, *resultp;
153 union acpi_object out_obj;
154 acpi_status status;
155 va_list ap;
156 char res_type;
157 int success;
158 int quiet;
160 if (!*fmt) {
161 printk(IBM_ERR "acpi_evalf() called with empty format\n");
162 return 0;
165 if (*fmt == 'q') {
166 quiet = 1;
167 fmt++;
168 } else
169 quiet = 0;
171 res_type = *(fmt++);
173 params.count = 0;
174 params.pointer = &in_objs[0];
176 va_start(ap, fmt);
177 while (*fmt) {
178 char c = *(fmt++);
179 switch (c) {
180 case 'd': /* int */
181 in_objs[params.count].integer.value = va_arg(ap, int);
182 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
183 break;
184 /* add more types as needed */
185 default:
186 printk(IBM_ERR "acpi_evalf() called "
187 "with invalid format character '%c'\n", c);
188 return 0;
191 va_end(ap);
193 if (res_type != 'v') {
194 result.length = sizeof(out_obj);
195 result.pointer = &out_obj;
196 resultp = &result;
197 } else
198 resultp = NULL;
200 status = acpi_evaluate_object(handle, method, &params, resultp);
202 switch (res_type) {
203 case 'd': /* int */
204 if (res)
205 *(int *)res = out_obj.integer.value;
206 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
207 break;
208 case 'v': /* void */
209 success = status == AE_OK;
210 break;
211 /* add more types as needed */
212 default:
213 printk(IBM_ERR "acpi_evalf() called "
214 "with invalid format character '%c'\n", res_type);
215 return 0;
218 if (!success && !quiet)
219 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
220 method, fmt0, status);
222 return success;
225 static void __unused acpi_print_int(acpi_handle handle, char *method)
227 int i;
229 if (acpi_evalf(handle, &i, method, "d"))
230 printk(IBM_INFO "%s = 0x%x\n", method, i);
231 else
232 printk(IBM_ERR "error calling %s\n", method);
235 static int acpi_ec_read(int i, u8 * p)
237 int v;
239 if (ecrd_handle) {
240 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
241 return 0;
242 *p = v;
243 } else {
244 if (ec_read(i, p) < 0)
245 return 0;
248 return 1;
251 static int acpi_ec_write(int i, u8 v)
253 if (ecwr_handle) {
254 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
255 return 0;
256 } else {
257 if (ec_write(i, v) < 0)
258 return 0;
261 return 1;
264 static int _sta(acpi_handle handle)
266 int status;
268 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
269 status = 0;
271 return status;
274 /*************************************************************************
275 * ACPI device model
278 static void __init ibm_handle_init(char *name,
279 acpi_handle * handle, acpi_handle parent,
280 char **paths, int num_paths, char **path)
282 int i;
283 acpi_status status;
285 for (i = 0; i < num_paths; i++) {
286 status = acpi_get_handle(parent, paths[i], handle);
287 if (ACPI_SUCCESS(status)) {
288 *path = paths[i];
289 return;
293 *handle = NULL;
296 static void dispatch_notify(acpi_handle handle, u32 event, void *data)
298 struct ibm_struct *ibm = data;
300 if (!ibm || !ibm->notify)
301 return;
303 ibm->notify(ibm, event);
306 static int __init setup_notify(struct ibm_struct *ibm)
308 acpi_status status;
309 int ret;
311 if (!*ibm->handle)
312 return 0;
314 ret = acpi_bus_get_device(*ibm->handle, &ibm->device);
315 if (ret < 0) {
316 printk(IBM_ERR "%s device not present\n", ibm->name);
317 return -ENODEV;
320 acpi_driver_data(ibm->device) = ibm;
321 sprintf(acpi_device_class(ibm->device), "%s/%s", IBM_NAME, ibm->name);
323 status = acpi_install_notify_handler(*ibm->handle, ibm->type,
324 dispatch_notify, ibm);
325 if (ACPI_FAILURE(status)) {
326 if (status == AE_ALREADY_EXISTS) {
327 printk(IBM_NOTICE "another device driver is already handling %s events\n",
328 ibm->name);
329 } else {
330 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
331 ibm->name, status);
333 return -ENODEV;
335 ibm->notify_installed = 1;
336 return 0;
339 static int __init ibm_device_add(struct acpi_device *device)
341 return 0;
344 static int __init register_ibmacpi_subdriver(struct ibm_struct *ibm)
346 int ret;
348 ibm->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
349 if (!ibm->driver) {
350 printk(IBM_ERR "kmalloc(ibm->driver) failed\n");
351 return -1;
354 sprintf(ibm->driver->name, "%s_%s", IBM_NAME, ibm->name);
355 ibm->driver->ids = ibm->hid;
356 ibm->driver->ops.add = &ibm_device_add;
358 ret = acpi_bus_register_driver(ibm->driver);
359 if (ret < 0) {
360 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
361 ibm->hid, ret);
362 kfree(ibm->driver);
365 return ret;
369 /****************************************************************************
370 ****************************************************************************
372 * Procfs Helpers
374 ****************************************************************************
375 ****************************************************************************/
377 static int dispatch_read(char *page, char **start, off_t off, int count,
378 int *eof, void *data)
380 struct ibm_struct *ibm = data;
381 int len;
383 if (!ibm || !ibm->read)
384 return -EINVAL;
386 len = ibm->read(page);
387 if (len < 0)
388 return len;
390 if (len <= off + count)
391 *eof = 1;
392 *start = page + off;
393 len -= off;
394 if (len > count)
395 len = count;
396 if (len < 0)
397 len = 0;
399 return len;
402 static int dispatch_write(struct file *file, const char __user * userbuf,
403 unsigned long count, void *data)
405 struct ibm_struct *ibm = data;
406 char *kernbuf;
407 int ret;
409 if (!ibm || !ibm->write)
410 return -EINVAL;
412 kernbuf = kmalloc(count + 2, GFP_KERNEL);
413 if (!kernbuf)
414 return -ENOMEM;
416 if (copy_from_user(kernbuf, userbuf, count)) {
417 kfree(kernbuf);
418 return -EFAULT;
421 kernbuf[count] = 0;
422 strcat(kernbuf, ",");
423 ret = ibm->write(kernbuf);
424 if (ret == 0)
425 ret = count;
427 kfree(kernbuf);
429 return ret;
432 static char *next_cmd(char **cmds)
434 char *start = *cmds;
435 char *end;
437 while ((end = strchr(start, ',')) && end == start)
438 start = end + 1;
440 if (!end)
441 return NULL;
443 *end = 0;
444 *cmds = end + 1;
445 return start;
449 /****************************************************************************
450 ****************************************************************************
452 * Subdrivers
454 ****************************************************************************
455 ****************************************************************************/
457 /*************************************************************************
458 * ibm-acpi init subdriver
461 static int ibm_acpi_driver_init(void)
463 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
464 printk(IBM_INFO "%s\n", IBM_URL);
466 if (ibm_thinkpad_ec_found)
467 printk(IBM_INFO "ThinkPad EC firmware %s\n",
468 ibm_thinkpad_ec_found);
470 return 0;
473 static int ibm_acpi_driver_read(char *p)
475 int len = 0;
477 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
478 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
480 return len;
483 /*************************************************************************
484 * Hotkey subdriver
487 static int hotkey_supported;
488 static int hotkey_mask_supported;
489 static int hotkey_orig_status;
490 static int hotkey_orig_mask;
492 static int hotkey_init(void)
494 /* hotkey not supported on 570 */
495 hotkey_supported = hkey_handle != NULL;
497 if (hotkey_supported) {
498 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
499 A30, R30, R31, T20-22, X20-21, X22-24 */
500 hotkey_mask_supported =
501 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
503 if (!hotkey_get(&hotkey_orig_status, &hotkey_orig_mask))
504 return -ENODEV;
507 return 0;
510 static void hotkey_exit(void)
512 if (hotkey_supported)
513 hotkey_set(hotkey_orig_status, hotkey_orig_mask);
516 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
518 int hkey;
520 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
521 acpi_bus_generate_event(ibm->device, event, hkey);
522 else {
523 printk(IBM_ERR "unknown hotkey event %d\n", event);
524 acpi_bus_generate_event(ibm->device, event, 0);
528 static int hotkey_get(int *status, int *mask)
530 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
531 return 0;
533 if (hotkey_mask_supported)
534 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
535 return 0;
537 return 1;
540 static int hotkey_set(int status, int mask)
542 int i;
544 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
545 return 0;
547 if (hotkey_mask_supported)
548 for (i = 0; i < 32; i++) {
549 int bit = ((1 << i) & mask) != 0;
550 if (!acpi_evalf(hkey_handle,
551 NULL, "MHKM", "vdd", i + 1, bit))
552 return 0;
555 return 1;
558 static int hotkey_read(char *p)
560 int status, mask;
561 int len = 0;
563 if (!hotkey_supported) {
564 len += sprintf(p + len, "status:\t\tnot supported\n");
565 return len;
568 if (!hotkey_get(&status, &mask))
569 return -EIO;
571 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
572 if (hotkey_mask_supported) {
573 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
574 len += sprintf(p + len,
575 "commands:\tenable, disable, reset, <mask>\n");
576 } else {
577 len += sprintf(p + len, "mask:\t\tnot supported\n");
578 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
581 return len;
584 static int hotkey_write(char *buf)
586 int status, mask;
587 char *cmd;
588 int do_cmd = 0;
590 if (!hotkey_supported)
591 return -ENODEV;
593 if (!hotkey_get(&status, &mask))
594 return -EIO;
596 while ((cmd = next_cmd(&buf))) {
597 if (strlencmp(cmd, "enable") == 0) {
598 status = 1;
599 } else if (strlencmp(cmd, "disable") == 0) {
600 status = 0;
601 } else if (strlencmp(cmd, "reset") == 0) {
602 status = hotkey_orig_status;
603 mask = hotkey_orig_mask;
604 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
605 /* mask set */
606 } else if (sscanf(cmd, "%x", &mask) == 1) {
607 /* mask set */
608 } else
609 return -EINVAL;
610 do_cmd = 1;
613 if (do_cmd && !hotkey_set(status, mask))
614 return -EIO;
616 return 0;
619 /*************************************************************************
620 * Bluetooth subdriver
623 static int bluetooth_supported;
625 static int bluetooth_init(void)
627 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
628 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
629 bluetooth_supported = hkey_handle &&
630 acpi_evalf(hkey_handle, NULL, "GBDC", "qv");
632 return 0;
635 static int bluetooth_status(void)
637 int status;
639 if (!bluetooth_supported ||
640 !acpi_evalf(hkey_handle, &status, "GBDC", "d"))
641 status = 0;
643 return status;
646 static int bluetooth_read(char *p)
648 int len = 0;
649 int status = bluetooth_status();
651 if (!bluetooth_supported)
652 len += sprintf(p + len, "status:\t\tnot supported\n");
653 else if (!(status & 1))
654 len += sprintf(p + len, "status:\t\tnot installed\n");
655 else {
656 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
657 len += sprintf(p + len, "commands:\tenable, disable\n");
660 return len;
663 static int bluetooth_write(char *buf)
665 int status = bluetooth_status();
666 char *cmd;
667 int do_cmd = 0;
669 if (!bluetooth_supported)
670 return -ENODEV;
672 while ((cmd = next_cmd(&buf))) {
673 if (strlencmp(cmd, "enable") == 0) {
674 status |= 2;
675 } else if (strlencmp(cmd, "disable") == 0) {
676 status &= ~2;
677 } else
678 return -EINVAL;
679 do_cmd = 1;
682 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
683 return -EIO;
685 return 0;
688 /*************************************************************************
689 * Wan subdriver
692 static int wan_supported;
694 static int wan_init(void)
696 wan_supported = hkey_handle &&
697 acpi_evalf(hkey_handle, NULL, "GWAN", "qv");
699 return 0;
702 static int wan_status(void)
704 int status;
706 if (!wan_supported || !acpi_evalf(hkey_handle, &status, "GWAN", "d"))
707 status = 0;
709 return status;
712 static int wan_read(char *p)
714 int len = 0;
715 int status = wan_status();
717 if (!wan_supported)
718 len += sprintf(p + len, "status:\t\tnot supported\n");
719 else if (!(status & 1))
720 len += sprintf(p + len, "status:\t\tnot installed\n");
721 else {
722 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 1));
723 len += sprintf(p + len, "commands:\tenable, disable\n");
726 return len;
729 static int wan_write(char *buf)
731 int status = wan_status();
732 char *cmd;
733 int do_cmd = 0;
735 if (!wan_supported)
736 return -ENODEV;
738 while ((cmd = next_cmd(&buf))) {
739 if (strlencmp(cmd, "enable") == 0) {
740 status |= 2;
741 } else if (strlencmp(cmd, "disable") == 0) {
742 status &= ~2;
743 } else
744 return -EINVAL;
745 do_cmd = 1;
748 if (do_cmd && !acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
749 return -EIO;
751 return 0;
754 /*************************************************************************
755 * Video subdriver
758 static enum video_access_mode video_supported;
759 static int video_orig_autosw;
761 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
762 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
763 "\\_SB.PCI0.VID0", /* 770e */
764 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
765 "\\_SB.PCI0.AGP.VID", /* all others */
766 ); /* R30, R31 */
768 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
770 static int video_init(void)
772 int ivga;
774 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
775 /* G41, assume IVGA doesn't change */
776 vid_handle = vid2_handle;
778 if (!vid_handle)
779 /* video switching not supported on R30, R31 */
780 video_supported = IBMACPI_VIDEO_NONE;
781 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
782 /* 570 */
783 video_supported = IBMACPI_VIDEO_570;
784 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
785 /* 600e/x, 770e, 770x */
786 video_supported = IBMACPI_VIDEO_770;
787 else
788 /* all others */
789 video_supported = IBMACPI_VIDEO_NEW;
791 return 0;
794 static void video_exit(void)
796 acpi_evalf(vid_handle, NULL, "_DOS", "vd", video_orig_autosw);
799 static int video_status(void)
801 int status = 0;
802 int i;
804 if (video_supported == IBMACPI_VIDEO_570) {
805 if (acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 0x87))
806 status = i & 3;
807 } else if (video_supported == IBMACPI_VIDEO_770) {
808 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
809 status |= 0x01 * i;
810 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
811 status |= 0x02 * i;
812 } else if (video_supported == IBMACPI_VIDEO_NEW) {
813 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1);
814 if (acpi_evalf(NULL, &i, "\\VCDC", "d"))
815 status |= 0x02 * i;
817 acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0);
818 if (acpi_evalf(NULL, &i, "\\VCDL", "d"))
819 status |= 0x01 * i;
820 if (acpi_evalf(NULL, &i, "\\VCDD", "d"))
821 status |= 0x08 * i;
824 return status;
827 static int video_autosw(void)
829 int autosw = 0;
831 if (video_supported == IBMACPI_VIDEO_570)
832 acpi_evalf(vid_handle, &autosw, "SWIT", "d");
833 else if (video_supported == IBMACPI_VIDEO_770 ||
834 video_supported == IBMACPI_VIDEO_NEW)
835 acpi_evalf(vid_handle, &autosw, "^VDEE", "d");
837 return autosw & 1;
840 static int video_switch(void)
842 int autosw = video_autosw();
843 int ret;
845 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
846 return -EIO;
847 ret = video_supported == IBMACPI_VIDEO_570 ?
848 acpi_evalf(ec_handle, NULL, "_Q16", "v") :
849 acpi_evalf(vid_handle, NULL, "VSWT", "v");
850 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
852 return ret;
855 static int video_expand(void)
857 if (video_supported == IBMACPI_VIDEO_570)
858 return acpi_evalf(ec_handle, NULL, "_Q17", "v");
859 else if (video_supported == IBMACPI_VIDEO_770)
860 return acpi_evalf(vid_handle, NULL, "VEXP", "v");
861 else
862 return acpi_evalf(NULL, NULL, "\\VEXP", "v");
865 static int video_switch2(int status)
867 int ret;
869 if (video_supported == IBMACPI_VIDEO_570) {
870 ret = acpi_evalf(NULL, NULL,
871 "\\_SB.PHS2", "vdd", 0x8b, status | 0x80);
872 } else if (video_supported == IBMACPI_VIDEO_770) {
873 int autosw = video_autosw();
874 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
875 return -EIO;
877 ret = acpi_evalf(vid_handle, NULL,
878 "ASWT", "vdd", status * 0x100, 0);
880 acpi_evalf(vid_handle, NULL, "_DOS", "vd", autosw);
881 } else {
882 ret = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
883 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
886 return ret;
889 static int video_read(char *p)
891 int status = video_status();
892 int autosw = video_autosw();
893 int len = 0;
895 if (!video_supported) {
896 len += sprintf(p + len, "status:\t\tnot supported\n");
897 return len;
900 len += sprintf(p + len, "status:\t\tsupported\n");
901 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
902 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
903 if (video_supported == IBMACPI_VIDEO_NEW)
904 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
905 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
906 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
907 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
908 if (video_supported == IBMACPI_VIDEO_NEW)
909 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
910 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
911 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
913 return len;
916 static int video_write(char *buf)
918 char *cmd;
919 int enable, disable, status;
921 if (!video_supported)
922 return -ENODEV;
924 enable = disable = 0;
926 while ((cmd = next_cmd(&buf))) {
927 if (strlencmp(cmd, "lcd_enable") == 0) {
928 enable |= 0x01;
929 } else if (strlencmp(cmd, "lcd_disable") == 0) {
930 disable |= 0x01;
931 } else if (strlencmp(cmd, "crt_enable") == 0) {
932 enable |= 0x02;
933 } else if (strlencmp(cmd, "crt_disable") == 0) {
934 disable |= 0x02;
935 } else if (video_supported == IBMACPI_VIDEO_NEW &&
936 strlencmp(cmd, "dvi_enable") == 0) {
937 enable |= 0x08;
938 } else if (video_supported == IBMACPI_VIDEO_NEW &&
939 strlencmp(cmd, "dvi_disable") == 0) {
940 disable |= 0x08;
941 } else if (strlencmp(cmd, "auto_enable") == 0) {
942 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 1))
943 return -EIO;
944 } else if (strlencmp(cmd, "auto_disable") == 0) {
945 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", 0))
946 return -EIO;
947 } else if (strlencmp(cmd, "video_switch") == 0) {
948 if (!video_switch())
949 return -EIO;
950 } else if (strlencmp(cmd, "expand_toggle") == 0) {
951 if (!video_expand())
952 return -EIO;
953 } else
954 return -EINVAL;
957 if (enable || disable) {
958 status = (video_status() & 0x0f & ~disable) | enable;
959 if (!video_switch2(status))
960 return -EIO;
963 return 0;
966 /*************************************************************************
967 * Light (thinklight) subdriver
970 static int light_supported;
971 static int light_status_supported;
973 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
974 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
976 static int light_init(void)
978 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
979 light_supported = (cmos_handle || lght_handle) && !ledb_handle;
981 if (light_supported)
982 /* light status not supported on
983 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
984 light_status_supported = acpi_evalf(ec_handle, NULL,
985 "KBLT", "qv");
987 return 0;
990 static int light_read(char *p)
992 int len = 0;
993 int status = 0;
995 if (!light_supported) {
996 len += sprintf(p + len, "status:\t\tnot supported\n");
997 } else if (!light_status_supported) {
998 len += sprintf(p + len, "status:\t\tunknown\n");
999 len += sprintf(p + len, "commands:\ton, off\n");
1000 } else {
1001 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1002 return -EIO;
1003 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1004 len += sprintf(p + len, "commands:\ton, off\n");
1007 return len;
1010 static int light_write(char *buf)
1012 int cmos_cmd, lght_cmd;
1013 char *cmd;
1014 int success;
1016 if (!light_supported)
1017 return -ENODEV;
1019 while ((cmd = next_cmd(&buf))) {
1020 if (strlencmp(cmd, "on") == 0) {
1021 cmos_cmd = 0x0c;
1022 lght_cmd = 1;
1023 } else if (strlencmp(cmd, "off") == 0) {
1024 cmos_cmd = 0x0d;
1025 lght_cmd = 0;
1026 } else
1027 return -EINVAL;
1029 success = cmos_handle ?
1030 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1031 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1032 if (!success)
1033 return -EIO;
1036 return 0;
1039 /*************************************************************************
1040 * Dock subdriver
1043 /* don't list other alternatives as we install a notify handler on the 570 */
1044 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1046 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1048 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1049 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1050 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1051 "\\_SB.PCI.ISA.SLCE", /* 570 */
1052 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1054 #define dock_docked() (_sta(dock_handle) & 1)
1056 static void dock_notify(struct ibm_struct *ibm, u32 event)
1058 int docked = dock_docked();
1059 int pci = ibm->hid && strstr(ibm->hid, IBM_PCI_HID);
1061 if (event == 1 && !pci) /* 570 */
1062 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1063 else if (event == 1 && pci) /* 570 */
1064 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1065 else if (event == 3 && docked)
1066 acpi_bus_generate_event(ibm->device, event, 1); /* button */
1067 else if (event == 3 && !docked)
1068 acpi_bus_generate_event(ibm->device, event, 2); /* undock */
1069 else if (event == 0 && docked)
1070 acpi_bus_generate_event(ibm->device, event, 3); /* dock */
1071 else {
1072 printk(IBM_ERR "unknown dock event %d, status %d\n",
1073 event, _sta(dock_handle));
1074 acpi_bus_generate_event(ibm->device, event, 0); /* unknown */
1078 static int dock_read(char *p)
1080 int len = 0;
1081 int docked = dock_docked();
1083 if (!dock_handle)
1084 len += sprintf(p + len, "status:\t\tnot supported\n");
1085 else if (!docked)
1086 len += sprintf(p + len, "status:\t\tundocked\n");
1087 else {
1088 len += sprintf(p + len, "status:\t\tdocked\n");
1089 len += sprintf(p + len, "commands:\tdock, undock\n");
1092 return len;
1095 static int dock_write(char *buf)
1097 char *cmd;
1099 if (!dock_docked())
1100 return -ENODEV;
1102 while ((cmd = next_cmd(&buf))) {
1103 if (strlencmp(cmd, "undock") == 0) {
1104 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1105 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1106 return -EIO;
1107 } else if (strlencmp(cmd, "dock") == 0) {
1108 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1109 return -EIO;
1110 } else
1111 return -EINVAL;
1114 return 0;
1117 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1119 /*************************************************************************
1120 * Bay subdriver
1123 #ifdef CONFIG_THINKPAD_ACPI_BAY
1124 static int bay_status_supported;
1125 static int bay_status2_supported;
1126 static int bay_eject_supported;
1127 static int bay_eject2_supported;
1129 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1130 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1131 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1132 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1133 ); /* A21e, R30, R31 */
1134 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1135 "_EJ0", /* all others */
1136 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1137 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1138 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1139 ); /* all others */
1140 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1141 "_EJ0", /* 770x */
1142 ); /* all others */
1144 static int bay_init(void)
1146 bay_status_supported = bay_handle &&
1147 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1148 bay_status2_supported = bay2_handle &&
1149 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1151 bay_eject_supported = bay_handle && bay_ej_handle &&
1152 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1153 bay_eject2_supported = bay2_handle && bay2_ej_handle &&
1154 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1156 return 0;
1159 static void bay_notify(struct ibm_struct *ibm, u32 event)
1161 acpi_bus_generate_event(ibm->device, event, 0);
1164 #define bay_occupied(b) (_sta(b##_handle) & 1)
1166 static int bay_read(char *p)
1168 int len = 0;
1169 int occupied = bay_occupied(bay);
1170 int occupied2 = bay_occupied(bay2);
1171 int eject, eject2;
1173 len += sprintf(p + len, "status:\t\t%s\n", bay_status_supported ?
1174 (occupied ? "occupied" : "unoccupied") :
1175 "not supported");
1176 if (bay_status2_supported)
1177 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1178 "occupied" : "unoccupied");
1180 eject = bay_eject_supported && occupied;
1181 eject2 = bay_eject2_supported && occupied2;
1183 if (eject && eject2)
1184 len += sprintf(p + len, "commands:\teject, eject2\n");
1185 else if (eject)
1186 len += sprintf(p + len, "commands:\teject\n");
1187 else if (eject2)
1188 len += sprintf(p + len, "commands:\teject2\n");
1190 return len;
1193 static int bay_write(char *buf)
1195 char *cmd;
1197 if (!bay_eject_supported && !bay_eject2_supported)
1198 return -ENODEV;
1200 while ((cmd = next_cmd(&buf))) {
1201 if (bay_eject_supported && strlencmp(cmd, "eject") == 0) {
1202 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1203 return -EIO;
1204 } else if (bay_eject2_supported &&
1205 strlencmp(cmd, "eject2") == 0) {
1206 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1207 return -EIO;
1208 } else
1209 return -EINVAL;
1212 return 0;
1214 #endif /* CONFIG_THINKPAD_ACPI_BAY */
1216 /*************************************************************************
1217 * CMOS subdriver
1220 static int cmos_eval(int cmos_cmd)
1222 if (cmos_handle)
1223 return acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd);
1224 else
1225 return 1;
1228 static int cmos_read(char *p)
1230 int len = 0;
1232 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1233 R30, R31, T20-22, X20-21 */
1234 if (!cmos_handle)
1235 len += sprintf(p + len, "status:\t\tnot supported\n");
1236 else {
1237 len += sprintf(p + len, "status:\t\tsupported\n");
1238 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1241 return len;
1244 static int cmos_write(char *buf)
1246 char *cmd;
1247 int cmos_cmd;
1249 if (!cmos_handle)
1250 return -EINVAL;
1252 while ((cmd = next_cmd(&buf))) {
1253 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1254 cmos_cmd >= 0 && cmos_cmd <= 21) {
1255 /* cmos_cmd set */
1256 } else
1257 return -EINVAL;
1259 if (!cmos_eval(cmos_cmd))
1260 return -EIO;
1263 return 0;
1267 /*************************************************************************
1268 * LED subdriver
1271 static enum led_access_mode led_supported;
1273 IBM_HANDLE(led, ec, "SLED", /* 570 */
1274 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1275 "LED", /* all others */
1276 ); /* R30, R31 */
1278 static int led_init(void)
1280 if (!led_handle)
1281 /* led not supported on R30, R31 */
1282 led_supported = IBMACPI_LED_NONE;
1283 else if (strlencmp(led_path, "SLED") == 0)
1284 /* 570 */
1285 led_supported = IBMACPI_LED_570;
1286 else if (strlencmp(led_path, "SYSL") == 0)
1287 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1288 led_supported = IBMACPI_LED_OLD;
1289 else
1290 /* all others */
1291 led_supported = IBMACPI_LED_NEW;
1293 return 0;
1296 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1298 static int led_read(char *p)
1300 int len = 0;
1302 if (!led_supported) {
1303 len += sprintf(p + len, "status:\t\tnot supported\n");
1304 return len;
1306 len += sprintf(p + len, "status:\t\tsupported\n");
1308 if (led_supported == IBMACPI_LED_570) {
1309 /* 570 */
1310 int i, status;
1311 for (i = 0; i < 8; i++) {
1312 if (!acpi_evalf(ec_handle,
1313 &status, "GLED", "dd", 1 << i))
1314 return -EIO;
1315 len += sprintf(p + len, "%d:\t\t%s\n",
1316 i, led_status(status));
1320 len += sprintf(p + len, "commands:\t"
1321 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1323 return len;
1326 /* off, on, blink */
1327 static const int led_sled_arg1[] = { 0, 1, 3 };
1328 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1329 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1330 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1332 static int led_write(char *buf)
1334 char *cmd;
1335 int led, ind, ret;
1337 if (!led_supported)
1338 return -ENODEV;
1340 while ((cmd = next_cmd(&buf))) {
1341 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1342 return -EINVAL;
1344 if (strstr(cmd, "off")) {
1345 ind = 0;
1346 } else if (strstr(cmd, "on")) {
1347 ind = 1;
1348 } else if (strstr(cmd, "blink")) {
1349 ind = 2;
1350 } else
1351 return -EINVAL;
1353 if (led_supported == IBMACPI_LED_570) {
1354 /* 570 */
1355 led = 1 << led;
1356 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1357 led, led_sled_arg1[ind]))
1358 return -EIO;
1359 } else if (led_supported == IBMACPI_LED_OLD) {
1360 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1361 led = 1 << led;
1362 ret = ec_write(IBMACPI_LED_EC_HLMS, led);
1363 if (ret >= 0)
1364 ret =
1365 ec_write(IBMACPI_LED_EC_HLBL,
1366 led * led_exp_hlbl[ind]);
1367 if (ret >= 0)
1368 ret =
1369 ec_write(IBMACPI_LED_EC_HLCL,
1370 led * led_exp_hlcl[ind]);
1371 if (ret < 0)
1372 return ret;
1373 } else {
1374 /* all others */
1375 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1376 led, led_led_arg1[ind]))
1377 return -EIO;
1381 return 0;
1384 /*************************************************************************
1385 * Beep subdriver
1388 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1390 static int beep_read(char *p)
1392 int len = 0;
1394 if (!beep_handle)
1395 len += sprintf(p + len, "status:\t\tnot supported\n");
1396 else {
1397 len += sprintf(p + len, "status:\t\tsupported\n");
1398 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1401 return len;
1404 static int beep_write(char *buf)
1406 char *cmd;
1407 int beep_cmd;
1409 if (!beep_handle)
1410 return -ENODEV;
1412 while ((cmd = next_cmd(&buf))) {
1413 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1414 beep_cmd >= 0 && beep_cmd <= 17) {
1415 /* beep_cmd set */
1416 } else
1417 return -EINVAL;
1418 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1419 return -EIO;
1422 return 0;
1425 /*************************************************************************
1426 * Thermal subdriver
1429 static enum thermal_access_mode thermal_read_mode;
1431 static int thermal_init(void)
1433 u8 t, ta1, ta2;
1434 int i;
1435 int acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1437 if (ibm_thinkpad_ec_found && experimental) {
1439 * Direct EC access mode: sensors at registers
1440 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1441 * non-implemented, thermal sensors return 0x80 when
1442 * not available
1445 ta1 = ta2 = 0;
1446 for (i = 0; i < 8; i++) {
1447 if (likely(acpi_ec_read(0x78 + i, &t))) {
1448 ta1 |= t;
1449 } else {
1450 ta1 = 0;
1451 break;
1453 if (likely(acpi_ec_read(0xC0 + i, &t))) {
1454 ta2 |= t;
1455 } else {
1456 ta1 = 0;
1457 break;
1460 if (ta1 == 0) {
1461 /* This is sheer paranoia, but we handle it anyway */
1462 if (acpi_tmp7) {
1463 printk(IBM_ERR
1464 "ThinkPad ACPI EC access misbehaving, "
1465 "falling back to ACPI TMPx access mode\n");
1466 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1467 } else {
1468 printk(IBM_ERR
1469 "ThinkPad ACPI EC access misbehaving, "
1470 "disabling thermal sensors access\n");
1471 thermal_read_mode = IBMACPI_THERMAL_NONE;
1473 } else {
1474 thermal_read_mode =
1475 (ta2 != 0) ?
1476 IBMACPI_THERMAL_TPEC_16 : IBMACPI_THERMAL_TPEC_8;
1478 } else if (acpi_tmp7) {
1479 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1480 /* 600e/x, 770e, 770x */
1481 thermal_read_mode = IBMACPI_THERMAL_ACPI_UPDT;
1482 } else {
1483 /* Standard ACPI TMPx access, max 8 sensors */
1484 thermal_read_mode = IBMACPI_THERMAL_ACPI_TMP07;
1486 } else {
1487 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1488 thermal_read_mode = IBMACPI_THERMAL_NONE;
1491 return 0;
1494 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1496 int i, t;
1497 s8 tmp;
1498 char tmpi[] = "TMPi";
1500 if (!s)
1501 return -EINVAL;
1503 switch (thermal_read_mode) {
1504 #if IBMACPI_MAX_THERMAL_SENSORS >= 16
1505 case IBMACPI_THERMAL_TPEC_16:
1506 for (i = 0; i < 8; i++) {
1507 if (!acpi_ec_read(0xC0 + i, &tmp))
1508 return -EIO;
1509 s->temp[i + 8] = tmp * 1000;
1511 /* fallthrough */
1512 #endif
1513 case IBMACPI_THERMAL_TPEC_8:
1514 for (i = 0; i < 8; i++) {
1515 if (!acpi_ec_read(0x78 + i, &tmp))
1516 return -EIO;
1517 s->temp[i] = tmp * 1000;
1519 return (thermal_read_mode == IBMACPI_THERMAL_TPEC_16) ? 16 : 8;
1521 case IBMACPI_THERMAL_ACPI_UPDT:
1522 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1523 return -EIO;
1524 for (i = 0; i < 8; i++) {
1525 tmpi[3] = '0' + i;
1526 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1527 return -EIO;
1528 s->temp[i] = (t - 2732) * 100;
1530 return 8;
1532 case IBMACPI_THERMAL_ACPI_TMP07:
1533 for (i = 0; i < 8; i++) {
1534 tmpi[3] = '0' + i;
1535 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1536 return -EIO;
1537 s->temp[i] = t * 1000;
1539 return 8;
1541 case IBMACPI_THERMAL_NONE:
1542 default:
1543 return 0;
1547 static int thermal_read(char *p)
1549 int len = 0;
1550 int n, i;
1551 struct ibm_thermal_sensors_struct t;
1553 n = thermal_get_sensors(&t);
1554 if (unlikely(n < 0))
1555 return n;
1557 len += sprintf(p + len, "temperatures:\t");
1559 if (n > 0) {
1560 for (i = 0; i < (n - 1); i++)
1561 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1562 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1563 } else
1564 len += sprintf(p + len, "not supported\n");
1566 return len;
1569 /*************************************************************************
1570 * EC Dump subdriver
1573 static u8 ecdump_regs[256];
1575 static int ecdump_read(char *p)
1577 int len = 0;
1578 int i, j;
1579 u8 v;
1581 len += sprintf(p + len, "EC "
1582 " +00 +01 +02 +03 +04 +05 +06 +07"
1583 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1584 for (i = 0; i < 256; i += 16) {
1585 len += sprintf(p + len, "EC 0x%02x:", i);
1586 for (j = 0; j < 16; j++) {
1587 if (!acpi_ec_read(i + j, &v))
1588 break;
1589 if (v != ecdump_regs[i + j])
1590 len += sprintf(p + len, " *%02x", v);
1591 else
1592 len += sprintf(p + len, " %02x", v);
1593 ecdump_regs[i + j] = v;
1595 len += sprintf(p + len, "\n");
1596 if (j != 16)
1597 break;
1600 /* These are way too dangerous to advertise openly... */
1601 #if 0
1602 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
1603 " (<offset> is 00-ff, <value> is 00-ff)\n");
1604 len += sprintf(p + len, "commands:\t0x<offset> <value> "
1605 " (<offset> is 00-ff, <value> is 0-255)\n");
1606 #endif
1607 return len;
1610 static int ecdump_write(char *buf)
1612 char *cmd;
1613 int i, v;
1615 while ((cmd = next_cmd(&buf))) {
1616 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
1617 /* i and v set */
1618 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
1619 /* i and v set */
1620 } else
1621 return -EINVAL;
1622 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
1623 if (!acpi_ec_write(i, v))
1624 return -EIO;
1625 } else
1626 return -EINVAL;
1629 return 0;
1632 /*************************************************************************
1633 * Backlight/brightness subdriver
1636 static struct backlight_device *ibm_backlight_device = NULL;
1638 static struct backlight_properties ibm_backlight_data = {
1639 .owner = THIS_MODULE,
1640 .get_brightness = brightness_get,
1641 .update_status = brightness_update_status,
1642 .max_brightness = 7,
1645 static int brightness_init(void)
1647 int b;
1649 b = brightness_get(NULL);
1650 if (b < 0)
1651 return b;
1653 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
1654 &ibm_backlight_data);
1655 if (IS_ERR(ibm_backlight_device)) {
1656 printk(IBM_ERR "Could not register backlight device\n");
1657 return PTR_ERR(ibm_backlight_device);
1660 ibm_backlight_device->props->brightness = b;
1661 brightness_update_status(ibm_backlight_device);
1663 return 0;
1666 static void brightness_exit(void)
1668 if (ibm_backlight_device) {
1669 backlight_device_unregister(ibm_backlight_device);
1670 ibm_backlight_device = NULL;
1674 static int brightness_update_status(struct backlight_device *bd)
1676 return brightness_set(
1677 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
1678 bd->props->power == FB_BLANK_UNBLANK) ?
1679 bd->props->brightness : 0);
1682 static int brightness_get(struct backlight_device *bd)
1684 u8 level;
1685 if (!acpi_ec_read(brightness_offset, &level))
1686 return -EIO;
1688 level &= 0x7;
1690 return level;
1693 static int brightness_set(int value)
1695 int cmos_cmd, inc, i;
1696 int current_value = brightness_get(NULL);
1698 value &= 7;
1700 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
1701 inc = value > current_value ? 1 : -1;
1702 for (i = current_value; i != value; i += inc) {
1703 if (!cmos_eval(cmos_cmd))
1704 return -EIO;
1705 if (!acpi_ec_write(brightness_offset, i + inc))
1706 return -EIO;
1709 return 0;
1712 static int brightness_read(char *p)
1714 int len = 0;
1715 int level;
1717 if ((level = brightness_get(NULL)) < 0) {
1718 len += sprintf(p + len, "level:\t\tunreadable\n");
1719 } else {
1720 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
1721 len += sprintf(p + len, "commands:\tup, down\n");
1722 len += sprintf(p + len, "commands:\tlevel <level>"
1723 " (<level> is 0-7)\n");
1726 return len;
1729 static int brightness_write(char *buf)
1731 int level;
1732 int new_level;
1733 char *cmd;
1735 while ((cmd = next_cmd(&buf))) {
1736 if ((level = brightness_get(NULL)) < 0)
1737 return level;
1738 level &= 7;
1740 if (strlencmp(cmd, "up") == 0) {
1741 new_level = level == 7 ? 7 : level + 1;
1742 } else if (strlencmp(cmd, "down") == 0) {
1743 new_level = level == 0 ? 0 : level - 1;
1744 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1745 new_level >= 0 && new_level <= 7) {
1746 /* new_level set */
1747 } else
1748 return -EINVAL;
1750 brightness_set(new_level);
1753 return 0;
1756 /*************************************************************************
1757 * Volume subdriver
1760 static int volume_read(char *p)
1762 int len = 0;
1763 u8 level;
1765 if (!acpi_ec_read(volume_offset, &level)) {
1766 len += sprintf(p + len, "level:\t\tunreadable\n");
1767 } else {
1768 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
1769 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
1770 len += sprintf(p + len, "commands:\tup, down, mute\n");
1771 len += sprintf(p + len, "commands:\tlevel <level>"
1772 " (<level> is 0-15)\n");
1775 return len;
1778 static int volume_write(char *buf)
1780 int cmos_cmd, inc, i;
1781 u8 level, mute;
1782 int new_level, new_mute;
1783 char *cmd;
1785 while ((cmd = next_cmd(&buf))) {
1786 if (!acpi_ec_read(volume_offset, &level))
1787 return -EIO;
1788 new_mute = mute = level & 0x40;
1789 new_level = level = level & 0xf;
1791 if (strlencmp(cmd, "up") == 0) {
1792 if (mute)
1793 new_mute = 0;
1794 else
1795 new_level = level == 15 ? 15 : level + 1;
1796 } else if (strlencmp(cmd, "down") == 0) {
1797 if (mute)
1798 new_mute = 0;
1799 else
1800 new_level = level == 0 ? 0 : level - 1;
1801 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
1802 new_level >= 0 && new_level <= 15) {
1803 /* new_level set */
1804 } else if (strlencmp(cmd, "mute") == 0) {
1805 new_mute = 0x40;
1806 } else
1807 return -EINVAL;
1809 if (new_level != level) { /* mute doesn't change */
1810 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
1811 inc = new_level > level ? 1 : -1;
1813 if (mute && (!cmos_eval(cmos_cmd) ||
1814 !acpi_ec_write(volume_offset, level)))
1815 return -EIO;
1817 for (i = level; i != new_level; i += inc)
1818 if (!cmos_eval(cmos_cmd) ||
1819 !acpi_ec_write(volume_offset, i + inc))
1820 return -EIO;
1822 if (mute && (!cmos_eval(TP_CMOS_VOLUME_MUTE) ||
1823 !acpi_ec_write(volume_offset,
1824 new_level + mute)))
1825 return -EIO;
1828 if (new_mute != mute) { /* level doesn't change */
1829 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
1831 if (!cmos_eval(cmos_cmd) ||
1832 !acpi_ec_write(volume_offset, level + new_mute))
1833 return -EIO;
1837 return 0;
1841 /*************************************************************************
1842 * Fan subdriver
1846 * FAN ACCESS MODES
1848 * IBMACPI_FAN_RD_ACPI_GFAN:
1849 * ACPI GFAN method: returns fan level
1851 * see IBMACPI_FAN_WR_ACPI_SFAN
1852 * EC 0x2f not available if GFAN exists
1854 * IBMACPI_FAN_WR_ACPI_SFAN:
1855 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
1857 * EC 0x2f might be available *for reading*, but never for writing.
1859 * IBMACPI_FAN_WR_TPEC:
1860 * ThinkPad EC register 0x2f (HFSP): fan control loop mode Supported
1861 * on almost all ThinkPads
1863 * Fan speed changes of any sort (including those caused by the
1864 * disengaged mode) are usually done slowly by the firmware as the
1865 * maximum ammount of fan duty cycle change per second seems to be
1866 * limited.
1868 * Reading is not available if GFAN exists.
1869 * Writing is not available if SFAN exists.
1871 * Bits
1872 * 7 automatic mode engaged;
1873 * (default operation mode of the ThinkPad)
1874 * fan level is ignored in this mode.
1875 * 6 disengage mode (takes precedence over bit 7);
1876 * not available on all thinkpads. May disable
1877 * the tachometer, and speeds up fan to 100% duty-cycle,
1878 * which speeds it up far above the standard RPM
1879 * levels. It is not impossible that it could cause
1880 * hardware damage.
1881 * 5-3 unused in some models. Extra bits for fan level
1882 * in others, but still useless as all values above
1883 * 7 map to the same speed as level 7 in these models.
1884 * 2-0 fan level (0..7 usually)
1885 * 0x00 = stop
1886 * 0x07 = max (set when temperatures critical)
1887 * Some ThinkPads may have other levels, see
1888 * IBMACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
1890 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
1891 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
1892 * does so, its initial value is meaningless (0x07).
1894 * For firmware bugs, refer to:
1895 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1897 * ----
1899 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
1900 * Main fan tachometer reading (in RPM)
1902 * This register is present on all ThinkPads with a new-style EC, and
1903 * it is known not to be present on the A21m/e, and T22, as there is
1904 * something else in offset 0x84 according to the ACPI DSDT. Other
1905 * ThinkPads from this same time period (and earlier) probably lack the
1906 * tachometer as well.
1908 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
1909 * was never fixed by IBM to report the EC firmware version string
1910 * probably support the tachometer (like the early X models), so
1911 * detecting it is quite hard. We need more data to know for sure.
1913 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
1914 * might result.
1916 * FIRMWARE BUG: when EC 0x2f bit 6 is set (disengaged mode), this
1917 * register is not invalidated in ThinkPads that disable tachometer
1918 * readings. Thus, the tachometer readings go stale.
1920 * For firmware bugs, refer to:
1921 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
1923 * IBMACPI_FAN_WR_ACPI_FANS:
1924 * ThinkPad X31, X40, X41. Not available in the X60.
1926 * FANS ACPI handle: takes three arguments: low speed, medium speed,
1927 * high speed. ACPI DSDT seems to map these three speeds to levels
1928 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
1929 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
1931 * The speeds are stored on handles
1932 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
1934 * There are three default speed sets, acessible as handles:
1935 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
1937 * ACPI DSDT switches which set is in use depending on various
1938 * factors.
1940 * IBMACPI_FAN_WR_TPEC is also available and should be used to
1941 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
1942 * but the ACPI tables just mention level 7.
1945 static enum fan_status_access_mode fan_status_access_mode;
1946 static enum fan_control_access_mode fan_control_access_mode;
1947 static enum fan_control_commands fan_control_commands;
1949 static int fan_control_status_known;
1950 static u8 fan_control_initial_status;
1952 static void fan_watchdog_fire(struct work_struct *ignored);
1953 static int fan_watchdog_maxinterval;
1954 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
1956 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
1957 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
1958 "\\FSPD", /* 600e/x, 770e, 770x */
1959 ); /* all others */
1960 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
1961 "JFNS", /* 770x-JL */
1962 ); /* all others */
1964 static int fan_init(void)
1966 fan_status_access_mode = IBMACPI_FAN_NONE;
1967 fan_control_access_mode = IBMACPI_FAN_WR_NONE;
1968 fan_control_commands = 0;
1969 fan_control_status_known = 1;
1970 fan_watchdog_maxinterval = 0;
1972 if (gfan_handle) {
1973 /* 570, 600e/x, 770e, 770x */
1974 fan_status_access_mode = IBMACPI_FAN_RD_ACPI_GFAN;
1975 } else {
1976 /* all other ThinkPads: note that even old-style
1977 * ThinkPad ECs supports the fan control register */
1978 if (likely(acpi_ec_read(fan_status_offset,
1979 &fan_control_initial_status))) {
1980 fan_status_access_mode = IBMACPI_FAN_RD_TPEC;
1982 /* In some ThinkPads, neither the EC nor the ACPI
1983 * DSDT initialize the fan status, and it ends up
1984 * being set to 0x07 when it *could* be either
1985 * 0x07 or 0x80.
1987 * Enable for TP-1Y (T43), TP-78 (R51e),
1988 * TP-76 (R52), TP-70 (T43, R52), which are known
1989 * to be buggy. */
1990 if (fan_control_initial_status == 0x07 &&
1991 ibm_thinkpad_ec_found &&
1992 ((ibm_thinkpad_ec_found[0] == '1' &&
1993 ibm_thinkpad_ec_found[1] == 'Y') ||
1994 (ibm_thinkpad_ec_found[0] == '7' &&
1995 (ibm_thinkpad_ec_found[1] == '6' ||
1996 ibm_thinkpad_ec_found[1] == '8' ||
1997 ibm_thinkpad_ec_found[1] == '0'))
1998 )) {
1999 printk(IBM_NOTICE
2000 "fan_init: initial fan status is "
2001 "unknown, assuming it is in auto "
2002 "mode\n");
2003 fan_control_status_known = 0;
2005 } else {
2006 printk(IBM_ERR
2007 "ThinkPad ACPI EC access misbehaving, "
2008 "fan status and control unavailable\n");
2009 return 0;
2013 if (sfan_handle) {
2014 /* 570, 770x-JL */
2015 fan_control_access_mode = IBMACPI_FAN_WR_ACPI_SFAN;
2016 fan_control_commands |=
2017 IBMACPI_FAN_CMD_LEVEL | IBMACPI_FAN_CMD_ENABLE;
2018 } else {
2019 if (!gfan_handle) {
2020 /* gfan without sfan means no fan control */
2021 /* all other models implement TP EC 0x2f control */
2023 if (fans_handle) {
2024 /* X31, X40, X41 */
2025 fan_control_access_mode =
2026 IBMACPI_FAN_WR_ACPI_FANS;
2027 fan_control_commands |=
2028 IBMACPI_FAN_CMD_SPEED |
2029 IBMACPI_FAN_CMD_LEVEL |
2030 IBMACPI_FAN_CMD_ENABLE;
2031 } else {
2032 fan_control_access_mode = IBMACPI_FAN_WR_TPEC;
2033 fan_control_commands |=
2034 IBMACPI_FAN_CMD_LEVEL |
2035 IBMACPI_FAN_CMD_ENABLE;
2040 return 0;
2043 static int fan_get_status(u8 *status)
2045 u8 s;
2047 /* TODO:
2048 * Add IBMACPI_FAN_RD_ACPI_FANS ? */
2050 switch (fan_status_access_mode) {
2051 case IBMACPI_FAN_RD_ACPI_GFAN:
2052 /* 570, 600e/x, 770e, 770x */
2054 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
2055 return -EIO;
2057 if (likely(status))
2058 *status = s & 0x07;
2060 break;
2062 case IBMACPI_FAN_RD_TPEC:
2063 /* all except 570, 600e/x, 770e, 770x */
2064 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2065 return -EIO;
2067 if (likely(status))
2068 *status = s;
2070 break;
2072 default:
2073 return -ENXIO;
2076 return 0;
2079 static void fan_exit(void)
2081 cancel_delayed_work(&fan_watchdog_task);
2082 flush_scheduled_work();
2085 static int fan_get_speed(unsigned int *speed)
2087 u8 hi, lo;
2089 switch (fan_status_access_mode) {
2090 case IBMACPI_FAN_RD_TPEC:
2091 /* all except 570, 600e/x, 770e, 770x */
2092 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2093 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2094 return -EIO;
2096 if (likely(speed))
2097 *speed = (hi << 8) | lo;
2099 break;
2101 default:
2102 return -ENXIO;
2105 return 0;
2108 static void fan_watchdog_fire(struct work_struct *ignored)
2110 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2111 if (fan_set_enable()) {
2112 printk(IBM_ERR "fan watchdog: error while enabling fan\n");
2113 /* reschedule for later */
2114 fan_watchdog_reset();
2118 static void fan_watchdog_reset(void)
2120 static int fan_watchdog_active = 0;
2122 if (fan_watchdog_active)
2123 cancel_delayed_work(&fan_watchdog_task);
2125 if (fan_watchdog_maxinterval > 0) {
2126 fan_watchdog_active = 1;
2127 if (!schedule_delayed_work(&fan_watchdog_task,
2128 msecs_to_jiffies(fan_watchdog_maxinterval
2129 * 1000))) {
2130 printk(IBM_ERR "failed to schedule the fan watchdog, "
2131 "watchdog will not trigger\n");
2133 } else
2134 fan_watchdog_active = 0;
2137 static int fan_set_level(int level)
2139 switch (fan_control_access_mode) {
2140 case IBMACPI_FAN_WR_ACPI_SFAN:
2141 if (level >= 0 && level <= 7) {
2142 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2143 return -EIO;
2144 } else
2145 return -EINVAL;
2146 break;
2148 case IBMACPI_FAN_WR_ACPI_FANS:
2149 case IBMACPI_FAN_WR_TPEC:
2150 if ((level != IBMACPI_FAN_EC_AUTO) &&
2151 (level != IBMACPI_FAN_EC_DISENGAGED) &&
2152 ((level < 0) || (level > 7)))
2153 return -EINVAL;
2155 if (!acpi_ec_write(fan_status_offset, level))
2156 return -EIO;
2157 else
2158 fan_control_status_known = 1;
2159 break;
2161 default:
2162 return -ENXIO;
2164 return 0;
2167 static int fan_set_enable(void)
2169 u8 s;
2170 int rc;
2172 switch (fan_control_access_mode) {
2173 case IBMACPI_FAN_WR_ACPI_FANS:
2174 case IBMACPI_FAN_WR_TPEC:
2175 if ((rc = fan_get_status(&s)) < 0)
2176 return rc;
2178 /* Don't go out of emergency fan mode */
2179 if (s != 7)
2180 s = IBMACPI_FAN_EC_AUTO;
2182 if (!acpi_ec_write(fan_status_offset, s))
2183 return -EIO;
2184 else
2185 fan_control_status_known = 1;
2186 break;
2188 case IBMACPI_FAN_WR_ACPI_SFAN:
2189 if ((rc = fan_get_status(&s)) < 0)
2190 return rc;
2192 s &= 0x07;
2194 /* Set fan to at least level 4 */
2195 if (s < 4)
2196 s = 4;
2198 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2199 return -EIO;
2200 break;
2202 default:
2203 return -ENXIO;
2205 return 0;
2208 static int fan_set_disable(void)
2210 switch (fan_control_access_mode) {
2211 case IBMACPI_FAN_WR_ACPI_FANS:
2212 case IBMACPI_FAN_WR_TPEC:
2213 if (!acpi_ec_write(fan_status_offset, 0x00))
2214 return -EIO;
2215 else
2216 fan_control_status_known = 1;
2217 break;
2219 case IBMACPI_FAN_WR_ACPI_SFAN:
2220 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2221 return -EIO;
2222 break;
2224 default:
2225 return -ENXIO;
2227 return 0;
2230 static int fan_set_speed(int speed)
2232 switch (fan_control_access_mode) {
2233 case IBMACPI_FAN_WR_ACPI_FANS:
2234 if (speed >= 0 && speed <= 65535) {
2235 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2236 speed, speed, speed))
2237 return -EIO;
2238 } else
2239 return -EINVAL;
2240 break;
2242 default:
2243 return -ENXIO;
2245 return 0;
2248 static int fan_read(char *p)
2250 int len = 0;
2251 int rc;
2252 u8 status;
2253 unsigned int speed = 0;
2255 switch (fan_status_access_mode) {
2256 case IBMACPI_FAN_RD_ACPI_GFAN:
2257 /* 570, 600e/x, 770e, 770x */
2258 if ((rc = fan_get_status(&status)) < 0)
2259 return rc;
2261 len += sprintf(p + len, "status:\t\t%s\n"
2262 "level:\t\t%d\n",
2263 (status != 0) ? "enabled" : "disabled", status);
2264 break;
2266 case IBMACPI_FAN_RD_TPEC:
2267 /* all except 570, 600e/x, 770e, 770x */
2268 if ((rc = fan_get_status(&status)) < 0)
2269 return rc;
2271 if (unlikely(!fan_control_status_known)) {
2272 if (status != fan_control_initial_status)
2273 fan_control_status_known = 1;
2274 else
2275 /* Return most likely status. In fact, it
2276 * might be the only possible status */
2277 status = IBMACPI_FAN_EC_AUTO;
2280 len += sprintf(p + len, "status:\t\t%s\n",
2281 (status != 0) ? "enabled" : "disabled");
2283 /* No ThinkPad boots on disengaged mode, we can safely
2284 * assume the tachometer is online if fan control status
2285 * was unknown */
2286 if ((rc = fan_get_speed(&speed)) < 0)
2287 return rc;
2289 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2291 if (status & IBMACPI_FAN_EC_DISENGAGED)
2292 /* Disengaged mode takes precedence */
2293 len += sprintf(p + len, "level:\t\tdisengaged\n");
2294 else if (status & IBMACPI_FAN_EC_AUTO)
2295 len += sprintf(p + len, "level:\t\tauto\n");
2296 else
2297 len += sprintf(p + len, "level:\t\t%d\n", status);
2298 break;
2300 case IBMACPI_FAN_NONE:
2301 default:
2302 len += sprintf(p + len, "status:\t\tnot supported\n");
2305 if (fan_control_commands & IBMACPI_FAN_CMD_LEVEL) {
2306 len += sprintf(p + len, "commands:\tlevel <level>");
2308 switch (fan_control_access_mode) {
2309 case IBMACPI_FAN_WR_ACPI_SFAN:
2310 len += sprintf(p + len, " (<level> is 0-7)\n");
2311 break;
2313 default:
2314 len += sprintf(p + len, " (<level> is 0-7, "
2315 "auto, disengaged)\n");
2316 break;
2320 if (fan_control_commands & IBMACPI_FAN_CMD_ENABLE)
2321 len += sprintf(p + len, "commands:\tenable, disable\n"
2322 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2323 "1-120 (seconds))\n");
2325 if (fan_control_commands & IBMACPI_FAN_CMD_SPEED)
2326 len += sprintf(p + len, "commands:\tspeed <speed>"
2327 " (<speed> is 0-65535)\n");
2329 return len;
2332 static int fan_write_cmd_level(const char *cmd, int *rc)
2334 int level;
2336 if (strlencmp(cmd, "level auto") == 0)
2337 level = IBMACPI_FAN_EC_AUTO;
2338 else if (strlencmp(cmd, "level disengaged") == 0)
2339 level = IBMACPI_FAN_EC_DISENGAGED;
2340 else if (sscanf(cmd, "level %d", &level) != 1)
2341 return 0;
2343 if ((*rc = fan_set_level(level)) == -ENXIO)
2344 printk(IBM_ERR "level command accepted for unsupported "
2345 "access mode %d", fan_control_access_mode);
2347 return 1;
2350 static int fan_write_cmd_enable(const char *cmd, int *rc)
2352 if (strlencmp(cmd, "enable") != 0)
2353 return 0;
2355 if ((*rc = fan_set_enable()) == -ENXIO)
2356 printk(IBM_ERR "enable command accepted for unsupported "
2357 "access mode %d", fan_control_access_mode);
2359 return 1;
2362 static int fan_write_cmd_disable(const char *cmd, int *rc)
2364 if (strlencmp(cmd, "disable") != 0)
2365 return 0;
2367 if ((*rc = fan_set_disable()) == -ENXIO)
2368 printk(IBM_ERR "disable command accepted for unsupported "
2369 "access mode %d", fan_control_access_mode);
2371 return 1;
2374 static int fan_write_cmd_speed(const char *cmd, int *rc)
2376 int speed;
2378 /* TODO:
2379 * Support speed <low> <medium> <high> ? */
2381 if (sscanf(cmd, "speed %d", &speed) != 1)
2382 return 0;
2384 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2385 printk(IBM_ERR "speed command accepted for unsupported "
2386 "access mode %d", fan_control_access_mode);
2388 return 1;
2391 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2393 int interval;
2395 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2396 return 0;
2398 if (interval < 0 || interval > 120)
2399 *rc = -EINVAL;
2400 else
2401 fan_watchdog_maxinterval = interval;
2403 return 1;
2406 static int fan_write(char *buf)
2408 char *cmd;
2409 int rc = 0;
2411 while (!rc && (cmd = next_cmd(&buf))) {
2412 if (!((fan_control_commands & IBMACPI_FAN_CMD_LEVEL) &&
2413 fan_write_cmd_level(cmd, &rc)) &&
2414 !((fan_control_commands & IBMACPI_FAN_CMD_ENABLE) &&
2415 (fan_write_cmd_enable(cmd, &rc) ||
2416 fan_write_cmd_disable(cmd, &rc) ||
2417 fan_write_cmd_watchdog(cmd, &rc))) &&
2418 !((fan_control_commands & IBMACPI_FAN_CMD_SPEED) &&
2419 fan_write_cmd_speed(cmd, &rc))
2421 rc = -EINVAL;
2422 else if (!rc)
2423 fan_watchdog_reset();
2426 return rc;
2429 /****************************************************************************
2430 ****************************************************************************
2432 * Infrastructure
2434 ****************************************************************************
2435 ****************************************************************************/
2437 /* /proc support */
2438 static struct proc_dir_entry *proc_dir = NULL;
2440 /* Subdriver registry */
2441 static struct ibm_struct ibms[] = {
2443 .name = "driver",
2444 .init = ibm_acpi_driver_init,
2445 .read = ibm_acpi_driver_read,
2448 .name = "hotkey",
2449 .hid = IBM_HKEY_HID,
2450 .init = hotkey_init,
2451 .read = hotkey_read,
2452 .write = hotkey_write,
2453 .exit = hotkey_exit,
2454 .notify = hotkey_notify,
2455 .handle = &hkey_handle,
2456 .type = ACPI_DEVICE_NOTIFY,
2459 .name = "bluetooth",
2460 .init = bluetooth_init,
2461 .read = bluetooth_read,
2462 .write = bluetooth_write,
2465 .name = "wan",
2466 .init = wan_init,
2467 .read = wan_read,
2468 .write = wan_write,
2469 .experimental = 1,
2472 .name = "video",
2473 .init = video_init,
2474 .read = video_read,
2475 .write = video_write,
2476 .exit = video_exit,
2479 .name = "light",
2480 .init = light_init,
2481 .read = light_read,
2482 .write = light_write,
2484 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2486 .name = "dock",
2487 .read = dock_read,
2488 .write = dock_write,
2489 .notify = dock_notify,
2490 .handle = &dock_handle,
2491 .type = ACPI_SYSTEM_NOTIFY,
2494 .name = "dock",
2495 .hid = IBM_PCI_HID,
2496 .notify = dock_notify,
2497 .handle = &pci_handle,
2498 .type = ACPI_SYSTEM_NOTIFY,
2500 #endif
2501 #ifdef CONFIG_THINKPAD_ACPI_BAY
2503 .name = "bay",
2504 .init = bay_init,
2505 .read = bay_read,
2506 .write = bay_write,
2507 .notify = bay_notify,
2508 .handle = &bay_handle,
2509 .type = ACPI_SYSTEM_NOTIFY,
2511 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2513 .name = "cmos",
2514 .read = cmos_read,
2515 .write = cmos_write,
2518 .name = "led",
2519 .init = led_init,
2520 .read = led_read,
2521 .write = led_write,
2524 .name = "beep",
2525 .read = beep_read,
2526 .write = beep_write,
2529 .name = "thermal",
2530 .init = thermal_init,
2531 .read = thermal_read,
2534 .name = "ecdump",
2535 .read = ecdump_read,
2536 .write = ecdump_write,
2537 .experimental = 1,
2540 .name = "brightness",
2541 .read = brightness_read,
2542 .write = brightness_write,
2543 .init = brightness_init,
2544 .exit = brightness_exit,
2547 .name = "volume",
2548 .read = volume_read,
2549 .write = volume_write,
2552 .name = "fan",
2553 .read = fan_read,
2554 .write = fan_write,
2555 .init = fan_init,
2556 .exit = fan_exit,
2557 .experimental = 1,
2562 * Module and infrastructure proble, init and exit handling
2565 static int __init ibm_init(struct ibm_struct *ibm)
2567 int ret;
2568 struct proc_dir_entry *entry;
2570 if (ibm->experimental && !experimental)
2571 return 0;
2573 if (ibm->hid) {
2574 ret = register_ibmacpi_subdriver(ibm);
2575 if (ret < 0)
2576 return ret;
2577 ibm->driver_registered = 1;
2580 if (ibm->init) {
2581 ret = ibm->init();
2582 if (ret != 0)
2583 return ret;
2584 ibm->init_called = 1;
2587 if (ibm->read) {
2588 entry = create_proc_entry(ibm->name,
2589 S_IFREG | S_IRUGO | S_IWUSR,
2590 proc_dir);
2591 if (!entry) {
2592 printk(IBM_ERR "unable to create proc entry %s\n",
2593 ibm->name);
2594 return -ENODEV;
2596 entry->owner = THIS_MODULE;
2597 entry->data = ibm;
2598 entry->read_proc = &dispatch_read;
2599 if (ibm->write)
2600 entry->write_proc = &dispatch_write;
2601 ibm->proc_created = 1;
2604 if (ibm->notify) {
2605 ret = setup_notify(ibm);
2606 if (ret == -ENODEV) {
2607 printk(IBM_NOTICE "disabling subdriver %s\n",
2608 ibm->name);
2609 ibm_exit(ibm);
2610 return 0;
2612 if (ret < 0)
2613 return ret;
2616 return 0;
2619 static void ibm_exit(struct ibm_struct *ibm)
2621 if (ibm->notify_installed)
2622 acpi_remove_notify_handler(*ibm->handle, ibm->type,
2623 dispatch_notify);
2625 if (ibm->proc_created)
2626 remove_proc_entry(ibm->name, proc_dir);
2628 if (ibm->init_called && ibm->exit)
2629 ibm->exit();
2631 if (ibm->driver_registered) {
2632 acpi_bus_unregister_driver(ibm->driver);
2633 kfree(ibm->driver);
2637 /* Probing */
2639 static char *ibm_thinkpad_ec_found = NULL;
2641 static char* __init check_dmi_for_ec(void)
2643 struct dmi_device *dev = NULL;
2644 char ec_fw_string[18];
2647 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
2648 * X32 or newer, all Z series; Some models must have an
2649 * up-to-date BIOS or they will not be detected.
2651 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
2653 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
2654 if (sscanf(dev->name,
2655 "IBM ThinkPad Embedded Controller -[%17c",
2656 ec_fw_string) == 1) {
2657 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
2658 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
2659 return kstrdup(ec_fw_string, GFP_KERNEL);
2662 return NULL;
2665 /* Module init, exit, parameters */
2667 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
2669 unsigned int i;
2671 for (i = 0; i < ARRAY_SIZE(ibms); i++)
2672 if (strcmp(ibms[i].name, kp->name) == 0 && ibms[i].write) {
2673 if (strlen(val) > sizeof(ibms[i].param) - 2)
2674 return -ENOSPC;
2675 strcpy(ibms[i].param, val);
2676 strcat(ibms[i].param, ",");
2677 return 0;
2680 return -EINVAL;
2683 static int experimental;
2684 module_param(experimental, int, 0);
2686 #define IBM_PARAM(feature) \
2687 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
2689 IBM_PARAM(hotkey);
2690 IBM_PARAM(bluetooth);
2691 IBM_PARAM(video);
2692 IBM_PARAM(light);
2693 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2694 IBM_PARAM(dock);
2695 #endif
2696 #ifdef CONFIG_THINKPAD_ACPI_BAY
2697 IBM_PARAM(bay);
2698 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2699 IBM_PARAM(cmos);
2700 IBM_PARAM(led);
2701 IBM_PARAM(beep);
2702 IBM_PARAM(ecdump);
2703 IBM_PARAM(brightness);
2704 IBM_PARAM(volume);
2705 IBM_PARAM(fan);
2707 static int __init acpi_ibm_init(void)
2709 int ret, i;
2711 if (acpi_disabled)
2712 return -ENODEV;
2714 if (!acpi_specific_hotkey_enabled) {
2715 printk(IBM_ERR "using generic hotkey driver\n");
2716 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 acpi_ibm_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 acpi_ibm_exit();
2770 return ret;
2774 return 0;
2777 static void acpi_ibm_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(acpi_ibm_init);
2792 module_exit(acpi_ibm_exit);