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_radiosw(void)
1292 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1293 mutex_lock(&tpacpi_inputdev_send_mutex);
1295 input_report_switch(tpacpi_inputdev,
1296 SW_RFKILL_ALL, !!wlsw);
1297 input_sync(tpacpi_inputdev);
1299 mutex_unlock(&tpacpi_inputdev_send_mutex);
1303 static void tpacpi_input_send_tabletsw(void)
1307 if (tp_features.hotkey_tablet &&
1308 !hotkey_get_tablet_mode(&state)) {
1309 mutex_lock(&tpacpi_inputdev_send_mutex);
1311 input_report_switch(tpacpi_inputdev,
1312 SW_TABLET_MODE, !!state);
1313 input_sync(tpacpi_inputdev);
1315 mutex_unlock(&tpacpi_inputdev_send_mutex);
1319 static void tpacpi_input_send_key(unsigned int scancode)
1321 unsigned int keycode;
1323 keycode = hotkey_keycode_map[scancode];
1325 if (keycode != KEY_RESERVED) {
1326 mutex_lock(&tpacpi_inputdev_send_mutex);
1328 input_report_key(tpacpi_inputdev, keycode, 1);
1329 if (keycode == KEY_UNKNOWN)
1330 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1332 input_sync(tpacpi_inputdev);
1334 input_report_key(tpacpi_inputdev, keycode, 0);
1335 if (keycode == KEY_UNKNOWN)
1336 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1338 input_sync(tpacpi_inputdev);
1340 mutex_unlock(&tpacpi_inputdev_send_mutex);
1344 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1345 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1347 static void tpacpi_hotkey_send_key(unsigned int scancode)
1349 tpacpi_input_send_key(scancode);
1350 if (hotkey_report_mode < 2) {
1351 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1352 0x80, 0x1001 + scancode);
1356 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1360 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1361 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1362 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1363 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1364 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1365 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1367 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1368 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1369 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1371 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1372 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1373 n->displayexp_toggle =
1374 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1376 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1377 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1378 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1379 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1380 n->brightness_toggle =
1381 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1383 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1384 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1385 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1386 >> TP_NVRAM_POS_LEVEL_VOLUME;
1387 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1388 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1392 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1394 if ((mask & (1 << __scancode)) && \
1395 oldn->__member != newn->__member) \
1396 tpacpi_hotkey_send_key(__scancode); \
1399 #define TPACPI_MAY_SEND_KEY(__scancode) \
1400 do { if (mask & (1 << __scancode)) \
1401 tpacpi_hotkey_send_key(__scancode); } while (0)
1403 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1404 struct tp_nvram_state *newn,
1407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1412 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1414 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1417 if (oldn->volume_toggle != newn->volume_toggle) {
1418 if (oldn->mute != newn->mute) {
1419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1421 if (oldn->volume_level > newn->volume_level) {
1422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1423 } else if (oldn->volume_level < newn->volume_level) {
1424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1425 } else if (oldn->mute == newn->mute) {
1426 /* repeated key presses that didn't change state */
1428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1429 } else if (newn->volume_level != 0) {
1430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1437 /* handle brightness */
1438 if (oldn->brightness_toggle != newn->brightness_toggle) {
1439 if (oldn->brightness_level < newn->brightness_level) {
1440 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1441 } else if (oldn->brightness_level > newn->brightness_level) {
1442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1444 /* repeated key presses that didn't change state */
1445 if (newn->brightness_level != 0) {
1446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1454 #undef TPACPI_COMPARE_KEY
1455 #undef TPACPI_MAY_SEND_KEY
1457 static int hotkey_kthread(void *data)
1459 struct tp_nvram_state s[2];
1461 unsigned int si, so;
1463 unsigned int change_detector, must_reset;
1465 mutex_lock(&hotkey_thread_mutex);
1467 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1476 /* Initial state for compares */
1477 mutex_lock(&hotkey_thread_data_mutex);
1478 change_detector = hotkey_config_change;
1479 mask = hotkey_source_mask & hotkey_mask;
1480 mutex_unlock(&hotkey_thread_data_mutex);
1481 hotkey_read_nvram(&s[so], mask);
1483 while (!kthread_should_stop() && hotkey_poll_freq) {
1485 t = 1000/hotkey_poll_freq;
1486 t = msleep_interruptible(t);
1487 if (unlikely(kthread_should_stop()))
1489 must_reset = try_to_freeze();
1490 if (t > 0 && !must_reset)
1493 mutex_lock(&hotkey_thread_data_mutex);
1494 if (must_reset || hotkey_config_change != change_detector) {
1495 /* forget old state on thaw or config change */
1498 change_detector = hotkey_config_change;
1500 mask = hotkey_source_mask & hotkey_mask;
1501 mutex_unlock(&hotkey_thread_data_mutex);
1504 hotkey_read_nvram(&s[si], mask);
1505 if (likely(si != so)) {
1506 hotkey_compare_and_issue_event(&s[so], &s[si],
1516 mutex_unlock(&hotkey_thread_mutex);
1520 static void hotkey_poll_stop_sync(void)
1522 if (tpacpi_hotkey_task) {
1523 if (frozen(tpacpi_hotkey_task) ||
1524 freezing(tpacpi_hotkey_task))
1525 thaw_process(tpacpi_hotkey_task);
1527 kthread_stop(tpacpi_hotkey_task);
1528 tpacpi_hotkey_task = NULL;
1529 mutex_lock(&hotkey_thread_mutex);
1530 /* at this point, the thread did exit */
1531 mutex_unlock(&hotkey_thread_mutex);
1535 /* call with hotkey_mutex held */
1536 static void hotkey_poll_setup(int may_warn)
1538 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1539 hotkey_poll_freq > 0 &&
1540 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1541 if (!tpacpi_hotkey_task) {
1542 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1543 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1544 if (IS_ERR(tpacpi_hotkey_task)) {
1545 tpacpi_hotkey_task = NULL;
1547 "could not create kernel thread "
1548 "for hotkey polling\n");
1552 hotkey_poll_stop_sync();
1554 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1555 printk(TPACPI_NOTICE
1556 "hot keys 0x%08x require polling, "
1557 "which is currently disabled\n",
1558 hotkey_source_mask);
1563 static void hotkey_poll_setup_safe(int may_warn)
1565 mutex_lock(&hotkey_mutex);
1566 hotkey_poll_setup(may_warn);
1567 mutex_unlock(&hotkey_mutex);
1570 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1572 static void hotkey_poll_setup_safe(int __unused)
1576 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1578 static int hotkey_inputdev_open(struct input_dev *dev)
1580 switch (tpacpi_lifecycle) {
1581 case TPACPI_LIFE_INIT:
1583 * hotkey_init will call hotkey_poll_setup_safe
1584 * at the appropriate moment
1587 case TPACPI_LIFE_EXITING:
1589 case TPACPI_LIFE_RUNNING:
1590 hotkey_poll_setup_safe(0);
1594 /* Should only happen if tpacpi_lifecycle is corrupt */
1599 static void hotkey_inputdev_close(struct input_dev *dev)
1601 /* disable hotkey polling when possible */
1602 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1603 hotkey_poll_setup_safe(0);
1606 /* sysfs hotkey enable ------------------------------------------------- */
1607 static ssize_t hotkey_enable_show(struct device *dev,
1608 struct device_attribute *attr,
1613 res = hotkey_status_get(&status);
1617 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1620 static ssize_t hotkey_enable_store(struct device *dev,
1621 struct device_attribute *attr,
1622 const char *buf, size_t count)
1627 if (parse_strtoul(buf, 1, &t))
1630 res = hotkey_status_set(t);
1632 return (res) ? res : count;
1635 static struct device_attribute dev_attr_hotkey_enable =
1636 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1637 hotkey_enable_show, hotkey_enable_store);
1639 /* sysfs hotkey mask --------------------------------------------------- */
1640 static ssize_t hotkey_mask_show(struct device *dev,
1641 struct device_attribute *attr,
1646 if (mutex_lock_interruptible(&hotkey_mutex))
1647 return -ERESTARTSYS;
1648 res = hotkey_mask_get();
1649 mutex_unlock(&hotkey_mutex);
1652 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1655 static ssize_t hotkey_mask_store(struct device *dev,
1656 struct device_attribute *attr,
1657 const char *buf, size_t count)
1662 if (parse_strtoul(buf, 0xffffffffUL, &t))
1665 if (mutex_lock_interruptible(&hotkey_mutex))
1666 return -ERESTARTSYS;
1668 res = hotkey_mask_set(t);
1670 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1671 hotkey_poll_setup(1);
1674 mutex_unlock(&hotkey_mutex);
1676 return (res) ? res : count;
1679 static struct device_attribute dev_attr_hotkey_mask =
1680 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1681 hotkey_mask_show, hotkey_mask_store);
1683 /* sysfs hotkey bios_enabled ------------------------------------------- */
1684 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1685 struct device_attribute *attr,
1688 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1691 static struct device_attribute dev_attr_hotkey_bios_enabled =
1692 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1694 /* sysfs hotkey bios_mask ---------------------------------------------- */
1695 static ssize_t hotkey_bios_mask_show(struct device *dev,
1696 struct device_attribute *attr,
1699 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1702 static struct device_attribute dev_attr_hotkey_bios_mask =
1703 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1705 /* sysfs hotkey all_mask ----------------------------------------------- */
1706 static ssize_t hotkey_all_mask_show(struct device *dev,
1707 struct device_attribute *attr,
1710 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1711 hotkey_all_mask | hotkey_source_mask);
1714 static struct device_attribute dev_attr_hotkey_all_mask =
1715 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1717 /* sysfs hotkey recommended_mask --------------------------------------- */
1718 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1719 struct device_attribute *attr,
1722 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1723 (hotkey_all_mask | hotkey_source_mask)
1724 & ~hotkey_reserved_mask);
1727 static struct device_attribute dev_attr_hotkey_recommended_mask =
1728 __ATTR(hotkey_recommended_mask, S_IRUGO,
1729 hotkey_recommended_mask_show, NULL);
1731 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1733 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1734 static ssize_t hotkey_source_mask_show(struct device *dev,
1735 struct device_attribute *attr,
1738 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1741 static ssize_t hotkey_source_mask_store(struct device *dev,
1742 struct device_attribute *attr,
1743 const char *buf, size_t count)
1747 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1748 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1751 if (mutex_lock_interruptible(&hotkey_mutex))
1752 return -ERESTARTSYS;
1754 HOTKEY_CONFIG_CRITICAL_START
1755 hotkey_source_mask = t;
1756 HOTKEY_CONFIG_CRITICAL_END
1758 hotkey_poll_setup(1);
1760 mutex_unlock(&hotkey_mutex);
1765 static struct device_attribute dev_attr_hotkey_source_mask =
1766 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1767 hotkey_source_mask_show, hotkey_source_mask_store);
1769 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1770 static ssize_t hotkey_poll_freq_show(struct device *dev,
1771 struct device_attribute *attr,
1774 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1777 static ssize_t hotkey_poll_freq_store(struct device *dev,
1778 struct device_attribute *attr,
1779 const char *buf, size_t count)
1783 if (parse_strtoul(buf, 25, &t))
1786 if (mutex_lock_interruptible(&hotkey_mutex))
1787 return -ERESTARTSYS;
1789 hotkey_poll_freq = t;
1791 hotkey_poll_setup(1);
1792 mutex_unlock(&hotkey_mutex);
1797 static struct device_attribute dev_attr_hotkey_poll_freq =
1798 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1799 hotkey_poll_freq_show, hotkey_poll_freq_store);
1801 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1803 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1804 static ssize_t hotkey_radio_sw_show(struct device *dev,
1805 struct device_attribute *attr,
1809 res = hotkey_get_wlsw(&s);
1813 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1816 static struct device_attribute dev_attr_hotkey_radio_sw =
1817 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1819 static void hotkey_radio_sw_notify_change(void)
1821 if (tp_features.hotkey_wlsw)
1822 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1826 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1827 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1828 struct device_attribute *attr,
1832 res = hotkey_get_tablet_mode(&s);
1836 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1839 static struct device_attribute dev_attr_hotkey_tablet_mode =
1840 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1842 static void hotkey_tablet_mode_notify_change(void)
1844 if (tp_features.hotkey_tablet)
1845 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1846 "hotkey_tablet_mode");
1849 /* sysfs hotkey report_mode -------------------------------------------- */
1850 static ssize_t hotkey_report_mode_show(struct device *dev,
1851 struct device_attribute *attr,
1854 return snprintf(buf, PAGE_SIZE, "%d\n",
1855 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1858 static struct device_attribute dev_attr_hotkey_report_mode =
1859 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1861 /* sysfs wakeup reason (pollable) -------------------------------------- */
1862 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1863 struct device_attribute *attr,
1866 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1869 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1870 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1872 static void hotkey_wakeup_reason_notify_change(void)
1874 if (tp_features.hotkey_mask)
1875 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1879 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1880 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1881 struct device_attribute *attr,
1884 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1887 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1888 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1889 hotkey_wakeup_hotunplug_complete_show, NULL);
1891 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1893 if (tp_features.hotkey_mask)
1894 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1895 "wakeup_hotunplug_complete");
1898 /* --------------------------------------------------------------------- */
1900 static struct attribute *hotkey_attributes[] __initdata = {
1901 &dev_attr_hotkey_enable.attr,
1902 &dev_attr_hotkey_bios_enabled.attr,
1903 &dev_attr_hotkey_report_mode.attr,
1904 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1905 &dev_attr_hotkey_mask.attr,
1906 &dev_attr_hotkey_all_mask.attr,
1907 &dev_attr_hotkey_recommended_mask.attr,
1908 &dev_attr_hotkey_source_mask.attr,
1909 &dev_attr_hotkey_poll_freq.attr,
1913 static struct attribute *hotkey_mask_attributes[] __initdata = {
1914 &dev_attr_hotkey_bios_mask.attr,
1915 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1916 &dev_attr_hotkey_mask.attr,
1917 &dev_attr_hotkey_all_mask.attr,
1918 &dev_attr_hotkey_recommended_mask.attr,
1920 &dev_attr_hotkey_wakeup_reason.attr,
1921 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1924 static void hotkey_exit(void)
1926 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1927 hotkey_poll_stop_sync();
1930 if (hotkey_dev_attributes)
1931 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1933 kfree(hotkey_keycode_map);
1935 if (tp_features.hotkey) {
1936 dbg_printk(TPACPI_DBG_EXIT,
1937 "restoring original hot key mask\n");
1938 /* no short-circuit boolean operator below! */
1939 if ((hotkey_mask_set(hotkey_orig_mask) |
1940 hotkey_status_set(hotkey_orig_status)) != 0)
1942 "failed to restore hot key mask "
1943 "to BIOS defaults\n");
1947 static int __init hotkey_init(struct ibm_init_struct *iibm)
1949 /* Requirements for changing the default keymaps:
1951 * 1. Many of the keys are mapped to KEY_RESERVED for very
1952 * good reasons. Do not change them unless you have deep
1953 * knowledge on the IBM and Lenovo ThinkPad firmware for
1954 * the various ThinkPad models. The driver behaves
1955 * differently for KEY_RESERVED: such keys have their
1956 * hot key mask *unset* in mask_recommended, and also
1957 * in the initial hot key mask programmed into the
1958 * firmware at driver load time, which means the firm-
1959 * ware may react very differently if you change them to
1962 * 2. You must be subscribed to the linux-thinkpad and
1963 * ibm-acpi-devel mailing lists, and you should read the
1964 * list archives since 2007 if you want to change the
1965 * keymaps. This requirement exists so that you will
1966 * know the past history of problems with the thinkpad-
1967 * acpi driver keymaps, and also that you will be
1968 * listening to any bug reports;
1970 * 3. Do not send thinkpad-acpi specific patches directly to
1971 * for merging, *ever*. Send them to the linux-acpi
1972 * mailinglist for comments. Merging is to be done only
1973 * through acpi-test and the ACPI maintainer.
1975 * If the above is too much to ask, don't change the keymap.
1976 * Ask the thinkpad-acpi maintainer to do it, instead.
1978 static u16 ibm_keycode_map[] __initdata = {
1979 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1980 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1981 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1982 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1984 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1985 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1986 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1987 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1989 /* brightness: firmware always reacts to them, unless
1990 * X.org did some tricks in the radeon BIOS scratch
1991 * registers of *some* models */
1992 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1993 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1995 /* Thinklight: firmware always react to it */
1996 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1998 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1999 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2001 /* Volume: firmware always react to it and reprograms
2002 * the built-in *extra* mixer. Never map it to control
2003 * another mixer by default. */
2004 KEY_RESERVED, /* 0x14: VOLUME UP */
2005 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2006 KEY_RESERVED, /* 0x16: MUTE */
2008 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2010 /* (assignments unknown, please report if found) */
2011 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2012 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2014 static u16 lenovo_keycode_map[] __initdata = {
2015 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2016 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2017 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2018 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2020 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2021 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2022 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2023 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2025 /* These either have to go through ACPI video, or
2026 * act like in the IBM ThinkPads, so don't ever
2027 * enable them by default */
2028 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2029 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2031 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2033 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2034 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2036 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2037 * react to it and reprograms the built-in *extra* mixer.
2038 * Never map it to control another mixer by default.
2040 * T60?, T61, R60?, R61: firmware and EC tries to send
2041 * these over the regular keyboard, so these are no-ops,
2042 * but there are still weird bugs re. MUTE, so do not
2043 * change unless you get test reports from all Lenovo
2044 * models. May cause the BIOS to interfere with the
2047 KEY_RESERVED, /* 0x14: VOLUME UP */
2048 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2049 KEY_RESERVED, /* 0x16: MUTE */
2051 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2053 /* (assignments unknown, please report if found) */
2054 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2055 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2058 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2059 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2060 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2066 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2068 BUG_ON(!tpacpi_inputdev);
2069 BUG_ON(tpacpi_inputdev->open != NULL ||
2070 tpacpi_inputdev->close != NULL);
2072 TPACPI_ACPIHANDLE_INIT(hkey);
2073 mutex_init(&hotkey_mutex);
2075 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2076 mutex_init(&hotkey_thread_mutex);
2077 mutex_init(&hotkey_thread_data_mutex);
2080 /* hotkey not supported on 570 */
2081 tp_features.hotkey = hkey_handle != NULL;
2083 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2084 str_supported(tp_features.hotkey));
2086 if (!tp_features.hotkey)
2089 hotkey_dev_attributes = create_attr_set(13, NULL);
2090 if (!hotkey_dev_attributes)
2092 res = add_many_to_attr_set(hotkey_dev_attributes,
2094 ARRAY_SIZE(hotkey_attributes));
2098 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2099 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2100 for HKEY interface version 0x100 */
2101 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2102 if ((hkeyv >> 8) != 1) {
2103 printk(TPACPI_ERR "unknown version of the "
2104 "HKEY interface: 0x%x\n", hkeyv);
2105 printk(TPACPI_ERR "please report this to %s\n",
2109 * MHKV 0x100 in A31, R40, R40e,
2110 * T4x, X31, and later
2112 tp_features.hotkey_mask = 1;
2116 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2117 str_supported(tp_features.hotkey_mask));
2119 if (tp_features.hotkey_mask) {
2120 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2123 "missing MHKA handler, "
2124 "please report this to %s\n",
2126 /* FN+F12, FN+F4, FN+F3 */
2127 hotkey_all_mask = 0x080cU;
2131 /* hotkey_source_mask *must* be zero for
2132 * the first hotkey_mask_get */
2133 res = hotkey_status_get(&hotkey_orig_status);
2137 if (tp_features.hotkey_mask) {
2138 res = hotkey_mask_get();
2142 hotkey_orig_mask = hotkey_mask;
2143 res = add_many_to_attr_set(
2144 hotkey_dev_attributes,
2145 hotkey_mask_attributes,
2146 ARRAY_SIZE(hotkey_mask_attributes));
2151 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2152 if (tp_features.hotkey_mask) {
2153 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2156 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2159 vdbg_printk(TPACPI_DBG_INIT,
2160 "hotkey source mask 0x%08x, polling freq %d\n",
2161 hotkey_source_mask, hotkey_poll_freq);
2164 /* Not all thinkpads have a hardware radio switch */
2165 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2166 tp_features.hotkey_wlsw = 1;
2168 "radio switch found; radios are %s\n",
2169 enabled(status, 0));
2171 if (tp_features.hotkey_wlsw)
2172 res = add_to_attr_set(hotkey_dev_attributes,
2173 &dev_attr_hotkey_radio_sw.attr);
2175 /* For X41t, X60t, X61t Tablets... */
2176 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2177 tp_features.hotkey_tablet = 1;
2179 "possible tablet mode switch found; "
2180 "ThinkPad in %s mode\n",
2181 (status & TP_HOTKEY_TABLET_MASK)?
2182 "tablet" : "laptop");
2183 res = add_to_attr_set(hotkey_dev_attributes,
2184 &dev_attr_hotkey_tablet_mode.attr);
2188 res = register_attr_set_with_sysfs(
2189 hotkey_dev_attributes,
2190 &tpacpi_pdev->dev.kobj);
2194 /* Set up key map */
2196 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2198 if (!hotkey_keycode_map) {
2200 "failed to allocate memory for key map\n");
2205 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2206 dbg_printk(TPACPI_DBG_INIT,
2207 "using Lenovo default hot key map\n");
2208 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2209 TPACPI_HOTKEY_MAP_SIZE);
2211 dbg_printk(TPACPI_DBG_INIT,
2212 "using IBM default hot key map\n");
2213 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2214 TPACPI_HOTKEY_MAP_SIZE);
2217 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2218 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2219 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2220 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2221 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2222 tpacpi_inputdev->keycode = hotkey_keycode_map;
2223 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2224 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2225 set_bit(hotkey_keycode_map[i],
2226 tpacpi_inputdev->keybit);
2228 if (i < sizeof(hotkey_reserved_mask)*8)
2229 hotkey_reserved_mask |= 1 << i;
2233 if (tp_features.hotkey_wlsw) {
2234 set_bit(EV_SW, tpacpi_inputdev->evbit);
2235 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2237 if (tp_features.hotkey_tablet) {
2238 set_bit(EV_SW, tpacpi_inputdev->evbit);
2239 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2242 /* Do not issue duplicate brightness change events to
2244 if (!tp_features.bright_acpimode)
2245 /* update bright_acpimode... */
2246 tpacpi_check_std_acpi_brightness_support();
2248 if (tp_features.bright_acpimode) {
2250 "This ThinkPad has standard ACPI backlight "
2251 "brightness control, supported by the ACPI "
2253 printk(TPACPI_NOTICE
2254 "Disabling thinkpad-acpi brightness events "
2257 /* The hotkey_reserved_mask change below is not
2258 * necessary while the keys are at KEY_RESERVED in the
2259 * default map, but better safe than sorry, leave it
2260 * here as a marker of what we have to do, especially
2261 * when we finally become able to set this at runtime
2262 * on response to X.org requests */
2263 hotkey_reserved_mask |=
2264 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2265 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2268 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2269 res = hotkey_status_set(1);
2274 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2275 & ~hotkey_reserved_mask)
2276 | hotkey_orig_mask);
2277 if (res < 0 && res != -ENXIO) {
2282 dbg_printk(TPACPI_DBG_INIT,
2283 "legacy hot key reporting over procfs %s\n",
2284 (hotkey_report_mode < 2) ?
2285 "enabled" : "disabled");
2287 tpacpi_inputdev->open = &hotkey_inputdev_open;
2288 tpacpi_inputdev->close = &hotkey_inputdev_close;
2290 hotkey_poll_setup_safe(1);
2291 tpacpi_input_send_radiosw();
2292 tpacpi_input_send_tabletsw();
2297 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2298 hotkey_dev_attributes = NULL;
2300 return (res < 0)? res : 1;
2303 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2306 unsigned int scancode;
2311 if (event != 0x80) {
2313 "unknown HKEY notification event %d\n", event);
2314 /* forward it to userspace, maybe it knows how to handle it */
2315 acpi_bus_generate_netlink_event(
2316 ibm->acpi->device->pnp.device_class,
2317 ibm->acpi->device->dev.bus_id,
2323 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2324 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2337 switch (hkey >> 12) {
2339 /* 0x1000-0x1FFF: key presses */
2340 scancode = hkey & 0xfff;
2341 if (scancode > 0 && scancode < 0x21) {
2343 if (!(hotkey_source_mask & (1 << scancode))) {
2344 tpacpi_input_send_key(scancode);
2356 case 0x2304: /* suspend, undock */
2357 case 0x2404: /* hibernation, undock */
2358 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2361 case 0x2305: /* suspend, bay eject */
2362 case 0x2405: /* hibernation, bay eject */
2363 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2369 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2371 "woke up due to a hot-unplug "
2373 hotkey_wakeup_reason_notify_change();
2377 /* bay-related wakeups */
2378 if (hkey == 0x3003) {
2379 hotkey_autosleep_ack = 1;
2382 hotkey_wakeup_hotunplug_complete_notify_change();
2388 /* dock-related wakeups */
2389 if (hkey == 0x4003) {
2390 hotkey_autosleep_ack = 1;
2393 hotkey_wakeup_hotunplug_complete_notify_change();
2399 /* 0x5000-0x5FFF: human interface helpers */
2401 case 0x5010: /* Lenovo new BIOS: brightness changed */
2402 case 0x500b: /* X61t: tablet pen inserted into bay */
2403 case 0x500c: /* X61t: tablet pen removed from bay */
2405 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2406 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2407 tpacpi_input_send_tabletsw();
2408 hotkey_tablet_mode_notify_change();
2413 /* LID switch events. Do not propagate */
2421 /* 0x7000-0x7FFF: misc */
2422 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2423 tpacpi_input_send_radiosw();
2424 hotkey_radio_sw_notify_change();
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_input_send_radiosw();
2468 hotkey_radio_sw_notify_change();
2469 hotkey_tablet_mode_notify_change();
2470 hotkey_wakeup_reason_notify_change();
2471 hotkey_wakeup_hotunplug_complete_notify_change();
2472 hotkey_poll_setup_safe(0);
2475 /* procfs -------------------------------------------------------------- */
2476 static int hotkey_read(char *p)
2481 if (!tp_features.hotkey) {
2482 len += sprintf(p + len, "status:\t\tnot supported\n");
2486 if (mutex_lock_interruptible(&hotkey_mutex))
2487 return -ERESTARTSYS;
2488 res = hotkey_status_get(&status);
2490 res = hotkey_mask_get();
2491 mutex_unlock(&hotkey_mutex);
2495 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2496 if (tp_features.hotkey_mask) {
2497 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2498 len += sprintf(p + len,
2499 "commands:\tenable, disable, reset, <mask>\n");
2501 len += sprintf(p + len, "mask:\t\tnot supported\n");
2502 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2508 static int hotkey_write(char *buf)
2514 if (!tp_features.hotkey)
2517 if (mutex_lock_interruptible(&hotkey_mutex))
2518 return -ERESTARTSYS;
2524 while ((cmd = next_cmd(&buf))) {
2525 if (strlencmp(cmd, "enable") == 0) {
2527 } else if (strlencmp(cmd, "disable") == 0) {
2529 } else if (strlencmp(cmd, "reset") == 0) {
2530 status = hotkey_orig_status;
2531 mask = hotkey_orig_mask;
2532 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2534 } else if (sscanf(cmd, "%x", &mask) == 1) {
2542 res = hotkey_status_set(status);
2544 if (!res && mask != hotkey_mask)
2545 res = hotkey_mask_set(mask);
2548 mutex_unlock(&hotkey_mutex);
2552 static const struct acpi_device_id ibm_htk_device_ids[] = {
2553 {TPACPI_ACPI_HKEY_HID, 0},
2557 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2558 .hid = ibm_htk_device_ids,
2559 .notify = hotkey_notify,
2560 .handle = &hkey_handle,
2561 .type = ACPI_DEVICE_NOTIFY,
2564 static struct ibm_struct hotkey_driver_data = {
2566 .read = hotkey_read,
2567 .write = hotkey_write,
2568 .exit = hotkey_exit,
2569 .resume = hotkey_resume,
2570 .suspend = hotkey_suspend,
2571 .acpi = &ibm_hotkey_acpidriver,
2574 /*************************************************************************
2575 * Bluetooth subdriver
2579 /* ACPI GBDC/SBDC bits */
2580 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2581 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2582 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2585 static int bluetooth_get_radiosw(void);
2586 static int bluetooth_set_radiosw(int radio_on);
2588 /* sysfs bluetooth enable ---------------------------------------------- */
2589 static ssize_t bluetooth_enable_show(struct device *dev,
2590 struct device_attribute *attr,
2595 status = bluetooth_get_radiosw();
2599 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2602 static ssize_t bluetooth_enable_store(struct device *dev,
2603 struct device_attribute *attr,
2604 const char *buf, size_t count)
2609 if (parse_strtoul(buf, 1, &t))
2612 res = bluetooth_set_radiosw(t);
2614 return (res) ? res : count;
2617 static struct device_attribute dev_attr_bluetooth_enable =
2618 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2619 bluetooth_enable_show, bluetooth_enable_store);
2621 /* --------------------------------------------------------------------- */
2623 static struct attribute *bluetooth_attributes[] = {
2624 &dev_attr_bluetooth_enable.attr,
2628 static const struct attribute_group bluetooth_attr_group = {
2629 .attrs = bluetooth_attributes,
2632 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2637 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2639 TPACPI_ACPIHANDLE_INIT(hkey);
2641 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2642 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2643 tp_features.bluetooth = hkey_handle &&
2644 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2646 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2647 str_supported(tp_features.bluetooth),
2650 if (tp_features.bluetooth &&
2651 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2652 /* no bluetooth hardware present in system */
2653 tp_features.bluetooth = 0;
2654 dbg_printk(TPACPI_DBG_INIT,
2655 "bluetooth hardware not installed\n");
2658 if (tp_features.bluetooth) {
2659 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2660 &bluetooth_attr_group);
2665 return (tp_features.bluetooth)? 0 : 1;
2668 static void bluetooth_exit(void)
2670 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2671 &bluetooth_attr_group);
2674 static int bluetooth_get_radiosw(void)
2678 if (!tp_features.bluetooth)
2681 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2684 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2687 static int bluetooth_set_radiosw(int radio_on)
2691 if (!tp_features.bluetooth)
2694 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2697 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2699 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2700 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2706 /* procfs -------------------------------------------------------------- */
2707 static int bluetooth_read(char *p)
2710 int status = bluetooth_get_radiosw();
2712 if (!tp_features.bluetooth)
2713 len += sprintf(p + len, "status:\t\tnot supported\n");
2715 len += sprintf(p + len, "status:\t\t%s\n",
2716 (status)? "enabled" : "disabled");
2717 len += sprintf(p + len, "commands:\tenable, disable\n");
2723 static int bluetooth_write(char *buf)
2727 if (!tp_features.bluetooth)
2730 while ((cmd = next_cmd(&buf))) {
2731 if (strlencmp(cmd, "enable") == 0) {
2732 bluetooth_set_radiosw(1);
2733 } else if (strlencmp(cmd, "disable") == 0) {
2734 bluetooth_set_radiosw(0);
2742 static struct ibm_struct bluetooth_driver_data = {
2743 .name = "bluetooth",
2744 .read = bluetooth_read,
2745 .write = bluetooth_write,
2746 .exit = bluetooth_exit,
2749 /*************************************************************************
2754 /* ACPI GWAN/SWAN bits */
2755 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2756 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2757 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2760 static int wan_get_radiosw(void);
2761 static int wan_set_radiosw(int radio_on);
2763 /* sysfs wan enable ---------------------------------------------------- */
2764 static ssize_t wan_enable_show(struct device *dev,
2765 struct device_attribute *attr,
2770 status = wan_get_radiosw();
2774 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2777 static ssize_t wan_enable_store(struct device *dev,
2778 struct device_attribute *attr,
2779 const char *buf, size_t count)
2784 if (parse_strtoul(buf, 1, &t))
2787 res = wan_set_radiosw(t);
2789 return (res) ? res : count;
2792 static struct device_attribute dev_attr_wan_enable =
2793 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2794 wan_enable_show, wan_enable_store);
2796 /* --------------------------------------------------------------------- */
2798 static struct attribute *wan_attributes[] = {
2799 &dev_attr_wan_enable.attr,
2803 static const struct attribute_group wan_attr_group = {
2804 .attrs = wan_attributes,
2807 static int __init wan_init(struct ibm_init_struct *iibm)
2812 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2814 TPACPI_ACPIHANDLE_INIT(hkey);
2816 tp_features.wan = hkey_handle &&
2817 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2819 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2820 str_supported(tp_features.wan),
2823 if (tp_features.wan &&
2824 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
2825 /* no wan hardware present in system */
2826 tp_features.wan = 0;
2827 dbg_printk(TPACPI_DBG_INIT,
2828 "wan hardware not installed\n");
2831 if (tp_features.wan) {
2832 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2838 return (tp_features.wan)? 0 : 1;
2841 static void wan_exit(void)
2843 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2847 static int wan_get_radiosw(void)
2851 if (!tp_features.wan)
2854 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2857 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2860 static int wan_set_radiosw(int radio_on)
2864 if (!tp_features.wan)
2867 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2870 status |= TP_ACPI_WANCARD_RADIOSSW;
2872 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2873 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2879 /* procfs -------------------------------------------------------------- */
2880 static int wan_read(char *p)
2883 int status = wan_get_radiosw();
2885 if (!tp_features.wan)
2886 len += sprintf(p + len, "status:\t\tnot supported\n");
2888 len += sprintf(p + len, "status:\t\t%s\n",
2889 (status)? "enabled" : "disabled");
2890 len += sprintf(p + len, "commands:\tenable, disable\n");
2896 static int wan_write(char *buf)
2900 if (!tp_features.wan)
2903 while ((cmd = next_cmd(&buf))) {
2904 if (strlencmp(cmd, "enable") == 0) {
2906 } else if (strlencmp(cmd, "disable") == 0) {
2915 static struct ibm_struct wan_driver_data = {
2920 .flags.experimental = 1,
2923 /*************************************************************************
2927 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
2929 enum video_access_mode {
2930 TPACPI_VIDEO_NONE = 0,
2931 TPACPI_VIDEO_570, /* 570 */
2932 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2933 TPACPI_VIDEO_NEW, /* all others */
2936 enum { /* video status flags, based on VIDEO_570 */
2937 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2938 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2939 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2942 enum { /* TPACPI_VIDEO_570 constants */
2943 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2944 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2945 * video_status_flags */
2946 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2947 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2950 static enum video_access_mode video_supported;
2951 static int video_orig_autosw;
2953 static int video_autosw_get(void);
2954 static int video_autosw_set(int enable);
2956 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2958 static int __init video_init(struct ibm_init_struct *iibm)
2962 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2964 TPACPI_ACPIHANDLE_INIT(vid);
2965 TPACPI_ACPIHANDLE_INIT(vid2);
2967 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2968 /* G41, assume IVGA doesn't change */
2969 vid_handle = vid2_handle;
2972 /* video switching not supported on R30, R31 */
2973 video_supported = TPACPI_VIDEO_NONE;
2974 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2976 video_supported = TPACPI_VIDEO_570;
2977 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2978 /* 600e/x, 770e, 770x */
2979 video_supported = TPACPI_VIDEO_770;
2982 video_supported = TPACPI_VIDEO_NEW;
2984 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2985 str_supported(video_supported != TPACPI_VIDEO_NONE),
2988 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2991 static void video_exit(void)
2993 dbg_printk(TPACPI_DBG_EXIT,
2994 "restoring original video autoswitch mode\n");
2995 if (video_autosw_set(video_orig_autosw))
2996 printk(TPACPI_ERR "error while trying to restore original "
2997 "video autoswitch mode\n");
3000 static int video_outputsw_get(void)
3005 switch (video_supported) {
3006 case TPACPI_VIDEO_570:
3007 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3008 TP_ACPI_VIDEO_570_PHSCMD))
3010 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3012 case TPACPI_VIDEO_770:
3013 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3016 status |= TP_ACPI_VIDEO_S_LCD;
3017 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3020 status |= TP_ACPI_VIDEO_S_CRT;
3022 case TPACPI_VIDEO_NEW:
3023 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3024 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3027 status |= TP_ACPI_VIDEO_S_CRT;
3029 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3030 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3033 status |= TP_ACPI_VIDEO_S_LCD;
3034 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3037 status |= TP_ACPI_VIDEO_S_DVI;
3046 static int video_outputsw_set(int status)
3051 switch (video_supported) {
3052 case TPACPI_VIDEO_570:
3053 res = acpi_evalf(NULL, NULL,
3054 "\\_SB.PHS2", "vdd",
3055 TP_ACPI_VIDEO_570_PHS2CMD,
3056 status | TP_ACPI_VIDEO_570_PHS2SET);
3058 case TPACPI_VIDEO_770:
3059 autosw = video_autosw_get();
3063 res = video_autosw_set(1);
3066 res = acpi_evalf(vid_handle, NULL,
3067 "ASWT", "vdd", status * 0x100, 0);
3068 if (!autosw && video_autosw_set(autosw)) {
3070 "video auto-switch left enabled due to error\n");
3074 case TPACPI_VIDEO_NEW:
3075 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3076 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3082 return (res)? 0 : -EIO;
3085 static int video_autosw_get(void)
3089 switch (video_supported) {
3090 case TPACPI_VIDEO_570:
3091 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3094 case TPACPI_VIDEO_770:
3095 case TPACPI_VIDEO_NEW:
3096 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3106 static int video_autosw_set(int enable)
3108 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3113 static int video_outputsw_cycle(void)
3115 int autosw = video_autosw_get();
3121 switch (video_supported) {
3122 case TPACPI_VIDEO_570:
3123 res = video_autosw_set(1);
3126 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3128 case TPACPI_VIDEO_770:
3129 case TPACPI_VIDEO_NEW:
3130 res = video_autosw_set(1);
3133 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3138 if (!autosw && video_autosw_set(autosw)) {
3140 "video auto-switch left enabled due to error\n");
3144 return (res)? 0 : -EIO;
3147 static int video_expand_toggle(void)
3149 switch (video_supported) {
3150 case TPACPI_VIDEO_570:
3151 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3153 case TPACPI_VIDEO_770:
3154 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3156 case TPACPI_VIDEO_NEW:
3157 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3165 static int video_read(char *p)
3170 if (video_supported == TPACPI_VIDEO_NONE) {
3171 len += sprintf(p + len, "status:\t\tnot supported\n");
3175 status = video_outputsw_get();
3179 autosw = video_autosw_get();
3183 len += sprintf(p + len, "status:\t\tsupported\n");
3184 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3185 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3186 if (video_supported == TPACPI_VIDEO_NEW)
3187 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3188 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3189 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3190 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3191 if (video_supported == TPACPI_VIDEO_NEW)
3192 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3193 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3194 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3199 static int video_write(char *buf)
3202 int enable, disable, status;
3205 if (video_supported == TPACPI_VIDEO_NONE)
3211 while ((cmd = next_cmd(&buf))) {
3212 if (strlencmp(cmd, "lcd_enable") == 0) {
3213 enable |= TP_ACPI_VIDEO_S_LCD;
3214 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3215 disable |= TP_ACPI_VIDEO_S_LCD;
3216 } else if (strlencmp(cmd, "crt_enable") == 0) {
3217 enable |= TP_ACPI_VIDEO_S_CRT;
3218 } else if (strlencmp(cmd, "crt_disable") == 0) {
3219 disable |= TP_ACPI_VIDEO_S_CRT;
3220 } else if (video_supported == TPACPI_VIDEO_NEW &&
3221 strlencmp(cmd, "dvi_enable") == 0) {
3222 enable |= TP_ACPI_VIDEO_S_DVI;
3223 } else if (video_supported == TPACPI_VIDEO_NEW &&
3224 strlencmp(cmd, "dvi_disable") == 0) {
3225 disable |= TP_ACPI_VIDEO_S_DVI;
3226 } else if (strlencmp(cmd, "auto_enable") == 0) {
3227 res = video_autosw_set(1);
3230 } else if (strlencmp(cmd, "auto_disable") == 0) {
3231 res = video_autosw_set(0);
3234 } else if (strlencmp(cmd, "video_switch") == 0) {
3235 res = video_outputsw_cycle();
3238 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3239 res = video_expand_toggle();
3246 if (enable || disable) {
3247 status = video_outputsw_get();
3250 res = video_outputsw_set((status & ~disable) | enable);
3258 static struct ibm_struct video_driver_data = {
3261 .write = video_write,
3265 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3267 /*************************************************************************
3268 * Light (thinklight) subdriver
3271 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3272 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3274 static int light_get_status(void)
3278 if (tp_features.light_status) {
3279 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3287 static int light_set_status(int status)
3291 if (tp_features.light) {
3293 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3295 TP_CMOS_THINKLIGHT_ON :
3296 TP_CMOS_THINKLIGHT_OFF);
3298 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3301 return (rc)? 0 : -EIO;
3307 static void light_set_status_worker(struct work_struct *work)
3309 struct tpacpi_led_classdev *data =
3310 container_of(work, struct tpacpi_led_classdev, work);
3312 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3313 light_set_status((data->new_brightness != LED_OFF));
3316 static void light_sysfs_set(struct led_classdev *led_cdev,
3317 enum led_brightness brightness)
3319 struct tpacpi_led_classdev *data =
3320 container_of(led_cdev,
3321 struct tpacpi_led_classdev,
3323 data->new_brightness = brightness;
3324 queue_work(tpacpi_wq, &data->work);
3327 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3329 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3332 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3334 .name = "tpacpi::thinklight",
3335 .brightness_set = &light_sysfs_set,
3336 .brightness_get = &light_sysfs_get,
3340 static int __init light_init(struct ibm_init_struct *iibm)
3344 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3346 TPACPI_ACPIHANDLE_INIT(ledb);
3347 TPACPI_ACPIHANDLE_INIT(lght);
3348 TPACPI_ACPIHANDLE_INIT(cmos);
3349 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3351 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3352 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3354 if (tp_features.light)
3355 /* light status not supported on
3356 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3357 tp_features.light_status =
3358 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3360 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3361 str_supported(tp_features.light),
3362 str_supported(tp_features.light_status));
3364 if (!tp_features.light)
3367 rc = led_classdev_register(&tpacpi_pdev->dev,
3368 &tpacpi_led_thinklight.led_classdev);
3371 tp_features.light = 0;
3372 tp_features.light_status = 0;
3380 static void light_exit(void)
3382 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3383 if (work_pending(&tpacpi_led_thinklight.work))
3384 flush_workqueue(tpacpi_wq);
3387 static int light_read(char *p)
3392 if (!tp_features.light) {
3393 len += sprintf(p + len, "status:\t\tnot supported\n");
3394 } else if (!tp_features.light_status) {
3395 len += sprintf(p + len, "status:\t\tunknown\n");
3396 len += sprintf(p + len, "commands:\ton, off\n");
3398 status = light_get_status();
3401 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3402 len += sprintf(p + len, "commands:\ton, off\n");
3408 static int light_write(char *buf)
3413 if (!tp_features.light)
3416 while ((cmd = next_cmd(&buf))) {
3417 if (strlencmp(cmd, "on") == 0) {
3419 } else if (strlencmp(cmd, "off") == 0) {
3425 return light_set_status(newstatus);
3428 static struct ibm_struct light_driver_data = {
3431 .write = light_write,
3435 /*************************************************************************
3439 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3441 static void dock_notify(struct ibm_struct *ibm, u32 event);
3442 static int dock_read(char *p);
3443 static int dock_write(char *buf);
3445 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3446 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3447 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3448 "\\_SB.PCI.ISA.SLCE", /* 570 */
3449 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3451 /* don't list other alternatives as we install a notify handler on the 570 */
3452 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3454 static const struct acpi_device_id ibm_pci_device_ids[] = {
3455 {PCI_ROOT_HID_STRING, 0},
3459 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3461 .notify = dock_notify,
3462 .handle = &dock_handle,
3463 .type = ACPI_SYSTEM_NOTIFY,
3466 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3467 * We just use it to get notifications of dock hotplug
3468 * in very old thinkpads */
3469 .hid = ibm_pci_device_ids,
3470 .notify = dock_notify,
3471 .handle = &pci_handle,
3472 .type = ACPI_SYSTEM_NOTIFY,
3476 static struct ibm_struct dock_driver_data[2] = {
3480 .write = dock_write,
3481 .acpi = &ibm_dock_acpidriver[0],
3485 .acpi = &ibm_dock_acpidriver[1],
3489 #define dock_docked() (_sta(dock_handle) & 1)
3491 static int __init dock_init(struct ibm_init_struct *iibm)
3493 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3495 TPACPI_ACPIHANDLE_INIT(dock);
3497 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3498 str_supported(dock_handle != NULL));
3500 return (dock_handle)? 0 : 1;
3503 static int __init dock_init2(struct ibm_init_struct *iibm)
3507 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3509 if (dock_driver_data[0].flags.acpi_driver_registered &&
3510 dock_driver_data[0].flags.acpi_notify_installed) {
3511 TPACPI_ACPIHANDLE_INIT(pci);
3512 dock2_needed = (pci_handle != NULL);
3513 vdbg_printk(TPACPI_DBG_INIT,
3514 "dock PCI handler for the TP 570 is %s\n",
3515 str_supported(dock2_needed));
3517 vdbg_printk(TPACPI_DBG_INIT,
3518 "dock subdriver part 2 not required\n");
3522 return (dock2_needed)? 0 : 1;
3525 static void dock_notify(struct ibm_struct *ibm, u32 event)
3527 int docked = dock_docked();
3528 int pci = ibm->acpi->hid && ibm->acpi->device &&
3529 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3532 if (event == 1 && !pci) /* 570 */
3533 data = 1; /* button */
3534 else if (event == 1 && pci) /* 570 */
3535 data = 3; /* dock */
3536 else if (event == 3 && docked)
3537 data = 1; /* button */
3538 else if (event == 3 && !docked)
3539 data = 2; /* undock */
3540 else if (event == 0 && docked)
3541 data = 3; /* dock */
3543 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3544 event, _sta(dock_handle));
3545 data = 0; /* unknown */
3547 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3548 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3549 ibm->acpi->device->dev.bus_id,
3553 static int dock_read(char *p)
3556 int docked = dock_docked();
3559 len += sprintf(p + len, "status:\t\tnot supported\n");
3561 len += sprintf(p + len, "status:\t\tundocked\n");
3563 len += sprintf(p + len, "status:\t\tdocked\n");
3564 len += sprintf(p + len, "commands:\tdock, undock\n");
3570 static int dock_write(char *buf)
3577 while ((cmd = next_cmd(&buf))) {
3578 if (strlencmp(cmd, "undock") == 0) {
3579 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3580 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3582 } else if (strlencmp(cmd, "dock") == 0) {
3583 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3592 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3594 /*************************************************************************
3598 #ifdef CONFIG_THINKPAD_ACPI_BAY
3600 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3601 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3602 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3603 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3604 ); /* A21e, R30, R31 */
3605 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3606 "_EJ0", /* all others */
3607 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3608 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3609 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3611 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3615 static int __init bay_init(struct ibm_init_struct *iibm)
3617 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3619 TPACPI_ACPIHANDLE_INIT(bay);
3621 TPACPI_ACPIHANDLE_INIT(bay_ej);
3622 TPACPI_ACPIHANDLE_INIT(bay2);
3624 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3626 tp_features.bay_status = bay_handle &&
3627 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3628 tp_features.bay_status2 = bay2_handle &&
3629 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3631 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3632 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3633 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3634 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3636 vdbg_printk(TPACPI_DBG_INIT,
3637 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3638 str_supported(tp_features.bay_status),
3639 str_supported(tp_features.bay_eject),
3640 str_supported(tp_features.bay_status2),
3641 str_supported(tp_features.bay_eject2));
3643 return (tp_features.bay_status || tp_features.bay_eject ||
3644 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3647 static void bay_notify(struct ibm_struct *ibm, u32 event)
3649 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3650 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3651 ibm->acpi->device->dev.bus_id,
3655 #define bay_occupied(b) (_sta(b##_handle) & 1)
3657 static int bay_read(char *p)
3660 int occupied = bay_occupied(bay);
3661 int occupied2 = bay_occupied(bay2);
3664 len += sprintf(p + len, "status:\t\t%s\n",
3665 tp_features.bay_status ?
3666 (occupied ? "occupied" : "unoccupied") :
3668 if (tp_features.bay_status2)
3669 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3670 "occupied" : "unoccupied");
3672 eject = tp_features.bay_eject && occupied;
3673 eject2 = tp_features.bay_eject2 && occupied2;
3675 if (eject && eject2)
3676 len += sprintf(p + len, "commands:\teject, eject2\n");
3678 len += sprintf(p + len, "commands:\teject\n");
3680 len += sprintf(p + len, "commands:\teject2\n");
3685 static int bay_write(char *buf)
3689 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3692 while ((cmd = next_cmd(&buf))) {
3693 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3694 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3696 } else if (tp_features.bay_eject2 &&
3697 strlencmp(cmd, "eject2") == 0) {
3698 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3707 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3708 .notify = bay_notify,
3709 .handle = &bay_handle,
3710 .type = ACPI_SYSTEM_NOTIFY,
3713 static struct ibm_struct bay_driver_data = {
3717 .acpi = &ibm_bay_acpidriver,
3720 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3722 /*************************************************************************
3726 /* sysfs cmos_command -------------------------------------------------- */
3727 static ssize_t cmos_command_store(struct device *dev,
3728 struct device_attribute *attr,
3729 const char *buf, size_t count)
3731 unsigned long cmos_cmd;
3734 if (parse_strtoul(buf, 21, &cmos_cmd))
3737 res = issue_thinkpad_cmos_command(cmos_cmd);
3738 return (res)? res : count;
3741 static struct device_attribute dev_attr_cmos_command =
3742 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3744 /* --------------------------------------------------------------------- */
3746 static int __init cmos_init(struct ibm_init_struct *iibm)
3750 vdbg_printk(TPACPI_DBG_INIT,
3751 "initializing cmos commands subdriver\n");
3753 TPACPI_ACPIHANDLE_INIT(cmos);
3755 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3756 str_supported(cmos_handle != NULL));
3758 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3762 return (cmos_handle)? 0 : 1;
3765 static void cmos_exit(void)
3767 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3770 static int cmos_read(char *p)
3774 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3775 R30, R31, T20-22, X20-21 */
3777 len += sprintf(p + len, "status:\t\tnot supported\n");
3779 len += sprintf(p + len, "status:\t\tsupported\n");
3780 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3786 static int cmos_write(char *buf)
3791 while ((cmd = next_cmd(&buf))) {
3792 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3793 cmos_cmd >= 0 && cmos_cmd <= 21) {
3798 res = issue_thinkpad_cmos_command(cmos_cmd);
3806 static struct ibm_struct cmos_driver_data = {
3809 .write = cmos_write,
3813 /*************************************************************************
3817 enum led_access_mode {
3818 TPACPI_LED_NONE = 0,
3819 TPACPI_LED_570, /* 570 */
3820 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3821 TPACPI_LED_NEW, /* all others */
3824 enum { /* For TPACPI_LED_OLD */
3825 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3826 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3827 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3836 static enum led_access_mode led_supported;
3838 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
3839 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3840 /* T20-22, X20-21 */
3841 "LED", /* all others */
3844 #define TPACPI_LED_NUMLEDS 8
3845 static struct tpacpi_led_classdev *tpacpi_leds;
3846 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
3847 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
3848 /* there's a limit of 19 chars + NULL before 2.6.26 */
3850 "tpacpi:orange:batt",
3851 "tpacpi:green:batt",
3852 "tpacpi::dock_active",
3853 "tpacpi::bay_active",
3854 "tpacpi::dock_batt",
3855 "tpacpi::unknown_led",
3859 static int led_get_status(const unsigned int led)
3862 enum led_status_t led_s;
3864 switch (led_supported) {
3865 case TPACPI_LED_570:
3866 if (!acpi_evalf(ec_handle,
3867 &status, "GLED", "dd", 1 << led))
3869 led_s = (status == 0)?
3874 tpacpi_led_state_cache[led] = led_s;
3883 static int led_set_status(const unsigned int led,
3884 const enum led_status_t ledstatus)
3886 /* off, on, blink. Index is led_status_t */
3887 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
3888 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
3892 switch (led_supported) {
3893 case TPACPI_LED_570:
3897 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3898 (1 << led), led_sled_arg1[ledstatus]))
3901 case TPACPI_LED_OLD:
3902 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3905 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
3907 rc = ec_write(TPACPI_LED_EC_HLBL,
3908 (ledstatus == TPACPI_LED_BLINK) << led);
3910 rc = ec_write(TPACPI_LED_EC_HLCL,
3911 (ledstatus != TPACPI_LED_OFF) << led);
3913 case TPACPI_LED_NEW:
3915 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3916 led, led_led_arg1[ledstatus]))
3924 tpacpi_led_state_cache[led] = ledstatus;
3929 static void led_sysfs_set_status(unsigned int led,
3930 enum led_brightness brightness)
3933 (brightness == LED_OFF) ?
3935 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
3936 TPACPI_LED_BLINK : TPACPI_LED_ON);
3939 static void led_set_status_worker(struct work_struct *work)
3941 struct tpacpi_led_classdev *data =
3942 container_of(work, struct tpacpi_led_classdev, work);
3944 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3945 led_sysfs_set_status(data->led, data->new_brightness);
3948 static void led_sysfs_set(struct led_classdev *led_cdev,
3949 enum led_brightness brightness)
3951 struct tpacpi_led_classdev *data = container_of(led_cdev,
3952 struct tpacpi_led_classdev, led_classdev);
3954 data->new_brightness = brightness;
3955 queue_work(tpacpi_wq, &data->work);
3958 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
3959 unsigned long *delay_on, unsigned long *delay_off)
3961 struct tpacpi_led_classdev *data = container_of(led_cdev,
3962 struct tpacpi_led_classdev, led_classdev);
3964 /* Can we choose the flash rate? */
3965 if (*delay_on == 0 && *delay_off == 0) {
3966 /* yes. set them to the hardware blink rate (1 Hz) */
3967 *delay_on = 500; /* ms */
3968 *delay_off = 500; /* ms */
3969 } else if ((*delay_on != 500) || (*delay_off != 500))
3972 data->new_brightness = TPACPI_LED_BLINK;
3973 queue_work(tpacpi_wq, &data->work);
3978 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
3982 struct tpacpi_led_classdev *data = container_of(led_cdev,
3983 struct tpacpi_led_classdev, led_classdev);
3985 rc = led_get_status(data->led);
3987 if (rc == TPACPI_LED_OFF || rc < 0)
3988 rc = LED_OFF; /* no error handling in led class :( */
3995 static void led_exit(void)
3999 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4000 if (tpacpi_leds[i].led_classdev.name)
4001 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4007 static int __init led_init(struct ibm_init_struct *iibm)
4012 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4014 TPACPI_ACPIHANDLE_INIT(led);
4017 /* led not supported on R30, R31 */
4018 led_supported = TPACPI_LED_NONE;
4019 else if (strlencmp(led_path, "SLED") == 0)
4021 led_supported = TPACPI_LED_570;
4022 else if (strlencmp(led_path, "SYSL") == 0)
4023 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4024 led_supported = TPACPI_LED_OLD;
4027 led_supported = TPACPI_LED_NEW;
4029 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4030 str_supported(led_supported), led_supported);
4032 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4035 printk(TPACPI_ERR "Out of memory for LED data\n");
4039 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4040 tpacpi_leds[i].led = i;
4042 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4043 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4044 if (led_supported == TPACPI_LED_570)
4045 tpacpi_leds[i].led_classdev.brightness_get =
4048 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4050 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4052 rc = led_classdev_register(&tpacpi_pdev->dev,
4053 &tpacpi_leds[i].led_classdev);
4055 tpacpi_leds[i].led_classdev.name = NULL;
4061 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4064 #define str_led_status(s) \
4065 ((s) == TPACPI_LED_OFF ? "off" : \
4066 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4068 static int led_read(char *p)
4072 if (!led_supported) {
4073 len += sprintf(p + len, "status:\t\tnot supported\n");
4076 len += sprintf(p + len, "status:\t\tsupported\n");
4078 if (led_supported == TPACPI_LED_570) {
4081 for (i = 0; i < 8; i++) {
4082 status = led_get_status(i);
4085 len += sprintf(p + len, "%d:\t\t%s\n",
4086 i, str_led_status(status));
4090 len += sprintf(p + len, "commands:\t"
4091 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4096 static int led_write(char *buf)
4100 enum led_status_t s;
4105 while ((cmd = next_cmd(&buf))) {
4106 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4109 if (strstr(cmd, "off")) {
4111 } else if (strstr(cmd, "on")) {
4113 } else if (strstr(cmd, "blink")) {
4114 s = TPACPI_LED_BLINK;
4119 rc = led_set_status(led, s);
4127 static struct ibm_struct led_driver_data = {
4134 /*************************************************************************
4138 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4140 static int __init beep_init(struct ibm_init_struct *iibm)
4142 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4144 TPACPI_ACPIHANDLE_INIT(beep);
4146 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4147 str_supported(beep_handle != NULL));
4149 return (beep_handle)? 0 : 1;
4152 static int beep_read(char *p)
4157 len += sprintf(p + len, "status:\t\tnot supported\n");
4159 len += sprintf(p + len, "status:\t\tsupported\n");
4160 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4166 static int beep_write(char *buf)
4174 while ((cmd = next_cmd(&buf))) {
4175 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4176 beep_cmd >= 0 && beep_cmd <= 17) {
4180 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4187 static struct ibm_struct beep_driver_data = {
4190 .write = beep_write,
4193 /*************************************************************************
4197 enum thermal_access_mode {
4198 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4199 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4200 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4201 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4202 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4205 enum { /* TPACPI_THERMAL_TPEC_* */
4206 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4207 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4208 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4211 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4212 struct ibm_thermal_sensors_struct {
4213 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4216 static enum thermal_access_mode thermal_read_mode;
4218 /* idx is zero-based */
4219 static int thermal_get_sensor(int idx, s32 *value)
4225 t = TP_EC_THERMAL_TMP0;
4227 switch (thermal_read_mode) {
4228 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4229 case TPACPI_THERMAL_TPEC_16:
4230 if (idx >= 8 && idx <= 15) {
4231 t = TP_EC_THERMAL_TMP8;
4236 case TPACPI_THERMAL_TPEC_8:
4238 if (!acpi_ec_read(t + idx, &tmp))
4240 *value = tmp * 1000;
4245 case TPACPI_THERMAL_ACPI_UPDT:
4247 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4248 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4250 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4252 *value = (t - 2732) * 100;
4257 case TPACPI_THERMAL_ACPI_TMP07:
4259 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4260 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4262 if (t > 127 || t < -127)
4263 t = TP_EC_THERMAL_TMP_NA;
4269 case TPACPI_THERMAL_NONE:
4277 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4288 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4291 for (i = 0 ; i < n; i++) {
4292 res = thermal_get_sensor(i, &s->temp[i]);
4300 /* sysfs temp##_input -------------------------------------------------- */
4302 static ssize_t thermal_temp_input_show(struct device *dev,
4303 struct device_attribute *attr,
4306 struct sensor_device_attribute *sensor_attr =
4307 to_sensor_dev_attr(attr);
4308 int idx = sensor_attr->index;
4312 res = thermal_get_sensor(idx, &value);
4315 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4318 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4321 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4322 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4323 thermal_temp_input_show, NULL, _idxB)
4325 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4326 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4327 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4328 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4329 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4330 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4331 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4332 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4333 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4334 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4335 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4336 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4337 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4338 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4339 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4340 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4341 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4344 #define THERMAL_ATTRS(X) \
4345 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4347 static struct attribute *thermal_temp_input_attr[] = {
4367 static const struct attribute_group thermal_temp_input16_group = {
4368 .attrs = thermal_temp_input_attr
4371 static const struct attribute_group thermal_temp_input8_group = {
4372 .attrs = &thermal_temp_input_attr[8]
4375 #undef THERMAL_SENSOR_ATTR_TEMP
4376 #undef THERMAL_ATTRS
4378 /* --------------------------------------------------------------------- */
4380 static int __init thermal_init(struct ibm_init_struct *iibm)
4387 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4389 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4391 if (thinkpad_id.ec_model) {
4393 * Direct EC access mode: sensors at registers
4394 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4395 * non-implemented, thermal sensors return 0x80 when
4400 for (i = 0; i < 8; i++) {
4401 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4407 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4415 /* This is sheer paranoia, but we handle it anyway */
4418 "ThinkPad ACPI EC access misbehaving, "
4419 "falling back to ACPI TMPx access "
4421 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4424 "ThinkPad ACPI EC access misbehaving, "
4425 "disabling thermal sensors access\n");
4426 thermal_read_mode = TPACPI_THERMAL_NONE;
4431 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4433 } else if (acpi_tmp7) {
4434 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4435 /* 600e/x, 770e, 770x */
4436 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4438 /* Standard ACPI TMPx access, max 8 sensors */
4439 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4442 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4443 thermal_read_mode = TPACPI_THERMAL_NONE;
4446 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4447 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4450 switch (thermal_read_mode) {
4451 case TPACPI_THERMAL_TPEC_16:
4452 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4453 &thermal_temp_input16_group);
4457 case TPACPI_THERMAL_TPEC_8:
4458 case TPACPI_THERMAL_ACPI_TMP07:
4459 case TPACPI_THERMAL_ACPI_UPDT:
4460 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4461 &thermal_temp_input8_group);
4465 case TPACPI_THERMAL_NONE:
4473 static void thermal_exit(void)
4475 switch (thermal_read_mode) {
4476 case TPACPI_THERMAL_TPEC_16:
4477 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4478 &thermal_temp_input16_group);
4480 case TPACPI_THERMAL_TPEC_8:
4481 case TPACPI_THERMAL_ACPI_TMP07:
4482 case TPACPI_THERMAL_ACPI_UPDT:
4483 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4484 &thermal_temp_input16_group);
4486 case TPACPI_THERMAL_NONE:
4492 static int thermal_read(char *p)
4496 struct ibm_thermal_sensors_struct t;
4498 n = thermal_get_sensors(&t);
4499 if (unlikely(n < 0))
4502 len += sprintf(p + len, "temperatures:\t");
4505 for (i = 0; i < (n - 1); i++)
4506 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4507 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4509 len += sprintf(p + len, "not supported\n");
4514 static struct ibm_struct thermal_driver_data = {
4516 .read = thermal_read,
4517 .exit = thermal_exit,
4520 /*************************************************************************
4524 static u8 ecdump_regs[256];
4526 static int ecdump_read(char *p)
4532 len += sprintf(p + len, "EC "
4533 " +00 +01 +02 +03 +04 +05 +06 +07"
4534 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4535 for (i = 0; i < 256; i += 16) {
4536 len += sprintf(p + len, "EC 0x%02x:", i);
4537 for (j = 0; j < 16; j++) {
4538 if (!acpi_ec_read(i + j, &v))
4540 if (v != ecdump_regs[i + j])
4541 len += sprintf(p + len, " *%02x", v);
4543 len += sprintf(p + len, " %02x", v);
4544 ecdump_regs[i + j] = v;
4546 len += sprintf(p + len, "\n");
4551 /* These are way too dangerous to advertise openly... */
4553 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4554 " (<offset> is 00-ff, <value> is 00-ff)\n");
4555 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4556 " (<offset> is 00-ff, <value> is 0-255)\n");
4561 static int ecdump_write(char *buf)
4566 while ((cmd = next_cmd(&buf))) {
4567 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4569 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4573 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4574 if (!acpi_ec_write(i, v))
4583 static struct ibm_struct ecdump_driver_data = {
4585 .read = ecdump_read,
4586 .write = ecdump_write,
4587 .flags.experimental = 1,
4590 /*************************************************************************
4591 * Backlight/brightness subdriver
4594 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4597 TP_EC_BACKLIGHT = 0x31,
4599 /* TP_EC_BACKLIGHT bitmasks */
4600 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4601 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4602 TP_EC_BACKLIGHT_MAPSW = 0x20,
4605 static struct backlight_device *ibm_backlight_device;
4606 static int brightness_mode;
4607 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4609 static struct mutex brightness_mutex;
4612 * ThinkPads can read brightness from two places: EC 0x31, or
4613 * CMOS NVRAM byte 0x5E, bits 0-3.
4615 * EC 0x31 has the following layout
4616 * Bit 7: unknown function
4617 * Bit 6: unknown function
4618 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4619 * Bit 4: must be set to zero to avoid problems
4620 * Bit 3-0: backlight brightness level
4622 * brightness_get_raw returns status data in the EC 0x31 layout
4624 static int brightness_get_raw(int *status)
4626 u8 lec = 0, lcmos = 0, level = 0;
4628 if (brightness_mode & 1) {
4629 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4631 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4633 if (brightness_mode & 2) {
4634 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4635 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4636 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4637 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4641 if (brightness_mode == 3) {
4642 *status = lec; /* Prefer EC, CMOS is just a backing store */
4643 lec &= TP_EC_BACKLIGHT_LVLMSK;
4645 tp_warned.bright_cmos_ec_unsync = 0;
4647 if (!tp_warned.bright_cmos_ec_unsync) {
4649 "CMOS NVRAM (%u) and EC (%u) do not "
4650 "agree on display brightness level\n",
4651 (unsigned int) lcmos,
4652 (unsigned int) lec);
4653 tp_warned.bright_cmos_ec_unsync = 1;
4664 /* May return EINTR which can always be mapped to ERESTARTSYS */
4665 static int brightness_set(int value)
4667 int cmos_cmd, inc, i, res;
4671 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4675 res = mutex_lock_interruptible(&brightness_mutex);
4679 res = brightness_get_raw(¤t_value);
4683 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4684 current_value &= TP_EC_BACKLIGHT_LVLMSK;
4686 cmos_cmd = value > current_value ?
4687 TP_CMOS_BRIGHTNESS_UP :
4688 TP_CMOS_BRIGHTNESS_DOWN;
4689 inc = (value > current_value)? 1 : -1;
4692 for (i = current_value; i != value; i += inc) {
4693 if ((brightness_mode & 2) &&
4694 issue_thinkpad_cmos_command(cmos_cmd)) {
4698 if ((brightness_mode & 1) &&
4699 !acpi_ec_write(TP_EC_BACKLIGHT,
4700 (i + inc) | command_bits)) {
4707 mutex_unlock(&brightness_mutex);
4711 /* sysfs backlight class ----------------------------------------------- */
4713 static int brightness_update_status(struct backlight_device *bd)
4715 /* it is the backlight class's job (caller) to handle
4716 * EINTR and other errors properly */
4717 return brightness_set(
4718 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4719 bd->props.power == FB_BLANK_UNBLANK) ?
4720 bd->props.brightness : 0);
4723 static int brightness_get(struct backlight_device *bd)
4727 res = brightness_get_raw(&status);
4729 return 0; /* FIXME: teach backlight about error handling */
4731 return status & TP_EC_BACKLIGHT_LVLMSK;
4734 static struct backlight_ops ibm_backlight_data = {
4735 .get_brightness = brightness_get,
4736 .update_status = brightness_update_status,
4739 /* --------------------------------------------------------------------- */
4741 static int __init brightness_init(struct ibm_init_struct *iibm)
4745 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4747 mutex_init(&brightness_mutex);
4750 * We always attempt to detect acpi support, so as to switch
4751 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4752 * going to publish a backlight interface
4754 b = tpacpi_check_std_acpi_brightness_support();
4756 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4757 printk(TPACPI_NOTICE
4758 "Lenovo BIOS switched to ACPI backlight "
4761 if (brightness_enable > 1) {
4762 printk(TPACPI_NOTICE
4763 "standard ACPI backlight interface "
4764 "available, not loading native one...\n");
4769 if (!brightness_enable) {
4770 dbg_printk(TPACPI_DBG_INIT,
4771 "brightness support disabled by "
4772 "module parameter\n");
4778 "Unsupported brightness interface, "
4779 "please contact %s\n", TPACPI_MAIL);
4783 tp_features.bright_16levels = 1;
4785 if (!brightness_mode) {
4786 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4787 brightness_mode = 2;
4789 brightness_mode = 3;
4791 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4795 if (brightness_mode > 3)
4798 if (brightness_get_raw(&b) < 0)
4801 if (tp_features.bright_16levels)
4803 "detected a 16-level brightness capable ThinkPad\n");
4805 ibm_backlight_device = backlight_device_register(
4806 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4807 &ibm_backlight_data);
4808 if (IS_ERR(ibm_backlight_device)) {
4809 printk(TPACPI_ERR "Could not register backlight device\n");
4810 return PTR_ERR(ibm_backlight_device);
4812 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4814 ibm_backlight_device->props.max_brightness =
4815 (tp_features.bright_16levels)? 15 : 7;
4816 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
4817 backlight_update_status(ibm_backlight_device);
4822 static void brightness_exit(void)
4824 if (ibm_backlight_device) {
4825 vdbg_printk(TPACPI_DBG_EXIT,
4826 "calling backlight_device_unregister()\n");
4827 backlight_device_unregister(ibm_backlight_device);
4831 static int brightness_read(char *p)
4836 level = brightness_get(NULL);
4838 len += sprintf(p + len, "level:\t\tunreadable\n");
4840 len += sprintf(p + len, "level:\t\t%d\n", level);
4841 len += sprintf(p + len, "commands:\tup, down\n");
4842 len += sprintf(p + len, "commands:\tlevel <level>"
4843 " (<level> is 0-%d)\n",
4844 (tp_features.bright_16levels) ? 15 : 7);
4850 static int brightness_write(char *buf)
4855 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4857 level = brightness_get(NULL);
4861 while ((cmd = next_cmd(&buf))) {
4862 if (strlencmp(cmd, "up") == 0) {
4863 if (level < max_level)
4865 } else if (strlencmp(cmd, "down") == 0) {
4868 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4869 level >= 0 && level <= max_level) {
4876 * Now we know what the final level should be, so we try to set it.
4877 * Doing it this way makes the syscall restartable in case of EINTR
4879 rc = brightness_set(level);
4880 return (rc == -EINTR)? ERESTARTSYS : rc;
4883 static struct ibm_struct brightness_driver_data = {
4884 .name = "brightness",
4885 .read = brightness_read,
4886 .write = brightness_write,
4887 .exit = brightness_exit,
4890 /*************************************************************************
4894 static int volume_offset = 0x30;
4896 static int volume_read(char *p)
4901 if (!acpi_ec_read(volume_offset, &level)) {
4902 len += sprintf(p + len, "level:\t\tunreadable\n");
4904 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4905 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4906 len += sprintf(p + len, "commands:\tup, down, mute\n");
4907 len += sprintf(p + len, "commands:\tlevel <level>"
4908 " (<level> is 0-15)\n");
4914 static int volume_write(char *buf)
4916 int cmos_cmd, inc, i;
4918 int new_level, new_mute;
4921 while ((cmd = next_cmd(&buf))) {
4922 if (!acpi_ec_read(volume_offset, &level))
4924 new_mute = mute = level & 0x40;
4925 new_level = level = level & 0xf;
4927 if (strlencmp(cmd, "up") == 0) {
4931 new_level = level == 15 ? 15 : level + 1;
4932 } else if (strlencmp(cmd, "down") == 0) {
4936 new_level = level == 0 ? 0 : level - 1;
4937 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4938 new_level >= 0 && new_level <= 15) {
4940 } else if (strlencmp(cmd, "mute") == 0) {
4945 if (new_level != level) {
4946 /* mute doesn't change */
4948 cmos_cmd = (new_level > level) ?
4949 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4950 inc = new_level > level ? 1 : -1;
4952 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4953 !acpi_ec_write(volume_offset, level)))
4956 for (i = level; i != new_level; i += inc)
4957 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4958 !acpi_ec_write(volume_offset, i + inc))
4962 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4963 !acpi_ec_write(volume_offset, new_level + mute))) {
4968 if (new_mute != mute) {
4969 /* level doesn't change */
4971 cmos_cmd = (new_mute) ?
4972 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4974 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4975 !acpi_ec_write(volume_offset, level + new_mute))
4983 static struct ibm_struct volume_driver_data = {
4985 .read = volume_read,
4986 .write = volume_write,
4989 /*************************************************************************
4996 * TPACPI_FAN_RD_ACPI_GFAN:
4997 * ACPI GFAN method: returns fan level
4999 * see TPACPI_FAN_WR_ACPI_SFAN
5000 * EC 0x2f (HFSP) not available if GFAN exists
5002 * TPACPI_FAN_WR_ACPI_SFAN:
5003 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5005 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5008 * TPACPI_FAN_WR_TPEC:
5009 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5010 * Supported on almost all ThinkPads
5012 * Fan speed changes of any sort (including those caused by the
5013 * disengaged mode) are usually done slowly by the firmware as the
5014 * maximum ammount of fan duty cycle change per second seems to be
5017 * Reading is not available if GFAN exists.
5018 * Writing is not available if SFAN exists.
5021 * 7 automatic mode engaged;
5022 * (default operation mode of the ThinkPad)
5023 * fan level is ignored in this mode.
5024 * 6 full speed mode (takes precedence over bit 7);
5025 * not available on all thinkpads. May disable
5026 * the tachometer while the fan controller ramps up
5027 * the speed (which can take up to a few *minutes*).
5028 * Speeds up fan to 100% duty-cycle, which is far above
5029 * the standard RPM levels. It is not impossible that
5030 * it could cause hardware damage.
5031 * 5-3 unused in some models. Extra bits for fan level
5032 * in others, but still useless as all values above
5033 * 7 map to the same speed as level 7 in these models.
5034 * 2-0 fan level (0..7 usually)
5036 * 0x07 = max (set when temperatures critical)
5037 * Some ThinkPads may have other levels, see
5038 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5040 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5041 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5042 * does so, its initial value is meaningless (0x07).
5044 * For firmware bugs, refer to:
5045 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5049 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5050 * Main fan tachometer reading (in RPM)
5052 * This register is present on all ThinkPads with a new-style EC, and
5053 * it is known not to be present on the A21m/e, and T22, as there is
5054 * something else in offset 0x84 according to the ACPI DSDT. Other
5055 * ThinkPads from this same time period (and earlier) probably lack the
5056 * tachometer as well.
5058 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5059 * was never fixed by IBM to report the EC firmware version string
5060 * probably support the tachometer (like the early X models), so
5061 * detecting it is quite hard. We need more data to know for sure.
5063 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5066 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5069 * For firmware bugs, refer to:
5070 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5072 * TPACPI_FAN_WR_ACPI_FANS:
5073 * ThinkPad X31, X40, X41. Not available in the X60.
5075 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5076 * high speed. ACPI DSDT seems to map these three speeds to levels
5077 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5078 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5080 * The speeds are stored on handles
5081 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5083 * There are three default speed sets, acessible as handles:
5084 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5086 * ACPI DSDT switches which set is in use depending on various
5089 * TPACPI_FAN_WR_TPEC is also available and should be used to
5090 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5091 * but the ACPI tables just mention level 7.
5094 enum { /* Fan control constants */
5095 fan_status_offset = 0x2f, /* EC register 0x2f */
5096 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5097 * 0x84 must be read before 0x85 */
5099 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5100 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5102 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5105 enum fan_status_access_mode {
5106 TPACPI_FAN_NONE = 0, /* No fan status or control */
5107 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5108 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5111 enum fan_control_access_mode {
5112 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5113 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5114 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5115 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5118 enum fan_control_commands {
5119 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5120 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5121 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5122 * and also watchdog cmd */
5125 static int fan_control_allowed;
5127 static enum fan_status_access_mode fan_status_access_mode;
5128 static enum fan_control_access_mode fan_control_access_mode;
5129 static enum fan_control_commands fan_control_commands;
5131 static u8 fan_control_initial_status;
5132 static u8 fan_control_desired_level;
5133 static int fan_watchdog_maxinterval;
5135 static struct mutex fan_mutex;
5137 static void fan_watchdog_fire(struct work_struct *ignored);
5138 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5140 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5141 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5142 "\\FSPD", /* 600e/x, 770e, 770x */
5144 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5145 "JFNS", /* 770x-JL */
5149 * Call with fan_mutex held
5151 static void fan_update_desired_level(u8 status)
5154 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5156 fan_control_desired_level = 7;
5158 fan_control_desired_level = status;
5162 static int fan_get_status(u8 *status)
5167 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5169 switch (fan_status_access_mode) {
5170 case TPACPI_FAN_RD_ACPI_GFAN:
5171 /* 570, 600e/x, 770e, 770x */
5173 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5181 case TPACPI_FAN_RD_TPEC:
5182 /* all except 570, 600e/x, 770e, 770x */
5183 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5198 static int fan_get_status_safe(u8 *status)
5203 if (mutex_lock_interruptible(&fan_mutex))
5204 return -ERESTARTSYS;
5205 rc = fan_get_status(&s);
5207 fan_update_desired_level(s);
5208 mutex_unlock(&fan_mutex);
5216 static int fan_get_speed(unsigned int *speed)
5220 switch (fan_status_access_mode) {
5221 case TPACPI_FAN_RD_TPEC:
5222 /* all except 570, 600e/x, 770e, 770x */
5223 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5224 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5228 *speed = (hi << 8) | lo;
5239 static int fan_set_level(int level)
5241 if (!fan_control_allowed)
5244 switch (fan_control_access_mode) {
5245 case TPACPI_FAN_WR_ACPI_SFAN:
5246 if (level >= 0 && level <= 7) {
5247 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5253 case TPACPI_FAN_WR_ACPI_FANS:
5254 case TPACPI_FAN_WR_TPEC:
5255 if ((level != TP_EC_FAN_AUTO) &&
5256 (level != TP_EC_FAN_FULLSPEED) &&
5257 ((level < 0) || (level > 7)))
5260 /* safety net should the EC not support AUTO
5261 * or FULLSPEED mode bits and just ignore them */
5262 if (level & TP_EC_FAN_FULLSPEED)
5263 level |= 7; /* safety min speed 7 */
5264 else if (level & TP_EC_FAN_AUTO)
5265 level |= 4; /* safety min speed 4 */
5267 if (!acpi_ec_write(fan_status_offset, level))
5270 tp_features.fan_ctrl_status_undef = 0;
5279 static int fan_set_level_safe(int level)
5283 if (!fan_control_allowed)
5286 if (mutex_lock_interruptible(&fan_mutex))
5287 return -ERESTARTSYS;
5289 if (level == TPACPI_FAN_LAST_LEVEL)
5290 level = fan_control_desired_level;
5292 rc = fan_set_level(level);
5294 fan_update_desired_level(level);
5296 mutex_unlock(&fan_mutex);
5300 static int fan_set_enable(void)
5305 if (!fan_control_allowed)
5308 if (mutex_lock_interruptible(&fan_mutex))
5309 return -ERESTARTSYS;
5311 switch (fan_control_access_mode) {
5312 case TPACPI_FAN_WR_ACPI_FANS:
5313 case TPACPI_FAN_WR_TPEC:
5314 rc = fan_get_status(&s);
5318 /* Don't go out of emergency fan mode */
5321 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5324 if (!acpi_ec_write(fan_status_offset, s))
5327 tp_features.fan_ctrl_status_undef = 0;
5332 case TPACPI_FAN_WR_ACPI_SFAN:
5333 rc = fan_get_status(&s);
5339 /* Set fan to at least level 4 */
5342 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5352 mutex_unlock(&fan_mutex);
5356 static int fan_set_disable(void)
5360 if (!fan_control_allowed)
5363 if (mutex_lock_interruptible(&fan_mutex))
5364 return -ERESTARTSYS;
5367 switch (fan_control_access_mode) {
5368 case TPACPI_FAN_WR_ACPI_FANS:
5369 case TPACPI_FAN_WR_TPEC:
5370 if (!acpi_ec_write(fan_status_offset, 0x00))
5373 fan_control_desired_level = 0;
5374 tp_features.fan_ctrl_status_undef = 0;
5378 case TPACPI_FAN_WR_ACPI_SFAN:
5379 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5382 fan_control_desired_level = 0;
5390 mutex_unlock(&fan_mutex);
5394 static int fan_set_speed(int speed)
5398 if (!fan_control_allowed)
5401 if (mutex_lock_interruptible(&fan_mutex))
5402 return -ERESTARTSYS;
5405 switch (fan_control_access_mode) {
5406 case TPACPI_FAN_WR_ACPI_FANS:
5407 if (speed >= 0 && speed <= 65535) {
5408 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5409 speed, speed, speed))
5419 mutex_unlock(&fan_mutex);
5423 static void fan_watchdog_reset(void)
5425 static int fan_watchdog_active;
5427 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5430 if (fan_watchdog_active)
5431 cancel_delayed_work(&fan_watchdog_task);
5433 if (fan_watchdog_maxinterval > 0 &&
5434 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5435 fan_watchdog_active = 1;
5436 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5437 msecs_to_jiffies(fan_watchdog_maxinterval
5440 "failed to queue the fan watchdog, "
5441 "watchdog will not trigger\n");
5444 fan_watchdog_active = 0;
5447 static void fan_watchdog_fire(struct work_struct *ignored)
5451 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5454 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5455 rc = fan_set_enable();
5457 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5458 "will try again later...\n", -rc);
5459 /* reschedule for later */
5460 fan_watchdog_reset();
5465 * SYSFS fan layout: hwmon compatible (device)
5468 * 0: "disengaged" mode
5470 * 2: native EC "auto" mode (recommended, hardware default)
5472 * pwm*: set speed in manual mode, ignored otherwise.
5473 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5476 * fan*_input: tachometer reading, RPM
5479 * SYSFS fan layout: extensions
5481 * fan_watchdog (driver):
5482 * fan watchdog interval in seconds, 0 disables (default), max 120
5485 /* sysfs fan pwm1_enable ----------------------------------------------- */
5486 static ssize_t fan_pwm1_enable_show(struct device *dev,
5487 struct device_attribute *attr,
5493 res = fan_get_status_safe(&status);
5497 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5498 if (status != fan_control_initial_status) {
5499 tp_features.fan_ctrl_status_undef = 0;
5501 /* Return most likely status. In fact, it
5502 * might be the only possible status */
5503 status = TP_EC_FAN_AUTO;
5507 if (status & TP_EC_FAN_FULLSPEED) {
5509 } else if (status & TP_EC_FAN_AUTO) {
5514 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5517 static ssize_t fan_pwm1_enable_store(struct device *dev,
5518 struct device_attribute *attr,
5519 const char *buf, size_t count)
5524 if (parse_strtoul(buf, 2, &t))
5529 level = TP_EC_FAN_FULLSPEED;
5532 level = TPACPI_FAN_LAST_LEVEL;
5535 level = TP_EC_FAN_AUTO;
5538 /* reserved for software-controlled auto mode */
5544 res = fan_set_level_safe(level);
5550 fan_watchdog_reset();
5555 static struct device_attribute dev_attr_fan_pwm1_enable =
5556 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5557 fan_pwm1_enable_show, fan_pwm1_enable_store);
5559 /* sysfs fan pwm1 ------------------------------------------------------ */
5560 static ssize_t fan_pwm1_show(struct device *dev,
5561 struct device_attribute *attr,
5567 res = fan_get_status_safe(&status);
5571 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5572 if (status != fan_control_initial_status) {
5573 tp_features.fan_ctrl_status_undef = 0;
5575 status = TP_EC_FAN_AUTO;
5580 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5581 status = fan_control_desired_level;
5586 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5589 static ssize_t fan_pwm1_store(struct device *dev,
5590 struct device_attribute *attr,
5591 const char *buf, size_t count)
5595 u8 status, newlevel;
5597 if (parse_strtoul(buf, 255, &s))
5600 /* scale down from 0-255 to 0-7 */
5601 newlevel = (s >> 5) & 0x07;
5603 if (mutex_lock_interruptible(&fan_mutex))
5604 return -ERESTARTSYS;
5606 rc = fan_get_status(&status);
5607 if (!rc && (status &
5608 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5609 rc = fan_set_level(newlevel);
5613 fan_update_desired_level(newlevel);
5614 fan_watchdog_reset();
5618 mutex_unlock(&fan_mutex);
5619 return (rc)? rc : count;
5622 static struct device_attribute dev_attr_fan_pwm1 =
5623 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5624 fan_pwm1_show, fan_pwm1_store);
5626 /* sysfs fan fan1_input ------------------------------------------------ */
5627 static ssize_t fan_fan1_input_show(struct device *dev,
5628 struct device_attribute *attr,
5634 res = fan_get_speed(&speed);
5638 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5641 static struct device_attribute dev_attr_fan_fan1_input =
5642 __ATTR(fan1_input, S_IRUGO,
5643 fan_fan1_input_show, NULL);
5645 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5646 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5649 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5652 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5653 const char *buf, size_t count)
5657 if (parse_strtoul(buf, 120, &t))
5660 if (!fan_control_allowed)
5663 fan_watchdog_maxinterval = t;
5664 fan_watchdog_reset();
5669 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5670 fan_fan_watchdog_show, fan_fan_watchdog_store);
5672 /* --------------------------------------------------------------------- */
5673 static struct attribute *fan_attributes[] = {
5674 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5675 &dev_attr_fan_fan1_input.attr,
5679 static const struct attribute_group fan_attr_group = {
5680 .attrs = fan_attributes,
5683 static int __init fan_init(struct ibm_init_struct *iibm)
5687 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5689 mutex_init(&fan_mutex);
5690 fan_status_access_mode = TPACPI_FAN_NONE;
5691 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5692 fan_control_commands = 0;
5693 fan_watchdog_maxinterval = 0;
5694 tp_features.fan_ctrl_status_undef = 0;
5695 fan_control_desired_level = 7;
5697 TPACPI_ACPIHANDLE_INIT(fans);
5698 TPACPI_ACPIHANDLE_INIT(gfan);
5699 TPACPI_ACPIHANDLE_INIT(sfan);
5702 /* 570, 600e/x, 770e, 770x */
5703 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5705 /* all other ThinkPads: note that even old-style
5706 * ThinkPad ECs supports the fan control register */
5707 if (likely(acpi_ec_read(fan_status_offset,
5708 &fan_control_initial_status))) {
5709 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5711 /* In some ThinkPads, neither the EC nor the ACPI
5712 * DSDT initialize the fan status, and it ends up
5713 * being set to 0x07 when it *could* be either
5716 * Enable for TP-1Y (T43), TP-78 (R51e),
5717 * TP-76 (R52), TP-70 (T43, R52), which are known
5719 if (fan_control_initial_status == 0x07) {
5720 switch (thinkpad_id.ec_model) {
5721 case 0x5931: /* TP-1Y */
5722 case 0x3837: /* TP-78 */
5723 case 0x3637: /* TP-76 */
5724 case 0x3037: /* TP-70 */
5725 printk(TPACPI_NOTICE
5726 "fan_init: initial fan status "
5727 "is unknown, assuming it is "
5729 tp_features.fan_ctrl_status_undef = 1;
5735 "ThinkPad ACPI EC access misbehaving, "
5736 "fan status and control unavailable\n");
5743 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5744 fan_control_commands |=
5745 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5748 /* gfan without sfan means no fan control */
5749 /* all other models implement TP EC 0x2f control */
5753 fan_control_access_mode =
5754 TPACPI_FAN_WR_ACPI_FANS;
5755 fan_control_commands |=
5756 TPACPI_FAN_CMD_SPEED |
5757 TPACPI_FAN_CMD_LEVEL |
5758 TPACPI_FAN_CMD_ENABLE;
5760 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5761 fan_control_commands |=
5762 TPACPI_FAN_CMD_LEVEL |
5763 TPACPI_FAN_CMD_ENABLE;
5768 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5769 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5770 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5771 fan_status_access_mode, fan_control_access_mode);
5773 /* fan control master switch */
5774 if (!fan_control_allowed) {
5775 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5776 fan_control_commands = 0;
5777 dbg_printk(TPACPI_DBG_INIT,
5778 "fan control features disabled by parameter\n");
5781 /* update fan_control_desired_level */
5782 if (fan_status_access_mode != TPACPI_FAN_NONE)
5783 fan_get_status_safe(NULL);
5785 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5786 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5787 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5792 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5793 &driver_attr_fan_watchdog);
5795 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5804 static void fan_exit(void)
5806 vdbg_printk(TPACPI_DBG_EXIT,
5807 "cancelling any pending fan watchdog tasks\n");
5809 /* FIXME: can we really do this unconditionally? */
5810 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5811 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5812 &driver_attr_fan_watchdog);
5814 cancel_delayed_work(&fan_watchdog_task);
5815 flush_workqueue(tpacpi_wq);
5818 static int fan_read(char *p)
5823 unsigned int speed = 0;
5825 switch (fan_status_access_mode) {
5826 case TPACPI_FAN_RD_ACPI_GFAN:
5827 /* 570, 600e/x, 770e, 770x */
5828 rc = fan_get_status_safe(&status);
5832 len += sprintf(p + len, "status:\t\t%s\n"
5834 (status != 0) ? "enabled" : "disabled", status);
5837 case TPACPI_FAN_RD_TPEC:
5838 /* all except 570, 600e/x, 770e, 770x */
5839 rc = fan_get_status_safe(&status);
5843 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5844 if (status != fan_control_initial_status)
5845 tp_features.fan_ctrl_status_undef = 0;
5847 /* Return most likely status. In fact, it
5848 * might be the only possible status */
5849 status = TP_EC_FAN_AUTO;
5852 len += sprintf(p + len, "status:\t\t%s\n",
5853 (status != 0) ? "enabled" : "disabled");
5855 rc = fan_get_speed(&speed);
5859 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5861 if (status & TP_EC_FAN_FULLSPEED)
5862 /* Disengaged mode takes precedence */
5863 len += sprintf(p + len, "level:\t\tdisengaged\n");
5864 else if (status & TP_EC_FAN_AUTO)
5865 len += sprintf(p + len, "level:\t\tauto\n");
5867 len += sprintf(p + len, "level:\t\t%d\n", status);
5870 case TPACPI_FAN_NONE:
5872 len += sprintf(p + len, "status:\t\tnot supported\n");
5875 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5876 len += sprintf(p + len, "commands:\tlevel <level>");
5878 switch (fan_control_access_mode) {
5879 case TPACPI_FAN_WR_ACPI_SFAN:
5880 len += sprintf(p + len, " (<level> is 0-7)\n");
5884 len += sprintf(p + len, " (<level> is 0-7, "
5885 "auto, disengaged, full-speed)\n");
5890 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5891 len += sprintf(p + len, "commands:\tenable, disable\n"
5892 "commands:\twatchdog <timeout> (<timeout> "
5893 "is 0 (off), 1-120 (seconds))\n");
5895 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5896 len += sprintf(p + len, "commands:\tspeed <speed>"
5897 " (<speed> is 0-65535)\n");
5902 static int fan_write_cmd_level(const char *cmd, int *rc)
5906 if (strlencmp(cmd, "level auto") == 0)
5907 level = TP_EC_FAN_AUTO;
5908 else if ((strlencmp(cmd, "level disengaged") == 0) |
5909 (strlencmp(cmd, "level full-speed") == 0))
5910 level = TP_EC_FAN_FULLSPEED;
5911 else if (sscanf(cmd, "level %d", &level) != 1)
5914 *rc = fan_set_level_safe(level);
5916 printk(TPACPI_ERR "level command accepted for unsupported "
5917 "access mode %d", fan_control_access_mode);
5922 static int fan_write_cmd_enable(const char *cmd, int *rc)
5924 if (strlencmp(cmd, "enable") != 0)
5927 *rc = fan_set_enable();
5929 printk(TPACPI_ERR "enable command accepted for unsupported "
5930 "access mode %d", fan_control_access_mode);
5935 static int fan_write_cmd_disable(const char *cmd, int *rc)
5937 if (strlencmp(cmd, "disable") != 0)
5940 *rc = fan_set_disable();
5942 printk(TPACPI_ERR "disable command accepted for unsupported "
5943 "access mode %d", fan_control_access_mode);
5948 static int fan_write_cmd_speed(const char *cmd, int *rc)
5953 * Support speed <low> <medium> <high> ? */
5955 if (sscanf(cmd, "speed %d", &speed) != 1)
5958 *rc = fan_set_speed(speed);
5960 printk(TPACPI_ERR "speed command accepted for unsupported "
5961 "access mode %d", fan_control_access_mode);
5966 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5970 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5973 if (interval < 0 || interval > 120)
5976 fan_watchdog_maxinterval = interval;
5981 static int fan_write(char *buf)
5986 while (!rc && (cmd = next_cmd(&buf))) {
5987 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5988 fan_write_cmd_level(cmd, &rc)) &&
5989 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5990 (fan_write_cmd_enable(cmd, &rc) ||
5991 fan_write_cmd_disable(cmd, &rc) ||
5992 fan_write_cmd_watchdog(cmd, &rc))) &&
5993 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5994 fan_write_cmd_speed(cmd, &rc))
5998 fan_watchdog_reset();
6004 static struct ibm_struct fan_driver_data = {
6011 /****************************************************************************
6012 ****************************************************************************
6016 ****************************************************************************
6017 ****************************************************************************/
6019 /* sysfs name ---------------------------------------------------------- */
6020 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6021 struct device_attribute *attr,
6024 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6027 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6028 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6030 /* --------------------------------------------------------------------- */
6033 static struct proc_dir_entry *proc_dir;
6036 * Module and infrastructure proble, init and exit handling
6039 static int force_load;
6041 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6042 static const char * __init str_supported(int is_supported)
6044 static char text_unsupported[] __initdata = "not supported";
6046 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6048 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6050 static void ibm_exit(struct ibm_struct *ibm)
6052 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6054 list_del_init(&ibm->all_drivers);
6056 if (ibm->flags.acpi_notify_installed) {
6057 dbg_printk(TPACPI_DBG_EXIT,
6058 "%s: acpi_remove_notify_handler\n", ibm->name);
6060 acpi_remove_notify_handler(*ibm->acpi->handle,
6062 dispatch_acpi_notify);
6063 ibm->flags.acpi_notify_installed = 0;
6064 ibm->flags.acpi_notify_installed = 0;
6067 if (ibm->flags.proc_created) {
6068 dbg_printk(TPACPI_DBG_EXIT,
6069 "%s: remove_proc_entry\n", ibm->name);
6070 remove_proc_entry(ibm->name, proc_dir);
6071 ibm->flags.proc_created = 0;
6074 if (ibm->flags.acpi_driver_registered) {
6075 dbg_printk(TPACPI_DBG_EXIT,
6076 "%s: acpi_bus_unregister_driver\n", ibm->name);
6078 acpi_bus_unregister_driver(ibm->acpi->driver);
6079 kfree(ibm->acpi->driver);
6080 ibm->acpi->driver = NULL;
6081 ibm->flags.acpi_driver_registered = 0;
6084 if (ibm->flags.init_called && ibm->exit) {
6086 ibm->flags.init_called = 0;
6089 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6092 static int __init ibm_init(struct ibm_init_struct *iibm)
6095 struct ibm_struct *ibm = iibm->data;
6096 struct proc_dir_entry *entry;
6098 BUG_ON(ibm == NULL);
6100 INIT_LIST_HEAD(&ibm->all_drivers);
6102 if (ibm->flags.experimental && !experimental)
6105 dbg_printk(TPACPI_DBG_INIT,
6106 "probing for %s\n", ibm->name);
6109 ret = iibm->init(iibm);
6111 return 0; /* probe failed */
6115 ibm->flags.init_called = 1;
6119 if (ibm->acpi->hid) {
6120 ret = register_tpacpi_subdriver(ibm);
6125 if (ibm->acpi->notify) {
6126 ret = setup_acpi_notify(ibm);
6127 if (ret == -ENODEV) {
6128 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6138 dbg_printk(TPACPI_DBG_INIT,
6139 "%s installed\n", ibm->name);
6142 entry = create_proc_entry(ibm->name,
6143 S_IFREG | S_IRUGO | S_IWUSR,
6146 printk(TPACPI_ERR "unable to create proc entry %s\n",
6151 entry->owner = THIS_MODULE;
6153 entry->read_proc = &dispatch_procfs_read;
6155 entry->write_proc = &dispatch_procfs_write;
6156 ibm->flags.proc_created = 1;
6159 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6164 dbg_printk(TPACPI_DBG_INIT,
6165 "%s: at error exit path with result %d\n",
6169 return (ret < 0)? ret : 0;
6174 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
6176 const struct dmi_device *dev = NULL;
6177 char ec_fw_string[18];
6182 memset(tp, 0, sizeof(*tp));
6184 if (dmi_name_in_vendors("IBM"))
6185 tp->vendor = PCI_VENDOR_ID_IBM;
6186 else if (dmi_name_in_vendors("LENOVO"))
6187 tp->vendor = PCI_VENDOR_ID_LENOVO;
6191 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
6193 if (!tp->bios_version_str)
6195 tp->bios_model = tp->bios_version_str[0]
6196 | (tp->bios_version_str[1] << 8);
6199 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6200 * X32 or newer, all Z series; Some models must have an
6201 * up-to-date BIOS or they will not be detected.
6203 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6205 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6206 if (sscanf(dev->name,
6207 "IBM ThinkPad Embedded Controller -[%17c",
6208 ec_fw_string) == 1) {
6209 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6210 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6212 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6213 tp->ec_model = ec_fw_string[0]
6214 | (ec_fw_string[1] << 8);
6219 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
6221 if (tp->model_str && strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
6222 kfree(tp->model_str);
6223 tp->model_str = NULL;
6226 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
6230 static int __init probe_for_thinkpad(void)
6238 * Non-ancient models have better DMI tagging, but very old models
6241 is_thinkpad = (thinkpad_id.model_str != NULL);
6243 /* ec is required because many other handles are relative to it */
6244 TPACPI_ACPIHANDLE_INIT(ec);
6248 "Not yet supported ThinkPad detected!\n");
6253 * Risks a regression on very old machines, but reduces potential
6254 * false positives a damn great deal
6257 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6259 if (!is_thinkpad && !force_load)
6266 /* Module init, exit, parameters */
6268 static struct ibm_init_struct ibms_init[] __initdata = {
6270 .init = thinkpad_acpi_driver_init,
6271 .data = &thinkpad_acpi_driver_data,
6274 .init = hotkey_init,
6275 .data = &hotkey_driver_data,
6278 .init = bluetooth_init,
6279 .data = &bluetooth_driver_data,
6283 .data = &wan_driver_data,
6285 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6288 .data = &video_driver_data,
6293 .data = &light_driver_data,
6295 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6298 .data = &dock_driver_data[0],
6302 .data = &dock_driver_data[1],
6305 #ifdef CONFIG_THINKPAD_ACPI_BAY
6308 .data = &bay_driver_data,
6313 .data = &cmos_driver_data,
6317 .data = &led_driver_data,
6321 .data = &beep_driver_data,
6324 .init = thermal_init,
6325 .data = &thermal_driver_data,
6328 .data = &ecdump_driver_data,
6331 .init = brightness_init,
6332 .data = &brightness_driver_data,
6335 .data = &volume_driver_data,
6339 .data = &fan_driver_data,
6343 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6346 struct ibm_struct *ibm;
6348 if (!kp || !kp->name || !val)
6351 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6352 ibm = ibms_init[i].data;
6353 WARN_ON(ibm == NULL);
6355 if (!ibm || !ibm->name)
6358 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6359 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6361 strcpy(ibms_init[i].param, val);
6362 strcat(ibms_init[i].param, ",");
6370 module_param(experimental, int, 0);
6371 MODULE_PARM_DESC(experimental,
6372 "Enables experimental features when non-zero");
6374 module_param_named(debug, dbg_level, uint, 0);
6375 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6377 module_param(force_load, bool, 0);
6378 MODULE_PARM_DESC(force_load,
6379 "Attempts to load the driver even on a "
6380 "mis-identified ThinkPad when true");
6382 module_param_named(fan_control, fan_control_allowed, bool, 0);
6383 MODULE_PARM_DESC(fan_control,
6384 "Enables setting fan parameters features when true");
6386 module_param_named(brightness_mode, brightness_mode, int, 0);
6387 MODULE_PARM_DESC(brightness_mode,
6388 "Selects brightness control strategy: "
6389 "0=auto, 1=EC, 2=CMOS, 3=both");
6391 module_param(brightness_enable, uint, 0);
6392 MODULE_PARM_DESC(brightness_enable,
6393 "Enables backlight control when 1, disables when 0");
6395 module_param(hotkey_report_mode, uint, 0);
6396 MODULE_PARM_DESC(hotkey_report_mode,
6397 "used for backwards compatibility with userspace, "
6398 "see documentation");
6400 #define TPACPI_PARAM(feature) \
6401 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6402 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6403 "at module load, see documentation")
6405 TPACPI_PARAM(hotkey);
6406 TPACPI_PARAM(bluetooth);
6407 TPACPI_PARAM(video);
6408 TPACPI_PARAM(light);
6409 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6412 #ifdef CONFIG_THINKPAD_ACPI_BAY
6414 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6418 TPACPI_PARAM(ecdump);
6419 TPACPI_PARAM(brightness);
6420 TPACPI_PARAM(volume);
6423 static void thinkpad_acpi_module_exit(void)
6425 struct ibm_struct *ibm, *itmp;
6427 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6429 list_for_each_entry_safe_reverse(ibm, itmp,
6430 &tpacpi_all_drivers,
6435 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6437 if (tpacpi_inputdev) {
6438 if (tp_features.input_device_registered)
6439 input_unregister_device(tpacpi_inputdev);
6441 input_free_device(tpacpi_inputdev);
6445 hwmon_device_unregister(tpacpi_hwmon);
6447 if (tp_features.sensors_pdev_attrs_registered)
6448 device_remove_file(&tpacpi_sensors_pdev->dev,
6449 &dev_attr_thinkpad_acpi_pdev_name);
6450 if (tpacpi_sensors_pdev)
6451 platform_device_unregister(tpacpi_sensors_pdev);
6453 platform_device_unregister(tpacpi_pdev);
6455 if (tp_features.sensors_pdrv_attrs_registered)
6456 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6457 if (tp_features.platform_drv_attrs_registered)
6458 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6460 if (tp_features.sensors_pdrv_registered)
6461 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6463 if (tp_features.platform_drv_registered)
6464 platform_driver_unregister(&tpacpi_pdriver);
6467 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6470 destroy_workqueue(tpacpi_wq);
6472 kfree(thinkpad_id.bios_version_str);
6473 kfree(thinkpad_id.ec_version_str);
6474 kfree(thinkpad_id.model_str);
6478 static int __init thinkpad_acpi_module_init(void)
6482 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6484 /* Parameter checking */
6485 if (hotkey_report_mode > 2)
6488 /* Driver-level probe */
6490 get_thinkpad_model_data(&thinkpad_id);
6491 ret = probe_for_thinkpad();
6493 thinkpad_acpi_module_exit();
6497 /* Driver initialization */
6499 TPACPI_ACPIHANDLE_INIT(ecrd);
6500 TPACPI_ACPIHANDLE_INIT(ecwr);
6502 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6504 thinkpad_acpi_module_exit();
6508 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6511 "unable to create proc dir " TPACPI_PROC_DIR);
6512 thinkpad_acpi_module_exit();
6515 proc_dir->owner = THIS_MODULE;
6517 ret = platform_driver_register(&tpacpi_pdriver);
6520 "unable to register main platform driver\n");
6521 thinkpad_acpi_module_exit();
6524 tp_features.platform_drv_registered = 1;
6526 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6529 "unable to register hwmon platform driver\n");
6530 thinkpad_acpi_module_exit();
6533 tp_features.sensors_pdrv_registered = 1;
6535 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6537 tp_features.platform_drv_attrs_registered = 1;
6538 ret = tpacpi_create_driver_attributes(
6539 &tpacpi_hwmon_pdriver.driver);
6543 "unable to create sysfs driver attributes\n");
6544 thinkpad_acpi_module_exit();
6547 tp_features.sensors_pdrv_attrs_registered = 1;
6550 /* Device initialization */
6551 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6553 if (IS_ERR(tpacpi_pdev)) {
6554 ret = PTR_ERR(tpacpi_pdev);
6556 printk(TPACPI_ERR "unable to register platform device\n");
6557 thinkpad_acpi_module_exit();
6560 tpacpi_sensors_pdev = platform_device_register_simple(
6561 TPACPI_HWMON_DRVR_NAME,
6563 if (IS_ERR(tpacpi_sensors_pdev)) {
6564 ret = PTR_ERR(tpacpi_sensors_pdev);
6565 tpacpi_sensors_pdev = NULL;
6567 "unable to register hwmon platform device\n");
6568 thinkpad_acpi_module_exit();
6571 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6572 &dev_attr_thinkpad_acpi_pdev_name);
6575 "unable to create sysfs hwmon device attributes\n");
6576 thinkpad_acpi_module_exit();
6579 tp_features.sensors_pdev_attrs_registered = 1;
6580 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6581 if (IS_ERR(tpacpi_hwmon)) {
6582 ret = PTR_ERR(tpacpi_hwmon);
6583 tpacpi_hwmon = NULL;
6584 printk(TPACPI_ERR "unable to register hwmon device\n");
6585 thinkpad_acpi_module_exit();
6588 mutex_init(&tpacpi_inputdev_send_mutex);
6589 tpacpi_inputdev = input_allocate_device();
6590 if (!tpacpi_inputdev) {
6591 printk(TPACPI_ERR "unable to allocate input device\n");
6592 thinkpad_acpi_module_exit();
6595 /* Prepare input device, but don't register */
6596 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6597 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6598 tpacpi_inputdev->id.bustype = BUS_HOST;
6599 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6600 thinkpad_id.vendor :
6602 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6603 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6605 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6606 ret = ibm_init(&ibms_init[i]);
6607 if (ret >= 0 && *ibms_init[i].param)
6608 ret = ibms_init[i].data->write(ibms_init[i].param);
6610 thinkpad_acpi_module_exit();
6614 ret = input_register_device(tpacpi_inputdev);
6616 printk(TPACPI_ERR "unable to register input device\n");
6617 thinkpad_acpi_module_exit();
6620 tp_features.input_device_registered = 1;
6623 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6627 /* Please remove this in year 2009 */
6628 MODULE_ALIAS("ibm_acpi");
6630 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6633 * DMI matching for module autoloading
6635 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6636 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6638 * Only models listed in thinkwiki will be supported, so add yours
6639 * if it is not there yet.
6641 #define IBM_BIOS_MODULE_ALIAS(__type) \
6642 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6644 /* Non-ancient thinkpads */
6645 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6646 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6648 /* Ancient thinkpad BIOSes have to be identified by
6649 * BIOS type or model number, and there are far less
6650 * BIOS types than model numbers... */
6651 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6652 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6653 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6655 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6656 MODULE_DESCRIPTION(TPACPI_DESC);
6657 MODULE_VERSION(TPACPI_VERSION);
6658 MODULE_LICENSE("GPL");
6660 module_init(thinkpad_acpi_module_init);
6661 module_exit(thinkpad_acpi_module_exit);