core.c 29 KB

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