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
24 #define IBM_VERSION "0.14"
25 #define TPACPI_SYSFS_VERSION 0x000200
29 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
36 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
45 * 2005-01-16 0.10 fix module loading on R30, R31
46 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
63 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
69 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
85 #include "thinkpad_acpi.h"
87 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
88 MODULE_DESCRIPTION(IBM_DESC);
89 MODULE_VERSION(IBM_VERSION);
90 MODULE_LICENSE("GPL");
92 /* Please remove this in year 2009 */
93 MODULE_ALIAS("ibm_acpi");
96 * DMI matching for module autoloading
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
104 #define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
107 /* Non-ancient thinkpads */
108 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
111 /* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
118 #define __unused __attribute__ ((unused))
120 /****************************************************************************
121 ****************************************************************************
123 * ACPI Helpers and device model
125 ****************************************************************************
126 ****************************************************************************/
128 /*************************************************************************
132 static acpi_handle root_handle;
134 #define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
137 static char *object##_path; \
138 static char *object##_paths[] = { paths }
140 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
149 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
153 /*************************************************************************
157 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
158 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159 "\\CMS", /* R40, R40e */
162 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
168 /*************************************************************************
172 static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *fmt, ...)
176 struct acpi_object_list params;
177 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178 struct acpi_buffer result, *resultp;
179 union acpi_object out_obj;
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
200 params.pointer = &in_objs[0];
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
210 /* add more types as needed */
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
226 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
235 success = status == AE_OK;
237 /* add more types as needed */
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
251 static void __unused acpi_print_int(acpi_handle handle, char *method)
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
258 printk(IBM_ERR "error calling %s\n", method);
261 static int acpi_ec_read(int i, u8 * p)
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
270 if (ec_read(i, p) < 0)
277 static int acpi_ec_write(int i, u8 v)
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
283 if (ec_write(i, v) < 0)
290 static int _sta(acpi_handle handle)
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
300 static int issue_thinkpad_cmos_command(int cmos_cmd)
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
311 /*************************************************************************
315 static void drv_acpi_handle_init(char *name,
316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
341 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
343 struct ibm_struct *ibm = data;
345 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
348 ibm->acpi->notify(ibm, event);
351 static int __init setup_acpi_notify(struct ibm_struct *ibm)
358 if (!*ibm->acpi->handle)
361 vdbg_printk(TPACPI_DBG_INIT,
362 "setting up ACPI notify for %s\n", ibm->name);
364 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
373 IBM_ACPI_EVENT_PREFIX,
376 status = acpi_install_notify_handler(*ibm->acpi->handle,
377 ibm->acpi->type, dispatch_acpi_notify, ibm);
378 if (ACPI_FAILURE(status)) {
379 if (status == AE_ALREADY_EXISTS) {
380 printk(IBM_NOTICE "another device driver is already handling %s events\n",
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
388 ibm->flags.acpi_notify_installed = 1;
392 static int __init tpacpi_device_add(struct acpi_device *device)
397 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
406 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407 if (!ibm->acpi->driver) {
408 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
412 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413 ibm->acpi->driver->ids = ibm->acpi->hid;
414 ibm->acpi->driver->ops.add = &tpacpi_device_add;
416 rc = acpi_bus_register_driver(ibm->acpi->driver);
418 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
420 kfree(ibm->acpi->driver);
421 ibm->acpi->driver = NULL;
423 ibm->flags.acpi_driver_registered = 1;
429 /****************************************************************************
430 ****************************************************************************
434 ****************************************************************************
435 ****************************************************************************/
437 static int dispatch_procfs_read(char *page, char **start, off_t off,
438 int count, int *eof, void *data)
440 struct ibm_struct *ibm = data;
443 if (!ibm || !ibm->read)
446 len = ibm->read(page);
450 if (len <= off + count)
462 static int dispatch_procfs_write(struct file *file,
463 const char __user * userbuf,
464 unsigned long count, void *data)
466 struct ibm_struct *ibm = data;
470 if (!ibm || !ibm->write)
473 kernbuf = kmalloc(count + 2, GFP_KERNEL);
477 if (copy_from_user(kernbuf, userbuf, count)) {
483 strcat(kernbuf, ",");
484 ret = ibm->write(kernbuf);
493 static char *next_cmd(char **cmds)
498 while ((end = strchr(start, ',')) && end == start)
510 /****************************************************************************
511 ****************************************************************************
513 * Device model: input, hwmon and platform
515 ****************************************************************************
516 ****************************************************************************/
518 static struct platform_device *tpacpi_pdev;
519 static struct class_device *tpacpi_hwmon;
520 static struct input_dev *tpacpi_inputdev;
523 static int tpacpi_resume_handler(struct platform_device *pdev)
525 struct ibm_struct *ibm, *itmp;
527 list_for_each_entry_safe(ibm, itmp,
537 static struct platform_driver tpacpi_pdriver = {
539 .name = IBM_DRVR_NAME,
540 .owner = THIS_MODULE,
542 .resume = tpacpi_resume_handler,
546 /*************************************************************************
547 * thinkpad-acpi driver attributes
550 /* interface_version --------------------------------------------------- */
551 static ssize_t tpacpi_driver_interface_version_show(
552 struct device_driver *drv,
555 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
558 static DRIVER_ATTR(interface_version, S_IRUGO,
559 tpacpi_driver_interface_version_show, NULL);
561 /* debug_level --------------------------------------------------------- */
562 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
565 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
568 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
569 const char *buf, size_t count)
573 if (parse_strtoul(buf, 0xffff, &t))
581 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
582 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
584 /* version ------------------------------------------------------------- */
585 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
588 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
591 static DRIVER_ATTR(version, S_IRUGO,
592 tpacpi_driver_version_show, NULL);
594 /* --------------------------------------------------------------------- */
596 static struct driver_attribute* tpacpi_driver_attributes[] = {
597 &driver_attr_debug_level, &driver_attr_version,
598 &driver_attr_interface_version,
601 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
607 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
608 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
615 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
619 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
620 driver_remove_file(drv, tpacpi_driver_attributes[i]);
623 /*************************************************************************
624 * sysfs support helpers
627 struct attribute_set_obj {
628 struct attribute_set s;
630 } __attribute__((packed));
632 static struct attribute_set *create_attr_set(unsigned int max_members,
635 struct attribute_set_obj *sobj;
637 if (max_members == 0)
640 /* Allocates space for implicit NULL at the end too */
641 sobj = kzalloc(sizeof(struct attribute_set_obj) +
642 max_members * sizeof(struct attribute *),
646 sobj->s.max_members = max_members;
647 sobj->s.group.attrs = &sobj->a;
648 sobj->s.group.name = name;
653 /* not multi-threaded safe, use it in a single thread per set */
654 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
659 if (s->members >= s->max_members)
662 s->group.attrs[s->members] = attr;
668 static int add_many_to_attr_set(struct attribute_set* s,
669 struct attribute **attr,
674 for (i = 0; i < count; i++) {
675 res = add_to_attr_set(s, attr[i]);
683 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
685 sysfs_remove_group(kobj, &s->group);
689 static int parse_strtoul(const char *buf,
690 unsigned long max, unsigned long *value)
694 *value = simple_strtoul(buf, &endp, 0);
695 while (*endp && isspace(*endp))
697 if (*endp || *value > max)
703 /****************************************************************************
704 ****************************************************************************
708 ****************************************************************************
709 ****************************************************************************/
711 /*************************************************************************
712 * thinkpad-acpi init subdriver
715 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
717 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
718 printk(IBM_INFO "%s\n", IBM_URL);
720 if (ibm_thinkpad_ec_found)
721 printk(IBM_INFO "ThinkPad EC firmware %s\n",
722 ibm_thinkpad_ec_found);
727 static int thinkpad_acpi_driver_read(char *p)
731 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
732 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
737 static struct ibm_struct thinkpad_acpi_driver_data = {
739 .read = thinkpad_acpi_driver_read,
742 /*************************************************************************
746 static int hotkey_orig_status;
747 static u32 hotkey_orig_mask;
748 static u32 hotkey_all_mask;
749 static u32 hotkey_reserved_mask;
751 static u16 hotkey_keycode_map[] = {
752 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
753 KEY_FN_F1, KEY_FN_F2, KEY_FN_F3, KEY_SLEEP,
754 KEY_FN_F5, KEY_FN_F6, KEY_FN_F7, KEY_FN_F8,
755 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
756 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
757 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
758 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
759 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
760 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
761 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
762 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
763 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
764 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
765 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
766 KEY_RESERVED, /* 0x14: VOLUME UP */
767 KEY_RESERVED, /* 0x15: VOLUME DOWN */
768 KEY_RESERVED, /* 0x16: MUTE */
769 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
770 /* (assignments unknown, please report if found) */
771 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
772 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
775 static struct attribute_set *hotkey_dev_attributes;
777 static int hotkey_get_wlsw(int *status)
779 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
784 /* sysfs hotkey enable ------------------------------------------------- */
785 static ssize_t hotkey_enable_show(struct device *dev,
786 struct device_attribute *attr,
792 res = hotkey_get(&status, &mask);
796 return snprintf(buf, PAGE_SIZE, "%d\n", status);
799 static ssize_t hotkey_enable_store(struct device *dev,
800 struct device_attribute *attr,
801 const char *buf, size_t count)
807 if (parse_strtoul(buf, 1, &t))
810 res = hotkey_get(&status, &mask);
812 res = hotkey_set(t, mask);
814 return (res) ? res : count;
817 static struct device_attribute dev_attr_hotkey_enable =
818 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
819 hotkey_enable_show, hotkey_enable_store);
821 /* sysfs hotkey mask --------------------------------------------------- */
822 static ssize_t hotkey_mask_show(struct device *dev,
823 struct device_attribute *attr,
829 res = hotkey_get(&status, &mask);
833 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
836 static ssize_t hotkey_mask_store(struct device *dev,
837 struct device_attribute *attr,
838 const char *buf, size_t count)
844 if (parse_strtoul(buf, 0xffffffffUL, &t))
847 res = hotkey_get(&status, &mask);
849 hotkey_set(status, t);
851 return (res) ? res : count;
854 static struct device_attribute dev_attr_hotkey_mask =
855 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
856 hotkey_mask_show, hotkey_mask_store);
858 /* sysfs hotkey bios_enabled ------------------------------------------- */
859 static ssize_t hotkey_bios_enabled_show(struct device *dev,
860 struct device_attribute *attr,
863 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
866 static struct device_attribute dev_attr_hotkey_bios_enabled =
867 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
869 /* sysfs hotkey bios_mask ---------------------------------------------- */
870 static ssize_t hotkey_bios_mask_show(struct device *dev,
871 struct device_attribute *attr,
874 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
877 static struct device_attribute dev_attr_hotkey_bios_mask =
878 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
880 /* sysfs hotkey all_mask ----------------------------------------------- */
881 static ssize_t hotkey_all_mask_show(struct device *dev,
882 struct device_attribute *attr,
885 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
888 static struct device_attribute dev_attr_hotkey_all_mask =
889 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
891 /* sysfs hotkey recommended_mask --------------------------------------- */
892 static ssize_t hotkey_recommended_mask_show(struct device *dev,
893 struct device_attribute *attr,
896 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
897 hotkey_all_mask & ~hotkey_reserved_mask);
900 static struct device_attribute dev_attr_hotkey_recommended_mask =
901 __ATTR(hotkey_recommended_mask, S_IRUGO,
902 hotkey_recommended_mask_show, NULL);
904 /* sysfs hotkey radio_sw ----------------------------------------------- */
905 static ssize_t hotkey_radio_sw_show(struct device *dev,
906 struct device_attribute *attr,
910 res = hotkey_get_wlsw(&s);
914 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
917 static struct device_attribute dev_attr_hotkey_radio_sw =
918 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
920 /* --------------------------------------------------------------------- */
922 static struct attribute *hotkey_mask_attributes[] = {
923 &dev_attr_hotkey_mask.attr,
924 &dev_attr_hotkey_bios_enabled.attr,
925 &dev_attr_hotkey_bios_mask.attr,
926 &dev_attr_hotkey_all_mask.attr,
927 &dev_attr_hotkey_recommended_mask.attr,
930 static int __init hotkey_init(struct ibm_init_struct *iibm)
935 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
937 BUG_ON(!tpacpi_inputdev);
939 IBM_ACPIHANDLE_INIT(hkey);
940 mutex_init(&hotkey_mutex);
942 /* hotkey not supported on 570 */
943 tp_features.hotkey = hkey_handle != NULL;
945 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
946 str_supported(tp_features.hotkey));
948 if (tp_features.hotkey) {
949 hotkey_dev_attributes = create_attr_set(7, NULL);
950 if (!hotkey_dev_attributes)
952 res = add_to_attr_set(hotkey_dev_attributes,
953 &dev_attr_hotkey_enable.attr);
957 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
958 A30, R30, R31, T20-22, X20-21, X22-24 */
959 tp_features.hotkey_mask =
960 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
962 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
963 str_supported(tp_features.hotkey_mask));
965 if (tp_features.hotkey_mask) {
966 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
967 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
969 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
972 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
973 if (!res && tp_features.hotkey_mask) {
974 res = add_many_to_attr_set(hotkey_dev_attributes,
975 hotkey_mask_attributes,
976 ARRAY_SIZE(hotkey_mask_attributes));
979 /* Not all thinkpads have a hardware radio switch */
980 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
981 tp_features.hotkey_wlsw = 1;
983 "radio switch found; radios are %s\n",
985 res = add_to_attr_set(hotkey_dev_attributes,
986 &dev_attr_hotkey_radio_sw.attr);
990 res = register_attr_set_with_sysfs(
991 hotkey_dev_attributes,
992 &tpacpi_pdev->dev.kobj);
996 #ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
997 for (i = 0; i < 12; i++)
998 hotkey_keycode_map[i] = KEY_UNKNOWN;
999 #endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1001 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1002 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1003 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1004 tpacpi_inputdev->keycodesize = sizeof(hotkey_keycode_map[0]);
1005 tpacpi_inputdev->keycodemax = ARRAY_SIZE(hotkey_keycode_map);
1006 tpacpi_inputdev->keycode = &hotkey_keycode_map;
1007 for (i = 0; i < ARRAY_SIZE(hotkey_keycode_map); i++) {
1008 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1009 set_bit(hotkey_keycode_map[i],
1010 tpacpi_inputdev->keybit);
1012 if (i < sizeof(hotkey_reserved_mask)*8)
1013 hotkey_reserved_mask |= 1 << i;
1017 #ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1018 dbg_printk(TPACPI_DBG_INIT,
1019 "enabling hot key handling\n");
1020 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1021 | hotkey_orig_mask);
1024 #endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1027 return (tp_features.hotkey)? 0 : 1;
1030 static void hotkey_exit(void)
1034 if (tp_features.hotkey) {
1035 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1036 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1038 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1041 if (hotkey_dev_attributes) {
1042 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1043 hotkey_dev_attributes = NULL;
1047 static void tpacpi_input_send_key(unsigned int scancode,
1048 unsigned int keycode)
1050 if (keycode != KEY_RESERVED) {
1051 input_report_key(tpacpi_inputdev, keycode, 1);
1052 if (keycode == KEY_UNKNOWN)
1053 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1055 input_sync(tpacpi_inputdev);
1057 input_report_key(tpacpi_inputdev, keycode, 0);
1058 if (keycode == KEY_UNKNOWN)
1059 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1061 input_sync(tpacpi_inputdev);
1065 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1068 unsigned int keycode, scancode;
1071 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1072 if (tpacpi_inputdev->users > 0) {
1073 switch (hkey >> 12) {
1075 /* 0x1000-0x1FFF: key presses */
1076 scancode = hkey & 0xfff;
1077 if (scancode > 0 && scancode < 0x21) {
1079 keycode = hotkey_keycode_map[scancode];
1080 tpacpi_input_send_key(scancode, keycode);
1081 sendacpi = (keycode == KEY_RESERVED
1082 || keycode == KEY_UNKNOWN);
1085 "hotkey 0x%04x out of range for keyboard map\n",
1090 /* 0x5000-0x5FFF: LID */
1091 /* we don't handle it through this path, just
1092 * eat up known LID events */
1093 if (hkey != 0x5001 && hkey != 0x5002) {
1095 "unknown LID-related hotkey event: 0x%04x\n",
1100 /* case 2: dock-related */
1101 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1102 /* case 3: ultra-bay related. maybe bay in dock? */
1103 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1104 printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
1109 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1111 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1112 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1117 * Call with hotkey_mutex held
1119 static int hotkey_get(int *status, u32 *mask)
1121 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1124 if (tp_features.hotkey_mask)
1125 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1132 * Call with hotkey_mutex held
1134 static int hotkey_set(int status, u32 mask)
1138 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1141 if (tp_features.hotkey_mask)
1142 for (i = 0; i < 32; i++) {
1143 int bit = ((1 << i) & mask) != 0;
1144 if (!acpi_evalf(hkey_handle,
1145 NULL, "MHKM", "vdd", i + 1, bit))
1152 /* procfs -------------------------------------------------------------- */
1153 static int hotkey_read(char *p)
1159 if (!tp_features.hotkey) {
1160 len += sprintf(p + len, "status:\t\tnot supported\n");
1164 res = mutex_lock_interruptible(&hotkey_mutex);
1167 res = hotkey_get(&status, &mask);
1168 mutex_unlock(&hotkey_mutex);
1172 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1173 if (tp_features.hotkey_mask) {
1174 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1175 len += sprintf(p + len,
1176 "commands:\tenable, disable, reset, <mask>\n");
1178 len += sprintf(p + len, "mask:\t\tnot supported\n");
1179 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1185 static int hotkey_write(char *buf)
1192 if (!tp_features.hotkey)
1195 res = mutex_lock_interruptible(&hotkey_mutex);
1199 res = hotkey_get(&status, &mask);
1204 while ((cmd = next_cmd(&buf))) {
1205 if (strlencmp(cmd, "enable") == 0) {
1207 } else if (strlencmp(cmd, "disable") == 0) {
1209 } else if (strlencmp(cmd, "reset") == 0) {
1210 status = hotkey_orig_status;
1211 mask = hotkey_orig_mask;
1212 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1214 } else if (sscanf(cmd, "%x", &mask) == 1) {
1224 res = hotkey_set(status, mask);
1227 mutex_unlock(&hotkey_mutex);
1231 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1232 .hid = IBM_HKEY_HID,
1233 .notify = hotkey_notify,
1234 .handle = &hkey_handle,
1235 .type = ACPI_DEVICE_NOTIFY,
1238 static struct ibm_struct hotkey_driver_data = {
1240 .read = hotkey_read,
1241 .write = hotkey_write,
1242 .exit = hotkey_exit,
1243 .acpi = &ibm_hotkey_acpidriver,
1246 /*************************************************************************
1247 * Bluetooth subdriver
1250 /* sysfs bluetooth enable ---------------------------------------------- */
1251 static ssize_t bluetooth_enable_show(struct device *dev,
1252 struct device_attribute *attr,
1257 status = bluetooth_get_radiosw();
1261 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1264 static ssize_t bluetooth_enable_store(struct device *dev,
1265 struct device_attribute *attr,
1266 const char *buf, size_t count)
1271 if (parse_strtoul(buf, 1, &t))
1274 res = bluetooth_set_radiosw(t);
1276 return (res) ? res : count;
1279 static struct device_attribute dev_attr_bluetooth_enable =
1280 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1281 bluetooth_enable_show, bluetooth_enable_store);
1283 /* --------------------------------------------------------------------- */
1285 static struct attribute *bluetooth_attributes[] = {
1286 &dev_attr_bluetooth_enable.attr,
1290 static const struct attribute_group bluetooth_attr_group = {
1291 .attrs = bluetooth_attributes,
1294 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1299 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1301 IBM_ACPIHANDLE_INIT(hkey);
1303 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1304 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1305 tp_features.bluetooth = hkey_handle &&
1306 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1308 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1309 str_supported(tp_features.bluetooth),
1312 if (tp_features.bluetooth) {
1313 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1314 /* no bluetooth hardware present in system */
1315 tp_features.bluetooth = 0;
1316 dbg_printk(TPACPI_DBG_INIT,
1317 "bluetooth hardware not installed\n");
1319 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1320 &bluetooth_attr_group);
1326 return (tp_features.bluetooth)? 0 : 1;
1329 static void bluetooth_exit(void)
1331 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1332 &bluetooth_attr_group);
1335 static int bluetooth_get_radiosw(void)
1339 if (!tp_features.bluetooth)
1342 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1345 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1348 static int bluetooth_set_radiosw(int radio_on)
1352 if (!tp_features.bluetooth)
1355 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1358 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1360 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1361 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1367 /* procfs -------------------------------------------------------------- */
1368 static int bluetooth_read(char *p)
1371 int status = bluetooth_get_radiosw();
1373 if (!tp_features.bluetooth)
1374 len += sprintf(p + len, "status:\t\tnot supported\n");
1376 len += sprintf(p + len, "status:\t\t%s\n",
1377 (status)? "enabled" : "disabled");
1378 len += sprintf(p + len, "commands:\tenable, disable\n");
1384 static int bluetooth_write(char *buf)
1388 if (!tp_features.bluetooth)
1391 while ((cmd = next_cmd(&buf))) {
1392 if (strlencmp(cmd, "enable") == 0) {
1393 bluetooth_set_radiosw(1);
1394 } else if (strlencmp(cmd, "disable") == 0) {
1395 bluetooth_set_radiosw(0);
1403 static struct ibm_struct bluetooth_driver_data = {
1404 .name = "bluetooth",
1405 .read = bluetooth_read,
1406 .write = bluetooth_write,
1407 .exit = bluetooth_exit,
1410 /*************************************************************************
1414 /* sysfs wan enable ---------------------------------------------------- */
1415 static ssize_t wan_enable_show(struct device *dev,
1416 struct device_attribute *attr,
1421 status = wan_get_radiosw();
1425 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1428 static ssize_t wan_enable_store(struct device *dev,
1429 struct device_attribute *attr,
1430 const char *buf, size_t count)
1435 if (parse_strtoul(buf, 1, &t))
1438 res = wan_set_radiosw(t);
1440 return (res) ? res : count;
1443 static struct device_attribute dev_attr_wan_enable =
1444 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1445 wan_enable_show, wan_enable_store);
1447 /* --------------------------------------------------------------------- */
1449 static struct attribute *wan_attributes[] = {
1450 &dev_attr_wan_enable.attr,
1454 static const struct attribute_group wan_attr_group = {
1455 .attrs = wan_attributes,
1458 static int __init wan_init(struct ibm_init_struct *iibm)
1463 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1465 IBM_ACPIHANDLE_INIT(hkey);
1467 tp_features.wan = hkey_handle &&
1468 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1470 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1471 str_supported(tp_features.wan),
1474 if (tp_features.wan) {
1475 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1476 /* no wan hardware present in system */
1477 tp_features.wan = 0;
1478 dbg_printk(TPACPI_DBG_INIT,
1479 "wan hardware not installed\n");
1481 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1488 return (tp_features.wan)? 0 : 1;
1491 static void wan_exit(void)
1493 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1497 static int wan_get_radiosw(void)
1501 if (!tp_features.wan)
1504 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1507 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1510 static int wan_set_radiosw(int radio_on)
1514 if (!tp_features.wan)
1517 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1520 status |= TP_ACPI_WANCARD_RADIOSSW;
1522 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1523 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1529 /* procfs -------------------------------------------------------------- */
1530 static int wan_read(char *p)
1533 int status = wan_get_radiosw();
1535 if (!tp_features.wan)
1536 len += sprintf(p + len, "status:\t\tnot supported\n");
1538 len += sprintf(p + len, "status:\t\t%s\n",
1539 (status)? "enabled" : "disabled");
1540 len += sprintf(p + len, "commands:\tenable, disable\n");
1546 static int wan_write(char *buf)
1550 if (!tp_features.wan)
1553 while ((cmd = next_cmd(&buf))) {
1554 if (strlencmp(cmd, "enable") == 0) {
1556 } else if (strlencmp(cmd, "disable") == 0) {
1565 static struct ibm_struct wan_driver_data = {
1570 .flags.experimental = 1,
1573 /*************************************************************************
1577 static enum video_access_mode video_supported;
1578 static int video_orig_autosw;
1580 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1581 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1582 "\\_SB.PCI0.VID0", /* 770e */
1583 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1584 "\\_SB.PCI0.AGP.VID", /* all others */
1587 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1589 static int __init video_init(struct ibm_init_struct *iibm)
1593 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1595 IBM_ACPIHANDLE_INIT(vid);
1596 IBM_ACPIHANDLE_INIT(vid2);
1598 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1599 /* G41, assume IVGA doesn't change */
1600 vid_handle = vid2_handle;
1603 /* video switching not supported on R30, R31 */
1604 video_supported = TPACPI_VIDEO_NONE;
1605 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1607 video_supported = TPACPI_VIDEO_570;
1608 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1609 /* 600e/x, 770e, 770x */
1610 video_supported = TPACPI_VIDEO_770;
1613 video_supported = TPACPI_VIDEO_NEW;
1615 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1616 str_supported(video_supported != TPACPI_VIDEO_NONE),
1619 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1622 static void video_exit(void)
1624 dbg_printk(TPACPI_DBG_EXIT,
1625 "restoring original video autoswitch mode\n");
1626 if (video_autosw_set(video_orig_autosw))
1627 printk(IBM_ERR "error while trying to restore original "
1628 "video autoswitch mode\n");
1631 static int video_outputsw_get(void)
1636 switch (video_supported) {
1637 case TPACPI_VIDEO_570:
1638 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1639 TP_ACPI_VIDEO_570_PHSCMD))
1641 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1643 case TPACPI_VIDEO_770:
1644 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1647 status |= TP_ACPI_VIDEO_S_LCD;
1648 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1651 status |= TP_ACPI_VIDEO_S_CRT;
1653 case TPACPI_VIDEO_NEW:
1654 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1655 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1658 status |= TP_ACPI_VIDEO_S_CRT;
1660 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1661 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1664 status |= TP_ACPI_VIDEO_S_LCD;
1665 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1668 status |= TP_ACPI_VIDEO_S_DVI;
1677 static int video_outputsw_set(int status)
1682 switch (video_supported) {
1683 case TPACPI_VIDEO_570:
1684 res = acpi_evalf(NULL, NULL,
1685 "\\_SB.PHS2", "vdd",
1686 TP_ACPI_VIDEO_570_PHS2CMD,
1687 status | TP_ACPI_VIDEO_570_PHS2SET);
1689 case TPACPI_VIDEO_770:
1690 autosw = video_autosw_get();
1694 res = video_autosw_set(1);
1697 res = acpi_evalf(vid_handle, NULL,
1698 "ASWT", "vdd", status * 0x100, 0);
1699 if (!autosw && video_autosw_set(autosw)) {
1700 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1704 case TPACPI_VIDEO_NEW:
1705 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1706 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1712 return (res)? 0 : -EIO;
1715 static int video_autosw_get(void)
1719 switch (video_supported) {
1720 case TPACPI_VIDEO_570:
1721 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1724 case TPACPI_VIDEO_770:
1725 case TPACPI_VIDEO_NEW:
1726 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1736 static int video_autosw_set(int enable)
1738 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1743 static int video_outputsw_cycle(void)
1745 int autosw = video_autosw_get();
1751 switch (video_supported) {
1752 case TPACPI_VIDEO_570:
1753 res = video_autosw_set(1);
1756 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1758 case TPACPI_VIDEO_770:
1759 case TPACPI_VIDEO_NEW:
1760 res = video_autosw_set(1);
1763 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1768 if (!autosw && video_autosw_set(autosw)) {
1769 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1773 return (res)? 0 : -EIO;
1776 static int video_expand_toggle(void)
1778 switch (video_supported) {
1779 case TPACPI_VIDEO_570:
1780 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1782 case TPACPI_VIDEO_770:
1783 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1785 case TPACPI_VIDEO_NEW:
1786 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1794 static int video_read(char *p)
1799 if (video_supported == TPACPI_VIDEO_NONE) {
1800 len += sprintf(p + len, "status:\t\tnot supported\n");
1804 status = video_outputsw_get();
1808 autosw = video_autosw_get();
1812 len += sprintf(p + len, "status:\t\tsupported\n");
1813 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1814 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1815 if (video_supported == TPACPI_VIDEO_NEW)
1816 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1817 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1818 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1819 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1820 if (video_supported == TPACPI_VIDEO_NEW)
1821 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1822 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1823 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1828 static int video_write(char *buf)
1831 int enable, disable, status;
1834 if (video_supported == TPACPI_VIDEO_NONE)
1840 while ((cmd = next_cmd(&buf))) {
1841 if (strlencmp(cmd, "lcd_enable") == 0) {
1842 enable |= TP_ACPI_VIDEO_S_LCD;
1843 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1844 disable |= TP_ACPI_VIDEO_S_LCD;
1845 } else if (strlencmp(cmd, "crt_enable") == 0) {
1846 enable |= TP_ACPI_VIDEO_S_CRT;
1847 } else if (strlencmp(cmd, "crt_disable") == 0) {
1848 disable |= TP_ACPI_VIDEO_S_CRT;
1849 } else if (video_supported == TPACPI_VIDEO_NEW &&
1850 strlencmp(cmd, "dvi_enable") == 0) {
1851 enable |= TP_ACPI_VIDEO_S_DVI;
1852 } else if (video_supported == TPACPI_VIDEO_NEW &&
1853 strlencmp(cmd, "dvi_disable") == 0) {
1854 disable |= TP_ACPI_VIDEO_S_DVI;
1855 } else if (strlencmp(cmd, "auto_enable") == 0) {
1856 res = video_autosw_set(1);
1859 } else if (strlencmp(cmd, "auto_disable") == 0) {
1860 res = video_autosw_set(0);
1863 } else if (strlencmp(cmd, "video_switch") == 0) {
1864 res = video_outputsw_cycle();
1867 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1868 res = video_expand_toggle();
1875 if (enable || disable) {
1876 status = video_outputsw_get();
1879 res = video_outputsw_set((status & ~disable) | enable);
1887 static struct ibm_struct video_driver_data = {
1890 .write = video_write,
1894 /*************************************************************************
1895 * Light (thinklight) subdriver
1898 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1899 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1901 static int __init light_init(struct ibm_init_struct *iibm)
1903 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1905 IBM_ACPIHANDLE_INIT(ledb);
1906 IBM_ACPIHANDLE_INIT(lght);
1907 IBM_ACPIHANDLE_INIT(cmos);
1909 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1910 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1912 if (tp_features.light)
1913 /* light status not supported on
1914 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1915 tp_features.light_status =
1916 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1918 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1919 str_supported(tp_features.light));
1921 return (tp_features.light)? 0 : 1;
1924 static int light_read(char *p)
1929 if (!tp_features.light) {
1930 len += sprintf(p + len, "status:\t\tnot supported\n");
1931 } else if (!tp_features.light_status) {
1932 len += sprintf(p + len, "status:\t\tunknown\n");
1933 len += sprintf(p + len, "commands:\ton, off\n");
1935 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1937 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1938 len += sprintf(p + len, "commands:\ton, off\n");
1944 static int light_write(char *buf)
1946 int cmos_cmd, lght_cmd;
1950 if (!tp_features.light)
1953 while ((cmd = next_cmd(&buf))) {
1954 if (strlencmp(cmd, "on") == 0) {
1957 } else if (strlencmp(cmd, "off") == 0) {
1963 success = cmos_handle ?
1964 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1965 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1973 static struct ibm_struct light_driver_data = {
1976 .write = light_write,
1979 /*************************************************************************
1983 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1985 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1986 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1987 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1988 "\\_SB.PCI.ISA.SLCE", /* 570 */
1989 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1991 /* don't list other alternatives as we install a notify handler on the 570 */
1992 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1994 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1996 .notify = dock_notify,
1997 .handle = &dock_handle,
1998 .type = ACPI_SYSTEM_NOTIFY,
2002 .notify = dock_notify,
2003 .handle = &pci_handle,
2004 .type = ACPI_SYSTEM_NOTIFY,
2008 static struct ibm_struct dock_driver_data[2] = {
2012 .write = dock_write,
2013 .acpi = &ibm_dock_acpidriver[0],
2017 .acpi = &ibm_dock_acpidriver[1],
2021 #define dock_docked() (_sta(dock_handle) & 1)
2023 static int __init dock_init(struct ibm_init_struct *iibm)
2025 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2027 IBM_ACPIHANDLE_INIT(dock);
2029 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2030 str_supported(dock_handle != NULL));
2032 return (dock_handle)? 0 : 1;
2035 static int __init dock_init2(struct ibm_init_struct *iibm)
2039 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2041 if (dock_driver_data[0].flags.acpi_driver_registered &&
2042 dock_driver_data[0].flags.acpi_notify_installed) {
2043 IBM_ACPIHANDLE_INIT(pci);
2044 dock2_needed = (pci_handle != NULL);
2045 vdbg_printk(TPACPI_DBG_INIT,
2046 "dock PCI handler for the TP 570 is %s\n",
2047 str_supported(dock2_needed));
2049 vdbg_printk(TPACPI_DBG_INIT,
2050 "dock subdriver part 2 not required\n");
2054 return (dock2_needed)? 0 : 1;
2057 static void dock_notify(struct ibm_struct *ibm, u32 event)
2059 int docked = dock_docked();
2060 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
2062 if (event == 1 && !pci) /* 570 */
2063 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2064 else if (event == 1 && pci) /* 570 */
2065 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2066 else if (event == 3 && docked)
2067 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2068 else if (event == 3 && !docked)
2069 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
2070 else if (event == 0 && docked)
2071 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2073 printk(IBM_ERR "unknown dock event %d, status %d\n",
2074 event, _sta(dock_handle));
2075 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
2079 static int dock_read(char *p)
2082 int docked = dock_docked();
2085 len += sprintf(p + len, "status:\t\tnot supported\n");
2087 len += sprintf(p + len, "status:\t\tundocked\n");
2089 len += sprintf(p + len, "status:\t\tdocked\n");
2090 len += sprintf(p + len, "commands:\tdock, undock\n");
2096 static int dock_write(char *buf)
2103 while ((cmd = next_cmd(&buf))) {
2104 if (strlencmp(cmd, "undock") == 0) {
2105 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2106 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2108 } else if (strlencmp(cmd, "dock") == 0) {
2109 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2118 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2120 /*************************************************************************
2124 #ifdef CONFIG_THINKPAD_ACPI_BAY
2125 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2126 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2127 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2128 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2129 ); /* A21e, R30, R31 */
2130 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2131 "_EJ0", /* all others */
2132 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2133 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2134 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2136 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2140 static int __init bay_init(struct ibm_init_struct *iibm)
2142 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2144 IBM_ACPIHANDLE_INIT(bay);
2146 IBM_ACPIHANDLE_INIT(bay_ej);
2147 IBM_ACPIHANDLE_INIT(bay2);
2149 IBM_ACPIHANDLE_INIT(bay2_ej);
2151 tp_features.bay_status = bay_handle &&
2152 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2153 tp_features.bay_status2 = bay2_handle &&
2154 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2156 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2157 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2158 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2159 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2161 vdbg_printk(TPACPI_DBG_INIT,
2162 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2163 str_supported(tp_features.bay_status),
2164 str_supported(tp_features.bay_eject),
2165 str_supported(tp_features.bay_status2),
2166 str_supported(tp_features.bay_eject2));
2168 return (tp_features.bay_status || tp_features.bay_eject ||
2169 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2172 static void bay_notify(struct ibm_struct *ibm, u32 event)
2174 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2177 #define bay_occupied(b) (_sta(b##_handle) & 1)
2179 static int bay_read(char *p)
2182 int occupied = bay_occupied(bay);
2183 int occupied2 = bay_occupied(bay2);
2186 len += sprintf(p + len, "status:\t\t%s\n",
2187 tp_features.bay_status ?
2188 (occupied ? "occupied" : "unoccupied") :
2190 if (tp_features.bay_status2)
2191 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2192 "occupied" : "unoccupied");
2194 eject = tp_features.bay_eject && occupied;
2195 eject2 = tp_features.bay_eject2 && occupied2;
2197 if (eject && eject2)
2198 len += sprintf(p + len, "commands:\teject, eject2\n");
2200 len += sprintf(p + len, "commands:\teject\n");
2202 len += sprintf(p + len, "commands:\teject2\n");
2207 static int bay_write(char *buf)
2211 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2214 while ((cmd = next_cmd(&buf))) {
2215 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2216 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2218 } else if (tp_features.bay_eject2 &&
2219 strlencmp(cmd, "eject2") == 0) {
2220 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2229 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2230 .notify = bay_notify,
2231 .handle = &bay_handle,
2232 .type = ACPI_SYSTEM_NOTIFY,
2235 static struct ibm_struct bay_driver_data = {
2239 .acpi = &ibm_bay_acpidriver,
2242 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2244 /*************************************************************************
2248 /* sysfs cmos_command -------------------------------------------------- */
2249 static ssize_t cmos_command_store(struct device *dev,
2250 struct device_attribute *attr,
2251 const char *buf, size_t count)
2253 unsigned long cmos_cmd;
2256 if (parse_strtoul(buf, 21, &cmos_cmd))
2259 res = issue_thinkpad_cmos_command(cmos_cmd);
2260 return (res)? res : count;
2263 static struct device_attribute dev_attr_cmos_command =
2264 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2266 /* --------------------------------------------------------------------- */
2268 static int __init cmos_init(struct ibm_init_struct *iibm)
2272 vdbg_printk(TPACPI_DBG_INIT,
2273 "initializing cmos commands subdriver\n");
2275 IBM_ACPIHANDLE_INIT(cmos);
2277 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2278 str_supported(cmos_handle != NULL));
2280 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2284 return (cmos_handle)? 0 : 1;
2287 static void cmos_exit(void)
2289 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2292 static int cmos_read(char *p)
2296 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2297 R30, R31, T20-22, X20-21 */
2299 len += sprintf(p + len, "status:\t\tnot supported\n");
2301 len += sprintf(p + len, "status:\t\tsupported\n");
2302 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2308 static int cmos_write(char *buf)
2313 while ((cmd = next_cmd(&buf))) {
2314 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2315 cmos_cmd >= 0 && cmos_cmd <= 21) {
2320 res = issue_thinkpad_cmos_command(cmos_cmd);
2328 static struct ibm_struct cmos_driver_data = {
2331 .write = cmos_write,
2335 /*************************************************************************
2339 static enum led_access_mode led_supported;
2341 IBM_HANDLE(led, ec, "SLED", /* 570 */
2342 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2343 "LED", /* all others */
2346 static int __init led_init(struct ibm_init_struct *iibm)
2348 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2350 IBM_ACPIHANDLE_INIT(led);
2353 /* led not supported on R30, R31 */
2354 led_supported = TPACPI_LED_NONE;
2355 else if (strlencmp(led_path, "SLED") == 0)
2357 led_supported = TPACPI_LED_570;
2358 else if (strlencmp(led_path, "SYSL") == 0)
2359 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2360 led_supported = TPACPI_LED_OLD;
2363 led_supported = TPACPI_LED_NEW;
2365 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2366 str_supported(led_supported), led_supported);
2368 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2371 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2373 static int led_read(char *p)
2377 if (!led_supported) {
2378 len += sprintf(p + len, "status:\t\tnot supported\n");
2381 len += sprintf(p + len, "status:\t\tsupported\n");
2383 if (led_supported == TPACPI_LED_570) {
2386 for (i = 0; i < 8; i++) {
2387 if (!acpi_evalf(ec_handle,
2388 &status, "GLED", "dd", 1 << i))
2390 len += sprintf(p + len, "%d:\t\t%s\n",
2391 i, led_status(status));
2395 len += sprintf(p + len, "commands:\t"
2396 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2401 /* off, on, blink */
2402 static const int led_sled_arg1[] = { 0, 1, 3 };
2403 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2404 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2405 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2407 static int led_write(char *buf)
2415 while ((cmd = next_cmd(&buf))) {
2416 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2419 if (strstr(cmd, "off")) {
2421 } else if (strstr(cmd, "on")) {
2423 } else if (strstr(cmd, "blink")) {
2428 if (led_supported == TPACPI_LED_570) {
2431 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2432 led, led_sled_arg1[ind]))
2434 } else if (led_supported == TPACPI_LED_OLD) {
2435 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2437 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2440 ec_write(TPACPI_LED_EC_HLBL,
2441 led * led_exp_hlbl[ind]);
2444 ec_write(TPACPI_LED_EC_HLCL,
2445 led * led_exp_hlcl[ind]);
2450 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2451 led, led_led_arg1[ind]))
2459 static struct ibm_struct led_driver_data = {
2465 /*************************************************************************
2469 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2471 static int __init beep_init(struct ibm_init_struct *iibm)
2473 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2475 IBM_ACPIHANDLE_INIT(beep);
2477 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2478 str_supported(beep_handle != NULL));
2480 return (beep_handle)? 0 : 1;
2483 static int beep_read(char *p)
2488 len += sprintf(p + len, "status:\t\tnot supported\n");
2490 len += sprintf(p + len, "status:\t\tsupported\n");
2491 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2497 static int beep_write(char *buf)
2505 while ((cmd = next_cmd(&buf))) {
2506 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2507 beep_cmd >= 0 && beep_cmd <= 17) {
2511 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2518 static struct ibm_struct beep_driver_data = {
2521 .write = beep_write,
2524 /*************************************************************************
2528 static enum thermal_access_mode thermal_read_mode;
2530 /* sysfs temp##_input -------------------------------------------------- */
2532 static ssize_t thermal_temp_input_show(struct device *dev,
2533 struct device_attribute *attr,
2536 struct sensor_device_attribute *sensor_attr =
2537 to_sensor_dev_attr(attr);
2538 int idx = sensor_attr->index;
2542 res = thermal_get_sensor(idx, &value);
2545 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2548 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2551 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2552 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2554 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2555 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2556 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2557 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2558 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2559 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2560 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2561 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2562 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2563 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2564 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2565 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2566 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2567 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2568 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2569 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2570 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2573 #define THERMAL_ATTRS(X) \
2574 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2576 static struct attribute *thermal_temp_input_attr[] = {
2596 static const struct attribute_group thermal_temp_input16_group = {
2597 .attrs = thermal_temp_input_attr
2600 static const struct attribute_group thermal_temp_input8_group = {
2601 .attrs = &thermal_temp_input_attr[8]
2604 #undef THERMAL_SENSOR_ATTR_TEMP
2605 #undef THERMAL_ATTRS
2607 /* --------------------------------------------------------------------- */
2609 static int __init thermal_init(struct ibm_init_struct *iibm)
2616 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2618 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2620 if (ibm_thinkpad_ec_found && experimental) {
2622 * Direct EC access mode: sensors at registers
2623 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2624 * non-implemented, thermal sensors return 0x80 when
2629 for (i = 0; i < 8; i++) {
2630 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2636 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2644 /* This is sheer paranoia, but we handle it anyway */
2647 "ThinkPad ACPI EC access misbehaving, "
2648 "falling back to ACPI TMPx access mode\n");
2649 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2652 "ThinkPad ACPI EC access misbehaving, "
2653 "disabling thermal sensors access\n");
2654 thermal_read_mode = TPACPI_THERMAL_NONE;
2659 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2661 } else if (acpi_tmp7) {
2662 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2663 /* 600e/x, 770e, 770x */
2664 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2666 /* Standard ACPI TMPx access, max 8 sensors */
2667 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2670 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2671 thermal_read_mode = TPACPI_THERMAL_NONE;
2674 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2675 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2678 switch(thermal_read_mode) {
2679 case TPACPI_THERMAL_TPEC_16:
2680 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2681 &thermal_temp_input16_group);
2685 case TPACPI_THERMAL_TPEC_8:
2686 case TPACPI_THERMAL_ACPI_TMP07:
2687 case TPACPI_THERMAL_ACPI_UPDT:
2688 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2689 &thermal_temp_input8_group);
2693 case TPACPI_THERMAL_NONE:
2701 static void thermal_exit(void)
2703 switch(thermal_read_mode) {
2704 case TPACPI_THERMAL_TPEC_16:
2705 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2706 &thermal_temp_input16_group);
2708 case TPACPI_THERMAL_TPEC_8:
2709 case TPACPI_THERMAL_ACPI_TMP07:
2710 case TPACPI_THERMAL_ACPI_UPDT:
2711 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2712 &thermal_temp_input16_group);
2714 case TPACPI_THERMAL_NONE:
2720 /* idx is zero-based */
2721 static int thermal_get_sensor(int idx, s32 *value)
2727 t = TP_EC_THERMAL_TMP0;
2729 switch (thermal_read_mode) {
2730 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2731 case TPACPI_THERMAL_TPEC_16:
2732 if (idx >= 8 && idx <= 15) {
2733 t = TP_EC_THERMAL_TMP8;
2738 case TPACPI_THERMAL_TPEC_8:
2740 if (!acpi_ec_read(t + idx, &tmp))
2742 *value = tmp * 1000;
2747 case TPACPI_THERMAL_ACPI_UPDT:
2749 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2750 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2752 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2754 *value = (t - 2732) * 100;
2759 case TPACPI_THERMAL_ACPI_TMP07:
2761 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2762 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2769 case TPACPI_THERMAL_NONE:
2777 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2788 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2791 for(i = 0 ; i < n; i++) {
2792 res = thermal_get_sensor(i, &s->temp[i]);
2800 static int thermal_read(char *p)
2804 struct ibm_thermal_sensors_struct t;
2806 n = thermal_get_sensors(&t);
2807 if (unlikely(n < 0))
2810 len += sprintf(p + len, "temperatures:\t");
2813 for (i = 0; i < (n - 1); i++)
2814 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2815 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2817 len += sprintf(p + len, "not supported\n");
2822 static struct ibm_struct thermal_driver_data = {
2824 .read = thermal_read,
2825 .exit = thermal_exit,
2828 /*************************************************************************
2832 static u8 ecdump_regs[256];
2834 static int ecdump_read(char *p)
2840 len += sprintf(p + len, "EC "
2841 " +00 +01 +02 +03 +04 +05 +06 +07"
2842 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2843 for (i = 0; i < 256; i += 16) {
2844 len += sprintf(p + len, "EC 0x%02x:", i);
2845 for (j = 0; j < 16; j++) {
2846 if (!acpi_ec_read(i + j, &v))
2848 if (v != ecdump_regs[i + j])
2849 len += sprintf(p + len, " *%02x", v);
2851 len += sprintf(p + len, " %02x", v);
2852 ecdump_regs[i + j] = v;
2854 len += sprintf(p + len, "\n");
2859 /* These are way too dangerous to advertise openly... */
2861 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2862 " (<offset> is 00-ff, <value> is 00-ff)\n");
2863 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2864 " (<offset> is 00-ff, <value> is 0-255)\n");
2869 static int ecdump_write(char *buf)
2874 while ((cmd = next_cmd(&buf))) {
2875 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2877 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2881 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2882 if (!acpi_ec_write(i, v))
2891 static struct ibm_struct ecdump_driver_data = {
2893 .read = ecdump_read,
2894 .write = ecdump_write,
2895 .flags.experimental = 1,
2898 /*************************************************************************
2899 * Backlight/brightness subdriver
2902 static struct backlight_device *ibm_backlight_device;
2904 static struct backlight_ops ibm_backlight_data = {
2905 .get_brightness = brightness_get,
2906 .update_status = brightness_update_status,
2909 static int __init brightness_init(struct ibm_init_struct *iibm)
2913 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2915 b = brightness_get(NULL);
2919 ibm_backlight_device = backlight_device_register(
2920 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2921 &ibm_backlight_data);
2922 if (IS_ERR(ibm_backlight_device)) {
2923 printk(IBM_ERR "Could not register backlight device\n");
2924 return PTR_ERR(ibm_backlight_device);
2926 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2928 ibm_backlight_device->props.max_brightness = 7;
2929 ibm_backlight_device->props.brightness = b;
2930 backlight_update_status(ibm_backlight_device);
2935 static void brightness_exit(void)
2937 if (ibm_backlight_device) {
2938 vdbg_printk(TPACPI_DBG_EXIT,
2939 "calling backlight_device_unregister()\n");
2940 backlight_device_unregister(ibm_backlight_device);
2941 ibm_backlight_device = NULL;
2945 static int brightness_update_status(struct backlight_device *bd)
2947 return brightness_set(
2948 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2949 bd->props.power == FB_BLANK_UNBLANK) ?
2950 bd->props.brightness : 0);
2953 static int brightness_get(struct backlight_device *bd)
2956 if (!acpi_ec_read(brightness_offset, &level))
2964 static int brightness_set(int value)
2966 int cmos_cmd, inc, i;
2967 int current_value = brightness_get(NULL);
2971 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2972 inc = value > current_value ? 1 : -1;
2973 for (i = current_value; i != value; i += inc) {
2974 if (issue_thinkpad_cmos_command(cmos_cmd))
2976 if (!acpi_ec_write(brightness_offset, i + inc))
2983 static int brightness_read(char *p)
2988 if ((level = brightness_get(NULL)) < 0) {
2989 len += sprintf(p + len, "level:\t\tunreadable\n");
2991 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2992 len += sprintf(p + len, "commands:\tup, down\n");
2993 len += sprintf(p + len, "commands:\tlevel <level>"
2994 " (<level> is 0-7)\n");
3000 static int brightness_write(char *buf)
3006 while ((cmd = next_cmd(&buf))) {
3007 if ((level = brightness_get(NULL)) < 0)
3011 if (strlencmp(cmd, "up") == 0) {
3012 new_level = level == 7 ? 7 : level + 1;
3013 } else if (strlencmp(cmd, "down") == 0) {
3014 new_level = level == 0 ? 0 : level - 1;
3015 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3016 new_level >= 0 && new_level <= 7) {
3021 brightness_set(new_level);
3027 static struct ibm_struct brightness_driver_data = {
3028 .name = "brightness",
3029 .read = brightness_read,
3030 .write = brightness_write,
3031 .exit = brightness_exit,
3034 /*************************************************************************
3038 static int volume_read(char *p)
3043 if (!acpi_ec_read(volume_offset, &level)) {
3044 len += sprintf(p + len, "level:\t\tunreadable\n");
3046 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3047 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3048 len += sprintf(p + len, "commands:\tup, down, mute\n");
3049 len += sprintf(p + len, "commands:\tlevel <level>"
3050 " (<level> is 0-15)\n");
3056 static int volume_write(char *buf)
3058 int cmos_cmd, inc, i;
3060 int new_level, new_mute;
3063 while ((cmd = next_cmd(&buf))) {
3064 if (!acpi_ec_read(volume_offset, &level))
3066 new_mute = mute = level & 0x40;
3067 new_level = level = level & 0xf;
3069 if (strlencmp(cmd, "up") == 0) {
3073 new_level = level == 15 ? 15 : level + 1;
3074 } else if (strlencmp(cmd, "down") == 0) {
3078 new_level = level == 0 ? 0 : level - 1;
3079 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3080 new_level >= 0 && new_level <= 15) {
3082 } else if (strlencmp(cmd, "mute") == 0) {
3087 if (new_level != level) { /* mute doesn't change */
3088 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3089 inc = new_level > level ? 1 : -1;
3091 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3092 !acpi_ec_write(volume_offset, level)))
3095 for (i = level; i != new_level; i += inc)
3096 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3097 !acpi_ec_write(volume_offset, i + inc))
3100 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3101 !acpi_ec_write(volume_offset,
3106 if (new_mute != mute) { /* level doesn't change */
3107 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3109 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3110 !acpi_ec_write(volume_offset, level + new_mute))
3118 static struct ibm_struct volume_driver_data = {
3120 .read = volume_read,
3121 .write = volume_write,
3124 /*************************************************************************
3131 * TPACPI_FAN_RD_ACPI_GFAN:
3132 * ACPI GFAN method: returns fan level
3134 * see TPACPI_FAN_WR_ACPI_SFAN
3135 * EC 0x2f (HFSP) not available if GFAN exists
3137 * TPACPI_FAN_WR_ACPI_SFAN:
3138 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3140 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3143 * TPACPI_FAN_WR_TPEC:
3144 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3145 * Supported on almost all ThinkPads
3147 * Fan speed changes of any sort (including those caused by the
3148 * disengaged mode) are usually done slowly by the firmware as the
3149 * maximum ammount of fan duty cycle change per second seems to be
3152 * Reading is not available if GFAN exists.
3153 * Writing is not available if SFAN exists.
3156 * 7 automatic mode engaged;
3157 * (default operation mode of the ThinkPad)
3158 * fan level is ignored in this mode.
3159 * 6 full speed mode (takes precedence over bit 7);
3160 * not available on all thinkpads. May disable
3161 * the tachometer while the fan controller ramps up
3162 * the speed (which can take up to a few *minutes*).
3163 * Speeds up fan to 100% duty-cycle, which is far above
3164 * the standard RPM levels. It is not impossible that
3165 * it could cause hardware damage.
3166 * 5-3 unused in some models. Extra bits for fan level
3167 * in others, but still useless as all values above
3168 * 7 map to the same speed as level 7 in these models.
3169 * 2-0 fan level (0..7 usually)
3171 * 0x07 = max (set when temperatures critical)
3172 * Some ThinkPads may have other levels, see
3173 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3175 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3176 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3177 * does so, its initial value is meaningless (0x07).
3179 * For firmware bugs, refer to:
3180 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3184 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3185 * Main fan tachometer reading (in RPM)
3187 * This register is present on all ThinkPads with a new-style EC, and
3188 * it is known not to be present on the A21m/e, and T22, as there is
3189 * something else in offset 0x84 according to the ACPI DSDT. Other
3190 * ThinkPads from this same time period (and earlier) probably lack the
3191 * tachometer as well.
3193 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3194 * was never fixed by IBM to report the EC firmware version string
3195 * probably support the tachometer (like the early X models), so
3196 * detecting it is quite hard. We need more data to know for sure.
3198 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3201 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3204 * For firmware bugs, refer to:
3205 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3207 * TPACPI_FAN_WR_ACPI_FANS:
3208 * ThinkPad X31, X40, X41. Not available in the X60.
3210 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3211 * high speed. ACPI DSDT seems to map these three speeds to levels
3212 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3213 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3215 * The speeds are stored on handles
3216 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3218 * There are three default speed sets, acessible as handles:
3219 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3221 * ACPI DSDT switches which set is in use depending on various
3224 * TPACPI_FAN_WR_TPEC is also available and should be used to
3225 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3226 * but the ACPI tables just mention level 7.
3229 static enum fan_status_access_mode fan_status_access_mode;
3230 static enum fan_control_access_mode fan_control_access_mode;
3231 static enum fan_control_commands fan_control_commands;
3233 static u8 fan_control_initial_status;
3234 static u8 fan_control_desired_level;
3236 static void fan_watchdog_fire(struct work_struct *ignored);
3237 static int fan_watchdog_maxinterval;
3238 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3240 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3241 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3242 "\\FSPD", /* 600e/x, 770e, 770x */
3244 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3245 "JFNS", /* 770x-JL */
3249 * SYSFS fan layout: hwmon compatible (device)
3252 * 0: "disengaged" mode
3254 * 2: native EC "auto" mode (recommended, hardware default)
3256 * pwm*: set speed in manual mode, ignored otherwise.
3257 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3260 * fan*_input: tachometer reading, RPM
3263 * SYSFS fan layout: extensions
3265 * fan_watchdog (driver):
3266 * fan watchdog interval in seconds, 0 disables (default), max 120
3269 /* sysfs fan pwm1_enable ----------------------------------------------- */
3270 static ssize_t fan_pwm1_enable_show(struct device *dev,
3271 struct device_attribute *attr,
3277 res = fan_get_status_safe(&status);
3281 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3282 if (status != fan_control_initial_status) {
3283 tp_features.fan_ctrl_status_undef = 0;
3285 /* Return most likely status. In fact, it
3286 * might be the only possible status */
3287 status = TP_EC_FAN_AUTO;
3291 if (status & TP_EC_FAN_FULLSPEED) {
3293 } else if (status & TP_EC_FAN_AUTO) {
3298 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3301 static ssize_t fan_pwm1_enable_store(struct device *dev,
3302 struct device_attribute *attr,
3303 const char *buf, size_t count)
3308 if (parse_strtoul(buf, 2, &t))
3313 level = TP_EC_FAN_FULLSPEED;
3316 level = TPACPI_FAN_LAST_LEVEL;
3319 level = TP_EC_FAN_AUTO;
3322 /* reserved for software-controlled auto mode */
3328 res = fan_set_level_safe(level);
3334 fan_watchdog_reset();
3339 static struct device_attribute dev_attr_fan_pwm1_enable =
3340 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3341 fan_pwm1_enable_show, fan_pwm1_enable_store);
3343 /* sysfs fan pwm1 ------------------------------------------------------ */
3344 static ssize_t fan_pwm1_show(struct device *dev,
3345 struct device_attribute *attr,
3351 res = fan_get_status_safe(&status);
3355 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3356 if (status != fan_control_initial_status) {
3357 tp_features.fan_ctrl_status_undef = 0;
3359 status = TP_EC_FAN_AUTO;
3364 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3365 status = fan_control_desired_level;
3370 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3373 static ssize_t fan_pwm1_store(struct device *dev,
3374 struct device_attribute *attr,
3375 const char *buf, size_t count)
3379 u8 status, newlevel;
3381 if (parse_strtoul(buf, 255, &s))
3384 /* scale down from 0-255 to 0-7 */
3385 newlevel = (s >> 5) & 0x07;
3387 rc = mutex_lock_interruptible(&fan_mutex);
3391 rc = fan_get_status(&status);
3392 if (!rc && (status &
3393 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3394 rc = fan_set_level(newlevel);
3398 fan_update_desired_level(newlevel);
3399 fan_watchdog_reset();
3403 mutex_unlock(&fan_mutex);
3404 return (rc)? rc : count;
3407 static struct device_attribute dev_attr_fan_pwm1 =
3408 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3409 fan_pwm1_show, fan_pwm1_store);
3411 /* sysfs fan fan1_input ------------------------------------------------ */
3412 static ssize_t fan_fan1_input_show(struct device *dev,
3413 struct device_attribute *attr,
3419 res = fan_get_speed(&speed);
3423 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3426 static struct device_attribute dev_attr_fan_fan1_input =
3427 __ATTR(fan1_input, S_IRUGO,
3428 fan_fan1_input_show, NULL);
3430 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3431 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3434 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3437 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3438 const char *buf, size_t count)
3442 if (parse_strtoul(buf, 120, &t))
3445 if (!fan_control_allowed)
3448 fan_watchdog_maxinterval = t;
3449 fan_watchdog_reset();
3454 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3455 fan_fan_watchdog_show, fan_fan_watchdog_store);
3457 /* --------------------------------------------------------------------- */
3458 static struct attribute *fan_attributes[] = {
3459 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3460 &dev_attr_fan_fan1_input.attr,
3464 static const struct attribute_group fan_attr_group = {
3465 .attrs = fan_attributes,
3468 static int __init fan_init(struct ibm_init_struct *iibm)
3472 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3474 mutex_init(&fan_mutex);
3475 fan_status_access_mode = TPACPI_FAN_NONE;
3476 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3477 fan_control_commands = 0;
3478 fan_watchdog_maxinterval = 0;
3479 tp_features.fan_ctrl_status_undef = 0;
3480 fan_control_desired_level = 7;
3482 IBM_ACPIHANDLE_INIT(fans);
3483 IBM_ACPIHANDLE_INIT(gfan);
3484 IBM_ACPIHANDLE_INIT(sfan);
3487 /* 570, 600e/x, 770e, 770x */
3488 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3490 /* all other ThinkPads: note that even old-style
3491 * ThinkPad ECs supports the fan control register */
3492 if (likely(acpi_ec_read(fan_status_offset,
3493 &fan_control_initial_status))) {
3494 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3496 /* In some ThinkPads, neither the EC nor the ACPI
3497 * DSDT initialize the fan status, and it ends up
3498 * being set to 0x07 when it *could* be either
3501 * Enable for TP-1Y (T43), TP-78 (R51e),
3502 * TP-76 (R52), TP-70 (T43, R52), which are known
3504 if (fan_control_initial_status == 0x07 &&
3505 ibm_thinkpad_ec_found &&
3506 ((ibm_thinkpad_ec_found[0] == '1' &&
3507 ibm_thinkpad_ec_found[1] == 'Y') ||
3508 (ibm_thinkpad_ec_found[0] == '7' &&
3509 (ibm_thinkpad_ec_found[1] == '6' ||
3510 ibm_thinkpad_ec_found[1] == '8' ||
3511 ibm_thinkpad_ec_found[1] == '0'))
3514 "fan_init: initial fan status is "
3515 "unknown, assuming it is in auto "
3517 tp_features.fan_ctrl_status_undef = 1;
3521 "ThinkPad ACPI EC access misbehaving, "
3522 "fan status and control unavailable\n");
3529 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3530 fan_control_commands |=
3531 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3534 /* gfan without sfan means no fan control */
3535 /* all other models implement TP EC 0x2f control */
3539 fan_control_access_mode =
3540 TPACPI_FAN_WR_ACPI_FANS;
3541 fan_control_commands |=
3542 TPACPI_FAN_CMD_SPEED |
3543 TPACPI_FAN_CMD_LEVEL |
3544 TPACPI_FAN_CMD_ENABLE;
3546 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3547 fan_control_commands |=
3548 TPACPI_FAN_CMD_LEVEL |
3549 TPACPI_FAN_CMD_ENABLE;
3554 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3555 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3556 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3557 fan_status_access_mode, fan_control_access_mode);
3559 /* fan control master switch */
3560 if (!fan_control_allowed) {
3561 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3562 fan_control_commands = 0;
3563 dbg_printk(TPACPI_DBG_INIT,
3564 "fan control features disabled by parameter\n");
3567 /* update fan_control_desired_level */
3568 if (fan_status_access_mode != TPACPI_FAN_NONE)
3569 fan_get_status_safe(NULL);
3571 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3572 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3573 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3576 rc = driver_create_file(&tpacpi_pdriver.driver,
3577 &driver_attr_fan_watchdog);
3586 * Call with fan_mutex held
3588 static void fan_update_desired_level(u8 status)
3591 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3593 fan_control_desired_level = 7;
3595 fan_control_desired_level = status;
3599 static int fan_get_status(u8 *status)
3604 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3606 switch (fan_status_access_mode) {
3607 case TPACPI_FAN_RD_ACPI_GFAN:
3608 /* 570, 600e/x, 770e, 770x */
3610 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3618 case TPACPI_FAN_RD_TPEC:
3619 /* all except 570, 600e/x, 770e, 770x */
3620 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3635 static int fan_get_status_safe(u8 *status)
3640 rc = mutex_lock_interruptible(&fan_mutex);
3643 rc = fan_get_status(&s);
3645 fan_update_desired_level(s);
3646 mutex_unlock(&fan_mutex);
3654 static void fan_exit(void)
3656 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3658 /* FIXME: can we really do this unconditionally? */
3659 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3660 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3662 cancel_delayed_work(&fan_watchdog_task);
3663 flush_scheduled_work();
3666 static int fan_get_speed(unsigned int *speed)
3670 switch (fan_status_access_mode) {
3671 case TPACPI_FAN_RD_TPEC:
3672 /* all except 570, 600e/x, 770e, 770x */
3673 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3674 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3678 *speed = (hi << 8) | lo;
3689 static void fan_watchdog_fire(struct work_struct *ignored)
3693 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3694 rc = fan_set_enable();
3696 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3697 "will try again later...\n", -rc);
3698 /* reschedule for later */
3699 fan_watchdog_reset();
3703 static void fan_watchdog_reset(void)
3705 static int fan_watchdog_active;
3707 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3710 if (fan_watchdog_active)
3711 cancel_delayed_work(&fan_watchdog_task);
3713 if (fan_watchdog_maxinterval > 0) {
3714 fan_watchdog_active = 1;
3715 if (!schedule_delayed_work(&fan_watchdog_task,
3716 msecs_to_jiffies(fan_watchdog_maxinterval
3718 printk(IBM_ERR "failed to schedule the fan watchdog, "
3719 "watchdog will not trigger\n");
3722 fan_watchdog_active = 0;
3725 static int fan_set_level(int level)
3727 if (!fan_control_allowed)
3730 switch (fan_control_access_mode) {
3731 case TPACPI_FAN_WR_ACPI_SFAN:
3732 if (level >= 0 && level <= 7) {
3733 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3739 case TPACPI_FAN_WR_ACPI_FANS:
3740 case TPACPI_FAN_WR_TPEC:
3741 if ((level != TP_EC_FAN_AUTO) &&
3742 (level != TP_EC_FAN_FULLSPEED) &&
3743 ((level < 0) || (level > 7)))
3746 /* safety net should the EC not support AUTO
3747 * or FULLSPEED mode bits and just ignore them */
3748 if (level & TP_EC_FAN_FULLSPEED)
3749 level |= 7; /* safety min speed 7 */
3750 else if (level & TP_EC_FAN_FULLSPEED)
3751 level |= 4; /* safety min speed 4 */
3753 if (!acpi_ec_write(fan_status_offset, level))
3756 tp_features.fan_ctrl_status_undef = 0;
3765 static int fan_set_level_safe(int level)
3769 if (!fan_control_allowed)
3772 rc = mutex_lock_interruptible(&fan_mutex);
3776 if (level == TPACPI_FAN_LAST_LEVEL)
3777 level = fan_control_desired_level;
3779 rc = fan_set_level(level);
3781 fan_update_desired_level(level);
3783 mutex_unlock(&fan_mutex);
3787 static int fan_set_enable(void)
3792 if (!fan_control_allowed)
3795 rc = mutex_lock_interruptible(&fan_mutex);
3799 switch (fan_control_access_mode) {
3800 case TPACPI_FAN_WR_ACPI_FANS:
3801 case TPACPI_FAN_WR_TPEC:
3802 rc = fan_get_status(&s);
3806 /* Don't go out of emergency fan mode */
3809 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3812 if (!acpi_ec_write(fan_status_offset, s))
3815 tp_features.fan_ctrl_status_undef = 0;
3820 case TPACPI_FAN_WR_ACPI_SFAN:
3821 rc = fan_get_status(&s);
3827 /* Set fan to at least level 4 */
3830 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3840 mutex_unlock(&fan_mutex);
3844 static int fan_set_disable(void)
3848 if (!fan_control_allowed)
3851 rc = mutex_lock_interruptible(&fan_mutex);
3856 switch (fan_control_access_mode) {
3857 case TPACPI_FAN_WR_ACPI_FANS:
3858 case TPACPI_FAN_WR_TPEC:
3859 if (!acpi_ec_write(fan_status_offset, 0x00))
3862 fan_control_desired_level = 0;
3863 tp_features.fan_ctrl_status_undef = 0;
3867 case TPACPI_FAN_WR_ACPI_SFAN:
3868 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
3871 fan_control_desired_level = 0;
3879 mutex_unlock(&fan_mutex);
3883 static int fan_set_speed(int speed)
3887 if (!fan_control_allowed)
3890 rc = mutex_lock_interruptible(&fan_mutex);
3895 switch (fan_control_access_mode) {
3896 case TPACPI_FAN_WR_ACPI_FANS:
3897 if (speed >= 0 && speed <= 65535) {
3898 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3899 speed, speed, speed))
3909 mutex_unlock(&fan_mutex);
3913 static int fan_read(char *p)
3918 unsigned int speed = 0;
3920 switch (fan_status_access_mode) {
3921 case TPACPI_FAN_RD_ACPI_GFAN:
3922 /* 570, 600e/x, 770e, 770x */
3923 if ((rc = fan_get_status_safe(&status)) < 0)
3926 len += sprintf(p + len, "status:\t\t%s\n"
3928 (status != 0) ? "enabled" : "disabled", status);
3931 case TPACPI_FAN_RD_TPEC:
3932 /* all except 570, 600e/x, 770e, 770x */
3933 if ((rc = fan_get_status_safe(&status)) < 0)
3936 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3937 if (status != fan_control_initial_status)
3938 tp_features.fan_ctrl_status_undef = 0;
3940 /* Return most likely status. In fact, it
3941 * might be the only possible status */
3942 status = TP_EC_FAN_AUTO;
3945 len += sprintf(p + len, "status:\t\t%s\n",
3946 (status != 0) ? "enabled" : "disabled");
3948 if ((rc = fan_get_speed(&speed)) < 0)
3951 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3953 if (status & TP_EC_FAN_FULLSPEED)
3954 /* Disengaged mode takes precedence */
3955 len += sprintf(p + len, "level:\t\tdisengaged\n");
3956 else if (status & TP_EC_FAN_AUTO)
3957 len += sprintf(p + len, "level:\t\tauto\n");
3959 len += sprintf(p + len, "level:\t\t%d\n", status);
3962 case TPACPI_FAN_NONE:
3964 len += sprintf(p + len, "status:\t\tnot supported\n");
3967 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
3968 len += sprintf(p + len, "commands:\tlevel <level>");
3970 switch (fan_control_access_mode) {
3971 case TPACPI_FAN_WR_ACPI_SFAN:
3972 len += sprintf(p + len, " (<level> is 0-7)\n");
3976 len += sprintf(p + len, " (<level> is 0-7, "
3977 "auto, disengaged, full-speed)\n");
3982 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
3983 len += sprintf(p + len, "commands:\tenable, disable\n"
3984 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3985 "1-120 (seconds))\n");
3987 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
3988 len += sprintf(p + len, "commands:\tspeed <speed>"
3989 " (<speed> is 0-65535)\n");
3994 static int fan_write_cmd_level(const char *cmd, int *rc)
3998 if (strlencmp(cmd, "level auto") == 0)
3999 level = TP_EC_FAN_AUTO;
4000 else if ((strlencmp(cmd, "level disengaged") == 0) |
4001 (strlencmp(cmd, "level full-speed") == 0))
4002 level = TP_EC_FAN_FULLSPEED;
4003 else if (sscanf(cmd, "level %d", &level) != 1)
4006 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4007 printk(IBM_ERR "level command accepted for unsupported "
4008 "access mode %d", fan_control_access_mode);
4013 static int fan_write_cmd_enable(const char *cmd, int *rc)
4015 if (strlencmp(cmd, "enable") != 0)
4018 if ((*rc = fan_set_enable()) == -ENXIO)
4019 printk(IBM_ERR "enable command accepted for unsupported "
4020 "access mode %d", fan_control_access_mode);
4025 static int fan_write_cmd_disable(const char *cmd, int *rc)
4027 if (strlencmp(cmd, "disable") != 0)
4030 if ((*rc = fan_set_disable()) == -ENXIO)
4031 printk(IBM_ERR "disable command accepted for unsupported "
4032 "access mode %d", fan_control_access_mode);
4037 static int fan_write_cmd_speed(const char *cmd, int *rc)
4042 * Support speed <low> <medium> <high> ? */
4044 if (sscanf(cmd, "speed %d", &speed) != 1)
4047 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4048 printk(IBM_ERR "speed command accepted for unsupported "
4049 "access mode %d", fan_control_access_mode);
4054 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4058 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4061 if (interval < 0 || interval > 120)
4064 fan_watchdog_maxinterval = interval;
4069 static int fan_write(char *buf)
4074 while (!rc && (cmd = next_cmd(&buf))) {
4075 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4076 fan_write_cmd_level(cmd, &rc)) &&
4077 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4078 (fan_write_cmd_enable(cmd, &rc) ||
4079 fan_write_cmd_disable(cmd, &rc) ||
4080 fan_write_cmd_watchdog(cmd, &rc))) &&
4081 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4082 fan_write_cmd_speed(cmd, &rc))
4086 fan_watchdog_reset();
4092 static struct ibm_struct fan_driver_data = {
4099 /****************************************************************************
4100 ****************************************************************************
4104 ****************************************************************************
4105 ****************************************************************************/
4108 static struct proc_dir_entry *proc_dir;
4110 /* Subdriver registry */
4111 static LIST_HEAD(tpacpi_all_drivers);
4115 * Module and infrastructure proble, init and exit handling
4118 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4119 static const char * __init str_supported(int is_supported)
4121 static char text_unsupported[] __initdata = "not supported";
4123 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4125 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4127 static int __init ibm_init(struct ibm_init_struct *iibm)
4130 struct ibm_struct *ibm = iibm->data;
4131 struct proc_dir_entry *entry;
4133 BUG_ON(ibm == NULL);
4135 INIT_LIST_HEAD(&ibm->all_drivers);
4137 if (ibm->flags.experimental && !experimental)
4140 dbg_printk(TPACPI_DBG_INIT,
4141 "probing for %s\n", ibm->name);
4144 ret = iibm->init(iibm);
4146 return 0; /* probe failed */
4150 ibm->flags.init_called = 1;
4154 if (ibm->acpi->hid) {
4155 ret = register_tpacpi_subdriver(ibm);
4160 if (ibm->acpi->notify) {
4161 ret = setup_acpi_notify(ibm);
4162 if (ret == -ENODEV) {
4163 printk(IBM_NOTICE "disabling subdriver %s\n",
4173 dbg_printk(TPACPI_DBG_INIT,
4174 "%s installed\n", ibm->name);
4177 entry = create_proc_entry(ibm->name,
4178 S_IFREG | S_IRUGO | S_IWUSR,
4181 printk(IBM_ERR "unable to create proc entry %s\n",
4186 entry->owner = THIS_MODULE;
4188 entry->read_proc = &dispatch_procfs_read;
4190 entry->write_proc = &dispatch_procfs_write;
4191 ibm->flags.proc_created = 1;
4194 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4199 dbg_printk(TPACPI_DBG_INIT,
4200 "%s: at error exit path with result %d\n",
4204 return (ret < 0)? ret : 0;
4207 static void ibm_exit(struct ibm_struct *ibm)
4209 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4211 list_del_init(&ibm->all_drivers);
4213 if (ibm->flags.acpi_notify_installed) {
4214 dbg_printk(TPACPI_DBG_EXIT,
4215 "%s: acpi_remove_notify_handler\n", ibm->name);
4217 acpi_remove_notify_handler(*ibm->acpi->handle,
4219 dispatch_acpi_notify);
4220 ibm->flags.acpi_notify_installed = 0;
4221 ibm->flags.acpi_notify_installed = 0;
4224 if (ibm->flags.proc_created) {
4225 dbg_printk(TPACPI_DBG_EXIT,
4226 "%s: remove_proc_entry\n", ibm->name);
4227 remove_proc_entry(ibm->name, proc_dir);
4228 ibm->flags.proc_created = 0;
4231 if (ibm->flags.acpi_driver_registered) {
4232 dbg_printk(TPACPI_DBG_EXIT,
4233 "%s: acpi_bus_unregister_driver\n", ibm->name);
4235 acpi_bus_unregister_driver(ibm->acpi->driver);
4236 kfree(ibm->acpi->driver);
4237 ibm->acpi->driver = NULL;
4238 ibm->flags.acpi_driver_registered = 0;
4241 if (ibm->flags.init_called && ibm->exit) {
4243 ibm->flags.init_called = 0;
4246 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4251 static char *ibm_thinkpad_ec_found;
4253 static char* __init check_dmi_for_ec(void)
4255 struct dmi_device *dev = NULL;
4256 char ec_fw_string[18];
4259 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4260 * X32 or newer, all Z series; Some models must have an
4261 * up-to-date BIOS or they will not be detected.
4263 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4265 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4266 if (sscanf(dev->name,
4267 "IBM ThinkPad Embedded Controller -[%17c",
4268 ec_fw_string) == 1) {
4269 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4270 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4271 return kstrdup(ec_fw_string, GFP_KERNEL);
4277 static int __init probe_for_thinkpad(void)
4285 * Non-ancient models have better DMI tagging, but very old models
4288 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4290 /* ec is required because many other handles are relative to it */
4291 IBM_ACPIHANDLE_INIT(ec);
4295 "Not yet supported ThinkPad detected!\n");
4300 * Risks a regression on very old machines, but reduces potential
4301 * false positives a damn great deal
4304 is_thinkpad = dmi_name_in_vendors("IBM");
4306 if (!is_thinkpad && !force_load)
4313 /* Module init, exit, parameters */
4315 static struct ibm_init_struct ibms_init[] __initdata = {
4317 .init = thinkpad_acpi_driver_init,
4318 .data = &thinkpad_acpi_driver_data,
4321 .init = hotkey_init,
4322 .data = &hotkey_driver_data,
4325 .init = bluetooth_init,
4326 .data = &bluetooth_driver_data,
4330 .data = &wan_driver_data,
4334 .data = &video_driver_data,
4338 .data = &light_driver_data,
4340 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4343 .data = &dock_driver_data[0],
4347 .data = &dock_driver_data[1],
4350 #ifdef CONFIG_THINKPAD_ACPI_BAY
4353 .data = &bay_driver_data,
4358 .data = &cmos_driver_data,
4362 .data = &led_driver_data,
4366 .data = &beep_driver_data,
4369 .init = thermal_init,
4370 .data = &thermal_driver_data,
4373 .data = &ecdump_driver_data,
4376 .init = brightness_init,
4377 .data = &brightness_driver_data,
4380 .data = &volume_driver_data,
4384 .data = &fan_driver_data,
4388 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4391 struct ibm_struct *ibm;
4393 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4394 ibm = ibms_init[i].data;
4395 BUG_ON(ibm == NULL);
4397 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4398 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4400 strcpy(ibms_init[i].param, val);
4401 strcat(ibms_init[i].param, ",");
4409 static int experimental;
4410 module_param(experimental, int, 0);
4412 static u32 dbg_level;
4413 module_param_named(debug, dbg_level, uint, 0);
4415 static int force_load;
4416 module_param(force_load, int, 0);
4418 static int fan_control_allowed;
4419 module_param_named(fan_control, fan_control_allowed, int, 0);
4421 #define IBM_PARAM(feature) \
4422 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4425 IBM_PARAM(bluetooth);
4428 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4431 #ifdef CONFIG_THINKPAD_ACPI_BAY
4433 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4438 IBM_PARAM(brightness);
4442 static int __init thinkpad_acpi_module_init(void)
4446 /* Driver-level probe */
4447 ret = probe_for_thinkpad();
4451 /* Driver initialization */
4452 ibm_thinkpad_ec_found = check_dmi_for_ec();
4453 IBM_ACPIHANDLE_INIT(ecrd);
4454 IBM_ACPIHANDLE_INIT(ecwr);
4456 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4458 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4459 thinkpad_acpi_module_exit();
4462 proc_dir->owner = THIS_MODULE;
4464 ret = platform_driver_register(&tpacpi_pdriver);
4466 printk(IBM_ERR "unable to register platform driver\n");
4467 thinkpad_acpi_module_exit();
4470 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4472 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4473 thinkpad_acpi_module_exit();
4478 /* Device initialization */
4479 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4481 if (IS_ERR(tpacpi_pdev)) {
4482 ret = PTR_ERR(tpacpi_pdev);
4484 printk(IBM_ERR "unable to register platform device\n");
4485 thinkpad_acpi_module_exit();
4488 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4489 if (IS_ERR(tpacpi_hwmon)) {
4490 ret = PTR_ERR(tpacpi_hwmon);
4491 tpacpi_hwmon = NULL;
4492 printk(IBM_ERR "unable to register hwmon device\n");
4493 thinkpad_acpi_module_exit();
4496 tpacpi_inputdev = input_allocate_device();
4497 if (!tpacpi_inputdev) {
4498 printk(IBM_ERR "unable to allocate input device\n");
4499 thinkpad_acpi_module_exit();
4502 /* Prepare input device, but don't register */
4503 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4504 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4505 tpacpi_inputdev->id.bustype = BUS_HOST;
4506 tpacpi_inputdev->id.vendor = TPACPI_HKEY_INPUT_VENDOR;
4507 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4508 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4510 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4511 ret = ibm_init(&ibms_init[i]);
4512 if (ret >= 0 && *ibms_init[i].param)
4513 ret = ibms_init[i].data->write(ibms_init[i].param);
4515 thinkpad_acpi_module_exit();
4519 ret = input_register_device(tpacpi_inputdev);
4521 printk(IBM_ERR "unable to register input device\n");
4522 thinkpad_acpi_module_exit();
4525 tp_features.input_device_registered = 1;
4531 static void thinkpad_acpi_module_exit(void)
4533 struct ibm_struct *ibm, *itmp;
4535 list_for_each_entry_safe_reverse(ibm, itmp,
4536 &tpacpi_all_drivers,
4541 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4543 if (tpacpi_inputdev) {
4544 if (tp_features.input_device_registered)
4545 input_unregister_device(tpacpi_inputdev);
4547 input_free_device(tpacpi_inputdev);
4551 hwmon_device_unregister(tpacpi_hwmon);
4554 platform_device_unregister(tpacpi_pdev);
4556 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4557 platform_driver_unregister(&tpacpi_pdriver);
4560 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4562 kfree(ibm_thinkpad_ec_found);
4565 module_init(thinkpad_acpi_module_init);
4566 module_exit(thinkpad_acpi_module_exit);