2 * Eee PC WMI hotkey driver
4 * Copyright(C) 2010 Intel Corporation.
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>
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.
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.
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
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/slab.h>
33 #include <linux/input.h>
34 #include <linux/input/sparse-keymap.h>
36 #include <linux/backlight.h>
37 #include <linux/platform_device.h>
38 #include <acpi/acpi_bus.h>
39 #include <acpi/acpi_drivers.h>
41 #define EEEPC_WMI_FILE "eeepc-wmi"
43 MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
44 MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
45 MODULE_LICENSE("GPL");
47 #define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
48 #define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
50 MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID
);
51 MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID
);
53 #define NOTIFY_BRNUP_MIN 0x11
54 #define NOTIFY_BRNUP_MAX 0x1f
55 #define NOTIFY_BRNDOWN_MIN 0x20
56 #define NOTIFY_BRNDOWN_MAX 0x2e
58 #define EEEPC_WMI_METHODID_DEVS 0x53564544
59 #define EEEPC_WMI_METHODID_DSTS 0x53544344
61 #define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
63 static 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
} },
81 struct input_dev
*inputdev
;
82 struct backlight_device
*backlight_device
;
85 static struct platform_device
*platform_device
;
87 static int eeepc_wmi_input_init(struct eeepc_wmi
*eeepc
)
91 eeepc
->inputdev
= input_allocate_device();
95 eeepc
->inputdev
->name
= "Eee PC WMI hotkeys";
96 eeepc
->inputdev
->phys
= EEEPC_WMI_FILE
"/input0";
97 eeepc
->inputdev
->id
.bustype
= BUS_HOST
;
98 eeepc
->inputdev
->dev
.parent
= &platform_device
->dev
;
100 err
= sparse_keymap_setup(eeepc
->inputdev
, eeepc_wmi_keymap
, NULL
);
104 err
= input_register_device(eeepc
->inputdev
);
106 goto err_free_keymap
;
111 sparse_keymap_free(eeepc
->inputdev
);
113 input_free_device(eeepc
->inputdev
);
117 static void eeepc_wmi_input_exit(struct eeepc_wmi
*eeepc
)
119 if (eeepc
->inputdev
) {
120 sparse_keymap_free(eeepc
->inputdev
);
121 input_unregister_device(eeepc
->inputdev
);
124 eeepc
->inputdev
= NULL
;
127 static acpi_status
eeepc_wmi_get_devstate(u32 dev_id
, u32
*ctrl_param
)
129 struct acpi_buffer input
= { (acpi_size
)sizeof(u32
), &dev_id
};
130 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
131 union acpi_object
*obj
;
135 status
= wmi_evaluate_method(EEEPC_WMI_MGMT_GUID
,
136 1, EEEPC_WMI_METHODID_DSTS
, &input
, &output
);
138 if (ACPI_FAILURE(status
))
141 obj
= (union acpi_object
*)output
.pointer
;
142 if (obj
&& obj
->type
== ACPI_TYPE_INTEGER
)
143 tmp
= (u32
)obj
->integer
.value
;
156 static acpi_status
eeepc_wmi_set_devstate(u32 dev_id
, u32 ctrl_param
)
158 struct bios_args args
= {
160 .ctrl_param
= ctrl_param
,
162 struct acpi_buffer input
= { (acpi_size
)sizeof(args
), &args
};
165 status
= wmi_evaluate_method(EEEPC_WMI_MGMT_GUID
,
166 1, EEEPC_WMI_METHODID_DEVS
, &input
, NULL
);
171 static int read_brightness(struct backlight_device
*bd
)
173 static u32 ctrl_param
;
176 status
= eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT
, &ctrl_param
);
178 if (ACPI_FAILURE(status
))
181 return ctrl_param
& 0xFF;
184 static int update_bl_status(struct backlight_device
*bd
)
187 static u32 ctrl_param
;
190 ctrl_param
= bd
->props
.brightness
;
192 status
= eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT
, ctrl_param
);
194 if (ACPI_FAILURE(status
))
200 static const struct backlight_ops eeepc_wmi_bl_ops
= {
201 .get_brightness
= read_brightness
,
202 .update_status
= update_bl_status
,
205 static int eeepc_wmi_backlight_notify(struct eeepc_wmi
*eeepc
, int code
)
207 struct backlight_device
*bd
= eeepc
->backlight_device
;
208 int old
= bd
->props
.brightness
;
211 if (code
>= NOTIFY_BRNUP_MIN
&& code
<= NOTIFY_BRNUP_MAX
)
212 new = code
- NOTIFY_BRNUP_MIN
+ 1;
213 else if (code
>= NOTIFY_BRNDOWN_MIN
&& code
<= NOTIFY_BRNDOWN_MAX
)
214 new = code
- NOTIFY_BRNDOWN_MIN
;
216 bd
->props
.brightness
= new;
217 backlight_update_status(bd
);
218 backlight_force_update(bd
, BACKLIGHT_UPDATE_HOTKEY
);
223 static int eeepc_wmi_backlight_init(struct eeepc_wmi
*eeepc
)
225 struct backlight_device
*bd
;
226 struct backlight_properties props
;
228 memset(&props
, 0, sizeof(struct backlight_properties
));
229 props
.max_brightness
= 15;
230 bd
= backlight_device_register(EEEPC_WMI_FILE
,
231 &platform_device
->dev
, eeepc
,
232 &eeepc_wmi_bl_ops
, &props
);
234 pr_err("Could not register backlight device\n");
238 eeepc
->backlight_device
= bd
;
240 bd
->props
.brightness
= read_brightness(bd
);
241 bd
->props
.power
= FB_BLANK_UNBLANK
;
242 backlight_update_status(bd
);
247 static void eeepc_wmi_backlight_exit(struct eeepc_wmi
*eeepc
)
249 if (eeepc
->backlight_device
)
250 backlight_device_unregister(eeepc
->backlight_device
);
252 eeepc
->backlight_device
= NULL
;
255 static void eeepc_wmi_notify(u32 value
, void *context
)
257 struct eeepc_wmi
*eeepc
= context
;
258 struct acpi_buffer response
= { ACPI_ALLOCATE_BUFFER
, NULL
};
259 union acpi_object
*obj
;
264 status
= wmi_get_event_data(value
, &response
);
265 if (status
!= AE_OK
) {
266 pr_err("bad event status 0x%x\n", status
);
270 obj
= (union acpi_object
*)response
.pointer
;
272 if (obj
&& obj
->type
== ACPI_TYPE_INTEGER
) {
273 code
= obj
->integer
.value
;
276 if (code
>= NOTIFY_BRNUP_MIN
&& code
<= NOTIFY_BRNUP_MAX
)
277 code
= NOTIFY_BRNUP_MIN
;
278 else if (code
>= NOTIFY_BRNDOWN_MIN
&&
279 code
<= NOTIFY_BRNDOWN_MAX
)
280 code
= NOTIFY_BRNDOWN_MIN
;
282 if (code
== NOTIFY_BRNUP_MIN
|| code
== NOTIFY_BRNDOWN_MIN
) {
283 if (!acpi_video_backlight_support())
284 eeepc_wmi_backlight_notify(eeepc
, orig_code
);
287 if (!sparse_keymap_report_event(eeepc
->inputdev
,
289 pr_info("Unknown key %x pressed\n", code
);
295 static int __devinit
eeepc_wmi_platform_probe(struct platform_device
*device
)
297 struct eeepc_wmi
*eeepc
;
301 eeepc
= platform_get_drvdata(device
);
303 err
= eeepc_wmi_input_init(eeepc
);
307 if (!acpi_video_backlight_support()) {
308 err
= eeepc_wmi_backlight_init(eeepc
);
310 goto error_backlight
;
312 pr_info("Backlight controlled by ACPI video driver\n");
314 status
= wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID
,
315 eeepc_wmi_notify
, eeepc
);
316 if (ACPI_FAILURE(status
)) {
317 pr_err("Unable to register notify handler - %d\n",
326 eeepc_wmi_backlight_exit(eeepc
);
328 eeepc_wmi_input_exit(eeepc
);
333 static int __devexit
eeepc_wmi_platform_remove(struct platform_device
*device
)
335 struct eeepc_wmi
*eeepc
;
337 eeepc
= platform_get_drvdata(device
);
338 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID
);
339 eeepc_wmi_backlight_exit(eeepc
);
340 eeepc_wmi_input_exit(eeepc
);
345 static struct platform_driver platform_driver
= {
347 .name
= EEEPC_WMI_FILE
,
348 .owner
= THIS_MODULE
,
350 .probe
= eeepc_wmi_platform_probe
,
351 .remove
= __devexit_p(eeepc_wmi_platform_remove
),
354 static int __init
eeepc_wmi_init(void)
356 struct eeepc_wmi
*eeepc
;
359 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID
) ||
360 !wmi_has_guid(EEEPC_WMI_MGMT_GUID
)) {
361 pr_warning("No known WMI GUID found\n");
365 eeepc
= kzalloc(sizeof(struct eeepc_wmi
), GFP_KERNEL
);
369 platform_device
= platform_device_alloc(EEEPC_WMI_FILE
, -1);
370 if (!platform_device
) {
371 pr_warning("Unable to allocate platform device\n");
376 err
= platform_device_add(platform_device
);
378 pr_warning("Unable to add platform device\n");
382 platform_set_drvdata(platform_device
, eeepc
);
384 err
= platform_driver_register(&platform_driver
);
386 pr_warning("Unable to register platform driver\n");
393 platform_device_del(platform_device
);
395 platform_device_put(platform_device
);
402 static void __exit
eeepc_wmi_exit(void)
404 struct eeepc_wmi
*eeepc
;
406 eeepc
= platform_get_drvdata(platform_device
);
407 platform_driver_unregister(&platform_driver
);
408 platform_device_unregister(platform_device
);
412 module_init(eeepc_wmi_init
);
413 module_exit(eeepc_wmi_exit
);