rfkill-input.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. enum rfkill_state current_state; /* on/off */
  29. };
  30. static void rfkill_task_handler(struct work_struct *work)
  31. {
  32. struct rfkill_task *task = container_of(work, struct rfkill_task, work);
  33. enum rfkill_state state;
  34. mutex_lock(&task->mutex);
  35. /*
  36. * Use temp variable to fetch desired state to keep it
  37. * consistent even if rfkill_schedule_toggle() runs in
  38. * another thread or interrupts us.
  39. */
  40. state = task->desired_state;
  41. if (state != task->current_state) {
  42. rfkill_switch_all(task->type, state);
  43. task->current_state = state;
  44. }
  45. mutex_unlock(&task->mutex);
  46. }
  47. static void rfkill_schedule_toggle(struct rfkill_task *task)
  48. {
  49. unsigned long flags;
  50. spin_lock_irqsave(&task->lock, flags);
  51. if (time_after(jiffies, task->last + msecs_to_jiffies(200))) {
  52. task->desired_state = !task->desired_state;
  53. task->last = jiffies;
  54. schedule_work(&task->work);
  55. }
  56. spin_unlock_irqrestore(&task->lock, flags);
  57. }
  58. #define DEFINE_RFKILL_TASK(n, t) \
  59. struct rfkill_task n = { \
  60. .work = __WORK_INITIALIZER(n.work, \
  61. rfkill_task_handler), \
  62. .type = t, \
  63. .mutex = __MUTEX_INITIALIZER(n.mutex), \
  64. .lock = __SPIN_LOCK_UNLOCKED(n.lock), \
  65. .desired_state = RFKILL_STATE_ON, \
  66. .current_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 void rfkill_event(struct input_handle *handle, unsigned int type,
  72. unsigned int code, int down)
  73. {
  74. if (type == EV_KEY && down == 1) {
  75. switch (code) {
  76. case KEY_WLAN:
  77. rfkill_schedule_toggle(&rfkill_wlan);
  78. break;
  79. case KEY_BLUETOOTH:
  80. rfkill_schedule_toggle(&rfkill_bt);
  81. break;
  82. case KEY_UWB:
  83. rfkill_schedule_toggle(&rfkill_uwb);
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. }
  90. static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
  91. const struct input_device_id *id)
  92. {
  93. struct input_handle *handle;
  94. int error;
  95. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  96. if (!handle)
  97. return -ENOMEM;
  98. handle->dev = dev;
  99. handle->handler = handler;
  100. handle->name = "rfkill";
  101. error = input_register_handle(handle);
  102. if (error)
  103. goto err_free_handle;
  104. error = input_open_device(handle);
  105. if (error)
  106. goto err_unregister_handle;
  107. return 0;
  108. err_unregister_handle:
  109. input_unregister_handle(handle);
  110. err_free_handle:
  111. kfree(handle);
  112. return error;
  113. }
  114. static void rfkill_disconnect(struct input_handle *handle)
  115. {
  116. input_close_device(handle);
  117. input_unregister_handle(handle);
  118. kfree(handle);
  119. }
  120. static const struct input_device_id rfkill_ids[] = {
  121. {
  122. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  123. .evbit = { BIT_MASK(EV_KEY) },
  124. .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
  125. },
  126. {
  127. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  128. .evbit = { BIT_MASK(EV_KEY) },
  129. .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
  130. },
  131. {
  132. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  133. .evbit = { BIT_MASK(EV_KEY) },
  134. .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
  135. },
  136. { }
  137. };
  138. static struct input_handler rfkill_handler = {
  139. .event = rfkill_event,
  140. .connect = rfkill_connect,
  141. .disconnect = rfkill_disconnect,
  142. .name = "rfkill",
  143. .id_table = rfkill_ids,
  144. };
  145. static int __init rfkill_handler_init(void)
  146. {
  147. return input_register_handler(&rfkill_handler);
  148. }
  149. static void __exit rfkill_handler_exit(void)
  150. {
  151. input_unregister_handler(&rfkill_handler);
  152. flush_scheduled_work();
  153. }
  154. module_init(rfkill_handler_init);
  155. module_exit(rfkill_handler_exit);