core.c 29 KB

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