blob: 21df266c27accc317a343a4de4846b9e1eda9b5b [file] [log] [blame]
Yong Wangee027e42010-03-21 10:26:34 +08001/*
2 * Eee PC WMI hotkey driver
3 *
4 * Copyright(C) 2010 Intel Corporation.
5 *
6 * Portions based on wistron_btns.c:
7 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
8 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
9 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
Yong Wang81248882010-04-11 09:26:33 +080026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Yong Wangee027e42010-03-21 10:26:34 +080028#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/types.h>
Tejun Heoa32f3922010-04-05 11:37:59 +090032#include <linux/slab.h>
Yong Wangee027e42010-03-21 10:26:34 +080033#include <linux/input.h>
34#include <linux/input/sparse-keymap.h>
Yong Wang3d7b1652010-04-11 09:27:54 +080035#include <linux/fb.h>
36#include <linux/backlight.h>
Yong Wang45f2c692010-04-11 09:27:19 +080037#include <linux/platform_device.h>
Yong Wangee027e42010-03-21 10:26:34 +080038#include <acpi/acpi_bus.h>
39#include <acpi/acpi_drivers.h>
40
Yong Wang45f2c692010-04-11 09:27:19 +080041#define EEEPC_WMI_FILE "eeepc-wmi"
42
Yong Wangee027e42010-03-21 10:26:34 +080043MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
44MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
45MODULE_LICENSE("GPL");
46
47#define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
Yong Wang3d7b1652010-04-11 09:27:54 +080048#define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080049
50MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +080051MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
Yong Wangee027e42010-03-21 10:26:34 +080052
53#define NOTIFY_BRNUP_MIN 0x11
54#define NOTIFY_BRNUP_MAX 0x1f
55#define NOTIFY_BRNDOWN_MIN 0x20
56#define NOTIFY_BRNDOWN_MAX 0x2e
57
Yong Wang3d7b1652010-04-11 09:27:54 +080058#define EEEPC_WMI_METHODID_DEVS 0x53564544
59#define EEEPC_WMI_METHODID_DSTS 0x53544344
60
61#define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
62
Yong Wangee027e42010-03-21 10:26:34 +080063static const struct key_entry eeepc_wmi_keymap[] = {
64 /* Sleep already handled via generic ACPI code */
65 { KE_KEY, 0x5d, { KEY_WLAN } },
66 { KE_KEY, 0x32, { KEY_MUTE } },
67 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
68 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
69 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
70 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
71 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
Chris Bagwelleda17482010-10-11 18:47:17 -050072 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
73 { KE_KEY, 0xe1, { KEY_F14 } },
74 { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
75 { KE_KEY, 0xe0, { KEY_PROG1 } },
76 { KE_KEY, 0x5c, { KEY_F15 } },
Yong Wangee027e42010-03-21 10:26:34 +080077 { KE_END, 0},
78};
79
Yong Wang3d7b1652010-04-11 09:27:54 +080080struct bios_args {
81 u32 dev_id;
82 u32 ctrl_param;
83};
84
Yong Wang81248882010-04-11 09:26:33 +080085struct eeepc_wmi {
86 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +080087 struct backlight_device *backlight_device;
Yong Wang81248882010-04-11 09:26:33 +080088};
89
Yong Wang45f2c692010-04-11 09:27:19 +080090static struct platform_device *platform_device;
Yong Wangee027e42010-03-21 10:26:34 +080091
Yong Wang81248882010-04-11 09:26:33 +080092static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
Yong Wangee027e42010-03-21 10:26:34 +080093{
94 int err;
95
Yong Wang81248882010-04-11 09:26:33 +080096 eeepc->inputdev = input_allocate_device();
97 if (!eeepc->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +080098 return -ENOMEM;
99
Yong Wang81248882010-04-11 09:26:33 +0800100 eeepc->inputdev->name = "Eee PC WMI hotkeys";
Yong Wang45f2c692010-04-11 09:27:19 +0800101 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
Yong Wang81248882010-04-11 09:26:33 +0800102 eeepc->inputdev->id.bustype = BUS_HOST;
Yong Wang45f2c692010-04-11 09:27:19 +0800103 eeepc->inputdev->dev.parent = &platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800104
Yong Wang81248882010-04-11 09:26:33 +0800105 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800106 if (err)
107 goto err_free_dev;
108
Yong Wang81248882010-04-11 09:26:33 +0800109 err = input_register_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800110 if (err)
111 goto err_free_keymap;
112
113 return 0;
114
115err_free_keymap:
Yong Wang81248882010-04-11 09:26:33 +0800116 sparse_keymap_free(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800117err_free_dev:
Yong Wang81248882010-04-11 09:26:33 +0800118 input_free_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800119 return err;
120}
121
Yong Wang81248882010-04-11 09:26:33 +0800122static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
123{
124 if (eeepc->inputdev) {
125 sparse_keymap_free(eeepc->inputdev);
126 input_unregister_device(eeepc->inputdev);
127 }
128
129 eeepc->inputdev = NULL;
130}
131
Yong Wang3d7b1652010-04-11 09:27:54 +0800132static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *ctrl_param)
133{
134 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
135 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
136 union acpi_object *obj;
137 acpi_status status;
138 u32 tmp;
139
140 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
141 1, EEEPC_WMI_METHODID_DSTS, &input, &output);
142
143 if (ACPI_FAILURE(status))
144 return status;
145
146 obj = (union acpi_object *)output.pointer;
147 if (obj && obj->type == ACPI_TYPE_INTEGER)
148 tmp = (u32)obj->integer.value;
149 else
150 tmp = 0;
151
152 if (ctrl_param)
153 *ctrl_param = tmp;
154
155 kfree(obj);
156
157 return status;
158
159}
160
161static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param)
162{
163 struct bios_args args = {
164 .dev_id = dev_id,
165 .ctrl_param = ctrl_param,
166 };
167 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
168 acpi_status status;
169
170 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
171 1, EEEPC_WMI_METHODID_DEVS, &input, NULL);
172
173 return status;
174}
175
176static int read_brightness(struct backlight_device *bd)
177{
178 static u32 ctrl_param;
179 acpi_status status;
180
181 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &ctrl_param);
182
183 if (ACPI_FAILURE(status))
184 return -1;
185 else
186 return ctrl_param & 0xFF;
187}
188
189static int update_bl_status(struct backlight_device *bd)
190{
191
192 static u32 ctrl_param;
193 acpi_status status;
194
195 ctrl_param = bd->props.brightness;
196
197 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT, ctrl_param);
198
199 if (ACPI_FAILURE(status))
200 return -1;
201 else
202 return 0;
203}
204
205static const struct backlight_ops eeepc_wmi_bl_ops = {
206 .get_brightness = read_brightness,
207 .update_status = update_bl_status,
208};
209
210static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
211{
212 struct backlight_device *bd = eeepc->backlight_device;
213 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200214 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800215
216 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
217 new = code - NOTIFY_BRNUP_MIN + 1;
218 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
219 new = code - NOTIFY_BRNDOWN_MIN;
220
221 bd->props.brightness = new;
222 backlight_update_status(bd);
223 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
224
225 return old;
226}
227
228static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
229{
230 struct backlight_device *bd;
231 struct backlight_properties props;
232
233 memset(&props, 0, sizeof(struct backlight_properties));
234 props.max_brightness = 15;
235 bd = backlight_device_register(EEEPC_WMI_FILE,
236 &platform_device->dev, eeepc,
237 &eeepc_wmi_bl_ops, &props);
238 if (IS_ERR(bd)) {
239 pr_err("Could not register backlight device\n");
240 return PTR_ERR(bd);
241 }
242
243 eeepc->backlight_device = bd;
244
245 bd->props.brightness = read_brightness(bd);
246 bd->props.power = FB_BLANK_UNBLANK;
247 backlight_update_status(bd);
248
249 return 0;
250}
251
252static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
253{
254 if (eeepc->backlight_device)
255 backlight_device_unregister(eeepc->backlight_device);
256
257 eeepc->backlight_device = NULL;
258}
259
260static void eeepc_wmi_notify(u32 value, void *context)
261{
262 struct eeepc_wmi *eeepc = context;
263 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
264 union acpi_object *obj;
265 acpi_status status;
266 int code;
267 int orig_code;
268
269 status = wmi_get_event_data(value, &response);
270 if (status != AE_OK) {
271 pr_err("bad event status 0x%x\n", status);
272 return;
273 }
274
275 obj = (union acpi_object *)response.pointer;
276
277 if (obj && obj->type == ACPI_TYPE_INTEGER) {
278 code = obj->integer.value;
279 orig_code = code;
280
281 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
282 code = NOTIFY_BRNUP_MIN;
283 else if (code >= NOTIFY_BRNDOWN_MIN &&
284 code <= NOTIFY_BRNDOWN_MAX)
285 code = NOTIFY_BRNDOWN_MIN;
286
287 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
288 if (!acpi_video_backlight_support())
289 eeepc_wmi_backlight_notify(eeepc, orig_code);
290 }
291
292 if (!sparse_keymap_report_event(eeepc->inputdev,
293 code, 1, true))
294 pr_info("Unknown key %x pressed\n", code);
295 }
296
297 kfree(obj);
298}
299
Yong Wang45f2c692010-04-11 09:27:19 +0800300static int __devinit eeepc_wmi_platform_probe(struct platform_device *device)
Yong Wangee027e42010-03-21 10:26:34 +0800301{
Yong Wang45f2c692010-04-11 09:27:19 +0800302 struct eeepc_wmi *eeepc;
Yong Wangee027e42010-03-21 10:26:34 +0800303 int err;
304 acpi_status status;
305
Yong Wang45f2c692010-04-11 09:27:19 +0800306 eeepc = platform_get_drvdata(device);
307
308 err = eeepc_wmi_input_init(eeepc);
309 if (err)
Yong Wang3d7b1652010-04-11 09:27:54 +0800310 goto error_input;
311
312 if (!acpi_video_backlight_support()) {
313 err = eeepc_wmi_backlight_init(eeepc);
314 if (err)
315 goto error_backlight;
316 } else
317 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +0800318
319 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
320 eeepc_wmi_notify, eeepc);
321 if (ACPI_FAILURE(status)) {
322 pr_err("Unable to register notify handler - %d\n",
323 status);
324 err = -ENODEV;
325 goto error_wmi;
326 }
327
328 return 0;
329
330error_wmi:
Yong Wang3d7b1652010-04-11 09:27:54 +0800331 eeepc_wmi_backlight_exit(eeepc);
332error_backlight:
Yong Wang45f2c692010-04-11 09:27:19 +0800333 eeepc_wmi_input_exit(eeepc);
Yong Wang3d7b1652010-04-11 09:27:54 +0800334error_input:
Yong Wang45f2c692010-04-11 09:27:19 +0800335 return err;
336}
337
338static int __devexit eeepc_wmi_platform_remove(struct platform_device *device)
339{
340 struct eeepc_wmi *eeepc;
341
342 eeepc = platform_get_drvdata(device);
343 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +0800344 eeepc_wmi_backlight_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800345 eeepc_wmi_input_exit(eeepc);
346
347 return 0;
348}
349
350static struct platform_driver platform_driver = {
351 .driver = {
352 .name = EEEPC_WMI_FILE,
353 .owner = THIS_MODULE,
354 },
355 .probe = eeepc_wmi_platform_probe,
356 .remove = __devexit_p(eeepc_wmi_platform_remove),
357};
358
359static int __init eeepc_wmi_init(void)
360{
361 struct eeepc_wmi *eeepc;
362 int err;
363
Yong Wang3d7b1652010-04-11 09:27:54 +0800364 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
365 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
Yong Wang81248882010-04-11 09:26:33 +0800366 pr_warning("No known WMI GUID found\n");
Yong Wangee027e42010-03-21 10:26:34 +0800367 return -ENODEV;
368 }
369
Yong Wang81248882010-04-11 09:26:33 +0800370 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
371 if (!eeepc)
372 return -ENOMEM;
373
Yong Wang45f2c692010-04-11 09:27:19 +0800374 platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
375 if (!platform_device) {
376 pr_warning("Unable to allocate platform device\n");
377 err = -ENOMEM;
378 goto fail_platform;
Yong Wang81248882010-04-11 09:26:33 +0800379 }
Yong Wangee027e42010-03-21 10:26:34 +0800380
Yong Wang45f2c692010-04-11 09:27:19 +0800381 err = platform_device_add(platform_device);
382 if (err) {
383 pr_warning("Unable to add platform device\n");
384 goto put_dev;
385 }
386
387 platform_set_drvdata(platform_device, eeepc);
388
389 err = platform_driver_register(&platform_driver);
390 if (err) {
391 pr_warning("Unable to register platform driver\n");
392 goto del_dev;
Yong Wangee027e42010-03-21 10:26:34 +0800393 }
394
395 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +0800396
397del_dev:
398 platform_device_del(platform_device);
399put_dev:
400 platform_device_put(platform_device);
401fail_platform:
402 kfree(eeepc);
403
404 return err;
Yong Wangee027e42010-03-21 10:26:34 +0800405}
406
407static void __exit eeepc_wmi_exit(void)
408{
Yong Wang45f2c692010-04-11 09:27:19 +0800409 struct eeepc_wmi *eeepc;
410
411 eeepc = platform_get_drvdata(platform_device);
412 platform_driver_unregister(&platform_driver);
413 platform_device_unregister(platform_device);
Yong Wang81248882010-04-11 09:26:33 +0800414 kfree(eeepc);
Yong Wangee027e42010-03-21 10:26:34 +0800415}
416
417module_init(eeepc_wmi_init);
418module_exit(eeepc_wmi_exit);