core.c 29 KB

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