eeepc-wmi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/slab.h>
  31. #include <linux/input.h>
  32. #include <linux/input/sparse-keymap.h>
  33. #include <linux/fb.h>
  34. #include <linux/backlight.h>
  35. #include <linux/platform_device.h>
  36. #include <acpi/acpi_bus.h>
  37. #include <acpi/acpi_drivers.h>
  38. #define EEEPC_WMI_FILE "eeepc-wmi"
  39. MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
  40. MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
  41. MODULE_LICENSE("GPL");
  42. #define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
  43. #define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
  44. MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
  45. MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
  46. #define NOTIFY_BRNUP_MIN 0x11
  47. #define NOTIFY_BRNUP_MAX 0x1f
  48. #define NOTIFY_BRNDOWN_MIN 0x20
  49. #define NOTIFY_BRNDOWN_MAX 0x2e
  50. #define EEEPC_WMI_METHODID_DEVS 0x53564544
  51. #define EEEPC_WMI_METHODID_DSTS 0x53544344
  52. #define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
  53. static const struct key_entry eeepc_wmi_keymap[] = {
  54. /* Sleep already handled via generic ACPI code */
  55. { KE_KEY, 0x5d, { KEY_WLAN } },
  56. { KE_KEY, 0x32, { KEY_MUTE } },
  57. { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
  58. { KE_KEY, 0x30, { KEY_VOLUMEUP } },
  59. { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
  60. { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
  61. { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
  62. { KE_END, 0},
  63. };
  64. struct bios_args {
  65. u32 dev_id;
  66. u32 ctrl_param;
  67. };
  68. struct eeepc_wmi {
  69. struct input_dev *inputdev;
  70. struct backlight_device *backlight_device;
  71. };
  72. static struct platform_device *platform_device;
  73. static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
  74. {
  75. int err;
  76. eeepc->inputdev = input_allocate_device();
  77. if (!eeepc->inputdev)
  78. return -ENOMEM;
  79. eeepc->inputdev->name = "Eee PC WMI hotkeys";
  80. eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
  81. eeepc->inputdev->id.bustype = BUS_HOST;
  82. eeepc->inputdev->dev.parent = &platform_device->dev;
  83. err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
  84. if (err)
  85. goto err_free_dev;
  86. err = input_register_device(eeepc->inputdev);
  87. if (err)
  88. goto err_free_keymap;
  89. return 0;
  90. err_free_keymap:
  91. sparse_keymap_free(eeepc->inputdev);
  92. err_free_dev:
  93. input_free_device(eeepc->inputdev);
  94. return err;
  95. }
  96. static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
  97. {
  98. if (eeepc->inputdev) {
  99. sparse_keymap_free(eeepc->inputdev);
  100. input_unregister_device(eeepc->inputdev);
  101. }
  102. eeepc->inputdev = NULL;
  103. }
  104. static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *ctrl_param)
  105. {
  106. struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
  107. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  108. union acpi_object *obj;
  109. acpi_status status;
  110. u32 tmp;
  111. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  112. 1, EEEPC_WMI_METHODID_DSTS, &input, &output);
  113. if (ACPI_FAILURE(status))
  114. return status;
  115. obj = (union acpi_object *)output.pointer;
  116. if (obj && obj->type == ACPI_TYPE_INTEGER)
  117. tmp = (u32)obj->integer.value;
  118. else
  119. tmp = 0;
  120. if (ctrl_param)
  121. *ctrl_param = tmp;
  122. kfree(obj);
  123. return status;
  124. }
  125. static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param)
  126. {
  127. struct bios_args args = {
  128. .dev_id = dev_id,
  129. .ctrl_param = ctrl_param,
  130. };
  131. struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
  132. acpi_status status;
  133. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  134. 1, EEEPC_WMI_METHODID_DEVS, &input, NULL);
  135. return status;
  136. }
  137. static int read_brightness(struct backlight_device *bd)
  138. {
  139. static u32 ctrl_param;
  140. acpi_status status;
  141. status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &ctrl_param);
  142. if (ACPI_FAILURE(status))
  143. return -1;
  144. else
  145. return ctrl_param & 0xFF;
  146. }
  147. static int update_bl_status(struct backlight_device *bd)
  148. {
  149. static u32 ctrl_param;
  150. acpi_status status;
  151. ctrl_param = bd->props.brightness;
  152. status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT, ctrl_param);
  153. if (ACPI_FAILURE(status))
  154. return -1;
  155. else
  156. return 0;
  157. }
  158. static const struct backlight_ops eeepc_wmi_bl_ops = {
  159. .get_brightness = read_brightness,
  160. .update_status = update_bl_status,
  161. };
  162. static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
  163. {
  164. struct backlight_device *bd = eeepc->backlight_device;
  165. int old = bd->props.brightness;
  166. int new;
  167. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  168. new = code - NOTIFY_BRNUP_MIN + 1;
  169. else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
  170. new = code - NOTIFY_BRNDOWN_MIN;
  171. bd->props.brightness = new;
  172. backlight_update_status(bd);
  173. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  174. return old;
  175. }
  176. static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
  177. {
  178. struct backlight_device *bd;
  179. struct backlight_properties props;
  180. memset(&props, 0, sizeof(struct backlight_properties));
  181. props.max_brightness = 15;
  182. bd = backlight_device_register(EEEPC_WMI_FILE,
  183. &platform_device->dev, eeepc,
  184. &eeepc_wmi_bl_ops, &props);
  185. if (IS_ERR(bd)) {
  186. pr_err("Could not register backlight device\n");
  187. return PTR_ERR(bd);
  188. }
  189. eeepc->backlight_device = bd;
  190. bd->props.brightness = read_brightness(bd);
  191. bd->props.power = FB_BLANK_UNBLANK;
  192. backlight_update_status(bd);
  193. return 0;
  194. }
  195. static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
  196. {
  197. if (eeepc->backlight_device)
  198. backlight_device_unregister(eeepc->backlight_device);
  199. eeepc->backlight_device = NULL;
  200. }
  201. static void eeepc_wmi_notify(u32 value, void *context)
  202. {
  203. struct eeepc_wmi *eeepc = context;
  204. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  205. union acpi_object *obj;
  206. acpi_status status;
  207. int code;
  208. int orig_code;
  209. status = wmi_get_event_data(value, &response);
  210. if (status != AE_OK) {
  211. pr_err("bad event status 0x%x\n", status);
  212. return;
  213. }
  214. obj = (union acpi_object *)response.pointer;
  215. if (obj && obj->type == ACPI_TYPE_INTEGER) {
  216. code = obj->integer.value;
  217. orig_code = code;
  218. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  219. code = NOTIFY_BRNUP_MIN;
  220. else if (code >= NOTIFY_BRNDOWN_MIN &&
  221. code <= NOTIFY_BRNDOWN_MAX)
  222. code = NOTIFY_BRNDOWN_MIN;
  223. if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
  224. if (!acpi_video_backlight_support())
  225. eeepc_wmi_backlight_notify(eeepc, orig_code);
  226. }
  227. if (!sparse_keymap_report_event(eeepc->inputdev,
  228. code, 1, true))
  229. pr_info("Unknown key %x pressed\n", code);
  230. }
  231. kfree(obj);
  232. }
  233. static int __devinit eeepc_wmi_platform_probe(struct platform_device *device)
  234. {
  235. struct eeepc_wmi *eeepc;
  236. int err;
  237. acpi_status status;
  238. eeepc = platform_get_drvdata(device);
  239. err = eeepc_wmi_input_init(eeepc);
  240. if (err)
  241. goto error_input;
  242. if (!acpi_video_backlight_support()) {
  243. err = eeepc_wmi_backlight_init(eeepc);
  244. if (err)
  245. goto error_backlight;
  246. } else
  247. pr_info("Backlight controlled by ACPI video driver\n");
  248. status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
  249. eeepc_wmi_notify, eeepc);
  250. if (ACPI_FAILURE(status)) {
  251. pr_err("Unable to register notify handler - %d\n",
  252. status);
  253. err = -ENODEV;
  254. goto error_wmi;
  255. }
  256. return 0;
  257. error_wmi:
  258. eeepc_wmi_backlight_exit(eeepc);
  259. error_backlight:
  260. eeepc_wmi_input_exit(eeepc);
  261. error_input:
  262. return err;
  263. }
  264. static int __devexit eeepc_wmi_platform_remove(struct platform_device *device)
  265. {
  266. struct eeepc_wmi *eeepc;
  267. eeepc = platform_get_drvdata(device);
  268. wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
  269. eeepc_wmi_backlight_exit(eeepc);
  270. eeepc_wmi_input_exit(eeepc);
  271. return 0;
  272. }
  273. static struct platform_driver platform_driver = {
  274. .driver = {
  275. .name = EEEPC_WMI_FILE,
  276. .owner = THIS_MODULE,
  277. },
  278. .probe = eeepc_wmi_platform_probe,
  279. .remove = __devexit_p(eeepc_wmi_platform_remove),
  280. };
  281. static int __init eeepc_wmi_init(void)
  282. {
  283. struct eeepc_wmi *eeepc;
  284. int err;
  285. if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
  286. !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
  287. pr_warning("No known WMI GUID found\n");
  288. return -ENODEV;
  289. }
  290. eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
  291. if (!eeepc)
  292. return -ENOMEM;
  293. platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
  294. if (!platform_device) {
  295. pr_warning("Unable to allocate platform device\n");
  296. err = -ENOMEM;
  297. goto fail_platform;
  298. }
  299. err = platform_device_add(platform_device);
  300. if (err) {
  301. pr_warning("Unable to add platform device\n");
  302. goto put_dev;
  303. }
  304. platform_set_drvdata(platform_device, eeepc);
  305. err = platform_driver_register(&platform_driver);
  306. if (err) {
  307. pr_warning("Unable to register platform driver\n");
  308. goto del_dev;
  309. }
  310. return 0;
  311. del_dev:
  312. platform_device_del(platform_device);
  313. put_dev:
  314. platform_device_put(platform_device);
  315. fail_platform:
  316. kfree(eeepc);
  317. return err;
  318. }
  319. static void __exit eeepc_wmi_exit(void)
  320. {
  321. struct eeepc_wmi *eeepc;
  322. eeepc = platform_get_drvdata(platform_device);
  323. platform_driver_unregister(&platform_driver);
  324. platform_device_unregister(platform_device);
  325. kfree(eeepc);
  326. }
  327. module_init(eeepc_wmi_init);
  328. module_exit(eeepc_wmi_exit);