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.21"
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 <linux/rfkill.h>
72 #include <asm/uaccess.h>
74 #include <linux/dmi.h>
75 #include <linux/jiffies.h>
76 #include <linux/workqueue.h>
78 #include <acpi/acpi_drivers.h>
79 #include <acpi/acnamesp.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
127 /****************************************************************************
131 #define TPACPI_NAME "thinkpad"
132 #define TPACPI_DESC "ThinkPad ACPI Extras"
133 #define TPACPI_FILE TPACPI_NAME "_acpi"
134 #define TPACPI_URL "http://ibm-acpi.sf.net/"
135 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
137 #define TPACPI_PROC_DIR "ibm"
138 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
139 #define TPACPI_DRVR_NAME TPACPI_FILE
140 #define TPACPI_DRVR_SHORTNAME "tpacpi"
141 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
143 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
144 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
146 #define TPACPI_MAX_ACPI_ARGS 3
148 /* rfkill switches */
150 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
151 TPACPI_RFK_WWAN_SW_ID,
155 #define TPACPI_LOG TPACPI_FILE ": "
156 #define TPACPI_ERR KERN_ERR TPACPI_LOG
157 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
158 #define TPACPI_INFO KERN_INFO TPACPI_LOG
159 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
161 #define TPACPI_DBG_ALL 0xffff
162 #define TPACPI_DBG_ALL 0xffff
163 #define TPACPI_DBG_INIT 0x0001
164 #define TPACPI_DBG_EXIT 0x0002
165 #define dbg_printk(a_dbg_level, format, arg...) \
166 do { if (dbg_level & a_dbg_level) \
167 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
169 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
170 #define vdbg_printk(a_dbg_level, format, arg...) \
171 dbg_printk(a_dbg_level, format, ## arg)
172 static const char *str_supported(int is_supported);
174 #define vdbg_printk(a_dbg_level, format, arg...)
177 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
178 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
179 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
182 /****************************************************************************
183 * Driver-wide structs and misc. variables
188 struct tp_acpi_drv_struct {
189 const struct acpi_device_id *hid;
190 struct acpi_driver *driver;
192 void (*notify) (struct ibm_struct *, u32);
195 struct acpi_device *device;
201 int (*read) (char *);
202 int (*write) (char *);
204 void (*resume) (void);
205 void (*suspend) (pm_message_t state);
207 struct list_head all_drivers;
209 struct tp_acpi_drv_struct *acpi;
212 u8 acpi_driver_registered:1;
213 u8 acpi_notify_installed:1;
220 struct ibm_init_struct {
223 int (*init) (struct ibm_init_struct *);
224 struct ibm_struct *data;
228 #ifdef CONFIG_THINKPAD_ACPI_BAY
241 u32 bright_16levels:1;
242 u32 bright_acpimode:1;
244 u32 fan_ctrl_status_undef:1;
245 u32 input_device_registered:1;
246 u32 platform_drv_registered:1;
247 u32 platform_drv_attrs_registered:1;
248 u32 sensors_pdrv_registered:1;
249 u32 sensors_pdrv_attrs_registered:1;
250 u32 sensors_pdev_attrs_registered:1;
251 u32 hotkey_poll_active:1;
255 u16 hotkey_mask_ff:1;
256 u16 bright_cmos_ec_unsync:1;
259 struct thinkpad_id_data {
260 unsigned int vendor; /* ThinkPad vendor:
261 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
263 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
264 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
266 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
269 char *model_str; /* ThinkPad T43 */
270 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
272 static struct thinkpad_id_data thinkpad_id;
275 TPACPI_LIFE_INIT = 0,
280 static int experimental;
281 static u32 dbg_level;
283 static struct workqueue_struct *tpacpi_wq;
285 /* Special LED class that can defer work */
286 struct tpacpi_led_classdev {
287 struct led_classdev led_classdev;
288 struct work_struct work;
289 enum led_brightness new_brightness;
293 /****************************************************************************
294 ****************************************************************************
296 * ACPI Helpers and device model
298 ****************************************************************************
299 ****************************************************************************/
301 /*************************************************************************
305 static acpi_handle root_handle;
307 #define TPACPI_HANDLE(object, parent, paths...) \
308 static acpi_handle object##_handle; \
309 static acpi_handle *object##_parent = &parent##_handle; \
310 static char *object##_path; \
311 static char *object##_paths[] = { paths }
313 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
314 "\\_SB.PCI.ISA.EC", /* 570 */
315 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
316 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
317 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
318 "\\_SB.PCI0.ICH3.EC0", /* R31 */
319 "\\_SB.PCI0.LPC.EC", /* all others */
322 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
323 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
325 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
327 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
328 "\\CMS", /* R40, R40e */
331 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
332 "^HKEY", /* R30, R31 */
333 "HKEY", /* all others */
336 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
337 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
338 "\\_SB.PCI0.VID0", /* 770e */
339 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
340 "\\_SB.PCI0.AGP.VID", /* all others */
344 /*************************************************************************
348 static int acpi_evalf(acpi_handle handle,
349 void *res, char *method, char *fmt, ...)
352 struct acpi_object_list params;
353 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
354 struct acpi_buffer result, *resultp;
355 union acpi_object out_obj;
363 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
376 params.pointer = &in_objs[0];
383 in_objs[params.count].integer.value = va_arg(ap, int);
384 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
386 /* add more types as needed */
388 printk(TPACPI_ERR "acpi_evalf() called "
389 "with invalid format character '%c'\n", c);
395 if (res_type != 'v') {
396 result.length = sizeof(out_obj);
397 result.pointer = &out_obj;
402 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
407 *(int *)res = out_obj.integer.value;
408 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
411 success = status == AE_OK;
413 /* add more types as needed */
415 printk(TPACPI_ERR "acpi_evalf() called "
416 "with invalid format character '%c'\n", res_type);
420 if (!success && !quiet)
421 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
422 method, fmt0, status);
427 static int acpi_ec_read(int i, u8 *p)
432 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
436 if (ec_read(i, p) < 0)
443 static int acpi_ec_write(int i, u8 v)
446 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
449 if (ec_write(i, v) < 0)
456 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
457 static int _sta(acpi_handle handle)
461 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
468 static int issue_thinkpad_cmos_command(int cmos_cmd)
473 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
479 /*************************************************************************
483 #define TPACPI_ACPIHANDLE_INIT(object) \
484 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
485 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
487 static void drv_acpi_handle_init(char *name,
488 acpi_handle *handle, acpi_handle parent,
489 char **paths, int num_paths, char **path)
494 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
497 for (i = 0; i < num_paths; i++) {
498 status = acpi_get_handle(parent, paths[i], handle);
499 if (ACPI_SUCCESS(status)) {
501 dbg_printk(TPACPI_DBG_INIT,
502 "Found ACPI handle %s for %s\n",
508 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
513 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
515 struct ibm_struct *ibm = data;
517 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
520 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
523 ibm->acpi->notify(ibm, event);
526 static int __init setup_acpi_notify(struct ibm_struct *ibm)
533 if (!*ibm->acpi->handle)
536 vdbg_printk(TPACPI_DBG_INIT,
537 "setting up ACPI notify for %s\n", ibm->name);
539 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
541 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
546 acpi_driver_data(ibm->acpi->device) = ibm;
547 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
548 TPACPI_ACPI_EVENT_PREFIX,
551 status = acpi_install_notify_handler(*ibm->acpi->handle,
552 ibm->acpi->type, dispatch_acpi_notify, ibm);
553 if (ACPI_FAILURE(status)) {
554 if (status == AE_ALREADY_EXISTS) {
556 "another device driver is already "
557 "handling %s events\n", ibm->name);
560 "acpi_install_notify_handler(%s) failed: %d\n",
565 ibm->flags.acpi_notify_installed = 1;
569 static int __init tpacpi_device_add(struct acpi_device *device)
574 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
578 dbg_printk(TPACPI_DBG_INIT,
579 "registering %s as an ACPI driver\n", ibm->name);
583 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
584 if (!ibm->acpi->driver) {
586 "failed to allocate memory for ibm->acpi->driver\n");
590 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
591 ibm->acpi->driver->ids = ibm->acpi->hid;
593 ibm->acpi->driver->ops.add = &tpacpi_device_add;
595 rc = acpi_bus_register_driver(ibm->acpi->driver);
597 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
599 kfree(ibm->acpi->driver);
600 ibm->acpi->driver = NULL;
602 ibm->flags.acpi_driver_registered = 1;
608 /****************************************************************************
609 ****************************************************************************
613 ****************************************************************************
614 ****************************************************************************/
616 static int dispatch_procfs_read(char *page, char **start, off_t off,
617 int count, int *eof, void *data)
619 struct ibm_struct *ibm = data;
622 if (!ibm || !ibm->read)
625 len = ibm->read(page);
629 if (len <= off + count)
641 static int dispatch_procfs_write(struct file *file,
642 const char __user *userbuf,
643 unsigned long count, void *data)
645 struct ibm_struct *ibm = data;
649 if (!ibm || !ibm->write)
652 kernbuf = kmalloc(count + 2, GFP_KERNEL);
656 if (copy_from_user(kernbuf, userbuf, count)) {
662 strcat(kernbuf, ",");
663 ret = ibm->write(kernbuf);
672 static char *next_cmd(char **cmds)
677 while ((end = strchr(start, ',')) && end == start)
689 /****************************************************************************
690 ****************************************************************************
692 * Device model: input, hwmon and platform
694 ****************************************************************************
695 ****************************************************************************/
697 static struct platform_device *tpacpi_pdev;
698 static struct platform_device *tpacpi_sensors_pdev;
699 static struct device *tpacpi_hwmon;
700 static struct input_dev *tpacpi_inputdev;
701 static struct mutex tpacpi_inputdev_send_mutex;
702 static LIST_HEAD(tpacpi_all_drivers);
704 static int tpacpi_suspend_handler(struct platform_device *pdev,
707 struct ibm_struct *ibm, *itmp;
709 list_for_each_entry_safe(ibm, itmp,
713 (ibm->suspend)(state);
719 static int tpacpi_resume_handler(struct platform_device *pdev)
721 struct ibm_struct *ibm, *itmp;
723 list_for_each_entry_safe(ibm, itmp,
733 static struct platform_driver tpacpi_pdriver = {
735 .name = TPACPI_DRVR_NAME,
736 .owner = THIS_MODULE,
738 .suspend = tpacpi_suspend_handler,
739 .resume = tpacpi_resume_handler,
742 static struct platform_driver tpacpi_hwmon_pdriver = {
744 .name = TPACPI_HWMON_DRVR_NAME,
745 .owner = THIS_MODULE,
749 /*************************************************************************
750 * sysfs support helpers
753 struct attribute_set {
754 unsigned int members, max_members;
755 struct attribute_group group;
758 struct attribute_set_obj {
759 struct attribute_set s;
761 } __attribute__((packed));
763 static struct attribute_set *create_attr_set(unsigned int max_members,
766 struct attribute_set_obj *sobj;
768 if (max_members == 0)
771 /* Allocates space for implicit NULL at the end too */
772 sobj = kzalloc(sizeof(struct attribute_set_obj) +
773 max_members * sizeof(struct attribute *),
777 sobj->s.max_members = max_members;
778 sobj->s.group.attrs = &sobj->a;
779 sobj->s.group.name = name;
784 #define destroy_attr_set(_set) \
787 /* not multi-threaded safe, use it in a single thread per set */
788 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
793 if (s->members >= s->max_members)
796 s->group.attrs[s->members] = attr;
802 static int add_many_to_attr_set(struct attribute_set *s,
803 struct attribute **attr,
808 for (i = 0; i < count; i++) {
809 res = add_to_attr_set(s, attr[i]);
817 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
819 sysfs_remove_group(kobj, &s->group);
823 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
824 sysfs_create_group(_kobj, &_attr_set->group)
826 static int parse_strtoul(const char *buf,
827 unsigned long max, unsigned long *value)
831 while (*buf && isspace(*buf))
833 *value = simple_strtoul(buf, &endp, 0);
834 while (*endp && isspace(*endp))
836 if (*endp || *value > max)
842 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
844 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
845 union acpi_object *obj;
848 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
849 obj = (union acpi_object *)buffer.pointer;
850 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
851 printk(TPACPI_ERR "Unknown _BCL data, "
852 "please report this to %s\n", TPACPI_MAIL);
855 rc = obj->package.count;
861 kfree(buffer.pointer);
865 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
866 u32 lvl, void *context, void **rv)
868 char name[ACPI_PATH_SEGMENT_LENGTH];
869 struct acpi_buffer buffer = { sizeof(name), &name };
871 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
872 !strncmp("_BCL", name, sizeof(name) - 1)) {
874 **(int **)rv = tpacpi_query_bcl_levels(handle);
875 return AE_CTRL_TERMINATE;
882 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
884 static int __init tpacpi_check_std_acpi_brightness_support(void)
888 void *bcl_ptr = &bcl_levels;
891 TPACPI_ACPIHANDLE_INIT(vid);
897 * Search for a _BCL method, and execute it. This is safe on all
898 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
899 * BIOS in ACPI backlight control mode. We do NOT have to care
900 * about calling the _BCL method in an enabled video device, any
901 * will do for our purposes.
904 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
905 tpacpi_acpi_walk_find_bcl, NULL,
908 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
909 tp_features.bright_acpimode = 1;
910 return (bcl_levels - 2);
916 static int __init tpacpi_new_rfkill(const unsigned int id,
918 const enum rfkill_type rfktype,
920 int (*toggle_radio)(void *, enum rfkill_state),
921 int (*get_state)(void *, enum rfkill_state *))
924 enum rfkill_state initial_state;
926 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
929 "failed to allocate memory for rfkill class\n");
934 (*rfk)->get_state = get_state;
935 (*rfk)->toggle_radio = toggle_radio;
937 if (!get_state(NULL, &initial_state))
938 (*rfk)->state = initial_state;
940 res = rfkill_register(*rfk);
943 "failed to register %s rfkill switch: %d\n",
953 /*************************************************************************
954 * thinkpad-acpi driver attributes
957 /* interface_version --------------------------------------------------- */
958 static ssize_t tpacpi_driver_interface_version_show(
959 struct device_driver *drv,
962 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
965 static DRIVER_ATTR(interface_version, S_IRUGO,
966 tpacpi_driver_interface_version_show, NULL);
968 /* debug_level --------------------------------------------------------- */
969 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
972 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
975 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
976 const char *buf, size_t count)
980 if (parse_strtoul(buf, 0xffff, &t))
988 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
989 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
991 /* version ------------------------------------------------------------- */
992 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
995 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
996 TPACPI_DESC, TPACPI_VERSION);
999 static DRIVER_ATTR(version, S_IRUGO,
1000 tpacpi_driver_version_show, NULL);
1002 /* --------------------------------------------------------------------- */
1004 static struct driver_attribute *tpacpi_driver_attributes[] = {
1005 &driver_attr_debug_level, &driver_attr_version,
1006 &driver_attr_interface_version,
1009 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1015 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1016 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1023 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1027 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1028 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1031 /****************************************************************************
1032 ****************************************************************************
1036 ****************************************************************************
1037 ****************************************************************************/
1039 /*************************************************************************
1040 * thinkpad-acpi init subdriver
1043 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1045 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1046 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1048 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1049 (thinkpad_id.bios_version_str) ?
1050 thinkpad_id.bios_version_str : "unknown",
1051 (thinkpad_id.ec_version_str) ?
1052 thinkpad_id.ec_version_str : "unknown");
1054 if (thinkpad_id.vendor && thinkpad_id.model_str)
1055 printk(TPACPI_INFO "%s %s, model %s\n",
1056 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1057 "IBM" : ((thinkpad_id.vendor ==
1058 PCI_VENDOR_ID_LENOVO) ?
1059 "Lenovo" : "Unknown vendor"),
1060 thinkpad_id.model_str,
1061 (thinkpad_id.nummodel_str) ?
1062 thinkpad_id.nummodel_str : "unknown");
1067 static int thinkpad_acpi_driver_read(char *p)
1071 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1072 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1077 static struct ibm_struct thinkpad_acpi_driver_data = {
1079 .read = thinkpad_acpi_driver_read,
1082 /*************************************************************************
1086 enum { /* hot key scan codes (derived from ACPI DSDT) */
1087 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1088 TP_ACPI_HOTKEYSCAN_FNF2,
1089 TP_ACPI_HOTKEYSCAN_FNF3,
1090 TP_ACPI_HOTKEYSCAN_FNF4,
1091 TP_ACPI_HOTKEYSCAN_FNF5,
1092 TP_ACPI_HOTKEYSCAN_FNF6,
1093 TP_ACPI_HOTKEYSCAN_FNF7,
1094 TP_ACPI_HOTKEYSCAN_FNF8,
1095 TP_ACPI_HOTKEYSCAN_FNF9,
1096 TP_ACPI_HOTKEYSCAN_FNF10,
1097 TP_ACPI_HOTKEYSCAN_FNF11,
1098 TP_ACPI_HOTKEYSCAN_FNF12,
1099 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1100 TP_ACPI_HOTKEYSCAN_FNINSERT,
1101 TP_ACPI_HOTKEYSCAN_FNDELETE,
1102 TP_ACPI_HOTKEYSCAN_FNHOME,
1103 TP_ACPI_HOTKEYSCAN_FNEND,
1104 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1105 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1106 TP_ACPI_HOTKEYSCAN_FNSPACE,
1107 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1108 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1109 TP_ACPI_HOTKEYSCAN_MUTE,
1110 TP_ACPI_HOTKEYSCAN_THINKPAD,
1113 enum { /* Keys available through NVRAM polling */
1114 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1115 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1118 enum { /* Positions of some of the keys in hotkey masks */
1119 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1120 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1121 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1122 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1123 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1124 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1125 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1126 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1127 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1128 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1129 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1132 enum { /* NVRAM to ACPI HKEY group map */
1133 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1134 TP_ACPI_HKEY_ZOOM_MASK |
1135 TP_ACPI_HKEY_DISPSWTCH_MASK |
1136 TP_ACPI_HKEY_HIBERNATE_MASK,
1137 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1138 TP_ACPI_HKEY_BRGHTDWN_MASK,
1139 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1140 TP_ACPI_HKEY_VOLDWN_MASK |
1141 TP_ACPI_HKEY_MUTE_MASK,
1144 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1145 struct tp_nvram_state {
1146 u16 thinkpad_toggle:1;
1148 u16 display_toggle:1;
1149 u16 thinklight_toggle:1;
1150 u16 hibernate_toggle:1;
1151 u16 displayexp_toggle:1;
1152 u16 display_state:1;
1153 u16 brightness_toggle:1;
1154 u16 volume_toggle:1;
1157 u8 brightness_level;
1161 static struct task_struct *tpacpi_hotkey_task;
1162 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1163 static int hotkey_poll_freq = 10; /* Hz */
1164 static struct mutex hotkey_thread_mutex;
1165 static struct mutex hotkey_thread_data_mutex;
1166 static unsigned int hotkey_config_change;
1168 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1170 #define hotkey_source_mask 0U
1172 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1174 static struct mutex hotkey_mutex;
1176 static enum { /* Reasons for waking up */
1177 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1178 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1179 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1180 } hotkey_wakeup_reason;
1182 static int hotkey_autosleep_ack;
1184 static int hotkey_orig_status;
1185 static u32 hotkey_orig_mask;
1186 static u32 hotkey_all_mask;
1187 static u32 hotkey_reserved_mask;
1188 static u32 hotkey_mask;
1190 static unsigned int hotkey_report_mode;
1192 static u16 *hotkey_keycode_map;
1194 static struct attribute_set *hotkey_dev_attributes;
1196 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1197 #define HOTKEY_CONFIG_CRITICAL_START \
1199 mutex_lock(&hotkey_thread_data_mutex); \
1200 hotkey_config_change++; \
1202 #define HOTKEY_CONFIG_CRITICAL_END \
1203 mutex_unlock(&hotkey_thread_data_mutex);
1205 #define HOTKEY_CONFIG_CRITICAL_START
1206 #define HOTKEY_CONFIG_CRITICAL_END
1207 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1209 /* HKEY.MHKG() return bits */
1210 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1212 static int hotkey_get_wlsw(int *status)
1214 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1219 static int hotkey_get_tablet_mode(int *status)
1223 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1226 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1231 * Call with hotkey_mutex held
1233 static int hotkey_mask_get(void)
1237 if (tp_features.hotkey_mask) {
1238 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1241 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1247 * Call with hotkey_mutex held
1249 static int hotkey_mask_set(u32 mask)
1254 if (tp_features.hotkey_mask) {
1255 if (!tp_warned.hotkey_mask_ff &&
1256 (mask == 0xffff || mask == 0xffffff ||
1257 mask == 0xffffffff)) {
1258 tp_warned.hotkey_mask_ff = 1;
1259 printk(TPACPI_NOTICE
1260 "setting the hotkey mask to 0x%08x is likely "
1261 "not the best way to go about it\n", mask);
1262 printk(TPACPI_NOTICE
1263 "please consider using the driver defaults, "
1264 "and refer to up-to-date thinkpad-acpi "
1268 HOTKEY_CONFIG_CRITICAL_START
1269 for (i = 0; i < 32; i++) {
1271 /* enable in firmware mask only keys not in NVRAM
1272 * mode, but enable the key in the cached hotkey_mask
1273 * regardless of mode, or the key will end up
1274 * disabled by hotkey_mask_get() */
1275 if (!acpi_evalf(hkey_handle,
1276 NULL, "MHKM", "vdd", i + 1,
1277 !!((mask & ~hotkey_source_mask) & m))) {
1281 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1284 HOTKEY_CONFIG_CRITICAL_END
1286 /* hotkey_mask_get must be called unconditionally below */
1287 if (!hotkey_mask_get() && !rc &&
1288 (hotkey_mask & ~hotkey_source_mask) !=
1289 (mask & ~hotkey_source_mask)) {
1290 printk(TPACPI_NOTICE
1291 "requested hot key mask 0x%08x, but "
1292 "firmware forced it to 0x%08x\n",
1296 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1297 HOTKEY_CONFIG_CRITICAL_START
1298 hotkey_mask = mask & hotkey_source_mask;
1299 HOTKEY_CONFIG_CRITICAL_END
1301 if (hotkey_mask != mask) {
1302 printk(TPACPI_NOTICE
1303 "requested hot key mask 0x%08x, "
1304 "forced to 0x%08x (NVRAM poll mask is "
1305 "0x%08x): no firmware mask support\n",
1306 mask, hotkey_mask, hotkey_source_mask);
1311 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1317 static int hotkey_status_get(int *status)
1319 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1325 static int hotkey_status_set(int status)
1327 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1333 static void tpacpi_input_send_tabletsw(void)
1337 if (tp_features.hotkey_tablet &&
1338 !hotkey_get_tablet_mode(&state)) {
1339 mutex_lock(&tpacpi_inputdev_send_mutex);
1341 input_report_switch(tpacpi_inputdev,
1342 SW_TABLET_MODE, !!state);
1343 input_sync(tpacpi_inputdev);
1345 mutex_unlock(&tpacpi_inputdev_send_mutex);
1349 static void tpacpi_input_send_key(unsigned int scancode)
1351 unsigned int keycode;
1353 keycode = hotkey_keycode_map[scancode];
1355 if (keycode != KEY_RESERVED) {
1356 mutex_lock(&tpacpi_inputdev_send_mutex);
1358 input_report_key(tpacpi_inputdev, keycode, 1);
1359 if (keycode == KEY_UNKNOWN)
1360 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1362 input_sync(tpacpi_inputdev);
1364 input_report_key(tpacpi_inputdev, keycode, 0);
1365 if (keycode == KEY_UNKNOWN)
1366 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1368 input_sync(tpacpi_inputdev);
1370 mutex_unlock(&tpacpi_inputdev_send_mutex);
1374 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1375 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1377 static void tpacpi_hotkey_send_key(unsigned int scancode)
1379 tpacpi_input_send_key(scancode);
1380 if (hotkey_report_mode < 2) {
1381 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1382 0x80, 0x1001 + scancode);
1386 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1390 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1391 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1392 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1393 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1394 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1395 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1397 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1398 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1399 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1401 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1402 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1403 n->displayexp_toggle =
1404 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1406 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1407 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1408 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1409 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1410 n->brightness_toggle =
1411 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1413 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1414 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1415 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1416 >> TP_NVRAM_POS_LEVEL_VOLUME;
1417 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1418 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1422 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1424 if ((mask & (1 << __scancode)) && \
1425 oldn->__member != newn->__member) \
1426 tpacpi_hotkey_send_key(__scancode); \
1429 #define TPACPI_MAY_SEND_KEY(__scancode) \
1430 do { if (mask & (1 << __scancode)) \
1431 tpacpi_hotkey_send_key(__scancode); } while (0)
1433 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1434 struct tp_nvram_state *newn,
1437 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1438 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1439 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1440 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1442 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1444 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1447 if (oldn->volume_toggle != newn->volume_toggle) {
1448 if (oldn->mute != newn->mute) {
1449 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1451 if (oldn->volume_level > newn->volume_level) {
1452 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1453 } else if (oldn->volume_level < newn->volume_level) {
1454 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1455 } else if (oldn->mute == newn->mute) {
1456 /* repeated key presses that didn't change state */
1458 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1459 } else if (newn->volume_level != 0) {
1460 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1462 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1467 /* handle brightness */
1468 if (oldn->brightness_toggle != newn->brightness_toggle) {
1469 if (oldn->brightness_level < newn->brightness_level) {
1470 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1471 } else if (oldn->brightness_level > newn->brightness_level) {
1472 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1474 /* repeated key presses that didn't change state */
1475 if (newn->brightness_level != 0) {
1476 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1478 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1484 #undef TPACPI_COMPARE_KEY
1485 #undef TPACPI_MAY_SEND_KEY
1487 static int hotkey_kthread(void *data)
1489 struct tp_nvram_state s[2];
1491 unsigned int si, so;
1493 unsigned int change_detector, must_reset;
1495 mutex_lock(&hotkey_thread_mutex);
1497 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1506 /* Initial state for compares */
1507 mutex_lock(&hotkey_thread_data_mutex);
1508 change_detector = hotkey_config_change;
1509 mask = hotkey_source_mask & hotkey_mask;
1510 mutex_unlock(&hotkey_thread_data_mutex);
1511 hotkey_read_nvram(&s[so], mask);
1513 while (!kthread_should_stop() && hotkey_poll_freq) {
1515 t = 1000/hotkey_poll_freq;
1516 t = msleep_interruptible(t);
1517 if (unlikely(kthread_should_stop()))
1519 must_reset = try_to_freeze();
1520 if (t > 0 && !must_reset)
1523 mutex_lock(&hotkey_thread_data_mutex);
1524 if (must_reset || hotkey_config_change != change_detector) {
1525 /* forget old state on thaw or config change */
1528 change_detector = hotkey_config_change;
1530 mask = hotkey_source_mask & hotkey_mask;
1531 mutex_unlock(&hotkey_thread_data_mutex);
1534 hotkey_read_nvram(&s[si], mask);
1535 if (likely(si != so)) {
1536 hotkey_compare_and_issue_event(&s[so], &s[si],
1546 mutex_unlock(&hotkey_thread_mutex);
1550 static void hotkey_poll_stop_sync(void)
1552 if (tpacpi_hotkey_task) {
1553 if (frozen(tpacpi_hotkey_task) ||
1554 freezing(tpacpi_hotkey_task))
1555 thaw_process(tpacpi_hotkey_task);
1557 kthread_stop(tpacpi_hotkey_task);
1558 tpacpi_hotkey_task = NULL;
1559 mutex_lock(&hotkey_thread_mutex);
1560 /* at this point, the thread did exit */
1561 mutex_unlock(&hotkey_thread_mutex);
1565 /* call with hotkey_mutex held */
1566 static void hotkey_poll_setup(int may_warn)
1568 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1569 hotkey_poll_freq > 0 &&
1570 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1571 if (!tpacpi_hotkey_task) {
1572 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1573 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1574 if (IS_ERR(tpacpi_hotkey_task)) {
1575 tpacpi_hotkey_task = NULL;
1577 "could not create kernel thread "
1578 "for hotkey polling\n");
1582 hotkey_poll_stop_sync();
1584 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1585 printk(TPACPI_NOTICE
1586 "hot keys 0x%08x require polling, "
1587 "which is currently disabled\n",
1588 hotkey_source_mask);
1593 static void hotkey_poll_setup_safe(int may_warn)
1595 mutex_lock(&hotkey_mutex);
1596 hotkey_poll_setup(may_warn);
1597 mutex_unlock(&hotkey_mutex);
1600 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1602 static void hotkey_poll_setup_safe(int __unused)
1606 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1608 static int hotkey_inputdev_open(struct input_dev *dev)
1610 switch (tpacpi_lifecycle) {
1611 case TPACPI_LIFE_INIT:
1613 * hotkey_init will call hotkey_poll_setup_safe
1614 * at the appropriate moment
1617 case TPACPI_LIFE_EXITING:
1619 case TPACPI_LIFE_RUNNING:
1620 hotkey_poll_setup_safe(0);
1624 /* Should only happen if tpacpi_lifecycle is corrupt */
1629 static void hotkey_inputdev_close(struct input_dev *dev)
1631 /* disable hotkey polling when possible */
1632 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1633 hotkey_poll_setup_safe(0);
1636 /* sysfs hotkey enable ------------------------------------------------- */
1637 static ssize_t hotkey_enable_show(struct device *dev,
1638 struct device_attribute *attr,
1643 res = hotkey_status_get(&status);
1647 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1650 static ssize_t hotkey_enable_store(struct device *dev,
1651 struct device_attribute *attr,
1652 const char *buf, size_t count)
1657 if (parse_strtoul(buf, 1, &t))
1660 res = hotkey_status_set(t);
1662 return (res) ? res : count;
1665 static struct device_attribute dev_attr_hotkey_enable =
1666 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1667 hotkey_enable_show, hotkey_enable_store);
1669 /* sysfs hotkey mask --------------------------------------------------- */
1670 static ssize_t hotkey_mask_show(struct device *dev,
1671 struct device_attribute *attr,
1676 if (mutex_lock_interruptible(&hotkey_mutex))
1677 return -ERESTARTSYS;
1678 res = hotkey_mask_get();
1679 mutex_unlock(&hotkey_mutex);
1682 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1685 static ssize_t hotkey_mask_store(struct device *dev,
1686 struct device_attribute *attr,
1687 const char *buf, size_t count)
1692 if (parse_strtoul(buf, 0xffffffffUL, &t))
1695 if (mutex_lock_interruptible(&hotkey_mutex))
1696 return -ERESTARTSYS;
1698 res = hotkey_mask_set(t);
1700 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1701 hotkey_poll_setup(1);
1704 mutex_unlock(&hotkey_mutex);
1706 return (res) ? res : count;
1709 static struct device_attribute dev_attr_hotkey_mask =
1710 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1711 hotkey_mask_show, hotkey_mask_store);
1713 /* sysfs hotkey bios_enabled ------------------------------------------- */
1714 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1715 struct device_attribute *attr,
1718 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1721 static struct device_attribute dev_attr_hotkey_bios_enabled =
1722 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1724 /* sysfs hotkey bios_mask ---------------------------------------------- */
1725 static ssize_t hotkey_bios_mask_show(struct device *dev,
1726 struct device_attribute *attr,
1729 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1732 static struct device_attribute dev_attr_hotkey_bios_mask =
1733 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1735 /* sysfs hotkey all_mask ----------------------------------------------- */
1736 static ssize_t hotkey_all_mask_show(struct device *dev,
1737 struct device_attribute *attr,
1740 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1741 hotkey_all_mask | hotkey_source_mask);
1744 static struct device_attribute dev_attr_hotkey_all_mask =
1745 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1747 /* sysfs hotkey recommended_mask --------------------------------------- */
1748 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1749 struct device_attribute *attr,
1752 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1753 (hotkey_all_mask | hotkey_source_mask)
1754 & ~hotkey_reserved_mask);
1757 static struct device_attribute dev_attr_hotkey_recommended_mask =
1758 __ATTR(hotkey_recommended_mask, S_IRUGO,
1759 hotkey_recommended_mask_show, NULL);
1761 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1763 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1764 static ssize_t hotkey_source_mask_show(struct device *dev,
1765 struct device_attribute *attr,
1768 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1771 static ssize_t hotkey_source_mask_store(struct device *dev,
1772 struct device_attribute *attr,
1773 const char *buf, size_t count)
1777 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1778 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1781 if (mutex_lock_interruptible(&hotkey_mutex))
1782 return -ERESTARTSYS;
1784 HOTKEY_CONFIG_CRITICAL_START
1785 hotkey_source_mask = t;
1786 HOTKEY_CONFIG_CRITICAL_END
1788 hotkey_poll_setup(1);
1790 mutex_unlock(&hotkey_mutex);
1795 static struct device_attribute dev_attr_hotkey_source_mask =
1796 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1797 hotkey_source_mask_show, hotkey_source_mask_store);
1799 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1800 static ssize_t hotkey_poll_freq_show(struct device *dev,
1801 struct device_attribute *attr,
1804 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1807 static ssize_t hotkey_poll_freq_store(struct device *dev,
1808 struct device_attribute *attr,
1809 const char *buf, size_t count)
1813 if (parse_strtoul(buf, 25, &t))
1816 if (mutex_lock_interruptible(&hotkey_mutex))
1817 return -ERESTARTSYS;
1819 hotkey_poll_freq = t;
1821 hotkey_poll_setup(1);
1822 mutex_unlock(&hotkey_mutex);
1827 static struct device_attribute dev_attr_hotkey_poll_freq =
1828 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1829 hotkey_poll_freq_show, hotkey_poll_freq_store);
1831 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1833 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1834 static ssize_t hotkey_radio_sw_show(struct device *dev,
1835 struct device_attribute *attr,
1839 res = hotkey_get_wlsw(&s);
1843 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1846 static struct device_attribute dev_attr_hotkey_radio_sw =
1847 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1849 static void hotkey_radio_sw_notify_change(void)
1851 if (tp_features.hotkey_wlsw)
1852 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1856 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1857 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1858 struct device_attribute *attr,
1862 res = hotkey_get_tablet_mode(&s);
1866 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1869 static struct device_attribute dev_attr_hotkey_tablet_mode =
1870 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1872 static void hotkey_tablet_mode_notify_change(void)
1874 if (tp_features.hotkey_tablet)
1875 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1876 "hotkey_tablet_mode");
1879 /* sysfs hotkey report_mode -------------------------------------------- */
1880 static ssize_t hotkey_report_mode_show(struct device *dev,
1881 struct device_attribute *attr,
1884 return snprintf(buf, PAGE_SIZE, "%d\n",
1885 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1888 static struct device_attribute dev_attr_hotkey_report_mode =
1889 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1891 /* sysfs wakeup reason (pollable) -------------------------------------- */
1892 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1893 struct device_attribute *attr,
1896 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1899 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1900 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1902 static void hotkey_wakeup_reason_notify_change(void)
1904 if (tp_features.hotkey_mask)
1905 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1909 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1910 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1911 struct device_attribute *attr,
1914 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1917 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1918 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1919 hotkey_wakeup_hotunplug_complete_show, NULL);
1921 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1923 if (tp_features.hotkey_mask)
1924 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1925 "wakeup_hotunplug_complete");
1928 /* --------------------------------------------------------------------- */
1930 static struct attribute *hotkey_attributes[] __initdata = {
1931 &dev_attr_hotkey_enable.attr,
1932 &dev_attr_hotkey_bios_enabled.attr,
1933 &dev_attr_hotkey_report_mode.attr,
1934 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1935 &dev_attr_hotkey_mask.attr,
1936 &dev_attr_hotkey_all_mask.attr,
1937 &dev_attr_hotkey_recommended_mask.attr,
1938 &dev_attr_hotkey_source_mask.attr,
1939 &dev_attr_hotkey_poll_freq.attr,
1943 static struct attribute *hotkey_mask_attributes[] __initdata = {
1944 &dev_attr_hotkey_bios_mask.attr,
1945 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1946 &dev_attr_hotkey_mask.attr,
1947 &dev_attr_hotkey_all_mask.attr,
1948 &dev_attr_hotkey_recommended_mask.attr,
1950 &dev_attr_hotkey_wakeup_reason.attr,
1951 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1954 static void bluetooth_update_rfk(void);
1955 static void wan_update_rfk(void);
1956 static void tpacpi_send_radiosw_update(void)
1960 /* Sync these BEFORE sending any rfkill events */
1961 if (tp_features.bluetooth)
1962 bluetooth_update_rfk();
1963 if (tp_features.wan)
1966 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1967 mutex_lock(&tpacpi_inputdev_send_mutex);
1969 input_report_switch(tpacpi_inputdev,
1970 SW_RFKILL_ALL, !!wlsw);
1971 input_sync(tpacpi_inputdev);
1973 mutex_unlock(&tpacpi_inputdev_send_mutex);
1975 hotkey_radio_sw_notify_change();
1978 static void hotkey_exit(void)
1980 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1981 hotkey_poll_stop_sync();
1984 if (hotkey_dev_attributes)
1985 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1987 kfree(hotkey_keycode_map);
1989 if (tp_features.hotkey) {
1990 dbg_printk(TPACPI_DBG_EXIT,
1991 "restoring original hot key mask\n");
1992 /* no short-circuit boolean operator below! */
1993 if ((hotkey_mask_set(hotkey_orig_mask) |
1994 hotkey_status_set(hotkey_orig_status)) != 0)
1996 "failed to restore hot key mask "
1997 "to BIOS defaults\n");
2001 static int __init hotkey_init(struct ibm_init_struct *iibm)
2003 /* Requirements for changing the default keymaps:
2005 * 1. Many of the keys are mapped to KEY_RESERVED for very
2006 * good reasons. Do not change them unless you have deep
2007 * knowledge on the IBM and Lenovo ThinkPad firmware for
2008 * the various ThinkPad models. The driver behaves
2009 * differently for KEY_RESERVED: such keys have their
2010 * hot key mask *unset* in mask_recommended, and also
2011 * in the initial hot key mask programmed into the
2012 * firmware at driver load time, which means the firm-
2013 * ware may react very differently if you change them to
2016 * 2. You must be subscribed to the linux-thinkpad and
2017 * ibm-acpi-devel mailing lists, and you should read the
2018 * list archives since 2007 if you want to change the
2019 * keymaps. This requirement exists so that you will
2020 * know the past history of problems with the thinkpad-
2021 * acpi driver keymaps, and also that you will be
2022 * listening to any bug reports;
2024 * 3. Do not send thinkpad-acpi specific patches directly to
2025 * for merging, *ever*. Send them to the linux-acpi
2026 * mailinglist for comments. Merging is to be done only
2027 * through acpi-test and the ACPI maintainer.
2029 * If the above is too much to ask, don't change the keymap.
2030 * Ask the thinkpad-acpi maintainer to do it, instead.
2032 static u16 ibm_keycode_map[] __initdata = {
2033 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2034 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2035 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2036 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2038 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2039 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2040 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2041 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2043 /* brightness: firmware always reacts to them, unless
2044 * X.org did some tricks in the radeon BIOS scratch
2045 * registers of *some* models */
2046 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2047 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2049 /* Thinklight: firmware always react to it */
2050 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2052 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2053 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2055 /* Volume: firmware always react to it and reprograms
2056 * the built-in *extra* mixer. Never map it to control
2057 * another mixer by default. */
2058 KEY_RESERVED, /* 0x14: VOLUME UP */
2059 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2060 KEY_RESERVED, /* 0x16: MUTE */
2062 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2064 /* (assignments unknown, please report if found) */
2065 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2066 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2068 static u16 lenovo_keycode_map[] __initdata = {
2069 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2070 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2071 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2072 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2074 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2075 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2076 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2077 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2079 /* These either have to go through ACPI video, or
2080 * act like in the IBM ThinkPads, so don't ever
2081 * enable them by default */
2082 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2083 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2085 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2087 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2088 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2090 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2091 * react to it and reprograms the built-in *extra* mixer.
2092 * Never map it to control another mixer by default.
2094 * T60?, T61, R60?, R61: firmware and EC tries to send
2095 * these over the regular keyboard, so these are no-ops,
2096 * but there are still weird bugs re. MUTE, so do not
2097 * change unless you get test reports from all Lenovo
2098 * models. May cause the BIOS to interfere with the
2101 KEY_RESERVED, /* 0x14: VOLUME UP */
2102 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2103 KEY_RESERVED, /* 0x16: MUTE */
2105 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2107 /* (assignments unknown, please report if found) */
2108 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2109 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2112 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2113 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2114 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2120 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2122 BUG_ON(!tpacpi_inputdev);
2123 BUG_ON(tpacpi_inputdev->open != NULL ||
2124 tpacpi_inputdev->close != NULL);
2126 TPACPI_ACPIHANDLE_INIT(hkey);
2127 mutex_init(&hotkey_mutex);
2129 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2130 mutex_init(&hotkey_thread_mutex);
2131 mutex_init(&hotkey_thread_data_mutex);
2134 /* hotkey not supported on 570 */
2135 tp_features.hotkey = hkey_handle != NULL;
2137 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2138 str_supported(tp_features.hotkey));
2140 if (!tp_features.hotkey)
2143 hotkey_dev_attributes = create_attr_set(13, NULL);
2144 if (!hotkey_dev_attributes)
2146 res = add_many_to_attr_set(hotkey_dev_attributes,
2148 ARRAY_SIZE(hotkey_attributes));
2152 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2153 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2154 for HKEY interface version 0x100 */
2155 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2156 if ((hkeyv >> 8) != 1) {
2157 printk(TPACPI_ERR "unknown version of the "
2158 "HKEY interface: 0x%x\n", hkeyv);
2159 printk(TPACPI_ERR "please report this to %s\n",
2163 * MHKV 0x100 in A31, R40, R40e,
2164 * T4x, X31, and later
2166 tp_features.hotkey_mask = 1;
2170 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2171 str_supported(tp_features.hotkey_mask));
2173 if (tp_features.hotkey_mask) {
2174 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2177 "missing MHKA handler, "
2178 "please report this to %s\n",
2180 /* FN+F12, FN+F4, FN+F3 */
2181 hotkey_all_mask = 0x080cU;
2185 /* hotkey_source_mask *must* be zero for
2186 * the first hotkey_mask_get */
2187 res = hotkey_status_get(&hotkey_orig_status);
2191 if (tp_features.hotkey_mask) {
2192 res = hotkey_mask_get();
2196 hotkey_orig_mask = hotkey_mask;
2197 res = add_many_to_attr_set(
2198 hotkey_dev_attributes,
2199 hotkey_mask_attributes,
2200 ARRAY_SIZE(hotkey_mask_attributes));
2205 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2206 if (tp_features.hotkey_mask) {
2207 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2210 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2213 vdbg_printk(TPACPI_DBG_INIT,
2214 "hotkey source mask 0x%08x, polling freq %d\n",
2215 hotkey_source_mask, hotkey_poll_freq);
2218 /* Not all thinkpads have a hardware radio switch */
2219 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2220 tp_features.hotkey_wlsw = 1;
2222 "radio switch found; radios are %s\n",
2223 enabled(status, 0));
2225 if (tp_features.hotkey_wlsw)
2226 res = add_to_attr_set(hotkey_dev_attributes,
2227 &dev_attr_hotkey_radio_sw.attr);
2229 /* For X41t, X60t, X61t Tablets... */
2230 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2231 tp_features.hotkey_tablet = 1;
2233 "possible tablet mode switch found; "
2234 "ThinkPad in %s mode\n",
2235 (status & TP_HOTKEY_TABLET_MASK)?
2236 "tablet" : "laptop");
2237 res = add_to_attr_set(hotkey_dev_attributes,
2238 &dev_attr_hotkey_tablet_mode.attr);
2242 res = register_attr_set_with_sysfs(
2243 hotkey_dev_attributes,
2244 &tpacpi_pdev->dev.kobj);
2248 /* Set up key map */
2250 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2252 if (!hotkey_keycode_map) {
2254 "failed to allocate memory for key map\n");
2259 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2260 dbg_printk(TPACPI_DBG_INIT,
2261 "using Lenovo default hot key map\n");
2262 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2263 TPACPI_HOTKEY_MAP_SIZE);
2265 dbg_printk(TPACPI_DBG_INIT,
2266 "using IBM default hot key map\n");
2267 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2268 TPACPI_HOTKEY_MAP_SIZE);
2271 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2272 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2273 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2274 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2275 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2276 tpacpi_inputdev->keycode = hotkey_keycode_map;
2277 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2278 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2279 set_bit(hotkey_keycode_map[i],
2280 tpacpi_inputdev->keybit);
2282 if (i < sizeof(hotkey_reserved_mask)*8)
2283 hotkey_reserved_mask |= 1 << i;
2287 if (tp_features.hotkey_wlsw) {
2288 set_bit(EV_SW, tpacpi_inputdev->evbit);
2289 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2291 if (tp_features.hotkey_tablet) {
2292 set_bit(EV_SW, tpacpi_inputdev->evbit);
2293 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2296 /* Do not issue duplicate brightness change events to
2298 if (!tp_features.bright_acpimode)
2299 /* update bright_acpimode... */
2300 tpacpi_check_std_acpi_brightness_support();
2302 if (tp_features.bright_acpimode) {
2304 "This ThinkPad has standard ACPI backlight "
2305 "brightness control, supported by the ACPI "
2307 printk(TPACPI_NOTICE
2308 "Disabling thinkpad-acpi brightness events "
2311 /* The hotkey_reserved_mask change below is not
2312 * necessary while the keys are at KEY_RESERVED in the
2313 * default map, but better safe than sorry, leave it
2314 * here as a marker of what we have to do, especially
2315 * when we finally become able to set this at runtime
2316 * on response to X.org requests */
2317 hotkey_reserved_mask |=
2318 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2319 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2322 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2323 res = hotkey_status_set(1);
2328 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2329 & ~hotkey_reserved_mask)
2330 | hotkey_orig_mask);
2331 if (res < 0 && res != -ENXIO) {
2336 dbg_printk(TPACPI_DBG_INIT,
2337 "legacy hot key reporting over procfs %s\n",
2338 (hotkey_report_mode < 2) ?
2339 "enabled" : "disabled");
2341 tpacpi_inputdev->open = &hotkey_inputdev_open;
2342 tpacpi_inputdev->close = &hotkey_inputdev_close;
2344 hotkey_poll_setup_safe(1);
2345 tpacpi_send_radiosw_update();
2346 tpacpi_input_send_tabletsw();
2351 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2352 hotkey_dev_attributes = NULL;
2354 return (res < 0)? res : 1;
2357 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2360 unsigned int scancode;
2365 if (event != 0x80) {
2367 "unknown HKEY notification event %d\n", event);
2368 /* forward it to userspace, maybe it knows how to handle it */
2369 acpi_bus_generate_netlink_event(
2370 ibm->acpi->device->pnp.device_class,
2371 ibm->acpi->device->dev.bus_id,
2377 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2378 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2391 switch (hkey >> 12) {
2393 /* 0x1000-0x1FFF: key presses */
2394 scancode = hkey & 0xfff;
2395 if (scancode > 0 && scancode < 0x21) {
2397 if (!(hotkey_source_mask & (1 << scancode))) {
2398 tpacpi_input_send_key(scancode);
2410 case 0x2304: /* suspend, undock */
2411 case 0x2404: /* hibernation, undock */
2412 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2415 case 0x2305: /* suspend, bay eject */
2416 case 0x2405: /* hibernation, bay eject */
2417 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2423 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2425 "woke up due to a hot-unplug "
2427 hotkey_wakeup_reason_notify_change();
2431 /* bay-related wakeups */
2432 if (hkey == 0x3003) {
2433 hotkey_autosleep_ack = 1;
2436 hotkey_wakeup_hotunplug_complete_notify_change();
2442 /* dock-related wakeups */
2443 if (hkey == 0x4003) {
2444 hotkey_autosleep_ack = 1;
2447 hotkey_wakeup_hotunplug_complete_notify_change();
2453 /* 0x5000-0x5FFF: human interface helpers */
2455 case 0x5010: /* Lenovo new BIOS: brightness changed */
2456 case 0x500b: /* X61t: tablet pen inserted into bay */
2457 case 0x500c: /* X61t: tablet pen removed from bay */
2459 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2460 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2461 tpacpi_input_send_tabletsw();
2462 hotkey_tablet_mode_notify_change();
2467 /* LID switch events. Do not propagate */
2475 /* 0x7000-0x7FFF: misc */
2476 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2477 tpacpi_send_radiosw_update();
2481 /* fallthrough to default */
2486 printk(TPACPI_NOTICE
2487 "unhandled HKEY event 0x%04x\n", hkey);
2491 if (!ignore_acpi_ev &&
2492 (send_acpi_ev || hotkey_report_mode < 2)) {
2493 acpi_bus_generate_proc_event(ibm->acpi->device,
2497 /* netlink events */
2498 if (!ignore_acpi_ev && send_acpi_ev) {
2499 acpi_bus_generate_netlink_event(
2500 ibm->acpi->device->pnp.device_class,
2501 ibm->acpi->device->dev.bus_id,
2507 static void hotkey_suspend(pm_message_t state)
2509 /* Do these on suspend, we get the events on early resume! */
2510 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2511 hotkey_autosleep_ack = 0;
2514 static void hotkey_resume(void)
2516 if (hotkey_mask_get())
2518 "error while trying to read hot key mask "
2520 tpacpi_send_radiosw_update();
2521 hotkey_tablet_mode_notify_change();
2522 hotkey_wakeup_reason_notify_change();
2523 hotkey_wakeup_hotunplug_complete_notify_change();
2524 hotkey_poll_setup_safe(0);
2527 /* procfs -------------------------------------------------------------- */
2528 static int hotkey_read(char *p)
2533 if (!tp_features.hotkey) {
2534 len += sprintf(p + len, "status:\t\tnot supported\n");
2538 if (mutex_lock_interruptible(&hotkey_mutex))
2539 return -ERESTARTSYS;
2540 res = hotkey_status_get(&status);
2542 res = hotkey_mask_get();
2543 mutex_unlock(&hotkey_mutex);
2547 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2548 if (tp_features.hotkey_mask) {
2549 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2550 len += sprintf(p + len,
2551 "commands:\tenable, disable, reset, <mask>\n");
2553 len += sprintf(p + len, "mask:\t\tnot supported\n");
2554 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2560 static int hotkey_write(char *buf)
2566 if (!tp_features.hotkey)
2569 if (mutex_lock_interruptible(&hotkey_mutex))
2570 return -ERESTARTSYS;
2576 while ((cmd = next_cmd(&buf))) {
2577 if (strlencmp(cmd, "enable") == 0) {
2579 } else if (strlencmp(cmd, "disable") == 0) {
2581 } else if (strlencmp(cmd, "reset") == 0) {
2582 status = hotkey_orig_status;
2583 mask = hotkey_orig_mask;
2584 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2586 } else if (sscanf(cmd, "%x", &mask) == 1) {
2594 res = hotkey_status_set(status);
2596 if (!res && mask != hotkey_mask)
2597 res = hotkey_mask_set(mask);
2600 mutex_unlock(&hotkey_mutex);
2604 static const struct acpi_device_id ibm_htk_device_ids[] = {
2605 {TPACPI_ACPI_HKEY_HID, 0},
2609 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2610 .hid = ibm_htk_device_ids,
2611 .notify = hotkey_notify,
2612 .handle = &hkey_handle,
2613 .type = ACPI_DEVICE_NOTIFY,
2616 static struct ibm_struct hotkey_driver_data = {
2618 .read = hotkey_read,
2619 .write = hotkey_write,
2620 .exit = hotkey_exit,
2621 .resume = hotkey_resume,
2622 .suspend = hotkey_suspend,
2623 .acpi = &ibm_hotkey_acpidriver,
2626 /*************************************************************************
2627 * Bluetooth subdriver
2631 /* ACPI GBDC/SBDC bits */
2632 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2633 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2634 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2637 static struct rfkill *tpacpi_bluetooth_rfkill;
2639 static int bluetooth_get_radiosw(void)
2643 if (!tp_features.bluetooth)
2646 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2647 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2648 return RFKILL_STATE_HARD_BLOCKED;
2650 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2653 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2654 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2657 static void bluetooth_update_rfk(void)
2661 if (!tpacpi_bluetooth_rfkill)
2664 status = bluetooth_get_radiosw();
2667 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2670 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2674 if (!tp_features.bluetooth)
2677 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2678 * reason to risk weird behaviour. */
2679 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2683 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2686 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2688 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2689 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2693 bluetooth_update_rfk();
2698 /* sysfs bluetooth enable ---------------------------------------------- */
2699 static ssize_t bluetooth_enable_show(struct device *dev,
2700 struct device_attribute *attr,
2705 status = bluetooth_get_radiosw();
2709 return snprintf(buf, PAGE_SIZE, "%d\n",
2710 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2713 static ssize_t bluetooth_enable_store(struct device *dev,
2714 struct device_attribute *attr,
2715 const char *buf, size_t count)
2720 if (parse_strtoul(buf, 1, &t))
2723 res = bluetooth_set_radiosw(t, 1);
2725 return (res) ? res : count;
2728 static struct device_attribute dev_attr_bluetooth_enable =
2729 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2730 bluetooth_enable_show, bluetooth_enable_store);
2732 /* --------------------------------------------------------------------- */
2734 static struct attribute *bluetooth_attributes[] = {
2735 &dev_attr_bluetooth_enable.attr,
2739 static const struct attribute_group bluetooth_attr_group = {
2740 .attrs = bluetooth_attributes,
2743 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2745 int bts = bluetooth_get_radiosw();
2754 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2756 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2759 static void bluetooth_exit(void)
2761 if (tpacpi_bluetooth_rfkill)
2762 rfkill_unregister(tpacpi_bluetooth_rfkill);
2764 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2765 &bluetooth_attr_group);
2768 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2773 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2775 TPACPI_ACPIHANDLE_INIT(hkey);
2777 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2778 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2779 tp_features.bluetooth = hkey_handle &&
2780 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2782 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2783 str_supported(tp_features.bluetooth),
2786 if (tp_features.bluetooth &&
2787 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2788 /* no bluetooth hardware present in system */
2789 tp_features.bluetooth = 0;
2790 dbg_printk(TPACPI_DBG_INIT,
2791 "bluetooth hardware not installed\n");
2794 if (!tp_features.bluetooth)
2797 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2798 &bluetooth_attr_group);
2802 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2803 &tpacpi_bluetooth_rfkill,
2804 RFKILL_TYPE_BLUETOOTH,
2805 "tpacpi_bluetooth_sw",
2806 tpacpi_bluetooth_rfk_set,
2807 tpacpi_bluetooth_rfk_get);
2816 /* procfs -------------------------------------------------------------- */
2817 static int bluetooth_read(char *p)
2820 int status = bluetooth_get_radiosw();
2822 if (!tp_features.bluetooth)
2823 len += sprintf(p + len, "status:\t\tnot supported\n");
2825 len += sprintf(p + len, "status:\t\t%s\n",
2826 (status == RFKILL_STATE_UNBLOCKED) ?
2827 "enabled" : "disabled");
2828 len += sprintf(p + len, "commands:\tenable, disable\n");
2834 static int bluetooth_write(char *buf)
2838 if (!tp_features.bluetooth)
2841 while ((cmd = next_cmd(&buf))) {
2842 if (strlencmp(cmd, "enable") == 0) {
2843 bluetooth_set_radiosw(1, 1);
2844 } else if (strlencmp(cmd, "disable") == 0) {
2845 bluetooth_set_radiosw(0, 1);
2853 static struct ibm_struct bluetooth_driver_data = {
2854 .name = "bluetooth",
2855 .read = bluetooth_read,
2856 .write = bluetooth_write,
2857 .exit = bluetooth_exit,
2860 /*************************************************************************
2865 /* ACPI GWAN/SWAN bits */
2866 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2867 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2868 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2871 static struct rfkill *tpacpi_wan_rfkill;
2873 static int wan_get_radiosw(void)
2877 if (!tp_features.wan)
2880 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2881 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2882 return RFKILL_STATE_HARD_BLOCKED;
2884 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2887 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
2888 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2891 static void wan_update_rfk(void)
2895 if (!tpacpi_wan_rfkill)
2898 status = wan_get_radiosw();
2901 rfkill_force_state(tpacpi_wan_rfkill, status);
2904 static int wan_set_radiosw(int radio_on, int update_rfk)
2908 if (!tp_features.wan)
2911 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2912 * reason to risk weird behaviour. */
2913 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2917 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2920 status |= TP_ACPI_WANCARD_RADIOSSW;
2922 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2923 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2932 /* sysfs wan enable ---------------------------------------------------- */
2933 static ssize_t wan_enable_show(struct device *dev,
2934 struct device_attribute *attr,
2939 status = wan_get_radiosw();
2943 return snprintf(buf, PAGE_SIZE, "%d\n",
2944 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2947 static ssize_t wan_enable_store(struct device *dev,
2948 struct device_attribute *attr,
2949 const char *buf, size_t count)
2954 if (parse_strtoul(buf, 1, &t))
2957 res = wan_set_radiosw(t, 1);
2959 return (res) ? res : count;
2962 static struct device_attribute dev_attr_wan_enable =
2963 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2964 wan_enable_show, wan_enable_store);
2966 /* --------------------------------------------------------------------- */
2968 static struct attribute *wan_attributes[] = {
2969 &dev_attr_wan_enable.attr,
2973 static const struct attribute_group wan_attr_group = {
2974 .attrs = wan_attributes,
2977 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
2979 int wans = wan_get_radiosw();
2988 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
2990 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2993 static void wan_exit(void)
2995 if (tpacpi_wan_rfkill)
2996 rfkill_unregister(tpacpi_wan_rfkill);
2998 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3002 static int __init wan_init(struct ibm_init_struct *iibm)
3007 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3009 TPACPI_ACPIHANDLE_INIT(hkey);
3011 tp_features.wan = hkey_handle &&
3012 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3014 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3015 str_supported(tp_features.wan),
3018 if (tp_features.wan &&
3019 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3020 /* no wan hardware present in system */
3021 tp_features.wan = 0;
3022 dbg_printk(TPACPI_DBG_INIT,
3023 "wan hardware not installed\n");
3026 if (!tp_features.wan)
3029 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3034 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3039 tpacpi_wan_rfk_get);
3048 /* procfs -------------------------------------------------------------- */
3049 static int wan_read(char *p)
3052 int status = wan_get_radiosw();
3054 if (!tp_features.wan)
3055 len += sprintf(p + len, "status:\t\tnot supported\n");
3057 len += sprintf(p + len, "status:\t\t%s\n",
3058 (status == RFKILL_STATE_UNBLOCKED) ?
3059 "enabled" : "disabled");
3060 len += sprintf(p + len, "commands:\tenable, disable\n");
3066 static int wan_write(char *buf)
3070 if (!tp_features.wan)
3073 while ((cmd = next_cmd(&buf))) {
3074 if (strlencmp(cmd, "enable") == 0) {
3075 wan_set_radiosw(1, 1);
3076 } else if (strlencmp(cmd, "disable") == 0) {
3077 wan_set_radiosw(0, 1);
3085 static struct ibm_struct wan_driver_data = {
3092 /*************************************************************************
3096 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3098 enum video_access_mode {
3099 TPACPI_VIDEO_NONE = 0,
3100 TPACPI_VIDEO_570, /* 570 */
3101 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3102 TPACPI_VIDEO_NEW, /* all others */
3105 enum { /* video status flags, based on VIDEO_570 */
3106 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3107 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3108 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3111 enum { /* TPACPI_VIDEO_570 constants */
3112 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3113 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3114 * video_status_flags */
3115 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3116 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3119 static enum video_access_mode video_supported;
3120 static int video_orig_autosw;
3122 static int video_autosw_get(void);
3123 static int video_autosw_set(int enable);
3125 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3127 static int __init video_init(struct ibm_init_struct *iibm)
3131 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3133 TPACPI_ACPIHANDLE_INIT(vid);
3134 TPACPI_ACPIHANDLE_INIT(vid2);
3136 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3137 /* G41, assume IVGA doesn't change */
3138 vid_handle = vid2_handle;
3141 /* video switching not supported on R30, R31 */
3142 video_supported = TPACPI_VIDEO_NONE;
3143 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3145 video_supported = TPACPI_VIDEO_570;
3146 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3147 /* 600e/x, 770e, 770x */
3148 video_supported = TPACPI_VIDEO_770;
3151 video_supported = TPACPI_VIDEO_NEW;
3153 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3154 str_supported(video_supported != TPACPI_VIDEO_NONE),
3157 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3160 static void video_exit(void)
3162 dbg_printk(TPACPI_DBG_EXIT,
3163 "restoring original video autoswitch mode\n");
3164 if (video_autosw_set(video_orig_autosw))
3165 printk(TPACPI_ERR "error while trying to restore original "
3166 "video autoswitch mode\n");
3169 static int video_outputsw_get(void)
3174 switch (video_supported) {
3175 case TPACPI_VIDEO_570:
3176 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3177 TP_ACPI_VIDEO_570_PHSCMD))
3179 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3181 case TPACPI_VIDEO_770:
3182 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3185 status |= TP_ACPI_VIDEO_S_LCD;
3186 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3189 status |= TP_ACPI_VIDEO_S_CRT;
3191 case TPACPI_VIDEO_NEW:
3192 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3193 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3196 status |= TP_ACPI_VIDEO_S_CRT;
3198 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3199 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3202 status |= TP_ACPI_VIDEO_S_LCD;
3203 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3206 status |= TP_ACPI_VIDEO_S_DVI;
3215 static int video_outputsw_set(int status)
3220 switch (video_supported) {
3221 case TPACPI_VIDEO_570:
3222 res = acpi_evalf(NULL, NULL,
3223 "\\_SB.PHS2", "vdd",
3224 TP_ACPI_VIDEO_570_PHS2CMD,
3225 status | TP_ACPI_VIDEO_570_PHS2SET);
3227 case TPACPI_VIDEO_770:
3228 autosw = video_autosw_get();
3232 res = video_autosw_set(1);
3235 res = acpi_evalf(vid_handle, NULL,
3236 "ASWT", "vdd", status * 0x100, 0);
3237 if (!autosw && video_autosw_set(autosw)) {
3239 "video auto-switch left enabled due to error\n");
3243 case TPACPI_VIDEO_NEW:
3244 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3245 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3251 return (res)? 0 : -EIO;
3254 static int video_autosw_get(void)
3258 switch (video_supported) {
3259 case TPACPI_VIDEO_570:
3260 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3263 case TPACPI_VIDEO_770:
3264 case TPACPI_VIDEO_NEW:
3265 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3275 static int video_autosw_set(int enable)
3277 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3282 static int video_outputsw_cycle(void)
3284 int autosw = video_autosw_get();
3290 switch (video_supported) {
3291 case TPACPI_VIDEO_570:
3292 res = video_autosw_set(1);
3295 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3297 case TPACPI_VIDEO_770:
3298 case TPACPI_VIDEO_NEW:
3299 res = video_autosw_set(1);
3302 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3307 if (!autosw && video_autosw_set(autosw)) {
3309 "video auto-switch left enabled due to error\n");
3313 return (res)? 0 : -EIO;
3316 static int video_expand_toggle(void)
3318 switch (video_supported) {
3319 case TPACPI_VIDEO_570:
3320 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3322 case TPACPI_VIDEO_770:
3323 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3325 case TPACPI_VIDEO_NEW:
3326 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3334 static int video_read(char *p)
3339 if (video_supported == TPACPI_VIDEO_NONE) {
3340 len += sprintf(p + len, "status:\t\tnot supported\n");
3344 status = video_outputsw_get();
3348 autosw = video_autosw_get();
3352 len += sprintf(p + len, "status:\t\tsupported\n");
3353 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3354 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3355 if (video_supported == TPACPI_VIDEO_NEW)
3356 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3357 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3358 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3359 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3360 if (video_supported == TPACPI_VIDEO_NEW)
3361 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3362 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3363 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3368 static int video_write(char *buf)
3371 int enable, disable, status;
3374 if (video_supported == TPACPI_VIDEO_NONE)
3380 while ((cmd = next_cmd(&buf))) {
3381 if (strlencmp(cmd, "lcd_enable") == 0) {
3382 enable |= TP_ACPI_VIDEO_S_LCD;
3383 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3384 disable |= TP_ACPI_VIDEO_S_LCD;
3385 } else if (strlencmp(cmd, "crt_enable") == 0) {
3386 enable |= TP_ACPI_VIDEO_S_CRT;
3387 } else if (strlencmp(cmd, "crt_disable") == 0) {
3388 disable |= TP_ACPI_VIDEO_S_CRT;
3389 } else if (video_supported == TPACPI_VIDEO_NEW &&
3390 strlencmp(cmd, "dvi_enable") == 0) {
3391 enable |= TP_ACPI_VIDEO_S_DVI;
3392 } else if (video_supported == TPACPI_VIDEO_NEW &&
3393 strlencmp(cmd, "dvi_disable") == 0) {
3394 disable |= TP_ACPI_VIDEO_S_DVI;
3395 } else if (strlencmp(cmd, "auto_enable") == 0) {
3396 res = video_autosw_set(1);
3399 } else if (strlencmp(cmd, "auto_disable") == 0) {
3400 res = video_autosw_set(0);
3403 } else if (strlencmp(cmd, "video_switch") == 0) {
3404 res = video_outputsw_cycle();
3407 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3408 res = video_expand_toggle();
3415 if (enable || disable) {
3416 status = video_outputsw_get();
3419 res = video_outputsw_set((status & ~disable) | enable);
3427 static struct ibm_struct video_driver_data = {
3430 .write = video_write,
3434 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3436 /*************************************************************************
3437 * Light (thinklight) subdriver
3440 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3441 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3443 static int light_get_status(void)
3447 if (tp_features.light_status) {
3448 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3456 static int light_set_status(int status)
3460 if (tp_features.light) {
3462 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3464 TP_CMOS_THINKLIGHT_ON :
3465 TP_CMOS_THINKLIGHT_OFF);
3467 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3470 return (rc)? 0 : -EIO;
3476 static void light_set_status_worker(struct work_struct *work)
3478 struct tpacpi_led_classdev *data =
3479 container_of(work, struct tpacpi_led_classdev, work);
3481 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3482 light_set_status((data->new_brightness != LED_OFF));
3485 static void light_sysfs_set(struct led_classdev *led_cdev,
3486 enum led_brightness brightness)
3488 struct tpacpi_led_classdev *data =
3489 container_of(led_cdev,
3490 struct tpacpi_led_classdev,
3492 data->new_brightness = brightness;
3493 queue_work(tpacpi_wq, &data->work);
3496 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3498 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3501 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3503 .name = "tpacpi::thinklight",
3504 .brightness_set = &light_sysfs_set,
3505 .brightness_get = &light_sysfs_get,
3509 static int __init light_init(struct ibm_init_struct *iibm)
3513 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3515 TPACPI_ACPIHANDLE_INIT(ledb);
3516 TPACPI_ACPIHANDLE_INIT(lght);
3517 TPACPI_ACPIHANDLE_INIT(cmos);
3518 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3520 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3521 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3523 if (tp_features.light)
3524 /* light status not supported on
3525 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3526 tp_features.light_status =
3527 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3529 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3530 str_supported(tp_features.light),
3531 str_supported(tp_features.light_status));
3533 if (!tp_features.light)
3536 rc = led_classdev_register(&tpacpi_pdev->dev,
3537 &tpacpi_led_thinklight.led_classdev);
3540 tp_features.light = 0;
3541 tp_features.light_status = 0;
3549 static void light_exit(void)
3551 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3552 if (work_pending(&tpacpi_led_thinklight.work))
3553 flush_workqueue(tpacpi_wq);
3556 static int light_read(char *p)
3561 if (!tp_features.light) {
3562 len += sprintf(p + len, "status:\t\tnot supported\n");
3563 } else if (!tp_features.light_status) {
3564 len += sprintf(p + len, "status:\t\tunknown\n");
3565 len += sprintf(p + len, "commands:\ton, off\n");
3567 status = light_get_status();
3570 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3571 len += sprintf(p + len, "commands:\ton, off\n");
3577 static int light_write(char *buf)
3582 if (!tp_features.light)
3585 while ((cmd = next_cmd(&buf))) {
3586 if (strlencmp(cmd, "on") == 0) {
3588 } else if (strlencmp(cmd, "off") == 0) {
3594 return light_set_status(newstatus);
3597 static struct ibm_struct light_driver_data = {
3600 .write = light_write,
3604 /*************************************************************************
3608 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3610 static void dock_notify(struct ibm_struct *ibm, u32 event);
3611 static int dock_read(char *p);
3612 static int dock_write(char *buf);
3614 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3615 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3616 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3617 "\\_SB.PCI.ISA.SLCE", /* 570 */
3618 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3620 /* don't list other alternatives as we install a notify handler on the 570 */
3621 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3623 static const struct acpi_device_id ibm_pci_device_ids[] = {
3624 {PCI_ROOT_HID_STRING, 0},
3628 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3630 .notify = dock_notify,
3631 .handle = &dock_handle,
3632 .type = ACPI_SYSTEM_NOTIFY,
3635 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3636 * We just use it to get notifications of dock hotplug
3637 * in very old thinkpads */
3638 .hid = ibm_pci_device_ids,
3639 .notify = dock_notify,
3640 .handle = &pci_handle,
3641 .type = ACPI_SYSTEM_NOTIFY,
3645 static struct ibm_struct dock_driver_data[2] = {
3649 .write = dock_write,
3650 .acpi = &ibm_dock_acpidriver[0],
3654 .acpi = &ibm_dock_acpidriver[1],
3658 #define dock_docked() (_sta(dock_handle) & 1)
3660 static int __init dock_init(struct ibm_init_struct *iibm)
3662 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3664 TPACPI_ACPIHANDLE_INIT(dock);
3666 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3667 str_supported(dock_handle != NULL));
3669 return (dock_handle)? 0 : 1;
3672 static int __init dock_init2(struct ibm_init_struct *iibm)
3676 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3678 if (dock_driver_data[0].flags.acpi_driver_registered &&
3679 dock_driver_data[0].flags.acpi_notify_installed) {
3680 TPACPI_ACPIHANDLE_INIT(pci);
3681 dock2_needed = (pci_handle != NULL);
3682 vdbg_printk(TPACPI_DBG_INIT,
3683 "dock PCI handler for the TP 570 is %s\n",
3684 str_supported(dock2_needed));
3686 vdbg_printk(TPACPI_DBG_INIT,
3687 "dock subdriver part 2 not required\n");
3691 return (dock2_needed)? 0 : 1;
3694 static void dock_notify(struct ibm_struct *ibm, u32 event)
3696 int docked = dock_docked();
3697 int pci = ibm->acpi->hid && ibm->acpi->device &&
3698 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3701 if (event == 1 && !pci) /* 570 */
3702 data = 1; /* button */
3703 else if (event == 1 && pci) /* 570 */
3704 data = 3; /* dock */
3705 else if (event == 3 && docked)
3706 data = 1; /* button */
3707 else if (event == 3 && !docked)
3708 data = 2; /* undock */
3709 else if (event == 0 && docked)
3710 data = 3; /* dock */
3712 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3713 event, _sta(dock_handle));
3714 data = 0; /* unknown */
3716 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3717 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3718 ibm->acpi->device->dev.bus_id,
3722 static int dock_read(char *p)
3725 int docked = dock_docked();
3728 len += sprintf(p + len, "status:\t\tnot supported\n");
3730 len += sprintf(p + len, "status:\t\tundocked\n");
3732 len += sprintf(p + len, "status:\t\tdocked\n");
3733 len += sprintf(p + len, "commands:\tdock, undock\n");
3739 static int dock_write(char *buf)
3746 while ((cmd = next_cmd(&buf))) {
3747 if (strlencmp(cmd, "undock") == 0) {
3748 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3749 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3751 } else if (strlencmp(cmd, "dock") == 0) {
3752 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3761 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3763 /*************************************************************************
3767 #ifdef CONFIG_THINKPAD_ACPI_BAY
3769 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3770 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3771 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3772 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3773 ); /* A21e, R30, R31 */
3774 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3775 "_EJ0", /* all others */
3776 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3777 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3778 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3780 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3784 static int __init bay_init(struct ibm_init_struct *iibm)
3786 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3788 TPACPI_ACPIHANDLE_INIT(bay);
3790 TPACPI_ACPIHANDLE_INIT(bay_ej);
3791 TPACPI_ACPIHANDLE_INIT(bay2);
3793 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3795 tp_features.bay_status = bay_handle &&
3796 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3797 tp_features.bay_status2 = bay2_handle &&
3798 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3800 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3801 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3802 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3803 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3805 vdbg_printk(TPACPI_DBG_INIT,
3806 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3807 str_supported(tp_features.bay_status),
3808 str_supported(tp_features.bay_eject),
3809 str_supported(tp_features.bay_status2),
3810 str_supported(tp_features.bay_eject2));
3812 return (tp_features.bay_status || tp_features.bay_eject ||
3813 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3816 static void bay_notify(struct ibm_struct *ibm, u32 event)
3818 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3819 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3820 ibm->acpi->device->dev.bus_id,
3824 #define bay_occupied(b) (_sta(b##_handle) & 1)
3826 static int bay_read(char *p)
3829 int occupied = bay_occupied(bay);
3830 int occupied2 = bay_occupied(bay2);
3833 len += sprintf(p + len, "status:\t\t%s\n",
3834 tp_features.bay_status ?
3835 (occupied ? "occupied" : "unoccupied") :
3837 if (tp_features.bay_status2)
3838 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3839 "occupied" : "unoccupied");
3841 eject = tp_features.bay_eject && occupied;
3842 eject2 = tp_features.bay_eject2 && occupied2;
3844 if (eject && eject2)
3845 len += sprintf(p + len, "commands:\teject, eject2\n");
3847 len += sprintf(p + len, "commands:\teject\n");
3849 len += sprintf(p + len, "commands:\teject2\n");
3854 static int bay_write(char *buf)
3858 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3861 while ((cmd = next_cmd(&buf))) {
3862 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3863 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3865 } else if (tp_features.bay_eject2 &&
3866 strlencmp(cmd, "eject2") == 0) {
3867 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3876 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3877 .notify = bay_notify,
3878 .handle = &bay_handle,
3879 .type = ACPI_SYSTEM_NOTIFY,
3882 static struct ibm_struct bay_driver_data = {
3886 .acpi = &ibm_bay_acpidriver,
3889 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3891 /*************************************************************************
3895 /* sysfs cmos_command -------------------------------------------------- */
3896 static ssize_t cmos_command_store(struct device *dev,
3897 struct device_attribute *attr,
3898 const char *buf, size_t count)
3900 unsigned long cmos_cmd;
3903 if (parse_strtoul(buf, 21, &cmos_cmd))
3906 res = issue_thinkpad_cmos_command(cmos_cmd);
3907 return (res)? res : count;
3910 static struct device_attribute dev_attr_cmos_command =
3911 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3913 /* --------------------------------------------------------------------- */
3915 static int __init cmos_init(struct ibm_init_struct *iibm)
3919 vdbg_printk(TPACPI_DBG_INIT,
3920 "initializing cmos commands subdriver\n");
3922 TPACPI_ACPIHANDLE_INIT(cmos);
3924 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3925 str_supported(cmos_handle != NULL));
3927 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3931 return (cmos_handle)? 0 : 1;
3934 static void cmos_exit(void)
3936 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3939 static int cmos_read(char *p)
3943 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3944 R30, R31, T20-22, X20-21 */
3946 len += sprintf(p + len, "status:\t\tnot supported\n");
3948 len += sprintf(p + len, "status:\t\tsupported\n");
3949 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3955 static int cmos_write(char *buf)
3960 while ((cmd = next_cmd(&buf))) {
3961 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3962 cmos_cmd >= 0 && cmos_cmd <= 21) {
3967 res = issue_thinkpad_cmos_command(cmos_cmd);
3975 static struct ibm_struct cmos_driver_data = {
3978 .write = cmos_write,
3982 /*************************************************************************
3986 enum led_access_mode {
3987 TPACPI_LED_NONE = 0,
3988 TPACPI_LED_570, /* 570 */
3989 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3990 TPACPI_LED_NEW, /* all others */
3993 enum { /* For TPACPI_LED_OLD */
3994 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3995 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3996 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4005 static enum led_access_mode led_supported;
4007 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4008 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4009 /* T20-22, X20-21 */
4010 "LED", /* all others */
4013 #define TPACPI_LED_NUMLEDS 8
4014 static struct tpacpi_led_classdev *tpacpi_leds;
4015 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4016 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4017 /* there's a limit of 19 chars + NULL before 2.6.26 */
4019 "tpacpi:orange:batt",
4020 "tpacpi:green:batt",
4021 "tpacpi::dock_active",
4022 "tpacpi::bay_active",
4023 "tpacpi::dock_batt",
4024 "tpacpi::unknown_led",
4028 static int led_get_status(const unsigned int led)
4031 enum led_status_t led_s;
4033 switch (led_supported) {
4034 case TPACPI_LED_570:
4035 if (!acpi_evalf(ec_handle,
4036 &status, "GLED", "dd", 1 << led))
4038 led_s = (status == 0)?
4043 tpacpi_led_state_cache[led] = led_s;
4052 static int led_set_status(const unsigned int led,
4053 const enum led_status_t ledstatus)
4055 /* off, on, blink. Index is led_status_t */
4056 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4057 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4061 switch (led_supported) {
4062 case TPACPI_LED_570:
4066 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4067 (1 << led), led_sled_arg1[ledstatus]))
4070 case TPACPI_LED_OLD:
4071 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4074 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4076 rc = ec_write(TPACPI_LED_EC_HLBL,
4077 (ledstatus == TPACPI_LED_BLINK) << led);
4079 rc = ec_write(TPACPI_LED_EC_HLCL,
4080 (ledstatus != TPACPI_LED_OFF) << led);
4082 case TPACPI_LED_NEW:
4084 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4085 led, led_led_arg1[ledstatus]))
4093 tpacpi_led_state_cache[led] = ledstatus;
4098 static void led_sysfs_set_status(unsigned int led,
4099 enum led_brightness brightness)
4102 (brightness == LED_OFF) ?
4104 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4105 TPACPI_LED_BLINK : TPACPI_LED_ON);
4108 static void led_set_status_worker(struct work_struct *work)
4110 struct tpacpi_led_classdev *data =
4111 container_of(work, struct tpacpi_led_classdev, work);
4113 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4114 led_sysfs_set_status(data->led, data->new_brightness);
4117 static void led_sysfs_set(struct led_classdev *led_cdev,
4118 enum led_brightness brightness)
4120 struct tpacpi_led_classdev *data = container_of(led_cdev,
4121 struct tpacpi_led_classdev, led_classdev);
4123 data->new_brightness = brightness;
4124 queue_work(tpacpi_wq, &data->work);
4127 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4128 unsigned long *delay_on, unsigned long *delay_off)
4130 struct tpacpi_led_classdev *data = container_of(led_cdev,
4131 struct tpacpi_led_classdev, led_classdev);
4133 /* Can we choose the flash rate? */
4134 if (*delay_on == 0 && *delay_off == 0) {
4135 /* yes. set them to the hardware blink rate (1 Hz) */
4136 *delay_on = 500; /* ms */
4137 *delay_off = 500; /* ms */
4138 } else if ((*delay_on != 500) || (*delay_off != 500))
4141 data->new_brightness = TPACPI_LED_BLINK;
4142 queue_work(tpacpi_wq, &data->work);
4147 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4151 struct tpacpi_led_classdev *data = container_of(led_cdev,
4152 struct tpacpi_led_classdev, led_classdev);
4154 rc = led_get_status(data->led);
4156 if (rc == TPACPI_LED_OFF || rc < 0)
4157 rc = LED_OFF; /* no error handling in led class :( */
4164 static void led_exit(void)
4168 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4169 if (tpacpi_leds[i].led_classdev.name)
4170 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4176 static int __init led_init(struct ibm_init_struct *iibm)
4181 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4183 TPACPI_ACPIHANDLE_INIT(led);
4186 /* led not supported on R30, R31 */
4187 led_supported = TPACPI_LED_NONE;
4188 else if (strlencmp(led_path, "SLED") == 0)
4190 led_supported = TPACPI_LED_570;
4191 else if (strlencmp(led_path, "SYSL") == 0)
4192 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4193 led_supported = TPACPI_LED_OLD;
4196 led_supported = TPACPI_LED_NEW;
4198 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4199 str_supported(led_supported), led_supported);
4201 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4204 printk(TPACPI_ERR "Out of memory for LED data\n");
4208 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4209 tpacpi_leds[i].led = i;
4211 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4212 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4213 if (led_supported == TPACPI_LED_570)
4214 tpacpi_leds[i].led_classdev.brightness_get =
4217 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4219 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4221 rc = led_classdev_register(&tpacpi_pdev->dev,
4222 &tpacpi_leds[i].led_classdev);
4224 tpacpi_leds[i].led_classdev.name = NULL;
4230 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4233 #define str_led_status(s) \
4234 ((s) == TPACPI_LED_OFF ? "off" : \
4235 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4237 static int led_read(char *p)
4241 if (!led_supported) {
4242 len += sprintf(p + len, "status:\t\tnot supported\n");
4245 len += sprintf(p + len, "status:\t\tsupported\n");
4247 if (led_supported == TPACPI_LED_570) {
4250 for (i = 0; i < 8; i++) {
4251 status = led_get_status(i);
4254 len += sprintf(p + len, "%d:\t\t%s\n",
4255 i, str_led_status(status));
4259 len += sprintf(p + len, "commands:\t"
4260 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4265 static int led_write(char *buf)
4269 enum led_status_t s;
4274 while ((cmd = next_cmd(&buf))) {
4275 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4278 if (strstr(cmd, "off")) {
4280 } else if (strstr(cmd, "on")) {
4282 } else if (strstr(cmd, "blink")) {
4283 s = TPACPI_LED_BLINK;
4288 rc = led_set_status(led, s);
4296 static struct ibm_struct led_driver_data = {
4303 /*************************************************************************
4307 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4309 static int __init beep_init(struct ibm_init_struct *iibm)
4311 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4313 TPACPI_ACPIHANDLE_INIT(beep);
4315 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4316 str_supported(beep_handle != NULL));
4318 return (beep_handle)? 0 : 1;
4321 static int beep_read(char *p)
4326 len += sprintf(p + len, "status:\t\tnot supported\n");
4328 len += sprintf(p + len, "status:\t\tsupported\n");
4329 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4335 static int beep_write(char *buf)
4343 while ((cmd = next_cmd(&buf))) {
4344 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4345 beep_cmd >= 0 && beep_cmd <= 17) {
4349 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4356 static struct ibm_struct beep_driver_data = {
4359 .write = beep_write,
4362 /*************************************************************************
4366 enum thermal_access_mode {
4367 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4368 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4369 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4370 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4371 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4374 enum { /* TPACPI_THERMAL_TPEC_* */
4375 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4376 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4377 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4380 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4381 struct ibm_thermal_sensors_struct {
4382 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4385 static enum thermal_access_mode thermal_read_mode;
4387 /* idx is zero-based */
4388 static int thermal_get_sensor(int idx, s32 *value)
4394 t = TP_EC_THERMAL_TMP0;
4396 switch (thermal_read_mode) {
4397 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4398 case TPACPI_THERMAL_TPEC_16:
4399 if (idx >= 8 && idx <= 15) {
4400 t = TP_EC_THERMAL_TMP8;
4405 case TPACPI_THERMAL_TPEC_8:
4407 if (!acpi_ec_read(t + idx, &tmp))
4409 *value = tmp * 1000;
4414 case TPACPI_THERMAL_ACPI_UPDT:
4416 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4417 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4419 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4421 *value = (t - 2732) * 100;
4426 case TPACPI_THERMAL_ACPI_TMP07:
4428 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4429 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4431 if (t > 127 || t < -127)
4432 t = TP_EC_THERMAL_TMP_NA;
4438 case TPACPI_THERMAL_NONE:
4446 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4457 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4460 for (i = 0 ; i < n; i++) {
4461 res = thermal_get_sensor(i, &s->temp[i]);
4469 /* sysfs temp##_input -------------------------------------------------- */
4471 static ssize_t thermal_temp_input_show(struct device *dev,
4472 struct device_attribute *attr,
4475 struct sensor_device_attribute *sensor_attr =
4476 to_sensor_dev_attr(attr);
4477 int idx = sensor_attr->index;
4481 res = thermal_get_sensor(idx, &value);
4484 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4487 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4490 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4491 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4492 thermal_temp_input_show, NULL, _idxB)
4494 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4495 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4496 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4497 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4498 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4499 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4500 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4501 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4502 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4503 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4504 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4505 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4506 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4507 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4508 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4509 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4510 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4513 #define THERMAL_ATTRS(X) \
4514 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4516 static struct attribute *thermal_temp_input_attr[] = {
4536 static const struct attribute_group thermal_temp_input16_group = {
4537 .attrs = thermal_temp_input_attr
4540 static const struct attribute_group thermal_temp_input8_group = {
4541 .attrs = &thermal_temp_input_attr[8]
4544 #undef THERMAL_SENSOR_ATTR_TEMP
4545 #undef THERMAL_ATTRS
4547 /* --------------------------------------------------------------------- */
4549 static int __init thermal_init(struct ibm_init_struct *iibm)
4556 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4558 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4560 if (thinkpad_id.ec_model) {
4562 * Direct EC access mode: sensors at registers
4563 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4564 * non-implemented, thermal sensors return 0x80 when
4569 for (i = 0; i < 8; i++) {
4570 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4576 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4584 /* This is sheer paranoia, but we handle it anyway */
4587 "ThinkPad ACPI EC access misbehaving, "
4588 "falling back to ACPI TMPx access "
4590 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4593 "ThinkPad ACPI EC access misbehaving, "
4594 "disabling thermal sensors access\n");
4595 thermal_read_mode = TPACPI_THERMAL_NONE;
4600 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4602 } else if (acpi_tmp7) {
4603 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4604 /* 600e/x, 770e, 770x */
4605 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4607 /* Standard ACPI TMPx access, max 8 sensors */
4608 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4611 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4612 thermal_read_mode = TPACPI_THERMAL_NONE;
4615 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4616 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4619 switch (thermal_read_mode) {
4620 case TPACPI_THERMAL_TPEC_16:
4621 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4622 &thermal_temp_input16_group);
4626 case TPACPI_THERMAL_TPEC_8:
4627 case TPACPI_THERMAL_ACPI_TMP07:
4628 case TPACPI_THERMAL_ACPI_UPDT:
4629 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4630 &thermal_temp_input8_group);
4634 case TPACPI_THERMAL_NONE:
4642 static void thermal_exit(void)
4644 switch (thermal_read_mode) {
4645 case TPACPI_THERMAL_TPEC_16:
4646 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4647 &thermal_temp_input16_group);
4649 case TPACPI_THERMAL_TPEC_8:
4650 case TPACPI_THERMAL_ACPI_TMP07:
4651 case TPACPI_THERMAL_ACPI_UPDT:
4652 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4653 &thermal_temp_input16_group);
4655 case TPACPI_THERMAL_NONE:
4661 static int thermal_read(char *p)
4665 struct ibm_thermal_sensors_struct t;
4667 n = thermal_get_sensors(&t);
4668 if (unlikely(n < 0))
4671 len += sprintf(p + len, "temperatures:\t");
4674 for (i = 0; i < (n - 1); i++)
4675 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4676 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4678 len += sprintf(p + len, "not supported\n");
4683 static struct ibm_struct thermal_driver_data = {
4685 .read = thermal_read,
4686 .exit = thermal_exit,
4689 /*************************************************************************
4693 static u8 ecdump_regs[256];
4695 static int ecdump_read(char *p)
4701 len += sprintf(p + len, "EC "
4702 " +00 +01 +02 +03 +04 +05 +06 +07"
4703 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4704 for (i = 0; i < 256; i += 16) {
4705 len += sprintf(p + len, "EC 0x%02x:", i);
4706 for (j = 0; j < 16; j++) {
4707 if (!acpi_ec_read(i + j, &v))
4709 if (v != ecdump_regs[i + j])
4710 len += sprintf(p + len, " *%02x", v);
4712 len += sprintf(p + len, " %02x", v);
4713 ecdump_regs[i + j] = v;
4715 len += sprintf(p + len, "\n");
4720 /* These are way too dangerous to advertise openly... */
4722 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4723 " (<offset> is 00-ff, <value> is 00-ff)\n");
4724 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4725 " (<offset> is 00-ff, <value> is 0-255)\n");
4730 static int ecdump_write(char *buf)
4735 while ((cmd = next_cmd(&buf))) {
4736 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4738 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4742 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4743 if (!acpi_ec_write(i, v))
4752 static struct ibm_struct ecdump_driver_data = {
4754 .read = ecdump_read,
4755 .write = ecdump_write,
4756 .flags.experimental = 1,
4759 /*************************************************************************
4760 * Backlight/brightness subdriver
4763 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4766 TP_EC_BACKLIGHT = 0x31,
4768 /* TP_EC_BACKLIGHT bitmasks */
4769 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4770 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4771 TP_EC_BACKLIGHT_MAPSW = 0x20,
4774 static struct backlight_device *ibm_backlight_device;
4775 static int brightness_mode;
4776 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4778 static struct mutex brightness_mutex;
4781 * ThinkPads can read brightness from two places: EC 0x31, or
4782 * CMOS NVRAM byte 0x5E, bits 0-3.
4784 * EC 0x31 has the following layout
4785 * Bit 7: unknown function
4786 * Bit 6: unknown function
4787 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4788 * Bit 4: must be set to zero to avoid problems
4789 * Bit 3-0: backlight brightness level
4791 * brightness_get_raw returns status data in the EC 0x31 layout
4793 static int brightness_get_raw(int *status)
4795 u8 lec = 0, lcmos = 0, level = 0;
4797 if (brightness_mode & 1) {
4798 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4800 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4802 if (brightness_mode & 2) {
4803 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4804 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4805 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4806 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4810 if (brightness_mode == 3) {
4811 *status = lec; /* Prefer EC, CMOS is just a backing store */
4812 lec &= TP_EC_BACKLIGHT_LVLMSK;
4814 tp_warned.bright_cmos_ec_unsync = 0;
4816 if (!tp_warned.bright_cmos_ec_unsync) {
4818 "CMOS NVRAM (%u) and EC (%u) do not "
4819 "agree on display brightness level\n",
4820 (unsigned int) lcmos,
4821 (unsigned int) lec);
4822 tp_warned.bright_cmos_ec_unsync = 1;
4833 /* May return EINTR which can always be mapped to ERESTARTSYS */
4834 static int brightness_set(int value)
4836 int cmos_cmd, inc, i, res;
4840 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4844 res = mutex_lock_interruptible(&brightness_mutex);
4848 res = brightness_get_raw(¤t_value);
4852 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4853 current_value &= TP_EC_BACKLIGHT_LVLMSK;
4855 cmos_cmd = value > current_value ?
4856 TP_CMOS_BRIGHTNESS_UP :
4857 TP_CMOS_BRIGHTNESS_DOWN;
4858 inc = (value > current_value)? 1 : -1;
4861 for (i = current_value; i != value; i += inc) {
4862 if ((brightness_mode & 2) &&
4863 issue_thinkpad_cmos_command(cmos_cmd)) {
4867 if ((brightness_mode & 1) &&
4868 !acpi_ec_write(TP_EC_BACKLIGHT,
4869 (i + inc) | command_bits)) {
4876 mutex_unlock(&brightness_mutex);
4880 /* sysfs backlight class ----------------------------------------------- */
4882 static int brightness_update_status(struct backlight_device *bd)
4884 /* it is the backlight class's job (caller) to handle
4885 * EINTR and other errors properly */
4886 return brightness_set(
4887 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4888 bd->props.power == FB_BLANK_UNBLANK) ?
4889 bd->props.brightness : 0);
4892 static int brightness_get(struct backlight_device *bd)
4896 res = brightness_get_raw(&status);
4898 return 0; /* FIXME: teach backlight about error handling */
4900 return status & TP_EC_BACKLIGHT_LVLMSK;
4903 static struct backlight_ops ibm_backlight_data = {
4904 .get_brightness = brightness_get,
4905 .update_status = brightness_update_status,
4908 /* --------------------------------------------------------------------- */
4910 static int __init brightness_init(struct ibm_init_struct *iibm)
4914 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4916 mutex_init(&brightness_mutex);
4919 * We always attempt to detect acpi support, so as to switch
4920 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4921 * going to publish a backlight interface
4923 b = tpacpi_check_std_acpi_brightness_support();
4925 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4926 printk(TPACPI_NOTICE
4927 "Lenovo BIOS switched to ACPI backlight "
4930 if (brightness_enable > 1) {
4931 printk(TPACPI_NOTICE
4932 "standard ACPI backlight interface "
4933 "available, not loading native one...\n");
4938 if (!brightness_enable) {
4939 dbg_printk(TPACPI_DBG_INIT,
4940 "brightness support disabled by "
4941 "module parameter\n");
4947 "Unsupported brightness interface, "
4948 "please contact %s\n", TPACPI_MAIL);
4952 tp_features.bright_16levels = 1;
4954 if (!brightness_mode) {
4955 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4956 brightness_mode = 2;
4958 brightness_mode = 3;
4960 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4964 if (brightness_mode > 3)
4967 if (brightness_get_raw(&b) < 0)
4970 if (tp_features.bright_16levels)
4972 "detected a 16-level brightness capable ThinkPad\n");
4974 ibm_backlight_device = backlight_device_register(
4975 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4976 &ibm_backlight_data);
4977 if (IS_ERR(ibm_backlight_device)) {
4978 printk(TPACPI_ERR "Could not register backlight device\n");
4979 return PTR_ERR(ibm_backlight_device);
4981 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4983 ibm_backlight_device->props.max_brightness =
4984 (tp_features.bright_16levels)? 15 : 7;
4985 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
4986 backlight_update_status(ibm_backlight_device);
4991 static void brightness_exit(void)
4993 if (ibm_backlight_device) {
4994 vdbg_printk(TPACPI_DBG_EXIT,
4995 "calling backlight_device_unregister()\n");
4996 backlight_device_unregister(ibm_backlight_device);
5000 static int brightness_read(char *p)
5005 level = brightness_get(NULL);
5007 len += sprintf(p + len, "level:\t\tunreadable\n");
5009 len += sprintf(p + len, "level:\t\t%d\n", level);
5010 len += sprintf(p + len, "commands:\tup, down\n");
5011 len += sprintf(p + len, "commands:\tlevel <level>"
5012 " (<level> is 0-%d)\n",
5013 (tp_features.bright_16levels) ? 15 : 7);
5019 static int brightness_write(char *buf)
5024 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5026 level = brightness_get(NULL);
5030 while ((cmd = next_cmd(&buf))) {
5031 if (strlencmp(cmd, "up") == 0) {
5032 if (level < max_level)
5034 } else if (strlencmp(cmd, "down") == 0) {
5037 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5038 level >= 0 && level <= max_level) {
5045 * Now we know what the final level should be, so we try to set it.
5046 * Doing it this way makes the syscall restartable in case of EINTR
5048 rc = brightness_set(level);
5049 return (rc == -EINTR)? ERESTARTSYS : rc;
5052 static struct ibm_struct brightness_driver_data = {
5053 .name = "brightness",
5054 .read = brightness_read,
5055 .write = brightness_write,
5056 .exit = brightness_exit,
5059 /*************************************************************************
5063 static int volume_offset = 0x30;
5065 static int volume_read(char *p)
5070 if (!acpi_ec_read(volume_offset, &level)) {
5071 len += sprintf(p + len, "level:\t\tunreadable\n");
5073 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5074 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5075 len += sprintf(p + len, "commands:\tup, down, mute\n");
5076 len += sprintf(p + len, "commands:\tlevel <level>"
5077 " (<level> is 0-15)\n");
5083 static int volume_write(char *buf)
5085 int cmos_cmd, inc, i;
5087 int new_level, new_mute;
5090 while ((cmd = next_cmd(&buf))) {
5091 if (!acpi_ec_read(volume_offset, &level))
5093 new_mute = mute = level & 0x40;
5094 new_level = level = level & 0xf;
5096 if (strlencmp(cmd, "up") == 0) {
5100 new_level = level == 15 ? 15 : level + 1;
5101 } else if (strlencmp(cmd, "down") == 0) {
5105 new_level = level == 0 ? 0 : level - 1;
5106 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5107 new_level >= 0 && new_level <= 15) {
5109 } else if (strlencmp(cmd, "mute") == 0) {
5114 if (new_level != level) {
5115 /* mute doesn't change */
5117 cmos_cmd = (new_level > level) ?
5118 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5119 inc = new_level > level ? 1 : -1;
5121 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5122 !acpi_ec_write(volume_offset, level)))
5125 for (i = level; i != new_level; i += inc)
5126 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5127 !acpi_ec_write(volume_offset, i + inc))
5131 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5132 !acpi_ec_write(volume_offset, new_level + mute))) {
5137 if (new_mute != mute) {
5138 /* level doesn't change */
5140 cmos_cmd = (new_mute) ?
5141 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5143 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5144 !acpi_ec_write(volume_offset, level + new_mute))
5152 static struct ibm_struct volume_driver_data = {
5154 .read = volume_read,
5155 .write = volume_write,
5158 /*************************************************************************
5165 * TPACPI_FAN_RD_ACPI_GFAN:
5166 * ACPI GFAN method: returns fan level
5168 * see TPACPI_FAN_WR_ACPI_SFAN
5169 * EC 0x2f (HFSP) not available if GFAN exists
5171 * TPACPI_FAN_WR_ACPI_SFAN:
5172 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5174 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5177 * TPACPI_FAN_WR_TPEC:
5178 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5179 * Supported on almost all ThinkPads
5181 * Fan speed changes of any sort (including those caused by the
5182 * disengaged mode) are usually done slowly by the firmware as the
5183 * maximum ammount of fan duty cycle change per second seems to be
5186 * Reading is not available if GFAN exists.
5187 * Writing is not available if SFAN exists.
5190 * 7 automatic mode engaged;
5191 * (default operation mode of the ThinkPad)
5192 * fan level is ignored in this mode.
5193 * 6 full speed mode (takes precedence over bit 7);
5194 * not available on all thinkpads. May disable
5195 * the tachometer while the fan controller ramps up
5196 * the speed (which can take up to a few *minutes*).
5197 * Speeds up fan to 100% duty-cycle, which is far above
5198 * the standard RPM levels. It is not impossible that
5199 * it could cause hardware damage.
5200 * 5-3 unused in some models. Extra bits for fan level
5201 * in others, but still useless as all values above
5202 * 7 map to the same speed as level 7 in these models.
5203 * 2-0 fan level (0..7 usually)
5205 * 0x07 = max (set when temperatures critical)
5206 * Some ThinkPads may have other levels, see
5207 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5209 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5210 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5211 * does so, its initial value is meaningless (0x07).
5213 * For firmware bugs, refer to:
5214 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5218 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5219 * Main fan tachometer reading (in RPM)
5221 * This register is present on all ThinkPads with a new-style EC, and
5222 * it is known not to be present on the A21m/e, and T22, as there is
5223 * something else in offset 0x84 according to the ACPI DSDT. Other
5224 * ThinkPads from this same time period (and earlier) probably lack the
5225 * tachometer as well.
5227 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5228 * was never fixed by IBM to report the EC firmware version string
5229 * probably support the tachometer (like the early X models), so
5230 * detecting it is quite hard. We need more data to know for sure.
5232 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5235 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5238 * For firmware bugs, refer to:
5239 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5241 * TPACPI_FAN_WR_ACPI_FANS:
5242 * ThinkPad X31, X40, X41. Not available in the X60.
5244 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5245 * high speed. ACPI DSDT seems to map these three speeds to levels
5246 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5247 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5249 * The speeds are stored on handles
5250 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5252 * There are three default speed sets, acessible as handles:
5253 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5255 * ACPI DSDT switches which set is in use depending on various
5258 * TPACPI_FAN_WR_TPEC is also available and should be used to
5259 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5260 * but the ACPI tables just mention level 7.
5263 enum { /* Fan control constants */
5264 fan_status_offset = 0x2f, /* EC register 0x2f */
5265 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5266 * 0x84 must be read before 0x85 */
5268 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5269 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5271 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5274 enum fan_status_access_mode {
5275 TPACPI_FAN_NONE = 0, /* No fan status or control */
5276 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5277 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5280 enum fan_control_access_mode {
5281 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5282 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5283 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5284 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5287 enum fan_control_commands {
5288 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5289 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5290 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5291 * and also watchdog cmd */
5294 static int fan_control_allowed;
5296 static enum fan_status_access_mode fan_status_access_mode;
5297 static enum fan_control_access_mode fan_control_access_mode;
5298 static enum fan_control_commands fan_control_commands;
5300 static u8 fan_control_initial_status;
5301 static u8 fan_control_desired_level;
5302 static int fan_watchdog_maxinterval;
5304 static struct mutex fan_mutex;
5306 static void fan_watchdog_fire(struct work_struct *ignored);
5307 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5309 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5310 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5311 "\\FSPD", /* 600e/x, 770e, 770x */
5313 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5314 "JFNS", /* 770x-JL */
5318 * Call with fan_mutex held
5320 static void fan_update_desired_level(u8 status)
5323 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5325 fan_control_desired_level = 7;
5327 fan_control_desired_level = status;
5331 static int fan_get_status(u8 *status)
5336 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5338 switch (fan_status_access_mode) {
5339 case TPACPI_FAN_RD_ACPI_GFAN:
5340 /* 570, 600e/x, 770e, 770x */
5342 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5350 case TPACPI_FAN_RD_TPEC:
5351 /* all except 570, 600e/x, 770e, 770x */
5352 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5367 static int fan_get_status_safe(u8 *status)
5372 if (mutex_lock_interruptible(&fan_mutex))
5373 return -ERESTARTSYS;
5374 rc = fan_get_status(&s);
5376 fan_update_desired_level(s);
5377 mutex_unlock(&fan_mutex);
5385 static int fan_get_speed(unsigned int *speed)
5389 switch (fan_status_access_mode) {
5390 case TPACPI_FAN_RD_TPEC:
5391 /* all except 570, 600e/x, 770e, 770x */
5392 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5393 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5397 *speed = (hi << 8) | lo;
5408 static int fan_set_level(int level)
5410 if (!fan_control_allowed)
5413 switch (fan_control_access_mode) {
5414 case TPACPI_FAN_WR_ACPI_SFAN:
5415 if (level >= 0 && level <= 7) {
5416 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5422 case TPACPI_FAN_WR_ACPI_FANS:
5423 case TPACPI_FAN_WR_TPEC:
5424 if ((level != TP_EC_FAN_AUTO) &&
5425 (level != TP_EC_FAN_FULLSPEED) &&
5426 ((level < 0) || (level > 7)))
5429 /* safety net should the EC not support AUTO
5430 * or FULLSPEED mode bits and just ignore them */
5431 if (level & TP_EC_FAN_FULLSPEED)
5432 level |= 7; /* safety min speed 7 */
5433 else if (level & TP_EC_FAN_AUTO)
5434 level |= 4; /* safety min speed 4 */
5436 if (!acpi_ec_write(fan_status_offset, level))
5439 tp_features.fan_ctrl_status_undef = 0;
5448 static int fan_set_level_safe(int level)
5452 if (!fan_control_allowed)
5455 if (mutex_lock_interruptible(&fan_mutex))
5456 return -ERESTARTSYS;
5458 if (level == TPACPI_FAN_LAST_LEVEL)
5459 level = fan_control_desired_level;
5461 rc = fan_set_level(level);
5463 fan_update_desired_level(level);
5465 mutex_unlock(&fan_mutex);
5469 static int fan_set_enable(void)
5474 if (!fan_control_allowed)
5477 if (mutex_lock_interruptible(&fan_mutex))
5478 return -ERESTARTSYS;
5480 switch (fan_control_access_mode) {
5481 case TPACPI_FAN_WR_ACPI_FANS:
5482 case TPACPI_FAN_WR_TPEC:
5483 rc = fan_get_status(&s);
5487 /* Don't go out of emergency fan mode */
5490 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5493 if (!acpi_ec_write(fan_status_offset, s))
5496 tp_features.fan_ctrl_status_undef = 0;
5501 case TPACPI_FAN_WR_ACPI_SFAN:
5502 rc = fan_get_status(&s);
5508 /* Set fan to at least level 4 */
5511 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5521 mutex_unlock(&fan_mutex);
5525 static int fan_set_disable(void)
5529 if (!fan_control_allowed)
5532 if (mutex_lock_interruptible(&fan_mutex))
5533 return -ERESTARTSYS;
5536 switch (fan_control_access_mode) {
5537 case TPACPI_FAN_WR_ACPI_FANS:
5538 case TPACPI_FAN_WR_TPEC:
5539 if (!acpi_ec_write(fan_status_offset, 0x00))
5542 fan_control_desired_level = 0;
5543 tp_features.fan_ctrl_status_undef = 0;
5547 case TPACPI_FAN_WR_ACPI_SFAN:
5548 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5551 fan_control_desired_level = 0;
5559 mutex_unlock(&fan_mutex);
5563 static int fan_set_speed(int speed)
5567 if (!fan_control_allowed)
5570 if (mutex_lock_interruptible(&fan_mutex))
5571 return -ERESTARTSYS;
5574 switch (fan_control_access_mode) {
5575 case TPACPI_FAN_WR_ACPI_FANS:
5576 if (speed >= 0 && speed <= 65535) {
5577 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5578 speed, speed, speed))
5588 mutex_unlock(&fan_mutex);
5592 static void fan_watchdog_reset(void)
5594 static int fan_watchdog_active;
5596 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5599 if (fan_watchdog_active)
5600 cancel_delayed_work(&fan_watchdog_task);
5602 if (fan_watchdog_maxinterval > 0 &&
5603 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5604 fan_watchdog_active = 1;
5605 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5606 msecs_to_jiffies(fan_watchdog_maxinterval
5609 "failed to queue the fan watchdog, "
5610 "watchdog will not trigger\n");
5613 fan_watchdog_active = 0;
5616 static void fan_watchdog_fire(struct work_struct *ignored)
5620 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5623 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5624 rc = fan_set_enable();
5626 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5627 "will try again later...\n", -rc);
5628 /* reschedule for later */
5629 fan_watchdog_reset();
5634 * SYSFS fan layout: hwmon compatible (device)
5637 * 0: "disengaged" mode
5639 * 2: native EC "auto" mode (recommended, hardware default)
5641 * pwm*: set speed in manual mode, ignored otherwise.
5642 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5645 * fan*_input: tachometer reading, RPM
5648 * SYSFS fan layout: extensions
5650 * fan_watchdog (driver):
5651 * fan watchdog interval in seconds, 0 disables (default), max 120
5654 /* sysfs fan pwm1_enable ----------------------------------------------- */
5655 static ssize_t fan_pwm1_enable_show(struct device *dev,
5656 struct device_attribute *attr,
5662 res = fan_get_status_safe(&status);
5666 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5667 if (status != fan_control_initial_status) {
5668 tp_features.fan_ctrl_status_undef = 0;
5670 /* Return most likely status. In fact, it
5671 * might be the only possible status */
5672 status = TP_EC_FAN_AUTO;
5676 if (status & TP_EC_FAN_FULLSPEED) {
5678 } else if (status & TP_EC_FAN_AUTO) {
5683 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5686 static ssize_t fan_pwm1_enable_store(struct device *dev,
5687 struct device_attribute *attr,
5688 const char *buf, size_t count)
5693 if (parse_strtoul(buf, 2, &t))
5698 level = TP_EC_FAN_FULLSPEED;
5701 level = TPACPI_FAN_LAST_LEVEL;
5704 level = TP_EC_FAN_AUTO;
5707 /* reserved for software-controlled auto mode */
5713 res = fan_set_level_safe(level);
5719 fan_watchdog_reset();
5724 static struct device_attribute dev_attr_fan_pwm1_enable =
5725 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5726 fan_pwm1_enable_show, fan_pwm1_enable_store);
5728 /* sysfs fan pwm1 ------------------------------------------------------ */
5729 static ssize_t fan_pwm1_show(struct device *dev,
5730 struct device_attribute *attr,
5736 res = fan_get_status_safe(&status);
5740 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5741 if (status != fan_control_initial_status) {
5742 tp_features.fan_ctrl_status_undef = 0;
5744 status = TP_EC_FAN_AUTO;
5749 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5750 status = fan_control_desired_level;
5755 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5758 static ssize_t fan_pwm1_store(struct device *dev,
5759 struct device_attribute *attr,
5760 const char *buf, size_t count)
5764 u8 status, newlevel;
5766 if (parse_strtoul(buf, 255, &s))
5769 /* scale down from 0-255 to 0-7 */
5770 newlevel = (s >> 5) & 0x07;
5772 if (mutex_lock_interruptible(&fan_mutex))
5773 return -ERESTARTSYS;
5775 rc = fan_get_status(&status);
5776 if (!rc && (status &
5777 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5778 rc = fan_set_level(newlevel);
5782 fan_update_desired_level(newlevel);
5783 fan_watchdog_reset();
5787 mutex_unlock(&fan_mutex);
5788 return (rc)? rc : count;
5791 static struct device_attribute dev_attr_fan_pwm1 =
5792 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5793 fan_pwm1_show, fan_pwm1_store);
5795 /* sysfs fan fan1_input ------------------------------------------------ */
5796 static ssize_t fan_fan1_input_show(struct device *dev,
5797 struct device_attribute *attr,
5803 res = fan_get_speed(&speed);
5807 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5810 static struct device_attribute dev_attr_fan_fan1_input =
5811 __ATTR(fan1_input, S_IRUGO,
5812 fan_fan1_input_show, NULL);
5814 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5815 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5818 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5821 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5822 const char *buf, size_t count)
5826 if (parse_strtoul(buf, 120, &t))
5829 if (!fan_control_allowed)
5832 fan_watchdog_maxinterval = t;
5833 fan_watchdog_reset();
5838 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5839 fan_fan_watchdog_show, fan_fan_watchdog_store);
5841 /* --------------------------------------------------------------------- */
5842 static struct attribute *fan_attributes[] = {
5843 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5844 &dev_attr_fan_fan1_input.attr,
5848 static const struct attribute_group fan_attr_group = {
5849 .attrs = fan_attributes,
5852 static int __init fan_init(struct ibm_init_struct *iibm)
5856 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5858 mutex_init(&fan_mutex);
5859 fan_status_access_mode = TPACPI_FAN_NONE;
5860 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5861 fan_control_commands = 0;
5862 fan_watchdog_maxinterval = 0;
5863 tp_features.fan_ctrl_status_undef = 0;
5864 fan_control_desired_level = 7;
5866 TPACPI_ACPIHANDLE_INIT(fans);
5867 TPACPI_ACPIHANDLE_INIT(gfan);
5868 TPACPI_ACPIHANDLE_INIT(sfan);
5871 /* 570, 600e/x, 770e, 770x */
5872 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5874 /* all other ThinkPads: note that even old-style
5875 * ThinkPad ECs supports the fan control register */
5876 if (likely(acpi_ec_read(fan_status_offset,
5877 &fan_control_initial_status))) {
5878 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5880 /* In some ThinkPads, neither the EC nor the ACPI
5881 * DSDT initialize the fan status, and it ends up
5882 * being set to 0x07 when it *could* be either
5885 * Enable for TP-1Y (T43), TP-78 (R51e),
5886 * TP-76 (R52), TP-70 (T43, R52), which are known
5888 if (fan_control_initial_status == 0x07) {
5889 switch (thinkpad_id.ec_model) {
5890 case 0x5931: /* TP-1Y */
5891 case 0x3837: /* TP-78 */
5892 case 0x3637: /* TP-76 */
5893 case 0x3037: /* TP-70 */
5894 printk(TPACPI_NOTICE
5895 "fan_init: initial fan status "
5896 "is unknown, assuming it is "
5898 tp_features.fan_ctrl_status_undef = 1;
5904 "ThinkPad ACPI EC access misbehaving, "
5905 "fan status and control unavailable\n");
5912 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5913 fan_control_commands |=
5914 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5917 /* gfan without sfan means no fan control */
5918 /* all other models implement TP EC 0x2f control */
5922 fan_control_access_mode =
5923 TPACPI_FAN_WR_ACPI_FANS;
5924 fan_control_commands |=
5925 TPACPI_FAN_CMD_SPEED |
5926 TPACPI_FAN_CMD_LEVEL |
5927 TPACPI_FAN_CMD_ENABLE;
5929 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5930 fan_control_commands |=
5931 TPACPI_FAN_CMD_LEVEL |
5932 TPACPI_FAN_CMD_ENABLE;
5937 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5938 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5939 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5940 fan_status_access_mode, fan_control_access_mode);
5942 /* fan control master switch */
5943 if (!fan_control_allowed) {
5944 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5945 fan_control_commands = 0;
5946 dbg_printk(TPACPI_DBG_INIT,
5947 "fan control features disabled by parameter\n");
5950 /* update fan_control_desired_level */
5951 if (fan_status_access_mode != TPACPI_FAN_NONE)
5952 fan_get_status_safe(NULL);
5954 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5955 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5956 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5961 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5962 &driver_attr_fan_watchdog);
5964 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5973 static void fan_exit(void)
5975 vdbg_printk(TPACPI_DBG_EXIT,
5976 "cancelling any pending fan watchdog tasks\n");
5978 /* FIXME: can we really do this unconditionally? */
5979 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5980 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5981 &driver_attr_fan_watchdog);
5983 cancel_delayed_work(&fan_watchdog_task);
5984 flush_workqueue(tpacpi_wq);
5987 static void fan_suspend(pm_message_t state)
5989 if (!fan_control_allowed)
5992 /* Store fan status in cache */
5993 fan_get_status_safe(NULL);
5994 if (tp_features.fan_ctrl_status_undef)
5995 fan_control_desired_level = TP_EC_FAN_AUTO;
5998 static void fan_resume(void)
6001 u8 current_level = 7;
6002 bool do_set = false;
6004 /* DSDT *always* updates status on resume */
6005 tp_features.fan_ctrl_status_undef = 0;
6007 saved_fan_level = fan_control_desired_level;
6008 if (!fan_control_allowed ||
6009 (fan_get_status_safe(¤t_level) < 0))
6012 switch (fan_control_access_mode) {
6013 case TPACPI_FAN_WR_ACPI_SFAN:
6014 do_set = (saved_fan_level > current_level);
6016 case TPACPI_FAN_WR_ACPI_FANS:
6017 case TPACPI_FAN_WR_TPEC:
6018 do_set = ((saved_fan_level & TP_EC_FAN_FULLSPEED) ||
6019 (saved_fan_level == 7 &&
6020 !(current_level & TP_EC_FAN_FULLSPEED)));
6026 printk(TPACPI_NOTICE
6027 "restoring fan level to 0x%02x\n",
6029 fan_set_level_safe(saved_fan_level);
6033 static int fan_read(char *p)
6038 unsigned int speed = 0;
6040 switch (fan_status_access_mode) {
6041 case TPACPI_FAN_RD_ACPI_GFAN:
6042 /* 570, 600e/x, 770e, 770x */
6043 rc = fan_get_status_safe(&status);
6047 len += sprintf(p + len, "status:\t\t%s\n"
6049 (status != 0) ? "enabled" : "disabled", status);
6052 case TPACPI_FAN_RD_TPEC:
6053 /* all except 570, 600e/x, 770e, 770x */
6054 rc = fan_get_status_safe(&status);
6058 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6059 if (status != fan_control_initial_status)
6060 tp_features.fan_ctrl_status_undef = 0;
6062 /* Return most likely status. In fact, it
6063 * might be the only possible status */
6064 status = TP_EC_FAN_AUTO;
6067 len += sprintf(p + len, "status:\t\t%s\n",
6068 (status != 0) ? "enabled" : "disabled");
6070 rc = fan_get_speed(&speed);
6074 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6076 if (status & TP_EC_FAN_FULLSPEED)
6077 /* Disengaged mode takes precedence */
6078 len += sprintf(p + len, "level:\t\tdisengaged\n");
6079 else if (status & TP_EC_FAN_AUTO)
6080 len += sprintf(p + len, "level:\t\tauto\n");
6082 len += sprintf(p + len, "level:\t\t%d\n", status);
6085 case TPACPI_FAN_NONE:
6087 len += sprintf(p + len, "status:\t\tnot supported\n");
6090 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6091 len += sprintf(p + len, "commands:\tlevel <level>");
6093 switch (fan_control_access_mode) {
6094 case TPACPI_FAN_WR_ACPI_SFAN:
6095 len += sprintf(p + len, " (<level> is 0-7)\n");
6099 len += sprintf(p + len, " (<level> is 0-7, "
6100 "auto, disengaged, full-speed)\n");
6105 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6106 len += sprintf(p + len, "commands:\tenable, disable\n"
6107 "commands:\twatchdog <timeout> (<timeout> "
6108 "is 0 (off), 1-120 (seconds))\n");
6110 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6111 len += sprintf(p + len, "commands:\tspeed <speed>"
6112 " (<speed> is 0-65535)\n");
6117 static int fan_write_cmd_level(const char *cmd, int *rc)
6121 if (strlencmp(cmd, "level auto") == 0)
6122 level = TP_EC_FAN_AUTO;
6123 else if ((strlencmp(cmd, "level disengaged") == 0) |
6124 (strlencmp(cmd, "level full-speed") == 0))
6125 level = TP_EC_FAN_FULLSPEED;
6126 else if (sscanf(cmd, "level %d", &level) != 1)
6129 *rc = fan_set_level_safe(level);
6131 printk(TPACPI_ERR "level command accepted for unsupported "
6132 "access mode %d", fan_control_access_mode);
6137 static int fan_write_cmd_enable(const char *cmd, int *rc)
6139 if (strlencmp(cmd, "enable") != 0)
6142 *rc = fan_set_enable();
6144 printk(TPACPI_ERR "enable command accepted for unsupported "
6145 "access mode %d", fan_control_access_mode);
6150 static int fan_write_cmd_disable(const char *cmd, int *rc)
6152 if (strlencmp(cmd, "disable") != 0)
6155 *rc = fan_set_disable();
6157 printk(TPACPI_ERR "disable command accepted for unsupported "
6158 "access mode %d", fan_control_access_mode);
6163 static int fan_write_cmd_speed(const char *cmd, int *rc)
6168 * Support speed <low> <medium> <high> ? */
6170 if (sscanf(cmd, "speed %d", &speed) != 1)
6173 *rc = fan_set_speed(speed);
6175 printk(TPACPI_ERR "speed command accepted for unsupported "
6176 "access mode %d", fan_control_access_mode);
6181 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6185 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6188 if (interval < 0 || interval > 120)
6191 fan_watchdog_maxinterval = interval;
6196 static int fan_write(char *buf)
6201 while (!rc && (cmd = next_cmd(&buf))) {
6202 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6203 fan_write_cmd_level(cmd, &rc)) &&
6204 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6205 (fan_write_cmd_enable(cmd, &rc) ||
6206 fan_write_cmd_disable(cmd, &rc) ||
6207 fan_write_cmd_watchdog(cmd, &rc))) &&
6208 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6209 fan_write_cmd_speed(cmd, &rc))
6213 fan_watchdog_reset();
6219 static struct ibm_struct fan_driver_data = {
6224 .suspend = fan_suspend,
6225 .resume = fan_resume,
6228 /****************************************************************************
6229 ****************************************************************************
6233 ****************************************************************************
6234 ****************************************************************************/
6236 /* sysfs name ---------------------------------------------------------- */
6237 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6238 struct device_attribute *attr,
6241 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6244 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6245 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6247 /* --------------------------------------------------------------------- */
6250 static struct proc_dir_entry *proc_dir;
6253 * Module and infrastructure proble, init and exit handling
6256 static int force_load;
6258 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6259 static const char * __init str_supported(int is_supported)
6261 static char text_unsupported[] __initdata = "not supported";
6263 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6265 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6267 static void ibm_exit(struct ibm_struct *ibm)
6269 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6271 list_del_init(&ibm->all_drivers);
6273 if (ibm->flags.acpi_notify_installed) {
6274 dbg_printk(TPACPI_DBG_EXIT,
6275 "%s: acpi_remove_notify_handler\n", ibm->name);
6277 acpi_remove_notify_handler(*ibm->acpi->handle,
6279 dispatch_acpi_notify);
6280 ibm->flags.acpi_notify_installed = 0;
6281 ibm->flags.acpi_notify_installed = 0;
6284 if (ibm->flags.proc_created) {
6285 dbg_printk(TPACPI_DBG_EXIT,
6286 "%s: remove_proc_entry\n", ibm->name);
6287 remove_proc_entry(ibm->name, proc_dir);
6288 ibm->flags.proc_created = 0;
6291 if (ibm->flags.acpi_driver_registered) {
6292 dbg_printk(TPACPI_DBG_EXIT,
6293 "%s: acpi_bus_unregister_driver\n", ibm->name);
6295 acpi_bus_unregister_driver(ibm->acpi->driver);
6296 kfree(ibm->acpi->driver);
6297 ibm->acpi->driver = NULL;
6298 ibm->flags.acpi_driver_registered = 0;
6301 if (ibm->flags.init_called && ibm->exit) {
6303 ibm->flags.init_called = 0;
6306 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6309 static int __init ibm_init(struct ibm_init_struct *iibm)
6312 struct ibm_struct *ibm = iibm->data;
6313 struct proc_dir_entry *entry;
6315 BUG_ON(ibm == NULL);
6317 INIT_LIST_HEAD(&ibm->all_drivers);
6319 if (ibm->flags.experimental && !experimental)
6322 dbg_printk(TPACPI_DBG_INIT,
6323 "probing for %s\n", ibm->name);
6326 ret = iibm->init(iibm);
6328 return 0; /* probe failed */
6332 ibm->flags.init_called = 1;
6336 if (ibm->acpi->hid) {
6337 ret = register_tpacpi_subdriver(ibm);
6342 if (ibm->acpi->notify) {
6343 ret = setup_acpi_notify(ibm);
6344 if (ret == -ENODEV) {
6345 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6355 dbg_printk(TPACPI_DBG_INIT,
6356 "%s installed\n", ibm->name);
6359 entry = create_proc_entry(ibm->name,
6360 S_IFREG | S_IRUGO | S_IWUSR,
6363 printk(TPACPI_ERR "unable to create proc entry %s\n",
6368 entry->owner = THIS_MODULE;
6370 entry->read_proc = &dispatch_procfs_read;
6372 entry->write_proc = &dispatch_procfs_write;
6373 ibm->flags.proc_created = 1;
6376 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6381 dbg_printk(TPACPI_DBG_INIT,
6382 "%s: at error exit path with result %d\n",
6386 return (ret < 0)? ret : 0;
6391 /* returns 0 - probe ok, or < 0 - probe error.
6392 * Probe ok doesn't mean thinkpad found.
6393 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6394 static int __must_check __init get_thinkpad_model_data(
6395 struct thinkpad_id_data *tp)
6397 const struct dmi_device *dev = NULL;
6398 char ec_fw_string[18];
6404 memset(tp, 0, sizeof(*tp));
6406 if (dmi_name_in_vendors("IBM"))
6407 tp->vendor = PCI_VENDOR_ID_IBM;
6408 else if (dmi_name_in_vendors("LENOVO"))
6409 tp->vendor = PCI_VENDOR_ID_LENOVO;
6413 s = dmi_get_system_info(DMI_BIOS_VERSION);
6414 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6415 if (s && !tp->bios_version_str)
6417 if (!tp->bios_version_str)
6419 tp->bios_model = tp->bios_version_str[0]
6420 | (tp->bios_version_str[1] << 8);
6423 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6424 * X32 or newer, all Z series; Some models must have an
6425 * up-to-date BIOS or they will not be detected.
6427 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6429 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6430 if (sscanf(dev->name,
6431 "IBM ThinkPad Embedded Controller -[%17c",
6432 ec_fw_string) == 1) {
6433 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6434 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6436 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6437 if (!tp->ec_version_str)
6439 tp->ec_model = ec_fw_string[0]
6440 | (ec_fw_string[1] << 8);
6445 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6446 if (s && !strnicmp(s, "ThinkPad", 8)) {
6447 tp->model_str = kstrdup(s, GFP_KERNEL);
6452 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6453 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6454 if (s && !tp->nummodel_str)
6460 static int __init probe_for_thinkpad(void)
6468 * Non-ancient models have better DMI tagging, but very old models
6471 is_thinkpad = (thinkpad_id.model_str != NULL);
6473 /* ec is required because many other handles are relative to it */
6474 TPACPI_ACPIHANDLE_INIT(ec);
6478 "Not yet supported ThinkPad detected!\n");
6483 * Risks a regression on very old machines, but reduces potential
6484 * false positives a damn great deal
6487 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6489 if (!is_thinkpad && !force_load)
6496 /* Module init, exit, parameters */
6498 static struct ibm_init_struct ibms_init[] __initdata = {
6500 .init = thinkpad_acpi_driver_init,
6501 .data = &thinkpad_acpi_driver_data,
6504 .init = hotkey_init,
6505 .data = &hotkey_driver_data,
6508 .init = bluetooth_init,
6509 .data = &bluetooth_driver_data,
6513 .data = &wan_driver_data,
6515 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6518 .data = &video_driver_data,
6523 .data = &light_driver_data,
6525 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6528 .data = &dock_driver_data[0],
6532 .data = &dock_driver_data[1],
6535 #ifdef CONFIG_THINKPAD_ACPI_BAY
6538 .data = &bay_driver_data,
6543 .data = &cmos_driver_data,
6547 .data = &led_driver_data,
6551 .data = &beep_driver_data,
6554 .init = thermal_init,
6555 .data = &thermal_driver_data,
6558 .data = &ecdump_driver_data,
6561 .init = brightness_init,
6562 .data = &brightness_driver_data,
6565 .data = &volume_driver_data,
6569 .data = &fan_driver_data,
6573 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6576 struct ibm_struct *ibm;
6578 if (!kp || !kp->name || !val)
6581 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6582 ibm = ibms_init[i].data;
6583 WARN_ON(ibm == NULL);
6585 if (!ibm || !ibm->name)
6588 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6589 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6591 strcpy(ibms_init[i].param, val);
6592 strcat(ibms_init[i].param, ",");
6600 module_param(experimental, int, 0);
6601 MODULE_PARM_DESC(experimental,
6602 "Enables experimental features when non-zero");
6604 module_param_named(debug, dbg_level, uint, 0);
6605 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6607 module_param(force_load, bool, 0);
6608 MODULE_PARM_DESC(force_load,
6609 "Attempts to load the driver even on a "
6610 "mis-identified ThinkPad when true");
6612 module_param_named(fan_control, fan_control_allowed, bool, 0);
6613 MODULE_PARM_DESC(fan_control,
6614 "Enables setting fan parameters features when true");
6616 module_param_named(brightness_mode, brightness_mode, int, 0);
6617 MODULE_PARM_DESC(brightness_mode,
6618 "Selects brightness control strategy: "
6619 "0=auto, 1=EC, 2=CMOS, 3=both");
6621 module_param(brightness_enable, uint, 0);
6622 MODULE_PARM_DESC(brightness_enable,
6623 "Enables backlight control when 1, disables when 0");
6625 module_param(hotkey_report_mode, uint, 0);
6626 MODULE_PARM_DESC(hotkey_report_mode,
6627 "used for backwards compatibility with userspace, "
6628 "see documentation");
6630 #define TPACPI_PARAM(feature) \
6631 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6632 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6633 "at module load, see documentation")
6635 TPACPI_PARAM(hotkey);
6636 TPACPI_PARAM(bluetooth);
6637 TPACPI_PARAM(video);
6638 TPACPI_PARAM(light);
6639 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6642 #ifdef CONFIG_THINKPAD_ACPI_BAY
6644 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6648 TPACPI_PARAM(ecdump);
6649 TPACPI_PARAM(brightness);
6650 TPACPI_PARAM(volume);
6653 static void thinkpad_acpi_module_exit(void)
6655 struct ibm_struct *ibm, *itmp;
6657 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6659 list_for_each_entry_safe_reverse(ibm, itmp,
6660 &tpacpi_all_drivers,
6665 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6667 if (tpacpi_inputdev) {
6668 if (tp_features.input_device_registered)
6669 input_unregister_device(tpacpi_inputdev);
6671 input_free_device(tpacpi_inputdev);
6675 hwmon_device_unregister(tpacpi_hwmon);
6677 if (tp_features.sensors_pdev_attrs_registered)
6678 device_remove_file(&tpacpi_sensors_pdev->dev,
6679 &dev_attr_thinkpad_acpi_pdev_name);
6680 if (tpacpi_sensors_pdev)
6681 platform_device_unregister(tpacpi_sensors_pdev);
6683 platform_device_unregister(tpacpi_pdev);
6685 if (tp_features.sensors_pdrv_attrs_registered)
6686 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6687 if (tp_features.platform_drv_attrs_registered)
6688 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6690 if (tp_features.sensors_pdrv_registered)
6691 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6693 if (tp_features.platform_drv_registered)
6694 platform_driver_unregister(&tpacpi_pdriver);
6697 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6700 destroy_workqueue(tpacpi_wq);
6702 kfree(thinkpad_id.bios_version_str);
6703 kfree(thinkpad_id.ec_version_str);
6704 kfree(thinkpad_id.model_str);
6708 static int __init thinkpad_acpi_module_init(void)
6712 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6714 /* Parameter checking */
6715 if (hotkey_report_mode > 2)
6718 /* Driver-level probe */
6720 ret = get_thinkpad_model_data(&thinkpad_id);
6723 "unable to get DMI data: %d\n", ret);
6724 thinkpad_acpi_module_exit();
6727 ret = probe_for_thinkpad();
6729 thinkpad_acpi_module_exit();
6733 /* Driver initialization */
6735 TPACPI_ACPIHANDLE_INIT(ecrd);
6736 TPACPI_ACPIHANDLE_INIT(ecwr);
6738 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6740 thinkpad_acpi_module_exit();
6744 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6747 "unable to create proc dir " TPACPI_PROC_DIR);
6748 thinkpad_acpi_module_exit();
6751 proc_dir->owner = THIS_MODULE;
6753 ret = platform_driver_register(&tpacpi_pdriver);
6756 "unable to register main platform driver\n");
6757 thinkpad_acpi_module_exit();
6760 tp_features.platform_drv_registered = 1;
6762 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6765 "unable to register hwmon platform driver\n");
6766 thinkpad_acpi_module_exit();
6769 tp_features.sensors_pdrv_registered = 1;
6771 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6773 tp_features.platform_drv_attrs_registered = 1;
6774 ret = tpacpi_create_driver_attributes(
6775 &tpacpi_hwmon_pdriver.driver);
6779 "unable to create sysfs driver attributes\n");
6780 thinkpad_acpi_module_exit();
6783 tp_features.sensors_pdrv_attrs_registered = 1;
6786 /* Device initialization */
6787 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6789 if (IS_ERR(tpacpi_pdev)) {
6790 ret = PTR_ERR(tpacpi_pdev);
6792 printk(TPACPI_ERR "unable to register platform device\n");
6793 thinkpad_acpi_module_exit();
6796 tpacpi_sensors_pdev = platform_device_register_simple(
6797 TPACPI_HWMON_DRVR_NAME,
6799 if (IS_ERR(tpacpi_sensors_pdev)) {
6800 ret = PTR_ERR(tpacpi_sensors_pdev);
6801 tpacpi_sensors_pdev = NULL;
6803 "unable to register hwmon platform device\n");
6804 thinkpad_acpi_module_exit();
6807 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6808 &dev_attr_thinkpad_acpi_pdev_name);
6811 "unable to create sysfs hwmon device attributes\n");
6812 thinkpad_acpi_module_exit();
6815 tp_features.sensors_pdev_attrs_registered = 1;
6816 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6817 if (IS_ERR(tpacpi_hwmon)) {
6818 ret = PTR_ERR(tpacpi_hwmon);
6819 tpacpi_hwmon = NULL;
6820 printk(TPACPI_ERR "unable to register hwmon device\n");
6821 thinkpad_acpi_module_exit();
6824 mutex_init(&tpacpi_inputdev_send_mutex);
6825 tpacpi_inputdev = input_allocate_device();
6826 if (!tpacpi_inputdev) {
6827 printk(TPACPI_ERR "unable to allocate input device\n");
6828 thinkpad_acpi_module_exit();
6831 /* Prepare input device, but don't register */
6832 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6833 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6834 tpacpi_inputdev->id.bustype = BUS_HOST;
6835 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6836 thinkpad_id.vendor :
6838 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6839 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6841 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6842 ret = ibm_init(&ibms_init[i]);
6843 if (ret >= 0 && *ibms_init[i].param)
6844 ret = ibms_init[i].data->write(ibms_init[i].param);
6846 thinkpad_acpi_module_exit();
6850 ret = input_register_device(tpacpi_inputdev);
6852 printk(TPACPI_ERR "unable to register input device\n");
6853 thinkpad_acpi_module_exit();
6856 tp_features.input_device_registered = 1;
6859 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6863 /* Please remove this in year 2009 */
6864 MODULE_ALIAS("ibm_acpi");
6866 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6869 * DMI matching for module autoloading
6871 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6872 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6874 * Only models listed in thinkwiki will be supported, so add yours
6875 * if it is not there yet.
6877 #define IBM_BIOS_MODULE_ALIAS(__type) \
6878 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6880 /* Non-ancient thinkpads */
6881 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6882 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6884 /* Ancient thinkpad BIOSes have to be identified by
6885 * BIOS type or model number, and there are far less
6886 * BIOS types than model numbers... */
6887 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6888 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6889 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6891 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6892 MODULE_DESCRIPTION(TPACPI_DESC);
6893 MODULE_VERSION(TPACPI_VERSION);
6894 MODULE_LICENSE("GPL");
6896 module_init(thinkpad_acpi_module_init);
6897 module_exit(thinkpad_acpi_module_exit);