rfkill-input.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 =
  67. rfkill_state_complement(task->desired_state);
  68. task->last = jiffies;
  69. schedule_work(&task->work);
  70. }
  71. spin_unlock_irqrestore(&task->lock, flags);
  72. }
  73. #define DEFINE_RFKILL_TASK(n, t) \
  74. struct rfkill_task n = { \
  75. .work = __WORK_INITIALIZER(n.work, \
  76. rfkill_task_handler), \
  77. .type = t, \
  78. .mutex = __MUTEX_INITIALIZER(n.mutex), \
  79. .lock = __SPIN_LOCK_UNLOCKED(n.lock), \
  80. .desired_state = RFKILL_STATE_UNBLOCKED, \
  81. }
  82. static DEFINE_RFKILL_TASK(rfkill_wlan, RFKILL_TYPE_WLAN);
  83. static DEFINE_RFKILL_TASK(rfkill_bt, RFKILL_TYPE_BLUETOOTH);
  84. static DEFINE_RFKILL_TASK(rfkill_uwb, RFKILL_TYPE_UWB);
  85. static DEFINE_RFKILL_TASK(rfkill_wimax, RFKILL_TYPE_WIMAX);
  86. static DEFINE_RFKILL_TASK(rfkill_wwan, RFKILL_TYPE_WWAN);
  87. static void rfkill_event(struct input_handle *handle, unsigned int type,
  88. unsigned int code, int data)
  89. {
  90. if (type == EV_KEY && data == 1) {
  91. switch (code) {
  92. case KEY_WLAN:
  93. rfkill_schedule_toggle(&rfkill_wlan);
  94. break;
  95. case KEY_BLUETOOTH:
  96. rfkill_schedule_toggle(&rfkill_bt);
  97. break;
  98. case KEY_UWB:
  99. rfkill_schedule_toggle(&rfkill_uwb);
  100. break;
  101. case KEY_WIMAX:
  102. rfkill_schedule_toggle(&rfkill_wimax);
  103. break;
  104. default:
  105. break;
  106. }
  107. } else if (type == EV_SW) {
  108. switch (code) {
  109. case SW_RFKILL_ALL:
  110. /* EVERY radio type. data != 0 means radios ON */
  111. /* handle EPO (emergency power off) through shortcut */
  112. if (data) {
  113. rfkill_schedule_set(&rfkill_wwan,
  114. RFKILL_STATE_UNBLOCKED);
  115. rfkill_schedule_set(&rfkill_wimax,
  116. RFKILL_STATE_UNBLOCKED);
  117. rfkill_schedule_set(&rfkill_uwb,
  118. RFKILL_STATE_UNBLOCKED);
  119. rfkill_schedule_set(&rfkill_bt,
  120. RFKILL_STATE_UNBLOCKED);
  121. rfkill_schedule_set(&rfkill_wlan,
  122. RFKILL_STATE_UNBLOCKED);
  123. } else
  124. rfkill_schedule_epo();
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. }
  131. static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
  132. const struct input_device_id *id)
  133. {
  134. struct input_handle *handle;
  135. int error;
  136. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  137. if (!handle)
  138. return -ENOMEM;
  139. handle->dev = dev;
  140. handle->handler = handler;
  141. handle->name = "rfkill";
  142. error = input_register_handle(handle);
  143. if (error)
  144. goto err_free_handle;
  145. error = input_open_device(handle);
  146. if (error)
  147. goto err_unregister_handle;
  148. return 0;
  149. err_unregister_handle:
  150. input_unregister_handle(handle);
  151. err_free_handle:
  152. kfree(handle);
  153. return error;
  154. }
  155. static void rfkill_disconnect(struct input_handle *handle)
  156. {
  157. input_close_device(handle);
  158. input_unregister_handle(handle);
  159. kfree(handle);
  160. }
  161. static const struct input_device_id rfkill_ids[] = {
  162. {
  163. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  164. .evbit = { BIT_MASK(EV_KEY) },
  165. .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
  166. },
  167. {
  168. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  169. .evbit = { BIT_MASK(EV_KEY) },
  170. .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
  171. },
  172. {
  173. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  174. .evbit = { BIT_MASK(EV_KEY) },
  175. .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
  176. },
  177. {
  178. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  179. .evbit = { BIT_MASK(EV_KEY) },
  180. .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
  181. },
  182. {
  183. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
  184. .evbit = { BIT(EV_SW) },
  185. .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
  186. },
  187. { }
  188. };
  189. static struct input_handler rfkill_handler = {
  190. .event = rfkill_event,
  191. .connect = rfkill_connect,
  192. .disconnect = rfkill_disconnect,
  193. .name = "rfkill",
  194. .id_table = rfkill_ids,
  195. };
  196. static int __init rfkill_handler_init(void)
  197. {
  198. return input_register_handler(&rfkill_handler);
  199. }
  200. static void __exit rfkill_handler_exit(void)
  201. {
  202. input_unregister_handler(&rfkill_handler);
  203. flush_scheduled_work();
  204. }
  205. module_init(rfkill_handler_init);
  206. module_exit(rfkill_handler_exit);