core.c 28 KB

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