rfkill-input.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Input layer to RF Kill interface connector
  3. *
  4. * Copyright (c) 2007 Dmitry Torokhov
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/input.h>
  13. #include <linux/slab.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/init.h>
  16. #include <linux/rfkill.h>
  17. #include "rfkill-input.h"
  18. MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
  19. MODULE_DESCRIPTION("Input layer to RF switch connector");
  20. MODULE_LICENSE("GPL");
  21. struct rfkill_task {
  22. struct work_struct work;
  23. enum rfkill_type type;
  24. struct mutex mutex; /* ensures that task is serialized */
  25. spinlock_t lock; /* for accessing last and desired state */
  26. unsigned long last; /* last schedule */
  27. enum rfkill_state desired_state; /* on/off */
  28. };
  29. static void rfkill_task_handler(struct work_struct *work)
  30. {
  31. struct rfkill_task *task = container_of(work, struct rfkill_task, work);
  32. mutex_lock(&task->mutex);
  33. rfkill_switch_all(task->type, task->desired_state);
  34. mutex_unlock(&task->mutex);
  35. }
  36. static void rfkill_task_epo_handler(struct work_struct *work)
  37. {
  38. rfkill_epo();
  39. }
  40. static DECLARE_WORK(epo_work, rfkill_task_epo_handler);
  41. static void rfkill_schedule_epo(void)
  42. {
  43. schedule_work(&epo_work);
  44. }
  45. static void rfkill_schedule_set(struct rfkill_task *task,
  46. enum rfkill_state desired_state)
  47. {
  48. unsigned long flags;
  49. if (unlikely(work_pending(&epo_work)))
  50. return;
  51. spin_lock_irqsave(&task->lock, flags);
  52. if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
  53. task->desired_state = desired_state;
  54. task->last = jiffies;
  55. schedule_work(&task->work);
  56. }
  57. spin_unlock_irqrestore(&task->lock, flags);
  58. }
  59. static void rfkill_schedule_toggle(struct rfkill_task *task)
  60. {
  61. unsigned long flags;
  62. if (unlikely(work_pending(&epo_work)))
  63. return;
  64. spin_lock_irqsave(&task->lock, flags);
  65. if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
  66. task->desired_state = !task->desired_state;
  67. task->last = jiffies;
  68. schedule_work(&task->work);
  69. }
  70. spin_unlock_irqrestore(&task->lock, flags);
  71. }
  72. #define DEFINE_RFKILL_TASK(n, t) \
  73. struct rfkill_task n = { \
  74. .work = __WORK_INITIALIZER(n.work, \
  75. rfkill_task_handler), \
  76. .type = t, \
  77. .mutex = __MUTEX_INITIALIZER(n.mutex), \
  78. .lock = __SPIN_LOCK_UNLOCKED(n.lock), \
  79. .desired_state = RFKILL_STATE_ON, \
  80. }
  81. static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN);
  82. static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH);
  83. static DEFINE_RFKILL_TASK(rfkill_uwb, RFKILL_TYPE_UWB);
  84. static DEFINE_RFKILL_TASK(rfkill_wimax, RFKILL_TYPE_WIMAX);
  85. static DEFINE_RFKILL_TASK(rfkill_wwan, RFKILL_TYPE_WWAN);
  86. static void rfkill_event(struct input_handle *handle, unsigned int type,
  87. unsigned int code, int data)
  88. {
  89. if (type == EV_KEY && data == 1) {
  90. switch (code) {
  91. case KEY_WLAN:
  92. rfkill_schedule_toggle(&rfkill_wlan);
  93. break;
  94. case KEY_BLUETOOTH:
  95. rfkill_schedule_toggle(&rfkill_bt);
  96. break;
  97. case KEY_UWB:
  98. rfkill_schedule_toggle(&rfkill_uwb);
  99. break;
  100. case KEY_WIMAX:
  101. rfkill_schedule_toggle(&rfkill_wimax);
  102. break;
  103. default:
  104. break;
  105. }
  106. } else if (type == EV_SW) {
  107. switch (code) {
  108. case SW_RFKILL_ALL:
  109. /* EVERY radio type. data != 0 means radios ON */
  110. /* handle EPO (emergency power off) through shortcut */
  111. if (data) {
  112. rfkill_schedule_set(&rfkill_wwan,
  113. RFKILL_STATE_ON);
  114. rfkill_schedule_set(&rfkill_wimax,
  115. RFKILL_STATE_ON);
  116. rfkill_schedule_set(&rfkill_uwb,
  117. RFKILL_STATE_ON);
  118. rfkill_schedule_set(&rfkill_bt,
  119. RFKILL_STATE_ON);
  120. rfkill_schedule_set(&rfkill_wlan,
  121. RFKILL_STATE_ON);
  122. } else
  123. rfkill_schedule_epo();
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. }
  130. static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
  131. const struct input_device_id *id)
  132. {
  133. struct input_handle *handle;
  134. int error;
  135. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  136. if (!handle)
  137. return -ENOMEM;
  138. handle->dev = dev;
  139. handle->handler = handler;
  140. handle->name = "rfkill";
  141. error = input_register_handle(handle);
  142. if (error)
  143. goto err_free_handle;
  144. error = input_open_device(handle);
  145. if (error)
  146. goto err_unregister_handle;
  147. return 0;
  148. err_unregister_handle:
  149. input_unregister_handle(handle);
  150. err_free_handle:
  151. kfree(handle);
  152. return error;
  153. }
  154. static void rfkill_disconnect(struct input_handle *handle)
  155. {
  156. input_close_device(handle);
  157. input_unregister_handle(handle);
  158. kfree(handle);
  159. }
  160. static const struct input_device_id rfkill_ids[] = {
  161. {
  162. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  163. .evbit = { BIT_MASK(EV_KEY) },
  164. .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
  165. },
  166. {
  167. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  168. .evbit = { BIT_MASK(EV_KEY) },
  169. .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
  170. },
  171. {
  172. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  173. .evbit = { BIT_MASK(EV_KEY) },
  174. .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
  175. },
  176. {
  177. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  178. .evbit = { BIT_MASK(EV_KEY) },
  179. .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
  180. },
  181. {
  182. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
  183. .evbit = { BIT(EV_SW) },
  184. .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
  185. },
  186. { }
  187. };
  188. static struct input_handler rfkill_handler = {
  189. .event = rfkill_event,
  190. .connect = rfkill_connect,
  191. .disconnect = rfkill_disconnect,
  192. .name = "rfkill",
  193. .id_table = rfkill_ids,
  194. };
  195. static int __init rfkill_handler_init(void)
  196. {
  197. return input_register_handler(&rfkill_handler);
  198. }
  199. static void __exit rfkill_handler_exit(void)
  200. {
  201. input_unregister_handler(&rfkill_handler);
  202. flush_scheduled_work();
  203. }
  204. module_init(rfkill_handler_init);
  205. module_exit(rfkill_handler_exit);