eeepc-wmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_METHODID_CFVS 0x53564643
  53. #define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
  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_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
  64. { KE_KEY, 0xe1, { KEY_F14 } },
  65. { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
  66. { KE_KEY, 0xe0, { KEY_PROG1 } },
  67. { KE_KEY, 0x5c, { KEY_F15 } },
  68. { KE_END, 0},
  69. };
  70. struct bios_args {
  71. u32 dev_id;
  72. u32 ctrl_param;
  73. };
  74. struct eeepc_wmi {
  75. struct input_dev *inputdev;
  76. struct backlight_device *backlight_device;
  77. };
  78. static struct platform_device *platform_device;
  79. static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
  80. {
  81. int err;
  82. eeepc->inputdev = input_allocate_device();
  83. if (!eeepc->inputdev)
  84. return -ENOMEM;
  85. eeepc->inputdev->name = "Eee PC WMI hotkeys";
  86. eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
  87. eeepc->inputdev->id.bustype = BUS_HOST;
  88. eeepc->inputdev->dev.parent = &platform_device->dev;
  89. err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
  90. if (err)
  91. goto err_free_dev;
  92. err = input_register_device(eeepc->inputdev);
  93. if (err)
  94. goto err_free_keymap;
  95. return 0;
  96. err_free_keymap:
  97. sparse_keymap_free(eeepc->inputdev);
  98. err_free_dev:
  99. input_free_device(eeepc->inputdev);
  100. return err;
  101. }
  102. static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
  103. {
  104. if (eeepc->inputdev) {
  105. sparse_keymap_free(eeepc->inputdev);
  106. input_unregister_device(eeepc->inputdev);
  107. }
  108. eeepc->inputdev = NULL;
  109. }
  110. static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *ctrl_param)
  111. {
  112. struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
  113. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  114. union acpi_object *obj;
  115. acpi_status status;
  116. u32 tmp;
  117. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  118. 1, EEEPC_WMI_METHODID_DSTS, &input, &output);
  119. if (ACPI_FAILURE(status))
  120. return status;
  121. obj = (union acpi_object *)output.pointer;
  122. if (obj && obj->type == ACPI_TYPE_INTEGER)
  123. tmp = (u32)obj->integer.value;
  124. else
  125. tmp = 0;
  126. if (ctrl_param)
  127. *ctrl_param = tmp;
  128. kfree(obj);
  129. return status;
  130. }
  131. static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param)
  132. {
  133. struct bios_args args = {
  134. .dev_id = dev_id,
  135. .ctrl_param = ctrl_param,
  136. };
  137. struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
  138. acpi_status status;
  139. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  140. 1, EEEPC_WMI_METHODID_DEVS, &input, NULL);
  141. return status;
  142. }
  143. static int read_brightness(struct backlight_device *bd)
  144. {
  145. static u32 ctrl_param;
  146. acpi_status status;
  147. status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &ctrl_param);
  148. if (ACPI_FAILURE(status))
  149. return -1;
  150. else
  151. return ctrl_param & 0xFF;
  152. }
  153. static int update_bl_status(struct backlight_device *bd)
  154. {
  155. static u32 ctrl_param;
  156. acpi_status status;
  157. ctrl_param = bd->props.brightness;
  158. status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT, ctrl_param);
  159. if (ACPI_FAILURE(status))
  160. return -1;
  161. else
  162. return 0;
  163. }
  164. static const struct backlight_ops eeepc_wmi_bl_ops = {
  165. .get_brightness = read_brightness,
  166. .update_status = update_bl_status,
  167. };
  168. static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
  169. {
  170. struct backlight_device *bd = eeepc->backlight_device;
  171. int old = bd->props.brightness;
  172. int new = old;
  173. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  174. new = code - NOTIFY_BRNUP_MIN + 1;
  175. else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
  176. new = code - NOTIFY_BRNDOWN_MIN;
  177. bd->props.brightness = new;
  178. backlight_update_status(bd);
  179. backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
  180. return old;
  181. }
  182. static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
  183. {
  184. struct backlight_device *bd;
  185. struct backlight_properties props;
  186. memset(&props, 0, sizeof(struct backlight_properties));
  187. props.max_brightness = 15;
  188. bd = backlight_device_register(EEEPC_WMI_FILE,
  189. &platform_device->dev, eeepc,
  190. &eeepc_wmi_bl_ops, &props);
  191. if (IS_ERR(bd)) {
  192. pr_err("Could not register backlight device\n");
  193. return PTR_ERR(bd);
  194. }
  195. eeepc->backlight_device = bd;
  196. bd->props.brightness = read_brightness(bd);
  197. bd->props.power = FB_BLANK_UNBLANK;
  198. backlight_update_status(bd);
  199. return 0;
  200. }
  201. static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
  202. {
  203. if (eeepc->backlight_device)
  204. backlight_device_unregister(eeepc->backlight_device);
  205. eeepc->backlight_device = NULL;
  206. }
  207. static void eeepc_wmi_notify(u32 value, void *context)
  208. {
  209. struct eeepc_wmi *eeepc = context;
  210. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  211. union acpi_object *obj;
  212. acpi_status status;
  213. int code;
  214. int orig_code;
  215. status = wmi_get_event_data(value, &response);
  216. if (status != AE_OK) {
  217. pr_err("bad event status 0x%x\n", status);
  218. return;
  219. }
  220. obj = (union acpi_object *)response.pointer;
  221. if (obj && obj->type == ACPI_TYPE_INTEGER) {
  222. code = obj->integer.value;
  223. orig_code = code;
  224. if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
  225. code = NOTIFY_BRNUP_MIN;
  226. else if (code >= NOTIFY_BRNDOWN_MIN &&
  227. code <= NOTIFY_BRNDOWN_MAX)
  228. code = NOTIFY_BRNDOWN_MIN;
  229. if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
  230. if (!acpi_video_backlight_support())
  231. eeepc_wmi_backlight_notify(eeepc, orig_code);
  232. }
  233. if (!sparse_keymap_report_event(eeepc->inputdev,
  234. code, 1, true))
  235. pr_info("Unknown key %x pressed\n", code);
  236. }
  237. kfree(obj);
  238. }
  239. static int store_cpufv(struct device *dev, struct device_attribute *attr,
  240. const char *buf, size_t count)
  241. {
  242. int value;
  243. struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
  244. acpi_status status;
  245. if (!count || sscanf(buf, "%i", &value) != 1)
  246. return -EINVAL;
  247. if (value < 0 || value > 2)
  248. return -EINVAL;
  249. status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
  250. 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
  251. if (ACPI_FAILURE(status))
  252. return -EIO;
  253. else
  254. return count;
  255. }
  256. static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
  257. static void eeepc_wmi_sysfs_exit(struct platform_device *device)
  258. {
  259. device_remove_file(&device->dev, &dev_attr_cpufv);
  260. }
  261. static int eeepc_wmi_sysfs_init(struct platform_device *device)
  262. {
  263. int retval = -ENOMEM;
  264. retval = device_create_file(&device->dev, &dev_attr_cpufv);
  265. if (retval)
  266. goto error_sysfs;
  267. return 0;
  268. error_sysfs:
  269. eeepc_wmi_sysfs_exit(platform_device);
  270. return retval;
  271. }
  272. static int __devinit eeepc_wmi_platform_probe(struct platform_device *device)
  273. {
  274. struct eeepc_wmi *eeepc;
  275. int err;
  276. acpi_status status;
  277. eeepc = platform_get_drvdata(device);
  278. err = eeepc_wmi_input_init(eeepc);
  279. if (err)
  280. goto error_input;
  281. if (!acpi_video_backlight_support()) {
  282. err = eeepc_wmi_backlight_init(eeepc);
  283. if (err)
  284. goto error_backlight;
  285. } else
  286. pr_info("Backlight controlled by ACPI video driver\n");
  287. status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
  288. eeepc_wmi_notify, eeepc);
  289. if (ACPI_FAILURE(status)) {
  290. pr_err("Unable to register notify handler - %d\n",
  291. status);
  292. err = -ENODEV;
  293. goto error_wmi;
  294. }
  295. return 0;
  296. error_wmi:
  297. eeepc_wmi_backlight_exit(eeepc);
  298. error_backlight:
  299. eeepc_wmi_input_exit(eeepc);
  300. error_input:
  301. return err;
  302. }
  303. static int __devexit eeepc_wmi_platform_remove(struct platform_device *device)
  304. {
  305. struct eeepc_wmi *eeepc;
  306. eeepc = platform_get_drvdata(device);
  307. wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
  308. eeepc_wmi_backlight_exit(eeepc);
  309. eeepc_wmi_input_exit(eeepc);
  310. return 0;
  311. }
  312. static struct platform_driver platform_driver = {
  313. .driver = {
  314. .name = EEEPC_WMI_FILE,
  315. .owner = THIS_MODULE,
  316. },
  317. .probe = eeepc_wmi_platform_probe,
  318. .remove = __devexit_p(eeepc_wmi_platform_remove),
  319. };
  320. static int __init eeepc_wmi_init(void)
  321. {
  322. struct eeepc_wmi *eeepc;
  323. int err;
  324. if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
  325. !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
  326. pr_warning("No known WMI GUID found\n");
  327. return -ENODEV;
  328. }
  329. eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
  330. if (!eeepc)
  331. return -ENOMEM;
  332. platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
  333. if (!platform_device) {
  334. pr_warning("Unable to allocate platform device\n");
  335. err = -ENOMEM;
  336. goto fail_platform;
  337. }
  338. err = platform_device_add(platform_device);
  339. if (err) {
  340. pr_warning("Unable to add platform device\n");
  341. goto put_dev;
  342. }
  343. platform_set_drvdata(platform_device, eeepc);
  344. err = platform_driver_register(&platform_driver);
  345. if (err) {
  346. pr_warning("Unable to register platform driver\n");
  347. goto del_dev;
  348. }
  349. err = eeepc_wmi_sysfs_init(platform_device);
  350. if (err)
  351. goto del_sysfs;
  352. return 0;
  353. del_sysfs:
  354. eeepc_wmi_sysfs_exit(platform_device);
  355. del_dev:
  356. platform_device_del(platform_device);
  357. put_dev:
  358. platform_device_put(platform_device);
  359. fail_platform:
  360. kfree(eeepc);
  361. return err;
  362. }
  363. static void __exit eeepc_wmi_exit(void)
  364. {
  365. struct eeepc_wmi *eeepc;
  366. eeepc_wmi_sysfs_exit(platform_device);
  367. eeepc = platform_get_drvdata(platform_device);
  368. platform_driver_unregister(&platform_driver);
  369. platform_device_unregister(platform_device);
  370. kfree(eeepc);
  371. }
  372. module_init(eeepc_wmi_init);
  373. module_exit(eeepc_wmi_exit);