rfkill-input.c 5.6 KB

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