core.c 29 KB

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