core.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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/sched.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/device.h>
  32. #include <linux/miscdevice.h>
  33. #include <linux/wait.h>
  34. #include <linux/poll.h>
  35. #include <linux/fs.h>
  36. #include <linux/slab.h>
  37. #include "rfkill.h"
  38. #define POLL_INTERVAL (5 * HZ)
  39. #define RFKILL_BLOCK_HW BIT(0)
  40. #define RFKILL_BLOCK_SW BIT(1)
  41. #define RFKILL_BLOCK_SW_PREV BIT(2)
  42. #define RFKILL_BLOCK_ANY (RFKILL_BLOCK_HW |\
  43. RFKILL_BLOCK_SW |\
  44. RFKILL_BLOCK_SW_PREV)
  45. #define RFKILL_BLOCK_SW_SETCALL BIT(31)
  46. struct rfkill {
  47. spinlock_t lock;
  48. const char *name;
  49. enum rfkill_type type;
  50. unsigned long state;
  51. u32 idx;
  52. bool registered;
  53. bool persistent;
  54. const struct rfkill_ops *ops;
  55. void *data;
  56. #ifdef CONFIG_RFKILL_LEDS
  57. struct led_trigger led_trigger;
  58. const char *ledtrigname;
  59. #endif
  60. struct device dev;
  61. struct list_head node;
  62. struct delayed_work poll_work;
  63. struct work_struct uevent_work;
  64. struct work_struct sync_work;
  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. static bool __rfkill_set_hw_state(struct rfkill *rfkill,
  193. bool blocked, bool *change)
  194. {
  195. unsigned long flags;
  196. bool prev, any;
  197. BUG_ON(!rfkill);
  198. spin_lock_irqsave(&rfkill->lock, flags);
  199. prev = !!(rfkill->state & RFKILL_BLOCK_HW);
  200. if (blocked)
  201. rfkill->state |= RFKILL_BLOCK_HW;
  202. else
  203. rfkill->state &= ~RFKILL_BLOCK_HW;
  204. *change = prev != blocked;
  205. any = !!(rfkill->state & RFKILL_BLOCK_ANY);
  206. spin_unlock_irqrestore(&rfkill->lock, flags);
  207. rfkill_led_trigger_event(rfkill);
  208. return any;
  209. }
  210. /**
  211. * rfkill_set_block - wrapper for set_block method
  212. *
  213. * @rfkill: the rfkill struct to use
  214. * @blocked: the new software state
  215. *
  216. * Calls the set_block method (when applicable) and handles notifications
  217. * etc. as well.
  218. */
  219. static void rfkill_set_block(struct rfkill *rfkill, bool blocked)
  220. {
  221. unsigned long flags;
  222. int err;
  223. if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
  224. return;
  225. /*
  226. * Some platforms (...!) generate input events which affect the
  227. * _hard_ kill state -- whenever something tries to change the
  228. * current software state query the hardware state too.
  229. */
  230. if (rfkill->ops->query)
  231. rfkill->ops->query(rfkill, rfkill->data);
  232. spin_lock_irqsave(&rfkill->lock, flags);
  233. if (rfkill->state & RFKILL_BLOCK_SW)
  234. rfkill->state |= RFKILL_BLOCK_SW_PREV;
  235. else
  236. rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
  237. if (blocked)
  238. rfkill->state |= RFKILL_BLOCK_SW;
  239. else
  240. rfkill->state &= ~RFKILL_BLOCK_SW;
  241. rfkill->state |= RFKILL_BLOCK_SW_SETCALL;
  242. spin_unlock_irqrestore(&rfkill->lock, flags);
  243. err = rfkill->ops->set_block(rfkill->data, blocked);
  244. spin_lock_irqsave(&rfkill->lock, flags);
  245. if (err) {
  246. /*
  247. * Failed -- reset status to _prev, this may be different
  248. * from what set set _PREV to earlier in this function
  249. * if rfkill_set_sw_state was invoked.
  250. */
  251. if (rfkill->state & RFKILL_BLOCK_SW_PREV)
  252. rfkill->state |= RFKILL_BLOCK_SW;
  253. else
  254. rfkill->state &= ~RFKILL_BLOCK_SW;
  255. }
  256. rfkill->state &= ~RFKILL_BLOCK_SW_SETCALL;
  257. rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
  258. spin_unlock_irqrestore(&rfkill->lock, flags);
  259. rfkill_led_trigger_event(rfkill);
  260. rfkill_event(rfkill);
  261. }
  262. #ifdef CONFIG_RFKILL_INPUT
  263. static atomic_t rfkill_input_disabled = ATOMIC_INIT(0);
  264. /**
  265. * __rfkill_switch_all - Toggle state of all switches of given type
  266. * @type: type of interfaces to be affected
  267. * @state: the new state
  268. *
  269. * This function sets the state of all switches of given type,
  270. * unless a specific switch is claimed by userspace (in which case,
  271. * that switch is left alone) or suspended.
  272. *
  273. * Caller must have acquired rfkill_global_mutex.
  274. */
  275. static void __rfkill_switch_all(const enum rfkill_type type, bool blocked)
  276. {
  277. struct rfkill *rfkill;
  278. rfkill_global_states[type].cur = blocked;
  279. list_for_each_entry(rfkill, &rfkill_list, node) {
  280. if (rfkill->type != type && type != RFKILL_TYPE_ALL)
  281. continue;
  282. rfkill_set_block(rfkill, blocked);
  283. }
  284. }
  285. /**
  286. * rfkill_switch_all - Toggle state of all switches of given type
  287. * @type: type of interfaces to be affected
  288. * @state: the new state
  289. *
  290. * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
  291. * Please refer to __rfkill_switch_all() for details.
  292. *
  293. * Does nothing if the EPO lock is active.
  294. */
  295. void rfkill_switch_all(enum rfkill_type type, bool blocked)
  296. {
  297. if (atomic_read(&rfkill_input_disabled))
  298. return;
  299. mutex_lock(&rfkill_global_mutex);
  300. if (!rfkill_epo_lock_active)
  301. __rfkill_switch_all(type, blocked);
  302. mutex_unlock(&rfkill_global_mutex);
  303. }
  304. /**
  305. * rfkill_epo - emergency power off all transmitters
  306. *
  307. * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
  308. * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
  309. *
  310. * The global state before the EPO is saved and can be restored later
  311. * using rfkill_restore_states().
  312. */
  313. void rfkill_epo(void)
  314. {
  315. struct rfkill *rfkill;
  316. int i;
  317. if (atomic_read(&rfkill_input_disabled))
  318. return;
  319. mutex_lock(&rfkill_global_mutex);
  320. rfkill_epo_lock_active = true;
  321. list_for_each_entry(rfkill, &rfkill_list, node)
  322. rfkill_set_block(rfkill, true);
  323. for (i = 0; i < NUM_RFKILL_TYPES; i++) {
  324. rfkill_global_states[i].sav = rfkill_global_states[i].cur;
  325. rfkill_global_states[i].cur = true;
  326. }
  327. mutex_unlock(&rfkill_global_mutex);
  328. }
  329. /**
  330. * rfkill_restore_states - restore global states
  331. *
  332. * Restore (and sync switches to) the global state from the
  333. * states in rfkill_default_states. This can undo the effects of
  334. * a call to rfkill_epo().
  335. */
  336. void rfkill_restore_states(void)
  337. {
  338. int i;
  339. if (atomic_read(&rfkill_input_disabled))
  340. return;
  341. mutex_lock(&rfkill_global_mutex);
  342. rfkill_epo_lock_active = false;
  343. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  344. __rfkill_switch_all(i, rfkill_global_states[i].sav);
  345. mutex_unlock(&rfkill_global_mutex);
  346. }
  347. /**
  348. * rfkill_remove_epo_lock - unlock state changes
  349. *
  350. * Used by rfkill-input manually unlock state changes, when
  351. * the EPO switch is deactivated.
  352. */
  353. void rfkill_remove_epo_lock(void)
  354. {
  355. if (atomic_read(&rfkill_input_disabled))
  356. return;
  357. mutex_lock(&rfkill_global_mutex);
  358. rfkill_epo_lock_active = false;
  359. mutex_unlock(&rfkill_global_mutex);
  360. }
  361. /**
  362. * rfkill_is_epo_lock_active - returns true EPO is active
  363. *
  364. * Returns 0 (false) if there is NOT an active EPO contidion,
  365. * and 1 (true) if there is an active EPO contition, which
  366. * locks all radios in one of the BLOCKED states.
  367. *
  368. * Can be called in atomic context.
  369. */
  370. bool rfkill_is_epo_lock_active(void)
  371. {
  372. return rfkill_epo_lock_active;
  373. }
  374. /**
  375. * rfkill_get_global_sw_state - returns global state for a type
  376. * @type: the type to get the global state of
  377. *
  378. * Returns the current global state for a given wireless
  379. * device type.
  380. */
  381. bool rfkill_get_global_sw_state(const enum rfkill_type type)
  382. {
  383. return rfkill_global_states[type].cur;
  384. }
  385. #endif
  386. bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  387. {
  388. bool ret, change;
  389. ret = __rfkill_set_hw_state(rfkill, blocked, &change);
  390. if (!rfkill->registered)
  391. return ret;
  392. if (change)
  393. schedule_work(&rfkill->uevent_work);
  394. return ret;
  395. }
  396. EXPORT_SYMBOL(rfkill_set_hw_state);
  397. static void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  398. {
  399. u32 bit = RFKILL_BLOCK_SW;
  400. /* if in a ops->set_block right now, use other bit */
  401. if (rfkill->state & RFKILL_BLOCK_SW_SETCALL)
  402. bit = RFKILL_BLOCK_SW_PREV;
  403. if (blocked)
  404. rfkill->state |= bit;
  405. else
  406. rfkill->state &= ~bit;
  407. }
  408. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  409. {
  410. unsigned long flags;
  411. bool prev, hwblock;
  412. BUG_ON(!rfkill);
  413. spin_lock_irqsave(&rfkill->lock, flags);
  414. prev = !!(rfkill->state & RFKILL_BLOCK_SW);
  415. __rfkill_set_sw_state(rfkill, blocked);
  416. hwblock = !!(rfkill->state & RFKILL_BLOCK_HW);
  417. blocked = blocked || hwblock;
  418. spin_unlock_irqrestore(&rfkill->lock, flags);
  419. if (!rfkill->registered)
  420. return blocked;
  421. if (prev != blocked && !hwblock)
  422. schedule_work(&rfkill->uevent_work);
  423. rfkill_led_trigger_event(rfkill);
  424. return blocked;
  425. }
  426. EXPORT_SYMBOL(rfkill_set_sw_state);
  427. void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
  428. {
  429. unsigned long flags;
  430. BUG_ON(!rfkill);
  431. BUG_ON(rfkill->registered);
  432. spin_lock_irqsave(&rfkill->lock, flags);
  433. __rfkill_set_sw_state(rfkill, blocked);
  434. rfkill->persistent = true;
  435. spin_unlock_irqrestore(&rfkill->lock, flags);
  436. }
  437. EXPORT_SYMBOL(rfkill_init_sw_state);
  438. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  439. {
  440. unsigned long flags;
  441. bool swprev, hwprev;
  442. BUG_ON(!rfkill);
  443. spin_lock_irqsave(&rfkill->lock, flags);
  444. /*
  445. * No need to care about prev/setblock ... this is for uevent only
  446. * and that will get triggered by rfkill_set_block anyway.
  447. */
  448. swprev = !!(rfkill->state & RFKILL_BLOCK_SW);
  449. hwprev = !!(rfkill->state & RFKILL_BLOCK_HW);
  450. __rfkill_set_sw_state(rfkill, sw);
  451. if (hw)
  452. rfkill->state |= RFKILL_BLOCK_HW;
  453. else
  454. rfkill->state &= ~RFKILL_BLOCK_HW;
  455. spin_unlock_irqrestore(&rfkill->lock, flags);
  456. if (!rfkill->registered) {
  457. rfkill->persistent = true;
  458. } else {
  459. if (swprev != sw || hwprev != hw)
  460. schedule_work(&rfkill->uevent_work);
  461. rfkill_led_trigger_event(rfkill);
  462. }
  463. }
  464. EXPORT_SYMBOL(rfkill_set_states);
  465. static ssize_t rfkill_name_show(struct device *dev,
  466. struct device_attribute *attr,
  467. char *buf)
  468. {
  469. struct rfkill *rfkill = to_rfkill(dev);
  470. return sprintf(buf, "%s\n", rfkill->name);
  471. }
  472. static const char *rfkill_get_type_str(enum rfkill_type type)
  473. {
  474. BUILD_BUG_ON(NUM_RFKILL_TYPES != RFKILL_TYPE_FM + 1);
  475. switch (type) {
  476. case RFKILL_TYPE_WLAN:
  477. return "wlan";
  478. case RFKILL_TYPE_BLUETOOTH:
  479. return "bluetooth";
  480. case RFKILL_TYPE_UWB:
  481. return "ultrawideband";
  482. case RFKILL_TYPE_WIMAX:
  483. return "wimax";
  484. case RFKILL_TYPE_WWAN:
  485. return "wwan";
  486. case RFKILL_TYPE_GPS:
  487. return "gps";
  488. case RFKILL_TYPE_FM:
  489. return "fm";
  490. default:
  491. BUG();
  492. }
  493. }
  494. static ssize_t rfkill_type_show(struct device *dev,
  495. struct device_attribute *attr,
  496. char *buf)
  497. {
  498. struct rfkill *rfkill = to_rfkill(dev);
  499. return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
  500. }
  501. static ssize_t rfkill_idx_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->idx);
  507. }
  508. static ssize_t rfkill_persistent_show(struct device *dev,
  509. struct device_attribute *attr,
  510. char *buf)
  511. {
  512. struct rfkill *rfkill = to_rfkill(dev);
  513. return sprintf(buf, "%d\n", rfkill->persistent);
  514. }
  515. static ssize_t rfkill_hard_show(struct device *dev,
  516. struct device_attribute *attr,
  517. char *buf)
  518. {
  519. struct rfkill *rfkill = to_rfkill(dev);
  520. return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_HW) ? 1 : 0 );
  521. }
  522. static ssize_t rfkill_soft_show(struct device *dev,
  523. struct device_attribute *attr,
  524. char *buf)
  525. {
  526. struct rfkill *rfkill = to_rfkill(dev);
  527. return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_SW) ? 1 : 0 );
  528. }
  529. static ssize_t rfkill_soft_store(struct device *dev,
  530. struct device_attribute *attr,
  531. const char *buf, size_t count)
  532. {
  533. struct rfkill *rfkill = to_rfkill(dev);
  534. unsigned long state;
  535. int err;
  536. if (!capable(CAP_NET_ADMIN))
  537. return -EPERM;
  538. err = kstrtoul(buf, 0, &state);
  539. if (err)
  540. return err;
  541. if (state > 1 )
  542. return -EINVAL;
  543. mutex_lock(&rfkill_global_mutex);
  544. rfkill_set_block(rfkill, state);
  545. mutex_unlock(&rfkill_global_mutex);
  546. return err ?: count;
  547. }
  548. static u8 user_state_from_blocked(unsigned long state)
  549. {
  550. if (state & RFKILL_BLOCK_HW)
  551. return RFKILL_USER_STATE_HARD_BLOCKED;
  552. if (state & RFKILL_BLOCK_SW)
  553. return RFKILL_USER_STATE_SOFT_BLOCKED;
  554. return RFKILL_USER_STATE_UNBLOCKED;
  555. }
  556. static ssize_t rfkill_state_show(struct device *dev,
  557. struct device_attribute *attr,
  558. char *buf)
  559. {
  560. struct rfkill *rfkill = to_rfkill(dev);
  561. return sprintf(buf, "%d\n", user_state_from_blocked(rfkill->state));
  562. }
  563. static ssize_t rfkill_state_store(struct device *dev,
  564. struct device_attribute *attr,
  565. const char *buf, size_t count)
  566. {
  567. struct rfkill *rfkill = to_rfkill(dev);
  568. unsigned long state;
  569. int err;
  570. if (!capable(CAP_NET_ADMIN))
  571. return -EPERM;
  572. err = kstrtoul(buf, 0, &state);
  573. if (err)
  574. return err;
  575. if (state != RFKILL_USER_STATE_SOFT_BLOCKED &&
  576. state != RFKILL_USER_STATE_UNBLOCKED)
  577. return -EINVAL;
  578. mutex_lock(&rfkill_global_mutex);
  579. rfkill_set_block(rfkill, state == RFKILL_USER_STATE_SOFT_BLOCKED);
  580. mutex_unlock(&rfkill_global_mutex);
  581. return err ?: count;
  582. }
  583. static ssize_t rfkill_claim_show(struct device *dev,
  584. struct device_attribute *attr,
  585. char *buf)
  586. {
  587. return sprintf(buf, "%d\n", 0);
  588. }
  589. static ssize_t rfkill_claim_store(struct device *dev,
  590. struct device_attribute *attr,
  591. const char *buf, size_t count)
  592. {
  593. return -EOPNOTSUPP;
  594. }
  595. static struct device_attribute rfkill_dev_attrs[] = {
  596. __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
  597. __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
  598. __ATTR(index, S_IRUGO, rfkill_idx_show, NULL),
  599. __ATTR(persistent, S_IRUGO, rfkill_persistent_show, NULL),
  600. __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
  601. __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
  602. __ATTR(soft, S_IRUGO|S_IWUSR, rfkill_soft_show, rfkill_soft_store),
  603. __ATTR(hard, S_IRUGO, rfkill_hard_show, NULL),
  604. __ATTR_NULL
  605. };
  606. static void rfkill_release(struct device *dev)
  607. {
  608. struct rfkill *rfkill = to_rfkill(dev);
  609. kfree(rfkill);
  610. }
  611. static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  612. {
  613. struct rfkill *rfkill = to_rfkill(dev);
  614. unsigned long flags;
  615. u32 state;
  616. int error;
  617. error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
  618. if (error)
  619. return error;
  620. error = add_uevent_var(env, "RFKILL_TYPE=%s",
  621. rfkill_get_type_str(rfkill->type));
  622. if (error)
  623. return error;
  624. spin_lock_irqsave(&rfkill->lock, flags);
  625. state = rfkill->state;
  626. spin_unlock_irqrestore(&rfkill->lock, flags);
  627. error = add_uevent_var(env, "RFKILL_STATE=%d",
  628. user_state_from_blocked(state));
  629. return error;
  630. }
  631. void rfkill_pause_polling(struct rfkill *rfkill)
  632. {
  633. BUG_ON(!rfkill);
  634. if (!rfkill->ops->poll)
  635. return;
  636. cancel_delayed_work_sync(&rfkill->poll_work);
  637. }
  638. EXPORT_SYMBOL(rfkill_pause_polling);
  639. void rfkill_resume_polling(struct rfkill *rfkill)
  640. {
  641. BUG_ON(!rfkill);
  642. if (!rfkill->ops->poll)
  643. return;
  644. schedule_work(&rfkill->poll_work.work);
  645. }
  646. EXPORT_SYMBOL(rfkill_resume_polling);
  647. static int rfkill_suspend(struct device *dev, pm_message_t state)
  648. {
  649. struct rfkill *rfkill = to_rfkill(dev);
  650. rfkill_pause_polling(rfkill);
  651. return 0;
  652. }
  653. static int rfkill_resume(struct device *dev)
  654. {
  655. struct rfkill *rfkill = to_rfkill(dev);
  656. bool cur;
  657. if (!rfkill->persistent) {
  658. cur = !!(rfkill->state & RFKILL_BLOCK_SW);
  659. rfkill_set_block(rfkill, cur);
  660. }
  661. rfkill_resume_polling(rfkill);
  662. return 0;
  663. }
  664. static struct class rfkill_class = {
  665. .name = "rfkill",
  666. .dev_release = rfkill_release,
  667. .dev_attrs = rfkill_dev_attrs,
  668. .dev_uevent = rfkill_dev_uevent,
  669. .suspend = rfkill_suspend,
  670. .resume = rfkill_resume,
  671. };
  672. bool rfkill_blocked(struct rfkill *rfkill)
  673. {
  674. unsigned long flags;
  675. u32 state;
  676. spin_lock_irqsave(&rfkill->lock, flags);
  677. state = rfkill->state;
  678. spin_unlock_irqrestore(&rfkill->lock, flags);
  679. return !!(state & RFKILL_BLOCK_ANY);
  680. }
  681. EXPORT_SYMBOL(rfkill_blocked);
  682. struct rfkill * __must_check rfkill_alloc(const char *name,
  683. struct device *parent,
  684. const enum rfkill_type type,
  685. const struct rfkill_ops *ops,
  686. void *ops_data)
  687. {
  688. struct rfkill *rfkill;
  689. struct device *dev;
  690. if (WARN_ON(!ops))
  691. return NULL;
  692. if (WARN_ON(!ops->set_block))
  693. return NULL;
  694. if (WARN_ON(!name))
  695. return NULL;
  696. if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
  697. return NULL;
  698. rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
  699. if (!rfkill)
  700. return NULL;
  701. spin_lock_init(&rfkill->lock);
  702. INIT_LIST_HEAD(&rfkill->node);
  703. rfkill->type = type;
  704. rfkill->name = name;
  705. rfkill->ops = ops;
  706. rfkill->data = ops_data;
  707. dev = &rfkill->dev;
  708. dev->class = &rfkill_class;
  709. dev->parent = parent;
  710. device_initialize(dev);
  711. return rfkill;
  712. }
  713. EXPORT_SYMBOL(rfkill_alloc);
  714. static void rfkill_poll(struct work_struct *work)
  715. {
  716. struct rfkill *rfkill;
  717. rfkill = container_of(work, struct rfkill, poll_work.work);
  718. /*
  719. * Poll hardware state -- driver will use one of the
  720. * rfkill_set{,_hw,_sw}_state functions and use its
  721. * return value to update the current status.
  722. */
  723. rfkill->ops->poll(rfkill, rfkill->data);
  724. schedule_delayed_work(&rfkill->poll_work,
  725. round_jiffies_relative(POLL_INTERVAL));
  726. }
  727. static void rfkill_uevent_work(struct work_struct *work)
  728. {
  729. struct rfkill *rfkill;
  730. rfkill = container_of(work, struct rfkill, uevent_work);
  731. mutex_lock(&rfkill_global_mutex);
  732. rfkill_event(rfkill);
  733. mutex_unlock(&rfkill_global_mutex);
  734. }
  735. static void rfkill_sync_work(struct work_struct *work)
  736. {
  737. struct rfkill *rfkill;
  738. bool cur;
  739. rfkill = container_of(work, struct rfkill, sync_work);
  740. mutex_lock(&rfkill_global_mutex);
  741. cur = rfkill_global_states[rfkill->type].cur;
  742. rfkill_set_block(rfkill, cur);
  743. mutex_unlock(&rfkill_global_mutex);
  744. }
  745. int __must_check rfkill_register(struct rfkill *rfkill)
  746. {
  747. static unsigned long rfkill_no;
  748. struct device *dev = &rfkill->dev;
  749. int error;
  750. BUG_ON(!rfkill);
  751. mutex_lock(&rfkill_global_mutex);
  752. if (rfkill->registered) {
  753. error = -EALREADY;
  754. goto unlock;
  755. }
  756. rfkill->idx = rfkill_no;
  757. dev_set_name(dev, "rfkill%lu", rfkill_no);
  758. rfkill_no++;
  759. list_add_tail(&rfkill->node, &rfkill_list);
  760. error = device_add(dev);
  761. if (error)
  762. goto remove;
  763. error = rfkill_led_trigger_register(rfkill);
  764. if (error)
  765. goto devdel;
  766. rfkill->registered = true;
  767. INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
  768. INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
  769. INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
  770. if (rfkill->ops->poll)
  771. schedule_delayed_work(&rfkill->poll_work,
  772. round_jiffies_relative(POLL_INTERVAL));
  773. if (!rfkill->persistent || rfkill_epo_lock_active) {
  774. schedule_work(&rfkill->sync_work);
  775. } else {
  776. #ifdef CONFIG_RFKILL_INPUT
  777. bool soft_blocked = !!(rfkill->state & RFKILL_BLOCK_SW);
  778. if (!atomic_read(&rfkill_input_disabled))
  779. __rfkill_switch_all(rfkill->type, soft_blocked);
  780. #endif
  781. }
  782. rfkill_send_events(rfkill, RFKILL_OP_ADD);
  783. mutex_unlock(&rfkill_global_mutex);
  784. return 0;
  785. devdel:
  786. device_del(&rfkill->dev);
  787. remove:
  788. list_del_init(&rfkill->node);
  789. unlock:
  790. mutex_unlock(&rfkill_global_mutex);
  791. return error;
  792. }
  793. EXPORT_SYMBOL(rfkill_register);
  794. void rfkill_unregister(struct rfkill *rfkill)
  795. {
  796. BUG_ON(!rfkill);
  797. if (rfkill->ops->poll)
  798. cancel_delayed_work_sync(&rfkill->poll_work);
  799. cancel_work_sync(&rfkill->uevent_work);
  800. cancel_work_sync(&rfkill->sync_work);
  801. rfkill->registered = false;
  802. device_del(&rfkill->dev);
  803. mutex_lock(&rfkill_global_mutex);
  804. rfkill_send_events(rfkill, RFKILL_OP_DEL);
  805. list_del_init(&rfkill->node);
  806. mutex_unlock(&rfkill_global_mutex);
  807. rfkill_led_trigger_unregister(rfkill);
  808. }
  809. EXPORT_SYMBOL(rfkill_unregister);
  810. void rfkill_destroy(struct rfkill *rfkill)
  811. {
  812. if (rfkill)
  813. put_device(&rfkill->dev);
  814. }
  815. EXPORT_SYMBOL(rfkill_destroy);
  816. static int rfkill_fop_open(struct inode *inode, struct file *file)
  817. {
  818. struct rfkill_data *data;
  819. struct rfkill *rfkill;
  820. struct rfkill_int_event *ev, *tmp;
  821. data = kzalloc(sizeof(*data), GFP_KERNEL);
  822. if (!data)
  823. return -ENOMEM;
  824. INIT_LIST_HEAD(&data->events);
  825. mutex_init(&data->mtx);
  826. init_waitqueue_head(&data->read_wait);
  827. mutex_lock(&rfkill_global_mutex);
  828. mutex_lock(&data->mtx);
  829. /*
  830. * start getting events from elsewhere but hold mtx to get
  831. * startup events added first
  832. */
  833. list_for_each_entry(rfkill, &rfkill_list, node) {
  834. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  835. if (!ev)
  836. goto free;
  837. rfkill_fill_event(&ev->ev, rfkill, RFKILL_OP_ADD);
  838. list_add_tail(&ev->list, &data->events);
  839. }
  840. list_add(&data->list, &rfkill_fds);
  841. mutex_unlock(&data->mtx);
  842. mutex_unlock(&rfkill_global_mutex);
  843. file->private_data = data;
  844. return nonseekable_open(inode, file);
  845. free:
  846. mutex_unlock(&data->mtx);
  847. mutex_unlock(&rfkill_global_mutex);
  848. mutex_destroy(&data->mtx);
  849. list_for_each_entry_safe(ev, tmp, &data->events, list)
  850. kfree(ev);
  851. kfree(data);
  852. return -ENOMEM;
  853. }
  854. static unsigned int rfkill_fop_poll(struct file *file, poll_table *wait)
  855. {
  856. struct rfkill_data *data = file->private_data;
  857. unsigned int res = POLLOUT | POLLWRNORM;
  858. poll_wait(file, &data->read_wait, wait);
  859. mutex_lock(&data->mtx);
  860. if (!list_empty(&data->events))
  861. res = POLLIN | POLLRDNORM;
  862. mutex_unlock(&data->mtx);
  863. return res;
  864. }
  865. static bool rfkill_readable(struct rfkill_data *data)
  866. {
  867. bool r;
  868. mutex_lock(&data->mtx);
  869. r = !list_empty(&data->events);
  870. mutex_unlock(&data->mtx);
  871. return r;
  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. ret = wait_event_interruptible(data->read_wait,
  888. rfkill_readable(data));
  889. mutex_lock(&data->mtx);
  890. if (ret)
  891. goto out;
  892. }
  893. ev = list_first_entry(&data->events, struct rfkill_int_event,
  894. list);
  895. sz = min_t(unsigned long, sizeof(ev->ev), count);
  896. ret = sz;
  897. if (copy_to_user(buf, &ev->ev, sz))
  898. ret = -EFAULT;
  899. list_del(&ev->list);
  900. kfree(ev);
  901. out:
  902. mutex_unlock(&data->mtx);
  903. return ret;
  904. }
  905. static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
  906. size_t count, loff_t *pos)
  907. {
  908. struct rfkill *rfkill;
  909. struct rfkill_event ev;
  910. /* we don't need the 'hard' variable but accept it */
  911. if (count < RFKILL_EVENT_SIZE_V1 - 1)
  912. return -EINVAL;
  913. /*
  914. * Copy as much data as we can accept into our 'ev' buffer,
  915. * but tell userspace how much we've copied so it can determine
  916. * our API version even in a write() call, if it cares.
  917. */
  918. count = min(count, sizeof(ev));
  919. if (copy_from_user(&ev, buf, count))
  920. return -EFAULT;
  921. if (ev.op != RFKILL_OP_CHANGE && ev.op != RFKILL_OP_CHANGE_ALL)
  922. return -EINVAL;
  923. if (ev.type >= NUM_RFKILL_TYPES)
  924. return -EINVAL;
  925. mutex_lock(&rfkill_global_mutex);
  926. if (ev.op == RFKILL_OP_CHANGE_ALL) {
  927. if (ev.type == RFKILL_TYPE_ALL) {
  928. enum rfkill_type i;
  929. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  930. rfkill_global_states[i].cur = ev.soft;
  931. } else {
  932. rfkill_global_states[ev.type].cur = ev.soft;
  933. }
  934. }
  935. list_for_each_entry(rfkill, &rfkill_list, node) {
  936. if (rfkill->idx != ev.idx && ev.op != RFKILL_OP_CHANGE_ALL)
  937. continue;
  938. if (rfkill->type != ev.type && ev.type != RFKILL_TYPE_ALL)
  939. continue;
  940. rfkill_set_block(rfkill, ev.soft);
  941. }
  942. mutex_unlock(&rfkill_global_mutex);
  943. return count;
  944. }
  945. static int rfkill_fop_release(struct inode *inode, struct file *file)
  946. {
  947. struct rfkill_data *data = file->private_data;
  948. struct rfkill_int_event *ev, *tmp;
  949. mutex_lock(&rfkill_global_mutex);
  950. list_del(&data->list);
  951. mutex_unlock(&rfkill_global_mutex);
  952. mutex_destroy(&data->mtx);
  953. list_for_each_entry_safe(ev, tmp, &data->events, list)
  954. kfree(ev);
  955. #ifdef CONFIG_RFKILL_INPUT
  956. if (data->input_handler)
  957. if (atomic_dec_return(&rfkill_input_disabled) == 0)
  958. printk(KERN_DEBUG "rfkill: input handler enabled\n");
  959. #endif
  960. kfree(data);
  961. return 0;
  962. }
  963. #ifdef CONFIG_RFKILL_INPUT
  964. static long rfkill_fop_ioctl(struct file *file, unsigned int cmd,
  965. unsigned long arg)
  966. {
  967. struct rfkill_data *data = file->private_data;
  968. if (_IOC_TYPE(cmd) != RFKILL_IOC_MAGIC)
  969. return -ENOSYS;
  970. if (_IOC_NR(cmd) != RFKILL_IOC_NOINPUT)
  971. return -ENOSYS;
  972. mutex_lock(&data->mtx);
  973. if (!data->input_handler) {
  974. if (atomic_inc_return(&rfkill_input_disabled) == 1)
  975. printk(KERN_DEBUG "rfkill: input handler disabled\n");
  976. data->input_handler = true;
  977. }
  978. mutex_unlock(&data->mtx);
  979. return 0;
  980. }
  981. #endif
  982. static const struct file_operations rfkill_fops = {
  983. .owner = THIS_MODULE,
  984. .open = rfkill_fop_open,
  985. .read = rfkill_fop_read,
  986. .write = rfkill_fop_write,
  987. .poll = rfkill_fop_poll,
  988. .release = rfkill_fop_release,
  989. #ifdef CONFIG_RFKILL_INPUT
  990. .unlocked_ioctl = rfkill_fop_ioctl,
  991. .compat_ioctl = rfkill_fop_ioctl,
  992. #endif
  993. .llseek = no_llseek,
  994. };
  995. static struct miscdevice rfkill_miscdev = {
  996. .name = "rfkill",
  997. .fops = &rfkill_fops,
  998. .minor = MISC_DYNAMIC_MINOR,
  999. };
  1000. static int __init rfkill_init(void)
  1001. {
  1002. int error;
  1003. int i;
  1004. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  1005. rfkill_global_states[i].cur = !rfkill_default_state;
  1006. error = class_register(&rfkill_class);
  1007. if (error)
  1008. goto out;
  1009. error = misc_register(&rfkill_miscdev);
  1010. if (error) {
  1011. class_unregister(&rfkill_class);
  1012. goto out;
  1013. }
  1014. #ifdef CONFIG_RFKILL_INPUT
  1015. error = rfkill_handler_init();
  1016. if (error) {
  1017. misc_deregister(&rfkill_miscdev);
  1018. class_unregister(&rfkill_class);
  1019. goto out;
  1020. }
  1021. #endif
  1022. out:
  1023. return error;
  1024. }
  1025. subsys_initcall(rfkill_init);
  1026. static void __exit rfkill_exit(void)
  1027. {
  1028. #ifdef CONFIG_RFKILL_INPUT
  1029. rfkill_handler_exit();
  1030. #endif
  1031. misc_deregister(&rfkill_miscdev);
  1032. class_unregister(&rfkill_class);
  1033. }
  1034. module_exit(rfkill_exit);