core.c 29 KB

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