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
227 u32 bright_16levels:1;
228 u32 bright_acpimode:1;
230 u32 fan_ctrl_status_undef:1;
231 u32 input_device_registered:1;
232 u32 platform_drv_registered:1;
233 u32 platform_drv_attrs_registered:1;
234 u32 sensors_pdrv_registered:1;
235 u32 sensors_pdrv_attrs_registered:1;
236 u32 sensors_pdev_attrs_registered:1;
237 u32 hotkey_poll_active:1;
241 u16 hotkey_mask_ff:1;
242 u16 bright_cmos_ec_unsync:1;
245 struct thinkpad_id_data {
246 unsigned int vendor; /* ThinkPad vendor:
247 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
249 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
250 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
252 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
255 char *model_str; /* ThinkPad T43 */
256 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
258 static struct thinkpad_id_data thinkpad_id;
261 TPACPI_LIFE_INIT = 0,
266 static int experimental;
267 static u32 dbg_level;
269 /****************************************************************************
270 ****************************************************************************
272 * ACPI Helpers and device model
274 ****************************************************************************
275 ****************************************************************************/
277 /*************************************************************************
281 static acpi_handle root_handle;
283 #define TPACPI_HANDLE(object, parent, paths...) \
284 static acpi_handle object##_handle; \
285 static acpi_handle *object##_parent = &parent##_handle; \
286 static char *object##_path; \
287 static char *object##_paths[] = { paths }
289 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
290 "\\_SB.PCI.ISA.EC", /* 570 */
291 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
292 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
293 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
294 "\\_SB.PCI0.ICH3.EC0", /* R31 */
295 "\\_SB.PCI0.LPC.EC", /* all others */
298 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
299 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
301 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
303 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
304 "\\CMS", /* R40, R40e */
307 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
308 "^HKEY", /* R30, R31 */
309 "HKEY", /* all others */
312 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
313 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
314 "\\_SB.PCI0.VID0", /* 770e */
315 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
316 "\\_SB.PCI0.AGP.VID", /* all others */
320 /*************************************************************************
324 static int acpi_evalf(acpi_handle handle,
325 void *res, char *method, char *fmt, ...)
328 struct acpi_object_list params;
329 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
330 struct acpi_buffer result, *resultp;
331 union acpi_object out_obj;
339 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
352 params.pointer = &in_objs[0];
359 in_objs[params.count].integer.value = va_arg(ap, int);
360 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
362 /* add more types as needed */
364 printk(TPACPI_ERR "acpi_evalf() called "
365 "with invalid format character '%c'\n", c);
371 if (res_type != 'v') {
372 result.length = sizeof(out_obj);
373 result.pointer = &out_obj;
378 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
383 *(int *)res = out_obj.integer.value;
384 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
387 success = status == AE_OK;
389 /* add more types as needed */
391 printk(TPACPI_ERR "acpi_evalf() called "
392 "with invalid format character '%c'\n", res_type);
396 if (!success && !quiet)
397 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
398 method, fmt0, status);
403 static int acpi_ec_read(int i, u8 *p)
408 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
412 if (ec_read(i, p) < 0)
419 static int acpi_ec_write(int i, u8 v)
422 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
425 if (ec_write(i, v) < 0)
432 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
433 static int _sta(acpi_handle handle)
437 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
444 static int issue_thinkpad_cmos_command(int cmos_cmd)
449 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
455 /*************************************************************************
459 #define TPACPI_ACPIHANDLE_INIT(object) \
460 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
461 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
463 static void drv_acpi_handle_init(char *name,
464 acpi_handle *handle, acpi_handle parent,
465 char **paths, int num_paths, char **path)
470 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
473 for (i = 0; i < num_paths; i++) {
474 status = acpi_get_handle(parent, paths[i], handle);
475 if (ACPI_SUCCESS(status)) {
477 dbg_printk(TPACPI_DBG_INIT,
478 "Found ACPI handle %s for %s\n",
484 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
489 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
491 struct ibm_struct *ibm = data;
493 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
496 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
499 ibm->acpi->notify(ibm, event);
502 static int __init setup_acpi_notify(struct ibm_struct *ibm)
509 if (!*ibm->acpi->handle)
512 vdbg_printk(TPACPI_DBG_INIT,
513 "setting up ACPI notify for %s\n", ibm->name);
515 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
517 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
522 acpi_driver_data(ibm->acpi->device) = ibm;
523 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
524 TPACPI_ACPI_EVENT_PREFIX,
527 status = acpi_install_notify_handler(*ibm->acpi->handle,
528 ibm->acpi->type, dispatch_acpi_notify, ibm);
529 if (ACPI_FAILURE(status)) {
530 if (status == AE_ALREADY_EXISTS) {
532 "another device driver is already "
533 "handling %s events\n", ibm->name);
536 "acpi_install_notify_handler(%s) failed: %d\n",
541 ibm->flags.acpi_notify_installed = 1;
545 static int __init tpacpi_device_add(struct acpi_device *device)
550 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
554 dbg_printk(TPACPI_DBG_INIT,
555 "registering %s as an ACPI driver\n", ibm->name);
559 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
560 if (!ibm->acpi->driver) {
561 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
565 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
566 ibm->acpi->driver->ids = ibm->acpi->hid;
568 ibm->acpi->driver->ops.add = &tpacpi_device_add;
570 rc = acpi_bus_register_driver(ibm->acpi->driver);
572 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
574 kfree(ibm->acpi->driver);
575 ibm->acpi->driver = NULL;
577 ibm->flags.acpi_driver_registered = 1;
583 /****************************************************************************
584 ****************************************************************************
588 ****************************************************************************
589 ****************************************************************************/
591 static int dispatch_procfs_read(char *page, char **start, off_t off,
592 int count, int *eof, void *data)
594 struct ibm_struct *ibm = data;
597 if (!ibm || !ibm->read)
600 len = ibm->read(page);
604 if (len <= off + count)
616 static int dispatch_procfs_write(struct file *file,
617 const char __user *userbuf,
618 unsigned long count, void *data)
620 struct ibm_struct *ibm = data;
624 if (!ibm || !ibm->write)
627 kernbuf = kmalloc(count + 2, GFP_KERNEL);
631 if (copy_from_user(kernbuf, userbuf, count)) {
637 strcat(kernbuf, ",");
638 ret = ibm->write(kernbuf);
647 static char *next_cmd(char **cmds)
652 while ((end = strchr(start, ',')) && end == start)
664 /****************************************************************************
665 ****************************************************************************
667 * Device model: input, hwmon and platform
669 ****************************************************************************
670 ****************************************************************************/
672 static struct platform_device *tpacpi_pdev;
673 static struct platform_device *tpacpi_sensors_pdev;
674 static struct device *tpacpi_hwmon;
675 static struct input_dev *tpacpi_inputdev;
676 static struct mutex tpacpi_inputdev_send_mutex;
677 static LIST_HEAD(tpacpi_all_drivers);
679 static int tpacpi_suspend_handler(struct platform_device *pdev,
682 struct ibm_struct *ibm, *itmp;
684 list_for_each_entry_safe(ibm, itmp,
688 (ibm->suspend)(state);
694 static int tpacpi_resume_handler(struct platform_device *pdev)
696 struct ibm_struct *ibm, *itmp;
698 list_for_each_entry_safe(ibm, itmp,
708 static struct platform_driver tpacpi_pdriver = {
710 .name = TPACPI_DRVR_NAME,
711 .owner = THIS_MODULE,
713 .suspend = tpacpi_suspend_handler,
714 .resume = tpacpi_resume_handler,
717 static struct platform_driver tpacpi_hwmon_pdriver = {
719 .name = TPACPI_HWMON_DRVR_NAME,
720 .owner = THIS_MODULE,
724 /*************************************************************************
725 * sysfs support helpers
728 struct attribute_set {
729 unsigned int members, max_members;
730 struct attribute_group group;
733 struct attribute_set_obj {
734 struct attribute_set s;
736 } __attribute__((packed));
738 static struct attribute_set *create_attr_set(unsigned int max_members,
741 struct attribute_set_obj *sobj;
743 if (max_members == 0)
746 /* Allocates space for implicit NULL at the end too */
747 sobj = kzalloc(sizeof(struct attribute_set_obj) +
748 max_members * sizeof(struct attribute *),
752 sobj->s.max_members = max_members;
753 sobj->s.group.attrs = &sobj->a;
754 sobj->s.group.name = name;
759 #define destroy_attr_set(_set) \
762 /* not multi-threaded safe, use it in a single thread per set */
763 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
768 if (s->members >= s->max_members)
771 s->group.attrs[s->members] = attr;
777 static int add_many_to_attr_set(struct attribute_set *s,
778 struct attribute **attr,
783 for (i = 0; i < count; i++) {
784 res = add_to_attr_set(s, attr[i]);
792 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
794 sysfs_remove_group(kobj, &s->group);
798 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
799 sysfs_create_group(_kobj, &_attr_set->group)
801 static int parse_strtoul(const char *buf,
802 unsigned long max, unsigned long *value)
806 while (*buf && isspace(*buf))
808 *value = simple_strtoul(buf, &endp, 0);
809 while (*endp && isspace(*endp))
811 if (*endp || *value > max)
817 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
819 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
820 union acpi_object *obj;
823 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
824 obj = (union acpi_object *)buffer.pointer;
825 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
826 printk(TPACPI_ERR "Unknown _BCL data, "
827 "please report this to %s\n", TPACPI_MAIL);
830 rc = obj->package.count;
836 kfree(buffer.pointer);
840 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
841 u32 lvl, void *context, void **rv)
843 char name[ACPI_PATH_SEGMENT_LENGTH];
844 struct acpi_buffer buffer = { sizeof(name), &name };
846 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
847 !strncmp("_BCL", name, sizeof(name) - 1)) {
849 **(int **)rv = tpacpi_query_bcl_levels(handle);
850 return AE_CTRL_TERMINATE;
857 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
859 static int __init tpacpi_check_std_acpi_brightness_support(void)
863 void *bcl_ptr = &bcl_levels;
866 TPACPI_ACPIHANDLE_INIT(vid);
872 * Search for a _BCL method, and execute it. This is safe on all
873 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
874 * BIOS in ACPI backlight control mode. We do NOT have to care
875 * about calling the _BCL method in an enabled video device, any
876 * will do for our purposes.
879 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
880 tpacpi_acpi_walk_find_bcl, NULL,
883 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
884 tp_features.bright_acpimode = 1;
885 return (bcl_levels - 2);
891 /*************************************************************************
892 * thinkpad-acpi driver attributes
895 /* interface_version --------------------------------------------------- */
896 static ssize_t tpacpi_driver_interface_version_show(
897 struct device_driver *drv,
900 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
903 static DRIVER_ATTR(interface_version, S_IRUGO,
904 tpacpi_driver_interface_version_show, NULL);
906 /* debug_level --------------------------------------------------------- */
907 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
910 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
913 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
914 const char *buf, size_t count)
918 if (parse_strtoul(buf, 0xffff, &t))
926 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
927 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
929 /* version ------------------------------------------------------------- */
930 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
933 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
934 TPACPI_DESC, TPACPI_VERSION);
937 static DRIVER_ATTR(version, S_IRUGO,
938 tpacpi_driver_version_show, NULL);
940 /* --------------------------------------------------------------------- */
942 static struct driver_attribute *tpacpi_driver_attributes[] = {
943 &driver_attr_debug_level, &driver_attr_version,
944 &driver_attr_interface_version,
947 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
953 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
954 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
961 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
965 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
966 driver_remove_file(drv, tpacpi_driver_attributes[i]);
969 /****************************************************************************
970 ****************************************************************************
974 ****************************************************************************
975 ****************************************************************************/
977 /*************************************************************************
978 * thinkpad-acpi init subdriver
981 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
983 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
984 printk(TPACPI_INFO "%s\n", TPACPI_URL);
986 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
987 (thinkpad_id.bios_version_str) ?
988 thinkpad_id.bios_version_str : "unknown",
989 (thinkpad_id.ec_version_str) ?
990 thinkpad_id.ec_version_str : "unknown");
992 if (thinkpad_id.vendor && thinkpad_id.model_str)
993 printk(TPACPI_INFO "%s %s, model %s\n",
994 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
995 "IBM" : ((thinkpad_id.vendor ==
996 PCI_VENDOR_ID_LENOVO) ?
997 "Lenovo" : "Unknown vendor"),
998 thinkpad_id.model_str,
999 (thinkpad_id.nummodel_str) ?
1000 thinkpad_id.nummodel_str : "unknown");
1005 static int thinkpad_acpi_driver_read(char *p)
1009 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1010 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1015 static struct ibm_struct thinkpad_acpi_driver_data = {
1017 .read = thinkpad_acpi_driver_read,
1020 /*************************************************************************
1024 enum { /* hot key scan codes (derived from ACPI DSDT) */
1025 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1026 TP_ACPI_HOTKEYSCAN_FNF2,
1027 TP_ACPI_HOTKEYSCAN_FNF3,
1028 TP_ACPI_HOTKEYSCAN_FNF4,
1029 TP_ACPI_HOTKEYSCAN_FNF5,
1030 TP_ACPI_HOTKEYSCAN_FNF6,
1031 TP_ACPI_HOTKEYSCAN_FNF7,
1032 TP_ACPI_HOTKEYSCAN_FNF8,
1033 TP_ACPI_HOTKEYSCAN_FNF9,
1034 TP_ACPI_HOTKEYSCAN_FNF10,
1035 TP_ACPI_HOTKEYSCAN_FNF11,
1036 TP_ACPI_HOTKEYSCAN_FNF12,
1037 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1038 TP_ACPI_HOTKEYSCAN_FNINSERT,
1039 TP_ACPI_HOTKEYSCAN_FNDELETE,
1040 TP_ACPI_HOTKEYSCAN_FNHOME,
1041 TP_ACPI_HOTKEYSCAN_FNEND,
1042 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1043 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1044 TP_ACPI_HOTKEYSCAN_FNSPACE,
1045 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1046 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1047 TP_ACPI_HOTKEYSCAN_MUTE,
1048 TP_ACPI_HOTKEYSCAN_THINKPAD,
1051 enum { /* Keys available through NVRAM polling */
1052 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1053 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1056 enum { /* Positions of some of the keys in hotkey masks */
1057 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1058 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1059 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1060 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1061 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1062 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1063 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1064 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1065 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1066 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1067 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1070 enum { /* NVRAM to ACPI HKEY group map */
1071 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1072 TP_ACPI_HKEY_ZOOM_MASK |
1073 TP_ACPI_HKEY_DISPSWTCH_MASK |
1074 TP_ACPI_HKEY_HIBERNATE_MASK,
1075 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1076 TP_ACPI_HKEY_BRGHTDWN_MASK,
1077 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1078 TP_ACPI_HKEY_VOLDWN_MASK |
1079 TP_ACPI_HKEY_MUTE_MASK,
1082 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1083 struct tp_nvram_state {
1084 u16 thinkpad_toggle:1;
1086 u16 display_toggle:1;
1087 u16 thinklight_toggle:1;
1088 u16 hibernate_toggle:1;
1089 u16 displayexp_toggle:1;
1090 u16 display_state:1;
1091 u16 brightness_toggle:1;
1092 u16 volume_toggle:1;
1095 u8 brightness_level;
1099 static struct task_struct *tpacpi_hotkey_task;
1100 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1101 static int hotkey_poll_freq = 10; /* Hz */
1102 static struct mutex hotkey_thread_mutex;
1103 static struct mutex hotkey_thread_data_mutex;
1104 static unsigned int hotkey_config_change;
1106 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1108 #define hotkey_source_mask 0U
1110 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1112 static struct mutex hotkey_mutex;
1114 static enum { /* Reasons for waking up */
1115 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1116 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1117 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1118 } hotkey_wakeup_reason;
1120 static int hotkey_autosleep_ack;
1122 static int hotkey_orig_status;
1123 static u32 hotkey_orig_mask;
1124 static u32 hotkey_all_mask;
1125 static u32 hotkey_reserved_mask;
1126 static u32 hotkey_mask;
1128 static unsigned int hotkey_report_mode;
1130 static u16 *hotkey_keycode_map;
1132 static struct attribute_set *hotkey_dev_attributes;
1134 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1135 #define HOTKEY_CONFIG_CRITICAL_START \
1137 mutex_lock(&hotkey_thread_data_mutex); \
1138 hotkey_config_change++; \
1140 #define HOTKEY_CONFIG_CRITICAL_END \
1141 mutex_unlock(&hotkey_thread_data_mutex);
1143 #define HOTKEY_CONFIG_CRITICAL_START
1144 #define HOTKEY_CONFIG_CRITICAL_END
1145 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1147 /* HKEY.MHKG() return bits */
1148 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1150 static int hotkey_get_wlsw(int *status)
1152 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1157 static int hotkey_get_tablet_mode(int *status)
1161 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1164 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1169 * Call with hotkey_mutex held
1171 static int hotkey_mask_get(void)
1175 if (tp_features.hotkey_mask) {
1176 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1179 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1185 * Call with hotkey_mutex held
1187 static int hotkey_mask_set(u32 mask)
1192 if (tp_features.hotkey_mask) {
1193 if (!tp_warned.hotkey_mask_ff &&
1194 (mask == 0xffff || mask == 0xffffff ||
1195 mask == 0xffffffff)) {
1196 tp_warned.hotkey_mask_ff = 1;
1197 printk(TPACPI_NOTICE
1198 "setting the hotkey mask to 0x%08x is likely "
1199 "not the best way to go about it\n", mask);
1200 printk(TPACPI_NOTICE
1201 "please consider using the driver defaults, "
1202 "and refer to up-to-date thinkpad-acpi "
1206 HOTKEY_CONFIG_CRITICAL_START
1207 for (i = 0; i < 32; i++) {
1209 /* enable in firmware mask only keys not in NVRAM
1210 * mode, but enable the key in the cached hotkey_mask
1211 * regardless of mode, or the key will end up
1212 * disabled by hotkey_mask_get() */
1213 if (!acpi_evalf(hkey_handle,
1214 NULL, "MHKM", "vdd", i + 1,
1215 !!((mask & ~hotkey_source_mask) & m))) {
1219 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1222 HOTKEY_CONFIG_CRITICAL_END
1224 /* hotkey_mask_get must be called unconditionally below */
1225 if (!hotkey_mask_get() && !rc &&
1226 (hotkey_mask & ~hotkey_source_mask) !=
1227 (mask & ~hotkey_source_mask)) {
1228 printk(TPACPI_NOTICE
1229 "requested hot key mask 0x%08x, but "
1230 "firmware forced it to 0x%08x\n",
1234 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1235 HOTKEY_CONFIG_CRITICAL_START
1236 hotkey_mask = mask & hotkey_source_mask;
1237 HOTKEY_CONFIG_CRITICAL_END
1239 if (hotkey_mask != mask) {
1240 printk(TPACPI_NOTICE
1241 "requested hot key mask 0x%08x, "
1242 "forced to 0x%08x (NVRAM poll mask is "
1243 "0x%08x): no firmware mask support\n",
1244 mask, hotkey_mask, hotkey_source_mask);
1249 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1255 static int hotkey_status_get(int *status)
1257 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1263 static int hotkey_status_set(int status)
1265 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1271 static void tpacpi_input_send_radiosw(void)
1275 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1276 mutex_lock(&tpacpi_inputdev_send_mutex);
1278 input_report_switch(tpacpi_inputdev,
1280 input_sync(tpacpi_inputdev);
1282 mutex_unlock(&tpacpi_inputdev_send_mutex);
1286 static void tpacpi_input_send_tabletsw(void)
1290 if (tp_features.hotkey_tablet &&
1291 !hotkey_get_tablet_mode(&state)) {
1292 mutex_lock(&tpacpi_inputdev_send_mutex);
1294 input_report_switch(tpacpi_inputdev,
1295 SW_TABLET_MODE, !!state);
1296 input_sync(tpacpi_inputdev);
1298 mutex_unlock(&tpacpi_inputdev_send_mutex);
1302 static void tpacpi_input_send_key(unsigned int scancode)
1304 unsigned int keycode;
1306 keycode = hotkey_keycode_map[scancode];
1308 if (keycode != KEY_RESERVED) {
1309 mutex_lock(&tpacpi_inputdev_send_mutex);
1311 input_report_key(tpacpi_inputdev, keycode, 1);
1312 if (keycode == KEY_UNKNOWN)
1313 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1315 input_sync(tpacpi_inputdev);
1317 input_report_key(tpacpi_inputdev, keycode, 0);
1318 if (keycode == KEY_UNKNOWN)
1319 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1321 input_sync(tpacpi_inputdev);
1323 mutex_unlock(&tpacpi_inputdev_send_mutex);
1327 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1328 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1330 static void tpacpi_hotkey_send_key(unsigned int scancode)
1332 tpacpi_input_send_key(scancode);
1333 if (hotkey_report_mode < 2) {
1334 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1335 0x80, 0x1001 + scancode);
1339 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1343 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1344 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1345 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1346 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1347 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1348 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1350 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1351 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1352 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1354 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1355 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1356 n->displayexp_toggle =
1357 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1359 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1360 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1361 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1362 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1363 n->brightness_toggle =
1364 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1366 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1367 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1368 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1369 >> TP_NVRAM_POS_LEVEL_VOLUME;
1370 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1371 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1375 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1377 if ((mask & (1 << __scancode)) && \
1378 oldn->__member != newn->__member) \
1379 tpacpi_hotkey_send_key(__scancode); \
1382 #define TPACPI_MAY_SEND_KEY(__scancode) \
1383 do { if (mask & (1 << __scancode)) \
1384 tpacpi_hotkey_send_key(__scancode); } while (0)
1386 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1387 struct tp_nvram_state *newn,
1390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1400 if (oldn->volume_toggle != newn->volume_toggle) {
1401 if (oldn->mute != newn->mute) {
1402 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1404 if (oldn->volume_level > newn->volume_level) {
1405 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1406 } else if (oldn->volume_level < newn->volume_level) {
1407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1408 } else if (oldn->mute == newn->mute) {
1409 /* repeated key presses that didn't change state */
1411 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1412 } else if (newn->volume_level != 0) {
1413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1420 /* handle brightness */
1421 if (oldn->brightness_toggle != newn->brightness_toggle) {
1422 if (oldn->brightness_level < newn->brightness_level) {
1423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1424 } else if (oldn->brightness_level > newn->brightness_level) {
1425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1427 /* repeated key presses that didn't change state */
1428 if (newn->brightness_level != 0) {
1429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1437 #undef TPACPI_COMPARE_KEY
1438 #undef TPACPI_MAY_SEND_KEY
1440 static int hotkey_kthread(void *data)
1442 struct tp_nvram_state s[2];
1444 unsigned int si, so;
1446 unsigned int change_detector, must_reset;
1448 mutex_lock(&hotkey_thread_mutex);
1450 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1459 /* Initial state for compares */
1460 mutex_lock(&hotkey_thread_data_mutex);
1461 change_detector = hotkey_config_change;
1462 mask = hotkey_source_mask & hotkey_mask;
1463 mutex_unlock(&hotkey_thread_data_mutex);
1464 hotkey_read_nvram(&s[so], mask);
1466 while (!kthread_should_stop() && hotkey_poll_freq) {
1468 t = 1000/hotkey_poll_freq;
1469 t = msleep_interruptible(t);
1470 if (unlikely(kthread_should_stop()))
1472 must_reset = try_to_freeze();
1473 if (t > 0 && !must_reset)
1476 mutex_lock(&hotkey_thread_data_mutex);
1477 if (must_reset || hotkey_config_change != change_detector) {
1478 /* forget old state on thaw or config change */
1481 change_detector = hotkey_config_change;
1483 mask = hotkey_source_mask & hotkey_mask;
1484 mutex_unlock(&hotkey_thread_data_mutex);
1487 hotkey_read_nvram(&s[si], mask);
1488 if (likely(si != so)) {
1489 hotkey_compare_and_issue_event(&s[so], &s[si],
1499 mutex_unlock(&hotkey_thread_mutex);
1503 static void hotkey_poll_stop_sync(void)
1505 if (tpacpi_hotkey_task) {
1506 if (frozen(tpacpi_hotkey_task) ||
1507 freezing(tpacpi_hotkey_task))
1508 thaw_process(tpacpi_hotkey_task);
1510 kthread_stop(tpacpi_hotkey_task);
1511 tpacpi_hotkey_task = NULL;
1512 mutex_lock(&hotkey_thread_mutex);
1513 /* at this point, the thread did exit */
1514 mutex_unlock(&hotkey_thread_mutex);
1518 /* call with hotkey_mutex held */
1519 static void hotkey_poll_setup(int may_warn)
1521 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1522 hotkey_poll_freq > 0 &&
1523 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1524 if (!tpacpi_hotkey_task) {
1525 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1528 if (IS_ERR(tpacpi_hotkey_task)) {
1529 tpacpi_hotkey_task = NULL;
1531 "could not create kernel thread "
1532 "for hotkey polling\n");
1536 hotkey_poll_stop_sync();
1538 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1539 printk(TPACPI_NOTICE
1540 "hot keys 0x%08x require polling, "
1541 "which is currently disabled\n",
1542 hotkey_source_mask);
1547 static void hotkey_poll_setup_safe(int may_warn)
1549 mutex_lock(&hotkey_mutex);
1550 hotkey_poll_setup(may_warn);
1551 mutex_unlock(&hotkey_mutex);
1554 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1556 static void hotkey_poll_setup_safe(int __unused)
1560 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1562 static int hotkey_inputdev_open(struct input_dev *dev)
1564 switch (tpacpi_lifecycle) {
1565 case TPACPI_LIFE_INIT:
1567 * hotkey_init will call hotkey_poll_setup_safe
1568 * at the appropriate moment
1571 case TPACPI_LIFE_EXITING:
1573 case TPACPI_LIFE_RUNNING:
1574 hotkey_poll_setup_safe(0);
1578 /* Should only happen if tpacpi_lifecycle is corrupt */
1583 static void hotkey_inputdev_close(struct input_dev *dev)
1585 /* disable hotkey polling when possible */
1586 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1587 hotkey_poll_setup_safe(0);
1590 /* sysfs hotkey enable ------------------------------------------------- */
1591 static ssize_t hotkey_enable_show(struct device *dev,
1592 struct device_attribute *attr,
1597 res = hotkey_status_get(&status);
1601 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1604 static ssize_t hotkey_enable_store(struct device *dev,
1605 struct device_attribute *attr,
1606 const char *buf, size_t count)
1611 if (parse_strtoul(buf, 1, &t))
1614 res = hotkey_status_set(t);
1616 return (res) ? res : count;
1619 static struct device_attribute dev_attr_hotkey_enable =
1620 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1621 hotkey_enable_show, hotkey_enable_store);
1623 /* sysfs hotkey mask --------------------------------------------------- */
1624 static ssize_t hotkey_mask_show(struct device *dev,
1625 struct device_attribute *attr,
1630 if (mutex_lock_interruptible(&hotkey_mutex))
1631 return -ERESTARTSYS;
1632 res = hotkey_mask_get();
1633 mutex_unlock(&hotkey_mutex);
1636 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1639 static ssize_t hotkey_mask_store(struct device *dev,
1640 struct device_attribute *attr,
1641 const char *buf, size_t count)
1646 if (parse_strtoul(buf, 0xffffffffUL, &t))
1649 if (mutex_lock_interruptible(&hotkey_mutex))
1650 return -ERESTARTSYS;
1652 res = hotkey_mask_set(t);
1654 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1655 hotkey_poll_setup(1);
1658 mutex_unlock(&hotkey_mutex);
1660 return (res) ? res : count;
1663 static struct device_attribute dev_attr_hotkey_mask =
1664 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1665 hotkey_mask_show, hotkey_mask_store);
1667 /* sysfs hotkey bios_enabled ------------------------------------------- */
1668 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1669 struct device_attribute *attr,
1672 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1675 static struct device_attribute dev_attr_hotkey_bios_enabled =
1676 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1678 /* sysfs hotkey bios_mask ---------------------------------------------- */
1679 static ssize_t hotkey_bios_mask_show(struct device *dev,
1680 struct device_attribute *attr,
1683 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1686 static struct device_attribute dev_attr_hotkey_bios_mask =
1687 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1689 /* sysfs hotkey all_mask ----------------------------------------------- */
1690 static ssize_t hotkey_all_mask_show(struct device *dev,
1691 struct device_attribute *attr,
1694 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1695 hotkey_all_mask | hotkey_source_mask);
1698 static struct device_attribute dev_attr_hotkey_all_mask =
1699 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1701 /* sysfs hotkey recommended_mask --------------------------------------- */
1702 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1703 struct device_attribute *attr,
1706 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1707 (hotkey_all_mask | hotkey_source_mask)
1708 & ~hotkey_reserved_mask);
1711 static struct device_attribute dev_attr_hotkey_recommended_mask =
1712 __ATTR(hotkey_recommended_mask, S_IRUGO,
1713 hotkey_recommended_mask_show, NULL);
1715 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1717 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1718 static ssize_t hotkey_source_mask_show(struct device *dev,
1719 struct device_attribute *attr,
1722 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1725 static ssize_t hotkey_source_mask_store(struct device *dev,
1726 struct device_attribute *attr,
1727 const char *buf, size_t count)
1731 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1732 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1735 if (mutex_lock_interruptible(&hotkey_mutex))
1736 return -ERESTARTSYS;
1738 HOTKEY_CONFIG_CRITICAL_START
1739 hotkey_source_mask = t;
1740 HOTKEY_CONFIG_CRITICAL_END
1742 hotkey_poll_setup(1);
1744 mutex_unlock(&hotkey_mutex);
1749 static struct device_attribute dev_attr_hotkey_source_mask =
1750 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1751 hotkey_source_mask_show, hotkey_source_mask_store);
1753 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1754 static ssize_t hotkey_poll_freq_show(struct device *dev,
1755 struct device_attribute *attr,
1758 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1761 static ssize_t hotkey_poll_freq_store(struct device *dev,
1762 struct device_attribute *attr,
1763 const char *buf, size_t count)
1767 if (parse_strtoul(buf, 25, &t))
1770 if (mutex_lock_interruptible(&hotkey_mutex))
1771 return -ERESTARTSYS;
1773 hotkey_poll_freq = t;
1775 hotkey_poll_setup(1);
1776 mutex_unlock(&hotkey_mutex);
1781 static struct device_attribute dev_attr_hotkey_poll_freq =
1782 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1783 hotkey_poll_freq_show, hotkey_poll_freq_store);
1785 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1787 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1788 static ssize_t hotkey_radio_sw_show(struct device *dev,
1789 struct device_attribute *attr,
1793 res = hotkey_get_wlsw(&s);
1797 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1800 static struct device_attribute dev_attr_hotkey_radio_sw =
1801 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1803 static void hotkey_radio_sw_notify_change(void)
1805 if (tp_features.hotkey_wlsw)
1806 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1810 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1811 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1812 struct device_attribute *attr,
1816 res = hotkey_get_tablet_mode(&s);
1820 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1823 static struct device_attribute dev_attr_hotkey_tablet_mode =
1824 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1826 static void hotkey_tablet_mode_notify_change(void)
1828 if (tp_features.hotkey_tablet)
1829 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1830 "hotkey_tablet_mode");
1833 /* sysfs hotkey report_mode -------------------------------------------- */
1834 static ssize_t hotkey_report_mode_show(struct device *dev,
1835 struct device_attribute *attr,
1838 return snprintf(buf, PAGE_SIZE, "%d\n",
1839 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1842 static struct device_attribute dev_attr_hotkey_report_mode =
1843 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1845 /* sysfs wakeup reason (pollable) -------------------------------------- */
1846 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1847 struct device_attribute *attr,
1850 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1853 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1854 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1856 static void hotkey_wakeup_reason_notify_change(void)
1858 if (tp_features.hotkey_mask)
1859 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1863 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1864 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1865 struct device_attribute *attr,
1868 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1871 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1872 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1873 hotkey_wakeup_hotunplug_complete_show, NULL);
1875 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1877 if (tp_features.hotkey_mask)
1878 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1879 "wakeup_hotunplug_complete");
1882 /* --------------------------------------------------------------------- */
1884 static struct attribute *hotkey_attributes[] __initdata = {
1885 &dev_attr_hotkey_enable.attr,
1886 &dev_attr_hotkey_bios_enabled.attr,
1887 &dev_attr_hotkey_report_mode.attr,
1888 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1889 &dev_attr_hotkey_mask.attr,
1890 &dev_attr_hotkey_all_mask.attr,
1891 &dev_attr_hotkey_recommended_mask.attr,
1892 &dev_attr_hotkey_source_mask.attr,
1893 &dev_attr_hotkey_poll_freq.attr,
1897 static struct attribute *hotkey_mask_attributes[] __initdata = {
1898 &dev_attr_hotkey_bios_mask.attr,
1899 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1900 &dev_attr_hotkey_mask.attr,
1901 &dev_attr_hotkey_all_mask.attr,
1902 &dev_attr_hotkey_recommended_mask.attr,
1904 &dev_attr_hotkey_wakeup_reason.attr,
1905 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1908 static int __init hotkey_init(struct ibm_init_struct *iibm)
1910 /* Requirements for changing the default keymaps:
1912 * 1. Many of the keys are mapped to KEY_RESERVED for very
1913 * good reasons. Do not change them unless you have deep
1914 * knowledge on the IBM and Lenovo ThinkPad firmware for
1915 * the various ThinkPad models. The driver behaves
1916 * differently for KEY_RESERVED: such keys have their
1917 * hot key mask *unset* in mask_recommended, and also
1918 * in the initial hot key mask programmed into the
1919 * firmware at driver load time, which means the firm-
1920 * ware may react very differently if you change them to
1923 * 2. You must be subscribed to the linux-thinkpad and
1924 * ibm-acpi-devel mailing lists, and you should read the
1925 * list archives since 2007 if you want to change the
1926 * keymaps. This requirement exists so that you will
1927 * know the past history of problems with the thinkpad-
1928 * acpi driver keymaps, and also that you will be
1929 * listening to any bug reports;
1931 * 3. Do not send thinkpad-acpi specific patches directly to
1932 * for merging, *ever*. Send them to the linux-acpi
1933 * mailinglist for comments. Merging is to be done only
1934 * through acpi-test and the ACPI maintainer.
1936 * If the above is too much to ask, don't change the keymap.
1937 * Ask the thinkpad-acpi maintainer to do it, instead.
1939 static u16 ibm_keycode_map[] __initdata = {
1940 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1941 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1942 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1943 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1945 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1946 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1947 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1948 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1950 /* brightness: firmware always reacts to them, unless
1951 * X.org did some tricks in the radeon BIOS scratch
1952 * registers of *some* models */
1953 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1954 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1956 /* Thinklight: firmware always react to it */
1957 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1959 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1960 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1962 /* Volume: firmware always react to it and reprograms
1963 * the built-in *extra* mixer. Never map it to control
1964 * another mixer by default. */
1965 KEY_RESERVED, /* 0x14: VOLUME UP */
1966 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1967 KEY_RESERVED, /* 0x16: MUTE */
1969 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1971 /* (assignments unknown, please report if found) */
1972 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1973 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1975 static u16 lenovo_keycode_map[] __initdata = {
1976 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1977 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1978 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1979 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1981 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1982 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1983 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1984 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1986 /* These either have to go through ACPI video, or
1987 * act like in the IBM ThinkPads, so don't ever
1988 * enable them by default */
1989 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1990 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1992 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1994 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1995 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1997 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1998 * react to it and reprograms the built-in *extra* mixer.
1999 * Never map it to control another mixer by default.
2001 * T60?, T61, R60?, R61: firmware and EC tries to send
2002 * these over the regular keyboard, so these are no-ops,
2003 * but there are still weird bugs re. MUTE, so do not
2004 * change unless you get test reports from all Lenovo
2005 * models. May cause the BIOS to interfere with the
2008 KEY_RESERVED, /* 0x14: VOLUME UP */
2009 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2010 KEY_RESERVED, /* 0x16: MUTE */
2012 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2014 /* (assignments unknown, please report if found) */
2015 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2016 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2019 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2020 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2021 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2027 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2029 BUG_ON(!tpacpi_inputdev);
2030 BUG_ON(tpacpi_inputdev->open != NULL ||
2031 tpacpi_inputdev->close != NULL);
2033 TPACPI_ACPIHANDLE_INIT(hkey);
2034 mutex_init(&hotkey_mutex);
2036 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2037 mutex_init(&hotkey_thread_mutex);
2038 mutex_init(&hotkey_thread_data_mutex);
2041 /* hotkey not supported on 570 */
2042 tp_features.hotkey = hkey_handle != NULL;
2044 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2045 str_supported(tp_features.hotkey));
2047 if (tp_features.hotkey) {
2048 hotkey_dev_attributes = create_attr_set(13, NULL);
2049 if (!hotkey_dev_attributes)
2051 res = add_many_to_attr_set(hotkey_dev_attributes,
2053 ARRAY_SIZE(hotkey_attributes));
2057 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2058 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2059 for HKEY interface version 0x100 */
2060 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2061 if ((hkeyv >> 8) != 1) {
2062 printk(TPACPI_ERR "unknown version of the "
2063 "HKEY interface: 0x%x\n", hkeyv);
2064 printk(TPACPI_ERR "please report this to %s\n",
2068 * MHKV 0x100 in A31, R40, R40e,
2069 * T4x, X31, and later
2071 tp_features.hotkey_mask = 1;
2075 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2076 str_supported(tp_features.hotkey_mask));
2078 if (tp_features.hotkey_mask) {
2079 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2082 "missing MHKA handler, "
2083 "please report this to %s\n",
2085 /* FN+F12, FN+F4, FN+F3 */
2086 hotkey_all_mask = 0x080cU;
2090 /* hotkey_source_mask *must* be zero for
2091 * the first hotkey_mask_get */
2092 res = hotkey_status_get(&hotkey_orig_status);
2093 if (!res && tp_features.hotkey_mask) {
2094 res = hotkey_mask_get();
2095 hotkey_orig_mask = hotkey_mask;
2097 res = add_many_to_attr_set(
2098 hotkey_dev_attributes,
2099 hotkey_mask_attributes,
2100 ARRAY_SIZE(hotkey_mask_attributes));
2104 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2105 if (tp_features.hotkey_mask) {
2106 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2109 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2112 vdbg_printk(TPACPI_DBG_INIT,
2113 "hotkey source mask 0x%08x, polling freq %d\n",
2114 hotkey_source_mask, hotkey_poll_freq);
2117 /* Not all thinkpads have a hardware radio switch */
2118 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2119 tp_features.hotkey_wlsw = 1;
2121 "radio switch found; radios are %s\n",
2122 enabled(status, 0));
2123 res = add_to_attr_set(hotkey_dev_attributes,
2124 &dev_attr_hotkey_radio_sw.attr);
2127 /* For X41t, X60t, X61t Tablets... */
2128 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2129 tp_features.hotkey_tablet = 1;
2131 "possible tablet mode switch found; "
2132 "ThinkPad in %s mode\n",
2133 (status & TP_HOTKEY_TABLET_MASK)?
2134 "tablet" : "laptop");
2135 res = add_to_attr_set(hotkey_dev_attributes,
2136 &dev_attr_hotkey_tablet_mode.attr);
2140 res = register_attr_set_with_sysfs(
2141 hotkey_dev_attributes,
2142 &tpacpi_pdev->dev.kobj);
2146 /* Set up key map */
2148 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2150 if (!hotkey_keycode_map) {
2152 "failed to allocate memory for key map\n");
2156 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2157 dbg_printk(TPACPI_DBG_INIT,
2158 "using Lenovo default hot key map\n");
2159 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2160 TPACPI_HOTKEY_MAP_SIZE);
2162 dbg_printk(TPACPI_DBG_INIT,
2163 "using IBM default hot key map\n");
2164 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2165 TPACPI_HOTKEY_MAP_SIZE);
2168 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2169 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2170 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2171 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2172 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2173 tpacpi_inputdev->keycode = hotkey_keycode_map;
2174 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2175 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2176 set_bit(hotkey_keycode_map[i],
2177 tpacpi_inputdev->keybit);
2179 if (i < sizeof(hotkey_reserved_mask)*8)
2180 hotkey_reserved_mask |= 1 << i;
2184 if (tp_features.hotkey_wlsw) {
2185 set_bit(EV_SW, tpacpi_inputdev->evbit);
2186 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2188 if (tp_features.hotkey_tablet) {
2189 set_bit(EV_SW, tpacpi_inputdev->evbit);
2190 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2193 /* Do not issue duplicate brightness change events to
2195 if (!tp_features.bright_acpimode)
2196 /* update bright_acpimode... */
2197 tpacpi_check_std_acpi_brightness_support();
2199 if (tp_features.bright_acpimode) {
2201 "This ThinkPad has standard ACPI backlight "
2202 "brightness control, supported by the ACPI "
2204 printk(TPACPI_NOTICE
2205 "Disabling thinkpad-acpi brightness events "
2208 /* The hotkey_reserved_mask change below is not
2209 * necessary while the keys are at KEY_RESERVED in the
2210 * default map, but better safe than sorry, leave it
2211 * here as a marker of what we have to do, especially
2212 * when we finally become able to set this at runtime
2213 * on response to X.org requests */
2214 hotkey_reserved_mask |=
2215 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2216 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2219 dbg_printk(TPACPI_DBG_INIT,
2220 "enabling hot key handling\n");
2221 res = hotkey_status_set(1);
2224 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2225 & ~hotkey_reserved_mask)
2226 | hotkey_orig_mask);
2227 if (res < 0 && res != -ENXIO)
2230 dbg_printk(TPACPI_DBG_INIT,
2231 "legacy hot key reporting over procfs %s\n",
2232 (hotkey_report_mode < 2) ?
2233 "enabled" : "disabled");
2235 tpacpi_inputdev->open = &hotkey_inputdev_open;
2236 tpacpi_inputdev->close = &hotkey_inputdev_close;
2238 hotkey_poll_setup_safe(1);
2239 tpacpi_input_send_radiosw();
2240 tpacpi_input_send_tabletsw();
2243 return (tp_features.hotkey)? 0 : 1;
2246 static void hotkey_exit(void)
2248 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2249 hotkey_poll_stop_sync();
2252 if (tp_features.hotkey) {
2253 dbg_printk(TPACPI_DBG_EXIT,
2254 "restoring original hot key mask\n");
2255 /* no short-circuit boolean operator below! */
2256 if ((hotkey_mask_set(hotkey_orig_mask) |
2257 hotkey_status_set(hotkey_orig_status)) != 0)
2259 "failed to restore hot key mask "
2260 "to BIOS defaults\n");
2263 if (hotkey_dev_attributes) {
2264 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2265 hotkey_dev_attributes = NULL;
2269 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2272 unsigned int scancode;
2277 if (event != 0x80) {
2279 "unknown HKEY notification event %d\n", event);
2280 /* forward it to userspace, maybe it knows how to handle it */
2281 acpi_bus_generate_netlink_event(
2282 ibm->acpi->device->pnp.device_class,
2283 ibm->acpi->device->dev.bus_id,
2289 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2290 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2303 switch (hkey >> 12) {
2305 /* 0x1000-0x1FFF: key presses */
2306 scancode = hkey & 0xfff;
2307 if (scancode > 0 && scancode < 0x21) {
2309 if (!(hotkey_source_mask & (1 << scancode))) {
2310 tpacpi_input_send_key(scancode);
2322 case 0x2304: /* suspend, undock */
2323 case 0x2404: /* hibernation, undock */
2324 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2327 case 0x2305: /* suspend, bay eject */
2328 case 0x2405: /* hibernation, bay eject */
2329 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2335 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2337 "woke up due to a hot-unplug "
2339 hotkey_wakeup_reason_notify_change();
2343 /* bay-related wakeups */
2344 if (hkey == 0x3003) {
2345 hotkey_autosleep_ack = 1;
2348 hotkey_wakeup_hotunplug_complete_notify_change();
2354 /* dock-related wakeups */
2355 if (hkey == 0x4003) {
2356 hotkey_autosleep_ack = 1;
2359 hotkey_wakeup_hotunplug_complete_notify_change();
2365 /* 0x5000-0x5FFF: human interface helpers */
2367 case 0x5010: /* Lenovo new BIOS: brightness changed */
2368 case 0x500b: /* X61t: tablet pen inserted into bay */
2369 case 0x500c: /* X61t: tablet pen removed from bay */
2371 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2372 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2373 tpacpi_input_send_tabletsw();
2374 hotkey_tablet_mode_notify_change();
2379 /* LID switch events. Do not propagate */
2387 /* 0x7000-0x7FFF: misc */
2388 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2389 tpacpi_input_send_radiosw();
2390 hotkey_radio_sw_notify_change();
2394 /* fallthrough to default */
2399 printk(TPACPI_NOTICE
2400 "unhandled HKEY event 0x%04x\n", hkey);
2404 if (!ignore_acpi_ev &&
2405 (send_acpi_ev || hotkey_report_mode < 2)) {
2406 acpi_bus_generate_proc_event(ibm->acpi->device,
2410 /* netlink events */
2411 if (!ignore_acpi_ev && send_acpi_ev) {
2412 acpi_bus_generate_netlink_event(
2413 ibm->acpi->device->pnp.device_class,
2414 ibm->acpi->device->dev.bus_id,
2420 static void hotkey_suspend(pm_message_t state)
2422 /* Do these on suspend, we get the events on early resume! */
2423 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2424 hotkey_autosleep_ack = 0;
2427 static void hotkey_resume(void)
2429 if (hotkey_mask_get())
2431 "error while trying to read hot key mask "
2433 tpacpi_input_send_radiosw();
2434 hotkey_radio_sw_notify_change();
2435 hotkey_tablet_mode_notify_change();
2436 hotkey_wakeup_reason_notify_change();
2437 hotkey_wakeup_hotunplug_complete_notify_change();
2438 hotkey_poll_setup_safe(0);
2441 /* procfs -------------------------------------------------------------- */
2442 static int hotkey_read(char *p)
2447 if (!tp_features.hotkey) {
2448 len += sprintf(p + len, "status:\t\tnot supported\n");
2452 if (mutex_lock_interruptible(&hotkey_mutex))
2453 return -ERESTARTSYS;
2454 res = hotkey_status_get(&status);
2456 res = hotkey_mask_get();
2457 mutex_unlock(&hotkey_mutex);
2461 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2462 if (tp_features.hotkey_mask) {
2463 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2464 len += sprintf(p + len,
2465 "commands:\tenable, disable, reset, <mask>\n");
2467 len += sprintf(p + len, "mask:\t\tnot supported\n");
2468 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2474 static int hotkey_write(char *buf)
2480 if (!tp_features.hotkey)
2483 if (mutex_lock_interruptible(&hotkey_mutex))
2484 return -ERESTARTSYS;
2490 while ((cmd = next_cmd(&buf))) {
2491 if (strlencmp(cmd, "enable") == 0) {
2493 } else if (strlencmp(cmd, "disable") == 0) {
2495 } else if (strlencmp(cmd, "reset") == 0) {
2496 status = hotkey_orig_status;
2497 mask = hotkey_orig_mask;
2498 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2500 } else if (sscanf(cmd, "%x", &mask) == 1) {
2508 res = hotkey_status_set(status);
2510 if (!res && mask != hotkey_mask)
2511 res = hotkey_mask_set(mask);
2514 mutex_unlock(&hotkey_mutex);
2518 static const struct acpi_device_id ibm_htk_device_ids[] = {
2519 {TPACPI_ACPI_HKEY_HID, 0},
2523 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2524 .hid = ibm_htk_device_ids,
2525 .notify = hotkey_notify,
2526 .handle = &hkey_handle,
2527 .type = ACPI_DEVICE_NOTIFY,
2530 static struct ibm_struct hotkey_driver_data = {
2532 .read = hotkey_read,
2533 .write = hotkey_write,
2534 .exit = hotkey_exit,
2535 .resume = hotkey_resume,
2536 .suspend = hotkey_suspend,
2537 .acpi = &ibm_hotkey_acpidriver,
2540 /*************************************************************************
2541 * Bluetooth subdriver
2545 /* ACPI GBDC/SBDC bits */
2546 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2547 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2548 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2551 static int bluetooth_get_radiosw(void);
2552 static int bluetooth_set_radiosw(int radio_on);
2554 /* sysfs bluetooth enable ---------------------------------------------- */
2555 static ssize_t bluetooth_enable_show(struct device *dev,
2556 struct device_attribute *attr,
2561 status = bluetooth_get_radiosw();
2565 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2568 static ssize_t bluetooth_enable_store(struct device *dev,
2569 struct device_attribute *attr,
2570 const char *buf, size_t count)
2575 if (parse_strtoul(buf, 1, &t))
2578 res = bluetooth_set_radiosw(t);
2580 return (res) ? res : count;
2583 static struct device_attribute dev_attr_bluetooth_enable =
2584 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2585 bluetooth_enable_show, bluetooth_enable_store);
2587 /* --------------------------------------------------------------------- */
2589 static struct attribute *bluetooth_attributes[] = {
2590 &dev_attr_bluetooth_enable.attr,
2594 static const struct attribute_group bluetooth_attr_group = {
2595 .attrs = bluetooth_attributes,
2598 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2603 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2605 TPACPI_ACPIHANDLE_INIT(hkey);
2607 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2608 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2609 tp_features.bluetooth = hkey_handle &&
2610 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2612 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2613 str_supported(tp_features.bluetooth),
2616 if (tp_features.bluetooth) {
2617 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2618 /* no bluetooth hardware present in system */
2619 tp_features.bluetooth = 0;
2620 dbg_printk(TPACPI_DBG_INIT,
2621 "bluetooth hardware not installed\n");
2623 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2624 &bluetooth_attr_group);
2630 return (tp_features.bluetooth)? 0 : 1;
2633 static void bluetooth_exit(void)
2635 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2636 &bluetooth_attr_group);
2639 static int bluetooth_get_radiosw(void)
2643 if (!tp_features.bluetooth)
2646 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2649 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2652 static int bluetooth_set_radiosw(int radio_on)
2656 if (!tp_features.bluetooth)
2659 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2662 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2664 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2665 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2671 /* procfs -------------------------------------------------------------- */
2672 static int bluetooth_read(char *p)
2675 int status = bluetooth_get_radiosw();
2677 if (!tp_features.bluetooth)
2678 len += sprintf(p + len, "status:\t\tnot supported\n");
2680 len += sprintf(p + len, "status:\t\t%s\n",
2681 (status)? "enabled" : "disabled");
2682 len += sprintf(p + len, "commands:\tenable, disable\n");
2688 static int bluetooth_write(char *buf)
2692 if (!tp_features.bluetooth)
2695 while ((cmd = next_cmd(&buf))) {
2696 if (strlencmp(cmd, "enable") == 0) {
2697 bluetooth_set_radiosw(1);
2698 } else if (strlencmp(cmd, "disable") == 0) {
2699 bluetooth_set_radiosw(0);
2707 static struct ibm_struct bluetooth_driver_data = {
2708 .name = "bluetooth",
2709 .read = bluetooth_read,
2710 .write = bluetooth_write,
2711 .exit = bluetooth_exit,
2714 /*************************************************************************
2719 /* ACPI GWAN/SWAN bits */
2720 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2721 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2722 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2725 static int wan_get_radiosw(void);
2726 static int wan_set_radiosw(int radio_on);
2728 /* sysfs wan enable ---------------------------------------------------- */
2729 static ssize_t wan_enable_show(struct device *dev,
2730 struct device_attribute *attr,
2735 status = wan_get_radiosw();
2739 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2742 static ssize_t wan_enable_store(struct device *dev,
2743 struct device_attribute *attr,
2744 const char *buf, size_t count)
2749 if (parse_strtoul(buf, 1, &t))
2752 res = wan_set_radiosw(t);
2754 return (res) ? res : count;
2757 static struct device_attribute dev_attr_wan_enable =
2758 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2759 wan_enable_show, wan_enable_store);
2761 /* --------------------------------------------------------------------- */
2763 static struct attribute *wan_attributes[] = {
2764 &dev_attr_wan_enable.attr,
2768 static const struct attribute_group wan_attr_group = {
2769 .attrs = wan_attributes,
2772 static int __init wan_init(struct ibm_init_struct *iibm)
2777 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2779 TPACPI_ACPIHANDLE_INIT(hkey);
2781 tp_features.wan = hkey_handle &&
2782 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2784 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2785 str_supported(tp_features.wan),
2788 if (tp_features.wan) {
2789 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2790 /* no wan hardware present in system */
2791 tp_features.wan = 0;
2792 dbg_printk(TPACPI_DBG_INIT,
2793 "wan hardware not installed\n");
2795 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2802 return (tp_features.wan)? 0 : 1;
2805 static void wan_exit(void)
2807 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2811 static int wan_get_radiosw(void)
2815 if (!tp_features.wan)
2818 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2821 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2824 static int wan_set_radiosw(int radio_on)
2828 if (!tp_features.wan)
2831 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2834 status |= TP_ACPI_WANCARD_RADIOSSW;
2836 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2837 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2843 /* procfs -------------------------------------------------------------- */
2844 static int wan_read(char *p)
2847 int status = wan_get_radiosw();
2849 if (!tp_features.wan)
2850 len += sprintf(p + len, "status:\t\tnot supported\n");
2852 len += sprintf(p + len, "status:\t\t%s\n",
2853 (status)? "enabled" : "disabled");
2854 len += sprintf(p + len, "commands:\tenable, disable\n");
2860 static int wan_write(char *buf)
2864 if (!tp_features.wan)
2867 while ((cmd = next_cmd(&buf))) {
2868 if (strlencmp(cmd, "enable") == 0) {
2870 } else if (strlencmp(cmd, "disable") == 0) {
2879 static struct ibm_struct wan_driver_data = {
2884 .flags.experimental = 1,
2887 /*************************************************************************
2891 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2893 enum video_access_mode {
2894 TPACPI_VIDEO_NONE = 0,
2895 TPACPI_VIDEO_570, /* 570 */
2896 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2897 TPACPI_VIDEO_NEW, /* all others */
2900 enum { /* video status flags, based on VIDEO_570 */
2901 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2902 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2903 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2906 enum { /* TPACPI_VIDEO_570 constants */
2907 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2908 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2909 * video_status_flags */
2910 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2911 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2914 static enum video_access_mode video_supported;
2915 static int video_orig_autosw;
2917 static int video_autosw_get(void);
2918 static int video_autosw_set(int enable);
2920 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2922 static int __init video_init(struct ibm_init_struct *iibm)
2926 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2928 TPACPI_ACPIHANDLE_INIT(vid);
2929 TPACPI_ACPIHANDLE_INIT(vid2);
2931 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2932 /* G41, assume IVGA doesn't change */
2933 vid_handle = vid2_handle;
2936 /* video switching not supported on R30, R31 */
2937 video_supported = TPACPI_VIDEO_NONE;
2938 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2940 video_supported = TPACPI_VIDEO_570;
2941 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2942 /* 600e/x, 770e, 770x */
2943 video_supported = TPACPI_VIDEO_770;
2946 video_supported = TPACPI_VIDEO_NEW;
2948 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2949 str_supported(video_supported != TPACPI_VIDEO_NONE),
2952 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2955 static void video_exit(void)
2957 dbg_printk(TPACPI_DBG_EXIT,
2958 "restoring original video autoswitch mode\n");
2959 if (video_autosw_set(video_orig_autosw))
2960 printk(TPACPI_ERR "error while trying to restore original "
2961 "video autoswitch mode\n");
2964 static int video_outputsw_get(void)
2969 switch (video_supported) {
2970 case TPACPI_VIDEO_570:
2971 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2972 TP_ACPI_VIDEO_570_PHSCMD))
2974 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2976 case TPACPI_VIDEO_770:
2977 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2980 status |= TP_ACPI_VIDEO_S_LCD;
2981 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2984 status |= TP_ACPI_VIDEO_S_CRT;
2986 case TPACPI_VIDEO_NEW:
2987 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2988 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2991 status |= TP_ACPI_VIDEO_S_CRT;
2993 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2994 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2997 status |= TP_ACPI_VIDEO_S_LCD;
2998 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3001 status |= TP_ACPI_VIDEO_S_DVI;
3010 static int video_outputsw_set(int status)
3015 switch (video_supported) {
3016 case TPACPI_VIDEO_570:
3017 res = acpi_evalf(NULL, NULL,
3018 "\\_SB.PHS2", "vdd",
3019 TP_ACPI_VIDEO_570_PHS2CMD,
3020 status | TP_ACPI_VIDEO_570_PHS2SET);
3022 case TPACPI_VIDEO_770:
3023 autosw = video_autosw_get();
3027 res = video_autosw_set(1);
3030 res = acpi_evalf(vid_handle, NULL,
3031 "ASWT", "vdd", status * 0x100, 0);
3032 if (!autosw && video_autosw_set(autosw)) {
3034 "video auto-switch left enabled due to error\n");
3038 case TPACPI_VIDEO_NEW:
3039 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3040 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3046 return (res)? 0 : -EIO;
3049 static int video_autosw_get(void)
3053 switch (video_supported) {
3054 case TPACPI_VIDEO_570:
3055 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3058 case TPACPI_VIDEO_770:
3059 case TPACPI_VIDEO_NEW:
3060 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3070 static int video_autosw_set(int enable)
3072 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3077 static int video_outputsw_cycle(void)
3079 int autosw = video_autosw_get();
3085 switch (video_supported) {
3086 case TPACPI_VIDEO_570:
3087 res = video_autosw_set(1);
3090 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3092 case TPACPI_VIDEO_770:
3093 case TPACPI_VIDEO_NEW:
3094 res = video_autosw_set(1);
3097 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3102 if (!autosw && video_autosw_set(autosw)) {
3104 "video auto-switch left enabled due to error\n");
3108 return (res)? 0 : -EIO;
3111 static int video_expand_toggle(void)
3113 switch (video_supported) {
3114 case TPACPI_VIDEO_570:
3115 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3117 case TPACPI_VIDEO_770:
3118 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3120 case TPACPI_VIDEO_NEW:
3121 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3129 static int video_read(char *p)
3134 if (video_supported == TPACPI_VIDEO_NONE) {
3135 len += sprintf(p + len, "status:\t\tnot supported\n");
3139 status = video_outputsw_get();
3143 autosw = video_autosw_get();
3147 len += sprintf(p + len, "status:\t\tsupported\n");
3148 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3149 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3150 if (video_supported == TPACPI_VIDEO_NEW)
3151 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3152 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3153 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3154 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3155 if (video_supported == TPACPI_VIDEO_NEW)
3156 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3157 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3158 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3163 static int video_write(char *buf)
3166 int enable, disable, status;
3169 if (video_supported == TPACPI_VIDEO_NONE)
3175 while ((cmd = next_cmd(&buf))) {
3176 if (strlencmp(cmd, "lcd_enable") == 0) {
3177 enable |= TP_ACPI_VIDEO_S_LCD;
3178 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3179 disable |= TP_ACPI_VIDEO_S_LCD;
3180 } else if (strlencmp(cmd, "crt_enable") == 0) {
3181 enable |= TP_ACPI_VIDEO_S_CRT;
3182 } else if (strlencmp(cmd, "crt_disable") == 0) {
3183 disable |= TP_ACPI_VIDEO_S_CRT;
3184 } else if (video_supported == TPACPI_VIDEO_NEW &&
3185 strlencmp(cmd, "dvi_enable") == 0) {
3186 enable |= TP_ACPI_VIDEO_S_DVI;
3187 } else if (video_supported == TPACPI_VIDEO_NEW &&
3188 strlencmp(cmd, "dvi_disable") == 0) {
3189 disable |= TP_ACPI_VIDEO_S_DVI;
3190 } else if (strlencmp(cmd, "auto_enable") == 0) {
3191 res = video_autosw_set(1);
3194 } else if (strlencmp(cmd, "auto_disable") == 0) {
3195 res = video_autosw_set(0);
3198 } else if (strlencmp(cmd, "video_switch") == 0) {
3199 res = video_outputsw_cycle();
3202 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3203 res = video_expand_toggle();
3210 if (enable || disable) {
3211 status = video_outputsw_get();
3214 res = video_outputsw_set((status & ~disable) | enable);
3222 static struct ibm_struct video_driver_data = {
3225 .write = video_write,
3229 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3231 /*************************************************************************
3232 * Light (thinklight) subdriver
3235 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3236 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3238 static int __init light_init(struct ibm_init_struct *iibm)
3240 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3242 TPACPI_ACPIHANDLE_INIT(ledb);
3243 TPACPI_ACPIHANDLE_INIT(lght);
3244 TPACPI_ACPIHANDLE_INIT(cmos);
3246 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3247 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3249 if (tp_features.light)
3250 /* light status not supported on
3251 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3252 tp_features.light_status =
3253 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3255 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3256 str_supported(tp_features.light));
3258 return (tp_features.light)? 0 : 1;
3261 static int light_read(char *p)
3266 if (!tp_features.light) {
3267 len += sprintf(p + len, "status:\t\tnot supported\n");
3268 } else if (!tp_features.light_status) {
3269 len += sprintf(p + len, "status:\t\tunknown\n");
3270 len += sprintf(p + len, "commands:\ton, off\n");
3272 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3274 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3275 len += sprintf(p + len, "commands:\ton, off\n");
3281 static int light_write(char *buf)
3283 int cmos_cmd, lght_cmd;
3287 if (!tp_features.light)
3290 while ((cmd = next_cmd(&buf))) {
3291 if (strlencmp(cmd, "on") == 0) {
3294 } else if (strlencmp(cmd, "off") == 0) {
3300 success = cmos_handle ?
3301 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3302 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3310 static struct ibm_struct light_driver_data = {
3313 .write = light_write,
3316 /*************************************************************************
3320 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3322 static void dock_notify(struct ibm_struct *ibm, u32 event);
3323 static int dock_read(char *p);
3324 static int dock_write(char *buf);
3326 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3327 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3328 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3329 "\\_SB.PCI.ISA.SLCE", /* 570 */
3330 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3332 /* don't list other alternatives as we install a notify handler on the 570 */
3333 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3335 static const struct acpi_device_id ibm_pci_device_ids[] = {
3336 {PCI_ROOT_HID_STRING, 0},
3340 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3342 .notify = dock_notify,
3343 .handle = &dock_handle,
3344 .type = ACPI_SYSTEM_NOTIFY,
3347 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3348 * We just use it to get notifications of dock hotplug
3349 * in very old thinkpads */
3350 .hid = ibm_pci_device_ids,
3351 .notify = dock_notify,
3352 .handle = &pci_handle,
3353 .type = ACPI_SYSTEM_NOTIFY,
3357 static struct ibm_struct dock_driver_data[2] = {
3361 .write = dock_write,
3362 .acpi = &ibm_dock_acpidriver[0],
3366 .acpi = &ibm_dock_acpidriver[1],
3370 #define dock_docked() (_sta(dock_handle) & 1)
3372 static int __init dock_init(struct ibm_init_struct *iibm)
3374 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3376 TPACPI_ACPIHANDLE_INIT(dock);
3378 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3379 str_supported(dock_handle != NULL));
3381 return (dock_handle)? 0 : 1;
3384 static int __init dock_init2(struct ibm_init_struct *iibm)
3388 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3390 if (dock_driver_data[0].flags.acpi_driver_registered &&
3391 dock_driver_data[0].flags.acpi_notify_installed) {
3392 TPACPI_ACPIHANDLE_INIT(pci);
3393 dock2_needed = (pci_handle != NULL);
3394 vdbg_printk(TPACPI_DBG_INIT,
3395 "dock PCI handler for the TP 570 is %s\n",
3396 str_supported(dock2_needed));
3398 vdbg_printk(TPACPI_DBG_INIT,
3399 "dock subdriver part 2 not required\n");
3403 return (dock2_needed)? 0 : 1;
3406 static void dock_notify(struct ibm_struct *ibm, u32 event)
3408 int docked = dock_docked();
3409 int pci = ibm->acpi->hid && ibm->acpi->device &&
3410 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3413 if (event == 1 && !pci) /* 570 */
3414 data = 1; /* button */
3415 else if (event == 1 && pci) /* 570 */
3416 data = 3; /* dock */
3417 else if (event == 3 && docked)
3418 data = 1; /* button */
3419 else if (event == 3 && !docked)
3420 data = 2; /* undock */
3421 else if (event == 0 && docked)
3422 data = 3; /* dock */
3424 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3425 event, _sta(dock_handle));
3426 data = 0; /* unknown */
3428 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3429 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3430 ibm->acpi->device->dev.bus_id,
3434 static int dock_read(char *p)
3437 int docked = dock_docked();
3440 len += sprintf(p + len, "status:\t\tnot supported\n");
3442 len += sprintf(p + len, "status:\t\tundocked\n");
3444 len += sprintf(p + len, "status:\t\tdocked\n");
3445 len += sprintf(p + len, "commands:\tdock, undock\n");
3451 static int dock_write(char *buf)
3458 while ((cmd = next_cmd(&buf))) {
3459 if (strlencmp(cmd, "undock") == 0) {
3460 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3461 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3463 } else if (strlencmp(cmd, "dock") == 0) {
3464 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3473 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3475 /*************************************************************************
3479 #ifdef CONFIG_THINKPAD_ACPI_BAY
3481 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3482 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3483 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3484 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3485 ); /* A21e, R30, R31 */
3486 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3487 "_EJ0", /* all others */
3488 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3489 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3490 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3492 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3496 static int __init bay_init(struct ibm_init_struct *iibm)
3498 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3500 TPACPI_ACPIHANDLE_INIT(bay);
3502 TPACPI_ACPIHANDLE_INIT(bay_ej);
3503 TPACPI_ACPIHANDLE_INIT(bay2);
3505 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3507 tp_features.bay_status = bay_handle &&
3508 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3509 tp_features.bay_status2 = bay2_handle &&
3510 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3512 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3513 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3514 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3515 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3517 vdbg_printk(TPACPI_DBG_INIT,
3518 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3519 str_supported(tp_features.bay_status),
3520 str_supported(tp_features.bay_eject),
3521 str_supported(tp_features.bay_status2),
3522 str_supported(tp_features.bay_eject2));
3524 return (tp_features.bay_status || tp_features.bay_eject ||
3525 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3528 static void bay_notify(struct ibm_struct *ibm, u32 event)
3530 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3531 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3532 ibm->acpi->device->dev.bus_id,
3536 #define bay_occupied(b) (_sta(b##_handle) & 1)
3538 static int bay_read(char *p)
3541 int occupied = bay_occupied(bay);
3542 int occupied2 = bay_occupied(bay2);
3545 len += sprintf(p + len, "status:\t\t%s\n",
3546 tp_features.bay_status ?
3547 (occupied ? "occupied" : "unoccupied") :
3549 if (tp_features.bay_status2)
3550 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3551 "occupied" : "unoccupied");
3553 eject = tp_features.bay_eject && occupied;
3554 eject2 = tp_features.bay_eject2 && occupied2;
3556 if (eject && eject2)
3557 len += sprintf(p + len, "commands:\teject, eject2\n");
3559 len += sprintf(p + len, "commands:\teject\n");
3561 len += sprintf(p + len, "commands:\teject2\n");
3566 static int bay_write(char *buf)
3570 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3573 while ((cmd = next_cmd(&buf))) {
3574 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3575 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3577 } else if (tp_features.bay_eject2 &&
3578 strlencmp(cmd, "eject2") == 0) {
3579 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3588 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3589 .notify = bay_notify,
3590 .handle = &bay_handle,
3591 .type = ACPI_SYSTEM_NOTIFY,
3594 static struct ibm_struct bay_driver_data = {
3598 .acpi = &ibm_bay_acpidriver,
3601 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3603 /*************************************************************************
3607 /* sysfs cmos_command -------------------------------------------------- */
3608 static ssize_t cmos_command_store(struct device *dev,
3609 struct device_attribute *attr,
3610 const char *buf, size_t count)
3612 unsigned long cmos_cmd;
3615 if (parse_strtoul(buf, 21, &cmos_cmd))
3618 res = issue_thinkpad_cmos_command(cmos_cmd);
3619 return (res)? res : count;
3622 static struct device_attribute dev_attr_cmos_command =
3623 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3625 /* --------------------------------------------------------------------- */
3627 static int __init cmos_init(struct ibm_init_struct *iibm)
3631 vdbg_printk(TPACPI_DBG_INIT,
3632 "initializing cmos commands subdriver\n");
3634 TPACPI_ACPIHANDLE_INIT(cmos);
3636 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3637 str_supported(cmos_handle != NULL));
3639 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3643 return (cmos_handle)? 0 : 1;
3646 static void cmos_exit(void)
3648 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3651 static int cmos_read(char *p)
3655 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3656 R30, R31, T20-22, X20-21 */
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-21)\n");
3667 static int cmos_write(char *buf)
3672 while ((cmd = next_cmd(&buf))) {
3673 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3674 cmos_cmd >= 0 && cmos_cmd <= 21) {
3679 res = issue_thinkpad_cmos_command(cmos_cmd);
3687 static struct ibm_struct cmos_driver_data = {
3690 .write = cmos_write,
3694 /*************************************************************************
3698 enum led_access_mode {
3699 TPACPI_LED_NONE = 0,
3700 TPACPI_LED_570, /* 570 */
3701 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3702 TPACPI_LED_NEW, /* all others */
3705 enum { /* For TPACPI_LED_OLD */
3706 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3707 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3708 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3711 static enum led_access_mode led_supported;
3713 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3714 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3715 /* T20-22, X20-21 */
3716 "LED", /* all others */
3719 static int __init led_init(struct ibm_init_struct *iibm)
3721 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3723 TPACPI_ACPIHANDLE_INIT(led);
3726 /* led not supported on R30, R31 */
3727 led_supported = TPACPI_LED_NONE;
3728 else if (strlencmp(led_path, "SLED") == 0)
3730 led_supported = TPACPI_LED_570;
3731 else if (strlencmp(led_path, "SYSL") == 0)
3732 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3733 led_supported = TPACPI_LED_OLD;
3736 led_supported = TPACPI_LED_NEW;
3738 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3739 str_supported(led_supported), led_supported);
3741 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3744 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3746 static int led_read(char *p)
3750 if (!led_supported) {
3751 len += sprintf(p + len, "status:\t\tnot supported\n");
3754 len += sprintf(p + len, "status:\t\tsupported\n");
3756 if (led_supported == TPACPI_LED_570) {
3759 for (i = 0; i < 8; i++) {
3760 if (!acpi_evalf(ec_handle,
3761 &status, "GLED", "dd", 1 << i))
3763 len += sprintf(p + len, "%d:\t\t%s\n",
3764 i, led_status(status));
3768 len += sprintf(p + len, "commands:\t"
3769 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3774 /* off, on, blink */
3775 static const int led_sled_arg1[] = { 0, 1, 3 };
3776 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3777 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3778 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3780 static int led_write(char *buf)
3788 while ((cmd = next_cmd(&buf))) {
3789 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3792 if (strstr(cmd, "off")) {
3794 } else if (strstr(cmd, "on")) {
3796 } else if (strstr(cmd, "blink")) {
3801 if (led_supported == TPACPI_LED_570) {
3804 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3805 led, led_sled_arg1[ind]))
3807 } else if (led_supported == TPACPI_LED_OLD) {
3808 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3810 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3812 ret = ec_write(TPACPI_LED_EC_HLBL,
3813 led * led_exp_hlbl[ind]);
3815 ret = ec_write(TPACPI_LED_EC_HLCL,
3816 led * led_exp_hlcl[ind]);
3821 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3822 led, led_led_arg1[ind]))
3830 static struct ibm_struct led_driver_data = {
3836 /*************************************************************************
3840 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3842 static int __init beep_init(struct ibm_init_struct *iibm)
3844 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3846 TPACPI_ACPIHANDLE_INIT(beep);
3848 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3849 str_supported(beep_handle != NULL));
3851 return (beep_handle)? 0 : 1;
3854 static int beep_read(char *p)
3859 len += sprintf(p + len, "status:\t\tnot supported\n");
3861 len += sprintf(p + len, "status:\t\tsupported\n");
3862 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3868 static int beep_write(char *buf)
3876 while ((cmd = next_cmd(&buf))) {
3877 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3878 beep_cmd >= 0 && beep_cmd <= 17) {
3882 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3889 static struct ibm_struct beep_driver_data = {
3892 .write = beep_write,
3895 /*************************************************************************
3899 enum thermal_access_mode {
3900 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3901 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3902 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3903 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3904 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3907 enum { /* TPACPI_THERMAL_TPEC_* */
3908 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3909 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3910 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3913 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3914 struct ibm_thermal_sensors_struct {
3915 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3918 static enum thermal_access_mode thermal_read_mode;
3920 /* idx is zero-based */
3921 static int thermal_get_sensor(int idx, s32 *value)
3927 t = TP_EC_THERMAL_TMP0;
3929 switch (thermal_read_mode) {
3930 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3931 case TPACPI_THERMAL_TPEC_16:
3932 if (idx >= 8 && idx <= 15) {
3933 t = TP_EC_THERMAL_TMP8;
3938 case TPACPI_THERMAL_TPEC_8:
3940 if (!acpi_ec_read(t + idx, &tmp))
3942 *value = tmp * 1000;
3947 case TPACPI_THERMAL_ACPI_UPDT:
3949 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3950 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3952 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3954 *value = (t - 2732) * 100;
3959 case TPACPI_THERMAL_ACPI_TMP07:
3961 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3962 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3964 if (t > 127 || t < -127)
3965 t = TP_EC_THERMAL_TMP_NA;
3971 case TPACPI_THERMAL_NONE:
3979 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3990 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3993 for (i = 0 ; i < n; i++) {
3994 res = thermal_get_sensor(i, &s->temp[i]);
4002 /* sysfs temp##_input -------------------------------------------------- */
4004 static ssize_t thermal_temp_input_show(struct device *dev,
4005 struct device_attribute *attr,
4008 struct sensor_device_attribute *sensor_attr =
4009 to_sensor_dev_attr(attr);
4010 int idx = sensor_attr->index;
4014 res = thermal_get_sensor(idx, &value);
4017 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4020 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4023 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4024 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4025 thermal_temp_input_show, NULL, _idxB)
4027 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4028 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4029 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4030 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4031 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4032 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4033 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4034 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4035 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4036 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4037 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4038 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4039 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4040 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4041 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4042 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4043 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4046 #define THERMAL_ATTRS(X) \
4047 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4049 static struct attribute *thermal_temp_input_attr[] = {
4069 static const struct attribute_group thermal_temp_input16_group = {
4070 .attrs = thermal_temp_input_attr
4073 static const struct attribute_group thermal_temp_input8_group = {
4074 .attrs = &thermal_temp_input_attr[8]
4077 #undef THERMAL_SENSOR_ATTR_TEMP
4078 #undef THERMAL_ATTRS
4080 /* --------------------------------------------------------------------- */
4082 static int __init thermal_init(struct ibm_init_struct *iibm)
4089 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4091 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4093 if (thinkpad_id.ec_model) {
4095 * Direct EC access mode: sensors at registers
4096 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4097 * non-implemented, thermal sensors return 0x80 when
4102 for (i = 0; i < 8; i++) {
4103 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4109 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4117 /* This is sheer paranoia, but we handle it anyway */
4120 "ThinkPad ACPI EC access misbehaving, "
4121 "falling back to ACPI TMPx access "
4123 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4126 "ThinkPad ACPI EC access misbehaving, "
4127 "disabling thermal sensors access\n");
4128 thermal_read_mode = TPACPI_THERMAL_NONE;
4133 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4135 } else if (acpi_tmp7) {
4136 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4137 /* 600e/x, 770e, 770x */
4138 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4140 /* Standard ACPI TMPx access, max 8 sensors */
4141 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4144 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4145 thermal_read_mode = TPACPI_THERMAL_NONE;
4148 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4149 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4152 switch (thermal_read_mode) {
4153 case TPACPI_THERMAL_TPEC_16:
4154 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4155 &thermal_temp_input16_group);
4159 case TPACPI_THERMAL_TPEC_8:
4160 case TPACPI_THERMAL_ACPI_TMP07:
4161 case TPACPI_THERMAL_ACPI_UPDT:
4162 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4163 &thermal_temp_input8_group);
4167 case TPACPI_THERMAL_NONE:
4175 static void thermal_exit(void)
4177 switch (thermal_read_mode) {
4178 case TPACPI_THERMAL_TPEC_16:
4179 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4180 &thermal_temp_input16_group);
4182 case TPACPI_THERMAL_TPEC_8:
4183 case TPACPI_THERMAL_ACPI_TMP07:
4184 case TPACPI_THERMAL_ACPI_UPDT:
4185 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4186 &thermal_temp_input16_group);
4188 case TPACPI_THERMAL_NONE:
4194 static int thermal_read(char *p)
4198 struct ibm_thermal_sensors_struct t;
4200 n = thermal_get_sensors(&t);
4201 if (unlikely(n < 0))
4204 len += sprintf(p + len, "temperatures:\t");
4207 for (i = 0; i < (n - 1); i++)
4208 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4209 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4211 len += sprintf(p + len, "not supported\n");
4216 static struct ibm_struct thermal_driver_data = {
4218 .read = thermal_read,
4219 .exit = thermal_exit,
4222 /*************************************************************************
4226 static u8 ecdump_regs[256];
4228 static int ecdump_read(char *p)
4234 len += sprintf(p + len, "EC "
4235 " +00 +01 +02 +03 +04 +05 +06 +07"
4236 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4237 for (i = 0; i < 256; i += 16) {
4238 len += sprintf(p + len, "EC 0x%02x:", i);
4239 for (j = 0; j < 16; j++) {
4240 if (!acpi_ec_read(i + j, &v))
4242 if (v != ecdump_regs[i + j])
4243 len += sprintf(p + len, " *%02x", v);
4245 len += sprintf(p + len, " %02x", v);
4246 ecdump_regs[i + j] = v;
4248 len += sprintf(p + len, "\n");
4253 /* These are way too dangerous to advertise openly... */
4255 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4256 " (<offset> is 00-ff, <value> is 00-ff)\n");
4257 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4258 " (<offset> is 00-ff, <value> is 0-255)\n");
4263 static int ecdump_write(char *buf)
4268 while ((cmd = next_cmd(&buf))) {
4269 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4271 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4275 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4276 if (!acpi_ec_write(i, v))
4285 static struct ibm_struct ecdump_driver_data = {
4287 .read = ecdump_read,
4288 .write = ecdump_write,
4289 .flags.experimental = 1,
4292 /*************************************************************************
4293 * Backlight/brightness subdriver
4296 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4298 static struct backlight_device *ibm_backlight_device;
4299 static int brightness_offset = 0x31;
4300 static int brightness_mode;
4301 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4303 static struct mutex brightness_mutex;
4306 * ThinkPads can read brightness from two places: EC 0x31, or
4307 * CMOS NVRAM byte 0x5E, bits 0-3.
4309 static int brightness_get(struct backlight_device *bd)
4311 u8 lec = 0, lcmos = 0, level = 0;
4313 if (brightness_mode & 1) {
4314 if (!acpi_ec_read(brightness_offset, &lec))
4316 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4319 if (brightness_mode & 2) {
4320 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4321 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4322 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4323 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4327 if (brightness_mode == 3) {
4329 tp_warned.bright_cmos_ec_unsync = 0;
4331 if (!tp_warned.bright_cmos_ec_unsync) {
4333 "CMOS NVRAM (%u) and EC (%u) do not "
4334 "agree on display brightness level\n",
4335 (unsigned int) lcmos,
4336 (unsigned int) lec);
4337 tp_warned.bright_cmos_ec_unsync = 1;
4346 /* May return EINTR which can always be mapped to ERESTARTSYS */
4347 static int brightness_set(int value)
4349 int cmos_cmd, inc, i, res;
4352 if (value > ((tp_features.bright_16levels)? 15 : 7))
4355 res = mutex_lock_interruptible(&brightness_mutex);
4359 current_value = brightness_get(NULL);
4360 if (current_value < 0) {
4361 res = current_value;
4365 cmos_cmd = value > current_value ?
4366 TP_CMOS_BRIGHTNESS_UP :
4367 TP_CMOS_BRIGHTNESS_DOWN;
4368 inc = (value > current_value)? 1 : -1;
4371 for (i = current_value; i != value; i += inc) {
4372 if ((brightness_mode & 2) &&
4373 issue_thinkpad_cmos_command(cmos_cmd)) {
4377 if ((brightness_mode & 1) &&
4378 !acpi_ec_write(brightness_offset, i + inc)) {
4385 mutex_unlock(&brightness_mutex);
4389 /* sysfs backlight class ----------------------------------------------- */
4391 static int brightness_update_status(struct backlight_device *bd)
4393 /* it is the backlight class's job (caller) to handle
4394 * EINTR and other errors properly */
4395 return brightness_set(
4396 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4397 bd->props.power == FB_BLANK_UNBLANK) ?
4398 bd->props.brightness : 0);
4401 static struct backlight_ops ibm_backlight_data = {
4402 .get_brightness = brightness_get,
4403 .update_status = brightness_update_status,
4406 /* --------------------------------------------------------------------- */
4408 static int __init brightness_init(struct ibm_init_struct *iibm)
4412 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4414 mutex_init(&brightness_mutex);
4417 * We always attempt to detect acpi support, so as to switch
4418 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4419 * going to publish a backlight interface
4421 b = tpacpi_check_std_acpi_brightness_support();
4423 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4424 printk(TPACPI_NOTICE
4425 "Lenovo BIOS switched to ACPI backlight "
4428 if (brightness_enable > 1) {
4429 printk(TPACPI_NOTICE
4430 "standard ACPI backlight interface "
4431 "available, not loading native one...\n");
4436 if (!brightness_enable) {
4437 dbg_printk(TPACPI_DBG_INIT,
4438 "brightness support disabled by "
4439 "module parameter\n");
4445 "Unsupported brightness interface, "
4446 "please contact %s\n", TPACPI_MAIL);
4450 tp_features.bright_16levels = 1;
4452 if (!brightness_mode) {
4453 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4454 brightness_mode = 2;
4456 brightness_mode = 3;
4458 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4462 if (brightness_mode > 3)
4465 b = brightness_get(NULL);
4469 if (tp_features.bright_16levels)
4471 "detected a 16-level brightness capable ThinkPad\n");
4473 ibm_backlight_device = backlight_device_register(
4474 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4475 &ibm_backlight_data);
4476 if (IS_ERR(ibm_backlight_device)) {
4477 printk(TPACPI_ERR "Could not register backlight device\n");
4478 return PTR_ERR(ibm_backlight_device);
4480 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4482 ibm_backlight_device->props.max_brightness =
4483 (tp_features.bright_16levels)? 15 : 7;
4484 ibm_backlight_device->props.brightness = b;
4485 backlight_update_status(ibm_backlight_device);
4490 static void brightness_exit(void)
4492 if (ibm_backlight_device) {
4493 vdbg_printk(TPACPI_DBG_EXIT,
4494 "calling backlight_device_unregister()\n");
4495 backlight_device_unregister(ibm_backlight_device);
4496 ibm_backlight_device = NULL;
4500 static int brightness_read(char *p)
4505 level = brightness_get(NULL);
4507 len += sprintf(p + len, "level:\t\tunreadable\n");
4509 len += sprintf(p + len, "level:\t\t%d\n", level);
4510 len += sprintf(p + len, "commands:\tup, down\n");
4511 len += sprintf(p + len, "commands:\tlevel <level>"
4512 " (<level> is 0-%d)\n",
4513 (tp_features.bright_16levels) ? 15 : 7);
4519 static int brightness_write(char *buf)
4524 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4526 level = brightness_get(NULL);
4530 while ((cmd = next_cmd(&buf))) {
4531 if (strlencmp(cmd, "up") == 0) {
4532 if (level < max_level)
4534 } else if (strlencmp(cmd, "down") == 0) {
4537 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4538 level >= 0 && level <= max_level) {
4545 * Now we know what the final level should be, so we try to set it.
4546 * Doing it this way makes the syscall restartable in case of EINTR
4548 rc = brightness_set(level);
4549 return (rc == -EINTR)? ERESTARTSYS : rc;
4552 static struct ibm_struct brightness_driver_data = {
4553 .name = "brightness",
4554 .read = brightness_read,
4555 .write = brightness_write,
4556 .exit = brightness_exit,
4559 /*************************************************************************
4563 static int volume_offset = 0x30;
4565 static int volume_read(char *p)
4570 if (!acpi_ec_read(volume_offset, &level)) {
4571 len += sprintf(p + len, "level:\t\tunreadable\n");
4573 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4574 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4575 len += sprintf(p + len, "commands:\tup, down, mute\n");
4576 len += sprintf(p + len, "commands:\tlevel <level>"
4577 " (<level> is 0-15)\n");
4583 static int volume_write(char *buf)
4585 int cmos_cmd, inc, i;
4587 int new_level, new_mute;
4590 while ((cmd = next_cmd(&buf))) {
4591 if (!acpi_ec_read(volume_offset, &level))
4593 new_mute = mute = level & 0x40;
4594 new_level = level = level & 0xf;
4596 if (strlencmp(cmd, "up") == 0) {
4600 new_level = level == 15 ? 15 : level + 1;
4601 } else if (strlencmp(cmd, "down") == 0) {
4605 new_level = level == 0 ? 0 : level - 1;
4606 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4607 new_level >= 0 && new_level <= 15) {
4609 } else if (strlencmp(cmd, "mute") == 0) {
4614 if (new_level != level) {
4615 /* mute doesn't change */
4617 cmos_cmd = (new_level > level) ?
4618 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4619 inc = new_level > level ? 1 : -1;
4621 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4622 !acpi_ec_write(volume_offset, level)))
4625 for (i = level; i != new_level; i += inc)
4626 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4627 !acpi_ec_write(volume_offset, i + inc))
4631 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4632 !acpi_ec_write(volume_offset, new_level + mute))) {
4637 if (new_mute != mute) {
4638 /* level doesn't change */
4640 cmos_cmd = (new_mute) ?
4641 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4643 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4644 !acpi_ec_write(volume_offset, level + new_mute))
4652 static struct ibm_struct volume_driver_data = {
4654 .read = volume_read,
4655 .write = volume_write,
4658 /*************************************************************************
4665 * TPACPI_FAN_RD_ACPI_GFAN:
4666 * ACPI GFAN method: returns fan level
4668 * see TPACPI_FAN_WR_ACPI_SFAN
4669 * EC 0x2f (HFSP) not available if GFAN exists
4671 * TPACPI_FAN_WR_ACPI_SFAN:
4672 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4674 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4677 * TPACPI_FAN_WR_TPEC:
4678 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4679 * Supported on almost all ThinkPads
4681 * Fan speed changes of any sort (including those caused by the
4682 * disengaged mode) are usually done slowly by the firmware as the
4683 * maximum ammount of fan duty cycle change per second seems to be
4686 * Reading is not available if GFAN exists.
4687 * Writing is not available if SFAN exists.
4690 * 7 automatic mode engaged;
4691 * (default operation mode of the ThinkPad)
4692 * fan level is ignored in this mode.
4693 * 6 full speed mode (takes precedence over bit 7);
4694 * not available on all thinkpads. May disable
4695 * the tachometer while the fan controller ramps up
4696 * the speed (which can take up to a few *minutes*).
4697 * Speeds up fan to 100% duty-cycle, which is far above
4698 * the standard RPM levels. It is not impossible that
4699 * it could cause hardware damage.
4700 * 5-3 unused in some models. Extra bits for fan level
4701 * in others, but still useless as all values above
4702 * 7 map to the same speed as level 7 in these models.
4703 * 2-0 fan level (0..7 usually)
4705 * 0x07 = max (set when temperatures critical)
4706 * Some ThinkPads may have other levels, see
4707 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4709 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4710 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4711 * does so, its initial value is meaningless (0x07).
4713 * For firmware bugs, refer to:
4714 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4718 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4719 * Main fan tachometer reading (in RPM)
4721 * This register is present on all ThinkPads with a new-style EC, and
4722 * it is known not to be present on the A21m/e, and T22, as there is
4723 * something else in offset 0x84 according to the ACPI DSDT. Other
4724 * ThinkPads from this same time period (and earlier) probably lack the
4725 * tachometer as well.
4727 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4728 * was never fixed by IBM to report the EC firmware version string
4729 * probably support the tachometer (like the early X models), so
4730 * detecting it is quite hard. We need more data to know for sure.
4732 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4735 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4738 * For firmware bugs, refer to:
4739 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4741 * TPACPI_FAN_WR_ACPI_FANS:
4742 * ThinkPad X31, X40, X41. Not available in the X60.
4744 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4745 * high speed. ACPI DSDT seems to map these three speeds to levels
4746 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4747 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4749 * The speeds are stored on handles
4750 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4752 * There are three default speed sets, acessible as handles:
4753 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4755 * ACPI DSDT switches which set is in use depending on various
4758 * TPACPI_FAN_WR_TPEC is also available and should be used to
4759 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4760 * but the ACPI tables just mention level 7.
4763 enum { /* Fan control constants */
4764 fan_status_offset = 0x2f, /* EC register 0x2f */
4765 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4766 * 0x84 must be read before 0x85 */
4768 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4769 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4771 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4774 enum fan_status_access_mode {
4775 TPACPI_FAN_NONE = 0, /* No fan status or control */
4776 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4777 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4780 enum fan_control_access_mode {
4781 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4782 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4783 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4784 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4787 enum fan_control_commands {
4788 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4789 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4790 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4791 * and also watchdog cmd */
4794 static int fan_control_allowed;
4796 static enum fan_status_access_mode fan_status_access_mode;
4797 static enum fan_control_access_mode fan_control_access_mode;
4798 static enum fan_control_commands fan_control_commands;
4800 static u8 fan_control_initial_status;
4801 static u8 fan_control_desired_level;
4802 static int fan_watchdog_maxinterval;
4804 static struct mutex fan_mutex;
4806 static void fan_watchdog_fire(struct work_struct *ignored);
4807 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4809 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4810 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4811 "\\FSPD", /* 600e/x, 770e, 770x */
4813 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4814 "JFNS", /* 770x-JL */
4818 * Call with fan_mutex held
4820 static void fan_update_desired_level(u8 status)
4823 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4825 fan_control_desired_level = 7;
4827 fan_control_desired_level = status;
4831 static int fan_get_status(u8 *status)
4836 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4838 switch (fan_status_access_mode) {
4839 case TPACPI_FAN_RD_ACPI_GFAN:
4840 /* 570, 600e/x, 770e, 770x */
4842 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4850 case TPACPI_FAN_RD_TPEC:
4851 /* all except 570, 600e/x, 770e, 770x */
4852 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4867 static int fan_get_status_safe(u8 *status)
4872 if (mutex_lock_interruptible(&fan_mutex))
4873 return -ERESTARTSYS;
4874 rc = fan_get_status(&s);
4876 fan_update_desired_level(s);
4877 mutex_unlock(&fan_mutex);
4885 static int fan_get_speed(unsigned int *speed)
4889 switch (fan_status_access_mode) {
4890 case TPACPI_FAN_RD_TPEC:
4891 /* all except 570, 600e/x, 770e, 770x */
4892 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4893 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4897 *speed = (hi << 8) | lo;
4908 static int fan_set_level(int level)
4910 if (!fan_control_allowed)
4913 switch (fan_control_access_mode) {
4914 case TPACPI_FAN_WR_ACPI_SFAN:
4915 if (level >= 0 && level <= 7) {
4916 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4922 case TPACPI_FAN_WR_ACPI_FANS:
4923 case TPACPI_FAN_WR_TPEC:
4924 if ((level != TP_EC_FAN_AUTO) &&
4925 (level != TP_EC_FAN_FULLSPEED) &&
4926 ((level < 0) || (level > 7)))
4929 /* safety net should the EC not support AUTO
4930 * or FULLSPEED mode bits and just ignore them */
4931 if (level & TP_EC_FAN_FULLSPEED)
4932 level |= 7; /* safety min speed 7 */
4933 else if (level & TP_EC_FAN_AUTO)
4934 level |= 4; /* safety min speed 4 */
4936 if (!acpi_ec_write(fan_status_offset, level))
4939 tp_features.fan_ctrl_status_undef = 0;
4948 static int fan_set_level_safe(int level)
4952 if (!fan_control_allowed)
4955 if (mutex_lock_interruptible(&fan_mutex))
4956 return -ERESTARTSYS;
4958 if (level == TPACPI_FAN_LAST_LEVEL)
4959 level = fan_control_desired_level;
4961 rc = fan_set_level(level);
4963 fan_update_desired_level(level);
4965 mutex_unlock(&fan_mutex);
4969 static int fan_set_enable(void)
4974 if (!fan_control_allowed)
4977 if (mutex_lock_interruptible(&fan_mutex))
4978 return -ERESTARTSYS;
4980 switch (fan_control_access_mode) {
4981 case TPACPI_FAN_WR_ACPI_FANS:
4982 case TPACPI_FAN_WR_TPEC:
4983 rc = fan_get_status(&s);
4987 /* Don't go out of emergency fan mode */
4990 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4993 if (!acpi_ec_write(fan_status_offset, s))
4996 tp_features.fan_ctrl_status_undef = 0;
5001 case TPACPI_FAN_WR_ACPI_SFAN:
5002 rc = fan_get_status(&s);
5008 /* Set fan to at least level 4 */
5011 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5021 mutex_unlock(&fan_mutex);
5025 static int fan_set_disable(void)
5029 if (!fan_control_allowed)
5032 if (mutex_lock_interruptible(&fan_mutex))
5033 return -ERESTARTSYS;
5036 switch (fan_control_access_mode) {
5037 case TPACPI_FAN_WR_ACPI_FANS:
5038 case TPACPI_FAN_WR_TPEC:
5039 if (!acpi_ec_write(fan_status_offset, 0x00))
5042 fan_control_desired_level = 0;
5043 tp_features.fan_ctrl_status_undef = 0;
5047 case TPACPI_FAN_WR_ACPI_SFAN:
5048 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5051 fan_control_desired_level = 0;
5059 mutex_unlock(&fan_mutex);
5063 static int fan_set_speed(int speed)
5067 if (!fan_control_allowed)
5070 if (mutex_lock_interruptible(&fan_mutex))
5071 return -ERESTARTSYS;
5074 switch (fan_control_access_mode) {
5075 case TPACPI_FAN_WR_ACPI_FANS:
5076 if (speed >= 0 && speed <= 65535) {
5077 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5078 speed, speed, speed))
5088 mutex_unlock(&fan_mutex);
5092 static void fan_watchdog_reset(void)
5094 static int fan_watchdog_active;
5096 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5099 if (fan_watchdog_active)
5100 cancel_delayed_work(&fan_watchdog_task);
5102 if (fan_watchdog_maxinterval > 0 &&
5103 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5104 fan_watchdog_active = 1;
5105 if (!schedule_delayed_work(&fan_watchdog_task,
5106 msecs_to_jiffies(fan_watchdog_maxinterval
5109 "failed to schedule the fan watchdog, "
5110 "watchdog will not trigger\n");
5113 fan_watchdog_active = 0;
5116 static void fan_watchdog_fire(struct work_struct *ignored)
5120 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5123 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5124 rc = fan_set_enable();
5126 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5127 "will try again later...\n", -rc);
5128 /* reschedule for later */
5129 fan_watchdog_reset();
5134 * SYSFS fan layout: hwmon compatible (device)
5137 * 0: "disengaged" mode
5139 * 2: native EC "auto" mode (recommended, hardware default)
5141 * pwm*: set speed in manual mode, ignored otherwise.
5142 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5145 * fan*_input: tachometer reading, RPM
5148 * SYSFS fan layout: extensions
5150 * fan_watchdog (driver):
5151 * fan watchdog interval in seconds, 0 disables (default), max 120
5154 /* sysfs fan pwm1_enable ----------------------------------------------- */
5155 static ssize_t fan_pwm1_enable_show(struct device *dev,
5156 struct device_attribute *attr,
5162 res = fan_get_status_safe(&status);
5166 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5167 if (status != fan_control_initial_status) {
5168 tp_features.fan_ctrl_status_undef = 0;
5170 /* Return most likely status. In fact, it
5171 * might be the only possible status */
5172 status = TP_EC_FAN_AUTO;
5176 if (status & TP_EC_FAN_FULLSPEED) {
5178 } else if (status & TP_EC_FAN_AUTO) {
5183 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5186 static ssize_t fan_pwm1_enable_store(struct device *dev,
5187 struct device_attribute *attr,
5188 const char *buf, size_t count)
5193 if (parse_strtoul(buf, 2, &t))
5198 level = TP_EC_FAN_FULLSPEED;
5201 level = TPACPI_FAN_LAST_LEVEL;
5204 level = TP_EC_FAN_AUTO;
5207 /* reserved for software-controlled auto mode */
5213 res = fan_set_level_safe(level);
5219 fan_watchdog_reset();
5224 static struct device_attribute dev_attr_fan_pwm1_enable =
5225 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5226 fan_pwm1_enable_show, fan_pwm1_enable_store);
5228 /* sysfs fan pwm1 ------------------------------------------------------ */
5229 static ssize_t fan_pwm1_show(struct device *dev,
5230 struct device_attribute *attr,
5236 res = fan_get_status_safe(&status);
5240 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5241 if (status != fan_control_initial_status) {
5242 tp_features.fan_ctrl_status_undef = 0;
5244 status = TP_EC_FAN_AUTO;
5249 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5250 status = fan_control_desired_level;
5255 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5258 static ssize_t fan_pwm1_store(struct device *dev,
5259 struct device_attribute *attr,
5260 const char *buf, size_t count)
5264 u8 status, newlevel;
5266 if (parse_strtoul(buf, 255, &s))
5269 /* scale down from 0-255 to 0-7 */
5270 newlevel = (s >> 5) & 0x07;
5272 if (mutex_lock_interruptible(&fan_mutex))
5273 return -ERESTARTSYS;
5275 rc = fan_get_status(&status);
5276 if (!rc && (status &
5277 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5278 rc = fan_set_level(newlevel);
5282 fan_update_desired_level(newlevel);
5283 fan_watchdog_reset();
5287 mutex_unlock(&fan_mutex);
5288 return (rc)? rc : count;
5291 static struct device_attribute dev_attr_fan_pwm1 =
5292 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5293 fan_pwm1_show, fan_pwm1_store);
5295 /* sysfs fan fan1_input ------------------------------------------------ */
5296 static ssize_t fan_fan1_input_show(struct device *dev,
5297 struct device_attribute *attr,
5303 res = fan_get_speed(&speed);
5307 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5310 static struct device_attribute dev_attr_fan_fan1_input =
5311 __ATTR(fan1_input, S_IRUGO,
5312 fan_fan1_input_show, NULL);
5314 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5315 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5318 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5321 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5322 const char *buf, size_t count)
5326 if (parse_strtoul(buf, 120, &t))
5329 if (!fan_control_allowed)
5332 fan_watchdog_maxinterval = t;
5333 fan_watchdog_reset();
5338 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5339 fan_fan_watchdog_show, fan_fan_watchdog_store);
5341 /* --------------------------------------------------------------------- */
5342 static struct attribute *fan_attributes[] = {
5343 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5344 &dev_attr_fan_fan1_input.attr,
5348 static const struct attribute_group fan_attr_group = {
5349 .attrs = fan_attributes,
5352 static int __init fan_init(struct ibm_init_struct *iibm)
5356 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5358 mutex_init(&fan_mutex);
5359 fan_status_access_mode = TPACPI_FAN_NONE;
5360 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5361 fan_control_commands = 0;
5362 fan_watchdog_maxinterval = 0;
5363 tp_features.fan_ctrl_status_undef = 0;
5364 fan_control_desired_level = 7;
5366 TPACPI_ACPIHANDLE_INIT(fans);
5367 TPACPI_ACPIHANDLE_INIT(gfan);
5368 TPACPI_ACPIHANDLE_INIT(sfan);
5371 /* 570, 600e/x, 770e, 770x */
5372 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5374 /* all other ThinkPads: note that even old-style
5375 * ThinkPad ECs supports the fan control register */
5376 if (likely(acpi_ec_read(fan_status_offset,
5377 &fan_control_initial_status))) {
5378 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5380 /* In some ThinkPads, neither the EC nor the ACPI
5381 * DSDT initialize the fan status, and it ends up
5382 * being set to 0x07 when it *could* be either
5385 * Enable for TP-1Y (T43), TP-78 (R51e),
5386 * TP-76 (R52), TP-70 (T43, R52), which are known
5388 if (fan_control_initial_status == 0x07) {
5389 switch (thinkpad_id.ec_model) {
5390 case 0x5931: /* TP-1Y */
5391 case 0x3837: /* TP-78 */
5392 case 0x3637: /* TP-76 */
5393 case 0x3037: /* TP-70 */
5394 printk(TPACPI_NOTICE
5395 "fan_init: initial fan status "
5396 "is unknown, assuming it is "
5398 tp_features.fan_ctrl_status_undef = 1;
5404 "ThinkPad ACPI EC access misbehaving, "
5405 "fan status and control unavailable\n");
5412 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5413 fan_control_commands |=
5414 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5417 /* gfan without sfan means no fan control */
5418 /* all other models implement TP EC 0x2f control */
5422 fan_control_access_mode =
5423 TPACPI_FAN_WR_ACPI_FANS;
5424 fan_control_commands |=
5425 TPACPI_FAN_CMD_SPEED |
5426 TPACPI_FAN_CMD_LEVEL |
5427 TPACPI_FAN_CMD_ENABLE;
5429 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5430 fan_control_commands |=
5431 TPACPI_FAN_CMD_LEVEL |
5432 TPACPI_FAN_CMD_ENABLE;
5437 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5438 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5439 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5440 fan_status_access_mode, fan_control_access_mode);
5442 /* fan control master switch */
5443 if (!fan_control_allowed) {
5444 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5445 fan_control_commands = 0;
5446 dbg_printk(TPACPI_DBG_INIT,
5447 "fan control features disabled by parameter\n");
5450 /* update fan_control_desired_level */
5451 if (fan_status_access_mode != TPACPI_FAN_NONE)
5452 fan_get_status_safe(NULL);
5454 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5455 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5456 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5459 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5460 &driver_attr_fan_watchdog);
5468 static void fan_exit(void)
5470 vdbg_printk(TPACPI_DBG_EXIT,
5471 "cancelling any pending fan watchdog tasks\n");
5473 /* FIXME: can we really do this unconditionally? */
5474 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5475 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5476 &driver_attr_fan_watchdog);
5478 cancel_delayed_work(&fan_watchdog_task);
5479 flush_scheduled_work();
5482 static int fan_read(char *p)
5487 unsigned int speed = 0;
5489 switch (fan_status_access_mode) {
5490 case TPACPI_FAN_RD_ACPI_GFAN:
5491 /* 570, 600e/x, 770e, 770x */
5492 rc = fan_get_status_safe(&status);
5496 len += sprintf(p + len, "status:\t\t%s\n"
5498 (status != 0) ? "enabled" : "disabled", status);
5501 case TPACPI_FAN_RD_TPEC:
5502 /* all except 570, 600e/x, 770e, 770x */
5503 rc = fan_get_status_safe(&status);
5507 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5508 if (status != fan_control_initial_status)
5509 tp_features.fan_ctrl_status_undef = 0;
5511 /* Return most likely status. In fact, it
5512 * might be the only possible status */
5513 status = TP_EC_FAN_AUTO;
5516 len += sprintf(p + len, "status:\t\t%s\n",
5517 (status != 0) ? "enabled" : "disabled");
5519 rc = fan_get_speed(&speed);
5523 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5525 if (status & TP_EC_FAN_FULLSPEED)
5526 /* Disengaged mode takes precedence */
5527 len += sprintf(p + len, "level:\t\tdisengaged\n");
5528 else if (status & TP_EC_FAN_AUTO)
5529 len += sprintf(p + len, "level:\t\tauto\n");
5531 len += sprintf(p + len, "level:\t\t%d\n", status);
5534 case TPACPI_FAN_NONE:
5536 len += sprintf(p + len, "status:\t\tnot supported\n");
5539 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5540 len += sprintf(p + len, "commands:\tlevel <level>");
5542 switch (fan_control_access_mode) {
5543 case TPACPI_FAN_WR_ACPI_SFAN:
5544 len += sprintf(p + len, " (<level> is 0-7)\n");
5548 len += sprintf(p + len, " (<level> is 0-7, "
5549 "auto, disengaged, full-speed)\n");
5554 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5555 len += sprintf(p + len, "commands:\tenable, disable\n"
5556 "commands:\twatchdog <timeout> (<timeout> "
5557 "is 0 (off), 1-120 (seconds))\n");
5559 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5560 len += sprintf(p + len, "commands:\tspeed <speed>"
5561 " (<speed> is 0-65535)\n");
5566 static int fan_write_cmd_level(const char *cmd, int *rc)
5570 if (strlencmp(cmd, "level auto") == 0)
5571 level = TP_EC_FAN_AUTO;
5572 else if ((strlencmp(cmd, "level disengaged") == 0) |
5573 (strlencmp(cmd, "level full-speed") == 0))
5574 level = TP_EC_FAN_FULLSPEED;
5575 else if (sscanf(cmd, "level %d", &level) != 1)
5578 *rc = fan_set_level_safe(level);
5580 printk(TPACPI_ERR "level command accepted for unsupported "
5581 "access mode %d", fan_control_access_mode);
5586 static int fan_write_cmd_enable(const char *cmd, int *rc)
5588 if (strlencmp(cmd, "enable") != 0)
5591 *rc = fan_set_enable();
5593 printk(TPACPI_ERR "enable command accepted for unsupported "
5594 "access mode %d", fan_control_access_mode);
5599 static int fan_write_cmd_disable(const char *cmd, int *rc)
5601 if (strlencmp(cmd, "disable") != 0)
5604 *rc = fan_set_disable();
5606 printk(TPACPI_ERR "disable command accepted for unsupported "
5607 "access mode %d", fan_control_access_mode);
5612 static int fan_write_cmd_speed(const char *cmd, int *rc)
5617 * Support speed <low> <medium> <high> ? */
5619 if (sscanf(cmd, "speed %d", &speed) != 1)
5622 *rc = fan_set_speed(speed);
5624 printk(TPACPI_ERR "speed command accepted for unsupported "
5625 "access mode %d", fan_control_access_mode);
5630 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5634 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5637 if (interval < 0 || interval > 120)
5640 fan_watchdog_maxinterval = interval;
5645 static int fan_write(char *buf)
5650 while (!rc && (cmd = next_cmd(&buf))) {
5651 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5652 fan_write_cmd_level(cmd, &rc)) &&
5653 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5654 (fan_write_cmd_enable(cmd, &rc) ||
5655 fan_write_cmd_disable(cmd, &rc) ||
5656 fan_write_cmd_watchdog(cmd, &rc))) &&
5657 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5658 fan_write_cmd_speed(cmd, &rc))
5662 fan_watchdog_reset();
5668 static struct ibm_struct fan_driver_data = {
5675 /****************************************************************************
5676 ****************************************************************************
5680 ****************************************************************************
5681 ****************************************************************************/
5683 /* sysfs name ---------------------------------------------------------- */
5684 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5685 struct device_attribute *attr,
5688 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5691 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5692 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5694 /* --------------------------------------------------------------------- */
5697 static struct proc_dir_entry *proc_dir;
5700 * Module and infrastructure proble, init and exit handling
5703 static int force_load;
5705 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5706 static const char * __init str_supported(int is_supported)
5708 static char text_unsupported[] __initdata = "not supported";
5710 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5712 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5714 static void ibm_exit(struct ibm_struct *ibm)
5716 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5718 list_del_init(&ibm->all_drivers);
5720 if (ibm->flags.acpi_notify_installed) {
5721 dbg_printk(TPACPI_DBG_EXIT,
5722 "%s: acpi_remove_notify_handler\n", ibm->name);
5724 acpi_remove_notify_handler(*ibm->acpi->handle,
5726 dispatch_acpi_notify);
5727 ibm->flags.acpi_notify_installed = 0;
5728 ibm->flags.acpi_notify_installed = 0;
5731 if (ibm->flags.proc_created) {
5732 dbg_printk(TPACPI_DBG_EXIT,
5733 "%s: remove_proc_entry\n", ibm->name);
5734 remove_proc_entry(ibm->name, proc_dir);
5735 ibm->flags.proc_created = 0;
5738 if (ibm->flags.acpi_driver_registered) {
5739 dbg_printk(TPACPI_DBG_EXIT,
5740 "%s: acpi_bus_unregister_driver\n", ibm->name);
5742 acpi_bus_unregister_driver(ibm->acpi->driver);
5743 kfree(ibm->acpi->driver);
5744 ibm->acpi->driver = NULL;
5745 ibm->flags.acpi_driver_registered = 0;
5748 if (ibm->flags.init_called && ibm->exit) {
5750 ibm->flags.init_called = 0;
5753 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5756 static int __init ibm_init(struct ibm_init_struct *iibm)
5759 struct ibm_struct *ibm = iibm->data;
5760 struct proc_dir_entry *entry;
5762 BUG_ON(ibm == NULL);
5764 INIT_LIST_HEAD(&ibm->all_drivers);
5766 if (ibm->flags.experimental && !experimental)
5769 dbg_printk(TPACPI_DBG_INIT,
5770 "probing for %s\n", ibm->name);
5773 ret = iibm->init(iibm);
5775 return 0; /* probe failed */
5779 ibm->flags.init_called = 1;
5783 if (ibm->acpi->hid) {
5784 ret = register_tpacpi_subdriver(ibm);
5789 if (ibm->acpi->notify) {
5790 ret = setup_acpi_notify(ibm);
5791 if (ret == -ENODEV) {
5792 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5802 dbg_printk(TPACPI_DBG_INIT,
5803 "%s installed\n", ibm->name);
5806 entry = create_proc_entry(ibm->name,
5807 S_IFREG | S_IRUGO | S_IWUSR,
5810 printk(TPACPI_ERR "unable to create proc entry %s\n",
5815 entry->owner = THIS_MODULE;
5817 entry->read_proc = &dispatch_procfs_read;
5819 entry->write_proc = &dispatch_procfs_write;
5820 ibm->flags.proc_created = 1;
5823 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5828 dbg_printk(TPACPI_DBG_INIT,
5829 "%s: at error exit path with result %d\n",
5833 return (ret < 0)? ret : 0;
5838 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5840 const struct dmi_device *dev = NULL;
5841 char ec_fw_string[18];
5846 memset(tp, 0, sizeof(*tp));
5848 if (dmi_name_in_vendors("IBM"))
5849 tp->vendor = PCI_VENDOR_ID_IBM;
5850 else if (dmi_name_in_vendors("LENOVO"))
5851 tp->vendor = PCI_VENDOR_ID_LENOVO;
5855 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5857 if (!tp->bios_version_str)
5859 tp->bios_model = tp->bios_version_str[0]
5860 | (tp->bios_version_str[1] << 8);
5863 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5864 * X32 or newer, all Z series; Some models must have an
5865 * up-to-date BIOS or they will not be detected.
5867 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5869 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5870 if (sscanf(dev->name,
5871 "IBM ThinkPad Embedded Controller -[%17c",
5872 ec_fw_string) == 1) {
5873 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5874 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5876 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5877 tp->ec_model = ec_fw_string[0]
5878 | (ec_fw_string[1] << 8);
5883 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5885 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5886 kfree(tp->model_str);
5887 tp->model_str = NULL;
5890 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
5894 static int __init probe_for_thinkpad(void)
5902 * Non-ancient models have better DMI tagging, but very old models
5905 is_thinkpad = (thinkpad_id.model_str != NULL);
5907 /* ec is required because many other handles are relative to it */
5908 TPACPI_ACPIHANDLE_INIT(ec);
5912 "Not yet supported ThinkPad detected!\n");
5917 * Risks a regression on very old machines, but reduces potential
5918 * false positives a damn great deal
5921 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5923 if (!is_thinkpad && !force_load)
5930 /* Module init, exit, parameters */
5932 static struct ibm_init_struct ibms_init[] __initdata = {
5934 .init = thinkpad_acpi_driver_init,
5935 .data = &thinkpad_acpi_driver_data,
5938 .init = hotkey_init,
5939 .data = &hotkey_driver_data,
5942 .init = bluetooth_init,
5943 .data = &bluetooth_driver_data,
5947 .data = &wan_driver_data,
5949 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
5952 .data = &video_driver_data,
5957 .data = &light_driver_data,
5959 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5962 .data = &dock_driver_data[0],
5966 .data = &dock_driver_data[1],
5969 #ifdef CONFIG_THINKPAD_ACPI_BAY
5972 .data = &bay_driver_data,
5977 .data = &cmos_driver_data,
5981 .data = &led_driver_data,
5985 .data = &beep_driver_data,
5988 .init = thermal_init,
5989 .data = &thermal_driver_data,
5992 .data = &ecdump_driver_data,
5995 .init = brightness_init,
5996 .data = &brightness_driver_data,
5999 .data = &volume_driver_data,
6003 .data = &fan_driver_data,
6007 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6010 struct ibm_struct *ibm;
6012 if (!kp || !kp->name || !val)
6015 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6016 ibm = ibms_init[i].data;
6017 WARN_ON(ibm == NULL);
6019 if (!ibm || !ibm->name)
6022 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6023 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6025 strcpy(ibms_init[i].param, val);
6026 strcat(ibms_init[i].param, ",");
6034 module_param(experimental, int, 0);
6035 MODULE_PARM_DESC(experimental,
6036 "Enables experimental features when non-zero");
6038 module_param_named(debug, dbg_level, uint, 0);
6039 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6041 module_param(force_load, bool, 0);
6042 MODULE_PARM_DESC(force_load,
6043 "Attempts to load the driver even on a "
6044 "mis-identified ThinkPad when true");
6046 module_param_named(fan_control, fan_control_allowed, bool, 0);
6047 MODULE_PARM_DESC(fan_control,
6048 "Enables setting fan parameters features when true");
6050 module_param_named(brightness_mode, brightness_mode, int, 0);
6051 MODULE_PARM_DESC(brightness_mode,
6052 "Selects brightness control strategy: "
6053 "0=auto, 1=EC, 2=CMOS, 3=both");
6055 module_param(brightness_enable, uint, 0);
6056 MODULE_PARM_DESC(brightness_enable,
6057 "Enables backlight control when 1, disables when 0");
6059 module_param(hotkey_report_mode, uint, 0);
6060 MODULE_PARM_DESC(hotkey_report_mode,
6061 "used for backwards compatibility with userspace, "
6062 "see documentation");
6064 #define TPACPI_PARAM(feature) \
6065 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6066 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6067 "at module load, see documentation")
6069 TPACPI_PARAM(hotkey);
6070 TPACPI_PARAM(bluetooth);
6071 TPACPI_PARAM(video);
6072 TPACPI_PARAM(light);
6073 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6076 #ifdef CONFIG_THINKPAD_ACPI_BAY
6078 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6082 TPACPI_PARAM(ecdump);
6083 TPACPI_PARAM(brightness);
6084 TPACPI_PARAM(volume);
6087 static void thinkpad_acpi_module_exit(void)
6089 struct ibm_struct *ibm, *itmp;
6091 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6093 list_for_each_entry_safe_reverse(ibm, itmp,
6094 &tpacpi_all_drivers,
6099 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6101 if (tpacpi_inputdev) {
6102 if (tp_features.input_device_registered)
6103 input_unregister_device(tpacpi_inputdev);
6105 input_free_device(tpacpi_inputdev);
6109 hwmon_device_unregister(tpacpi_hwmon);
6111 if (tp_features.sensors_pdev_attrs_registered)
6112 device_remove_file(&tpacpi_sensors_pdev->dev,
6113 &dev_attr_thinkpad_acpi_pdev_name);
6114 if (tpacpi_sensors_pdev)
6115 platform_device_unregister(tpacpi_sensors_pdev);
6117 platform_device_unregister(tpacpi_pdev);
6119 if (tp_features.sensors_pdrv_attrs_registered)
6120 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6121 if (tp_features.platform_drv_attrs_registered)
6122 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6124 if (tp_features.sensors_pdrv_registered)
6125 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6127 if (tp_features.platform_drv_registered)
6128 platform_driver_unregister(&tpacpi_pdriver);
6131 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6133 kfree(thinkpad_id.bios_version_str);
6134 kfree(thinkpad_id.ec_version_str);
6135 kfree(thinkpad_id.model_str);
6139 static int __init thinkpad_acpi_module_init(void)
6143 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6145 /* Parameter checking */
6146 if (hotkey_report_mode > 2)
6149 /* Driver-level probe */
6151 get_thinkpad_model_data(&thinkpad_id);
6152 ret = probe_for_thinkpad();
6154 thinkpad_acpi_module_exit();
6158 /* Driver initialization */
6160 TPACPI_ACPIHANDLE_INIT(ecrd);
6161 TPACPI_ACPIHANDLE_INIT(ecwr);
6163 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6166 "unable to create proc dir " TPACPI_PROC_DIR);
6167 thinkpad_acpi_module_exit();
6170 proc_dir->owner = THIS_MODULE;
6172 ret = platform_driver_register(&tpacpi_pdriver);
6175 "unable to register main platform driver\n");
6176 thinkpad_acpi_module_exit();
6179 tp_features.platform_drv_registered = 1;
6181 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6184 "unable to register hwmon platform driver\n");
6185 thinkpad_acpi_module_exit();
6188 tp_features.sensors_pdrv_registered = 1;
6190 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6192 tp_features.platform_drv_attrs_registered = 1;
6193 ret = tpacpi_create_driver_attributes(
6194 &tpacpi_hwmon_pdriver.driver);
6198 "unable to create sysfs driver attributes\n");
6199 thinkpad_acpi_module_exit();
6202 tp_features.sensors_pdrv_attrs_registered = 1;
6205 /* Device initialization */
6206 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6208 if (IS_ERR(tpacpi_pdev)) {
6209 ret = PTR_ERR(tpacpi_pdev);
6211 printk(TPACPI_ERR "unable to register platform device\n");
6212 thinkpad_acpi_module_exit();
6215 tpacpi_sensors_pdev = platform_device_register_simple(
6216 TPACPI_HWMON_DRVR_NAME,
6218 if (IS_ERR(tpacpi_sensors_pdev)) {
6219 ret = PTR_ERR(tpacpi_sensors_pdev);
6220 tpacpi_sensors_pdev = NULL;
6222 "unable to register hwmon platform device\n");
6223 thinkpad_acpi_module_exit();
6226 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6227 &dev_attr_thinkpad_acpi_pdev_name);
6230 "unable to create sysfs hwmon device attributes\n");
6231 thinkpad_acpi_module_exit();
6234 tp_features.sensors_pdev_attrs_registered = 1;
6235 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6236 if (IS_ERR(tpacpi_hwmon)) {
6237 ret = PTR_ERR(tpacpi_hwmon);
6238 tpacpi_hwmon = NULL;
6239 printk(TPACPI_ERR "unable to register hwmon device\n");
6240 thinkpad_acpi_module_exit();
6243 mutex_init(&tpacpi_inputdev_send_mutex);
6244 tpacpi_inputdev = input_allocate_device();
6245 if (!tpacpi_inputdev) {
6246 printk(TPACPI_ERR "unable to allocate input device\n");
6247 thinkpad_acpi_module_exit();
6250 /* Prepare input device, but don't register */
6251 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6252 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6253 tpacpi_inputdev->id.bustype = BUS_HOST;
6254 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6255 thinkpad_id.vendor :
6257 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6258 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6260 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6261 ret = ibm_init(&ibms_init[i]);
6262 if (ret >= 0 && *ibms_init[i].param)
6263 ret = ibms_init[i].data->write(ibms_init[i].param);
6265 thinkpad_acpi_module_exit();
6269 ret = input_register_device(tpacpi_inputdev);
6271 printk(TPACPI_ERR "unable to register input device\n");
6272 thinkpad_acpi_module_exit();
6275 tp_features.input_device_registered = 1;
6278 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6282 /* Please remove this in year 2009 */
6283 MODULE_ALIAS("ibm_acpi");
6286 * DMI matching for module autoloading
6288 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6289 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6291 * Only models listed in thinkwiki will be supported, so add yours
6292 * if it is not there yet.
6294 #define IBM_BIOS_MODULE_ALIAS(__type) \
6295 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6297 /* Non-ancient thinkpads */
6298 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6299 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6301 /* Ancient thinkpad BIOSes have to be identified by
6302 * BIOS type or model number, and there are far less
6303 * BIOS types than model numbers... */
6304 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6305 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6306 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6308 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6309 MODULE_DESCRIPTION(TPACPI_DESC);
6310 MODULE_VERSION(TPACPI_VERSION);
6311 MODULE_LICENSE("GPL");
6313 module_init(thinkpad_acpi_module_init);
6314 module_exit(thinkpad_acpi_module_exit);