core.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /*
  2. * Copyright (C) 2006 - 2007 Ivo van Doorn
  3. * Copyright (C) 2007 Dmitry Torokhov
  4. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the
  18. * Free Software Foundation, Inc.,
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/capability.h>
  26. #include <linux/list.h>
  27. #include <linux/mutex.h>
  28. #include <linux/rfkill.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/wait.h>
  32. #include <linux/poll.h>
  33. #include <linux/fs.h>
  34. #include "rfkill.h"
  35. #define POLL_INTERVAL (5 * HZ)
  36. #define RFKILL_BLOCK_HW BIT(0)
  37. #define RFKILL_BLOCK_SW BIT(1)
  38. #define RFKILL_BLOCK_SW_PREV BIT(2)
  39. #define RFKILL_BLOCK_ANY (RFKILL_BLOCK_HW |\
  40. RFKILL_BLOCK_SW |\
  41. RFKILL_BLOCK_SW_PREV)
  42. #define RFKILL_BLOCK_SW_SETCALL BIT(31)
  43. struct rfkill {
  44. spinlock_t lock;
  45. const char *name;
  46. enum rfkill_type type;
  47. unsigned long state;
  48. u32 idx;
  49. bool registered;
  50. bool persistent;
  51. const struct rfkill_ops *ops;
  52. void *data;
  53. #ifdef CONFIG_RFKILL_LEDS
  54. struct led_trigger led_trigger;
  55. const char *ledtrigname;
  56. #endif
  57. struct device dev;
  58. struct list_head node;
  59. struct delayed_work poll_work;
  60. struct work_struct uevent_work;
  61. struct work_struct sync_work;
  62. };
  63. #define to_rfkill(d) container_of(d, struct rfkill, dev)
  64. struct rfkill_int_event {
  65. struct list_head list;
  66. struct rfkill_event ev;
  67. };
  68. struct rfkill_data {
  69. struct list_head list;
  70. struct list_head events;
  71. struct mutex mtx;
  72. wait_queue_head_t read_wait;
  73. bool input_handler;
  74. };
  75. MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
  76. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  77. MODULE_DESCRIPTION("RF switch support");
  78. MODULE_LICENSE("GPL");
  79. /*
  80. * The locking here should be made much smarter, we currently have
  81. * a bit of a stupid situation because drivers might want to register
  82. * the rfkill struct under their own lock, and take this lock during
  83. * rfkill method calls -- which will cause an AB-BA deadlock situation.
  84. *
  85. * To fix that, we need to rework this code here to be mostly lock-free
  86. * and only use the mutex for list manipulations, not to protect the
  87. * various other global variables. Then we can avoid holding the mutex
  88. * around driver operations, and all is happy.
  89. */
  90. static LIST_HEAD(rfkill_list); /* list of registered rf switches */
  91. static DEFINE_MUTEX(rfkill_global_mutex);
  92. static LIST_HEAD(rfkill_fds); /* list of open fds of /dev/rfkill */
  93. static unsigned int rfkill_default_state = 1;
  94. module_param_named(default_state, rfkill_default_state, uint, 0444);
  95. MODULE_PARM_DESC(default_state,
  96. "Default initial state for all radio types, 0 = radio off");
  97. static struct {
  98. bool cur, sav;
  99. } rfkill_global_states[NUM_RFKILL_TYPES];
  100. static bool rfkill_epo_lock_active;
  101. #ifdef CONFIG_RFKILL_LEDS
  102. static void rfkill_led_trigger_event(struct rfkill *rfkill)
  103. {
  104. struct led_trigger *trigger;
  105. if (!rfkill->registered)
  106. return;
  107. trigger = &rfkill->led_trigger;
  108. if (rfkill->state & RFKILL_BLOCK_ANY)
  109. led_trigger_event(trigger, LED_OFF);
  110. else
  111. led_trigger_event(trigger, LED_FULL);
  112. }
  113. static void rfkill_led_trigger_activate(struct led_classdev *led)
  114. {
  115. struct rfkill *rfkill;
  116. rfkill = container_of(led->trigger, struct rfkill, led_trigger);
  117. rfkill_led_trigger_event(rfkill);
  118. }
  119. const char *rfkill_get_led_trigger_name(struct rfkill *rfkill)
  120. {
  121. return rfkill->led_trigger.name;
  122. }
  123. EXPORT_SYMBOL(rfkill_get_led_trigger_name);
  124. void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name)
  125. {
  126. BUG_ON(!rfkill);
  127. rfkill->ledtrigname = name;
  128. }
  129. EXPORT_SYMBOL(rfkill_set_led_trigger_name);
  130. static int rfkill_led_trigger_register(struct rfkill *rfkill)
  131. {
  132. rfkill->led_trigger.name = rfkill->ledtrigname
  133. ? : dev_name(&rfkill->dev);
  134. rfkill->led_trigger.activate = rfkill_led_trigger_activate;
  135. return led_trigger_register(&rfkill->led_trigger);
  136. }
  137. static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
  138. {
  139. led_trigger_unregister(&rfkill->led_trigger);
  140. }
  141. #else
  142. static void rfkill_led_trigger_event(struct rfkill *rfkill)
  143. {
  144. }
  145. static inline int rfkill_led_trigger_register(struct rfkill *rfkill)
  146. {
  147. return 0;
  148. }
  149. static inline void rfkill_led_trigger_unregister(struct rfkill *rfkill)
  150. {
  151. }
  152. #endif /* CONFIG_RFKILL_LEDS */
  153. static void rfkill_fill_event(struct rfkill_event *ev, struct rfkill *rfkill,
  154. enum rfkill_operation op)
  155. {
  156. unsigned long flags;
  157. ev->idx = rfkill->idx;
  158. ev->type = rfkill->type;
  159. ev->op = op;
  160. spin_lock_irqsave(&rfkill->lock, flags);
  161. ev->hard = !!(rfkill->state & RFKILL_BLOCK_HW);
  162. ev->soft = !!(rfkill->state & (RFKILL_BLOCK_SW |
  163. RFKILL_BLOCK_SW_PREV));
  164. spin_unlock_irqrestore(&rfkill->lock, flags);
  165. }
  166. static void rfkill_send_events(struct rfkill *rfkill, enum rfkill_operation op)
  167. {
  168. struct rfkill_data *data;
  169. struct rfkill_int_event *ev;
  170. list_for_each_entry(data, &rfkill_fds, list) {
  171. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  172. if (!ev)
  173. continue;
  174. rfkill_fill_event(&ev->ev, rfkill, op);
  175. mutex_lock(&data->mtx);
  176. list_add_tail(&ev->list, &data->events);
  177. mutex_unlock(&data->mtx);
  178. wake_up_interruptible(&data->read_wait);
  179. }
  180. }
  181. static void rfkill_event(struct rfkill *rfkill)
  182. {
  183. if (!rfkill->registered)
  184. return;
  185. kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
  186. /* also send event to /dev/rfkill */
  187. rfkill_send_events(rfkill, RFKILL_OP_CHANGE);
  188. }
  189. static bool __rfkill_set_hw_state(struct rfkill *rfkill,
  190. bool blocked, bool *change)
  191. {
  192. unsigned long flags;
  193. bool prev, any;
  194. BUG_ON(!rfkill);
  195. spin_lock_irqsave(&rfkill->lock, flags);
  196. prev = !!(rfkill->state & RFKILL_BLOCK_HW);
  197. if (blocked)
  198. rfkill->state |= RFKILL_BLOCK_HW;
  199. else
  200. rfkill->state &= ~RFKILL_BLOCK_HW;
  201. *change = prev != blocked;
  202. any = rfkill->state & RFKILL_BLOCK_ANY;
  203. spin_unlock_irqrestore(&rfkill->lock, flags);
  204. rfkill_led_trigger_event(rfkill);
  205. return any;
  206. }
  207. /**
  208. * rfkill_set_block - wrapper for set_block method
  209. *
  210. * @rfkill: the rfkill struct to use
  211. * @blocked: the new software state
  212. *
  213. * Calls the set_block method (when applicable) and handles notifications
  214. * etc. as well.
  215. */
  216. static void rfkill_set_block(struct rfkill *rfkill, bool blocked)
  217. {
  218. unsigned long flags;
  219. int err;
  220. if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
  221. return;
  222. /*
  223. * Some platforms (...!) generate input events which affect the
  224. * _hard_ kill state -- whenever something tries to change the
  225. * current software state query the hardware state too.
  226. */
  227. if (rfkill->ops->query)
  228. rfkill->ops->query(rfkill, rfkill->data);
  229. spin_lock_irqsave(&rfkill->lock, flags);
  230. if (rfkill->state & RFKILL_BLOCK_SW)
  231. rfkill->state |= RFKILL_BLOCK_SW_PREV;
  232. else
  233. rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
  234. if (blocked)
  235. rfkill->state |= RFKILL_BLOCK_SW;
  236. else
  237. rfkill->state &= ~RFKILL_BLOCK_SW;
  238. rfkill->state |= RFKILL_BLOCK_SW_SETCALL;
  239. spin_unlock_irqrestore(&rfkill->lock, flags);
  240. err = rfkill->ops->set_block(rfkill->data, blocked);
  241. spin_lock_irqsave(&rfkill->lock, flags);
  242. if (err) {
  243. /*
  244. * Failed -- reset status to _prev, this may be different
  245. * from what set set _PREV to earlier in this function
  246. * if rfkill_set_sw_state was invoked.
  247. */
  248. if (rfkill->state & RFKILL_BLOCK_SW_PREV)
  249. rfkill->state |= RFKILL_BLOCK_SW;
  250. else
  251. rfkill->state &= ~RFKILL_BLOCK_SW;
  252. }
  253. rfkill->state &= ~RFKILL_BLOCK_SW_SETCALL;
  254. rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
  255. spin_unlock_irqrestore(&rfkill->lock, flags);
  256. rfkill_led_trigger_event(rfkill);
  257. rfkill_event(rfkill);
  258. }
  259. #ifdef CONFIG_RFKILL_INPUT
  260. static atomic_t rfkill_input_disabled = ATOMIC_INIT(0);
  261. /**
  262. * __rfkill_switch_all - Toggle state of all switches of given type
  263. * @type: type of interfaces to be affected
  264. * @state: the new state
  265. *
  266. * This function sets the state of all switches of given type,
  267. * unless a specific switch is claimed by userspace (in which case,
  268. * that switch is left alone) or suspended.
  269. *
  270. * Caller must have acquired rfkill_global_mutex.
  271. */
  272. static void __rfkill_switch_all(const enum rfkill_type type, bool blocked)
  273. {
  274. struct rfkill *rfkill;
  275. rfkill_global_states[type].cur = blocked;
  276. list_for_each_entry(rfkill, &rfkill_list, node) {
  277. if (rfkill->type != type)
  278. continue;
  279. rfkill_set_block(rfkill, blocked);
  280. }
  281. }
  282. /**
  283. * rfkill_switch_all - Toggle state of all switches of given type
  284. * @type: type of interfaces to be affected
  285. * @state: the new state
  286. *
  287. * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
  288. * Please refer to __rfkill_switch_all() for details.
  289. *
  290. * Does nothing if the EPO lock is active.
  291. */
  292. void rfkill_switch_all(enum rfkill_type type, bool blocked)
  293. {
  294. if (atomic_read(&rfkill_input_disabled))
  295. return;
  296. mutex_lock(&rfkill_global_mutex);
  297. if (!rfkill_epo_lock_active)
  298. __rfkill_switch_all(type, blocked);
  299. mutex_unlock(&rfkill_global_mutex);
  300. }
  301. /**
  302. * rfkill_epo - emergency power off all transmitters
  303. *
  304. * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
  305. * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
  306. *
  307. * The global state before the EPO is saved and can be restored later
  308. * using rfkill_restore_states().
  309. */
  310. void rfkill_epo(void)
  311. {
  312. struct rfkill *rfkill;
  313. int i;
  314. if (atomic_read(&rfkill_input_disabled))
  315. return;
  316. mutex_lock(&rfkill_global_mutex);
  317. rfkill_epo_lock_active = true;
  318. list_for_each_entry(rfkill, &rfkill_list, node)
  319. rfkill_set_block(rfkill, true);
  320. for (i = 0; i < NUM_RFKILL_TYPES; i++) {
  321. rfkill_global_states[i].sav = rfkill_global_states[i].cur;
  322. rfkill_global_states[i].cur = true;
  323. }
  324. mutex_unlock(&rfkill_global_mutex);
  325. }
  326. /**
  327. * rfkill_restore_states - restore global states
  328. *
  329. * Restore (and sync switches to) the global state from the
  330. * states in rfkill_default_states. This can undo the effects of
  331. * a call to rfkill_epo().
  332. */
  333. void rfkill_restore_states(void)
  334. {
  335. int i;
  336. if (atomic_read(&rfkill_input_disabled))
  337. return;
  338. mutex_lock(&rfkill_global_mutex);
  339. rfkill_epo_lock_active = false;
  340. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  341. __rfkill_switch_all(i, rfkill_global_states[i].sav);
  342. mutex_unlock(&rfkill_global_mutex);
  343. }
  344. /**
  345. * rfkill_remove_epo_lock - unlock state changes
  346. *
  347. * Used by rfkill-input manually unlock state changes, when
  348. * the EPO switch is deactivated.
  349. */
  350. void rfkill_remove_epo_lock(void)
  351. {
  352. if (atomic_read(&rfkill_input_disabled))
  353. return;
  354. mutex_lock(&rfkill_global_mutex);
  355. rfkill_epo_lock_active = false;
  356. mutex_unlock(&rfkill_global_mutex);
  357. }
  358. /**
  359. * rfkill_is_epo_lock_active - returns true EPO is active
  360. *
  361. * Returns 0 (false) if there is NOT an active EPO contidion,
  362. * and 1 (true) if there is an active EPO contition, which
  363. * locks all radios in one of the BLOCKED states.
  364. *
  365. * Can be called in atomic context.
  366. */
  367. bool rfkill_is_epo_lock_active(void)
  368. {
  369. return rfkill_epo_lock_active;
  370. }
  371. /**
  372. * rfkill_get_global_sw_state - returns global state for a type
  373. * @type: the type to get the global state of
  374. *
  375. * Returns the current global state for a given wireless
  376. * device type.
  377. */
  378. bool rfkill_get_global_sw_state(const enum rfkill_type type)
  379. {
  380. return rfkill_global_states[type].cur;
  381. }
  382. #endif
  383. bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  384. {
  385. bool ret, change;
  386. ret = __rfkill_set_hw_state(rfkill, blocked, &change);
  387. if (!rfkill->registered)
  388. return ret;
  389. if (change)
  390. schedule_work(&rfkill->uevent_work);
  391. return ret;
  392. }
  393. EXPORT_SYMBOL(rfkill_set_hw_state);
  394. static void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  395. {
  396. u32 bit = RFKILL_BLOCK_SW;
  397. /* if in a ops->set_block right now, use other bit */
  398. if (rfkill->state & RFKILL_BLOCK_SW_SETCALL)
  399. bit = RFKILL_BLOCK_SW_PREV;
  400. if (blocked)
  401. rfkill->state |= bit;
  402. else
  403. rfkill->state &= ~bit;
  404. }
  405. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  406. {
  407. unsigned long flags;
  408. bool prev, hwblock;
  409. BUG_ON(!rfkill);
  410. spin_lock_irqsave(&rfkill->lock, flags);
  411. prev = !!(rfkill->state & RFKILL_BLOCK_SW);
  412. __rfkill_set_sw_state(rfkill, blocked);
  413. hwblock = !!(rfkill->state & RFKILL_BLOCK_HW);
  414. blocked = blocked || hwblock;
  415. spin_unlock_irqrestore(&rfkill->lock, flags);
  416. if (!rfkill->registered)
  417. return blocked;
  418. if (prev != blocked && !hwblock)
  419. schedule_work(&rfkill->uevent_work);
  420. rfkill_led_trigger_event(rfkill);
  421. return blocked;
  422. }
  423. EXPORT_SYMBOL(rfkill_set_sw_state);
  424. void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
  425. {
  426. unsigned long flags;
  427. BUG_ON(!rfkill);
  428. BUG_ON(rfkill->registered);
  429. spin_lock_irqsave(&rfkill->lock, flags);
  430. __rfkill_set_sw_state(rfkill, blocked);
  431. rfkill->persistent = true;
  432. spin_unlock_irqrestore(&rfkill->lock, flags);
  433. }
  434. EXPORT_SYMBOL(rfkill_init_sw_state);
  435. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  436. {
  437. unsigned long flags;
  438. bool swprev, hwprev;
  439. BUG_ON(!rfkill);
  440. spin_lock_irqsave(&rfkill->lock, flags);
  441. /*
  442. * No need to care about prev/setblock ... this is for uevent only
  443. * and that will get triggered by rfkill_set_block anyway.
  444. */
  445. swprev = !!(rfkill->state & RFKILL_BLOCK_SW);
  446. hwprev = !!(rfkill->state & RFKILL_BLOCK_HW);
  447. __rfkill_set_sw_state(rfkill, sw);
  448. if (hw)
  449. rfkill->state |= RFKILL_BLOCK_HW;
  450. else
  451. rfkill->state &= ~RFKILL_BLOCK_HW;
  452. spin_unlock_irqrestore(&rfkill->lock, flags);
  453. if (!rfkill->registered) {
  454. rfkill->persistent = true;
  455. } else {
  456. if (swprev != sw || hwprev != hw)
  457. schedule_work(&rfkill->uevent_work);
  458. rfkill_led_trigger_event(rfkill);
  459. }
  460. }
  461. EXPORT_SYMBOL(rfkill_set_states);
  462. static ssize_t rfkill_name_show(struct device *dev,
  463. struct device_attribute *attr,
  464. char *buf)
  465. {
  466. struct rfkill *rfkill = to_rfkill(dev);
  467. return sprintf(buf, "%s\n", rfkill->name);
  468. }
  469. static const char *rfkill_get_type_str(enum rfkill_type type)
  470. {
  471. switch (type) {
  472. case RFKILL_TYPE_WLAN:
  473. return "wlan";
  474. case RFKILL_TYPE_BLUETOOTH:
  475. return "bluetooth";
  476. case RFKILL_TYPE_UWB:
  477. return "ultrawideband";
  478. case RFKILL_TYPE_WIMAX:
  479. return "wimax";
  480. case RFKILL_TYPE_WWAN:
  481. return "wwan";
  482. default:
  483. BUG();
  484. }
  485. BUILD_BUG_ON(NUM_RFKILL_TYPES != RFKILL_TYPE_WWAN + 1);
  486. }
  487. static ssize_t rfkill_type_show(struct device *dev,
  488. struct device_attribute *attr,
  489. char *buf)
  490. {
  491. struct rfkill *rfkill = to_rfkill(dev);
  492. return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
  493. }
  494. static ssize_t rfkill_idx_show(struct device *dev,
  495. struct device_attribute *attr,
  496. char *buf)
  497. {
  498. struct rfkill *rfkill = to_rfkill(dev);
  499. return sprintf(buf, "%d\n", rfkill->idx);
  500. }
  501. static ssize_t rfkill_persistent_show(struct device *dev,
  502. struct device_attribute *attr,
  503. char *buf)
  504. {
  505. struct rfkill *rfkill = to_rfkill(dev);
  506. return sprintf(buf, "%d\n", rfkill->persistent);
  507. }
  508. static u8 user_state_from_blocked(unsigned long state)
  509. {
  510. if (state & RFKILL_BLOCK_HW)
  511. return RFKILL_USER_STATE_HARD_BLOCKED;
  512. if (state & RFKILL_BLOCK_SW)
  513. return RFKILL_USER_STATE_SOFT_BLOCKED;
  514. return RFKILL_USER_STATE_UNBLOCKED;
  515. }
  516. static ssize_t rfkill_state_show(struct device *dev,
  517. struct device_attribute *attr,
  518. char *buf)
  519. {
  520. struct rfkill *rfkill = to_rfkill(dev);
  521. unsigned long flags;
  522. u32 state;
  523. spin_lock_irqsave(&rfkill->lock, flags);
  524. state = rfkill->state;
  525. spin_unlock_irqrestore(&rfkill->lock, flags);
  526. return sprintf(buf, "%d\n", user_state_from_blocked(state));
  527. }
  528. static ssize_t rfkill_state_store(struct device *dev,
  529. struct device_attribute *attr,
  530. const char *buf, size_t count)
  531. {
  532. struct rfkill *rfkill = to_rfkill(dev);
  533. unsigned long state;
  534. int err;
  535. if (!capable(CAP_NET_ADMIN))
  536. return -EPERM;
  537. err = strict_strtoul(buf, 0, &state);
  538. if (err)
  539. return err;
  540. if (state != RFKILL_USER_STATE_SOFT_BLOCKED &&
  541. state != RFKILL_USER_STATE_UNBLOCKED)
  542. return -EINVAL;
  543. mutex_lock(&rfkill_global_mutex);
  544. rfkill_set_block(rfkill, state == RFKILL_USER_STATE_SOFT_BLOCKED);
  545. mutex_unlock(&rfkill_global_mutex);
  546. return err ?: count;
  547. }
  548. static ssize_t rfkill_claim_show(struct device *dev,
  549. struct device_attribute *attr,
  550. char *buf)
  551. {
  552. return sprintf(buf, "%d\n", 0);
  553. }
  554. static ssize_t rfkill_claim_store(struct device *dev,
  555. struct device_attribute *attr,
  556. const char *buf, size_t count)
  557. {
  558. return -EOPNOTSUPP;
  559. }
  560. static struct device_attribute rfkill_dev_attrs[] = {
  561. __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
  562. __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
  563. __ATTR(index, S_IRUGO, rfkill_idx_show, NULL),
  564. __ATTR(persistent, S_IRUGO, rfkill_persistent_show, NULL),
  565. __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
  566. __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
  567. __ATTR_NULL
  568. };
  569. static void rfkill_release(struct device *dev)
  570. {
  571. struct rfkill *rfkill = to_rfkill(dev);
  572. kfree(rfkill);
  573. }
  574. static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  575. {
  576. struct rfkill *rfkill = to_rfkill(dev);
  577. unsigned long flags;
  578. u32 state;
  579. int error;
  580. error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
  581. if (error)
  582. return error;
  583. error = add_uevent_var(env, "RFKILL_TYPE=%s",
  584. rfkill_get_type_str(rfkill->type));
  585. if (error)
  586. return error;
  587. spin_lock_irqsave(&rfkill->lock, flags);
  588. state = rfkill->state;
  589. spin_unlock_irqrestore(&rfkill->lock, flags);
  590. error = add_uevent_var(env, "RFKILL_STATE=%d",
  591. user_state_from_blocked(state));
  592. return error;
  593. }
  594. void rfkill_pause_polling(struct rfkill *rfkill)
  595. {
  596. BUG_ON(!rfkill);
  597. if (!rfkill->ops->poll)
  598. return;
  599. cancel_delayed_work_sync(&rfkill->poll_work);
  600. }
  601. EXPORT_SYMBOL(rfkill_pause_polling);
  602. void rfkill_resume_polling(struct rfkill *rfkill)
  603. {
  604. BUG_ON(!rfkill);
  605. if (!rfkill->ops->poll)
  606. return;
  607. schedule_work(&rfkill->poll_work.work);
  608. }
  609. EXPORT_SYMBOL(rfkill_resume_polling);
  610. static int rfkill_suspend(struct device *dev, pm_message_t state)
  611. {
  612. struct rfkill *rfkill = to_rfkill(dev);
  613. rfkill_pause_polling(rfkill);
  614. return 0;
  615. }
  616. static int rfkill_resume(struct device *dev)
  617. {
  618. struct rfkill *rfkill = to_rfkill(dev);
  619. bool cur;
  620. if (!rfkill->persistent) {
  621. cur = !!(rfkill->state & RFKILL_BLOCK_SW);
  622. rfkill_set_block(rfkill, cur);
  623. }
  624. rfkill_resume_polling(rfkill);
  625. return 0;
  626. }
  627. static struct class rfkill_class = {
  628. .name = "rfkill",
  629. .dev_release = rfkill_release,
  630. .dev_attrs = rfkill_dev_attrs,
  631. .dev_uevent = rfkill_dev_uevent,
  632. .suspend = rfkill_suspend,
  633. .resume = rfkill_resume,
  634. };
  635. bool rfkill_blocked(struct rfkill *rfkill)
  636. {
  637. unsigned long flags;
  638. u32 state;
  639. spin_lock_irqsave(&rfkill->lock, flags);
  640. state = rfkill->state;
  641. spin_unlock_irqrestore(&rfkill->lock, flags);
  642. return !!(state & RFKILL_BLOCK_ANY);
  643. }
  644. EXPORT_SYMBOL(rfkill_blocked);
  645. struct rfkill * __must_check rfkill_alloc(const char *name,
  646. struct device *parent,
  647. const enum rfkill_type type,
  648. const struct rfkill_ops *ops,
  649. void *ops_data)
  650. {
  651. struct rfkill *rfkill;
  652. struct device *dev;
  653. if (WARN_ON(!ops))
  654. return NULL;
  655. if (WARN_ON(!ops->set_block))
  656. return NULL;
  657. if (WARN_ON(!name))
  658. return NULL;
  659. if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
  660. return NULL;
  661. rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
  662. if (!rfkill)
  663. return NULL;
  664. spin_lock_init(&rfkill->lock);
  665. INIT_LIST_HEAD(&rfkill->node);
  666. rfkill->type = type;
  667. rfkill->name = name;
  668. rfkill->ops = ops;
  669. rfkill->data = ops_data;
  670. dev = &rfkill->dev;
  671. dev->class = &rfkill_class;
  672. dev->parent = parent;
  673. device_initialize(dev);
  674. return rfkill;
  675. }
  676. EXPORT_SYMBOL(rfkill_alloc);
  677. static void rfkill_poll(struct work_struct *work)
  678. {
  679. struct rfkill *rfkill;
  680. rfkill = container_of(work, struct rfkill, poll_work.work);
  681. /*
  682. * Poll hardware state -- driver will use one of the
  683. * rfkill_set{,_hw,_sw}_state functions and use its
  684. * return value to update the current status.
  685. */
  686. rfkill->ops->poll(rfkill, rfkill->data);
  687. schedule_delayed_work(&rfkill->poll_work,
  688. round_jiffies_relative(POLL_INTERVAL));
  689. }
  690. static void rfkill_uevent_work(struct work_struct *work)
  691. {
  692. struct rfkill *rfkill;
  693. rfkill = container_of(work, struct rfkill, uevent_work);
  694. mutex_lock(&rfkill_global_mutex);
  695. rfkill_event(rfkill);
  696. mutex_unlock(&rfkill_global_mutex);
  697. }
  698. static void rfkill_sync_work(struct work_struct *work)
  699. {
  700. struct rfkill *rfkill;
  701. bool cur;
  702. rfkill = container_of(work, struct rfkill, sync_work);
  703. mutex_lock(&rfkill_global_mutex);
  704. cur = rfkill_global_states[rfkill->type].cur;
  705. rfkill_set_block(rfkill, cur);
  706. mutex_unlock(&rfkill_global_mutex);
  707. }
  708. int __must_check rfkill_register(struct rfkill *rfkill)
  709. {
  710. static unsigned long rfkill_no;
  711. struct device *dev = &rfkill->dev;
  712. int error;
  713. BUG_ON(!rfkill);
  714. mutex_lock(&rfkill_global_mutex);
  715. if (rfkill->registered) {
  716. error = -EALREADY;
  717. goto unlock;
  718. }
  719. rfkill->idx = rfkill_no;
  720. dev_set_name(dev, "rfkill%lu", rfkill_no);
  721. rfkill_no++;
  722. list_add_tail(&rfkill->node, &rfkill_list);
  723. error = device_add(dev);
  724. if (error)
  725. goto remove;
  726. error = rfkill_led_trigger_register(rfkill);
  727. if (error)
  728. goto devdel;
  729. rfkill->registered = true;
  730. INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
  731. INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
  732. INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
  733. if (rfkill->ops->poll)
  734. schedule_delayed_work(&rfkill->poll_work,
  735. round_jiffies_relative(POLL_INTERVAL));
  736. if (!rfkill->persistent || rfkill_epo_lock_active) {
  737. schedule_work(&rfkill->sync_work);
  738. } else {
  739. #ifdef CONFIG_RFKILL_INPUT
  740. bool soft_blocked = !!(rfkill->state & RFKILL_BLOCK_SW);
  741. if (!atomic_read(&rfkill_input_disabled))
  742. __rfkill_switch_all(rfkill->type, soft_blocked);
  743. #endif
  744. }
  745. rfkill_send_events(rfkill, RFKILL_OP_ADD);
  746. mutex_unlock(&rfkill_global_mutex);
  747. return 0;
  748. devdel:
  749. device_del(&rfkill->dev);
  750. remove:
  751. list_del_init(&rfkill->node);
  752. unlock:
  753. mutex_unlock(&rfkill_global_mutex);
  754. return error;
  755. }
  756. EXPORT_SYMBOL(rfkill_register);
  757. void rfkill_unregister(struct rfkill *rfkill)
  758. {
  759. BUG_ON(!rfkill);
  760. if (rfkill->ops->poll)
  761. cancel_delayed_work_sync(&rfkill->poll_work);
  762. cancel_work_sync(&rfkill->uevent_work);
  763. cancel_work_sync(&rfkill->sync_work);
  764. rfkill->registered = false;
  765. device_del(&rfkill->dev);
  766. mutex_lock(&rfkill_global_mutex);
  767. rfkill_send_events(rfkill, RFKILL_OP_DEL);
  768. list_del_init(&rfkill->node);
  769. mutex_unlock(&rfkill_global_mutex);
  770. rfkill_led_trigger_unregister(rfkill);
  771. }
  772. EXPORT_SYMBOL(rfkill_unregister);
  773. void rfkill_destroy(struct rfkill *rfkill)
  774. {
  775. if (rfkill)
  776. put_device(&rfkill->dev);
  777. }
  778. EXPORT_SYMBOL(rfkill_destroy);
  779. static int rfkill_fop_open(struct inode *inode, struct file *file)
  780. {
  781. struct rfkill_data *data;
  782. struct rfkill *rfkill;
  783. struct rfkill_int_event *ev, *tmp;
  784. data = kzalloc(sizeof(*data), GFP_KERNEL);
  785. if (!data)
  786. return -ENOMEM;
  787. INIT_LIST_HEAD(&data->events);
  788. mutex_init(&data->mtx);
  789. init_waitqueue_head(&data->read_wait);
  790. mutex_lock(&rfkill_global_mutex);
  791. mutex_lock(&data->mtx);
  792. /*
  793. * start getting events from elsewhere but hold mtx to get
  794. * startup events added first
  795. */
  796. list_add(&data->list, &rfkill_fds);
  797. list_for_each_entry(rfkill, &rfkill_list, node) {
  798. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  799. if (!ev)
  800. goto free;
  801. rfkill_fill_event(&ev->ev, rfkill, RFKILL_OP_ADD);
  802. list_add_tail(&ev->list, &data->events);
  803. }
  804. mutex_unlock(&data->mtx);
  805. mutex_unlock(&rfkill_global_mutex);
  806. file->private_data = data;
  807. return nonseekable_open(inode, file);
  808. free:
  809. mutex_unlock(&data->mtx);
  810. mutex_unlock(&rfkill_global_mutex);
  811. mutex_destroy(&data->mtx);
  812. list_for_each_entry_safe(ev, tmp, &data->events, list)
  813. kfree(ev);
  814. kfree(data);
  815. return -ENOMEM;
  816. }
  817. static unsigned int rfkill_fop_poll(struct file *file, poll_table *wait)
  818. {
  819. struct rfkill_data *data = file->private_data;
  820. unsigned int res = POLLOUT | POLLWRNORM;
  821. poll_wait(file, &data->read_wait, wait);
  822. mutex_lock(&data->mtx);
  823. if (!list_empty(&data->events))
  824. res = POLLIN | POLLRDNORM;
  825. mutex_unlock(&data->mtx);
  826. return res;
  827. }
  828. static bool rfkill_readable(struct rfkill_data *data)
  829. {
  830. bool r;
  831. mutex_lock(&data->mtx);
  832. r = !list_empty(&data->events);
  833. mutex_unlock(&data->mtx);
  834. return r;
  835. }
  836. static ssize_t rfkill_fop_read(struct file *file, char __user *buf,
  837. size_t count, loff_t *pos)
  838. {
  839. struct rfkill_data *data = file->private_data;
  840. struct rfkill_int_event *ev;
  841. unsigned long sz;
  842. int ret;
  843. mutex_lock(&data->mtx);
  844. while (list_empty(&data->events)) {
  845. if (file->f_flags & O_NONBLOCK) {
  846. ret = -EAGAIN;
  847. goto out;
  848. }
  849. mutex_unlock(&data->mtx);
  850. ret = wait_event_interruptible(data->read_wait,
  851. rfkill_readable(data));
  852. mutex_lock(&data->mtx);
  853. if (ret)
  854. goto out;
  855. }
  856. ev = list_first_entry(&data->events, struct rfkill_int_event,
  857. list);
  858. sz = min_t(unsigned long, sizeof(ev->ev), count);
  859. ret = sz;
  860. if (copy_to_user(buf, &ev->ev, sz))
  861. ret = -EFAULT;
  862. list_del(&ev->list);
  863. kfree(ev);
  864. out:
  865. mutex_unlock(&data->mtx);
  866. return ret;
  867. }
  868. static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
  869. size_t count, loff_t *pos)
  870. {
  871. struct rfkill *rfkill;
  872. struct rfkill_event ev;
  873. /* we don't need the 'hard' variable but accept it */
  874. if (count < sizeof(ev) - 1)
  875. return -EINVAL;
  876. if (copy_from_user(&ev, buf, sizeof(ev) - 1))
  877. return -EFAULT;
  878. if (ev.op != RFKILL_OP_CHANGE && ev.op != RFKILL_OP_CHANGE_ALL)
  879. return -EINVAL;
  880. if (ev.type >= NUM_RFKILL_TYPES)
  881. return -EINVAL;
  882. mutex_lock(&rfkill_global_mutex);
  883. if (ev.op == RFKILL_OP_CHANGE_ALL) {
  884. if (ev.type == RFKILL_TYPE_ALL) {
  885. enum rfkill_type i;
  886. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  887. rfkill_global_states[i].cur = ev.soft;
  888. } else {
  889. rfkill_global_states[ev.type].cur = ev.soft;
  890. }
  891. }
  892. list_for_each_entry(rfkill, &rfkill_list, node) {
  893. if (rfkill->idx != ev.idx && ev.op != RFKILL_OP_CHANGE_ALL)
  894. continue;
  895. if (rfkill->type != ev.type && ev.type != RFKILL_TYPE_ALL)
  896. continue;
  897. rfkill_set_block(rfkill, ev.soft);
  898. }
  899. mutex_unlock(&rfkill_global_mutex);
  900. return count;
  901. }
  902. static int rfkill_fop_release(struct inode *inode, struct file *file)
  903. {
  904. struct rfkill_data *data = file->private_data;
  905. struct rfkill_int_event *ev, *tmp;
  906. mutex_lock(&rfkill_global_mutex);
  907. list_del(&data->list);
  908. mutex_unlock(&rfkill_global_mutex);
  909. mutex_destroy(&data->mtx);
  910. list_for_each_entry_safe(ev, tmp, &data->events, list)
  911. kfree(ev);
  912. #ifdef CONFIG_RFKILL_INPUT
  913. if (data->input_handler)
  914. if (atomic_dec_return(&rfkill_input_disabled) == 0)
  915. printk(KERN_DEBUG "rfkill: input handler enabled\n");
  916. #endif
  917. kfree(data);
  918. return 0;
  919. }
  920. #ifdef CONFIG_RFKILL_INPUT
  921. static long rfkill_fop_ioctl(struct file *file, unsigned int cmd,
  922. unsigned long arg)
  923. {
  924. struct rfkill_data *data = file->private_data;
  925. if (_IOC_TYPE(cmd) != RFKILL_IOC_MAGIC)
  926. return -ENOSYS;
  927. if (_IOC_NR(cmd) != RFKILL_IOC_NOINPUT)
  928. return -ENOSYS;
  929. mutex_lock(&data->mtx);
  930. if (!data->input_handler) {
  931. if (atomic_inc_return(&rfkill_input_disabled) == 1)
  932. printk(KERN_DEBUG "rfkill: input handler disabled\n");
  933. data->input_handler = true;
  934. }
  935. mutex_unlock(&data->mtx);
  936. return 0;
  937. }
  938. #endif
  939. static const struct file_operations rfkill_fops = {
  940. .open = rfkill_fop_open,
  941. .read = rfkill_fop_read,
  942. .write = rfkill_fop_write,
  943. .poll = rfkill_fop_poll,
  944. .release = rfkill_fop_release,
  945. #ifdef CONFIG_RFKILL_INPUT
  946. .unlocked_ioctl = rfkill_fop_ioctl,
  947. .compat_ioctl = rfkill_fop_ioctl,
  948. #endif
  949. };
  950. static struct miscdevice rfkill_miscdev = {
  951. .name = "rfkill",
  952. .fops = &rfkill_fops,
  953. .minor = MISC_DYNAMIC_MINOR,
  954. };
  955. static int __init rfkill_init(void)
  956. {
  957. int error;
  958. int i;
  959. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  960. rfkill_global_states[i].cur = !rfkill_default_state;
  961. error = class_register(&rfkill_class);
  962. if (error)
  963. goto out;
  964. error = misc_register(&rfkill_miscdev);
  965. if (error) {
  966. class_unregister(&rfkill_class);
  967. goto out;
  968. }
  969. #ifdef CONFIG_RFKILL_INPUT
  970. error = rfkill_handler_init();
  971. if (error) {
  972. misc_deregister(&rfkill_miscdev);
  973. class_unregister(&rfkill_class);
  974. goto out;
  975. }
  976. #endif
  977. out:
  978. return error;
  979. }
  980. subsys_initcall(rfkill_init);
  981. static void __exit rfkill_exit(void)
  982. {
  983. #ifdef CONFIG_RFKILL_INPUT
  984. rfkill_handler_exit();
  985. #endif
  986. misc_deregister(&rfkill_miscdev);
  987. class_unregister(&rfkill_class);
  988. }
  989. module_exit(rfkill_exit);