core.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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/spinlock.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/wait.h>
  32. #include <linux/poll.h>
  33. #include <linux/fs.h>
  34. #include "rfkill.h"
  35. #define POLL_INTERVAL (5 * HZ)
  36. #define RFKILL_BLOCK_HW BIT(0)
  37. #define RFKILL_BLOCK_SW BIT(1)
  38. #define RFKILL_BLOCK_SW_PREV BIT(2)
  39. #define RFKILL_BLOCK_ANY (RFKILL_BLOCK_HW |\
  40. RFKILL_BLOCK_SW |\
  41. RFKILL_BLOCK_SW_PREV)
  42. #define RFKILL_BLOCK_SW_SETCALL BIT(31)
  43. struct rfkill {
  44. spinlock_t lock;
  45. const char *name;
  46. enum rfkill_type type;
  47. unsigned long state;
  48. u32 idx;
  49. bool registered;
  50. bool suspended;
  51. const struct rfkill_ops *ops;
  52. void *data;
  53. #ifdef CONFIG_RFKILL_LEDS
  54. struct led_trigger led_trigger;
  55. const char *ledtrigname;
  56. #endif
  57. struct device dev;
  58. struct list_head node;
  59. struct delayed_work poll_work;
  60. struct work_struct uevent_work;
  61. struct work_struct sync_work;
  62. };
  63. #define to_rfkill(d) container_of(d, struct rfkill, dev)
  64. struct rfkill_int_event {
  65. struct list_head list;
  66. struct rfkill_event ev;
  67. };
  68. struct rfkill_data {
  69. struct list_head list;
  70. struct list_head events;
  71. struct mutex mtx;
  72. wait_queue_head_t read_wait;
  73. bool input_handler;
  74. };
  75. MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
  76. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  77. MODULE_DESCRIPTION("RF switch support");
  78. MODULE_LICENSE("GPL");
  79. /*
  80. * The locking here should be made much smarter, we currently have
  81. * a bit of a stupid situation because drivers might want to register
  82. * the rfkill struct under their own lock, and take this lock during
  83. * rfkill method calls -- which will cause an AB-BA deadlock situation.
  84. *
  85. * To fix that, we need to rework this code here to be mostly lock-free
  86. * and only use the mutex for list manipulations, not to protect the
  87. * various other global variables. Then we can avoid holding the mutex
  88. * around driver operations, and all is happy.
  89. */
  90. static LIST_HEAD(rfkill_list); /* list of registered rf switches */
  91. static DEFINE_MUTEX(rfkill_global_mutex);
  92. static LIST_HEAD(rfkill_fds); /* list of open fds of /dev/rfkill */
  93. static unsigned int rfkill_default_state = 1;
  94. module_param_named(default_state, rfkill_default_state, uint, 0444);
  95. MODULE_PARM_DESC(default_state,
  96. "Default initial state for all radio types, 0 = radio off");
  97. static struct {
  98. bool cur, def;
  99. } rfkill_global_states[NUM_RFKILL_TYPES];
  100. static unsigned long rfkill_states_default_locked;
  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 || rfkill->suspended)
  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. /*
  222. * Some platforms (...!) generate input events which affect the
  223. * _hard_ kill state -- whenever something tries to change the
  224. * current software state query the hardware state too.
  225. */
  226. if (rfkill->ops->query)
  227. rfkill->ops->query(rfkill, rfkill->data);
  228. spin_lock_irqsave(&rfkill->lock, flags);
  229. if (rfkill->state & RFKILL_BLOCK_SW)
  230. rfkill->state |= RFKILL_BLOCK_SW_PREV;
  231. else
  232. rfkill->state &= ~RFKILL_BLOCK_SW_PREV;
  233. if (blocked)
  234. rfkill->state |= RFKILL_BLOCK_SW;
  235. else
  236. rfkill->state &= ~RFKILL_BLOCK_SW;
  237. rfkill->state |= RFKILL_BLOCK_SW_SETCALL;
  238. spin_unlock_irqrestore(&rfkill->lock, flags);
  239. if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
  240. return;
  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].def = 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].def);
  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. void rfkill_set_global_sw_state(const enum rfkill_type type, bool blocked)
  385. {
  386. BUG_ON(type == RFKILL_TYPE_ALL);
  387. mutex_lock(&rfkill_global_mutex);
  388. /* don't allow unblock when epo */
  389. if (rfkill_epo_lock_active && !blocked)
  390. goto out;
  391. /* too late */
  392. if (rfkill_states_default_locked & BIT(type))
  393. goto out;
  394. rfkill_states_default_locked |= BIT(type);
  395. rfkill_global_states[type].cur = blocked;
  396. rfkill_global_states[type].def = blocked;
  397. out:
  398. mutex_unlock(&rfkill_global_mutex);
  399. }
  400. EXPORT_SYMBOL(rfkill_set_global_sw_state);
  401. bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked)
  402. {
  403. bool ret, change;
  404. ret = __rfkill_set_hw_state(rfkill, blocked, &change);
  405. if (!rfkill->registered)
  406. return ret;
  407. if (change)
  408. schedule_work(&rfkill->uevent_work);
  409. return ret;
  410. }
  411. EXPORT_SYMBOL(rfkill_set_hw_state);
  412. static void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  413. {
  414. u32 bit = RFKILL_BLOCK_SW;
  415. /* if in a ops->set_block right now, use other bit */
  416. if (rfkill->state & RFKILL_BLOCK_SW_SETCALL)
  417. bit = RFKILL_BLOCK_SW_PREV;
  418. if (blocked)
  419. rfkill->state |= bit;
  420. else
  421. rfkill->state &= ~bit;
  422. }
  423. bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked)
  424. {
  425. unsigned long flags;
  426. bool prev, hwblock;
  427. BUG_ON(!rfkill);
  428. spin_lock_irqsave(&rfkill->lock, flags);
  429. prev = !!(rfkill->state & RFKILL_BLOCK_SW);
  430. __rfkill_set_sw_state(rfkill, blocked);
  431. hwblock = !!(rfkill->state & RFKILL_BLOCK_HW);
  432. blocked = blocked || hwblock;
  433. spin_unlock_irqrestore(&rfkill->lock, flags);
  434. if (!rfkill->registered)
  435. return blocked;
  436. if (prev != blocked && !hwblock)
  437. schedule_work(&rfkill->uevent_work);
  438. rfkill_led_trigger_event(rfkill);
  439. return blocked;
  440. }
  441. EXPORT_SYMBOL(rfkill_set_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. spin_unlock_irqrestore(&rfkill->lock, flags);
  456. if (!rfkill->registered)
  457. return;
  458. if (swprev != sw || hwprev != hw)
  459. schedule_work(&rfkill->uevent_work);
  460. rfkill_led_trigger_event(rfkill);
  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. default:
  484. BUG();
  485. }
  486. BUILD_BUG_ON(NUM_RFKILL_TYPES != RFKILL_TYPE_WWAN + 1);
  487. }
  488. static ssize_t rfkill_type_show(struct device *dev,
  489. struct device_attribute *attr,
  490. char *buf)
  491. {
  492. struct rfkill *rfkill = to_rfkill(dev);
  493. return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
  494. }
  495. static ssize_t rfkill_idx_show(struct device *dev,
  496. struct device_attribute *attr,
  497. char *buf)
  498. {
  499. struct rfkill *rfkill = to_rfkill(dev);
  500. return sprintf(buf, "%d\n", rfkill->idx);
  501. }
  502. static u8 user_state_from_blocked(unsigned long state)
  503. {
  504. if (state & RFKILL_BLOCK_HW)
  505. return RFKILL_USER_STATE_HARD_BLOCKED;
  506. if (state & RFKILL_BLOCK_SW)
  507. return RFKILL_USER_STATE_SOFT_BLOCKED;
  508. return RFKILL_USER_STATE_UNBLOCKED;
  509. }
  510. static ssize_t rfkill_state_show(struct device *dev,
  511. struct device_attribute *attr,
  512. char *buf)
  513. {
  514. struct rfkill *rfkill = to_rfkill(dev);
  515. unsigned long flags;
  516. u32 state;
  517. spin_lock_irqsave(&rfkill->lock, flags);
  518. state = rfkill->state;
  519. spin_unlock_irqrestore(&rfkill->lock, flags);
  520. return sprintf(buf, "%d\n", user_state_from_blocked(state));
  521. }
  522. static ssize_t rfkill_state_store(struct device *dev,
  523. struct device_attribute *attr,
  524. const char *buf, size_t count)
  525. {
  526. /*
  527. * The intention was that userspace can only take control over
  528. * a given device when/if rfkill-input doesn't control it due
  529. * to user_claim. Since user_claim is currently unsupported,
  530. * we never support changing the state from userspace -- this
  531. * can be implemented again later.
  532. */
  533. return -EPERM;
  534. }
  535. static ssize_t rfkill_claim_show(struct device *dev,
  536. struct device_attribute *attr,
  537. char *buf)
  538. {
  539. return sprintf(buf, "%d\n", 0);
  540. }
  541. static ssize_t rfkill_claim_store(struct device *dev,
  542. struct device_attribute *attr,
  543. const char *buf, size_t count)
  544. {
  545. return -EOPNOTSUPP;
  546. }
  547. static struct device_attribute rfkill_dev_attrs[] = {
  548. __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
  549. __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
  550. __ATTR(index, S_IRUGO, rfkill_idx_show, NULL),
  551. __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
  552. __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
  553. __ATTR_NULL
  554. };
  555. static void rfkill_release(struct device *dev)
  556. {
  557. struct rfkill *rfkill = to_rfkill(dev);
  558. kfree(rfkill);
  559. }
  560. static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  561. {
  562. struct rfkill *rfkill = to_rfkill(dev);
  563. unsigned long flags;
  564. u32 state;
  565. int error;
  566. error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
  567. if (error)
  568. return error;
  569. error = add_uevent_var(env, "RFKILL_TYPE=%s",
  570. rfkill_get_type_str(rfkill->type));
  571. if (error)
  572. return error;
  573. spin_lock_irqsave(&rfkill->lock, flags);
  574. state = rfkill->state;
  575. spin_unlock_irqrestore(&rfkill->lock, flags);
  576. error = add_uevent_var(env, "RFKILL_STATE=%d",
  577. user_state_from_blocked(state));
  578. return error;
  579. }
  580. void rfkill_pause_polling(struct rfkill *rfkill)
  581. {
  582. BUG_ON(!rfkill);
  583. if (!rfkill->ops->poll)
  584. return;
  585. cancel_delayed_work_sync(&rfkill->poll_work);
  586. }
  587. EXPORT_SYMBOL(rfkill_pause_polling);
  588. void rfkill_resume_polling(struct rfkill *rfkill)
  589. {
  590. BUG_ON(!rfkill);
  591. if (!rfkill->ops->poll)
  592. return;
  593. schedule_work(&rfkill->poll_work.work);
  594. }
  595. EXPORT_SYMBOL(rfkill_resume_polling);
  596. static int rfkill_suspend(struct device *dev, pm_message_t state)
  597. {
  598. struct rfkill *rfkill = to_rfkill(dev);
  599. rfkill_pause_polling(rfkill);
  600. rfkill->suspended = true;
  601. return 0;
  602. }
  603. static int rfkill_resume(struct device *dev)
  604. {
  605. struct rfkill *rfkill = to_rfkill(dev);
  606. bool cur;
  607. mutex_lock(&rfkill_global_mutex);
  608. cur = rfkill_global_states[rfkill->type].cur;
  609. rfkill_set_block(rfkill, cur);
  610. mutex_unlock(&rfkill_global_mutex);
  611. rfkill->suspended = false;
  612. schedule_work(&rfkill->uevent_work);
  613. rfkill_resume_polling(rfkill);
  614. return 0;
  615. }
  616. static struct class rfkill_class = {
  617. .name = "rfkill",
  618. .dev_release = rfkill_release,
  619. .dev_attrs = rfkill_dev_attrs,
  620. .dev_uevent = rfkill_dev_uevent,
  621. .suspend = rfkill_suspend,
  622. .resume = rfkill_resume,
  623. };
  624. bool rfkill_blocked(struct rfkill *rfkill)
  625. {
  626. unsigned long flags;
  627. u32 state;
  628. spin_lock_irqsave(&rfkill->lock, flags);
  629. state = rfkill->state;
  630. spin_unlock_irqrestore(&rfkill->lock, flags);
  631. return !!(state & RFKILL_BLOCK_ANY);
  632. }
  633. EXPORT_SYMBOL(rfkill_blocked);
  634. struct rfkill * __must_check rfkill_alloc(const char *name,
  635. struct device *parent,
  636. const enum rfkill_type type,
  637. const struct rfkill_ops *ops,
  638. void *ops_data)
  639. {
  640. struct rfkill *rfkill;
  641. struct device *dev;
  642. if (WARN_ON(!ops))
  643. return NULL;
  644. if (WARN_ON(!ops->set_block))
  645. return NULL;
  646. if (WARN_ON(!name))
  647. return NULL;
  648. if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
  649. return NULL;
  650. rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
  651. if (!rfkill)
  652. return NULL;
  653. spin_lock_init(&rfkill->lock);
  654. INIT_LIST_HEAD(&rfkill->node);
  655. rfkill->type = type;
  656. rfkill->name = name;
  657. rfkill->ops = ops;
  658. rfkill->data = ops_data;
  659. dev = &rfkill->dev;
  660. dev->class = &rfkill_class;
  661. dev->parent = parent;
  662. device_initialize(dev);
  663. return rfkill;
  664. }
  665. EXPORT_SYMBOL(rfkill_alloc);
  666. static void rfkill_poll(struct work_struct *work)
  667. {
  668. struct rfkill *rfkill;
  669. rfkill = container_of(work, struct rfkill, poll_work.work);
  670. /*
  671. * Poll hardware state -- driver will use one of the
  672. * rfkill_set{,_hw,_sw}_state functions and use its
  673. * return value to update the current status.
  674. */
  675. rfkill->ops->poll(rfkill, rfkill->data);
  676. schedule_delayed_work(&rfkill->poll_work,
  677. round_jiffies_relative(POLL_INTERVAL));
  678. }
  679. static void rfkill_uevent_work(struct work_struct *work)
  680. {
  681. struct rfkill *rfkill;
  682. rfkill = container_of(work, struct rfkill, uevent_work);
  683. mutex_lock(&rfkill_global_mutex);
  684. rfkill_event(rfkill);
  685. mutex_unlock(&rfkill_global_mutex);
  686. }
  687. static void rfkill_sync_work(struct work_struct *work)
  688. {
  689. struct rfkill *rfkill;
  690. bool cur;
  691. rfkill = container_of(work, struct rfkill, sync_work);
  692. mutex_lock(&rfkill_global_mutex);
  693. cur = rfkill_global_states[rfkill->type].cur;
  694. rfkill_set_block(rfkill, cur);
  695. mutex_unlock(&rfkill_global_mutex);
  696. }
  697. int __must_check rfkill_register(struct rfkill *rfkill)
  698. {
  699. static unsigned long rfkill_no;
  700. struct device *dev = &rfkill->dev;
  701. int error;
  702. BUG_ON(!rfkill);
  703. mutex_lock(&rfkill_global_mutex);
  704. if (rfkill->registered) {
  705. error = -EALREADY;
  706. goto unlock;
  707. }
  708. rfkill->idx = rfkill_no;
  709. dev_set_name(dev, "rfkill%lu", rfkill_no);
  710. rfkill_no++;
  711. if (!(rfkill_states_default_locked & BIT(rfkill->type))) {
  712. /* first of its kind */
  713. BUILD_BUG_ON(NUM_RFKILL_TYPES >
  714. sizeof(rfkill_states_default_locked) * 8);
  715. rfkill_states_default_locked |= BIT(rfkill->type);
  716. rfkill_global_states[rfkill->type].cur =
  717. rfkill_global_states[rfkill->type].def;
  718. }
  719. list_add_tail(&rfkill->node, &rfkill_list);
  720. error = device_add(dev);
  721. if (error)
  722. goto remove;
  723. error = rfkill_led_trigger_register(rfkill);
  724. if (error)
  725. goto devdel;
  726. rfkill->registered = true;
  727. INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
  728. INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
  729. INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
  730. if (rfkill->ops->poll)
  731. schedule_delayed_work(&rfkill->poll_work,
  732. round_jiffies_relative(POLL_INTERVAL));
  733. schedule_work(&rfkill->sync_work);
  734. rfkill_send_events(rfkill, RFKILL_OP_ADD);
  735. mutex_unlock(&rfkill_global_mutex);
  736. return 0;
  737. devdel:
  738. device_del(&rfkill->dev);
  739. remove:
  740. list_del_init(&rfkill->node);
  741. unlock:
  742. mutex_unlock(&rfkill_global_mutex);
  743. return error;
  744. }
  745. EXPORT_SYMBOL(rfkill_register);
  746. void rfkill_unregister(struct rfkill *rfkill)
  747. {
  748. BUG_ON(!rfkill);
  749. if (rfkill->ops->poll)
  750. cancel_delayed_work_sync(&rfkill->poll_work);
  751. cancel_work_sync(&rfkill->uevent_work);
  752. cancel_work_sync(&rfkill->sync_work);
  753. rfkill->registered = false;
  754. device_del(&rfkill->dev);
  755. mutex_lock(&rfkill_global_mutex);
  756. rfkill_send_events(rfkill, RFKILL_OP_DEL);
  757. list_del_init(&rfkill->node);
  758. mutex_unlock(&rfkill_global_mutex);
  759. rfkill_led_trigger_unregister(rfkill);
  760. }
  761. EXPORT_SYMBOL(rfkill_unregister);
  762. void rfkill_destroy(struct rfkill *rfkill)
  763. {
  764. if (rfkill)
  765. put_device(&rfkill->dev);
  766. }
  767. EXPORT_SYMBOL(rfkill_destroy);
  768. static int rfkill_fop_open(struct inode *inode, struct file *file)
  769. {
  770. struct rfkill_data *data;
  771. struct rfkill *rfkill;
  772. struct rfkill_int_event *ev, *tmp;
  773. data = kzalloc(sizeof(*data), GFP_KERNEL);
  774. if (!data)
  775. return -ENOMEM;
  776. INIT_LIST_HEAD(&data->events);
  777. mutex_init(&data->mtx);
  778. init_waitqueue_head(&data->read_wait);
  779. mutex_lock(&rfkill_global_mutex);
  780. mutex_lock(&data->mtx);
  781. /*
  782. * start getting events from elsewhere but hold mtx to get
  783. * startup events added first
  784. */
  785. list_add(&data->list, &rfkill_fds);
  786. list_for_each_entry(rfkill, &rfkill_list, node) {
  787. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  788. if (!ev)
  789. goto free;
  790. rfkill_fill_event(&ev->ev, rfkill, RFKILL_OP_ADD);
  791. list_add_tail(&ev->list, &data->events);
  792. }
  793. mutex_unlock(&data->mtx);
  794. mutex_unlock(&rfkill_global_mutex);
  795. file->private_data = data;
  796. return nonseekable_open(inode, file);
  797. free:
  798. mutex_unlock(&data->mtx);
  799. mutex_unlock(&rfkill_global_mutex);
  800. mutex_destroy(&data->mtx);
  801. list_for_each_entry_safe(ev, tmp, &data->events, list)
  802. kfree(ev);
  803. kfree(data);
  804. return -ENOMEM;
  805. }
  806. static unsigned int rfkill_fop_poll(struct file *file, poll_table *wait)
  807. {
  808. struct rfkill_data *data = file->private_data;
  809. unsigned int res = POLLOUT | POLLWRNORM;
  810. poll_wait(file, &data->read_wait, wait);
  811. mutex_lock(&data->mtx);
  812. if (!list_empty(&data->events))
  813. res = POLLIN | POLLRDNORM;
  814. mutex_unlock(&data->mtx);
  815. return res;
  816. }
  817. static bool rfkill_readable(struct rfkill_data *data)
  818. {
  819. bool r;
  820. mutex_lock(&data->mtx);
  821. r = !list_empty(&data->events);
  822. mutex_unlock(&data->mtx);
  823. return r;
  824. }
  825. static ssize_t rfkill_fop_read(struct file *file, char __user *buf,
  826. size_t count, loff_t *pos)
  827. {
  828. struct rfkill_data *data = file->private_data;
  829. struct rfkill_int_event *ev;
  830. unsigned long sz;
  831. int ret;
  832. mutex_lock(&data->mtx);
  833. while (list_empty(&data->events)) {
  834. if (file->f_flags & O_NONBLOCK) {
  835. ret = -EAGAIN;
  836. goto out;
  837. }
  838. mutex_unlock(&data->mtx);
  839. ret = wait_event_interruptible(data->read_wait,
  840. rfkill_readable(data));
  841. mutex_lock(&data->mtx);
  842. if (ret)
  843. goto out;
  844. }
  845. ev = list_first_entry(&data->events, struct rfkill_int_event,
  846. list);
  847. sz = min_t(unsigned long, sizeof(ev->ev), count);
  848. ret = sz;
  849. if (copy_to_user(buf, &ev->ev, sz))
  850. ret = -EFAULT;
  851. list_del(&ev->list);
  852. kfree(ev);
  853. out:
  854. mutex_unlock(&data->mtx);
  855. return ret;
  856. }
  857. static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
  858. size_t count, loff_t *pos)
  859. {
  860. struct rfkill *rfkill;
  861. struct rfkill_event ev;
  862. /* we don't need the 'hard' variable but accept it */
  863. if (count < sizeof(ev) - 1)
  864. return -EINVAL;
  865. if (copy_from_user(&ev, buf, sizeof(ev) - 1))
  866. return -EFAULT;
  867. if (ev.op != RFKILL_OP_CHANGE && ev.op != RFKILL_OP_CHANGE_ALL)
  868. return -EINVAL;
  869. if (ev.type >= NUM_RFKILL_TYPES)
  870. return -EINVAL;
  871. mutex_lock(&rfkill_global_mutex);
  872. if (ev.op == RFKILL_OP_CHANGE_ALL) {
  873. if (ev.type == RFKILL_TYPE_ALL) {
  874. enum rfkill_type i;
  875. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  876. rfkill_global_states[i].cur = ev.soft;
  877. } else {
  878. rfkill_global_states[ev.type].cur = ev.soft;
  879. }
  880. }
  881. list_for_each_entry(rfkill, &rfkill_list, node) {
  882. if (rfkill->idx != ev.idx && ev.op != RFKILL_OP_CHANGE_ALL)
  883. continue;
  884. if (rfkill->type != ev.type && ev.type != RFKILL_TYPE_ALL)
  885. continue;
  886. rfkill_set_block(rfkill, ev.soft);
  887. }
  888. mutex_unlock(&rfkill_global_mutex);
  889. return count;
  890. }
  891. static int rfkill_fop_release(struct inode *inode, struct file *file)
  892. {
  893. struct rfkill_data *data = file->private_data;
  894. struct rfkill_int_event *ev, *tmp;
  895. mutex_lock(&rfkill_global_mutex);
  896. list_del(&data->list);
  897. mutex_unlock(&rfkill_global_mutex);
  898. mutex_destroy(&data->mtx);
  899. list_for_each_entry_safe(ev, tmp, &data->events, list)
  900. kfree(ev);
  901. #ifdef CONFIG_RFKILL_INPUT
  902. if (data->input_handler)
  903. if (atomic_dec_return(&rfkill_input_disabled) == 0)
  904. printk(KERN_DEBUG "rfkill: input handler enabled\n");
  905. #endif
  906. kfree(data);
  907. return 0;
  908. }
  909. #ifdef CONFIG_RFKILL_INPUT
  910. static long rfkill_fop_ioctl(struct file *file, unsigned int cmd,
  911. unsigned long arg)
  912. {
  913. struct rfkill_data *data = file->private_data;
  914. if (_IOC_TYPE(cmd) != RFKILL_IOC_MAGIC)
  915. return -ENOSYS;
  916. if (_IOC_NR(cmd) != RFKILL_IOC_NOINPUT)
  917. return -ENOSYS;
  918. mutex_lock(&data->mtx);
  919. if (!data->input_handler) {
  920. if (atomic_inc_return(&rfkill_input_disabled) == 1)
  921. printk(KERN_DEBUG "rfkill: input handler disabled\n");
  922. data->input_handler = true;
  923. }
  924. mutex_unlock(&data->mtx);
  925. return 0;
  926. }
  927. #endif
  928. static const struct file_operations rfkill_fops = {
  929. .open = rfkill_fop_open,
  930. .read = rfkill_fop_read,
  931. .write = rfkill_fop_write,
  932. .poll = rfkill_fop_poll,
  933. .release = rfkill_fop_release,
  934. #ifdef CONFIG_RFKILL_INPUT
  935. .unlocked_ioctl = rfkill_fop_ioctl,
  936. .compat_ioctl = rfkill_fop_ioctl,
  937. #endif
  938. };
  939. static struct miscdevice rfkill_miscdev = {
  940. .name = "rfkill",
  941. .fops = &rfkill_fops,
  942. .minor = MISC_DYNAMIC_MINOR,
  943. };
  944. static int __init rfkill_init(void)
  945. {
  946. int error;
  947. int i;
  948. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  949. rfkill_global_states[i].def = !rfkill_default_state;
  950. error = class_register(&rfkill_class);
  951. if (error)
  952. goto out;
  953. error = misc_register(&rfkill_miscdev);
  954. if (error) {
  955. class_unregister(&rfkill_class);
  956. goto out;
  957. }
  958. #ifdef CONFIG_RFKILL_INPUT
  959. error = rfkill_handler_init();
  960. if (error) {
  961. misc_deregister(&rfkill_miscdev);
  962. class_unregister(&rfkill_class);
  963. goto out;
  964. }
  965. #endif
  966. out:
  967. return error;
  968. }
  969. subsys_initcall(rfkill_init);
  970. static void __exit rfkill_exit(void)
  971. {
  972. #ifdef CONFIG_RFKILL_INPUT
  973. rfkill_handler_exit();
  974. #endif
  975. misc_deregister(&rfkill_miscdev);
  976. class_unregister(&rfkill_class);
  977. }
  978. module_exit(rfkill_exit);