input.c 8.8 KB

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