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 <linux/leds.h>
71 #include <asm/uaccess.h>
73 #include <linux/dmi.h>
74 #include <linux/jiffies.h>
75 #include <linux/workqueue.h>
77 #include <acpi/acpi_drivers.h>
78 #include <acpi/acnamesp.h>
80 #include <linux/pci_ids.h>
83 /* ThinkPad CMOS commands */
84 #define TP_CMOS_VOLUME_DOWN 0
85 #define TP_CMOS_VOLUME_UP 1
86 #define TP_CMOS_VOLUME_MUTE 2
87 #define TP_CMOS_BRIGHTNESS_UP 4
88 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 #define TP_CMOS_THINKLIGHT_ON 12
90 #define TP_CMOS_THINKLIGHT_OFF 13
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
101 /* NVRAM bit masks */
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /****************************************************************************
130 #define TPACPI_NAME "thinkpad"
131 #define TPACPI_DESC "ThinkPad ACPI Extras"
132 #define TPACPI_FILE TPACPI_NAME "_acpi"
133 #define TPACPI_URL "http://ibm-acpi.sf.net/"
134 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
136 #define TPACPI_PROC_DIR "ibm"
137 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
138 #define TPACPI_DRVR_NAME TPACPI_FILE
139 #define TPACPI_DRVR_SHORTNAME "tpacpi"
140 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
142 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
144 #define TPACPI_MAX_ACPI_ARGS 3
147 #define TPACPI_LOG TPACPI_FILE ": "
148 #define TPACPI_ERR KERN_ERR TPACPI_LOG
149 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
150 #define TPACPI_INFO KERN_INFO TPACPI_LOG
151 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
153 #define TPACPI_DBG_ALL 0xffff
154 #define TPACPI_DBG_ALL 0xffff
155 #define TPACPI_DBG_INIT 0x0001
156 #define TPACPI_DBG_EXIT 0x0002
157 #define dbg_printk(a_dbg_level, format, arg...) \
158 do { if (dbg_level & a_dbg_level) \
159 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
161 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
162 #define vdbg_printk(a_dbg_level, format, arg...) \
163 dbg_printk(a_dbg_level, format, ## arg)
164 static const char *str_supported(int is_supported);
166 #define vdbg_printk(a_dbg_level, format, arg...)
169 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
170 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
171 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
174 /****************************************************************************
175 * Driver-wide structs and misc. variables
180 struct tp_acpi_drv_struct {
181 const struct acpi_device_id *hid;
182 struct acpi_driver *driver;
184 void (*notify) (struct ibm_struct *, u32);
187 struct acpi_device *device;
193 int (*read) (char *);
194 int (*write) (char *);
196 void (*resume) (void);
197 void (*suspend) (pm_message_t state);
199 struct list_head all_drivers;
201 struct tp_acpi_drv_struct *acpi;
204 u8 acpi_driver_registered:1;
205 u8 acpi_notify_installed:1;
212 struct ibm_init_struct {
215 int (*init) (struct ibm_init_struct *);
216 struct ibm_struct *data;
220 #ifdef CONFIG_THINKPAD_ACPI_BAY
233 u32 bright_16levels:1;
234 u32 bright_acpimode:1;
236 u32 fan_ctrl_status_undef:1;
237 u32 input_device_registered:1;
238 u32 platform_drv_registered:1;
239 u32 platform_drv_attrs_registered:1;
240 u32 sensors_pdrv_registered:1;
241 u32 sensors_pdrv_attrs_registered:1;
242 u32 sensors_pdev_attrs_registered:1;
243 u32 hotkey_poll_active:1;
247 u16 hotkey_mask_ff:1;
248 u16 bright_cmos_ec_unsync:1;
251 struct thinkpad_id_data {
252 unsigned int vendor; /* ThinkPad vendor:
253 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
255 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
256 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
258 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
261 char *model_str; /* ThinkPad T43 */
262 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
264 static struct thinkpad_id_data thinkpad_id;
267 TPACPI_LIFE_INIT = 0,
272 static int experimental;
273 static u32 dbg_level;
275 /* Special LED class that can defer work */
276 struct tpacpi_led_classdev {
277 struct led_classdev led_classdev;
278 struct work_struct work;
279 enum led_brightness new_brightness;
282 /****************************************************************************
283 ****************************************************************************
285 * ACPI Helpers and device model
287 ****************************************************************************
288 ****************************************************************************/
290 /*************************************************************************
294 static acpi_handle root_handle;
296 #define TPACPI_HANDLE(object, parent, paths...) \
297 static acpi_handle object##_handle; \
298 static acpi_handle *object##_parent = &parent##_handle; \
299 static char *object##_path; \
300 static char *object##_paths[] = { paths }
302 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
303 "\\_SB.PCI.ISA.EC", /* 570 */
304 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
305 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
306 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
307 "\\_SB.PCI0.ICH3.EC0", /* R31 */
308 "\\_SB.PCI0.LPC.EC", /* all others */
311 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
312 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
314 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
316 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
317 "\\CMS", /* R40, R40e */
320 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
321 "^HKEY", /* R30, R31 */
322 "HKEY", /* all others */
325 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
326 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
327 "\\_SB.PCI0.VID0", /* 770e */
328 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
329 "\\_SB.PCI0.AGP.VID", /* all others */
333 /*************************************************************************
337 static int acpi_evalf(acpi_handle handle,
338 void *res, char *method, char *fmt, ...)
341 struct acpi_object_list params;
342 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
343 struct acpi_buffer result, *resultp;
344 union acpi_object out_obj;
352 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
365 params.pointer = &in_objs[0];
372 in_objs[params.count].integer.value = va_arg(ap, int);
373 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
375 /* add more types as needed */
377 printk(TPACPI_ERR "acpi_evalf() called "
378 "with invalid format character '%c'\n", c);
384 if (res_type != 'v') {
385 result.length = sizeof(out_obj);
386 result.pointer = &out_obj;
391 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
396 *(int *)res = out_obj.integer.value;
397 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
400 success = status == AE_OK;
402 /* add more types as needed */
404 printk(TPACPI_ERR "acpi_evalf() called "
405 "with invalid format character '%c'\n", res_type);
409 if (!success && !quiet)
410 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
411 method, fmt0, status);
416 static int acpi_ec_read(int i, u8 *p)
421 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
425 if (ec_read(i, p) < 0)
432 static int acpi_ec_write(int i, u8 v)
435 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
438 if (ec_write(i, v) < 0)
445 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
446 static int _sta(acpi_handle handle)
450 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
457 static int issue_thinkpad_cmos_command(int cmos_cmd)
462 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
468 /*************************************************************************
472 #define TPACPI_ACPIHANDLE_INIT(object) \
473 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
474 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
476 static void drv_acpi_handle_init(char *name,
477 acpi_handle *handle, acpi_handle parent,
478 char **paths, int num_paths, char **path)
483 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
486 for (i = 0; i < num_paths; i++) {
487 status = acpi_get_handle(parent, paths[i], handle);
488 if (ACPI_SUCCESS(status)) {
490 dbg_printk(TPACPI_DBG_INIT,
491 "Found ACPI handle %s for %s\n",
497 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
502 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
504 struct ibm_struct *ibm = data;
506 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
509 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
512 ibm->acpi->notify(ibm, event);
515 static int __init setup_acpi_notify(struct ibm_struct *ibm)
522 if (!*ibm->acpi->handle)
525 vdbg_printk(TPACPI_DBG_INIT,
526 "setting up ACPI notify for %s\n", ibm->name);
528 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
530 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
535 acpi_driver_data(ibm->acpi->device) = ibm;
536 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
537 TPACPI_ACPI_EVENT_PREFIX,
540 status = acpi_install_notify_handler(*ibm->acpi->handle,
541 ibm->acpi->type, dispatch_acpi_notify, ibm);
542 if (ACPI_FAILURE(status)) {
543 if (status == AE_ALREADY_EXISTS) {
545 "another device driver is already "
546 "handling %s events\n", ibm->name);
549 "acpi_install_notify_handler(%s) failed: %d\n",
554 ibm->flags.acpi_notify_installed = 1;
558 static int __init tpacpi_device_add(struct acpi_device *device)
563 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
567 dbg_printk(TPACPI_DBG_INIT,
568 "registering %s as an ACPI driver\n", ibm->name);
572 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
573 if (!ibm->acpi->driver) {
574 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
578 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
579 ibm->acpi->driver->ids = ibm->acpi->hid;
581 ibm->acpi->driver->ops.add = &tpacpi_device_add;
583 rc = acpi_bus_register_driver(ibm->acpi->driver);
585 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
587 kfree(ibm->acpi->driver);
588 ibm->acpi->driver = NULL;
590 ibm->flags.acpi_driver_registered = 1;
596 /****************************************************************************
597 ****************************************************************************
601 ****************************************************************************
602 ****************************************************************************/
604 static int dispatch_procfs_read(char *page, char **start, off_t off,
605 int count, int *eof, void *data)
607 struct ibm_struct *ibm = data;
610 if (!ibm || !ibm->read)
613 len = ibm->read(page);
617 if (len <= off + count)
629 static int dispatch_procfs_write(struct file *file,
630 const char __user *userbuf,
631 unsigned long count, void *data)
633 struct ibm_struct *ibm = data;
637 if (!ibm || !ibm->write)
640 kernbuf = kmalloc(count + 2, GFP_KERNEL);
644 if (copy_from_user(kernbuf, userbuf, count)) {
650 strcat(kernbuf, ",");
651 ret = ibm->write(kernbuf);
660 static char *next_cmd(char **cmds)
665 while ((end = strchr(start, ',')) && end == start)
677 /****************************************************************************
678 ****************************************************************************
680 * Device model: input, hwmon and platform
682 ****************************************************************************
683 ****************************************************************************/
685 static struct platform_device *tpacpi_pdev;
686 static struct platform_device *tpacpi_sensors_pdev;
687 static struct device *tpacpi_hwmon;
688 static struct input_dev *tpacpi_inputdev;
689 static struct mutex tpacpi_inputdev_send_mutex;
690 static LIST_HEAD(tpacpi_all_drivers);
692 static int tpacpi_suspend_handler(struct platform_device *pdev,
695 struct ibm_struct *ibm, *itmp;
697 list_for_each_entry_safe(ibm, itmp,
701 (ibm->suspend)(state);
707 static int tpacpi_resume_handler(struct platform_device *pdev)
709 struct ibm_struct *ibm, *itmp;
711 list_for_each_entry_safe(ibm, itmp,
721 static struct platform_driver tpacpi_pdriver = {
723 .name = TPACPI_DRVR_NAME,
724 .owner = THIS_MODULE,
726 .suspend = tpacpi_suspend_handler,
727 .resume = tpacpi_resume_handler,
730 static struct platform_driver tpacpi_hwmon_pdriver = {
732 .name = TPACPI_HWMON_DRVR_NAME,
733 .owner = THIS_MODULE,
737 /*************************************************************************
738 * sysfs support helpers
741 struct attribute_set {
742 unsigned int members, max_members;
743 struct attribute_group group;
746 struct attribute_set_obj {
747 struct attribute_set s;
749 } __attribute__((packed));
751 static struct attribute_set *create_attr_set(unsigned int max_members,
754 struct attribute_set_obj *sobj;
756 if (max_members == 0)
759 /* Allocates space for implicit NULL at the end too */
760 sobj = kzalloc(sizeof(struct attribute_set_obj) +
761 max_members * sizeof(struct attribute *),
765 sobj->s.max_members = max_members;
766 sobj->s.group.attrs = &sobj->a;
767 sobj->s.group.name = name;
772 #define destroy_attr_set(_set) \
775 /* not multi-threaded safe, use it in a single thread per set */
776 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
781 if (s->members >= s->max_members)
784 s->group.attrs[s->members] = attr;
790 static int add_many_to_attr_set(struct attribute_set *s,
791 struct attribute **attr,
796 for (i = 0; i < count; i++) {
797 res = add_to_attr_set(s, attr[i]);
805 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
807 sysfs_remove_group(kobj, &s->group);
811 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
812 sysfs_create_group(_kobj, &_attr_set->group)
814 static int parse_strtoul(const char *buf,
815 unsigned long max, unsigned long *value)
819 while (*buf && isspace(*buf))
821 *value = simple_strtoul(buf, &endp, 0);
822 while (*endp && isspace(*endp))
824 if (*endp || *value > max)
830 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
832 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
833 union acpi_object *obj;
836 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
837 obj = (union acpi_object *)buffer.pointer;
838 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
839 printk(TPACPI_ERR "Unknown _BCL data, "
840 "please report this to %s\n", TPACPI_MAIL);
843 rc = obj->package.count;
849 kfree(buffer.pointer);
853 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
854 u32 lvl, void *context, void **rv)
856 char name[ACPI_PATH_SEGMENT_LENGTH];
857 struct acpi_buffer buffer = { sizeof(name), &name };
859 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
860 !strncmp("_BCL", name, sizeof(name) - 1)) {
862 **(int **)rv = tpacpi_query_bcl_levels(handle);
863 return AE_CTRL_TERMINATE;
870 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
872 static int __init tpacpi_check_std_acpi_brightness_support(void)
876 void *bcl_ptr = &bcl_levels;
879 TPACPI_ACPIHANDLE_INIT(vid);
885 * Search for a _BCL method, and execute it. This is safe on all
886 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
887 * BIOS in ACPI backlight control mode. We do NOT have to care
888 * about calling the _BCL method in an enabled video device, any
889 * will do for our purposes.
892 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
893 tpacpi_acpi_walk_find_bcl, NULL,
896 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
897 tp_features.bright_acpimode = 1;
898 return (bcl_levels - 2);
904 /*************************************************************************
905 * thinkpad-acpi driver attributes
908 /* interface_version --------------------------------------------------- */
909 static ssize_t tpacpi_driver_interface_version_show(
910 struct device_driver *drv,
913 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
916 static DRIVER_ATTR(interface_version, S_IRUGO,
917 tpacpi_driver_interface_version_show, NULL);
919 /* debug_level --------------------------------------------------------- */
920 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
923 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
926 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
927 const char *buf, size_t count)
931 if (parse_strtoul(buf, 0xffff, &t))
939 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
940 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
942 /* version ------------------------------------------------------------- */
943 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
946 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
947 TPACPI_DESC, TPACPI_VERSION);
950 static DRIVER_ATTR(version, S_IRUGO,
951 tpacpi_driver_version_show, NULL);
953 /* --------------------------------------------------------------------- */
955 static struct driver_attribute *tpacpi_driver_attributes[] = {
956 &driver_attr_debug_level, &driver_attr_version,
957 &driver_attr_interface_version,
960 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
966 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
967 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
974 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
978 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
979 driver_remove_file(drv, tpacpi_driver_attributes[i]);
982 /****************************************************************************
983 ****************************************************************************
987 ****************************************************************************
988 ****************************************************************************/
990 /*************************************************************************
991 * thinkpad-acpi init subdriver
994 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
996 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
997 printk(TPACPI_INFO "%s\n", TPACPI_URL);
999 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1000 (thinkpad_id.bios_version_str) ?
1001 thinkpad_id.bios_version_str : "unknown",
1002 (thinkpad_id.ec_version_str) ?
1003 thinkpad_id.ec_version_str : "unknown");
1005 if (thinkpad_id.vendor && thinkpad_id.model_str)
1006 printk(TPACPI_INFO "%s %s, model %s\n",
1007 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1008 "IBM" : ((thinkpad_id.vendor ==
1009 PCI_VENDOR_ID_LENOVO) ?
1010 "Lenovo" : "Unknown vendor"),
1011 thinkpad_id.model_str,
1012 (thinkpad_id.nummodel_str) ?
1013 thinkpad_id.nummodel_str : "unknown");
1018 static int thinkpad_acpi_driver_read(char *p)
1022 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1023 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1028 static struct ibm_struct thinkpad_acpi_driver_data = {
1030 .read = thinkpad_acpi_driver_read,
1033 /*************************************************************************
1037 enum { /* hot key scan codes (derived from ACPI DSDT) */
1038 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1039 TP_ACPI_HOTKEYSCAN_FNF2,
1040 TP_ACPI_HOTKEYSCAN_FNF3,
1041 TP_ACPI_HOTKEYSCAN_FNF4,
1042 TP_ACPI_HOTKEYSCAN_FNF5,
1043 TP_ACPI_HOTKEYSCAN_FNF6,
1044 TP_ACPI_HOTKEYSCAN_FNF7,
1045 TP_ACPI_HOTKEYSCAN_FNF8,
1046 TP_ACPI_HOTKEYSCAN_FNF9,
1047 TP_ACPI_HOTKEYSCAN_FNF10,
1048 TP_ACPI_HOTKEYSCAN_FNF11,
1049 TP_ACPI_HOTKEYSCAN_FNF12,
1050 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1051 TP_ACPI_HOTKEYSCAN_FNINSERT,
1052 TP_ACPI_HOTKEYSCAN_FNDELETE,
1053 TP_ACPI_HOTKEYSCAN_FNHOME,
1054 TP_ACPI_HOTKEYSCAN_FNEND,
1055 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1056 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1057 TP_ACPI_HOTKEYSCAN_FNSPACE,
1058 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1059 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1060 TP_ACPI_HOTKEYSCAN_MUTE,
1061 TP_ACPI_HOTKEYSCAN_THINKPAD,
1064 enum { /* Keys available through NVRAM polling */
1065 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1066 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1069 enum { /* Positions of some of the keys in hotkey masks */
1070 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1071 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1072 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1073 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1074 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1075 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1076 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1077 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1078 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1079 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1080 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1083 enum { /* NVRAM to ACPI HKEY group map */
1084 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1085 TP_ACPI_HKEY_ZOOM_MASK |
1086 TP_ACPI_HKEY_DISPSWTCH_MASK |
1087 TP_ACPI_HKEY_HIBERNATE_MASK,
1088 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1089 TP_ACPI_HKEY_BRGHTDWN_MASK,
1090 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1091 TP_ACPI_HKEY_VOLDWN_MASK |
1092 TP_ACPI_HKEY_MUTE_MASK,
1095 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1096 struct tp_nvram_state {
1097 u16 thinkpad_toggle:1;
1099 u16 display_toggle:1;
1100 u16 thinklight_toggle:1;
1101 u16 hibernate_toggle:1;
1102 u16 displayexp_toggle:1;
1103 u16 display_state:1;
1104 u16 brightness_toggle:1;
1105 u16 volume_toggle:1;
1108 u8 brightness_level;
1112 static struct task_struct *tpacpi_hotkey_task;
1113 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1114 static int hotkey_poll_freq = 10; /* Hz */
1115 static struct mutex hotkey_thread_mutex;
1116 static struct mutex hotkey_thread_data_mutex;
1117 static unsigned int hotkey_config_change;
1119 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1121 #define hotkey_source_mask 0U
1123 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1125 static struct mutex hotkey_mutex;
1127 static enum { /* Reasons for waking up */
1128 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1129 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1130 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1131 } hotkey_wakeup_reason;
1133 static int hotkey_autosleep_ack;
1135 static int hotkey_orig_status;
1136 static u32 hotkey_orig_mask;
1137 static u32 hotkey_all_mask;
1138 static u32 hotkey_reserved_mask;
1139 static u32 hotkey_mask;
1141 static unsigned int hotkey_report_mode;
1143 static u16 *hotkey_keycode_map;
1145 static struct attribute_set *hotkey_dev_attributes;
1147 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1148 #define HOTKEY_CONFIG_CRITICAL_START \
1150 mutex_lock(&hotkey_thread_data_mutex); \
1151 hotkey_config_change++; \
1153 #define HOTKEY_CONFIG_CRITICAL_END \
1154 mutex_unlock(&hotkey_thread_data_mutex);
1156 #define HOTKEY_CONFIG_CRITICAL_START
1157 #define HOTKEY_CONFIG_CRITICAL_END
1158 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1160 /* HKEY.MHKG() return bits */
1161 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1163 static int hotkey_get_wlsw(int *status)
1165 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1170 static int hotkey_get_tablet_mode(int *status)
1174 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1177 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1182 * Call with hotkey_mutex held
1184 static int hotkey_mask_get(void)
1188 if (tp_features.hotkey_mask) {
1189 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1192 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1198 * Call with hotkey_mutex held
1200 static int hotkey_mask_set(u32 mask)
1205 if (tp_features.hotkey_mask) {
1206 if (!tp_warned.hotkey_mask_ff &&
1207 (mask == 0xffff || mask == 0xffffff ||
1208 mask == 0xffffffff)) {
1209 tp_warned.hotkey_mask_ff = 1;
1210 printk(TPACPI_NOTICE
1211 "setting the hotkey mask to 0x%08x is likely "
1212 "not the best way to go about it\n", mask);
1213 printk(TPACPI_NOTICE
1214 "please consider using the driver defaults, "
1215 "and refer to up-to-date thinkpad-acpi "
1219 HOTKEY_CONFIG_CRITICAL_START
1220 for (i = 0; i < 32; i++) {
1222 /* enable in firmware mask only keys not in NVRAM
1223 * mode, but enable the key in the cached hotkey_mask
1224 * regardless of mode, or the key will end up
1225 * disabled by hotkey_mask_get() */
1226 if (!acpi_evalf(hkey_handle,
1227 NULL, "MHKM", "vdd", i + 1,
1228 !!((mask & ~hotkey_source_mask) & m))) {
1232 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1235 HOTKEY_CONFIG_CRITICAL_END
1237 /* hotkey_mask_get must be called unconditionally below */
1238 if (!hotkey_mask_get() && !rc &&
1239 (hotkey_mask & ~hotkey_source_mask) !=
1240 (mask & ~hotkey_source_mask)) {
1241 printk(TPACPI_NOTICE
1242 "requested hot key mask 0x%08x, but "
1243 "firmware forced it to 0x%08x\n",
1247 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1248 HOTKEY_CONFIG_CRITICAL_START
1249 hotkey_mask = mask & hotkey_source_mask;
1250 HOTKEY_CONFIG_CRITICAL_END
1252 if (hotkey_mask != mask) {
1253 printk(TPACPI_NOTICE
1254 "requested hot key mask 0x%08x, "
1255 "forced to 0x%08x (NVRAM poll mask is "
1256 "0x%08x): no firmware mask support\n",
1257 mask, hotkey_mask, hotkey_source_mask);
1262 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1268 static int hotkey_status_get(int *status)
1270 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1276 static int hotkey_status_set(int status)
1278 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1284 static void tpacpi_input_send_radiosw(void)
1288 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1289 mutex_lock(&tpacpi_inputdev_send_mutex);
1291 input_report_switch(tpacpi_inputdev,
1293 input_sync(tpacpi_inputdev);
1295 mutex_unlock(&tpacpi_inputdev_send_mutex);
1299 static void tpacpi_input_send_tabletsw(void)
1303 if (tp_features.hotkey_tablet &&
1304 !hotkey_get_tablet_mode(&state)) {
1305 mutex_lock(&tpacpi_inputdev_send_mutex);
1307 input_report_switch(tpacpi_inputdev,
1308 SW_TABLET_MODE, !!state);
1309 input_sync(tpacpi_inputdev);
1311 mutex_unlock(&tpacpi_inputdev_send_mutex);
1315 static void tpacpi_input_send_key(unsigned int scancode)
1317 unsigned int keycode;
1319 keycode = hotkey_keycode_map[scancode];
1321 if (keycode != KEY_RESERVED) {
1322 mutex_lock(&tpacpi_inputdev_send_mutex);
1324 input_report_key(tpacpi_inputdev, keycode, 1);
1325 if (keycode == KEY_UNKNOWN)
1326 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1328 input_sync(tpacpi_inputdev);
1330 input_report_key(tpacpi_inputdev, keycode, 0);
1331 if (keycode == KEY_UNKNOWN)
1332 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1334 input_sync(tpacpi_inputdev);
1336 mutex_unlock(&tpacpi_inputdev_send_mutex);
1340 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1341 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1343 static void tpacpi_hotkey_send_key(unsigned int scancode)
1345 tpacpi_input_send_key(scancode);
1346 if (hotkey_report_mode < 2) {
1347 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1348 0x80, 0x1001 + scancode);
1352 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1356 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1357 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1358 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1359 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1360 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1361 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1363 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1364 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1365 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1367 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1368 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1369 n->displayexp_toggle =
1370 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1372 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1373 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1374 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1375 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1376 n->brightness_toggle =
1377 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1379 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1380 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1381 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1382 >> TP_NVRAM_POS_LEVEL_VOLUME;
1383 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1384 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1388 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1390 if ((mask & (1 << __scancode)) && \
1391 oldn->__member != newn->__member) \
1392 tpacpi_hotkey_send_key(__scancode); \
1395 #define TPACPI_MAY_SEND_KEY(__scancode) \
1396 do { if (mask & (1 << __scancode)) \
1397 tpacpi_hotkey_send_key(__scancode); } while (0)
1399 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1400 struct tp_nvram_state *newn,
1403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1413 if (oldn->volume_toggle != newn->volume_toggle) {
1414 if (oldn->mute != newn->mute) {
1415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1417 if (oldn->volume_level > newn->volume_level) {
1418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1419 } else if (oldn->volume_level < newn->volume_level) {
1420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1421 } else if (oldn->mute == newn->mute) {
1422 /* repeated key presses that didn't change state */
1424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1425 } else if (newn->volume_level != 0) {
1426 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1433 /* handle brightness */
1434 if (oldn->brightness_toggle != newn->brightness_toggle) {
1435 if (oldn->brightness_level < newn->brightness_level) {
1436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1437 } else if (oldn->brightness_level > newn->brightness_level) {
1438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1440 /* repeated key presses that didn't change state */
1441 if (newn->brightness_level != 0) {
1442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1450 #undef TPACPI_COMPARE_KEY
1451 #undef TPACPI_MAY_SEND_KEY
1453 static int hotkey_kthread(void *data)
1455 struct tp_nvram_state s[2];
1457 unsigned int si, so;
1459 unsigned int change_detector, must_reset;
1461 mutex_lock(&hotkey_thread_mutex);
1463 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1472 /* Initial state for compares */
1473 mutex_lock(&hotkey_thread_data_mutex);
1474 change_detector = hotkey_config_change;
1475 mask = hotkey_source_mask & hotkey_mask;
1476 mutex_unlock(&hotkey_thread_data_mutex);
1477 hotkey_read_nvram(&s[so], mask);
1479 while (!kthread_should_stop() && hotkey_poll_freq) {
1481 t = 1000/hotkey_poll_freq;
1482 t = msleep_interruptible(t);
1483 if (unlikely(kthread_should_stop()))
1485 must_reset = try_to_freeze();
1486 if (t > 0 && !must_reset)
1489 mutex_lock(&hotkey_thread_data_mutex);
1490 if (must_reset || hotkey_config_change != change_detector) {
1491 /* forget old state on thaw or config change */
1494 change_detector = hotkey_config_change;
1496 mask = hotkey_source_mask & hotkey_mask;
1497 mutex_unlock(&hotkey_thread_data_mutex);
1500 hotkey_read_nvram(&s[si], mask);
1501 if (likely(si != so)) {
1502 hotkey_compare_and_issue_event(&s[so], &s[si],
1512 mutex_unlock(&hotkey_thread_mutex);
1516 static void hotkey_poll_stop_sync(void)
1518 if (tpacpi_hotkey_task) {
1519 if (frozen(tpacpi_hotkey_task) ||
1520 freezing(tpacpi_hotkey_task))
1521 thaw_process(tpacpi_hotkey_task);
1523 kthread_stop(tpacpi_hotkey_task);
1524 tpacpi_hotkey_task = NULL;
1525 mutex_lock(&hotkey_thread_mutex);
1526 /* at this point, the thread did exit */
1527 mutex_unlock(&hotkey_thread_mutex);
1531 /* call with hotkey_mutex held */
1532 static void hotkey_poll_setup(int may_warn)
1534 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1535 hotkey_poll_freq > 0 &&
1536 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1537 if (!tpacpi_hotkey_task) {
1538 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1539 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1540 if (IS_ERR(tpacpi_hotkey_task)) {
1541 tpacpi_hotkey_task = NULL;
1543 "could not create kernel thread "
1544 "for hotkey polling\n");
1548 hotkey_poll_stop_sync();
1550 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1551 printk(TPACPI_NOTICE
1552 "hot keys 0x%08x require polling, "
1553 "which is currently disabled\n",
1554 hotkey_source_mask);
1559 static void hotkey_poll_setup_safe(int may_warn)
1561 mutex_lock(&hotkey_mutex);
1562 hotkey_poll_setup(may_warn);
1563 mutex_unlock(&hotkey_mutex);
1566 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1568 static void hotkey_poll_setup_safe(int __unused)
1572 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1574 static int hotkey_inputdev_open(struct input_dev *dev)
1576 switch (tpacpi_lifecycle) {
1577 case TPACPI_LIFE_INIT:
1579 * hotkey_init will call hotkey_poll_setup_safe
1580 * at the appropriate moment
1583 case TPACPI_LIFE_EXITING:
1585 case TPACPI_LIFE_RUNNING:
1586 hotkey_poll_setup_safe(0);
1590 /* Should only happen if tpacpi_lifecycle is corrupt */
1595 static void hotkey_inputdev_close(struct input_dev *dev)
1597 /* disable hotkey polling when possible */
1598 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1599 hotkey_poll_setup_safe(0);
1602 /* sysfs hotkey enable ------------------------------------------------- */
1603 static ssize_t hotkey_enable_show(struct device *dev,
1604 struct device_attribute *attr,
1609 res = hotkey_status_get(&status);
1613 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1616 static ssize_t hotkey_enable_store(struct device *dev,
1617 struct device_attribute *attr,
1618 const char *buf, size_t count)
1623 if (parse_strtoul(buf, 1, &t))
1626 res = hotkey_status_set(t);
1628 return (res) ? res : count;
1631 static struct device_attribute dev_attr_hotkey_enable =
1632 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1633 hotkey_enable_show, hotkey_enable_store);
1635 /* sysfs hotkey mask --------------------------------------------------- */
1636 static ssize_t hotkey_mask_show(struct device *dev,
1637 struct device_attribute *attr,
1642 if (mutex_lock_interruptible(&hotkey_mutex))
1643 return -ERESTARTSYS;
1644 res = hotkey_mask_get();
1645 mutex_unlock(&hotkey_mutex);
1648 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1651 static ssize_t hotkey_mask_store(struct device *dev,
1652 struct device_attribute *attr,
1653 const char *buf, size_t count)
1658 if (parse_strtoul(buf, 0xffffffffUL, &t))
1661 if (mutex_lock_interruptible(&hotkey_mutex))
1662 return -ERESTARTSYS;
1664 res = hotkey_mask_set(t);
1666 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1667 hotkey_poll_setup(1);
1670 mutex_unlock(&hotkey_mutex);
1672 return (res) ? res : count;
1675 static struct device_attribute dev_attr_hotkey_mask =
1676 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1677 hotkey_mask_show, hotkey_mask_store);
1679 /* sysfs hotkey bios_enabled ------------------------------------------- */
1680 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1681 struct device_attribute *attr,
1684 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1687 static struct device_attribute dev_attr_hotkey_bios_enabled =
1688 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1690 /* sysfs hotkey bios_mask ---------------------------------------------- */
1691 static ssize_t hotkey_bios_mask_show(struct device *dev,
1692 struct device_attribute *attr,
1695 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1698 static struct device_attribute dev_attr_hotkey_bios_mask =
1699 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1701 /* sysfs hotkey all_mask ----------------------------------------------- */
1702 static ssize_t hotkey_all_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);
1710 static struct device_attribute dev_attr_hotkey_all_mask =
1711 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1713 /* sysfs hotkey recommended_mask --------------------------------------- */
1714 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1715 struct device_attribute *attr,
1718 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1719 (hotkey_all_mask | hotkey_source_mask)
1720 & ~hotkey_reserved_mask);
1723 static struct device_attribute dev_attr_hotkey_recommended_mask =
1724 __ATTR(hotkey_recommended_mask, S_IRUGO,
1725 hotkey_recommended_mask_show, NULL);
1727 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1729 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1730 static ssize_t hotkey_source_mask_show(struct device *dev,
1731 struct device_attribute *attr,
1734 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1737 static ssize_t hotkey_source_mask_store(struct device *dev,
1738 struct device_attribute *attr,
1739 const char *buf, size_t count)
1743 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1744 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1747 if (mutex_lock_interruptible(&hotkey_mutex))
1748 return -ERESTARTSYS;
1750 HOTKEY_CONFIG_CRITICAL_START
1751 hotkey_source_mask = t;
1752 HOTKEY_CONFIG_CRITICAL_END
1754 hotkey_poll_setup(1);
1756 mutex_unlock(&hotkey_mutex);
1761 static struct device_attribute dev_attr_hotkey_source_mask =
1762 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1763 hotkey_source_mask_show, hotkey_source_mask_store);
1765 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1766 static ssize_t hotkey_poll_freq_show(struct device *dev,
1767 struct device_attribute *attr,
1770 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1773 static ssize_t hotkey_poll_freq_store(struct device *dev,
1774 struct device_attribute *attr,
1775 const char *buf, size_t count)
1779 if (parse_strtoul(buf, 25, &t))
1782 if (mutex_lock_interruptible(&hotkey_mutex))
1783 return -ERESTARTSYS;
1785 hotkey_poll_freq = t;
1787 hotkey_poll_setup(1);
1788 mutex_unlock(&hotkey_mutex);
1793 static struct device_attribute dev_attr_hotkey_poll_freq =
1794 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1795 hotkey_poll_freq_show, hotkey_poll_freq_store);
1797 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1799 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1800 static ssize_t hotkey_radio_sw_show(struct device *dev,
1801 struct device_attribute *attr,
1805 res = hotkey_get_wlsw(&s);
1809 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1812 static struct device_attribute dev_attr_hotkey_radio_sw =
1813 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1815 static void hotkey_radio_sw_notify_change(void)
1817 if (tp_features.hotkey_wlsw)
1818 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1822 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1823 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1824 struct device_attribute *attr,
1828 res = hotkey_get_tablet_mode(&s);
1832 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1835 static struct device_attribute dev_attr_hotkey_tablet_mode =
1836 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1838 static void hotkey_tablet_mode_notify_change(void)
1840 if (tp_features.hotkey_tablet)
1841 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1842 "hotkey_tablet_mode");
1845 /* sysfs hotkey report_mode -------------------------------------------- */
1846 static ssize_t hotkey_report_mode_show(struct device *dev,
1847 struct device_attribute *attr,
1850 return snprintf(buf, PAGE_SIZE, "%d\n",
1851 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1854 static struct device_attribute dev_attr_hotkey_report_mode =
1855 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1857 /* sysfs wakeup reason (pollable) -------------------------------------- */
1858 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1859 struct device_attribute *attr,
1862 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1865 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1866 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1868 static void hotkey_wakeup_reason_notify_change(void)
1870 if (tp_features.hotkey_mask)
1871 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1875 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1876 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1877 struct device_attribute *attr,
1880 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1883 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1884 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1885 hotkey_wakeup_hotunplug_complete_show, NULL);
1887 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1889 if (tp_features.hotkey_mask)
1890 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1891 "wakeup_hotunplug_complete");
1894 /* --------------------------------------------------------------------- */
1896 static struct attribute *hotkey_attributes[] __initdata = {
1897 &dev_attr_hotkey_enable.attr,
1898 &dev_attr_hotkey_bios_enabled.attr,
1899 &dev_attr_hotkey_report_mode.attr,
1900 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1901 &dev_attr_hotkey_mask.attr,
1902 &dev_attr_hotkey_all_mask.attr,
1903 &dev_attr_hotkey_recommended_mask.attr,
1904 &dev_attr_hotkey_source_mask.attr,
1905 &dev_attr_hotkey_poll_freq.attr,
1909 static struct attribute *hotkey_mask_attributes[] __initdata = {
1910 &dev_attr_hotkey_bios_mask.attr,
1911 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1912 &dev_attr_hotkey_mask.attr,
1913 &dev_attr_hotkey_all_mask.attr,
1914 &dev_attr_hotkey_recommended_mask.attr,
1916 &dev_attr_hotkey_wakeup_reason.attr,
1917 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1920 static int __init hotkey_init(struct ibm_init_struct *iibm)
1922 /* Requirements for changing the default keymaps:
1924 * 1. Many of the keys are mapped to KEY_RESERVED for very
1925 * good reasons. Do not change them unless you have deep
1926 * knowledge on the IBM and Lenovo ThinkPad firmware for
1927 * the various ThinkPad models. The driver behaves
1928 * differently for KEY_RESERVED: such keys have their
1929 * hot key mask *unset* in mask_recommended, and also
1930 * in the initial hot key mask programmed into the
1931 * firmware at driver load time, which means the firm-
1932 * ware may react very differently if you change them to
1935 * 2. You must be subscribed to the linux-thinkpad and
1936 * ibm-acpi-devel mailing lists, and you should read the
1937 * list archives since 2007 if you want to change the
1938 * keymaps. This requirement exists so that you will
1939 * know the past history of problems with the thinkpad-
1940 * acpi driver keymaps, and also that you will be
1941 * listening to any bug reports;
1943 * 3. Do not send thinkpad-acpi specific patches directly to
1944 * for merging, *ever*. Send them to the linux-acpi
1945 * mailinglist for comments. Merging is to be done only
1946 * through acpi-test and the ACPI maintainer.
1948 * If the above is too much to ask, don't change the keymap.
1949 * Ask the thinkpad-acpi maintainer to do it, instead.
1951 static u16 ibm_keycode_map[] __initdata = {
1952 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1953 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1954 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1955 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1957 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1958 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1959 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1960 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1962 /* brightness: firmware always reacts to them, unless
1963 * X.org did some tricks in the radeon BIOS scratch
1964 * registers of *some* models */
1965 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1966 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1968 /* Thinklight: firmware always react to it */
1969 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1971 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1972 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1974 /* Volume: firmware always react to it and reprograms
1975 * the built-in *extra* mixer. Never map it to control
1976 * another mixer by default. */
1977 KEY_RESERVED, /* 0x14: VOLUME UP */
1978 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1979 KEY_RESERVED, /* 0x16: MUTE */
1981 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1983 /* (assignments unknown, please report if found) */
1984 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1985 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1987 static u16 lenovo_keycode_map[] __initdata = {
1988 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1989 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1990 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1991 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1993 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1994 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1995 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1996 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1998 /* These either have to go through ACPI video, or
1999 * act like in the IBM ThinkPads, so don't ever
2000 * enable them by default */
2001 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2002 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2004 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2006 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2007 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2009 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2010 * react to it and reprograms the built-in *extra* mixer.
2011 * Never map it to control another mixer by default.
2013 * T60?, T61, R60?, R61: firmware and EC tries to send
2014 * these over the regular keyboard, so these are no-ops,
2015 * but there are still weird bugs re. MUTE, so do not
2016 * change unless you get test reports from all Lenovo
2017 * models. May cause the BIOS to interfere with the
2020 KEY_RESERVED, /* 0x14: VOLUME UP */
2021 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2022 KEY_RESERVED, /* 0x16: MUTE */
2024 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2026 /* (assignments unknown, please report if found) */
2027 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2028 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2031 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2032 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2033 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2039 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2041 BUG_ON(!tpacpi_inputdev);
2042 BUG_ON(tpacpi_inputdev->open != NULL ||
2043 tpacpi_inputdev->close != NULL);
2045 TPACPI_ACPIHANDLE_INIT(hkey);
2046 mutex_init(&hotkey_mutex);
2048 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2049 mutex_init(&hotkey_thread_mutex);
2050 mutex_init(&hotkey_thread_data_mutex);
2053 /* hotkey not supported on 570 */
2054 tp_features.hotkey = hkey_handle != NULL;
2056 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2057 str_supported(tp_features.hotkey));
2059 if (tp_features.hotkey) {
2060 hotkey_dev_attributes = create_attr_set(13, NULL);
2061 if (!hotkey_dev_attributes)
2063 res = add_many_to_attr_set(hotkey_dev_attributes,
2065 ARRAY_SIZE(hotkey_attributes));
2069 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2070 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2071 for HKEY interface version 0x100 */
2072 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2073 if ((hkeyv >> 8) != 1) {
2074 printk(TPACPI_ERR "unknown version of the "
2075 "HKEY interface: 0x%x\n", hkeyv);
2076 printk(TPACPI_ERR "please report this to %s\n",
2080 * MHKV 0x100 in A31, R40, R40e,
2081 * T4x, X31, and later
2083 tp_features.hotkey_mask = 1;
2087 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2088 str_supported(tp_features.hotkey_mask));
2090 if (tp_features.hotkey_mask) {
2091 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2094 "missing MHKA handler, "
2095 "please report this to %s\n",
2097 /* FN+F12, FN+F4, FN+F3 */
2098 hotkey_all_mask = 0x080cU;
2102 /* hotkey_source_mask *must* be zero for
2103 * the first hotkey_mask_get */
2104 res = hotkey_status_get(&hotkey_orig_status);
2105 if (!res && tp_features.hotkey_mask) {
2106 res = hotkey_mask_get();
2107 hotkey_orig_mask = hotkey_mask;
2109 res = add_many_to_attr_set(
2110 hotkey_dev_attributes,
2111 hotkey_mask_attributes,
2112 ARRAY_SIZE(hotkey_mask_attributes));
2116 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2117 if (tp_features.hotkey_mask) {
2118 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2121 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2124 vdbg_printk(TPACPI_DBG_INIT,
2125 "hotkey source mask 0x%08x, polling freq %d\n",
2126 hotkey_source_mask, hotkey_poll_freq);
2129 /* Not all thinkpads have a hardware radio switch */
2130 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2131 tp_features.hotkey_wlsw = 1;
2133 "radio switch found; radios are %s\n",
2134 enabled(status, 0));
2135 res = add_to_attr_set(hotkey_dev_attributes,
2136 &dev_attr_hotkey_radio_sw.attr);
2139 /* For X41t, X60t, X61t Tablets... */
2140 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2141 tp_features.hotkey_tablet = 1;
2143 "possible tablet mode switch found; "
2144 "ThinkPad in %s mode\n",
2145 (status & TP_HOTKEY_TABLET_MASK)?
2146 "tablet" : "laptop");
2147 res = add_to_attr_set(hotkey_dev_attributes,
2148 &dev_attr_hotkey_tablet_mode.attr);
2152 res = register_attr_set_with_sysfs(
2153 hotkey_dev_attributes,
2154 &tpacpi_pdev->dev.kobj);
2158 /* Set up key map */
2160 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2162 if (!hotkey_keycode_map) {
2164 "failed to allocate memory for key map\n");
2168 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2169 dbg_printk(TPACPI_DBG_INIT,
2170 "using Lenovo default hot key map\n");
2171 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2172 TPACPI_HOTKEY_MAP_SIZE);
2174 dbg_printk(TPACPI_DBG_INIT,
2175 "using IBM default hot key map\n");
2176 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2177 TPACPI_HOTKEY_MAP_SIZE);
2180 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2181 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2182 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2183 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2184 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2185 tpacpi_inputdev->keycode = hotkey_keycode_map;
2186 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2187 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2188 set_bit(hotkey_keycode_map[i],
2189 tpacpi_inputdev->keybit);
2191 if (i < sizeof(hotkey_reserved_mask)*8)
2192 hotkey_reserved_mask |= 1 << i;
2196 if (tp_features.hotkey_wlsw) {
2197 set_bit(EV_SW, tpacpi_inputdev->evbit);
2198 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2200 if (tp_features.hotkey_tablet) {
2201 set_bit(EV_SW, tpacpi_inputdev->evbit);
2202 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2205 /* Do not issue duplicate brightness change events to
2207 if (!tp_features.bright_acpimode)
2208 /* update bright_acpimode... */
2209 tpacpi_check_std_acpi_brightness_support();
2211 if (tp_features.bright_acpimode) {
2213 "This ThinkPad has standard ACPI backlight "
2214 "brightness control, supported by the ACPI "
2216 printk(TPACPI_NOTICE
2217 "Disabling thinkpad-acpi brightness events "
2220 /* The hotkey_reserved_mask change below is not
2221 * necessary while the keys are at KEY_RESERVED in the
2222 * default map, but better safe than sorry, leave it
2223 * here as a marker of what we have to do, especially
2224 * when we finally become able to set this at runtime
2225 * on response to X.org requests */
2226 hotkey_reserved_mask |=
2227 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2228 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2231 dbg_printk(TPACPI_DBG_INIT,
2232 "enabling hot key handling\n");
2233 res = hotkey_status_set(1);
2236 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2237 & ~hotkey_reserved_mask)
2238 | hotkey_orig_mask);
2239 if (res < 0 && res != -ENXIO)
2242 dbg_printk(TPACPI_DBG_INIT,
2243 "legacy hot key reporting over procfs %s\n",
2244 (hotkey_report_mode < 2) ?
2245 "enabled" : "disabled");
2247 tpacpi_inputdev->open = &hotkey_inputdev_open;
2248 tpacpi_inputdev->close = &hotkey_inputdev_close;
2250 hotkey_poll_setup_safe(1);
2251 tpacpi_input_send_radiosw();
2252 tpacpi_input_send_tabletsw();
2255 return (tp_features.hotkey)? 0 : 1;
2258 static void hotkey_exit(void)
2260 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2261 hotkey_poll_stop_sync();
2264 if (tp_features.hotkey) {
2265 dbg_printk(TPACPI_DBG_EXIT,
2266 "restoring original hot key mask\n");
2267 /* no short-circuit boolean operator below! */
2268 if ((hotkey_mask_set(hotkey_orig_mask) |
2269 hotkey_status_set(hotkey_orig_status)) != 0)
2271 "failed to restore hot key mask "
2272 "to BIOS defaults\n");
2275 if (hotkey_dev_attributes) {
2276 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2277 hotkey_dev_attributes = NULL;
2281 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2284 unsigned int scancode;
2289 if (event != 0x80) {
2291 "unknown HKEY notification event %d\n", event);
2292 /* forward it to userspace, maybe it knows how to handle it */
2293 acpi_bus_generate_netlink_event(
2294 ibm->acpi->device->pnp.device_class,
2295 ibm->acpi->device->dev.bus_id,
2301 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2302 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2315 switch (hkey >> 12) {
2317 /* 0x1000-0x1FFF: key presses */
2318 scancode = hkey & 0xfff;
2319 if (scancode > 0 && scancode < 0x21) {
2321 if (!(hotkey_source_mask & (1 << scancode))) {
2322 tpacpi_input_send_key(scancode);
2334 case 0x2304: /* suspend, undock */
2335 case 0x2404: /* hibernation, undock */
2336 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2339 case 0x2305: /* suspend, bay eject */
2340 case 0x2405: /* hibernation, bay eject */
2341 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2347 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2349 "woke up due to a hot-unplug "
2351 hotkey_wakeup_reason_notify_change();
2355 /* bay-related wakeups */
2356 if (hkey == 0x3003) {
2357 hotkey_autosleep_ack = 1;
2360 hotkey_wakeup_hotunplug_complete_notify_change();
2366 /* dock-related wakeups */
2367 if (hkey == 0x4003) {
2368 hotkey_autosleep_ack = 1;
2371 hotkey_wakeup_hotunplug_complete_notify_change();
2377 /* 0x5000-0x5FFF: human interface helpers */
2379 case 0x5010: /* Lenovo new BIOS: brightness changed */
2380 case 0x500b: /* X61t: tablet pen inserted into bay */
2381 case 0x500c: /* X61t: tablet pen removed from bay */
2383 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2384 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2385 tpacpi_input_send_tabletsw();
2386 hotkey_tablet_mode_notify_change();
2391 /* LID switch events. Do not propagate */
2399 /* 0x7000-0x7FFF: misc */
2400 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2401 tpacpi_input_send_radiosw();
2402 hotkey_radio_sw_notify_change();
2406 /* fallthrough to default */
2411 printk(TPACPI_NOTICE
2412 "unhandled HKEY event 0x%04x\n", hkey);
2416 if (!ignore_acpi_ev &&
2417 (send_acpi_ev || hotkey_report_mode < 2)) {
2418 acpi_bus_generate_proc_event(ibm->acpi->device,
2422 /* netlink events */
2423 if (!ignore_acpi_ev && send_acpi_ev) {
2424 acpi_bus_generate_netlink_event(
2425 ibm->acpi->device->pnp.device_class,
2426 ibm->acpi->device->dev.bus_id,
2432 static void hotkey_suspend(pm_message_t state)
2434 /* Do these on suspend, we get the events on early resume! */
2435 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2436 hotkey_autosleep_ack = 0;
2439 static void hotkey_resume(void)
2441 if (hotkey_mask_get())
2443 "error while trying to read hot key mask "
2445 tpacpi_input_send_radiosw();
2446 hotkey_radio_sw_notify_change();
2447 hotkey_tablet_mode_notify_change();
2448 hotkey_wakeup_reason_notify_change();
2449 hotkey_wakeup_hotunplug_complete_notify_change();
2450 hotkey_poll_setup_safe(0);
2453 /* procfs -------------------------------------------------------------- */
2454 static int hotkey_read(char *p)
2459 if (!tp_features.hotkey) {
2460 len += sprintf(p + len, "status:\t\tnot supported\n");
2464 if (mutex_lock_interruptible(&hotkey_mutex))
2465 return -ERESTARTSYS;
2466 res = hotkey_status_get(&status);
2468 res = hotkey_mask_get();
2469 mutex_unlock(&hotkey_mutex);
2473 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2474 if (tp_features.hotkey_mask) {
2475 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2476 len += sprintf(p + len,
2477 "commands:\tenable, disable, reset, <mask>\n");
2479 len += sprintf(p + len, "mask:\t\tnot supported\n");
2480 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2486 static int hotkey_write(char *buf)
2492 if (!tp_features.hotkey)
2495 if (mutex_lock_interruptible(&hotkey_mutex))
2496 return -ERESTARTSYS;
2502 while ((cmd = next_cmd(&buf))) {
2503 if (strlencmp(cmd, "enable") == 0) {
2505 } else if (strlencmp(cmd, "disable") == 0) {
2507 } else if (strlencmp(cmd, "reset") == 0) {
2508 status = hotkey_orig_status;
2509 mask = hotkey_orig_mask;
2510 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2512 } else if (sscanf(cmd, "%x", &mask) == 1) {
2520 res = hotkey_status_set(status);
2522 if (!res && mask != hotkey_mask)
2523 res = hotkey_mask_set(mask);
2526 mutex_unlock(&hotkey_mutex);
2530 static const struct acpi_device_id ibm_htk_device_ids[] = {
2531 {TPACPI_ACPI_HKEY_HID, 0},
2535 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2536 .hid = ibm_htk_device_ids,
2537 .notify = hotkey_notify,
2538 .handle = &hkey_handle,
2539 .type = ACPI_DEVICE_NOTIFY,
2542 static struct ibm_struct hotkey_driver_data = {
2544 .read = hotkey_read,
2545 .write = hotkey_write,
2546 .exit = hotkey_exit,
2547 .resume = hotkey_resume,
2548 .suspend = hotkey_suspend,
2549 .acpi = &ibm_hotkey_acpidriver,
2552 /*************************************************************************
2553 * Bluetooth subdriver
2557 /* ACPI GBDC/SBDC bits */
2558 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2559 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2560 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2563 static int bluetooth_get_radiosw(void);
2564 static int bluetooth_set_radiosw(int radio_on);
2566 /* sysfs bluetooth enable ---------------------------------------------- */
2567 static ssize_t bluetooth_enable_show(struct device *dev,
2568 struct device_attribute *attr,
2573 status = bluetooth_get_radiosw();
2577 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2580 static ssize_t bluetooth_enable_store(struct device *dev,
2581 struct device_attribute *attr,
2582 const char *buf, size_t count)
2587 if (parse_strtoul(buf, 1, &t))
2590 res = bluetooth_set_radiosw(t);
2592 return (res) ? res : count;
2595 static struct device_attribute dev_attr_bluetooth_enable =
2596 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2597 bluetooth_enable_show, bluetooth_enable_store);
2599 /* --------------------------------------------------------------------- */
2601 static struct attribute *bluetooth_attributes[] = {
2602 &dev_attr_bluetooth_enable.attr,
2606 static const struct attribute_group bluetooth_attr_group = {
2607 .attrs = bluetooth_attributes,
2610 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2615 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2617 TPACPI_ACPIHANDLE_INIT(hkey);
2619 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2620 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2621 tp_features.bluetooth = hkey_handle &&
2622 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2624 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2625 str_supported(tp_features.bluetooth),
2628 if (tp_features.bluetooth) {
2629 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2630 /* no bluetooth hardware present in system */
2631 tp_features.bluetooth = 0;
2632 dbg_printk(TPACPI_DBG_INIT,
2633 "bluetooth hardware not installed\n");
2635 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2636 &bluetooth_attr_group);
2642 return (tp_features.bluetooth)? 0 : 1;
2645 static void bluetooth_exit(void)
2647 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2648 &bluetooth_attr_group);
2651 static int bluetooth_get_radiosw(void)
2655 if (!tp_features.bluetooth)
2658 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2661 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2664 static int bluetooth_set_radiosw(int radio_on)
2668 if (!tp_features.bluetooth)
2671 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2674 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2676 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2677 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2683 /* procfs -------------------------------------------------------------- */
2684 static int bluetooth_read(char *p)
2687 int status = bluetooth_get_radiosw();
2689 if (!tp_features.bluetooth)
2690 len += sprintf(p + len, "status:\t\tnot supported\n");
2692 len += sprintf(p + len, "status:\t\t%s\n",
2693 (status)? "enabled" : "disabled");
2694 len += sprintf(p + len, "commands:\tenable, disable\n");
2700 static int bluetooth_write(char *buf)
2704 if (!tp_features.bluetooth)
2707 while ((cmd = next_cmd(&buf))) {
2708 if (strlencmp(cmd, "enable") == 0) {
2709 bluetooth_set_radiosw(1);
2710 } else if (strlencmp(cmd, "disable") == 0) {
2711 bluetooth_set_radiosw(0);
2719 static struct ibm_struct bluetooth_driver_data = {
2720 .name = "bluetooth",
2721 .read = bluetooth_read,
2722 .write = bluetooth_write,
2723 .exit = bluetooth_exit,
2726 /*************************************************************************
2731 /* ACPI GWAN/SWAN bits */
2732 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2733 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2734 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2737 static int wan_get_radiosw(void);
2738 static int wan_set_radiosw(int radio_on);
2740 /* sysfs wan enable ---------------------------------------------------- */
2741 static ssize_t wan_enable_show(struct device *dev,
2742 struct device_attribute *attr,
2747 status = wan_get_radiosw();
2751 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2754 static ssize_t wan_enable_store(struct device *dev,
2755 struct device_attribute *attr,
2756 const char *buf, size_t count)
2761 if (parse_strtoul(buf, 1, &t))
2764 res = wan_set_radiosw(t);
2766 return (res) ? res : count;
2769 static struct device_attribute dev_attr_wan_enable =
2770 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2771 wan_enable_show, wan_enable_store);
2773 /* --------------------------------------------------------------------- */
2775 static struct attribute *wan_attributes[] = {
2776 &dev_attr_wan_enable.attr,
2780 static const struct attribute_group wan_attr_group = {
2781 .attrs = wan_attributes,
2784 static int __init wan_init(struct ibm_init_struct *iibm)
2789 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2791 TPACPI_ACPIHANDLE_INIT(hkey);
2793 tp_features.wan = hkey_handle &&
2794 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2796 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2797 str_supported(tp_features.wan),
2800 if (tp_features.wan) {
2801 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2802 /* no wan hardware present in system */
2803 tp_features.wan = 0;
2804 dbg_printk(TPACPI_DBG_INIT,
2805 "wan hardware not installed\n");
2807 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2814 return (tp_features.wan)? 0 : 1;
2817 static void wan_exit(void)
2819 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2823 static int wan_get_radiosw(void)
2827 if (!tp_features.wan)
2830 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2833 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2836 static int wan_set_radiosw(int radio_on)
2840 if (!tp_features.wan)
2843 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2846 status |= TP_ACPI_WANCARD_RADIOSSW;
2848 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2849 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2855 /* procfs -------------------------------------------------------------- */
2856 static int wan_read(char *p)
2859 int status = wan_get_radiosw();
2861 if (!tp_features.wan)
2862 len += sprintf(p + len, "status:\t\tnot supported\n");
2864 len += sprintf(p + len, "status:\t\t%s\n",
2865 (status)? "enabled" : "disabled");
2866 len += sprintf(p + len, "commands:\tenable, disable\n");
2872 static int wan_write(char *buf)
2876 if (!tp_features.wan)
2879 while ((cmd = next_cmd(&buf))) {
2880 if (strlencmp(cmd, "enable") == 0) {
2882 } else if (strlencmp(cmd, "disable") == 0) {
2891 static struct ibm_struct wan_driver_data = {
2896 .flags.experimental = 1,
2899 /*************************************************************************
2903 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2905 enum video_access_mode {
2906 TPACPI_VIDEO_NONE = 0,
2907 TPACPI_VIDEO_570, /* 570 */
2908 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2909 TPACPI_VIDEO_NEW, /* all others */
2912 enum { /* video status flags, based on VIDEO_570 */
2913 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2914 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2915 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2918 enum { /* TPACPI_VIDEO_570 constants */
2919 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2920 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2921 * video_status_flags */
2922 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2923 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2926 static enum video_access_mode video_supported;
2927 static int video_orig_autosw;
2929 static int video_autosw_get(void);
2930 static int video_autosw_set(int enable);
2932 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2934 static int __init video_init(struct ibm_init_struct *iibm)
2938 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2940 TPACPI_ACPIHANDLE_INIT(vid);
2941 TPACPI_ACPIHANDLE_INIT(vid2);
2943 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2944 /* G41, assume IVGA doesn't change */
2945 vid_handle = vid2_handle;
2948 /* video switching not supported on R30, R31 */
2949 video_supported = TPACPI_VIDEO_NONE;
2950 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2952 video_supported = TPACPI_VIDEO_570;
2953 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2954 /* 600e/x, 770e, 770x */
2955 video_supported = TPACPI_VIDEO_770;
2958 video_supported = TPACPI_VIDEO_NEW;
2960 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2961 str_supported(video_supported != TPACPI_VIDEO_NONE),
2964 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2967 static void video_exit(void)
2969 dbg_printk(TPACPI_DBG_EXIT,
2970 "restoring original video autoswitch mode\n");
2971 if (video_autosw_set(video_orig_autosw))
2972 printk(TPACPI_ERR "error while trying to restore original "
2973 "video autoswitch mode\n");
2976 static int video_outputsw_get(void)
2981 switch (video_supported) {
2982 case TPACPI_VIDEO_570:
2983 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2984 TP_ACPI_VIDEO_570_PHSCMD))
2986 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2988 case TPACPI_VIDEO_770:
2989 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2992 status |= TP_ACPI_VIDEO_S_LCD;
2993 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2996 status |= TP_ACPI_VIDEO_S_CRT;
2998 case TPACPI_VIDEO_NEW:
2999 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3000 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3003 status |= TP_ACPI_VIDEO_S_CRT;
3005 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3006 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3009 status |= TP_ACPI_VIDEO_S_LCD;
3010 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3013 status |= TP_ACPI_VIDEO_S_DVI;
3022 static int video_outputsw_set(int status)
3027 switch (video_supported) {
3028 case TPACPI_VIDEO_570:
3029 res = acpi_evalf(NULL, NULL,
3030 "\\_SB.PHS2", "vdd",
3031 TP_ACPI_VIDEO_570_PHS2CMD,
3032 status | TP_ACPI_VIDEO_570_PHS2SET);
3034 case TPACPI_VIDEO_770:
3035 autosw = video_autosw_get();
3039 res = video_autosw_set(1);
3042 res = acpi_evalf(vid_handle, NULL,
3043 "ASWT", "vdd", status * 0x100, 0);
3044 if (!autosw && video_autosw_set(autosw)) {
3046 "video auto-switch left enabled due to error\n");
3050 case TPACPI_VIDEO_NEW:
3051 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3052 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3058 return (res)? 0 : -EIO;
3061 static int video_autosw_get(void)
3065 switch (video_supported) {
3066 case TPACPI_VIDEO_570:
3067 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3070 case TPACPI_VIDEO_770:
3071 case TPACPI_VIDEO_NEW:
3072 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3082 static int video_autosw_set(int enable)
3084 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3089 static int video_outputsw_cycle(void)
3091 int autosw = video_autosw_get();
3097 switch (video_supported) {
3098 case TPACPI_VIDEO_570:
3099 res = video_autosw_set(1);
3102 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3104 case TPACPI_VIDEO_770:
3105 case TPACPI_VIDEO_NEW:
3106 res = video_autosw_set(1);
3109 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3114 if (!autosw && video_autosw_set(autosw)) {
3116 "video auto-switch left enabled due to error\n");
3120 return (res)? 0 : -EIO;
3123 static int video_expand_toggle(void)
3125 switch (video_supported) {
3126 case TPACPI_VIDEO_570:
3127 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3129 case TPACPI_VIDEO_770:
3130 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3132 case TPACPI_VIDEO_NEW:
3133 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3141 static int video_read(char *p)
3146 if (video_supported == TPACPI_VIDEO_NONE) {
3147 len += sprintf(p + len, "status:\t\tnot supported\n");
3151 status = video_outputsw_get();
3155 autosw = video_autosw_get();
3159 len += sprintf(p + len, "status:\t\tsupported\n");
3160 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3161 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3162 if (video_supported == TPACPI_VIDEO_NEW)
3163 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3164 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3165 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3166 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3167 if (video_supported == TPACPI_VIDEO_NEW)
3168 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3169 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3170 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3175 static int video_write(char *buf)
3178 int enable, disable, status;
3181 if (video_supported == TPACPI_VIDEO_NONE)
3187 while ((cmd = next_cmd(&buf))) {
3188 if (strlencmp(cmd, "lcd_enable") == 0) {
3189 enable |= TP_ACPI_VIDEO_S_LCD;
3190 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3191 disable |= TP_ACPI_VIDEO_S_LCD;
3192 } else if (strlencmp(cmd, "crt_enable") == 0) {
3193 enable |= TP_ACPI_VIDEO_S_CRT;
3194 } else if (strlencmp(cmd, "crt_disable") == 0) {
3195 disable |= TP_ACPI_VIDEO_S_CRT;
3196 } else if (video_supported == TPACPI_VIDEO_NEW &&
3197 strlencmp(cmd, "dvi_enable") == 0) {
3198 enable |= TP_ACPI_VIDEO_S_DVI;
3199 } else if (video_supported == TPACPI_VIDEO_NEW &&
3200 strlencmp(cmd, "dvi_disable") == 0) {
3201 disable |= TP_ACPI_VIDEO_S_DVI;
3202 } else if (strlencmp(cmd, "auto_enable") == 0) {
3203 res = video_autosw_set(1);
3206 } else if (strlencmp(cmd, "auto_disable") == 0) {
3207 res = video_autosw_set(0);
3210 } else if (strlencmp(cmd, "video_switch") == 0) {
3211 res = video_outputsw_cycle();
3214 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3215 res = video_expand_toggle();
3222 if (enable || disable) {
3223 status = video_outputsw_get();
3226 res = video_outputsw_set((status & ~disable) | enable);
3234 static struct ibm_struct video_driver_data = {
3237 .write = video_write,
3241 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3243 /*************************************************************************
3244 * Light (thinklight) subdriver
3247 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3248 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3250 static int light_get_status(void)
3254 if (tp_features.light_status) {
3255 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3263 static int light_set_status(int status)
3267 if (tp_features.light) {
3269 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3271 TP_CMOS_THINKLIGHT_ON :
3272 TP_CMOS_THINKLIGHT_OFF);
3274 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3277 return (rc)? 0 : -EIO;
3283 static int __init light_init(struct ibm_init_struct *iibm)
3285 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3287 TPACPI_ACPIHANDLE_INIT(ledb);
3288 TPACPI_ACPIHANDLE_INIT(lght);
3289 TPACPI_ACPIHANDLE_INIT(cmos);
3291 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3292 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3294 if (tp_features.light)
3295 /* light status not supported on
3296 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3297 tp_features.light_status =
3298 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3300 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3301 str_supported(tp_features.light));
3303 return (tp_features.light)? 0 : 1;
3306 static int light_read(char *p)
3311 if (!tp_features.light) {
3312 len += sprintf(p + len, "status:\t\tnot supported\n");
3313 } else if (!tp_features.light_status) {
3314 len += sprintf(p + len, "status:\t\tunknown\n");
3315 len += sprintf(p + len, "commands:\ton, off\n");
3317 status = light_get_status();
3320 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3321 len += sprintf(p + len, "commands:\ton, off\n");
3327 static int light_write(char *buf)
3332 if (!tp_features.light)
3335 while ((cmd = next_cmd(&buf))) {
3336 if (strlencmp(cmd, "on") == 0) {
3338 } else if (strlencmp(cmd, "off") == 0) {
3344 return light_set_status(newstatus);
3347 static struct ibm_struct light_driver_data = {
3350 .write = light_write,
3353 /*************************************************************************
3357 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3359 static void dock_notify(struct ibm_struct *ibm, u32 event);
3360 static int dock_read(char *p);
3361 static int dock_write(char *buf);
3363 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3364 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3365 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3366 "\\_SB.PCI.ISA.SLCE", /* 570 */
3367 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3369 /* don't list other alternatives as we install a notify handler on the 570 */
3370 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3372 static const struct acpi_device_id ibm_pci_device_ids[] = {
3373 {PCI_ROOT_HID_STRING, 0},
3377 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3379 .notify = dock_notify,
3380 .handle = &dock_handle,
3381 .type = ACPI_SYSTEM_NOTIFY,
3384 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3385 * We just use it to get notifications of dock hotplug
3386 * in very old thinkpads */
3387 .hid = ibm_pci_device_ids,
3388 .notify = dock_notify,
3389 .handle = &pci_handle,
3390 .type = ACPI_SYSTEM_NOTIFY,
3394 static struct ibm_struct dock_driver_data[2] = {
3398 .write = dock_write,
3399 .acpi = &ibm_dock_acpidriver[0],
3403 .acpi = &ibm_dock_acpidriver[1],
3407 #define dock_docked() (_sta(dock_handle) & 1)
3409 static int __init dock_init(struct ibm_init_struct *iibm)
3411 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3413 TPACPI_ACPIHANDLE_INIT(dock);
3415 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3416 str_supported(dock_handle != NULL));
3418 return (dock_handle)? 0 : 1;
3421 static int __init dock_init2(struct ibm_init_struct *iibm)
3425 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3427 if (dock_driver_data[0].flags.acpi_driver_registered &&
3428 dock_driver_data[0].flags.acpi_notify_installed) {
3429 TPACPI_ACPIHANDLE_INIT(pci);
3430 dock2_needed = (pci_handle != NULL);
3431 vdbg_printk(TPACPI_DBG_INIT,
3432 "dock PCI handler for the TP 570 is %s\n",
3433 str_supported(dock2_needed));
3435 vdbg_printk(TPACPI_DBG_INIT,
3436 "dock subdriver part 2 not required\n");
3440 return (dock2_needed)? 0 : 1;
3443 static void dock_notify(struct ibm_struct *ibm, u32 event)
3445 int docked = dock_docked();
3446 int pci = ibm->acpi->hid && ibm->acpi->device &&
3447 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3450 if (event == 1 && !pci) /* 570 */
3451 data = 1; /* button */
3452 else if (event == 1 && pci) /* 570 */
3453 data = 3; /* dock */
3454 else if (event == 3 && docked)
3455 data = 1; /* button */
3456 else if (event == 3 && !docked)
3457 data = 2; /* undock */
3458 else if (event == 0 && docked)
3459 data = 3; /* dock */
3461 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3462 event, _sta(dock_handle));
3463 data = 0; /* unknown */
3465 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3466 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3467 ibm->acpi->device->dev.bus_id,
3471 static int dock_read(char *p)
3474 int docked = dock_docked();
3477 len += sprintf(p + len, "status:\t\tnot supported\n");
3479 len += sprintf(p + len, "status:\t\tundocked\n");
3481 len += sprintf(p + len, "status:\t\tdocked\n");
3482 len += sprintf(p + len, "commands:\tdock, undock\n");
3488 static int dock_write(char *buf)
3495 while ((cmd = next_cmd(&buf))) {
3496 if (strlencmp(cmd, "undock") == 0) {
3497 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3498 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3500 } else if (strlencmp(cmd, "dock") == 0) {
3501 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3510 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3512 /*************************************************************************
3516 #ifdef CONFIG_THINKPAD_ACPI_BAY
3518 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3519 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3520 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3521 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3522 ); /* A21e, R30, R31 */
3523 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3524 "_EJ0", /* all others */
3525 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3526 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3527 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3529 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3533 static int __init bay_init(struct ibm_init_struct *iibm)
3535 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3537 TPACPI_ACPIHANDLE_INIT(bay);
3539 TPACPI_ACPIHANDLE_INIT(bay_ej);
3540 TPACPI_ACPIHANDLE_INIT(bay2);
3542 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3544 tp_features.bay_status = bay_handle &&
3545 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3546 tp_features.bay_status2 = bay2_handle &&
3547 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3549 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3550 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3551 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3552 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3554 vdbg_printk(TPACPI_DBG_INIT,
3555 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3556 str_supported(tp_features.bay_status),
3557 str_supported(tp_features.bay_eject),
3558 str_supported(tp_features.bay_status2),
3559 str_supported(tp_features.bay_eject2));
3561 return (tp_features.bay_status || tp_features.bay_eject ||
3562 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3565 static void bay_notify(struct ibm_struct *ibm, u32 event)
3567 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3568 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3569 ibm->acpi->device->dev.bus_id,
3573 #define bay_occupied(b) (_sta(b##_handle) & 1)
3575 static int bay_read(char *p)
3578 int occupied = bay_occupied(bay);
3579 int occupied2 = bay_occupied(bay2);
3582 len += sprintf(p + len, "status:\t\t%s\n",
3583 tp_features.bay_status ?
3584 (occupied ? "occupied" : "unoccupied") :
3586 if (tp_features.bay_status2)
3587 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3588 "occupied" : "unoccupied");
3590 eject = tp_features.bay_eject && occupied;
3591 eject2 = tp_features.bay_eject2 && occupied2;
3593 if (eject && eject2)
3594 len += sprintf(p + len, "commands:\teject, eject2\n");
3596 len += sprintf(p + len, "commands:\teject\n");
3598 len += sprintf(p + len, "commands:\teject2\n");
3603 static int bay_write(char *buf)
3607 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3610 while ((cmd = next_cmd(&buf))) {
3611 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3612 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3614 } else if (tp_features.bay_eject2 &&
3615 strlencmp(cmd, "eject2") == 0) {
3616 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3625 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3626 .notify = bay_notify,
3627 .handle = &bay_handle,
3628 .type = ACPI_SYSTEM_NOTIFY,
3631 static struct ibm_struct bay_driver_data = {
3635 .acpi = &ibm_bay_acpidriver,
3638 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3640 /*************************************************************************
3644 /* sysfs cmos_command -------------------------------------------------- */
3645 static ssize_t cmos_command_store(struct device *dev,
3646 struct device_attribute *attr,
3647 const char *buf, size_t count)
3649 unsigned long cmos_cmd;
3652 if (parse_strtoul(buf, 21, &cmos_cmd))
3655 res = issue_thinkpad_cmos_command(cmos_cmd);
3656 return (res)? res : count;
3659 static struct device_attribute dev_attr_cmos_command =
3660 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3662 /* --------------------------------------------------------------------- */
3664 static int __init cmos_init(struct ibm_init_struct *iibm)
3668 vdbg_printk(TPACPI_DBG_INIT,
3669 "initializing cmos commands subdriver\n");
3671 TPACPI_ACPIHANDLE_INIT(cmos);
3673 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3674 str_supported(cmos_handle != NULL));
3676 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3680 return (cmos_handle)? 0 : 1;
3683 static void cmos_exit(void)
3685 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3688 static int cmos_read(char *p)
3692 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3693 R30, R31, T20-22, X20-21 */
3695 len += sprintf(p + len, "status:\t\tnot supported\n");
3697 len += sprintf(p + len, "status:\t\tsupported\n");
3698 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3704 static int cmos_write(char *buf)
3709 while ((cmd = next_cmd(&buf))) {
3710 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3711 cmos_cmd >= 0 && cmos_cmd <= 21) {
3716 res = issue_thinkpad_cmos_command(cmos_cmd);
3724 static struct ibm_struct cmos_driver_data = {
3727 .write = cmos_write,
3731 /*************************************************************************
3735 enum led_access_mode {
3736 TPACPI_LED_NONE = 0,
3737 TPACPI_LED_570, /* 570 */
3738 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3739 TPACPI_LED_NEW, /* all others */
3742 enum { /* For TPACPI_LED_OLD */
3743 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3744 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3745 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3754 static enum led_access_mode led_supported;
3756 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3757 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3758 /* T20-22, X20-21 */
3759 "LED", /* all others */
3762 static int led_get_status(unsigned int led)
3766 switch (led_supported) {
3767 case TPACPI_LED_570:
3768 if (!acpi_evalf(ec_handle,
3769 &status, "GLED", "dd", 1 << led))
3771 return (status == 0)?
3783 static int led_set_status(unsigned int led, enum led_status_t ledstatus)
3785 /* off, on, blink. Index is led_status_t */
3786 static const int const led_sled_arg1[] = { 0, 1, 3 };
3787 static const int const led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3788 static const int const led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3789 static const int const led_led_arg1[] = { 0, 0x80, 0xc0 };
3793 switch (led_supported) {
3794 case TPACPI_LED_570:
3797 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3798 led, led_sled_arg1[ledstatus]))
3801 case TPACPI_LED_OLD:
3802 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3804 rc = ec_write(TPACPI_LED_EC_HLMS, led);
3806 rc = ec_write(TPACPI_LED_EC_HLBL,
3807 led * led_exp_hlbl[ledstatus]);
3809 rc = ec_write(TPACPI_LED_EC_HLCL,
3810 led * led_exp_hlcl[ledstatus]);
3812 case TPACPI_LED_NEW:
3814 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3815 led, led_led_arg1[ledstatus]))
3825 static int __init led_init(struct ibm_init_struct *iibm)
3827 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3829 TPACPI_ACPIHANDLE_INIT(led);
3832 /* led not supported on R30, R31 */
3833 led_supported = TPACPI_LED_NONE;
3834 else if (strlencmp(led_path, "SLED") == 0)
3836 led_supported = TPACPI_LED_570;
3837 else if (strlencmp(led_path, "SYSL") == 0)
3838 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3839 led_supported = TPACPI_LED_OLD;
3842 led_supported = TPACPI_LED_NEW;
3844 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3845 str_supported(led_supported), led_supported);
3847 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3850 #define str_led_status(s) \
3851 ((s) == TPACPI_LED_OFF ? "off" : \
3852 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
3854 static int led_read(char *p)
3858 if (!led_supported) {
3859 len += sprintf(p + len, "status:\t\tnot supported\n");
3862 len += sprintf(p + len, "status:\t\tsupported\n");
3864 if (led_supported == TPACPI_LED_570) {
3867 for (i = 0; i < 8; i++) {
3868 status = led_get_status(i);
3871 len += sprintf(p + len, "%d:\t\t%s\n",
3872 i, str_led_status(status));
3876 len += sprintf(p + len, "commands:\t"
3877 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3882 static int led_write(char *buf)
3886 enum led_status_t s;
3891 while ((cmd = next_cmd(&buf))) {
3892 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3895 if (strstr(cmd, "off")) {
3897 } else if (strstr(cmd, "on")) {
3899 } else if (strstr(cmd, "blink")) {
3900 s = TPACPI_LED_BLINK;
3905 rc = led_set_status(led, s);
3913 static struct ibm_struct led_driver_data = {
3919 /*************************************************************************
3923 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3925 static int __init beep_init(struct ibm_init_struct *iibm)
3927 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3929 TPACPI_ACPIHANDLE_INIT(beep);
3931 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3932 str_supported(beep_handle != NULL));
3934 return (beep_handle)? 0 : 1;
3937 static int beep_read(char *p)
3942 len += sprintf(p + len, "status:\t\tnot supported\n");
3944 len += sprintf(p + len, "status:\t\tsupported\n");
3945 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3951 static int beep_write(char *buf)
3959 while ((cmd = next_cmd(&buf))) {
3960 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3961 beep_cmd >= 0 && beep_cmd <= 17) {
3965 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3972 static struct ibm_struct beep_driver_data = {
3975 .write = beep_write,
3978 /*************************************************************************
3982 enum thermal_access_mode {
3983 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3984 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3985 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3986 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3987 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3990 enum { /* TPACPI_THERMAL_TPEC_* */
3991 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3992 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3993 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3996 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3997 struct ibm_thermal_sensors_struct {
3998 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4001 static enum thermal_access_mode thermal_read_mode;
4003 /* idx is zero-based */
4004 static int thermal_get_sensor(int idx, s32 *value)
4010 t = TP_EC_THERMAL_TMP0;
4012 switch (thermal_read_mode) {
4013 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4014 case TPACPI_THERMAL_TPEC_16:
4015 if (idx >= 8 && idx <= 15) {
4016 t = TP_EC_THERMAL_TMP8;
4021 case TPACPI_THERMAL_TPEC_8:
4023 if (!acpi_ec_read(t + idx, &tmp))
4025 *value = tmp * 1000;
4030 case TPACPI_THERMAL_ACPI_UPDT:
4032 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4033 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4035 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4037 *value = (t - 2732) * 100;
4042 case TPACPI_THERMAL_ACPI_TMP07:
4044 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4045 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4047 if (t > 127 || t < -127)
4048 t = TP_EC_THERMAL_TMP_NA;
4054 case TPACPI_THERMAL_NONE:
4062 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4073 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4076 for (i = 0 ; i < n; i++) {
4077 res = thermal_get_sensor(i, &s->temp[i]);
4085 /* sysfs temp##_input -------------------------------------------------- */
4087 static ssize_t thermal_temp_input_show(struct device *dev,
4088 struct device_attribute *attr,
4091 struct sensor_device_attribute *sensor_attr =
4092 to_sensor_dev_attr(attr);
4093 int idx = sensor_attr->index;
4097 res = thermal_get_sensor(idx, &value);
4100 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4103 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4106 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4107 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4108 thermal_temp_input_show, NULL, _idxB)
4110 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4111 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4112 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4113 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4114 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4115 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4116 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4117 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4118 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4119 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4120 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4121 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4122 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4123 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4124 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4125 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4126 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4129 #define THERMAL_ATTRS(X) \
4130 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4132 static struct attribute *thermal_temp_input_attr[] = {
4152 static const struct attribute_group thermal_temp_input16_group = {
4153 .attrs = thermal_temp_input_attr
4156 static const struct attribute_group thermal_temp_input8_group = {
4157 .attrs = &thermal_temp_input_attr[8]
4160 #undef THERMAL_SENSOR_ATTR_TEMP
4161 #undef THERMAL_ATTRS
4163 /* --------------------------------------------------------------------- */
4165 static int __init thermal_init(struct ibm_init_struct *iibm)
4172 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4174 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4176 if (thinkpad_id.ec_model) {
4178 * Direct EC access mode: sensors at registers
4179 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4180 * non-implemented, thermal sensors return 0x80 when
4185 for (i = 0; i < 8; i++) {
4186 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4192 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4200 /* This is sheer paranoia, but we handle it anyway */
4203 "ThinkPad ACPI EC access misbehaving, "
4204 "falling back to ACPI TMPx access "
4206 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4209 "ThinkPad ACPI EC access misbehaving, "
4210 "disabling thermal sensors access\n");
4211 thermal_read_mode = TPACPI_THERMAL_NONE;
4216 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4218 } else if (acpi_tmp7) {
4219 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4220 /* 600e/x, 770e, 770x */
4221 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4223 /* Standard ACPI TMPx access, max 8 sensors */
4224 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4227 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4228 thermal_read_mode = TPACPI_THERMAL_NONE;
4231 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4232 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4235 switch (thermal_read_mode) {
4236 case TPACPI_THERMAL_TPEC_16:
4237 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4238 &thermal_temp_input16_group);
4242 case TPACPI_THERMAL_TPEC_8:
4243 case TPACPI_THERMAL_ACPI_TMP07:
4244 case TPACPI_THERMAL_ACPI_UPDT:
4245 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4246 &thermal_temp_input8_group);
4250 case TPACPI_THERMAL_NONE:
4258 static void thermal_exit(void)
4260 switch (thermal_read_mode) {
4261 case TPACPI_THERMAL_TPEC_16:
4262 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4263 &thermal_temp_input16_group);
4265 case TPACPI_THERMAL_TPEC_8:
4266 case TPACPI_THERMAL_ACPI_TMP07:
4267 case TPACPI_THERMAL_ACPI_UPDT:
4268 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4269 &thermal_temp_input16_group);
4271 case TPACPI_THERMAL_NONE:
4277 static int thermal_read(char *p)
4281 struct ibm_thermal_sensors_struct t;
4283 n = thermal_get_sensors(&t);
4284 if (unlikely(n < 0))
4287 len += sprintf(p + len, "temperatures:\t");
4290 for (i = 0; i < (n - 1); i++)
4291 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4292 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4294 len += sprintf(p + len, "not supported\n");
4299 static struct ibm_struct thermal_driver_data = {
4301 .read = thermal_read,
4302 .exit = thermal_exit,
4305 /*************************************************************************
4309 static u8 ecdump_regs[256];
4311 static int ecdump_read(char *p)
4317 len += sprintf(p + len, "EC "
4318 " +00 +01 +02 +03 +04 +05 +06 +07"
4319 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4320 for (i = 0; i < 256; i += 16) {
4321 len += sprintf(p + len, "EC 0x%02x:", i);
4322 for (j = 0; j < 16; j++) {
4323 if (!acpi_ec_read(i + j, &v))
4325 if (v != ecdump_regs[i + j])
4326 len += sprintf(p + len, " *%02x", v);
4328 len += sprintf(p + len, " %02x", v);
4329 ecdump_regs[i + j] = v;
4331 len += sprintf(p + len, "\n");
4336 /* These are way too dangerous to advertise openly... */
4338 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4339 " (<offset> is 00-ff, <value> is 00-ff)\n");
4340 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4341 " (<offset> is 00-ff, <value> is 0-255)\n");
4346 static int ecdump_write(char *buf)
4351 while ((cmd = next_cmd(&buf))) {
4352 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4354 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4358 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4359 if (!acpi_ec_write(i, v))
4368 static struct ibm_struct ecdump_driver_data = {
4370 .read = ecdump_read,
4371 .write = ecdump_write,
4372 .flags.experimental = 1,
4375 /*************************************************************************
4376 * Backlight/brightness subdriver
4379 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4382 TP_EC_BACKLIGHT = 0x31,
4384 /* TP_EC_BACKLIGHT bitmasks */
4385 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4386 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4387 TP_EC_BACKLIGHT_MAPSW = 0x20,
4390 static struct backlight_device *ibm_backlight_device;
4391 static int brightness_mode;
4392 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4394 static struct mutex brightness_mutex;
4397 * ThinkPads can read brightness from two places: EC 0x31, or
4398 * CMOS NVRAM byte 0x5E, bits 0-3.
4400 * EC 0x31 has the following layout
4401 * Bit 7: unknown function
4402 * Bit 6: unknown function
4403 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4404 * Bit 4: must be set to zero to avoid problems
4405 * Bit 3-0: backlight brightness level
4407 * brightness_get_raw returns status data in the EC 0x31 layout
4409 static int brightness_get_raw(int *status)
4411 u8 lec = 0, lcmos = 0, level = 0;
4413 if (brightness_mode & 1) {
4414 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4416 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4418 if (brightness_mode & 2) {
4419 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4420 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4421 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4422 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4426 if (brightness_mode == 3) {
4427 *status = lec; /* Prefer EC, CMOS is just a backing store */
4428 lec &= TP_EC_BACKLIGHT_LVLMSK;
4430 tp_warned.bright_cmos_ec_unsync = 0;
4432 if (!tp_warned.bright_cmos_ec_unsync) {
4434 "CMOS NVRAM (%u) and EC (%u) do not "
4435 "agree on display brightness level\n",
4436 (unsigned int) lcmos,
4437 (unsigned int) lec);
4438 tp_warned.bright_cmos_ec_unsync = 1;
4449 /* May return EINTR which can always be mapped to ERESTARTSYS */
4450 static int brightness_set(int value)
4452 int cmos_cmd, inc, i, res;
4456 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4460 res = mutex_lock_interruptible(&brightness_mutex);
4464 res = brightness_get_raw(¤t_value);
4468 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4469 current_value &= TP_EC_BACKLIGHT_LVLMSK;
4471 cmos_cmd = value > current_value ?
4472 TP_CMOS_BRIGHTNESS_UP :
4473 TP_CMOS_BRIGHTNESS_DOWN;
4474 inc = (value > current_value)? 1 : -1;
4477 for (i = current_value; i != value; i += inc) {
4478 if ((brightness_mode & 2) &&
4479 issue_thinkpad_cmos_command(cmos_cmd)) {
4483 if ((brightness_mode & 1) &&
4484 !acpi_ec_write(TP_EC_BACKLIGHT,
4485 (i + inc) | command_bits)) {
4492 mutex_unlock(&brightness_mutex);
4496 /* sysfs backlight class ----------------------------------------------- */
4498 static int brightness_update_status(struct backlight_device *bd)
4500 /* it is the backlight class's job (caller) to handle
4501 * EINTR and other errors properly */
4502 return brightness_set(
4503 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4504 bd->props.power == FB_BLANK_UNBLANK) ?
4505 bd->props.brightness : 0);
4508 static int brightness_get(struct backlight_device *bd)
4512 res = brightness_get_raw(&status);
4514 return 0; /* FIXME: teach backlight about error handling */
4516 return status & TP_EC_BACKLIGHT_LVLMSK;
4519 static struct backlight_ops ibm_backlight_data = {
4520 .get_brightness = brightness_get,
4521 .update_status = brightness_update_status,
4524 /* --------------------------------------------------------------------- */
4526 static int __init brightness_init(struct ibm_init_struct *iibm)
4530 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4532 mutex_init(&brightness_mutex);
4535 * We always attempt to detect acpi support, so as to switch
4536 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4537 * going to publish a backlight interface
4539 b = tpacpi_check_std_acpi_brightness_support();
4541 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4542 printk(TPACPI_NOTICE
4543 "Lenovo BIOS switched to ACPI backlight "
4546 if (brightness_enable > 1) {
4547 printk(TPACPI_NOTICE
4548 "standard ACPI backlight interface "
4549 "available, not loading native one...\n");
4554 if (!brightness_enable) {
4555 dbg_printk(TPACPI_DBG_INIT,
4556 "brightness support disabled by "
4557 "module parameter\n");
4563 "Unsupported brightness interface, "
4564 "please contact %s\n", TPACPI_MAIL);
4568 tp_features.bright_16levels = 1;
4570 if (!brightness_mode) {
4571 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4572 brightness_mode = 2;
4574 brightness_mode = 3;
4576 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4580 if (brightness_mode > 3)
4583 if (brightness_get_raw(&b) < 0)
4586 if (tp_features.bright_16levels)
4588 "detected a 16-level brightness capable ThinkPad\n");
4590 ibm_backlight_device = backlight_device_register(
4591 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4592 &ibm_backlight_data);
4593 if (IS_ERR(ibm_backlight_device)) {
4594 printk(TPACPI_ERR "Could not register backlight device\n");
4595 return PTR_ERR(ibm_backlight_device);
4597 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4599 ibm_backlight_device->props.max_brightness =
4600 (tp_features.bright_16levels)? 15 : 7;
4601 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
4602 backlight_update_status(ibm_backlight_device);
4607 static void brightness_exit(void)
4609 if (ibm_backlight_device) {
4610 vdbg_printk(TPACPI_DBG_EXIT,
4611 "calling backlight_device_unregister()\n");
4612 backlight_device_unregister(ibm_backlight_device);
4613 ibm_backlight_device = NULL;
4617 static int brightness_read(char *p)
4622 level = brightness_get(NULL);
4624 len += sprintf(p + len, "level:\t\tunreadable\n");
4626 len += sprintf(p + len, "level:\t\t%d\n", level);
4627 len += sprintf(p + len, "commands:\tup, down\n");
4628 len += sprintf(p + len, "commands:\tlevel <level>"
4629 " (<level> is 0-%d)\n",
4630 (tp_features.bright_16levels) ? 15 : 7);
4636 static int brightness_write(char *buf)
4641 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4643 level = brightness_get(NULL);
4647 while ((cmd = next_cmd(&buf))) {
4648 if (strlencmp(cmd, "up") == 0) {
4649 if (level < max_level)
4651 } else if (strlencmp(cmd, "down") == 0) {
4654 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4655 level >= 0 && level <= max_level) {
4662 * Now we know what the final level should be, so we try to set it.
4663 * Doing it this way makes the syscall restartable in case of EINTR
4665 rc = brightness_set(level);
4666 return (rc == -EINTR)? ERESTARTSYS : rc;
4669 static struct ibm_struct brightness_driver_data = {
4670 .name = "brightness",
4671 .read = brightness_read,
4672 .write = brightness_write,
4673 .exit = brightness_exit,
4676 /*************************************************************************
4680 static int volume_offset = 0x30;
4682 static int volume_read(char *p)
4687 if (!acpi_ec_read(volume_offset, &level)) {
4688 len += sprintf(p + len, "level:\t\tunreadable\n");
4690 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4691 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4692 len += sprintf(p + len, "commands:\tup, down, mute\n");
4693 len += sprintf(p + len, "commands:\tlevel <level>"
4694 " (<level> is 0-15)\n");
4700 static int volume_write(char *buf)
4702 int cmos_cmd, inc, i;
4704 int new_level, new_mute;
4707 while ((cmd = next_cmd(&buf))) {
4708 if (!acpi_ec_read(volume_offset, &level))
4710 new_mute = mute = level & 0x40;
4711 new_level = level = level & 0xf;
4713 if (strlencmp(cmd, "up") == 0) {
4717 new_level = level == 15 ? 15 : level + 1;
4718 } else if (strlencmp(cmd, "down") == 0) {
4722 new_level = level == 0 ? 0 : level - 1;
4723 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4724 new_level >= 0 && new_level <= 15) {
4726 } else if (strlencmp(cmd, "mute") == 0) {
4731 if (new_level != level) {
4732 /* mute doesn't change */
4734 cmos_cmd = (new_level > level) ?
4735 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4736 inc = new_level > level ? 1 : -1;
4738 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4739 !acpi_ec_write(volume_offset, level)))
4742 for (i = level; i != new_level; i += inc)
4743 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4744 !acpi_ec_write(volume_offset, i + inc))
4748 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4749 !acpi_ec_write(volume_offset, new_level + mute))) {
4754 if (new_mute != mute) {
4755 /* level doesn't change */
4757 cmos_cmd = (new_mute) ?
4758 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4760 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4761 !acpi_ec_write(volume_offset, level + new_mute))
4769 static struct ibm_struct volume_driver_data = {
4771 .read = volume_read,
4772 .write = volume_write,
4775 /*************************************************************************
4782 * TPACPI_FAN_RD_ACPI_GFAN:
4783 * ACPI GFAN method: returns fan level
4785 * see TPACPI_FAN_WR_ACPI_SFAN
4786 * EC 0x2f (HFSP) not available if GFAN exists
4788 * TPACPI_FAN_WR_ACPI_SFAN:
4789 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4791 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4794 * TPACPI_FAN_WR_TPEC:
4795 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4796 * Supported on almost all ThinkPads
4798 * Fan speed changes of any sort (including those caused by the
4799 * disengaged mode) are usually done slowly by the firmware as the
4800 * maximum ammount of fan duty cycle change per second seems to be
4803 * Reading is not available if GFAN exists.
4804 * Writing is not available if SFAN exists.
4807 * 7 automatic mode engaged;
4808 * (default operation mode of the ThinkPad)
4809 * fan level is ignored in this mode.
4810 * 6 full speed mode (takes precedence over bit 7);
4811 * not available on all thinkpads. May disable
4812 * the tachometer while the fan controller ramps up
4813 * the speed (which can take up to a few *minutes*).
4814 * Speeds up fan to 100% duty-cycle, which is far above
4815 * the standard RPM levels. It is not impossible that
4816 * it could cause hardware damage.
4817 * 5-3 unused in some models. Extra bits for fan level
4818 * in others, but still useless as all values above
4819 * 7 map to the same speed as level 7 in these models.
4820 * 2-0 fan level (0..7 usually)
4822 * 0x07 = max (set when temperatures critical)
4823 * Some ThinkPads may have other levels, see
4824 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4826 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4827 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4828 * does so, its initial value is meaningless (0x07).
4830 * For firmware bugs, refer to:
4831 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4835 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4836 * Main fan tachometer reading (in RPM)
4838 * This register is present on all ThinkPads with a new-style EC, and
4839 * it is known not to be present on the A21m/e, and T22, as there is
4840 * something else in offset 0x84 according to the ACPI DSDT. Other
4841 * ThinkPads from this same time period (and earlier) probably lack the
4842 * tachometer as well.
4844 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4845 * was never fixed by IBM to report the EC firmware version string
4846 * probably support the tachometer (like the early X models), so
4847 * detecting it is quite hard. We need more data to know for sure.
4849 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4852 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4855 * For firmware bugs, refer to:
4856 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4858 * TPACPI_FAN_WR_ACPI_FANS:
4859 * ThinkPad X31, X40, X41. Not available in the X60.
4861 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4862 * high speed. ACPI DSDT seems to map these three speeds to levels
4863 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4864 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4866 * The speeds are stored on handles
4867 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4869 * There are three default speed sets, acessible as handles:
4870 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4872 * ACPI DSDT switches which set is in use depending on various
4875 * TPACPI_FAN_WR_TPEC is also available and should be used to
4876 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4877 * but the ACPI tables just mention level 7.
4880 enum { /* Fan control constants */
4881 fan_status_offset = 0x2f, /* EC register 0x2f */
4882 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4883 * 0x84 must be read before 0x85 */
4885 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4886 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4888 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4891 enum fan_status_access_mode {
4892 TPACPI_FAN_NONE = 0, /* No fan status or control */
4893 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4894 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4897 enum fan_control_access_mode {
4898 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4899 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4900 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4901 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4904 enum fan_control_commands {
4905 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4906 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4907 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4908 * and also watchdog cmd */
4911 static int fan_control_allowed;
4913 static enum fan_status_access_mode fan_status_access_mode;
4914 static enum fan_control_access_mode fan_control_access_mode;
4915 static enum fan_control_commands fan_control_commands;
4917 static u8 fan_control_initial_status;
4918 static u8 fan_control_desired_level;
4919 static int fan_watchdog_maxinterval;
4921 static struct mutex fan_mutex;
4923 static void fan_watchdog_fire(struct work_struct *ignored);
4924 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4926 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4927 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
4928 "\\FSPD", /* 600e/x, 770e, 770x */
4930 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
4931 "JFNS", /* 770x-JL */
4935 * Call with fan_mutex held
4937 static void fan_update_desired_level(u8 status)
4940 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4942 fan_control_desired_level = 7;
4944 fan_control_desired_level = status;
4948 static int fan_get_status(u8 *status)
4953 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4955 switch (fan_status_access_mode) {
4956 case TPACPI_FAN_RD_ACPI_GFAN:
4957 /* 570, 600e/x, 770e, 770x */
4959 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4967 case TPACPI_FAN_RD_TPEC:
4968 /* all except 570, 600e/x, 770e, 770x */
4969 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4984 static int fan_get_status_safe(u8 *status)
4989 if (mutex_lock_interruptible(&fan_mutex))
4990 return -ERESTARTSYS;
4991 rc = fan_get_status(&s);
4993 fan_update_desired_level(s);
4994 mutex_unlock(&fan_mutex);
5002 static int fan_get_speed(unsigned int *speed)
5006 switch (fan_status_access_mode) {
5007 case TPACPI_FAN_RD_TPEC:
5008 /* all except 570, 600e/x, 770e, 770x */
5009 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5010 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5014 *speed = (hi << 8) | lo;
5025 static int fan_set_level(int level)
5027 if (!fan_control_allowed)
5030 switch (fan_control_access_mode) {
5031 case TPACPI_FAN_WR_ACPI_SFAN:
5032 if (level >= 0 && level <= 7) {
5033 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5039 case TPACPI_FAN_WR_ACPI_FANS:
5040 case TPACPI_FAN_WR_TPEC:
5041 if ((level != TP_EC_FAN_AUTO) &&
5042 (level != TP_EC_FAN_FULLSPEED) &&
5043 ((level < 0) || (level > 7)))
5046 /* safety net should the EC not support AUTO
5047 * or FULLSPEED mode bits and just ignore them */
5048 if (level & TP_EC_FAN_FULLSPEED)
5049 level |= 7; /* safety min speed 7 */
5050 else if (level & TP_EC_FAN_AUTO)
5051 level |= 4; /* safety min speed 4 */
5053 if (!acpi_ec_write(fan_status_offset, level))
5056 tp_features.fan_ctrl_status_undef = 0;
5065 static int fan_set_level_safe(int level)
5069 if (!fan_control_allowed)
5072 if (mutex_lock_interruptible(&fan_mutex))
5073 return -ERESTARTSYS;
5075 if (level == TPACPI_FAN_LAST_LEVEL)
5076 level = fan_control_desired_level;
5078 rc = fan_set_level(level);
5080 fan_update_desired_level(level);
5082 mutex_unlock(&fan_mutex);
5086 static int fan_set_enable(void)
5091 if (!fan_control_allowed)
5094 if (mutex_lock_interruptible(&fan_mutex))
5095 return -ERESTARTSYS;
5097 switch (fan_control_access_mode) {
5098 case TPACPI_FAN_WR_ACPI_FANS:
5099 case TPACPI_FAN_WR_TPEC:
5100 rc = fan_get_status(&s);
5104 /* Don't go out of emergency fan mode */
5107 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5110 if (!acpi_ec_write(fan_status_offset, s))
5113 tp_features.fan_ctrl_status_undef = 0;
5118 case TPACPI_FAN_WR_ACPI_SFAN:
5119 rc = fan_get_status(&s);
5125 /* Set fan to at least level 4 */
5128 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5138 mutex_unlock(&fan_mutex);
5142 static int fan_set_disable(void)
5146 if (!fan_control_allowed)
5149 if (mutex_lock_interruptible(&fan_mutex))
5150 return -ERESTARTSYS;
5153 switch (fan_control_access_mode) {
5154 case TPACPI_FAN_WR_ACPI_FANS:
5155 case TPACPI_FAN_WR_TPEC:
5156 if (!acpi_ec_write(fan_status_offset, 0x00))
5159 fan_control_desired_level = 0;
5160 tp_features.fan_ctrl_status_undef = 0;
5164 case TPACPI_FAN_WR_ACPI_SFAN:
5165 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5168 fan_control_desired_level = 0;
5176 mutex_unlock(&fan_mutex);
5180 static int fan_set_speed(int speed)
5184 if (!fan_control_allowed)
5187 if (mutex_lock_interruptible(&fan_mutex))
5188 return -ERESTARTSYS;
5191 switch (fan_control_access_mode) {
5192 case TPACPI_FAN_WR_ACPI_FANS:
5193 if (speed >= 0 && speed <= 65535) {
5194 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5195 speed, speed, speed))
5205 mutex_unlock(&fan_mutex);
5209 static void fan_watchdog_reset(void)
5211 static int fan_watchdog_active;
5213 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5216 if (fan_watchdog_active)
5217 cancel_delayed_work(&fan_watchdog_task);
5219 if (fan_watchdog_maxinterval > 0 &&
5220 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5221 fan_watchdog_active = 1;
5222 if (!schedule_delayed_work(&fan_watchdog_task,
5223 msecs_to_jiffies(fan_watchdog_maxinterval
5226 "failed to schedule the fan watchdog, "
5227 "watchdog will not trigger\n");
5230 fan_watchdog_active = 0;
5233 static void fan_watchdog_fire(struct work_struct *ignored)
5237 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5240 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5241 rc = fan_set_enable();
5243 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5244 "will try again later...\n", -rc);
5245 /* reschedule for later */
5246 fan_watchdog_reset();
5251 * SYSFS fan layout: hwmon compatible (device)
5254 * 0: "disengaged" mode
5256 * 2: native EC "auto" mode (recommended, hardware default)
5258 * pwm*: set speed in manual mode, ignored otherwise.
5259 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5262 * fan*_input: tachometer reading, RPM
5265 * SYSFS fan layout: extensions
5267 * fan_watchdog (driver):
5268 * fan watchdog interval in seconds, 0 disables (default), max 120
5271 /* sysfs fan pwm1_enable ----------------------------------------------- */
5272 static ssize_t fan_pwm1_enable_show(struct device *dev,
5273 struct device_attribute *attr,
5279 res = fan_get_status_safe(&status);
5283 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5284 if (status != fan_control_initial_status) {
5285 tp_features.fan_ctrl_status_undef = 0;
5287 /* Return most likely status. In fact, it
5288 * might be the only possible status */
5289 status = TP_EC_FAN_AUTO;
5293 if (status & TP_EC_FAN_FULLSPEED) {
5295 } else if (status & TP_EC_FAN_AUTO) {
5300 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5303 static ssize_t fan_pwm1_enable_store(struct device *dev,
5304 struct device_attribute *attr,
5305 const char *buf, size_t count)
5310 if (parse_strtoul(buf, 2, &t))
5315 level = TP_EC_FAN_FULLSPEED;
5318 level = TPACPI_FAN_LAST_LEVEL;
5321 level = TP_EC_FAN_AUTO;
5324 /* reserved for software-controlled auto mode */
5330 res = fan_set_level_safe(level);
5336 fan_watchdog_reset();
5341 static struct device_attribute dev_attr_fan_pwm1_enable =
5342 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5343 fan_pwm1_enable_show, fan_pwm1_enable_store);
5345 /* sysfs fan pwm1 ------------------------------------------------------ */
5346 static ssize_t fan_pwm1_show(struct device *dev,
5347 struct device_attribute *attr,
5353 res = fan_get_status_safe(&status);
5357 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5358 if (status != fan_control_initial_status) {
5359 tp_features.fan_ctrl_status_undef = 0;
5361 status = TP_EC_FAN_AUTO;
5366 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5367 status = fan_control_desired_level;
5372 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5375 static ssize_t fan_pwm1_store(struct device *dev,
5376 struct device_attribute *attr,
5377 const char *buf, size_t count)
5381 u8 status, newlevel;
5383 if (parse_strtoul(buf, 255, &s))
5386 /* scale down from 0-255 to 0-7 */
5387 newlevel = (s >> 5) & 0x07;
5389 if (mutex_lock_interruptible(&fan_mutex))
5390 return -ERESTARTSYS;
5392 rc = fan_get_status(&status);
5393 if (!rc && (status &
5394 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5395 rc = fan_set_level(newlevel);
5399 fan_update_desired_level(newlevel);
5400 fan_watchdog_reset();
5404 mutex_unlock(&fan_mutex);
5405 return (rc)? rc : count;
5408 static struct device_attribute dev_attr_fan_pwm1 =
5409 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5410 fan_pwm1_show, fan_pwm1_store);
5412 /* sysfs fan fan1_input ------------------------------------------------ */
5413 static ssize_t fan_fan1_input_show(struct device *dev,
5414 struct device_attribute *attr,
5420 res = fan_get_speed(&speed);
5424 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5427 static struct device_attribute dev_attr_fan_fan1_input =
5428 __ATTR(fan1_input, S_IRUGO,
5429 fan_fan1_input_show, NULL);
5431 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5432 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5435 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5438 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5439 const char *buf, size_t count)
5443 if (parse_strtoul(buf, 120, &t))
5446 if (!fan_control_allowed)
5449 fan_watchdog_maxinterval = t;
5450 fan_watchdog_reset();
5455 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5456 fan_fan_watchdog_show, fan_fan_watchdog_store);
5458 /* --------------------------------------------------------------------- */
5459 static struct attribute *fan_attributes[] = {
5460 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5461 &dev_attr_fan_fan1_input.attr,
5465 static const struct attribute_group fan_attr_group = {
5466 .attrs = fan_attributes,
5469 static int __init fan_init(struct ibm_init_struct *iibm)
5473 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5475 mutex_init(&fan_mutex);
5476 fan_status_access_mode = TPACPI_FAN_NONE;
5477 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5478 fan_control_commands = 0;
5479 fan_watchdog_maxinterval = 0;
5480 tp_features.fan_ctrl_status_undef = 0;
5481 fan_control_desired_level = 7;
5483 TPACPI_ACPIHANDLE_INIT(fans);
5484 TPACPI_ACPIHANDLE_INIT(gfan);
5485 TPACPI_ACPIHANDLE_INIT(sfan);
5488 /* 570, 600e/x, 770e, 770x */
5489 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5491 /* all other ThinkPads: note that even old-style
5492 * ThinkPad ECs supports the fan control register */
5493 if (likely(acpi_ec_read(fan_status_offset,
5494 &fan_control_initial_status))) {
5495 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5497 /* In some ThinkPads, neither the EC nor the ACPI
5498 * DSDT initialize the fan status, and it ends up
5499 * being set to 0x07 when it *could* be either
5502 * Enable for TP-1Y (T43), TP-78 (R51e),
5503 * TP-76 (R52), TP-70 (T43, R52), which are known
5505 if (fan_control_initial_status == 0x07) {
5506 switch (thinkpad_id.ec_model) {
5507 case 0x5931: /* TP-1Y */
5508 case 0x3837: /* TP-78 */
5509 case 0x3637: /* TP-76 */
5510 case 0x3037: /* TP-70 */
5511 printk(TPACPI_NOTICE
5512 "fan_init: initial fan status "
5513 "is unknown, assuming it is "
5515 tp_features.fan_ctrl_status_undef = 1;
5521 "ThinkPad ACPI EC access misbehaving, "
5522 "fan status and control unavailable\n");
5529 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5530 fan_control_commands |=
5531 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5534 /* gfan without sfan means no fan control */
5535 /* all other models implement TP EC 0x2f control */
5539 fan_control_access_mode =
5540 TPACPI_FAN_WR_ACPI_FANS;
5541 fan_control_commands |=
5542 TPACPI_FAN_CMD_SPEED |
5543 TPACPI_FAN_CMD_LEVEL |
5544 TPACPI_FAN_CMD_ENABLE;
5546 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5547 fan_control_commands |=
5548 TPACPI_FAN_CMD_LEVEL |
5549 TPACPI_FAN_CMD_ENABLE;
5554 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5555 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5556 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5557 fan_status_access_mode, fan_control_access_mode);
5559 /* fan control master switch */
5560 if (!fan_control_allowed) {
5561 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5562 fan_control_commands = 0;
5563 dbg_printk(TPACPI_DBG_INIT,
5564 "fan control features disabled by parameter\n");
5567 /* update fan_control_desired_level */
5568 if (fan_status_access_mode != TPACPI_FAN_NONE)
5569 fan_get_status_safe(NULL);
5571 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5572 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5573 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5576 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5577 &driver_attr_fan_watchdog);
5585 static void fan_exit(void)
5587 vdbg_printk(TPACPI_DBG_EXIT,
5588 "cancelling any pending fan watchdog tasks\n");
5590 /* FIXME: can we really do this unconditionally? */
5591 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5592 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5593 &driver_attr_fan_watchdog);
5595 cancel_delayed_work(&fan_watchdog_task);
5596 flush_scheduled_work();
5599 static int fan_read(char *p)
5604 unsigned int speed = 0;
5606 switch (fan_status_access_mode) {
5607 case TPACPI_FAN_RD_ACPI_GFAN:
5608 /* 570, 600e/x, 770e, 770x */
5609 rc = fan_get_status_safe(&status);
5613 len += sprintf(p + len, "status:\t\t%s\n"
5615 (status != 0) ? "enabled" : "disabled", status);
5618 case TPACPI_FAN_RD_TPEC:
5619 /* all except 570, 600e/x, 770e, 770x */
5620 rc = fan_get_status_safe(&status);
5624 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5625 if (status != fan_control_initial_status)
5626 tp_features.fan_ctrl_status_undef = 0;
5628 /* Return most likely status. In fact, it
5629 * might be the only possible status */
5630 status = TP_EC_FAN_AUTO;
5633 len += sprintf(p + len, "status:\t\t%s\n",
5634 (status != 0) ? "enabled" : "disabled");
5636 rc = fan_get_speed(&speed);
5640 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5642 if (status & TP_EC_FAN_FULLSPEED)
5643 /* Disengaged mode takes precedence */
5644 len += sprintf(p + len, "level:\t\tdisengaged\n");
5645 else if (status & TP_EC_FAN_AUTO)
5646 len += sprintf(p + len, "level:\t\tauto\n");
5648 len += sprintf(p + len, "level:\t\t%d\n", status);
5651 case TPACPI_FAN_NONE:
5653 len += sprintf(p + len, "status:\t\tnot supported\n");
5656 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5657 len += sprintf(p + len, "commands:\tlevel <level>");
5659 switch (fan_control_access_mode) {
5660 case TPACPI_FAN_WR_ACPI_SFAN:
5661 len += sprintf(p + len, " (<level> is 0-7)\n");
5665 len += sprintf(p + len, " (<level> is 0-7, "
5666 "auto, disengaged, full-speed)\n");
5671 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5672 len += sprintf(p + len, "commands:\tenable, disable\n"
5673 "commands:\twatchdog <timeout> (<timeout> "
5674 "is 0 (off), 1-120 (seconds))\n");
5676 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5677 len += sprintf(p + len, "commands:\tspeed <speed>"
5678 " (<speed> is 0-65535)\n");
5683 static int fan_write_cmd_level(const char *cmd, int *rc)
5687 if (strlencmp(cmd, "level auto") == 0)
5688 level = TP_EC_FAN_AUTO;
5689 else if ((strlencmp(cmd, "level disengaged") == 0) |
5690 (strlencmp(cmd, "level full-speed") == 0))
5691 level = TP_EC_FAN_FULLSPEED;
5692 else if (sscanf(cmd, "level %d", &level) != 1)
5695 *rc = fan_set_level_safe(level);
5697 printk(TPACPI_ERR "level command accepted for unsupported "
5698 "access mode %d", fan_control_access_mode);
5703 static int fan_write_cmd_enable(const char *cmd, int *rc)
5705 if (strlencmp(cmd, "enable") != 0)
5708 *rc = fan_set_enable();
5710 printk(TPACPI_ERR "enable command accepted for unsupported "
5711 "access mode %d", fan_control_access_mode);
5716 static int fan_write_cmd_disable(const char *cmd, int *rc)
5718 if (strlencmp(cmd, "disable") != 0)
5721 *rc = fan_set_disable();
5723 printk(TPACPI_ERR "disable command accepted for unsupported "
5724 "access mode %d", fan_control_access_mode);
5729 static int fan_write_cmd_speed(const char *cmd, int *rc)
5734 * Support speed <low> <medium> <high> ? */
5736 if (sscanf(cmd, "speed %d", &speed) != 1)
5739 *rc = fan_set_speed(speed);
5741 printk(TPACPI_ERR "speed command accepted for unsupported "
5742 "access mode %d", fan_control_access_mode);
5747 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5751 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5754 if (interval < 0 || interval > 120)
5757 fan_watchdog_maxinterval = interval;
5762 static int fan_write(char *buf)
5767 while (!rc && (cmd = next_cmd(&buf))) {
5768 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5769 fan_write_cmd_level(cmd, &rc)) &&
5770 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5771 (fan_write_cmd_enable(cmd, &rc) ||
5772 fan_write_cmd_disable(cmd, &rc) ||
5773 fan_write_cmd_watchdog(cmd, &rc))) &&
5774 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5775 fan_write_cmd_speed(cmd, &rc))
5779 fan_watchdog_reset();
5785 static struct ibm_struct fan_driver_data = {
5792 /****************************************************************************
5793 ****************************************************************************
5797 ****************************************************************************
5798 ****************************************************************************/
5800 /* sysfs name ---------------------------------------------------------- */
5801 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5802 struct device_attribute *attr,
5805 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
5808 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5809 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5811 /* --------------------------------------------------------------------- */
5814 static struct proc_dir_entry *proc_dir;
5817 * Module and infrastructure proble, init and exit handling
5820 static int force_load;
5822 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5823 static const char * __init str_supported(int is_supported)
5825 static char text_unsupported[] __initdata = "not supported";
5827 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5829 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5831 static void ibm_exit(struct ibm_struct *ibm)
5833 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5835 list_del_init(&ibm->all_drivers);
5837 if (ibm->flags.acpi_notify_installed) {
5838 dbg_printk(TPACPI_DBG_EXIT,
5839 "%s: acpi_remove_notify_handler\n", ibm->name);
5841 acpi_remove_notify_handler(*ibm->acpi->handle,
5843 dispatch_acpi_notify);
5844 ibm->flags.acpi_notify_installed = 0;
5845 ibm->flags.acpi_notify_installed = 0;
5848 if (ibm->flags.proc_created) {
5849 dbg_printk(TPACPI_DBG_EXIT,
5850 "%s: remove_proc_entry\n", ibm->name);
5851 remove_proc_entry(ibm->name, proc_dir);
5852 ibm->flags.proc_created = 0;
5855 if (ibm->flags.acpi_driver_registered) {
5856 dbg_printk(TPACPI_DBG_EXIT,
5857 "%s: acpi_bus_unregister_driver\n", ibm->name);
5859 acpi_bus_unregister_driver(ibm->acpi->driver);
5860 kfree(ibm->acpi->driver);
5861 ibm->acpi->driver = NULL;
5862 ibm->flags.acpi_driver_registered = 0;
5865 if (ibm->flags.init_called && ibm->exit) {
5867 ibm->flags.init_called = 0;
5870 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5873 static int __init ibm_init(struct ibm_init_struct *iibm)
5876 struct ibm_struct *ibm = iibm->data;
5877 struct proc_dir_entry *entry;
5879 BUG_ON(ibm == NULL);
5881 INIT_LIST_HEAD(&ibm->all_drivers);
5883 if (ibm->flags.experimental && !experimental)
5886 dbg_printk(TPACPI_DBG_INIT,
5887 "probing for %s\n", ibm->name);
5890 ret = iibm->init(iibm);
5892 return 0; /* probe failed */
5896 ibm->flags.init_called = 1;
5900 if (ibm->acpi->hid) {
5901 ret = register_tpacpi_subdriver(ibm);
5906 if (ibm->acpi->notify) {
5907 ret = setup_acpi_notify(ibm);
5908 if (ret == -ENODEV) {
5909 printk(TPACPI_NOTICE "disabling subdriver %s\n",
5919 dbg_printk(TPACPI_DBG_INIT,
5920 "%s installed\n", ibm->name);
5923 entry = create_proc_entry(ibm->name,
5924 S_IFREG | S_IRUGO | S_IWUSR,
5927 printk(TPACPI_ERR "unable to create proc entry %s\n",
5932 entry->owner = THIS_MODULE;
5934 entry->read_proc = &dispatch_procfs_read;
5936 entry->write_proc = &dispatch_procfs_write;
5937 ibm->flags.proc_created = 1;
5940 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5945 dbg_printk(TPACPI_DBG_INIT,
5946 "%s: at error exit path with result %d\n",
5950 return (ret < 0)? ret : 0;
5955 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5957 const struct dmi_device *dev = NULL;
5958 char ec_fw_string[18];
5963 memset(tp, 0, sizeof(*tp));
5965 if (dmi_name_in_vendors("IBM"))
5966 tp->vendor = PCI_VENDOR_ID_IBM;
5967 else if (dmi_name_in_vendors("LENOVO"))
5968 tp->vendor = PCI_VENDOR_ID_LENOVO;
5972 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5974 if (!tp->bios_version_str)
5976 tp->bios_model = tp->bios_version_str[0]
5977 | (tp->bios_version_str[1] << 8);
5980 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5981 * X32 or newer, all Z series; Some models must have an
5982 * up-to-date BIOS or they will not be detected.
5984 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5986 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5987 if (sscanf(dev->name,
5988 "IBM ThinkPad Embedded Controller -[%17c",
5989 ec_fw_string) == 1) {
5990 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5991 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5993 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5994 tp->ec_model = ec_fw_string[0]
5995 | (ec_fw_string[1] << 8);
6000 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
6002 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
6003 kfree(tp->model_str);
6004 tp->model_str = NULL;
6007 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
6011 static int __init probe_for_thinkpad(void)
6019 * Non-ancient models have better DMI tagging, but very old models
6022 is_thinkpad = (thinkpad_id.model_str != NULL);
6024 /* ec is required because many other handles are relative to it */
6025 TPACPI_ACPIHANDLE_INIT(ec);
6029 "Not yet supported ThinkPad detected!\n");
6034 * Risks a regression on very old machines, but reduces potential
6035 * false positives a damn great deal
6038 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6040 if (!is_thinkpad && !force_load)
6047 /* Module init, exit, parameters */
6049 static struct ibm_init_struct ibms_init[] __initdata = {
6051 .init = thinkpad_acpi_driver_init,
6052 .data = &thinkpad_acpi_driver_data,
6055 .init = hotkey_init,
6056 .data = &hotkey_driver_data,
6059 .init = bluetooth_init,
6060 .data = &bluetooth_driver_data,
6064 .data = &wan_driver_data,
6066 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6069 .data = &video_driver_data,
6074 .data = &light_driver_data,
6076 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6079 .data = &dock_driver_data[0],
6083 .data = &dock_driver_data[1],
6086 #ifdef CONFIG_THINKPAD_ACPI_BAY
6089 .data = &bay_driver_data,
6094 .data = &cmos_driver_data,
6098 .data = &led_driver_data,
6102 .data = &beep_driver_data,
6105 .init = thermal_init,
6106 .data = &thermal_driver_data,
6109 .data = &ecdump_driver_data,
6112 .init = brightness_init,
6113 .data = &brightness_driver_data,
6116 .data = &volume_driver_data,
6120 .data = &fan_driver_data,
6124 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6127 struct ibm_struct *ibm;
6129 if (!kp || !kp->name || !val)
6132 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6133 ibm = ibms_init[i].data;
6134 WARN_ON(ibm == NULL);
6136 if (!ibm || !ibm->name)
6139 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6140 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6142 strcpy(ibms_init[i].param, val);
6143 strcat(ibms_init[i].param, ",");
6151 module_param(experimental, int, 0);
6152 MODULE_PARM_DESC(experimental,
6153 "Enables experimental features when non-zero");
6155 module_param_named(debug, dbg_level, uint, 0);
6156 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6158 module_param(force_load, bool, 0);
6159 MODULE_PARM_DESC(force_load,
6160 "Attempts to load the driver even on a "
6161 "mis-identified ThinkPad when true");
6163 module_param_named(fan_control, fan_control_allowed, bool, 0);
6164 MODULE_PARM_DESC(fan_control,
6165 "Enables setting fan parameters features when true");
6167 module_param_named(brightness_mode, brightness_mode, int, 0);
6168 MODULE_PARM_DESC(brightness_mode,
6169 "Selects brightness control strategy: "
6170 "0=auto, 1=EC, 2=CMOS, 3=both");
6172 module_param(brightness_enable, uint, 0);
6173 MODULE_PARM_DESC(brightness_enable,
6174 "Enables backlight control when 1, disables when 0");
6176 module_param(hotkey_report_mode, uint, 0);
6177 MODULE_PARM_DESC(hotkey_report_mode,
6178 "used for backwards compatibility with userspace, "
6179 "see documentation");
6181 #define TPACPI_PARAM(feature) \
6182 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6183 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6184 "at module load, see documentation")
6186 TPACPI_PARAM(hotkey);
6187 TPACPI_PARAM(bluetooth);
6188 TPACPI_PARAM(video);
6189 TPACPI_PARAM(light);
6190 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6193 #ifdef CONFIG_THINKPAD_ACPI_BAY
6195 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6199 TPACPI_PARAM(ecdump);
6200 TPACPI_PARAM(brightness);
6201 TPACPI_PARAM(volume);
6204 static void thinkpad_acpi_module_exit(void)
6206 struct ibm_struct *ibm, *itmp;
6208 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6210 list_for_each_entry_safe_reverse(ibm, itmp,
6211 &tpacpi_all_drivers,
6216 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6218 if (tpacpi_inputdev) {
6219 if (tp_features.input_device_registered)
6220 input_unregister_device(tpacpi_inputdev);
6222 input_free_device(tpacpi_inputdev);
6226 hwmon_device_unregister(tpacpi_hwmon);
6228 if (tp_features.sensors_pdev_attrs_registered)
6229 device_remove_file(&tpacpi_sensors_pdev->dev,
6230 &dev_attr_thinkpad_acpi_pdev_name);
6231 if (tpacpi_sensors_pdev)
6232 platform_device_unregister(tpacpi_sensors_pdev);
6234 platform_device_unregister(tpacpi_pdev);
6236 if (tp_features.sensors_pdrv_attrs_registered)
6237 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6238 if (tp_features.platform_drv_attrs_registered)
6239 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6241 if (tp_features.sensors_pdrv_registered)
6242 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6244 if (tp_features.platform_drv_registered)
6245 platform_driver_unregister(&tpacpi_pdriver);
6248 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6250 kfree(thinkpad_id.bios_version_str);
6251 kfree(thinkpad_id.ec_version_str);
6252 kfree(thinkpad_id.model_str);
6256 static int __init thinkpad_acpi_module_init(void)
6260 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6262 /* Parameter checking */
6263 if (hotkey_report_mode > 2)
6266 /* Driver-level probe */
6268 get_thinkpad_model_data(&thinkpad_id);
6269 ret = probe_for_thinkpad();
6271 thinkpad_acpi_module_exit();
6275 /* Driver initialization */
6277 TPACPI_ACPIHANDLE_INIT(ecrd);
6278 TPACPI_ACPIHANDLE_INIT(ecwr);
6280 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6283 "unable to create proc dir " TPACPI_PROC_DIR);
6284 thinkpad_acpi_module_exit();
6287 proc_dir->owner = THIS_MODULE;
6289 ret = platform_driver_register(&tpacpi_pdriver);
6292 "unable to register main platform driver\n");
6293 thinkpad_acpi_module_exit();
6296 tp_features.platform_drv_registered = 1;
6298 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6301 "unable to register hwmon platform driver\n");
6302 thinkpad_acpi_module_exit();
6305 tp_features.sensors_pdrv_registered = 1;
6307 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6309 tp_features.platform_drv_attrs_registered = 1;
6310 ret = tpacpi_create_driver_attributes(
6311 &tpacpi_hwmon_pdriver.driver);
6315 "unable to create sysfs driver attributes\n");
6316 thinkpad_acpi_module_exit();
6319 tp_features.sensors_pdrv_attrs_registered = 1;
6322 /* Device initialization */
6323 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6325 if (IS_ERR(tpacpi_pdev)) {
6326 ret = PTR_ERR(tpacpi_pdev);
6328 printk(TPACPI_ERR "unable to register platform device\n");
6329 thinkpad_acpi_module_exit();
6332 tpacpi_sensors_pdev = platform_device_register_simple(
6333 TPACPI_HWMON_DRVR_NAME,
6335 if (IS_ERR(tpacpi_sensors_pdev)) {
6336 ret = PTR_ERR(tpacpi_sensors_pdev);
6337 tpacpi_sensors_pdev = NULL;
6339 "unable to register hwmon platform device\n");
6340 thinkpad_acpi_module_exit();
6343 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6344 &dev_attr_thinkpad_acpi_pdev_name);
6347 "unable to create sysfs hwmon device attributes\n");
6348 thinkpad_acpi_module_exit();
6351 tp_features.sensors_pdev_attrs_registered = 1;
6352 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6353 if (IS_ERR(tpacpi_hwmon)) {
6354 ret = PTR_ERR(tpacpi_hwmon);
6355 tpacpi_hwmon = NULL;
6356 printk(TPACPI_ERR "unable to register hwmon device\n");
6357 thinkpad_acpi_module_exit();
6360 mutex_init(&tpacpi_inputdev_send_mutex);
6361 tpacpi_inputdev = input_allocate_device();
6362 if (!tpacpi_inputdev) {
6363 printk(TPACPI_ERR "unable to allocate input device\n");
6364 thinkpad_acpi_module_exit();
6367 /* Prepare input device, but don't register */
6368 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6369 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6370 tpacpi_inputdev->id.bustype = BUS_HOST;
6371 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6372 thinkpad_id.vendor :
6374 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6375 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6377 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6378 ret = ibm_init(&ibms_init[i]);
6379 if (ret >= 0 && *ibms_init[i].param)
6380 ret = ibms_init[i].data->write(ibms_init[i].param);
6382 thinkpad_acpi_module_exit();
6386 ret = input_register_device(tpacpi_inputdev);
6388 printk(TPACPI_ERR "unable to register input device\n");
6389 thinkpad_acpi_module_exit();
6392 tp_features.input_device_registered = 1;
6395 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6399 /* Please remove this in year 2009 */
6400 MODULE_ALIAS("ibm_acpi");
6402 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6405 * DMI matching for module autoloading
6407 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6408 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6410 * Only models listed in thinkwiki will be supported, so add yours
6411 * if it is not there yet.
6413 #define IBM_BIOS_MODULE_ALIAS(__type) \
6414 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6416 /* Non-ancient thinkpads */
6417 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6418 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6420 /* Ancient thinkpad BIOSes have to be identified by
6421 * BIOS type or model number, and there are far less
6422 * BIOS types than model numbers... */
6423 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6424 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6425 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6427 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6428 MODULE_DESCRIPTION(TPACPI_DESC);
6429 MODULE_VERSION(TPACPI_VERSION);
6430 MODULE_LICENSE("GPL");
6432 module_init(thinkpad_acpi_module_init);
6433 module_exit(thinkpad_acpi_module_exit);