core.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /*
  2. * Copyright (C) 2006 - 2007 Ivo van Doorn
  3. * Copyright (C) 2007 Dmitry Torokhov
  4. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/capability.h>
  24. #include <linux/list.h>
  25. #include <linux/mutex.h>
  26. #include <linux/rfkill.h>
  27. #include <linux/sched.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/device.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/wait.h>
  32. #include <linux/poll.h>
  33. #include <linux/fs.h>
  34. #include <linux/slab.h>
  35. #include "rfkill.h"
  36. #define POLL_INTERVAL (5 * HZ)
  37. #define RFKILL_BLOCK_HW BIT(0)
  38. #define RFKILL_BLOCK_SW BIT(1)
  39. #define RFKILL_BLOCK_SW_PREV BIT(2)
  40. #define RFKILL_BLOCK_ANY (RFKILL_BLOCK_HW |\
  41. RFKILL_BLOCK_SW |\
  42. RFKILL_BLOCK_SW_PREV)
  43. #define RFKILL_BLOCK_SW_SETCALL BIT(31)
  44. struct rfkill {
  45. spinlock_t lock;
  46. 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. bool prev, curr;
  221. int err;
  222. if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
  223. return;
  224. /*
  225. * Some platforms (...!) generate input events which affect the
  226. * _hard_ kill state -- whenever something tries to change the
  227. * current software state query the hardware state too.
  228. */
  229. if (rfkill->ops->query)
  230. rfkill->ops->query(rfkill, rfkill->data);
  231. spin_lock_irqsave(&rfkill->lock, flags);
  232. prev = rfkill->state & RFKILL_BLOCK_SW;
  233. if (rfkill->state & RFKILL_BLOCK_SW)
  234. rfkill->state |= RFKILL_BLOCK_SW_PREV;
  235. else
  236. rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
  237. if (blocked)
  238. rfkill->state |= RFKILL_BLOCK_SW;
  239. else
  240. rfkill->state &= ~RFKILL_BLOCK_SW;
  241. rfkill->state |= RFKILL_BLOCK_SW_SETCALL;
  242. spin_unlock_irqrestore(&rfkill->lock, flags);
  243. err = rfkill->ops->set_block(rfkill->data, blocked);
  244. spin_lock_irqsave(&rfkill->lock, flags);
  245. if (err) {
  246. /*
  247. * Failed -- reset status to _prev, this may be different
  248. * from what set set _PREV to earlier in this function
  249. * if rfkill_set_sw_state was invoked.
  250. */
  251. if (rfkill->state & RFKILL_BLOCK_SW_PREV)
  252. rfkill->state |= RFKILL_BLOCK_SW;
  253. else
  254. rfkill->state &= ~RFKILL_BLOCK_SW;
  255. }
  256. rfkill->state &= ~RFKILL_BLOCK_SW_SETCALL;
  257. rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
  258. curr = rfkill->state & RFKILL_BLOCK_SW;
  259. spin_unlock_irqrestore(&rfkill->lock, flags);
  260. rfkill_led_trigger_event(rfkill);
  261. if (prev != curr)
  262. rfkill_event(rfkill);
  263. }
  264. #ifdef CONFIG_RFKILL_INPUT
  265. static atomic_t rfkill_input_disabled = ATOMIC_INIT(0);
  266. /**
  267. * __rfkill_switch_all - Toggle state of all switches of given type
  268. * @type: type of interfaces to be affected
  269. * @blocked: the new state
  270. *
  271. * This function sets the state of all switches of given type,
  272. * unless a specific switch is claimed by userspace (in which case,
  273. * that switch is left alone) or suspended.
  274. *
  275. * Caller must have acquired rfkill_global_mutex.
  276. */
  277. static void __rfkill_switch_all(const enum rfkill_type type, bool blocked)
  278. {
  279. struct rfkill *rfkill;
  280. rfkill_global_states[type].cur = blocked;
  281. list_for_each_entry(rfkill, &rfkill_list, node) {
  282. if (rfkill->type != type && type != RFKILL_TYPE_ALL)
  283. continue;
  284. rfkill_set_block(rfkill, blocked);
  285. }
  286. }
  287. /**
  288. * rfkill_switch_all - Toggle state of all switches of given type
  289. * @type: type of interfaces to be affected
  290. * @blocked: the new state
  291. *
  292. * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
  293. * Please refer to __rfkill_switch_all() for details.
  294. *
  295. * Does nothing if the EPO lock is active.
  296. */
  297. void rfkill_switch_all(enum rfkill_type type, bool blocked)
  298. {
  299. if (atomic_read(&rfkill_input_disabled))
  300. return;
  301. mutex_lock(&rfkill_global_mutex);
  302. if (!rfkill_epo_lock_active)
  303. __rfkill_switch_all(type, blocked);
  304. mutex_unlock(&rfkill_global_mutex);
  305. }
  306. /**
  307. * rfkill_epo - emergency power off all transmitters
  308. *
  309. * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
  310. * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
  311. *
  312. * The global state before the EPO is saved and can be restored later
  313. * using rfkill_restore_states().
  314. */
  315. void rfkill_epo(void)
  316. {
  317. struct rfkill *rfkill;
  318. int i;
  319. if (atomic_read(&rfkill_input_disabled))
  320. return;
  321. mutex_lock(&rfkill_global_mutex);
  322. rfkill_epo_lock_active = true;
  323. list_for_each_entry(rfkill, &rfkill_list, node)
  324. rfkill_set_block(rfkill, true);
  325. for (i = 0; i < NUM_RFKILL_TYPES; i++) {
  326. rfkill_global_states[i].sav = rfkill_global_states[i].cur;
  327. rfkill_global_states[i].cur = true;
  328. }
  329. mutex_unlock(&rfkill_global_mutex);
  330. }
  331. /**
  332. * rfkill_restore_states - restore global states
  333. *
  334. * Restore (and sync switches to) the global state from the
  335. * states in rfkill_default_states. This can undo the effects of
  336. * a call to rfkill_epo().
  337. */
  338. void rfkill_restore_states(void)
  339. {
  340. int i;
  341. if (atomic_read(&rfkill_input_disabled))
  342. return;
  343. mutex_lock(&rfkill_global_mutex);
  344. rfkill_epo_lock_active = false;
  345. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  346. __rfkill_switch_all(i, rfkill_global_states[i].sav);
  347. mutex_unlock(&rfkill_global_mutex);
  348. }
  349. /**
  350. * rfkill_remove_epo_lock - unlock state changes
  351. *
  352. * Used by rfkill-input manually unlock state changes, when
  353. * the EPO switch is deactivated.
  354. */
  355. void rfkill_remove_epo_lock(void)
  356. {
  357. if (atomic_read(&rfkill_input_disabled))
  358. return;
  359. mutex_lock(&rfkill_global_mutex);
  360. rfkill_epo_lock_active = false;
  361. mutex_unlock(&rfkill_global_mutex);
  362. }
  363. /**
  364. * rfkill_is_epo_lock_active - returns true EPO is active
  365. *
  366. * Returns 0 (false) if there is NOT an active EPO contidion,
  367. * and 1 (true) if there is an active EPO contition, which
  368. * locks all radios in one of the BLOCKED states.
  369. *
  370. * Can be called in atomic context.
  371. */
  372. bool rfkill_is_epo_lock_active(void)
  373. {
  374. return rfkill_epo_lock_active;
  375. }
  376. /**
  377. * rfkill_get_global_sw_state - returns global state for a type
  378. * @type: the type to get the global state of
  379. *
  380. * Returns the current global state for a given wireless
  381. * device type.
  382. */
  383. bool rfkill_get_global_sw_state(const enum rfkill_type type)
  384. {
  385. return rfkill_global_states[type].cur;
  386. }
  387. #endif
  388. bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  389. {
  390. bool ret, change;
  391. ret = __rfkill_set_hw_state(rfkill, blocked, &change);
  392. if (!rfkill->registered)
  393. return ret;
  394. if (change)
  395. schedule_work(&rfkill->uevent_work);
  396. return ret;
  397. }
  398. EXPORT_SYMBOL(rfkill_set_hw_state);
  399. static void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  400. {
  401. u32 bit = RFKILL_BLOCK_SW;
  402. /* if in a ops->set_block right now, use other bit */
  403. if (rfkill->state & RFKILL_BLOCK_SW_SETCALL)
  404. bit = RFKILL_BLOCK_SW_PREV;
  405. if (blocked)
  406. rfkill->state |= bit;
  407. else
  408. rfkill->state &= ~bit;
  409. }
  410. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  411. {
  412. unsigned long flags;
  413. bool prev, hwblock;
  414. BUG_ON(!rfkill);
  415. spin_lock_irqsave(&rfkill->lock, flags);
  416. prev = !!(rfkill->state & RFKILL_BLOCK_SW);
  417. __rfkill_set_sw_state(rfkill, blocked);
  418. hwblock = !!(rfkill->state & RFKILL_BLOCK_HW);
  419. blocked = blocked || hwblock;
  420. spin_unlock_irqrestore(&rfkill->lock, flags);
  421. if (!rfkill->registered)
  422. return blocked;
  423. if (prev != blocked && !hwblock)
  424. schedule_work(&rfkill->uevent_work);
  425. rfkill_led_trigger_event(rfkill);
  426. return blocked;
  427. }
  428. EXPORT_SYMBOL(rfkill_set_sw_state);
  429. void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked)
  430. {
  431. unsigned long flags;
  432. BUG_ON(!rfkill);
  433. BUG_ON(rfkill->registered);
  434. spin_lock_irqsave(&rfkill->lock, flags);
  435. __rfkill_set_sw_state(rfkill, blocked);
  436. rfkill->persistent = true;
  437. spin_unlock_irqrestore(&rfkill->lock, flags);
  438. }
  439. EXPORT_SYMBOL(rfkill_init_sw_state);
  440. void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
  441. {
  442. unsigned long flags;
  443. bool swprev, hwprev;
  444. BUG_ON(!rfkill);
  445. spin_lock_irqsave(&rfkill->lock, flags);
  446. /*
  447. * No need to care about prev/setblock ... this is for uevent only
  448. * and that will get triggered by rfkill_set_block anyway.
  449. */
  450. swprev = !!(rfkill->state & RFKILL_BLOCK_SW);
  451. hwprev = !!(rfkill->state & RFKILL_BLOCK_HW);
  452. __rfkill_set_sw_state(rfkill, sw);
  453. if (hw)
  454. rfkill->state |= RFKILL_BLOCK_HW;
  455. else
  456. rfkill->state &= ~RFKILL_BLOCK_HW;
  457. spin_unlock_irqrestore(&rfkill->lock, flags);
  458. if (!rfkill->registered) {
  459. rfkill->persistent = true;
  460. } else {
  461. if (swprev != sw || hwprev != hw)
  462. schedule_work(&rfkill->uevent_work);
  463. rfkill_led_trigger_event(rfkill);
  464. }
  465. }
  466. EXPORT_SYMBOL(rfkill_set_states);
  467. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  468. char *buf)
  469. {
  470. struct rfkill *rfkill = to_rfkill(dev);
  471. return sprintf(buf, "%s\n", rfkill->name);
  472. }
  473. static DEVICE_ATTR_RO(name);
  474. static const char *rfkill_get_type_str(enum rfkill_type type)
  475. {
  476. BUILD_BUG_ON(NUM_RFKILL_TYPES != RFKILL_TYPE_NFC + 1);
  477. switch (type) {
  478. case RFKILL_TYPE_WLAN:
  479. return "wlan";
  480. case RFKILL_TYPE_BLUETOOTH:
  481. return "bluetooth";
  482. case RFKILL_TYPE_UWB:
  483. return "ultrawideband";
  484. case RFKILL_TYPE_WIMAX:
  485. return "wimax";
  486. case RFKILL_TYPE_WWAN:
  487. return "wwan";
  488. case RFKILL_TYPE_GPS:
  489. return "gps";
  490. case RFKILL_TYPE_FM:
  491. return "fm";
  492. case RFKILL_TYPE_NFC:
  493. return "nfc";
  494. default:
  495. BUG();
  496. }
  497. }
  498. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  499. char *buf)
  500. {
  501. struct rfkill *rfkill = to_rfkill(dev);
  502. return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
  503. }
  504. static DEVICE_ATTR_RO(type);
  505. static ssize_t index_show(struct device *dev, struct device_attribute *attr,
  506. char *buf)
  507. {
  508. struct rfkill *rfkill = to_rfkill(dev);
  509. return sprintf(buf, "%d\n", rfkill->idx);
  510. }
  511. static DEVICE_ATTR_RO(index);
  512. static ssize_t persistent_show(struct device *dev,
  513. struct device_attribute *attr, char *buf)
  514. {
  515. struct rfkill *rfkill = to_rfkill(dev);
  516. return sprintf(buf, "%d\n", rfkill->persistent);
  517. }
  518. static DEVICE_ATTR_RO(persistent);
  519. static ssize_t hard_show(struct device *dev, struct device_attribute *attr,
  520. char *buf)
  521. {
  522. struct rfkill *rfkill = to_rfkill(dev);
  523. return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_HW) ? 1 : 0 );
  524. }
  525. static DEVICE_ATTR_RO(hard);
  526. static ssize_t soft_show(struct device *dev, struct device_attribute *attr,
  527. char *buf)
  528. {
  529. struct rfkill *rfkill = to_rfkill(dev);
  530. return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_SW) ? 1 : 0 );
  531. }
  532. static ssize_t soft_store(struct device *dev, 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 = kstrtoul(buf, 0, &state);
  541. if (err)
  542. return err;
  543. if (state > 1 )
  544. return -EINVAL;
  545. mutex_lock(&rfkill_global_mutex);
  546. rfkill_set_block(rfkill, state);
  547. mutex_unlock(&rfkill_global_mutex);
  548. return count;
  549. }
  550. static DEVICE_ATTR_RW(soft);
  551. static u8 user_state_from_blocked(unsigned long state)
  552. {
  553. if (state & RFKILL_BLOCK_HW)
  554. return RFKILL_USER_STATE_HARD_BLOCKED;
  555. if (state & RFKILL_BLOCK_SW)
  556. return RFKILL_USER_STATE_SOFT_BLOCKED;
  557. return RFKILL_USER_STATE_UNBLOCKED;
  558. }
  559. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  560. char *buf)
  561. {
  562. struct rfkill *rfkill = to_rfkill(dev);
  563. return sprintf(buf, "%d\n", user_state_from_blocked(rfkill->state));
  564. }
  565. static ssize_t state_store(struct device *dev, struct device_attribute *attr,
  566. const char *buf, size_t count)
  567. {
  568. struct rfkill *rfkill = to_rfkill(dev);
  569. unsigned long state;
  570. int err;
  571. if (!capable(CAP_NET_ADMIN))
  572. return -EPERM;
  573. err = kstrtoul(buf, 0, &state);
  574. if (err)
  575. return err;
  576. if (state != RFKILL_USER_STATE_SOFT_BLOCKED &&
  577. state != RFKILL_USER_STATE_UNBLOCKED)
  578. return -EINVAL;
  579. mutex_lock(&rfkill_global_mutex);
  580. rfkill_set_block(rfkill, state == RFKILL_USER_STATE_SOFT_BLOCKED);
  581. mutex_unlock(&rfkill_global_mutex);
  582. return count;
  583. }
  584. static DEVICE_ATTR_RW(state);
  585. static ssize_t claim_show(struct device *dev, struct device_attribute *attr,
  586. char *buf)
  587. {
  588. return sprintf(buf, "%d\n", 0);
  589. }
  590. static DEVICE_ATTR_RO(claim);
  591. static struct attribute *rfkill_dev_attrs[] = {
  592. &dev_attr_name.attr,
  593. &dev_attr_type.attr,
  594. &dev_attr_index.attr,
  595. &dev_attr_persistent.attr,
  596. &dev_attr_state.attr,
  597. &dev_attr_claim.attr,
  598. &dev_attr_soft.attr,
  599. &dev_attr_hard.attr,
  600. NULL,
  601. };
  602. ATTRIBUTE_GROUPS(rfkill_dev);
  603. static void rfkill_release(struct device *dev)
  604. {
  605. struct rfkill *rfkill = to_rfkill(dev);
  606. kfree(rfkill);
  607. }
  608. static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  609. {
  610. struct rfkill *rfkill = to_rfkill(dev);
  611. unsigned long flags;
  612. u32 state;
  613. int error;
  614. error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
  615. if (error)
  616. return error;
  617. error = add_uevent_var(env, "RFKILL_TYPE=%s",
  618. rfkill_get_type_str(rfkill->type));
  619. if (error)
  620. return error;
  621. spin_lock_irqsave(&rfkill->lock, flags);
  622. state = rfkill->state;
  623. spin_unlock_irqrestore(&rfkill->lock, flags);
  624. error = add_uevent_var(env, "RFKILL_STATE=%d",
  625. user_state_from_blocked(state));
  626. return error;
  627. }
  628. void rfkill_pause_polling(struct rfkill *rfkill)
  629. {
  630. BUG_ON(!rfkill);
  631. if (!rfkill->ops->poll)
  632. return;
  633. cancel_delayed_work_sync(&rfkill->poll_work);
  634. }
  635. EXPORT_SYMBOL(rfkill_pause_polling);
  636. #ifdef CONFIG_RFKILL_PM
  637. void rfkill_resume_polling(struct rfkill *rfkill)
  638. {
  639. BUG_ON(!rfkill);
  640. if (!rfkill->ops->poll)
  641. return;
  642. queue_delayed_work(system_power_efficient_wq,
  643. &rfkill->poll_work, 0);
  644. }
  645. EXPORT_SYMBOL(rfkill_resume_polling);
  646. static int rfkill_suspend(struct device *dev, pm_message_t state)
  647. {
  648. struct rfkill *rfkill = to_rfkill(dev);
  649. rfkill_pause_polling(rfkill);
  650. return 0;
  651. }
  652. static int rfkill_resume(struct device *dev)
  653. {
  654. struct rfkill *rfkill = to_rfkill(dev);
  655. bool cur;
  656. if (!rfkill->persistent) {
  657. cur = !!(rfkill->state & RFKILL_BLOCK_SW);
  658. rfkill_set_block(rfkill, cur);
  659. }
  660. rfkill_resume_polling(rfkill);
  661. return 0;
  662. }
  663. #endif
  664. static struct class rfkill_class = {
  665. .name = "rfkill",
  666. .dev_release = rfkill_release,
  667. .dev_groups = rfkill_dev_groups,
  668. .dev_uevent = rfkill_dev_uevent,
  669. #ifdef CONFIG_RFKILL_PM
  670. .suspend = rfkill_suspend,
  671. .resume = rfkill_resume,
  672. #endif
  673. };
  674. bool rfkill_blocked(struct rfkill *rfkill)
  675. {
  676. unsigned long flags;
  677. u32 state;
  678. spin_lock_irqsave(&rfkill->lock, flags);
  679. state = rfkill->state;
  680. spin_unlock_irqrestore(&rfkill->lock, flags);
  681. return !!(state & RFKILL_BLOCK_ANY);
  682. }
  683. EXPORT_SYMBOL(rfkill_blocked);
  684. struct rfkill * __must_check rfkill_alloc(const char *name,
  685. struct device *parent,
  686. const enum rfkill_type type,
  687. const struct rfkill_ops *ops,
  688. void *ops_data)
  689. {
  690. struct rfkill *rfkill;
  691. struct device *dev;
  692. if (WARN_ON(!ops))
  693. return NULL;
  694. if (WARN_ON(!ops->set_block))
  695. return NULL;
  696. if (WARN_ON(!name))
  697. return NULL;
  698. if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
  699. return NULL;
  700. rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
  701. if (!rfkill)
  702. return NULL;
  703. spin_lock_init(&rfkill->lock);
  704. INIT_LIST_HEAD(&rfkill->node);
  705. rfkill->type = type;
  706. rfkill->name = name;
  707. rfkill->ops = ops;
  708. rfkill->data = ops_data;
  709. dev = &rfkill->dev;
  710. dev->class = &rfkill_class;
  711. dev->parent = parent;
  712. device_initialize(dev);
  713. return rfkill;
  714. }
  715. EXPORT_SYMBOL(rfkill_alloc);
  716. static void rfkill_poll(struct work_struct *work)
  717. {
  718. struct rfkill *rfkill;
  719. rfkill = container_of(work, struct rfkill, poll_work.work);
  720. /*
  721. * Poll hardware state -- driver will use one of the
  722. * rfkill_set{,_hw,_sw}_state functions and use its
  723. * return value to update the current status.
  724. */
  725. rfkill->ops->poll(rfkill, rfkill->data);
  726. queue_delayed_work(system_power_efficient_wq,
  727. &rfkill->poll_work,
  728. round_jiffies_relative(POLL_INTERVAL));
  729. }
  730. static void rfkill_uevent_work(struct work_struct *work)
  731. {
  732. struct rfkill *rfkill;
  733. rfkill = container_of(work, struct rfkill, uevent_work);
  734. mutex_lock(&rfkill_global_mutex);
  735. rfkill_event(rfkill);
  736. mutex_unlock(&rfkill_global_mutex);
  737. }
  738. static void rfkill_sync_work(struct work_struct *work)
  739. {
  740. struct rfkill *rfkill;
  741. bool cur;
  742. rfkill = container_of(work, struct rfkill, sync_work);
  743. mutex_lock(&rfkill_global_mutex);
  744. cur = rfkill_global_states[rfkill->type].cur;
  745. rfkill_set_block(rfkill, cur);
  746. mutex_unlock(&rfkill_global_mutex);
  747. }
  748. int __must_check rfkill_register(struct rfkill *rfkill)
  749. {
  750. static unsigned long rfkill_no;
  751. struct device *dev = &rfkill->dev;
  752. int error;
  753. BUG_ON(!rfkill);
  754. mutex_lock(&rfkill_global_mutex);
  755. if (rfkill->registered) {
  756. error = -EALREADY;
  757. goto unlock;
  758. }
  759. rfkill->idx = rfkill_no;
  760. dev_set_name(dev, "rfkill%lu", rfkill_no);
  761. rfkill_no++;
  762. list_add_tail(&rfkill->node, &rfkill_list);
  763. error = device_add(dev);
  764. if (error)
  765. goto remove;
  766. error = rfkill_led_trigger_register(rfkill);
  767. if (error)
  768. goto devdel;
  769. rfkill->registered = true;
  770. INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
  771. INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
  772. INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
  773. if (rfkill->ops->poll)
  774. queue_delayed_work(system_power_efficient_wq,
  775. &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);