rfkill-input.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 void rfkill_event(struct input_handle *handle, unsigned int type,
  70. unsigned int code, int down)
  71. {
  72. if (type == EV_KEY && down == 1) {
  73. switch (code) {
  74. case KEY_WLAN:
  75. rfkill_schedule_toggle(&rfkill_wlan);
  76. break;
  77. case KEY_BLUETOOTH:
  78. rfkill_schedule_toggle(&rfkill_bt);
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. }
  85. static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
  86. const struct input_device_id *id)
  87. {
  88. struct input_handle *handle;
  89. int error;
  90. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  91. if (!handle)
  92. return -ENOMEM;
  93. handle->dev = dev;
  94. handle->handler = handler;
  95. handle->name = "rfkill";
  96. error = input_register_handle(handle);
  97. if (error)
  98. goto err_free_handle;
  99. error = input_open_device(handle);
  100. if (error)
  101. goto err_unregister_handle;
  102. return 0;
  103. err_unregister_handle:
  104. input_unregister_handle(handle);
  105. err_free_handle:
  106. kfree(handle);
  107. return error;
  108. }
  109. static void rfkill_disconnect(struct input_handle *handle)
  110. {
  111. input_close_device(handle);
  112. input_unregister_handle(handle);
  113. kfree(handle);
  114. }
  115. static const struct input_device_id rfkill_ids[] = {
  116. {
  117. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  118. .evbit = { BIT(EV_KEY) },
  119. .keybit = { [LONG(KEY_WLAN)] = BIT(KEY_WLAN) },
  120. },
  121. {
  122. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  123. .evbit = { BIT(EV_KEY) },
  124. .keybit = { [LONG(KEY_BLUETOOTH)] = BIT(KEY_BLUETOOTH) },
  125. },
  126. { }
  127. };
  128. static struct input_handler rfkill_handler = {
  129. .event = rfkill_event,
  130. .connect = rfkill_connect,
  131. .disconnect = rfkill_disconnect,
  132. .name = "rfkill",
  133. .id_table = rfkill_ids,
  134. };
  135. static int __init rfkill_handler_init(void)
  136. {
  137. return input_register_handler(&rfkill_handler);
  138. }
  139. static void __exit rfkill_handler_exit(void)
  140. {
  141. input_unregister_handler(&rfkill_handler);
  142. flush_scheduled_work();
  143. }
  144. module_init(rfkill_handler_init);
  145. module_exit(rfkill_handler_exit);