input.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 (delayed_work_pending(&rfkill_op_work))
  127. return;
  128. schedule_delayed_work(&rfkill_op_work,
  129. rfkill_ratelimit(rfkill_last_scheduled));
  130. rfkill_last_scheduled = jiffies;
  131. }
  132. static void rfkill_schedule_global_op(enum rfkill_sched_op op)
  133. {
  134. unsigned long flags;
  135. spin_lock_irqsave(&rfkill_op_lock, flags);
  136. rfkill_op = op;
  137. rfkill_op_pending = true;
  138. if (op == RFKILL_GLOBAL_OP_EPO && !rfkill_is_epo_lock_active()) {
  139. /* bypass the limiter for EPO */
  140. cancel_delayed_work(&rfkill_op_work);
  141. schedule_delayed_work(&rfkill_op_work, 0);
  142. rfkill_last_scheduled = jiffies;
  143. } else
  144. rfkill_schedule_ratelimited();
  145. spin_unlock_irqrestore(&rfkill_op_lock, flags);
  146. }
  147. static void rfkill_schedule_toggle(enum rfkill_type type)
  148. {
  149. unsigned long flags;
  150. if (rfkill_is_epo_lock_active())
  151. return;
  152. spin_lock_irqsave(&rfkill_op_lock, flags);
  153. if (!rfkill_op_pending) {
  154. __set_bit(type, rfkill_sw_pending);
  155. __change_bit(type, rfkill_sw_state);
  156. rfkill_schedule_ratelimited();
  157. }
  158. spin_unlock_irqrestore(&rfkill_op_lock, flags);
  159. }
  160. static void rfkill_schedule_evsw_rfkillall(int state)
  161. {
  162. if (state)
  163. rfkill_schedule_global_op(rfkill_master_switch_op);
  164. else
  165. rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
  166. }
  167. static void rfkill_event(struct input_handle *handle, unsigned int type,
  168. unsigned int code, int data)
  169. {
  170. if (type == EV_KEY && data == 1) {
  171. switch (code) {
  172. case KEY_WLAN:
  173. rfkill_schedule_toggle(RFKILL_TYPE_WLAN);
  174. break;
  175. case KEY_BLUETOOTH:
  176. rfkill_schedule_toggle(RFKILL_TYPE_BLUETOOTH);
  177. break;
  178. case KEY_UWB:
  179. rfkill_schedule_toggle(RFKILL_TYPE_UWB);
  180. break;
  181. case KEY_WIMAX:
  182. rfkill_schedule_toggle(RFKILL_TYPE_WIMAX);
  183. break;
  184. case KEY_RFKILL:
  185. rfkill_schedule_toggle(RFKILL_TYPE_ALL);
  186. break;
  187. }
  188. } else if (type == EV_SW && code == SW_RFKILL_ALL)
  189. rfkill_schedule_evsw_rfkillall(data);
  190. }
  191. static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
  192. const struct input_device_id *id)
  193. {
  194. struct input_handle *handle;
  195. int error;
  196. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  197. if (!handle)
  198. return -ENOMEM;
  199. handle->dev = dev;
  200. handle->handler = handler;
  201. handle->name = "rfkill";
  202. /* causes rfkill_start() to be called */
  203. error = input_register_handle(handle);
  204. if (error)
  205. goto err_free_handle;
  206. error = input_open_device(handle);
  207. if (error)
  208. goto err_unregister_handle;
  209. return 0;
  210. err_unregister_handle:
  211. input_unregister_handle(handle);
  212. err_free_handle:
  213. kfree(handle);
  214. return error;
  215. }
  216. static void rfkill_start(struct input_handle *handle)
  217. {
  218. /*
  219. * Take event_lock to guard against configuration changes, we
  220. * should be able to deal with concurrency with rfkill_event()
  221. * just fine (which event_lock will also avoid).
  222. */
  223. spin_lock_irq(&handle->dev->event_lock);
  224. if (test_bit(EV_SW, handle->dev->evbit) &&
  225. test_bit(SW_RFKILL_ALL, handle->dev->swbit))
  226. rfkill_schedule_evsw_rfkillall(test_bit(SW_RFKILL_ALL,
  227. handle->dev->sw));
  228. spin_unlock_irq(&handle->dev->event_lock);
  229. }
  230. static void rfkill_disconnect(struct input_handle *handle)
  231. {
  232. input_close_device(handle);
  233. input_unregister_handle(handle);
  234. kfree(handle);
  235. }
  236. static const struct input_device_id rfkill_ids[] = {
  237. {
  238. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  239. .evbit = { BIT_MASK(EV_KEY) },
  240. .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
  241. },
  242. {
  243. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  244. .evbit = { BIT_MASK(EV_KEY) },
  245. .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
  246. },
  247. {
  248. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  249. .evbit = { BIT_MASK(EV_KEY) },
  250. .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
  251. },
  252. {
  253. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  254. .evbit = { BIT_MASK(EV_KEY) },
  255. .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
  256. },
  257. {
  258. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  259. .evbit = { BIT_MASK(EV_KEY) },
  260. .keybit = { [BIT_WORD(KEY_RFKILL)] = BIT_MASK(KEY_RFKILL) },
  261. },
  262. {
  263. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
  264. .evbit = { BIT(EV_SW) },
  265. .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
  266. },
  267. { }
  268. };
  269. static struct input_handler rfkill_handler = {
  270. .name = "rfkill",
  271. .event = rfkill_event,
  272. .connect = rfkill_connect,
  273. .start = rfkill_start,
  274. .disconnect = rfkill_disconnect,
  275. .id_table = rfkill_ids,
  276. };
  277. int __init rfkill_handler_init(void)
  278. {
  279. switch (rfkill_master_switch_mode) {
  280. case RFKILL_INPUT_MASTER_UNBLOCKALL:
  281. rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNBLOCK;
  282. break;
  283. case RFKILL_INPUT_MASTER_RESTORE:
  284. rfkill_master_switch_op = RFKILL_GLOBAL_OP_RESTORE;
  285. break;
  286. case RFKILL_INPUT_MASTER_UNLOCK:
  287. rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNLOCK;
  288. break;
  289. default:
  290. return -EINVAL;
  291. }
  292. spin_lock_init(&rfkill_op_lock);
  293. /* Avoid delay at first schedule */
  294. rfkill_last_scheduled =
  295. jiffies - msecs_to_jiffies(RFKILL_OPS_DELAY) - 1;
  296. return input_register_handler(&rfkill_handler);
  297. }
  298. void __exit rfkill_handler_exit(void)
  299. {
  300. input_unregister_handler(&rfkill_handler);
  301. cancel_delayed_work_sync(&rfkill_op_work);
  302. }