core.c 28 KB

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