input.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. case KEY_RFKILL:
  184. rfkill_schedule_toggle(RFKILL_TYPE_ALL);
  185. break;
  186. }
  187. } else if (type == EV_SW && code == SW_RFKILL_ALL)
  188. rfkill_schedule_evsw_rfkillall(data);
  189. }
  190. static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
  191. const struct input_device_id *id)
  192. {
  193. struct input_handle *handle;
  194. int error;
  195. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  196. if (!handle)
  197. return -ENOMEM;
  198. handle->dev = dev;
  199. handle->handler = handler;
  200. handle->name = "rfkill";
  201. /* causes rfkill_start() to be called */
  202. error = input_register_handle(handle);
  203. if (error)
  204. goto err_free_handle;
  205. error = input_open_device(handle);
  206. if (error)
  207. goto err_unregister_handle;
  208. return 0;
  209. err_unregister_handle:
  210. input_unregister_handle(handle);
  211. err_free_handle:
  212. kfree(handle);
  213. return error;
  214. }
  215. static void rfkill_start(struct input_handle *handle)
  216. {
  217. /*
  218. * Take event_lock to guard against configuration changes, we
  219. * should be able to deal with concurrency with rfkill_event()
  220. * just fine (which event_lock will also avoid).
  221. */
  222. spin_lock_irq(&handle->dev->event_lock);
  223. if (test_bit(EV_SW, handle->dev->evbit) &&
  224. test_bit(SW_RFKILL_ALL, handle->dev->swbit))
  225. rfkill_schedule_evsw_rfkillall(test_bit(SW_RFKILL_ALL,
  226. handle->dev->sw));
  227. spin_unlock_irq(&handle->dev->event_lock);
  228. }
  229. static void rfkill_disconnect(struct input_handle *handle)
  230. {
  231. input_close_device(handle);
  232. input_unregister_handle(handle);
  233. kfree(handle);
  234. }
  235. static const struct input_device_id rfkill_ids[] = {
  236. {
  237. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  238. .evbit = { BIT_MASK(EV_KEY) },
  239. .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
  240. },
  241. {
  242. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  243. .evbit = { BIT_MASK(EV_KEY) },
  244. .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
  245. },
  246. {
  247. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  248. .evbit = { BIT_MASK(EV_KEY) },
  249. .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
  250. },
  251. {
  252. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  253. .evbit = { BIT_MASK(EV_KEY) },
  254. .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
  255. },
  256. {
  257. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  258. .evbit = { BIT_MASK(EV_KEY) },
  259. .keybit = { [BIT_WORD(KEY_RFKILL)] = BIT_MASK(KEY_RFKILL) },
  260. },
  261. {
  262. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
  263. .evbit = { BIT(EV_SW) },
  264. .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
  265. },
  266. { }
  267. };
  268. static struct input_handler rfkill_handler = {
  269. .name = "rfkill",
  270. .event = rfkill_event,
  271. .connect = rfkill_connect,
  272. .start = rfkill_start,
  273. .disconnect = rfkill_disconnect,
  274. .id_table = rfkill_ids,
  275. };
  276. int __init rfkill_handler_init(void)
  277. {
  278. switch (rfkill_master_switch_mode) {
  279. case RFKILL_INPUT_MASTER_UNBLOCKALL:
  280. rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNBLOCK;
  281. break;
  282. case RFKILL_INPUT_MASTER_RESTORE:
  283. rfkill_master_switch_op = RFKILL_GLOBAL_OP_RESTORE;
  284. break;
  285. case RFKILL_INPUT_MASTER_UNLOCK:
  286. rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNLOCK;
  287. break;
  288. default:
  289. return -EINVAL;
  290. }
  291. spin_lock_init(&rfkill_op_lock);
  292. /* Avoid delay at first schedule */
  293. rfkill_last_scheduled =
  294. jiffies - msecs_to_jiffies(RFKILL_OPS_DELAY) - 1;
  295. return input_register_handler(&rfkill_handler);
  296. }
  297. void __exit rfkill_handler_exit(void)
  298. {
  299. input_unregister_handler(&rfkill_handler);
  300. cancel_delayed_work_sync(&rfkill_op_work);
  301. }