core.c 29 KB

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