topstar-laptop.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * ACPI driver for Topstar notebooks (hotkeys support only)
  3. *
  4. * Copyright (c) 2009 Herton Ronaldo Krzesinski <herton@mandriva.com.br>
  5. *
  6. * Implementation inspired by existing x86 platform drivers, in special
  7. * asus/eepc/fujitsu-laptop, thanks to their authors
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/acpi.h>
  18. #include <linux/input.h>
  19. #define ACPI_TOPSTAR_CLASS "topstar"
  20. struct topstar_hkey {
  21. struct input_dev *inputdev;
  22. };
  23. struct tps_key_entry {
  24. u8 code;
  25. u16 keycode;
  26. };
  27. static struct tps_key_entry topstar_keymap[] = {
  28. { 0x80, KEY_BRIGHTNESSUP },
  29. { 0x81, KEY_BRIGHTNESSDOWN },
  30. { 0x83, KEY_VOLUMEUP },
  31. { 0x84, KEY_VOLUMEDOWN },
  32. { 0x85, KEY_MUTE },
  33. { 0x86, KEY_SWITCHVIDEOMODE },
  34. { 0x87, KEY_F13 }, /* touchpad enable/disable key */
  35. { 0x88, KEY_WLAN },
  36. { 0x8a, KEY_WWW },
  37. { 0x8b, KEY_MAIL },
  38. { 0x8c, KEY_MEDIA },
  39. { 0x96, KEY_F14 }, /* G key? */
  40. { }
  41. };
  42. static struct tps_key_entry *tps_get_key_by_scancode(int code)
  43. {
  44. struct tps_key_entry *key;
  45. for (key = topstar_keymap; key->code; key++)
  46. if (code == key->code)
  47. return key;
  48. return NULL;
  49. }
  50. static struct tps_key_entry *tps_get_key_by_keycode(int code)
  51. {
  52. struct tps_key_entry *key;
  53. for (key = topstar_keymap; key->code; key++)
  54. if (code == key->keycode)
  55. return key;
  56. return NULL;
  57. }
  58. static void acpi_topstar_notify(struct acpi_device *device, u32 event)
  59. {
  60. struct tps_key_entry *key;
  61. static bool dup_evnt[2];
  62. bool *dup;
  63. struct topstar_hkey *hkey = acpi_driver_data(device);
  64. /* 0x83 and 0x84 key events comes duplicated... */
  65. if (event == 0x83 || event == 0x84) {
  66. dup = &dup_evnt[event - 0x83];
  67. if (*dup) {
  68. *dup = false;
  69. return;
  70. }
  71. *dup = true;
  72. }
  73. /*
  74. * 'G key' generate two event codes, convert to only
  75. * one event/key code for now (3G switch?)
  76. */
  77. if (event == 0x97)
  78. event = 0x96;
  79. key = tps_get_key_by_scancode(event);
  80. if (key) {
  81. input_report_key(hkey->inputdev, key->keycode, 1);
  82. input_sync(hkey->inputdev);
  83. input_report_key(hkey->inputdev, key->keycode, 0);
  84. input_sync(hkey->inputdev);
  85. return;
  86. }
  87. /* Known non hotkey events don't handled or that we don't care yet */
  88. if (event == 0x8e || event == 0x8f || event == 0x90)
  89. return;
  90. pr_info("unknown event = 0x%02x\n", event);
  91. }
  92. static int acpi_topstar_fncx_switch(struct acpi_device *device, bool state)
  93. {
  94. acpi_status status;
  95. union acpi_object fncx_params[1] = {
  96. { .type = ACPI_TYPE_INTEGER }
  97. };
  98. struct acpi_object_list fncx_arg_list = { 1, &fncx_params[0] };
  99. fncx_params[0].integer.value = state ? 0x86 : 0x87;
  100. status = acpi_evaluate_object(device->handle, "FNCX", &fncx_arg_list, NULL);
  101. if (ACPI_FAILURE(status)) {
  102. pr_err("Unable to switch FNCX notifications\n");
  103. return -ENODEV;
  104. }
  105. return 0;
  106. }
  107. static int topstar_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  108. {
  109. struct tps_key_entry *key = tps_get_key_by_scancode(scancode);
  110. if (!key)
  111. return -EINVAL;
  112. *keycode = key->keycode;
  113. return 0;
  114. }
  115. static int topstar_setkeycode(struct input_dev *dev, int scancode, int keycode)
  116. {
  117. struct tps_key_entry *key;
  118. int old_keycode;
  119. if (keycode < 0 || keycode > KEY_MAX)
  120. return -EINVAL;
  121. key = tps_get_key_by_scancode(scancode);
  122. if (!key)
  123. return -EINVAL;
  124. old_keycode = key->keycode;
  125. key->keycode = keycode;
  126. set_bit(keycode, dev->keybit);
  127. if (!tps_get_key_by_keycode(old_keycode))
  128. clear_bit(old_keycode, dev->keybit);
  129. return 0;
  130. }
  131. static int acpi_topstar_init_hkey(struct topstar_hkey *hkey)
  132. {
  133. struct tps_key_entry *key;
  134. hkey->inputdev = input_allocate_device();
  135. if (!hkey->inputdev) {
  136. pr_err("Unable to allocate input device\n");
  137. return -ENODEV;
  138. }
  139. hkey->inputdev->name = "Topstar Laptop extra buttons";
  140. hkey->inputdev->phys = "topstar/input0";
  141. hkey->inputdev->id.bustype = BUS_HOST;
  142. hkey->inputdev->getkeycode = topstar_getkeycode;
  143. hkey->inputdev->setkeycode = topstar_setkeycode;
  144. for (key = topstar_keymap; key->code; key++) {
  145. set_bit(EV_KEY, hkey->inputdev->evbit);
  146. set_bit(key->keycode, hkey->inputdev->keybit);
  147. }
  148. if (input_register_device(hkey->inputdev)) {
  149. pr_err("Unable to register input device\n");
  150. input_free_device(hkey->inputdev);
  151. return -ENODEV;
  152. }
  153. return 0;
  154. }
  155. static int acpi_topstar_add(struct acpi_device *device)
  156. {
  157. struct topstar_hkey *tps_hkey;
  158. tps_hkey = kzalloc(sizeof(struct topstar_hkey), GFP_KERNEL);
  159. if (!tps_hkey)
  160. return -ENOMEM;
  161. strcpy(acpi_device_name(device), "Topstar TPSACPI");
  162. strcpy(acpi_device_class(device), ACPI_TOPSTAR_CLASS);
  163. if (acpi_topstar_fncx_switch(device, true))
  164. goto add_err;
  165. if (acpi_topstar_init_hkey(tps_hkey))
  166. goto add_err;
  167. device->driver_data = tps_hkey;
  168. return 0;
  169. add_err:
  170. kfree(tps_hkey);
  171. return -ENODEV;
  172. }
  173. static int acpi_topstar_remove(struct acpi_device *device, int type)
  174. {
  175. struct topstar_hkey *tps_hkey = acpi_driver_data(device);
  176. acpi_topstar_fncx_switch(device, false);
  177. input_unregister_device(tps_hkey->inputdev);
  178. kfree(tps_hkey);
  179. return 0;
  180. }
  181. static const struct acpi_device_id topstar_device_ids[] = {
  182. { "TPSACPI01", 0 },
  183. { "", 0 },
  184. };
  185. MODULE_DEVICE_TABLE(acpi, topstar_device_ids);
  186. static struct acpi_driver acpi_topstar_driver = {
  187. .name = "Topstar laptop ACPI driver",
  188. .class = ACPI_TOPSTAR_CLASS,
  189. .ids = topstar_device_ids,
  190. .ops = {
  191. .add = acpi_topstar_add,
  192. .remove = acpi_topstar_remove,
  193. .notify = acpi_topstar_notify,
  194. },
  195. };
  196. static int __init topstar_laptop_init(void)
  197. {
  198. int ret;
  199. ret = acpi_bus_register_driver(&acpi_topstar_driver);
  200. if (ret < 0)
  201. return ret;
  202. printk(KERN_INFO "Topstar Laptop ACPI extras driver loaded\n");
  203. return 0;
  204. }
  205. static void __exit topstar_laptop_exit(void)
  206. {
  207. acpi_bus_unregister_driver(&acpi_topstar_driver);
  208. }
  209. module_init(topstar_laptop_init);
  210. module_exit(topstar_laptop_exit);
  211. MODULE_AUTHOR("Herton Ronaldo Krzesinski");
  212. MODULE_DESCRIPTION("Topstar Laptop ACPI Extras driver");
  213. MODULE_LICENSE("GPL");