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.20"
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"
143 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
145 #define TPACPI_MAX_ACPI_ARGS 3
148 #define TPACPI_LOG TPACPI_FILE ": "
149 #define TPACPI_ERR KERN_ERR TPACPI_LOG
150 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
151 #define TPACPI_INFO KERN_INFO TPACPI_LOG
152 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
154 #define TPACPI_DBG_ALL 0xffff
155 #define TPACPI_DBG_ALL 0xffff
156 #define TPACPI_DBG_INIT 0x0001
157 #define TPACPI_DBG_EXIT 0x0002
158 #define dbg_printk(a_dbg_level, format, arg...) \
159 do { if (dbg_level & a_dbg_level) \
160 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
162 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
163 #define vdbg_printk(a_dbg_level, format, arg...) \
164 dbg_printk(a_dbg_level, format, ## arg)
165 static const char *str_supported(int is_supported);
167 #define vdbg_printk(a_dbg_level, format, arg...)
170 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
171 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
172 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
175 /****************************************************************************
176 * Driver-wide structs and misc. variables
181 struct tp_acpi_drv_struct {
182 const struct acpi_device_id *hid;
183 struct acpi_driver *driver;
185 void (*notify) (struct ibm_struct *, u32);
188 struct acpi_device *device;
194 int (*read) (char *);
195 int (*write) (char *);
197 void (*resume) (void);
198 void (*suspend) (pm_message_t state);
200 struct list_head all_drivers;
202 struct tp_acpi_drv_struct *acpi;
205 u8 acpi_driver_registered:1;
206 u8 acpi_notify_installed:1;
213 struct ibm_init_struct {
216 int (*init) (struct ibm_init_struct *);
217 struct ibm_struct *data;
221 #ifdef CONFIG_THINKPAD_ACPI_BAY
234 u32 bright_16levels:1;
235 u32 bright_acpimode:1;
237 u32 fan_ctrl_status_undef:1;
238 u32 input_device_registered:1;
239 u32 platform_drv_registered:1;
240 u32 platform_drv_attrs_registered:1;
241 u32 sensors_pdrv_registered:1;
242 u32 sensors_pdrv_attrs_registered:1;
243 u32 sensors_pdev_attrs_registered:1;
244 u32 hotkey_poll_active:1;
248 u16 hotkey_mask_ff:1;
249 u16 bright_cmos_ec_unsync:1;
252 struct thinkpad_id_data {
253 unsigned int vendor; /* ThinkPad vendor:
254 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
256 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
257 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
259 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
262 char *model_str; /* ThinkPad T43 */
263 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
265 static struct thinkpad_id_data thinkpad_id;
268 TPACPI_LIFE_INIT = 0,
273 static int experimental;
274 static u32 dbg_level;
276 static struct workqueue_struct *tpacpi_wq;
278 /* Special LED class that can defer work */
279 struct tpacpi_led_classdev {
280 struct led_classdev led_classdev;
281 struct work_struct work;
282 enum led_brightness new_brightness;
286 /****************************************************************************
287 ****************************************************************************
289 * ACPI Helpers and device model
291 ****************************************************************************
292 ****************************************************************************/
294 /*************************************************************************
298 static acpi_handle root_handle;
300 #define TPACPI_HANDLE(object, parent, paths...) \
301 static acpi_handle object##_handle; \
302 static acpi_handle *object##_parent = &parent##_handle; \
303 static char *object##_path; \
304 static char *object##_paths[] = { paths }
306 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
307 "\\_SB.PCI.ISA.EC", /* 570 */
308 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
309 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
310 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
311 "\\_SB.PCI0.ICH3.EC0", /* R31 */
312 "\\_SB.PCI0.LPC.EC", /* all others */
315 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
316 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
318 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
320 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
321 "\\CMS", /* R40, R40e */
324 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
325 "^HKEY", /* R30, R31 */
326 "HKEY", /* all others */
329 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
330 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
331 "\\_SB.PCI0.VID0", /* 770e */
332 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
333 "\\_SB.PCI0.AGP.VID", /* all others */
337 /*************************************************************************
341 static int acpi_evalf(acpi_handle handle,
342 void *res, char *method, char *fmt, ...)
345 struct acpi_object_list params;
346 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
347 struct acpi_buffer result, *resultp;
348 union acpi_object out_obj;
356 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
369 params.pointer = &in_objs[0];
376 in_objs[params.count].integer.value = va_arg(ap, int);
377 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
379 /* add more types as needed */
381 printk(TPACPI_ERR "acpi_evalf() called "
382 "with invalid format character '%c'\n", c);
388 if (res_type != 'v') {
389 result.length = sizeof(out_obj);
390 result.pointer = &out_obj;
395 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
400 *(int *)res = out_obj.integer.value;
401 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
404 success = status == AE_OK;
406 /* add more types as needed */
408 printk(TPACPI_ERR "acpi_evalf() called "
409 "with invalid format character '%c'\n", res_type);
413 if (!success && !quiet)
414 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
415 method, fmt0, status);
420 static int acpi_ec_read(int i, u8 *p)
425 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
429 if (ec_read(i, p) < 0)
436 static int acpi_ec_write(int i, u8 v)
439 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
442 if (ec_write(i, v) < 0)
449 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
450 static int _sta(acpi_handle handle)
454 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
461 static int issue_thinkpad_cmos_command(int cmos_cmd)
466 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
472 /*************************************************************************
476 #define TPACPI_ACPIHANDLE_INIT(object) \
477 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
478 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
480 static void drv_acpi_handle_init(char *name,
481 acpi_handle *handle, acpi_handle parent,
482 char **paths, int num_paths, char **path)
487 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
490 for (i = 0; i < num_paths; i++) {
491 status = acpi_get_handle(parent, paths[i], handle);
492 if (ACPI_SUCCESS(status)) {
494 dbg_printk(TPACPI_DBG_INIT,
495 "Found ACPI handle %s for %s\n",
501 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
506 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
508 struct ibm_struct *ibm = data;
510 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
513 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
516 ibm->acpi->notify(ibm, event);
519 static int __init setup_acpi_notify(struct ibm_struct *ibm)
526 if (!*ibm->acpi->handle)
529 vdbg_printk(TPACPI_DBG_INIT,
530 "setting up ACPI notify for %s\n", ibm->name);
532 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
534 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
539 acpi_driver_data(ibm->acpi->device) = ibm;
540 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
541 TPACPI_ACPI_EVENT_PREFIX,
544 status = acpi_install_notify_handler(*ibm->acpi->handle,
545 ibm->acpi->type, dispatch_acpi_notify, ibm);
546 if (ACPI_FAILURE(status)) {
547 if (status == AE_ALREADY_EXISTS) {
549 "another device driver is already "
550 "handling %s events\n", ibm->name);
553 "acpi_install_notify_handler(%s) failed: %d\n",
558 ibm->flags.acpi_notify_installed = 1;
562 static int __init tpacpi_device_add(struct acpi_device *device)
567 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
571 dbg_printk(TPACPI_DBG_INIT,
572 "registering %s as an ACPI driver\n", ibm->name);
576 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
577 if (!ibm->acpi->driver) {
578 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
582 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
583 ibm->acpi->driver->ids = ibm->acpi->hid;
585 ibm->acpi->driver->ops.add = &tpacpi_device_add;
587 rc = acpi_bus_register_driver(ibm->acpi->driver);
589 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
591 kfree(ibm->acpi->driver);
592 ibm->acpi->driver = NULL;
594 ibm->flags.acpi_driver_registered = 1;
600 /****************************************************************************
601 ****************************************************************************
605 ****************************************************************************
606 ****************************************************************************/
608 static int dispatch_procfs_read(char *page, char **start, off_t off,
609 int count, int *eof, void *data)
611 struct ibm_struct *ibm = data;
614 if (!ibm || !ibm->read)
617 len = ibm->read(page);
621 if (len <= off + count)
633 static int dispatch_procfs_write(struct file *file,
634 const char __user *userbuf,
635 unsigned long count, void *data)
637 struct ibm_struct *ibm = data;
641 if (!ibm || !ibm->write)
644 kernbuf = kmalloc(count + 2, GFP_KERNEL);
648 if (copy_from_user(kernbuf, userbuf, count)) {
654 strcat(kernbuf, ",");
655 ret = ibm->write(kernbuf);
664 static char *next_cmd(char **cmds)
669 while ((end = strchr(start, ',')) && end == start)
681 /****************************************************************************
682 ****************************************************************************
684 * Device model: input, hwmon and platform
686 ****************************************************************************
687 ****************************************************************************/
689 static struct platform_device *tpacpi_pdev;
690 static struct platform_device *tpacpi_sensors_pdev;
691 static struct device *tpacpi_hwmon;
692 static struct input_dev *tpacpi_inputdev;
693 static struct mutex tpacpi_inputdev_send_mutex;
694 static LIST_HEAD(tpacpi_all_drivers);
696 static int tpacpi_suspend_handler(struct platform_device *pdev,
699 struct ibm_struct *ibm, *itmp;
701 list_for_each_entry_safe(ibm, itmp,
705 (ibm->suspend)(state);
711 static int tpacpi_resume_handler(struct platform_device *pdev)
713 struct ibm_struct *ibm, *itmp;
715 list_for_each_entry_safe(ibm, itmp,
725 static struct platform_driver tpacpi_pdriver = {
727 .name = TPACPI_DRVR_NAME,
728 .owner = THIS_MODULE,
730 .suspend = tpacpi_suspend_handler,
731 .resume = tpacpi_resume_handler,
734 static struct platform_driver tpacpi_hwmon_pdriver = {
736 .name = TPACPI_HWMON_DRVR_NAME,
737 .owner = THIS_MODULE,
741 /*************************************************************************
742 * sysfs support helpers
745 struct attribute_set {
746 unsigned int members, max_members;
747 struct attribute_group group;
750 struct attribute_set_obj {
751 struct attribute_set s;
753 } __attribute__((packed));
755 static struct attribute_set *create_attr_set(unsigned int max_members,
758 struct attribute_set_obj *sobj;
760 if (max_members == 0)
763 /* Allocates space for implicit NULL at the end too */
764 sobj = kzalloc(sizeof(struct attribute_set_obj) +
765 max_members * sizeof(struct attribute *),
769 sobj->s.max_members = max_members;
770 sobj->s.group.attrs = &sobj->a;
771 sobj->s.group.name = name;
776 #define destroy_attr_set(_set) \
779 /* not multi-threaded safe, use it in a single thread per set */
780 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
785 if (s->members >= s->max_members)
788 s->group.attrs[s->members] = attr;
794 static int add_many_to_attr_set(struct attribute_set *s,
795 struct attribute **attr,
800 for (i = 0; i < count; i++) {
801 res = add_to_attr_set(s, attr[i]);
809 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
811 sysfs_remove_group(kobj, &s->group);
815 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
816 sysfs_create_group(_kobj, &_attr_set->group)
818 static int parse_strtoul(const char *buf,
819 unsigned long max, unsigned long *value)
823 while (*buf && isspace(*buf))
825 *value = simple_strtoul(buf, &endp, 0);
826 while (*endp && isspace(*endp))
828 if (*endp || *value > max)
834 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
836 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
837 union acpi_object *obj;
840 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
841 obj = (union acpi_object *)buffer.pointer;
842 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
843 printk(TPACPI_ERR "Unknown _BCL data, "
844 "please report this to %s\n", TPACPI_MAIL);
847 rc = obj->package.count;
853 kfree(buffer.pointer);
857 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
858 u32 lvl, void *context, void **rv)
860 char name[ACPI_PATH_SEGMENT_LENGTH];
861 struct acpi_buffer buffer = { sizeof(name), &name };
863 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
864 !strncmp("_BCL", name, sizeof(name) - 1)) {
866 **(int **)rv = tpacpi_query_bcl_levels(handle);
867 return AE_CTRL_TERMINATE;
874 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
876 static int __init tpacpi_check_std_acpi_brightness_support(void)
880 void *bcl_ptr = &bcl_levels;
883 TPACPI_ACPIHANDLE_INIT(vid);
889 * Search for a _BCL method, and execute it. This is safe on all
890 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
891 * BIOS in ACPI backlight control mode. We do NOT have to care
892 * about calling the _BCL method in an enabled video device, any
893 * will do for our purposes.
896 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
897 tpacpi_acpi_walk_find_bcl, NULL,
900 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
901 tp_features.bright_acpimode = 1;
902 return (bcl_levels - 2);
908 /*************************************************************************
909 * thinkpad-acpi driver attributes
912 /* interface_version --------------------------------------------------- */
913 static ssize_t tpacpi_driver_interface_version_show(
914 struct device_driver *drv,
917 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
920 static DRIVER_ATTR(interface_version, S_IRUGO,
921 tpacpi_driver_interface_version_show, NULL);
923 /* debug_level --------------------------------------------------------- */
924 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
927 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
930 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
931 const char *buf, size_t count)
935 if (parse_strtoul(buf, 0xffff, &t))
943 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
944 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
946 /* version ------------------------------------------------------------- */
947 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
950 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
951 TPACPI_DESC, TPACPI_VERSION);
954 static DRIVER_ATTR(version, S_IRUGO,
955 tpacpi_driver_version_show, NULL);
957 /* --------------------------------------------------------------------- */
959 static struct driver_attribute *tpacpi_driver_attributes[] = {
960 &driver_attr_debug_level, &driver_attr_version,
961 &driver_attr_interface_version,
964 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
970 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
971 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
978 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
982 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
983 driver_remove_file(drv, tpacpi_driver_attributes[i]);
986 /****************************************************************************
987 ****************************************************************************
991 ****************************************************************************
992 ****************************************************************************/
994 /*************************************************************************
995 * thinkpad-acpi init subdriver
998 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1000 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1001 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1003 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1004 (thinkpad_id.bios_version_str) ?
1005 thinkpad_id.bios_version_str : "unknown",
1006 (thinkpad_id.ec_version_str) ?
1007 thinkpad_id.ec_version_str : "unknown");
1009 if (thinkpad_id.vendor && thinkpad_id.model_str)
1010 printk(TPACPI_INFO "%s %s, model %s\n",
1011 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1012 "IBM" : ((thinkpad_id.vendor ==
1013 PCI_VENDOR_ID_LENOVO) ?
1014 "Lenovo" : "Unknown vendor"),
1015 thinkpad_id.model_str,
1016 (thinkpad_id.nummodel_str) ?
1017 thinkpad_id.nummodel_str : "unknown");
1022 static int thinkpad_acpi_driver_read(char *p)
1026 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1027 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1032 static struct ibm_struct thinkpad_acpi_driver_data = {
1034 .read = thinkpad_acpi_driver_read,
1037 /*************************************************************************
1041 enum { /* hot key scan codes (derived from ACPI DSDT) */
1042 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1043 TP_ACPI_HOTKEYSCAN_FNF2,
1044 TP_ACPI_HOTKEYSCAN_FNF3,
1045 TP_ACPI_HOTKEYSCAN_FNF4,
1046 TP_ACPI_HOTKEYSCAN_FNF5,
1047 TP_ACPI_HOTKEYSCAN_FNF6,
1048 TP_ACPI_HOTKEYSCAN_FNF7,
1049 TP_ACPI_HOTKEYSCAN_FNF8,
1050 TP_ACPI_HOTKEYSCAN_FNF9,
1051 TP_ACPI_HOTKEYSCAN_FNF10,
1052 TP_ACPI_HOTKEYSCAN_FNF11,
1053 TP_ACPI_HOTKEYSCAN_FNF12,
1054 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1055 TP_ACPI_HOTKEYSCAN_FNINSERT,
1056 TP_ACPI_HOTKEYSCAN_FNDELETE,
1057 TP_ACPI_HOTKEYSCAN_FNHOME,
1058 TP_ACPI_HOTKEYSCAN_FNEND,
1059 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1060 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1061 TP_ACPI_HOTKEYSCAN_FNSPACE,
1062 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1063 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1064 TP_ACPI_HOTKEYSCAN_MUTE,
1065 TP_ACPI_HOTKEYSCAN_THINKPAD,
1068 enum { /* Keys available through NVRAM polling */
1069 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1070 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1073 enum { /* Positions of some of the keys in hotkey masks */
1074 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1075 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1076 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1077 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1078 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1079 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1080 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1081 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1082 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1083 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1084 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1087 enum { /* NVRAM to ACPI HKEY group map */
1088 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1089 TP_ACPI_HKEY_ZOOM_MASK |
1090 TP_ACPI_HKEY_DISPSWTCH_MASK |
1091 TP_ACPI_HKEY_HIBERNATE_MASK,
1092 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1093 TP_ACPI_HKEY_BRGHTDWN_MASK,
1094 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1095 TP_ACPI_HKEY_VOLDWN_MASK |
1096 TP_ACPI_HKEY_MUTE_MASK,
1099 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1100 struct tp_nvram_state {
1101 u16 thinkpad_toggle:1;
1103 u16 display_toggle:1;
1104 u16 thinklight_toggle:1;
1105 u16 hibernate_toggle:1;
1106 u16 displayexp_toggle:1;
1107 u16 display_state:1;
1108 u16 brightness_toggle:1;
1109 u16 volume_toggle:1;
1112 u8 brightness_level;
1116 static struct task_struct *tpacpi_hotkey_task;
1117 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1118 static int hotkey_poll_freq = 10; /* Hz */
1119 static struct mutex hotkey_thread_mutex;
1120 static struct mutex hotkey_thread_data_mutex;
1121 static unsigned int hotkey_config_change;
1123 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1125 #define hotkey_source_mask 0U
1127 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1129 static struct mutex hotkey_mutex;
1131 static enum { /* Reasons for waking up */
1132 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1133 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1134 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1135 } hotkey_wakeup_reason;
1137 static int hotkey_autosleep_ack;
1139 static int hotkey_orig_status;
1140 static u32 hotkey_orig_mask;
1141 static u32 hotkey_all_mask;
1142 static u32 hotkey_reserved_mask;
1143 static u32 hotkey_mask;
1145 static unsigned int hotkey_report_mode;
1147 static u16 *hotkey_keycode_map;
1149 static struct attribute_set *hotkey_dev_attributes;
1151 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1152 #define HOTKEY_CONFIG_CRITICAL_START \
1154 mutex_lock(&hotkey_thread_data_mutex); \
1155 hotkey_config_change++; \
1157 #define HOTKEY_CONFIG_CRITICAL_END \
1158 mutex_unlock(&hotkey_thread_data_mutex);
1160 #define HOTKEY_CONFIG_CRITICAL_START
1161 #define HOTKEY_CONFIG_CRITICAL_END
1162 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1164 /* HKEY.MHKG() return bits */
1165 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1167 static int hotkey_get_wlsw(int *status)
1169 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1174 static int hotkey_get_tablet_mode(int *status)
1178 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1181 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1186 * Call with hotkey_mutex held
1188 static int hotkey_mask_get(void)
1192 if (tp_features.hotkey_mask) {
1193 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1196 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1202 * Call with hotkey_mutex held
1204 static int hotkey_mask_set(u32 mask)
1209 if (tp_features.hotkey_mask) {
1210 if (!tp_warned.hotkey_mask_ff &&
1211 (mask == 0xffff || mask == 0xffffff ||
1212 mask == 0xffffffff)) {
1213 tp_warned.hotkey_mask_ff = 1;
1214 printk(TPACPI_NOTICE
1215 "setting the hotkey mask to 0x%08x is likely "
1216 "not the best way to go about it\n", mask);
1217 printk(TPACPI_NOTICE
1218 "please consider using the driver defaults, "
1219 "and refer to up-to-date thinkpad-acpi "
1223 HOTKEY_CONFIG_CRITICAL_START
1224 for (i = 0; i < 32; i++) {
1226 /* enable in firmware mask only keys not in NVRAM
1227 * mode, but enable the key in the cached hotkey_mask
1228 * regardless of mode, or the key will end up
1229 * disabled by hotkey_mask_get() */
1230 if (!acpi_evalf(hkey_handle,
1231 NULL, "MHKM", "vdd", i + 1,
1232 !!((mask & ~hotkey_source_mask) & m))) {
1236 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1239 HOTKEY_CONFIG_CRITICAL_END
1241 /* hotkey_mask_get must be called unconditionally below */
1242 if (!hotkey_mask_get() && !rc &&
1243 (hotkey_mask & ~hotkey_source_mask) !=
1244 (mask & ~hotkey_source_mask)) {
1245 printk(TPACPI_NOTICE
1246 "requested hot key mask 0x%08x, but "
1247 "firmware forced it to 0x%08x\n",
1251 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1252 HOTKEY_CONFIG_CRITICAL_START
1253 hotkey_mask = mask & hotkey_source_mask;
1254 HOTKEY_CONFIG_CRITICAL_END
1256 if (hotkey_mask != mask) {
1257 printk(TPACPI_NOTICE
1258 "requested hot key mask 0x%08x, "
1259 "forced to 0x%08x (NVRAM poll mask is "
1260 "0x%08x): no firmware mask support\n",
1261 mask, hotkey_mask, hotkey_source_mask);
1266 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1272 static int hotkey_status_get(int *status)
1274 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1280 static int hotkey_status_set(int status)
1282 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1288 static void tpacpi_input_send_tabletsw(void)
1292 if (tp_features.hotkey_tablet &&
1293 !hotkey_get_tablet_mode(&state)) {
1294 mutex_lock(&tpacpi_inputdev_send_mutex);
1296 input_report_switch(tpacpi_inputdev,
1297 SW_TABLET_MODE, !!state);
1298 input_sync(tpacpi_inputdev);
1300 mutex_unlock(&tpacpi_inputdev_send_mutex);
1304 static void tpacpi_input_send_key(unsigned int scancode)
1306 unsigned int keycode;
1308 keycode = hotkey_keycode_map[scancode];
1310 if (keycode != KEY_RESERVED) {
1311 mutex_lock(&tpacpi_inputdev_send_mutex);
1313 input_report_key(tpacpi_inputdev, keycode, 1);
1314 if (keycode == KEY_UNKNOWN)
1315 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1317 input_sync(tpacpi_inputdev);
1319 input_report_key(tpacpi_inputdev, keycode, 0);
1320 if (keycode == KEY_UNKNOWN)
1321 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1323 input_sync(tpacpi_inputdev);
1325 mutex_unlock(&tpacpi_inputdev_send_mutex);
1329 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1330 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1332 static void tpacpi_hotkey_send_key(unsigned int scancode)
1334 tpacpi_input_send_key(scancode);
1335 if (hotkey_report_mode < 2) {
1336 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1337 0x80, 0x1001 + scancode);
1341 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1345 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1346 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1347 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1348 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1349 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1350 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1352 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1353 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1354 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1356 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1357 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1358 n->displayexp_toggle =
1359 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1361 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1362 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1363 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1364 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1365 n->brightness_toggle =
1366 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1368 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1369 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1370 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1371 >> TP_NVRAM_POS_LEVEL_VOLUME;
1372 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1373 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1377 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1379 if ((mask & (1 << __scancode)) && \
1380 oldn->__member != newn->__member) \
1381 tpacpi_hotkey_send_key(__scancode); \
1384 #define TPACPI_MAY_SEND_KEY(__scancode) \
1385 do { if (mask & (1 << __scancode)) \
1386 tpacpi_hotkey_send_key(__scancode); } while (0)
1388 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1389 struct tp_nvram_state *newn,
1392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1394 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1399 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1402 if (oldn->volume_toggle != newn->volume_toggle) {
1403 if (oldn->mute != newn->mute) {
1404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1406 if (oldn->volume_level > newn->volume_level) {
1407 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1408 } else if (oldn->volume_level < newn->volume_level) {
1409 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1410 } else if (oldn->mute == newn->mute) {
1411 /* repeated key presses that didn't change state */
1413 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1414 } else if (newn->volume_level != 0) {
1415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1417 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1422 /* handle brightness */
1423 if (oldn->brightness_toggle != newn->brightness_toggle) {
1424 if (oldn->brightness_level < newn->brightness_level) {
1425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1426 } else if (oldn->brightness_level > newn->brightness_level) {
1427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1429 /* repeated key presses that didn't change state */
1430 if (newn->brightness_level != 0) {
1431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1439 #undef TPACPI_COMPARE_KEY
1440 #undef TPACPI_MAY_SEND_KEY
1442 static int hotkey_kthread(void *data)
1444 struct tp_nvram_state s[2];
1446 unsigned int si, so;
1448 unsigned int change_detector, must_reset;
1450 mutex_lock(&hotkey_thread_mutex);
1452 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1461 /* Initial state for compares */
1462 mutex_lock(&hotkey_thread_data_mutex);
1463 change_detector = hotkey_config_change;
1464 mask = hotkey_source_mask & hotkey_mask;
1465 mutex_unlock(&hotkey_thread_data_mutex);
1466 hotkey_read_nvram(&s[so], mask);
1468 while (!kthread_should_stop() && hotkey_poll_freq) {
1470 t = 1000/hotkey_poll_freq;
1471 t = msleep_interruptible(t);
1472 if (unlikely(kthread_should_stop()))
1474 must_reset = try_to_freeze();
1475 if (t > 0 && !must_reset)
1478 mutex_lock(&hotkey_thread_data_mutex);
1479 if (must_reset || hotkey_config_change != change_detector) {
1480 /* forget old state on thaw or config change */
1483 change_detector = hotkey_config_change;
1485 mask = hotkey_source_mask & hotkey_mask;
1486 mutex_unlock(&hotkey_thread_data_mutex);
1489 hotkey_read_nvram(&s[si], mask);
1490 if (likely(si != so)) {
1491 hotkey_compare_and_issue_event(&s[so], &s[si],
1501 mutex_unlock(&hotkey_thread_mutex);
1505 static void hotkey_poll_stop_sync(void)
1507 if (tpacpi_hotkey_task) {
1508 if (frozen(tpacpi_hotkey_task) ||
1509 freezing(tpacpi_hotkey_task))
1510 thaw_process(tpacpi_hotkey_task);
1512 kthread_stop(tpacpi_hotkey_task);
1513 tpacpi_hotkey_task = NULL;
1514 mutex_lock(&hotkey_thread_mutex);
1515 /* at this point, the thread did exit */
1516 mutex_unlock(&hotkey_thread_mutex);
1520 /* call with hotkey_mutex held */
1521 static void hotkey_poll_setup(int may_warn)
1523 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1524 hotkey_poll_freq > 0 &&
1525 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1526 if (!tpacpi_hotkey_task) {
1527 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1528 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1529 if (IS_ERR(tpacpi_hotkey_task)) {
1530 tpacpi_hotkey_task = NULL;
1532 "could not create kernel thread "
1533 "for hotkey polling\n");
1537 hotkey_poll_stop_sync();
1539 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1540 printk(TPACPI_NOTICE
1541 "hot keys 0x%08x require polling, "
1542 "which is currently disabled\n",
1543 hotkey_source_mask);
1548 static void hotkey_poll_setup_safe(int may_warn)
1550 mutex_lock(&hotkey_mutex);
1551 hotkey_poll_setup(may_warn);
1552 mutex_unlock(&hotkey_mutex);
1555 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1557 static void hotkey_poll_setup_safe(int __unused)
1561 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1563 static int hotkey_inputdev_open(struct input_dev *dev)
1565 switch (tpacpi_lifecycle) {
1566 case TPACPI_LIFE_INIT:
1568 * hotkey_init will call hotkey_poll_setup_safe
1569 * at the appropriate moment
1572 case TPACPI_LIFE_EXITING:
1574 case TPACPI_LIFE_RUNNING:
1575 hotkey_poll_setup_safe(0);
1579 /* Should only happen if tpacpi_lifecycle is corrupt */
1584 static void hotkey_inputdev_close(struct input_dev *dev)
1586 /* disable hotkey polling when possible */
1587 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1588 hotkey_poll_setup_safe(0);
1591 /* sysfs hotkey enable ------------------------------------------------- */
1592 static ssize_t hotkey_enable_show(struct device *dev,
1593 struct device_attribute *attr,
1598 res = hotkey_status_get(&status);
1602 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1605 static ssize_t hotkey_enable_store(struct device *dev,
1606 struct device_attribute *attr,
1607 const char *buf, size_t count)
1612 if (parse_strtoul(buf, 1, &t))
1615 res = hotkey_status_set(t);
1617 return (res) ? res : count;
1620 static struct device_attribute dev_attr_hotkey_enable =
1621 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1622 hotkey_enable_show, hotkey_enable_store);
1624 /* sysfs hotkey mask --------------------------------------------------- */
1625 static ssize_t hotkey_mask_show(struct device *dev,
1626 struct device_attribute *attr,
1631 if (mutex_lock_interruptible(&hotkey_mutex))
1632 return -ERESTARTSYS;
1633 res = hotkey_mask_get();
1634 mutex_unlock(&hotkey_mutex);
1637 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1640 static ssize_t hotkey_mask_store(struct device *dev,
1641 struct device_attribute *attr,
1642 const char *buf, size_t count)
1647 if (parse_strtoul(buf, 0xffffffffUL, &t))
1650 if (mutex_lock_interruptible(&hotkey_mutex))
1651 return -ERESTARTSYS;
1653 res = hotkey_mask_set(t);
1655 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1656 hotkey_poll_setup(1);
1659 mutex_unlock(&hotkey_mutex);
1661 return (res) ? res : count;
1664 static struct device_attribute dev_attr_hotkey_mask =
1665 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1666 hotkey_mask_show, hotkey_mask_store);
1668 /* sysfs hotkey bios_enabled ------------------------------------------- */
1669 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1670 struct device_attribute *attr,
1673 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1676 static struct device_attribute dev_attr_hotkey_bios_enabled =
1677 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1679 /* sysfs hotkey bios_mask ---------------------------------------------- */
1680 static ssize_t hotkey_bios_mask_show(struct device *dev,
1681 struct device_attribute *attr,
1684 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1687 static struct device_attribute dev_attr_hotkey_bios_mask =
1688 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1690 /* sysfs hotkey all_mask ----------------------------------------------- */
1691 static ssize_t hotkey_all_mask_show(struct device *dev,
1692 struct device_attribute *attr,
1695 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1696 hotkey_all_mask | hotkey_source_mask);
1699 static struct device_attribute dev_attr_hotkey_all_mask =
1700 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1702 /* sysfs hotkey recommended_mask --------------------------------------- */
1703 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1704 struct device_attribute *attr,
1707 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1708 (hotkey_all_mask | hotkey_source_mask)
1709 & ~hotkey_reserved_mask);
1712 static struct device_attribute dev_attr_hotkey_recommended_mask =
1713 __ATTR(hotkey_recommended_mask, S_IRUGO,
1714 hotkey_recommended_mask_show, NULL);
1716 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1718 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1719 static ssize_t hotkey_source_mask_show(struct device *dev,
1720 struct device_attribute *attr,
1723 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1726 static ssize_t hotkey_source_mask_store(struct device *dev,
1727 struct device_attribute *attr,
1728 const char *buf, size_t count)
1732 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1733 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1736 if (mutex_lock_interruptible(&hotkey_mutex))
1737 return -ERESTARTSYS;
1739 HOTKEY_CONFIG_CRITICAL_START
1740 hotkey_source_mask = t;
1741 HOTKEY_CONFIG_CRITICAL_END
1743 hotkey_poll_setup(1);
1745 mutex_unlock(&hotkey_mutex);
1750 static struct device_attribute dev_attr_hotkey_source_mask =
1751 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1752 hotkey_source_mask_show, hotkey_source_mask_store);
1754 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1755 static ssize_t hotkey_poll_freq_show(struct device *dev,
1756 struct device_attribute *attr,
1759 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1762 static ssize_t hotkey_poll_freq_store(struct device *dev,
1763 struct device_attribute *attr,
1764 const char *buf, size_t count)
1768 if (parse_strtoul(buf, 25, &t))
1771 if (mutex_lock_interruptible(&hotkey_mutex))
1772 return -ERESTARTSYS;
1774 hotkey_poll_freq = t;
1776 hotkey_poll_setup(1);
1777 mutex_unlock(&hotkey_mutex);
1782 static struct device_attribute dev_attr_hotkey_poll_freq =
1783 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1784 hotkey_poll_freq_show, hotkey_poll_freq_store);
1786 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1788 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1789 static ssize_t hotkey_radio_sw_show(struct device *dev,
1790 struct device_attribute *attr,
1794 res = hotkey_get_wlsw(&s);
1798 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1801 static struct device_attribute dev_attr_hotkey_radio_sw =
1802 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1804 static void hotkey_radio_sw_notify_change(void)
1806 if (tp_features.hotkey_wlsw)
1807 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1811 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1812 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1813 struct device_attribute *attr,
1817 res = hotkey_get_tablet_mode(&s);
1821 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1824 static struct device_attribute dev_attr_hotkey_tablet_mode =
1825 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1827 static void hotkey_tablet_mode_notify_change(void)
1829 if (tp_features.hotkey_tablet)
1830 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1831 "hotkey_tablet_mode");
1834 /* sysfs hotkey report_mode -------------------------------------------- */
1835 static ssize_t hotkey_report_mode_show(struct device *dev,
1836 struct device_attribute *attr,
1839 return snprintf(buf, PAGE_SIZE, "%d\n",
1840 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1843 static struct device_attribute dev_attr_hotkey_report_mode =
1844 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1846 /* sysfs wakeup reason (pollable) -------------------------------------- */
1847 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1848 struct device_attribute *attr,
1851 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1854 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1855 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1857 static void hotkey_wakeup_reason_notify_change(void)
1859 if (tp_features.hotkey_mask)
1860 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1864 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1865 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1866 struct device_attribute *attr,
1869 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1872 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1873 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1874 hotkey_wakeup_hotunplug_complete_show, NULL);
1876 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1878 if (tp_features.hotkey_mask)
1879 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1880 "wakeup_hotunplug_complete");
1883 /* --------------------------------------------------------------------- */
1885 static struct attribute *hotkey_attributes[] __initdata = {
1886 &dev_attr_hotkey_enable.attr,
1887 &dev_attr_hotkey_bios_enabled.attr,
1888 &dev_attr_hotkey_report_mode.attr,
1889 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1890 &dev_attr_hotkey_mask.attr,
1891 &dev_attr_hotkey_all_mask.attr,
1892 &dev_attr_hotkey_recommended_mask.attr,
1893 &dev_attr_hotkey_source_mask.attr,
1894 &dev_attr_hotkey_poll_freq.attr,
1898 static struct attribute *hotkey_mask_attributes[] __initdata = {
1899 &dev_attr_hotkey_bios_mask.attr,
1900 #ifndef 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,
1905 &dev_attr_hotkey_wakeup_reason.attr,
1906 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1909 static void tpacpi_send_radiosw_update(void)
1913 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1914 mutex_lock(&tpacpi_inputdev_send_mutex);
1916 input_report_switch(tpacpi_inputdev,
1917 SW_RFKILL_ALL, !!wlsw);
1918 input_sync(tpacpi_inputdev);
1920 mutex_unlock(&tpacpi_inputdev_send_mutex);
1922 hotkey_radio_sw_notify_change();
1925 static void hotkey_exit(void)
1927 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1928 hotkey_poll_stop_sync();
1931 if (hotkey_dev_attributes)
1932 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1934 kfree(hotkey_keycode_map);
1936 if (tp_features.hotkey) {
1937 dbg_printk(TPACPI_DBG_EXIT,
1938 "restoring original hot key mask\n");
1939 /* no short-circuit boolean operator below! */
1940 if ((hotkey_mask_set(hotkey_orig_mask) |
1941 hotkey_status_set(hotkey_orig_status)) != 0)
1943 "failed to restore hot key mask "
1944 "to BIOS defaults\n");
1948 static int __init hotkey_init(struct ibm_init_struct *iibm)
1950 /* Requirements for changing the default keymaps:
1952 * 1. Many of the keys are mapped to KEY_RESERVED for very
1953 * good reasons. Do not change them unless you have deep
1954 * knowledge on the IBM and Lenovo ThinkPad firmware for
1955 * the various ThinkPad models. The driver behaves
1956 * differently for KEY_RESERVED: such keys have their
1957 * hot key mask *unset* in mask_recommended, and also
1958 * in the initial hot key mask programmed into the
1959 * firmware at driver load time, which means the firm-
1960 * ware may react very differently if you change them to
1963 * 2. You must be subscribed to the linux-thinkpad and
1964 * ibm-acpi-devel mailing lists, and you should read the
1965 * list archives since 2007 if you want to change the
1966 * keymaps. This requirement exists so that you will
1967 * know the past history of problems with the thinkpad-
1968 * acpi driver keymaps, and also that you will be
1969 * listening to any bug reports;
1971 * 3. Do not send thinkpad-acpi specific patches directly to
1972 * for merging, *ever*. Send them to the linux-acpi
1973 * mailinglist for comments. Merging is to be done only
1974 * through acpi-test and the ACPI maintainer.
1976 * If the above is too much to ask, don't change the keymap.
1977 * Ask the thinkpad-acpi maintainer to do it, instead.
1979 static u16 ibm_keycode_map[] __initdata = {
1980 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1981 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1982 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1983 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1985 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1986 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1987 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1988 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1990 /* brightness: firmware always reacts to them, unless
1991 * X.org did some tricks in the radeon BIOS scratch
1992 * registers of *some* models */
1993 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1994 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1996 /* Thinklight: firmware always react to it */
1997 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1999 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2000 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2002 /* Volume: firmware always react to it and reprograms
2003 * the built-in *extra* mixer. Never map it to control
2004 * another mixer by default. */
2005 KEY_RESERVED, /* 0x14: VOLUME UP */
2006 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2007 KEY_RESERVED, /* 0x16: MUTE */
2009 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2011 /* (assignments unknown, please report if found) */
2012 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2013 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2015 static u16 lenovo_keycode_map[] __initdata = {
2016 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2017 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2018 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2019 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2021 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2022 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2023 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2024 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2026 /* These either have to go through ACPI video, or
2027 * act like in the IBM ThinkPads, so don't ever
2028 * enable them by default */
2029 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2030 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2032 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2034 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2035 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2037 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2038 * react to it and reprograms the built-in *extra* mixer.
2039 * Never map it to control another mixer by default.
2041 * T60?, T61, R60?, R61: firmware and EC tries to send
2042 * these over the regular keyboard, so these are no-ops,
2043 * but there are still weird bugs re. MUTE, so do not
2044 * change unless you get test reports from all Lenovo
2045 * models. May cause the BIOS to interfere with the
2048 KEY_RESERVED, /* 0x14: VOLUME UP */
2049 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2050 KEY_RESERVED, /* 0x16: MUTE */
2052 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2054 /* (assignments unknown, please report if found) */
2055 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2056 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2059 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2060 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2061 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2067 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2069 BUG_ON(!tpacpi_inputdev);
2070 BUG_ON(tpacpi_inputdev->open != NULL ||
2071 tpacpi_inputdev->close != NULL);
2073 TPACPI_ACPIHANDLE_INIT(hkey);
2074 mutex_init(&hotkey_mutex);
2076 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2077 mutex_init(&hotkey_thread_mutex);
2078 mutex_init(&hotkey_thread_data_mutex);
2081 /* hotkey not supported on 570 */
2082 tp_features.hotkey = hkey_handle != NULL;
2084 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2085 str_supported(tp_features.hotkey));
2087 if (!tp_features.hotkey)
2090 hotkey_dev_attributes = create_attr_set(13, NULL);
2091 if (!hotkey_dev_attributes)
2093 res = add_many_to_attr_set(hotkey_dev_attributes,
2095 ARRAY_SIZE(hotkey_attributes));
2099 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2100 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2101 for HKEY interface version 0x100 */
2102 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2103 if ((hkeyv >> 8) != 1) {
2104 printk(TPACPI_ERR "unknown version of the "
2105 "HKEY interface: 0x%x\n", hkeyv);
2106 printk(TPACPI_ERR "please report this to %s\n",
2110 * MHKV 0x100 in A31, R40, R40e,
2111 * T4x, X31, and later
2113 tp_features.hotkey_mask = 1;
2117 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2118 str_supported(tp_features.hotkey_mask));
2120 if (tp_features.hotkey_mask) {
2121 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2124 "missing MHKA handler, "
2125 "please report this to %s\n",
2127 /* FN+F12, FN+F4, FN+F3 */
2128 hotkey_all_mask = 0x080cU;
2132 /* hotkey_source_mask *must* be zero for
2133 * the first hotkey_mask_get */
2134 res = hotkey_status_get(&hotkey_orig_status);
2138 if (tp_features.hotkey_mask) {
2139 res = hotkey_mask_get();
2143 hotkey_orig_mask = hotkey_mask;
2144 res = add_many_to_attr_set(
2145 hotkey_dev_attributes,
2146 hotkey_mask_attributes,
2147 ARRAY_SIZE(hotkey_mask_attributes));
2152 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2153 if (tp_features.hotkey_mask) {
2154 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2157 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2160 vdbg_printk(TPACPI_DBG_INIT,
2161 "hotkey source mask 0x%08x, polling freq %d\n",
2162 hotkey_source_mask, hotkey_poll_freq);
2165 /* Not all thinkpads have a hardware radio switch */
2166 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2167 tp_features.hotkey_wlsw = 1;
2169 "radio switch found; radios are %s\n",
2170 enabled(status, 0));
2172 if (tp_features.hotkey_wlsw)
2173 res = add_to_attr_set(hotkey_dev_attributes,
2174 &dev_attr_hotkey_radio_sw.attr);
2176 /* For X41t, X60t, X61t Tablets... */
2177 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2178 tp_features.hotkey_tablet = 1;
2180 "possible tablet mode switch found; "
2181 "ThinkPad in %s mode\n",
2182 (status & TP_HOTKEY_TABLET_MASK)?
2183 "tablet" : "laptop");
2184 res = add_to_attr_set(hotkey_dev_attributes,
2185 &dev_attr_hotkey_tablet_mode.attr);
2189 res = register_attr_set_with_sysfs(
2190 hotkey_dev_attributes,
2191 &tpacpi_pdev->dev.kobj);
2195 /* Set up key map */
2197 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2199 if (!hotkey_keycode_map) {
2201 "failed to allocate memory for key map\n");
2206 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2207 dbg_printk(TPACPI_DBG_INIT,
2208 "using Lenovo default hot key map\n");
2209 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2210 TPACPI_HOTKEY_MAP_SIZE);
2212 dbg_printk(TPACPI_DBG_INIT,
2213 "using IBM default hot key map\n");
2214 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2215 TPACPI_HOTKEY_MAP_SIZE);
2218 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2219 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2220 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2221 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2222 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2223 tpacpi_inputdev->keycode = hotkey_keycode_map;
2224 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2225 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2226 set_bit(hotkey_keycode_map[i],
2227 tpacpi_inputdev->keybit);
2229 if (i < sizeof(hotkey_reserved_mask)*8)
2230 hotkey_reserved_mask |= 1 << i;
2234 if (tp_features.hotkey_wlsw) {
2235 set_bit(EV_SW, tpacpi_inputdev->evbit);
2236 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2238 if (tp_features.hotkey_tablet) {
2239 set_bit(EV_SW, tpacpi_inputdev->evbit);
2240 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2243 /* Do not issue duplicate brightness change events to
2245 if (!tp_features.bright_acpimode)
2246 /* update bright_acpimode... */
2247 tpacpi_check_std_acpi_brightness_support();
2249 if (tp_features.bright_acpimode) {
2251 "This ThinkPad has standard ACPI backlight "
2252 "brightness control, supported by the ACPI "
2254 printk(TPACPI_NOTICE
2255 "Disabling thinkpad-acpi brightness events "
2258 /* The hotkey_reserved_mask change below is not
2259 * necessary while the keys are at KEY_RESERVED in the
2260 * default map, but better safe than sorry, leave it
2261 * here as a marker of what we have to do, especially
2262 * when we finally become able to set this at runtime
2263 * on response to X.org requests */
2264 hotkey_reserved_mask |=
2265 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2266 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2269 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2270 res = hotkey_status_set(1);
2275 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2276 & ~hotkey_reserved_mask)
2277 | hotkey_orig_mask);
2278 if (res < 0 && res != -ENXIO) {
2283 dbg_printk(TPACPI_DBG_INIT,
2284 "legacy hot key reporting over procfs %s\n",
2285 (hotkey_report_mode < 2) ?
2286 "enabled" : "disabled");
2288 tpacpi_inputdev->open = &hotkey_inputdev_open;
2289 tpacpi_inputdev->close = &hotkey_inputdev_close;
2291 hotkey_poll_setup_safe(1);
2292 tpacpi_send_radiosw_update();
2293 tpacpi_input_send_tabletsw();
2298 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2299 hotkey_dev_attributes = NULL;
2301 return (res < 0)? res : 1;
2304 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2307 unsigned int scancode;
2312 if (event != 0x80) {
2314 "unknown HKEY notification event %d\n", event);
2315 /* forward it to userspace, maybe it knows how to handle it */
2316 acpi_bus_generate_netlink_event(
2317 ibm->acpi->device->pnp.device_class,
2318 ibm->acpi->device->dev.bus_id,
2324 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2325 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2338 switch (hkey >> 12) {
2340 /* 0x1000-0x1FFF: key presses */
2341 scancode = hkey & 0xfff;
2342 if (scancode > 0 && scancode < 0x21) {
2344 if (!(hotkey_source_mask & (1 << scancode))) {
2345 tpacpi_input_send_key(scancode);
2357 case 0x2304: /* suspend, undock */
2358 case 0x2404: /* hibernation, undock */
2359 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2362 case 0x2305: /* suspend, bay eject */
2363 case 0x2405: /* hibernation, bay eject */
2364 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2370 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2372 "woke up due to a hot-unplug "
2374 hotkey_wakeup_reason_notify_change();
2378 /* bay-related wakeups */
2379 if (hkey == 0x3003) {
2380 hotkey_autosleep_ack = 1;
2383 hotkey_wakeup_hotunplug_complete_notify_change();
2389 /* dock-related wakeups */
2390 if (hkey == 0x4003) {
2391 hotkey_autosleep_ack = 1;
2394 hotkey_wakeup_hotunplug_complete_notify_change();
2400 /* 0x5000-0x5FFF: human interface helpers */
2402 case 0x5010: /* Lenovo new BIOS: brightness changed */
2403 case 0x500b: /* X61t: tablet pen inserted into bay */
2404 case 0x500c: /* X61t: tablet pen removed from bay */
2406 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2407 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2408 tpacpi_input_send_tabletsw();
2409 hotkey_tablet_mode_notify_change();
2414 /* LID switch events. Do not propagate */
2422 /* 0x7000-0x7FFF: misc */
2423 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2424 tpacpi_send_radiosw_update();
2428 /* fallthrough to default */
2433 printk(TPACPI_NOTICE
2434 "unhandled HKEY event 0x%04x\n", hkey);
2438 if (!ignore_acpi_ev &&
2439 (send_acpi_ev || hotkey_report_mode < 2)) {
2440 acpi_bus_generate_proc_event(ibm->acpi->device,
2444 /* netlink events */
2445 if (!ignore_acpi_ev && send_acpi_ev) {
2446 acpi_bus_generate_netlink_event(
2447 ibm->acpi->device->pnp.device_class,
2448 ibm->acpi->device->dev.bus_id,
2454 static void hotkey_suspend(pm_message_t state)
2456 /* Do these on suspend, we get the events on early resume! */
2457 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2458 hotkey_autosleep_ack = 0;
2461 static void hotkey_resume(void)
2463 if (hotkey_mask_get())
2465 "error while trying to read hot key mask "
2467 tpacpi_send_radiosw_update();
2468 hotkey_tablet_mode_notify_change();
2469 hotkey_wakeup_reason_notify_change();
2470 hotkey_wakeup_hotunplug_complete_notify_change();
2471 hotkey_poll_setup_safe(0);
2474 /* procfs -------------------------------------------------------------- */
2475 static int hotkey_read(char *p)
2480 if (!tp_features.hotkey) {
2481 len += sprintf(p + len, "status:\t\tnot supported\n");
2485 if (mutex_lock_interruptible(&hotkey_mutex))
2486 return -ERESTARTSYS;
2487 res = hotkey_status_get(&status);
2489 res = hotkey_mask_get();
2490 mutex_unlock(&hotkey_mutex);
2494 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2495 if (tp_features.hotkey_mask) {
2496 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2497 len += sprintf(p + len,
2498 "commands:\tenable, disable, reset, <mask>\n");
2500 len += sprintf(p + len, "mask:\t\tnot supported\n");
2501 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2507 static int hotkey_write(char *buf)
2513 if (!tp_features.hotkey)
2516 if (mutex_lock_interruptible(&hotkey_mutex))
2517 return -ERESTARTSYS;
2523 while ((cmd = next_cmd(&buf))) {
2524 if (strlencmp(cmd, "enable") == 0) {
2526 } else if (strlencmp(cmd, "disable") == 0) {
2528 } else if (strlencmp(cmd, "reset") == 0) {
2529 status = hotkey_orig_status;
2530 mask = hotkey_orig_mask;
2531 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2533 } else if (sscanf(cmd, "%x", &mask) == 1) {
2541 res = hotkey_status_set(status);
2543 if (!res && mask != hotkey_mask)
2544 res = hotkey_mask_set(mask);
2547 mutex_unlock(&hotkey_mutex);
2551 static const struct acpi_device_id ibm_htk_device_ids[] = {
2552 {TPACPI_ACPI_HKEY_HID, 0},
2556 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2557 .hid = ibm_htk_device_ids,
2558 .notify = hotkey_notify,
2559 .handle = &hkey_handle,
2560 .type = ACPI_DEVICE_NOTIFY,
2563 static struct ibm_struct hotkey_driver_data = {
2565 .read = hotkey_read,
2566 .write = hotkey_write,
2567 .exit = hotkey_exit,
2568 .resume = hotkey_resume,
2569 .suspend = hotkey_suspend,
2570 .acpi = &ibm_hotkey_acpidriver,
2573 /*************************************************************************
2574 * Bluetooth subdriver
2578 /* ACPI GBDC/SBDC bits */
2579 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2580 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2581 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2584 static int bluetooth_get_radiosw(void)
2588 if (!tp_features.bluetooth)
2591 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2592 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2595 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2598 return (status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0;
2601 static int bluetooth_set_radiosw(int radio_on)
2605 if (!tp_features.bluetooth)
2608 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2609 * reason to risk weird behaviour. */
2610 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2614 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2617 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2619 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2620 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2626 /* sysfs bluetooth enable ---------------------------------------------- */
2627 static ssize_t bluetooth_enable_show(struct device *dev,
2628 struct device_attribute *attr,
2633 status = bluetooth_get_radiosw();
2637 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2640 static ssize_t bluetooth_enable_store(struct device *dev,
2641 struct device_attribute *attr,
2642 const char *buf, size_t count)
2647 if (parse_strtoul(buf, 1, &t))
2650 res = bluetooth_set_radiosw(t);
2652 return (res) ? res : count;
2655 static struct device_attribute dev_attr_bluetooth_enable =
2656 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2657 bluetooth_enable_show, bluetooth_enable_store);
2659 /* --------------------------------------------------------------------- */
2661 static struct attribute *bluetooth_attributes[] = {
2662 &dev_attr_bluetooth_enable.attr,
2666 static const struct attribute_group bluetooth_attr_group = {
2667 .attrs = bluetooth_attributes,
2670 static void bluetooth_exit(void)
2672 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2673 &bluetooth_attr_group);
2676 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2681 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2683 TPACPI_ACPIHANDLE_INIT(hkey);
2685 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2686 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2687 tp_features.bluetooth = hkey_handle &&
2688 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2690 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2691 str_supported(tp_features.bluetooth),
2694 if (tp_features.bluetooth &&
2695 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2696 /* no bluetooth hardware present in system */
2697 tp_features.bluetooth = 0;
2698 dbg_printk(TPACPI_DBG_INIT,
2699 "bluetooth hardware not installed\n");
2702 if (tp_features.bluetooth) {
2703 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2704 &bluetooth_attr_group);
2709 return (tp_features.bluetooth)? 0 : 1;
2712 /* procfs -------------------------------------------------------------- */
2713 static int bluetooth_read(char *p)
2716 int status = bluetooth_get_radiosw();
2718 if (!tp_features.bluetooth)
2719 len += sprintf(p + len, "status:\t\tnot supported\n");
2721 len += sprintf(p + len, "status:\t\t%s\n",
2722 (status)? "enabled" : "disabled");
2723 len += sprintf(p + len, "commands:\tenable, disable\n");
2729 static int bluetooth_write(char *buf)
2733 if (!tp_features.bluetooth)
2736 while ((cmd = next_cmd(&buf))) {
2737 if (strlencmp(cmd, "enable") == 0) {
2738 bluetooth_set_radiosw(1);
2739 } else if (strlencmp(cmd, "disable") == 0) {
2740 bluetooth_set_radiosw(0);
2748 static struct ibm_struct bluetooth_driver_data = {
2749 .name = "bluetooth",
2750 .read = bluetooth_read,
2751 .write = bluetooth_write,
2752 .exit = bluetooth_exit,
2755 /*************************************************************************
2760 /* ACPI GWAN/SWAN bits */
2761 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2762 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2763 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2766 static int wan_get_radiosw(void)
2770 if (!tp_features.wan)
2773 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2774 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2777 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2780 return (status & TP_ACPI_WANCARD_RADIOSSW) != 0;
2783 static int wan_set_radiosw(int radio_on)
2787 if (!tp_features.wan)
2790 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2791 * reason to risk weird behaviour. */
2792 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2796 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2799 status |= TP_ACPI_WANCARD_RADIOSSW;
2801 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2802 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2808 /* sysfs wan enable ---------------------------------------------------- */
2809 static ssize_t wan_enable_show(struct device *dev,
2810 struct device_attribute *attr,
2815 status = wan_get_radiosw();
2819 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2822 static ssize_t wan_enable_store(struct device *dev,
2823 struct device_attribute *attr,
2824 const char *buf, size_t count)
2829 if (parse_strtoul(buf, 1, &t))
2832 res = wan_set_radiosw(t);
2834 return (res) ? res : count;
2837 static struct device_attribute dev_attr_wan_enable =
2838 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2839 wan_enable_show, wan_enable_store);
2841 /* --------------------------------------------------------------------- */
2843 static struct attribute *wan_attributes[] = {
2844 &dev_attr_wan_enable.attr,
2848 static const struct attribute_group wan_attr_group = {
2849 .attrs = wan_attributes,
2852 static void wan_exit(void)
2854 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2858 static int __init wan_init(struct ibm_init_struct *iibm)
2863 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2865 TPACPI_ACPIHANDLE_INIT(hkey);
2867 tp_features.wan = hkey_handle &&
2868 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2870 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2871 str_supported(tp_features.wan),
2874 if (tp_features.wan &&
2875 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
2876 /* no wan hardware present in system */
2877 tp_features.wan = 0;
2878 dbg_printk(TPACPI_DBG_INIT,
2879 "wan hardware not installed\n");
2882 if (tp_features.wan) {
2883 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2889 return (tp_features.wan)? 0 : 1;
2892 /* procfs -------------------------------------------------------------- */
2893 static int wan_read(char *p)
2896 int status = wan_get_radiosw();
2898 if (!tp_features.wan)
2899 len += sprintf(p + len, "status:\t\tnot supported\n");
2901 len += sprintf(p + len, "status:\t\t%s\n",
2902 (status)? "enabled" : "disabled");
2903 len += sprintf(p + len, "commands:\tenable, disable\n");
2909 static int wan_write(char *buf)
2913 if (!tp_features.wan)
2916 while ((cmd = next_cmd(&buf))) {
2917 if (strlencmp(cmd, "enable") == 0) {
2919 } else if (strlencmp(cmd, "disable") == 0) {
2928 static struct ibm_struct wan_driver_data = {
2933 .flags.experimental = 1,
2936 /*************************************************************************
2940 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2942 enum video_access_mode {
2943 TPACPI_VIDEO_NONE = 0,
2944 TPACPI_VIDEO_570, /* 570 */
2945 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2946 TPACPI_VIDEO_NEW, /* all others */
2949 enum { /* video status flags, based on VIDEO_570 */
2950 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2951 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2952 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2955 enum { /* TPACPI_VIDEO_570 constants */
2956 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2957 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2958 * video_status_flags */
2959 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2960 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2963 static enum video_access_mode video_supported;
2964 static int video_orig_autosw;
2966 static int video_autosw_get(void);
2967 static int video_autosw_set(int enable);
2969 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2971 static int __init video_init(struct ibm_init_struct *iibm)
2975 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2977 TPACPI_ACPIHANDLE_INIT(vid);
2978 TPACPI_ACPIHANDLE_INIT(vid2);
2980 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2981 /* G41, assume IVGA doesn't change */
2982 vid_handle = vid2_handle;
2985 /* video switching not supported on R30, R31 */
2986 video_supported = TPACPI_VIDEO_NONE;
2987 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2989 video_supported = TPACPI_VIDEO_570;
2990 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2991 /* 600e/x, 770e, 770x */
2992 video_supported = TPACPI_VIDEO_770;
2995 video_supported = TPACPI_VIDEO_NEW;
2997 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2998 str_supported(video_supported != TPACPI_VIDEO_NONE),
3001 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3004 static void video_exit(void)
3006 dbg_printk(TPACPI_DBG_EXIT,
3007 "restoring original video autoswitch mode\n");
3008 if (video_autosw_set(video_orig_autosw))
3009 printk(TPACPI_ERR "error while trying to restore original "
3010 "video autoswitch mode\n");
3013 static int video_outputsw_get(void)
3018 switch (video_supported) {
3019 case TPACPI_VIDEO_570:
3020 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3021 TP_ACPI_VIDEO_570_PHSCMD))
3023 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3025 case TPACPI_VIDEO_770:
3026 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3029 status |= TP_ACPI_VIDEO_S_LCD;
3030 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3033 status |= TP_ACPI_VIDEO_S_CRT;
3035 case TPACPI_VIDEO_NEW:
3036 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3037 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3040 status |= TP_ACPI_VIDEO_S_CRT;
3042 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3043 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3046 status |= TP_ACPI_VIDEO_S_LCD;
3047 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3050 status |= TP_ACPI_VIDEO_S_DVI;
3059 static int video_outputsw_set(int status)
3064 switch (video_supported) {
3065 case TPACPI_VIDEO_570:
3066 res = acpi_evalf(NULL, NULL,
3067 "\\_SB.PHS2", "vdd",
3068 TP_ACPI_VIDEO_570_PHS2CMD,
3069 status | TP_ACPI_VIDEO_570_PHS2SET);
3071 case TPACPI_VIDEO_770:
3072 autosw = video_autosw_get();
3076 res = video_autosw_set(1);
3079 res = acpi_evalf(vid_handle, NULL,
3080 "ASWT", "vdd", status * 0x100, 0);
3081 if (!autosw && video_autosw_set(autosw)) {
3083 "video auto-switch left enabled due to error\n");
3087 case TPACPI_VIDEO_NEW:
3088 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3089 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3095 return (res)? 0 : -EIO;
3098 static int video_autosw_get(void)
3102 switch (video_supported) {
3103 case TPACPI_VIDEO_570:
3104 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3107 case TPACPI_VIDEO_770:
3108 case TPACPI_VIDEO_NEW:
3109 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3119 static int video_autosw_set(int enable)
3121 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3126 static int video_outputsw_cycle(void)
3128 int autosw = video_autosw_get();
3134 switch (video_supported) {
3135 case TPACPI_VIDEO_570:
3136 res = video_autosw_set(1);
3139 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3141 case TPACPI_VIDEO_770:
3142 case TPACPI_VIDEO_NEW:
3143 res = video_autosw_set(1);
3146 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3151 if (!autosw && video_autosw_set(autosw)) {
3153 "video auto-switch left enabled due to error\n");
3157 return (res)? 0 : -EIO;
3160 static int video_expand_toggle(void)
3162 switch (video_supported) {
3163 case TPACPI_VIDEO_570:
3164 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3166 case TPACPI_VIDEO_770:
3167 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3169 case TPACPI_VIDEO_NEW:
3170 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3178 static int video_read(char *p)
3183 if (video_supported == TPACPI_VIDEO_NONE) {
3184 len += sprintf(p + len, "status:\t\tnot supported\n");
3188 status = video_outputsw_get();
3192 autosw = video_autosw_get();
3196 len += sprintf(p + len, "status:\t\tsupported\n");
3197 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3198 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3199 if (video_supported == TPACPI_VIDEO_NEW)
3200 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3201 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3202 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3203 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3204 if (video_supported == TPACPI_VIDEO_NEW)
3205 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3206 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3207 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3212 static int video_write(char *buf)
3215 int enable, disable, status;
3218 if (video_supported == TPACPI_VIDEO_NONE)
3224 while ((cmd = next_cmd(&buf))) {
3225 if (strlencmp(cmd, "lcd_enable") == 0) {
3226 enable |= TP_ACPI_VIDEO_S_LCD;
3227 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3228 disable |= TP_ACPI_VIDEO_S_LCD;
3229 } else if (strlencmp(cmd, "crt_enable") == 0) {
3230 enable |= TP_ACPI_VIDEO_S_CRT;
3231 } else if (strlencmp(cmd, "crt_disable") == 0) {
3232 disable |= TP_ACPI_VIDEO_S_CRT;
3233 } else if (video_supported == TPACPI_VIDEO_NEW &&
3234 strlencmp(cmd, "dvi_enable") == 0) {
3235 enable |= TP_ACPI_VIDEO_S_DVI;
3236 } else if (video_supported == TPACPI_VIDEO_NEW &&
3237 strlencmp(cmd, "dvi_disable") == 0) {
3238 disable |= TP_ACPI_VIDEO_S_DVI;
3239 } else if (strlencmp(cmd, "auto_enable") == 0) {
3240 res = video_autosw_set(1);
3243 } else if (strlencmp(cmd, "auto_disable") == 0) {
3244 res = video_autosw_set(0);
3247 } else if (strlencmp(cmd, "video_switch") == 0) {
3248 res = video_outputsw_cycle();
3251 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3252 res = video_expand_toggle();
3259 if (enable || disable) {
3260 status = video_outputsw_get();
3263 res = video_outputsw_set((status & ~disable) | enable);
3271 static struct ibm_struct video_driver_data = {
3274 .write = video_write,
3278 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3280 /*************************************************************************
3281 * Light (thinklight) subdriver
3284 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3285 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3287 static int light_get_status(void)
3291 if (tp_features.light_status) {
3292 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3300 static int light_set_status(int status)
3304 if (tp_features.light) {
3306 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3308 TP_CMOS_THINKLIGHT_ON :
3309 TP_CMOS_THINKLIGHT_OFF);
3311 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3314 return (rc)? 0 : -EIO;
3320 static void light_set_status_worker(struct work_struct *work)
3322 struct tpacpi_led_classdev *data =
3323 container_of(work, struct tpacpi_led_classdev, work);
3325 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3326 light_set_status((data->new_brightness != LED_OFF));
3329 static void light_sysfs_set(struct led_classdev *led_cdev,
3330 enum led_brightness brightness)
3332 struct tpacpi_led_classdev *data =
3333 container_of(led_cdev,
3334 struct tpacpi_led_classdev,
3336 data->new_brightness = brightness;
3337 queue_work(tpacpi_wq, &data->work);
3340 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3342 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3345 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3347 .name = "tpacpi::thinklight",
3348 .brightness_set = &light_sysfs_set,
3349 .brightness_get = &light_sysfs_get,
3353 static int __init light_init(struct ibm_init_struct *iibm)
3357 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3359 TPACPI_ACPIHANDLE_INIT(ledb);
3360 TPACPI_ACPIHANDLE_INIT(lght);
3361 TPACPI_ACPIHANDLE_INIT(cmos);
3362 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3364 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3365 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3367 if (tp_features.light)
3368 /* light status not supported on
3369 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3370 tp_features.light_status =
3371 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3373 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3374 str_supported(tp_features.light),
3375 str_supported(tp_features.light_status));
3377 if (!tp_features.light)
3380 rc = led_classdev_register(&tpacpi_pdev->dev,
3381 &tpacpi_led_thinklight.led_classdev);
3384 tp_features.light = 0;
3385 tp_features.light_status = 0;
3393 static void light_exit(void)
3395 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3396 if (work_pending(&tpacpi_led_thinklight.work))
3397 flush_workqueue(tpacpi_wq);
3400 static int light_read(char *p)
3405 if (!tp_features.light) {
3406 len += sprintf(p + len, "status:\t\tnot supported\n");
3407 } else if (!tp_features.light_status) {
3408 len += sprintf(p + len, "status:\t\tunknown\n");
3409 len += sprintf(p + len, "commands:\ton, off\n");
3411 status = light_get_status();
3414 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3415 len += sprintf(p + len, "commands:\ton, off\n");
3421 static int light_write(char *buf)
3426 if (!tp_features.light)
3429 while ((cmd = next_cmd(&buf))) {
3430 if (strlencmp(cmd, "on") == 0) {
3432 } else if (strlencmp(cmd, "off") == 0) {
3438 return light_set_status(newstatus);
3441 static struct ibm_struct light_driver_data = {
3444 .write = light_write,
3448 /*************************************************************************
3452 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3454 static void dock_notify(struct ibm_struct *ibm, u32 event);
3455 static int dock_read(char *p);
3456 static int dock_write(char *buf);
3458 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3459 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3460 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3461 "\\_SB.PCI.ISA.SLCE", /* 570 */
3462 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3464 /* don't list other alternatives as we install a notify handler on the 570 */
3465 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3467 static const struct acpi_device_id ibm_pci_device_ids[] = {
3468 {PCI_ROOT_HID_STRING, 0},
3472 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3474 .notify = dock_notify,
3475 .handle = &dock_handle,
3476 .type = ACPI_SYSTEM_NOTIFY,
3479 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3480 * We just use it to get notifications of dock hotplug
3481 * in very old thinkpads */
3482 .hid = ibm_pci_device_ids,
3483 .notify = dock_notify,
3484 .handle = &pci_handle,
3485 .type = ACPI_SYSTEM_NOTIFY,
3489 static struct ibm_struct dock_driver_data[2] = {
3493 .write = dock_write,
3494 .acpi = &ibm_dock_acpidriver[0],
3498 .acpi = &ibm_dock_acpidriver[1],
3502 #define dock_docked() (_sta(dock_handle) & 1)
3504 static int __init dock_init(struct ibm_init_struct *iibm)
3506 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3508 TPACPI_ACPIHANDLE_INIT(dock);
3510 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3511 str_supported(dock_handle != NULL));
3513 return (dock_handle)? 0 : 1;
3516 static int __init dock_init2(struct ibm_init_struct *iibm)
3520 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3522 if (dock_driver_data[0].flags.acpi_driver_registered &&
3523 dock_driver_data[0].flags.acpi_notify_installed) {
3524 TPACPI_ACPIHANDLE_INIT(pci);
3525 dock2_needed = (pci_handle != NULL);
3526 vdbg_printk(TPACPI_DBG_INIT,
3527 "dock PCI handler for the TP 570 is %s\n",
3528 str_supported(dock2_needed));
3530 vdbg_printk(TPACPI_DBG_INIT,
3531 "dock subdriver part 2 not required\n");
3535 return (dock2_needed)? 0 : 1;
3538 static void dock_notify(struct ibm_struct *ibm, u32 event)
3540 int docked = dock_docked();
3541 int pci = ibm->acpi->hid && ibm->acpi->device &&
3542 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3545 if (event == 1 && !pci) /* 570 */
3546 data = 1; /* button */
3547 else if (event == 1 && pci) /* 570 */
3548 data = 3; /* dock */
3549 else if (event == 3 && docked)
3550 data = 1; /* button */
3551 else if (event == 3 && !docked)
3552 data = 2; /* undock */
3553 else if (event == 0 && docked)
3554 data = 3; /* dock */
3556 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3557 event, _sta(dock_handle));
3558 data = 0; /* unknown */
3560 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3561 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3562 ibm->acpi->device->dev.bus_id,
3566 static int dock_read(char *p)
3569 int docked = dock_docked();
3572 len += sprintf(p + len, "status:\t\tnot supported\n");
3574 len += sprintf(p + len, "status:\t\tundocked\n");
3576 len += sprintf(p + len, "status:\t\tdocked\n");
3577 len += sprintf(p + len, "commands:\tdock, undock\n");
3583 static int dock_write(char *buf)
3590 while ((cmd = next_cmd(&buf))) {
3591 if (strlencmp(cmd, "undock") == 0) {
3592 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3593 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3595 } else if (strlencmp(cmd, "dock") == 0) {
3596 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3605 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3607 /*************************************************************************
3611 #ifdef CONFIG_THINKPAD_ACPI_BAY
3613 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3614 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3615 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3616 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3617 ); /* A21e, R30, R31 */
3618 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3619 "_EJ0", /* all others */
3620 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3621 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3622 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3624 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3628 static int __init bay_init(struct ibm_init_struct *iibm)
3630 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3632 TPACPI_ACPIHANDLE_INIT(bay);
3634 TPACPI_ACPIHANDLE_INIT(bay_ej);
3635 TPACPI_ACPIHANDLE_INIT(bay2);
3637 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3639 tp_features.bay_status = bay_handle &&
3640 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3641 tp_features.bay_status2 = bay2_handle &&
3642 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3644 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3645 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3646 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3647 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3649 vdbg_printk(TPACPI_DBG_INIT,
3650 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3651 str_supported(tp_features.bay_status),
3652 str_supported(tp_features.bay_eject),
3653 str_supported(tp_features.bay_status2),
3654 str_supported(tp_features.bay_eject2));
3656 return (tp_features.bay_status || tp_features.bay_eject ||
3657 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3660 static void bay_notify(struct ibm_struct *ibm, u32 event)
3662 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3663 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3664 ibm->acpi->device->dev.bus_id,
3668 #define bay_occupied(b) (_sta(b##_handle) & 1)
3670 static int bay_read(char *p)
3673 int occupied = bay_occupied(bay);
3674 int occupied2 = bay_occupied(bay2);
3677 len += sprintf(p + len, "status:\t\t%s\n",
3678 tp_features.bay_status ?
3679 (occupied ? "occupied" : "unoccupied") :
3681 if (tp_features.bay_status2)
3682 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3683 "occupied" : "unoccupied");
3685 eject = tp_features.bay_eject && occupied;
3686 eject2 = tp_features.bay_eject2 && occupied2;
3688 if (eject && eject2)
3689 len += sprintf(p + len, "commands:\teject, eject2\n");
3691 len += sprintf(p + len, "commands:\teject\n");
3693 len += sprintf(p + len, "commands:\teject2\n");
3698 static int bay_write(char *buf)
3702 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3705 while ((cmd = next_cmd(&buf))) {
3706 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3707 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3709 } else if (tp_features.bay_eject2 &&
3710 strlencmp(cmd, "eject2") == 0) {
3711 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3720 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3721 .notify = bay_notify,
3722 .handle = &bay_handle,
3723 .type = ACPI_SYSTEM_NOTIFY,
3726 static struct ibm_struct bay_driver_data = {
3730 .acpi = &ibm_bay_acpidriver,
3733 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3735 /*************************************************************************
3739 /* sysfs cmos_command -------------------------------------------------- */
3740 static ssize_t cmos_command_store(struct device *dev,
3741 struct device_attribute *attr,
3742 const char *buf, size_t count)
3744 unsigned long cmos_cmd;
3747 if (parse_strtoul(buf, 21, &cmos_cmd))
3750 res = issue_thinkpad_cmos_command(cmos_cmd);
3751 return (res)? res : count;
3754 static struct device_attribute dev_attr_cmos_command =
3755 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3757 /* --------------------------------------------------------------------- */
3759 static int __init cmos_init(struct ibm_init_struct *iibm)
3763 vdbg_printk(TPACPI_DBG_INIT,
3764 "initializing cmos commands subdriver\n");
3766 TPACPI_ACPIHANDLE_INIT(cmos);
3768 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3769 str_supported(cmos_handle != NULL));
3771 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3775 return (cmos_handle)? 0 : 1;
3778 static void cmos_exit(void)
3780 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3783 static int cmos_read(char *p)
3787 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3788 R30, R31, T20-22, X20-21 */
3790 len += sprintf(p + len, "status:\t\tnot supported\n");
3792 len += sprintf(p + len, "status:\t\tsupported\n");
3793 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3799 static int cmos_write(char *buf)
3804 while ((cmd = next_cmd(&buf))) {
3805 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3806 cmos_cmd >= 0 && cmos_cmd <= 21) {
3811 res = issue_thinkpad_cmos_command(cmos_cmd);
3819 static struct ibm_struct cmos_driver_data = {
3822 .write = cmos_write,
3826 /*************************************************************************
3830 enum led_access_mode {
3831 TPACPI_LED_NONE = 0,
3832 TPACPI_LED_570, /* 570 */
3833 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3834 TPACPI_LED_NEW, /* all others */
3837 enum { /* For TPACPI_LED_OLD */
3838 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3839 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3840 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3849 static enum led_access_mode led_supported;
3851 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3852 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3853 /* T20-22, X20-21 */
3854 "LED", /* all others */
3857 #define TPACPI_LED_NUMLEDS 8
3858 static struct tpacpi_led_classdev *tpacpi_leds;
3859 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
3860 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
3861 /* there's a limit of 19 chars + NULL before 2.6.26 */
3863 "tpacpi:orange:batt",
3864 "tpacpi:green:batt",
3865 "tpacpi::dock_active",
3866 "tpacpi::bay_active",
3867 "tpacpi::dock_batt",
3868 "tpacpi::unknown_led",
3872 static int led_get_status(const unsigned int led)
3875 enum led_status_t led_s;
3877 switch (led_supported) {
3878 case TPACPI_LED_570:
3879 if (!acpi_evalf(ec_handle,
3880 &status, "GLED", "dd", 1 << led))
3882 led_s = (status == 0)?
3887 tpacpi_led_state_cache[led] = led_s;
3896 static int led_set_status(const unsigned int led,
3897 const enum led_status_t ledstatus)
3899 /* off, on, blink. Index is led_status_t */
3900 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
3901 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
3905 switch (led_supported) {
3906 case TPACPI_LED_570:
3910 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3911 (1 << led), led_sled_arg1[ledstatus]))
3914 case TPACPI_LED_OLD:
3915 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3918 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
3920 rc = ec_write(TPACPI_LED_EC_HLBL,
3921 (ledstatus == TPACPI_LED_BLINK) << led);
3923 rc = ec_write(TPACPI_LED_EC_HLCL,
3924 (ledstatus != TPACPI_LED_OFF) << led);
3926 case TPACPI_LED_NEW:
3928 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3929 led, led_led_arg1[ledstatus]))
3937 tpacpi_led_state_cache[led] = ledstatus;
3942 static void led_sysfs_set_status(unsigned int led,
3943 enum led_brightness brightness)
3946 (brightness == LED_OFF) ?
3948 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
3949 TPACPI_LED_BLINK : TPACPI_LED_ON);
3952 static void led_set_status_worker(struct work_struct *work)
3954 struct tpacpi_led_classdev *data =
3955 container_of(work, struct tpacpi_led_classdev, work);
3957 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3958 led_sysfs_set_status(data->led, data->new_brightness);
3961 static void led_sysfs_set(struct led_classdev *led_cdev,
3962 enum led_brightness brightness)
3964 struct tpacpi_led_classdev *data = container_of(led_cdev,
3965 struct tpacpi_led_classdev, led_classdev);
3967 data->new_brightness = brightness;
3968 queue_work(tpacpi_wq, &data->work);
3971 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
3972 unsigned long *delay_on, unsigned long *delay_off)
3974 struct tpacpi_led_classdev *data = container_of(led_cdev,
3975 struct tpacpi_led_classdev, led_classdev);
3977 /* Can we choose the flash rate? */
3978 if (*delay_on == 0 && *delay_off == 0) {
3979 /* yes. set them to the hardware blink rate (1 Hz) */
3980 *delay_on = 500; /* ms */
3981 *delay_off = 500; /* ms */
3982 } else if ((*delay_on != 500) || (*delay_off != 500))
3985 data->new_brightness = TPACPI_LED_BLINK;
3986 queue_work(tpacpi_wq, &data->work);
3991 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
3995 struct tpacpi_led_classdev *data = container_of(led_cdev,
3996 struct tpacpi_led_classdev, led_classdev);
3998 rc = led_get_status(data->led);
4000 if (rc == TPACPI_LED_OFF || rc < 0)
4001 rc = LED_OFF; /* no error handling in led class :( */
4008 static void led_exit(void)
4012 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4013 if (tpacpi_leds[i].led_classdev.name)
4014 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4020 static int __init led_init(struct ibm_init_struct *iibm)
4025 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4027 TPACPI_ACPIHANDLE_INIT(led);
4030 /* led not supported on R30, R31 */
4031 led_supported = TPACPI_LED_NONE;
4032 else if (strlencmp(led_path, "SLED") == 0)
4034 led_supported = TPACPI_LED_570;
4035 else if (strlencmp(led_path, "SYSL") == 0)
4036 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4037 led_supported = TPACPI_LED_OLD;
4040 led_supported = TPACPI_LED_NEW;
4042 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4043 str_supported(led_supported), led_supported);
4045 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4048 printk(TPACPI_ERR "Out of memory for LED data\n");
4052 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4053 tpacpi_leds[i].led = i;
4055 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4056 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4057 if (led_supported == TPACPI_LED_570)
4058 tpacpi_leds[i].led_classdev.brightness_get =
4061 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4063 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4065 rc = led_classdev_register(&tpacpi_pdev->dev,
4066 &tpacpi_leds[i].led_classdev);
4068 tpacpi_leds[i].led_classdev.name = NULL;
4074 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4077 #define str_led_status(s) \
4078 ((s) == TPACPI_LED_OFF ? "off" : \
4079 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4081 static int led_read(char *p)
4085 if (!led_supported) {
4086 len += sprintf(p + len, "status:\t\tnot supported\n");
4089 len += sprintf(p + len, "status:\t\tsupported\n");
4091 if (led_supported == TPACPI_LED_570) {
4094 for (i = 0; i < 8; i++) {
4095 status = led_get_status(i);
4098 len += sprintf(p + len, "%d:\t\t%s\n",
4099 i, str_led_status(status));
4103 len += sprintf(p + len, "commands:\t"
4104 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4109 static int led_write(char *buf)
4113 enum led_status_t s;
4118 while ((cmd = next_cmd(&buf))) {
4119 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4122 if (strstr(cmd, "off")) {
4124 } else if (strstr(cmd, "on")) {
4126 } else if (strstr(cmd, "blink")) {
4127 s = TPACPI_LED_BLINK;
4132 rc = led_set_status(led, s);
4140 static struct ibm_struct led_driver_data = {
4147 /*************************************************************************
4151 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4153 static int __init beep_init(struct ibm_init_struct *iibm)
4155 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4157 TPACPI_ACPIHANDLE_INIT(beep);
4159 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4160 str_supported(beep_handle != NULL));
4162 return (beep_handle)? 0 : 1;
4165 static int beep_read(char *p)
4170 len += sprintf(p + len, "status:\t\tnot supported\n");
4172 len += sprintf(p + len, "status:\t\tsupported\n");
4173 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4179 static int beep_write(char *buf)
4187 while ((cmd = next_cmd(&buf))) {
4188 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4189 beep_cmd >= 0 && beep_cmd <= 17) {
4193 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4200 static struct ibm_struct beep_driver_data = {
4203 .write = beep_write,
4206 /*************************************************************************
4210 enum thermal_access_mode {
4211 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4212 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4213 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4214 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4215 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4218 enum { /* TPACPI_THERMAL_TPEC_* */
4219 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4220 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4221 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4224 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4225 struct ibm_thermal_sensors_struct {
4226 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4229 static enum thermal_access_mode thermal_read_mode;
4231 /* idx is zero-based */
4232 static int thermal_get_sensor(int idx, s32 *value)
4238 t = TP_EC_THERMAL_TMP0;
4240 switch (thermal_read_mode) {
4241 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4242 case TPACPI_THERMAL_TPEC_16:
4243 if (idx >= 8 && idx <= 15) {
4244 t = TP_EC_THERMAL_TMP8;
4249 case TPACPI_THERMAL_TPEC_8:
4251 if (!acpi_ec_read(t + idx, &tmp))
4253 *value = tmp * 1000;
4258 case TPACPI_THERMAL_ACPI_UPDT:
4260 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4261 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4263 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4265 *value = (t - 2732) * 100;
4270 case TPACPI_THERMAL_ACPI_TMP07:
4272 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4273 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4275 if (t > 127 || t < -127)
4276 t = TP_EC_THERMAL_TMP_NA;
4282 case TPACPI_THERMAL_NONE:
4290 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4301 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4304 for (i = 0 ; i < n; i++) {
4305 res = thermal_get_sensor(i, &s->temp[i]);
4313 /* sysfs temp##_input -------------------------------------------------- */
4315 static ssize_t thermal_temp_input_show(struct device *dev,
4316 struct device_attribute *attr,
4319 struct sensor_device_attribute *sensor_attr =
4320 to_sensor_dev_attr(attr);
4321 int idx = sensor_attr->index;
4325 res = thermal_get_sensor(idx, &value);
4328 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4331 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4334 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4335 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4336 thermal_temp_input_show, NULL, _idxB)
4338 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4339 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4340 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4341 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4342 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4343 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4344 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4345 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4346 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4347 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4348 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4349 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4350 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4351 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4352 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4353 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4354 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4357 #define THERMAL_ATTRS(X) \
4358 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4360 static struct attribute *thermal_temp_input_attr[] = {
4380 static const struct attribute_group thermal_temp_input16_group = {
4381 .attrs = thermal_temp_input_attr
4384 static const struct attribute_group thermal_temp_input8_group = {
4385 .attrs = &thermal_temp_input_attr[8]
4388 #undef THERMAL_SENSOR_ATTR_TEMP
4389 #undef THERMAL_ATTRS
4391 /* --------------------------------------------------------------------- */
4393 static int __init thermal_init(struct ibm_init_struct *iibm)
4400 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4402 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4404 if (thinkpad_id.ec_model) {
4406 * Direct EC access mode: sensors at registers
4407 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4408 * non-implemented, thermal sensors return 0x80 when
4413 for (i = 0; i < 8; i++) {
4414 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4420 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4428 /* This is sheer paranoia, but we handle it anyway */
4431 "ThinkPad ACPI EC access misbehaving, "
4432 "falling back to ACPI TMPx access "
4434 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4437 "ThinkPad ACPI EC access misbehaving, "
4438 "disabling thermal sensors access\n");
4439 thermal_read_mode = TPACPI_THERMAL_NONE;
4444 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4446 } else if (acpi_tmp7) {
4447 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4448 /* 600e/x, 770e, 770x */
4449 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4451 /* Standard ACPI TMPx access, max 8 sensors */
4452 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4455 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4456 thermal_read_mode = TPACPI_THERMAL_NONE;
4459 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4460 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4463 switch (thermal_read_mode) {
4464 case TPACPI_THERMAL_TPEC_16:
4465 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4466 &thermal_temp_input16_group);
4470 case TPACPI_THERMAL_TPEC_8:
4471 case TPACPI_THERMAL_ACPI_TMP07:
4472 case TPACPI_THERMAL_ACPI_UPDT:
4473 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4474 &thermal_temp_input8_group);
4478 case TPACPI_THERMAL_NONE:
4486 static void thermal_exit(void)
4488 switch (thermal_read_mode) {
4489 case TPACPI_THERMAL_TPEC_16:
4490 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4491 &thermal_temp_input16_group);
4493 case TPACPI_THERMAL_TPEC_8:
4494 case TPACPI_THERMAL_ACPI_TMP07:
4495 case TPACPI_THERMAL_ACPI_UPDT:
4496 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4497 &thermal_temp_input16_group);
4499 case TPACPI_THERMAL_NONE:
4505 static int thermal_read(char *p)
4509 struct ibm_thermal_sensors_struct t;
4511 n = thermal_get_sensors(&t);
4512 if (unlikely(n < 0))
4515 len += sprintf(p + len, "temperatures:\t");
4518 for (i = 0; i < (n - 1); i++)
4519 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4520 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4522 len += sprintf(p + len, "not supported\n");
4527 static struct ibm_struct thermal_driver_data = {
4529 .read = thermal_read,
4530 .exit = thermal_exit,
4533 /*************************************************************************
4537 static u8 ecdump_regs[256];
4539 static int ecdump_read(char *p)
4545 len += sprintf(p + len, "EC "
4546 " +00 +01 +02 +03 +04 +05 +06 +07"
4547 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4548 for (i = 0; i < 256; i += 16) {
4549 len += sprintf(p + len, "EC 0x%02x:", i);
4550 for (j = 0; j < 16; j++) {
4551 if (!acpi_ec_read(i + j, &v))
4553 if (v != ecdump_regs[i + j])
4554 len += sprintf(p + len, " *%02x", v);
4556 len += sprintf(p + len, " %02x", v);
4557 ecdump_regs[i + j] = v;
4559 len += sprintf(p + len, "\n");
4564 /* These are way too dangerous to advertise openly... */
4566 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4567 " (<offset> is 00-ff, <value> is 00-ff)\n");
4568 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4569 " (<offset> is 00-ff, <value> is 0-255)\n");
4574 static int ecdump_write(char *buf)
4579 while ((cmd = next_cmd(&buf))) {
4580 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4582 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4586 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4587 if (!acpi_ec_write(i, v))
4596 static struct ibm_struct ecdump_driver_data = {
4598 .read = ecdump_read,
4599 .write = ecdump_write,
4600 .flags.experimental = 1,
4603 /*************************************************************************
4604 * Backlight/brightness subdriver
4607 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4610 TP_EC_BACKLIGHT = 0x31,
4612 /* TP_EC_BACKLIGHT bitmasks */
4613 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4614 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4615 TP_EC_BACKLIGHT_MAPSW = 0x20,
4618 static struct backlight_device *ibm_backlight_device;
4619 static int brightness_mode;
4620 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4622 static struct mutex brightness_mutex;
4625 * ThinkPads can read brightness from two places: EC 0x31, or
4626 * CMOS NVRAM byte 0x5E, bits 0-3.
4628 * EC 0x31 has the following layout
4629 * Bit 7: unknown function
4630 * Bit 6: unknown function
4631 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4632 * Bit 4: must be set to zero to avoid problems
4633 * Bit 3-0: backlight brightness level
4635 * brightness_get_raw returns status data in the EC 0x31 layout
4637 static int brightness_get_raw(int *status)
4639 u8 lec = 0, lcmos = 0, level = 0;
4641 if (brightness_mode & 1) {
4642 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4644 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4646 if (brightness_mode & 2) {
4647 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4648 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4649 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4650 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4654 if (brightness_mode == 3) {
4655 *status = lec; /* Prefer EC, CMOS is just a backing store */
4656 lec &= TP_EC_BACKLIGHT_LVLMSK;
4658 tp_warned.bright_cmos_ec_unsync = 0;
4660 if (!tp_warned.bright_cmos_ec_unsync) {
4662 "CMOS NVRAM (%u) and EC (%u) do not "
4663 "agree on display brightness level\n",
4664 (unsigned int) lcmos,
4665 (unsigned int) lec);
4666 tp_warned.bright_cmos_ec_unsync = 1;
4677 /* May return EINTR which can always be mapped to ERESTARTSYS */
4678 static int brightness_set(int value)
4680 int cmos_cmd, inc, i, res;
4684 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4688 res = mutex_lock_interruptible(&brightness_mutex);
4692 res = brightness_get_raw(¤t_value);
4696 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4697 current_value &= TP_EC_BACKLIGHT_LVLMSK;
4699 cmos_cmd = value > current_value ?
4700 TP_CMOS_BRIGHTNESS_UP :
4701 TP_CMOS_BRIGHTNESS_DOWN;
4702 inc = (value > current_value)? 1 : -1;
4705 for (i = current_value; i != value; i += inc) {
4706 if ((brightness_mode & 2) &&
4707 issue_thinkpad_cmos_command(cmos_cmd)) {
4711 if ((brightness_mode & 1) &&
4712 !acpi_ec_write(TP_EC_BACKLIGHT,
4713 (i + inc) | command_bits)) {
4720 mutex_unlock(&brightness_mutex);
4724 /* sysfs backlight class ----------------------------------------------- */
4726 static int brightness_update_status(struct backlight_device *bd)
4728 /* it is the backlight class's job (caller) to handle
4729 * EINTR and other errors properly */
4730 return brightness_set(
4731 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4732 bd->props.power == FB_BLANK_UNBLANK) ?
4733 bd->props.brightness : 0);
4736 static int brightness_get(struct backlight_device *bd)
4740 res = brightness_get_raw(&status);
4742 return 0; /* FIXME: teach backlight about error handling */
4744 return status & TP_EC_BACKLIGHT_LVLMSK;
4747 static struct backlight_ops ibm_backlight_data = {
4748 .get_brightness = brightness_get,
4749 .update_status = brightness_update_status,
4752 /* --------------------------------------------------------------------- */
4754 static int __init brightness_init(struct ibm_init_struct *iibm)
4758 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4760 mutex_init(&brightness_mutex);
4763 * We always attempt to detect acpi support, so as to switch
4764 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4765 * going to publish a backlight interface
4767 b = tpacpi_check_std_acpi_brightness_support();
4769 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4770 printk(TPACPI_NOTICE
4771 "Lenovo BIOS switched to ACPI backlight "
4774 if (brightness_enable > 1) {
4775 printk(TPACPI_NOTICE
4776 "standard ACPI backlight interface "
4777 "available, not loading native one...\n");
4782 if (!brightness_enable) {
4783 dbg_printk(TPACPI_DBG_INIT,
4784 "brightness support disabled by "
4785 "module parameter\n");
4791 "Unsupported brightness interface, "
4792 "please contact %s\n", TPACPI_MAIL);
4796 tp_features.bright_16levels = 1;
4798 if (!brightness_mode) {
4799 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4800 brightness_mode = 2;
4802 brightness_mode = 3;
4804 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4808 if (brightness_mode > 3)
4811 if (brightness_get_raw(&b) < 0)
4814 if (tp_features.bright_16levels)
4816 "detected a 16-level brightness capable ThinkPad\n");
4818 ibm_backlight_device = backlight_device_register(
4819 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4820 &ibm_backlight_data);
4821 if (IS_ERR(ibm_backlight_device)) {
4822 printk(TPACPI_ERR "Could not register backlight device\n");
4823 return PTR_ERR(ibm_backlight_device);
4825 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4827 ibm_backlight_device->props.max_brightness =
4828 (tp_features.bright_16levels)? 15 : 7;
4829 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
4830 backlight_update_status(ibm_backlight_device);
4835 static void brightness_exit(void)
4837 if (ibm_backlight_device) {
4838 vdbg_printk(TPACPI_DBG_EXIT,
4839 "calling backlight_device_unregister()\n");
4840 backlight_device_unregister(ibm_backlight_device);
4844 static int brightness_read(char *p)
4849 level = brightness_get(NULL);
4851 len += sprintf(p + len, "level:\t\tunreadable\n");
4853 len += sprintf(p + len, "level:\t\t%d\n", level);
4854 len += sprintf(p + len, "commands:\tup, down\n");
4855 len += sprintf(p + len, "commands:\tlevel <level>"
4856 " (<level> is 0-%d)\n",
4857 (tp_features.bright_16levels) ? 15 : 7);
4863 static int brightness_write(char *buf)
4868 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4870 level = brightness_get(NULL);
4874 while ((cmd = next_cmd(&buf))) {
4875 if (strlencmp(cmd, "up") == 0) {
4876 if (level < max_level)
4878 } else if (strlencmp(cmd, "down") == 0) {
4881 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4882 level >= 0 && level <= max_level) {
4889 * Now we know what the final level should be, so we try to set it.
4890 * Doing it this way makes the syscall restartable in case of EINTR
4892 rc = brightness_set(level);
4893 return (rc == -EINTR)? ERESTARTSYS : rc;
4896 static struct ibm_struct brightness_driver_data = {
4897 .name = "brightness",
4898 .read = brightness_read,
4899 .write = brightness_write,
4900 .exit = brightness_exit,
4903 /*************************************************************************
4907 static int volume_offset = 0x30;
4909 static int volume_read(char *p)
4914 if (!acpi_ec_read(volume_offset, &level)) {
4915 len += sprintf(p + len, "level:\t\tunreadable\n");
4917 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4918 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4919 len += sprintf(p + len, "commands:\tup, down, mute\n");
4920 len += sprintf(p + len, "commands:\tlevel <level>"
4921 " (<level> is 0-15)\n");
4927 static int volume_write(char *buf)
4929 int cmos_cmd, inc, i;
4931 int new_level, new_mute;
4934 while ((cmd = next_cmd(&buf))) {
4935 if (!acpi_ec_read(volume_offset, &level))
4937 new_mute = mute = level & 0x40;
4938 new_level = level = level & 0xf;
4940 if (strlencmp(cmd, "up") == 0) {
4944 new_level = level == 15 ? 15 : level + 1;
4945 } else if (strlencmp(cmd, "down") == 0) {
4949 new_level = level == 0 ? 0 : level - 1;
4950 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4951 new_level >= 0 && new_level <= 15) {
4953 } else if (strlencmp(cmd, "mute") == 0) {
4958 if (new_level != level) {
4959 /* mute doesn't change */
4961 cmos_cmd = (new_level > level) ?
4962 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4963 inc = new_level > level ? 1 : -1;
4965 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4966 !acpi_ec_write(volume_offset, level)))
4969 for (i = level; i != new_level; i += inc)
4970 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4971 !acpi_ec_write(volume_offset, i + inc))
4975 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4976 !acpi_ec_write(volume_offset, new_level + mute))) {
4981 if (new_mute != mute) {
4982 /* level doesn't change */
4984 cmos_cmd = (new_mute) ?
4985 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4987 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4988 !acpi_ec_write(volume_offset, level + new_mute))
4996 static struct ibm_struct volume_driver_data = {
4998 .read = volume_read,
4999 .write = volume_write,
5002 /*************************************************************************
5009 * TPACPI_FAN_RD_ACPI_GFAN:
5010 * ACPI GFAN method: returns fan level
5012 * see TPACPI_FAN_WR_ACPI_SFAN
5013 * EC 0x2f (HFSP) not available if GFAN exists
5015 * TPACPI_FAN_WR_ACPI_SFAN:
5016 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5018 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5021 * TPACPI_FAN_WR_TPEC:
5022 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5023 * Supported on almost all ThinkPads
5025 * Fan speed changes of any sort (including those caused by the
5026 * disengaged mode) are usually done slowly by the firmware as the
5027 * maximum ammount of fan duty cycle change per second seems to be
5030 * Reading is not available if GFAN exists.
5031 * Writing is not available if SFAN exists.
5034 * 7 automatic mode engaged;
5035 * (default operation mode of the ThinkPad)
5036 * fan level is ignored in this mode.
5037 * 6 full speed mode (takes precedence over bit 7);
5038 * not available on all thinkpads. May disable
5039 * the tachometer while the fan controller ramps up
5040 * the speed (which can take up to a few *minutes*).
5041 * Speeds up fan to 100% duty-cycle, which is far above
5042 * the standard RPM levels. It is not impossible that
5043 * it could cause hardware damage.
5044 * 5-3 unused in some models. Extra bits for fan level
5045 * in others, but still useless as all values above
5046 * 7 map to the same speed as level 7 in these models.
5047 * 2-0 fan level (0..7 usually)
5049 * 0x07 = max (set when temperatures critical)
5050 * Some ThinkPads may have other levels, see
5051 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5053 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5054 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5055 * does so, its initial value is meaningless (0x07).
5057 * For firmware bugs, refer to:
5058 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5062 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5063 * Main fan tachometer reading (in RPM)
5065 * This register is present on all ThinkPads with a new-style EC, and
5066 * it is known not to be present on the A21m/e, and T22, as there is
5067 * something else in offset 0x84 according to the ACPI DSDT. Other
5068 * ThinkPads from this same time period (and earlier) probably lack the
5069 * tachometer as well.
5071 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5072 * was never fixed by IBM to report the EC firmware version string
5073 * probably support the tachometer (like the early X models), so
5074 * detecting it is quite hard. We need more data to know for sure.
5076 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5079 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5082 * For firmware bugs, refer to:
5083 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5085 * TPACPI_FAN_WR_ACPI_FANS:
5086 * ThinkPad X31, X40, X41. Not available in the X60.
5088 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5089 * high speed. ACPI DSDT seems to map these three speeds to levels
5090 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5091 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5093 * The speeds are stored on handles
5094 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5096 * There are three default speed sets, acessible as handles:
5097 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5099 * ACPI DSDT switches which set is in use depending on various
5102 * TPACPI_FAN_WR_TPEC is also available and should be used to
5103 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5104 * but the ACPI tables just mention level 7.
5107 enum { /* Fan control constants */
5108 fan_status_offset = 0x2f, /* EC register 0x2f */
5109 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5110 * 0x84 must be read before 0x85 */
5112 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5113 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5115 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5118 enum fan_status_access_mode {
5119 TPACPI_FAN_NONE = 0, /* No fan status or control */
5120 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5121 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5124 enum fan_control_access_mode {
5125 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5126 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5127 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5128 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5131 enum fan_control_commands {
5132 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5133 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5134 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5135 * and also watchdog cmd */
5138 static int fan_control_allowed;
5140 static enum fan_status_access_mode fan_status_access_mode;
5141 static enum fan_control_access_mode fan_control_access_mode;
5142 static enum fan_control_commands fan_control_commands;
5144 static u8 fan_control_initial_status;
5145 static u8 fan_control_desired_level;
5146 static int fan_watchdog_maxinterval;
5148 static struct mutex fan_mutex;
5150 static void fan_watchdog_fire(struct work_struct *ignored);
5151 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5153 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5154 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5155 "\\FSPD", /* 600e/x, 770e, 770x */
5157 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5158 "JFNS", /* 770x-JL */
5162 * Call with fan_mutex held
5164 static void fan_update_desired_level(u8 status)
5167 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5169 fan_control_desired_level = 7;
5171 fan_control_desired_level = status;
5175 static int fan_get_status(u8 *status)
5180 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5182 switch (fan_status_access_mode) {
5183 case TPACPI_FAN_RD_ACPI_GFAN:
5184 /* 570, 600e/x, 770e, 770x */
5186 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5194 case TPACPI_FAN_RD_TPEC:
5195 /* all except 570, 600e/x, 770e, 770x */
5196 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5211 static int fan_get_status_safe(u8 *status)
5216 if (mutex_lock_interruptible(&fan_mutex))
5217 return -ERESTARTSYS;
5218 rc = fan_get_status(&s);
5220 fan_update_desired_level(s);
5221 mutex_unlock(&fan_mutex);
5229 static int fan_get_speed(unsigned int *speed)
5233 switch (fan_status_access_mode) {
5234 case TPACPI_FAN_RD_TPEC:
5235 /* all except 570, 600e/x, 770e, 770x */
5236 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5237 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5241 *speed = (hi << 8) | lo;
5252 static int fan_set_level(int level)
5254 if (!fan_control_allowed)
5257 switch (fan_control_access_mode) {
5258 case TPACPI_FAN_WR_ACPI_SFAN:
5259 if (level >= 0 && level <= 7) {
5260 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5266 case TPACPI_FAN_WR_ACPI_FANS:
5267 case TPACPI_FAN_WR_TPEC:
5268 if ((level != TP_EC_FAN_AUTO) &&
5269 (level != TP_EC_FAN_FULLSPEED) &&
5270 ((level < 0) || (level > 7)))
5273 /* safety net should the EC not support AUTO
5274 * or FULLSPEED mode bits and just ignore them */
5275 if (level & TP_EC_FAN_FULLSPEED)
5276 level |= 7; /* safety min speed 7 */
5277 else if (level & TP_EC_FAN_AUTO)
5278 level |= 4; /* safety min speed 4 */
5280 if (!acpi_ec_write(fan_status_offset, level))
5283 tp_features.fan_ctrl_status_undef = 0;
5292 static int fan_set_level_safe(int level)
5296 if (!fan_control_allowed)
5299 if (mutex_lock_interruptible(&fan_mutex))
5300 return -ERESTARTSYS;
5302 if (level == TPACPI_FAN_LAST_LEVEL)
5303 level = fan_control_desired_level;
5305 rc = fan_set_level(level);
5307 fan_update_desired_level(level);
5309 mutex_unlock(&fan_mutex);
5313 static int fan_set_enable(void)
5318 if (!fan_control_allowed)
5321 if (mutex_lock_interruptible(&fan_mutex))
5322 return -ERESTARTSYS;
5324 switch (fan_control_access_mode) {
5325 case TPACPI_FAN_WR_ACPI_FANS:
5326 case TPACPI_FAN_WR_TPEC:
5327 rc = fan_get_status(&s);
5331 /* Don't go out of emergency fan mode */
5334 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5337 if (!acpi_ec_write(fan_status_offset, s))
5340 tp_features.fan_ctrl_status_undef = 0;
5345 case TPACPI_FAN_WR_ACPI_SFAN:
5346 rc = fan_get_status(&s);
5352 /* Set fan to at least level 4 */
5355 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5365 mutex_unlock(&fan_mutex);
5369 static int fan_set_disable(void)
5373 if (!fan_control_allowed)
5376 if (mutex_lock_interruptible(&fan_mutex))
5377 return -ERESTARTSYS;
5380 switch (fan_control_access_mode) {
5381 case TPACPI_FAN_WR_ACPI_FANS:
5382 case TPACPI_FAN_WR_TPEC:
5383 if (!acpi_ec_write(fan_status_offset, 0x00))
5386 fan_control_desired_level = 0;
5387 tp_features.fan_ctrl_status_undef = 0;
5391 case TPACPI_FAN_WR_ACPI_SFAN:
5392 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5395 fan_control_desired_level = 0;
5403 mutex_unlock(&fan_mutex);
5407 static int fan_set_speed(int speed)
5411 if (!fan_control_allowed)
5414 if (mutex_lock_interruptible(&fan_mutex))
5415 return -ERESTARTSYS;
5418 switch (fan_control_access_mode) {
5419 case TPACPI_FAN_WR_ACPI_FANS:
5420 if (speed >= 0 && speed <= 65535) {
5421 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5422 speed, speed, speed))
5432 mutex_unlock(&fan_mutex);
5436 static void fan_watchdog_reset(void)
5438 static int fan_watchdog_active;
5440 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5443 if (fan_watchdog_active)
5444 cancel_delayed_work(&fan_watchdog_task);
5446 if (fan_watchdog_maxinterval > 0 &&
5447 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5448 fan_watchdog_active = 1;
5449 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5450 msecs_to_jiffies(fan_watchdog_maxinterval
5453 "failed to queue the fan watchdog, "
5454 "watchdog will not trigger\n");
5457 fan_watchdog_active = 0;
5460 static void fan_watchdog_fire(struct work_struct *ignored)
5464 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5467 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5468 rc = fan_set_enable();
5470 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5471 "will try again later...\n", -rc);
5472 /* reschedule for later */
5473 fan_watchdog_reset();
5478 * SYSFS fan layout: hwmon compatible (device)
5481 * 0: "disengaged" mode
5483 * 2: native EC "auto" mode (recommended, hardware default)
5485 * pwm*: set speed in manual mode, ignored otherwise.
5486 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5489 * fan*_input: tachometer reading, RPM
5492 * SYSFS fan layout: extensions
5494 * fan_watchdog (driver):
5495 * fan watchdog interval in seconds, 0 disables (default), max 120
5498 /* sysfs fan pwm1_enable ----------------------------------------------- */
5499 static ssize_t fan_pwm1_enable_show(struct device *dev,
5500 struct device_attribute *attr,
5506 res = fan_get_status_safe(&status);
5510 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5511 if (status != fan_control_initial_status) {
5512 tp_features.fan_ctrl_status_undef = 0;
5514 /* Return most likely status. In fact, it
5515 * might be the only possible status */
5516 status = TP_EC_FAN_AUTO;
5520 if (status & TP_EC_FAN_FULLSPEED) {
5522 } else if (status & TP_EC_FAN_AUTO) {
5527 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5530 static ssize_t fan_pwm1_enable_store(struct device *dev,
5531 struct device_attribute *attr,
5532 const char *buf, size_t count)
5537 if (parse_strtoul(buf, 2, &t))
5542 level = TP_EC_FAN_FULLSPEED;
5545 level = TPACPI_FAN_LAST_LEVEL;
5548 level = TP_EC_FAN_AUTO;
5551 /* reserved for software-controlled auto mode */
5557 res = fan_set_level_safe(level);
5563 fan_watchdog_reset();
5568 static struct device_attribute dev_attr_fan_pwm1_enable =
5569 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5570 fan_pwm1_enable_show, fan_pwm1_enable_store);
5572 /* sysfs fan pwm1 ------------------------------------------------------ */
5573 static ssize_t fan_pwm1_show(struct device *dev,
5574 struct device_attribute *attr,
5580 res = fan_get_status_safe(&status);
5584 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5585 if (status != fan_control_initial_status) {
5586 tp_features.fan_ctrl_status_undef = 0;
5588 status = TP_EC_FAN_AUTO;
5593 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5594 status = fan_control_desired_level;
5599 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5602 static ssize_t fan_pwm1_store(struct device *dev,
5603 struct device_attribute *attr,
5604 const char *buf, size_t count)
5608 u8 status, newlevel;
5610 if (parse_strtoul(buf, 255, &s))
5613 /* scale down from 0-255 to 0-7 */
5614 newlevel = (s >> 5) & 0x07;
5616 if (mutex_lock_interruptible(&fan_mutex))
5617 return -ERESTARTSYS;
5619 rc = fan_get_status(&status);
5620 if (!rc && (status &
5621 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5622 rc = fan_set_level(newlevel);
5626 fan_update_desired_level(newlevel);
5627 fan_watchdog_reset();
5631 mutex_unlock(&fan_mutex);
5632 return (rc)? rc : count;
5635 static struct device_attribute dev_attr_fan_pwm1 =
5636 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5637 fan_pwm1_show, fan_pwm1_store);
5639 /* sysfs fan fan1_input ------------------------------------------------ */
5640 static ssize_t fan_fan1_input_show(struct device *dev,
5641 struct device_attribute *attr,
5647 res = fan_get_speed(&speed);
5651 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5654 static struct device_attribute dev_attr_fan_fan1_input =
5655 __ATTR(fan1_input, S_IRUGO,
5656 fan_fan1_input_show, NULL);
5658 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5659 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5662 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5665 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5666 const char *buf, size_t count)
5670 if (parse_strtoul(buf, 120, &t))
5673 if (!fan_control_allowed)
5676 fan_watchdog_maxinterval = t;
5677 fan_watchdog_reset();
5682 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5683 fan_fan_watchdog_show, fan_fan_watchdog_store);
5685 /* --------------------------------------------------------------------- */
5686 static struct attribute *fan_attributes[] = {
5687 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5688 &dev_attr_fan_fan1_input.attr,
5692 static const struct attribute_group fan_attr_group = {
5693 .attrs = fan_attributes,
5696 static int __init fan_init(struct ibm_init_struct *iibm)
5700 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5702 mutex_init(&fan_mutex);
5703 fan_status_access_mode = TPACPI_FAN_NONE;
5704 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5705 fan_control_commands = 0;
5706 fan_watchdog_maxinterval = 0;
5707 tp_features.fan_ctrl_status_undef = 0;
5708 fan_control_desired_level = 7;
5710 TPACPI_ACPIHANDLE_INIT(fans);
5711 TPACPI_ACPIHANDLE_INIT(gfan);
5712 TPACPI_ACPIHANDLE_INIT(sfan);
5715 /* 570, 600e/x, 770e, 770x */
5716 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5718 /* all other ThinkPads: note that even old-style
5719 * ThinkPad ECs supports the fan control register */
5720 if (likely(acpi_ec_read(fan_status_offset,
5721 &fan_control_initial_status))) {
5722 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5724 /* In some ThinkPads, neither the EC nor the ACPI
5725 * DSDT initialize the fan status, and it ends up
5726 * being set to 0x07 when it *could* be either
5729 * Enable for TP-1Y (T43), TP-78 (R51e),
5730 * TP-76 (R52), TP-70 (T43, R52), which are known
5732 if (fan_control_initial_status == 0x07) {
5733 switch (thinkpad_id.ec_model) {
5734 case 0x5931: /* TP-1Y */
5735 case 0x3837: /* TP-78 */
5736 case 0x3637: /* TP-76 */
5737 case 0x3037: /* TP-70 */
5738 printk(TPACPI_NOTICE
5739 "fan_init: initial fan status "
5740 "is unknown, assuming it is "
5742 tp_features.fan_ctrl_status_undef = 1;
5748 "ThinkPad ACPI EC access misbehaving, "
5749 "fan status and control unavailable\n");
5756 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5757 fan_control_commands |=
5758 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5761 /* gfan without sfan means no fan control */
5762 /* all other models implement TP EC 0x2f control */
5766 fan_control_access_mode =
5767 TPACPI_FAN_WR_ACPI_FANS;
5768 fan_control_commands |=
5769 TPACPI_FAN_CMD_SPEED |
5770 TPACPI_FAN_CMD_LEVEL |
5771 TPACPI_FAN_CMD_ENABLE;
5773 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5774 fan_control_commands |=
5775 TPACPI_FAN_CMD_LEVEL |
5776 TPACPI_FAN_CMD_ENABLE;
5781 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5782 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5783 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5784 fan_status_access_mode, fan_control_access_mode);
5786 /* fan control master switch */
5787 if (!fan_control_allowed) {
5788 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5789 fan_control_commands = 0;
5790 dbg_printk(TPACPI_DBG_INIT,
5791 "fan control features disabled by parameter\n");
5794 /* update fan_control_desired_level */
5795 if (fan_status_access_mode != TPACPI_FAN_NONE)
5796 fan_get_status_safe(NULL);
5798 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5799 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5800 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5805 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5806 &driver_attr_fan_watchdog);
5808 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5817 static void fan_exit(void)
5819 vdbg_printk(TPACPI_DBG_EXIT,
5820 "cancelling any pending fan watchdog tasks\n");
5822 /* FIXME: can we really do this unconditionally? */
5823 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5824 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5825 &driver_attr_fan_watchdog);
5827 cancel_delayed_work(&fan_watchdog_task);
5828 flush_workqueue(tpacpi_wq);
5831 static int fan_read(char *p)
5836 unsigned int speed = 0;
5838 switch (fan_status_access_mode) {
5839 case TPACPI_FAN_RD_ACPI_GFAN:
5840 /* 570, 600e/x, 770e, 770x */
5841 rc = fan_get_status_safe(&status);
5845 len += sprintf(p + len, "status:\t\t%s\n"
5847 (status != 0) ? "enabled" : "disabled", status);
5850 case TPACPI_FAN_RD_TPEC:
5851 /* all except 570, 600e/x, 770e, 770x */
5852 rc = fan_get_status_safe(&status);
5856 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5857 if (status != fan_control_initial_status)
5858 tp_features.fan_ctrl_status_undef = 0;
5860 /* Return most likely status. In fact, it
5861 * might be the only possible status */
5862 status = TP_EC_FAN_AUTO;
5865 len += sprintf(p + len, "status:\t\t%s\n",
5866 (status != 0) ? "enabled" : "disabled");
5868 rc = fan_get_speed(&speed);
5872 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5874 if (status & TP_EC_FAN_FULLSPEED)
5875 /* Disengaged mode takes precedence */
5876 len += sprintf(p + len, "level:\t\tdisengaged\n");
5877 else if (status & TP_EC_FAN_AUTO)
5878 len += sprintf(p + len, "level:\t\tauto\n");
5880 len += sprintf(p + len, "level:\t\t%d\n", status);
5883 case TPACPI_FAN_NONE:
5885 len += sprintf(p + len, "status:\t\tnot supported\n");
5888 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5889 len += sprintf(p + len, "commands:\tlevel <level>");
5891 switch (fan_control_access_mode) {
5892 case TPACPI_FAN_WR_ACPI_SFAN:
5893 len += sprintf(p + len, " (<level> is 0-7)\n");
5897 len += sprintf(p + len, " (<level> is 0-7, "
5898 "auto, disengaged, full-speed)\n");
5903 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5904 len += sprintf(p + len, "commands:\tenable, disable\n"
5905 "commands:\twatchdog <timeout> (<timeout> "
5906 "is 0 (off), 1-120 (seconds))\n");
5908 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5909 len += sprintf(p + len, "commands:\tspeed <speed>"
5910 " (<speed> is 0-65535)\n");
5915 static int fan_write_cmd_level(const char *cmd, int *rc)
5919 if (strlencmp(cmd, "level auto") == 0)
5920 level = TP_EC_FAN_AUTO;
5921 else if ((strlencmp(cmd, "level disengaged") == 0) |
5922 (strlencmp(cmd, "level full-speed") == 0))
5923 level = TP_EC_FAN_FULLSPEED;
5924 else if (sscanf(cmd, "level %d", &level) != 1)
5927 *rc = fan_set_level_safe(level);
5929 printk(TPACPI_ERR "level command accepted for unsupported "
5930 "access mode %d", fan_control_access_mode);
5935 static int fan_write_cmd_enable(const char *cmd, int *rc)
5937 if (strlencmp(cmd, "enable") != 0)
5940 *rc = fan_set_enable();
5942 printk(TPACPI_ERR "enable command accepted for unsupported "
5943 "access mode %d", fan_control_access_mode);
5948 static int fan_write_cmd_disable(const char *cmd, int *rc)
5950 if (strlencmp(cmd, "disable") != 0)
5953 *rc = fan_set_disable();
5955 printk(TPACPI_ERR "disable command accepted for unsupported "
5956 "access mode %d", fan_control_access_mode);
5961 static int fan_write_cmd_speed(const char *cmd, int *rc)
5966 * Support speed <low> <medium> <high> ? */
5968 if (sscanf(cmd, "speed %d", &speed) != 1)
5971 *rc = fan_set_speed(speed);
5973 printk(TPACPI_ERR "speed command accepted for unsupported "
5974 "access mode %d", fan_control_access_mode);
5979 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5983 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5986 if (interval < 0 || interval > 120)
5989 fan_watchdog_maxinterval = interval;
5994 static int fan_write(char *buf)
5999 while (!rc && (cmd = next_cmd(&buf))) {
6000 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6001 fan_write_cmd_level(cmd, &rc)) &&
6002 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6003 (fan_write_cmd_enable(cmd, &rc) ||
6004 fan_write_cmd_disable(cmd, &rc) ||
6005 fan_write_cmd_watchdog(cmd, &rc))) &&
6006 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6007 fan_write_cmd_speed(cmd, &rc))
6011 fan_watchdog_reset();
6017 static struct ibm_struct fan_driver_data = {
6024 /****************************************************************************
6025 ****************************************************************************
6029 ****************************************************************************
6030 ****************************************************************************/
6032 /* sysfs name ---------------------------------------------------------- */
6033 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6034 struct device_attribute *attr,
6037 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6040 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6041 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6043 /* --------------------------------------------------------------------- */
6046 static struct proc_dir_entry *proc_dir;
6049 * Module and infrastructure proble, init and exit handling
6052 static int force_load;
6054 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6055 static const char * __init str_supported(int is_supported)
6057 static char text_unsupported[] __initdata = "not supported";
6059 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6061 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6063 static void ibm_exit(struct ibm_struct *ibm)
6065 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6067 list_del_init(&ibm->all_drivers);
6069 if (ibm->flags.acpi_notify_installed) {
6070 dbg_printk(TPACPI_DBG_EXIT,
6071 "%s: acpi_remove_notify_handler\n", ibm->name);
6073 acpi_remove_notify_handler(*ibm->acpi->handle,
6075 dispatch_acpi_notify);
6076 ibm->flags.acpi_notify_installed = 0;
6077 ibm->flags.acpi_notify_installed = 0;
6080 if (ibm->flags.proc_created) {
6081 dbg_printk(TPACPI_DBG_EXIT,
6082 "%s: remove_proc_entry\n", ibm->name);
6083 remove_proc_entry(ibm->name, proc_dir);
6084 ibm->flags.proc_created = 0;
6087 if (ibm->flags.acpi_driver_registered) {
6088 dbg_printk(TPACPI_DBG_EXIT,
6089 "%s: acpi_bus_unregister_driver\n", ibm->name);
6091 acpi_bus_unregister_driver(ibm->acpi->driver);
6092 kfree(ibm->acpi->driver);
6093 ibm->acpi->driver = NULL;
6094 ibm->flags.acpi_driver_registered = 0;
6097 if (ibm->flags.init_called && ibm->exit) {
6099 ibm->flags.init_called = 0;
6102 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6105 static int __init ibm_init(struct ibm_init_struct *iibm)
6108 struct ibm_struct *ibm = iibm->data;
6109 struct proc_dir_entry *entry;
6111 BUG_ON(ibm == NULL);
6113 INIT_LIST_HEAD(&ibm->all_drivers);
6115 if (ibm->flags.experimental && !experimental)
6118 dbg_printk(TPACPI_DBG_INIT,
6119 "probing for %s\n", ibm->name);
6122 ret = iibm->init(iibm);
6124 return 0; /* probe failed */
6128 ibm->flags.init_called = 1;
6132 if (ibm->acpi->hid) {
6133 ret = register_tpacpi_subdriver(ibm);
6138 if (ibm->acpi->notify) {
6139 ret = setup_acpi_notify(ibm);
6140 if (ret == -ENODEV) {
6141 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6151 dbg_printk(TPACPI_DBG_INIT,
6152 "%s installed\n", ibm->name);
6155 entry = create_proc_entry(ibm->name,
6156 S_IFREG | S_IRUGO | S_IWUSR,
6159 printk(TPACPI_ERR "unable to create proc entry %s\n",
6164 entry->owner = THIS_MODULE;
6166 entry->read_proc = &dispatch_procfs_read;
6168 entry->write_proc = &dispatch_procfs_write;
6169 ibm->flags.proc_created = 1;
6172 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6177 dbg_printk(TPACPI_DBG_INIT,
6178 "%s: at error exit path with result %d\n",
6182 return (ret < 0)? ret : 0;
6187 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
6189 const struct dmi_device *dev = NULL;
6190 char ec_fw_string[18];
6195 memset(tp, 0, sizeof(*tp));
6197 if (dmi_name_in_vendors("IBM"))
6198 tp->vendor = PCI_VENDOR_ID_IBM;
6199 else if (dmi_name_in_vendors("LENOVO"))
6200 tp->vendor = PCI_VENDOR_ID_LENOVO;
6204 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
6206 if (!tp->bios_version_str)
6208 tp->bios_model = tp->bios_version_str[0]
6209 | (tp->bios_version_str[1] << 8);
6212 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6213 * X32 or newer, all Z series; Some models must have an
6214 * up-to-date BIOS or they will not be detected.
6216 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6218 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6219 if (sscanf(dev->name,
6220 "IBM ThinkPad Embedded Controller -[%17c",
6221 ec_fw_string) == 1) {
6222 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6223 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6225 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6226 tp->ec_model = ec_fw_string[0]
6227 | (ec_fw_string[1] << 8);
6232 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
6234 if (tp->model_str && strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
6235 kfree(tp->model_str);
6236 tp->model_str = NULL;
6239 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
6243 static int __init probe_for_thinkpad(void)
6251 * Non-ancient models have better DMI tagging, but very old models
6254 is_thinkpad = (thinkpad_id.model_str != NULL);
6256 /* ec is required because many other handles are relative to it */
6257 TPACPI_ACPIHANDLE_INIT(ec);
6261 "Not yet supported ThinkPad detected!\n");
6266 * Risks a regression on very old machines, but reduces potential
6267 * false positives a damn great deal
6270 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6272 if (!is_thinkpad && !force_load)
6279 /* Module init, exit, parameters */
6281 static struct ibm_init_struct ibms_init[] __initdata = {
6283 .init = thinkpad_acpi_driver_init,
6284 .data = &thinkpad_acpi_driver_data,
6287 .init = hotkey_init,
6288 .data = &hotkey_driver_data,
6291 .init = bluetooth_init,
6292 .data = &bluetooth_driver_data,
6296 .data = &wan_driver_data,
6298 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6301 .data = &video_driver_data,
6306 .data = &light_driver_data,
6308 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6311 .data = &dock_driver_data[0],
6315 .data = &dock_driver_data[1],
6318 #ifdef CONFIG_THINKPAD_ACPI_BAY
6321 .data = &bay_driver_data,
6326 .data = &cmos_driver_data,
6330 .data = &led_driver_data,
6334 .data = &beep_driver_data,
6337 .init = thermal_init,
6338 .data = &thermal_driver_data,
6341 .data = &ecdump_driver_data,
6344 .init = brightness_init,
6345 .data = &brightness_driver_data,
6348 .data = &volume_driver_data,
6352 .data = &fan_driver_data,
6356 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6359 struct ibm_struct *ibm;
6361 if (!kp || !kp->name || !val)
6364 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6365 ibm = ibms_init[i].data;
6366 WARN_ON(ibm == NULL);
6368 if (!ibm || !ibm->name)
6371 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6372 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6374 strcpy(ibms_init[i].param, val);
6375 strcat(ibms_init[i].param, ",");
6383 module_param(experimental, int, 0);
6384 MODULE_PARM_DESC(experimental,
6385 "Enables experimental features when non-zero");
6387 module_param_named(debug, dbg_level, uint, 0);
6388 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6390 module_param(force_load, bool, 0);
6391 MODULE_PARM_DESC(force_load,
6392 "Attempts to load the driver even on a "
6393 "mis-identified ThinkPad when true");
6395 module_param_named(fan_control, fan_control_allowed, bool, 0);
6396 MODULE_PARM_DESC(fan_control,
6397 "Enables setting fan parameters features when true");
6399 module_param_named(brightness_mode, brightness_mode, int, 0);
6400 MODULE_PARM_DESC(brightness_mode,
6401 "Selects brightness control strategy: "
6402 "0=auto, 1=EC, 2=CMOS, 3=both");
6404 module_param(brightness_enable, uint, 0);
6405 MODULE_PARM_DESC(brightness_enable,
6406 "Enables backlight control when 1, disables when 0");
6408 module_param(hotkey_report_mode, uint, 0);
6409 MODULE_PARM_DESC(hotkey_report_mode,
6410 "used for backwards compatibility with userspace, "
6411 "see documentation");
6413 #define TPACPI_PARAM(feature) \
6414 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6415 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6416 "at module load, see documentation")
6418 TPACPI_PARAM(hotkey);
6419 TPACPI_PARAM(bluetooth);
6420 TPACPI_PARAM(video);
6421 TPACPI_PARAM(light);
6422 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6425 #ifdef CONFIG_THINKPAD_ACPI_BAY
6427 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6431 TPACPI_PARAM(ecdump);
6432 TPACPI_PARAM(brightness);
6433 TPACPI_PARAM(volume);
6436 static void thinkpad_acpi_module_exit(void)
6438 struct ibm_struct *ibm, *itmp;
6440 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6442 list_for_each_entry_safe_reverse(ibm, itmp,
6443 &tpacpi_all_drivers,
6448 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6450 if (tpacpi_inputdev) {
6451 if (tp_features.input_device_registered)
6452 input_unregister_device(tpacpi_inputdev);
6454 input_free_device(tpacpi_inputdev);
6458 hwmon_device_unregister(tpacpi_hwmon);
6460 if (tp_features.sensors_pdev_attrs_registered)
6461 device_remove_file(&tpacpi_sensors_pdev->dev,
6462 &dev_attr_thinkpad_acpi_pdev_name);
6463 if (tpacpi_sensors_pdev)
6464 platform_device_unregister(tpacpi_sensors_pdev);
6466 platform_device_unregister(tpacpi_pdev);
6468 if (tp_features.sensors_pdrv_attrs_registered)
6469 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6470 if (tp_features.platform_drv_attrs_registered)
6471 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6473 if (tp_features.sensors_pdrv_registered)
6474 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6476 if (tp_features.platform_drv_registered)
6477 platform_driver_unregister(&tpacpi_pdriver);
6480 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6483 destroy_workqueue(tpacpi_wq);
6485 kfree(thinkpad_id.bios_version_str);
6486 kfree(thinkpad_id.ec_version_str);
6487 kfree(thinkpad_id.model_str);
6491 static int __init thinkpad_acpi_module_init(void)
6495 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6497 /* Parameter checking */
6498 if (hotkey_report_mode > 2)
6501 /* Driver-level probe */
6503 get_thinkpad_model_data(&thinkpad_id);
6504 ret = probe_for_thinkpad();
6506 thinkpad_acpi_module_exit();
6510 /* Driver initialization */
6512 TPACPI_ACPIHANDLE_INIT(ecrd);
6513 TPACPI_ACPIHANDLE_INIT(ecwr);
6515 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6517 thinkpad_acpi_module_exit();
6521 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6524 "unable to create proc dir " TPACPI_PROC_DIR);
6525 thinkpad_acpi_module_exit();
6528 proc_dir->owner = THIS_MODULE;
6530 ret = platform_driver_register(&tpacpi_pdriver);
6533 "unable to register main platform driver\n");
6534 thinkpad_acpi_module_exit();
6537 tp_features.platform_drv_registered = 1;
6539 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6542 "unable to register hwmon platform driver\n");
6543 thinkpad_acpi_module_exit();
6546 tp_features.sensors_pdrv_registered = 1;
6548 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6550 tp_features.platform_drv_attrs_registered = 1;
6551 ret = tpacpi_create_driver_attributes(
6552 &tpacpi_hwmon_pdriver.driver);
6556 "unable to create sysfs driver attributes\n");
6557 thinkpad_acpi_module_exit();
6560 tp_features.sensors_pdrv_attrs_registered = 1;
6563 /* Device initialization */
6564 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6566 if (IS_ERR(tpacpi_pdev)) {
6567 ret = PTR_ERR(tpacpi_pdev);
6569 printk(TPACPI_ERR "unable to register platform device\n");
6570 thinkpad_acpi_module_exit();
6573 tpacpi_sensors_pdev = platform_device_register_simple(
6574 TPACPI_HWMON_DRVR_NAME,
6576 if (IS_ERR(tpacpi_sensors_pdev)) {
6577 ret = PTR_ERR(tpacpi_sensors_pdev);
6578 tpacpi_sensors_pdev = NULL;
6580 "unable to register hwmon platform device\n");
6581 thinkpad_acpi_module_exit();
6584 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6585 &dev_attr_thinkpad_acpi_pdev_name);
6588 "unable to create sysfs hwmon device attributes\n");
6589 thinkpad_acpi_module_exit();
6592 tp_features.sensors_pdev_attrs_registered = 1;
6593 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6594 if (IS_ERR(tpacpi_hwmon)) {
6595 ret = PTR_ERR(tpacpi_hwmon);
6596 tpacpi_hwmon = NULL;
6597 printk(TPACPI_ERR "unable to register hwmon device\n");
6598 thinkpad_acpi_module_exit();
6601 mutex_init(&tpacpi_inputdev_send_mutex);
6602 tpacpi_inputdev = input_allocate_device();
6603 if (!tpacpi_inputdev) {
6604 printk(TPACPI_ERR "unable to allocate input device\n");
6605 thinkpad_acpi_module_exit();
6608 /* Prepare input device, but don't register */
6609 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6610 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6611 tpacpi_inputdev->id.bustype = BUS_HOST;
6612 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6613 thinkpad_id.vendor :
6615 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6616 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6618 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6619 ret = ibm_init(&ibms_init[i]);
6620 if (ret >= 0 && *ibms_init[i].param)
6621 ret = ibms_init[i].data->write(ibms_init[i].param);
6623 thinkpad_acpi_module_exit();
6627 ret = input_register_device(tpacpi_inputdev);
6629 printk(TPACPI_ERR "unable to register input device\n");
6630 thinkpad_acpi_module_exit();
6633 tp_features.input_device_registered = 1;
6636 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6640 /* Please remove this in year 2009 */
6641 MODULE_ALIAS("ibm_acpi");
6643 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6646 * DMI matching for module autoloading
6648 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6649 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6651 * Only models listed in thinkwiki will be supported, so add yours
6652 * if it is not there yet.
6654 #define IBM_BIOS_MODULE_ALIAS(__type) \
6655 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6657 /* Non-ancient thinkpads */
6658 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6659 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6661 /* Ancient thinkpad BIOSes have to be identified by
6662 * BIOS type or model number, and there are far less
6663 * BIOS types than model numbers... */
6664 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6665 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6666 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6668 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6669 MODULE_DESCRIPTION(TPACPI_DESC);
6670 MODULE_VERSION(TPACPI_VERSION);
6671 MODULE_LICENSE("GPL");
6673 module_init(thinkpad_acpi_module_init);
6674 module_exit(thinkpad_acpi_module_exit);