input.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Input layer to RF Kill interface connector
  3. *
  4. * Copyright (c) 2007 Dmitry Torokhov
  5. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  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. * If you ever run into a situation in which you have a SW_ type rfkill
  12. * input device, then you can revive code that was removed in the patch
  13. * "rfkill-input: remove unused code".
  14. */
  15. #include <linux/input.h>
  16. #include <linux/slab.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/init.h>
  19. #include <linux/rfkill.h>
  20. #include <linux/sched.h>
  21. #include "rfkill.h"
  22. enum rfkill_input_master_mode {
  23. RFKILL_INPUT_MASTER_UNLOCK = 0,
  24. RFKILL_INPUT_MASTER_RESTORE = 1,
  25. RFKILL_INPUT_MASTER_UNBLOCKALL = 2,
  26. NUM_RFKILL_INPUT_MASTER_MODES
  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 (only unlock); 1=restore; 2=unblock all");
  35. static spinlock_t rfkill_op_lock;
  36. static bool rfkill_op_pending;
  37. static unsigned long rfkill_sw_pending[BITS_TO_LONGS(NUM_RFKILL_TYPES)];
  38. static unsigned long rfkill_sw_state[BITS_TO_LONGS(NUM_RFKILL_TYPES)];
  39. enum rfkill_sched_op {
  40. RFKILL_GLOBAL_OP_EPO = 0,
  41. RFKILL_GLOBAL_OP_RESTORE,
  42. RFKILL_GLOBAL_OP_UNLOCK,
  43. RFKILL_GLOBAL_OP_UNBLOCK,
  44. };
  45. static enum rfkill_sched_op rfkill_master_switch_op;
  46. static enum rfkill_sched_op rfkill_op;
  47. static void __rfkill_handle_global_op(enum rfkill_sched_op op)
  48. {
  49. unsigned int i;
  50. switch (op) {
  51. case RFKILL_GLOBAL_OP_EPO:
  52. rfkill_epo();
  53. break;
  54. case RFKILL_GLOBAL_OP_RESTORE:
  55. rfkill_restore_states();
  56. break;
  57. case RFKILL_GLOBAL_OP_UNLOCK:
  58. rfkill_remove_epo_lock();
  59. break;
  60. case RFKILL_GLOBAL_OP_UNBLOCK:
  61. rfkill_remove_epo_lock();
  62. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  63. rfkill_switch_all(i, false);
  64. break;
  65. default:
  66. /* memory corruption or bug, fail safely */
  67. rfkill_epo();
  68. WARN(1, "Unknown requested operation %d! "
  69. "rfkill Emergency Power Off activated\n",
  70. op);
  71. }
  72. }
  73. static void __rfkill_handle_normal_op(const enum rfkill_type type,
  74. const bool complement)
  75. {
  76. bool blocked;
  77. blocked = rfkill_get_global_sw_state(type);
  78. if (complement)
  79. blocked = !blocked;
  80. rfkill_switch_all(type, blocked);
  81. }
  82. static void rfkill_op_handler(struct work_struct *work)
  83. {
  84. unsigned int i;
  85. bool c;
  86. spin_lock_irq(&rfkill_op_lock);
  87. do {
  88. if (rfkill_op_pending) {
  89. enum rfkill_sched_op op = rfkill_op;
  90. rfkill_op_pending = false;
  91. memset(rfkill_sw_pending, 0,
  92. sizeof(rfkill_sw_pending));
  93. spin_unlock_irq(&rfkill_op_lock);
  94. __rfkill_handle_global_op(op);
  95. spin_lock_irq(&rfkill_op_lock);
  96. /*
  97. * handle global ops first -- during unlocked period
  98. * we might have gotten a new global op.
  99. */
  100. if (rfkill_op_pending)
  101. continue;
  102. }
  103. if (rfkill_is_epo_lock_active())
  104. continue;
  105. for (i = 0; i < NUM_RFKILL_TYPES; i++) {
  106. if (__test_and_clear_bit(i, rfkill_sw_pending)) {
  107. c = __test_and_clear_bit(i, rfkill_sw_state);
  108. spin_unlock_irq(&rfkill_op_lock);
  109. __rfkill_handle_normal_op(i, c);
  110. spin_lock_irq(&rfkill_op_lock);
  111. }
  112. }
  113. } while (rfkill_op_pending);
  114. spin_unlock_irq(&rfkill_op_lock);
  115. }
  116. static DECLARE_DELAYED_WORK(rfkill_op_work, rfkill_op_handler);
  117. static unsigned long rfkill_last_scheduled;
  118. static unsigned long rfkill_ratelimit(const unsigned long last)
  119. {
  120. const unsigned long delay = msecs_to_jiffies(RFKILL_OPS_DELAY);
  121. return (time_after(jiffies, last + delay)) ? 0 : delay;
  122. }
  123. static void rfkill_schedule_ratelimited(void)
  124. {
  125. if (delayed_work_pending(&rfkill_op_work))
  126. return;
  127. schedule_delayed_work(&rfkill_op_work,
  128. rfkill_ratelimit(rfkill_last_scheduled));
  129. rfkill_last_scheduled = jiffies;
  130. }
  131. static void rfkill_schedule_global_op(enum rfkill_sched_op op)
  132. {
  133. unsigned long flags;
  134. spin_lock_irqsave(&rfkill_op_lock, flags);
  135. rfkill_op = op;
  136. rfkill_op_pending = true;
  137. if (op == RFKILL_GLOBAL_OP_EPO && !rfkill_is_epo_lock_active()) {
  138. /* bypass the limiter for EPO */
  139. cancel_delayed_work(&rfkill_op_work);
  140. schedule_delayed_work(&rfkill_op_work, 0);
  141. rfkill_last_scheduled = jiffies;
  142. } else
  143. rfkill_schedule_ratelimited();
  144. spin_unlock_irqrestore(&rfkill_op_lock, flags);
  145. }
  146. static void rfkill_schedule_toggle(enum rfkill_type type)
  147. {
  148. unsigned long flags;
  149. if (rfkill_is_epo_lock_active())
  150. return;
  151. spin_lock_irqsave(&rfkill_op_lock, flags);
  152. if (!rfkill_op_pending) {
  153. __set_bit(type, rfkill_sw_pending);
  154. __change_bit(type, rfkill_sw_state);
  155. rfkill_schedule_ratelimited();
  156. }
  157. spin_unlock_irqrestore(&rfkill_op_lock, flags);
  158. }
  159. static void rfkill_schedule_evsw_rfkillall(int state)
  160. {
  161. if (state)
  162. rfkill_schedule_global_op(rfkill_master_switch_op);
  163. else
  164. rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
  165. }
  166. static void rfkill_event(struct input_handle *handle, unsigned int type,
  167. unsigned int code, int data)
  168. {
  169. if (type == EV_KEY && data == 1) {
  170. switch (code) {
  171. case KEY_WLAN:
  172. rfkill_schedule_toggle(RFKILL_TYPE_WLAN);
  173. break;
  174. case KEY_BLUETOOTH:
  175. rfkill_schedule_toggle(RFKILL_TYPE_BLUETOOTH);
  176. break;
  177. case KEY_UWB:
  178. rfkill_schedule_toggle(RFKILL_TYPE_UWB);
  179. break;
  180. case KEY_WIMAX:
  181. rfkill_schedule_toggle(RFKILL_TYPE_WIMAX);
  182. break;
  183. }
  184. } else if (type == EV_SW && code == SW_RFKILL_ALL)
  185. rfkill_schedule_evsw_rfkillall(data);
  186. }
  187. static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
  188. const struct input_device_id *id)
  189. {
  190. struct input_handle *handle;
  191. int error;
  192. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  193. if (!handle)
  194. return -ENOMEM;
  195. handle->dev = dev;
  196. handle->handler = handler;
  197. handle->name = "rfkill";
  198. /* causes rfkill_start() to be called */
  199. error = input_register_handle(handle);
  200. if (error)
  201. goto err_free_handle;
  202. error = input_open_device(handle);
  203. if (error)
  204. goto err_unregister_handle;
  205. return 0;
  206. err_unregister_handle:
  207. input_unregister_handle(handle);
  208. err_free_handle:
  209. kfree(handle);
  210. return error;
  211. }
  212. static void rfkill_start(struct input_handle *handle)
  213. {
  214. /*
  215. * Take event_lock to guard against configuration changes, we
  216. * should be able to deal with concurrency with rfkill_event()
  217. * just fine (which event_lock will also avoid).
  218. */
  219. spin_lock_irq(&handle->dev->event_lock);
  220. if (test_bit(EV_SW, handle->dev->evbit) &&
  221. test_bit(SW_RFKILL_ALL, handle->dev->swbit))
  222. rfkill_schedule_evsw_rfkillall(test_bit(SW_RFKILL_ALL,
  223. handle->dev->sw));
  224. spin_unlock_irq(&handle->dev->event_lock);
  225. }
  226. static void rfkill_disconnect(struct input_handle *handle)
  227. {
  228. input_close_device(handle);
  229. input_unregister_handle(handle);
  230. kfree(handle);
  231. }
  232. static const struct input_device_id rfkill_ids[] = {
  233. {
  234. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  235. .evbit = { BIT_MASK(EV_KEY) },
  236. .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
  237. },
  238. {
  239. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  240. .evbit = { BIT_MASK(EV_KEY) },
  241. .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
  242. },
  243. {
  244. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  245. .evbit = { BIT_MASK(EV_KEY) },
  246. .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
  247. },
  248. {
  249. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  250. .evbit = { BIT_MASK(EV_KEY) },
  251. .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
  252. },
  253. {
  254. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
  255. .evbit = { BIT(EV_SW) },
  256. .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
  257. },
  258. { }
  259. };
  260. static struct input_handler rfkill_handler = {
  261. .name = "rfkill",
  262. .event = rfkill_event,
  263. .connect = rfkill_connect,
  264. .start = rfkill_start,
  265. .disconnect = rfkill_disconnect,
  266. .id_table = rfkill_ids,
  267. };
  268. int __init rfkill_handler_init(void)
  269. {
  270. switch (rfkill_master_switch_mode) {
  271. case RFKILL_INPUT_MASTER_UNBLOCKALL:
  272. rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNBLOCK;
  273. break;
  274. case RFKILL_INPUT_MASTER_RESTORE:
  275. rfkill_master_switch_op = RFKILL_GLOBAL_OP_RESTORE;
  276. break;
  277. case RFKILL_INPUT_MASTER_UNLOCK:
  278. rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNLOCK;
  279. break;
  280. default:
  281. return -EINVAL;
  282. }
  283. spin_lock_init(&rfkill_op_lock);
  284. /* Avoid delay at first schedule */
  285. rfkill_last_scheduled =
  286. jiffies - msecs_to_jiffies(RFKILL_OPS_DELAY) - 1;
  287. return input_register_handler(&rfkill_handler);
  288. }
  289. void __exit rfkill_handler_exit(void)
  290. {
  291. input_unregister_handler(&rfkill_handler);
  292. cancel_delayed_work_sync(&rfkill_op_work);
  293. }