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 mutex_lock(&tpacpi_inputdev_send_mutex);
1166 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1167 input_report_switch(tpacpi_inputdev,
1169 input_sync(tpacpi_inputdev);
1172 mutex_unlock(&tpacpi_inputdev_send_mutex);
1175 static void tpacpi_input_send_key(unsigned int scancode)
1177 unsigned int keycode;
1179 keycode = hotkey_keycode_map[scancode];
1181 if (keycode != KEY_RESERVED) {
1182 mutex_lock(&tpacpi_inputdev_send_mutex);
1184 input_report_key(tpacpi_inputdev, keycode, 1);
1185 if (keycode == KEY_UNKNOWN)
1186 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1188 input_sync(tpacpi_inputdev);
1190 input_report_key(tpacpi_inputdev, keycode, 0);
1191 if (keycode == KEY_UNKNOWN)
1192 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1194 input_sync(tpacpi_inputdev);
1196 mutex_unlock(&tpacpi_inputdev_send_mutex);
1200 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1201 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1203 static void tpacpi_hotkey_send_key(unsigned int scancode)
1205 tpacpi_input_send_key(scancode);
1206 if (hotkey_report_mode < 2) {
1207 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1208 0x80, 0x1001 + scancode);
1212 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1216 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1217 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1218 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1219 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1220 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1221 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1223 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1224 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1225 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1227 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1228 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1229 n->displayexp_toggle =
1230 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1232 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1233 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1234 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1235 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1236 n->brightness_toggle =
1237 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1239 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1240 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1241 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1242 >> TP_NVRAM_POS_LEVEL_VOLUME;
1243 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1244 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1248 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1250 if ((mask & (1 << __scancode)) && \
1251 oldn->__member != newn->__member) \
1252 tpacpi_hotkey_send_key(__scancode); \
1255 #define TPACPI_MAY_SEND_KEY(__scancode) \
1256 do { if (mask & (1 << __scancode)) \
1257 tpacpi_hotkey_send_key(__scancode); } while (0)
1259 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1260 struct tp_nvram_state *newn,
1263 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1264 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1265 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1266 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1268 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1270 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1273 if (oldn->volume_toggle != newn->volume_toggle) {
1274 if (oldn->mute != newn->mute) {
1275 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1277 if (oldn->volume_level > newn->volume_level) {
1278 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1279 } else if (oldn->volume_level < newn->volume_level) {
1280 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1281 } else if (oldn->mute == newn->mute) {
1282 /* repeated key presses that didn't change state */
1284 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1285 } else if (newn->volume_level != 0) {
1286 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1288 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1293 /* handle brightness */
1294 if (oldn->brightness_toggle != newn->brightness_toggle) {
1295 if (oldn->brightness_level < newn->brightness_level) {
1296 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1297 } else if (oldn->brightness_level > newn->brightness_level) {
1298 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1300 /* repeated key presses that didn't change state */
1301 if (newn->brightness_level != 0) {
1302 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1304 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1310 #undef TPACPI_COMPARE_KEY
1311 #undef TPACPI_MAY_SEND_KEY
1313 static int hotkey_kthread(void *data)
1315 struct tp_nvram_state s[2];
1317 unsigned int si, so;
1319 unsigned int change_detector, must_reset;
1321 mutex_lock(&hotkey_thread_mutex);
1323 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1332 /* Initial state for compares */
1333 mutex_lock(&hotkey_thread_data_mutex);
1334 change_detector = hotkey_config_change;
1335 mask = hotkey_source_mask & hotkey_mask;
1336 mutex_unlock(&hotkey_thread_data_mutex);
1337 hotkey_read_nvram(&s[so], mask);
1339 while (!kthread_should_stop() && hotkey_poll_freq) {
1341 t = 1000/hotkey_poll_freq;
1342 t = msleep_interruptible(t);
1343 if (unlikely(kthread_should_stop()))
1345 must_reset = try_to_freeze();
1346 if (t > 0 && !must_reset)
1349 mutex_lock(&hotkey_thread_data_mutex);
1350 if (must_reset || hotkey_config_change != change_detector) {
1351 /* forget old state on thaw or config change */
1354 change_detector = hotkey_config_change;
1356 mask = hotkey_source_mask & hotkey_mask;
1357 mutex_unlock(&hotkey_thread_data_mutex);
1360 hotkey_read_nvram(&s[si], mask);
1361 if (likely(si != so)) {
1362 hotkey_compare_and_issue_event(&s[so], &s[si],
1372 mutex_unlock(&hotkey_thread_mutex);
1376 static void hotkey_poll_stop_sync(void)
1378 if (tpacpi_hotkey_task) {
1379 if (frozen(tpacpi_hotkey_task) ||
1380 freezing(tpacpi_hotkey_task))
1381 thaw_process(tpacpi_hotkey_task);
1383 kthread_stop(tpacpi_hotkey_task);
1384 tpacpi_hotkey_task = NULL;
1385 mutex_lock(&hotkey_thread_mutex);
1386 /* at this point, the thread did exit */
1387 mutex_unlock(&hotkey_thread_mutex);
1391 /* call with hotkey_mutex held */
1392 static void hotkey_poll_setup(int may_warn)
1394 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1395 hotkey_poll_freq > 0 &&
1396 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1397 if (!tpacpi_hotkey_task) {
1398 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1401 if (IS_ERR(tpacpi_hotkey_task)) {
1402 tpacpi_hotkey_task = NULL;
1404 "could not create kernel thread "
1405 "for hotkey polling\n");
1409 hotkey_poll_stop_sync();
1411 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1412 printk(TPACPI_NOTICE
1413 "hot keys 0x%08x require polling, "
1414 "which is currently disabled\n",
1415 hotkey_source_mask);
1420 static void hotkey_poll_setup_safe(int may_warn)
1422 mutex_lock(&hotkey_mutex);
1423 hotkey_poll_setup(may_warn);
1424 mutex_unlock(&hotkey_mutex);
1427 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1429 static void hotkey_poll_setup_safe(int __unused)
1433 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1435 static int hotkey_inputdev_open(struct input_dev *dev)
1437 switch (tpacpi_lifecycle) {
1438 case TPACPI_LIFE_INIT:
1440 * hotkey_init will call hotkey_poll_setup_safe
1441 * at the appropriate moment
1444 case TPACPI_LIFE_EXITING:
1446 case TPACPI_LIFE_RUNNING:
1447 hotkey_poll_setup_safe(0);
1451 /* Should only happen if tpacpi_lifecycle is corrupt */
1456 static void hotkey_inputdev_close(struct input_dev *dev)
1458 /* disable hotkey polling when possible */
1459 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1460 hotkey_poll_setup_safe(0);
1463 /* sysfs hotkey enable ------------------------------------------------- */
1464 static ssize_t hotkey_enable_show(struct device *dev,
1465 struct device_attribute *attr,
1470 res = hotkey_status_get(&status);
1474 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1477 static ssize_t hotkey_enable_store(struct device *dev,
1478 struct device_attribute *attr,
1479 const char *buf, size_t count)
1484 if (parse_strtoul(buf, 1, &t))
1487 res = hotkey_status_set(t);
1489 return (res) ? res : count;
1492 static struct device_attribute dev_attr_hotkey_enable =
1493 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1494 hotkey_enable_show, hotkey_enable_store);
1496 /* sysfs hotkey mask --------------------------------------------------- */
1497 static ssize_t hotkey_mask_show(struct device *dev,
1498 struct device_attribute *attr,
1503 if (mutex_lock_interruptible(&hotkey_mutex))
1504 return -ERESTARTSYS;
1505 res = hotkey_mask_get();
1506 mutex_unlock(&hotkey_mutex);
1509 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1512 static ssize_t hotkey_mask_store(struct device *dev,
1513 struct device_attribute *attr,
1514 const char *buf, size_t count)
1519 if (parse_strtoul(buf, 0xffffffffUL, &t))
1522 if (mutex_lock_interruptible(&hotkey_mutex))
1523 return -ERESTARTSYS;
1525 res = hotkey_mask_set(t);
1527 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1528 hotkey_poll_setup(1);
1531 mutex_unlock(&hotkey_mutex);
1533 return (res) ? res : count;
1536 static struct device_attribute dev_attr_hotkey_mask =
1537 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1538 hotkey_mask_show, hotkey_mask_store);
1540 /* sysfs hotkey bios_enabled ------------------------------------------- */
1541 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1542 struct device_attribute *attr,
1545 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1548 static struct device_attribute dev_attr_hotkey_bios_enabled =
1549 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1551 /* sysfs hotkey bios_mask ---------------------------------------------- */
1552 static ssize_t hotkey_bios_mask_show(struct device *dev,
1553 struct device_attribute *attr,
1556 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1559 static struct device_attribute dev_attr_hotkey_bios_mask =
1560 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1562 /* sysfs hotkey all_mask ----------------------------------------------- */
1563 static ssize_t hotkey_all_mask_show(struct device *dev,
1564 struct device_attribute *attr,
1567 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1568 hotkey_all_mask | hotkey_source_mask);
1571 static struct device_attribute dev_attr_hotkey_all_mask =
1572 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1574 /* sysfs hotkey recommended_mask --------------------------------------- */
1575 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1576 struct device_attribute *attr,
1579 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1580 (hotkey_all_mask | hotkey_source_mask)
1581 & ~hotkey_reserved_mask);
1584 static struct device_attribute dev_attr_hotkey_recommended_mask =
1585 __ATTR(hotkey_recommended_mask, S_IRUGO,
1586 hotkey_recommended_mask_show, NULL);
1588 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1590 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1591 static ssize_t hotkey_source_mask_show(struct device *dev,
1592 struct device_attribute *attr,
1595 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1598 static ssize_t hotkey_source_mask_store(struct device *dev,
1599 struct device_attribute *attr,
1600 const char *buf, size_t count)
1604 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1605 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1608 if (mutex_lock_interruptible(&hotkey_mutex))
1609 return -ERESTARTSYS;
1611 HOTKEY_CONFIG_CRITICAL_START
1612 hotkey_source_mask = t;
1613 HOTKEY_CONFIG_CRITICAL_END
1615 hotkey_poll_setup(1);
1617 mutex_unlock(&hotkey_mutex);
1622 static struct device_attribute dev_attr_hotkey_source_mask =
1623 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1624 hotkey_source_mask_show, hotkey_source_mask_store);
1626 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1627 static ssize_t hotkey_poll_freq_show(struct device *dev,
1628 struct device_attribute *attr,
1631 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1634 static ssize_t hotkey_poll_freq_store(struct device *dev,
1635 struct device_attribute *attr,
1636 const char *buf, size_t count)
1640 if (parse_strtoul(buf, 25, &t))
1643 if (mutex_lock_interruptible(&hotkey_mutex))
1644 return -ERESTARTSYS;
1646 hotkey_poll_freq = t;
1648 hotkey_poll_setup(1);
1649 mutex_unlock(&hotkey_mutex);
1654 static struct device_attribute dev_attr_hotkey_poll_freq =
1655 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1656 hotkey_poll_freq_show, hotkey_poll_freq_store);
1658 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1660 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1661 static ssize_t hotkey_radio_sw_show(struct device *dev,
1662 struct device_attribute *attr,
1666 res = hotkey_get_wlsw(&s);
1670 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1673 static struct device_attribute dev_attr_hotkey_radio_sw =
1674 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1676 static void hotkey_radio_sw_notify_change(void)
1678 if (tp_features.hotkey_wlsw)
1679 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1683 /* sysfs hotkey report_mode -------------------------------------------- */
1684 static ssize_t hotkey_report_mode_show(struct device *dev,
1685 struct device_attribute *attr,
1688 return snprintf(buf, PAGE_SIZE, "%d\n",
1689 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1692 static struct device_attribute dev_attr_hotkey_report_mode =
1693 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1695 /* sysfs wakeup reason (pollable) -------------------------------------- */
1696 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1697 struct device_attribute *attr,
1700 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1703 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1704 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1706 static void hotkey_wakeup_reason_notify_change(void)
1708 if (tp_features.hotkey_mask)
1709 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1713 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1714 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1715 struct device_attribute *attr,
1718 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1721 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1722 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1723 hotkey_wakeup_hotunplug_complete_show, NULL);
1725 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1727 if (tp_features.hotkey_mask)
1728 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1729 "wakeup_hotunplug_complete");
1732 /* --------------------------------------------------------------------- */
1734 static struct attribute *hotkey_attributes[] __initdata = {
1735 &dev_attr_hotkey_enable.attr,
1736 &dev_attr_hotkey_bios_enabled.attr,
1737 &dev_attr_hotkey_report_mode.attr,
1738 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1739 &dev_attr_hotkey_mask.attr,
1740 &dev_attr_hotkey_all_mask.attr,
1741 &dev_attr_hotkey_recommended_mask.attr,
1742 &dev_attr_hotkey_source_mask.attr,
1743 &dev_attr_hotkey_poll_freq.attr,
1747 static struct attribute *hotkey_mask_attributes[] __initdata = {
1748 &dev_attr_hotkey_bios_mask.attr,
1749 #ifndef 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,
1754 &dev_attr_hotkey_wakeup_reason.attr,
1755 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1758 static int __init hotkey_init(struct ibm_init_struct *iibm)
1760 /* Requirements for changing the default keymaps:
1762 * 1. Many of the keys are mapped to KEY_RESERVED for very
1763 * good reasons. Do not change them unless you have deep
1764 * knowledge on the IBM and Lenovo ThinkPad firmware for
1765 * the various ThinkPad models. The driver behaves
1766 * differently for KEY_RESERVED: such keys have their
1767 * hot key mask *unset* in mask_recommended, and also
1768 * in the initial hot key mask programmed into the
1769 * firmware at driver load time, which means the firm-
1770 * ware may react very differently if you change them to
1773 * 2. You must be subscribed to the linux-thinkpad and
1774 * ibm-acpi-devel mailing lists, and you should read the
1775 * list archives since 2007 if you want to change the
1776 * keymaps. This requirement exists so that you will
1777 * know the past history of problems with the thinkpad-
1778 * acpi driver keymaps, and also that you will be
1779 * listening to any bug reports;
1781 * 3. Do not send thinkpad-acpi specific patches directly to
1782 * for merging, *ever*. Send them to the linux-acpi
1783 * mailinglist for comments. Merging is to be done only
1784 * through acpi-test and the ACPI maintainer.
1786 * If the above is too much to ask, don't change the keymap.
1787 * Ask the thinkpad-acpi maintainer to do it, instead.
1789 static u16 ibm_keycode_map[] __initdata = {
1790 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1791 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1792 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1793 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1795 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1796 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1797 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1798 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1800 /* brightness: firmware always reacts to them, unless
1801 * X.org did some tricks in the radeon BIOS scratch
1802 * registers of *some* models */
1803 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1804 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1806 /* Thinklight: firmware always react to it */
1807 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1809 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1810 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1812 /* Volume: firmware always react to it and reprograms
1813 * the built-in *extra* mixer. Never map it to control
1814 * another mixer by default. */
1815 KEY_RESERVED, /* 0x14: VOLUME UP */
1816 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1817 KEY_RESERVED, /* 0x16: MUTE */
1819 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1821 /* (assignments unknown, please report if found) */
1822 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1823 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1825 static u16 lenovo_keycode_map[] __initdata = {
1826 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1827 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1828 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1829 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1831 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1832 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1833 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1834 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1836 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1837 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1839 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1841 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1842 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1844 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1845 * react to it and reprograms the built-in *extra* mixer.
1846 * Never map it to control another mixer by default.
1848 * T60?, T61, R60?, R61: firmware and EC tries to send
1849 * these over the regular keyboard, so these are no-ops,
1850 * but there are still weird bugs re. MUTE, so do not
1851 * change unless you get test reports from all Lenovo
1852 * models. May cause the BIOS to interfere with the
1855 KEY_RESERVED, /* 0x14: VOLUME UP */
1856 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1857 KEY_RESERVED, /* 0x16: MUTE */
1859 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1861 /* (assignments unknown, please report if found) */
1862 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1863 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1866 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1867 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1868 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1874 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1876 BUG_ON(!tpacpi_inputdev);
1877 BUG_ON(tpacpi_inputdev->open != NULL ||
1878 tpacpi_inputdev->close != NULL);
1880 TPACPI_ACPIHANDLE_INIT(hkey);
1881 mutex_init(&hotkey_mutex);
1883 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1884 mutex_init(&hotkey_thread_mutex);
1885 mutex_init(&hotkey_thread_data_mutex);
1888 /* hotkey not supported on 570 */
1889 tp_features.hotkey = hkey_handle != NULL;
1891 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1892 str_supported(tp_features.hotkey));
1894 if (tp_features.hotkey) {
1895 hotkey_dev_attributes = create_attr_set(12, NULL);
1896 if (!hotkey_dev_attributes)
1898 res = add_many_to_attr_set(hotkey_dev_attributes,
1900 ARRAY_SIZE(hotkey_attributes));
1904 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1905 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1906 for HKEY interface version 0x100 */
1907 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1908 if ((hkeyv >> 8) != 1) {
1909 printk(TPACPI_ERR "unknown version of the "
1910 "HKEY interface: 0x%x\n", hkeyv);
1911 printk(TPACPI_ERR "please report this to %s\n",
1915 * MHKV 0x100 in A31, R40, R40e,
1916 * T4x, X31, and later
1918 tp_features.hotkey_mask = 1;
1922 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1923 str_supported(tp_features.hotkey_mask));
1925 if (tp_features.hotkey_mask) {
1926 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1929 "missing MHKA handler, "
1930 "please report this to %s\n",
1932 /* FN+F12, FN+F4, FN+F3 */
1933 hotkey_all_mask = 0x080cU;
1937 /* hotkey_source_mask *must* be zero for
1938 * the first hotkey_mask_get */
1939 res = hotkey_status_get(&hotkey_orig_status);
1940 if (!res && tp_features.hotkey_mask) {
1941 res = hotkey_mask_get();
1942 hotkey_orig_mask = hotkey_mask;
1944 res = add_many_to_attr_set(
1945 hotkey_dev_attributes,
1946 hotkey_mask_attributes,
1947 ARRAY_SIZE(hotkey_mask_attributes));
1951 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1952 if (tp_features.hotkey_mask) {
1953 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1956 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1959 vdbg_printk(TPACPI_DBG_INIT,
1960 "hotkey source mask 0x%08x, polling freq %d\n",
1961 hotkey_source_mask, hotkey_poll_freq);
1964 /* Not all thinkpads have a hardware radio switch */
1965 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1966 tp_features.hotkey_wlsw = 1;
1968 "radio switch found; radios are %s\n",
1969 enabled(status, 0));
1970 res = add_to_attr_set(hotkey_dev_attributes,
1971 &dev_attr_hotkey_radio_sw.attr);
1975 res = register_attr_set_with_sysfs(
1976 hotkey_dev_attributes,
1977 &tpacpi_pdev->dev.kobj);
1981 /* Set up key map */
1983 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1985 if (!hotkey_keycode_map) {
1987 "failed to allocate memory for key map\n");
1991 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1992 dbg_printk(TPACPI_DBG_INIT,
1993 "using Lenovo default hot key map\n");
1994 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1995 TPACPI_HOTKEY_MAP_SIZE);
1997 dbg_printk(TPACPI_DBG_INIT,
1998 "using IBM default hot key map\n");
1999 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2000 TPACPI_HOTKEY_MAP_SIZE);
2003 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2004 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2005 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2006 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2007 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2008 tpacpi_inputdev->keycode = hotkey_keycode_map;
2009 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2010 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2011 set_bit(hotkey_keycode_map[i],
2012 tpacpi_inputdev->keybit);
2014 if (i < sizeof(hotkey_reserved_mask)*8)
2015 hotkey_reserved_mask |= 1 << i;
2019 if (tp_features.hotkey_wlsw) {
2020 set_bit(EV_SW, tpacpi_inputdev->evbit);
2021 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2024 dbg_printk(TPACPI_DBG_INIT,
2025 "enabling hot key handling\n");
2026 res = hotkey_status_set(1);
2029 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2030 & ~hotkey_reserved_mask)
2031 | hotkey_orig_mask);
2032 if (res < 0 && res != -ENXIO)
2035 dbg_printk(TPACPI_DBG_INIT,
2036 "legacy hot key reporting over procfs %s\n",
2037 (hotkey_report_mode < 2) ?
2038 "enabled" : "disabled");
2040 tpacpi_inputdev->open = &hotkey_inputdev_open;
2041 tpacpi_inputdev->close = &hotkey_inputdev_close;
2043 hotkey_poll_setup_safe(1);
2044 tpacpi_input_send_radiosw();
2047 return (tp_features.hotkey)? 0 : 1;
2050 static void hotkey_exit(void)
2052 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2053 hotkey_poll_stop_sync();
2056 if (tp_features.hotkey) {
2057 dbg_printk(TPACPI_DBG_EXIT,
2058 "restoring original hot key mask\n");
2059 /* no short-circuit boolean operator below! */
2060 if ((hotkey_mask_set(hotkey_orig_mask) |
2061 hotkey_status_set(hotkey_orig_status)) != 0)
2063 "failed to restore hot key mask "
2064 "to BIOS defaults\n");
2067 if (hotkey_dev_attributes) {
2068 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2069 hotkey_dev_attributes = NULL;
2073 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2076 unsigned int scancode;
2081 if (event != 0x80) {
2083 "unknown HKEY notification event %d\n", event);
2084 /* forward it to userspace, maybe it knows how to handle it */
2085 acpi_bus_generate_netlink_event(
2086 ibm->acpi->device->pnp.device_class,
2087 ibm->acpi->device->dev.bus_id,
2093 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2094 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2107 switch (hkey >> 12) {
2109 /* 0x1000-0x1FFF: key presses */
2110 scancode = hkey & 0xfff;
2111 if (scancode > 0 && scancode < 0x21) {
2113 if (!(hotkey_source_mask & (1 << scancode))) {
2114 tpacpi_input_send_key(scancode);
2126 case 0x2304: /* suspend, undock */
2127 case 0x2404: /* hibernation, undock */
2128 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2131 case 0x2305: /* suspend, bay eject */
2132 case 0x2405: /* hibernation, bay eject */
2133 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2139 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2141 "woke up due to a hot-unplug "
2143 hotkey_wakeup_reason_notify_change();
2147 /* bay-related wakeups */
2148 if (hkey == 0x3003) {
2149 hotkey_autosleep_ack = 1;
2152 hotkey_wakeup_hotunplug_complete_notify_change();
2158 /* dock-related wakeups */
2159 if (hkey == 0x4003) {
2160 hotkey_autosleep_ack = 1;
2163 hotkey_wakeup_hotunplug_complete_notify_change();
2169 /* 0x5000-0x5FFF: human interface helpers */
2171 case 0x5010: /* Lenovo new BIOS: brightness changed */
2172 case 0x5009: /* X61t: swivel up (tablet mode) */
2173 case 0x500a: /* X61t: swivel down (normal mode) */
2174 case 0x500b: /* X61t: tablet pen inserted into bay */
2175 case 0x500c: /* X61t: tablet pen removed from bay */
2179 /* LID switch events. Do not propagate */
2187 /* 0x7000-0x7FFF: misc */
2188 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2189 tpacpi_input_send_radiosw();
2190 hotkey_radio_sw_notify_change();
2194 /* fallthrough to default */
2199 printk(TPACPI_NOTICE
2200 "unhandled HKEY event 0x%04x\n", hkey);
2204 if (!ignore_acpi_ev &&
2205 (send_acpi_ev || hotkey_report_mode < 2)) {
2206 acpi_bus_generate_proc_event(ibm->acpi->device,
2210 /* netlink events */
2211 if (!ignore_acpi_ev && send_acpi_ev) {
2212 acpi_bus_generate_netlink_event(
2213 ibm->acpi->device->pnp.device_class,
2214 ibm->acpi->device->dev.bus_id,
2220 static void hotkey_suspend(pm_message_t state)
2222 /* Do these on suspend, we get the events on early resume! */
2223 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2224 hotkey_autosleep_ack = 0;
2227 static void hotkey_resume(void)
2229 if (hotkey_mask_get())
2231 "error while trying to read hot key mask "
2233 tpacpi_input_send_radiosw();
2234 hotkey_radio_sw_notify_change();
2235 hotkey_wakeup_reason_notify_change();
2236 hotkey_wakeup_hotunplug_complete_notify_change();
2237 hotkey_poll_setup_safe(0);
2240 /* procfs -------------------------------------------------------------- */
2241 static int hotkey_read(char *p)
2246 if (!tp_features.hotkey) {
2247 len += sprintf(p + len, "status:\t\tnot supported\n");
2251 if (mutex_lock_interruptible(&hotkey_mutex))
2252 return -ERESTARTSYS;
2253 res = hotkey_status_get(&status);
2255 res = hotkey_mask_get();
2256 mutex_unlock(&hotkey_mutex);
2260 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2261 if (tp_features.hotkey_mask) {
2262 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2263 len += sprintf(p + len,
2264 "commands:\tenable, disable, reset, <mask>\n");
2266 len += sprintf(p + len, "mask:\t\tnot supported\n");
2267 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2273 static int hotkey_write(char *buf)
2279 if (!tp_features.hotkey)
2282 if (mutex_lock_interruptible(&hotkey_mutex))
2283 return -ERESTARTSYS;
2289 while ((cmd = next_cmd(&buf))) {
2290 if (strlencmp(cmd, "enable") == 0) {
2292 } else if (strlencmp(cmd, "disable") == 0) {
2294 } else if (strlencmp(cmd, "reset") == 0) {
2295 status = hotkey_orig_status;
2296 mask = hotkey_orig_mask;
2297 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2299 } else if (sscanf(cmd, "%x", &mask) == 1) {
2307 res = hotkey_status_set(status);
2309 if (!res && mask != hotkey_mask)
2310 res = hotkey_mask_set(mask);
2313 mutex_unlock(&hotkey_mutex);
2317 static const struct acpi_device_id ibm_htk_device_ids[] = {
2318 {TPACPI_ACPI_HKEY_HID, 0},
2322 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2323 .hid = ibm_htk_device_ids,
2324 .notify = hotkey_notify,
2325 .handle = &hkey_handle,
2326 .type = ACPI_DEVICE_NOTIFY,
2329 static struct ibm_struct hotkey_driver_data = {
2331 .read = hotkey_read,
2332 .write = hotkey_write,
2333 .exit = hotkey_exit,
2334 .resume = hotkey_resume,
2335 .suspend = hotkey_suspend,
2336 .acpi = &ibm_hotkey_acpidriver,
2339 /*************************************************************************
2340 * Bluetooth subdriver
2344 /* ACPI GBDC/SBDC bits */
2345 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2346 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2347 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2350 static int bluetooth_get_radiosw(void);
2351 static int bluetooth_set_radiosw(int radio_on);
2353 /* sysfs bluetooth enable ---------------------------------------------- */
2354 static ssize_t bluetooth_enable_show(struct device *dev,
2355 struct device_attribute *attr,
2360 status = bluetooth_get_radiosw();
2364 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2367 static ssize_t bluetooth_enable_store(struct device *dev,
2368 struct device_attribute *attr,
2369 const char *buf, size_t count)
2374 if (parse_strtoul(buf, 1, &t))
2377 res = bluetooth_set_radiosw(t);
2379 return (res) ? res : count;
2382 static struct device_attribute dev_attr_bluetooth_enable =
2383 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2384 bluetooth_enable_show, bluetooth_enable_store);
2386 /* --------------------------------------------------------------------- */
2388 static struct attribute *bluetooth_attributes[] = {
2389 &dev_attr_bluetooth_enable.attr,
2393 static const struct attribute_group bluetooth_attr_group = {
2394 .attrs = bluetooth_attributes,
2397 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2402 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2404 TPACPI_ACPIHANDLE_INIT(hkey);
2406 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2407 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2408 tp_features.bluetooth = hkey_handle &&
2409 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2411 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2412 str_supported(tp_features.bluetooth),
2415 if (tp_features.bluetooth) {
2416 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2417 /* no bluetooth hardware present in system */
2418 tp_features.bluetooth = 0;
2419 dbg_printk(TPACPI_DBG_INIT,
2420 "bluetooth hardware not installed\n");
2422 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2423 &bluetooth_attr_group);
2429 return (tp_features.bluetooth)? 0 : 1;
2432 static void bluetooth_exit(void)
2434 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2435 &bluetooth_attr_group);
2438 static int bluetooth_get_radiosw(void)
2442 if (!tp_features.bluetooth)
2445 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2448 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2451 static int bluetooth_set_radiosw(int radio_on)
2455 if (!tp_features.bluetooth)
2458 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2461 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2463 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2464 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2470 /* procfs -------------------------------------------------------------- */
2471 static int bluetooth_read(char *p)
2474 int status = bluetooth_get_radiosw();
2476 if (!tp_features.bluetooth)
2477 len += sprintf(p + len, "status:\t\tnot supported\n");
2479 len += sprintf(p + len, "status:\t\t%s\n",
2480 (status)? "enabled" : "disabled");
2481 len += sprintf(p + len, "commands:\tenable, disable\n");
2487 static int bluetooth_write(char *buf)
2491 if (!tp_features.bluetooth)
2494 while ((cmd = next_cmd(&buf))) {
2495 if (strlencmp(cmd, "enable") == 0) {
2496 bluetooth_set_radiosw(1);
2497 } else if (strlencmp(cmd, "disable") == 0) {
2498 bluetooth_set_radiosw(0);
2506 static struct ibm_struct bluetooth_driver_data = {
2507 .name = "bluetooth",
2508 .read = bluetooth_read,
2509 .write = bluetooth_write,
2510 .exit = bluetooth_exit,
2513 /*************************************************************************
2518 /* ACPI GWAN/SWAN bits */
2519 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2520 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2521 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2524 static int wan_get_radiosw(void);
2525 static int wan_set_radiosw(int radio_on);
2527 /* sysfs wan enable ---------------------------------------------------- */
2528 static ssize_t wan_enable_show(struct device *dev,
2529 struct device_attribute *attr,
2534 status = wan_get_radiosw();
2538 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2541 static ssize_t wan_enable_store(struct device *dev,
2542 struct device_attribute *attr,
2543 const char *buf, size_t count)
2548 if (parse_strtoul(buf, 1, &t))
2551 res = wan_set_radiosw(t);
2553 return (res) ? res : count;
2556 static struct device_attribute dev_attr_wan_enable =
2557 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2558 wan_enable_show, wan_enable_store);
2560 /* --------------------------------------------------------------------- */
2562 static struct attribute *wan_attributes[] = {
2563 &dev_attr_wan_enable.attr,
2567 static const struct attribute_group wan_attr_group = {
2568 .attrs = wan_attributes,
2571 static int __init wan_init(struct ibm_init_struct *iibm)
2576 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2578 TPACPI_ACPIHANDLE_INIT(hkey);
2580 tp_features.wan = hkey_handle &&
2581 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2583 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2584 str_supported(tp_features.wan),
2587 if (tp_features.wan) {
2588 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2589 /* no wan hardware present in system */
2590 tp_features.wan = 0;
2591 dbg_printk(TPACPI_DBG_INIT,
2592 "wan hardware not installed\n");
2594 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2601 return (tp_features.wan)? 0 : 1;
2604 static void wan_exit(void)
2606 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2610 static int wan_get_radiosw(void)
2614 if (!tp_features.wan)
2617 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2620 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2623 static int wan_set_radiosw(int radio_on)
2627 if (!tp_features.wan)
2630 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2633 status |= TP_ACPI_WANCARD_RADIOSSW;
2635 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2636 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2642 /* procfs -------------------------------------------------------------- */
2643 static int wan_read(char *p)
2646 int status = wan_get_radiosw();
2648 if (!tp_features.wan)
2649 len += sprintf(p + len, "status:\t\tnot supported\n");
2651 len += sprintf(p + len, "status:\t\t%s\n",
2652 (status)? "enabled" : "disabled");
2653 len += sprintf(p + len, "commands:\tenable, disable\n");
2659 static int wan_write(char *buf)
2663 if (!tp_features.wan)
2666 while ((cmd = next_cmd(&buf))) {
2667 if (strlencmp(cmd, "enable") == 0) {
2669 } else if (strlencmp(cmd, "disable") == 0) {
2678 static struct ibm_struct wan_driver_data = {
2683 .flags.experimental = 1,
2686 /*************************************************************************
2690 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2692 enum video_access_mode {
2693 TPACPI_VIDEO_NONE = 0,
2694 TPACPI_VIDEO_570, /* 570 */
2695 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2696 TPACPI_VIDEO_NEW, /* all others */
2699 enum { /* video status flags, based on VIDEO_570 */
2700 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2701 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2702 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2705 enum { /* TPACPI_VIDEO_570 constants */
2706 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2707 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2708 * video_status_flags */
2709 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2710 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2713 static enum video_access_mode video_supported;
2714 static int video_orig_autosw;
2716 static int video_autosw_get(void);
2717 static int video_autosw_set(int enable);
2719 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2721 static int __init video_init(struct ibm_init_struct *iibm)
2725 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2727 TPACPI_ACPIHANDLE_INIT(vid);
2728 TPACPI_ACPIHANDLE_INIT(vid2);
2730 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2731 /* G41, assume IVGA doesn't change */
2732 vid_handle = vid2_handle;
2735 /* video switching not supported on R30, R31 */
2736 video_supported = TPACPI_VIDEO_NONE;
2737 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2739 video_supported = TPACPI_VIDEO_570;
2740 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2741 /* 600e/x, 770e, 770x */
2742 video_supported = TPACPI_VIDEO_770;
2745 video_supported = TPACPI_VIDEO_NEW;
2747 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2748 str_supported(video_supported != TPACPI_VIDEO_NONE),
2751 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2754 static void video_exit(void)
2756 dbg_printk(TPACPI_DBG_EXIT,
2757 "restoring original video autoswitch mode\n");
2758 if (video_autosw_set(video_orig_autosw))
2759 printk(TPACPI_ERR "error while trying to restore original "
2760 "video autoswitch mode\n");
2763 static int video_outputsw_get(void)
2768 switch (video_supported) {
2769 case TPACPI_VIDEO_570:
2770 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2771 TP_ACPI_VIDEO_570_PHSCMD))
2773 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2775 case TPACPI_VIDEO_770:
2776 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2779 status |= TP_ACPI_VIDEO_S_LCD;
2780 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2783 status |= TP_ACPI_VIDEO_S_CRT;
2785 case TPACPI_VIDEO_NEW:
2786 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2787 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2790 status |= TP_ACPI_VIDEO_S_CRT;
2792 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2793 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2796 status |= TP_ACPI_VIDEO_S_LCD;
2797 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2800 status |= TP_ACPI_VIDEO_S_DVI;
2809 static int video_outputsw_set(int status)
2814 switch (video_supported) {
2815 case TPACPI_VIDEO_570:
2816 res = acpi_evalf(NULL, NULL,
2817 "\\_SB.PHS2", "vdd",
2818 TP_ACPI_VIDEO_570_PHS2CMD,
2819 status | TP_ACPI_VIDEO_570_PHS2SET);
2821 case TPACPI_VIDEO_770:
2822 autosw = video_autosw_get();
2826 res = video_autosw_set(1);
2829 res = acpi_evalf(vid_handle, NULL,
2830 "ASWT", "vdd", status * 0x100, 0);
2831 if (!autosw && video_autosw_set(autosw)) {
2833 "video auto-switch left enabled due to error\n");
2837 case TPACPI_VIDEO_NEW:
2838 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2839 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2845 return (res)? 0 : -EIO;
2848 static int video_autosw_get(void)
2852 switch (video_supported) {
2853 case TPACPI_VIDEO_570:
2854 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2857 case TPACPI_VIDEO_770:
2858 case TPACPI_VIDEO_NEW:
2859 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2869 static int video_autosw_set(int enable)
2871 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2876 static int video_outputsw_cycle(void)
2878 int autosw = video_autosw_get();
2884 switch (video_supported) {
2885 case TPACPI_VIDEO_570:
2886 res = video_autosw_set(1);
2889 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2891 case TPACPI_VIDEO_770:
2892 case TPACPI_VIDEO_NEW:
2893 res = video_autosw_set(1);
2896 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2901 if (!autosw && video_autosw_set(autosw)) {
2903 "video auto-switch left enabled due to error\n");
2907 return (res)? 0 : -EIO;
2910 static int video_expand_toggle(void)
2912 switch (video_supported) {
2913 case TPACPI_VIDEO_570:
2914 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2916 case TPACPI_VIDEO_770:
2917 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2919 case TPACPI_VIDEO_NEW:
2920 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2928 static int video_read(char *p)
2933 if (video_supported == TPACPI_VIDEO_NONE) {
2934 len += sprintf(p + len, "status:\t\tnot supported\n");
2938 status = video_outputsw_get();
2942 autosw = video_autosw_get();
2946 len += sprintf(p + len, "status:\t\tsupported\n");
2947 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2948 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2949 if (video_supported == TPACPI_VIDEO_NEW)
2950 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2951 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2952 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2953 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2954 if (video_supported == TPACPI_VIDEO_NEW)
2955 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2956 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2957 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2962 static int video_write(char *buf)
2965 int enable, disable, status;
2968 if (video_supported == TPACPI_VIDEO_NONE)
2974 while ((cmd = next_cmd(&buf))) {
2975 if (strlencmp(cmd, "lcd_enable") == 0) {
2976 enable |= TP_ACPI_VIDEO_S_LCD;
2977 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2978 disable |= TP_ACPI_VIDEO_S_LCD;
2979 } else if (strlencmp(cmd, "crt_enable") == 0) {
2980 enable |= TP_ACPI_VIDEO_S_CRT;
2981 } else if (strlencmp(cmd, "crt_disable") == 0) {
2982 disable |= TP_ACPI_VIDEO_S_CRT;
2983 } else if (video_supported == TPACPI_VIDEO_NEW &&
2984 strlencmp(cmd, "dvi_enable") == 0) {
2985 enable |= TP_ACPI_VIDEO_S_DVI;
2986 } else if (video_supported == TPACPI_VIDEO_NEW &&
2987 strlencmp(cmd, "dvi_disable") == 0) {
2988 disable |= TP_ACPI_VIDEO_S_DVI;
2989 } else if (strlencmp(cmd, "auto_enable") == 0) {
2990 res = video_autosw_set(1);
2993 } else if (strlencmp(cmd, "auto_disable") == 0) {
2994 res = video_autosw_set(0);
2997 } else if (strlencmp(cmd, "video_switch") == 0) {
2998 res = video_outputsw_cycle();
3001 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3002 res = video_expand_toggle();
3009 if (enable || disable) {
3010 status = video_outputsw_get();
3013 res = video_outputsw_set((status & ~disable) | enable);
3021 static struct ibm_struct video_driver_data = {
3024 .write = video_write,
3028 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3030 /*************************************************************************
3031 * Light (thinklight) subdriver
3034 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3035 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3037 static int __init light_init(struct ibm_init_struct *iibm)
3039 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3041 TPACPI_ACPIHANDLE_INIT(ledb);
3042 TPACPI_ACPIHANDLE_INIT(lght);
3043 TPACPI_ACPIHANDLE_INIT(cmos);
3045 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3046 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3048 if (tp_features.light)
3049 /* light status not supported on
3050 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3051 tp_features.light_status =
3052 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3054 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3055 str_supported(tp_features.light));
3057 return (tp_features.light)? 0 : 1;
3060 static int light_read(char *p)
3065 if (!tp_features.light) {
3066 len += sprintf(p + len, "status:\t\tnot supported\n");
3067 } else if (!tp_features.light_status) {
3068 len += sprintf(p + len, "status:\t\tunknown\n");
3069 len += sprintf(p + len, "commands:\ton, off\n");
3071 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3073 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3074 len += sprintf(p + len, "commands:\ton, off\n");
3080 static int light_write(char *buf)
3082 int cmos_cmd, lght_cmd;
3086 if (!tp_features.light)
3089 while ((cmd = next_cmd(&buf))) {
3090 if (strlencmp(cmd, "on") == 0) {
3093 } else if (strlencmp(cmd, "off") == 0) {
3099 success = cmos_handle ?
3100 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3101 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3109 static struct ibm_struct light_driver_data = {
3112 .write = light_write,
3115 /*************************************************************************
3119 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3121 static void dock_notify(struct ibm_struct *ibm, u32 event);
3122 static int dock_read(char *p);
3123 static int dock_write(char *buf);
3125 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3126 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3127 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3128 "\\_SB.PCI.ISA.SLCE", /* 570 */
3129 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3131 /* don't list other alternatives as we install a notify handler on the 570 */
3132 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3134 static const struct acpi_device_id ibm_pci_device_ids[] = {
3135 {PCI_ROOT_HID_STRING, 0},
3139 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3141 .notify = dock_notify,
3142 .handle = &dock_handle,
3143 .type = ACPI_SYSTEM_NOTIFY,
3146 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3147 * We just use it to get notifications of dock hotplug
3148 * in very old thinkpads */
3149 .hid = ibm_pci_device_ids,
3150 .notify = dock_notify,
3151 .handle = &pci_handle,
3152 .type = ACPI_SYSTEM_NOTIFY,
3156 static struct ibm_struct dock_driver_data[2] = {
3160 .write = dock_write,
3161 .acpi = &ibm_dock_acpidriver[0],
3165 .acpi = &ibm_dock_acpidriver[1],
3169 #define dock_docked() (_sta(dock_handle) & 1)
3171 static int __init dock_init(struct ibm_init_struct *iibm)
3173 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3175 TPACPI_ACPIHANDLE_INIT(dock);
3177 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3178 str_supported(dock_handle != NULL));
3180 return (dock_handle)? 0 : 1;
3183 static int __init dock_init2(struct ibm_init_struct *iibm)
3187 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3189 if (dock_driver_data[0].flags.acpi_driver_registered &&
3190 dock_driver_data[0].flags.acpi_notify_installed) {
3191 TPACPI_ACPIHANDLE_INIT(pci);
3192 dock2_needed = (pci_handle != NULL);
3193 vdbg_printk(TPACPI_DBG_INIT,
3194 "dock PCI handler for the TP 570 is %s\n",
3195 str_supported(dock2_needed));
3197 vdbg_printk(TPACPI_DBG_INIT,
3198 "dock subdriver part 2 not required\n");
3202 return (dock2_needed)? 0 : 1;
3205 static void dock_notify(struct ibm_struct *ibm, u32 event)
3207 int docked = dock_docked();
3208 int pci = ibm->acpi->hid && ibm->acpi->device &&
3209 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3212 if (event == 1 && !pci) /* 570 */
3213 data = 1; /* button */
3214 else if (event == 1 && pci) /* 570 */
3215 data = 3; /* dock */
3216 else if (event == 3 && docked)
3217 data = 1; /* button */
3218 else if (event == 3 && !docked)
3219 data = 2; /* undock */
3220 else if (event == 0 && docked)
3221 data = 3; /* dock */
3223 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3224 event, _sta(dock_handle));
3225 data = 0; /* unknown */
3227 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3228 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3229 ibm->acpi->device->dev.bus_id,
3233 static int dock_read(char *p)
3236 int docked = dock_docked();
3239 len += sprintf(p + len, "status:\t\tnot supported\n");
3241 len += sprintf(p + len, "status:\t\tundocked\n");
3243 len += sprintf(p + len, "status:\t\tdocked\n");
3244 len += sprintf(p + len, "commands:\tdock, undock\n");
3250 static int dock_write(char *buf)
3257 while ((cmd = next_cmd(&buf))) {
3258 if (strlencmp(cmd, "undock") == 0) {
3259 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3260 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3262 } else if (strlencmp(cmd, "dock") == 0) {
3263 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3272 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3274 /*************************************************************************
3278 #ifdef CONFIG_THINKPAD_ACPI_BAY
3280 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3281 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3282 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3283 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3284 ); /* A21e, R30, R31 */
3285 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3286 "_EJ0", /* all others */
3287 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3288 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3289 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3291 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3295 static int __init bay_init(struct ibm_init_struct *iibm)
3297 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3299 TPACPI_ACPIHANDLE_INIT(bay);
3301 TPACPI_ACPIHANDLE_INIT(bay_ej);
3302 TPACPI_ACPIHANDLE_INIT(bay2);
3304 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3306 tp_features.bay_status = bay_handle &&
3307 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3308 tp_features.bay_status2 = bay2_handle &&
3309 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3311 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3312 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3313 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3314 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3316 vdbg_printk(TPACPI_DBG_INIT,
3317 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3318 str_supported(tp_features.bay_status),
3319 str_supported(tp_features.bay_eject),
3320 str_supported(tp_features.bay_status2),
3321 str_supported(tp_features.bay_eject2));
3323 return (tp_features.bay_status || tp_features.bay_eject ||
3324 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3327 static void bay_notify(struct ibm_struct *ibm, u32 event)
3329 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3330 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3331 ibm->acpi->device->dev.bus_id,
3335 #define bay_occupied(b) (_sta(b##_handle) & 1)
3337 static int bay_read(char *p)
3340 int occupied = bay_occupied(bay);
3341 int occupied2 = bay_occupied(bay2);
3344 len += sprintf(p + len, "status:\t\t%s\n",
3345 tp_features.bay_status ?
3346 (occupied ? "occupied" : "unoccupied") :
3348 if (tp_features.bay_status2)
3349 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3350 "occupied" : "unoccupied");
3352 eject = tp_features.bay_eject && occupied;
3353 eject2 = tp_features.bay_eject2 && occupied2;
3355 if (eject && eject2)
3356 len += sprintf(p + len, "commands:\teject, eject2\n");
3358 len += sprintf(p + len, "commands:\teject\n");
3360 len += sprintf(p + len, "commands:\teject2\n");
3365 static int bay_write(char *buf)
3369 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3372 while ((cmd = next_cmd(&buf))) {
3373 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3374 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3376 } else if (tp_features.bay_eject2 &&
3377 strlencmp(cmd, "eject2") == 0) {
3378 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3387 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3388 .notify = bay_notify,
3389 .handle = &bay_handle,
3390 .type = ACPI_SYSTEM_NOTIFY,
3393 static struct ibm_struct bay_driver_data = {
3397 .acpi = &ibm_bay_acpidriver,
3400 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3402 /*************************************************************************
3406 /* sysfs cmos_command -------------------------------------------------- */
3407 static ssize_t cmos_command_store(struct device *dev,
3408 struct device_attribute *attr,
3409 const char *buf, size_t count)
3411 unsigned long cmos_cmd;
3414 if (parse_strtoul(buf, 21, &cmos_cmd))
3417 res = issue_thinkpad_cmos_command(cmos_cmd);
3418 return (res)? res : count;
3421 static struct device_attribute dev_attr_cmos_command =
3422 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3424 /* --------------------------------------------------------------------- */
3426 static int __init cmos_init(struct ibm_init_struct *iibm)
3430 vdbg_printk(TPACPI_DBG_INIT,
3431 "initializing cmos commands subdriver\n");
3433 TPACPI_ACPIHANDLE_INIT(cmos);
3435 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3436 str_supported(cmos_handle != NULL));
3438 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3442 return (cmos_handle)? 0 : 1;
3445 static void cmos_exit(void)
3447 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3450 static int cmos_read(char *p)
3454 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3455 R30, R31, T20-22, X20-21 */
3457 len += sprintf(p + len, "status:\t\tnot supported\n");
3459 len += sprintf(p + len, "status:\t\tsupported\n");
3460 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3466 static int cmos_write(char *buf)
3471 while ((cmd = next_cmd(&buf))) {
3472 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3473 cmos_cmd >= 0 && cmos_cmd <= 21) {
3478 res = issue_thinkpad_cmos_command(cmos_cmd);
3486 static struct ibm_struct cmos_driver_data = {
3489 .write = cmos_write,
3493 /*************************************************************************
3497 enum led_access_mode {
3498 TPACPI_LED_NONE = 0,
3499 TPACPI_LED_570, /* 570 */
3500 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3501 TPACPI_LED_NEW, /* all others */
3504 enum { /* For TPACPI_LED_OLD */
3505 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3506 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3507 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3510 static enum led_access_mode led_supported;
3512 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3513 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3514 /* T20-22, X20-21 */
3515 "LED", /* all others */
3518 static int __init led_init(struct ibm_init_struct *iibm)
3520 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3522 TPACPI_ACPIHANDLE_INIT(led);
3525 /* led not supported on R30, R31 */
3526 led_supported = TPACPI_LED_NONE;
3527 else if (strlencmp(led_path, "SLED") == 0)
3529 led_supported = TPACPI_LED_570;
3530 else if (strlencmp(led_path, "SYSL") == 0)
3531 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3532 led_supported = TPACPI_LED_OLD;
3535 led_supported = TPACPI_LED_NEW;
3537 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3538 str_supported(led_supported), led_supported);
3540 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3543 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3545 static int led_read(char *p)
3549 if (!led_supported) {
3550 len += sprintf(p + len, "status:\t\tnot supported\n");
3553 len += sprintf(p + len, "status:\t\tsupported\n");
3555 if (led_supported == TPACPI_LED_570) {
3558 for (i = 0; i < 8; i++) {
3559 if (!acpi_evalf(ec_handle,
3560 &status, "GLED", "dd", 1 << i))
3562 len += sprintf(p + len, "%d:\t\t%s\n",
3563 i, led_status(status));
3567 len += sprintf(p + len, "commands:\t"
3568 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3573 /* off, on, blink */
3574 static const int led_sled_arg1[] = { 0, 1, 3 };
3575 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3576 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3577 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3579 static int led_write(char *buf)
3587 while ((cmd = next_cmd(&buf))) {
3588 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3591 if (strstr(cmd, "off")) {
3593 } else if (strstr(cmd, "on")) {
3595 } else if (strstr(cmd, "blink")) {
3600 if (led_supported == TPACPI_LED_570) {
3603 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3604 led, led_sled_arg1[ind]))
3606 } else if (led_supported == TPACPI_LED_OLD) {
3607 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3609 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3611 ret = ec_write(TPACPI_LED_EC_HLBL,
3612 led * led_exp_hlbl[ind]);
3614 ret = ec_write(TPACPI_LED_EC_HLCL,
3615 led * led_exp_hlcl[ind]);
3620 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3621 led, led_led_arg1[ind]))
3629 static struct ibm_struct led_driver_data = {
3635 /*************************************************************************
3639 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3641 static int __init beep_init(struct ibm_init_struct *iibm)
3643 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3645 TPACPI_ACPIHANDLE_INIT(beep);
3647 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3648 str_supported(beep_handle != NULL));
3650 return (beep_handle)? 0 : 1;
3653 static int beep_read(char *p)
3658 len += sprintf(p + len, "status:\t\tnot supported\n");
3660 len += sprintf(p + len, "status:\t\tsupported\n");
3661 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3667 static int beep_write(char *buf)
3675 while ((cmd = next_cmd(&buf))) {
3676 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3677 beep_cmd >= 0 && beep_cmd <= 17) {
3681 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3688 static struct ibm_struct beep_driver_data = {
3691 .write = beep_write,
3694 /*************************************************************************
3698 enum thermal_access_mode {
3699 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3700 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3701 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3702 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3703 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3706 enum { /* TPACPI_THERMAL_TPEC_* */
3707 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3708 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3709 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3712 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3713 struct ibm_thermal_sensors_struct {
3714 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3717 static enum thermal_access_mode thermal_read_mode;
3719 /* idx is zero-based */
3720 static int thermal_get_sensor(int idx, s32 *value)
3726 t = TP_EC_THERMAL_TMP0;
3728 switch (thermal_read_mode) {
3729 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3730 case TPACPI_THERMAL_TPEC_16:
3731 if (idx >= 8 && idx <= 15) {
3732 t = TP_EC_THERMAL_TMP8;
3737 case TPACPI_THERMAL_TPEC_8:
3739 if (!acpi_ec_read(t + idx, &tmp))
3741 *value = tmp * 1000;
3746 case TPACPI_THERMAL_ACPI_UPDT:
3748 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3749 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3751 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3753 *value = (t - 2732) * 100;
3758 case TPACPI_THERMAL_ACPI_TMP07:
3760 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3761 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3763 if (t > 127 || t < -127)
3764 t = TP_EC_THERMAL_TMP_NA;
3770 case TPACPI_THERMAL_NONE:
3778 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3789 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3792 for (i = 0 ; i < n; i++) {
3793 res = thermal_get_sensor(i, &s->temp[i]);
3801 /* sysfs temp##_input -------------------------------------------------- */
3803 static ssize_t thermal_temp_input_show(struct device *dev,
3804 struct device_attribute *attr,
3807 struct sensor_device_attribute *sensor_attr =
3808 to_sensor_dev_attr(attr);
3809 int idx = sensor_attr->index;
3813 res = thermal_get_sensor(idx, &value);
3816 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3819 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3822 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3823 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
3824 thermal_temp_input_show, NULL, _idxB)
3826 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3827 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3828 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3829 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3830 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3831 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3832 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3833 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3834 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3835 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3836 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3837 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3838 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3839 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3840 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3841 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3842 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3845 #define THERMAL_ATTRS(X) \
3846 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3848 static struct attribute *thermal_temp_input_attr[] = {
3868 static const struct attribute_group thermal_temp_input16_group = {
3869 .attrs = thermal_temp_input_attr
3872 static const struct attribute_group thermal_temp_input8_group = {
3873 .attrs = &thermal_temp_input_attr[8]
3876 #undef THERMAL_SENSOR_ATTR_TEMP
3877 #undef THERMAL_ATTRS
3879 /* --------------------------------------------------------------------- */
3881 static int __init thermal_init(struct ibm_init_struct *iibm)
3888 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3890 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3892 if (thinkpad_id.ec_model) {
3894 * Direct EC access mode: sensors at registers
3895 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3896 * non-implemented, thermal sensors return 0x80 when
3901 for (i = 0; i < 8; i++) {
3902 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3908 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3916 /* This is sheer paranoia, but we handle it anyway */
3919 "ThinkPad ACPI EC access misbehaving, "
3920 "falling back to ACPI TMPx access "
3922 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3925 "ThinkPad ACPI EC access misbehaving, "
3926 "disabling thermal sensors access\n");
3927 thermal_read_mode = TPACPI_THERMAL_NONE;
3932 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3934 } else if (acpi_tmp7) {
3935 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3936 /* 600e/x, 770e, 770x */
3937 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3939 /* Standard ACPI TMPx access, max 8 sensors */
3940 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3943 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3944 thermal_read_mode = TPACPI_THERMAL_NONE;
3947 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3948 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3951 switch (thermal_read_mode) {
3952 case TPACPI_THERMAL_TPEC_16:
3953 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3954 &thermal_temp_input16_group);
3958 case TPACPI_THERMAL_TPEC_8:
3959 case TPACPI_THERMAL_ACPI_TMP07:
3960 case TPACPI_THERMAL_ACPI_UPDT:
3961 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3962 &thermal_temp_input8_group);
3966 case TPACPI_THERMAL_NONE:
3974 static void thermal_exit(void)
3976 switch (thermal_read_mode) {
3977 case TPACPI_THERMAL_TPEC_16:
3978 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3979 &thermal_temp_input16_group);
3981 case TPACPI_THERMAL_TPEC_8:
3982 case TPACPI_THERMAL_ACPI_TMP07:
3983 case TPACPI_THERMAL_ACPI_UPDT:
3984 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3985 &thermal_temp_input16_group);
3987 case TPACPI_THERMAL_NONE:
3993 static int thermal_read(char *p)
3997 struct ibm_thermal_sensors_struct t;
3999 n = thermal_get_sensors(&t);
4000 if (unlikely(n < 0))
4003 len += sprintf(p + len, "temperatures:\t");
4006 for (i = 0; i < (n - 1); i++)
4007 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4008 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4010 len += sprintf(p + len, "not supported\n");
4015 static struct ibm_struct thermal_driver_data = {
4017 .read = thermal_read,
4018 .exit = thermal_exit,
4021 /*************************************************************************
4025 static u8 ecdump_regs[256];
4027 static int ecdump_read(char *p)
4033 len += sprintf(p + len, "EC "
4034 " +00 +01 +02 +03 +04 +05 +06 +07"
4035 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4036 for (i = 0; i < 256; i += 16) {
4037 len += sprintf(p + len, "EC 0x%02x:", i);
4038 for (j = 0; j < 16; j++) {
4039 if (!acpi_ec_read(i + j, &v))
4041 if (v != ecdump_regs[i + j])
4042 len += sprintf(p + len, " *%02x", v);
4044 len += sprintf(p + len, " %02x", v);
4045 ecdump_regs[i + j] = v;
4047 len += sprintf(p + len, "\n");
4052 /* These are way too dangerous to advertise openly... */
4054 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4055 " (<offset> is 00-ff, <value> is 00-ff)\n");
4056 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4057 " (<offset> is 00-ff, <value> is 0-255)\n");
4062 static int ecdump_write(char *buf)
4067 while ((cmd = next_cmd(&buf))) {
4068 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4070 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4074 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4075 if (!acpi_ec_write(i, v))
4084 static struct ibm_struct ecdump_driver_data = {
4086 .read = ecdump_read,
4087 .write = ecdump_write,
4088 .flags.experimental = 1,
4091 /*************************************************************************
4092 * Backlight/brightness subdriver
4095 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4097 static struct backlight_device *ibm_backlight_device;
4098 static int brightness_offset = 0x31;
4099 static int brightness_mode;
4100 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4102 static struct mutex brightness_mutex;
4105 * ThinkPads can read brightness from two places: EC 0x31, or
4106 * CMOS NVRAM byte 0x5E, bits 0-3.
4108 static int brightness_get(struct backlight_device *bd)
4110 u8 lec = 0, lcmos = 0, level = 0;
4112 if (brightness_mode & 1) {
4113 if (!acpi_ec_read(brightness_offset, &lec))
4115 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4118 if (brightness_mode & 2) {
4119 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4120 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4121 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4122 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4126 if (brightness_mode == 3 && lec != lcmos) {
4128 "CMOS NVRAM (%u) and EC (%u) do not agree "
4129 "on display brightness level\n",
4130 (unsigned int) lcmos,
4131 (unsigned int) lec);
4138 /* May return EINTR which can always be mapped to ERESTARTSYS */
4139 static int brightness_set(int value)
4141 int cmos_cmd, inc, i, res;
4144 if (value > ((tp_features.bright_16levels)? 15 : 7))
4147 res = mutex_lock_interruptible(&brightness_mutex);
4151 current_value = brightness_get(NULL);
4152 if (current_value < 0) {
4153 res = current_value;
4157 cmos_cmd = value > current_value ?
4158 TP_CMOS_BRIGHTNESS_UP :
4159 TP_CMOS_BRIGHTNESS_DOWN;
4160 inc = (value > current_value)? 1 : -1;
4163 for (i = current_value; i != value; i += inc) {
4164 if ((brightness_mode & 2) &&
4165 issue_thinkpad_cmos_command(cmos_cmd)) {
4169 if ((brightness_mode & 1) &&
4170 !acpi_ec_write(brightness_offset, i + inc)) {
4177 mutex_unlock(&brightness_mutex);
4181 /* sysfs backlight class ----------------------------------------------- */
4183 static int brightness_update_status(struct backlight_device *bd)
4185 /* it is the backlight class's job (caller) to handle
4186 * EINTR and other errors properly */
4187 return brightness_set(
4188 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4189 bd->props.power == FB_BLANK_UNBLANK) ?
4190 bd->props.brightness : 0);
4193 static struct backlight_ops ibm_backlight_data = {
4194 .get_brightness = brightness_get,
4195 .update_status = brightness_update_status,
4198 /* --------------------------------------------------------------------- */
4200 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4202 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4203 union acpi_object *obj;
4206 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4207 obj = (union acpi_object *)buffer.pointer;
4208 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4209 printk(TPACPI_ERR "Unknown BCLL data, "
4210 "please report this to %s\n", TPACPI_MAIL);
4213 rc = obj->package.count;
4219 kfree(buffer.pointer);
4223 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4224 void *context, void **rv)
4226 char name[ACPI_PATH_SEGMENT_LENGTH];
4227 struct acpi_buffer buffer = { sizeof(name), &name };
4229 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4230 !strncmp("BCLL", name, sizeof(name) - 1)) {
4231 if (tpacpi_query_bcll_levels(handle) == 16) {
4233 return AE_CTRL_TERMINATE;
4242 static int __init brightness_check_levels(void)
4245 void *found_node = NULL;
4248 TPACPI_ACPIHANDLE_INIT(vid);
4253 /* Search for a BCLL package with 16 levels */
4254 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4255 brightness_find_bcll, NULL,
4258 return (ACPI_SUCCESS(status) && found_node != NULL);
4261 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4262 void *context, void **rv)
4264 char name[ACPI_PATH_SEGMENT_LENGTH];
4265 struct acpi_buffer buffer = { sizeof(name), &name };
4267 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4268 !strncmp("_BCL", name, sizeof(name) - 1)) {
4270 return AE_CTRL_TERMINATE;
4276 static int __init brightness_check_std_acpi_support(void)
4279 void *found_node = NULL;
4282 TPACPI_ACPIHANDLE_INIT(vid);
4287 /* Search for a _BCL method, but don't execute it */
4288 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4289 brightness_find_bcl, NULL, &found_node);
4291 return (ACPI_SUCCESS(status) && found_node != NULL);
4294 static int __init brightness_init(struct ibm_init_struct *iibm)
4298 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4300 mutex_init(&brightness_mutex);
4302 if (!brightness_enable) {
4303 dbg_printk(TPACPI_DBG_INIT,
4304 "brightness support disabled by "
4305 "module parameter\n");
4307 } else if (brightness_enable > 1) {
4308 if (brightness_check_std_acpi_support()) {
4309 printk(TPACPI_NOTICE
4310 "standard ACPI backlight interface "
4311 "available, not loading native one...\n");
4316 if (!brightness_mode) {
4317 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4318 brightness_mode = 2;
4320 brightness_mode = 3;
4322 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4326 if (brightness_mode > 3)
4329 tp_features.bright_16levels =
4330 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4331 brightness_check_levels();
4333 b = brightness_get(NULL);
4337 if (tp_features.bright_16levels)
4339 "detected a 16-level brightness capable ThinkPad\n");
4341 ibm_backlight_device = backlight_device_register(
4342 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4343 &ibm_backlight_data);
4344 if (IS_ERR(ibm_backlight_device)) {
4345 printk(TPACPI_ERR "Could not register backlight device\n");
4346 return PTR_ERR(ibm_backlight_device);
4348 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4350 ibm_backlight_device->props.max_brightness =
4351 (tp_features.bright_16levels)? 15 : 7;
4352 ibm_backlight_device->props.brightness = b;
4353 backlight_update_status(ibm_backlight_device);
4358 static void brightness_exit(void)
4360 if (ibm_backlight_device) {
4361 vdbg_printk(TPACPI_DBG_EXIT,
4362 "calling backlight_device_unregister()\n");
4363 backlight_device_unregister(ibm_backlight_device);
4364 ibm_backlight_device = NULL;
4368 static int brightness_read(char *p)
4373 level = brightness_get(NULL);
4375 len += sprintf(p + len, "level:\t\tunreadable\n");
4377 len += sprintf(p + len, "level:\t\t%d\n", level);
4378 len += sprintf(p + len, "commands:\tup, down\n");
4379 len += sprintf(p + len, "commands:\tlevel <level>"
4380 " (<level> is 0-%d)\n",
4381 (tp_features.bright_16levels) ? 15 : 7);
4387 static int brightness_write(char *buf)
4392 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4394 level = brightness_get(NULL);
4398 while ((cmd = next_cmd(&buf))) {
4399 if (strlencmp(cmd, "up") == 0) {
4400 if (level < max_level)
4402 } else if (strlencmp(cmd, "down") == 0) {
4405 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4406 level >= 0 && level <= max_level) {
4413 * Now we know what the final level should be, so we try to set it.
4414 * Doing it this way makes the syscall restartable in case of EINTR
4416 rc = brightness_set(level);
4417 return (rc == -EINTR)? ERESTARTSYS : rc;
4420 static struct ibm_struct brightness_driver_data = {
4421 .name = "brightness",
4422 .read = brightness_read,
4423 .write = brightness_write,
4424 .exit = brightness_exit,
4427 /*************************************************************************
4431 static int volume_offset = 0x30;
4433 static int volume_read(char *p)
4438 if (!acpi_ec_read(volume_offset, &level)) {
4439 len += sprintf(p + len, "level:\t\tunreadable\n");
4441 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4442 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4443 len += sprintf(p + len, "commands:\tup, down, mute\n");
4444 len += sprintf(p + len, "commands:\tlevel <level>"
4445 " (<level> is 0-15)\n");
4451 static int volume_write(char *buf)
4453 int cmos_cmd, inc, i;
4455 int new_level, new_mute;
4458 while ((cmd = next_cmd(&buf))) {
4459 if (!acpi_ec_read(volume_offset, &level))
4461 new_mute = mute = level & 0x40;
4462 new_level = level = level & 0xf;
4464 if (strlencmp(cmd, "up") == 0) {
4468 new_level = level == 15 ? 15 : level + 1;
4469 } else if (strlencmp(cmd, "down") == 0) {
4473 new_level = level == 0 ? 0 : level - 1;
4474 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4475 new_level >= 0 && new_level <= 15) {
4477 } else if (strlencmp(cmd, "mute") == 0) {
4482 if (new_level != level) {
4483 /* mute doesn't change */
4485 cmos_cmd = (new_level > level) ?
4486 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4487 inc = new_level > level ? 1 : -1;
4489 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4490 !acpi_ec_write(volume_offset, level)))
4493 for (i = level; i != new_level; i += inc)
4494 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4495 !acpi_ec_write(volume_offset, i + inc))
4499 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4500 !acpi_ec_write(volume_offset, new_level + mute))) {
4505 if (new_mute != mute) {
4506 /* level doesn't change */
4508 cmos_cmd = (new_mute) ?
4509 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4511 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4512 !acpi_ec_write(volume_offset, level + new_mute))
4520 static struct ibm_struct volume_driver_data = {
4522 .read = volume_read,
4523 .write = volume_write,
4526 /*************************************************************************
4533 * TPACPI_FAN_RD_ACPI_GFAN:
4534 * ACPI GFAN method: returns fan level
4536 * see TPACPI_FAN_WR_ACPI_SFAN
4537 * EC 0x2f (HFSP) not available if GFAN exists
4539 * TPACPI_FAN_WR_ACPI_SFAN:
4540 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4542 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4545 * TPACPI_FAN_WR_TPEC:
4546 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4547 * Supported on almost all ThinkPads
4549 * Fan speed changes of any sort (including those caused by the
4550 * disengaged mode) are usually done slowly by the firmware as the
4551 * maximum ammount of fan duty cycle change per second seems to be
4554 * Reading is not available if GFAN exists.
4555 * Writing is not available if SFAN exists.
4558 * 7 automatic mode engaged;
4559 * (default operation mode of the ThinkPad)
4560 * fan level is ignored in this mode.
4561 * 6 full speed mode (takes precedence over bit 7);
4562 * not available on all thinkpads. May disable
4563 * the tachometer while the fan controller ramps up
4564 * the speed (which can take up to a few *minutes*).
4565 * Speeds up fan to 100% duty-cycle, which is far above
4566 * the standard RPM levels. It is not impossible that
4567 * it could cause hardware damage.
4568 * 5-3 unused in some models. Extra bits for fan level
4569 * in others, but still useless as all values above
4570 * 7 map to the same speed as level 7 in these models.
4571 * 2-0 fan level (0..7 usually)
4573 * 0x07 = max (set when temperatures critical)
4574 * Some ThinkPads may have other levels, see
4575 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4577 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4578 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4579 * does so, its initial value is meaningless (0x07).
4581 * For firmware bugs, refer to:
4582 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4586 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4587 * Main fan tachometer reading (in RPM)
4589 * This register is present on all ThinkPads with a new-style EC, and
4590 * it is known not to be present on the A21m/e, and T22, as there is
4591 * something else in offset 0x84 according to the ACPI DSDT. Other
4592 * ThinkPads from this same time period (and earlier) probably lack the
4593 * tachometer as well.
4595 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4596 * was never fixed by IBM to report the EC firmware version string
4597 * probably support the tachometer (like the early X models), so
4598 * detecting it is quite hard. We need more data to know for sure.
4600 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4603 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4606 * For firmware bugs, refer to:
4607 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4609 * TPACPI_FAN_WR_ACPI_FANS:
4610 * ThinkPad X31, X40, X41. Not available in the X60.
4612 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4613 * high speed. ACPI DSDT seems to map these three speeds to levels
4614 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4615 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4617 * The speeds are stored on handles
4618 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4620 * There are three default speed sets, acessible as handles:
4621 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4623 * ACPI DSDT switches which set is in use depending on various
4626 * TPACPI_FAN_WR_TPEC is also available and should be used to
4627 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4628 * but the ACPI tables just mention level 7.
4631 enum { /* Fan control constants */
4632 fan_status_offset = 0x2f, /* EC register 0x2f */
4633 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4634 * 0x84 must be read before 0x85 */
4636 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4637 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4639 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4642 enum fan_status_access_mode {
4643 TPACPI_FAN_NONE = 0, /* No fan status or control */
4644 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4645 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4648 enum fan_control_access_mode {
4649 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4650 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4651 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4652 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4655 enum fan_control_commands {
4656 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4657 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4658 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4659 * and also watchdog cmd */
4662 static int fan_control_allowed;
4664 static enum fan_status_access_mode fan_status_access_mode;
4665 static enum fan_control_access_mode fan_control_access_mode;
4666 static enum fan_control_commands fan_control_commands;
4668 static u8 fan_control_initial_status;
4669 static u8 fan_control_desired_level;
4670 static int fan_watchdog_maxinterval;
4672 static struct mutex fan_mutex;
4674 static void fan_watchdog_fire(struct work_struct *ignored);
4675 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4677 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4678 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4679 "\\FSPD", /* 600e/x, 770e, 770x */
4681 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4682 "JFNS", /* 770x-JL */
4686 * Call with fan_mutex held
4688 static void fan_update_desired_level(u8 status)
4691 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4693 fan_control_desired_level = 7;
4695 fan_control_desired_level = status;
4699 static int fan_get_status(u8 *status)
4704 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4706 switch (fan_status_access_mode) {
4707 case TPACPI_FAN_RD_ACPI_GFAN:
4708 /* 570, 600e/x, 770e, 770x */
4710 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4718 case TPACPI_FAN_RD_TPEC:
4719 /* all except 570, 600e/x, 770e, 770x */
4720 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4735 static int fan_get_status_safe(u8 *status)
4740 if (mutex_lock_interruptible(&fan_mutex))
4741 return -ERESTARTSYS;
4742 rc = fan_get_status(&s);
4744 fan_update_desired_level(s);
4745 mutex_unlock(&fan_mutex);
4753 static int fan_get_speed(unsigned int *speed)
4757 switch (fan_status_access_mode) {
4758 case TPACPI_FAN_RD_TPEC:
4759 /* all except 570, 600e/x, 770e, 770x */
4760 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4761 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4765 *speed = (hi << 8) | lo;
4776 static int fan_set_level(int level)
4778 if (!fan_control_allowed)
4781 switch (fan_control_access_mode) {
4782 case TPACPI_FAN_WR_ACPI_SFAN:
4783 if (level >= 0 && level <= 7) {
4784 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4790 case TPACPI_FAN_WR_ACPI_FANS:
4791 case TPACPI_FAN_WR_TPEC:
4792 if ((level != TP_EC_FAN_AUTO) &&
4793 (level != TP_EC_FAN_FULLSPEED) &&
4794 ((level < 0) || (level > 7)))
4797 /* safety net should the EC not support AUTO
4798 * or FULLSPEED mode bits and just ignore them */
4799 if (level & TP_EC_FAN_FULLSPEED)
4800 level |= 7; /* safety min speed 7 */
4801 else if (level & TP_EC_FAN_AUTO)
4802 level |= 4; /* safety min speed 4 */
4804 if (!acpi_ec_write(fan_status_offset, level))
4807 tp_features.fan_ctrl_status_undef = 0;
4816 static int fan_set_level_safe(int level)
4820 if (!fan_control_allowed)
4823 if (mutex_lock_interruptible(&fan_mutex))
4824 return -ERESTARTSYS;
4826 if (level == TPACPI_FAN_LAST_LEVEL)
4827 level = fan_control_desired_level;
4829 rc = fan_set_level(level);
4831 fan_update_desired_level(level);
4833 mutex_unlock(&fan_mutex);
4837 static int fan_set_enable(void)
4842 if (!fan_control_allowed)
4845 if (mutex_lock_interruptible(&fan_mutex))
4846 return -ERESTARTSYS;
4848 switch (fan_control_access_mode) {
4849 case TPACPI_FAN_WR_ACPI_FANS:
4850 case TPACPI_FAN_WR_TPEC:
4851 rc = fan_get_status(&s);
4855 /* Don't go out of emergency fan mode */
4858 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4861 if (!acpi_ec_write(fan_status_offset, s))
4864 tp_features.fan_ctrl_status_undef = 0;
4869 case TPACPI_FAN_WR_ACPI_SFAN:
4870 rc = fan_get_status(&s);
4876 /* Set fan to at least level 4 */
4879 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4889 mutex_unlock(&fan_mutex);
4893 static int fan_set_disable(void)
4897 if (!fan_control_allowed)
4900 if (mutex_lock_interruptible(&fan_mutex))
4901 return -ERESTARTSYS;
4904 switch (fan_control_access_mode) {
4905 case TPACPI_FAN_WR_ACPI_FANS:
4906 case TPACPI_FAN_WR_TPEC:
4907 if (!acpi_ec_write(fan_status_offset, 0x00))
4910 fan_control_desired_level = 0;
4911 tp_features.fan_ctrl_status_undef = 0;
4915 case TPACPI_FAN_WR_ACPI_SFAN:
4916 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4919 fan_control_desired_level = 0;
4927 mutex_unlock(&fan_mutex);
4931 static int fan_set_speed(int speed)
4935 if (!fan_control_allowed)
4938 if (mutex_lock_interruptible(&fan_mutex))
4939 return -ERESTARTSYS;
4942 switch (fan_control_access_mode) {
4943 case TPACPI_FAN_WR_ACPI_FANS:
4944 if (speed >= 0 && speed <= 65535) {
4945 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4946 speed, speed, speed))
4956 mutex_unlock(&fan_mutex);
4960 static void fan_watchdog_reset(void)
4962 static int fan_watchdog_active;
4964 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4967 if (fan_watchdog_active)
4968 cancel_delayed_work(&fan_watchdog_task);
4970 if (fan_watchdog_maxinterval > 0 &&
4971 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4972 fan_watchdog_active = 1;
4973 if (!schedule_delayed_work(&fan_watchdog_task,
4974 msecs_to_jiffies(fan_watchdog_maxinterval
4977 "failed to schedule the fan watchdog, "
4978 "watchdog will not trigger\n");
4981 fan_watchdog_active = 0;
4984 static void fan_watchdog_fire(struct work_struct *ignored)
4988 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4991 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
4992 rc = fan_set_enable();
4994 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
4995 "will try again later...\n", -rc);
4996 /* reschedule for later */
4997 fan_watchdog_reset();
5002 * SYSFS fan layout: hwmon compatible (device)
5005 * 0: "disengaged" mode
5007 * 2: native EC "auto" mode (recommended, hardware default)
5009 * pwm*: set speed in manual mode, ignored otherwise.
5010 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5013 * fan*_input: tachometer reading, RPM
5016 * SYSFS fan layout: extensions
5018 * fan_watchdog (driver):
5019 * fan watchdog interval in seconds, 0 disables (default), max 120
5022 /* sysfs fan pwm1_enable ----------------------------------------------- */
5023 static ssize_t fan_pwm1_enable_show(struct device *dev,
5024 struct device_attribute *attr,
5030 res = fan_get_status_safe(&status);
5034 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5035 if (status != fan_control_initial_status) {
5036 tp_features.fan_ctrl_status_undef = 0;
5038 /* Return most likely status. In fact, it
5039 * might be the only possible status */
5040 status = TP_EC_FAN_AUTO;
5044 if (status & TP_EC_FAN_FULLSPEED) {
5046 } else if (status & TP_EC_FAN_AUTO) {
5051 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5054 static ssize_t fan_pwm1_enable_store(struct device *dev,
5055 struct device_attribute *attr,
5056 const char *buf, size_t count)
5061 if (parse_strtoul(buf, 2, &t))
5066 level = TP_EC_FAN_FULLSPEED;
5069 level = TPACPI_FAN_LAST_LEVEL;
5072 level = TP_EC_FAN_AUTO;
5075 /* reserved for software-controlled auto mode */
5081 res = fan_set_level_safe(level);
5087 fan_watchdog_reset();
5092 static struct device_attribute dev_attr_fan_pwm1_enable =
5093 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5094 fan_pwm1_enable_show, fan_pwm1_enable_store);
5096 /* sysfs fan pwm1 ------------------------------------------------------ */
5097 static ssize_t fan_pwm1_show(struct device *dev,
5098 struct device_attribute *attr,
5104 res = fan_get_status_safe(&status);
5108 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5109 if (status != fan_control_initial_status) {
5110 tp_features.fan_ctrl_status_undef = 0;
5112 status = TP_EC_FAN_AUTO;
5117 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5118 status = fan_control_desired_level;
5123 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5126 static ssize_t fan_pwm1_store(struct device *dev,
5127 struct device_attribute *attr,
5128 const char *buf, size_t count)
5132 u8 status, newlevel;
5134 if (parse_strtoul(buf, 255, &s))
5137 /* scale down from 0-255 to 0-7 */
5138 newlevel = (s >> 5) & 0x07;
5140 if (mutex_lock_interruptible(&fan_mutex))
5141 return -ERESTARTSYS;
5143 rc = fan_get_status(&status);
5144 if (!rc && (status &
5145 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5146 rc = fan_set_level(newlevel);
5150 fan_update_desired_level(newlevel);
5151 fan_watchdog_reset();
5155 mutex_unlock(&fan_mutex);
5156 return (rc)? rc : count;
5159 static struct device_attribute dev_attr_fan_pwm1 =
5160 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5161 fan_pwm1_show, fan_pwm1_store);
5163 /* sysfs fan fan1_input ------------------------------------------------ */
5164 static ssize_t fan_fan1_input_show(struct device *dev,
5165 struct device_attribute *attr,
5171 res = fan_get_speed(&speed);
5175 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5178 static struct device_attribute dev_attr_fan_fan1_input =
5179 __ATTR(fan1_input, S_IRUGO,
5180 fan_fan1_input_show, NULL);
5182 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5183 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5186 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5189 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5190 const char *buf, size_t count)
5194 if (parse_strtoul(buf, 120, &t))
5197 if (!fan_control_allowed)
5200 fan_watchdog_maxinterval = t;
5201 fan_watchdog_reset();
5206 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5207 fan_fan_watchdog_show, fan_fan_watchdog_store);
5209 /* --------------------------------------------------------------------- */
5210 static struct attribute *fan_attributes[] = {
5211 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5212 &dev_attr_fan_fan1_input.attr,
5216 static const struct attribute_group fan_attr_group = {
5217 .attrs = fan_attributes,
5220 static int __init fan_init(struct ibm_init_struct *iibm)
5224 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5226 mutex_init(&fan_mutex);
5227 fan_status_access_mode = TPACPI_FAN_NONE;
5228 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5229 fan_control_commands = 0;
5230 fan_watchdog_maxinterval = 0;
5231 tp_features.fan_ctrl_status_undef = 0;
5232 fan_control_desired_level = 7;
5234 TPACPI_ACPIHANDLE_INIT(fans);
5235 TPACPI_ACPIHANDLE_INIT(gfan);
5236 TPACPI_ACPIHANDLE_INIT(sfan);
5239 /* 570, 600e/x, 770e, 770x */
5240 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5242 /* all other ThinkPads: note that even old-style
5243 * ThinkPad ECs supports the fan control register */
5244 if (likely(acpi_ec_read(fan_status_offset,
5245 &fan_control_initial_status))) {
5246 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5248 /* In some ThinkPads, neither the EC nor the ACPI
5249 * DSDT initialize the fan status, and it ends up
5250 * being set to 0x07 when it *could* be either
5253 * Enable for TP-1Y (T43), TP-78 (R51e),
5254 * TP-76 (R52), TP-70 (T43, R52), which are known
5256 if (fan_control_initial_status == 0x07) {
5257 switch (thinkpad_id.ec_model) {
5258 case 0x5931: /* TP-1Y */
5259 case 0x3837: /* TP-78 */
5260 case 0x3637: /* TP-76 */
5261 case 0x3037: /* TP-70 */
5262 printk(TPACPI_NOTICE
5263 "fan_init: initial fan status "
5264 "is unknown, assuming it is "
5266 tp_features.fan_ctrl_status_undef = 1;
5272 "ThinkPad ACPI EC access misbehaving, "
5273 "fan status and control unavailable\n");
5280 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5281 fan_control_commands |=
5282 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5285 /* gfan without sfan means no fan control */
5286 /* all other models implement TP EC 0x2f control */
5290 fan_control_access_mode =
5291 TPACPI_FAN_WR_ACPI_FANS;
5292 fan_control_commands |=
5293 TPACPI_FAN_CMD_SPEED |
5294 TPACPI_FAN_CMD_LEVEL |
5295 TPACPI_FAN_CMD_ENABLE;
5297 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5298 fan_control_commands |=
5299 TPACPI_FAN_CMD_LEVEL |
5300 TPACPI_FAN_CMD_ENABLE;
5305 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5306 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5307 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5308 fan_status_access_mode, fan_control_access_mode);
5310 /* fan control master switch */
5311 if (!fan_control_allowed) {
5312 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5313 fan_control_commands = 0;
5314 dbg_printk(TPACPI_DBG_INIT,
5315 "fan control features disabled by parameter\n");
5318 /* update fan_control_desired_level */
5319 if (fan_status_access_mode != TPACPI_FAN_NONE)
5320 fan_get_status_safe(NULL);
5322 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5323 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5324 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5327 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5328 &driver_attr_fan_watchdog);
5336 static void fan_exit(void)
5338 vdbg_printk(TPACPI_DBG_EXIT,
5339 "cancelling any pending fan watchdog tasks\n");
5341 /* FIXME: can we really do this unconditionally? */
5342 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5343 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5344 &driver_attr_fan_watchdog);
5346 cancel_delayed_work(&fan_watchdog_task);
5347 flush_scheduled_work();
5350 static int fan_read(char *p)
5355 unsigned int speed = 0;
5357 switch (fan_status_access_mode) {
5358 case TPACPI_FAN_RD_ACPI_GFAN:
5359 /* 570, 600e/x, 770e, 770x */
5360 rc = fan_get_status_safe(&status);
5364 len += sprintf(p + len, "status:\t\t%s\n"
5366 (status != 0) ? "enabled" : "disabled", status);
5369 case TPACPI_FAN_RD_TPEC:
5370 /* all except 570, 600e/x, 770e, 770x */
5371 rc = fan_get_status_safe(&status);
5375 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5376 if (status != fan_control_initial_status)
5377 tp_features.fan_ctrl_status_undef = 0;
5379 /* Return most likely status. In fact, it
5380 * might be the only possible status */
5381 status = TP_EC_FAN_AUTO;
5384 len += sprintf(p + len, "status:\t\t%s\n",
5385 (status != 0) ? "enabled" : "disabled");
5387 rc = fan_get_speed(&speed);
5391 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5393 if (status & TP_EC_FAN_FULLSPEED)
5394 /* Disengaged mode takes precedence */
5395 len += sprintf(p + len, "level:\t\tdisengaged\n");
5396 else if (status & TP_EC_FAN_AUTO)
5397 len += sprintf(p + len, "level:\t\tauto\n");
5399 len += sprintf(p + len, "level:\t\t%d\n", status);
5402 case TPACPI_FAN_NONE:
5404 len += sprintf(p + len, "status:\t\tnot supported\n");
5407 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5408 len += sprintf(p + len, "commands:\tlevel <level>");
5410 switch (fan_control_access_mode) {
5411 case TPACPI_FAN_WR_ACPI_SFAN:
5412 len += sprintf(p + len, " (<level> is 0-7)\n");
5416 len += sprintf(p + len, " (<level> is 0-7, "
5417 "auto, disengaged, full-speed)\n");
5422 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5423 len += sprintf(p + len, "commands:\tenable, disable\n"
5424 "commands:\twatchdog <timeout> (<timeout> "
5425 "is 0 (off), 1-120 (seconds))\n");
5427 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5428 len += sprintf(p + len, "commands:\tspeed <speed>"
5429 " (<speed> is 0-65535)\n");
5434 static int fan_write_cmd_level(const char *cmd, int *rc)
5438 if (strlencmp(cmd, "level auto") == 0)
5439 level = TP_EC_FAN_AUTO;
5440 else if ((strlencmp(cmd, "level disengaged") == 0) |
5441 (strlencmp(cmd, "level full-speed") == 0))
5442 level = TP_EC_FAN_FULLSPEED;
5443 else if (sscanf(cmd, "level %d", &level) != 1)
5446 *rc = fan_set_level_safe(level);
5448 printk(TPACPI_ERR "level command accepted for unsupported "
5449 "access mode %d", fan_control_access_mode);
5454 static int fan_write_cmd_enable(const char *cmd, int *rc)
5456 if (strlencmp(cmd, "enable") != 0)
5459 *rc = fan_set_enable();
5461 printk(TPACPI_ERR "enable command accepted for unsupported "
5462 "access mode %d", fan_control_access_mode);
5467 static int fan_write_cmd_disable(const char *cmd, int *rc)
5469 if (strlencmp(cmd, "disable") != 0)
5472 *rc = fan_set_disable();
5474 printk(TPACPI_ERR "disable command accepted for unsupported "
5475 "access mode %d", fan_control_access_mode);
5480 static int fan_write_cmd_speed(const char *cmd, int *rc)
5485 * Support speed <low> <medium> <high> ? */
5487 if (sscanf(cmd, "speed %d", &speed) != 1)
5490 *rc = fan_set_speed(speed);
5492 printk(TPACPI_ERR "speed command accepted for unsupported "
5493 "access mode %d", fan_control_access_mode);
5498 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5502 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5505 if (interval < 0 || interval > 120)
5508 fan_watchdog_maxinterval = interval;
5513 static int fan_write(char *buf)
5518 while (!rc && (cmd = next_cmd(&buf))) {
5519 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5520 fan_write_cmd_level(cmd, &rc)) &&
5521 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5522 (fan_write_cmd_enable(cmd, &rc) ||
5523 fan_write_cmd_disable(cmd, &rc) ||
5524 fan_write_cmd_watchdog(cmd, &rc))) &&
5525 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5526 fan_write_cmd_speed(cmd, &rc))
5530 fan_watchdog_reset();
5536 static struct ibm_struct fan_driver_data = {
5543 /****************************************************************************
5544 ****************************************************************************
5548 ****************************************************************************
5549 ****************************************************************************/
5551 /* sysfs name ---------------------------------------------------------- */
5552 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5553 struct device_attribute *attr,
5556 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5559 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5560 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5562 /* --------------------------------------------------------------------- */
5565 static struct proc_dir_entry *proc_dir;
5568 * Module and infrastructure proble, init and exit handling
5571 static int force_load;
5573 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5574 static const char * __init str_supported(int is_supported)
5576 static char text_unsupported[] __initdata = "not supported";
5578 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5580 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5582 static void ibm_exit(struct ibm_struct *ibm)
5584 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5586 list_del_init(&ibm->all_drivers);
5588 if (ibm->flags.acpi_notify_installed) {
5589 dbg_printk(TPACPI_DBG_EXIT,
5590 "%s: acpi_remove_notify_handler\n", ibm->name);
5592 acpi_remove_notify_handler(*ibm->acpi->handle,
5594 dispatch_acpi_notify);
5595 ibm->flags.acpi_notify_installed = 0;
5596 ibm->flags.acpi_notify_installed = 0;
5599 if (ibm->flags.proc_created) {
5600 dbg_printk(TPACPI_DBG_EXIT,
5601 "%s: remove_proc_entry\n", ibm->name);
5602 remove_proc_entry(ibm->name, proc_dir);
5603 ibm->flags.proc_created = 0;
5606 if (ibm->flags.acpi_driver_registered) {
5607 dbg_printk(TPACPI_DBG_EXIT,
5608 "%s: acpi_bus_unregister_driver\n", ibm->name);
5610 acpi_bus_unregister_driver(ibm->acpi->driver);
5611 kfree(ibm->acpi->driver);
5612 ibm->acpi->driver = NULL;
5613 ibm->flags.acpi_driver_registered = 0;
5616 if (ibm->flags.init_called && ibm->exit) {
5618 ibm->flags.init_called = 0;
5621 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5624 static int __init ibm_init(struct ibm_init_struct *iibm)
5627 struct ibm_struct *ibm = iibm->data;
5628 struct proc_dir_entry *entry;
5630 BUG_ON(ibm == NULL);
5632 INIT_LIST_HEAD(&ibm->all_drivers);
5634 if (ibm->flags.experimental && !experimental)
5637 dbg_printk(TPACPI_DBG_INIT,
5638 "probing for %s\n", ibm->name);
5641 ret = iibm->init(iibm);
5643 return 0; /* probe failed */
5647 ibm->flags.init_called = 1;
5651 if (ibm->acpi->hid) {
5652 ret = register_tpacpi_subdriver(ibm);
5657 if (ibm->acpi->notify) {
5658 ret = setup_acpi_notify(ibm);
5659 if (ret == -ENODEV) {
5660 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5670 dbg_printk(TPACPI_DBG_INIT,
5671 "%s installed\n", ibm->name);
5674 entry = create_proc_entry(ibm->name,
5675 S_IFREG | S_IRUGO | S_IWUSR,
5678 printk(TPACPI_ERR "unable to create proc entry %s\n",
5683 entry->owner = THIS_MODULE;
5685 entry->read_proc = &dispatch_procfs_read;
5687 entry->write_proc = &dispatch_procfs_write;
5688 ibm->flags.proc_created = 1;
5691 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5696 dbg_printk(TPACPI_DBG_INIT,
5697 "%s: at error exit path with result %d\n",
5701 return (ret < 0)? ret : 0;
5706 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5708 const struct dmi_device *dev = NULL;
5709 char ec_fw_string[18];
5714 memset(tp, 0, sizeof(*tp));
5716 if (dmi_name_in_vendors("IBM"))
5717 tp->vendor = PCI_VENDOR_ID_IBM;
5718 else if (dmi_name_in_vendors("LENOVO"))
5719 tp->vendor = PCI_VENDOR_ID_LENOVO;
5723 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5725 if (!tp->bios_version_str)
5727 tp->bios_model = tp->bios_version_str[0]
5728 | (tp->bios_version_str[1] << 8);
5731 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5732 * X32 or newer, all Z series; Some models must have an
5733 * up-to-date BIOS or they will not be detected.
5735 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5737 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5738 if (sscanf(dev->name,
5739 "IBM ThinkPad Embedded Controller -[%17c",
5740 ec_fw_string) == 1) {
5741 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5742 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5744 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5745 tp->ec_model = ec_fw_string[0]
5746 | (ec_fw_string[1] << 8);
5751 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5753 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5754 kfree(tp->model_str);
5755 tp->model_str = NULL;
5759 static int __init probe_for_thinkpad(void)
5767 * Non-ancient models have better DMI tagging, but very old models
5770 is_thinkpad = (thinkpad_id.model_str != NULL);
5772 /* ec is required because many other handles are relative to it */
5773 TPACPI_ACPIHANDLE_INIT(ec);
5777 "Not yet supported ThinkPad detected!\n");
5782 * Risks a regression on very old machines, but reduces potential
5783 * false positives a damn great deal
5786 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5788 if (!is_thinkpad && !force_load)
5795 /* Module init, exit, parameters */
5797 static struct ibm_init_struct ibms_init[] __initdata = {
5799 .init = thinkpad_acpi_driver_init,
5800 .data = &thinkpad_acpi_driver_data,
5803 .init = hotkey_init,
5804 .data = &hotkey_driver_data,
5807 .init = bluetooth_init,
5808 .data = &bluetooth_driver_data,
5812 .data = &wan_driver_data,
5814 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5817 .data = &video_driver_data,
5822 .data = &light_driver_data,
5824 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5827 .data = &dock_driver_data[0],
5831 .data = &dock_driver_data[1],
5834 #ifdef CONFIG_THINKPAD_ACPI_BAY
5837 .data = &bay_driver_data,
5842 .data = &cmos_driver_data,
5846 .data = &led_driver_data,
5850 .data = &beep_driver_data,
5853 .init = thermal_init,
5854 .data = &thermal_driver_data,
5857 .data = &ecdump_driver_data,
5860 .init = brightness_init,
5861 .data = &brightness_driver_data,
5864 .data = &volume_driver_data,
5868 .data = &fan_driver_data,
5872 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5875 struct ibm_struct *ibm;
5877 if (!kp || !kp->name || !val)
5880 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5881 ibm = ibms_init[i].data;
5882 WARN_ON(ibm == NULL);
5884 if (!ibm || !ibm->name)
5887 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5888 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5890 strcpy(ibms_init[i].param, val);
5891 strcat(ibms_init[i].param, ",");
5899 module_param(experimental, int, 0);
5900 MODULE_PARM_DESC(experimental,
5901 "Enables experimental features when non-zero");
5903 module_param_named(debug, dbg_level, uint, 0);
5904 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
5906 module_param(force_load, bool, 0);
5907 MODULE_PARM_DESC(force_load,
5908 "Attempts to load the driver even on a "
5909 "mis-identified ThinkPad when true");
5911 module_param_named(fan_control, fan_control_allowed, bool, 0);
5912 MODULE_PARM_DESC(fan_control,
5913 "Enables setting fan parameters features when true");
5915 module_param_named(brightness_mode, brightness_mode, int, 0);
5916 MODULE_PARM_DESC(brightness_mode,
5917 "Selects brightness control strategy: "
5918 "0=auto, 1=EC, 2=CMOS, 3=both");
5920 module_param(brightness_enable, uint, 0);
5921 MODULE_PARM_DESC(brightness_enable,
5922 "Enables backlight control when 1, disables when 0");
5924 module_param(hotkey_report_mode, uint, 0);
5925 MODULE_PARM_DESC(hotkey_report_mode,
5926 "used for backwards compatibility with userspace, "
5927 "see documentation");
5929 #define TPACPI_PARAM(feature) \
5930 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
5931 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
5932 "at module load, see documentation")
5934 TPACPI_PARAM(hotkey);
5935 TPACPI_PARAM(bluetooth);
5936 TPACPI_PARAM(video);
5937 TPACPI_PARAM(light);
5938 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5941 #ifdef CONFIG_THINKPAD_ACPI_BAY
5943 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5947 TPACPI_PARAM(ecdump);
5948 TPACPI_PARAM(brightness);
5949 TPACPI_PARAM(volume);
5952 static void thinkpad_acpi_module_exit(void)
5954 struct ibm_struct *ibm, *itmp;
5956 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5958 list_for_each_entry_safe_reverse(ibm, itmp,
5959 &tpacpi_all_drivers,
5964 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5966 if (tpacpi_inputdev) {
5967 if (tp_features.input_device_registered)
5968 input_unregister_device(tpacpi_inputdev);
5970 input_free_device(tpacpi_inputdev);
5974 hwmon_device_unregister(tpacpi_hwmon);
5976 if (tp_features.sensors_pdev_attrs_registered)
5977 device_remove_file(&tpacpi_sensors_pdev->dev,
5978 &dev_attr_thinkpad_acpi_pdev_name);
5979 if (tpacpi_sensors_pdev)
5980 platform_device_unregister(tpacpi_sensors_pdev);
5982 platform_device_unregister(tpacpi_pdev);
5984 if (tp_features.sensors_pdrv_attrs_registered)
5985 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5986 if (tp_features.platform_drv_attrs_registered)
5987 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5989 if (tp_features.sensors_pdrv_registered)
5990 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5992 if (tp_features.platform_drv_registered)
5993 platform_driver_unregister(&tpacpi_pdriver);
5996 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
5998 kfree(thinkpad_id.bios_version_str);
5999 kfree(thinkpad_id.ec_version_str);
6000 kfree(thinkpad_id.model_str);
6004 static int __init thinkpad_acpi_module_init(void)
6008 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6010 /* Parameter checking */
6011 if (hotkey_report_mode > 2)
6014 /* Driver-level probe */
6016 get_thinkpad_model_data(&thinkpad_id);
6017 ret = probe_for_thinkpad();
6019 thinkpad_acpi_module_exit();
6023 /* Driver initialization */
6025 TPACPI_ACPIHANDLE_INIT(ecrd);
6026 TPACPI_ACPIHANDLE_INIT(ecwr);
6028 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6031 "unable to create proc dir " TPACPI_PROC_DIR);
6032 thinkpad_acpi_module_exit();
6035 proc_dir->owner = THIS_MODULE;
6037 ret = platform_driver_register(&tpacpi_pdriver);
6040 "unable to register main platform driver\n");
6041 thinkpad_acpi_module_exit();
6044 tp_features.platform_drv_registered = 1;
6046 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6049 "unable to register hwmon platform driver\n");
6050 thinkpad_acpi_module_exit();
6053 tp_features.sensors_pdrv_registered = 1;
6055 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6057 tp_features.platform_drv_attrs_registered = 1;
6058 ret = tpacpi_create_driver_attributes(
6059 &tpacpi_hwmon_pdriver.driver);
6063 "unable to create sysfs driver attributes\n");
6064 thinkpad_acpi_module_exit();
6067 tp_features.sensors_pdrv_attrs_registered = 1;
6070 /* Device initialization */
6071 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6073 if (IS_ERR(tpacpi_pdev)) {
6074 ret = PTR_ERR(tpacpi_pdev);
6076 printk(TPACPI_ERR "unable to register platform device\n");
6077 thinkpad_acpi_module_exit();
6080 tpacpi_sensors_pdev = platform_device_register_simple(
6081 TPACPI_HWMON_DRVR_NAME,
6083 if (IS_ERR(tpacpi_sensors_pdev)) {
6084 ret = PTR_ERR(tpacpi_sensors_pdev);
6085 tpacpi_sensors_pdev = NULL;
6087 "unable to register hwmon platform device\n");
6088 thinkpad_acpi_module_exit();
6091 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6092 &dev_attr_thinkpad_acpi_pdev_name);
6095 "unable to create sysfs hwmon device attributes\n");
6096 thinkpad_acpi_module_exit();
6099 tp_features.sensors_pdev_attrs_registered = 1;
6100 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6101 if (IS_ERR(tpacpi_hwmon)) {
6102 ret = PTR_ERR(tpacpi_hwmon);
6103 tpacpi_hwmon = NULL;
6104 printk(TPACPI_ERR "unable to register hwmon device\n");
6105 thinkpad_acpi_module_exit();
6108 mutex_init(&tpacpi_inputdev_send_mutex);
6109 tpacpi_inputdev = input_allocate_device();
6110 if (!tpacpi_inputdev) {
6111 printk(TPACPI_ERR "unable to allocate input device\n");
6112 thinkpad_acpi_module_exit();
6115 /* Prepare input device, but don't register */
6116 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6117 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6118 tpacpi_inputdev->id.bustype = BUS_HOST;
6119 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6120 thinkpad_id.vendor :
6122 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6123 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6125 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6126 ret = ibm_init(&ibms_init[i]);
6127 if (ret >= 0 && *ibms_init[i].param)
6128 ret = ibms_init[i].data->write(ibms_init[i].param);
6130 thinkpad_acpi_module_exit();
6134 ret = input_register_device(tpacpi_inputdev);
6136 printk(TPACPI_ERR "unable to register input device\n");
6137 thinkpad_acpi_module_exit();
6140 tp_features.input_device_registered = 1;
6143 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6147 /* Please remove this in year 2009 */
6148 MODULE_ALIAS("ibm_acpi");
6151 * DMI matching for module autoloading
6153 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6154 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6156 * Only models listed in thinkwiki will be supported, so add yours
6157 * if it is not there yet.
6159 #define IBM_BIOS_MODULE_ALIAS(__type) \
6160 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6162 /* Non-ancient thinkpads */
6163 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6164 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6166 /* Ancient thinkpad BIOSes have to be identified by
6167 * BIOS type or model number, and there are far less
6168 * BIOS types than model numbers... */
6169 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6170 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6171 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6173 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6174 MODULE_DESCRIPTION(TPACPI_DESC);
6175 MODULE_VERSION(TPACPI_VERSION);
6176 MODULE_LICENSE("GPL");
6178 module_init(thinkpad_acpi_module_init);
6179 module_exit(thinkpad_acpi_module_exit);