2 * Lenovo front-screen buttons testing driver
4 * Copyright (C) 2010 Javier S. Pedro
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
25 #include <linux/input.h>
26 #include <linux/types.h>
27 #include <linux/input.h>
28 #include <acpi/acpi_drivers.h>
29 #include <linux/acpi.h>
31 #define DRIVER_NAME "lsrot"
32 #define DRIVER_AUTHOR "javispedro"
33 #define DRIVER_DESC "Lenovo front-screen buttons driver"
36 #define WMI_ACPI_EVENT_GUID "ABBC0F20-8EA1-11D1-00A0-C90629100000"
37 /* WMI block class (with set/get methods) */
38 #define WMI_ACPI_BLOCK_GUID "ABBC0F40-8EA1-11D1-00A0-C90629100000"
40 /* Convert AP[0-7] into proper WSIO argument */
41 /* Example: to write to AP05, arg0 must be 8+5 = 13 */
42 #define AP_BIT(x) (8 + (x))
44 /* Offsets to interesting parts of the event "buffer" */
45 #define EVID(x) ((x)[1])
46 #define SKEY(x) ((x)[4])
47 #define TBMD(x) ((x)[0x17])
48 #define RTAG(x) ((x)[0x18])
51 #define EVID_FRONT_KEY 0x19
52 #define EVID_HINGE 0x2C
53 #define EVID_ORIENTATION 0x2D
55 #define SKEY_TOUCH 0x86
56 #define SKEY_TOUCH_MAPPING KEY_LEFTMETA
57 #define SKEY_ROTATE 0x90
58 #define SKEY_ROTATE_MAPPING KEY_F12
60 /* the input device */
61 static struct input_dev *input_dev;
63 static void report_key(int key)
65 input_report_key(input_dev, key, 1);
66 input_sync(input_dev);
67 input_report_key(input_dev, key, 0);
68 input_sync(input_dev);
71 static void handle_key_event(u8 *data)
75 report_key(SKEY_TOUCH_MAPPING);
78 report_key(SKEY_ROTATE_MAPPING);
81 printk(KERN_WARNING "%s: unknown key code 0x%hx\n", DRIVER_NAME, SKEY(data));
86 static void handle_hinge_event(u8 *data)
88 bool tablet_mode = TBMD(data);
89 input_report_switch(input_dev, SW_TABLET_MODE, tablet_mode);
90 input_sync(input_dev);
93 static void handle_orientation_event(u8 *data)
95 u8 orientation = RTAG(data);
96 /* TODO : Understand RTAG values */
97 printk(KERN_DEBUG "%s: new orientation: 0x%hx\n", DRIVER_NAME, orientation);
100 static int set_ap_bit(int ap, bool enabled)
103 static char buffer[0x80];
104 struct acpi_buffer input = { sizeof(buffer), buffer };
106 /* Actually, only bytes 0, 1, 8, 9, 10, 11 and 16 are read by DSDT,
107 * but let's play safe and clear them all. */
108 memset(buffer, 0, sizeof(buffer));
109 /* The DSDT expects all those magic numbers. It does nothing otherwise. */
112 buffer[8] = AP_BIT(ap);
113 buffer[10] = 0; /* Redundant: this really has to be zero, otherwise
114 you're tricking the DSDT into firing a false event. */
115 buffer[16] = enabled ? 1 : 0;
117 printk(KERN_DEBUG "%s: setting ap bit %d to %d\n", DRIVER_NAME,
118 ap, enabled ? 1 : 0);
120 status = wmi_set_block(WMI_ACPI_BLOCK_GUID, 0, &input);
121 if (ACPI_FAILURE(status)) {
122 printk(KERN_WARNING "%s: failed to set ap bit\n", DRIVER_NAME);
129 static void event_handler(u32 value, void *context)
131 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
132 union acpi_object *obj;
135 status = wmi_get_event_data(value, &response);
136 if (ACPI_FAILURE(status)) {
137 printk(KERN_WARNING "%s: bad event\n", DRIVER_NAME);
141 obj = (union acpi_object *)response.pointer;
143 if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == 0x40) {
144 u8 * data = (u8*) obj->buffer.pointer;
145 switch (EVID(data)) {
147 handle_key_event(data);
150 handle_hinge_event(data);
152 case EVID_ORIENTATION:
153 handle_orientation_event(data);
156 printk(KERN_WARNING "%s: unknown event code: %hx\n", DRIVER_NAME,
161 printk(KERN_WARNING "%s: bad event\n", DRIVER_NAME);
167 static int __init lsrot_init_module (void)
172 if (!wmi_has_guid(WMI_ACPI_EVENT_GUID) || !wmi_has_guid(WMI_ACPI_BLOCK_GUID)) {
173 printk(KERN_ERR "%s: missing required wmi guids\n",
179 input_dev = input_allocate_device();
185 input_dev->name = DRIVER_DESC;
186 input_dev->id.bustype = BUS_HOST;
188 set_bit(EV_KEY, input_dev->evbit);
189 set_bit(EV_SW, input_dev->evbit);
190 set_bit(SKEY_TOUCH_MAPPING, input_dev->keybit);
191 set_bit(SKEY_ROTATE_MAPPING, input_dev->keybit);
192 set_bit(SW_TABLET_MODE, input_dev->swbit);
194 err = input_register_device(input_dev);
199 /* AP05 is the only one that sets EC0.APLN */
200 if (set_ap_bit(5, true)) {
201 printk(KERN_ERR "%s: failed to set ap bit\n", DRIVER_NAME);
203 goto err_clean_input_dev;
206 status = wmi_install_notify_handler(WMI_ACPI_EVENT_GUID,
207 event_handler, NULL);
209 if (ACPI_FAILURE(status)) {
210 printk(KERN_ERR "%s: unable to register WMI event handler\n",
213 goto err_clean_input_dev;
219 input_free_device(input_dev);
224 static void __exit lsrot_cleanup_module (void)
226 wmi_remove_notify_handler(WMI_ACPI_EVENT_GUID);
227 set_ap_bit(5, false);
228 input_unregister_device(input_dev);
229 input_free_device(input_dev);
232 module_init(lsrot_init_module);
233 module_exit(lsrot_cleanup_module);
235 MODULE_LICENSE("GPL");
237 MODULE_AUTHOR(DRIVER_AUTHOR);
238 MODULE_DESCRIPTION(DRIVER_DESC);
240 MODULE_ALIAS("wmi:" WMI_ACPI_EVENT_GUID);
241 MODULE_ALIAS("wmi:" WMI_ACPI_BLOCK_GUID);