rfkill-input.c 4.3 KB

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