2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 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 TPACPI_VERSION "0.19"
25 #define TPACPI_SYSFS_VERSION 0x020200
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <asm/uaccess.h>
72 #include <linux/dmi.h>
73 #include <linux/jiffies.h>
74 #include <linux/workqueue.h>
76 #include <acpi/acpi_drivers.h>
77 #include <acpi/acnamesp.h>
79 #include <linux/pci_ids.h>
82 /* ThinkPad CMOS commands */
83 #define TP_CMOS_VOLUME_DOWN 0
84 #define TP_CMOS_VOLUME_UP 1
85 #define TP_CMOS_VOLUME_MUTE 2
86 #define TP_CMOS_BRIGHTNESS_UP 4
87 #define TP_CMOS_BRIGHTNESS_DOWN 5
91 TP_NVRAM_ADDR_HK2 = 0x57,
92 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
93 TP_NVRAM_ADDR_VIDEO = 0x59,
94 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
95 TP_NVRAM_ADDR_MIXER = 0x60,
100 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
101 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
102 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
103 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
104 TP_NVRAM_MASK_THINKLIGHT = 0x10,
105 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
106 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
107 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
108 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
109 TP_NVRAM_MASK_MUTE = 0x40,
110 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
111 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
112 TP_NVRAM_POS_LEVEL_VOLUME = 0,
116 #define TPACPI_ACPI_HKEY_HID "IBM0068"
119 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
120 #define TPACPI_HKEY_INPUT_VERSION 0x4101
123 /****************************************************************************
127 #define TPACPI_NAME "thinkpad"
128 #define TPACPI_DESC "ThinkPad ACPI Extras"
129 #define TPACPI_FILE TPACPI_NAME "_acpi"
130 #define TPACPI_URL "http://ibm-acpi.sf.net/"
131 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
133 #define TPACPI_PROC_DIR "ibm"
134 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
135 #define TPACPI_DRVR_NAME TPACPI_FILE
136 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
138 #define TPACPI_MAX_ACPI_ARGS 3
141 #define TPACPI_LOG TPACPI_FILE ": "
142 #define TPACPI_ERR KERN_ERR TPACPI_LOG
143 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
144 #define TPACPI_INFO KERN_INFO TPACPI_LOG
145 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
147 #define TPACPI_DBG_ALL 0xffff
148 #define TPACPI_DBG_ALL 0xffff
149 #define TPACPI_DBG_INIT 0x0001
150 #define TPACPI_DBG_EXIT 0x0002
151 #define dbg_printk(a_dbg_level, format, arg...) \
152 do { if (dbg_level & a_dbg_level) \
153 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
155 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
156 #define vdbg_printk(a_dbg_level, format, arg...) \
157 dbg_printk(a_dbg_level, format, ## arg)
158 static const char *str_supported(int is_supported);
160 #define vdbg_printk(a_dbg_level, format, arg...)
163 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
164 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
165 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
168 /****************************************************************************
169 * Driver-wide structs and misc. variables
174 struct tp_acpi_drv_struct {
175 const struct acpi_device_id *hid;
176 struct acpi_driver *driver;
178 void (*notify) (struct ibm_struct *, u32);
181 struct acpi_device *device;
187 int (*read) (char *);
188 int (*write) (char *);
190 void (*resume) (void);
191 void (*suspend) (pm_message_t state);
193 struct list_head all_drivers;
195 struct tp_acpi_drv_struct *acpi;
198 u8 acpi_driver_registered:1;
199 u8 acpi_notify_installed:1;
206 struct ibm_init_struct {
209 int (*init) (struct ibm_init_struct *);
210 struct ibm_struct *data;
214 #ifdef CONFIG_THINKPAD_ACPI_BAY
226 u32 bright_16levels:1;
228 u32 fan_ctrl_status_undef:1;
229 u32 input_device_registered:1;
230 u32 platform_drv_registered:1;
231 u32 platform_drv_attrs_registered:1;
232 u32 sensors_pdrv_registered:1;
233 u32 sensors_pdrv_attrs_registered:1;
234 u32 sensors_pdev_attrs_registered:1;
235 u32 hotkey_poll_active:1;
238 struct thinkpad_id_data {
239 unsigned int vendor; /* ThinkPad vendor:
240 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
242 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
243 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
245 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
250 static struct thinkpad_id_data thinkpad_id;
253 TPACPI_LIFE_INIT = 0,
258 static int experimental;
259 static u32 dbg_level;
261 /****************************************************************************
262 ****************************************************************************
264 * ACPI Helpers and device model
266 ****************************************************************************
267 ****************************************************************************/
269 /*************************************************************************
273 static acpi_handle root_handle;
275 #define TPACPI_HANDLE(object, parent, paths...) \
276 static acpi_handle object##_handle; \
277 static acpi_handle *object##_parent = &parent##_handle; \
278 static char *object##_path; \
279 static char *object##_paths[] = { paths }
281 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
282 "\\_SB.PCI.ISA.EC", /* 570 */
283 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
284 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
285 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
286 "\\_SB.PCI0.ICH3.EC0", /* R31 */
287 "\\_SB.PCI0.LPC.EC", /* all others */
290 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
291 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
293 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
295 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
296 "\\CMS", /* R40, R40e */
299 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
300 "^HKEY", /* R30, R31 */
301 "HKEY", /* all others */
305 /*************************************************************************
309 static int acpi_evalf(acpi_handle handle,
310 void *res, char *method, char *fmt, ...)
313 struct acpi_object_list params;
314 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
315 struct acpi_buffer result, *resultp;
316 union acpi_object out_obj;
324 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
337 params.pointer = &in_objs[0];
344 in_objs[params.count].integer.value = va_arg(ap, int);
345 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
347 /* add more types as needed */
349 printk(TPACPI_ERR "acpi_evalf() called "
350 "with invalid format character '%c'\n", c);
356 if (res_type != 'v') {
357 result.length = sizeof(out_obj);
358 result.pointer = &out_obj;
363 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
368 *(int *)res = out_obj.integer.value;
369 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
372 success = status == AE_OK;
374 /* add more types as needed */
376 printk(TPACPI_ERR "acpi_evalf() called "
377 "with invalid format character '%c'\n", res_type);
381 if (!success && !quiet)
382 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
383 method, fmt0, status);
388 static int acpi_ec_read(int i, u8 *p)
393 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
397 if (ec_read(i, p) < 0)
404 static int acpi_ec_write(int i, u8 v)
407 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
410 if (ec_write(i, v) < 0)
417 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
418 static int _sta(acpi_handle handle)
422 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
429 static int issue_thinkpad_cmos_command(int cmos_cmd)
434 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
440 /*************************************************************************
444 #define TPACPI_ACPIHANDLE_INIT(object) \
445 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
446 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
448 static void drv_acpi_handle_init(char *name,
449 acpi_handle *handle, acpi_handle parent,
450 char **paths, int num_paths, char **path)
455 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
458 for (i = 0; i < num_paths; i++) {
459 status = acpi_get_handle(parent, paths[i], handle);
460 if (ACPI_SUCCESS(status)) {
462 dbg_printk(TPACPI_DBG_INIT,
463 "Found ACPI handle %s for %s\n",
469 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
474 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
476 struct ibm_struct *ibm = data;
478 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
481 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
484 ibm->acpi->notify(ibm, event);
487 static int __init setup_acpi_notify(struct ibm_struct *ibm)
494 if (!*ibm->acpi->handle)
497 vdbg_printk(TPACPI_DBG_INIT,
498 "setting up ACPI notify for %s\n", ibm->name);
500 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
502 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
507 acpi_driver_data(ibm->acpi->device) = ibm;
508 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
509 TPACPI_ACPI_EVENT_PREFIX,
512 status = acpi_install_notify_handler(*ibm->acpi->handle,
513 ibm->acpi->type, dispatch_acpi_notify, ibm);
514 if (ACPI_FAILURE(status)) {
515 if (status == AE_ALREADY_EXISTS) {
517 "another device driver is already "
518 "handling %s events\n", ibm->name);
521 "acpi_install_notify_handler(%s) failed: %d\n",
526 ibm->flags.acpi_notify_installed = 1;
530 static int __init tpacpi_device_add(struct acpi_device *device)
535 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
539 dbg_printk(TPACPI_DBG_INIT,
540 "registering %s as an ACPI driver\n", ibm->name);
544 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
545 if (!ibm->acpi->driver) {
546 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
550 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
551 ibm->acpi->driver->ids = ibm->acpi->hid;
553 ibm->acpi->driver->ops.add = &tpacpi_device_add;
555 rc = acpi_bus_register_driver(ibm->acpi->driver);
557 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
559 kfree(ibm->acpi->driver);
560 ibm->acpi->driver = NULL;
562 ibm->flags.acpi_driver_registered = 1;
568 /****************************************************************************
569 ****************************************************************************
573 ****************************************************************************
574 ****************************************************************************/
576 static int dispatch_procfs_read(char *page, char **start, off_t off,
577 int count, int *eof, void *data)
579 struct ibm_struct *ibm = data;
582 if (!ibm || !ibm->read)
585 len = ibm->read(page);
589 if (len <= off + count)
601 static int dispatch_procfs_write(struct file *file,
602 const char __user *userbuf,
603 unsigned long count, void *data)
605 struct ibm_struct *ibm = data;
609 if (!ibm || !ibm->write)
612 kernbuf = kmalloc(count + 2, GFP_KERNEL);
616 if (copy_from_user(kernbuf, userbuf, count)) {
622 strcat(kernbuf, ",");
623 ret = ibm->write(kernbuf);
632 static char *next_cmd(char **cmds)
637 while ((end = strchr(start, ',')) && end == start)
649 /****************************************************************************
650 ****************************************************************************
652 * Device model: input, hwmon and platform
654 ****************************************************************************
655 ****************************************************************************/
657 static struct platform_device *tpacpi_pdev;
658 static struct platform_device *tpacpi_sensors_pdev;
659 static struct device *tpacpi_hwmon;
660 static struct input_dev *tpacpi_inputdev;
661 static struct mutex tpacpi_inputdev_send_mutex;
662 static LIST_HEAD(tpacpi_all_drivers);
664 static int tpacpi_suspend_handler(struct platform_device *pdev,
667 struct ibm_struct *ibm, *itmp;
669 list_for_each_entry_safe(ibm, itmp,
673 (ibm->suspend)(state);
679 static int tpacpi_resume_handler(struct platform_device *pdev)
681 struct ibm_struct *ibm, *itmp;
683 list_for_each_entry_safe(ibm, itmp,
693 static struct platform_driver tpacpi_pdriver = {
695 .name = TPACPI_DRVR_NAME,
696 .owner = THIS_MODULE,
698 .suspend = tpacpi_suspend_handler,
699 .resume = tpacpi_resume_handler,
702 static struct platform_driver tpacpi_hwmon_pdriver = {
704 .name = TPACPI_HWMON_DRVR_NAME,
705 .owner = THIS_MODULE,
709 /*************************************************************************
710 * sysfs support helpers
713 struct attribute_set {
714 unsigned int members, max_members;
715 struct attribute_group group;
718 struct attribute_set_obj {
719 struct attribute_set s;
721 } __attribute__((packed));
723 static struct attribute_set *create_attr_set(unsigned int max_members,
726 struct attribute_set_obj *sobj;
728 if (max_members == 0)
731 /* Allocates space for implicit NULL at the end too */
732 sobj = kzalloc(sizeof(struct attribute_set_obj) +
733 max_members * sizeof(struct attribute *),
737 sobj->s.max_members = max_members;
738 sobj->s.group.attrs = &sobj->a;
739 sobj->s.group.name = name;
744 #define destroy_attr_set(_set) \
747 /* not multi-threaded safe, use it in a single thread per set */
748 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
753 if (s->members >= s->max_members)
756 s->group.attrs[s->members] = attr;
762 static int add_many_to_attr_set(struct attribute_set *s,
763 struct attribute **attr,
768 for (i = 0; i < count; i++) {
769 res = add_to_attr_set(s, attr[i]);
777 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
779 sysfs_remove_group(kobj, &s->group);
783 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
784 sysfs_create_group(_kobj, &_attr_set->group)
786 static int parse_strtoul(const char *buf,
787 unsigned long max, unsigned long *value)
791 while (*buf && isspace(*buf))
793 *value = simple_strtoul(buf, &endp, 0);
794 while (*endp && isspace(*endp))
796 if (*endp || *value > max)
802 /*************************************************************************
803 * thinkpad-acpi driver attributes
806 /* interface_version --------------------------------------------------- */
807 static ssize_t tpacpi_driver_interface_version_show(
808 struct device_driver *drv,
811 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
814 static DRIVER_ATTR(interface_version, S_IRUGO,
815 tpacpi_driver_interface_version_show, NULL);
817 /* debug_level --------------------------------------------------------- */
818 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
821 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
824 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
825 const char *buf, size_t count)
829 if (parse_strtoul(buf, 0xffff, &t))
837 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
838 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
840 /* version ------------------------------------------------------------- */
841 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
844 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
845 TPACPI_DESC, TPACPI_VERSION);
848 static DRIVER_ATTR(version, S_IRUGO,
849 tpacpi_driver_version_show, NULL);
851 /* --------------------------------------------------------------------- */
853 static struct driver_attribute *tpacpi_driver_attributes[] = {
854 &driver_attr_debug_level, &driver_attr_version,
855 &driver_attr_interface_version,
858 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
864 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
865 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
872 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
876 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
877 driver_remove_file(drv, tpacpi_driver_attributes[i]);
880 /****************************************************************************
881 ****************************************************************************
885 ****************************************************************************
886 ****************************************************************************/
888 /*************************************************************************
889 * thinkpad-acpi init subdriver
892 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
894 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
895 printk(TPACPI_INFO "%s\n", TPACPI_URL);
897 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
898 (thinkpad_id.bios_version_str) ?
899 thinkpad_id.bios_version_str : "unknown",
900 (thinkpad_id.ec_version_str) ?
901 thinkpad_id.ec_version_str : "unknown");
903 if (thinkpad_id.vendor && thinkpad_id.model_str)
904 printk(TPACPI_INFO "%s %s\n",
905 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
906 "IBM" : ((thinkpad_id.vendor ==
907 PCI_VENDOR_ID_LENOVO) ?
908 "Lenovo" : "Unknown vendor"),
909 thinkpad_id.model_str);
914 static int thinkpad_acpi_driver_read(char *p)
918 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
919 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
924 static struct ibm_struct thinkpad_acpi_driver_data = {
926 .read = thinkpad_acpi_driver_read,
929 /*************************************************************************
933 enum { /* hot key scan codes (derived from ACPI DSDT) */
934 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
935 TP_ACPI_HOTKEYSCAN_FNF2,
936 TP_ACPI_HOTKEYSCAN_FNF3,
937 TP_ACPI_HOTKEYSCAN_FNF4,
938 TP_ACPI_HOTKEYSCAN_FNF5,
939 TP_ACPI_HOTKEYSCAN_FNF6,
940 TP_ACPI_HOTKEYSCAN_FNF7,
941 TP_ACPI_HOTKEYSCAN_FNF8,
942 TP_ACPI_HOTKEYSCAN_FNF9,
943 TP_ACPI_HOTKEYSCAN_FNF10,
944 TP_ACPI_HOTKEYSCAN_FNF11,
945 TP_ACPI_HOTKEYSCAN_FNF12,
946 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
947 TP_ACPI_HOTKEYSCAN_FNINSERT,
948 TP_ACPI_HOTKEYSCAN_FNDELETE,
949 TP_ACPI_HOTKEYSCAN_FNHOME,
950 TP_ACPI_HOTKEYSCAN_FNEND,
951 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
952 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
953 TP_ACPI_HOTKEYSCAN_FNSPACE,
954 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
955 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
956 TP_ACPI_HOTKEYSCAN_MUTE,
957 TP_ACPI_HOTKEYSCAN_THINKPAD,
960 enum { /* Keys available through NVRAM polling */
961 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
962 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
965 enum { /* Positions of some of the keys in hotkey masks */
966 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
967 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
968 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
969 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
970 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
971 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
972 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
973 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
974 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
975 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
976 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
979 enum { /* NVRAM to ACPI HKEY group map */
980 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
981 TP_ACPI_HKEY_ZOOM_MASK |
982 TP_ACPI_HKEY_DISPSWTCH_MASK |
983 TP_ACPI_HKEY_HIBERNATE_MASK,
984 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
985 TP_ACPI_HKEY_BRGHTDWN_MASK,
986 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
987 TP_ACPI_HKEY_VOLDWN_MASK |
988 TP_ACPI_HKEY_MUTE_MASK,
991 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
992 struct tp_nvram_state {
993 u16 thinkpad_toggle:1;
995 u16 display_toggle:1;
996 u16 thinklight_toggle:1;
997 u16 hibernate_toggle:1;
998 u16 displayexp_toggle:1;
1000 u16 brightness_toggle:1;
1001 u16 volume_toggle:1;
1004 u8 brightness_level;
1008 static struct task_struct *tpacpi_hotkey_task;
1009 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1010 static int hotkey_poll_freq = 10; /* Hz */
1011 static struct mutex hotkey_thread_mutex;
1012 static struct mutex hotkey_thread_data_mutex;
1013 static unsigned int hotkey_config_change;
1015 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1017 #define hotkey_source_mask 0U
1019 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1021 static struct mutex hotkey_mutex;
1023 static enum { /* Reasons for waking up */
1024 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1025 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1026 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1027 } hotkey_wakeup_reason;
1029 static int hotkey_autosleep_ack;
1031 static int hotkey_orig_status;
1032 static u32 hotkey_orig_mask;
1033 static u32 hotkey_all_mask;
1034 static u32 hotkey_reserved_mask;
1035 static u32 hotkey_mask;
1037 static unsigned int hotkey_report_mode;
1039 static u16 *hotkey_keycode_map;
1041 static struct attribute_set *hotkey_dev_attributes;
1043 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1044 #define HOTKEY_CONFIG_CRITICAL_START \
1046 mutex_lock(&hotkey_thread_data_mutex); \
1047 hotkey_config_change++; \
1049 #define HOTKEY_CONFIG_CRITICAL_END \
1050 mutex_unlock(&hotkey_thread_data_mutex);
1052 #define HOTKEY_CONFIG_CRITICAL_START
1053 #define HOTKEY_CONFIG_CRITICAL_END
1054 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1056 static int hotkey_get_wlsw(int *status)
1058 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1064 * Call with hotkey_mutex held
1066 static int hotkey_mask_get(void)
1070 if (tp_features.hotkey_mask) {
1071 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1074 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1080 * Call with hotkey_mutex held
1082 static int hotkey_mask_set(u32 mask)
1087 if (tp_features.hotkey_mask) {
1088 HOTKEY_CONFIG_CRITICAL_START
1089 for (i = 0; i < 32; i++) {
1091 /* enable in firmware mask only keys not in NVRAM
1092 * mode, but enable the key in the cached hotkey_mask
1093 * regardless of mode, or the key will end up
1094 * disabled by hotkey_mask_get() */
1095 if (!acpi_evalf(hkey_handle,
1096 NULL, "MHKM", "vdd", i + 1,
1097 !!((mask & ~hotkey_source_mask) & m))) {
1101 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1104 HOTKEY_CONFIG_CRITICAL_END
1106 /* hotkey_mask_get must be called unconditionally below */
1107 if (!hotkey_mask_get() && !rc &&
1108 (hotkey_mask & ~hotkey_source_mask) !=
1109 (mask & ~hotkey_source_mask)) {
1110 printk(TPACPI_NOTICE
1111 "requested hot key mask 0x%08x, but "
1112 "firmware forced it to 0x%08x\n",
1116 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1117 HOTKEY_CONFIG_CRITICAL_START
1118 hotkey_mask = mask & hotkey_source_mask;
1119 HOTKEY_CONFIG_CRITICAL_END
1121 if (hotkey_mask != mask) {
1122 printk(TPACPI_NOTICE
1123 "requested hot key mask 0x%08x, "
1124 "forced to 0x%08x (NVRAM poll mask is "
1125 "0x%08x): no firmware mask support\n",
1126 mask, hotkey_mask, hotkey_source_mask);
1131 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1137 static int hotkey_status_get(int *status)
1139 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1145 static int hotkey_status_set(int status)
1147 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1153 static void tpacpi_input_send_radiosw(void)
1157 mutex_lock(&tpacpi_inputdev_send_mutex);
1159 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1160 input_report_switch(tpacpi_inputdev,
1162 input_sync(tpacpi_inputdev);
1165 mutex_unlock(&tpacpi_inputdev_send_mutex);
1168 static void tpacpi_input_send_key(unsigned int scancode)
1170 unsigned int keycode;
1172 keycode = hotkey_keycode_map[scancode];
1174 if (keycode != KEY_RESERVED) {
1175 mutex_lock(&tpacpi_inputdev_send_mutex);
1177 input_report_key(tpacpi_inputdev, keycode, 1);
1178 if (keycode == KEY_UNKNOWN)
1179 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1181 input_sync(tpacpi_inputdev);
1183 input_report_key(tpacpi_inputdev, keycode, 0);
1184 if (keycode == KEY_UNKNOWN)
1185 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1187 input_sync(tpacpi_inputdev);
1189 mutex_unlock(&tpacpi_inputdev_send_mutex);
1193 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1194 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1196 static void tpacpi_hotkey_send_key(unsigned int scancode)
1198 tpacpi_input_send_key(scancode);
1199 if (hotkey_report_mode < 2) {
1200 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1201 0x80, 0x1001 + scancode);
1205 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1209 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1210 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1211 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1212 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1213 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1214 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1216 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1217 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1218 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1220 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1221 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1222 n->displayexp_toggle =
1223 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1225 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1226 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1227 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1228 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1229 n->brightness_toggle =
1230 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1232 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1233 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1234 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1235 >> TP_NVRAM_POS_LEVEL_VOLUME;
1236 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1237 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1241 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1243 if ((mask & (1 << __scancode)) && \
1244 oldn->__member != newn->__member) \
1245 tpacpi_hotkey_send_key(__scancode); \
1248 #define TPACPI_MAY_SEND_KEY(__scancode) \
1249 do { if (mask & (1 << __scancode)) \
1250 tpacpi_hotkey_send_key(__scancode); } while (0)
1252 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1253 struct tp_nvram_state *newn,
1256 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1257 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1258 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1259 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1261 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1263 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1266 if (oldn->volume_toggle != newn->volume_toggle) {
1267 if (oldn->mute != newn->mute) {
1268 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1270 if (oldn->volume_level > newn->volume_level) {
1271 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1272 } else if (oldn->volume_level < newn->volume_level) {
1273 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1274 } else if (oldn->mute == newn->mute) {
1275 /* repeated key presses that didn't change state */
1277 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1278 } else if (newn->volume_level != 0) {
1279 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1281 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1286 /* handle brightness */
1287 if (oldn->brightness_toggle != newn->brightness_toggle) {
1288 if (oldn->brightness_level < newn->brightness_level) {
1289 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1290 } else if (oldn->brightness_level > newn->brightness_level) {
1291 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1293 /* repeated key presses that didn't change state */
1294 if (newn->brightness_level != 0) {
1295 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1297 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1303 #undef TPACPI_COMPARE_KEY
1304 #undef TPACPI_MAY_SEND_KEY
1306 static int hotkey_kthread(void *data)
1308 struct tp_nvram_state s[2];
1310 unsigned int si, so;
1312 unsigned int change_detector, must_reset;
1314 mutex_lock(&hotkey_thread_mutex);
1316 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1325 /* Initial state for compares */
1326 mutex_lock(&hotkey_thread_data_mutex);
1327 change_detector = hotkey_config_change;
1328 mask = hotkey_source_mask & hotkey_mask;
1329 mutex_unlock(&hotkey_thread_data_mutex);
1330 hotkey_read_nvram(&s[so], mask);
1332 while (!kthread_should_stop() && hotkey_poll_freq) {
1334 t = 1000/hotkey_poll_freq;
1335 t = msleep_interruptible(t);
1336 if (unlikely(kthread_should_stop()))
1338 must_reset = try_to_freeze();
1339 if (t > 0 && !must_reset)
1342 mutex_lock(&hotkey_thread_data_mutex);
1343 if (must_reset || hotkey_config_change != change_detector) {
1344 /* forget old state on thaw or config change */
1347 change_detector = hotkey_config_change;
1349 mask = hotkey_source_mask & hotkey_mask;
1350 mutex_unlock(&hotkey_thread_data_mutex);
1353 hotkey_read_nvram(&s[si], mask);
1354 if (likely(si != so)) {
1355 hotkey_compare_and_issue_event(&s[so], &s[si],
1365 mutex_unlock(&hotkey_thread_mutex);
1369 static void hotkey_poll_stop_sync(void)
1371 if (tpacpi_hotkey_task) {
1372 if (frozen(tpacpi_hotkey_task) ||
1373 freezing(tpacpi_hotkey_task))
1374 thaw_process(tpacpi_hotkey_task);
1376 kthread_stop(tpacpi_hotkey_task);
1377 tpacpi_hotkey_task = NULL;
1378 mutex_lock(&hotkey_thread_mutex);
1379 /* at this point, the thread did exit */
1380 mutex_unlock(&hotkey_thread_mutex);
1384 /* call with hotkey_mutex held */
1385 static void hotkey_poll_setup(int may_warn)
1387 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1388 hotkey_poll_freq > 0 &&
1389 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1390 if (!tpacpi_hotkey_task) {
1391 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1394 if (IS_ERR(tpacpi_hotkey_task)) {
1395 tpacpi_hotkey_task = NULL;
1397 "could not create kernel thread "
1398 "for hotkey polling\n");
1402 hotkey_poll_stop_sync();
1404 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1405 printk(TPACPI_NOTICE
1406 "hot keys 0x%08x require polling, "
1407 "which is currently disabled\n",
1408 hotkey_source_mask);
1413 static void hotkey_poll_setup_safe(int may_warn)
1415 mutex_lock(&hotkey_mutex);
1416 hotkey_poll_setup(may_warn);
1417 mutex_unlock(&hotkey_mutex);
1420 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1422 static void hotkey_poll_setup_safe(int __unused)
1426 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1428 static int hotkey_inputdev_open(struct input_dev *dev)
1430 switch (tpacpi_lifecycle) {
1431 case TPACPI_LIFE_INIT:
1433 * hotkey_init will call hotkey_poll_setup_safe
1434 * at the appropriate moment
1437 case TPACPI_LIFE_EXITING:
1439 case TPACPI_LIFE_RUNNING:
1440 hotkey_poll_setup_safe(0);
1444 /* Should only happen if tpacpi_lifecycle is corrupt */
1449 static void hotkey_inputdev_close(struct input_dev *dev)
1451 /* disable hotkey polling when possible */
1452 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1453 hotkey_poll_setup_safe(0);
1456 /* sysfs hotkey enable ------------------------------------------------- */
1457 static ssize_t hotkey_enable_show(struct device *dev,
1458 struct device_attribute *attr,
1463 res = hotkey_status_get(&status);
1467 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1470 static ssize_t hotkey_enable_store(struct device *dev,
1471 struct device_attribute *attr,
1472 const char *buf, size_t count)
1477 if (parse_strtoul(buf, 1, &t))
1480 res = hotkey_status_set(t);
1482 return (res) ? res : count;
1485 static struct device_attribute dev_attr_hotkey_enable =
1486 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1487 hotkey_enable_show, hotkey_enable_store);
1489 /* sysfs hotkey mask --------------------------------------------------- */
1490 static ssize_t hotkey_mask_show(struct device *dev,
1491 struct device_attribute *attr,
1496 if (mutex_lock_interruptible(&hotkey_mutex))
1497 return -ERESTARTSYS;
1498 res = hotkey_mask_get();
1499 mutex_unlock(&hotkey_mutex);
1502 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1505 static ssize_t hotkey_mask_store(struct device *dev,
1506 struct device_attribute *attr,
1507 const char *buf, size_t count)
1512 if (parse_strtoul(buf, 0xffffffffUL, &t))
1515 if (mutex_lock_interruptible(&hotkey_mutex))
1516 return -ERESTARTSYS;
1518 res = hotkey_mask_set(t);
1520 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1521 hotkey_poll_setup(1);
1524 mutex_unlock(&hotkey_mutex);
1526 return (res) ? res : count;
1529 static struct device_attribute dev_attr_hotkey_mask =
1530 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1531 hotkey_mask_show, hotkey_mask_store);
1533 /* sysfs hotkey bios_enabled ------------------------------------------- */
1534 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1535 struct device_attribute *attr,
1538 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1541 static struct device_attribute dev_attr_hotkey_bios_enabled =
1542 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1544 /* sysfs hotkey bios_mask ---------------------------------------------- */
1545 static ssize_t hotkey_bios_mask_show(struct device *dev,
1546 struct device_attribute *attr,
1549 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1552 static struct device_attribute dev_attr_hotkey_bios_mask =
1553 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1555 /* sysfs hotkey all_mask ----------------------------------------------- */
1556 static ssize_t hotkey_all_mask_show(struct device *dev,
1557 struct device_attribute *attr,
1560 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1561 hotkey_all_mask | hotkey_source_mask);
1564 static struct device_attribute dev_attr_hotkey_all_mask =
1565 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1567 /* sysfs hotkey recommended_mask --------------------------------------- */
1568 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1569 struct device_attribute *attr,
1572 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1573 (hotkey_all_mask | hotkey_source_mask)
1574 & ~hotkey_reserved_mask);
1577 static struct device_attribute dev_attr_hotkey_recommended_mask =
1578 __ATTR(hotkey_recommended_mask, S_IRUGO,
1579 hotkey_recommended_mask_show, NULL);
1581 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1583 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1584 static ssize_t hotkey_source_mask_show(struct device *dev,
1585 struct device_attribute *attr,
1588 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1591 static ssize_t hotkey_source_mask_store(struct device *dev,
1592 struct device_attribute *attr,
1593 const char *buf, size_t count)
1597 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1598 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1601 if (mutex_lock_interruptible(&hotkey_mutex))
1602 return -ERESTARTSYS;
1604 HOTKEY_CONFIG_CRITICAL_START
1605 hotkey_source_mask = t;
1606 HOTKEY_CONFIG_CRITICAL_END
1608 hotkey_poll_setup(1);
1610 mutex_unlock(&hotkey_mutex);
1615 static struct device_attribute dev_attr_hotkey_source_mask =
1616 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1617 hotkey_source_mask_show, hotkey_source_mask_store);
1619 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1620 static ssize_t hotkey_poll_freq_show(struct device *dev,
1621 struct device_attribute *attr,
1624 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1627 static ssize_t hotkey_poll_freq_store(struct device *dev,
1628 struct device_attribute *attr,
1629 const char *buf, size_t count)
1633 if (parse_strtoul(buf, 25, &t))
1636 if (mutex_lock_interruptible(&hotkey_mutex))
1637 return -ERESTARTSYS;
1639 hotkey_poll_freq = t;
1641 hotkey_poll_setup(1);
1642 mutex_unlock(&hotkey_mutex);
1647 static struct device_attribute dev_attr_hotkey_poll_freq =
1648 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1649 hotkey_poll_freq_show, hotkey_poll_freq_store);
1651 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1653 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1654 static ssize_t hotkey_radio_sw_show(struct device *dev,
1655 struct device_attribute *attr,
1659 res = hotkey_get_wlsw(&s);
1663 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1666 static struct device_attribute dev_attr_hotkey_radio_sw =
1667 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1669 static void hotkey_radio_sw_notify_change(void)
1671 if (tp_features.hotkey_wlsw)
1672 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1676 /* sysfs hotkey report_mode -------------------------------------------- */
1677 static ssize_t hotkey_report_mode_show(struct device *dev,
1678 struct device_attribute *attr,
1681 return snprintf(buf, PAGE_SIZE, "%d\n",
1682 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1685 static struct device_attribute dev_attr_hotkey_report_mode =
1686 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1688 /* sysfs wakeup reason (pollable) -------------------------------------- */
1689 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1690 struct device_attribute *attr,
1693 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1696 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1697 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1699 static void hotkey_wakeup_reason_notify_change(void)
1701 if (tp_features.hotkey_mask)
1702 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1706 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1707 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1708 struct device_attribute *attr,
1711 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1714 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1715 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1716 hotkey_wakeup_hotunplug_complete_show, NULL);
1718 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1720 if (tp_features.hotkey_mask)
1721 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1722 "wakeup_hotunplug_complete");
1725 /* --------------------------------------------------------------------- */
1727 static struct attribute *hotkey_attributes[] __initdata = {
1728 &dev_attr_hotkey_enable.attr,
1729 &dev_attr_hotkey_bios_enabled.attr,
1730 &dev_attr_hotkey_report_mode.attr,
1731 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1732 &dev_attr_hotkey_mask.attr,
1733 &dev_attr_hotkey_all_mask.attr,
1734 &dev_attr_hotkey_recommended_mask.attr,
1735 &dev_attr_hotkey_source_mask.attr,
1736 &dev_attr_hotkey_poll_freq.attr,
1740 static struct attribute *hotkey_mask_attributes[] __initdata = {
1741 &dev_attr_hotkey_bios_mask.attr,
1742 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1743 &dev_attr_hotkey_mask.attr,
1744 &dev_attr_hotkey_all_mask.attr,
1745 &dev_attr_hotkey_recommended_mask.attr,
1747 &dev_attr_hotkey_wakeup_reason.attr,
1748 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1751 static int __init hotkey_init(struct ibm_init_struct *iibm)
1753 /* Requirements for changing the default keymaps:
1755 * 1. Many of the keys are mapped to KEY_RESERVED for very
1756 * good reasons. Do not change them unless you have deep
1757 * knowledge on the IBM and Lenovo ThinkPad firmware for
1758 * the various ThinkPad models. The driver behaves
1759 * differently for KEY_RESERVED: such keys have their
1760 * hot key mask *unset* in mask_recommended, and also
1761 * in the initial hot key mask programmed into the
1762 * firmware at driver load time, which means the firm-
1763 * ware may react very differently if you change them to
1766 * 2. You must be subscribed to the linux-thinkpad and
1767 * ibm-acpi-devel mailing lists, and you should read the
1768 * list archives since 2007 if you want to change the
1769 * keymaps. This requirement exists so that you will
1770 * know the past history of problems with the thinkpad-
1771 * acpi driver keymaps, and also that you will be
1772 * listening to any bug reports;
1774 * 3. Do not send thinkpad-acpi specific patches directly to
1775 * for merging, *ever*. Send them to the linux-acpi
1776 * mailinglist for comments. Merging is to be done only
1777 * through acpi-test and the ACPI maintainer.
1779 * If the above is too much to ask, don't change the keymap.
1780 * Ask the thinkpad-acpi maintainer to do it, instead.
1782 static u16 ibm_keycode_map[] __initdata = {
1783 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1784 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1785 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1786 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1788 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1789 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1790 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1791 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1793 /* brightness: firmware always reacts to them, unless
1794 * X.org did some tricks in the radeon BIOS scratch
1795 * registers of *some* models */
1796 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1797 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1799 /* Thinklight: firmware always react to it */
1800 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1802 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1803 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1805 /* Volume: firmware always react to it and reprograms
1806 * the built-in *extra* mixer. Never map it to control
1807 * another mixer by default. */
1808 KEY_RESERVED, /* 0x14: VOLUME UP */
1809 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1810 KEY_RESERVED, /* 0x16: MUTE */
1812 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1814 /* (assignments unknown, please report if found) */
1815 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1816 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1818 static u16 lenovo_keycode_map[] __initdata = {
1819 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1820 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1821 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1822 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1824 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1825 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1826 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1827 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1829 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1830 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1832 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1834 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1835 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1837 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1838 * react to it and reprograms the built-in *extra* mixer.
1839 * Never map it to control another mixer by default.
1841 * T60?, T61, R60?, R61: firmware and EC tries to send
1842 * these over the regular keyboard, so these are no-ops,
1843 * but there are still weird bugs re. MUTE, so do not
1844 * change unless you get test reports from all Lenovo
1845 * models. May cause the BIOS to interfere with the
1848 KEY_RESERVED, /* 0x14: VOLUME UP */
1849 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1850 KEY_RESERVED, /* 0x16: MUTE */
1852 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1854 /* (assignments unknown, please report if found) */
1855 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1856 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1859 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1860 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1861 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1867 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1869 BUG_ON(!tpacpi_inputdev);
1870 BUG_ON(tpacpi_inputdev->open != NULL ||
1871 tpacpi_inputdev->close != NULL);
1873 TPACPI_ACPIHANDLE_INIT(hkey);
1874 mutex_init(&hotkey_mutex);
1876 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1877 mutex_init(&hotkey_thread_mutex);
1878 mutex_init(&hotkey_thread_data_mutex);
1881 /* hotkey not supported on 570 */
1882 tp_features.hotkey = hkey_handle != NULL;
1884 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1885 str_supported(tp_features.hotkey));
1887 if (tp_features.hotkey) {
1888 hotkey_dev_attributes = create_attr_set(12, NULL);
1889 if (!hotkey_dev_attributes)
1891 res = add_many_to_attr_set(hotkey_dev_attributes,
1893 ARRAY_SIZE(hotkey_attributes));
1897 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1898 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1899 for HKEY interface version 0x100 */
1900 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1901 if ((hkeyv >> 8) != 1) {
1902 printk(TPACPI_ERR "unknown version of the "
1903 "HKEY interface: 0x%x\n", hkeyv);
1904 printk(TPACPI_ERR "please report this to %s\n",
1908 * MHKV 0x100 in A31, R40, R40e,
1909 * T4x, X31, and later
1911 tp_features.hotkey_mask = 1;
1915 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1916 str_supported(tp_features.hotkey_mask));
1918 if (tp_features.hotkey_mask) {
1919 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1922 "missing MHKA handler, "
1923 "please report this to %s\n",
1925 /* FN+F12, FN+F4, FN+F3 */
1926 hotkey_all_mask = 0x080cU;
1930 /* hotkey_source_mask *must* be zero for
1931 * the first hotkey_mask_get */
1932 res = hotkey_status_get(&hotkey_orig_status);
1933 if (!res && tp_features.hotkey_mask) {
1934 res = hotkey_mask_get();
1935 hotkey_orig_mask = hotkey_mask;
1937 res = add_many_to_attr_set(
1938 hotkey_dev_attributes,
1939 hotkey_mask_attributes,
1940 ARRAY_SIZE(hotkey_mask_attributes));
1944 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1945 if (tp_features.hotkey_mask) {
1946 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1949 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1952 vdbg_printk(TPACPI_DBG_INIT,
1953 "hotkey source mask 0x%08x, polling freq %d\n",
1954 hotkey_source_mask, hotkey_poll_freq);
1957 /* Not all thinkpads have a hardware radio switch */
1958 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1959 tp_features.hotkey_wlsw = 1;
1961 "radio switch found; radios are %s\n",
1962 enabled(status, 0));
1963 res = add_to_attr_set(hotkey_dev_attributes,
1964 &dev_attr_hotkey_radio_sw.attr);
1968 res = register_attr_set_with_sysfs(
1969 hotkey_dev_attributes,
1970 &tpacpi_pdev->dev.kobj);
1974 /* Set up key map */
1976 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1978 if (!hotkey_keycode_map) {
1980 "failed to allocate memory for key map\n");
1984 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1985 dbg_printk(TPACPI_DBG_INIT,
1986 "using Lenovo default hot key map\n");
1987 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1988 TPACPI_HOTKEY_MAP_SIZE);
1990 dbg_printk(TPACPI_DBG_INIT,
1991 "using IBM default hot key map\n");
1992 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1993 TPACPI_HOTKEY_MAP_SIZE);
1996 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1997 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1998 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1999 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2000 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2001 tpacpi_inputdev->keycode = hotkey_keycode_map;
2002 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2003 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2004 set_bit(hotkey_keycode_map[i],
2005 tpacpi_inputdev->keybit);
2007 if (i < sizeof(hotkey_reserved_mask)*8)
2008 hotkey_reserved_mask |= 1 << i;
2012 if (tp_features.hotkey_wlsw) {
2013 set_bit(EV_SW, tpacpi_inputdev->evbit);
2014 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2017 dbg_printk(TPACPI_DBG_INIT,
2018 "enabling hot key handling\n");
2019 res = hotkey_status_set(1);
2022 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2023 & ~hotkey_reserved_mask)
2024 | hotkey_orig_mask);
2025 if (res < 0 && res != -ENXIO)
2028 dbg_printk(TPACPI_DBG_INIT,
2029 "legacy hot key reporting over procfs %s\n",
2030 (hotkey_report_mode < 2) ?
2031 "enabled" : "disabled");
2033 tpacpi_inputdev->open = &hotkey_inputdev_open;
2034 tpacpi_inputdev->close = &hotkey_inputdev_close;
2036 hotkey_poll_setup_safe(1);
2037 tpacpi_input_send_radiosw();
2040 return (tp_features.hotkey)? 0 : 1;
2043 static void hotkey_exit(void)
2045 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2046 hotkey_poll_stop_sync();
2049 if (tp_features.hotkey) {
2050 dbg_printk(TPACPI_DBG_EXIT,
2051 "restoring original hot key mask\n");
2052 /* no short-circuit boolean operator below! */
2053 if ((hotkey_mask_set(hotkey_orig_mask) |
2054 hotkey_status_set(hotkey_orig_status)) != 0)
2056 "failed to restore hot key mask "
2057 "to BIOS defaults\n");
2060 if (hotkey_dev_attributes) {
2061 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2062 hotkey_dev_attributes = NULL;
2066 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2069 unsigned int scancode;
2074 if (event != 0x80) {
2076 "unknown HKEY notification event %d\n", event);
2077 /* forward it to userspace, maybe it knows how to handle it */
2078 acpi_bus_generate_netlink_event(
2079 ibm->acpi->device->pnp.device_class,
2080 ibm->acpi->device->dev.bus_id,
2086 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2087 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2100 switch (hkey >> 12) {
2102 /* 0x1000-0x1FFF: key presses */
2103 scancode = hkey & 0xfff;
2104 if (scancode > 0 && scancode < 0x21) {
2106 if (!(hotkey_source_mask & (1 << scancode))) {
2107 tpacpi_input_send_key(scancode);
2119 case 0x2304: /* suspend, undock */
2120 case 0x2404: /* hibernation, undock */
2121 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2124 case 0x2305: /* suspend, bay eject */
2125 case 0x2405: /* hibernation, bay eject */
2126 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2132 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2134 "woke up due to a hot-unplug "
2136 hotkey_wakeup_reason_notify_change();
2140 /* bay-related wakeups */
2141 if (hkey == 0x3003) {
2142 hotkey_autosleep_ack = 1;
2145 hotkey_wakeup_hotunplug_complete_notify_change();
2151 /* dock-related wakeups */
2152 if (hkey == 0x4003) {
2153 hotkey_autosleep_ack = 1;
2156 hotkey_wakeup_hotunplug_complete_notify_change();
2162 /* 0x5000-0x5FFF: human interface helpers */
2164 case 0x5010: /* Lenovo new BIOS: brightness changed */
2165 case 0x5009: /* X61t: swivel up (tablet mode) */
2166 case 0x500a: /* X61t: swivel down (normal mode) */
2167 case 0x500b: /* X61t: tablet pen inserted into bay */
2168 case 0x500c: /* X61t: tablet pen removed from bay */
2172 /* LID switch events. Do not propagate */
2180 /* 0x7000-0x7FFF: misc */
2181 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2182 tpacpi_input_send_radiosw();
2183 hotkey_radio_sw_notify_change();
2187 /* fallthrough to default */
2192 printk(TPACPI_NOTICE
2193 "unhandled HKEY event 0x%04x\n", hkey);
2197 if (!ignore_acpi_ev &&
2198 (send_acpi_ev || hotkey_report_mode < 2)) {
2199 acpi_bus_generate_proc_event(ibm->acpi->device,
2203 /* netlink events */
2204 if (!ignore_acpi_ev && send_acpi_ev) {
2205 acpi_bus_generate_netlink_event(
2206 ibm->acpi->device->pnp.device_class,
2207 ibm->acpi->device->dev.bus_id,
2213 static void hotkey_suspend(pm_message_t state)
2215 /* Do these on suspend, we get the events on early resume! */
2216 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2217 hotkey_autosleep_ack = 0;
2220 static void hotkey_resume(void)
2222 if (hotkey_mask_get())
2224 "error while trying to read hot key mask "
2226 tpacpi_input_send_radiosw();
2227 hotkey_radio_sw_notify_change();
2228 hotkey_wakeup_reason_notify_change();
2229 hotkey_wakeup_hotunplug_complete_notify_change();
2230 hotkey_poll_setup_safe(0);
2233 /* procfs -------------------------------------------------------------- */
2234 static int hotkey_read(char *p)
2239 if (!tp_features.hotkey) {
2240 len += sprintf(p + len, "status:\t\tnot supported\n");
2244 if (mutex_lock_interruptible(&hotkey_mutex))
2245 return -ERESTARTSYS;
2246 res = hotkey_status_get(&status);
2248 res = hotkey_mask_get();
2249 mutex_unlock(&hotkey_mutex);
2253 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2254 if (tp_features.hotkey_mask) {
2255 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2256 len += sprintf(p + len,
2257 "commands:\tenable, disable, reset, <mask>\n");
2259 len += sprintf(p + len, "mask:\t\tnot supported\n");
2260 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2266 static int hotkey_write(char *buf)
2272 if (!tp_features.hotkey)
2275 if (mutex_lock_interruptible(&hotkey_mutex))
2276 return -ERESTARTSYS;
2282 while ((cmd = next_cmd(&buf))) {
2283 if (strlencmp(cmd, "enable") == 0) {
2285 } else if (strlencmp(cmd, "disable") == 0) {
2287 } else if (strlencmp(cmd, "reset") == 0) {
2288 status = hotkey_orig_status;
2289 mask = hotkey_orig_mask;
2290 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2292 } else if (sscanf(cmd, "%x", &mask) == 1) {
2300 res = hotkey_status_set(status);
2302 if (!res && mask != hotkey_mask)
2303 res = hotkey_mask_set(mask);
2306 mutex_unlock(&hotkey_mutex);
2310 static const struct acpi_device_id ibm_htk_device_ids[] = {
2311 {TPACPI_ACPI_HKEY_HID, 0},
2315 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2316 .hid = ibm_htk_device_ids,
2317 .notify = hotkey_notify,
2318 .handle = &hkey_handle,
2319 .type = ACPI_DEVICE_NOTIFY,
2322 static struct ibm_struct hotkey_driver_data = {
2324 .read = hotkey_read,
2325 .write = hotkey_write,
2326 .exit = hotkey_exit,
2327 .resume = hotkey_resume,
2328 .suspend = hotkey_suspend,
2329 .acpi = &ibm_hotkey_acpidriver,
2332 /*************************************************************************
2333 * Bluetooth subdriver
2337 /* ACPI GBDC/SBDC bits */
2338 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2339 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2340 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2343 static int bluetooth_get_radiosw(void);
2344 static int bluetooth_set_radiosw(int radio_on);
2346 /* sysfs bluetooth enable ---------------------------------------------- */
2347 static ssize_t bluetooth_enable_show(struct device *dev,
2348 struct device_attribute *attr,
2353 status = bluetooth_get_radiosw();
2357 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2360 static ssize_t bluetooth_enable_store(struct device *dev,
2361 struct device_attribute *attr,
2362 const char *buf, size_t count)
2367 if (parse_strtoul(buf, 1, &t))
2370 res = bluetooth_set_radiosw(t);
2372 return (res) ? res : count;
2375 static struct device_attribute dev_attr_bluetooth_enable =
2376 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2377 bluetooth_enable_show, bluetooth_enable_store);
2379 /* --------------------------------------------------------------------- */
2381 static struct attribute *bluetooth_attributes[] = {
2382 &dev_attr_bluetooth_enable.attr,
2386 static const struct attribute_group bluetooth_attr_group = {
2387 .attrs = bluetooth_attributes,
2390 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2395 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2397 TPACPI_ACPIHANDLE_INIT(hkey);
2399 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2400 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2401 tp_features.bluetooth = hkey_handle &&
2402 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2404 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2405 str_supported(tp_features.bluetooth),
2408 if (tp_features.bluetooth) {
2409 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2410 /* no bluetooth hardware present in system */
2411 tp_features.bluetooth = 0;
2412 dbg_printk(TPACPI_DBG_INIT,
2413 "bluetooth hardware not installed\n");
2415 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2416 &bluetooth_attr_group);
2422 return (tp_features.bluetooth)? 0 : 1;
2425 static void bluetooth_exit(void)
2427 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2428 &bluetooth_attr_group);
2431 static int bluetooth_get_radiosw(void)
2435 if (!tp_features.bluetooth)
2438 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2441 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2444 static int bluetooth_set_radiosw(int radio_on)
2448 if (!tp_features.bluetooth)
2451 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2454 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2456 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2457 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2463 /* procfs -------------------------------------------------------------- */
2464 static int bluetooth_read(char *p)
2467 int status = bluetooth_get_radiosw();
2469 if (!tp_features.bluetooth)
2470 len += sprintf(p + len, "status:\t\tnot supported\n");
2472 len += sprintf(p + len, "status:\t\t%s\n",
2473 (status)? "enabled" : "disabled");
2474 len += sprintf(p + len, "commands:\tenable, disable\n");
2480 static int bluetooth_write(char *buf)
2484 if (!tp_features.bluetooth)
2487 while ((cmd = next_cmd(&buf))) {
2488 if (strlencmp(cmd, "enable") == 0) {
2489 bluetooth_set_radiosw(1);
2490 } else if (strlencmp(cmd, "disable") == 0) {
2491 bluetooth_set_radiosw(0);
2499 static struct ibm_struct bluetooth_driver_data = {
2500 .name = "bluetooth",
2501 .read = bluetooth_read,
2502 .write = bluetooth_write,
2503 .exit = bluetooth_exit,
2506 /*************************************************************************
2511 /* ACPI GWAN/SWAN bits */
2512 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2513 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2514 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2517 static int wan_get_radiosw(void);
2518 static int wan_set_radiosw(int radio_on);
2520 /* sysfs wan enable ---------------------------------------------------- */
2521 static ssize_t wan_enable_show(struct device *dev,
2522 struct device_attribute *attr,
2527 status = wan_get_radiosw();
2531 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2534 static ssize_t wan_enable_store(struct device *dev,
2535 struct device_attribute *attr,
2536 const char *buf, size_t count)
2541 if (parse_strtoul(buf, 1, &t))
2544 res = wan_set_radiosw(t);
2546 return (res) ? res : count;
2549 static struct device_attribute dev_attr_wan_enable =
2550 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2551 wan_enable_show, wan_enable_store);
2553 /* --------------------------------------------------------------------- */
2555 static struct attribute *wan_attributes[] = {
2556 &dev_attr_wan_enable.attr,
2560 static const struct attribute_group wan_attr_group = {
2561 .attrs = wan_attributes,
2564 static int __init wan_init(struct ibm_init_struct *iibm)
2569 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2571 TPACPI_ACPIHANDLE_INIT(hkey);
2573 tp_features.wan = hkey_handle &&
2574 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2576 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2577 str_supported(tp_features.wan),
2580 if (tp_features.wan) {
2581 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2582 /* no wan hardware present in system */
2583 tp_features.wan = 0;
2584 dbg_printk(TPACPI_DBG_INIT,
2585 "wan hardware not installed\n");
2587 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2594 return (tp_features.wan)? 0 : 1;
2597 static void wan_exit(void)
2599 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2603 static int wan_get_radiosw(void)
2607 if (!tp_features.wan)
2610 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2613 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2616 static int wan_set_radiosw(int radio_on)
2620 if (!tp_features.wan)
2623 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2626 status |= TP_ACPI_WANCARD_RADIOSSW;
2628 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2629 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2635 /* procfs -------------------------------------------------------------- */
2636 static int wan_read(char *p)
2639 int status = wan_get_radiosw();
2641 if (!tp_features.wan)
2642 len += sprintf(p + len, "status:\t\tnot supported\n");
2644 len += sprintf(p + len, "status:\t\t%s\n",
2645 (status)? "enabled" : "disabled");
2646 len += sprintf(p + len, "commands:\tenable, disable\n");
2652 static int wan_write(char *buf)
2656 if (!tp_features.wan)
2659 while ((cmd = next_cmd(&buf))) {
2660 if (strlencmp(cmd, "enable") == 0) {
2662 } else if (strlencmp(cmd, "disable") == 0) {
2671 static struct ibm_struct wan_driver_data = {
2676 .flags.experimental = 1,
2679 /*************************************************************************
2683 enum video_access_mode {
2684 TPACPI_VIDEO_NONE = 0,
2685 TPACPI_VIDEO_570, /* 570 */
2686 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2687 TPACPI_VIDEO_NEW, /* all others */
2690 enum { /* video status flags, based on VIDEO_570 */
2691 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2692 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2693 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2696 enum { /* TPACPI_VIDEO_570 constants */
2697 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2698 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2699 * video_status_flags */
2700 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2701 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2704 static enum video_access_mode video_supported;
2705 static int video_orig_autosw;
2707 static int video_autosw_get(void);
2708 static int video_autosw_set(int enable);
2710 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2711 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2712 "\\_SB.PCI0.VID0", /* 770e */
2713 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2714 "\\_SB.PCI0.AGP.VID", /* all others */
2717 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2719 static int __init video_init(struct ibm_init_struct *iibm)
2723 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2725 TPACPI_ACPIHANDLE_INIT(vid);
2726 TPACPI_ACPIHANDLE_INIT(vid2);
2728 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2729 /* G41, assume IVGA doesn't change */
2730 vid_handle = vid2_handle;
2733 /* video switching not supported on R30, R31 */
2734 video_supported = TPACPI_VIDEO_NONE;
2735 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2737 video_supported = TPACPI_VIDEO_570;
2738 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2739 /* 600e/x, 770e, 770x */
2740 video_supported = TPACPI_VIDEO_770;
2743 video_supported = TPACPI_VIDEO_NEW;
2745 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2746 str_supported(video_supported != TPACPI_VIDEO_NONE),
2749 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2752 static void video_exit(void)
2754 dbg_printk(TPACPI_DBG_EXIT,
2755 "restoring original video autoswitch mode\n");
2756 if (video_autosw_set(video_orig_autosw))
2757 printk(TPACPI_ERR "error while trying to restore original "
2758 "video autoswitch mode\n");
2761 static int video_outputsw_get(void)
2766 switch (video_supported) {
2767 case TPACPI_VIDEO_570:
2768 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2769 TP_ACPI_VIDEO_570_PHSCMD))
2771 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2773 case TPACPI_VIDEO_770:
2774 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2777 status |= TP_ACPI_VIDEO_S_LCD;
2778 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2781 status |= TP_ACPI_VIDEO_S_CRT;
2783 case TPACPI_VIDEO_NEW:
2784 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2785 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2788 status |= TP_ACPI_VIDEO_S_CRT;
2790 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2791 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2794 status |= TP_ACPI_VIDEO_S_LCD;
2795 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2798 status |= TP_ACPI_VIDEO_S_DVI;
2807 static int video_outputsw_set(int status)
2812 switch (video_supported) {
2813 case TPACPI_VIDEO_570:
2814 res = acpi_evalf(NULL, NULL,
2815 "\\_SB.PHS2", "vdd",
2816 TP_ACPI_VIDEO_570_PHS2CMD,
2817 status | TP_ACPI_VIDEO_570_PHS2SET);
2819 case TPACPI_VIDEO_770:
2820 autosw = video_autosw_get();
2824 res = video_autosw_set(1);
2827 res = acpi_evalf(vid_handle, NULL,
2828 "ASWT", "vdd", status * 0x100, 0);
2829 if (!autosw && video_autosw_set(autosw)) {
2831 "video auto-switch left enabled due to error\n");
2835 case TPACPI_VIDEO_NEW:
2836 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2837 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2843 return (res)? 0 : -EIO;
2846 static int video_autosw_get(void)
2850 switch (video_supported) {
2851 case TPACPI_VIDEO_570:
2852 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2855 case TPACPI_VIDEO_770:
2856 case TPACPI_VIDEO_NEW:
2857 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2867 static int video_autosw_set(int enable)
2869 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2874 static int video_outputsw_cycle(void)
2876 int autosw = video_autosw_get();
2882 switch (video_supported) {
2883 case TPACPI_VIDEO_570:
2884 res = video_autosw_set(1);
2887 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2889 case TPACPI_VIDEO_770:
2890 case TPACPI_VIDEO_NEW:
2891 res = video_autosw_set(1);
2894 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2899 if (!autosw && video_autosw_set(autosw)) {
2901 "video auto-switch left enabled due to error\n");
2905 return (res)? 0 : -EIO;
2908 static int video_expand_toggle(void)
2910 switch (video_supported) {
2911 case TPACPI_VIDEO_570:
2912 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2914 case TPACPI_VIDEO_770:
2915 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2917 case TPACPI_VIDEO_NEW:
2918 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2926 static int video_read(char *p)
2931 if (video_supported == TPACPI_VIDEO_NONE) {
2932 len += sprintf(p + len, "status:\t\tnot supported\n");
2936 status = video_outputsw_get();
2940 autosw = video_autosw_get();
2944 len += sprintf(p + len, "status:\t\tsupported\n");
2945 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2946 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2947 if (video_supported == TPACPI_VIDEO_NEW)
2948 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2949 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2950 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2951 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2952 if (video_supported == TPACPI_VIDEO_NEW)
2953 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2954 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2955 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2960 static int video_write(char *buf)
2963 int enable, disable, status;
2966 if (video_supported == TPACPI_VIDEO_NONE)
2972 while ((cmd = next_cmd(&buf))) {
2973 if (strlencmp(cmd, "lcd_enable") == 0) {
2974 enable |= TP_ACPI_VIDEO_S_LCD;
2975 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2976 disable |= TP_ACPI_VIDEO_S_LCD;
2977 } else if (strlencmp(cmd, "crt_enable") == 0) {
2978 enable |= TP_ACPI_VIDEO_S_CRT;
2979 } else if (strlencmp(cmd, "crt_disable") == 0) {
2980 disable |= TP_ACPI_VIDEO_S_CRT;
2981 } else if (video_supported == TPACPI_VIDEO_NEW &&
2982 strlencmp(cmd, "dvi_enable") == 0) {
2983 enable |= TP_ACPI_VIDEO_S_DVI;
2984 } else if (video_supported == TPACPI_VIDEO_NEW &&
2985 strlencmp(cmd, "dvi_disable") == 0) {
2986 disable |= TP_ACPI_VIDEO_S_DVI;
2987 } else if (strlencmp(cmd, "auto_enable") == 0) {
2988 res = video_autosw_set(1);
2991 } else if (strlencmp(cmd, "auto_disable") == 0) {
2992 res = video_autosw_set(0);
2995 } else if (strlencmp(cmd, "video_switch") == 0) {
2996 res = video_outputsw_cycle();
2999 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3000 res = video_expand_toggle();
3007 if (enable || disable) {
3008 status = video_outputsw_get();
3011 res = video_outputsw_set((status & ~disable) | enable);
3019 static struct ibm_struct video_driver_data = {
3022 .write = video_write,
3026 /*************************************************************************
3027 * Light (thinklight) subdriver
3030 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3031 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3033 static int __init light_init(struct ibm_init_struct *iibm)
3035 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3037 TPACPI_ACPIHANDLE_INIT(ledb);
3038 TPACPI_ACPIHANDLE_INIT(lght);
3039 TPACPI_ACPIHANDLE_INIT(cmos);
3041 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3042 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3044 if (tp_features.light)
3045 /* light status not supported on
3046 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3047 tp_features.light_status =
3048 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3050 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3051 str_supported(tp_features.light));
3053 return (tp_features.light)? 0 : 1;
3056 static int light_read(char *p)
3061 if (!tp_features.light) {
3062 len += sprintf(p + len, "status:\t\tnot supported\n");
3063 } else if (!tp_features.light_status) {
3064 len += sprintf(p + len, "status:\t\tunknown\n");
3065 len += sprintf(p + len, "commands:\ton, off\n");
3067 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3069 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3070 len += sprintf(p + len, "commands:\ton, off\n");
3076 static int light_write(char *buf)
3078 int cmos_cmd, lght_cmd;
3082 if (!tp_features.light)
3085 while ((cmd = next_cmd(&buf))) {
3086 if (strlencmp(cmd, "on") == 0) {
3089 } else if (strlencmp(cmd, "off") == 0) {
3095 success = cmos_handle ?
3096 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3097 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3105 static struct ibm_struct light_driver_data = {
3108 .write = light_write,
3111 /*************************************************************************
3115 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3117 static void dock_notify(struct ibm_struct *ibm, u32 event);
3118 static int dock_read(char *p);
3119 static int dock_write(char *buf);
3121 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3122 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3123 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3124 "\\_SB.PCI.ISA.SLCE", /* 570 */
3125 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3127 /* don't list other alternatives as we install a notify handler on the 570 */
3128 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3130 static const struct acpi_device_id ibm_pci_device_ids[] = {
3131 {PCI_ROOT_HID_STRING, 0},
3135 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3137 .notify = dock_notify,
3138 .handle = &dock_handle,
3139 .type = ACPI_SYSTEM_NOTIFY,
3142 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3143 * We just use it to get notifications of dock hotplug
3144 * in very old thinkpads */
3145 .hid = ibm_pci_device_ids,
3146 .notify = dock_notify,
3147 .handle = &pci_handle,
3148 .type = ACPI_SYSTEM_NOTIFY,
3152 static struct ibm_struct dock_driver_data[2] = {
3156 .write = dock_write,
3157 .acpi = &ibm_dock_acpidriver[0],
3161 .acpi = &ibm_dock_acpidriver[1],
3165 #define dock_docked() (_sta(dock_handle) & 1)
3167 static int __init dock_init(struct ibm_init_struct *iibm)
3169 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3171 TPACPI_ACPIHANDLE_INIT(dock);
3173 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3174 str_supported(dock_handle != NULL));
3176 return (dock_handle)? 0 : 1;
3179 static int __init dock_init2(struct ibm_init_struct *iibm)
3183 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3185 if (dock_driver_data[0].flags.acpi_driver_registered &&
3186 dock_driver_data[0].flags.acpi_notify_installed) {
3187 TPACPI_ACPIHANDLE_INIT(pci);
3188 dock2_needed = (pci_handle != NULL);
3189 vdbg_printk(TPACPI_DBG_INIT,
3190 "dock PCI handler for the TP 570 is %s\n",
3191 str_supported(dock2_needed));
3193 vdbg_printk(TPACPI_DBG_INIT,
3194 "dock subdriver part 2 not required\n");
3198 return (dock2_needed)? 0 : 1;
3201 static void dock_notify(struct ibm_struct *ibm, u32 event)
3203 int docked = dock_docked();
3204 int pci = ibm->acpi->hid && ibm->acpi->device &&
3205 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3208 if (event == 1 && !pci) /* 570 */
3209 data = 1; /* button */
3210 else if (event == 1 && pci) /* 570 */
3211 data = 3; /* dock */
3212 else if (event == 3 && docked)
3213 data = 1; /* button */
3214 else if (event == 3 && !docked)
3215 data = 2; /* undock */
3216 else if (event == 0 && docked)
3217 data = 3; /* dock */
3219 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3220 event, _sta(dock_handle));
3221 data = 0; /* unknown */
3223 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3224 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3225 ibm->acpi->device->dev.bus_id,
3229 static int dock_read(char *p)
3232 int docked = dock_docked();
3235 len += sprintf(p + len, "status:\t\tnot supported\n");
3237 len += sprintf(p + len, "status:\t\tundocked\n");
3239 len += sprintf(p + len, "status:\t\tdocked\n");
3240 len += sprintf(p + len, "commands:\tdock, undock\n");
3246 static int dock_write(char *buf)
3253 while ((cmd = next_cmd(&buf))) {
3254 if (strlencmp(cmd, "undock") == 0) {
3255 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3256 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3258 } else if (strlencmp(cmd, "dock") == 0) {
3259 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3268 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3270 /*************************************************************************
3274 #ifdef CONFIG_THINKPAD_ACPI_BAY
3276 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3277 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3278 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3279 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3280 ); /* A21e, R30, R31 */
3281 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3282 "_EJ0", /* all others */
3283 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3284 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3285 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3287 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3291 static int __init bay_init(struct ibm_init_struct *iibm)
3293 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3295 TPACPI_ACPIHANDLE_INIT(bay);
3297 TPACPI_ACPIHANDLE_INIT(bay_ej);
3298 TPACPI_ACPIHANDLE_INIT(bay2);
3300 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3302 tp_features.bay_status = bay_handle &&
3303 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3304 tp_features.bay_status2 = bay2_handle &&
3305 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3307 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3308 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3309 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3310 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3312 vdbg_printk(TPACPI_DBG_INIT,
3313 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3314 str_supported(tp_features.bay_status),
3315 str_supported(tp_features.bay_eject),
3316 str_supported(tp_features.bay_status2),
3317 str_supported(tp_features.bay_eject2));
3319 return (tp_features.bay_status || tp_features.bay_eject ||
3320 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3323 static void bay_notify(struct ibm_struct *ibm, u32 event)
3325 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3326 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3327 ibm->acpi->device->dev.bus_id,
3331 #define bay_occupied(b) (_sta(b##_handle) & 1)
3333 static int bay_read(char *p)
3336 int occupied = bay_occupied(bay);
3337 int occupied2 = bay_occupied(bay2);
3340 len += sprintf(p + len, "status:\t\t%s\n",
3341 tp_features.bay_status ?
3342 (occupied ? "occupied" : "unoccupied") :
3344 if (tp_features.bay_status2)
3345 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3346 "occupied" : "unoccupied");
3348 eject = tp_features.bay_eject && occupied;
3349 eject2 = tp_features.bay_eject2 && occupied2;
3351 if (eject && eject2)
3352 len += sprintf(p + len, "commands:\teject, eject2\n");
3354 len += sprintf(p + len, "commands:\teject\n");
3356 len += sprintf(p + len, "commands:\teject2\n");
3361 static int bay_write(char *buf)
3365 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3368 while ((cmd = next_cmd(&buf))) {
3369 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3370 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3372 } else if (tp_features.bay_eject2 &&
3373 strlencmp(cmd, "eject2") == 0) {
3374 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3383 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3384 .notify = bay_notify,
3385 .handle = &bay_handle,
3386 .type = ACPI_SYSTEM_NOTIFY,
3389 static struct ibm_struct bay_driver_data = {
3393 .acpi = &ibm_bay_acpidriver,
3396 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3398 /*************************************************************************
3402 /* sysfs cmos_command -------------------------------------------------- */
3403 static ssize_t cmos_command_store(struct device *dev,
3404 struct device_attribute *attr,
3405 const char *buf, size_t count)
3407 unsigned long cmos_cmd;
3410 if (parse_strtoul(buf, 21, &cmos_cmd))
3413 res = issue_thinkpad_cmos_command(cmos_cmd);
3414 return (res)? res : count;
3417 static struct device_attribute dev_attr_cmos_command =
3418 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3420 /* --------------------------------------------------------------------- */
3422 static int __init cmos_init(struct ibm_init_struct *iibm)
3426 vdbg_printk(TPACPI_DBG_INIT,
3427 "initializing cmos commands subdriver\n");
3429 TPACPI_ACPIHANDLE_INIT(cmos);
3431 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3432 str_supported(cmos_handle != NULL));
3434 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3438 return (cmos_handle)? 0 : 1;
3441 static void cmos_exit(void)
3443 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3446 static int cmos_read(char *p)
3450 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3451 R30, R31, T20-22, X20-21 */
3453 len += sprintf(p + len, "status:\t\tnot supported\n");
3455 len += sprintf(p + len, "status:\t\tsupported\n");
3456 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3462 static int cmos_write(char *buf)
3467 while ((cmd = next_cmd(&buf))) {
3468 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3469 cmos_cmd >= 0 && cmos_cmd <= 21) {
3474 res = issue_thinkpad_cmos_command(cmos_cmd);
3482 static struct ibm_struct cmos_driver_data = {
3485 .write = cmos_write,
3489 /*************************************************************************
3493 enum led_access_mode {
3494 TPACPI_LED_NONE = 0,
3495 TPACPI_LED_570, /* 570 */
3496 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3497 TPACPI_LED_NEW, /* all others */
3500 enum { /* For TPACPI_LED_OLD */
3501 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3502 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3503 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3506 static enum led_access_mode led_supported;
3508 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3509 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3510 /* T20-22, X20-21 */
3511 "LED", /* all others */
3514 static int __init led_init(struct ibm_init_struct *iibm)
3516 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3518 TPACPI_ACPIHANDLE_INIT(led);
3521 /* led not supported on R30, R31 */
3522 led_supported = TPACPI_LED_NONE;
3523 else if (strlencmp(led_path, "SLED") == 0)
3525 led_supported = TPACPI_LED_570;
3526 else if (strlencmp(led_path, "SYSL") == 0)
3527 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3528 led_supported = TPACPI_LED_OLD;
3531 led_supported = TPACPI_LED_NEW;
3533 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3534 str_supported(led_supported), led_supported);
3536 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3539 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3541 static int led_read(char *p)
3545 if (!led_supported) {
3546 len += sprintf(p + len, "status:\t\tnot supported\n");
3549 len += sprintf(p + len, "status:\t\tsupported\n");
3551 if (led_supported == TPACPI_LED_570) {
3554 for (i = 0; i < 8; i++) {
3555 if (!acpi_evalf(ec_handle,
3556 &status, "GLED", "dd", 1 << i))
3558 len += sprintf(p + len, "%d:\t\t%s\n",
3559 i, led_status(status));
3563 len += sprintf(p + len, "commands:\t"
3564 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3569 /* off, on, blink */
3570 static const int led_sled_arg1[] = { 0, 1, 3 };
3571 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3572 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3573 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3575 static int led_write(char *buf)
3583 while ((cmd = next_cmd(&buf))) {
3584 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3587 if (strstr(cmd, "off")) {
3589 } else if (strstr(cmd, "on")) {
3591 } else if (strstr(cmd, "blink")) {
3596 if (led_supported == TPACPI_LED_570) {
3599 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3600 led, led_sled_arg1[ind]))
3602 } else if (led_supported == TPACPI_LED_OLD) {
3603 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3605 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3607 ret = ec_write(TPACPI_LED_EC_HLBL,
3608 led * led_exp_hlbl[ind]);
3610 ret = ec_write(TPACPI_LED_EC_HLCL,
3611 led * led_exp_hlcl[ind]);
3616 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3617 led, led_led_arg1[ind]))
3625 static struct ibm_struct led_driver_data = {
3631 /*************************************************************************
3635 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3637 static int __init beep_init(struct ibm_init_struct *iibm)
3639 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3641 TPACPI_ACPIHANDLE_INIT(beep);
3643 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3644 str_supported(beep_handle != NULL));
3646 return (beep_handle)? 0 : 1;
3649 static int beep_read(char *p)
3654 len += sprintf(p + len, "status:\t\tnot supported\n");
3656 len += sprintf(p + len, "status:\t\tsupported\n");
3657 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3663 static int beep_write(char *buf)
3671 while ((cmd = next_cmd(&buf))) {
3672 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3673 beep_cmd >= 0 && beep_cmd <= 17) {
3677 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3684 static struct ibm_struct beep_driver_data = {
3687 .write = beep_write,
3690 /*************************************************************************
3694 enum thermal_access_mode {
3695 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3696 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3697 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3698 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3699 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3702 enum { /* TPACPI_THERMAL_TPEC_* */
3703 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3704 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3705 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3708 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3709 struct ibm_thermal_sensors_struct {
3710 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3713 static enum thermal_access_mode thermal_read_mode;
3715 /* idx is zero-based */
3716 static int thermal_get_sensor(int idx, s32 *value)
3722 t = TP_EC_THERMAL_TMP0;
3724 switch (thermal_read_mode) {
3725 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3726 case TPACPI_THERMAL_TPEC_16:
3727 if (idx >= 8 && idx <= 15) {
3728 t = TP_EC_THERMAL_TMP8;
3733 case TPACPI_THERMAL_TPEC_8:
3735 if (!acpi_ec_read(t + idx, &tmp))
3737 *value = tmp * 1000;
3742 case TPACPI_THERMAL_ACPI_UPDT:
3744 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3745 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3747 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3749 *value = (t - 2732) * 100;
3754 case TPACPI_THERMAL_ACPI_TMP07:
3756 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3757 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3759 if (t > 127 || t < -127)
3760 t = TP_EC_THERMAL_TMP_NA;
3766 case TPACPI_THERMAL_NONE:
3774 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3785 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3788 for (i = 0 ; i < n; i++) {
3789 res = thermal_get_sensor(i, &s->temp[i]);
3797 /* sysfs temp##_input -------------------------------------------------- */
3799 static ssize_t thermal_temp_input_show(struct device *dev,
3800 struct device_attribute *attr,
3803 struct sensor_device_attribute *sensor_attr =
3804 to_sensor_dev_attr(attr);
3805 int idx = sensor_attr->index;
3809 res = thermal_get_sensor(idx, &value);
3812 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3815 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3818 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3819 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3820 thermal_temp_input_show, NULL, _idxB)
3822 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3823 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3824 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3825 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3826 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3827 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3828 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3829 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3830 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3831 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3832 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3833 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3834 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3835 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3836 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3837 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3838 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3841 #define THERMAL_ATTRS(X) \
3842 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3844 static struct attribute *thermal_temp_input_attr[] = {
3864 static const struct attribute_group thermal_temp_input16_group = {
3865 .attrs = thermal_temp_input_attr
3868 static const struct attribute_group thermal_temp_input8_group = {
3869 .attrs = &thermal_temp_input_attr[8]
3872 #undef THERMAL_SENSOR_ATTR_TEMP
3873 #undef THERMAL_ATTRS
3875 /* --------------------------------------------------------------------- */
3877 static int __init thermal_init(struct ibm_init_struct *iibm)
3884 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3886 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3888 if (thinkpad_id.ec_model) {
3890 * Direct EC access mode: sensors at registers
3891 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3892 * non-implemented, thermal sensors return 0x80 when
3897 for (i = 0; i < 8; i++) {
3898 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3904 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3912 /* This is sheer paranoia, but we handle it anyway */
3915 "ThinkPad ACPI EC access misbehaving, "
3916 "falling back to ACPI TMPx access "
3918 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3921 "ThinkPad ACPI EC access misbehaving, "
3922 "disabling thermal sensors access\n");
3923 thermal_read_mode = TPACPI_THERMAL_NONE;
3928 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3930 } else if (acpi_tmp7) {
3931 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3932 /* 600e/x, 770e, 770x */
3933 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3935 /* Standard ACPI TMPx access, max 8 sensors */
3936 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3939 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3940 thermal_read_mode = TPACPI_THERMAL_NONE;
3943 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3944 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3947 switch (thermal_read_mode) {
3948 case TPACPI_THERMAL_TPEC_16:
3949 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3950 &thermal_temp_input16_group);
3954 case TPACPI_THERMAL_TPEC_8:
3955 case TPACPI_THERMAL_ACPI_TMP07:
3956 case TPACPI_THERMAL_ACPI_UPDT:
3957 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3958 &thermal_temp_input8_group);
3962 case TPACPI_THERMAL_NONE:
3970 static void thermal_exit(void)
3972 switch (thermal_read_mode) {
3973 case TPACPI_THERMAL_TPEC_16:
3974 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3975 &thermal_temp_input16_group);
3977 case TPACPI_THERMAL_TPEC_8:
3978 case TPACPI_THERMAL_ACPI_TMP07:
3979 case TPACPI_THERMAL_ACPI_UPDT:
3980 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3981 &thermal_temp_input16_group);
3983 case TPACPI_THERMAL_NONE:
3989 static int thermal_read(char *p)
3993 struct ibm_thermal_sensors_struct t;
3995 n = thermal_get_sensors(&t);
3996 if (unlikely(n < 0))
3999 len += sprintf(p + len, "temperatures:\t");
4002 for (i = 0; i < (n - 1); i++)
4003 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4004 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4006 len += sprintf(p + len, "not supported\n");
4011 static struct ibm_struct thermal_driver_data = {
4013 .read = thermal_read,
4014 .exit = thermal_exit,
4017 /*************************************************************************
4021 static u8 ecdump_regs[256];
4023 static int ecdump_read(char *p)
4029 len += sprintf(p + len, "EC "
4030 " +00 +01 +02 +03 +04 +05 +06 +07"
4031 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4032 for (i = 0; i < 256; i += 16) {
4033 len += sprintf(p + len, "EC 0x%02x:", i);
4034 for (j = 0; j < 16; j++) {
4035 if (!acpi_ec_read(i + j, &v))
4037 if (v != ecdump_regs[i + j])
4038 len += sprintf(p + len, " *%02x", v);
4040 len += sprintf(p + len, " %02x", v);
4041 ecdump_regs[i + j] = v;
4043 len += sprintf(p + len, "\n");
4048 /* These are way too dangerous to advertise openly... */
4050 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4051 " (<offset> is 00-ff, <value> is 00-ff)\n");
4052 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4053 " (<offset> is 00-ff, <value> is 0-255)\n");
4058 static int ecdump_write(char *buf)
4063 while ((cmd = next_cmd(&buf))) {
4064 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4066 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4070 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4071 if (!acpi_ec_write(i, v))
4080 static struct ibm_struct ecdump_driver_data = {
4082 .read = ecdump_read,
4083 .write = ecdump_write,
4084 .flags.experimental = 1,
4087 /*************************************************************************
4088 * Backlight/brightness subdriver
4091 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4093 static struct backlight_device *ibm_backlight_device;
4094 static int brightness_offset = 0x31;
4095 static int brightness_mode;
4096 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4098 static struct mutex brightness_mutex;
4101 * ThinkPads can read brightness from two places: EC 0x31, or
4102 * CMOS NVRAM byte 0x5E, bits 0-3.
4104 static int brightness_get(struct backlight_device *bd)
4106 u8 lec = 0, lcmos = 0, level = 0;
4108 if (brightness_mode & 1) {
4109 if (!acpi_ec_read(brightness_offset, &lec))
4111 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4114 if (brightness_mode & 2) {
4115 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4116 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4117 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4118 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4122 if (brightness_mode == 3 && lec != lcmos) {
4124 "CMOS NVRAM (%u) and EC (%u) do not agree "
4125 "on display brightness level\n",
4126 (unsigned int) lcmos,
4127 (unsigned int) lec);
4134 /* May return EINTR which can always be mapped to ERESTARTSYS */
4135 static int brightness_set(int value)
4137 int cmos_cmd, inc, i, res;
4140 if (value > ((tp_features.bright_16levels)? 15 : 7))
4143 res = mutex_lock_interruptible(&brightness_mutex);
4147 current_value = brightness_get(NULL);
4148 if (current_value < 0) {
4149 res = current_value;
4153 cmos_cmd = value > current_value ?
4154 TP_CMOS_BRIGHTNESS_UP :
4155 TP_CMOS_BRIGHTNESS_DOWN;
4156 inc = (value > current_value)? 1 : -1;
4159 for (i = current_value; i != value; i += inc) {
4160 if ((brightness_mode & 2) &&
4161 issue_thinkpad_cmos_command(cmos_cmd)) {
4165 if ((brightness_mode & 1) &&
4166 !acpi_ec_write(brightness_offset, i + inc)) {
4173 mutex_unlock(&brightness_mutex);
4177 /* sysfs backlight class ----------------------------------------------- */
4179 static int brightness_update_status(struct backlight_device *bd)
4181 /* it is the backlight class's job (caller) to handle
4182 * EINTR and other errors properly */
4183 return brightness_set(
4184 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4185 bd->props.power == FB_BLANK_UNBLANK) ?
4186 bd->props.brightness : 0);
4189 static struct backlight_ops ibm_backlight_data = {
4190 .get_brightness = brightness_get,
4191 .update_status = brightness_update_status,
4194 /* --------------------------------------------------------------------- */
4196 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4198 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4199 union acpi_object *obj;
4202 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4203 obj = (union acpi_object *)buffer.pointer;
4204 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4205 printk(TPACPI_ERR "Unknown BCLL data, "
4206 "please report this to %s\n", TPACPI_MAIL);
4209 rc = obj->package.count;
4215 kfree(buffer.pointer);
4219 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4220 void *context, void **rv)
4222 char name[ACPI_PATH_SEGMENT_LENGTH];
4223 struct acpi_buffer buffer = { sizeof(name), &name };
4225 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4226 !strncmp("BCLL", name, sizeof(name) - 1)) {
4227 if (tpacpi_query_bcll_levels(handle) == 16) {
4229 return AE_CTRL_TERMINATE;
4238 static int __init brightness_check_levels(void)
4241 void *found_node = NULL;
4244 TPACPI_ACPIHANDLE_INIT(vid);
4249 /* Search for a BCLL package with 16 levels */
4250 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4251 brightness_find_bcll, NULL,
4254 return (ACPI_SUCCESS(status) && found_node != NULL);
4257 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4258 void *context, void **rv)
4260 char name[ACPI_PATH_SEGMENT_LENGTH];
4261 struct acpi_buffer buffer = { sizeof(name), &name };
4263 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4264 !strncmp("_BCL", name, sizeof(name) - 1)) {
4266 return AE_CTRL_TERMINATE;
4272 static int __init brightness_check_std_acpi_support(void)
4275 void *found_node = NULL;
4278 TPACPI_ACPIHANDLE_INIT(vid);
4283 /* Search for a _BCL method, but don't execute it */
4284 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4285 brightness_find_bcl, NULL, &found_node);
4287 return (ACPI_SUCCESS(status) && found_node != NULL);
4290 static int __init brightness_init(struct ibm_init_struct *iibm)
4294 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4296 mutex_init(&brightness_mutex);
4298 if (!brightness_enable) {
4299 dbg_printk(TPACPI_DBG_INIT,
4300 "brightness support disabled by "
4301 "module parameter\n");
4303 } else if (brightness_enable > 1) {
4304 if (brightness_check_std_acpi_support()) {
4305 printk(TPACPI_NOTICE
4306 "standard ACPI backlight interface "
4307 "available, not loading native one...\n");
4312 if (!brightness_mode) {
4313 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4314 brightness_mode = 2;
4316 brightness_mode = 3;
4318 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4322 if (brightness_mode > 3)
4325 tp_features.bright_16levels =
4326 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4327 brightness_check_levels();
4329 b = brightness_get(NULL);
4333 if (tp_features.bright_16levels)
4335 "detected a 16-level brightness capable ThinkPad\n");
4337 ibm_backlight_device = backlight_device_register(
4338 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4339 &ibm_backlight_data);
4340 if (IS_ERR(ibm_backlight_device)) {
4341 printk(TPACPI_ERR "Could not register backlight device\n");
4342 return PTR_ERR(ibm_backlight_device);
4344 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4346 ibm_backlight_device->props.max_brightness =
4347 (tp_features.bright_16levels)? 15 : 7;
4348 ibm_backlight_device->props.brightness = b;
4349 backlight_update_status(ibm_backlight_device);
4354 static void brightness_exit(void)
4356 if (ibm_backlight_device) {
4357 vdbg_printk(TPACPI_DBG_EXIT,
4358 "calling backlight_device_unregister()\n");
4359 backlight_device_unregister(ibm_backlight_device);
4360 ibm_backlight_device = NULL;
4364 static int brightness_read(char *p)
4369 level = brightness_get(NULL);
4371 len += sprintf(p + len, "level:\t\tunreadable\n");
4373 len += sprintf(p + len, "level:\t\t%d\n", level);
4374 len += sprintf(p + len, "commands:\tup, down\n");
4375 len += sprintf(p + len, "commands:\tlevel <level>"
4376 " (<level> is 0-%d)\n",
4377 (tp_features.bright_16levels) ? 15 : 7);
4383 static int brightness_write(char *buf)
4388 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4390 level = brightness_get(NULL);
4394 while ((cmd = next_cmd(&buf))) {
4395 if (strlencmp(cmd, "up") == 0) {
4396 if (level < max_level)
4398 } else if (strlencmp(cmd, "down") == 0) {
4401 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4402 level >= 0 && level <= max_level) {
4409 * Now we know what the final level should be, so we try to set it.
4410 * Doing it this way makes the syscall restartable in case of EINTR
4412 rc = brightness_set(level);
4413 return (rc == -EINTR)? ERESTARTSYS : rc;
4416 static struct ibm_struct brightness_driver_data = {
4417 .name = "brightness",
4418 .read = brightness_read,
4419 .write = brightness_write,
4420 .exit = brightness_exit,
4423 /*************************************************************************
4427 static int volume_offset = 0x30;
4429 static int volume_read(char *p)
4434 if (!acpi_ec_read(volume_offset, &level)) {
4435 len += sprintf(p + len, "level:\t\tunreadable\n");
4437 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4438 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4439 len += sprintf(p + len, "commands:\tup, down, mute\n");
4440 len += sprintf(p + len, "commands:\tlevel <level>"
4441 " (<level> is 0-15)\n");
4447 static int volume_write(char *buf)
4449 int cmos_cmd, inc, i;
4451 int new_level, new_mute;
4454 while ((cmd = next_cmd(&buf))) {
4455 if (!acpi_ec_read(volume_offset, &level))
4457 new_mute = mute = level & 0x40;
4458 new_level = level = level & 0xf;
4460 if (strlencmp(cmd, "up") == 0) {
4464 new_level = level == 15 ? 15 : level + 1;
4465 } else if (strlencmp(cmd, "down") == 0) {
4469 new_level = level == 0 ? 0 : level - 1;
4470 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4471 new_level >= 0 && new_level <= 15) {
4473 } else if (strlencmp(cmd, "mute") == 0) {
4478 if (new_level != level) {
4479 /* mute doesn't change */
4481 cmos_cmd = (new_level > level) ?
4482 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4483 inc = new_level > level ? 1 : -1;
4485 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4486 !acpi_ec_write(volume_offset, level)))
4489 for (i = level; i != new_level; i += inc)
4490 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4491 !acpi_ec_write(volume_offset, i + inc))
4495 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4496 !acpi_ec_write(volume_offset, new_level + mute))) {
4501 if (new_mute != mute) {
4502 /* level doesn't change */
4504 cmos_cmd = (new_mute) ?
4505 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4507 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4508 !acpi_ec_write(volume_offset, level + new_mute))
4516 static struct ibm_struct volume_driver_data = {
4518 .read = volume_read,
4519 .write = volume_write,
4522 /*************************************************************************
4529 * TPACPI_FAN_RD_ACPI_GFAN:
4530 * ACPI GFAN method: returns fan level
4532 * see TPACPI_FAN_WR_ACPI_SFAN
4533 * EC 0x2f (HFSP) not available if GFAN exists
4535 * TPACPI_FAN_WR_ACPI_SFAN:
4536 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4538 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4541 * TPACPI_FAN_WR_TPEC:
4542 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4543 * Supported on almost all ThinkPads
4545 * Fan speed changes of any sort (including those caused by the
4546 * disengaged mode) are usually done slowly by the firmware as the
4547 * maximum ammount of fan duty cycle change per second seems to be
4550 * Reading is not available if GFAN exists.
4551 * Writing is not available if SFAN exists.
4554 * 7 automatic mode engaged;
4555 * (default operation mode of the ThinkPad)
4556 * fan level is ignored in this mode.
4557 * 6 full speed mode (takes precedence over bit 7);
4558 * not available on all thinkpads. May disable
4559 * the tachometer while the fan controller ramps up
4560 * the speed (which can take up to a few *minutes*).
4561 * Speeds up fan to 100% duty-cycle, which is far above
4562 * the standard RPM levels. It is not impossible that
4563 * it could cause hardware damage.
4564 * 5-3 unused in some models. Extra bits for fan level
4565 * in others, but still useless as all values above
4566 * 7 map to the same speed as level 7 in these models.
4567 * 2-0 fan level (0..7 usually)
4569 * 0x07 = max (set when temperatures critical)
4570 * Some ThinkPads may have other levels, see
4571 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4573 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4574 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4575 * does so, its initial value is meaningless (0x07).
4577 * For firmware bugs, refer to:
4578 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4582 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4583 * Main fan tachometer reading (in RPM)
4585 * This register is present on all ThinkPads with a new-style EC, and
4586 * it is known not to be present on the A21m/e, and T22, as there is
4587 * something else in offset 0x84 according to the ACPI DSDT. Other
4588 * ThinkPads from this same time period (and earlier) probably lack the
4589 * tachometer as well.
4591 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4592 * was never fixed by IBM to report the EC firmware version string
4593 * probably support the tachometer (like the early X models), so
4594 * detecting it is quite hard. We need more data to know for sure.
4596 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4599 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4602 * For firmware bugs, refer to:
4603 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4605 * TPACPI_FAN_WR_ACPI_FANS:
4606 * ThinkPad X31, X40, X41. Not available in the X60.
4608 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4609 * high speed. ACPI DSDT seems to map these three speeds to levels
4610 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4611 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4613 * The speeds are stored on handles
4614 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4616 * There are three default speed sets, acessible as handles:
4617 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4619 * ACPI DSDT switches which set is in use depending on various
4622 * TPACPI_FAN_WR_TPEC is also available and should be used to
4623 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4624 * but the ACPI tables just mention level 7.
4627 enum { /* Fan control constants */
4628 fan_status_offset = 0x2f, /* EC register 0x2f */
4629 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4630 * 0x84 must be read before 0x85 */
4632 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4633 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4635 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4638 enum fan_status_access_mode {
4639 TPACPI_FAN_NONE = 0, /* No fan status or control */
4640 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4641 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4644 enum fan_control_access_mode {
4645 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4646 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4647 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4648 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4651 enum fan_control_commands {
4652 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4653 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4654 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4655 * and also watchdog cmd */
4658 static int fan_control_allowed;
4660 static enum fan_status_access_mode fan_status_access_mode;
4661 static enum fan_control_access_mode fan_control_access_mode;
4662 static enum fan_control_commands fan_control_commands;
4664 static u8 fan_control_initial_status;
4665 static u8 fan_control_desired_level;
4666 static int fan_watchdog_maxinterval;
4668 static struct mutex fan_mutex;
4670 static void fan_watchdog_fire(struct work_struct *ignored);
4671 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4673 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4674 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4675 "\\FSPD", /* 600e/x, 770e, 770x */
4677 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4678 "JFNS", /* 770x-JL */
4682 * Call with fan_mutex held
4684 static void fan_update_desired_level(u8 status)
4687 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4689 fan_control_desired_level = 7;
4691 fan_control_desired_level = status;
4695 static int fan_get_status(u8 *status)
4700 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4702 switch (fan_status_access_mode) {
4703 case TPACPI_FAN_RD_ACPI_GFAN:
4704 /* 570, 600e/x, 770e, 770x */
4706 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4714 case TPACPI_FAN_RD_TPEC:
4715 /* all except 570, 600e/x, 770e, 770x */
4716 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4731 static int fan_get_status_safe(u8 *status)
4736 if (mutex_lock_interruptible(&fan_mutex))
4737 return -ERESTARTSYS;
4738 rc = fan_get_status(&s);
4740 fan_update_desired_level(s);
4741 mutex_unlock(&fan_mutex);
4749 static int fan_get_speed(unsigned int *speed)
4753 switch (fan_status_access_mode) {
4754 case TPACPI_FAN_RD_TPEC:
4755 /* all except 570, 600e/x, 770e, 770x */
4756 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4757 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4761 *speed = (hi << 8) | lo;
4772 static int fan_set_level(int level)
4774 if (!fan_control_allowed)
4777 switch (fan_control_access_mode) {
4778 case TPACPI_FAN_WR_ACPI_SFAN:
4779 if (level >= 0 && level <= 7) {
4780 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4786 case TPACPI_FAN_WR_ACPI_FANS:
4787 case TPACPI_FAN_WR_TPEC:
4788 if ((level != TP_EC_FAN_AUTO) &&
4789 (level != TP_EC_FAN_FULLSPEED) &&
4790 ((level < 0) || (level > 7)))
4793 /* safety net should the EC not support AUTO
4794 * or FULLSPEED mode bits and just ignore them */
4795 if (level & TP_EC_FAN_FULLSPEED)
4796 level |= 7; /* safety min speed 7 */
4797 else if (level & TP_EC_FAN_AUTO)
4798 level |= 4; /* safety min speed 4 */
4800 if (!acpi_ec_write(fan_status_offset, level))
4803 tp_features.fan_ctrl_status_undef = 0;
4812 static int fan_set_level_safe(int level)
4816 if (!fan_control_allowed)
4819 if (mutex_lock_interruptible(&fan_mutex))
4820 return -ERESTARTSYS;
4822 if (level == TPACPI_FAN_LAST_LEVEL)
4823 level = fan_control_desired_level;
4825 rc = fan_set_level(level);
4827 fan_update_desired_level(level);
4829 mutex_unlock(&fan_mutex);
4833 static int fan_set_enable(void)
4838 if (!fan_control_allowed)
4841 if (mutex_lock_interruptible(&fan_mutex))
4842 return -ERESTARTSYS;
4844 switch (fan_control_access_mode) {
4845 case TPACPI_FAN_WR_ACPI_FANS:
4846 case TPACPI_FAN_WR_TPEC:
4847 rc = fan_get_status(&s);
4851 /* Don't go out of emergency fan mode */
4854 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4857 if (!acpi_ec_write(fan_status_offset, s))
4860 tp_features.fan_ctrl_status_undef = 0;
4865 case TPACPI_FAN_WR_ACPI_SFAN:
4866 rc = fan_get_status(&s);
4872 /* Set fan to at least level 4 */
4875 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4885 mutex_unlock(&fan_mutex);
4889 static int fan_set_disable(void)
4893 if (!fan_control_allowed)
4896 if (mutex_lock_interruptible(&fan_mutex))
4897 return -ERESTARTSYS;
4900 switch (fan_control_access_mode) {
4901 case TPACPI_FAN_WR_ACPI_FANS:
4902 case TPACPI_FAN_WR_TPEC:
4903 if (!acpi_ec_write(fan_status_offset, 0x00))
4906 fan_control_desired_level = 0;
4907 tp_features.fan_ctrl_status_undef = 0;
4911 case TPACPI_FAN_WR_ACPI_SFAN:
4912 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4915 fan_control_desired_level = 0;
4923 mutex_unlock(&fan_mutex);
4927 static int fan_set_speed(int speed)
4931 if (!fan_control_allowed)
4934 if (mutex_lock_interruptible(&fan_mutex))
4935 return -ERESTARTSYS;
4938 switch (fan_control_access_mode) {
4939 case TPACPI_FAN_WR_ACPI_FANS:
4940 if (speed >= 0 && speed <= 65535) {
4941 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4942 speed, speed, speed))
4952 mutex_unlock(&fan_mutex);
4956 static void fan_watchdog_reset(void)
4958 static int fan_watchdog_active;
4960 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4963 if (fan_watchdog_active)
4964 cancel_delayed_work(&fan_watchdog_task);
4966 if (fan_watchdog_maxinterval > 0 &&
4967 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4968 fan_watchdog_active = 1;
4969 if (!schedule_delayed_work(&fan_watchdog_task,
4970 msecs_to_jiffies(fan_watchdog_maxinterval
4973 "failed to schedule the fan watchdog, "
4974 "watchdog will not trigger\n");
4977 fan_watchdog_active = 0;
4980 static void fan_watchdog_fire(struct work_struct *ignored)
4984 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4987 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
4988 rc = fan_set_enable();
4990 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
4991 "will try again later...\n", -rc);
4992 /* reschedule for later */
4993 fan_watchdog_reset();
4998 * SYSFS fan layout: hwmon compatible (device)
5001 * 0: "disengaged" mode
5003 * 2: native EC "auto" mode (recommended, hardware default)
5005 * pwm*: set speed in manual mode, ignored otherwise.
5006 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5009 * fan*_input: tachometer reading, RPM
5012 * SYSFS fan layout: extensions
5014 * fan_watchdog (driver):
5015 * fan watchdog interval in seconds, 0 disables (default), max 120
5018 /* sysfs fan pwm1_enable ----------------------------------------------- */
5019 static ssize_t fan_pwm1_enable_show(struct device *dev,
5020 struct device_attribute *attr,
5026 res = fan_get_status_safe(&status);
5030 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5031 if (status != fan_control_initial_status) {
5032 tp_features.fan_ctrl_status_undef = 0;
5034 /* Return most likely status. In fact, it
5035 * might be the only possible status */
5036 status = TP_EC_FAN_AUTO;
5040 if (status & TP_EC_FAN_FULLSPEED) {
5042 } else if (status & TP_EC_FAN_AUTO) {
5047 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5050 static ssize_t fan_pwm1_enable_store(struct device *dev,
5051 struct device_attribute *attr,
5052 const char *buf, size_t count)
5057 if (parse_strtoul(buf, 2, &t))
5062 level = TP_EC_FAN_FULLSPEED;
5065 level = TPACPI_FAN_LAST_LEVEL;
5068 level = TP_EC_FAN_AUTO;
5071 /* reserved for software-controlled auto mode */
5077 res = fan_set_level_safe(level);
5083 fan_watchdog_reset();
5088 static struct device_attribute dev_attr_fan_pwm1_enable =
5089 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5090 fan_pwm1_enable_show, fan_pwm1_enable_store);
5092 /* sysfs fan pwm1 ------------------------------------------------------ */
5093 static ssize_t fan_pwm1_show(struct device *dev,
5094 struct device_attribute *attr,
5100 res = fan_get_status_safe(&status);
5104 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5105 if (status != fan_control_initial_status) {
5106 tp_features.fan_ctrl_status_undef = 0;
5108 status = TP_EC_FAN_AUTO;
5113 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5114 status = fan_control_desired_level;
5119 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5122 static ssize_t fan_pwm1_store(struct device *dev,
5123 struct device_attribute *attr,
5124 const char *buf, size_t count)
5128 u8 status, newlevel;
5130 if (parse_strtoul(buf, 255, &s))
5133 /* scale down from 0-255 to 0-7 */
5134 newlevel = (s >> 5) & 0x07;
5136 if (mutex_lock_interruptible(&fan_mutex))
5137 return -ERESTARTSYS;
5139 rc = fan_get_status(&status);
5140 if (!rc && (status &
5141 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5142 rc = fan_set_level(newlevel);
5146 fan_update_desired_level(newlevel);
5147 fan_watchdog_reset();
5151 mutex_unlock(&fan_mutex);
5152 return (rc)? rc : count;
5155 static struct device_attribute dev_attr_fan_pwm1 =
5156 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5157 fan_pwm1_show, fan_pwm1_store);
5159 /* sysfs fan fan1_input ------------------------------------------------ */
5160 static ssize_t fan_fan1_input_show(struct device *dev,
5161 struct device_attribute *attr,
5167 res = fan_get_speed(&speed);
5171 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5174 static struct device_attribute dev_attr_fan_fan1_input =
5175 __ATTR(fan1_input, S_IRUGO,
5176 fan_fan1_input_show, NULL);
5178 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5179 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5182 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5185 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5186 const char *buf, size_t count)
5190 if (parse_strtoul(buf, 120, &t))
5193 if (!fan_control_allowed)
5196 fan_watchdog_maxinterval = t;
5197 fan_watchdog_reset();
5202 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5203 fan_fan_watchdog_show, fan_fan_watchdog_store);
5205 /* --------------------------------------------------------------------- */
5206 static struct attribute *fan_attributes[] = {
5207 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5208 &dev_attr_fan_fan1_input.attr,
5212 static const struct attribute_group fan_attr_group = {
5213 .attrs = fan_attributes,
5216 static int __init fan_init(struct ibm_init_struct *iibm)
5220 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5222 mutex_init(&fan_mutex);
5223 fan_status_access_mode = TPACPI_FAN_NONE;
5224 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5225 fan_control_commands = 0;
5226 fan_watchdog_maxinterval = 0;
5227 tp_features.fan_ctrl_status_undef = 0;
5228 fan_control_desired_level = 7;
5230 TPACPI_ACPIHANDLE_INIT(fans);
5231 TPACPI_ACPIHANDLE_INIT(gfan);
5232 TPACPI_ACPIHANDLE_INIT(sfan);
5235 /* 570, 600e/x, 770e, 770x */
5236 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5238 /* all other ThinkPads: note that even old-style
5239 * ThinkPad ECs supports the fan control register */
5240 if (likely(acpi_ec_read(fan_status_offset,
5241 &fan_control_initial_status))) {
5242 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5244 /* In some ThinkPads, neither the EC nor the ACPI
5245 * DSDT initialize the fan status, and it ends up
5246 * being set to 0x07 when it *could* be either
5249 * Enable for TP-1Y (T43), TP-78 (R51e),
5250 * TP-76 (R52), TP-70 (T43, R52), which are known
5252 if (fan_control_initial_status == 0x07) {
5253 switch (thinkpad_id.ec_model) {
5254 case 0x5931: /* TP-1Y */
5255 case 0x3837: /* TP-78 */
5256 case 0x3637: /* TP-76 */
5257 case 0x3037: /* TP-70 */
5258 printk(TPACPI_NOTICE
5259 "fan_init: initial fan status "
5260 "is unknown, assuming it is "
5262 tp_features.fan_ctrl_status_undef = 1;
5268 "ThinkPad ACPI EC access misbehaving, "
5269 "fan status and control unavailable\n");
5276 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5277 fan_control_commands |=
5278 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5281 /* gfan without sfan means no fan control */
5282 /* all other models implement TP EC 0x2f control */
5286 fan_control_access_mode =
5287 TPACPI_FAN_WR_ACPI_FANS;
5288 fan_control_commands |=
5289 TPACPI_FAN_CMD_SPEED |
5290 TPACPI_FAN_CMD_LEVEL |
5291 TPACPI_FAN_CMD_ENABLE;
5293 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5294 fan_control_commands |=
5295 TPACPI_FAN_CMD_LEVEL |
5296 TPACPI_FAN_CMD_ENABLE;
5301 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5302 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5303 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5304 fan_status_access_mode, fan_control_access_mode);
5306 /* fan control master switch */
5307 if (!fan_control_allowed) {
5308 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5309 fan_control_commands = 0;
5310 dbg_printk(TPACPI_DBG_INIT,
5311 "fan control features disabled by parameter\n");
5314 /* update fan_control_desired_level */
5315 if (fan_status_access_mode != TPACPI_FAN_NONE)
5316 fan_get_status_safe(NULL);
5318 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5319 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5320 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5323 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5324 &driver_attr_fan_watchdog);
5332 static void fan_exit(void)
5334 vdbg_printk(TPACPI_DBG_EXIT,
5335 "cancelling any pending fan watchdog tasks\n");
5337 /* FIXME: can we really do this unconditionally? */
5338 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5339 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5340 &driver_attr_fan_watchdog);
5342 cancel_delayed_work(&fan_watchdog_task);
5343 flush_scheduled_work();
5346 static int fan_read(char *p)
5351 unsigned int speed = 0;
5353 switch (fan_status_access_mode) {
5354 case TPACPI_FAN_RD_ACPI_GFAN:
5355 /* 570, 600e/x, 770e, 770x */
5356 rc = fan_get_status_safe(&status);
5360 len += sprintf(p + len, "status:\t\t%s\n"
5362 (status != 0) ? "enabled" : "disabled", status);
5365 case TPACPI_FAN_RD_TPEC:
5366 /* all except 570, 600e/x, 770e, 770x */
5367 rc = fan_get_status_safe(&status);
5371 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5372 if (status != fan_control_initial_status)
5373 tp_features.fan_ctrl_status_undef = 0;
5375 /* Return most likely status. In fact, it
5376 * might be the only possible status */
5377 status = TP_EC_FAN_AUTO;
5380 len += sprintf(p + len, "status:\t\t%s\n",
5381 (status != 0) ? "enabled" : "disabled");
5383 rc = fan_get_speed(&speed);
5387 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5389 if (status & TP_EC_FAN_FULLSPEED)
5390 /* Disengaged mode takes precedence */
5391 len += sprintf(p + len, "level:\t\tdisengaged\n");
5392 else if (status & TP_EC_FAN_AUTO)
5393 len += sprintf(p + len, "level:\t\tauto\n");
5395 len += sprintf(p + len, "level:\t\t%d\n", status);
5398 case TPACPI_FAN_NONE:
5400 len += sprintf(p + len, "status:\t\tnot supported\n");
5403 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5404 len += sprintf(p + len, "commands:\tlevel <level>");
5406 switch (fan_control_access_mode) {
5407 case TPACPI_FAN_WR_ACPI_SFAN:
5408 len += sprintf(p + len, " (<level> is 0-7)\n");
5412 len += sprintf(p + len, " (<level> is 0-7, "
5413 "auto, disengaged, full-speed)\n");
5418 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5419 len += sprintf(p + len, "commands:\tenable, disable\n"
5420 "commands:\twatchdog <timeout> (<timeout> "
5421 "is 0 (off), 1-120 (seconds))\n");
5423 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5424 len += sprintf(p + len, "commands:\tspeed <speed>"
5425 " (<speed> is 0-65535)\n");
5430 static int fan_write_cmd_level(const char *cmd, int *rc)
5434 if (strlencmp(cmd, "level auto") == 0)
5435 level = TP_EC_FAN_AUTO;
5436 else if ((strlencmp(cmd, "level disengaged") == 0) |
5437 (strlencmp(cmd, "level full-speed") == 0))
5438 level = TP_EC_FAN_FULLSPEED;
5439 else if (sscanf(cmd, "level %d", &level) != 1)
5442 *rc = fan_set_level_safe(level);
5444 printk(TPACPI_ERR "level command accepted for unsupported "
5445 "access mode %d", fan_control_access_mode);
5450 static int fan_write_cmd_enable(const char *cmd, int *rc)
5452 if (strlencmp(cmd, "enable") != 0)
5455 *rc = fan_set_enable();
5457 printk(TPACPI_ERR "enable command accepted for unsupported "
5458 "access mode %d", fan_control_access_mode);
5463 static int fan_write_cmd_disable(const char *cmd, int *rc)
5465 if (strlencmp(cmd, "disable") != 0)
5468 *rc = fan_set_disable();
5470 printk(TPACPI_ERR "disable command accepted for unsupported "
5471 "access mode %d", fan_control_access_mode);
5476 static int fan_write_cmd_speed(const char *cmd, int *rc)
5481 * Support speed <low> <medium> <high> ? */
5483 if (sscanf(cmd, "speed %d", &speed) != 1)
5486 *rc = fan_set_speed(speed);
5488 printk(TPACPI_ERR "speed command accepted for unsupported "
5489 "access mode %d", fan_control_access_mode);
5494 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5498 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5501 if (interval < 0 || interval > 120)
5504 fan_watchdog_maxinterval = interval;
5509 static int fan_write(char *buf)
5514 while (!rc && (cmd = next_cmd(&buf))) {
5515 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5516 fan_write_cmd_level(cmd, &rc)) &&
5517 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5518 (fan_write_cmd_enable(cmd, &rc) ||
5519 fan_write_cmd_disable(cmd, &rc) ||
5520 fan_write_cmd_watchdog(cmd, &rc))) &&
5521 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5522 fan_write_cmd_speed(cmd, &rc))
5526 fan_watchdog_reset();
5532 static struct ibm_struct fan_driver_data = {
5539 /****************************************************************************
5540 ****************************************************************************
5544 ****************************************************************************
5545 ****************************************************************************/
5547 /* sysfs name ---------------------------------------------------------- */
5548 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5549 struct device_attribute *attr,
5552 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5555 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5556 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5558 /* --------------------------------------------------------------------- */
5561 static struct proc_dir_entry *proc_dir;
5564 * Module and infrastructure proble, init and exit handling
5567 static int force_load;
5569 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5570 static const char * __init str_supported(int is_supported)
5572 static char text_unsupported[] __initdata = "not supported";
5574 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5576 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5578 static void ibm_exit(struct ibm_struct *ibm)
5580 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5582 list_del_init(&ibm->all_drivers);
5584 if (ibm->flags.acpi_notify_installed) {
5585 dbg_printk(TPACPI_DBG_EXIT,
5586 "%s: acpi_remove_notify_handler\n", ibm->name);
5588 acpi_remove_notify_handler(*ibm->acpi->handle,
5590 dispatch_acpi_notify);
5591 ibm->flags.acpi_notify_installed = 0;
5592 ibm->flags.acpi_notify_installed = 0;
5595 if (ibm->flags.proc_created) {
5596 dbg_printk(TPACPI_DBG_EXIT,
5597 "%s: remove_proc_entry\n", ibm->name);
5598 remove_proc_entry(ibm->name, proc_dir);
5599 ibm->flags.proc_created = 0;
5602 if (ibm->flags.acpi_driver_registered) {
5603 dbg_printk(TPACPI_DBG_EXIT,
5604 "%s: acpi_bus_unregister_driver\n", ibm->name);
5606 acpi_bus_unregister_driver(ibm->acpi->driver);
5607 kfree(ibm->acpi->driver);
5608 ibm->acpi->driver = NULL;
5609 ibm->flags.acpi_driver_registered = 0;
5612 if (ibm->flags.init_called && ibm->exit) {
5614 ibm->flags.init_called = 0;
5617 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5620 static int __init ibm_init(struct ibm_init_struct *iibm)
5623 struct ibm_struct *ibm = iibm->data;
5624 struct proc_dir_entry *entry;
5626 BUG_ON(ibm == NULL);
5628 INIT_LIST_HEAD(&ibm->all_drivers);
5630 if (ibm->flags.experimental && !experimental)
5633 dbg_printk(TPACPI_DBG_INIT,
5634 "probing for %s\n", ibm->name);
5637 ret = iibm->init(iibm);
5639 return 0; /* probe failed */
5643 ibm->flags.init_called = 1;
5647 if (ibm->acpi->hid) {
5648 ret = register_tpacpi_subdriver(ibm);
5653 if (ibm->acpi->notify) {
5654 ret = setup_acpi_notify(ibm);
5655 if (ret == -ENODEV) {
5656 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5666 dbg_printk(TPACPI_DBG_INIT,
5667 "%s installed\n", ibm->name);
5670 entry = create_proc_entry(ibm->name,
5671 S_IFREG | S_IRUGO | S_IWUSR,
5674 printk(TPACPI_ERR "unable to create proc entry %s\n",
5679 entry->owner = THIS_MODULE;
5681 entry->read_proc = &dispatch_procfs_read;
5683 entry->write_proc = &dispatch_procfs_write;
5684 ibm->flags.proc_created = 1;
5687 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5692 dbg_printk(TPACPI_DBG_INIT,
5693 "%s: at error exit path with result %d\n",
5697 return (ret < 0)? ret : 0;
5702 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5704 const struct dmi_device *dev = NULL;
5705 char ec_fw_string[18];
5710 memset(tp, 0, sizeof(*tp));
5712 if (dmi_name_in_vendors("IBM"))
5713 tp->vendor = PCI_VENDOR_ID_IBM;
5714 else if (dmi_name_in_vendors("LENOVO"))
5715 tp->vendor = PCI_VENDOR_ID_LENOVO;
5719 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5721 if (!tp->bios_version_str)
5723 tp->bios_model = tp->bios_version_str[0]
5724 | (tp->bios_version_str[1] << 8);
5727 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5728 * X32 or newer, all Z series; Some models must have an
5729 * up-to-date BIOS or they will not be detected.
5731 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5733 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5734 if (sscanf(dev->name,
5735 "IBM ThinkPad Embedded Controller -[%17c",
5736 ec_fw_string) == 1) {
5737 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5738 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5740 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5741 tp->ec_model = ec_fw_string[0]
5742 | (ec_fw_string[1] << 8);
5747 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5749 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5750 kfree(tp->model_str);
5751 tp->model_str = NULL;
5755 static int __init probe_for_thinkpad(void)
5763 * Non-ancient models have better DMI tagging, but very old models
5766 is_thinkpad = (thinkpad_id.model_str != NULL);
5768 /* ec is required because many other handles are relative to it */
5769 TPACPI_ACPIHANDLE_INIT(ec);
5773 "Not yet supported ThinkPad detected!\n");
5778 * Risks a regression on very old machines, but reduces potential
5779 * false positives a damn great deal
5782 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5784 if (!is_thinkpad && !force_load)
5791 /* Module init, exit, parameters */
5793 static struct ibm_init_struct ibms_init[] __initdata = {
5795 .init = thinkpad_acpi_driver_init,
5796 .data = &thinkpad_acpi_driver_data,
5799 .init = hotkey_init,
5800 .data = &hotkey_driver_data,
5803 .init = bluetooth_init,
5804 .data = &bluetooth_driver_data,
5808 .data = &wan_driver_data,
5812 .data = &video_driver_data,
5816 .data = &light_driver_data,
5818 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5821 .data = &dock_driver_data[0],
5825 .data = &dock_driver_data[1],
5828 #ifdef CONFIG_THINKPAD_ACPI_BAY
5831 .data = &bay_driver_data,
5836 .data = &cmos_driver_data,
5840 .data = &led_driver_data,
5844 .data = &beep_driver_data,
5847 .init = thermal_init,
5848 .data = &thermal_driver_data,
5851 .data = &ecdump_driver_data,
5854 .init = brightness_init,
5855 .data = &brightness_driver_data,
5858 .data = &volume_driver_data,
5862 .data = &fan_driver_data,
5866 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5869 struct ibm_struct *ibm;
5871 if (!kp || !kp->name || !val)
5874 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5875 ibm = ibms_init[i].data;
5876 WARN_ON(ibm == NULL);
5878 if (!ibm || !ibm->name)
5881 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5882 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5884 strcpy(ibms_init[i].param, val);
5885 strcat(ibms_init[i].param, ",");
5893 module_param(experimental, int, 0);
5894 MODULE_PARM_DESC(experimental,
5895 "Enables experimental features when non-zero");
5897 module_param_named(debug, dbg_level, uint, 0);
5898 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5900 module_param(force_load, bool, 0);
5901 MODULE_PARM_DESC(force_load,
5902 "Attempts to load the driver even on a "
5903 "mis-identified ThinkPad when true");
5905 module_param_named(fan_control, fan_control_allowed, bool, 0);
5906 MODULE_PARM_DESC(fan_control,
5907 "Enables setting fan parameters features when true");
5909 module_param_named(brightness_mode, brightness_mode, int, 0);
5910 MODULE_PARM_DESC(brightness_mode,
5911 "Selects brightness control strategy: "
5912 "0=auto, 1=EC, 2=CMOS, 3=both");
5914 module_param(brightness_enable, uint, 0);
5915 MODULE_PARM_DESC(brightness_enable,
5916 "Enables backlight control when 1, disables when 0");
5918 module_param(hotkey_report_mode, uint, 0);
5919 MODULE_PARM_DESC(hotkey_report_mode,
5920 "used for backwards compatibility with userspace, "
5921 "see documentation");
5923 #define TPACPI_PARAM(feature) \
5924 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5925 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
5926 "at module load, see documentation")
5928 TPACPI_PARAM(hotkey);
5929 TPACPI_PARAM(bluetooth);
5930 TPACPI_PARAM(video);
5931 TPACPI_PARAM(light);
5932 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5935 #ifdef CONFIG_THINKPAD_ACPI_BAY
5937 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5941 TPACPI_PARAM(ecdump);
5942 TPACPI_PARAM(brightness);
5943 TPACPI_PARAM(volume);
5946 static void thinkpad_acpi_module_exit(void)
5948 struct ibm_struct *ibm, *itmp;
5950 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5952 list_for_each_entry_safe_reverse(ibm, itmp,
5953 &tpacpi_all_drivers,
5958 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5960 if (tpacpi_inputdev) {
5961 if (tp_features.input_device_registered)
5962 input_unregister_device(tpacpi_inputdev);
5964 input_free_device(tpacpi_inputdev);
5968 hwmon_device_unregister(tpacpi_hwmon);
5970 if (tp_features.sensors_pdev_attrs_registered)
5971 device_remove_file(&tpacpi_sensors_pdev->dev,
5972 &dev_attr_thinkpad_acpi_pdev_name);
5973 if (tpacpi_sensors_pdev)
5974 platform_device_unregister(tpacpi_sensors_pdev);
5976 platform_device_unregister(tpacpi_pdev);
5978 if (tp_features.sensors_pdrv_attrs_registered)
5979 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5980 if (tp_features.platform_drv_attrs_registered)
5981 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5983 if (tp_features.sensors_pdrv_registered)
5984 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5986 if (tp_features.platform_drv_registered)
5987 platform_driver_unregister(&tpacpi_pdriver);
5990 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
5992 kfree(thinkpad_id.bios_version_str);
5993 kfree(thinkpad_id.ec_version_str);
5994 kfree(thinkpad_id.model_str);
5998 static int __init thinkpad_acpi_module_init(void)
6002 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6004 /* Parameter checking */
6005 if (hotkey_report_mode > 2)
6008 /* Driver-level probe */
6010 get_thinkpad_model_data(&thinkpad_id);
6011 ret = probe_for_thinkpad();
6013 thinkpad_acpi_module_exit();
6017 /* Driver initialization */
6019 TPACPI_ACPIHANDLE_INIT(ecrd);
6020 TPACPI_ACPIHANDLE_INIT(ecwr);
6022 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6025 "unable to create proc dir " TPACPI_PROC_DIR);
6026 thinkpad_acpi_module_exit();
6029 proc_dir->owner = THIS_MODULE;
6031 ret = platform_driver_register(&tpacpi_pdriver);
6034 "unable to register main platform driver\n");
6035 thinkpad_acpi_module_exit();
6038 tp_features.platform_drv_registered = 1;
6040 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6043 "unable to register hwmon platform driver\n");
6044 thinkpad_acpi_module_exit();
6047 tp_features.sensors_pdrv_registered = 1;
6049 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6051 tp_features.platform_drv_attrs_registered = 1;
6052 ret = tpacpi_create_driver_attributes(
6053 &tpacpi_hwmon_pdriver.driver);
6057 "unable to create sysfs driver attributes\n");
6058 thinkpad_acpi_module_exit();
6061 tp_features.sensors_pdrv_attrs_registered = 1;
6064 /* Device initialization */
6065 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6067 if (IS_ERR(tpacpi_pdev)) {
6068 ret = PTR_ERR(tpacpi_pdev);
6070 printk(TPACPI_ERR "unable to register platform device\n");
6071 thinkpad_acpi_module_exit();
6074 tpacpi_sensors_pdev = platform_device_register_simple(
6075 TPACPI_HWMON_DRVR_NAME,
6077 if (IS_ERR(tpacpi_sensors_pdev)) {
6078 ret = PTR_ERR(tpacpi_sensors_pdev);
6079 tpacpi_sensors_pdev = NULL;
6081 "unable to register hwmon platform device\n");
6082 thinkpad_acpi_module_exit();
6085 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6086 &dev_attr_thinkpad_acpi_pdev_name);
6089 "unable to create sysfs hwmon device attributes\n");
6090 thinkpad_acpi_module_exit();
6093 tp_features.sensors_pdev_attrs_registered = 1;
6094 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6095 if (IS_ERR(tpacpi_hwmon)) {
6096 ret = PTR_ERR(tpacpi_hwmon);
6097 tpacpi_hwmon = NULL;
6098 printk(TPACPI_ERR "unable to register hwmon device\n");
6099 thinkpad_acpi_module_exit();
6102 mutex_init(&tpacpi_inputdev_send_mutex);
6103 tpacpi_inputdev = input_allocate_device();
6104 if (!tpacpi_inputdev) {
6105 printk(TPACPI_ERR "unable to allocate input device\n");
6106 thinkpad_acpi_module_exit();
6109 /* Prepare input device, but don't register */
6110 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6111 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6112 tpacpi_inputdev->id.bustype = BUS_HOST;
6113 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6114 thinkpad_id.vendor :
6116 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6117 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6119 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6120 ret = ibm_init(&ibms_init[i]);
6121 if (ret >= 0 && *ibms_init[i].param)
6122 ret = ibms_init[i].data->write(ibms_init[i].param);
6124 thinkpad_acpi_module_exit();
6128 ret = input_register_device(tpacpi_inputdev);
6130 printk(TPACPI_ERR "unable to register input device\n");
6131 thinkpad_acpi_module_exit();
6134 tp_features.input_device_registered = 1;
6137 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6141 /* Please remove this in year 2009 */
6142 MODULE_ALIAS("ibm_acpi");
6145 * DMI matching for module autoloading
6147 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6148 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6150 * Only models listed in thinkwiki will be supported, so add yours
6151 * if it is not there yet.
6153 #define IBM_BIOS_MODULE_ALIAS(__type) \
6154 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6156 /* Non-ancient thinkpads */
6157 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6158 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6160 /* Ancient thinkpad BIOSes have to be identified by
6161 * BIOS type or model number, and there are far less
6162 * BIOS types than model numbers... */
6163 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6164 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6165 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6167 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6168 MODULE_DESCRIPTION(TPACPI_DESC);
6169 MODULE_VERSION(TPACPI_VERSION);
6170 MODULE_LICENSE("GPL");
6172 module_init(thinkpad_acpi_module_init);
6173 module_exit(thinkpad_acpi_module_exit);