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) {
585 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
589 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
590 ibm->acpi->driver->ids = ibm->acpi->hid;
592 ibm->acpi->driver->ops.add = &tpacpi_device_add;
594 rc = acpi_bus_register_driver(ibm->acpi->driver);
596 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
598 kfree(ibm->acpi->driver);
599 ibm->acpi->driver = NULL;
601 ibm->flags.acpi_driver_registered = 1;
607 /****************************************************************************
608 ****************************************************************************
612 ****************************************************************************
613 ****************************************************************************/
615 static int dispatch_procfs_read(char *page, char **start, off_t off,
616 int count, int *eof, void *data)
618 struct ibm_struct *ibm = data;
621 if (!ibm || !ibm->read)
624 len = ibm->read(page);
628 if (len <= off + count)
640 static int dispatch_procfs_write(struct file *file,
641 const char __user *userbuf,
642 unsigned long count, void *data)
644 struct ibm_struct *ibm = data;
648 if (!ibm || !ibm->write)
651 kernbuf = kmalloc(count + 2, GFP_KERNEL);
655 if (copy_from_user(kernbuf, userbuf, count)) {
661 strcat(kernbuf, ",");
662 ret = ibm->write(kernbuf);
671 static char *next_cmd(char **cmds)
676 while ((end = strchr(start, ',')) && end == start)
688 /****************************************************************************
689 ****************************************************************************
691 * Device model: input, hwmon and platform
693 ****************************************************************************
694 ****************************************************************************/
696 static struct platform_device *tpacpi_pdev;
697 static struct platform_device *tpacpi_sensors_pdev;
698 static struct device *tpacpi_hwmon;
699 static struct input_dev *tpacpi_inputdev;
700 static struct mutex tpacpi_inputdev_send_mutex;
701 static LIST_HEAD(tpacpi_all_drivers);
703 static int tpacpi_suspend_handler(struct platform_device *pdev,
706 struct ibm_struct *ibm, *itmp;
708 list_for_each_entry_safe(ibm, itmp,
712 (ibm->suspend)(state);
718 static int tpacpi_resume_handler(struct platform_device *pdev)
720 struct ibm_struct *ibm, *itmp;
722 list_for_each_entry_safe(ibm, itmp,
732 static struct platform_driver tpacpi_pdriver = {
734 .name = TPACPI_DRVR_NAME,
735 .owner = THIS_MODULE,
737 .suspend = tpacpi_suspend_handler,
738 .resume = tpacpi_resume_handler,
741 static struct platform_driver tpacpi_hwmon_pdriver = {
743 .name = TPACPI_HWMON_DRVR_NAME,
744 .owner = THIS_MODULE,
748 /*************************************************************************
749 * sysfs support helpers
752 struct attribute_set {
753 unsigned int members, max_members;
754 struct attribute_group group;
757 struct attribute_set_obj {
758 struct attribute_set s;
760 } __attribute__((packed));
762 static struct attribute_set *create_attr_set(unsigned int max_members,
765 struct attribute_set_obj *sobj;
767 if (max_members == 0)
770 /* Allocates space for implicit NULL at the end too */
771 sobj = kzalloc(sizeof(struct attribute_set_obj) +
772 max_members * sizeof(struct attribute *),
776 sobj->s.max_members = max_members;
777 sobj->s.group.attrs = &sobj->a;
778 sobj->s.group.name = name;
783 #define destroy_attr_set(_set) \
786 /* not multi-threaded safe, use it in a single thread per set */
787 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
792 if (s->members >= s->max_members)
795 s->group.attrs[s->members] = attr;
801 static int add_many_to_attr_set(struct attribute_set *s,
802 struct attribute **attr,
807 for (i = 0; i < count; i++) {
808 res = add_to_attr_set(s, attr[i]);
816 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
818 sysfs_remove_group(kobj, &s->group);
822 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
823 sysfs_create_group(_kobj, &_attr_set->group)
825 static int parse_strtoul(const char *buf,
826 unsigned long max, unsigned long *value)
830 while (*buf && isspace(*buf))
832 *value = simple_strtoul(buf, &endp, 0);
833 while (*endp && isspace(*endp))
835 if (*endp || *value > max)
841 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
843 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
844 union acpi_object *obj;
847 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
848 obj = (union acpi_object *)buffer.pointer;
849 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
850 printk(TPACPI_ERR "Unknown _BCL data, "
851 "please report this to %s\n", TPACPI_MAIL);
854 rc = obj->package.count;
860 kfree(buffer.pointer);
864 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
865 u32 lvl, void *context, void **rv)
867 char name[ACPI_PATH_SEGMENT_LENGTH];
868 struct acpi_buffer buffer = { sizeof(name), &name };
870 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
871 !strncmp("_BCL", name, sizeof(name) - 1)) {
873 **(int **)rv = tpacpi_query_bcl_levels(handle);
874 return AE_CTRL_TERMINATE;
881 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
883 static int __init tpacpi_check_std_acpi_brightness_support(void)
887 void *bcl_ptr = &bcl_levels;
890 TPACPI_ACPIHANDLE_INIT(vid);
896 * Search for a _BCL method, and execute it. This is safe on all
897 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
898 * BIOS in ACPI backlight control mode. We do NOT have to care
899 * about calling the _BCL method in an enabled video device, any
900 * will do for our purposes.
903 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
904 tpacpi_acpi_walk_find_bcl, NULL,
907 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
908 tp_features.bright_acpimode = 1;
909 return (bcl_levels - 2);
915 static int __init tpacpi_new_rfkill(const unsigned int id,
917 const enum rfkill_type rfktype,
919 int (*toggle_radio)(void *, enum rfkill_state),
920 int (*get_state)(void *, enum rfkill_state *))
923 enum rfkill_state initial_state;
925 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
928 "failed to allocate memory for rfkill class\n");
933 (*rfk)->get_state = get_state;
934 (*rfk)->toggle_radio = toggle_radio;
936 if (!get_state(NULL, &initial_state))
937 (*rfk)->state = initial_state;
939 res = rfkill_register(*rfk);
942 "failed to register %s rfkill switch: %d\n",
952 /*************************************************************************
953 * thinkpad-acpi driver attributes
956 /* interface_version --------------------------------------------------- */
957 static ssize_t tpacpi_driver_interface_version_show(
958 struct device_driver *drv,
961 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
964 static DRIVER_ATTR(interface_version, S_IRUGO,
965 tpacpi_driver_interface_version_show, NULL);
967 /* debug_level --------------------------------------------------------- */
968 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
971 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
974 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
975 const char *buf, size_t count)
979 if (parse_strtoul(buf, 0xffff, &t))
987 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
988 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
990 /* version ------------------------------------------------------------- */
991 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
994 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
995 TPACPI_DESC, TPACPI_VERSION);
998 static DRIVER_ATTR(version, S_IRUGO,
999 tpacpi_driver_version_show, NULL);
1001 /* --------------------------------------------------------------------- */
1003 static struct driver_attribute *tpacpi_driver_attributes[] = {
1004 &driver_attr_debug_level, &driver_attr_version,
1005 &driver_attr_interface_version,
1008 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1014 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1015 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1022 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1026 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1027 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1030 /****************************************************************************
1031 ****************************************************************************
1035 ****************************************************************************
1036 ****************************************************************************/
1038 /*************************************************************************
1039 * thinkpad-acpi init subdriver
1042 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1044 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1045 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1047 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1048 (thinkpad_id.bios_version_str) ?
1049 thinkpad_id.bios_version_str : "unknown",
1050 (thinkpad_id.ec_version_str) ?
1051 thinkpad_id.ec_version_str : "unknown");
1053 if (thinkpad_id.vendor && thinkpad_id.model_str)
1054 printk(TPACPI_INFO "%s %s, model %s\n",
1055 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1056 "IBM" : ((thinkpad_id.vendor ==
1057 PCI_VENDOR_ID_LENOVO) ?
1058 "Lenovo" : "Unknown vendor"),
1059 thinkpad_id.model_str,
1060 (thinkpad_id.nummodel_str) ?
1061 thinkpad_id.nummodel_str : "unknown");
1066 static int thinkpad_acpi_driver_read(char *p)
1070 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1071 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1076 static struct ibm_struct thinkpad_acpi_driver_data = {
1078 .read = thinkpad_acpi_driver_read,
1081 /*************************************************************************
1085 enum { /* hot key scan codes (derived from ACPI DSDT) */
1086 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1087 TP_ACPI_HOTKEYSCAN_FNF2,
1088 TP_ACPI_HOTKEYSCAN_FNF3,
1089 TP_ACPI_HOTKEYSCAN_FNF4,
1090 TP_ACPI_HOTKEYSCAN_FNF5,
1091 TP_ACPI_HOTKEYSCAN_FNF6,
1092 TP_ACPI_HOTKEYSCAN_FNF7,
1093 TP_ACPI_HOTKEYSCAN_FNF8,
1094 TP_ACPI_HOTKEYSCAN_FNF9,
1095 TP_ACPI_HOTKEYSCAN_FNF10,
1096 TP_ACPI_HOTKEYSCAN_FNF11,
1097 TP_ACPI_HOTKEYSCAN_FNF12,
1098 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1099 TP_ACPI_HOTKEYSCAN_FNINSERT,
1100 TP_ACPI_HOTKEYSCAN_FNDELETE,
1101 TP_ACPI_HOTKEYSCAN_FNHOME,
1102 TP_ACPI_HOTKEYSCAN_FNEND,
1103 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1104 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1105 TP_ACPI_HOTKEYSCAN_FNSPACE,
1106 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1107 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1108 TP_ACPI_HOTKEYSCAN_MUTE,
1109 TP_ACPI_HOTKEYSCAN_THINKPAD,
1112 enum { /* Keys available through NVRAM polling */
1113 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1114 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1117 enum { /* Positions of some of the keys in hotkey masks */
1118 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1119 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1120 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1121 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1122 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1123 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1124 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1125 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1126 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1127 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1128 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1131 enum { /* NVRAM to ACPI HKEY group map */
1132 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1133 TP_ACPI_HKEY_ZOOM_MASK |
1134 TP_ACPI_HKEY_DISPSWTCH_MASK |
1135 TP_ACPI_HKEY_HIBERNATE_MASK,
1136 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1137 TP_ACPI_HKEY_BRGHTDWN_MASK,
1138 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1139 TP_ACPI_HKEY_VOLDWN_MASK |
1140 TP_ACPI_HKEY_MUTE_MASK,
1143 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1144 struct tp_nvram_state {
1145 u16 thinkpad_toggle:1;
1147 u16 display_toggle:1;
1148 u16 thinklight_toggle:1;
1149 u16 hibernate_toggle:1;
1150 u16 displayexp_toggle:1;
1151 u16 display_state:1;
1152 u16 brightness_toggle:1;
1153 u16 volume_toggle:1;
1156 u8 brightness_level;
1160 static struct task_struct *tpacpi_hotkey_task;
1161 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1162 static int hotkey_poll_freq = 10; /* Hz */
1163 static struct mutex hotkey_thread_mutex;
1164 static struct mutex hotkey_thread_data_mutex;
1165 static unsigned int hotkey_config_change;
1167 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1169 #define hotkey_source_mask 0U
1171 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1173 static struct mutex hotkey_mutex;
1175 static enum { /* Reasons for waking up */
1176 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1177 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1178 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1179 } hotkey_wakeup_reason;
1181 static int hotkey_autosleep_ack;
1183 static int hotkey_orig_status;
1184 static u32 hotkey_orig_mask;
1185 static u32 hotkey_all_mask;
1186 static u32 hotkey_reserved_mask;
1187 static u32 hotkey_mask;
1189 static unsigned int hotkey_report_mode;
1191 static u16 *hotkey_keycode_map;
1193 static struct attribute_set *hotkey_dev_attributes;
1195 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1196 #define HOTKEY_CONFIG_CRITICAL_START \
1198 mutex_lock(&hotkey_thread_data_mutex); \
1199 hotkey_config_change++; \
1201 #define HOTKEY_CONFIG_CRITICAL_END \
1202 mutex_unlock(&hotkey_thread_data_mutex);
1204 #define HOTKEY_CONFIG_CRITICAL_START
1205 #define HOTKEY_CONFIG_CRITICAL_END
1206 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1208 /* HKEY.MHKG() return bits */
1209 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1211 static int hotkey_get_wlsw(int *status)
1213 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1218 static int hotkey_get_tablet_mode(int *status)
1222 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1225 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1230 * Call with hotkey_mutex held
1232 static int hotkey_mask_get(void)
1236 if (tp_features.hotkey_mask) {
1237 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1240 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1246 * Call with hotkey_mutex held
1248 static int hotkey_mask_set(u32 mask)
1253 if (tp_features.hotkey_mask) {
1254 if (!tp_warned.hotkey_mask_ff &&
1255 (mask == 0xffff || mask == 0xffffff ||
1256 mask == 0xffffffff)) {
1257 tp_warned.hotkey_mask_ff = 1;
1258 printk(TPACPI_NOTICE
1259 "setting the hotkey mask to 0x%08x is likely "
1260 "not the best way to go about it\n", mask);
1261 printk(TPACPI_NOTICE
1262 "please consider using the driver defaults, "
1263 "and refer to up-to-date thinkpad-acpi "
1267 HOTKEY_CONFIG_CRITICAL_START
1268 for (i = 0; i < 32; i++) {
1270 /* enable in firmware mask only keys not in NVRAM
1271 * mode, but enable the key in the cached hotkey_mask
1272 * regardless of mode, or the key will end up
1273 * disabled by hotkey_mask_get() */
1274 if (!acpi_evalf(hkey_handle,
1275 NULL, "MHKM", "vdd", i + 1,
1276 !!((mask & ~hotkey_source_mask) & m))) {
1280 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1283 HOTKEY_CONFIG_CRITICAL_END
1285 /* hotkey_mask_get must be called unconditionally below */
1286 if (!hotkey_mask_get() && !rc &&
1287 (hotkey_mask & ~hotkey_source_mask) !=
1288 (mask & ~hotkey_source_mask)) {
1289 printk(TPACPI_NOTICE
1290 "requested hot key mask 0x%08x, but "
1291 "firmware forced it to 0x%08x\n",
1295 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1296 HOTKEY_CONFIG_CRITICAL_START
1297 hotkey_mask = mask & hotkey_source_mask;
1298 HOTKEY_CONFIG_CRITICAL_END
1300 if (hotkey_mask != mask) {
1301 printk(TPACPI_NOTICE
1302 "requested hot key mask 0x%08x, "
1303 "forced to 0x%08x (NVRAM poll mask is "
1304 "0x%08x): no firmware mask support\n",
1305 mask, hotkey_mask, hotkey_source_mask);
1310 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1316 static int hotkey_status_get(int *status)
1318 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1324 static int hotkey_status_set(int status)
1326 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1332 static void tpacpi_input_send_tabletsw(void)
1336 if (tp_features.hotkey_tablet &&
1337 !hotkey_get_tablet_mode(&state)) {
1338 mutex_lock(&tpacpi_inputdev_send_mutex);
1340 input_report_switch(tpacpi_inputdev,
1341 SW_TABLET_MODE, !!state);
1342 input_sync(tpacpi_inputdev);
1344 mutex_unlock(&tpacpi_inputdev_send_mutex);
1348 static void tpacpi_input_send_key(unsigned int scancode)
1350 unsigned int keycode;
1352 keycode = hotkey_keycode_map[scancode];
1354 if (keycode != KEY_RESERVED) {
1355 mutex_lock(&tpacpi_inputdev_send_mutex);
1357 input_report_key(tpacpi_inputdev, keycode, 1);
1358 if (keycode == KEY_UNKNOWN)
1359 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1361 input_sync(tpacpi_inputdev);
1363 input_report_key(tpacpi_inputdev, keycode, 0);
1364 if (keycode == KEY_UNKNOWN)
1365 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1367 input_sync(tpacpi_inputdev);
1369 mutex_unlock(&tpacpi_inputdev_send_mutex);
1373 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1374 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1376 static void tpacpi_hotkey_send_key(unsigned int scancode)
1378 tpacpi_input_send_key(scancode);
1379 if (hotkey_report_mode < 2) {
1380 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1381 0x80, 0x1001 + scancode);
1385 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1389 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1390 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1391 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1392 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1393 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1394 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1396 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1397 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1398 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1400 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1401 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1402 n->displayexp_toggle =
1403 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1405 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1406 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1407 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1408 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1409 n->brightness_toggle =
1410 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1412 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1413 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1414 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1415 >> TP_NVRAM_POS_LEVEL_VOLUME;
1416 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1417 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1421 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1423 if ((mask & (1 << __scancode)) && \
1424 oldn->__member != newn->__member) \
1425 tpacpi_hotkey_send_key(__scancode); \
1428 #define TPACPI_MAY_SEND_KEY(__scancode) \
1429 do { if (mask & (1 << __scancode)) \
1430 tpacpi_hotkey_send_key(__scancode); } while (0)
1432 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1433 struct tp_nvram_state *newn,
1436 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1437 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1438 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1439 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1441 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1443 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1446 if (oldn->volume_toggle != newn->volume_toggle) {
1447 if (oldn->mute != newn->mute) {
1448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1450 if (oldn->volume_level > newn->volume_level) {
1451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1452 } else if (oldn->volume_level < newn->volume_level) {
1453 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1454 } else if (oldn->mute == newn->mute) {
1455 /* repeated key presses that didn't change state */
1457 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1458 } else if (newn->volume_level != 0) {
1459 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1461 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1466 /* handle brightness */
1467 if (oldn->brightness_toggle != newn->brightness_toggle) {
1468 if (oldn->brightness_level < newn->brightness_level) {
1469 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1470 } else if (oldn->brightness_level > newn->brightness_level) {
1471 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1473 /* repeated key presses that didn't change state */
1474 if (newn->brightness_level != 0) {
1475 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1477 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1483 #undef TPACPI_COMPARE_KEY
1484 #undef TPACPI_MAY_SEND_KEY
1486 static int hotkey_kthread(void *data)
1488 struct tp_nvram_state s[2];
1490 unsigned int si, so;
1492 unsigned int change_detector, must_reset;
1494 mutex_lock(&hotkey_thread_mutex);
1496 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1505 /* Initial state for compares */
1506 mutex_lock(&hotkey_thread_data_mutex);
1507 change_detector = hotkey_config_change;
1508 mask = hotkey_source_mask & hotkey_mask;
1509 mutex_unlock(&hotkey_thread_data_mutex);
1510 hotkey_read_nvram(&s[so], mask);
1512 while (!kthread_should_stop() && hotkey_poll_freq) {
1514 t = 1000/hotkey_poll_freq;
1515 t = msleep_interruptible(t);
1516 if (unlikely(kthread_should_stop()))
1518 must_reset = try_to_freeze();
1519 if (t > 0 && !must_reset)
1522 mutex_lock(&hotkey_thread_data_mutex);
1523 if (must_reset || hotkey_config_change != change_detector) {
1524 /* forget old state on thaw or config change */
1527 change_detector = hotkey_config_change;
1529 mask = hotkey_source_mask & hotkey_mask;
1530 mutex_unlock(&hotkey_thread_data_mutex);
1533 hotkey_read_nvram(&s[si], mask);
1534 if (likely(si != so)) {
1535 hotkey_compare_and_issue_event(&s[so], &s[si],
1545 mutex_unlock(&hotkey_thread_mutex);
1549 static void hotkey_poll_stop_sync(void)
1551 if (tpacpi_hotkey_task) {
1552 if (frozen(tpacpi_hotkey_task) ||
1553 freezing(tpacpi_hotkey_task))
1554 thaw_process(tpacpi_hotkey_task);
1556 kthread_stop(tpacpi_hotkey_task);
1557 tpacpi_hotkey_task = NULL;
1558 mutex_lock(&hotkey_thread_mutex);
1559 /* at this point, the thread did exit */
1560 mutex_unlock(&hotkey_thread_mutex);
1564 /* call with hotkey_mutex held */
1565 static void hotkey_poll_setup(int may_warn)
1567 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1568 hotkey_poll_freq > 0 &&
1569 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1570 if (!tpacpi_hotkey_task) {
1571 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1572 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1573 if (IS_ERR(tpacpi_hotkey_task)) {
1574 tpacpi_hotkey_task = NULL;
1576 "could not create kernel thread "
1577 "for hotkey polling\n");
1581 hotkey_poll_stop_sync();
1583 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1584 printk(TPACPI_NOTICE
1585 "hot keys 0x%08x require polling, "
1586 "which is currently disabled\n",
1587 hotkey_source_mask);
1592 static void hotkey_poll_setup_safe(int may_warn)
1594 mutex_lock(&hotkey_mutex);
1595 hotkey_poll_setup(may_warn);
1596 mutex_unlock(&hotkey_mutex);
1599 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1601 static void hotkey_poll_setup_safe(int __unused)
1605 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1607 static int hotkey_inputdev_open(struct input_dev *dev)
1609 switch (tpacpi_lifecycle) {
1610 case TPACPI_LIFE_INIT:
1612 * hotkey_init will call hotkey_poll_setup_safe
1613 * at the appropriate moment
1616 case TPACPI_LIFE_EXITING:
1618 case TPACPI_LIFE_RUNNING:
1619 hotkey_poll_setup_safe(0);
1623 /* Should only happen if tpacpi_lifecycle is corrupt */
1628 static void hotkey_inputdev_close(struct input_dev *dev)
1630 /* disable hotkey polling when possible */
1631 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1632 hotkey_poll_setup_safe(0);
1635 /* sysfs hotkey enable ------------------------------------------------- */
1636 static ssize_t hotkey_enable_show(struct device *dev,
1637 struct device_attribute *attr,
1642 res = hotkey_status_get(&status);
1646 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1649 static ssize_t hotkey_enable_store(struct device *dev,
1650 struct device_attribute *attr,
1651 const char *buf, size_t count)
1656 if (parse_strtoul(buf, 1, &t))
1659 res = hotkey_status_set(t);
1661 return (res) ? res : count;
1664 static struct device_attribute dev_attr_hotkey_enable =
1665 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1666 hotkey_enable_show, hotkey_enable_store);
1668 /* sysfs hotkey mask --------------------------------------------------- */
1669 static ssize_t hotkey_mask_show(struct device *dev,
1670 struct device_attribute *attr,
1675 if (mutex_lock_interruptible(&hotkey_mutex))
1676 return -ERESTARTSYS;
1677 res = hotkey_mask_get();
1678 mutex_unlock(&hotkey_mutex);
1681 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1684 static ssize_t hotkey_mask_store(struct device *dev,
1685 struct device_attribute *attr,
1686 const char *buf, size_t count)
1691 if (parse_strtoul(buf, 0xffffffffUL, &t))
1694 if (mutex_lock_interruptible(&hotkey_mutex))
1695 return -ERESTARTSYS;
1697 res = hotkey_mask_set(t);
1699 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1700 hotkey_poll_setup(1);
1703 mutex_unlock(&hotkey_mutex);
1705 return (res) ? res : count;
1708 static struct device_attribute dev_attr_hotkey_mask =
1709 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1710 hotkey_mask_show, hotkey_mask_store);
1712 /* sysfs hotkey bios_enabled ------------------------------------------- */
1713 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1714 struct device_attribute *attr,
1717 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1720 static struct device_attribute dev_attr_hotkey_bios_enabled =
1721 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1723 /* sysfs hotkey bios_mask ---------------------------------------------- */
1724 static ssize_t hotkey_bios_mask_show(struct device *dev,
1725 struct device_attribute *attr,
1728 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1731 static struct device_attribute dev_attr_hotkey_bios_mask =
1732 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1734 /* sysfs hotkey all_mask ----------------------------------------------- */
1735 static ssize_t hotkey_all_mask_show(struct device *dev,
1736 struct device_attribute *attr,
1739 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1740 hotkey_all_mask | hotkey_source_mask);
1743 static struct device_attribute dev_attr_hotkey_all_mask =
1744 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1746 /* sysfs hotkey recommended_mask --------------------------------------- */
1747 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1748 struct device_attribute *attr,
1751 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1752 (hotkey_all_mask | hotkey_source_mask)
1753 & ~hotkey_reserved_mask);
1756 static struct device_attribute dev_attr_hotkey_recommended_mask =
1757 __ATTR(hotkey_recommended_mask, S_IRUGO,
1758 hotkey_recommended_mask_show, NULL);
1760 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1762 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1763 static ssize_t hotkey_source_mask_show(struct device *dev,
1764 struct device_attribute *attr,
1767 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1770 static ssize_t hotkey_source_mask_store(struct device *dev,
1771 struct device_attribute *attr,
1772 const char *buf, size_t count)
1776 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1777 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1780 if (mutex_lock_interruptible(&hotkey_mutex))
1781 return -ERESTARTSYS;
1783 HOTKEY_CONFIG_CRITICAL_START
1784 hotkey_source_mask = t;
1785 HOTKEY_CONFIG_CRITICAL_END
1787 hotkey_poll_setup(1);
1789 mutex_unlock(&hotkey_mutex);
1794 static struct device_attribute dev_attr_hotkey_source_mask =
1795 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1796 hotkey_source_mask_show, hotkey_source_mask_store);
1798 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1799 static ssize_t hotkey_poll_freq_show(struct device *dev,
1800 struct device_attribute *attr,
1803 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1806 static ssize_t hotkey_poll_freq_store(struct device *dev,
1807 struct device_attribute *attr,
1808 const char *buf, size_t count)
1812 if (parse_strtoul(buf, 25, &t))
1815 if (mutex_lock_interruptible(&hotkey_mutex))
1816 return -ERESTARTSYS;
1818 hotkey_poll_freq = t;
1820 hotkey_poll_setup(1);
1821 mutex_unlock(&hotkey_mutex);
1826 static struct device_attribute dev_attr_hotkey_poll_freq =
1827 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1828 hotkey_poll_freq_show, hotkey_poll_freq_store);
1830 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1832 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1833 static ssize_t hotkey_radio_sw_show(struct device *dev,
1834 struct device_attribute *attr,
1838 res = hotkey_get_wlsw(&s);
1842 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1845 static struct device_attribute dev_attr_hotkey_radio_sw =
1846 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1848 static void hotkey_radio_sw_notify_change(void)
1850 if (tp_features.hotkey_wlsw)
1851 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1855 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1856 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1857 struct device_attribute *attr,
1861 res = hotkey_get_tablet_mode(&s);
1865 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1868 static struct device_attribute dev_attr_hotkey_tablet_mode =
1869 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1871 static void hotkey_tablet_mode_notify_change(void)
1873 if (tp_features.hotkey_tablet)
1874 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1875 "hotkey_tablet_mode");
1878 /* sysfs hotkey report_mode -------------------------------------------- */
1879 static ssize_t hotkey_report_mode_show(struct device *dev,
1880 struct device_attribute *attr,
1883 return snprintf(buf, PAGE_SIZE, "%d\n",
1884 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1887 static struct device_attribute dev_attr_hotkey_report_mode =
1888 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1890 /* sysfs wakeup reason (pollable) -------------------------------------- */
1891 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1892 struct device_attribute *attr,
1895 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1898 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1899 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1901 static void hotkey_wakeup_reason_notify_change(void)
1903 if (tp_features.hotkey_mask)
1904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1908 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1909 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1910 struct device_attribute *attr,
1913 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1916 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1917 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1918 hotkey_wakeup_hotunplug_complete_show, NULL);
1920 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1922 if (tp_features.hotkey_mask)
1923 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1924 "wakeup_hotunplug_complete");
1927 /* --------------------------------------------------------------------- */
1929 static struct attribute *hotkey_attributes[] __initdata = {
1930 &dev_attr_hotkey_enable.attr,
1931 &dev_attr_hotkey_bios_enabled.attr,
1932 &dev_attr_hotkey_report_mode.attr,
1933 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1934 &dev_attr_hotkey_mask.attr,
1935 &dev_attr_hotkey_all_mask.attr,
1936 &dev_attr_hotkey_recommended_mask.attr,
1937 &dev_attr_hotkey_source_mask.attr,
1938 &dev_attr_hotkey_poll_freq.attr,
1942 static struct attribute *hotkey_mask_attributes[] __initdata = {
1943 &dev_attr_hotkey_bios_mask.attr,
1944 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1945 &dev_attr_hotkey_mask.attr,
1946 &dev_attr_hotkey_all_mask.attr,
1947 &dev_attr_hotkey_recommended_mask.attr,
1949 &dev_attr_hotkey_wakeup_reason.attr,
1950 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1953 static void bluetooth_update_rfk(void);
1954 static void wan_update_rfk(void);
1955 static void tpacpi_send_radiosw_update(void)
1959 /* Sync these BEFORE sending any rfkill events */
1960 if (tp_features.bluetooth)
1961 bluetooth_update_rfk();
1962 if (tp_features.wan)
1965 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1966 mutex_lock(&tpacpi_inputdev_send_mutex);
1968 input_report_switch(tpacpi_inputdev,
1969 SW_RFKILL_ALL, !!wlsw);
1970 input_sync(tpacpi_inputdev);
1972 mutex_unlock(&tpacpi_inputdev_send_mutex);
1974 hotkey_radio_sw_notify_change();
1977 static void hotkey_exit(void)
1979 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1980 hotkey_poll_stop_sync();
1983 if (hotkey_dev_attributes)
1984 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1986 kfree(hotkey_keycode_map);
1988 if (tp_features.hotkey) {
1989 dbg_printk(TPACPI_DBG_EXIT,
1990 "restoring original hot key mask\n");
1991 /* no short-circuit boolean operator below! */
1992 if ((hotkey_mask_set(hotkey_orig_mask) |
1993 hotkey_status_set(hotkey_orig_status)) != 0)
1995 "failed to restore hot key mask "
1996 "to BIOS defaults\n");
2000 static int __init hotkey_init(struct ibm_init_struct *iibm)
2002 /* Requirements for changing the default keymaps:
2004 * 1. Many of the keys are mapped to KEY_RESERVED for very
2005 * good reasons. Do not change them unless you have deep
2006 * knowledge on the IBM and Lenovo ThinkPad firmware for
2007 * the various ThinkPad models. The driver behaves
2008 * differently for KEY_RESERVED: such keys have their
2009 * hot key mask *unset* in mask_recommended, and also
2010 * in the initial hot key mask programmed into the
2011 * firmware at driver load time, which means the firm-
2012 * ware may react very differently if you change them to
2015 * 2. You must be subscribed to the linux-thinkpad and
2016 * ibm-acpi-devel mailing lists, and you should read the
2017 * list archives since 2007 if you want to change the
2018 * keymaps. This requirement exists so that you will
2019 * know the past history of problems with the thinkpad-
2020 * acpi driver keymaps, and also that you will be
2021 * listening to any bug reports;
2023 * 3. Do not send thinkpad-acpi specific patches directly to
2024 * for merging, *ever*. Send them to the linux-acpi
2025 * mailinglist for comments. Merging is to be done only
2026 * through acpi-test and the ACPI maintainer.
2028 * If the above is too much to ask, don't change the keymap.
2029 * Ask the thinkpad-acpi maintainer to do it, instead.
2031 static u16 ibm_keycode_map[] __initdata = {
2032 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2033 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2034 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2035 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2037 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2038 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2039 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2040 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2042 /* brightness: firmware always reacts to them, unless
2043 * X.org did some tricks in the radeon BIOS scratch
2044 * registers of *some* models */
2045 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2046 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2048 /* Thinklight: firmware always react to it */
2049 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2051 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2052 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2054 /* Volume: firmware always react to it and reprograms
2055 * the built-in *extra* mixer. Never map it to control
2056 * another mixer by default. */
2057 KEY_RESERVED, /* 0x14: VOLUME UP */
2058 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2059 KEY_RESERVED, /* 0x16: MUTE */
2061 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2063 /* (assignments unknown, please report if found) */
2064 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2065 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2067 static u16 lenovo_keycode_map[] __initdata = {
2068 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2069 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2070 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2071 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2073 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2074 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2075 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2076 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2078 /* These either have to go through ACPI video, or
2079 * act like in the IBM ThinkPads, so don't ever
2080 * enable them by default */
2081 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2082 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2084 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2086 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2087 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2089 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2090 * react to it and reprograms the built-in *extra* mixer.
2091 * Never map it to control another mixer by default.
2093 * T60?, T61, R60?, R61: firmware and EC tries to send
2094 * these over the regular keyboard, so these are no-ops,
2095 * but there are still weird bugs re. MUTE, so do not
2096 * change unless you get test reports from all Lenovo
2097 * models. May cause the BIOS to interfere with the
2100 KEY_RESERVED, /* 0x14: VOLUME UP */
2101 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2102 KEY_RESERVED, /* 0x16: MUTE */
2104 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2106 /* (assignments unknown, please report if found) */
2107 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2108 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2111 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2112 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2113 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2119 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2121 BUG_ON(!tpacpi_inputdev);
2122 BUG_ON(tpacpi_inputdev->open != NULL ||
2123 tpacpi_inputdev->close != NULL);
2125 TPACPI_ACPIHANDLE_INIT(hkey);
2126 mutex_init(&hotkey_mutex);
2128 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2129 mutex_init(&hotkey_thread_mutex);
2130 mutex_init(&hotkey_thread_data_mutex);
2133 /* hotkey not supported on 570 */
2134 tp_features.hotkey = hkey_handle != NULL;
2136 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2137 str_supported(tp_features.hotkey));
2139 if (!tp_features.hotkey)
2142 hotkey_dev_attributes = create_attr_set(13, NULL);
2143 if (!hotkey_dev_attributes)
2145 res = add_many_to_attr_set(hotkey_dev_attributes,
2147 ARRAY_SIZE(hotkey_attributes));
2151 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2152 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2153 for HKEY interface version 0x100 */
2154 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2155 if ((hkeyv >> 8) != 1) {
2156 printk(TPACPI_ERR "unknown version of the "
2157 "HKEY interface: 0x%x\n", hkeyv);
2158 printk(TPACPI_ERR "please report this to %s\n",
2162 * MHKV 0x100 in A31, R40, R40e,
2163 * T4x, X31, and later
2165 tp_features.hotkey_mask = 1;
2169 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2170 str_supported(tp_features.hotkey_mask));
2172 if (tp_features.hotkey_mask) {
2173 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2176 "missing MHKA handler, "
2177 "please report this to %s\n",
2179 /* FN+F12, FN+F4, FN+F3 */
2180 hotkey_all_mask = 0x080cU;
2184 /* hotkey_source_mask *must* be zero for
2185 * the first hotkey_mask_get */
2186 res = hotkey_status_get(&hotkey_orig_status);
2190 if (tp_features.hotkey_mask) {
2191 res = hotkey_mask_get();
2195 hotkey_orig_mask = hotkey_mask;
2196 res = add_many_to_attr_set(
2197 hotkey_dev_attributes,
2198 hotkey_mask_attributes,
2199 ARRAY_SIZE(hotkey_mask_attributes));
2204 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2205 if (tp_features.hotkey_mask) {
2206 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2209 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2212 vdbg_printk(TPACPI_DBG_INIT,
2213 "hotkey source mask 0x%08x, polling freq %d\n",
2214 hotkey_source_mask, hotkey_poll_freq);
2217 /* Not all thinkpads have a hardware radio switch */
2218 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2219 tp_features.hotkey_wlsw = 1;
2221 "radio switch found; radios are %s\n",
2222 enabled(status, 0));
2224 if (tp_features.hotkey_wlsw)
2225 res = add_to_attr_set(hotkey_dev_attributes,
2226 &dev_attr_hotkey_radio_sw.attr);
2228 /* For X41t, X60t, X61t Tablets... */
2229 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2230 tp_features.hotkey_tablet = 1;
2232 "possible tablet mode switch found; "
2233 "ThinkPad in %s mode\n",
2234 (status & TP_HOTKEY_TABLET_MASK)?
2235 "tablet" : "laptop");
2236 res = add_to_attr_set(hotkey_dev_attributes,
2237 &dev_attr_hotkey_tablet_mode.attr);
2241 res = register_attr_set_with_sysfs(
2242 hotkey_dev_attributes,
2243 &tpacpi_pdev->dev.kobj);
2247 /* Set up key map */
2249 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2251 if (!hotkey_keycode_map) {
2253 "failed to allocate memory for key map\n");
2258 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2259 dbg_printk(TPACPI_DBG_INIT,
2260 "using Lenovo default hot key map\n");
2261 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2262 TPACPI_HOTKEY_MAP_SIZE);
2264 dbg_printk(TPACPI_DBG_INIT,
2265 "using IBM default hot key map\n");
2266 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2267 TPACPI_HOTKEY_MAP_SIZE);
2270 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2271 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2272 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2273 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2274 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2275 tpacpi_inputdev->keycode = hotkey_keycode_map;
2276 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2277 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2278 set_bit(hotkey_keycode_map[i],
2279 tpacpi_inputdev->keybit);
2281 if (i < sizeof(hotkey_reserved_mask)*8)
2282 hotkey_reserved_mask |= 1 << i;
2286 if (tp_features.hotkey_wlsw) {
2287 set_bit(EV_SW, tpacpi_inputdev->evbit);
2288 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2290 if (tp_features.hotkey_tablet) {
2291 set_bit(EV_SW, tpacpi_inputdev->evbit);
2292 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2295 /* Do not issue duplicate brightness change events to
2297 if (!tp_features.bright_acpimode)
2298 /* update bright_acpimode... */
2299 tpacpi_check_std_acpi_brightness_support();
2301 if (tp_features.bright_acpimode) {
2303 "This ThinkPad has standard ACPI backlight "
2304 "brightness control, supported by the ACPI "
2306 printk(TPACPI_NOTICE
2307 "Disabling thinkpad-acpi brightness events "
2310 /* The hotkey_reserved_mask change below is not
2311 * necessary while the keys are at KEY_RESERVED in the
2312 * default map, but better safe than sorry, leave it
2313 * here as a marker of what we have to do, especially
2314 * when we finally become able to set this at runtime
2315 * on response to X.org requests */
2316 hotkey_reserved_mask |=
2317 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2318 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2321 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2322 res = hotkey_status_set(1);
2327 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2328 & ~hotkey_reserved_mask)
2329 | hotkey_orig_mask);
2330 if (res < 0 && res != -ENXIO) {
2335 dbg_printk(TPACPI_DBG_INIT,
2336 "legacy hot key reporting over procfs %s\n",
2337 (hotkey_report_mode < 2) ?
2338 "enabled" : "disabled");
2340 tpacpi_inputdev->open = &hotkey_inputdev_open;
2341 tpacpi_inputdev->close = &hotkey_inputdev_close;
2343 hotkey_poll_setup_safe(1);
2344 tpacpi_send_radiosw_update();
2345 tpacpi_input_send_tabletsw();
2350 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2351 hotkey_dev_attributes = NULL;
2353 return (res < 0)? res : 1;
2356 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2359 unsigned int scancode;
2364 if (event != 0x80) {
2366 "unknown HKEY notification event %d\n", event);
2367 /* forward it to userspace, maybe it knows how to handle it */
2368 acpi_bus_generate_netlink_event(
2369 ibm->acpi->device->pnp.device_class,
2370 ibm->acpi->device->dev.bus_id,
2376 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2377 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2390 switch (hkey >> 12) {
2392 /* 0x1000-0x1FFF: key presses */
2393 scancode = hkey & 0xfff;
2394 if (scancode > 0 && scancode < 0x21) {
2396 if (!(hotkey_source_mask & (1 << scancode))) {
2397 tpacpi_input_send_key(scancode);
2409 case 0x2304: /* suspend, undock */
2410 case 0x2404: /* hibernation, undock */
2411 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2414 case 0x2305: /* suspend, bay eject */
2415 case 0x2405: /* hibernation, bay eject */
2416 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2422 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2424 "woke up due to a hot-unplug "
2426 hotkey_wakeup_reason_notify_change();
2430 /* bay-related wakeups */
2431 if (hkey == 0x3003) {
2432 hotkey_autosleep_ack = 1;
2435 hotkey_wakeup_hotunplug_complete_notify_change();
2441 /* dock-related wakeups */
2442 if (hkey == 0x4003) {
2443 hotkey_autosleep_ack = 1;
2446 hotkey_wakeup_hotunplug_complete_notify_change();
2452 /* 0x5000-0x5FFF: human interface helpers */
2454 case 0x5010: /* Lenovo new BIOS: brightness changed */
2455 case 0x500b: /* X61t: tablet pen inserted into bay */
2456 case 0x500c: /* X61t: tablet pen removed from bay */
2458 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2459 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2460 tpacpi_input_send_tabletsw();
2461 hotkey_tablet_mode_notify_change();
2466 /* LID switch events. Do not propagate */
2474 /* 0x7000-0x7FFF: misc */
2475 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2476 tpacpi_send_radiosw_update();
2480 /* fallthrough to default */
2485 printk(TPACPI_NOTICE
2486 "unhandled HKEY event 0x%04x\n", hkey);
2490 if (!ignore_acpi_ev &&
2491 (send_acpi_ev || hotkey_report_mode < 2)) {
2492 acpi_bus_generate_proc_event(ibm->acpi->device,
2496 /* netlink events */
2497 if (!ignore_acpi_ev && send_acpi_ev) {
2498 acpi_bus_generate_netlink_event(
2499 ibm->acpi->device->pnp.device_class,
2500 ibm->acpi->device->dev.bus_id,
2506 static void hotkey_suspend(pm_message_t state)
2508 /* Do these on suspend, we get the events on early resume! */
2509 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2510 hotkey_autosleep_ack = 0;
2513 static void hotkey_resume(void)
2515 if (hotkey_mask_get())
2517 "error while trying to read hot key mask "
2519 tpacpi_send_radiosw_update();
2520 hotkey_tablet_mode_notify_change();
2521 hotkey_wakeup_reason_notify_change();
2522 hotkey_wakeup_hotunplug_complete_notify_change();
2523 hotkey_poll_setup_safe(0);
2526 /* procfs -------------------------------------------------------------- */
2527 static int hotkey_read(char *p)
2532 if (!tp_features.hotkey) {
2533 len += sprintf(p + len, "status:\t\tnot supported\n");
2537 if (mutex_lock_interruptible(&hotkey_mutex))
2538 return -ERESTARTSYS;
2539 res = hotkey_status_get(&status);
2541 res = hotkey_mask_get();
2542 mutex_unlock(&hotkey_mutex);
2546 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2547 if (tp_features.hotkey_mask) {
2548 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2549 len += sprintf(p + len,
2550 "commands:\tenable, disable, reset, <mask>\n");
2552 len += sprintf(p + len, "mask:\t\tnot supported\n");
2553 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2559 static int hotkey_write(char *buf)
2565 if (!tp_features.hotkey)
2568 if (mutex_lock_interruptible(&hotkey_mutex))
2569 return -ERESTARTSYS;
2575 while ((cmd = next_cmd(&buf))) {
2576 if (strlencmp(cmd, "enable") == 0) {
2578 } else if (strlencmp(cmd, "disable") == 0) {
2580 } else if (strlencmp(cmd, "reset") == 0) {
2581 status = hotkey_orig_status;
2582 mask = hotkey_orig_mask;
2583 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2585 } else if (sscanf(cmd, "%x", &mask) == 1) {
2593 res = hotkey_status_set(status);
2595 if (!res && mask != hotkey_mask)
2596 res = hotkey_mask_set(mask);
2599 mutex_unlock(&hotkey_mutex);
2603 static const struct acpi_device_id ibm_htk_device_ids[] = {
2604 {TPACPI_ACPI_HKEY_HID, 0},
2608 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2609 .hid = ibm_htk_device_ids,
2610 .notify = hotkey_notify,
2611 .handle = &hkey_handle,
2612 .type = ACPI_DEVICE_NOTIFY,
2615 static struct ibm_struct hotkey_driver_data = {
2617 .read = hotkey_read,
2618 .write = hotkey_write,
2619 .exit = hotkey_exit,
2620 .resume = hotkey_resume,
2621 .suspend = hotkey_suspend,
2622 .acpi = &ibm_hotkey_acpidriver,
2625 /*************************************************************************
2626 * Bluetooth subdriver
2630 /* ACPI GBDC/SBDC bits */
2631 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2632 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2633 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2636 static struct rfkill *tpacpi_bluetooth_rfkill;
2638 static int bluetooth_get_radiosw(void)
2642 if (!tp_features.bluetooth)
2645 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2646 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2647 return RFKILL_STATE_HARD_BLOCKED;
2649 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2652 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2653 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2656 static void bluetooth_update_rfk(void)
2660 if (!tpacpi_bluetooth_rfkill)
2663 status = bluetooth_get_radiosw();
2666 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2669 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2673 if (!tp_features.bluetooth)
2676 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2677 * reason to risk weird behaviour. */
2678 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2682 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2685 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2687 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2688 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2692 bluetooth_update_rfk();
2697 /* sysfs bluetooth enable ---------------------------------------------- */
2698 static ssize_t bluetooth_enable_show(struct device *dev,
2699 struct device_attribute *attr,
2704 status = bluetooth_get_radiosw();
2708 return snprintf(buf, PAGE_SIZE, "%d\n",
2709 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2712 static ssize_t bluetooth_enable_store(struct device *dev,
2713 struct device_attribute *attr,
2714 const char *buf, size_t count)
2719 if (parse_strtoul(buf, 1, &t))
2722 res = bluetooth_set_radiosw(t, 1);
2724 return (res) ? res : count;
2727 static struct device_attribute dev_attr_bluetooth_enable =
2728 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2729 bluetooth_enable_show, bluetooth_enable_store);
2731 /* --------------------------------------------------------------------- */
2733 static struct attribute *bluetooth_attributes[] = {
2734 &dev_attr_bluetooth_enable.attr,
2738 static const struct attribute_group bluetooth_attr_group = {
2739 .attrs = bluetooth_attributes,
2742 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2744 int bts = bluetooth_get_radiosw();
2753 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2755 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2758 static void bluetooth_exit(void)
2760 if (tpacpi_bluetooth_rfkill)
2761 rfkill_unregister(tpacpi_bluetooth_rfkill);
2763 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2764 &bluetooth_attr_group);
2767 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2772 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2774 TPACPI_ACPIHANDLE_INIT(hkey);
2776 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2777 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2778 tp_features.bluetooth = hkey_handle &&
2779 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2781 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2782 str_supported(tp_features.bluetooth),
2785 if (tp_features.bluetooth &&
2786 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2787 /* no bluetooth hardware present in system */
2788 tp_features.bluetooth = 0;
2789 dbg_printk(TPACPI_DBG_INIT,
2790 "bluetooth hardware not installed\n");
2793 if (!tp_features.bluetooth)
2796 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2797 &bluetooth_attr_group);
2801 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2802 &tpacpi_bluetooth_rfkill,
2803 RFKILL_TYPE_BLUETOOTH,
2804 "tpacpi_bluetooth_sw",
2805 tpacpi_bluetooth_rfk_set,
2806 tpacpi_bluetooth_rfk_get);
2815 /* procfs -------------------------------------------------------------- */
2816 static int bluetooth_read(char *p)
2819 int status = bluetooth_get_radiosw();
2821 if (!tp_features.bluetooth)
2822 len += sprintf(p + len, "status:\t\tnot supported\n");
2824 len += sprintf(p + len, "status:\t\t%s\n",
2825 (status == RFKILL_STATE_UNBLOCKED) ?
2826 "enabled" : "disabled");
2827 len += sprintf(p + len, "commands:\tenable, disable\n");
2833 static int bluetooth_write(char *buf)
2837 if (!tp_features.bluetooth)
2840 while ((cmd = next_cmd(&buf))) {
2841 if (strlencmp(cmd, "enable") == 0) {
2842 bluetooth_set_radiosw(1, 1);
2843 } else if (strlencmp(cmd, "disable") == 0) {
2844 bluetooth_set_radiosw(0, 1);
2852 static struct ibm_struct bluetooth_driver_data = {
2853 .name = "bluetooth",
2854 .read = bluetooth_read,
2855 .write = bluetooth_write,
2856 .exit = bluetooth_exit,
2859 /*************************************************************************
2864 /* ACPI GWAN/SWAN bits */
2865 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2866 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2867 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2870 static struct rfkill *tpacpi_wan_rfkill;
2872 static int wan_get_radiosw(void)
2876 if (!tp_features.wan)
2879 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2880 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2881 return RFKILL_STATE_HARD_BLOCKED;
2883 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2886 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
2887 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2890 static void wan_update_rfk(void)
2894 if (!tpacpi_wan_rfkill)
2897 status = wan_get_radiosw();
2900 rfkill_force_state(tpacpi_wan_rfkill, status);
2903 static int wan_set_radiosw(int radio_on, int update_rfk)
2907 if (!tp_features.wan)
2910 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2911 * reason to risk weird behaviour. */
2912 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2916 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2919 status |= TP_ACPI_WANCARD_RADIOSSW;
2921 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2922 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2931 /* sysfs wan enable ---------------------------------------------------- */
2932 static ssize_t wan_enable_show(struct device *dev,
2933 struct device_attribute *attr,
2938 status = wan_get_radiosw();
2942 return snprintf(buf, PAGE_SIZE, "%d\n",
2943 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2946 static ssize_t wan_enable_store(struct device *dev,
2947 struct device_attribute *attr,
2948 const char *buf, size_t count)
2953 if (parse_strtoul(buf, 1, &t))
2956 res = wan_set_radiosw(t, 1);
2958 return (res) ? res : count;
2961 static struct device_attribute dev_attr_wan_enable =
2962 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2963 wan_enable_show, wan_enable_store);
2965 /* --------------------------------------------------------------------- */
2967 static struct attribute *wan_attributes[] = {
2968 &dev_attr_wan_enable.attr,
2972 static const struct attribute_group wan_attr_group = {
2973 .attrs = wan_attributes,
2976 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
2978 int wans = wan_get_radiosw();
2987 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
2989 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2992 static void wan_exit(void)
2994 if (tpacpi_wan_rfkill)
2995 rfkill_unregister(tpacpi_wan_rfkill);
2997 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3001 static int __init wan_init(struct ibm_init_struct *iibm)
3006 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3008 TPACPI_ACPIHANDLE_INIT(hkey);
3010 tp_features.wan = hkey_handle &&
3011 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3013 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3014 str_supported(tp_features.wan),
3017 if (tp_features.wan &&
3018 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3019 /* no wan hardware present in system */
3020 tp_features.wan = 0;
3021 dbg_printk(TPACPI_DBG_INIT,
3022 "wan hardware not installed\n");
3025 if (!tp_features.wan)
3028 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3033 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3038 tpacpi_wan_rfk_get);
3047 /* procfs -------------------------------------------------------------- */
3048 static int wan_read(char *p)
3051 int status = wan_get_radiosw();
3053 if (!tp_features.wan)
3054 len += sprintf(p + len, "status:\t\tnot supported\n");
3056 len += sprintf(p + len, "status:\t\t%s\n",
3057 (status == RFKILL_STATE_UNBLOCKED) ?
3058 "enabled" : "disabled");
3059 len += sprintf(p + len, "commands:\tenable, disable\n");
3065 static int wan_write(char *buf)
3069 if (!tp_features.wan)
3072 while ((cmd = next_cmd(&buf))) {
3073 if (strlencmp(cmd, "enable") == 0) {
3074 wan_set_radiosw(1, 1);
3075 } else if (strlencmp(cmd, "disable") == 0) {
3076 wan_set_radiosw(0, 1);
3084 static struct ibm_struct wan_driver_data = {
3091 /*************************************************************************
3095 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3097 enum video_access_mode {
3098 TPACPI_VIDEO_NONE = 0,
3099 TPACPI_VIDEO_570, /* 570 */
3100 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3101 TPACPI_VIDEO_NEW, /* all others */
3104 enum { /* video status flags, based on VIDEO_570 */
3105 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3106 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3107 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3110 enum { /* TPACPI_VIDEO_570 constants */
3111 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3112 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3113 * video_status_flags */
3114 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3115 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3118 static enum video_access_mode video_supported;
3119 static int video_orig_autosw;
3121 static int video_autosw_get(void);
3122 static int video_autosw_set(int enable);
3124 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3126 static int __init video_init(struct ibm_init_struct *iibm)
3130 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3132 TPACPI_ACPIHANDLE_INIT(vid);
3133 TPACPI_ACPIHANDLE_INIT(vid2);
3135 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3136 /* G41, assume IVGA doesn't change */
3137 vid_handle = vid2_handle;
3140 /* video switching not supported on R30, R31 */
3141 video_supported = TPACPI_VIDEO_NONE;
3142 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3144 video_supported = TPACPI_VIDEO_570;
3145 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3146 /* 600e/x, 770e, 770x */
3147 video_supported = TPACPI_VIDEO_770;
3150 video_supported = TPACPI_VIDEO_NEW;
3152 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3153 str_supported(video_supported != TPACPI_VIDEO_NONE),
3156 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3159 static void video_exit(void)
3161 dbg_printk(TPACPI_DBG_EXIT,
3162 "restoring original video autoswitch mode\n");
3163 if (video_autosw_set(video_orig_autosw))
3164 printk(TPACPI_ERR "error while trying to restore original "
3165 "video autoswitch mode\n");
3168 static int video_outputsw_get(void)
3173 switch (video_supported) {
3174 case TPACPI_VIDEO_570:
3175 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3176 TP_ACPI_VIDEO_570_PHSCMD))
3178 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3180 case TPACPI_VIDEO_770:
3181 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3184 status |= TP_ACPI_VIDEO_S_LCD;
3185 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3188 status |= TP_ACPI_VIDEO_S_CRT;
3190 case TPACPI_VIDEO_NEW:
3191 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3192 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3195 status |= TP_ACPI_VIDEO_S_CRT;
3197 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3198 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3201 status |= TP_ACPI_VIDEO_S_LCD;
3202 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3205 status |= TP_ACPI_VIDEO_S_DVI;
3214 static int video_outputsw_set(int status)
3219 switch (video_supported) {
3220 case TPACPI_VIDEO_570:
3221 res = acpi_evalf(NULL, NULL,
3222 "\\_SB.PHS2", "vdd",
3223 TP_ACPI_VIDEO_570_PHS2CMD,
3224 status | TP_ACPI_VIDEO_570_PHS2SET);
3226 case TPACPI_VIDEO_770:
3227 autosw = video_autosw_get();
3231 res = video_autosw_set(1);
3234 res = acpi_evalf(vid_handle, NULL,
3235 "ASWT", "vdd", status * 0x100, 0);
3236 if (!autosw && video_autosw_set(autosw)) {
3238 "video auto-switch left enabled due to error\n");
3242 case TPACPI_VIDEO_NEW:
3243 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3244 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3250 return (res)? 0 : -EIO;
3253 static int video_autosw_get(void)
3257 switch (video_supported) {
3258 case TPACPI_VIDEO_570:
3259 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3262 case TPACPI_VIDEO_770:
3263 case TPACPI_VIDEO_NEW:
3264 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3274 static int video_autosw_set(int enable)
3276 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3281 static int video_outputsw_cycle(void)
3283 int autosw = video_autosw_get();
3289 switch (video_supported) {
3290 case TPACPI_VIDEO_570:
3291 res = video_autosw_set(1);
3294 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3296 case TPACPI_VIDEO_770:
3297 case TPACPI_VIDEO_NEW:
3298 res = video_autosw_set(1);
3301 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3306 if (!autosw && video_autosw_set(autosw)) {
3308 "video auto-switch left enabled due to error\n");
3312 return (res)? 0 : -EIO;
3315 static int video_expand_toggle(void)
3317 switch (video_supported) {
3318 case TPACPI_VIDEO_570:
3319 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3321 case TPACPI_VIDEO_770:
3322 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3324 case TPACPI_VIDEO_NEW:
3325 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3333 static int video_read(char *p)
3338 if (video_supported == TPACPI_VIDEO_NONE) {
3339 len += sprintf(p + len, "status:\t\tnot supported\n");
3343 status = video_outputsw_get();
3347 autosw = video_autosw_get();
3351 len += sprintf(p + len, "status:\t\tsupported\n");
3352 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3353 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3354 if (video_supported == TPACPI_VIDEO_NEW)
3355 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3356 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3357 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3358 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3359 if (video_supported == TPACPI_VIDEO_NEW)
3360 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3361 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3362 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3367 static int video_write(char *buf)
3370 int enable, disable, status;
3373 if (video_supported == TPACPI_VIDEO_NONE)
3379 while ((cmd = next_cmd(&buf))) {
3380 if (strlencmp(cmd, "lcd_enable") == 0) {
3381 enable |= TP_ACPI_VIDEO_S_LCD;
3382 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3383 disable |= TP_ACPI_VIDEO_S_LCD;
3384 } else if (strlencmp(cmd, "crt_enable") == 0) {
3385 enable |= TP_ACPI_VIDEO_S_CRT;
3386 } else if (strlencmp(cmd, "crt_disable") == 0) {
3387 disable |= TP_ACPI_VIDEO_S_CRT;
3388 } else if (video_supported == TPACPI_VIDEO_NEW &&
3389 strlencmp(cmd, "dvi_enable") == 0) {
3390 enable |= TP_ACPI_VIDEO_S_DVI;
3391 } else if (video_supported == TPACPI_VIDEO_NEW &&
3392 strlencmp(cmd, "dvi_disable") == 0) {
3393 disable |= TP_ACPI_VIDEO_S_DVI;
3394 } else if (strlencmp(cmd, "auto_enable") == 0) {
3395 res = video_autosw_set(1);
3398 } else if (strlencmp(cmd, "auto_disable") == 0) {
3399 res = video_autosw_set(0);
3402 } else if (strlencmp(cmd, "video_switch") == 0) {
3403 res = video_outputsw_cycle();
3406 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3407 res = video_expand_toggle();
3414 if (enable || disable) {
3415 status = video_outputsw_get();
3418 res = video_outputsw_set((status & ~disable) | enable);
3426 static struct ibm_struct video_driver_data = {
3429 .write = video_write,
3433 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3435 /*************************************************************************
3436 * Light (thinklight) subdriver
3439 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3440 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3442 static int light_get_status(void)
3446 if (tp_features.light_status) {
3447 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3455 static int light_set_status(int status)
3459 if (tp_features.light) {
3461 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3463 TP_CMOS_THINKLIGHT_ON :
3464 TP_CMOS_THINKLIGHT_OFF);
3466 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3469 return (rc)? 0 : -EIO;
3475 static void light_set_status_worker(struct work_struct *work)
3477 struct tpacpi_led_classdev *data =
3478 container_of(work, struct tpacpi_led_classdev, work);
3480 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3481 light_set_status((data->new_brightness != LED_OFF));
3484 static void light_sysfs_set(struct led_classdev *led_cdev,
3485 enum led_brightness brightness)
3487 struct tpacpi_led_classdev *data =
3488 container_of(led_cdev,
3489 struct tpacpi_led_classdev,
3491 data->new_brightness = brightness;
3492 queue_work(tpacpi_wq, &data->work);
3495 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3497 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3500 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3502 .name = "tpacpi::thinklight",
3503 .brightness_set = &light_sysfs_set,
3504 .brightness_get = &light_sysfs_get,
3508 static int __init light_init(struct ibm_init_struct *iibm)
3512 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3514 TPACPI_ACPIHANDLE_INIT(ledb);
3515 TPACPI_ACPIHANDLE_INIT(lght);
3516 TPACPI_ACPIHANDLE_INIT(cmos);
3517 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3519 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3520 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3522 if (tp_features.light)
3523 /* light status not supported on
3524 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3525 tp_features.light_status =
3526 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3528 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3529 str_supported(tp_features.light),
3530 str_supported(tp_features.light_status));
3532 if (!tp_features.light)
3535 rc = led_classdev_register(&tpacpi_pdev->dev,
3536 &tpacpi_led_thinklight.led_classdev);
3539 tp_features.light = 0;
3540 tp_features.light_status = 0;
3548 static void light_exit(void)
3550 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3551 if (work_pending(&tpacpi_led_thinklight.work))
3552 flush_workqueue(tpacpi_wq);
3555 static int light_read(char *p)
3560 if (!tp_features.light) {
3561 len += sprintf(p + len, "status:\t\tnot supported\n");
3562 } else if (!tp_features.light_status) {
3563 len += sprintf(p + len, "status:\t\tunknown\n");
3564 len += sprintf(p + len, "commands:\ton, off\n");
3566 status = light_get_status();
3569 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3570 len += sprintf(p + len, "commands:\ton, off\n");
3576 static int light_write(char *buf)
3581 if (!tp_features.light)
3584 while ((cmd = next_cmd(&buf))) {
3585 if (strlencmp(cmd, "on") == 0) {
3587 } else if (strlencmp(cmd, "off") == 0) {
3593 return light_set_status(newstatus);
3596 static struct ibm_struct light_driver_data = {
3599 .write = light_write,
3603 /*************************************************************************
3607 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3609 static void dock_notify(struct ibm_struct *ibm, u32 event);
3610 static int dock_read(char *p);
3611 static int dock_write(char *buf);
3613 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3614 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3615 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3616 "\\_SB.PCI.ISA.SLCE", /* 570 */
3617 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3619 /* don't list other alternatives as we install a notify handler on the 570 */
3620 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3622 static const struct acpi_device_id ibm_pci_device_ids[] = {
3623 {PCI_ROOT_HID_STRING, 0},
3627 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3629 .notify = dock_notify,
3630 .handle = &dock_handle,
3631 .type = ACPI_SYSTEM_NOTIFY,
3634 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3635 * We just use it to get notifications of dock hotplug
3636 * in very old thinkpads */
3637 .hid = ibm_pci_device_ids,
3638 .notify = dock_notify,
3639 .handle = &pci_handle,
3640 .type = ACPI_SYSTEM_NOTIFY,
3644 static struct ibm_struct dock_driver_data[2] = {
3648 .write = dock_write,
3649 .acpi = &ibm_dock_acpidriver[0],
3653 .acpi = &ibm_dock_acpidriver[1],
3657 #define dock_docked() (_sta(dock_handle) & 1)
3659 static int __init dock_init(struct ibm_init_struct *iibm)
3661 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3663 TPACPI_ACPIHANDLE_INIT(dock);
3665 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3666 str_supported(dock_handle != NULL));
3668 return (dock_handle)? 0 : 1;
3671 static int __init dock_init2(struct ibm_init_struct *iibm)
3675 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3677 if (dock_driver_data[0].flags.acpi_driver_registered &&
3678 dock_driver_data[0].flags.acpi_notify_installed) {
3679 TPACPI_ACPIHANDLE_INIT(pci);
3680 dock2_needed = (pci_handle != NULL);
3681 vdbg_printk(TPACPI_DBG_INIT,
3682 "dock PCI handler for the TP 570 is %s\n",
3683 str_supported(dock2_needed));
3685 vdbg_printk(TPACPI_DBG_INIT,
3686 "dock subdriver part 2 not required\n");
3690 return (dock2_needed)? 0 : 1;
3693 static void dock_notify(struct ibm_struct *ibm, u32 event)
3695 int docked = dock_docked();
3696 int pci = ibm->acpi->hid && ibm->acpi->device &&
3697 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3700 if (event == 1 && !pci) /* 570 */
3701 data = 1; /* button */
3702 else if (event == 1 && pci) /* 570 */
3703 data = 3; /* dock */
3704 else if (event == 3 && docked)
3705 data = 1; /* button */
3706 else if (event == 3 && !docked)
3707 data = 2; /* undock */
3708 else if (event == 0 && docked)
3709 data = 3; /* dock */
3711 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3712 event, _sta(dock_handle));
3713 data = 0; /* unknown */
3715 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3716 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3717 ibm->acpi->device->dev.bus_id,
3721 static int dock_read(char *p)
3724 int docked = dock_docked();
3727 len += sprintf(p + len, "status:\t\tnot supported\n");
3729 len += sprintf(p + len, "status:\t\tundocked\n");
3731 len += sprintf(p + len, "status:\t\tdocked\n");
3732 len += sprintf(p + len, "commands:\tdock, undock\n");
3738 static int dock_write(char *buf)
3745 while ((cmd = next_cmd(&buf))) {
3746 if (strlencmp(cmd, "undock") == 0) {
3747 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3748 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3750 } else if (strlencmp(cmd, "dock") == 0) {
3751 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3760 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3762 /*************************************************************************
3766 #ifdef CONFIG_THINKPAD_ACPI_BAY
3768 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3769 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3770 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3771 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3772 ); /* A21e, R30, R31 */
3773 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3774 "_EJ0", /* all others */
3775 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3776 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3777 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3779 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3783 static int __init bay_init(struct ibm_init_struct *iibm)
3785 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3787 TPACPI_ACPIHANDLE_INIT(bay);
3789 TPACPI_ACPIHANDLE_INIT(bay_ej);
3790 TPACPI_ACPIHANDLE_INIT(bay2);
3792 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3794 tp_features.bay_status = bay_handle &&
3795 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3796 tp_features.bay_status2 = bay2_handle &&
3797 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3799 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3800 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3801 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3802 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3804 vdbg_printk(TPACPI_DBG_INIT,
3805 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3806 str_supported(tp_features.bay_status),
3807 str_supported(tp_features.bay_eject),
3808 str_supported(tp_features.bay_status2),
3809 str_supported(tp_features.bay_eject2));
3811 return (tp_features.bay_status || tp_features.bay_eject ||
3812 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3815 static void bay_notify(struct ibm_struct *ibm, u32 event)
3817 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3818 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3819 ibm->acpi->device->dev.bus_id,
3823 #define bay_occupied(b) (_sta(b##_handle) & 1)
3825 static int bay_read(char *p)
3828 int occupied = bay_occupied(bay);
3829 int occupied2 = bay_occupied(bay2);
3832 len += sprintf(p + len, "status:\t\t%s\n",
3833 tp_features.bay_status ?
3834 (occupied ? "occupied" : "unoccupied") :
3836 if (tp_features.bay_status2)
3837 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3838 "occupied" : "unoccupied");
3840 eject = tp_features.bay_eject && occupied;
3841 eject2 = tp_features.bay_eject2 && occupied2;
3843 if (eject && eject2)
3844 len += sprintf(p + len, "commands:\teject, eject2\n");
3846 len += sprintf(p + len, "commands:\teject\n");
3848 len += sprintf(p + len, "commands:\teject2\n");
3853 static int bay_write(char *buf)
3857 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3860 while ((cmd = next_cmd(&buf))) {
3861 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3862 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3864 } else if (tp_features.bay_eject2 &&
3865 strlencmp(cmd, "eject2") == 0) {
3866 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3875 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3876 .notify = bay_notify,
3877 .handle = &bay_handle,
3878 .type = ACPI_SYSTEM_NOTIFY,
3881 static struct ibm_struct bay_driver_data = {
3885 .acpi = &ibm_bay_acpidriver,
3888 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3890 /*************************************************************************
3894 /* sysfs cmos_command -------------------------------------------------- */
3895 static ssize_t cmos_command_store(struct device *dev,
3896 struct device_attribute *attr,
3897 const char *buf, size_t count)
3899 unsigned long cmos_cmd;
3902 if (parse_strtoul(buf, 21, &cmos_cmd))
3905 res = issue_thinkpad_cmos_command(cmos_cmd);
3906 return (res)? res : count;
3909 static struct device_attribute dev_attr_cmos_command =
3910 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3912 /* --------------------------------------------------------------------- */
3914 static int __init cmos_init(struct ibm_init_struct *iibm)
3918 vdbg_printk(TPACPI_DBG_INIT,
3919 "initializing cmos commands subdriver\n");
3921 TPACPI_ACPIHANDLE_INIT(cmos);
3923 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3924 str_supported(cmos_handle != NULL));
3926 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3930 return (cmos_handle)? 0 : 1;
3933 static void cmos_exit(void)
3935 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3938 static int cmos_read(char *p)
3942 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3943 R30, R31, T20-22, X20-21 */
3945 len += sprintf(p + len, "status:\t\tnot supported\n");
3947 len += sprintf(p + len, "status:\t\tsupported\n");
3948 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3954 static int cmos_write(char *buf)
3959 while ((cmd = next_cmd(&buf))) {
3960 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3961 cmos_cmd >= 0 && cmos_cmd <= 21) {
3966 res = issue_thinkpad_cmos_command(cmos_cmd);
3974 static struct ibm_struct cmos_driver_data = {
3977 .write = cmos_write,
3981 /*************************************************************************
3985 enum led_access_mode {
3986 TPACPI_LED_NONE = 0,
3987 TPACPI_LED_570, /* 570 */
3988 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3989 TPACPI_LED_NEW, /* all others */
3992 enum { /* For TPACPI_LED_OLD */
3993 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3994 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3995 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4004 static enum led_access_mode led_supported;
4006 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4007 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4008 /* T20-22, X20-21 */
4009 "LED", /* all others */
4012 #define TPACPI_LED_NUMLEDS 8
4013 static struct tpacpi_led_classdev *tpacpi_leds;
4014 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4015 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4016 /* there's a limit of 19 chars + NULL before 2.6.26 */
4018 "tpacpi:orange:batt",
4019 "tpacpi:green:batt",
4020 "tpacpi::dock_active",
4021 "tpacpi::bay_active",
4022 "tpacpi::dock_batt",
4023 "tpacpi::unknown_led",
4027 static int led_get_status(const unsigned int led)
4030 enum led_status_t led_s;
4032 switch (led_supported) {
4033 case TPACPI_LED_570:
4034 if (!acpi_evalf(ec_handle,
4035 &status, "GLED", "dd", 1 << led))
4037 led_s = (status == 0)?
4042 tpacpi_led_state_cache[led] = led_s;
4051 static int led_set_status(const unsigned int led,
4052 const enum led_status_t ledstatus)
4054 /* off, on, blink. Index is led_status_t */
4055 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4056 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4060 switch (led_supported) {
4061 case TPACPI_LED_570:
4065 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4066 (1 << led), led_sled_arg1[ledstatus]))
4069 case TPACPI_LED_OLD:
4070 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4073 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4075 rc = ec_write(TPACPI_LED_EC_HLBL,
4076 (ledstatus == TPACPI_LED_BLINK) << led);
4078 rc = ec_write(TPACPI_LED_EC_HLCL,
4079 (ledstatus != TPACPI_LED_OFF) << led);
4081 case TPACPI_LED_NEW:
4083 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4084 led, led_led_arg1[ledstatus]))
4092 tpacpi_led_state_cache[led] = ledstatus;
4097 static void led_sysfs_set_status(unsigned int led,
4098 enum led_brightness brightness)
4101 (brightness == LED_OFF) ?
4103 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4104 TPACPI_LED_BLINK : TPACPI_LED_ON);
4107 static void led_set_status_worker(struct work_struct *work)
4109 struct tpacpi_led_classdev *data =
4110 container_of(work, struct tpacpi_led_classdev, work);
4112 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4113 led_sysfs_set_status(data->led, data->new_brightness);
4116 static void led_sysfs_set(struct led_classdev *led_cdev,
4117 enum led_brightness brightness)
4119 struct tpacpi_led_classdev *data = container_of(led_cdev,
4120 struct tpacpi_led_classdev, led_classdev);
4122 data->new_brightness = brightness;
4123 queue_work(tpacpi_wq, &data->work);
4126 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4127 unsigned long *delay_on, unsigned long *delay_off)
4129 struct tpacpi_led_classdev *data = container_of(led_cdev,
4130 struct tpacpi_led_classdev, led_classdev);
4132 /* Can we choose the flash rate? */
4133 if (*delay_on == 0 && *delay_off == 0) {
4134 /* yes. set them to the hardware blink rate (1 Hz) */
4135 *delay_on = 500; /* ms */
4136 *delay_off = 500; /* ms */
4137 } else if ((*delay_on != 500) || (*delay_off != 500))
4140 data->new_brightness = TPACPI_LED_BLINK;
4141 queue_work(tpacpi_wq, &data->work);
4146 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4150 struct tpacpi_led_classdev *data = container_of(led_cdev,
4151 struct tpacpi_led_classdev, led_classdev);
4153 rc = led_get_status(data->led);
4155 if (rc == TPACPI_LED_OFF || rc < 0)
4156 rc = LED_OFF; /* no error handling in led class :( */
4163 static void led_exit(void)
4167 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4168 if (tpacpi_leds[i].led_classdev.name)
4169 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4175 static int __init led_init(struct ibm_init_struct *iibm)
4180 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4182 TPACPI_ACPIHANDLE_INIT(led);
4185 /* led not supported on R30, R31 */
4186 led_supported = TPACPI_LED_NONE;
4187 else if (strlencmp(led_path, "SLED") == 0)
4189 led_supported = TPACPI_LED_570;
4190 else if (strlencmp(led_path, "SYSL") == 0)
4191 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4192 led_supported = TPACPI_LED_OLD;
4195 led_supported = TPACPI_LED_NEW;
4197 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4198 str_supported(led_supported), led_supported);
4200 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4203 printk(TPACPI_ERR "Out of memory for LED data\n");
4207 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4208 tpacpi_leds[i].led = i;
4210 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4211 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4212 if (led_supported == TPACPI_LED_570)
4213 tpacpi_leds[i].led_classdev.brightness_get =
4216 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4218 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4220 rc = led_classdev_register(&tpacpi_pdev->dev,
4221 &tpacpi_leds[i].led_classdev);
4223 tpacpi_leds[i].led_classdev.name = NULL;
4229 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4232 #define str_led_status(s) \
4233 ((s) == TPACPI_LED_OFF ? "off" : \
4234 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4236 static int led_read(char *p)
4240 if (!led_supported) {
4241 len += sprintf(p + len, "status:\t\tnot supported\n");
4244 len += sprintf(p + len, "status:\t\tsupported\n");
4246 if (led_supported == TPACPI_LED_570) {
4249 for (i = 0; i < 8; i++) {
4250 status = led_get_status(i);
4253 len += sprintf(p + len, "%d:\t\t%s\n",
4254 i, str_led_status(status));
4258 len += sprintf(p + len, "commands:\t"
4259 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4264 static int led_write(char *buf)
4268 enum led_status_t s;
4273 while ((cmd = next_cmd(&buf))) {
4274 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4277 if (strstr(cmd, "off")) {
4279 } else if (strstr(cmd, "on")) {
4281 } else if (strstr(cmd, "blink")) {
4282 s = TPACPI_LED_BLINK;
4287 rc = led_set_status(led, s);
4295 static struct ibm_struct led_driver_data = {
4302 /*************************************************************************
4306 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4308 static int __init beep_init(struct ibm_init_struct *iibm)
4310 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4312 TPACPI_ACPIHANDLE_INIT(beep);
4314 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4315 str_supported(beep_handle != NULL));
4317 return (beep_handle)? 0 : 1;
4320 static int beep_read(char *p)
4325 len += sprintf(p + len, "status:\t\tnot supported\n");
4327 len += sprintf(p + len, "status:\t\tsupported\n");
4328 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4334 static int beep_write(char *buf)
4342 while ((cmd = next_cmd(&buf))) {
4343 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4344 beep_cmd >= 0 && beep_cmd <= 17) {
4348 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4355 static struct ibm_struct beep_driver_data = {
4358 .write = beep_write,
4361 /*************************************************************************
4365 enum thermal_access_mode {
4366 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4367 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4368 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4369 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4370 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4373 enum { /* TPACPI_THERMAL_TPEC_* */
4374 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4375 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4376 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4379 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4380 struct ibm_thermal_sensors_struct {
4381 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4384 static enum thermal_access_mode thermal_read_mode;
4386 /* idx is zero-based */
4387 static int thermal_get_sensor(int idx, s32 *value)
4393 t = TP_EC_THERMAL_TMP0;
4395 switch (thermal_read_mode) {
4396 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4397 case TPACPI_THERMAL_TPEC_16:
4398 if (idx >= 8 && idx <= 15) {
4399 t = TP_EC_THERMAL_TMP8;
4404 case TPACPI_THERMAL_TPEC_8:
4406 if (!acpi_ec_read(t + idx, &tmp))
4408 *value = tmp * 1000;
4413 case TPACPI_THERMAL_ACPI_UPDT:
4415 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4416 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4418 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4420 *value = (t - 2732) * 100;
4425 case TPACPI_THERMAL_ACPI_TMP07:
4427 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4428 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4430 if (t > 127 || t < -127)
4431 t = TP_EC_THERMAL_TMP_NA;
4437 case TPACPI_THERMAL_NONE:
4445 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4456 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4459 for (i = 0 ; i < n; i++) {
4460 res = thermal_get_sensor(i, &s->temp[i]);
4468 /* sysfs temp##_input -------------------------------------------------- */
4470 static ssize_t thermal_temp_input_show(struct device *dev,
4471 struct device_attribute *attr,
4474 struct sensor_device_attribute *sensor_attr =
4475 to_sensor_dev_attr(attr);
4476 int idx = sensor_attr->index;
4480 res = thermal_get_sensor(idx, &value);
4483 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4486 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4489 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4490 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4491 thermal_temp_input_show, NULL, _idxB)
4493 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4494 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4495 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4496 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4497 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4498 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4499 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4500 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4501 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4502 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4503 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4504 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4505 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4506 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4507 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4508 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4509 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4512 #define THERMAL_ATTRS(X) \
4513 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4515 static struct attribute *thermal_temp_input_attr[] = {
4535 static const struct attribute_group thermal_temp_input16_group = {
4536 .attrs = thermal_temp_input_attr
4539 static const struct attribute_group thermal_temp_input8_group = {
4540 .attrs = &thermal_temp_input_attr[8]
4543 #undef THERMAL_SENSOR_ATTR_TEMP
4544 #undef THERMAL_ATTRS
4546 /* --------------------------------------------------------------------- */
4548 static int __init thermal_init(struct ibm_init_struct *iibm)
4555 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4557 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4559 if (thinkpad_id.ec_model) {
4561 * Direct EC access mode: sensors at registers
4562 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4563 * non-implemented, thermal sensors return 0x80 when
4568 for (i = 0; i < 8; i++) {
4569 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4575 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4583 /* This is sheer paranoia, but we handle it anyway */
4586 "ThinkPad ACPI EC access misbehaving, "
4587 "falling back to ACPI TMPx access "
4589 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4592 "ThinkPad ACPI EC access misbehaving, "
4593 "disabling thermal sensors access\n");
4594 thermal_read_mode = TPACPI_THERMAL_NONE;
4599 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4601 } else if (acpi_tmp7) {
4602 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4603 /* 600e/x, 770e, 770x */
4604 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4606 /* Standard ACPI TMPx access, max 8 sensors */
4607 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4610 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4611 thermal_read_mode = TPACPI_THERMAL_NONE;
4614 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4615 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4618 switch (thermal_read_mode) {
4619 case TPACPI_THERMAL_TPEC_16:
4620 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4621 &thermal_temp_input16_group);
4625 case TPACPI_THERMAL_TPEC_8:
4626 case TPACPI_THERMAL_ACPI_TMP07:
4627 case TPACPI_THERMAL_ACPI_UPDT:
4628 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4629 &thermal_temp_input8_group);
4633 case TPACPI_THERMAL_NONE:
4641 static void thermal_exit(void)
4643 switch (thermal_read_mode) {
4644 case TPACPI_THERMAL_TPEC_16:
4645 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4646 &thermal_temp_input16_group);
4648 case TPACPI_THERMAL_TPEC_8:
4649 case TPACPI_THERMAL_ACPI_TMP07:
4650 case TPACPI_THERMAL_ACPI_UPDT:
4651 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4652 &thermal_temp_input16_group);
4654 case TPACPI_THERMAL_NONE:
4660 static int thermal_read(char *p)
4664 struct ibm_thermal_sensors_struct t;
4666 n = thermal_get_sensors(&t);
4667 if (unlikely(n < 0))
4670 len += sprintf(p + len, "temperatures:\t");
4673 for (i = 0; i < (n - 1); i++)
4674 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4675 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4677 len += sprintf(p + len, "not supported\n");
4682 static struct ibm_struct thermal_driver_data = {
4684 .read = thermal_read,
4685 .exit = thermal_exit,
4688 /*************************************************************************
4692 static u8 ecdump_regs[256];
4694 static int ecdump_read(char *p)
4700 len += sprintf(p + len, "EC "
4701 " +00 +01 +02 +03 +04 +05 +06 +07"
4702 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4703 for (i = 0; i < 256; i += 16) {
4704 len += sprintf(p + len, "EC 0x%02x:", i);
4705 for (j = 0; j < 16; j++) {
4706 if (!acpi_ec_read(i + j, &v))
4708 if (v != ecdump_regs[i + j])
4709 len += sprintf(p + len, " *%02x", v);
4711 len += sprintf(p + len, " %02x", v);
4712 ecdump_regs[i + j] = v;
4714 len += sprintf(p + len, "\n");
4719 /* These are way too dangerous to advertise openly... */
4721 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4722 " (<offset> is 00-ff, <value> is 00-ff)\n");
4723 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4724 " (<offset> is 00-ff, <value> is 0-255)\n");
4729 static int ecdump_write(char *buf)
4734 while ((cmd = next_cmd(&buf))) {
4735 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4737 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4741 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4742 if (!acpi_ec_write(i, v))
4751 static struct ibm_struct ecdump_driver_data = {
4753 .read = ecdump_read,
4754 .write = ecdump_write,
4755 .flags.experimental = 1,
4758 /*************************************************************************
4759 * Backlight/brightness subdriver
4762 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4765 TP_EC_BACKLIGHT = 0x31,
4767 /* TP_EC_BACKLIGHT bitmasks */
4768 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4769 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4770 TP_EC_BACKLIGHT_MAPSW = 0x20,
4773 static struct backlight_device *ibm_backlight_device;
4774 static int brightness_mode;
4775 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4777 static struct mutex brightness_mutex;
4780 * ThinkPads can read brightness from two places: EC 0x31, or
4781 * CMOS NVRAM byte 0x5E, bits 0-3.
4783 * EC 0x31 has the following layout
4784 * Bit 7: unknown function
4785 * Bit 6: unknown function
4786 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4787 * Bit 4: must be set to zero to avoid problems
4788 * Bit 3-0: backlight brightness level
4790 * brightness_get_raw returns status data in the EC 0x31 layout
4792 static int brightness_get_raw(int *status)
4794 u8 lec = 0, lcmos = 0, level = 0;
4796 if (brightness_mode & 1) {
4797 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4799 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4801 if (brightness_mode & 2) {
4802 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4803 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4804 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4805 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4809 if (brightness_mode == 3) {
4810 *status = lec; /* Prefer EC, CMOS is just a backing store */
4811 lec &= TP_EC_BACKLIGHT_LVLMSK;
4813 tp_warned.bright_cmos_ec_unsync = 0;
4815 if (!tp_warned.bright_cmos_ec_unsync) {
4817 "CMOS NVRAM (%u) and EC (%u) do not "
4818 "agree on display brightness level\n",
4819 (unsigned int) lcmos,
4820 (unsigned int) lec);
4821 tp_warned.bright_cmos_ec_unsync = 1;
4832 /* May return EINTR which can always be mapped to ERESTARTSYS */
4833 static int brightness_set(int value)
4835 int cmos_cmd, inc, i, res;
4839 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4843 res = mutex_lock_interruptible(&brightness_mutex);
4847 res = brightness_get_raw(¤t_value);
4851 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4852 current_value &= TP_EC_BACKLIGHT_LVLMSK;
4854 cmos_cmd = value > current_value ?
4855 TP_CMOS_BRIGHTNESS_UP :
4856 TP_CMOS_BRIGHTNESS_DOWN;
4857 inc = (value > current_value)? 1 : -1;
4860 for (i = current_value; i != value; i += inc) {
4861 if ((brightness_mode & 2) &&
4862 issue_thinkpad_cmos_command(cmos_cmd)) {
4866 if ((brightness_mode & 1) &&
4867 !acpi_ec_write(TP_EC_BACKLIGHT,
4868 (i + inc) | command_bits)) {
4875 mutex_unlock(&brightness_mutex);
4879 /* sysfs backlight class ----------------------------------------------- */
4881 static int brightness_update_status(struct backlight_device *bd)
4883 /* it is the backlight class's job (caller) to handle
4884 * EINTR and other errors properly */
4885 return brightness_set(
4886 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4887 bd->props.power == FB_BLANK_UNBLANK) ?
4888 bd->props.brightness : 0);
4891 static int brightness_get(struct backlight_device *bd)
4895 res = brightness_get_raw(&status);
4897 return 0; /* FIXME: teach backlight about error handling */
4899 return status & TP_EC_BACKLIGHT_LVLMSK;
4902 static struct backlight_ops ibm_backlight_data = {
4903 .get_brightness = brightness_get,
4904 .update_status = brightness_update_status,
4907 /* --------------------------------------------------------------------- */
4909 static int __init brightness_init(struct ibm_init_struct *iibm)
4913 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4915 mutex_init(&brightness_mutex);
4918 * We always attempt to detect acpi support, so as to switch
4919 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4920 * going to publish a backlight interface
4922 b = tpacpi_check_std_acpi_brightness_support();
4924 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4925 printk(TPACPI_NOTICE
4926 "Lenovo BIOS switched to ACPI backlight "
4929 if (brightness_enable > 1) {
4930 printk(TPACPI_NOTICE
4931 "standard ACPI backlight interface "
4932 "available, not loading native one...\n");
4937 if (!brightness_enable) {
4938 dbg_printk(TPACPI_DBG_INIT,
4939 "brightness support disabled by "
4940 "module parameter\n");
4946 "Unsupported brightness interface, "
4947 "please contact %s\n", TPACPI_MAIL);
4951 tp_features.bright_16levels = 1;
4953 if (!brightness_mode) {
4954 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4955 brightness_mode = 2;
4957 brightness_mode = 3;
4959 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4963 if (brightness_mode > 3)
4966 if (brightness_get_raw(&b) < 0)
4969 if (tp_features.bright_16levels)
4971 "detected a 16-level brightness capable ThinkPad\n");
4973 ibm_backlight_device = backlight_device_register(
4974 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4975 &ibm_backlight_data);
4976 if (IS_ERR(ibm_backlight_device)) {
4977 printk(TPACPI_ERR "Could not register backlight device\n");
4978 return PTR_ERR(ibm_backlight_device);
4980 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4982 ibm_backlight_device->props.max_brightness =
4983 (tp_features.bright_16levels)? 15 : 7;
4984 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
4985 backlight_update_status(ibm_backlight_device);
4990 static void brightness_exit(void)
4992 if (ibm_backlight_device) {
4993 vdbg_printk(TPACPI_DBG_EXIT,
4994 "calling backlight_device_unregister()\n");
4995 backlight_device_unregister(ibm_backlight_device);
4999 static int brightness_read(char *p)
5004 level = brightness_get(NULL);
5006 len += sprintf(p + len, "level:\t\tunreadable\n");
5008 len += sprintf(p + len, "level:\t\t%d\n", level);
5009 len += sprintf(p + len, "commands:\tup, down\n");
5010 len += sprintf(p + len, "commands:\tlevel <level>"
5011 " (<level> is 0-%d)\n",
5012 (tp_features.bright_16levels) ? 15 : 7);
5018 static int brightness_write(char *buf)
5023 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5025 level = brightness_get(NULL);
5029 while ((cmd = next_cmd(&buf))) {
5030 if (strlencmp(cmd, "up") == 0) {
5031 if (level < max_level)
5033 } else if (strlencmp(cmd, "down") == 0) {
5036 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5037 level >= 0 && level <= max_level) {
5044 * Now we know what the final level should be, so we try to set it.
5045 * Doing it this way makes the syscall restartable in case of EINTR
5047 rc = brightness_set(level);
5048 return (rc == -EINTR)? ERESTARTSYS : rc;
5051 static struct ibm_struct brightness_driver_data = {
5052 .name = "brightness",
5053 .read = brightness_read,
5054 .write = brightness_write,
5055 .exit = brightness_exit,
5058 /*************************************************************************
5062 static int volume_offset = 0x30;
5064 static int volume_read(char *p)
5069 if (!acpi_ec_read(volume_offset, &level)) {
5070 len += sprintf(p + len, "level:\t\tunreadable\n");
5072 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5073 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5074 len += sprintf(p + len, "commands:\tup, down, mute\n");
5075 len += sprintf(p + len, "commands:\tlevel <level>"
5076 " (<level> is 0-15)\n");
5082 static int volume_write(char *buf)
5084 int cmos_cmd, inc, i;
5086 int new_level, new_mute;
5089 while ((cmd = next_cmd(&buf))) {
5090 if (!acpi_ec_read(volume_offset, &level))
5092 new_mute = mute = level & 0x40;
5093 new_level = level = level & 0xf;
5095 if (strlencmp(cmd, "up") == 0) {
5099 new_level = level == 15 ? 15 : level + 1;
5100 } else if (strlencmp(cmd, "down") == 0) {
5104 new_level = level == 0 ? 0 : level - 1;
5105 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5106 new_level >= 0 && new_level <= 15) {
5108 } else if (strlencmp(cmd, "mute") == 0) {
5113 if (new_level != level) {
5114 /* mute doesn't change */
5116 cmos_cmd = (new_level > level) ?
5117 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5118 inc = new_level > level ? 1 : -1;
5120 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5121 !acpi_ec_write(volume_offset, level)))
5124 for (i = level; i != new_level; i += inc)
5125 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5126 !acpi_ec_write(volume_offset, i + inc))
5130 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5131 !acpi_ec_write(volume_offset, new_level + mute))) {
5136 if (new_mute != mute) {
5137 /* level doesn't change */
5139 cmos_cmd = (new_mute) ?
5140 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5142 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5143 !acpi_ec_write(volume_offset, level + new_mute))
5151 static struct ibm_struct volume_driver_data = {
5153 .read = volume_read,
5154 .write = volume_write,
5157 /*************************************************************************
5164 * TPACPI_FAN_RD_ACPI_GFAN:
5165 * ACPI GFAN method: returns fan level
5167 * see TPACPI_FAN_WR_ACPI_SFAN
5168 * EC 0x2f (HFSP) not available if GFAN exists
5170 * TPACPI_FAN_WR_ACPI_SFAN:
5171 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5173 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5176 * TPACPI_FAN_WR_TPEC:
5177 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5178 * Supported on almost all ThinkPads
5180 * Fan speed changes of any sort (including those caused by the
5181 * disengaged mode) are usually done slowly by the firmware as the
5182 * maximum ammount of fan duty cycle change per second seems to be
5185 * Reading is not available if GFAN exists.
5186 * Writing is not available if SFAN exists.
5189 * 7 automatic mode engaged;
5190 * (default operation mode of the ThinkPad)
5191 * fan level is ignored in this mode.
5192 * 6 full speed mode (takes precedence over bit 7);
5193 * not available on all thinkpads. May disable
5194 * the tachometer while the fan controller ramps up
5195 * the speed (which can take up to a few *minutes*).
5196 * Speeds up fan to 100% duty-cycle, which is far above
5197 * the standard RPM levels. It is not impossible that
5198 * it could cause hardware damage.
5199 * 5-3 unused in some models. Extra bits for fan level
5200 * in others, but still useless as all values above
5201 * 7 map to the same speed as level 7 in these models.
5202 * 2-0 fan level (0..7 usually)
5204 * 0x07 = max (set when temperatures critical)
5205 * Some ThinkPads may have other levels, see
5206 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5208 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5209 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5210 * does so, its initial value is meaningless (0x07).
5212 * For firmware bugs, refer to:
5213 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5217 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5218 * Main fan tachometer reading (in RPM)
5220 * This register is present on all ThinkPads with a new-style EC, and
5221 * it is known not to be present on the A21m/e, and T22, as there is
5222 * something else in offset 0x84 according to the ACPI DSDT. Other
5223 * ThinkPads from this same time period (and earlier) probably lack the
5224 * tachometer as well.
5226 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5227 * was never fixed by IBM to report the EC firmware version string
5228 * probably support the tachometer (like the early X models), so
5229 * detecting it is quite hard. We need more data to know for sure.
5231 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5234 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5237 * For firmware bugs, refer to:
5238 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5240 * TPACPI_FAN_WR_ACPI_FANS:
5241 * ThinkPad X31, X40, X41. Not available in the X60.
5243 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5244 * high speed. ACPI DSDT seems to map these three speeds to levels
5245 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5246 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5248 * The speeds are stored on handles
5249 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5251 * There are three default speed sets, acessible as handles:
5252 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5254 * ACPI DSDT switches which set is in use depending on various
5257 * TPACPI_FAN_WR_TPEC is also available and should be used to
5258 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5259 * but the ACPI tables just mention level 7.
5262 enum { /* Fan control constants */
5263 fan_status_offset = 0x2f, /* EC register 0x2f */
5264 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5265 * 0x84 must be read before 0x85 */
5267 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5268 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5270 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5273 enum fan_status_access_mode {
5274 TPACPI_FAN_NONE = 0, /* No fan status or control */
5275 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5276 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5279 enum fan_control_access_mode {
5280 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5281 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5282 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5283 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5286 enum fan_control_commands {
5287 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5288 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5289 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5290 * and also watchdog cmd */
5293 static int fan_control_allowed;
5295 static enum fan_status_access_mode fan_status_access_mode;
5296 static enum fan_control_access_mode fan_control_access_mode;
5297 static enum fan_control_commands fan_control_commands;
5299 static u8 fan_control_initial_status;
5300 static u8 fan_control_desired_level;
5301 static int fan_watchdog_maxinterval;
5303 static struct mutex fan_mutex;
5305 static void fan_watchdog_fire(struct work_struct *ignored);
5306 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5308 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5309 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5310 "\\FSPD", /* 600e/x, 770e, 770x */
5312 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5313 "JFNS", /* 770x-JL */
5317 * Call with fan_mutex held
5319 static void fan_update_desired_level(u8 status)
5322 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5324 fan_control_desired_level = 7;
5326 fan_control_desired_level = status;
5330 static int fan_get_status(u8 *status)
5335 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5337 switch (fan_status_access_mode) {
5338 case TPACPI_FAN_RD_ACPI_GFAN:
5339 /* 570, 600e/x, 770e, 770x */
5341 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5349 case TPACPI_FAN_RD_TPEC:
5350 /* all except 570, 600e/x, 770e, 770x */
5351 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5366 static int fan_get_status_safe(u8 *status)
5371 if (mutex_lock_interruptible(&fan_mutex))
5372 return -ERESTARTSYS;
5373 rc = fan_get_status(&s);
5375 fan_update_desired_level(s);
5376 mutex_unlock(&fan_mutex);
5384 static int fan_get_speed(unsigned int *speed)
5388 switch (fan_status_access_mode) {
5389 case TPACPI_FAN_RD_TPEC:
5390 /* all except 570, 600e/x, 770e, 770x */
5391 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5392 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5396 *speed = (hi << 8) | lo;
5407 static int fan_set_level(int level)
5409 if (!fan_control_allowed)
5412 switch (fan_control_access_mode) {
5413 case TPACPI_FAN_WR_ACPI_SFAN:
5414 if (level >= 0 && level <= 7) {
5415 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5421 case TPACPI_FAN_WR_ACPI_FANS:
5422 case TPACPI_FAN_WR_TPEC:
5423 if ((level != TP_EC_FAN_AUTO) &&
5424 (level != TP_EC_FAN_FULLSPEED) &&
5425 ((level < 0) || (level > 7)))
5428 /* safety net should the EC not support AUTO
5429 * or FULLSPEED mode bits and just ignore them */
5430 if (level & TP_EC_FAN_FULLSPEED)
5431 level |= 7; /* safety min speed 7 */
5432 else if (level & TP_EC_FAN_AUTO)
5433 level |= 4; /* safety min speed 4 */
5435 if (!acpi_ec_write(fan_status_offset, level))
5438 tp_features.fan_ctrl_status_undef = 0;
5447 static int fan_set_level_safe(int level)
5451 if (!fan_control_allowed)
5454 if (mutex_lock_interruptible(&fan_mutex))
5455 return -ERESTARTSYS;
5457 if (level == TPACPI_FAN_LAST_LEVEL)
5458 level = fan_control_desired_level;
5460 rc = fan_set_level(level);
5462 fan_update_desired_level(level);
5464 mutex_unlock(&fan_mutex);
5468 static int fan_set_enable(void)
5473 if (!fan_control_allowed)
5476 if (mutex_lock_interruptible(&fan_mutex))
5477 return -ERESTARTSYS;
5479 switch (fan_control_access_mode) {
5480 case TPACPI_FAN_WR_ACPI_FANS:
5481 case TPACPI_FAN_WR_TPEC:
5482 rc = fan_get_status(&s);
5486 /* Don't go out of emergency fan mode */
5489 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5492 if (!acpi_ec_write(fan_status_offset, s))
5495 tp_features.fan_ctrl_status_undef = 0;
5500 case TPACPI_FAN_WR_ACPI_SFAN:
5501 rc = fan_get_status(&s);
5507 /* Set fan to at least level 4 */
5510 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5520 mutex_unlock(&fan_mutex);
5524 static int fan_set_disable(void)
5528 if (!fan_control_allowed)
5531 if (mutex_lock_interruptible(&fan_mutex))
5532 return -ERESTARTSYS;
5535 switch (fan_control_access_mode) {
5536 case TPACPI_FAN_WR_ACPI_FANS:
5537 case TPACPI_FAN_WR_TPEC:
5538 if (!acpi_ec_write(fan_status_offset, 0x00))
5541 fan_control_desired_level = 0;
5542 tp_features.fan_ctrl_status_undef = 0;
5546 case TPACPI_FAN_WR_ACPI_SFAN:
5547 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5550 fan_control_desired_level = 0;
5558 mutex_unlock(&fan_mutex);
5562 static int fan_set_speed(int speed)
5566 if (!fan_control_allowed)
5569 if (mutex_lock_interruptible(&fan_mutex))
5570 return -ERESTARTSYS;
5573 switch (fan_control_access_mode) {
5574 case TPACPI_FAN_WR_ACPI_FANS:
5575 if (speed >= 0 && speed <= 65535) {
5576 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5577 speed, speed, speed))
5587 mutex_unlock(&fan_mutex);
5591 static void fan_watchdog_reset(void)
5593 static int fan_watchdog_active;
5595 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5598 if (fan_watchdog_active)
5599 cancel_delayed_work(&fan_watchdog_task);
5601 if (fan_watchdog_maxinterval > 0 &&
5602 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5603 fan_watchdog_active = 1;
5604 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5605 msecs_to_jiffies(fan_watchdog_maxinterval
5608 "failed to queue the fan watchdog, "
5609 "watchdog will not trigger\n");
5612 fan_watchdog_active = 0;
5615 static void fan_watchdog_fire(struct work_struct *ignored)
5619 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5622 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5623 rc = fan_set_enable();
5625 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5626 "will try again later...\n", -rc);
5627 /* reschedule for later */
5628 fan_watchdog_reset();
5633 * SYSFS fan layout: hwmon compatible (device)
5636 * 0: "disengaged" mode
5638 * 2: native EC "auto" mode (recommended, hardware default)
5640 * pwm*: set speed in manual mode, ignored otherwise.
5641 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5644 * fan*_input: tachometer reading, RPM
5647 * SYSFS fan layout: extensions
5649 * fan_watchdog (driver):
5650 * fan watchdog interval in seconds, 0 disables (default), max 120
5653 /* sysfs fan pwm1_enable ----------------------------------------------- */
5654 static ssize_t fan_pwm1_enable_show(struct device *dev,
5655 struct device_attribute *attr,
5661 res = fan_get_status_safe(&status);
5665 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5666 if (status != fan_control_initial_status) {
5667 tp_features.fan_ctrl_status_undef = 0;
5669 /* Return most likely status. In fact, it
5670 * might be the only possible status */
5671 status = TP_EC_FAN_AUTO;
5675 if (status & TP_EC_FAN_FULLSPEED) {
5677 } else if (status & TP_EC_FAN_AUTO) {
5682 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5685 static ssize_t fan_pwm1_enable_store(struct device *dev,
5686 struct device_attribute *attr,
5687 const char *buf, size_t count)
5692 if (parse_strtoul(buf, 2, &t))
5697 level = TP_EC_FAN_FULLSPEED;
5700 level = TPACPI_FAN_LAST_LEVEL;
5703 level = TP_EC_FAN_AUTO;
5706 /* reserved for software-controlled auto mode */
5712 res = fan_set_level_safe(level);
5718 fan_watchdog_reset();
5723 static struct device_attribute dev_attr_fan_pwm1_enable =
5724 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5725 fan_pwm1_enable_show, fan_pwm1_enable_store);
5727 /* sysfs fan pwm1 ------------------------------------------------------ */
5728 static ssize_t fan_pwm1_show(struct device *dev,
5729 struct device_attribute *attr,
5735 res = fan_get_status_safe(&status);
5739 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5740 if (status != fan_control_initial_status) {
5741 tp_features.fan_ctrl_status_undef = 0;
5743 status = TP_EC_FAN_AUTO;
5748 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5749 status = fan_control_desired_level;
5754 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5757 static ssize_t fan_pwm1_store(struct device *dev,
5758 struct device_attribute *attr,
5759 const char *buf, size_t count)
5763 u8 status, newlevel;
5765 if (parse_strtoul(buf, 255, &s))
5768 /* scale down from 0-255 to 0-7 */
5769 newlevel = (s >> 5) & 0x07;
5771 if (mutex_lock_interruptible(&fan_mutex))
5772 return -ERESTARTSYS;
5774 rc = fan_get_status(&status);
5775 if (!rc && (status &
5776 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5777 rc = fan_set_level(newlevel);
5781 fan_update_desired_level(newlevel);
5782 fan_watchdog_reset();
5786 mutex_unlock(&fan_mutex);
5787 return (rc)? rc : count;
5790 static struct device_attribute dev_attr_fan_pwm1 =
5791 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5792 fan_pwm1_show, fan_pwm1_store);
5794 /* sysfs fan fan1_input ------------------------------------------------ */
5795 static ssize_t fan_fan1_input_show(struct device *dev,
5796 struct device_attribute *attr,
5802 res = fan_get_speed(&speed);
5806 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5809 static struct device_attribute dev_attr_fan_fan1_input =
5810 __ATTR(fan1_input, S_IRUGO,
5811 fan_fan1_input_show, NULL);
5813 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5814 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5817 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5820 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5821 const char *buf, size_t count)
5825 if (parse_strtoul(buf, 120, &t))
5828 if (!fan_control_allowed)
5831 fan_watchdog_maxinterval = t;
5832 fan_watchdog_reset();
5837 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5838 fan_fan_watchdog_show, fan_fan_watchdog_store);
5840 /* --------------------------------------------------------------------- */
5841 static struct attribute *fan_attributes[] = {
5842 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5843 &dev_attr_fan_fan1_input.attr,
5847 static const struct attribute_group fan_attr_group = {
5848 .attrs = fan_attributes,
5851 static int __init fan_init(struct ibm_init_struct *iibm)
5855 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5857 mutex_init(&fan_mutex);
5858 fan_status_access_mode = TPACPI_FAN_NONE;
5859 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5860 fan_control_commands = 0;
5861 fan_watchdog_maxinterval = 0;
5862 tp_features.fan_ctrl_status_undef = 0;
5863 fan_control_desired_level = 7;
5865 TPACPI_ACPIHANDLE_INIT(fans);
5866 TPACPI_ACPIHANDLE_INIT(gfan);
5867 TPACPI_ACPIHANDLE_INIT(sfan);
5870 /* 570, 600e/x, 770e, 770x */
5871 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5873 /* all other ThinkPads: note that even old-style
5874 * ThinkPad ECs supports the fan control register */
5875 if (likely(acpi_ec_read(fan_status_offset,
5876 &fan_control_initial_status))) {
5877 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5879 /* In some ThinkPads, neither the EC nor the ACPI
5880 * DSDT initialize the fan status, and it ends up
5881 * being set to 0x07 when it *could* be either
5884 * Enable for TP-1Y (T43), TP-78 (R51e),
5885 * TP-76 (R52), TP-70 (T43, R52), which are known
5887 if (fan_control_initial_status == 0x07) {
5888 switch (thinkpad_id.ec_model) {
5889 case 0x5931: /* TP-1Y */
5890 case 0x3837: /* TP-78 */
5891 case 0x3637: /* TP-76 */
5892 case 0x3037: /* TP-70 */
5893 printk(TPACPI_NOTICE
5894 "fan_init: initial fan status "
5895 "is unknown, assuming it is "
5897 tp_features.fan_ctrl_status_undef = 1;
5903 "ThinkPad ACPI EC access misbehaving, "
5904 "fan status and control unavailable\n");
5911 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5912 fan_control_commands |=
5913 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5916 /* gfan without sfan means no fan control */
5917 /* all other models implement TP EC 0x2f control */
5921 fan_control_access_mode =
5922 TPACPI_FAN_WR_ACPI_FANS;
5923 fan_control_commands |=
5924 TPACPI_FAN_CMD_SPEED |
5925 TPACPI_FAN_CMD_LEVEL |
5926 TPACPI_FAN_CMD_ENABLE;
5928 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5929 fan_control_commands |=
5930 TPACPI_FAN_CMD_LEVEL |
5931 TPACPI_FAN_CMD_ENABLE;
5936 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5937 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5938 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5939 fan_status_access_mode, fan_control_access_mode);
5941 /* fan control master switch */
5942 if (!fan_control_allowed) {
5943 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5944 fan_control_commands = 0;
5945 dbg_printk(TPACPI_DBG_INIT,
5946 "fan control features disabled by parameter\n");
5949 /* update fan_control_desired_level */
5950 if (fan_status_access_mode != TPACPI_FAN_NONE)
5951 fan_get_status_safe(NULL);
5953 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5954 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5955 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5960 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5961 &driver_attr_fan_watchdog);
5963 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5972 static void fan_exit(void)
5974 vdbg_printk(TPACPI_DBG_EXIT,
5975 "cancelling any pending fan watchdog tasks\n");
5977 /* FIXME: can we really do this unconditionally? */
5978 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5979 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5980 &driver_attr_fan_watchdog);
5982 cancel_delayed_work(&fan_watchdog_task);
5983 flush_workqueue(tpacpi_wq);
5986 static int fan_read(char *p)
5991 unsigned int speed = 0;
5993 switch (fan_status_access_mode) {
5994 case TPACPI_FAN_RD_ACPI_GFAN:
5995 /* 570, 600e/x, 770e, 770x */
5996 rc = fan_get_status_safe(&status);
6000 len += sprintf(p + len, "status:\t\t%s\n"
6002 (status != 0) ? "enabled" : "disabled", status);
6005 case TPACPI_FAN_RD_TPEC:
6006 /* all except 570, 600e/x, 770e, 770x */
6007 rc = fan_get_status_safe(&status);
6011 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6012 if (status != fan_control_initial_status)
6013 tp_features.fan_ctrl_status_undef = 0;
6015 /* Return most likely status. In fact, it
6016 * might be the only possible status */
6017 status = TP_EC_FAN_AUTO;
6020 len += sprintf(p + len, "status:\t\t%s\n",
6021 (status != 0) ? "enabled" : "disabled");
6023 rc = fan_get_speed(&speed);
6027 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6029 if (status & TP_EC_FAN_FULLSPEED)
6030 /* Disengaged mode takes precedence */
6031 len += sprintf(p + len, "level:\t\tdisengaged\n");
6032 else if (status & TP_EC_FAN_AUTO)
6033 len += sprintf(p + len, "level:\t\tauto\n");
6035 len += sprintf(p + len, "level:\t\t%d\n", status);
6038 case TPACPI_FAN_NONE:
6040 len += sprintf(p + len, "status:\t\tnot supported\n");
6043 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6044 len += sprintf(p + len, "commands:\tlevel <level>");
6046 switch (fan_control_access_mode) {
6047 case TPACPI_FAN_WR_ACPI_SFAN:
6048 len += sprintf(p + len, " (<level> is 0-7)\n");
6052 len += sprintf(p + len, " (<level> is 0-7, "
6053 "auto, disengaged, full-speed)\n");
6058 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6059 len += sprintf(p + len, "commands:\tenable, disable\n"
6060 "commands:\twatchdog <timeout> (<timeout> "
6061 "is 0 (off), 1-120 (seconds))\n");
6063 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6064 len += sprintf(p + len, "commands:\tspeed <speed>"
6065 " (<speed> is 0-65535)\n");
6070 static int fan_write_cmd_level(const char *cmd, int *rc)
6074 if (strlencmp(cmd, "level auto") == 0)
6075 level = TP_EC_FAN_AUTO;
6076 else if ((strlencmp(cmd, "level disengaged") == 0) |
6077 (strlencmp(cmd, "level full-speed") == 0))
6078 level = TP_EC_FAN_FULLSPEED;
6079 else if (sscanf(cmd, "level %d", &level) != 1)
6082 *rc = fan_set_level_safe(level);
6084 printk(TPACPI_ERR "level command accepted for unsupported "
6085 "access mode %d", fan_control_access_mode);
6090 static int fan_write_cmd_enable(const char *cmd, int *rc)
6092 if (strlencmp(cmd, "enable") != 0)
6095 *rc = fan_set_enable();
6097 printk(TPACPI_ERR "enable command accepted for unsupported "
6098 "access mode %d", fan_control_access_mode);
6103 static int fan_write_cmd_disable(const char *cmd, int *rc)
6105 if (strlencmp(cmd, "disable") != 0)
6108 *rc = fan_set_disable();
6110 printk(TPACPI_ERR "disable command accepted for unsupported "
6111 "access mode %d", fan_control_access_mode);
6116 static int fan_write_cmd_speed(const char *cmd, int *rc)
6121 * Support speed <low> <medium> <high> ? */
6123 if (sscanf(cmd, "speed %d", &speed) != 1)
6126 *rc = fan_set_speed(speed);
6128 printk(TPACPI_ERR "speed command accepted for unsupported "
6129 "access mode %d", fan_control_access_mode);
6134 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6138 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6141 if (interval < 0 || interval > 120)
6144 fan_watchdog_maxinterval = interval;
6149 static int fan_write(char *buf)
6154 while (!rc && (cmd = next_cmd(&buf))) {
6155 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6156 fan_write_cmd_level(cmd, &rc)) &&
6157 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6158 (fan_write_cmd_enable(cmd, &rc) ||
6159 fan_write_cmd_disable(cmd, &rc) ||
6160 fan_write_cmd_watchdog(cmd, &rc))) &&
6161 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6162 fan_write_cmd_speed(cmd, &rc))
6166 fan_watchdog_reset();
6172 static struct ibm_struct fan_driver_data = {
6179 /****************************************************************************
6180 ****************************************************************************
6184 ****************************************************************************
6185 ****************************************************************************/
6187 /* sysfs name ---------------------------------------------------------- */
6188 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6189 struct device_attribute *attr,
6192 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6195 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6196 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6198 /* --------------------------------------------------------------------- */
6201 static struct proc_dir_entry *proc_dir;
6204 * Module and infrastructure proble, init and exit handling
6207 static int force_load;
6209 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6210 static const char * __init str_supported(int is_supported)
6212 static char text_unsupported[] __initdata = "not supported";
6214 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6216 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6218 static void ibm_exit(struct ibm_struct *ibm)
6220 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6222 list_del_init(&ibm->all_drivers);
6224 if (ibm->flags.acpi_notify_installed) {
6225 dbg_printk(TPACPI_DBG_EXIT,
6226 "%s: acpi_remove_notify_handler\n", ibm->name);
6228 acpi_remove_notify_handler(*ibm->acpi->handle,
6230 dispatch_acpi_notify);
6231 ibm->flags.acpi_notify_installed = 0;
6232 ibm->flags.acpi_notify_installed = 0;
6235 if (ibm->flags.proc_created) {
6236 dbg_printk(TPACPI_DBG_EXIT,
6237 "%s: remove_proc_entry\n", ibm->name);
6238 remove_proc_entry(ibm->name, proc_dir);
6239 ibm->flags.proc_created = 0;
6242 if (ibm->flags.acpi_driver_registered) {
6243 dbg_printk(TPACPI_DBG_EXIT,
6244 "%s: acpi_bus_unregister_driver\n", ibm->name);
6246 acpi_bus_unregister_driver(ibm->acpi->driver);
6247 kfree(ibm->acpi->driver);
6248 ibm->acpi->driver = NULL;
6249 ibm->flags.acpi_driver_registered = 0;
6252 if (ibm->flags.init_called && ibm->exit) {
6254 ibm->flags.init_called = 0;
6257 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6260 static int __init ibm_init(struct ibm_init_struct *iibm)
6263 struct ibm_struct *ibm = iibm->data;
6264 struct proc_dir_entry *entry;
6266 BUG_ON(ibm == NULL);
6268 INIT_LIST_HEAD(&ibm->all_drivers);
6270 if (ibm->flags.experimental && !experimental)
6273 dbg_printk(TPACPI_DBG_INIT,
6274 "probing for %s\n", ibm->name);
6277 ret = iibm->init(iibm);
6279 return 0; /* probe failed */
6283 ibm->flags.init_called = 1;
6287 if (ibm->acpi->hid) {
6288 ret = register_tpacpi_subdriver(ibm);
6293 if (ibm->acpi->notify) {
6294 ret = setup_acpi_notify(ibm);
6295 if (ret == -ENODEV) {
6296 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6306 dbg_printk(TPACPI_DBG_INIT,
6307 "%s installed\n", ibm->name);
6310 entry = create_proc_entry(ibm->name,
6311 S_IFREG | S_IRUGO | S_IWUSR,
6314 printk(TPACPI_ERR "unable to create proc entry %s\n",
6319 entry->owner = THIS_MODULE;
6321 entry->read_proc = &dispatch_procfs_read;
6323 entry->write_proc = &dispatch_procfs_write;
6324 ibm->flags.proc_created = 1;
6327 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6332 dbg_printk(TPACPI_DBG_INIT,
6333 "%s: at error exit path with result %d\n",
6337 return (ret < 0)? ret : 0;
6342 /* returns 0 - probe ok, or < 0 - probe error.
6343 * Probe ok doesn't mean thinkpad found.
6344 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6345 static int __must_check __init get_thinkpad_model_data(
6346 struct thinkpad_id_data *tp)
6348 const struct dmi_device *dev = NULL;
6349 char ec_fw_string[18];
6355 memset(tp, 0, sizeof(*tp));
6357 if (dmi_name_in_vendors("IBM"))
6358 tp->vendor = PCI_VENDOR_ID_IBM;
6359 else if (dmi_name_in_vendors("LENOVO"))
6360 tp->vendor = PCI_VENDOR_ID_LENOVO;
6364 s = dmi_get_system_info(DMI_BIOS_VERSION);
6365 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6366 if (s && !tp->bios_version_str)
6368 if (!tp->bios_version_str)
6370 tp->bios_model = tp->bios_version_str[0]
6371 | (tp->bios_version_str[1] << 8);
6374 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6375 * X32 or newer, all Z series; Some models must have an
6376 * up-to-date BIOS or they will not be detected.
6378 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6380 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6381 if (sscanf(dev->name,
6382 "IBM ThinkPad Embedded Controller -[%17c",
6383 ec_fw_string) == 1) {
6384 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6385 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6387 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6388 if (!tp->ec_version_str)
6390 tp->ec_model = ec_fw_string[0]
6391 | (ec_fw_string[1] << 8);
6396 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6397 if (s && !strnicmp(s, "ThinkPad", 8)) {
6398 tp->model_str = kstrdup(s, GFP_KERNEL);
6403 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6404 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6405 if (s && !tp->nummodel_str)
6411 static int __init probe_for_thinkpad(void)
6419 * Non-ancient models have better DMI tagging, but very old models
6422 is_thinkpad = (thinkpad_id.model_str != NULL);
6424 /* ec is required because many other handles are relative to it */
6425 TPACPI_ACPIHANDLE_INIT(ec);
6429 "Not yet supported ThinkPad detected!\n");
6434 * Risks a regression on very old machines, but reduces potential
6435 * false positives a damn great deal
6438 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6440 if (!is_thinkpad && !force_load)
6447 /* Module init, exit, parameters */
6449 static struct ibm_init_struct ibms_init[] __initdata = {
6451 .init = thinkpad_acpi_driver_init,
6452 .data = &thinkpad_acpi_driver_data,
6455 .init = hotkey_init,
6456 .data = &hotkey_driver_data,
6459 .init = bluetooth_init,
6460 .data = &bluetooth_driver_data,
6464 .data = &wan_driver_data,
6466 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6469 .data = &video_driver_data,
6474 .data = &light_driver_data,
6476 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6479 .data = &dock_driver_data[0],
6483 .data = &dock_driver_data[1],
6486 #ifdef CONFIG_THINKPAD_ACPI_BAY
6489 .data = &bay_driver_data,
6494 .data = &cmos_driver_data,
6498 .data = &led_driver_data,
6502 .data = &beep_driver_data,
6505 .init = thermal_init,
6506 .data = &thermal_driver_data,
6509 .data = &ecdump_driver_data,
6512 .init = brightness_init,
6513 .data = &brightness_driver_data,
6516 .data = &volume_driver_data,
6520 .data = &fan_driver_data,
6524 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6527 struct ibm_struct *ibm;
6529 if (!kp || !kp->name || !val)
6532 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6533 ibm = ibms_init[i].data;
6534 WARN_ON(ibm == NULL);
6536 if (!ibm || !ibm->name)
6539 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6540 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6542 strcpy(ibms_init[i].param, val);
6543 strcat(ibms_init[i].param, ",");
6551 module_param(experimental, int, 0);
6552 MODULE_PARM_DESC(experimental,
6553 "Enables experimental features when non-zero");
6555 module_param_named(debug, dbg_level, uint, 0);
6556 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6558 module_param(force_load, bool, 0);
6559 MODULE_PARM_DESC(force_load,
6560 "Attempts to load the driver even on a "
6561 "mis-identified ThinkPad when true");
6563 module_param_named(fan_control, fan_control_allowed, bool, 0);
6564 MODULE_PARM_DESC(fan_control,
6565 "Enables setting fan parameters features when true");
6567 module_param_named(brightness_mode, brightness_mode, int, 0);
6568 MODULE_PARM_DESC(brightness_mode,
6569 "Selects brightness control strategy: "
6570 "0=auto, 1=EC, 2=CMOS, 3=both");
6572 module_param(brightness_enable, uint, 0);
6573 MODULE_PARM_DESC(brightness_enable,
6574 "Enables backlight control when 1, disables when 0");
6576 module_param(hotkey_report_mode, uint, 0);
6577 MODULE_PARM_DESC(hotkey_report_mode,
6578 "used for backwards compatibility with userspace, "
6579 "see documentation");
6581 #define TPACPI_PARAM(feature) \
6582 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6583 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6584 "at module load, see documentation")
6586 TPACPI_PARAM(hotkey);
6587 TPACPI_PARAM(bluetooth);
6588 TPACPI_PARAM(video);
6589 TPACPI_PARAM(light);
6590 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6593 #ifdef CONFIG_THINKPAD_ACPI_BAY
6595 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6599 TPACPI_PARAM(ecdump);
6600 TPACPI_PARAM(brightness);
6601 TPACPI_PARAM(volume);
6604 static void thinkpad_acpi_module_exit(void)
6606 struct ibm_struct *ibm, *itmp;
6608 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6610 list_for_each_entry_safe_reverse(ibm, itmp,
6611 &tpacpi_all_drivers,
6616 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6618 if (tpacpi_inputdev) {
6619 if (tp_features.input_device_registered)
6620 input_unregister_device(tpacpi_inputdev);
6622 input_free_device(tpacpi_inputdev);
6626 hwmon_device_unregister(tpacpi_hwmon);
6628 if (tp_features.sensors_pdev_attrs_registered)
6629 device_remove_file(&tpacpi_sensors_pdev->dev,
6630 &dev_attr_thinkpad_acpi_pdev_name);
6631 if (tpacpi_sensors_pdev)
6632 platform_device_unregister(tpacpi_sensors_pdev);
6634 platform_device_unregister(tpacpi_pdev);
6636 if (tp_features.sensors_pdrv_attrs_registered)
6637 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6638 if (tp_features.platform_drv_attrs_registered)
6639 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6641 if (tp_features.sensors_pdrv_registered)
6642 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6644 if (tp_features.platform_drv_registered)
6645 platform_driver_unregister(&tpacpi_pdriver);
6648 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6651 destroy_workqueue(tpacpi_wq);
6653 kfree(thinkpad_id.bios_version_str);
6654 kfree(thinkpad_id.ec_version_str);
6655 kfree(thinkpad_id.model_str);
6659 static int __init thinkpad_acpi_module_init(void)
6663 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6665 /* Parameter checking */
6666 if (hotkey_report_mode > 2)
6669 /* Driver-level probe */
6671 ret = get_thinkpad_model_data(&thinkpad_id);
6674 "unable to get DMI data: %d\n", ret);
6675 thinkpad_acpi_module_exit();
6678 ret = probe_for_thinkpad();
6680 thinkpad_acpi_module_exit();
6684 /* Driver initialization */
6686 TPACPI_ACPIHANDLE_INIT(ecrd);
6687 TPACPI_ACPIHANDLE_INIT(ecwr);
6689 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6691 thinkpad_acpi_module_exit();
6695 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6698 "unable to create proc dir " TPACPI_PROC_DIR);
6699 thinkpad_acpi_module_exit();
6702 proc_dir->owner = THIS_MODULE;
6704 ret = platform_driver_register(&tpacpi_pdriver);
6707 "unable to register main platform driver\n");
6708 thinkpad_acpi_module_exit();
6711 tp_features.platform_drv_registered = 1;
6713 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6716 "unable to register hwmon platform driver\n");
6717 thinkpad_acpi_module_exit();
6720 tp_features.sensors_pdrv_registered = 1;
6722 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6724 tp_features.platform_drv_attrs_registered = 1;
6725 ret = tpacpi_create_driver_attributes(
6726 &tpacpi_hwmon_pdriver.driver);
6730 "unable to create sysfs driver attributes\n");
6731 thinkpad_acpi_module_exit();
6734 tp_features.sensors_pdrv_attrs_registered = 1;
6737 /* Device initialization */
6738 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6740 if (IS_ERR(tpacpi_pdev)) {
6741 ret = PTR_ERR(tpacpi_pdev);
6743 printk(TPACPI_ERR "unable to register platform device\n");
6744 thinkpad_acpi_module_exit();
6747 tpacpi_sensors_pdev = platform_device_register_simple(
6748 TPACPI_HWMON_DRVR_NAME,
6750 if (IS_ERR(tpacpi_sensors_pdev)) {
6751 ret = PTR_ERR(tpacpi_sensors_pdev);
6752 tpacpi_sensors_pdev = NULL;
6754 "unable to register hwmon platform device\n");
6755 thinkpad_acpi_module_exit();
6758 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6759 &dev_attr_thinkpad_acpi_pdev_name);
6762 "unable to create sysfs hwmon device attributes\n");
6763 thinkpad_acpi_module_exit();
6766 tp_features.sensors_pdev_attrs_registered = 1;
6767 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6768 if (IS_ERR(tpacpi_hwmon)) {
6769 ret = PTR_ERR(tpacpi_hwmon);
6770 tpacpi_hwmon = NULL;
6771 printk(TPACPI_ERR "unable to register hwmon device\n");
6772 thinkpad_acpi_module_exit();
6775 mutex_init(&tpacpi_inputdev_send_mutex);
6776 tpacpi_inputdev = input_allocate_device();
6777 if (!tpacpi_inputdev) {
6778 printk(TPACPI_ERR "unable to allocate input device\n");
6779 thinkpad_acpi_module_exit();
6782 /* Prepare input device, but don't register */
6783 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6784 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6785 tpacpi_inputdev->id.bustype = BUS_HOST;
6786 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6787 thinkpad_id.vendor :
6789 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6790 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6792 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6793 ret = ibm_init(&ibms_init[i]);
6794 if (ret >= 0 && *ibms_init[i].param)
6795 ret = ibms_init[i].data->write(ibms_init[i].param);
6797 thinkpad_acpi_module_exit();
6801 ret = input_register_device(tpacpi_inputdev);
6803 printk(TPACPI_ERR "unable to register input device\n");
6804 thinkpad_acpi_module_exit();
6807 tp_features.input_device_registered = 1;
6810 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6814 /* Please remove this in year 2009 */
6815 MODULE_ALIAS("ibm_acpi");
6817 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6820 * DMI matching for module autoloading
6822 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6823 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6825 * Only models listed in thinkwiki will be supported, so add yours
6826 * if it is not there yet.
6828 #define IBM_BIOS_MODULE_ALIAS(__type) \
6829 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6831 /* Non-ancient thinkpads */
6832 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6833 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6835 /* Ancient thinkpad BIOSes have to be identified by
6836 * BIOS type or model number, and there are far less
6837 * BIOS types than model numbers... */
6838 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6839 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6840 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6842 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6843 MODULE_DESCRIPTION(TPACPI_DESC);
6844 MODULE_VERSION(TPACPI_VERSION);
6845 MODULE_LICENSE("GPL");
6847 module_init(thinkpad_acpi_module_init);
6848 module_exit(thinkpad_acpi_module_exit);