Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 1 | /* |
| 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 Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 27 | |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 28 | #include <linux/kernel.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/types.h> |
Tejun Heo | a32f392 | 2010-04-05 11:37:59 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 33 | #include <linux/input.h> |
| 34 | #include <linux/input/sparse-keymap.h> |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 35 | #include <linux/platform_device.h> |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 36 | #include <acpi/acpi_bus.h> |
| 37 | #include <acpi/acpi_drivers.h> |
| 38 | |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 39 | #define EEEPC_WMI_FILE "eeepc-wmi" |
| 40 | |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 41 | MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>"); |
| 42 | MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver"); |
| 43 | MODULE_LICENSE("GPL"); |
| 44 | |
| 45 | #define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000" |
| 46 | |
| 47 | MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID); |
| 48 | |
| 49 | #define NOTIFY_BRNUP_MIN 0x11 |
| 50 | #define NOTIFY_BRNUP_MAX 0x1f |
| 51 | #define NOTIFY_BRNDOWN_MIN 0x20 |
| 52 | #define NOTIFY_BRNDOWN_MAX 0x2e |
| 53 | |
| 54 | static const struct key_entry eeepc_wmi_keymap[] = { |
| 55 | /* Sleep already handled via generic ACPI code */ |
| 56 | { KE_KEY, 0x5d, { KEY_WLAN } }, |
| 57 | { KE_KEY, 0x32, { KEY_MUTE } }, |
| 58 | { KE_KEY, 0x31, { KEY_VOLUMEDOWN } }, |
| 59 | { KE_KEY, 0x30, { KEY_VOLUMEUP } }, |
| 60 | { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } }, |
| 61 | { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } }, |
| 62 | { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } }, |
| 63 | { KE_END, 0}, |
| 64 | }; |
| 65 | |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 66 | struct eeepc_wmi { |
| 67 | struct input_dev *inputdev; |
| 68 | }; |
| 69 | |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 70 | static struct platform_device *platform_device; |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 71 | |
| 72 | static void eeepc_wmi_notify(u32 value, void *context) |
| 73 | { |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 74 | struct eeepc_wmi *eeepc = context; |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 75 | struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 76 | union acpi_object *obj; |
| 77 | acpi_status status; |
| 78 | int code; |
| 79 | |
| 80 | status = wmi_get_event_data(value, &response); |
| 81 | if (status != AE_OK) { |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 82 | pr_err("bad event status 0x%x\n", status); |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
| 86 | obj = (union acpi_object *)response.pointer; |
| 87 | |
| 88 | if (obj && obj->type == ACPI_TYPE_INTEGER) { |
| 89 | code = obj->integer.value; |
| 90 | |
| 91 | if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX) |
| 92 | code = NOTIFY_BRNUP_MIN; |
| 93 | else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX) |
| 94 | code = NOTIFY_BRNDOWN_MIN; |
| 95 | |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 96 | if (!sparse_keymap_report_event(eeepc->inputdev, |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 97 | code, 1, true)) |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 98 | pr_info("Unknown key %x pressed\n", code); |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | kfree(obj); |
| 102 | } |
| 103 | |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 104 | static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc) |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 105 | { |
| 106 | int err; |
| 107 | |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 108 | eeepc->inputdev = input_allocate_device(); |
| 109 | if (!eeepc->inputdev) |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 110 | return -ENOMEM; |
| 111 | |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 112 | eeepc->inputdev->name = "Eee PC WMI hotkeys"; |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 113 | eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0"; |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 114 | eeepc->inputdev->id.bustype = BUS_HOST; |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 115 | eeepc->inputdev->dev.parent = &platform_device->dev; |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 116 | |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 117 | err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL); |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 118 | if (err) |
| 119 | goto err_free_dev; |
| 120 | |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 121 | err = input_register_device(eeepc->inputdev); |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 122 | if (err) |
| 123 | goto err_free_keymap; |
| 124 | |
| 125 | return 0; |
| 126 | |
| 127 | err_free_keymap: |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 128 | sparse_keymap_free(eeepc->inputdev); |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 129 | err_free_dev: |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 130 | input_free_device(eeepc->inputdev); |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 131 | return err; |
| 132 | } |
| 133 | |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 134 | static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc) |
| 135 | { |
| 136 | if (eeepc->inputdev) { |
| 137 | sparse_keymap_free(eeepc->inputdev); |
| 138 | input_unregister_device(eeepc->inputdev); |
| 139 | } |
| 140 | |
| 141 | eeepc->inputdev = NULL; |
| 142 | } |
| 143 | |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 144 | static int __devinit eeepc_wmi_platform_probe(struct platform_device *device) |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 145 | { |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 146 | struct eeepc_wmi *eeepc; |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 147 | int err; |
| 148 | acpi_status status; |
| 149 | |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 150 | eeepc = platform_get_drvdata(device); |
| 151 | |
| 152 | err = eeepc_wmi_input_init(eeepc); |
| 153 | if (err) |
| 154 | return err; |
| 155 | |
| 156 | status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID, |
| 157 | eeepc_wmi_notify, eeepc); |
| 158 | if (ACPI_FAILURE(status)) { |
| 159 | pr_err("Unable to register notify handler - %d\n", |
| 160 | status); |
| 161 | err = -ENODEV; |
| 162 | goto error_wmi; |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | |
| 167 | error_wmi: |
| 168 | eeepc_wmi_input_exit(eeepc); |
| 169 | |
| 170 | return err; |
| 171 | } |
| 172 | |
| 173 | static int __devexit eeepc_wmi_platform_remove(struct platform_device *device) |
| 174 | { |
| 175 | struct eeepc_wmi *eeepc; |
| 176 | |
| 177 | eeepc = platform_get_drvdata(device); |
| 178 | wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID); |
| 179 | eeepc_wmi_input_exit(eeepc); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static struct platform_driver platform_driver = { |
| 185 | .driver = { |
| 186 | .name = EEEPC_WMI_FILE, |
| 187 | .owner = THIS_MODULE, |
| 188 | }, |
| 189 | .probe = eeepc_wmi_platform_probe, |
| 190 | .remove = __devexit_p(eeepc_wmi_platform_remove), |
| 191 | }; |
| 192 | |
| 193 | static int __init eeepc_wmi_init(void) |
| 194 | { |
| 195 | struct eeepc_wmi *eeepc; |
| 196 | int err; |
| 197 | |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 198 | if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID)) { |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 199 | pr_warning("No known WMI GUID found\n"); |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 200 | return -ENODEV; |
| 201 | } |
| 202 | |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 203 | eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL); |
| 204 | if (!eeepc) |
| 205 | return -ENOMEM; |
| 206 | |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 207 | platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1); |
| 208 | if (!platform_device) { |
| 209 | pr_warning("Unable to allocate platform device\n"); |
| 210 | err = -ENOMEM; |
| 211 | goto fail_platform; |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 212 | } |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 213 | |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 214 | err = platform_device_add(platform_device); |
| 215 | if (err) { |
| 216 | pr_warning("Unable to add platform device\n"); |
| 217 | goto put_dev; |
| 218 | } |
| 219 | |
| 220 | platform_set_drvdata(platform_device, eeepc); |
| 221 | |
| 222 | err = platform_driver_register(&platform_driver); |
| 223 | if (err) { |
| 224 | pr_warning("Unable to register platform driver\n"); |
| 225 | goto del_dev; |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return 0; |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 229 | |
| 230 | del_dev: |
| 231 | platform_device_del(platform_device); |
| 232 | put_dev: |
| 233 | platform_device_put(platform_device); |
| 234 | fail_platform: |
| 235 | kfree(eeepc); |
| 236 | |
| 237 | return err; |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static void __exit eeepc_wmi_exit(void) |
| 241 | { |
Yong Wang | 45f2c69 | 2010-04-11 09:27:19 +0800 | [diff] [blame^] | 242 | struct eeepc_wmi *eeepc; |
| 243 | |
| 244 | eeepc = platform_get_drvdata(platform_device); |
| 245 | platform_driver_unregister(&platform_driver); |
| 246 | platform_device_unregister(platform_device); |
Yong Wang | 8124888 | 2010-04-11 09:26:33 +0800 | [diff] [blame] | 247 | kfree(eeepc); |
Yong Wang | ee027e4 | 2010-03-21 10:26:34 +0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | module_init(eeepc_wmi_init); |
| 251 | module_exit(eeepc_wmi_exit); |