OSDN Git Service

x86: add driver for Lenovo ideapad S10-3T rotate button
[android-x86/kernel.git] / drivers / platform / x86 / lsrot.c
1 /*
2  *  Lenovo front-screen buttons testing driver
3  *
4  *  Copyright (C) 2010 Javier S. Pedro
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24
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>
30
31 #define DRIVER_NAME "lsrot"
32 #define DRIVER_AUTHOR "javispedro"
33 #define DRIVER_DESC "Lenovo front-screen buttons driver"
34
35 /* WMI event class */
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"
39
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))
43
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])
49
50 /* Experimentation */
51 #define EVID_FRONT_KEY          0x19
52 #define EVID_HINGE                      0x2C
53 #define EVID_ORIENTATION        0x2D
54
55 #define SKEY_TOUCH                      0x86
56 #define SKEY_TOUCH_MAPPING      KEY_LEFTMETA
57 #define SKEY_ROTATE                     0x90
58 #define SKEY_ROTATE_MAPPING     KEY_F12
59
60 /* the input device */
61 static struct input_dev *input_dev;
62
63 static void report_key(int key)
64 {
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);
69 }
70
71 static void handle_key_event(u8 *data)
72 {
73         switch (SKEY(data)) {
74                 case SKEY_TOUCH:
75                         report_key(SKEY_TOUCH_MAPPING);
76                 break;
77                 case SKEY_ROTATE:
78                         report_key(SKEY_ROTATE_MAPPING);
79                 break;
80                 default:
81                         printk(KERN_WARNING "%s: unknown key code 0x%hx\n", DRIVER_NAME, SKEY(data));
82                 break;
83         }
84 }
85
86 static void handle_hinge_event(u8 *data)
87 {
88         bool tablet_mode = TBMD(data);
89         input_report_switch(input_dev, SW_TABLET_MODE, tablet_mode);
90         input_sync(input_dev);
91 }
92
93 static void handle_orientation_event(u8 *data)
94 {
95         u8 orientation = RTAG(data);
96         /* TODO : Understand RTAG values */
97         printk(KERN_DEBUG "%s: new orientation: 0x%hx\n", DRIVER_NAME, orientation);
98 }
99
100 static int set_ap_bit(int ap, bool enabled)
101 {
102         acpi_status status;
103         static char buffer[0x80];
104         struct acpi_buffer input = { sizeof(buffer), buffer };
105
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. */
110         buffer[0] = 1;
111         buffer[1] = 16;
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;
116
117         printk(KERN_DEBUG "%s: setting ap bit %d to %d\n", DRIVER_NAME,
118                 ap, enabled ? 1 : 0);
119
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);
123                 return -1;
124         }
125
126         return 0;
127 }
128
129 static void event_handler(u32 value, void *context)
130 {
131         struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
132         union acpi_object *obj;
133         acpi_status status;
134
135         status = wmi_get_event_data(value, &response);
136         if (ACPI_FAILURE(status)) {
137                 printk(KERN_WARNING "%s: bad event\n", DRIVER_NAME);
138                 return;
139         }
140
141         obj = (union acpi_object *)response.pointer;
142
143         if (obj && obj->type == ACPI_TYPE_BUFFER && obj->buffer.length == 0x40) {
144                 u8 * data = (u8*) obj->buffer.pointer;
145                 switch (EVID(data)) {
146                         case EVID_FRONT_KEY:
147                                 handle_key_event(data);
148                         break;
149                         case EVID_HINGE:
150                                 handle_hinge_event(data);
151                         break;
152                         case EVID_ORIENTATION:
153                                 handle_orientation_event(data);
154                         break;
155                         default:
156                                 printk(KERN_WARNING "%s: unknown event code: %hx\n", DRIVER_NAME,
157                                         data[1]);
158                         break;
159                 }
160         } else {
161                 printk(KERN_WARNING "%s: bad event\n", DRIVER_NAME);
162         }
163
164         kfree(obj);
165 }
166
167 static int __init lsrot_init_module (void)
168 {
169         acpi_status status;
170         int err;
171
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",
174                        DRIVER_NAME);
175                 err = -ENODEV;
176                 goto err_clean;
177         }
178
179         input_dev = input_allocate_device();
180         if (!input_dev) {
181                 err = -ENOMEM;
182                 goto err_clean;
183         }
184
185         input_dev->name = DRIVER_DESC;
186         input_dev->id.bustype = BUS_HOST;
187
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);
193
194         err = input_register_device(input_dev);
195         if (err) {
196                 goto err_clean;
197         }
198
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);
202                 err = -ENXIO;
203                 goto err_clean_input_dev;
204         }
205
206         status = wmi_install_notify_handler(WMI_ACPI_EVENT_GUID,
207                                             event_handler, NULL);
208
209         if (ACPI_FAILURE(status)) {
210                 printk(KERN_ERR "%s: unable to register WMI event handler\n",
211                        DRIVER_NAME);
212                 err = -ENODEV;
213                 goto err_clean_input_dev;
214         }
215
216         return 0;
217
218 err_clean_input_dev:
219         input_free_device(input_dev);
220 err_clean:
221         return err;
222 }
223
224 static void __exit lsrot_cleanup_module (void)
225 {
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);
230 }
231
232 module_init(lsrot_init_module);
233 module_exit(lsrot_cleanup_module);
234
235 MODULE_LICENSE("GPL");
236
237 MODULE_AUTHOR(DRIVER_AUTHOR);
238 MODULE_DESCRIPTION(DRIVER_DESC);
239
240 MODULE_ALIAS("wmi:" WMI_ACPI_EVENT_GUID);
241 MODULE_ALIAS("wmi:" WMI_ACPI_BLOCK_GUID);