rfkill-input.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 <linux/sched.h>
  18. #include "rfkill-input.h"
  19. MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
  20. MODULE_DESCRIPTION("Input layer to RF switch connector");
  21. MODULE_LICENSE("GPL");
  22. enum rfkill_input_master_mode {
  23. RFKILL_INPUT_MASTER_DONOTHING = 0,
  24. RFKILL_INPUT_MASTER_RESTORE = 1,
  25. RFKILL_INPUT_MASTER_UNBLOCKALL = 2,
  26. RFKILL_INPUT_MASTER_MAX, /* marker */
  27. };
  28. /* Delay (in ms) between consecutive switch ops */
  29. #define RFKILL_OPS_DELAY 200
  30. static enum rfkill_input_master_mode rfkill_master_switch_mode =
  31. RFKILL_INPUT_MASTER_UNBLOCKALL;
  32. module_param_named(master_switch_mode, rfkill_master_switch_mode, uint, 0);
  33. MODULE_PARM_DESC(master_switch_mode,
  34. "SW_RFKILL_ALL ON should: 0=do nothing; 1=restore; 2=unblock all");
  35. enum rfkill_global_sched_op {
  36. RFKILL_GLOBAL_OP_EPO = 0,
  37. RFKILL_GLOBAL_OP_RESTORE,
  38. RFKILL_GLOBAL_OP_UNLOCK,
  39. RFKILL_GLOBAL_OP_UNBLOCK,
  40. };
  41. struct rfkill_task {
  42. struct delayed_work dwork;
  43. /* ensures that task is serialized */
  44. struct mutex mutex;
  45. /* protects everything below */
  46. spinlock_t lock;
  47. /* pending regular switch operations (1=pending) */
  48. unsigned long sw_pending[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
  49. /* should the state be complemented (1=yes) */
  50. unsigned long sw_togglestate[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
  51. bool global_op_pending;
  52. enum rfkill_global_sched_op op;
  53. /* last time it was scheduled */
  54. unsigned long last_scheduled;
  55. };
  56. static void __rfkill_handle_global_op(enum rfkill_global_sched_op op)
  57. {
  58. unsigned int i;
  59. switch (op) {
  60. case RFKILL_GLOBAL_OP_EPO:
  61. rfkill_epo();
  62. break;
  63. case RFKILL_GLOBAL_OP_RESTORE:
  64. rfkill_restore_states();
  65. break;
  66. case RFKILL_GLOBAL_OP_UNLOCK:
  67. rfkill_remove_epo_lock();
  68. break;
  69. case RFKILL_GLOBAL_OP_UNBLOCK:
  70. rfkill_remove_epo_lock();
  71. for (i = 0; i < RFKILL_TYPE_MAX; i++)
  72. rfkill_switch_all(i, RFKILL_STATE_UNBLOCKED);
  73. break;
  74. default:
  75. /* memory corruption or bug, fail safely */
  76. rfkill_epo();
  77. WARN(1, "Unknown requested operation %d! "
  78. "rfkill Emergency Power Off activated\n",
  79. op);
  80. }
  81. }
  82. static void __rfkill_handle_normal_op(const enum rfkill_type type,
  83. const bool c)
  84. {
  85. enum rfkill_state state;
  86. state = rfkill_get_global_state(type);
  87. if (c)
  88. state = rfkill_state_complement(state);
  89. rfkill_switch_all(type, state);
  90. }
  91. static void rfkill_task_handler(struct work_struct *work)
  92. {
  93. struct rfkill_task *task = container_of(work,
  94. struct rfkill_task, dwork.work);
  95. bool doit = true;
  96. mutex_lock(&task->mutex);
  97. spin_lock_irq(&task->lock);
  98. while (doit) {
  99. if (task->global_op_pending) {
  100. enum rfkill_global_sched_op op = task->op;
  101. task->global_op_pending = false;
  102. memset(task->sw_pending, 0, sizeof(task->sw_pending));
  103. spin_unlock_irq(&task->lock);
  104. __rfkill_handle_global_op(op);
  105. /* make sure we do at least one pass with
  106. * !task->global_op_pending */
  107. spin_lock_irq(&task->lock);
  108. continue;
  109. } else if (!rfkill_is_epo_lock_active()) {
  110. unsigned int i = 0;
  111. while (!task->global_op_pending &&
  112. i < RFKILL_TYPE_MAX) {
  113. if (test_and_clear_bit(i, task->sw_pending)) {
  114. bool c;
  115. c = test_and_clear_bit(i,
  116. task->sw_togglestate);
  117. spin_unlock_irq(&task->lock);
  118. __rfkill_handle_normal_op(i, c);
  119. spin_lock_irq(&task->lock);
  120. }
  121. i++;
  122. }
  123. }
  124. doit = task->global_op_pending;
  125. }
  126. spin_unlock_irq(&task->lock);
  127. mutex_unlock(&task->mutex);
  128. }
  129. static struct rfkill_task rfkill_task = {
  130. .dwork = __DELAYED_WORK_INITIALIZER(rfkill_task.dwork,
  131. rfkill_task_handler),
  132. .mutex = __MUTEX_INITIALIZER(rfkill_task.mutex),
  133. .lock = __SPIN_LOCK_UNLOCKED(rfkill_task.lock),
  134. };
  135. static unsigned long rfkill_ratelimit(const unsigned long last)
  136. {
  137. const unsigned long delay = msecs_to_jiffies(RFKILL_OPS_DELAY);
  138. return (time_after(jiffies, last + delay)) ? 0 : delay;
  139. }
  140. static void rfkill_schedule_ratelimited(void)
  141. {
  142. if (!delayed_work_pending(&rfkill_task.dwork)) {
  143. schedule_delayed_work(&rfkill_task.dwork,
  144. rfkill_ratelimit(rfkill_task.last_scheduled));
  145. rfkill_task.last_scheduled = jiffies;
  146. }
  147. }
  148. static void rfkill_schedule_global_op(enum rfkill_global_sched_op op)
  149. {
  150. unsigned long flags;
  151. spin_lock_irqsave(&rfkill_task.lock, flags);
  152. rfkill_task.op = op;
  153. rfkill_task.global_op_pending = true;
  154. if (op == RFKILL_GLOBAL_OP_EPO && !rfkill_is_epo_lock_active()) {
  155. /* bypass the limiter for EPO */
  156. cancel_delayed_work(&rfkill_task.dwork);
  157. schedule_delayed_work(&rfkill_task.dwork, 0);
  158. rfkill_task.last_scheduled = jiffies;
  159. } else
  160. rfkill_schedule_ratelimited();
  161. spin_unlock_irqrestore(&rfkill_task.lock, flags);
  162. }
  163. static void rfkill_schedule_toggle(enum rfkill_type type)
  164. {
  165. unsigned long flags;
  166. if (rfkill_is_epo_lock_active())
  167. return;
  168. spin_lock_irqsave(&rfkill_task.lock, flags);
  169. if (!rfkill_task.global_op_pending) {
  170. set_bit(type, rfkill_task.sw_pending);
  171. change_bit(type, rfkill_task.sw_togglestate);
  172. rfkill_schedule_ratelimited();
  173. }
  174. spin_unlock_irqrestore(&rfkill_task.lock, flags);
  175. }
  176. static void rfkill_schedule_evsw_rfkillall(int state)
  177. {
  178. if (state) {
  179. switch (rfkill_master_switch_mode) {
  180. case RFKILL_INPUT_MASTER_UNBLOCKALL:
  181. rfkill_schedule_global_op(RFKILL_GLOBAL_OP_UNBLOCK);
  182. break;
  183. case RFKILL_INPUT_MASTER_RESTORE:
  184. rfkill_schedule_global_op(RFKILL_GLOBAL_OP_RESTORE);
  185. break;
  186. case RFKILL_INPUT_MASTER_DONOTHING:
  187. rfkill_schedule_global_op(RFKILL_GLOBAL_OP_UNLOCK);
  188. break;
  189. default:
  190. /* memory corruption or driver bug! fail safely */
  191. rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
  192. WARN(1, "Unknown rfkill_master_switch_mode (%d), "
  193. "driver bug or memory corruption detected!\n",
  194. rfkill_master_switch_mode);
  195. break;
  196. }
  197. } else
  198. rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
  199. }
  200. static void rfkill_event(struct input_handle *handle, unsigned int type,
  201. unsigned int code, int data)
  202. {
  203. if (type == EV_KEY && data == 1) {
  204. enum rfkill_type t;
  205. switch (code) {
  206. case KEY_WLAN:
  207. t = RFKILL_TYPE_WLAN;
  208. break;
  209. case KEY_BLUETOOTH:
  210. t = RFKILL_TYPE_BLUETOOTH;
  211. break;
  212. case KEY_UWB:
  213. t = RFKILL_TYPE_UWB;
  214. break;
  215. case KEY_WIMAX:
  216. t = RFKILL_TYPE_WIMAX;
  217. break;
  218. default:
  219. return;
  220. }
  221. rfkill_schedule_toggle(t);
  222. return;
  223. } else if (type == EV_SW) {
  224. switch (code) {
  225. case SW_RFKILL_ALL:
  226. rfkill_schedule_evsw_rfkillall(data);
  227. return;
  228. default:
  229. return;
  230. }
  231. }
  232. }
  233. static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
  234. const struct input_device_id *id)
  235. {
  236. struct input_handle *handle;
  237. int error;
  238. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  239. if (!handle)
  240. return -ENOMEM;
  241. handle->dev = dev;
  242. handle->handler = handler;
  243. handle->name = "rfkill";
  244. /* causes rfkill_start() to be called */
  245. error = input_register_handle(handle);
  246. if (error)
  247. goto err_free_handle;
  248. error = input_open_device(handle);
  249. if (error)
  250. goto err_unregister_handle;
  251. return 0;
  252. err_unregister_handle:
  253. input_unregister_handle(handle);
  254. err_free_handle:
  255. kfree(handle);
  256. return error;
  257. }
  258. static void rfkill_start(struct input_handle *handle)
  259. {
  260. /* Take event_lock to guard against configuration changes, we
  261. * should be able to deal with concurrency with rfkill_event()
  262. * just fine (which event_lock will also avoid). */
  263. spin_lock_irq(&handle->dev->event_lock);
  264. if (test_bit(EV_SW, handle->dev->evbit)) {
  265. if (test_bit(SW_RFKILL_ALL, handle->dev->swbit))
  266. rfkill_schedule_evsw_rfkillall(test_bit(SW_RFKILL_ALL,
  267. handle->dev->sw));
  268. /* add resync for further EV_SW events here */
  269. }
  270. spin_unlock_irq(&handle->dev->event_lock);
  271. }
  272. static void rfkill_disconnect(struct input_handle *handle)
  273. {
  274. input_close_device(handle);
  275. input_unregister_handle(handle);
  276. kfree(handle);
  277. }
  278. static const struct input_device_id rfkill_ids[] = {
  279. {
  280. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  281. .evbit = { BIT_MASK(EV_KEY) },
  282. .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
  283. },
  284. {
  285. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  286. .evbit = { BIT_MASK(EV_KEY) },
  287. .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
  288. },
  289. {
  290. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  291. .evbit = { BIT_MASK(EV_KEY) },
  292. .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
  293. },
  294. {
  295. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  296. .evbit = { BIT_MASK(EV_KEY) },
  297. .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
  298. },
  299. {
  300. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
  301. .evbit = { BIT(EV_SW) },
  302. .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
  303. },
  304. { }
  305. };
  306. static struct input_handler rfkill_handler = {
  307. .event = rfkill_event,
  308. .connect = rfkill_connect,
  309. .disconnect = rfkill_disconnect,
  310. .start = rfkill_start,
  311. .name = "rfkill",
  312. .id_table = rfkill_ids,
  313. };
  314. static int __init rfkill_handler_init(void)
  315. {
  316. if (rfkill_master_switch_mode >= RFKILL_INPUT_MASTER_MAX)
  317. return -EINVAL;
  318. /*
  319. * The penalty to not doing this is a possible RFKILL_OPS_DELAY delay
  320. * at the first use. Acceptable, but if we can avoid it, why not?
  321. */
  322. rfkill_task.last_scheduled =
  323. jiffies - msecs_to_jiffies(RFKILL_OPS_DELAY) - 1;
  324. return input_register_handler(&rfkill_handler);
  325. }
  326. static void __exit rfkill_handler_exit(void)
  327. {
  328. input_unregister_handler(&rfkill_handler);
  329. cancel_delayed_work_sync(&rfkill_task.dwork);
  330. rfkill_remove_epo_lock();
  331. }
  332. module_init(rfkill_handler_init);
  333. module_exit(rfkill_handler_exit);