keyreset.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* drivers/input/keyreset.c
  2. *
  3. * Copyright (C) 2008 Google, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/input.h>
  16. #include <linux/keyreset.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/reboot.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/syscalls.h>
  23. struct keyreset_state {
  24. struct input_handler input_handler;
  25. unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
  26. unsigned long upbit[BITS_TO_LONGS(KEY_CNT)];
  27. unsigned long key[BITS_TO_LONGS(KEY_CNT)];
  28. spinlock_t lock;
  29. int key_down_target;
  30. int key_down;
  31. int key_up;
  32. int restart_disabled;
  33. int (*reset_fn)(void);
  34. };
  35. int restart_requested;
  36. static void deferred_restart(struct work_struct *dummy)
  37. {
  38. restart_requested = 2;
  39. sys_sync();
  40. restart_requested = 3;
  41. kernel_restart(NULL);
  42. }
  43. static DECLARE_WORK(restart_work, deferred_restart);
  44. static void keyreset_event(struct input_handle *handle, unsigned int type,
  45. unsigned int code, int value)
  46. {
  47. unsigned long flags;
  48. struct keyreset_state *state = handle->private;
  49. if (type != EV_KEY)
  50. return;
  51. if (code >= KEY_MAX)
  52. return;
  53. if (!test_bit(code, state->keybit))
  54. return;
  55. spin_lock_irqsave(&state->lock, flags);
  56. if (!test_bit(code, state->key) == !value)
  57. goto done;
  58. __change_bit(code, state->key);
  59. if (test_bit(code, state->upbit)) {
  60. if (value) {
  61. state->restart_disabled = 1;
  62. state->key_up++;
  63. } else
  64. state->key_up--;
  65. } else {
  66. if (value)
  67. state->key_down++;
  68. else
  69. state->key_down--;
  70. }
  71. if (state->key_down == 0 && state->key_up == 0)
  72. state->restart_disabled = 0;
  73. pr_debug("reset key changed %d %d new state %d-%d-%d\n", code, value,
  74. state->key_down, state->key_up, state->restart_disabled);
  75. if (value && !state->restart_disabled &&
  76. state->key_down == state->key_down_target) {
  77. state->restart_disabled = 1;
  78. if (restart_requested)
  79. panic("keyboard reset failed, %d", restart_requested);
  80. if (state->reset_fn) {
  81. restart_requested = state->reset_fn();
  82. } else {
  83. pr_info("keyboard reset\n");
  84. schedule_work(&restart_work);
  85. restart_requested = 1;
  86. }
  87. }
  88. done:
  89. spin_unlock_irqrestore(&state->lock, flags);
  90. }
  91. static int keyreset_connect(struct input_handler *handler,
  92. struct input_dev *dev,
  93. const struct input_device_id *id)
  94. {
  95. int i;
  96. int ret;
  97. struct input_handle *handle;
  98. struct keyreset_state *state =
  99. container_of(handler, struct keyreset_state, input_handler);
  100. for (i = 0; i < KEY_MAX; i++) {
  101. if (test_bit(i, state->keybit) && test_bit(i, dev->keybit))
  102. break;
  103. }
  104. if (i == KEY_MAX)
  105. return -ENODEV;
  106. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  107. if (!handle)
  108. return -ENOMEM;
  109. handle->dev = dev;
  110. handle->handler = handler;
  111. handle->name = "keyreset";
  112. handle->private = state;
  113. ret = input_register_handle(handle);
  114. if (ret)
  115. goto err_input_register_handle;
  116. ret = input_open_device(handle);
  117. if (ret)
  118. goto err_input_open_device;
  119. pr_info("using input dev %s for key reset\n", dev->name);
  120. return 0;
  121. err_input_open_device:
  122. input_unregister_handle(handle);
  123. err_input_register_handle:
  124. kfree(handle);
  125. return ret;
  126. }
  127. static void keyreset_disconnect(struct input_handle *handle)
  128. {
  129. input_close_device(handle);
  130. input_unregister_handle(handle);
  131. kfree(handle);
  132. }
  133. static const struct input_device_id keyreset_ids[] = {
  134. {
  135. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  136. .evbit = { BIT_MASK(EV_KEY) },
  137. },
  138. { },
  139. };
  140. MODULE_DEVICE_TABLE(input, keyreset_ids);
  141. static int keyreset_probe(struct platform_device *pdev)
  142. {
  143. int ret;
  144. int key, *keyp;
  145. struct keyreset_state *state;
  146. struct keyreset_platform_data *pdata = pdev->dev.platform_data;
  147. if (!pdata)
  148. return -EINVAL;
  149. state = kzalloc(sizeof(*state), GFP_KERNEL);
  150. if (!state)
  151. return -ENOMEM;
  152. spin_lock_init(&state->lock);
  153. keyp = pdata->keys_down;
  154. while ((key = *keyp++)) {
  155. if (key >= KEY_MAX)
  156. continue;
  157. state->key_down_target++;
  158. __set_bit(key, state->keybit);
  159. }
  160. if (pdata->keys_up) {
  161. keyp = pdata->keys_up;
  162. while ((key = *keyp++)) {
  163. if (key >= KEY_MAX)
  164. continue;
  165. __set_bit(key, state->keybit);
  166. __set_bit(key, state->upbit);
  167. }
  168. }
  169. if (pdata->reset_fn)
  170. state->reset_fn = pdata->reset_fn;
  171. state->input_handler.event = keyreset_event;
  172. state->input_handler.connect = keyreset_connect;
  173. state->input_handler.disconnect = keyreset_disconnect;
  174. state->input_handler.name = KEYRESET_NAME;
  175. state->input_handler.id_table = keyreset_ids;
  176. ret = input_register_handler(&state->input_handler);
  177. if (ret) {
  178. kfree(state);
  179. return ret;
  180. }
  181. platform_set_drvdata(pdev, state);
  182. return 0;
  183. }
  184. int keyreset_remove(struct platform_device *pdev)
  185. {
  186. struct keyreset_state *state = platform_get_drvdata(pdev);
  187. input_unregister_handler(&state->input_handler);
  188. kfree(state);
  189. return 0;
  190. }
  191. struct platform_driver keyreset_driver = {
  192. .driver.name = KEYRESET_NAME,
  193. .probe = keyreset_probe,
  194. .remove = keyreset_remove,
  195. };
  196. static int __init keyreset_init(void)
  197. {
  198. return platform_driver_register(&keyreset_driver);
  199. }
  200. static void __exit keyreset_exit(void)
  201. {
  202. return platform_driver_unregister(&keyreset_driver);
  203. }
  204. module_init(keyreset_init);
  205. module_exit(keyreset_exit);