core.c 29 KB

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