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 */
304 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
305 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
306 "\\_SB.PCI0.VID0", /* 770e */
307 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
308 "\\_SB.PCI0.AGP.VID", /* all others */
312 /*************************************************************************
316 static int acpi_evalf(acpi_handle handle,
317 void *res, char *method, char *fmt, ...)
320 struct acpi_object_list params;
321 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
322 struct acpi_buffer result, *resultp;
323 union acpi_object out_obj;
331 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
344 params.pointer = &in_objs[0];
351 in_objs[params.count].integer.value = va_arg(ap, int);
352 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
354 /* add more types as needed */
356 printk(TPACPI_ERR "acpi_evalf() called "
357 "with invalid format character '%c'\n", c);
363 if (res_type != 'v') {
364 result.length = sizeof(out_obj);
365 result.pointer = &out_obj;
370 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
375 *(int *)res = out_obj.integer.value;
376 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
379 success = status == AE_OK;
381 /* add more types as needed */
383 printk(TPACPI_ERR "acpi_evalf() called "
384 "with invalid format character '%c'\n", res_type);
388 if (!success && !quiet)
389 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
390 method, fmt0, status);
395 static int acpi_ec_read(int i, u8 *p)
400 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
404 if (ec_read(i, p) < 0)
411 static int acpi_ec_write(int i, u8 v)
414 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
417 if (ec_write(i, v) < 0)
424 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
425 static int _sta(acpi_handle handle)
429 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
436 static int issue_thinkpad_cmos_command(int cmos_cmd)
441 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
447 /*************************************************************************
451 #define TPACPI_ACPIHANDLE_INIT(object) \
452 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
453 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
455 static void drv_acpi_handle_init(char *name,
456 acpi_handle *handle, acpi_handle parent,
457 char **paths, int num_paths, char **path)
462 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
465 for (i = 0; i < num_paths; i++) {
466 status = acpi_get_handle(parent, paths[i], handle);
467 if (ACPI_SUCCESS(status)) {
469 dbg_printk(TPACPI_DBG_INIT,
470 "Found ACPI handle %s for %s\n",
476 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
481 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
483 struct ibm_struct *ibm = data;
485 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
488 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
491 ibm->acpi->notify(ibm, event);
494 static int __init setup_acpi_notify(struct ibm_struct *ibm)
501 if (!*ibm->acpi->handle)
504 vdbg_printk(TPACPI_DBG_INIT,
505 "setting up ACPI notify for %s\n", ibm->name);
507 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
509 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
514 acpi_driver_data(ibm->acpi->device) = ibm;
515 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
516 TPACPI_ACPI_EVENT_PREFIX,
519 status = acpi_install_notify_handler(*ibm->acpi->handle,
520 ibm->acpi->type, dispatch_acpi_notify, ibm);
521 if (ACPI_FAILURE(status)) {
522 if (status == AE_ALREADY_EXISTS) {
524 "another device driver is already "
525 "handling %s events\n", ibm->name);
528 "acpi_install_notify_handler(%s) failed: %d\n",
533 ibm->flags.acpi_notify_installed = 1;
537 static int __init tpacpi_device_add(struct acpi_device *device)
542 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
546 dbg_printk(TPACPI_DBG_INIT,
547 "registering %s as an ACPI driver\n", ibm->name);
551 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
552 if (!ibm->acpi->driver) {
553 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
557 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
558 ibm->acpi->driver->ids = ibm->acpi->hid;
560 ibm->acpi->driver->ops.add = &tpacpi_device_add;
562 rc = acpi_bus_register_driver(ibm->acpi->driver);
564 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
566 kfree(ibm->acpi->driver);
567 ibm->acpi->driver = NULL;
569 ibm->flags.acpi_driver_registered = 1;
575 /****************************************************************************
576 ****************************************************************************
580 ****************************************************************************
581 ****************************************************************************/
583 static int dispatch_procfs_read(char *page, char **start, off_t off,
584 int count, int *eof, void *data)
586 struct ibm_struct *ibm = data;
589 if (!ibm || !ibm->read)
592 len = ibm->read(page);
596 if (len <= off + count)
608 static int dispatch_procfs_write(struct file *file,
609 const char __user *userbuf,
610 unsigned long count, void *data)
612 struct ibm_struct *ibm = data;
616 if (!ibm || !ibm->write)
619 kernbuf = kmalloc(count + 2, GFP_KERNEL);
623 if (copy_from_user(kernbuf, userbuf, count)) {
629 strcat(kernbuf, ",");
630 ret = ibm->write(kernbuf);
639 static char *next_cmd(char **cmds)
644 while ((end = strchr(start, ',')) && end == start)
656 /****************************************************************************
657 ****************************************************************************
659 * Device model: input, hwmon and platform
661 ****************************************************************************
662 ****************************************************************************/
664 static struct platform_device *tpacpi_pdev;
665 static struct platform_device *tpacpi_sensors_pdev;
666 static struct device *tpacpi_hwmon;
667 static struct input_dev *tpacpi_inputdev;
668 static struct mutex tpacpi_inputdev_send_mutex;
669 static LIST_HEAD(tpacpi_all_drivers);
671 static int tpacpi_suspend_handler(struct platform_device *pdev,
674 struct ibm_struct *ibm, *itmp;
676 list_for_each_entry_safe(ibm, itmp,
680 (ibm->suspend)(state);
686 static int tpacpi_resume_handler(struct platform_device *pdev)
688 struct ibm_struct *ibm, *itmp;
690 list_for_each_entry_safe(ibm, itmp,
700 static struct platform_driver tpacpi_pdriver = {
702 .name = TPACPI_DRVR_NAME,
703 .owner = THIS_MODULE,
705 .suspend = tpacpi_suspend_handler,
706 .resume = tpacpi_resume_handler,
709 static struct platform_driver tpacpi_hwmon_pdriver = {
711 .name = TPACPI_HWMON_DRVR_NAME,
712 .owner = THIS_MODULE,
716 /*************************************************************************
717 * sysfs support helpers
720 struct attribute_set {
721 unsigned int members, max_members;
722 struct attribute_group group;
725 struct attribute_set_obj {
726 struct attribute_set s;
728 } __attribute__((packed));
730 static struct attribute_set *create_attr_set(unsigned int max_members,
733 struct attribute_set_obj *sobj;
735 if (max_members == 0)
738 /* Allocates space for implicit NULL at the end too */
739 sobj = kzalloc(sizeof(struct attribute_set_obj) +
740 max_members * sizeof(struct attribute *),
744 sobj->s.max_members = max_members;
745 sobj->s.group.attrs = &sobj->a;
746 sobj->s.group.name = name;
751 #define destroy_attr_set(_set) \
754 /* not multi-threaded safe, use it in a single thread per set */
755 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
760 if (s->members >= s->max_members)
763 s->group.attrs[s->members] = attr;
769 static int add_many_to_attr_set(struct attribute_set *s,
770 struct attribute **attr,
775 for (i = 0; i < count; i++) {
776 res = add_to_attr_set(s, attr[i]);
784 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
786 sysfs_remove_group(kobj, &s->group);
790 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
791 sysfs_create_group(_kobj, &_attr_set->group)
793 static int parse_strtoul(const char *buf,
794 unsigned long max, unsigned long *value)
798 while (*buf && isspace(*buf))
800 *value = simple_strtoul(buf, &endp, 0);
801 while (*endp && isspace(*endp))
803 if (*endp || *value > max)
809 /*************************************************************************
810 * thinkpad-acpi driver attributes
813 /* interface_version --------------------------------------------------- */
814 static ssize_t tpacpi_driver_interface_version_show(
815 struct device_driver *drv,
818 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
821 static DRIVER_ATTR(interface_version, S_IRUGO,
822 tpacpi_driver_interface_version_show, NULL);
824 /* debug_level --------------------------------------------------------- */
825 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
828 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
831 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
832 const char *buf, size_t count)
836 if (parse_strtoul(buf, 0xffff, &t))
844 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
845 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
847 /* version ------------------------------------------------------------- */
848 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
851 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
852 TPACPI_DESC, TPACPI_VERSION);
855 static DRIVER_ATTR(version, S_IRUGO,
856 tpacpi_driver_version_show, NULL);
858 /* --------------------------------------------------------------------- */
860 static struct driver_attribute *tpacpi_driver_attributes[] = {
861 &driver_attr_debug_level, &driver_attr_version,
862 &driver_attr_interface_version,
865 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
871 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
872 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
879 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
883 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
884 driver_remove_file(drv, tpacpi_driver_attributes[i]);
887 /****************************************************************************
888 ****************************************************************************
892 ****************************************************************************
893 ****************************************************************************/
895 /*************************************************************************
896 * thinkpad-acpi init subdriver
899 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
901 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
902 printk(TPACPI_INFO "%s\n", TPACPI_URL);
904 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
905 (thinkpad_id.bios_version_str) ?
906 thinkpad_id.bios_version_str : "unknown",
907 (thinkpad_id.ec_version_str) ?
908 thinkpad_id.ec_version_str : "unknown");
910 if (thinkpad_id.vendor && thinkpad_id.model_str)
911 printk(TPACPI_INFO "%s %s\n",
912 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
913 "IBM" : ((thinkpad_id.vendor ==
914 PCI_VENDOR_ID_LENOVO) ?
915 "Lenovo" : "Unknown vendor"),
916 thinkpad_id.model_str);
921 static int thinkpad_acpi_driver_read(char *p)
925 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
926 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
931 static struct ibm_struct thinkpad_acpi_driver_data = {
933 .read = thinkpad_acpi_driver_read,
936 /*************************************************************************
940 enum { /* hot key scan codes (derived from ACPI DSDT) */
941 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
942 TP_ACPI_HOTKEYSCAN_FNF2,
943 TP_ACPI_HOTKEYSCAN_FNF3,
944 TP_ACPI_HOTKEYSCAN_FNF4,
945 TP_ACPI_HOTKEYSCAN_FNF5,
946 TP_ACPI_HOTKEYSCAN_FNF6,
947 TP_ACPI_HOTKEYSCAN_FNF7,
948 TP_ACPI_HOTKEYSCAN_FNF8,
949 TP_ACPI_HOTKEYSCAN_FNF9,
950 TP_ACPI_HOTKEYSCAN_FNF10,
951 TP_ACPI_HOTKEYSCAN_FNF11,
952 TP_ACPI_HOTKEYSCAN_FNF12,
953 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
954 TP_ACPI_HOTKEYSCAN_FNINSERT,
955 TP_ACPI_HOTKEYSCAN_FNDELETE,
956 TP_ACPI_HOTKEYSCAN_FNHOME,
957 TP_ACPI_HOTKEYSCAN_FNEND,
958 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
959 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
960 TP_ACPI_HOTKEYSCAN_FNSPACE,
961 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
962 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
963 TP_ACPI_HOTKEYSCAN_MUTE,
964 TP_ACPI_HOTKEYSCAN_THINKPAD,
967 enum { /* Keys available through NVRAM polling */
968 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
969 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
972 enum { /* Positions of some of the keys in hotkey masks */
973 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
974 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
975 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
976 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
977 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
978 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
979 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
980 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
981 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
982 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
983 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
986 enum { /* NVRAM to ACPI HKEY group map */
987 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
988 TP_ACPI_HKEY_ZOOM_MASK |
989 TP_ACPI_HKEY_DISPSWTCH_MASK |
990 TP_ACPI_HKEY_HIBERNATE_MASK,
991 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
992 TP_ACPI_HKEY_BRGHTDWN_MASK,
993 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
994 TP_ACPI_HKEY_VOLDWN_MASK |
995 TP_ACPI_HKEY_MUTE_MASK,
998 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
999 struct tp_nvram_state {
1000 u16 thinkpad_toggle:1;
1002 u16 display_toggle:1;
1003 u16 thinklight_toggle:1;
1004 u16 hibernate_toggle:1;
1005 u16 displayexp_toggle:1;
1006 u16 display_state:1;
1007 u16 brightness_toggle:1;
1008 u16 volume_toggle:1;
1011 u8 brightness_level;
1015 static struct task_struct *tpacpi_hotkey_task;
1016 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1017 static int hotkey_poll_freq = 10; /* Hz */
1018 static struct mutex hotkey_thread_mutex;
1019 static struct mutex hotkey_thread_data_mutex;
1020 static unsigned int hotkey_config_change;
1022 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1024 #define hotkey_source_mask 0U
1026 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1028 static struct mutex hotkey_mutex;
1030 static enum { /* Reasons for waking up */
1031 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1032 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1033 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1034 } hotkey_wakeup_reason;
1036 static int hotkey_autosleep_ack;
1038 static int hotkey_orig_status;
1039 static u32 hotkey_orig_mask;
1040 static u32 hotkey_all_mask;
1041 static u32 hotkey_reserved_mask;
1042 static u32 hotkey_mask;
1044 static unsigned int hotkey_report_mode;
1046 static u16 *hotkey_keycode_map;
1048 static struct attribute_set *hotkey_dev_attributes;
1050 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1051 #define HOTKEY_CONFIG_CRITICAL_START \
1053 mutex_lock(&hotkey_thread_data_mutex); \
1054 hotkey_config_change++; \
1056 #define HOTKEY_CONFIG_CRITICAL_END \
1057 mutex_unlock(&hotkey_thread_data_mutex);
1059 #define HOTKEY_CONFIG_CRITICAL_START
1060 #define HOTKEY_CONFIG_CRITICAL_END
1061 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1063 static int hotkey_get_wlsw(int *status)
1065 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1071 * Call with hotkey_mutex held
1073 static int hotkey_mask_get(void)
1077 if (tp_features.hotkey_mask) {
1078 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1081 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1087 * Call with hotkey_mutex held
1089 static int hotkey_mask_set(u32 mask)
1094 if (tp_features.hotkey_mask) {
1095 HOTKEY_CONFIG_CRITICAL_START
1096 for (i = 0; i < 32; i++) {
1098 /* enable in firmware mask only keys not in NVRAM
1099 * mode, but enable the key in the cached hotkey_mask
1100 * regardless of mode, or the key will end up
1101 * disabled by hotkey_mask_get() */
1102 if (!acpi_evalf(hkey_handle,
1103 NULL, "MHKM", "vdd", i + 1,
1104 !!((mask & ~hotkey_source_mask) & m))) {
1108 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1111 HOTKEY_CONFIG_CRITICAL_END
1113 /* hotkey_mask_get must be called unconditionally below */
1114 if (!hotkey_mask_get() && !rc &&
1115 (hotkey_mask & ~hotkey_source_mask) !=
1116 (mask & ~hotkey_source_mask)) {
1117 printk(TPACPI_NOTICE
1118 "requested hot key mask 0x%08x, but "
1119 "firmware forced it to 0x%08x\n",
1123 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1124 HOTKEY_CONFIG_CRITICAL_START
1125 hotkey_mask = mask & hotkey_source_mask;
1126 HOTKEY_CONFIG_CRITICAL_END
1128 if (hotkey_mask != mask) {
1129 printk(TPACPI_NOTICE
1130 "requested hot key mask 0x%08x, "
1131 "forced to 0x%08x (NVRAM poll mask is "
1132 "0x%08x): no firmware mask support\n",
1133 mask, hotkey_mask, hotkey_source_mask);
1138 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1144 static int hotkey_status_get(int *status)
1146 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1152 static int hotkey_status_set(int status)
1154 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1160 static void tpacpi_input_send_radiosw(void)
1164 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1165 mutex_lock(&tpacpi_inputdev_send_mutex);
1167 input_report_switch(tpacpi_inputdev,
1169 input_sync(tpacpi_inputdev);
1171 mutex_unlock(&tpacpi_inputdev_send_mutex);
1175 static void tpacpi_input_send_tabletsw(unsigned int state)
1177 mutex_lock(&tpacpi_inputdev_send_mutex);
1179 input_report_switch(tpacpi_inputdev,
1180 SW_TABLET_MODE, !!state);
1181 input_sync(tpacpi_inputdev);
1183 mutex_unlock(&tpacpi_inputdev_send_mutex);
1186 static void tpacpi_input_send_key(unsigned int scancode)
1188 unsigned int keycode;
1190 keycode = hotkey_keycode_map[scancode];
1192 if (keycode != KEY_RESERVED) {
1193 mutex_lock(&tpacpi_inputdev_send_mutex);
1195 input_report_key(tpacpi_inputdev, keycode, 1);
1196 if (keycode == KEY_UNKNOWN)
1197 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1199 input_sync(tpacpi_inputdev);
1201 input_report_key(tpacpi_inputdev, keycode, 0);
1202 if (keycode == KEY_UNKNOWN)
1203 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1205 input_sync(tpacpi_inputdev);
1207 mutex_unlock(&tpacpi_inputdev_send_mutex);
1211 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1212 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1214 static void tpacpi_hotkey_send_key(unsigned int scancode)
1216 tpacpi_input_send_key(scancode);
1217 if (hotkey_report_mode < 2) {
1218 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1219 0x80, 0x1001 + scancode);
1223 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1227 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1228 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1229 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1230 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1231 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1232 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1234 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1235 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1236 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1238 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1239 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1240 n->displayexp_toggle =
1241 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1243 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1244 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1245 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1246 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1247 n->brightness_toggle =
1248 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1250 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1251 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1252 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1253 >> TP_NVRAM_POS_LEVEL_VOLUME;
1254 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1255 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1259 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1261 if ((mask & (1 << __scancode)) && \
1262 oldn->__member != newn->__member) \
1263 tpacpi_hotkey_send_key(__scancode); \
1266 #define TPACPI_MAY_SEND_KEY(__scancode) \
1267 do { if (mask & (1 << __scancode)) \
1268 tpacpi_hotkey_send_key(__scancode); } while (0)
1270 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1271 struct tp_nvram_state *newn,
1274 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1275 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1276 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1277 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1279 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1281 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1284 if (oldn->volume_toggle != newn->volume_toggle) {
1285 if (oldn->mute != newn->mute) {
1286 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1288 if (oldn->volume_level > newn->volume_level) {
1289 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1290 } else if (oldn->volume_level < newn->volume_level) {
1291 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1292 } else if (oldn->mute == newn->mute) {
1293 /* repeated key presses that didn't change state */
1295 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1296 } else if (newn->volume_level != 0) {
1297 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1299 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1304 /* handle brightness */
1305 if (oldn->brightness_toggle != newn->brightness_toggle) {
1306 if (oldn->brightness_level < newn->brightness_level) {
1307 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1308 } else if (oldn->brightness_level > newn->brightness_level) {
1309 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1311 /* repeated key presses that didn't change state */
1312 if (newn->brightness_level != 0) {
1313 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1315 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1321 #undef TPACPI_COMPARE_KEY
1322 #undef TPACPI_MAY_SEND_KEY
1324 static int hotkey_kthread(void *data)
1326 struct tp_nvram_state s[2];
1328 unsigned int si, so;
1330 unsigned int change_detector, must_reset;
1332 mutex_lock(&hotkey_thread_mutex);
1334 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1343 /* Initial state for compares */
1344 mutex_lock(&hotkey_thread_data_mutex);
1345 change_detector = hotkey_config_change;
1346 mask = hotkey_source_mask & hotkey_mask;
1347 mutex_unlock(&hotkey_thread_data_mutex);
1348 hotkey_read_nvram(&s[so], mask);
1350 while (!kthread_should_stop() && hotkey_poll_freq) {
1352 t = 1000/hotkey_poll_freq;
1353 t = msleep_interruptible(t);
1354 if (unlikely(kthread_should_stop()))
1356 must_reset = try_to_freeze();
1357 if (t > 0 && !must_reset)
1360 mutex_lock(&hotkey_thread_data_mutex);
1361 if (must_reset || hotkey_config_change != change_detector) {
1362 /* forget old state on thaw or config change */
1365 change_detector = hotkey_config_change;
1367 mask = hotkey_source_mask & hotkey_mask;
1368 mutex_unlock(&hotkey_thread_data_mutex);
1371 hotkey_read_nvram(&s[si], mask);
1372 if (likely(si != so)) {
1373 hotkey_compare_and_issue_event(&s[so], &s[si],
1383 mutex_unlock(&hotkey_thread_mutex);
1387 static void hotkey_poll_stop_sync(void)
1389 if (tpacpi_hotkey_task) {
1390 if (frozen(tpacpi_hotkey_task) ||
1391 freezing(tpacpi_hotkey_task))
1392 thaw_process(tpacpi_hotkey_task);
1394 kthread_stop(tpacpi_hotkey_task);
1395 tpacpi_hotkey_task = NULL;
1396 mutex_lock(&hotkey_thread_mutex);
1397 /* at this point, the thread did exit */
1398 mutex_unlock(&hotkey_thread_mutex);
1402 /* call with hotkey_mutex held */
1403 static void hotkey_poll_setup(int may_warn)
1405 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1406 hotkey_poll_freq > 0 &&
1407 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1408 if (!tpacpi_hotkey_task) {
1409 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1412 if (IS_ERR(tpacpi_hotkey_task)) {
1413 tpacpi_hotkey_task = NULL;
1415 "could not create kernel thread "
1416 "for hotkey polling\n");
1420 hotkey_poll_stop_sync();
1422 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1423 printk(TPACPI_NOTICE
1424 "hot keys 0x%08x require polling, "
1425 "which is currently disabled\n",
1426 hotkey_source_mask);
1431 static void hotkey_poll_setup_safe(int may_warn)
1433 mutex_lock(&hotkey_mutex);
1434 hotkey_poll_setup(may_warn);
1435 mutex_unlock(&hotkey_mutex);
1438 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1440 static void hotkey_poll_setup_safe(int __unused)
1444 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1446 static int hotkey_inputdev_open(struct input_dev *dev)
1448 switch (tpacpi_lifecycle) {
1449 case TPACPI_LIFE_INIT:
1451 * hotkey_init will call hotkey_poll_setup_safe
1452 * at the appropriate moment
1455 case TPACPI_LIFE_EXITING:
1457 case TPACPI_LIFE_RUNNING:
1458 hotkey_poll_setup_safe(0);
1462 /* Should only happen if tpacpi_lifecycle is corrupt */
1467 static void hotkey_inputdev_close(struct input_dev *dev)
1469 /* disable hotkey polling when possible */
1470 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1471 hotkey_poll_setup_safe(0);
1474 /* sysfs hotkey enable ------------------------------------------------- */
1475 static ssize_t hotkey_enable_show(struct device *dev,
1476 struct device_attribute *attr,
1481 res = hotkey_status_get(&status);
1485 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1488 static ssize_t hotkey_enable_store(struct device *dev,
1489 struct device_attribute *attr,
1490 const char *buf, size_t count)
1495 if (parse_strtoul(buf, 1, &t))
1498 res = hotkey_status_set(t);
1500 return (res) ? res : count;
1503 static struct device_attribute dev_attr_hotkey_enable =
1504 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1505 hotkey_enable_show, hotkey_enable_store);
1507 /* sysfs hotkey mask --------------------------------------------------- */
1508 static ssize_t hotkey_mask_show(struct device *dev,
1509 struct device_attribute *attr,
1514 if (mutex_lock_interruptible(&hotkey_mutex))
1515 return -ERESTARTSYS;
1516 res = hotkey_mask_get();
1517 mutex_unlock(&hotkey_mutex);
1520 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1523 static ssize_t hotkey_mask_store(struct device *dev,
1524 struct device_attribute *attr,
1525 const char *buf, size_t count)
1530 if (parse_strtoul(buf, 0xffffffffUL, &t))
1533 if (mutex_lock_interruptible(&hotkey_mutex))
1534 return -ERESTARTSYS;
1536 res = hotkey_mask_set(t);
1538 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1539 hotkey_poll_setup(1);
1542 mutex_unlock(&hotkey_mutex);
1544 return (res) ? res : count;
1547 static struct device_attribute dev_attr_hotkey_mask =
1548 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1549 hotkey_mask_show, hotkey_mask_store);
1551 /* sysfs hotkey bios_enabled ------------------------------------------- */
1552 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1553 struct device_attribute *attr,
1556 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1559 static struct device_attribute dev_attr_hotkey_bios_enabled =
1560 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1562 /* sysfs hotkey bios_mask ---------------------------------------------- */
1563 static ssize_t hotkey_bios_mask_show(struct device *dev,
1564 struct device_attribute *attr,
1567 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1570 static struct device_attribute dev_attr_hotkey_bios_mask =
1571 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1573 /* sysfs hotkey all_mask ----------------------------------------------- */
1574 static ssize_t hotkey_all_mask_show(struct device *dev,
1575 struct device_attribute *attr,
1578 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1579 hotkey_all_mask | hotkey_source_mask);
1582 static struct device_attribute dev_attr_hotkey_all_mask =
1583 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1585 /* sysfs hotkey recommended_mask --------------------------------------- */
1586 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1587 struct device_attribute *attr,
1590 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1591 (hotkey_all_mask | hotkey_source_mask)
1592 & ~hotkey_reserved_mask);
1595 static struct device_attribute dev_attr_hotkey_recommended_mask =
1596 __ATTR(hotkey_recommended_mask, S_IRUGO,
1597 hotkey_recommended_mask_show, NULL);
1599 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1601 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1602 static ssize_t hotkey_source_mask_show(struct device *dev,
1603 struct device_attribute *attr,
1606 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1609 static ssize_t hotkey_source_mask_store(struct device *dev,
1610 struct device_attribute *attr,
1611 const char *buf, size_t count)
1615 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1616 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1619 if (mutex_lock_interruptible(&hotkey_mutex))
1620 return -ERESTARTSYS;
1622 HOTKEY_CONFIG_CRITICAL_START
1623 hotkey_source_mask = t;
1624 HOTKEY_CONFIG_CRITICAL_END
1626 hotkey_poll_setup(1);
1628 mutex_unlock(&hotkey_mutex);
1633 static struct device_attribute dev_attr_hotkey_source_mask =
1634 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1635 hotkey_source_mask_show, hotkey_source_mask_store);
1637 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1638 static ssize_t hotkey_poll_freq_show(struct device *dev,
1639 struct device_attribute *attr,
1642 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1645 static ssize_t hotkey_poll_freq_store(struct device *dev,
1646 struct device_attribute *attr,
1647 const char *buf, size_t count)
1651 if (parse_strtoul(buf, 25, &t))
1654 if (mutex_lock_interruptible(&hotkey_mutex))
1655 return -ERESTARTSYS;
1657 hotkey_poll_freq = t;
1659 hotkey_poll_setup(1);
1660 mutex_unlock(&hotkey_mutex);
1665 static struct device_attribute dev_attr_hotkey_poll_freq =
1666 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1667 hotkey_poll_freq_show, hotkey_poll_freq_store);
1669 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1671 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1672 static ssize_t hotkey_radio_sw_show(struct device *dev,
1673 struct device_attribute *attr,
1677 res = hotkey_get_wlsw(&s);
1681 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1684 static struct device_attribute dev_attr_hotkey_radio_sw =
1685 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1687 static void hotkey_radio_sw_notify_change(void)
1689 if (tp_features.hotkey_wlsw)
1690 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1694 /* sysfs hotkey report_mode -------------------------------------------- */
1695 static ssize_t hotkey_report_mode_show(struct device *dev,
1696 struct device_attribute *attr,
1699 return snprintf(buf, PAGE_SIZE, "%d\n",
1700 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1703 static struct device_attribute dev_attr_hotkey_report_mode =
1704 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1706 /* sysfs wakeup reason (pollable) -------------------------------------- */
1707 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1708 struct device_attribute *attr,
1711 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1714 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1715 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1717 static void hotkey_wakeup_reason_notify_change(void)
1719 if (tp_features.hotkey_mask)
1720 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1724 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1725 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1726 struct device_attribute *attr,
1729 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1732 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1733 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1734 hotkey_wakeup_hotunplug_complete_show, NULL);
1736 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1738 if (tp_features.hotkey_mask)
1739 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1740 "wakeup_hotunplug_complete");
1743 /* --------------------------------------------------------------------- */
1745 static struct attribute *hotkey_attributes[] __initdata = {
1746 &dev_attr_hotkey_enable.attr,
1747 &dev_attr_hotkey_bios_enabled.attr,
1748 &dev_attr_hotkey_report_mode.attr,
1749 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1750 &dev_attr_hotkey_mask.attr,
1751 &dev_attr_hotkey_all_mask.attr,
1752 &dev_attr_hotkey_recommended_mask.attr,
1753 &dev_attr_hotkey_source_mask.attr,
1754 &dev_attr_hotkey_poll_freq.attr,
1758 static struct attribute *hotkey_mask_attributes[] __initdata = {
1759 &dev_attr_hotkey_bios_mask.attr,
1760 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1761 &dev_attr_hotkey_mask.attr,
1762 &dev_attr_hotkey_all_mask.attr,
1763 &dev_attr_hotkey_recommended_mask.attr,
1765 &dev_attr_hotkey_wakeup_reason.attr,
1766 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1769 static int __init hotkey_init(struct ibm_init_struct *iibm)
1771 /* Requirements for changing the default keymaps:
1773 * 1. Many of the keys are mapped to KEY_RESERVED for very
1774 * good reasons. Do not change them unless you have deep
1775 * knowledge on the IBM and Lenovo ThinkPad firmware for
1776 * the various ThinkPad models. The driver behaves
1777 * differently for KEY_RESERVED: such keys have their
1778 * hot key mask *unset* in mask_recommended, and also
1779 * in the initial hot key mask programmed into the
1780 * firmware at driver load time, which means the firm-
1781 * ware may react very differently if you change them to
1784 * 2. You must be subscribed to the linux-thinkpad and
1785 * ibm-acpi-devel mailing lists, and you should read the
1786 * list archives since 2007 if you want to change the
1787 * keymaps. This requirement exists so that you will
1788 * know the past history of problems with the thinkpad-
1789 * acpi driver keymaps, and also that you will be
1790 * listening to any bug reports;
1792 * 3. Do not send thinkpad-acpi specific patches directly to
1793 * for merging, *ever*. Send them to the linux-acpi
1794 * mailinglist for comments. Merging is to be done only
1795 * through acpi-test and the ACPI maintainer.
1797 * If the above is too much to ask, don't change the keymap.
1798 * Ask the thinkpad-acpi maintainer to do it, instead.
1800 static u16 ibm_keycode_map[] __initdata = {
1801 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1802 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1803 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1804 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1806 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1807 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1808 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1809 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1811 /* brightness: firmware always reacts to them, unless
1812 * X.org did some tricks in the radeon BIOS scratch
1813 * registers of *some* models */
1814 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1815 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1817 /* Thinklight: firmware always react to it */
1818 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1820 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1821 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1823 /* Volume: firmware always react to it and reprograms
1824 * the built-in *extra* mixer. Never map it to control
1825 * another mixer by default. */
1826 KEY_RESERVED, /* 0x14: VOLUME UP */
1827 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1828 KEY_RESERVED, /* 0x16: MUTE */
1830 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1832 /* (assignments unknown, please report if found) */
1833 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1834 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1836 static u16 lenovo_keycode_map[] __initdata = {
1837 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1838 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1839 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1840 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1842 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1843 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1844 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1845 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1847 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1848 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1850 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1852 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1853 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1855 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1856 * react to it and reprograms the built-in *extra* mixer.
1857 * Never map it to control another mixer by default.
1859 * T60?, T61, R60?, R61: firmware and EC tries to send
1860 * these over the regular keyboard, so these are no-ops,
1861 * but there are still weird bugs re. MUTE, so do not
1862 * change unless you get test reports from all Lenovo
1863 * models. May cause the BIOS to interfere with the
1866 KEY_RESERVED, /* 0x14: VOLUME UP */
1867 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1868 KEY_RESERVED, /* 0x16: MUTE */
1870 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1872 /* (assignments unknown, please report if found) */
1873 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1874 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1877 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1878 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1879 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1885 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1887 BUG_ON(!tpacpi_inputdev);
1888 BUG_ON(tpacpi_inputdev->open != NULL ||
1889 tpacpi_inputdev->close != NULL);
1891 TPACPI_ACPIHANDLE_INIT(hkey);
1892 mutex_init(&hotkey_mutex);
1894 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1895 mutex_init(&hotkey_thread_mutex);
1896 mutex_init(&hotkey_thread_data_mutex);
1899 /* hotkey not supported on 570 */
1900 tp_features.hotkey = hkey_handle != NULL;
1902 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1903 str_supported(tp_features.hotkey));
1905 if (tp_features.hotkey) {
1906 hotkey_dev_attributes = create_attr_set(12, NULL);
1907 if (!hotkey_dev_attributes)
1909 res = add_many_to_attr_set(hotkey_dev_attributes,
1911 ARRAY_SIZE(hotkey_attributes));
1915 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1916 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1917 for HKEY interface version 0x100 */
1918 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1919 if ((hkeyv >> 8) != 1) {
1920 printk(TPACPI_ERR "unknown version of the "
1921 "HKEY interface: 0x%x\n", hkeyv);
1922 printk(TPACPI_ERR "please report this to %s\n",
1926 * MHKV 0x100 in A31, R40, R40e,
1927 * T4x, X31, and later
1929 tp_features.hotkey_mask = 1;
1933 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1934 str_supported(tp_features.hotkey_mask));
1936 if (tp_features.hotkey_mask) {
1937 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1940 "missing MHKA handler, "
1941 "please report this to %s\n",
1943 /* FN+F12, FN+F4, FN+F3 */
1944 hotkey_all_mask = 0x080cU;
1948 /* hotkey_source_mask *must* be zero for
1949 * the first hotkey_mask_get */
1950 res = hotkey_status_get(&hotkey_orig_status);
1951 if (!res && tp_features.hotkey_mask) {
1952 res = hotkey_mask_get();
1953 hotkey_orig_mask = hotkey_mask;
1955 res = add_many_to_attr_set(
1956 hotkey_dev_attributes,
1957 hotkey_mask_attributes,
1958 ARRAY_SIZE(hotkey_mask_attributes));
1962 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1963 if (tp_features.hotkey_mask) {
1964 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1967 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1970 vdbg_printk(TPACPI_DBG_INIT,
1971 "hotkey source mask 0x%08x, polling freq %d\n",
1972 hotkey_source_mask, hotkey_poll_freq);
1975 /* Not all thinkpads have a hardware radio switch */
1976 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1977 tp_features.hotkey_wlsw = 1;
1979 "radio switch found; radios are %s\n",
1980 enabled(status, 0));
1981 res = add_to_attr_set(hotkey_dev_attributes,
1982 &dev_attr_hotkey_radio_sw.attr);
1986 res = register_attr_set_with_sysfs(
1987 hotkey_dev_attributes,
1988 &tpacpi_pdev->dev.kobj);
1992 /* Set up key map */
1994 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1996 if (!hotkey_keycode_map) {
1998 "failed to allocate memory for key map\n");
2002 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2003 dbg_printk(TPACPI_DBG_INIT,
2004 "using Lenovo default hot key map\n");
2005 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2006 TPACPI_HOTKEY_MAP_SIZE);
2008 dbg_printk(TPACPI_DBG_INIT,
2009 "using IBM default hot key map\n");
2010 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2011 TPACPI_HOTKEY_MAP_SIZE);
2014 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2015 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2016 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2017 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2018 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2019 tpacpi_inputdev->keycode = hotkey_keycode_map;
2020 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2021 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2022 set_bit(hotkey_keycode_map[i],
2023 tpacpi_inputdev->keybit);
2025 if (i < sizeof(hotkey_reserved_mask)*8)
2026 hotkey_reserved_mask |= 1 << i;
2030 if (tp_features.hotkey_wlsw) {
2031 set_bit(EV_SW, tpacpi_inputdev->evbit);
2032 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2034 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2035 set_bit(EV_SW, tpacpi_inputdev->evbit);
2036 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2039 dbg_printk(TPACPI_DBG_INIT,
2040 "enabling hot key handling\n");
2041 res = hotkey_status_set(1);
2044 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2045 & ~hotkey_reserved_mask)
2046 | hotkey_orig_mask);
2047 if (res < 0 && res != -ENXIO)
2050 dbg_printk(TPACPI_DBG_INIT,
2051 "legacy hot key reporting over procfs %s\n",
2052 (hotkey_report_mode < 2) ?
2053 "enabled" : "disabled");
2055 tpacpi_inputdev->open = &hotkey_inputdev_open;
2056 tpacpi_inputdev->close = &hotkey_inputdev_close;
2058 hotkey_poll_setup_safe(1);
2059 tpacpi_input_send_radiosw();
2062 return (tp_features.hotkey)? 0 : 1;
2065 static void hotkey_exit(void)
2067 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2068 hotkey_poll_stop_sync();
2071 if (tp_features.hotkey) {
2072 dbg_printk(TPACPI_DBG_EXIT,
2073 "restoring original hot key mask\n");
2074 /* no short-circuit boolean operator below! */
2075 if ((hotkey_mask_set(hotkey_orig_mask) |
2076 hotkey_status_set(hotkey_orig_status)) != 0)
2078 "failed to restore hot key mask "
2079 "to BIOS defaults\n");
2082 if (hotkey_dev_attributes) {
2083 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2084 hotkey_dev_attributes = NULL;
2088 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2091 unsigned int scancode;
2096 if (event != 0x80) {
2098 "unknown HKEY notification event %d\n", event);
2099 /* forward it to userspace, maybe it knows how to handle it */
2100 acpi_bus_generate_netlink_event(
2101 ibm->acpi->device->pnp.device_class,
2102 ibm->acpi->device->dev.bus_id,
2108 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2109 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2122 switch (hkey >> 12) {
2124 /* 0x1000-0x1FFF: key presses */
2125 scancode = hkey & 0xfff;
2126 if (scancode > 0 && scancode < 0x21) {
2128 if (!(hotkey_source_mask & (1 << scancode))) {
2129 tpacpi_input_send_key(scancode);
2141 case 0x2304: /* suspend, undock */
2142 case 0x2404: /* hibernation, undock */
2143 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2146 case 0x2305: /* suspend, bay eject */
2147 case 0x2405: /* hibernation, bay eject */
2148 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2154 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2156 "woke up due to a hot-unplug "
2158 hotkey_wakeup_reason_notify_change();
2162 /* bay-related wakeups */
2163 if (hkey == 0x3003) {
2164 hotkey_autosleep_ack = 1;
2167 hotkey_wakeup_hotunplug_complete_notify_change();
2173 /* dock-related wakeups */
2174 if (hkey == 0x4003) {
2175 hotkey_autosleep_ack = 1;
2178 hotkey_wakeup_hotunplug_complete_notify_change();
2184 /* 0x5000-0x5FFF: human interface helpers */
2186 case 0x5010: /* Lenovo new BIOS: brightness changed */
2187 case 0x500b: /* X61t: tablet pen inserted into bay */
2188 case 0x500c: /* X61t: tablet pen removed from bay */
2190 case 0x5009: /* X61t: swivel up (tablet mode) */
2191 case 0x500a: /* X61t: swivel down (normal mode) */
2192 tpacpi_input_send_tabletsw((hkey == 0x5009));
2197 /* LID switch events. Do not propagate */
2205 /* 0x7000-0x7FFF: misc */
2206 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2207 tpacpi_input_send_radiosw();
2208 hotkey_radio_sw_notify_change();
2212 /* fallthrough to default */
2217 printk(TPACPI_NOTICE
2218 "unhandled HKEY event 0x%04x\n", hkey);
2222 if (!ignore_acpi_ev &&
2223 (send_acpi_ev || hotkey_report_mode < 2)) {
2224 acpi_bus_generate_proc_event(ibm->acpi->device,
2228 /* netlink events */
2229 if (!ignore_acpi_ev && send_acpi_ev) {
2230 acpi_bus_generate_netlink_event(
2231 ibm->acpi->device->pnp.device_class,
2232 ibm->acpi->device->dev.bus_id,
2238 static void hotkey_suspend(pm_message_t state)
2240 /* Do these on suspend, we get the events on early resume! */
2241 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2242 hotkey_autosleep_ack = 0;
2245 static void hotkey_resume(void)
2247 if (hotkey_mask_get())
2249 "error while trying to read hot key mask "
2251 tpacpi_input_send_radiosw();
2252 hotkey_radio_sw_notify_change();
2253 hotkey_wakeup_reason_notify_change();
2254 hotkey_wakeup_hotunplug_complete_notify_change();
2255 hotkey_poll_setup_safe(0);
2258 /* procfs -------------------------------------------------------------- */
2259 static int hotkey_read(char *p)
2264 if (!tp_features.hotkey) {
2265 len += sprintf(p + len, "status:\t\tnot supported\n");
2269 if (mutex_lock_interruptible(&hotkey_mutex))
2270 return -ERESTARTSYS;
2271 res = hotkey_status_get(&status);
2273 res = hotkey_mask_get();
2274 mutex_unlock(&hotkey_mutex);
2278 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2279 if (tp_features.hotkey_mask) {
2280 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2281 len += sprintf(p + len,
2282 "commands:\tenable, disable, reset, <mask>\n");
2284 len += sprintf(p + len, "mask:\t\tnot supported\n");
2285 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2291 static int hotkey_write(char *buf)
2297 if (!tp_features.hotkey)
2300 if (mutex_lock_interruptible(&hotkey_mutex))
2301 return -ERESTARTSYS;
2307 while ((cmd = next_cmd(&buf))) {
2308 if (strlencmp(cmd, "enable") == 0) {
2310 } else if (strlencmp(cmd, "disable") == 0) {
2312 } else if (strlencmp(cmd, "reset") == 0) {
2313 status = hotkey_orig_status;
2314 mask = hotkey_orig_mask;
2315 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2317 } else if (sscanf(cmd, "%x", &mask) == 1) {
2325 res = hotkey_status_set(status);
2327 if (!res && mask != hotkey_mask)
2328 res = hotkey_mask_set(mask);
2331 mutex_unlock(&hotkey_mutex);
2335 static const struct acpi_device_id ibm_htk_device_ids[] = {
2336 {TPACPI_ACPI_HKEY_HID, 0},
2340 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2341 .hid = ibm_htk_device_ids,
2342 .notify = hotkey_notify,
2343 .handle = &hkey_handle,
2344 .type = ACPI_DEVICE_NOTIFY,
2347 static struct ibm_struct hotkey_driver_data = {
2349 .read = hotkey_read,
2350 .write = hotkey_write,
2351 .exit = hotkey_exit,
2352 .resume = hotkey_resume,
2353 .suspend = hotkey_suspend,
2354 .acpi = &ibm_hotkey_acpidriver,
2357 /*************************************************************************
2358 * Bluetooth subdriver
2362 /* ACPI GBDC/SBDC bits */
2363 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2364 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2365 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2368 static int bluetooth_get_radiosw(void);
2369 static int bluetooth_set_radiosw(int radio_on);
2371 /* sysfs bluetooth enable ---------------------------------------------- */
2372 static ssize_t bluetooth_enable_show(struct device *dev,
2373 struct device_attribute *attr,
2378 status = bluetooth_get_radiosw();
2382 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2385 static ssize_t bluetooth_enable_store(struct device *dev,
2386 struct device_attribute *attr,
2387 const char *buf, size_t count)
2392 if (parse_strtoul(buf, 1, &t))
2395 res = bluetooth_set_radiosw(t);
2397 return (res) ? res : count;
2400 static struct device_attribute dev_attr_bluetooth_enable =
2401 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2402 bluetooth_enable_show, bluetooth_enable_store);
2404 /* --------------------------------------------------------------------- */
2406 static struct attribute *bluetooth_attributes[] = {
2407 &dev_attr_bluetooth_enable.attr,
2411 static const struct attribute_group bluetooth_attr_group = {
2412 .attrs = bluetooth_attributes,
2415 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2420 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2422 TPACPI_ACPIHANDLE_INIT(hkey);
2424 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2425 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2426 tp_features.bluetooth = hkey_handle &&
2427 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2429 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2430 str_supported(tp_features.bluetooth),
2433 if (tp_features.bluetooth) {
2434 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2435 /* no bluetooth hardware present in system */
2436 tp_features.bluetooth = 0;
2437 dbg_printk(TPACPI_DBG_INIT,
2438 "bluetooth hardware not installed\n");
2440 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2441 &bluetooth_attr_group);
2447 return (tp_features.bluetooth)? 0 : 1;
2450 static void bluetooth_exit(void)
2452 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2453 &bluetooth_attr_group);
2456 static int bluetooth_get_radiosw(void)
2460 if (!tp_features.bluetooth)
2463 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2466 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2469 static int bluetooth_set_radiosw(int radio_on)
2473 if (!tp_features.bluetooth)
2476 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2479 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2481 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2482 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2488 /* procfs -------------------------------------------------------------- */
2489 static int bluetooth_read(char *p)
2492 int status = bluetooth_get_radiosw();
2494 if (!tp_features.bluetooth)
2495 len += sprintf(p + len, "status:\t\tnot supported\n");
2497 len += sprintf(p + len, "status:\t\t%s\n",
2498 (status)? "enabled" : "disabled");
2499 len += sprintf(p + len, "commands:\tenable, disable\n");
2505 static int bluetooth_write(char *buf)
2509 if (!tp_features.bluetooth)
2512 while ((cmd = next_cmd(&buf))) {
2513 if (strlencmp(cmd, "enable") == 0) {
2514 bluetooth_set_radiosw(1);
2515 } else if (strlencmp(cmd, "disable") == 0) {
2516 bluetooth_set_radiosw(0);
2524 static struct ibm_struct bluetooth_driver_data = {
2525 .name = "bluetooth",
2526 .read = bluetooth_read,
2527 .write = bluetooth_write,
2528 .exit = bluetooth_exit,
2531 /*************************************************************************
2536 /* ACPI GWAN/SWAN bits */
2537 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2538 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2539 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2542 static int wan_get_radiosw(void);
2543 static int wan_set_radiosw(int radio_on);
2545 /* sysfs wan enable ---------------------------------------------------- */
2546 static ssize_t wan_enable_show(struct device *dev,
2547 struct device_attribute *attr,
2552 status = wan_get_radiosw();
2556 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2559 static ssize_t wan_enable_store(struct device *dev,
2560 struct device_attribute *attr,
2561 const char *buf, size_t count)
2566 if (parse_strtoul(buf, 1, &t))
2569 res = wan_set_radiosw(t);
2571 return (res) ? res : count;
2574 static struct device_attribute dev_attr_wan_enable =
2575 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2576 wan_enable_show, wan_enable_store);
2578 /* --------------------------------------------------------------------- */
2580 static struct attribute *wan_attributes[] = {
2581 &dev_attr_wan_enable.attr,
2585 static const struct attribute_group wan_attr_group = {
2586 .attrs = wan_attributes,
2589 static int __init wan_init(struct ibm_init_struct *iibm)
2594 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2596 TPACPI_ACPIHANDLE_INIT(hkey);
2598 tp_features.wan = hkey_handle &&
2599 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2601 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2602 str_supported(tp_features.wan),
2605 if (tp_features.wan) {
2606 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2607 /* no wan hardware present in system */
2608 tp_features.wan = 0;
2609 dbg_printk(TPACPI_DBG_INIT,
2610 "wan hardware not installed\n");
2612 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2619 return (tp_features.wan)? 0 : 1;
2622 static void wan_exit(void)
2624 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2628 static int wan_get_radiosw(void)
2632 if (!tp_features.wan)
2635 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2638 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2641 static int wan_set_radiosw(int radio_on)
2645 if (!tp_features.wan)
2648 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2651 status |= TP_ACPI_WANCARD_RADIOSSW;
2653 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2654 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2660 /* procfs -------------------------------------------------------------- */
2661 static int wan_read(char *p)
2664 int status = wan_get_radiosw();
2666 if (!tp_features.wan)
2667 len += sprintf(p + len, "status:\t\tnot supported\n");
2669 len += sprintf(p + len, "status:\t\t%s\n",
2670 (status)? "enabled" : "disabled");
2671 len += sprintf(p + len, "commands:\tenable, disable\n");
2677 static int wan_write(char *buf)
2681 if (!tp_features.wan)
2684 while ((cmd = next_cmd(&buf))) {
2685 if (strlencmp(cmd, "enable") == 0) {
2687 } else if (strlencmp(cmd, "disable") == 0) {
2696 static struct ibm_struct wan_driver_data = {
2701 .flags.experimental = 1,
2704 /*************************************************************************
2708 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2710 enum video_access_mode {
2711 TPACPI_VIDEO_NONE = 0,
2712 TPACPI_VIDEO_570, /* 570 */
2713 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2714 TPACPI_VIDEO_NEW, /* all others */
2717 enum { /* video status flags, based on VIDEO_570 */
2718 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2719 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2720 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2723 enum { /* TPACPI_VIDEO_570 constants */
2724 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2725 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2726 * video_status_flags */
2727 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2728 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2731 static enum video_access_mode video_supported;
2732 static int video_orig_autosw;
2734 static int video_autosw_get(void);
2735 static int video_autosw_set(int enable);
2737 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2739 static int __init video_init(struct ibm_init_struct *iibm)
2743 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2745 TPACPI_ACPIHANDLE_INIT(vid);
2746 TPACPI_ACPIHANDLE_INIT(vid2);
2748 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2749 /* G41, assume IVGA doesn't change */
2750 vid_handle = vid2_handle;
2753 /* video switching not supported on R30, R31 */
2754 video_supported = TPACPI_VIDEO_NONE;
2755 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2757 video_supported = TPACPI_VIDEO_570;
2758 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2759 /* 600e/x, 770e, 770x */
2760 video_supported = TPACPI_VIDEO_770;
2763 video_supported = TPACPI_VIDEO_NEW;
2765 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2766 str_supported(video_supported != TPACPI_VIDEO_NONE),
2769 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2772 static void video_exit(void)
2774 dbg_printk(TPACPI_DBG_EXIT,
2775 "restoring original video autoswitch mode\n");
2776 if (video_autosw_set(video_orig_autosw))
2777 printk(TPACPI_ERR "error while trying to restore original "
2778 "video autoswitch mode\n");
2781 static int video_outputsw_get(void)
2786 switch (video_supported) {
2787 case TPACPI_VIDEO_570:
2788 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2789 TP_ACPI_VIDEO_570_PHSCMD))
2791 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2793 case TPACPI_VIDEO_770:
2794 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2797 status |= TP_ACPI_VIDEO_S_LCD;
2798 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2801 status |= TP_ACPI_VIDEO_S_CRT;
2803 case TPACPI_VIDEO_NEW:
2804 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2805 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2808 status |= TP_ACPI_VIDEO_S_CRT;
2810 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2811 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2814 status |= TP_ACPI_VIDEO_S_LCD;
2815 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2818 status |= TP_ACPI_VIDEO_S_DVI;
2827 static int video_outputsw_set(int status)
2832 switch (video_supported) {
2833 case TPACPI_VIDEO_570:
2834 res = acpi_evalf(NULL, NULL,
2835 "\\_SB.PHS2", "vdd",
2836 TP_ACPI_VIDEO_570_PHS2CMD,
2837 status | TP_ACPI_VIDEO_570_PHS2SET);
2839 case TPACPI_VIDEO_770:
2840 autosw = video_autosw_get();
2844 res = video_autosw_set(1);
2847 res = acpi_evalf(vid_handle, NULL,
2848 "ASWT", "vdd", status * 0x100, 0);
2849 if (!autosw && video_autosw_set(autosw)) {
2851 "video auto-switch left enabled due to error\n");
2855 case TPACPI_VIDEO_NEW:
2856 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2857 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2863 return (res)? 0 : -EIO;
2866 static int video_autosw_get(void)
2870 switch (video_supported) {
2871 case TPACPI_VIDEO_570:
2872 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2875 case TPACPI_VIDEO_770:
2876 case TPACPI_VIDEO_NEW:
2877 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2887 static int video_autosw_set(int enable)
2889 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2894 static int video_outputsw_cycle(void)
2896 int autosw = video_autosw_get();
2902 switch (video_supported) {
2903 case TPACPI_VIDEO_570:
2904 res = video_autosw_set(1);
2907 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2909 case TPACPI_VIDEO_770:
2910 case TPACPI_VIDEO_NEW:
2911 res = video_autosw_set(1);
2914 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2919 if (!autosw && video_autosw_set(autosw)) {
2921 "video auto-switch left enabled due to error\n");
2925 return (res)? 0 : -EIO;
2928 static int video_expand_toggle(void)
2930 switch (video_supported) {
2931 case TPACPI_VIDEO_570:
2932 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2934 case TPACPI_VIDEO_770:
2935 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2937 case TPACPI_VIDEO_NEW:
2938 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2946 static int video_read(char *p)
2951 if (video_supported == TPACPI_VIDEO_NONE) {
2952 len += sprintf(p + len, "status:\t\tnot supported\n");
2956 status = video_outputsw_get();
2960 autosw = video_autosw_get();
2964 len += sprintf(p + len, "status:\t\tsupported\n");
2965 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2966 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2967 if (video_supported == TPACPI_VIDEO_NEW)
2968 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2969 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2970 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2971 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2972 if (video_supported == TPACPI_VIDEO_NEW)
2973 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2974 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2975 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2980 static int video_write(char *buf)
2983 int enable, disable, status;
2986 if (video_supported == TPACPI_VIDEO_NONE)
2992 while ((cmd = next_cmd(&buf))) {
2993 if (strlencmp(cmd, "lcd_enable") == 0) {
2994 enable |= TP_ACPI_VIDEO_S_LCD;
2995 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2996 disable |= TP_ACPI_VIDEO_S_LCD;
2997 } else if (strlencmp(cmd, "crt_enable") == 0) {
2998 enable |= TP_ACPI_VIDEO_S_CRT;
2999 } else if (strlencmp(cmd, "crt_disable") == 0) {
3000 disable |= TP_ACPI_VIDEO_S_CRT;
3001 } else if (video_supported == TPACPI_VIDEO_NEW &&
3002 strlencmp(cmd, "dvi_enable") == 0) {
3003 enable |= TP_ACPI_VIDEO_S_DVI;
3004 } else if (video_supported == TPACPI_VIDEO_NEW &&
3005 strlencmp(cmd, "dvi_disable") == 0) {
3006 disable |= TP_ACPI_VIDEO_S_DVI;
3007 } else if (strlencmp(cmd, "auto_enable") == 0) {
3008 res = video_autosw_set(1);
3011 } else if (strlencmp(cmd, "auto_disable") == 0) {
3012 res = video_autosw_set(0);
3015 } else if (strlencmp(cmd, "video_switch") == 0) {
3016 res = video_outputsw_cycle();
3019 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3020 res = video_expand_toggle();
3027 if (enable || disable) {
3028 status = video_outputsw_get();
3031 res = video_outputsw_set((status & ~disable) | enable);
3039 static struct ibm_struct video_driver_data = {
3042 .write = video_write,
3046 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3048 /*************************************************************************
3049 * Light (thinklight) subdriver
3052 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3053 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3055 static int __init light_init(struct ibm_init_struct *iibm)
3057 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3059 TPACPI_ACPIHANDLE_INIT(ledb);
3060 TPACPI_ACPIHANDLE_INIT(lght);
3061 TPACPI_ACPIHANDLE_INIT(cmos);
3063 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3064 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3066 if (tp_features.light)
3067 /* light status not supported on
3068 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3069 tp_features.light_status =
3070 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3072 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3073 str_supported(tp_features.light));
3075 return (tp_features.light)? 0 : 1;
3078 static int light_read(char *p)
3083 if (!tp_features.light) {
3084 len += sprintf(p + len, "status:\t\tnot supported\n");
3085 } else if (!tp_features.light_status) {
3086 len += sprintf(p + len, "status:\t\tunknown\n");
3087 len += sprintf(p + len, "commands:\ton, off\n");
3089 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3091 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3092 len += sprintf(p + len, "commands:\ton, off\n");
3098 static int light_write(char *buf)
3100 int cmos_cmd, lght_cmd;
3104 if (!tp_features.light)
3107 while ((cmd = next_cmd(&buf))) {
3108 if (strlencmp(cmd, "on") == 0) {
3111 } else if (strlencmp(cmd, "off") == 0) {
3117 success = cmos_handle ?
3118 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3119 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3127 static struct ibm_struct light_driver_data = {
3130 .write = light_write,
3133 /*************************************************************************
3137 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3139 static void dock_notify(struct ibm_struct *ibm, u32 event);
3140 static int dock_read(char *p);
3141 static int dock_write(char *buf);
3143 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3144 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3145 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3146 "\\_SB.PCI.ISA.SLCE", /* 570 */
3147 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3149 /* don't list other alternatives as we install a notify handler on the 570 */
3150 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3152 static const struct acpi_device_id ibm_pci_device_ids[] = {
3153 {PCI_ROOT_HID_STRING, 0},
3157 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3159 .notify = dock_notify,
3160 .handle = &dock_handle,
3161 .type = ACPI_SYSTEM_NOTIFY,
3164 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3165 * We just use it to get notifications of dock hotplug
3166 * in very old thinkpads */
3167 .hid = ibm_pci_device_ids,
3168 .notify = dock_notify,
3169 .handle = &pci_handle,
3170 .type = ACPI_SYSTEM_NOTIFY,
3174 static struct ibm_struct dock_driver_data[2] = {
3178 .write = dock_write,
3179 .acpi = &ibm_dock_acpidriver[0],
3183 .acpi = &ibm_dock_acpidriver[1],
3187 #define dock_docked() (_sta(dock_handle) & 1)
3189 static int __init dock_init(struct ibm_init_struct *iibm)
3191 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3193 TPACPI_ACPIHANDLE_INIT(dock);
3195 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3196 str_supported(dock_handle != NULL));
3198 return (dock_handle)? 0 : 1;
3201 static int __init dock_init2(struct ibm_init_struct *iibm)
3205 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3207 if (dock_driver_data[0].flags.acpi_driver_registered &&
3208 dock_driver_data[0].flags.acpi_notify_installed) {
3209 TPACPI_ACPIHANDLE_INIT(pci);
3210 dock2_needed = (pci_handle != NULL);
3211 vdbg_printk(TPACPI_DBG_INIT,
3212 "dock PCI handler for the TP 570 is %s\n",
3213 str_supported(dock2_needed));
3215 vdbg_printk(TPACPI_DBG_INIT,
3216 "dock subdriver part 2 not required\n");
3220 return (dock2_needed)? 0 : 1;
3223 static void dock_notify(struct ibm_struct *ibm, u32 event)
3225 int docked = dock_docked();
3226 int pci = ibm->acpi->hid && ibm->acpi->device &&
3227 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3230 if (event == 1 && !pci) /* 570 */
3231 data = 1; /* button */
3232 else if (event == 1 && pci) /* 570 */
3233 data = 3; /* dock */
3234 else if (event == 3 && docked)
3235 data = 1; /* button */
3236 else if (event == 3 && !docked)
3237 data = 2; /* undock */
3238 else if (event == 0 && docked)
3239 data = 3; /* dock */
3241 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3242 event, _sta(dock_handle));
3243 data = 0; /* unknown */
3245 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3246 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3247 ibm->acpi->device->dev.bus_id,
3251 static int dock_read(char *p)
3254 int docked = dock_docked();
3257 len += sprintf(p + len, "status:\t\tnot supported\n");
3259 len += sprintf(p + len, "status:\t\tundocked\n");
3261 len += sprintf(p + len, "status:\t\tdocked\n");
3262 len += sprintf(p + len, "commands:\tdock, undock\n");
3268 static int dock_write(char *buf)
3275 while ((cmd = next_cmd(&buf))) {
3276 if (strlencmp(cmd, "undock") == 0) {
3277 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3278 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3280 } else if (strlencmp(cmd, "dock") == 0) {
3281 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3290 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3292 /*************************************************************************
3296 #ifdef CONFIG_THINKPAD_ACPI_BAY
3298 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3299 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3300 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3301 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3302 ); /* A21e, R30, R31 */
3303 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3304 "_EJ0", /* all others */
3305 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3306 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3307 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3309 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3313 static int __init bay_init(struct ibm_init_struct *iibm)
3315 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3317 TPACPI_ACPIHANDLE_INIT(bay);
3319 TPACPI_ACPIHANDLE_INIT(bay_ej);
3320 TPACPI_ACPIHANDLE_INIT(bay2);
3322 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3324 tp_features.bay_status = bay_handle &&
3325 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3326 tp_features.bay_status2 = bay2_handle &&
3327 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3329 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3330 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3331 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3332 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3334 vdbg_printk(TPACPI_DBG_INIT,
3335 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3336 str_supported(tp_features.bay_status),
3337 str_supported(tp_features.bay_eject),
3338 str_supported(tp_features.bay_status2),
3339 str_supported(tp_features.bay_eject2));
3341 return (tp_features.bay_status || tp_features.bay_eject ||
3342 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3345 static void bay_notify(struct ibm_struct *ibm, u32 event)
3347 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3348 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3349 ibm->acpi->device->dev.bus_id,
3353 #define bay_occupied(b) (_sta(b##_handle) & 1)
3355 static int bay_read(char *p)
3358 int occupied = bay_occupied(bay);
3359 int occupied2 = bay_occupied(bay2);
3362 len += sprintf(p + len, "status:\t\t%s\n",
3363 tp_features.bay_status ?
3364 (occupied ? "occupied" : "unoccupied") :
3366 if (tp_features.bay_status2)
3367 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3368 "occupied" : "unoccupied");
3370 eject = tp_features.bay_eject && occupied;
3371 eject2 = tp_features.bay_eject2 && occupied2;
3373 if (eject && eject2)
3374 len += sprintf(p + len, "commands:\teject, eject2\n");
3376 len += sprintf(p + len, "commands:\teject\n");
3378 len += sprintf(p + len, "commands:\teject2\n");
3383 static int bay_write(char *buf)
3387 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3390 while ((cmd = next_cmd(&buf))) {
3391 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3392 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3394 } else if (tp_features.bay_eject2 &&
3395 strlencmp(cmd, "eject2") == 0) {
3396 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3405 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3406 .notify = bay_notify,
3407 .handle = &bay_handle,
3408 .type = ACPI_SYSTEM_NOTIFY,
3411 static struct ibm_struct bay_driver_data = {
3415 .acpi = &ibm_bay_acpidriver,
3418 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3420 /*************************************************************************
3424 /* sysfs cmos_command -------------------------------------------------- */
3425 static ssize_t cmos_command_store(struct device *dev,
3426 struct device_attribute *attr,
3427 const char *buf, size_t count)
3429 unsigned long cmos_cmd;
3432 if (parse_strtoul(buf, 21, &cmos_cmd))
3435 res = issue_thinkpad_cmos_command(cmos_cmd);
3436 return (res)? res : count;
3439 static struct device_attribute dev_attr_cmos_command =
3440 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3442 /* --------------------------------------------------------------------- */
3444 static int __init cmos_init(struct ibm_init_struct *iibm)
3448 vdbg_printk(TPACPI_DBG_INIT,
3449 "initializing cmos commands subdriver\n");
3451 TPACPI_ACPIHANDLE_INIT(cmos);
3453 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3454 str_supported(cmos_handle != NULL));
3456 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3460 return (cmos_handle)? 0 : 1;
3463 static void cmos_exit(void)
3465 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3468 static int cmos_read(char *p)
3472 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3473 R30, R31, T20-22, X20-21 */
3475 len += sprintf(p + len, "status:\t\tnot supported\n");
3477 len += sprintf(p + len, "status:\t\tsupported\n");
3478 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3484 static int cmos_write(char *buf)
3489 while ((cmd = next_cmd(&buf))) {
3490 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3491 cmos_cmd >= 0 && cmos_cmd <= 21) {
3496 res = issue_thinkpad_cmos_command(cmos_cmd);
3504 static struct ibm_struct cmos_driver_data = {
3507 .write = cmos_write,
3511 /*************************************************************************
3515 enum led_access_mode {
3516 TPACPI_LED_NONE = 0,
3517 TPACPI_LED_570, /* 570 */
3518 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3519 TPACPI_LED_NEW, /* all others */
3522 enum { /* For TPACPI_LED_OLD */
3523 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3524 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3525 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3528 static enum led_access_mode led_supported;
3530 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3531 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3532 /* T20-22, X20-21 */
3533 "LED", /* all others */
3536 static int __init led_init(struct ibm_init_struct *iibm)
3538 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3540 TPACPI_ACPIHANDLE_INIT(led);
3543 /* led not supported on R30, R31 */
3544 led_supported = TPACPI_LED_NONE;
3545 else if (strlencmp(led_path, "SLED") == 0)
3547 led_supported = TPACPI_LED_570;
3548 else if (strlencmp(led_path, "SYSL") == 0)
3549 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3550 led_supported = TPACPI_LED_OLD;
3553 led_supported = TPACPI_LED_NEW;
3555 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3556 str_supported(led_supported), led_supported);
3558 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3561 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3563 static int led_read(char *p)
3567 if (!led_supported) {
3568 len += sprintf(p + len, "status:\t\tnot supported\n");
3571 len += sprintf(p + len, "status:\t\tsupported\n");
3573 if (led_supported == TPACPI_LED_570) {
3576 for (i = 0; i < 8; i++) {
3577 if (!acpi_evalf(ec_handle,
3578 &status, "GLED", "dd", 1 << i))
3580 len += sprintf(p + len, "%d:\t\t%s\n",
3581 i, led_status(status));
3585 len += sprintf(p + len, "commands:\t"
3586 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3591 /* off, on, blink */
3592 static const int led_sled_arg1[] = { 0, 1, 3 };
3593 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3594 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3595 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3597 static int led_write(char *buf)
3605 while ((cmd = next_cmd(&buf))) {
3606 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3609 if (strstr(cmd, "off")) {
3611 } else if (strstr(cmd, "on")) {
3613 } else if (strstr(cmd, "blink")) {
3618 if (led_supported == TPACPI_LED_570) {
3621 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3622 led, led_sled_arg1[ind]))
3624 } else if (led_supported == TPACPI_LED_OLD) {
3625 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3627 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3629 ret = ec_write(TPACPI_LED_EC_HLBL,
3630 led * led_exp_hlbl[ind]);
3632 ret = ec_write(TPACPI_LED_EC_HLCL,
3633 led * led_exp_hlcl[ind]);
3638 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3639 led, led_led_arg1[ind]))
3647 static struct ibm_struct led_driver_data = {
3653 /*************************************************************************
3657 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3659 static int __init beep_init(struct ibm_init_struct *iibm)
3661 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3663 TPACPI_ACPIHANDLE_INIT(beep);
3665 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3666 str_supported(beep_handle != NULL));
3668 return (beep_handle)? 0 : 1;
3671 static int beep_read(char *p)
3676 len += sprintf(p + len, "status:\t\tnot supported\n");
3678 len += sprintf(p + len, "status:\t\tsupported\n");
3679 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3685 static int beep_write(char *buf)
3693 while ((cmd = next_cmd(&buf))) {
3694 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3695 beep_cmd >= 0 && beep_cmd <= 17) {
3699 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3706 static struct ibm_struct beep_driver_data = {
3709 .write = beep_write,
3712 /*************************************************************************
3716 enum thermal_access_mode {
3717 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3718 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3719 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3720 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3721 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3724 enum { /* TPACPI_THERMAL_TPEC_* */
3725 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3726 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3727 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3730 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3731 struct ibm_thermal_sensors_struct {
3732 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3735 static enum thermal_access_mode thermal_read_mode;
3737 /* idx is zero-based */
3738 static int thermal_get_sensor(int idx, s32 *value)
3744 t = TP_EC_THERMAL_TMP0;
3746 switch (thermal_read_mode) {
3747 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3748 case TPACPI_THERMAL_TPEC_16:
3749 if (idx >= 8 && idx <= 15) {
3750 t = TP_EC_THERMAL_TMP8;
3755 case TPACPI_THERMAL_TPEC_8:
3757 if (!acpi_ec_read(t + idx, &tmp))
3759 *value = tmp * 1000;
3764 case TPACPI_THERMAL_ACPI_UPDT:
3766 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3767 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3769 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3771 *value = (t - 2732) * 100;
3776 case TPACPI_THERMAL_ACPI_TMP07:
3778 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3779 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3781 if (t > 127 || t < -127)
3782 t = TP_EC_THERMAL_TMP_NA;
3788 case TPACPI_THERMAL_NONE:
3796 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3807 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3810 for (i = 0 ; i < n; i++) {
3811 res = thermal_get_sensor(i, &s->temp[i]);
3819 /* sysfs temp##_input -------------------------------------------------- */
3821 static ssize_t thermal_temp_input_show(struct device *dev,
3822 struct device_attribute *attr,
3825 struct sensor_device_attribute *sensor_attr =
3826 to_sensor_dev_attr(attr);
3827 int idx = sensor_attr->index;
3831 res = thermal_get_sensor(idx, &value);
3834 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3837 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3840 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3841 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3842 thermal_temp_input_show, NULL, _idxB)
3844 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3845 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3846 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3847 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3848 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3849 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3850 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3851 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3852 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3853 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3854 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3855 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3856 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3857 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3858 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3859 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3860 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3863 #define THERMAL_ATTRS(X) \
3864 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3866 static struct attribute *thermal_temp_input_attr[] = {
3886 static const struct attribute_group thermal_temp_input16_group = {
3887 .attrs = thermal_temp_input_attr
3890 static const struct attribute_group thermal_temp_input8_group = {
3891 .attrs = &thermal_temp_input_attr[8]
3894 #undef THERMAL_SENSOR_ATTR_TEMP
3895 #undef THERMAL_ATTRS
3897 /* --------------------------------------------------------------------- */
3899 static int __init thermal_init(struct ibm_init_struct *iibm)
3906 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3908 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3910 if (thinkpad_id.ec_model) {
3912 * Direct EC access mode: sensors at registers
3913 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3914 * non-implemented, thermal sensors return 0x80 when
3919 for (i = 0; i < 8; i++) {
3920 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3926 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3934 /* This is sheer paranoia, but we handle it anyway */
3937 "ThinkPad ACPI EC access misbehaving, "
3938 "falling back to ACPI TMPx access "
3940 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3943 "ThinkPad ACPI EC access misbehaving, "
3944 "disabling thermal sensors access\n");
3945 thermal_read_mode = TPACPI_THERMAL_NONE;
3950 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3952 } else if (acpi_tmp7) {
3953 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3954 /* 600e/x, 770e, 770x */
3955 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3957 /* Standard ACPI TMPx access, max 8 sensors */
3958 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3961 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3962 thermal_read_mode = TPACPI_THERMAL_NONE;
3965 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3966 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3969 switch (thermal_read_mode) {
3970 case TPACPI_THERMAL_TPEC_16:
3971 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3972 &thermal_temp_input16_group);
3976 case TPACPI_THERMAL_TPEC_8:
3977 case TPACPI_THERMAL_ACPI_TMP07:
3978 case TPACPI_THERMAL_ACPI_UPDT:
3979 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3980 &thermal_temp_input8_group);
3984 case TPACPI_THERMAL_NONE:
3992 static void thermal_exit(void)
3994 switch (thermal_read_mode) {
3995 case TPACPI_THERMAL_TPEC_16:
3996 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3997 &thermal_temp_input16_group);
3999 case TPACPI_THERMAL_TPEC_8:
4000 case TPACPI_THERMAL_ACPI_TMP07:
4001 case TPACPI_THERMAL_ACPI_UPDT:
4002 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4003 &thermal_temp_input16_group);
4005 case TPACPI_THERMAL_NONE:
4011 static int thermal_read(char *p)
4015 struct ibm_thermal_sensors_struct t;
4017 n = thermal_get_sensors(&t);
4018 if (unlikely(n < 0))
4021 len += sprintf(p + len, "temperatures:\t");
4024 for (i = 0; i < (n - 1); i++)
4025 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4026 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4028 len += sprintf(p + len, "not supported\n");
4033 static struct ibm_struct thermal_driver_data = {
4035 .read = thermal_read,
4036 .exit = thermal_exit,
4039 /*************************************************************************
4043 static u8 ecdump_regs[256];
4045 static int ecdump_read(char *p)
4051 len += sprintf(p + len, "EC "
4052 " +00 +01 +02 +03 +04 +05 +06 +07"
4053 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4054 for (i = 0; i < 256; i += 16) {
4055 len += sprintf(p + len, "EC 0x%02x:", i);
4056 for (j = 0; j < 16; j++) {
4057 if (!acpi_ec_read(i + j, &v))
4059 if (v != ecdump_regs[i + j])
4060 len += sprintf(p + len, " *%02x", v);
4062 len += sprintf(p + len, " %02x", v);
4063 ecdump_regs[i + j] = v;
4065 len += sprintf(p + len, "\n");
4070 /* These are way too dangerous to advertise openly... */
4072 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4073 " (<offset> is 00-ff, <value> is 00-ff)\n");
4074 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4075 " (<offset> is 00-ff, <value> is 0-255)\n");
4080 static int ecdump_write(char *buf)
4085 while ((cmd = next_cmd(&buf))) {
4086 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4088 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4092 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4093 if (!acpi_ec_write(i, v))
4102 static struct ibm_struct ecdump_driver_data = {
4104 .read = ecdump_read,
4105 .write = ecdump_write,
4106 .flags.experimental = 1,
4109 /*************************************************************************
4110 * Backlight/brightness subdriver
4113 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4115 static struct backlight_device *ibm_backlight_device;
4116 static int brightness_offset = 0x31;
4117 static int brightness_mode;
4118 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4120 static struct mutex brightness_mutex;
4123 * ThinkPads can read brightness from two places: EC 0x31, or
4124 * CMOS NVRAM byte 0x5E, bits 0-3.
4126 static int brightness_get(struct backlight_device *bd)
4128 u8 lec = 0, lcmos = 0, level = 0;
4130 if (brightness_mode & 1) {
4131 if (!acpi_ec_read(brightness_offset, &lec))
4133 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4136 if (brightness_mode & 2) {
4137 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4138 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4139 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4140 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4144 if (brightness_mode == 3 && lec != lcmos) {
4146 "CMOS NVRAM (%u) and EC (%u) do not agree "
4147 "on display brightness level\n",
4148 (unsigned int) lcmos,
4149 (unsigned int) lec);
4156 /* May return EINTR which can always be mapped to ERESTARTSYS */
4157 static int brightness_set(int value)
4159 int cmos_cmd, inc, i, res;
4162 if (value > ((tp_features.bright_16levels)? 15 : 7))
4165 res = mutex_lock_interruptible(&brightness_mutex);
4169 current_value = brightness_get(NULL);
4170 if (current_value < 0) {
4171 res = current_value;
4175 cmos_cmd = value > current_value ?
4176 TP_CMOS_BRIGHTNESS_UP :
4177 TP_CMOS_BRIGHTNESS_DOWN;
4178 inc = (value > current_value)? 1 : -1;
4181 for (i = current_value; i != value; i += inc) {
4182 if ((brightness_mode & 2) &&
4183 issue_thinkpad_cmos_command(cmos_cmd)) {
4187 if ((brightness_mode & 1) &&
4188 !acpi_ec_write(brightness_offset, i + inc)) {
4195 mutex_unlock(&brightness_mutex);
4199 /* sysfs backlight class ----------------------------------------------- */
4201 static int brightness_update_status(struct backlight_device *bd)
4203 /* it is the backlight class's job (caller) to handle
4204 * EINTR and other errors properly */
4205 return brightness_set(
4206 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4207 bd->props.power == FB_BLANK_UNBLANK) ?
4208 bd->props.brightness : 0);
4211 static struct backlight_ops ibm_backlight_data = {
4212 .get_brightness = brightness_get,
4213 .update_status = brightness_update_status,
4216 /* --------------------------------------------------------------------- */
4218 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4220 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4221 union acpi_object *obj;
4224 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4225 obj = (union acpi_object *)buffer.pointer;
4226 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4227 printk(TPACPI_ERR "Unknown BCLL data, "
4228 "please report this to %s\n", TPACPI_MAIL);
4231 rc = obj->package.count;
4237 kfree(buffer.pointer);
4241 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4242 void *context, void **rv)
4244 char name[ACPI_PATH_SEGMENT_LENGTH];
4245 struct acpi_buffer buffer = { sizeof(name), &name };
4247 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4248 !strncmp("BCLL", name, sizeof(name) - 1)) {
4249 if (tpacpi_query_bcll_levels(handle) == 16) {
4251 return AE_CTRL_TERMINATE;
4260 static int __init brightness_check_levels(void)
4263 void *found_node = NULL;
4266 TPACPI_ACPIHANDLE_INIT(vid);
4271 /* Search for a BCLL package with 16 levels */
4272 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4273 brightness_find_bcll, NULL,
4276 return (ACPI_SUCCESS(status) && found_node != NULL);
4279 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4280 void *context, void **rv)
4282 char name[ACPI_PATH_SEGMENT_LENGTH];
4283 struct acpi_buffer buffer = { sizeof(name), &name };
4285 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4286 !strncmp("_BCL", name, sizeof(name) - 1)) {
4288 return AE_CTRL_TERMINATE;
4294 static int __init brightness_check_std_acpi_support(void)
4297 void *found_node = NULL;
4300 TPACPI_ACPIHANDLE_INIT(vid);
4305 /* Search for a _BCL method, but don't execute it */
4306 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4307 brightness_find_bcl, NULL, &found_node);
4309 return (ACPI_SUCCESS(status) && found_node != NULL);
4312 static int __init brightness_init(struct ibm_init_struct *iibm)
4316 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4318 mutex_init(&brightness_mutex);
4320 if (!brightness_enable) {
4321 dbg_printk(TPACPI_DBG_INIT,
4322 "brightness support disabled by "
4323 "module parameter\n");
4325 } else if (brightness_enable > 1) {
4326 if (brightness_check_std_acpi_support()) {
4327 printk(TPACPI_NOTICE
4328 "standard ACPI backlight interface "
4329 "available, not loading native one...\n");
4334 if (!brightness_mode) {
4335 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4336 brightness_mode = 2;
4338 brightness_mode = 3;
4340 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4344 if (brightness_mode > 3)
4347 tp_features.bright_16levels =
4348 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4349 brightness_check_levels();
4351 b = brightness_get(NULL);
4355 if (tp_features.bright_16levels)
4357 "detected a 16-level brightness capable ThinkPad\n");
4359 ibm_backlight_device = backlight_device_register(
4360 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4361 &ibm_backlight_data);
4362 if (IS_ERR(ibm_backlight_device)) {
4363 printk(TPACPI_ERR "Could not register backlight device\n");
4364 return PTR_ERR(ibm_backlight_device);
4366 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4368 ibm_backlight_device->props.max_brightness =
4369 (tp_features.bright_16levels)? 15 : 7;
4370 ibm_backlight_device->props.brightness = b;
4371 backlight_update_status(ibm_backlight_device);
4376 static void brightness_exit(void)
4378 if (ibm_backlight_device) {
4379 vdbg_printk(TPACPI_DBG_EXIT,
4380 "calling backlight_device_unregister()\n");
4381 backlight_device_unregister(ibm_backlight_device);
4382 ibm_backlight_device = NULL;
4386 static int brightness_read(char *p)
4391 level = brightness_get(NULL);
4393 len += sprintf(p + len, "level:\t\tunreadable\n");
4395 len += sprintf(p + len, "level:\t\t%d\n", level);
4396 len += sprintf(p + len, "commands:\tup, down\n");
4397 len += sprintf(p + len, "commands:\tlevel <level>"
4398 " (<level> is 0-%d)\n",
4399 (tp_features.bright_16levels) ? 15 : 7);
4405 static int brightness_write(char *buf)
4410 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4412 level = brightness_get(NULL);
4416 while ((cmd = next_cmd(&buf))) {
4417 if (strlencmp(cmd, "up") == 0) {
4418 if (level < max_level)
4420 } else if (strlencmp(cmd, "down") == 0) {
4423 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4424 level >= 0 && level <= max_level) {
4431 * Now we know what the final level should be, so we try to set it.
4432 * Doing it this way makes the syscall restartable in case of EINTR
4434 rc = brightness_set(level);
4435 return (rc == -EINTR)? ERESTARTSYS : rc;
4438 static struct ibm_struct brightness_driver_data = {
4439 .name = "brightness",
4440 .read = brightness_read,
4441 .write = brightness_write,
4442 .exit = brightness_exit,
4445 /*************************************************************************
4449 static int volume_offset = 0x30;
4451 static int volume_read(char *p)
4456 if (!acpi_ec_read(volume_offset, &level)) {
4457 len += sprintf(p + len, "level:\t\tunreadable\n");
4459 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4460 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4461 len += sprintf(p + len, "commands:\tup, down, mute\n");
4462 len += sprintf(p + len, "commands:\tlevel <level>"
4463 " (<level> is 0-15)\n");
4469 static int volume_write(char *buf)
4471 int cmos_cmd, inc, i;
4473 int new_level, new_mute;
4476 while ((cmd = next_cmd(&buf))) {
4477 if (!acpi_ec_read(volume_offset, &level))
4479 new_mute = mute = level & 0x40;
4480 new_level = level = level & 0xf;
4482 if (strlencmp(cmd, "up") == 0) {
4486 new_level = level == 15 ? 15 : level + 1;
4487 } else if (strlencmp(cmd, "down") == 0) {
4491 new_level = level == 0 ? 0 : level - 1;
4492 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4493 new_level >= 0 && new_level <= 15) {
4495 } else if (strlencmp(cmd, "mute") == 0) {
4500 if (new_level != level) {
4501 /* mute doesn't change */
4503 cmos_cmd = (new_level > level) ?
4504 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4505 inc = new_level > level ? 1 : -1;
4507 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4508 !acpi_ec_write(volume_offset, level)))
4511 for (i = level; i != new_level; i += inc)
4512 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4513 !acpi_ec_write(volume_offset, i + inc))
4517 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4518 !acpi_ec_write(volume_offset, new_level + mute))) {
4523 if (new_mute != mute) {
4524 /* level doesn't change */
4526 cmos_cmd = (new_mute) ?
4527 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4529 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4530 !acpi_ec_write(volume_offset, level + new_mute))
4538 static struct ibm_struct volume_driver_data = {
4540 .read = volume_read,
4541 .write = volume_write,
4544 /*************************************************************************
4551 * TPACPI_FAN_RD_ACPI_GFAN:
4552 * ACPI GFAN method: returns fan level
4554 * see TPACPI_FAN_WR_ACPI_SFAN
4555 * EC 0x2f (HFSP) not available if GFAN exists
4557 * TPACPI_FAN_WR_ACPI_SFAN:
4558 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4560 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4563 * TPACPI_FAN_WR_TPEC:
4564 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4565 * Supported on almost all ThinkPads
4567 * Fan speed changes of any sort (including those caused by the
4568 * disengaged mode) are usually done slowly by the firmware as the
4569 * maximum ammount of fan duty cycle change per second seems to be
4572 * Reading is not available if GFAN exists.
4573 * Writing is not available if SFAN exists.
4576 * 7 automatic mode engaged;
4577 * (default operation mode of the ThinkPad)
4578 * fan level is ignored in this mode.
4579 * 6 full speed mode (takes precedence over bit 7);
4580 * not available on all thinkpads. May disable
4581 * the tachometer while the fan controller ramps up
4582 * the speed (which can take up to a few *minutes*).
4583 * Speeds up fan to 100% duty-cycle, which is far above
4584 * the standard RPM levels. It is not impossible that
4585 * it could cause hardware damage.
4586 * 5-3 unused in some models. Extra bits for fan level
4587 * in others, but still useless as all values above
4588 * 7 map to the same speed as level 7 in these models.
4589 * 2-0 fan level (0..7 usually)
4591 * 0x07 = max (set when temperatures critical)
4592 * Some ThinkPads may have other levels, see
4593 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4595 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4596 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4597 * does so, its initial value is meaningless (0x07).
4599 * For firmware bugs, refer to:
4600 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4604 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4605 * Main fan tachometer reading (in RPM)
4607 * This register is present on all ThinkPads with a new-style EC, and
4608 * it is known not to be present on the A21m/e, and T22, as there is
4609 * something else in offset 0x84 according to the ACPI DSDT. Other
4610 * ThinkPads from this same time period (and earlier) probably lack the
4611 * tachometer as well.
4613 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4614 * was never fixed by IBM to report the EC firmware version string
4615 * probably support the tachometer (like the early X models), so
4616 * detecting it is quite hard. We need more data to know for sure.
4618 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4621 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4624 * For firmware bugs, refer to:
4625 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4627 * TPACPI_FAN_WR_ACPI_FANS:
4628 * ThinkPad X31, X40, X41. Not available in the X60.
4630 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4631 * high speed. ACPI DSDT seems to map these three speeds to levels
4632 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4633 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4635 * The speeds are stored on handles
4636 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4638 * There are three default speed sets, acessible as handles:
4639 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4641 * ACPI DSDT switches which set is in use depending on various
4644 * TPACPI_FAN_WR_TPEC is also available and should be used to
4645 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4646 * but the ACPI tables just mention level 7.
4649 enum { /* Fan control constants */
4650 fan_status_offset = 0x2f, /* EC register 0x2f */
4651 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4652 * 0x84 must be read before 0x85 */
4654 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4655 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4657 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4660 enum fan_status_access_mode {
4661 TPACPI_FAN_NONE = 0, /* No fan status or control */
4662 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4663 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4666 enum fan_control_access_mode {
4667 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4668 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4669 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4670 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4673 enum fan_control_commands {
4674 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4675 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4676 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4677 * and also watchdog cmd */
4680 static int fan_control_allowed;
4682 static enum fan_status_access_mode fan_status_access_mode;
4683 static enum fan_control_access_mode fan_control_access_mode;
4684 static enum fan_control_commands fan_control_commands;
4686 static u8 fan_control_initial_status;
4687 static u8 fan_control_desired_level;
4688 static int fan_watchdog_maxinterval;
4690 static struct mutex fan_mutex;
4692 static void fan_watchdog_fire(struct work_struct *ignored);
4693 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4695 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4696 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4697 "\\FSPD", /* 600e/x, 770e, 770x */
4699 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4700 "JFNS", /* 770x-JL */
4704 * Call with fan_mutex held
4706 static void fan_update_desired_level(u8 status)
4709 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4711 fan_control_desired_level = 7;
4713 fan_control_desired_level = status;
4717 static int fan_get_status(u8 *status)
4722 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4724 switch (fan_status_access_mode) {
4725 case TPACPI_FAN_RD_ACPI_GFAN:
4726 /* 570, 600e/x, 770e, 770x */
4728 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4736 case TPACPI_FAN_RD_TPEC:
4737 /* all except 570, 600e/x, 770e, 770x */
4738 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4753 static int fan_get_status_safe(u8 *status)
4758 if (mutex_lock_interruptible(&fan_mutex))
4759 return -ERESTARTSYS;
4760 rc = fan_get_status(&s);
4762 fan_update_desired_level(s);
4763 mutex_unlock(&fan_mutex);
4771 static int fan_get_speed(unsigned int *speed)
4775 switch (fan_status_access_mode) {
4776 case TPACPI_FAN_RD_TPEC:
4777 /* all except 570, 600e/x, 770e, 770x */
4778 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4779 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4783 *speed = (hi << 8) | lo;
4794 static int fan_set_level(int level)
4796 if (!fan_control_allowed)
4799 switch (fan_control_access_mode) {
4800 case TPACPI_FAN_WR_ACPI_SFAN:
4801 if (level >= 0 && level <= 7) {
4802 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4808 case TPACPI_FAN_WR_ACPI_FANS:
4809 case TPACPI_FAN_WR_TPEC:
4810 if ((level != TP_EC_FAN_AUTO) &&
4811 (level != TP_EC_FAN_FULLSPEED) &&
4812 ((level < 0) || (level > 7)))
4815 /* safety net should the EC not support AUTO
4816 * or FULLSPEED mode bits and just ignore them */
4817 if (level & TP_EC_FAN_FULLSPEED)
4818 level |= 7; /* safety min speed 7 */
4819 else if (level & TP_EC_FAN_AUTO)
4820 level |= 4; /* safety min speed 4 */
4822 if (!acpi_ec_write(fan_status_offset, level))
4825 tp_features.fan_ctrl_status_undef = 0;
4834 static int fan_set_level_safe(int level)
4838 if (!fan_control_allowed)
4841 if (mutex_lock_interruptible(&fan_mutex))
4842 return -ERESTARTSYS;
4844 if (level == TPACPI_FAN_LAST_LEVEL)
4845 level = fan_control_desired_level;
4847 rc = fan_set_level(level);
4849 fan_update_desired_level(level);
4851 mutex_unlock(&fan_mutex);
4855 static int fan_set_enable(void)
4860 if (!fan_control_allowed)
4863 if (mutex_lock_interruptible(&fan_mutex))
4864 return -ERESTARTSYS;
4866 switch (fan_control_access_mode) {
4867 case TPACPI_FAN_WR_ACPI_FANS:
4868 case TPACPI_FAN_WR_TPEC:
4869 rc = fan_get_status(&s);
4873 /* Don't go out of emergency fan mode */
4876 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4879 if (!acpi_ec_write(fan_status_offset, s))
4882 tp_features.fan_ctrl_status_undef = 0;
4887 case TPACPI_FAN_WR_ACPI_SFAN:
4888 rc = fan_get_status(&s);
4894 /* Set fan to at least level 4 */
4897 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4907 mutex_unlock(&fan_mutex);
4911 static int fan_set_disable(void)
4915 if (!fan_control_allowed)
4918 if (mutex_lock_interruptible(&fan_mutex))
4919 return -ERESTARTSYS;
4922 switch (fan_control_access_mode) {
4923 case TPACPI_FAN_WR_ACPI_FANS:
4924 case TPACPI_FAN_WR_TPEC:
4925 if (!acpi_ec_write(fan_status_offset, 0x00))
4928 fan_control_desired_level = 0;
4929 tp_features.fan_ctrl_status_undef = 0;
4933 case TPACPI_FAN_WR_ACPI_SFAN:
4934 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4937 fan_control_desired_level = 0;
4945 mutex_unlock(&fan_mutex);
4949 static int fan_set_speed(int speed)
4953 if (!fan_control_allowed)
4956 if (mutex_lock_interruptible(&fan_mutex))
4957 return -ERESTARTSYS;
4960 switch (fan_control_access_mode) {
4961 case TPACPI_FAN_WR_ACPI_FANS:
4962 if (speed >= 0 && speed <= 65535) {
4963 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4964 speed, speed, speed))
4974 mutex_unlock(&fan_mutex);
4978 static void fan_watchdog_reset(void)
4980 static int fan_watchdog_active;
4982 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4985 if (fan_watchdog_active)
4986 cancel_delayed_work(&fan_watchdog_task);
4988 if (fan_watchdog_maxinterval > 0 &&
4989 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4990 fan_watchdog_active = 1;
4991 if (!schedule_delayed_work(&fan_watchdog_task,
4992 msecs_to_jiffies(fan_watchdog_maxinterval
4995 "failed to schedule the fan watchdog, "
4996 "watchdog will not trigger\n");
4999 fan_watchdog_active = 0;
5002 static void fan_watchdog_fire(struct work_struct *ignored)
5006 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5009 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5010 rc = fan_set_enable();
5012 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5013 "will try again later...\n", -rc);
5014 /* reschedule for later */
5015 fan_watchdog_reset();
5020 * SYSFS fan layout: hwmon compatible (device)
5023 * 0: "disengaged" mode
5025 * 2: native EC "auto" mode (recommended, hardware default)
5027 * pwm*: set speed in manual mode, ignored otherwise.
5028 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5031 * fan*_input: tachometer reading, RPM
5034 * SYSFS fan layout: extensions
5036 * fan_watchdog (driver):
5037 * fan watchdog interval in seconds, 0 disables (default), max 120
5040 /* sysfs fan pwm1_enable ----------------------------------------------- */
5041 static ssize_t fan_pwm1_enable_show(struct device *dev,
5042 struct device_attribute *attr,
5048 res = fan_get_status_safe(&status);
5052 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5053 if (status != fan_control_initial_status) {
5054 tp_features.fan_ctrl_status_undef = 0;
5056 /* Return most likely status. In fact, it
5057 * might be the only possible status */
5058 status = TP_EC_FAN_AUTO;
5062 if (status & TP_EC_FAN_FULLSPEED) {
5064 } else if (status & TP_EC_FAN_AUTO) {
5069 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5072 static ssize_t fan_pwm1_enable_store(struct device *dev,
5073 struct device_attribute *attr,
5074 const char *buf, size_t count)
5079 if (parse_strtoul(buf, 2, &t))
5084 level = TP_EC_FAN_FULLSPEED;
5087 level = TPACPI_FAN_LAST_LEVEL;
5090 level = TP_EC_FAN_AUTO;
5093 /* reserved for software-controlled auto mode */
5099 res = fan_set_level_safe(level);
5105 fan_watchdog_reset();
5110 static struct device_attribute dev_attr_fan_pwm1_enable =
5111 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5112 fan_pwm1_enable_show, fan_pwm1_enable_store);
5114 /* sysfs fan pwm1 ------------------------------------------------------ */
5115 static ssize_t fan_pwm1_show(struct device *dev,
5116 struct device_attribute *attr,
5122 res = fan_get_status_safe(&status);
5126 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5127 if (status != fan_control_initial_status) {
5128 tp_features.fan_ctrl_status_undef = 0;
5130 status = TP_EC_FAN_AUTO;
5135 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5136 status = fan_control_desired_level;
5141 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5144 static ssize_t fan_pwm1_store(struct device *dev,
5145 struct device_attribute *attr,
5146 const char *buf, size_t count)
5150 u8 status, newlevel;
5152 if (parse_strtoul(buf, 255, &s))
5155 /* scale down from 0-255 to 0-7 */
5156 newlevel = (s >> 5) & 0x07;
5158 if (mutex_lock_interruptible(&fan_mutex))
5159 return -ERESTARTSYS;
5161 rc = fan_get_status(&status);
5162 if (!rc && (status &
5163 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5164 rc = fan_set_level(newlevel);
5168 fan_update_desired_level(newlevel);
5169 fan_watchdog_reset();
5173 mutex_unlock(&fan_mutex);
5174 return (rc)? rc : count;
5177 static struct device_attribute dev_attr_fan_pwm1 =
5178 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5179 fan_pwm1_show, fan_pwm1_store);
5181 /* sysfs fan fan1_input ------------------------------------------------ */
5182 static ssize_t fan_fan1_input_show(struct device *dev,
5183 struct device_attribute *attr,
5189 res = fan_get_speed(&speed);
5193 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5196 static struct device_attribute dev_attr_fan_fan1_input =
5197 __ATTR(fan1_input, S_IRUGO,
5198 fan_fan1_input_show, NULL);
5200 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5201 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5204 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5207 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5208 const char *buf, size_t count)
5212 if (parse_strtoul(buf, 120, &t))
5215 if (!fan_control_allowed)
5218 fan_watchdog_maxinterval = t;
5219 fan_watchdog_reset();
5224 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5225 fan_fan_watchdog_show, fan_fan_watchdog_store);
5227 /* --------------------------------------------------------------------- */
5228 static struct attribute *fan_attributes[] = {
5229 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5230 &dev_attr_fan_fan1_input.attr,
5234 static const struct attribute_group fan_attr_group = {
5235 .attrs = fan_attributes,
5238 static int __init fan_init(struct ibm_init_struct *iibm)
5242 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5244 mutex_init(&fan_mutex);
5245 fan_status_access_mode = TPACPI_FAN_NONE;
5246 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5247 fan_control_commands = 0;
5248 fan_watchdog_maxinterval = 0;
5249 tp_features.fan_ctrl_status_undef = 0;
5250 fan_control_desired_level = 7;
5252 TPACPI_ACPIHANDLE_INIT(fans);
5253 TPACPI_ACPIHANDLE_INIT(gfan);
5254 TPACPI_ACPIHANDLE_INIT(sfan);
5257 /* 570, 600e/x, 770e, 770x */
5258 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5260 /* all other ThinkPads: note that even old-style
5261 * ThinkPad ECs supports the fan control register */
5262 if (likely(acpi_ec_read(fan_status_offset,
5263 &fan_control_initial_status))) {
5264 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5266 /* In some ThinkPads, neither the EC nor the ACPI
5267 * DSDT initialize the fan status, and it ends up
5268 * being set to 0x07 when it *could* be either
5271 * Enable for TP-1Y (T43), TP-78 (R51e),
5272 * TP-76 (R52), TP-70 (T43, R52), which are known
5274 if (fan_control_initial_status == 0x07) {
5275 switch (thinkpad_id.ec_model) {
5276 case 0x5931: /* TP-1Y */
5277 case 0x3837: /* TP-78 */
5278 case 0x3637: /* TP-76 */
5279 case 0x3037: /* TP-70 */
5280 printk(TPACPI_NOTICE
5281 "fan_init: initial fan status "
5282 "is unknown, assuming it is "
5284 tp_features.fan_ctrl_status_undef = 1;
5290 "ThinkPad ACPI EC access misbehaving, "
5291 "fan status and control unavailable\n");
5298 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5299 fan_control_commands |=
5300 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5303 /* gfan without sfan means no fan control */
5304 /* all other models implement TP EC 0x2f control */
5308 fan_control_access_mode =
5309 TPACPI_FAN_WR_ACPI_FANS;
5310 fan_control_commands |=
5311 TPACPI_FAN_CMD_SPEED |
5312 TPACPI_FAN_CMD_LEVEL |
5313 TPACPI_FAN_CMD_ENABLE;
5315 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5316 fan_control_commands |=
5317 TPACPI_FAN_CMD_LEVEL |
5318 TPACPI_FAN_CMD_ENABLE;
5323 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5324 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5325 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5326 fan_status_access_mode, fan_control_access_mode);
5328 /* fan control master switch */
5329 if (!fan_control_allowed) {
5330 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5331 fan_control_commands = 0;
5332 dbg_printk(TPACPI_DBG_INIT,
5333 "fan control features disabled by parameter\n");
5336 /* update fan_control_desired_level */
5337 if (fan_status_access_mode != TPACPI_FAN_NONE)
5338 fan_get_status_safe(NULL);
5340 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5341 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5342 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5345 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5346 &driver_attr_fan_watchdog);
5354 static void fan_exit(void)
5356 vdbg_printk(TPACPI_DBG_EXIT,
5357 "cancelling any pending fan watchdog tasks\n");
5359 /* FIXME: can we really do this unconditionally? */
5360 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5361 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5362 &driver_attr_fan_watchdog);
5364 cancel_delayed_work(&fan_watchdog_task);
5365 flush_scheduled_work();
5368 static int fan_read(char *p)
5373 unsigned int speed = 0;
5375 switch (fan_status_access_mode) {
5376 case TPACPI_FAN_RD_ACPI_GFAN:
5377 /* 570, 600e/x, 770e, 770x */
5378 rc = fan_get_status_safe(&status);
5382 len += sprintf(p + len, "status:\t\t%s\n"
5384 (status != 0) ? "enabled" : "disabled", status);
5387 case TPACPI_FAN_RD_TPEC:
5388 /* all except 570, 600e/x, 770e, 770x */
5389 rc = fan_get_status_safe(&status);
5393 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5394 if (status != fan_control_initial_status)
5395 tp_features.fan_ctrl_status_undef = 0;
5397 /* Return most likely status. In fact, it
5398 * might be the only possible status */
5399 status = TP_EC_FAN_AUTO;
5402 len += sprintf(p + len, "status:\t\t%s\n",
5403 (status != 0) ? "enabled" : "disabled");
5405 rc = fan_get_speed(&speed);
5409 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5411 if (status & TP_EC_FAN_FULLSPEED)
5412 /* Disengaged mode takes precedence */
5413 len += sprintf(p + len, "level:\t\tdisengaged\n");
5414 else if (status & TP_EC_FAN_AUTO)
5415 len += sprintf(p + len, "level:\t\tauto\n");
5417 len += sprintf(p + len, "level:\t\t%d\n", status);
5420 case TPACPI_FAN_NONE:
5422 len += sprintf(p + len, "status:\t\tnot supported\n");
5425 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5426 len += sprintf(p + len, "commands:\tlevel <level>");
5428 switch (fan_control_access_mode) {
5429 case TPACPI_FAN_WR_ACPI_SFAN:
5430 len += sprintf(p + len, " (<level> is 0-7)\n");
5434 len += sprintf(p + len, " (<level> is 0-7, "
5435 "auto, disengaged, full-speed)\n");
5440 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5441 len += sprintf(p + len, "commands:\tenable, disable\n"
5442 "commands:\twatchdog <timeout> (<timeout> "
5443 "is 0 (off), 1-120 (seconds))\n");
5445 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5446 len += sprintf(p + len, "commands:\tspeed <speed>"
5447 " (<speed> is 0-65535)\n");
5452 static int fan_write_cmd_level(const char *cmd, int *rc)
5456 if (strlencmp(cmd, "level auto") == 0)
5457 level = TP_EC_FAN_AUTO;
5458 else if ((strlencmp(cmd, "level disengaged") == 0) |
5459 (strlencmp(cmd, "level full-speed") == 0))
5460 level = TP_EC_FAN_FULLSPEED;
5461 else if (sscanf(cmd, "level %d", &level) != 1)
5464 *rc = fan_set_level_safe(level);
5466 printk(TPACPI_ERR "level command accepted for unsupported "
5467 "access mode %d", fan_control_access_mode);
5472 static int fan_write_cmd_enable(const char *cmd, int *rc)
5474 if (strlencmp(cmd, "enable") != 0)
5477 *rc = fan_set_enable();
5479 printk(TPACPI_ERR "enable command accepted for unsupported "
5480 "access mode %d", fan_control_access_mode);
5485 static int fan_write_cmd_disable(const char *cmd, int *rc)
5487 if (strlencmp(cmd, "disable") != 0)
5490 *rc = fan_set_disable();
5492 printk(TPACPI_ERR "disable command accepted for unsupported "
5493 "access mode %d", fan_control_access_mode);
5498 static int fan_write_cmd_speed(const char *cmd, int *rc)
5503 * Support speed <low> <medium> <high> ? */
5505 if (sscanf(cmd, "speed %d", &speed) != 1)
5508 *rc = fan_set_speed(speed);
5510 printk(TPACPI_ERR "speed command accepted for unsupported "
5511 "access mode %d", fan_control_access_mode);
5516 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5520 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5523 if (interval < 0 || interval > 120)
5526 fan_watchdog_maxinterval = interval;
5531 static int fan_write(char *buf)
5536 while (!rc && (cmd = next_cmd(&buf))) {
5537 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5538 fan_write_cmd_level(cmd, &rc)) &&
5539 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5540 (fan_write_cmd_enable(cmd, &rc) ||
5541 fan_write_cmd_disable(cmd, &rc) ||
5542 fan_write_cmd_watchdog(cmd, &rc))) &&
5543 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5544 fan_write_cmd_speed(cmd, &rc))
5548 fan_watchdog_reset();
5554 static struct ibm_struct fan_driver_data = {
5561 /****************************************************************************
5562 ****************************************************************************
5566 ****************************************************************************
5567 ****************************************************************************/
5569 /* sysfs name ---------------------------------------------------------- */
5570 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5571 struct device_attribute *attr,
5574 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5577 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5578 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5580 /* --------------------------------------------------------------------- */
5583 static struct proc_dir_entry *proc_dir;
5586 * Module and infrastructure proble, init and exit handling
5589 static int force_load;
5591 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5592 static const char * __init str_supported(int is_supported)
5594 static char text_unsupported[] __initdata = "not supported";
5596 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5598 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5600 static void ibm_exit(struct ibm_struct *ibm)
5602 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5604 list_del_init(&ibm->all_drivers);
5606 if (ibm->flags.acpi_notify_installed) {
5607 dbg_printk(TPACPI_DBG_EXIT,
5608 "%s: acpi_remove_notify_handler\n", ibm->name);
5610 acpi_remove_notify_handler(*ibm->acpi->handle,
5612 dispatch_acpi_notify);
5613 ibm->flags.acpi_notify_installed = 0;
5614 ibm->flags.acpi_notify_installed = 0;
5617 if (ibm->flags.proc_created) {
5618 dbg_printk(TPACPI_DBG_EXIT,
5619 "%s: remove_proc_entry\n", ibm->name);
5620 remove_proc_entry(ibm->name, proc_dir);
5621 ibm->flags.proc_created = 0;
5624 if (ibm->flags.acpi_driver_registered) {
5625 dbg_printk(TPACPI_DBG_EXIT,
5626 "%s: acpi_bus_unregister_driver\n", ibm->name);
5628 acpi_bus_unregister_driver(ibm->acpi->driver);
5629 kfree(ibm->acpi->driver);
5630 ibm->acpi->driver = NULL;
5631 ibm->flags.acpi_driver_registered = 0;
5634 if (ibm->flags.init_called && ibm->exit) {
5636 ibm->flags.init_called = 0;
5639 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5642 static int __init ibm_init(struct ibm_init_struct *iibm)
5645 struct ibm_struct *ibm = iibm->data;
5646 struct proc_dir_entry *entry;
5648 BUG_ON(ibm == NULL);
5650 INIT_LIST_HEAD(&ibm->all_drivers);
5652 if (ibm->flags.experimental && !experimental)
5655 dbg_printk(TPACPI_DBG_INIT,
5656 "probing for %s\n", ibm->name);
5659 ret = iibm->init(iibm);
5661 return 0; /* probe failed */
5665 ibm->flags.init_called = 1;
5669 if (ibm->acpi->hid) {
5670 ret = register_tpacpi_subdriver(ibm);
5675 if (ibm->acpi->notify) {
5676 ret = setup_acpi_notify(ibm);
5677 if (ret == -ENODEV) {
5678 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5688 dbg_printk(TPACPI_DBG_INIT,
5689 "%s installed\n", ibm->name);
5692 entry = create_proc_entry(ibm->name,
5693 S_IFREG | S_IRUGO | S_IWUSR,
5696 printk(TPACPI_ERR "unable to create proc entry %s\n",
5701 entry->owner = THIS_MODULE;
5703 entry->read_proc = &dispatch_procfs_read;
5705 entry->write_proc = &dispatch_procfs_write;
5706 ibm->flags.proc_created = 1;
5709 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5714 dbg_printk(TPACPI_DBG_INIT,
5715 "%s: at error exit path with result %d\n",
5719 return (ret < 0)? ret : 0;
5724 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5726 const struct dmi_device *dev = NULL;
5727 char ec_fw_string[18];
5732 memset(tp, 0, sizeof(*tp));
5734 if (dmi_name_in_vendors("IBM"))
5735 tp->vendor = PCI_VENDOR_ID_IBM;
5736 else if (dmi_name_in_vendors("LENOVO"))
5737 tp->vendor = PCI_VENDOR_ID_LENOVO;
5741 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5743 if (!tp->bios_version_str)
5745 tp->bios_model = tp->bios_version_str[0]
5746 | (tp->bios_version_str[1] << 8);
5749 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5750 * X32 or newer, all Z series; Some models must have an
5751 * up-to-date BIOS or they will not be detected.
5753 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5755 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5756 if (sscanf(dev->name,
5757 "IBM ThinkPad Embedded Controller -[%17c",
5758 ec_fw_string) == 1) {
5759 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5760 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5762 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5763 tp->ec_model = ec_fw_string[0]
5764 | (ec_fw_string[1] << 8);
5769 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5771 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5772 kfree(tp->model_str);
5773 tp->model_str = NULL;
5777 static int __init probe_for_thinkpad(void)
5785 * Non-ancient models have better DMI tagging, but very old models
5788 is_thinkpad = (thinkpad_id.model_str != NULL);
5790 /* ec is required because many other handles are relative to it */
5791 TPACPI_ACPIHANDLE_INIT(ec);
5795 "Not yet supported ThinkPad detected!\n");
5800 * Risks a regression on very old machines, but reduces potential
5801 * false positives a damn great deal
5804 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5806 if (!is_thinkpad && !force_load)
5813 /* Module init, exit, parameters */
5815 static struct ibm_init_struct ibms_init[] __initdata = {
5817 .init = thinkpad_acpi_driver_init,
5818 .data = &thinkpad_acpi_driver_data,
5821 .init = hotkey_init,
5822 .data = &hotkey_driver_data,
5825 .init = bluetooth_init,
5826 .data = &bluetooth_driver_data,
5830 .data = &wan_driver_data,
5832 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5835 .data = &video_driver_data,
5840 .data = &light_driver_data,
5842 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5845 .data = &dock_driver_data[0],
5849 .data = &dock_driver_data[1],
5852 #ifdef CONFIG_THINKPAD_ACPI_BAY
5855 .data = &bay_driver_data,
5860 .data = &cmos_driver_data,
5864 .data = &led_driver_data,
5868 .data = &beep_driver_data,
5871 .init = thermal_init,
5872 .data = &thermal_driver_data,
5875 .data = &ecdump_driver_data,
5878 .init = brightness_init,
5879 .data = &brightness_driver_data,
5882 .data = &volume_driver_data,
5886 .data = &fan_driver_data,
5890 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5893 struct ibm_struct *ibm;
5895 if (!kp || !kp->name || !val)
5898 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5899 ibm = ibms_init[i].data;
5900 WARN_ON(ibm == NULL);
5902 if (!ibm || !ibm->name)
5905 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5906 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5908 strcpy(ibms_init[i].param, val);
5909 strcat(ibms_init[i].param, ",");
5917 module_param(experimental, int, 0);
5918 MODULE_PARM_DESC(experimental,
5919 "Enables experimental features when non-zero");
5921 module_param_named(debug, dbg_level, uint, 0);
5922 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5924 module_param(force_load, bool, 0);
5925 MODULE_PARM_DESC(force_load,
5926 "Attempts to load the driver even on a "
5927 "mis-identified ThinkPad when true");
5929 module_param_named(fan_control, fan_control_allowed, bool, 0);
5930 MODULE_PARM_DESC(fan_control,
5931 "Enables setting fan parameters features when true");
5933 module_param_named(brightness_mode, brightness_mode, int, 0);
5934 MODULE_PARM_DESC(brightness_mode,
5935 "Selects brightness control strategy: "
5936 "0=auto, 1=EC, 2=CMOS, 3=both");
5938 module_param(brightness_enable, uint, 0);
5939 MODULE_PARM_DESC(brightness_enable,
5940 "Enables backlight control when 1, disables when 0");
5942 module_param(hotkey_report_mode, uint, 0);
5943 MODULE_PARM_DESC(hotkey_report_mode,
5944 "used for backwards compatibility with userspace, "
5945 "see documentation");
5947 #define TPACPI_PARAM(feature) \
5948 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5949 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
5950 "at module load, see documentation")
5952 TPACPI_PARAM(hotkey);
5953 TPACPI_PARAM(bluetooth);
5954 TPACPI_PARAM(video);
5955 TPACPI_PARAM(light);
5956 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5959 #ifdef CONFIG_THINKPAD_ACPI_BAY
5961 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5965 TPACPI_PARAM(ecdump);
5966 TPACPI_PARAM(brightness);
5967 TPACPI_PARAM(volume);
5970 static void thinkpad_acpi_module_exit(void)
5972 struct ibm_struct *ibm, *itmp;
5974 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5976 list_for_each_entry_safe_reverse(ibm, itmp,
5977 &tpacpi_all_drivers,
5982 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5984 if (tpacpi_inputdev) {
5985 if (tp_features.input_device_registered)
5986 input_unregister_device(tpacpi_inputdev);
5988 input_free_device(tpacpi_inputdev);
5992 hwmon_device_unregister(tpacpi_hwmon);
5994 if (tp_features.sensors_pdev_attrs_registered)
5995 device_remove_file(&tpacpi_sensors_pdev->dev,
5996 &dev_attr_thinkpad_acpi_pdev_name);
5997 if (tpacpi_sensors_pdev)
5998 platform_device_unregister(tpacpi_sensors_pdev);
6000 platform_device_unregister(tpacpi_pdev);
6002 if (tp_features.sensors_pdrv_attrs_registered)
6003 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6004 if (tp_features.platform_drv_attrs_registered)
6005 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6007 if (tp_features.sensors_pdrv_registered)
6008 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6010 if (tp_features.platform_drv_registered)
6011 platform_driver_unregister(&tpacpi_pdriver);
6014 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6016 kfree(thinkpad_id.bios_version_str);
6017 kfree(thinkpad_id.ec_version_str);
6018 kfree(thinkpad_id.model_str);
6022 static int __init thinkpad_acpi_module_init(void)
6026 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6028 /* Parameter checking */
6029 if (hotkey_report_mode > 2)
6032 /* Driver-level probe */
6034 get_thinkpad_model_data(&thinkpad_id);
6035 ret = probe_for_thinkpad();
6037 thinkpad_acpi_module_exit();
6041 /* Driver initialization */
6043 TPACPI_ACPIHANDLE_INIT(ecrd);
6044 TPACPI_ACPIHANDLE_INIT(ecwr);
6046 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6049 "unable to create proc dir " TPACPI_PROC_DIR);
6050 thinkpad_acpi_module_exit();
6053 proc_dir->owner = THIS_MODULE;
6055 ret = platform_driver_register(&tpacpi_pdriver);
6058 "unable to register main platform driver\n");
6059 thinkpad_acpi_module_exit();
6062 tp_features.platform_drv_registered = 1;
6064 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6067 "unable to register hwmon platform driver\n");
6068 thinkpad_acpi_module_exit();
6071 tp_features.sensors_pdrv_registered = 1;
6073 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6075 tp_features.platform_drv_attrs_registered = 1;
6076 ret = tpacpi_create_driver_attributes(
6077 &tpacpi_hwmon_pdriver.driver);
6081 "unable to create sysfs driver attributes\n");
6082 thinkpad_acpi_module_exit();
6085 tp_features.sensors_pdrv_attrs_registered = 1;
6088 /* Device initialization */
6089 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6091 if (IS_ERR(tpacpi_pdev)) {
6092 ret = PTR_ERR(tpacpi_pdev);
6094 printk(TPACPI_ERR "unable to register platform device\n");
6095 thinkpad_acpi_module_exit();
6098 tpacpi_sensors_pdev = platform_device_register_simple(
6099 TPACPI_HWMON_DRVR_NAME,
6101 if (IS_ERR(tpacpi_sensors_pdev)) {
6102 ret = PTR_ERR(tpacpi_sensors_pdev);
6103 tpacpi_sensors_pdev = NULL;
6105 "unable to register hwmon platform device\n");
6106 thinkpad_acpi_module_exit();
6109 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6110 &dev_attr_thinkpad_acpi_pdev_name);
6113 "unable to create sysfs hwmon device attributes\n");
6114 thinkpad_acpi_module_exit();
6117 tp_features.sensors_pdev_attrs_registered = 1;
6118 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6119 if (IS_ERR(tpacpi_hwmon)) {
6120 ret = PTR_ERR(tpacpi_hwmon);
6121 tpacpi_hwmon = NULL;
6122 printk(TPACPI_ERR "unable to register hwmon device\n");
6123 thinkpad_acpi_module_exit();
6126 mutex_init(&tpacpi_inputdev_send_mutex);
6127 tpacpi_inputdev = input_allocate_device();
6128 if (!tpacpi_inputdev) {
6129 printk(TPACPI_ERR "unable to allocate input device\n");
6130 thinkpad_acpi_module_exit();
6133 /* Prepare input device, but don't register */
6134 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6135 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6136 tpacpi_inputdev->id.bustype = BUS_HOST;
6137 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6138 thinkpad_id.vendor :
6140 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6141 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6143 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6144 ret = ibm_init(&ibms_init[i]);
6145 if (ret >= 0 && *ibms_init[i].param)
6146 ret = ibms_init[i].data->write(ibms_init[i].param);
6148 thinkpad_acpi_module_exit();
6152 ret = input_register_device(tpacpi_inputdev);
6154 printk(TPACPI_ERR "unable to register input device\n");
6155 thinkpad_acpi_module_exit();
6158 tp_features.input_device_registered = 1;
6161 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6165 /* Please remove this in year 2009 */
6166 MODULE_ALIAS("ibm_acpi");
6169 * DMI matching for module autoloading
6171 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6172 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6174 * Only models listed in thinkwiki will be supported, so add yours
6175 * if it is not there yet.
6177 #define IBM_BIOS_MODULE_ALIAS(__type) \
6178 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6180 /* Non-ancient thinkpads */
6181 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6182 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6184 /* Ancient thinkpad BIOSes have to be identified by
6185 * BIOS type or model number, and there are far less
6186 * BIOS types than model numbers... */
6187 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6188 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6189 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6191 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6192 MODULE_DESCRIPTION(TPACPI_DESC);
6193 MODULE_VERSION(TPACPI_VERSION);
6194 MODULE_LICENSE("GPL");
6196 module_init(thinkpad_acpi_module_init);
6197 module_exit(thinkpad_acpi_module_exit);