ACPI: thinkpad-acpi: improve fan watchdog messages
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob9871a63417a2c16c87e203502e70717876e04fd7
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 static int issue_thinkpad_cmos_command(int cmos_cmd)
278 if (!cmos_handle)
279 return -ENXIO;
281 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
282 return -EIO;
284 return 0;
287 /*************************************************************************
288 * ACPI device model
291 static void drv_acpi_handle_init(char *name,
292 acpi_handle *handle, acpi_handle parent,
293 char **paths, int num_paths, char **path)
295 int i;
296 acpi_status status;
298 for (i = 0; i < num_paths; i++) {
299 status = acpi_get_handle(parent, paths[i], handle);
300 if (ACPI_SUCCESS(status)) {
301 *path = paths[i];
302 return;
306 *handle = NULL;
309 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
311 struct ibm_struct *ibm = data;
313 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
314 return;
316 ibm->acpi->notify(ibm, event);
319 static int __init setup_acpi_notify(struct ibm_struct *ibm)
321 acpi_status status;
322 int ret;
324 BUG_ON(!ibm->acpi);
326 if (!*ibm->acpi->handle)
327 return 0;
329 dbg_printk(TPACPI_DBG_INIT,
330 "setting up ACPI notify for %s\n", ibm->name);
332 ret = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
333 if (ret < 0) {
334 printk(IBM_ERR "%s device not present\n", ibm->name);
335 return -ENODEV;
338 acpi_driver_data(ibm->acpi->device) = ibm;
339 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
340 IBM_ACPI_EVENT_PREFIX,
341 ibm->name);
343 status = acpi_install_notify_handler(*ibm->acpi->handle,
344 ibm->acpi->type, dispatch_acpi_notify, ibm);
345 if (ACPI_FAILURE(status)) {
346 if (status == AE_ALREADY_EXISTS) {
347 printk(IBM_NOTICE "another device driver is already handling %s events\n",
348 ibm->name);
349 } else {
350 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
351 ibm->name, status);
353 return -ENODEV;
355 ibm->flags.acpi_notify_installed = 1;
356 return 0;
359 static int __init tpacpi_device_add(struct acpi_device *device)
361 return 0;
364 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
366 int ret;
368 dbg_printk(TPACPI_DBG_INIT,
369 "registering %s as an ACPI driver\n", ibm->name);
371 BUG_ON(!ibm->acpi);
373 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
374 if (!ibm->acpi->driver) {
375 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
376 return -ENOMEM;
379 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
380 ibm->acpi->driver->ids = ibm->acpi->hid;
381 ibm->acpi->driver->ops.add = &tpacpi_device_add;
383 ret = acpi_bus_register_driver(ibm->acpi->driver);
384 if (ret < 0) {
385 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
386 ibm->acpi->hid, ret);
387 kfree(ibm->acpi->driver);
388 ibm->acpi->driver = NULL;
389 } else if (!ret)
390 ibm->flags.acpi_driver_registered = 1;
392 return ret;
396 /****************************************************************************
397 ****************************************************************************
399 * Procfs Helpers
401 ****************************************************************************
402 ****************************************************************************/
404 static int dispatch_procfs_read(char *page, char **start, off_t off,
405 int count, int *eof, void *data)
407 struct ibm_struct *ibm = data;
408 int len;
410 if (!ibm || !ibm->read)
411 return -EINVAL;
413 len = ibm->read(page);
414 if (len < 0)
415 return len;
417 if (len <= off + count)
418 *eof = 1;
419 *start = page + off;
420 len -= off;
421 if (len > count)
422 len = count;
423 if (len < 0)
424 len = 0;
426 return len;
429 static int dispatch_procfs_write(struct file *file,
430 const char __user * userbuf,
431 unsigned long count, void *data)
433 struct ibm_struct *ibm = data;
434 char *kernbuf;
435 int ret;
437 if (!ibm || !ibm->write)
438 return -EINVAL;
440 kernbuf = kmalloc(count + 2, GFP_KERNEL);
441 if (!kernbuf)
442 return -ENOMEM;
444 if (copy_from_user(kernbuf, userbuf, count)) {
445 kfree(kernbuf);
446 return -EFAULT;
449 kernbuf[count] = 0;
450 strcat(kernbuf, ",");
451 ret = ibm->write(kernbuf);
452 if (ret == 0)
453 ret = count;
455 kfree(kernbuf);
457 return ret;
460 static char *next_cmd(char **cmds)
462 char *start = *cmds;
463 char *end;
465 while ((end = strchr(start, ',')) && end == start)
466 start = end + 1;
468 if (!end)
469 return NULL;
471 *end = 0;
472 *cmds = end + 1;
473 return start;
477 /****************************************************************************
478 ****************************************************************************
480 * Subdrivers
482 ****************************************************************************
483 ****************************************************************************/
485 /*************************************************************************
486 * thinkpad-acpi init subdriver
489 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
491 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
492 printk(IBM_INFO "%s\n", IBM_URL);
494 if (ibm_thinkpad_ec_found)
495 printk(IBM_INFO "ThinkPad EC firmware %s\n",
496 ibm_thinkpad_ec_found);
498 return 0;
501 static int thinkpad_acpi_driver_read(char *p)
503 int len = 0;
505 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
506 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
508 return len;
511 static struct ibm_struct thinkpad_acpi_driver_data = {
512 .name = "driver",
513 .read = thinkpad_acpi_driver_read,
516 /*************************************************************************
517 * Hotkey subdriver
520 static int hotkey_orig_status;
521 static int hotkey_orig_mask;
523 static int __init hotkey_init(struct ibm_init_struct *iibm)
525 int res;
527 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
529 IBM_ACPIHANDLE_INIT(hkey);
531 /* hotkey not supported on 570 */
532 tp_features.hotkey = hkey_handle != NULL;
534 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
535 str_supported(tp_features.hotkey));
537 if (tp_features.hotkey) {
538 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
539 A30, R30, R31, T20-22, X20-21, X22-24 */
540 tp_features.hotkey_mask =
541 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
543 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
544 str_supported(tp_features.hotkey_mask));
546 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
547 if (res)
548 return res;
551 return (tp_features.hotkey)? 0 : 1;
554 static void hotkey_exit(void)
556 int res;
558 if (tp_features.hotkey) {
559 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
560 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
561 if (res)
562 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
566 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
568 int hkey;
570 if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
571 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
572 else {
573 printk(IBM_ERR "unknown hotkey event %d\n", event);
574 acpi_bus_generate_event(ibm->acpi->device, event, 0);
578 static int hotkey_get(int *status, int *mask)
580 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
581 return -EIO;
583 if (tp_features.hotkey_mask)
584 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
585 return -EIO;
587 return 0;
590 static int hotkey_set(int status, int mask)
592 int i;
594 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
595 return -EIO;
597 if (tp_features.hotkey_mask)
598 for (i = 0; i < 32; i++) {
599 int bit = ((1 << i) & mask) != 0;
600 if (!acpi_evalf(hkey_handle,
601 NULL, "MHKM", "vdd", i + 1, bit))
602 return -EIO;
605 return 0;
608 static int hotkey_read(char *p)
610 int res, status, mask;
611 int len = 0;
613 if (!tp_features.hotkey) {
614 len += sprintf(p + len, "status:\t\tnot supported\n");
615 return len;
618 res = hotkey_get(&status, &mask);
619 if (res)
620 return res;
622 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
623 if (tp_features.hotkey_mask) {
624 len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
625 len += sprintf(p + len,
626 "commands:\tenable, disable, reset, <mask>\n");
627 } else {
628 len += sprintf(p + len, "mask:\t\tnot supported\n");
629 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
632 return len;
635 static int hotkey_write(char *buf)
637 int res, status, mask;
638 char *cmd;
639 int do_cmd = 0;
641 if (!tp_features.hotkey)
642 return -ENODEV;
644 res = hotkey_get(&status, &mask);
645 if (res)
646 return res;
648 while ((cmd = next_cmd(&buf))) {
649 if (strlencmp(cmd, "enable") == 0) {
650 status = 1;
651 } else if (strlencmp(cmd, "disable") == 0) {
652 status = 0;
653 } else if (strlencmp(cmd, "reset") == 0) {
654 status = hotkey_orig_status;
655 mask = hotkey_orig_mask;
656 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
657 /* mask set */
658 } else if (sscanf(cmd, "%x", &mask) == 1) {
659 /* mask set */
660 } else
661 return -EINVAL;
662 do_cmd = 1;
665 if (do_cmd) {
666 res = hotkey_set(status, mask);
667 if (res)
668 return res;
671 return 0;
674 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
675 .hid = IBM_HKEY_HID,
676 .notify = hotkey_notify,
677 .handle = &hkey_handle,
678 .type = ACPI_DEVICE_NOTIFY,
681 static struct ibm_struct hotkey_driver_data = {
682 .name = "hotkey",
683 .read = hotkey_read,
684 .write = hotkey_write,
685 .exit = hotkey_exit,
686 .acpi = &ibm_hotkey_acpidriver,
689 /*************************************************************************
690 * Bluetooth subdriver
693 static int __init bluetooth_init(struct ibm_init_struct *iibm)
695 int status = 0;
697 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
699 IBM_ACPIHANDLE_INIT(hkey);
701 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
702 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
703 tp_features.bluetooth = hkey_handle &&
704 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
706 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
707 str_supported(tp_features.bluetooth),
708 status);
710 if (tp_features.bluetooth &&
711 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
712 /* no bluetooth hardware present in system */
713 tp_features.bluetooth = 0;
714 dbg_printk(TPACPI_DBG_INIT,
715 "bluetooth hardware not installed\n");
718 return (tp_features.bluetooth)? 0 : 1;
721 static int bluetooth_get_radiosw(void)
723 int status;
725 if (!tp_features.bluetooth)
726 return -ENODEV;
728 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
729 return -EIO;
731 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
734 static int bluetooth_set_radiosw(int radio_on)
736 int status;
738 if (!tp_features.bluetooth)
739 return -ENODEV;
741 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
742 return -EIO;
743 if (radio_on)
744 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
745 else
746 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
747 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
748 return -EIO;
750 return 0;
753 static int bluetooth_read(char *p)
755 int len = 0;
756 int status = bluetooth_get_radiosw();
758 if (!tp_features.bluetooth)
759 len += sprintf(p + len, "status:\t\tnot supported\n");
760 else {
761 len += sprintf(p + len, "status:\t\t%s\n",
762 (status)? "enabled" : "disabled");
763 len += sprintf(p + len, "commands:\tenable, disable\n");
766 return len;
769 static int bluetooth_write(char *buf)
771 char *cmd;
773 if (!tp_features.bluetooth)
774 return -ENODEV;
776 while ((cmd = next_cmd(&buf))) {
777 if (strlencmp(cmd, "enable") == 0) {
778 bluetooth_set_radiosw(1);
779 } else if (strlencmp(cmd, "disable") == 0) {
780 bluetooth_set_radiosw(0);
781 } else
782 return -EINVAL;
785 return 0;
788 static struct ibm_struct bluetooth_driver_data = {
789 .name = "bluetooth",
790 .read = bluetooth_read,
791 .write = bluetooth_write,
794 /*************************************************************************
795 * Wan subdriver
798 static int __init wan_init(struct ibm_init_struct *iibm)
800 int status = 0;
802 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
804 IBM_ACPIHANDLE_INIT(hkey);
806 tp_features.wan = hkey_handle &&
807 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
809 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
810 str_supported(tp_features.wan),
811 status);
813 if (tp_features.wan &&
814 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
815 /* no wan hardware present in system */
816 tp_features.wan = 0;
817 dbg_printk(TPACPI_DBG_INIT,
818 "wan hardware not installed\n");
821 return (tp_features.wan)? 0 : 1;
824 static int wan_get_radiosw(void)
826 int status;
828 if (!tp_features.wan)
829 return -ENODEV;
831 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
832 return -EIO;
834 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
837 static int wan_set_radiosw(int radio_on)
839 int status;
841 if (!tp_features.wan)
842 return -ENODEV;
844 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
845 return -EIO;
846 if (radio_on)
847 status |= TP_ACPI_WANCARD_RADIOSSW;
848 else
849 status &= ~TP_ACPI_WANCARD_RADIOSSW;
850 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
851 return -EIO;
853 return 0;
856 static int wan_read(char *p)
858 int len = 0;
859 int status = wan_get_radiosw();
861 if (!tp_features.wan)
862 len += sprintf(p + len, "status:\t\tnot supported\n");
863 else {
864 len += sprintf(p + len, "status:\t\t%s\n",
865 (status)? "enabled" : "disabled");
866 len += sprintf(p + len, "commands:\tenable, disable\n");
869 return len;
872 static int wan_write(char *buf)
874 char *cmd;
876 if (!tp_features.wan)
877 return -ENODEV;
879 while ((cmd = next_cmd(&buf))) {
880 if (strlencmp(cmd, "enable") == 0) {
881 wan_set_radiosw(1);
882 } else if (strlencmp(cmd, "disable") == 0) {
883 wan_set_radiosw(0);
884 } else
885 return -EINVAL;
888 return 0;
891 static struct ibm_struct wan_driver_data = {
892 .name = "wan",
893 .read = wan_read,
894 .write = wan_write,
895 .flags.experimental = 1,
898 /*************************************************************************
899 * Video subdriver
902 static enum video_access_mode video_supported;
903 static int video_orig_autosw;
905 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
906 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
907 "\\_SB.PCI0.VID0", /* 770e */
908 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
909 "\\_SB.PCI0.AGP.VID", /* all others */
910 ); /* R30, R31 */
912 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
914 static int __init video_init(struct ibm_init_struct *iibm)
916 int ivga;
918 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
920 IBM_ACPIHANDLE_INIT(vid);
921 IBM_ACPIHANDLE_INIT(vid2);
923 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
924 /* G41, assume IVGA doesn't change */
925 vid_handle = vid2_handle;
927 if (!vid_handle)
928 /* video switching not supported on R30, R31 */
929 video_supported = TPACPI_VIDEO_NONE;
930 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
931 /* 570 */
932 video_supported = TPACPI_VIDEO_570;
933 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
934 /* 600e/x, 770e, 770x */
935 video_supported = TPACPI_VIDEO_770;
936 else
937 /* all others */
938 video_supported = TPACPI_VIDEO_NEW;
940 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
941 str_supported(video_supported != TPACPI_VIDEO_NONE),
942 video_supported);
944 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
947 static void video_exit(void)
949 dbg_printk(TPACPI_DBG_EXIT,
950 "restoring original video autoswitch mode\n");
951 if (video_autosw_set(video_orig_autosw))
952 printk(IBM_ERR "error while trying to restore original "
953 "video autoswitch mode\n");
956 static int video_outputsw_get(void)
958 int status = 0;
959 int i;
961 switch (video_supported) {
962 case TPACPI_VIDEO_570:
963 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
964 TP_ACPI_VIDEO_570_PHSCMD))
965 return -EIO;
966 status = i & TP_ACPI_VIDEO_570_PHSMASK;
967 break;
968 case TPACPI_VIDEO_770:
969 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
970 return -EIO;
971 if (i)
972 status |= TP_ACPI_VIDEO_S_LCD;
973 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
974 return -EIO;
975 if (i)
976 status |= TP_ACPI_VIDEO_S_CRT;
977 break;
978 case TPACPI_VIDEO_NEW:
979 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
980 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
981 return -EIO;
982 if (i)
983 status |= TP_ACPI_VIDEO_S_CRT;
985 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
986 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
987 return -EIO;
988 if (i)
989 status |= TP_ACPI_VIDEO_S_LCD;
990 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
991 return -EIO;
992 if (i)
993 status |= TP_ACPI_VIDEO_S_DVI;
994 break;
995 default:
996 return -ENOSYS;
999 return status;
1002 static int video_outputsw_set(int status)
1004 int autosw;
1005 int res = 0;
1007 switch (video_supported) {
1008 case TPACPI_VIDEO_570:
1009 res = acpi_evalf(NULL, NULL,
1010 "\\_SB.PHS2", "vdd",
1011 TP_ACPI_VIDEO_570_PHS2CMD,
1012 status | TP_ACPI_VIDEO_570_PHS2SET);
1013 break;
1014 case TPACPI_VIDEO_770:
1015 autosw = video_autosw_get();
1016 if (autosw < 0)
1017 return autosw;
1019 res = video_autosw_set(1);
1020 if (res)
1021 return res;
1022 res = acpi_evalf(vid_handle, NULL,
1023 "ASWT", "vdd", status * 0x100, 0);
1024 if (!autosw && video_autosw_set(autosw)) {
1025 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1026 return -EIO;
1028 break;
1029 case TPACPI_VIDEO_NEW:
1030 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1031 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1032 break;
1033 default:
1034 return -ENOSYS;
1037 return (res)? 0 : -EIO;
1040 static int video_autosw_get(void)
1042 int autosw = 0;
1044 switch (video_supported) {
1045 case TPACPI_VIDEO_570:
1046 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1047 return -EIO;
1048 break;
1049 case TPACPI_VIDEO_770:
1050 case TPACPI_VIDEO_NEW:
1051 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1052 return -EIO;
1053 break;
1054 default:
1055 return -ENOSYS;
1058 return autosw & 1;
1061 static int video_autosw_set(int enable)
1063 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1064 return -EIO;
1065 return 0;
1068 static int video_outputsw_cycle(void)
1070 int autosw = video_autosw_get();
1071 int res;
1073 if (autosw < 0)
1074 return autosw;
1076 switch (video_supported) {
1077 case TPACPI_VIDEO_570:
1078 res = video_autosw_set(1);
1079 if (res)
1080 return res;
1081 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1082 break;
1083 case TPACPI_VIDEO_770:
1084 case TPACPI_VIDEO_NEW:
1085 res = video_autosw_set(1);
1086 if (res)
1087 return res;
1088 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1089 break;
1090 default:
1091 return -ENOSYS;
1093 if (!autosw && video_autosw_set(autosw)) {
1094 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1095 return -EIO;
1098 return (res)? 0 : -EIO;
1101 static int video_expand_toggle(void)
1103 switch (video_supported) {
1104 case TPACPI_VIDEO_570:
1105 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1106 0 : -EIO;
1107 case TPACPI_VIDEO_770:
1108 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1109 0 : -EIO;
1110 case TPACPI_VIDEO_NEW:
1111 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1112 0 : -EIO;
1113 default:
1114 return -ENOSYS;
1116 /* not reached */
1119 static int video_read(char *p)
1121 int status, autosw;
1122 int len = 0;
1124 if (video_supported == TPACPI_VIDEO_NONE) {
1125 len += sprintf(p + len, "status:\t\tnot supported\n");
1126 return len;
1129 status = video_outputsw_get();
1130 if (status < 0)
1131 return status;
1133 autosw = video_autosw_get();
1134 if (autosw < 0)
1135 return autosw;
1137 len += sprintf(p + len, "status:\t\tsupported\n");
1138 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1139 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1140 if (video_supported == TPACPI_VIDEO_NEW)
1141 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1142 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1143 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1144 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1145 if (video_supported == TPACPI_VIDEO_NEW)
1146 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1147 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1148 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1150 return len;
1153 static int video_write(char *buf)
1155 char *cmd;
1156 int enable, disable, status;
1157 int res;
1159 if (video_supported == TPACPI_VIDEO_NONE)
1160 return -ENODEV;
1162 enable = 0;
1163 disable = 0;
1165 while ((cmd = next_cmd(&buf))) {
1166 if (strlencmp(cmd, "lcd_enable") == 0) {
1167 enable |= TP_ACPI_VIDEO_S_LCD;
1168 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1169 disable |= TP_ACPI_VIDEO_S_LCD;
1170 } else if (strlencmp(cmd, "crt_enable") == 0) {
1171 enable |= TP_ACPI_VIDEO_S_CRT;
1172 } else if (strlencmp(cmd, "crt_disable") == 0) {
1173 disable |= TP_ACPI_VIDEO_S_CRT;
1174 } else if (video_supported == TPACPI_VIDEO_NEW &&
1175 strlencmp(cmd, "dvi_enable") == 0) {
1176 enable |= TP_ACPI_VIDEO_S_DVI;
1177 } else if (video_supported == TPACPI_VIDEO_NEW &&
1178 strlencmp(cmd, "dvi_disable") == 0) {
1179 disable |= TP_ACPI_VIDEO_S_DVI;
1180 } else if (strlencmp(cmd, "auto_enable") == 0) {
1181 res = video_autosw_set(1);
1182 if (res)
1183 return res;
1184 } else if (strlencmp(cmd, "auto_disable") == 0) {
1185 res = video_autosw_set(0);
1186 if (res)
1187 return res;
1188 } else if (strlencmp(cmd, "video_switch") == 0) {
1189 res = video_outputsw_cycle();
1190 if (res)
1191 return res;
1192 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1193 res = video_expand_toggle();
1194 if (res)
1195 return res;
1196 } else
1197 return -EINVAL;
1200 if (enable || disable) {
1201 status = video_outputsw_get();
1202 if (status < 0)
1203 return status;
1204 res = video_outputsw_set((status & ~disable) | enable);
1205 if (res)
1206 return res;
1209 return 0;
1212 static struct ibm_struct video_driver_data = {
1213 .name = "video",
1214 .read = video_read,
1215 .write = video_write,
1216 .exit = video_exit,
1219 /*************************************************************************
1220 * Light (thinklight) subdriver
1223 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1224 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1226 static int __init light_init(struct ibm_init_struct *iibm)
1228 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1230 IBM_ACPIHANDLE_INIT(ledb);
1231 IBM_ACPIHANDLE_INIT(lght);
1232 IBM_ACPIHANDLE_INIT(cmos);
1234 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1235 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1237 if (tp_features.light)
1238 /* light status not supported on
1239 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1240 tp_features.light_status =
1241 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1243 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1244 str_supported(tp_features.light));
1246 return (tp_features.light)? 0 : 1;
1249 static int light_read(char *p)
1251 int len = 0;
1252 int status = 0;
1254 if (!tp_features.light) {
1255 len += sprintf(p + len, "status:\t\tnot supported\n");
1256 } else if (!tp_features.light_status) {
1257 len += sprintf(p + len, "status:\t\tunknown\n");
1258 len += sprintf(p + len, "commands:\ton, off\n");
1259 } else {
1260 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1261 return -EIO;
1262 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1263 len += sprintf(p + len, "commands:\ton, off\n");
1266 return len;
1269 static int light_write(char *buf)
1271 int cmos_cmd, lght_cmd;
1272 char *cmd;
1273 int success;
1275 if (!tp_features.light)
1276 return -ENODEV;
1278 while ((cmd = next_cmd(&buf))) {
1279 if (strlencmp(cmd, "on") == 0) {
1280 cmos_cmd = 0x0c;
1281 lght_cmd = 1;
1282 } else if (strlencmp(cmd, "off") == 0) {
1283 cmos_cmd = 0x0d;
1284 lght_cmd = 0;
1285 } else
1286 return -EINVAL;
1288 success = cmos_handle ?
1289 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1290 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1291 if (!success)
1292 return -EIO;
1295 return 0;
1298 static struct ibm_struct light_driver_data = {
1299 .name = "light",
1300 .read = light_read,
1301 .write = light_write,
1304 /*************************************************************************
1305 * Dock subdriver
1308 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1310 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1311 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1312 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1313 "\\_SB.PCI.ISA.SLCE", /* 570 */
1314 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1316 /* don't list other alternatives as we install a notify handler on the 570 */
1317 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1319 #define dock_docked() (_sta(dock_handle) & 1)
1321 static int __init dock_init(struct ibm_init_struct *iibm)
1323 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1325 IBM_ACPIHANDLE_INIT(dock);
1326 IBM_ACPIHANDLE_INIT(pci);
1328 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1329 str_supported(dock_handle != NULL));
1331 return (dock_handle)? 0 : 1;
1334 static void dock_notify(struct ibm_struct *ibm, u32 event)
1336 int docked = dock_docked();
1337 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
1339 if (event == 1 && !pci) /* 570 */
1340 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1341 else if (event == 1 && pci) /* 570 */
1342 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1343 else if (event == 3 && docked)
1344 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1345 else if (event == 3 && !docked)
1346 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
1347 else if (event == 0 && docked)
1348 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1349 else {
1350 printk(IBM_ERR "unknown dock event %d, status %d\n",
1351 event, _sta(dock_handle));
1352 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
1356 static int dock_read(char *p)
1358 int len = 0;
1359 int docked = dock_docked();
1361 if (!dock_handle)
1362 len += sprintf(p + len, "status:\t\tnot supported\n");
1363 else if (!docked)
1364 len += sprintf(p + len, "status:\t\tundocked\n");
1365 else {
1366 len += sprintf(p + len, "status:\t\tdocked\n");
1367 len += sprintf(p + len, "commands:\tdock, undock\n");
1370 return len;
1373 static int dock_write(char *buf)
1375 char *cmd;
1377 if (!dock_docked())
1378 return -ENODEV;
1380 while ((cmd = next_cmd(&buf))) {
1381 if (strlencmp(cmd, "undock") == 0) {
1382 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1383 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1384 return -EIO;
1385 } else if (strlencmp(cmd, "dock") == 0) {
1386 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1387 return -EIO;
1388 } else
1389 return -EINVAL;
1392 return 0;
1395 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1397 .notify = dock_notify,
1398 .handle = &dock_handle,
1399 .type = ACPI_SYSTEM_NOTIFY,
1402 .hid = IBM_PCI_HID,
1403 .notify = dock_notify,
1404 .handle = &pci_handle,
1405 .type = ACPI_SYSTEM_NOTIFY,
1409 static struct ibm_struct dock_driver_data[2] = {
1411 .name = "dock",
1412 .read = dock_read,
1413 .write = dock_write,
1414 .acpi = &ibm_dock_acpidriver[0],
1417 .name = "dock",
1418 .acpi = &ibm_dock_acpidriver[1],
1422 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1424 /*************************************************************************
1425 * Bay subdriver
1428 #ifdef CONFIG_THINKPAD_ACPI_BAY
1429 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1430 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1431 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1432 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1433 ); /* A21e, R30, R31 */
1434 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1435 "_EJ0", /* all others */
1436 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1437 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1438 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1439 ); /* all others */
1440 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1441 "_EJ0", /* 770x */
1442 ); /* all others */
1444 static int __init bay_init(struct ibm_init_struct *iibm)
1446 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1448 IBM_ACPIHANDLE_INIT(bay);
1449 if (bay_handle)
1450 IBM_ACPIHANDLE_INIT(bay_ej);
1451 IBM_ACPIHANDLE_INIT(bay2);
1452 if (bay2_handle)
1453 IBM_ACPIHANDLE_INIT(bay2_ej);
1455 tp_features.bay_status = bay_handle &&
1456 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1457 tp_features.bay_status2 = bay2_handle &&
1458 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1460 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1461 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1462 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1463 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1465 vdbg_printk(TPACPI_DBG_INIT,
1466 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1467 str_supported(tp_features.bay_status),
1468 str_supported(tp_features.bay_eject),
1469 str_supported(tp_features.bay_status2),
1470 str_supported(tp_features.bay_eject2));
1472 return (tp_features.bay_status || tp_features.bay_eject ||
1473 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
1476 static void bay_notify(struct ibm_struct *ibm, u32 event)
1478 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1481 #define bay_occupied(b) (_sta(b##_handle) & 1)
1483 static int bay_read(char *p)
1485 int len = 0;
1486 int occupied = bay_occupied(bay);
1487 int occupied2 = bay_occupied(bay2);
1488 int eject, eject2;
1490 len += sprintf(p + len, "status:\t\t%s\n",
1491 tp_features.bay_status ?
1492 (occupied ? "occupied" : "unoccupied") :
1493 "not supported");
1494 if (tp_features.bay_status2)
1495 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
1496 "occupied" : "unoccupied");
1498 eject = tp_features.bay_eject && occupied;
1499 eject2 = tp_features.bay_eject2 && occupied2;
1501 if (eject && eject2)
1502 len += sprintf(p + len, "commands:\teject, eject2\n");
1503 else if (eject)
1504 len += sprintf(p + len, "commands:\teject\n");
1505 else if (eject2)
1506 len += sprintf(p + len, "commands:\teject2\n");
1508 return len;
1511 static int bay_write(char *buf)
1513 char *cmd;
1515 if (!tp_features.bay_eject && !tp_features.bay_eject2)
1516 return -ENODEV;
1518 while ((cmd = next_cmd(&buf))) {
1519 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
1520 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
1521 return -EIO;
1522 } else if (tp_features.bay_eject2 &&
1523 strlencmp(cmd, "eject2") == 0) {
1524 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
1525 return -EIO;
1526 } else
1527 return -EINVAL;
1530 return 0;
1533 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
1534 .notify = bay_notify,
1535 .handle = &bay_handle,
1536 .type = ACPI_SYSTEM_NOTIFY,
1539 static struct ibm_struct bay_driver_data = {
1540 .name = "bay",
1541 .read = bay_read,
1542 .write = bay_write,
1543 .acpi = &ibm_bay_acpidriver,
1546 #endif /* CONFIG_THINKPAD_ACPI_BAY */
1548 /*************************************************************************
1549 * CMOS subdriver
1552 static int __init cmos_init(struct ibm_init_struct *iibm)
1554 vdbg_printk(TPACPI_DBG_INIT,
1555 "initializing cmos commands subdriver\n");
1557 IBM_ACPIHANDLE_INIT(cmos);
1559 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
1560 str_supported(cmos_handle != NULL));
1561 return (cmos_handle)? 0 : 1;
1564 static int cmos_read(char *p)
1566 int len = 0;
1568 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1569 R30, R31, T20-22, X20-21 */
1570 if (!cmos_handle)
1571 len += sprintf(p + len, "status:\t\tnot supported\n");
1572 else {
1573 len += sprintf(p + len, "status:\t\tsupported\n");
1574 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
1577 return len;
1580 static int cmos_write(char *buf)
1582 char *cmd;
1583 int cmos_cmd, res;
1585 while ((cmd = next_cmd(&buf))) {
1586 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
1587 cmos_cmd >= 0 && cmos_cmd <= 21) {
1588 /* cmos_cmd set */
1589 } else
1590 return -EINVAL;
1592 res = issue_thinkpad_cmos_command(cmos_cmd);
1593 if (res)
1594 return res;
1597 return 0;
1600 static struct ibm_struct cmos_driver_data = {
1601 .name = "cmos",
1602 .read = cmos_read,
1603 .write = cmos_write,
1606 /*************************************************************************
1607 * LED subdriver
1610 static enum led_access_mode led_supported;
1612 IBM_HANDLE(led, ec, "SLED", /* 570 */
1613 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1614 "LED", /* all others */
1615 ); /* R30, R31 */
1617 static int __init led_init(struct ibm_init_struct *iibm)
1619 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
1621 IBM_ACPIHANDLE_INIT(led);
1623 if (!led_handle)
1624 /* led not supported on R30, R31 */
1625 led_supported = TPACPI_LED_NONE;
1626 else if (strlencmp(led_path, "SLED") == 0)
1627 /* 570 */
1628 led_supported = TPACPI_LED_570;
1629 else if (strlencmp(led_path, "SYSL") == 0)
1630 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
1631 led_supported = TPACPI_LED_OLD;
1632 else
1633 /* all others */
1634 led_supported = TPACPI_LED_NEW;
1636 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
1637 str_supported(led_supported), led_supported);
1639 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
1642 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
1644 static int led_read(char *p)
1646 int len = 0;
1648 if (!led_supported) {
1649 len += sprintf(p + len, "status:\t\tnot supported\n");
1650 return len;
1652 len += sprintf(p + len, "status:\t\tsupported\n");
1654 if (led_supported == TPACPI_LED_570) {
1655 /* 570 */
1656 int i, status;
1657 for (i = 0; i < 8; i++) {
1658 if (!acpi_evalf(ec_handle,
1659 &status, "GLED", "dd", 1 << i))
1660 return -EIO;
1661 len += sprintf(p + len, "%d:\t\t%s\n",
1662 i, led_status(status));
1666 len += sprintf(p + len, "commands:\t"
1667 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
1669 return len;
1672 /* off, on, blink */
1673 static const int led_sled_arg1[] = { 0, 1, 3 };
1674 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
1675 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
1676 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
1678 static int led_write(char *buf)
1680 char *cmd;
1681 int led, ind, ret;
1683 if (!led_supported)
1684 return -ENODEV;
1686 while ((cmd = next_cmd(&buf))) {
1687 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
1688 return -EINVAL;
1690 if (strstr(cmd, "off")) {
1691 ind = 0;
1692 } else if (strstr(cmd, "on")) {
1693 ind = 1;
1694 } else if (strstr(cmd, "blink")) {
1695 ind = 2;
1696 } else
1697 return -EINVAL;
1699 if (led_supported == TPACPI_LED_570) {
1700 /* 570 */
1701 led = 1 << led;
1702 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1703 led, led_sled_arg1[ind]))
1704 return -EIO;
1705 } else if (led_supported == TPACPI_LED_OLD) {
1706 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
1707 led = 1 << led;
1708 ret = ec_write(TPACPI_LED_EC_HLMS, led);
1709 if (ret >= 0)
1710 ret =
1711 ec_write(TPACPI_LED_EC_HLBL,
1712 led * led_exp_hlbl[ind]);
1713 if (ret >= 0)
1714 ret =
1715 ec_write(TPACPI_LED_EC_HLCL,
1716 led * led_exp_hlcl[ind]);
1717 if (ret < 0)
1718 return ret;
1719 } else {
1720 /* all others */
1721 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
1722 led, led_led_arg1[ind]))
1723 return -EIO;
1727 return 0;
1730 static struct ibm_struct led_driver_data = {
1731 .name = "led",
1732 .read = led_read,
1733 .write = led_write,
1736 /*************************************************************************
1737 * Beep subdriver
1740 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
1742 static int __init beep_init(struct ibm_init_struct *iibm)
1744 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
1746 IBM_ACPIHANDLE_INIT(beep);
1748 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
1749 str_supported(beep_handle != NULL));
1751 return (beep_handle)? 0 : 1;
1754 static int beep_read(char *p)
1756 int len = 0;
1758 if (!beep_handle)
1759 len += sprintf(p + len, "status:\t\tnot supported\n");
1760 else {
1761 len += sprintf(p + len, "status:\t\tsupported\n");
1762 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
1765 return len;
1768 static int beep_write(char *buf)
1770 char *cmd;
1771 int beep_cmd;
1773 if (!beep_handle)
1774 return -ENODEV;
1776 while ((cmd = next_cmd(&buf))) {
1777 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
1778 beep_cmd >= 0 && beep_cmd <= 17) {
1779 /* beep_cmd set */
1780 } else
1781 return -EINVAL;
1782 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
1783 return -EIO;
1786 return 0;
1789 static struct ibm_struct beep_driver_data = {
1790 .name = "beep",
1791 .read = beep_read,
1792 .write = beep_write,
1795 /*************************************************************************
1796 * Thermal subdriver
1799 static enum thermal_access_mode thermal_read_mode;
1801 static int __init thermal_init(struct ibm_init_struct *iibm)
1803 u8 t, ta1, ta2;
1804 int i;
1805 int acpi_tmp7;
1807 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
1809 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
1811 if (ibm_thinkpad_ec_found && experimental) {
1813 * Direct EC access mode: sensors at registers
1814 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
1815 * non-implemented, thermal sensors return 0x80 when
1816 * not available
1819 ta1 = ta2 = 0;
1820 for (i = 0; i < 8; i++) {
1821 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
1822 ta1 |= t;
1823 } else {
1824 ta1 = 0;
1825 break;
1827 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
1828 ta2 |= t;
1829 } else {
1830 ta1 = 0;
1831 break;
1834 if (ta1 == 0) {
1835 /* This is sheer paranoia, but we handle it anyway */
1836 if (acpi_tmp7) {
1837 printk(IBM_ERR
1838 "ThinkPad ACPI EC access misbehaving, "
1839 "falling back to ACPI TMPx access mode\n");
1840 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
1841 } else {
1842 printk(IBM_ERR
1843 "ThinkPad ACPI EC access misbehaving, "
1844 "disabling thermal sensors access\n");
1845 thermal_read_mode = TPACPI_THERMAL_NONE;
1847 } else {
1848 thermal_read_mode =
1849 (ta2 != 0) ?
1850 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
1852 } else if (acpi_tmp7) {
1853 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
1854 /* 600e/x, 770e, 770x */
1855 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
1856 } else {
1857 /* Standard ACPI TMPx access, max 8 sensors */
1858 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
1860 } else {
1861 /* temperatures not supported on 570, G4x, R30, R31, R32 */
1862 thermal_read_mode = TPACPI_THERMAL_NONE;
1865 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
1866 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
1867 thermal_read_mode);
1869 return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
1872 /* idx is zero-based */
1873 static int thermal_get_sensor(int idx, s32 *value)
1875 int t;
1876 s8 tmp;
1877 char tmpi[5];
1879 t = TP_EC_THERMAL_TMP0;
1881 switch (thermal_read_mode) {
1882 #if TPACPI_MAX_THERMAL_SENSORS >= 16
1883 case TPACPI_THERMAL_TPEC_16:
1884 if (idx >= 8 && idx <= 15) {
1885 t = TP_EC_THERMAL_TMP8;
1886 idx -= 8;
1888 /* fallthrough */
1889 #endif
1890 case TPACPI_THERMAL_TPEC_8:
1891 if (idx <= 7) {
1892 if (!acpi_ec_read(t + idx, &tmp))
1893 return -EIO;
1894 *value = tmp * 1000;
1895 return 0;
1897 break;
1899 case TPACPI_THERMAL_ACPI_UPDT:
1900 if (idx <= 7) {
1901 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
1902 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
1903 return -EIO;
1904 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1905 return -EIO;
1906 *value = (t - 2732) * 100;
1907 return 0;
1909 break;
1911 case TPACPI_THERMAL_ACPI_TMP07:
1912 if (idx <= 7) {
1913 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
1914 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
1915 return -EIO;
1916 *value = t * 1000;
1917 return 0;
1919 break;
1921 case TPACPI_THERMAL_NONE:
1922 default:
1923 return -ENOSYS;
1926 return -EINVAL;
1929 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
1931 int res, i;
1932 int n;
1934 n = 8;
1935 i = 0;
1937 if (!s)
1938 return -EINVAL;
1940 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
1941 n = 16;
1943 for(i = 0 ; i < n; i++) {
1944 res = thermal_get_sensor(i, &s->temp[i]);
1945 if (res)
1946 return res;
1949 return n;
1952 static int thermal_read(char *p)
1954 int len = 0;
1955 int n, i;
1956 struct ibm_thermal_sensors_struct t;
1958 n = thermal_get_sensors(&t);
1959 if (unlikely(n < 0))
1960 return n;
1962 len += sprintf(p + len, "temperatures:\t");
1964 if (n > 0) {
1965 for (i = 0; i < (n - 1); i++)
1966 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
1967 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
1968 } else
1969 len += sprintf(p + len, "not supported\n");
1971 return len;
1974 static struct ibm_struct thermal_driver_data = {
1975 .name = "thermal",
1976 .read = thermal_read,
1979 /*************************************************************************
1980 * EC Dump subdriver
1983 static u8 ecdump_regs[256];
1985 static int ecdump_read(char *p)
1987 int len = 0;
1988 int i, j;
1989 u8 v;
1991 len += sprintf(p + len, "EC "
1992 " +00 +01 +02 +03 +04 +05 +06 +07"
1993 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
1994 for (i = 0; i < 256; i += 16) {
1995 len += sprintf(p + len, "EC 0x%02x:", i);
1996 for (j = 0; j < 16; j++) {
1997 if (!acpi_ec_read(i + j, &v))
1998 break;
1999 if (v != ecdump_regs[i + j])
2000 len += sprintf(p + len, " *%02x", v);
2001 else
2002 len += sprintf(p + len, " %02x", v);
2003 ecdump_regs[i + j] = v;
2005 len += sprintf(p + len, "\n");
2006 if (j != 16)
2007 break;
2010 /* These are way too dangerous to advertise openly... */
2011 #if 0
2012 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2013 " (<offset> is 00-ff, <value> is 00-ff)\n");
2014 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2015 " (<offset> is 00-ff, <value> is 0-255)\n");
2016 #endif
2017 return len;
2020 static int ecdump_write(char *buf)
2022 char *cmd;
2023 int i, v;
2025 while ((cmd = next_cmd(&buf))) {
2026 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2027 /* i and v set */
2028 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2029 /* i and v set */
2030 } else
2031 return -EINVAL;
2032 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2033 if (!acpi_ec_write(i, v))
2034 return -EIO;
2035 } else
2036 return -EINVAL;
2039 return 0;
2042 static struct ibm_struct ecdump_driver_data = {
2043 .name = "ecdump",
2044 .read = ecdump_read,
2045 .write = ecdump_write,
2046 .flags.experimental = 1,
2049 /*************************************************************************
2050 * Backlight/brightness subdriver
2053 static struct backlight_device *ibm_backlight_device = NULL;
2055 static struct backlight_properties ibm_backlight_data = {
2056 .owner = THIS_MODULE,
2057 .get_brightness = brightness_get,
2058 .update_status = brightness_update_status,
2059 .max_brightness = 7,
2062 static int __init brightness_init(struct ibm_init_struct *iibm)
2064 int b;
2066 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2068 b = brightness_get(NULL);
2069 if (b < 0)
2070 return b;
2072 ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
2073 &ibm_backlight_data);
2074 if (IS_ERR(ibm_backlight_device)) {
2075 printk(IBM_ERR "Could not register backlight device\n");
2076 return PTR_ERR(ibm_backlight_device);
2078 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2080 ibm_backlight_device->props->brightness = b;
2081 brightness_update_status(ibm_backlight_device);
2083 return 0;
2086 static void brightness_exit(void)
2088 if (ibm_backlight_device) {
2089 vdbg_printk(TPACPI_DBG_EXIT,
2090 "calling backlight_device_unregister()\n");
2091 backlight_device_unregister(ibm_backlight_device);
2092 ibm_backlight_device = NULL;
2096 static int brightness_update_status(struct backlight_device *bd)
2098 return brightness_set(
2099 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
2100 bd->props->power == FB_BLANK_UNBLANK) ?
2101 bd->props->brightness : 0);
2104 static int brightness_get(struct backlight_device *bd)
2106 u8 level;
2107 if (!acpi_ec_read(brightness_offset, &level))
2108 return -EIO;
2110 level &= 0x7;
2112 return level;
2115 static int brightness_set(int value)
2117 int cmos_cmd, inc, i;
2118 int current_value = brightness_get(NULL);
2120 value &= 7;
2122 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2123 inc = value > current_value ? 1 : -1;
2124 for (i = current_value; i != value; i += inc) {
2125 if (issue_thinkpad_cmos_command(cmos_cmd))
2126 return -EIO;
2127 if (!acpi_ec_write(brightness_offset, i + inc))
2128 return -EIO;
2131 return 0;
2134 static int brightness_read(char *p)
2136 int len = 0;
2137 int level;
2139 if ((level = brightness_get(NULL)) < 0) {
2140 len += sprintf(p + len, "level:\t\tunreadable\n");
2141 } else {
2142 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2143 len += sprintf(p + len, "commands:\tup, down\n");
2144 len += sprintf(p + len, "commands:\tlevel <level>"
2145 " (<level> is 0-7)\n");
2148 return len;
2151 static int brightness_write(char *buf)
2153 int level;
2154 int new_level;
2155 char *cmd;
2157 while ((cmd = next_cmd(&buf))) {
2158 if ((level = brightness_get(NULL)) < 0)
2159 return level;
2160 level &= 7;
2162 if (strlencmp(cmd, "up") == 0) {
2163 new_level = level == 7 ? 7 : level + 1;
2164 } else if (strlencmp(cmd, "down") == 0) {
2165 new_level = level == 0 ? 0 : level - 1;
2166 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2167 new_level >= 0 && new_level <= 7) {
2168 /* new_level set */
2169 } else
2170 return -EINVAL;
2172 brightness_set(new_level);
2175 return 0;
2178 static struct ibm_struct brightness_driver_data = {
2179 .name = "brightness",
2180 .read = brightness_read,
2181 .write = brightness_write,
2182 .exit = brightness_exit,
2185 /*************************************************************************
2186 * Volume subdriver
2189 static int volume_read(char *p)
2191 int len = 0;
2192 u8 level;
2194 if (!acpi_ec_read(volume_offset, &level)) {
2195 len += sprintf(p + len, "level:\t\tunreadable\n");
2196 } else {
2197 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2198 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2199 len += sprintf(p + len, "commands:\tup, down, mute\n");
2200 len += sprintf(p + len, "commands:\tlevel <level>"
2201 " (<level> is 0-15)\n");
2204 return len;
2207 static int volume_write(char *buf)
2209 int cmos_cmd, inc, i;
2210 u8 level, mute;
2211 int new_level, new_mute;
2212 char *cmd;
2214 while ((cmd = next_cmd(&buf))) {
2215 if (!acpi_ec_read(volume_offset, &level))
2216 return -EIO;
2217 new_mute = mute = level & 0x40;
2218 new_level = level = level & 0xf;
2220 if (strlencmp(cmd, "up") == 0) {
2221 if (mute)
2222 new_mute = 0;
2223 else
2224 new_level = level == 15 ? 15 : level + 1;
2225 } else if (strlencmp(cmd, "down") == 0) {
2226 if (mute)
2227 new_mute = 0;
2228 else
2229 new_level = level == 0 ? 0 : level - 1;
2230 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2231 new_level >= 0 && new_level <= 15) {
2232 /* new_level set */
2233 } else if (strlencmp(cmd, "mute") == 0) {
2234 new_mute = 0x40;
2235 } else
2236 return -EINVAL;
2238 if (new_level != level) { /* mute doesn't change */
2239 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
2240 inc = new_level > level ? 1 : -1;
2242 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
2243 !acpi_ec_write(volume_offset, level)))
2244 return -EIO;
2246 for (i = level; i != new_level; i += inc)
2247 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2248 !acpi_ec_write(volume_offset, i + inc))
2249 return -EIO;
2251 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
2252 !acpi_ec_write(volume_offset,
2253 new_level + mute)))
2254 return -EIO;
2257 if (new_mute != mute) { /* level doesn't change */
2258 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
2260 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2261 !acpi_ec_write(volume_offset, level + new_mute))
2262 return -EIO;
2266 return 0;
2269 static struct ibm_struct volume_driver_data = {
2270 .name = "volume",
2271 .read = volume_read,
2272 .write = volume_write,
2275 /*************************************************************************
2276 * Fan subdriver
2280 * FAN ACCESS MODES
2282 * TPACPI_FAN_RD_ACPI_GFAN:
2283 * ACPI GFAN method: returns fan level
2285 * see TPACPI_FAN_WR_ACPI_SFAN
2286 * EC 0x2f (HFSP) not available if GFAN exists
2288 * TPACPI_FAN_WR_ACPI_SFAN:
2289 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2291 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2292 * it for writing.
2294 * TPACPI_FAN_WR_TPEC:
2295 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2296 * Supported on almost all ThinkPads
2298 * Fan speed changes of any sort (including those caused by the
2299 * disengaged mode) are usually done slowly by the firmware as the
2300 * maximum ammount of fan duty cycle change per second seems to be
2301 * limited.
2303 * Reading is not available if GFAN exists.
2304 * Writing is not available if SFAN exists.
2306 * Bits
2307 * 7 automatic mode engaged;
2308 * (default operation mode of the ThinkPad)
2309 * fan level is ignored in this mode.
2310 * 6 full speed mode (takes precedence over bit 7);
2311 * not available on all thinkpads. May disable
2312 * the tachometer while the fan controller ramps up
2313 * the speed (which can take up to a few *minutes*).
2314 * Speeds up fan to 100% duty-cycle, which is far above
2315 * the standard RPM levels. It is not impossible that
2316 * it could cause hardware damage.
2317 * 5-3 unused in some models. Extra bits for fan level
2318 * in others, but still useless as all values above
2319 * 7 map to the same speed as level 7 in these models.
2320 * 2-0 fan level (0..7 usually)
2321 * 0x00 = stop
2322 * 0x07 = max (set when temperatures critical)
2323 * Some ThinkPads may have other levels, see
2324 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
2326 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
2327 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
2328 * does so, its initial value is meaningless (0x07).
2330 * For firmware bugs, refer to:
2331 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2333 * ----
2335 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
2336 * Main fan tachometer reading (in RPM)
2338 * This register is present on all ThinkPads with a new-style EC, and
2339 * it is known not to be present on the A21m/e, and T22, as there is
2340 * something else in offset 0x84 according to the ACPI DSDT. Other
2341 * ThinkPads from this same time period (and earlier) probably lack the
2342 * tachometer as well.
2344 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
2345 * was never fixed by IBM to report the EC firmware version string
2346 * probably support the tachometer (like the early X models), so
2347 * detecting it is quite hard. We need more data to know for sure.
2349 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
2350 * might result.
2352 * FIRMWARE BUG: may go stale while the EC is switching to full speed
2353 * mode.
2355 * For firmware bugs, refer to:
2356 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
2358 * TPACPI_FAN_WR_ACPI_FANS:
2359 * ThinkPad X31, X40, X41. Not available in the X60.
2361 * FANS ACPI handle: takes three arguments: low speed, medium speed,
2362 * high speed. ACPI DSDT seems to map these three speeds to levels
2363 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
2364 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
2366 * The speeds are stored on handles
2367 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
2369 * There are three default speed sets, acessible as handles:
2370 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
2372 * ACPI DSDT switches which set is in use depending on various
2373 * factors.
2375 * TPACPI_FAN_WR_TPEC is also available and should be used to
2376 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
2377 * but the ACPI tables just mention level 7.
2380 static enum fan_status_access_mode fan_status_access_mode;
2381 static enum fan_control_access_mode fan_control_access_mode;
2382 static enum fan_control_commands fan_control_commands;
2384 static u8 fan_control_initial_status;
2386 static void fan_watchdog_fire(struct work_struct *ignored);
2387 static int fan_watchdog_maxinterval;
2388 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
2390 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
2391 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
2392 "\\FSPD", /* 600e/x, 770e, 770x */
2393 ); /* all others */
2394 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
2395 "JFNS", /* 770x-JL */
2396 ); /* all others */
2398 static int __init fan_init(struct ibm_init_struct *iibm)
2400 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
2402 fan_status_access_mode = TPACPI_FAN_NONE;
2403 fan_control_access_mode = TPACPI_FAN_WR_NONE;
2404 fan_control_commands = 0;
2405 fan_watchdog_maxinterval = 0;
2406 tp_features.fan_ctrl_status_undef = 0;
2408 IBM_ACPIHANDLE_INIT(fans);
2409 IBM_ACPIHANDLE_INIT(gfan);
2410 IBM_ACPIHANDLE_INIT(sfan);
2412 if (gfan_handle) {
2413 /* 570, 600e/x, 770e, 770x */
2414 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
2415 } else {
2416 /* all other ThinkPads: note that even old-style
2417 * ThinkPad ECs supports the fan control register */
2418 if (likely(acpi_ec_read(fan_status_offset,
2419 &fan_control_initial_status))) {
2420 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
2422 /* In some ThinkPads, neither the EC nor the ACPI
2423 * DSDT initialize the fan status, and it ends up
2424 * being set to 0x07 when it *could* be either
2425 * 0x07 or 0x80.
2427 * Enable for TP-1Y (T43), TP-78 (R51e),
2428 * TP-76 (R52), TP-70 (T43, R52), which are known
2429 * to be buggy. */
2430 if (fan_control_initial_status == 0x07 &&
2431 ibm_thinkpad_ec_found &&
2432 ((ibm_thinkpad_ec_found[0] == '1' &&
2433 ibm_thinkpad_ec_found[1] == 'Y') ||
2434 (ibm_thinkpad_ec_found[0] == '7' &&
2435 (ibm_thinkpad_ec_found[1] == '6' ||
2436 ibm_thinkpad_ec_found[1] == '8' ||
2437 ibm_thinkpad_ec_found[1] == '0'))
2438 )) {
2439 printk(IBM_NOTICE
2440 "fan_init: initial fan status is "
2441 "unknown, assuming it is in auto "
2442 "mode\n");
2443 tp_features.fan_ctrl_status_undef = 1;
2445 } else {
2446 printk(IBM_ERR
2447 "ThinkPad ACPI EC access misbehaving, "
2448 "fan status and control unavailable\n");
2449 return 1;
2453 if (sfan_handle) {
2454 /* 570, 770x-JL */
2455 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
2456 fan_control_commands |=
2457 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
2458 } else {
2459 if (!gfan_handle) {
2460 /* gfan without sfan means no fan control */
2461 /* all other models implement TP EC 0x2f control */
2463 if (fans_handle) {
2464 /* X31, X40, X41 */
2465 fan_control_access_mode =
2466 TPACPI_FAN_WR_ACPI_FANS;
2467 fan_control_commands |=
2468 TPACPI_FAN_CMD_SPEED |
2469 TPACPI_FAN_CMD_LEVEL |
2470 TPACPI_FAN_CMD_ENABLE;
2471 } else {
2472 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
2473 fan_control_commands |=
2474 TPACPI_FAN_CMD_LEVEL |
2475 TPACPI_FAN_CMD_ENABLE;
2480 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
2481 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
2482 fan_control_access_mode != TPACPI_FAN_WR_NONE),
2483 fan_status_access_mode, fan_control_access_mode);
2485 return (fan_status_access_mode != TPACPI_FAN_NONE ||
2486 fan_control_access_mode != TPACPI_FAN_WR_NONE)?
2487 0 : 1;
2490 static int fan_get_status(u8 *status)
2492 u8 s;
2494 /* TODO:
2495 * Add TPACPI_FAN_RD_ACPI_FANS ? */
2497 switch (fan_status_access_mode) {
2498 case TPACPI_FAN_RD_ACPI_GFAN:
2499 /* 570, 600e/x, 770e, 770x */
2501 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
2502 return -EIO;
2504 if (likely(status))
2505 *status = s & 0x07;
2507 break;
2509 case TPACPI_FAN_RD_TPEC:
2510 /* all except 570, 600e/x, 770e, 770x */
2511 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
2512 return -EIO;
2514 if (likely(status))
2515 *status = s;
2517 break;
2519 default:
2520 return -ENXIO;
2523 return 0;
2526 static void fan_exit(void)
2528 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
2529 cancel_delayed_work(&fan_watchdog_task);
2530 flush_scheduled_work();
2533 static int fan_get_speed(unsigned int *speed)
2535 u8 hi, lo;
2537 switch (fan_status_access_mode) {
2538 case TPACPI_FAN_RD_TPEC:
2539 /* all except 570, 600e/x, 770e, 770x */
2540 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
2541 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
2542 return -EIO;
2544 if (likely(speed))
2545 *speed = (hi << 8) | lo;
2547 break;
2549 default:
2550 return -ENXIO;
2553 return 0;
2556 static void fan_watchdog_fire(struct work_struct *ignored)
2558 int rc;
2560 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
2561 rc = fan_set_enable();
2562 if (rc < 0) {
2563 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
2564 "will try again later...\n", -rc);
2565 /* reschedule for later */
2566 fan_watchdog_reset();
2570 static void fan_watchdog_reset(void)
2572 static int fan_watchdog_active = 0;
2574 if (fan_watchdog_active)
2575 cancel_delayed_work(&fan_watchdog_task);
2577 if (fan_watchdog_maxinterval > 0) {
2578 fan_watchdog_active = 1;
2579 if (!schedule_delayed_work(&fan_watchdog_task,
2580 msecs_to_jiffies(fan_watchdog_maxinterval
2581 * 1000))) {
2582 printk(IBM_ERR "failed to schedule the fan watchdog, "
2583 "watchdog will not trigger\n");
2585 } else
2586 fan_watchdog_active = 0;
2589 static int fan_set_level(int level)
2591 switch (fan_control_access_mode) {
2592 case TPACPI_FAN_WR_ACPI_SFAN:
2593 if (level >= 0 && level <= 7) {
2594 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
2595 return -EIO;
2596 } else
2597 return -EINVAL;
2598 break;
2600 case TPACPI_FAN_WR_ACPI_FANS:
2601 case TPACPI_FAN_WR_TPEC:
2602 if ((level != TP_EC_FAN_AUTO) &&
2603 (level != TP_EC_FAN_FULLSPEED) &&
2604 ((level < 0) || (level > 7)))
2605 return -EINVAL;
2607 if (!acpi_ec_write(fan_status_offset, level))
2608 return -EIO;
2609 else
2610 tp_features.fan_ctrl_status_undef = 0;
2611 break;
2613 default:
2614 return -ENXIO;
2616 return 0;
2619 static int fan_set_enable(void)
2621 u8 s;
2622 int rc;
2624 switch (fan_control_access_mode) {
2625 case TPACPI_FAN_WR_ACPI_FANS:
2626 case TPACPI_FAN_WR_TPEC:
2627 if ((rc = fan_get_status(&s)) < 0)
2628 return rc;
2630 /* Don't go out of emergency fan mode */
2631 if (s != 7)
2632 s = TP_EC_FAN_AUTO;
2634 if (!acpi_ec_write(fan_status_offset, s))
2635 return -EIO;
2636 else
2637 tp_features.fan_ctrl_status_undef = 0;
2638 break;
2640 case TPACPI_FAN_WR_ACPI_SFAN:
2641 if ((rc = fan_get_status(&s)) < 0)
2642 return rc;
2644 s &= 0x07;
2646 /* Set fan to at least level 4 */
2647 if (s < 4)
2648 s = 4;
2650 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
2651 return -EIO;
2652 break;
2654 default:
2655 return -ENXIO;
2657 return 0;
2660 static int fan_set_disable(void)
2662 switch (fan_control_access_mode) {
2663 case TPACPI_FAN_WR_ACPI_FANS:
2664 case TPACPI_FAN_WR_TPEC:
2665 if (!acpi_ec_write(fan_status_offset, 0x00))
2666 return -EIO;
2667 else
2668 tp_features.fan_ctrl_status_undef = 0;
2669 break;
2671 case TPACPI_FAN_WR_ACPI_SFAN:
2672 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
2673 return -EIO;
2674 break;
2676 default:
2677 return -ENXIO;
2679 return 0;
2682 static int fan_set_speed(int speed)
2684 switch (fan_control_access_mode) {
2685 case TPACPI_FAN_WR_ACPI_FANS:
2686 if (speed >= 0 && speed <= 65535) {
2687 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
2688 speed, speed, speed))
2689 return -EIO;
2690 } else
2691 return -EINVAL;
2692 break;
2694 default:
2695 return -ENXIO;
2697 return 0;
2700 static int fan_read(char *p)
2702 int len = 0;
2703 int rc;
2704 u8 status;
2705 unsigned int speed = 0;
2707 switch (fan_status_access_mode) {
2708 case TPACPI_FAN_RD_ACPI_GFAN:
2709 /* 570, 600e/x, 770e, 770x */
2710 if ((rc = fan_get_status(&status)) < 0)
2711 return rc;
2713 len += sprintf(p + len, "status:\t\t%s\n"
2714 "level:\t\t%d\n",
2715 (status != 0) ? "enabled" : "disabled", status);
2716 break;
2718 case TPACPI_FAN_RD_TPEC:
2719 /* all except 570, 600e/x, 770e, 770x */
2720 if ((rc = fan_get_status(&status)) < 0)
2721 return rc;
2723 if (unlikely(tp_features.fan_ctrl_status_undef)) {
2724 if (status != fan_control_initial_status)
2725 tp_features.fan_ctrl_status_undef = 0;
2726 else
2727 /* Return most likely status. In fact, it
2728 * might be the only possible status */
2729 status = TP_EC_FAN_AUTO;
2732 len += sprintf(p + len, "status:\t\t%s\n",
2733 (status != 0) ? "enabled" : "disabled");
2735 if ((rc = fan_get_speed(&speed)) < 0)
2736 return rc;
2738 len += sprintf(p + len, "speed:\t\t%d\n", speed);
2740 if (status & TP_EC_FAN_FULLSPEED)
2741 /* Disengaged mode takes precedence */
2742 len += sprintf(p + len, "level:\t\tdisengaged\n");
2743 else if (status & TP_EC_FAN_AUTO)
2744 len += sprintf(p + len, "level:\t\tauto\n");
2745 else
2746 len += sprintf(p + len, "level:\t\t%d\n", status);
2747 break;
2749 case TPACPI_FAN_NONE:
2750 default:
2751 len += sprintf(p + len, "status:\t\tnot supported\n");
2754 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
2755 len += sprintf(p + len, "commands:\tlevel <level>");
2757 switch (fan_control_access_mode) {
2758 case TPACPI_FAN_WR_ACPI_SFAN:
2759 len += sprintf(p + len, " (<level> is 0-7)\n");
2760 break;
2762 default:
2763 len += sprintf(p + len, " (<level> is 0-7, "
2764 "auto, disengaged)\n");
2765 break;
2769 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
2770 len += sprintf(p + len, "commands:\tenable, disable\n"
2771 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
2772 "1-120 (seconds))\n");
2774 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
2775 len += sprintf(p + len, "commands:\tspeed <speed>"
2776 " (<speed> is 0-65535)\n");
2778 return len;
2781 static int fan_write_cmd_level(const char *cmd, int *rc)
2783 int level;
2785 if (strlencmp(cmd, "level auto") == 0)
2786 level = TP_EC_FAN_AUTO;
2787 else if (strlencmp(cmd, "level disengaged") == 0)
2788 level = TP_EC_FAN_FULLSPEED;
2789 else if (sscanf(cmd, "level %d", &level) != 1)
2790 return 0;
2792 if ((*rc = fan_set_level(level)) == -ENXIO)
2793 printk(IBM_ERR "level command accepted for unsupported "
2794 "access mode %d", fan_control_access_mode);
2796 return 1;
2799 static int fan_write_cmd_enable(const char *cmd, int *rc)
2801 if (strlencmp(cmd, "enable") != 0)
2802 return 0;
2804 if ((*rc = fan_set_enable()) == -ENXIO)
2805 printk(IBM_ERR "enable command accepted for unsupported "
2806 "access mode %d", fan_control_access_mode);
2808 return 1;
2811 static int fan_write_cmd_disable(const char *cmd, int *rc)
2813 if (strlencmp(cmd, "disable") != 0)
2814 return 0;
2816 if ((*rc = fan_set_disable()) == -ENXIO)
2817 printk(IBM_ERR "disable command accepted for unsupported "
2818 "access mode %d", fan_control_access_mode);
2820 return 1;
2823 static int fan_write_cmd_speed(const char *cmd, int *rc)
2825 int speed;
2827 /* TODO:
2828 * Support speed <low> <medium> <high> ? */
2830 if (sscanf(cmd, "speed %d", &speed) != 1)
2831 return 0;
2833 if ((*rc = fan_set_speed(speed)) == -ENXIO)
2834 printk(IBM_ERR "speed command accepted for unsupported "
2835 "access mode %d", fan_control_access_mode);
2837 return 1;
2840 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
2842 int interval;
2844 if (sscanf(cmd, "watchdog %d", &interval) != 1)
2845 return 0;
2847 if (interval < 0 || interval > 120)
2848 *rc = -EINVAL;
2849 else
2850 fan_watchdog_maxinterval = interval;
2852 return 1;
2855 static int fan_write(char *buf)
2857 char *cmd;
2858 int rc = 0;
2860 while (!rc && (cmd = next_cmd(&buf))) {
2861 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
2862 fan_write_cmd_level(cmd, &rc)) &&
2863 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
2864 (fan_write_cmd_enable(cmd, &rc) ||
2865 fan_write_cmd_disable(cmd, &rc) ||
2866 fan_write_cmd_watchdog(cmd, &rc))) &&
2867 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
2868 fan_write_cmd_speed(cmd, &rc))
2870 rc = -EINVAL;
2871 else if (!rc)
2872 fan_watchdog_reset();
2875 return rc;
2878 static struct ibm_struct fan_driver_data = {
2879 .name = "fan",
2880 .read = fan_read,
2881 .write = fan_write,
2882 .exit = fan_exit,
2883 .flags.experimental = 1,
2886 /****************************************************************************
2887 ****************************************************************************
2889 * Infrastructure
2891 ****************************************************************************
2892 ****************************************************************************/
2894 /* /proc support */
2895 static struct proc_dir_entry *proc_dir = NULL;
2897 /* Subdriver registry */
2898 static LIST_HEAD(tpacpi_all_drivers);
2902 * Module and infrastructure proble, init and exit handling
2905 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
2906 static const char * __init str_supported(int is_supported)
2908 static char text_unsupported[] __initdata = "not supported";
2910 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
2912 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
2914 static int __init ibm_init(struct ibm_init_struct *iibm)
2916 int ret;
2917 struct ibm_struct *ibm = iibm->data;
2918 struct proc_dir_entry *entry;
2920 BUG_ON(ibm == NULL);
2922 INIT_LIST_HEAD(&ibm->all_drivers);
2924 if (ibm->flags.experimental && !experimental)
2925 return 0;
2927 dbg_printk(TPACPI_DBG_INIT,
2928 "probing for %s\n", ibm->name);
2930 if (iibm->init) {
2931 ret = iibm->init(iibm);
2932 if (ret > 0)
2933 return 0; /* probe failed */
2934 if (ret)
2935 return ret;
2937 ibm->flags.init_called = 1;
2940 if (ibm->acpi) {
2941 if (ibm->acpi->hid) {
2942 ret = register_tpacpi_subdriver(ibm);
2943 if (ret)
2944 goto err_out;
2947 if (ibm->acpi->notify) {
2948 ret = setup_acpi_notify(ibm);
2949 if (ret == -ENODEV) {
2950 printk(IBM_NOTICE "disabling subdriver %s\n",
2951 ibm->name);
2952 ret = 0;
2953 goto err_out;
2955 if (ret < 0)
2956 goto err_out;
2960 dbg_printk(TPACPI_DBG_INIT,
2961 "%s installed\n", ibm->name);
2963 if (ibm->read) {
2964 entry = create_proc_entry(ibm->name,
2965 S_IFREG | S_IRUGO | S_IWUSR,
2966 proc_dir);
2967 if (!entry) {
2968 printk(IBM_ERR "unable to create proc entry %s\n",
2969 ibm->name);
2970 ret = -ENODEV;
2971 goto err_out;
2973 entry->owner = THIS_MODULE;
2974 entry->data = ibm;
2975 entry->read_proc = &dispatch_procfs_read;
2976 if (ibm->write)
2977 entry->write_proc = &dispatch_procfs_write;
2978 ibm->flags.proc_created = 1;
2981 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
2983 return 0;
2985 err_out:
2986 dbg_printk(TPACPI_DBG_INIT,
2987 "%s: at error exit path with result %d\n",
2988 ibm->name, ret);
2990 ibm_exit(ibm);
2991 return (ret < 0)? ret : 0;
2994 static void ibm_exit(struct ibm_struct *ibm)
2996 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
2998 list_del_init(&ibm->all_drivers);
3000 if (ibm->flags.acpi_notify_installed) {
3001 dbg_printk(TPACPI_DBG_EXIT,
3002 "%s: acpi_remove_notify_handler\n", ibm->name);
3003 BUG_ON(!ibm->acpi);
3004 acpi_remove_notify_handler(*ibm->acpi->handle,
3005 ibm->acpi->type,
3006 dispatch_acpi_notify);
3007 ibm->flags.acpi_notify_installed = 0;
3008 ibm->flags.acpi_notify_installed = 0;
3011 if (ibm->flags.proc_created) {
3012 dbg_printk(TPACPI_DBG_EXIT,
3013 "%s: remove_proc_entry\n", ibm->name);
3014 remove_proc_entry(ibm->name, proc_dir);
3015 ibm->flags.proc_created = 0;
3018 if (ibm->flags.acpi_driver_registered) {
3019 dbg_printk(TPACPI_DBG_EXIT,
3020 "%s: acpi_bus_unregister_driver\n", ibm->name);
3021 BUG_ON(!ibm->acpi);
3022 acpi_bus_unregister_driver(ibm->acpi->driver);
3023 kfree(ibm->acpi->driver);
3024 ibm->acpi->driver = NULL;
3025 ibm->flags.acpi_driver_registered = 0;
3028 if (ibm->flags.init_called && ibm->exit) {
3029 ibm->exit();
3030 ibm->flags.init_called = 0;
3033 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
3036 /* Probing */
3038 static char *ibm_thinkpad_ec_found = NULL;
3040 static char* __init check_dmi_for_ec(void)
3042 struct dmi_device *dev = NULL;
3043 char ec_fw_string[18];
3046 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
3047 * X32 or newer, all Z series; Some models must have an
3048 * up-to-date BIOS or they will not be detected.
3050 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
3052 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
3053 if (sscanf(dev->name,
3054 "IBM ThinkPad Embedded Controller -[%17c",
3055 ec_fw_string) == 1) {
3056 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
3057 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
3058 return kstrdup(ec_fw_string, GFP_KERNEL);
3061 return NULL;
3064 static int __init probe_for_thinkpad(void)
3066 int is_thinkpad;
3068 if (acpi_disabled)
3069 return -ENODEV;
3072 * Non-ancient models have better DMI tagging, but very old models
3073 * don't.
3075 is_thinkpad = dmi_name_in_vendors("ThinkPad");
3077 /* ec is required because many other handles are relative to it */
3078 IBM_ACPIHANDLE_INIT(ec);
3079 if (!ec_handle) {
3080 if (is_thinkpad)
3081 printk(IBM_ERR
3082 "Not yet supported ThinkPad detected!\n");
3083 return -ENODEV;
3087 * Risks a regression on very old machines, but reduces potential
3088 * false positives a damn great deal
3090 if (!is_thinkpad)
3091 is_thinkpad = dmi_name_in_vendors("IBM");
3093 if (!is_thinkpad && !force_load)
3094 return -ENODEV;
3096 return 0;
3100 /* Module init, exit, parameters */
3102 static struct ibm_init_struct ibms_init[] __initdata = {
3104 .init = thinkpad_acpi_driver_init,
3105 .data = &thinkpad_acpi_driver_data,
3108 .init = hotkey_init,
3109 .data = &hotkey_driver_data,
3112 .init = bluetooth_init,
3113 .data = &bluetooth_driver_data,
3116 .init = wan_init,
3117 .data = &wan_driver_data,
3120 .init = video_init,
3121 .data = &video_driver_data,
3124 .init = light_init,
3125 .data = &light_driver_data,
3127 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3129 .init = dock_init,
3130 .data = &dock_driver_data[0],
3133 .data = &dock_driver_data[1],
3135 #endif
3136 #ifdef CONFIG_THINKPAD_ACPI_BAY
3138 .init = bay_init,
3139 .data = &bay_driver_data,
3141 #endif
3143 .init = cmos_init,
3144 .data = &cmos_driver_data,
3147 .init = led_init,
3148 .data = &led_driver_data,
3151 .init = beep_init,
3152 .data = &beep_driver_data,
3155 .init = thermal_init,
3156 .data = &thermal_driver_data,
3159 .data = &ecdump_driver_data,
3162 .init = brightness_init,
3163 .data = &brightness_driver_data,
3166 .data = &volume_driver_data,
3169 .init = fan_init,
3170 .data = &fan_driver_data,
3174 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
3176 unsigned int i;
3177 struct ibm_struct *ibm;
3179 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3180 ibm = ibms_init[i].data;
3181 BUG_ON(ibm == NULL);
3183 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
3184 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
3185 return -ENOSPC;
3186 strcpy(ibms_init[i].param, val);
3187 strcat(ibms_init[i].param, ",");
3188 return 0;
3192 return -EINVAL;
3195 static int experimental;
3196 module_param(experimental, int, 0);
3198 static u32 dbg_level;
3199 module_param_named(debug, dbg_level, uint, 0);
3201 static int force_load;
3202 module_param(force_load, int, 0);
3204 #define IBM_PARAM(feature) \
3205 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
3207 IBM_PARAM(hotkey);
3208 IBM_PARAM(bluetooth);
3209 IBM_PARAM(video);
3210 IBM_PARAM(light);
3211 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3212 IBM_PARAM(dock);
3213 #endif
3214 #ifdef CONFIG_THINKPAD_ACPI_BAY
3215 IBM_PARAM(bay);
3216 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3217 IBM_PARAM(cmos);
3218 IBM_PARAM(led);
3219 IBM_PARAM(beep);
3220 IBM_PARAM(ecdump);
3221 IBM_PARAM(brightness);
3222 IBM_PARAM(volume);
3223 IBM_PARAM(fan);
3225 static int __init thinkpad_acpi_module_init(void)
3227 int ret, i;
3229 ret = probe_for_thinkpad();
3230 if (ret)
3231 return ret;
3233 ibm_thinkpad_ec_found = check_dmi_for_ec();
3234 IBM_ACPIHANDLE_INIT(ecrd);
3235 IBM_ACPIHANDLE_INIT(ecwr);
3237 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
3238 if (!proc_dir) {
3239 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
3240 thinkpad_acpi_module_exit();
3241 return -ENODEV;
3243 proc_dir->owner = THIS_MODULE;
3245 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
3246 ret = ibm_init(&ibms_init[i]);
3247 if (ret >= 0 && *ibms_init[i].param)
3248 ret = ibms_init[i].data->write(ibms_init[i].param);
3249 if (ret < 0) {
3250 thinkpad_acpi_module_exit();
3251 return ret;
3255 return 0;
3258 static void thinkpad_acpi_module_exit(void)
3260 struct ibm_struct *ibm, *itmp;
3262 list_for_each_entry_safe_reverse(ibm, itmp,
3263 &tpacpi_all_drivers,
3264 all_drivers) {
3265 ibm_exit(ibm);
3268 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
3270 if (proc_dir)
3271 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
3273 kfree(ibm_thinkpad_ec_found);
3276 module_init(thinkpad_acpi_module_init);
3277 module_exit(thinkpad_acpi_module_exit);