core.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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 u8 user_state_from_blocked(unsigned long state)
  515. {
  516. if (state & RFKILL_BLOCK_HW)
  517. return RFKILL_USER_STATE_HARD_BLOCKED;
  518. if (state & RFKILL_BLOCK_SW)
  519. return RFKILL_USER_STATE_SOFT_BLOCKED;
  520. return RFKILL_USER_STATE_UNBLOCKED;
  521. }
  522. static ssize_t rfkill_state_show(struct device *dev,
  523. struct device_attribute *attr,
  524. char *buf)
  525. {
  526. struct rfkill *rfkill = to_rfkill(dev);
  527. unsigned long flags;
  528. u32 state;
  529. spin_lock_irqsave(&rfkill->lock, flags);
  530. state = rfkill->state;
  531. spin_unlock_irqrestore(&rfkill->lock, flags);
  532. return sprintf(buf, "%d\n", user_state_from_blocked(state));
  533. }
  534. static ssize_t rfkill_state_store(struct device *dev,
  535. struct device_attribute *attr,
  536. const char *buf, size_t count)
  537. {
  538. struct rfkill *rfkill = to_rfkill(dev);
  539. unsigned long state;
  540. int err;
  541. if (!capable(CAP_NET_ADMIN))
  542. return -EPERM;
  543. err = strict_strtoul(buf, 0, &state);
  544. if (err)
  545. return err;
  546. if (state != RFKILL_USER_STATE_SOFT_BLOCKED &&
  547. state != RFKILL_USER_STATE_UNBLOCKED)
  548. return -EINVAL;
  549. mutex_lock(&rfkill_global_mutex);
  550. rfkill_set_block(rfkill, state == RFKILL_USER_STATE_SOFT_BLOCKED);
  551. mutex_unlock(&rfkill_global_mutex);
  552. return err ?: count;
  553. }
  554. static ssize_t rfkill_claim_show(struct device *dev,
  555. struct device_attribute *attr,
  556. char *buf)
  557. {
  558. return sprintf(buf, "%d\n", 0);
  559. }
  560. static ssize_t rfkill_claim_store(struct device *dev,
  561. struct device_attribute *attr,
  562. const char *buf, size_t count)
  563. {
  564. return -EOPNOTSUPP;
  565. }
  566. static struct device_attribute rfkill_dev_attrs[] = {
  567. __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
  568. __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
  569. __ATTR(index, S_IRUGO, rfkill_idx_show, NULL),
  570. __ATTR(persistent, S_IRUGO, rfkill_persistent_show, NULL),
  571. __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
  572. __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
  573. __ATTR_NULL
  574. };
  575. static void rfkill_release(struct device *dev)
  576. {
  577. struct rfkill *rfkill = to_rfkill(dev);
  578. kfree(rfkill);
  579. }
  580. static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  581. {
  582. struct rfkill *rfkill = to_rfkill(dev);
  583. unsigned long flags;
  584. u32 state;
  585. int error;
  586. error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
  587. if (error)
  588. return error;
  589. error = add_uevent_var(env, "RFKILL_TYPE=%s",
  590. rfkill_get_type_str(rfkill->type));
  591. if (error)
  592. return error;
  593. spin_lock_irqsave(&rfkill->lock, flags);
  594. state = rfkill->state;
  595. spin_unlock_irqrestore(&rfkill->lock, flags);
  596. error = add_uevent_var(env, "RFKILL_STATE=%d",
  597. user_state_from_blocked(state));
  598. return error;
  599. }
  600. void rfkill_pause_polling(struct rfkill *rfkill)
  601. {
  602. BUG_ON(!rfkill);
  603. if (!rfkill->ops->poll)
  604. return;
  605. cancel_delayed_work_sync(&rfkill->poll_work);
  606. }
  607. EXPORT_SYMBOL(rfkill_pause_polling);
  608. void rfkill_resume_polling(struct rfkill *rfkill)
  609. {
  610. BUG_ON(!rfkill);
  611. if (!rfkill->ops->poll)
  612. return;
  613. schedule_work(&rfkill->poll_work.work);
  614. }
  615. EXPORT_SYMBOL(rfkill_resume_polling);
  616. static int rfkill_suspend(struct device *dev, pm_message_t state)
  617. {
  618. struct rfkill *rfkill = to_rfkill(dev);
  619. rfkill_pause_polling(rfkill);
  620. return 0;
  621. }
  622. static int rfkill_resume(struct device *dev)
  623. {
  624. struct rfkill *rfkill = to_rfkill(dev);
  625. bool cur;
  626. if (!rfkill->persistent) {
  627. cur = !!(rfkill->state & RFKILL_BLOCK_SW);
  628. rfkill_set_block(rfkill, cur);
  629. }
  630. rfkill_resume_polling(rfkill);
  631. return 0;
  632. }
  633. static struct class rfkill_class = {
  634. .name = "rfkill",
  635. .dev_release = rfkill_release,
  636. .dev_attrs = rfkill_dev_attrs,
  637. .dev_uevent = rfkill_dev_uevent,
  638. .suspend = rfkill_suspend,
  639. .resume = rfkill_resume,
  640. };
  641. bool rfkill_blocked(struct rfkill *rfkill)
  642. {
  643. unsigned long flags;
  644. u32 state;
  645. spin_lock_irqsave(&rfkill->lock, flags);
  646. state = rfkill->state;
  647. spin_unlock_irqrestore(&rfkill->lock, flags);
  648. return !!(state & RFKILL_BLOCK_ANY);
  649. }
  650. EXPORT_SYMBOL(rfkill_blocked);
  651. struct rfkill * __must_check rfkill_alloc(const char *name,
  652. struct device *parent,
  653. const enum rfkill_type type,
  654. const struct rfkill_ops *ops,
  655. void *ops_data)
  656. {
  657. struct rfkill *rfkill;
  658. struct device *dev;
  659. if (WARN_ON(!ops))
  660. return NULL;
  661. if (WARN_ON(!ops->set_block))
  662. return NULL;
  663. if (WARN_ON(!name))
  664. return NULL;
  665. if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
  666. return NULL;
  667. rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL);
  668. if (!rfkill)
  669. return NULL;
  670. spin_lock_init(&rfkill->lock);
  671. INIT_LIST_HEAD(&rfkill->node);
  672. rfkill->type = type;
  673. rfkill->name = name;
  674. rfkill->ops = ops;
  675. rfkill->data = ops_data;
  676. dev = &rfkill->dev;
  677. dev->class = &rfkill_class;
  678. dev->parent = parent;
  679. device_initialize(dev);
  680. return rfkill;
  681. }
  682. EXPORT_SYMBOL(rfkill_alloc);
  683. static void rfkill_poll(struct work_struct *work)
  684. {
  685. struct rfkill *rfkill;
  686. rfkill = container_of(work, struct rfkill, poll_work.work);
  687. /*
  688. * Poll hardware state -- driver will use one of the
  689. * rfkill_set{,_hw,_sw}_state functions and use its
  690. * return value to update the current status.
  691. */
  692. rfkill->ops->poll(rfkill, rfkill->data);
  693. schedule_delayed_work(&rfkill->poll_work,
  694. round_jiffies_relative(POLL_INTERVAL));
  695. }
  696. static void rfkill_uevent_work(struct work_struct *work)
  697. {
  698. struct rfkill *rfkill;
  699. rfkill = container_of(work, struct rfkill, uevent_work);
  700. mutex_lock(&rfkill_global_mutex);
  701. rfkill_event(rfkill);
  702. mutex_unlock(&rfkill_global_mutex);
  703. }
  704. static void rfkill_sync_work(struct work_struct *work)
  705. {
  706. struct rfkill *rfkill;
  707. bool cur;
  708. rfkill = container_of(work, struct rfkill, sync_work);
  709. mutex_lock(&rfkill_global_mutex);
  710. cur = rfkill_global_states[rfkill->type].cur;
  711. rfkill_set_block(rfkill, cur);
  712. mutex_unlock(&rfkill_global_mutex);
  713. }
  714. int __must_check rfkill_register(struct rfkill *rfkill)
  715. {
  716. static unsigned long rfkill_no;
  717. struct device *dev = &rfkill->dev;
  718. int error;
  719. BUG_ON(!rfkill);
  720. mutex_lock(&rfkill_global_mutex);
  721. if (rfkill->registered) {
  722. error = -EALREADY;
  723. goto unlock;
  724. }
  725. rfkill->idx = rfkill_no;
  726. dev_set_name(dev, "rfkill%lu", rfkill_no);
  727. rfkill_no++;
  728. list_add_tail(&rfkill->node, &rfkill_list);
  729. error = device_add(dev);
  730. if (error)
  731. goto remove;
  732. error = rfkill_led_trigger_register(rfkill);
  733. if (error)
  734. goto devdel;
  735. rfkill->registered = true;
  736. INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll);
  737. INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work);
  738. INIT_WORK(&rfkill->sync_work, rfkill_sync_work);
  739. if (rfkill->ops->poll)
  740. schedule_delayed_work(&rfkill->poll_work,
  741. round_jiffies_relative(POLL_INTERVAL));
  742. if (!rfkill->persistent || rfkill_epo_lock_active) {
  743. schedule_work(&rfkill->sync_work);
  744. } else {
  745. #ifdef CONFIG_RFKILL_INPUT
  746. bool soft_blocked = !!(rfkill->state & RFKILL_BLOCK_SW);
  747. if (!atomic_read(&rfkill_input_disabled))
  748. __rfkill_switch_all(rfkill->type, soft_blocked);
  749. #endif
  750. }
  751. rfkill_send_events(rfkill, RFKILL_OP_ADD);
  752. mutex_unlock(&rfkill_global_mutex);
  753. return 0;
  754. devdel:
  755. device_del(&rfkill->dev);
  756. remove:
  757. list_del_init(&rfkill->node);
  758. unlock:
  759. mutex_unlock(&rfkill_global_mutex);
  760. return error;
  761. }
  762. EXPORT_SYMBOL(rfkill_register);
  763. void rfkill_unregister(struct rfkill *rfkill)
  764. {
  765. BUG_ON(!rfkill);
  766. if (rfkill->ops->poll)
  767. cancel_delayed_work_sync(&rfkill->poll_work);
  768. cancel_work_sync(&rfkill->uevent_work);
  769. cancel_work_sync(&rfkill->sync_work);
  770. rfkill->registered = false;
  771. device_del(&rfkill->dev);
  772. mutex_lock(&rfkill_global_mutex);
  773. rfkill_send_events(rfkill, RFKILL_OP_DEL);
  774. list_del_init(&rfkill->node);
  775. mutex_unlock(&rfkill_global_mutex);
  776. rfkill_led_trigger_unregister(rfkill);
  777. }
  778. EXPORT_SYMBOL(rfkill_unregister);
  779. void rfkill_destroy(struct rfkill *rfkill)
  780. {
  781. if (rfkill)
  782. put_device(&rfkill->dev);
  783. }
  784. EXPORT_SYMBOL(rfkill_destroy);
  785. static int rfkill_fop_open(struct inode *inode, struct file *file)
  786. {
  787. struct rfkill_data *data;
  788. struct rfkill *rfkill;
  789. struct rfkill_int_event *ev, *tmp;
  790. data = kzalloc(sizeof(*data), GFP_KERNEL);
  791. if (!data)
  792. return -ENOMEM;
  793. INIT_LIST_HEAD(&data->events);
  794. mutex_init(&data->mtx);
  795. init_waitqueue_head(&data->read_wait);
  796. mutex_lock(&rfkill_global_mutex);
  797. mutex_lock(&data->mtx);
  798. /*
  799. * start getting events from elsewhere but hold mtx to get
  800. * startup events added first
  801. */
  802. list_add(&data->list, &rfkill_fds);
  803. list_for_each_entry(rfkill, &rfkill_list, node) {
  804. ev = kzalloc(sizeof(*ev), GFP_KERNEL);
  805. if (!ev)
  806. goto free;
  807. rfkill_fill_event(&ev->ev, rfkill, RFKILL_OP_ADD);
  808. list_add_tail(&ev->list, &data->events);
  809. }
  810. mutex_unlock(&data->mtx);
  811. mutex_unlock(&rfkill_global_mutex);
  812. file->private_data = data;
  813. return nonseekable_open(inode, file);
  814. free:
  815. mutex_unlock(&data->mtx);
  816. mutex_unlock(&rfkill_global_mutex);
  817. mutex_destroy(&data->mtx);
  818. list_for_each_entry_safe(ev, tmp, &data->events, list)
  819. kfree(ev);
  820. kfree(data);
  821. return -ENOMEM;
  822. }
  823. static unsigned int rfkill_fop_poll(struct file *file, poll_table *wait)
  824. {
  825. struct rfkill_data *data = file->private_data;
  826. unsigned int res = POLLOUT | POLLWRNORM;
  827. poll_wait(file, &data->read_wait, wait);
  828. mutex_lock(&data->mtx);
  829. if (!list_empty(&data->events))
  830. res = POLLIN | POLLRDNORM;
  831. mutex_unlock(&data->mtx);
  832. return res;
  833. }
  834. static bool rfkill_readable(struct rfkill_data *data)
  835. {
  836. bool r;
  837. mutex_lock(&data->mtx);
  838. r = !list_empty(&data->events);
  839. mutex_unlock(&data->mtx);
  840. return r;
  841. }
  842. static ssize_t rfkill_fop_read(struct file *file, char __user *buf,
  843. size_t count, loff_t *pos)
  844. {
  845. struct rfkill_data *data = file->private_data;
  846. struct rfkill_int_event *ev;
  847. unsigned long sz;
  848. int ret;
  849. mutex_lock(&data->mtx);
  850. while (list_empty(&data->events)) {
  851. if (file->f_flags & O_NONBLOCK) {
  852. ret = -EAGAIN;
  853. goto out;
  854. }
  855. mutex_unlock(&data->mtx);
  856. ret = wait_event_interruptible(data->read_wait,
  857. rfkill_readable(data));
  858. mutex_lock(&data->mtx);
  859. if (ret)
  860. goto out;
  861. }
  862. ev = list_first_entry(&data->events, struct rfkill_int_event,
  863. list);
  864. sz = min_t(unsigned long, sizeof(ev->ev), count);
  865. ret = sz;
  866. if (copy_to_user(buf, &ev->ev, sz))
  867. ret = -EFAULT;
  868. list_del(&ev->list);
  869. kfree(ev);
  870. out:
  871. mutex_unlock(&data->mtx);
  872. return ret;
  873. }
  874. static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
  875. size_t count, loff_t *pos)
  876. {
  877. struct rfkill *rfkill;
  878. struct rfkill_event ev;
  879. /* we don't need the 'hard' variable but accept it */
  880. if (count < RFKILL_EVENT_SIZE_V1 - 1)
  881. return -EINVAL;
  882. /*
  883. * Copy as much data as we can accept into our 'ev' buffer,
  884. * but tell userspace how much we've copied so it can determine
  885. * our API version even in a write() call, if it cares.
  886. */
  887. count = min(count, sizeof(ev));
  888. if (copy_from_user(&ev, buf, count))
  889. return -EFAULT;
  890. if (ev.op != RFKILL_OP_CHANGE && ev.op != RFKILL_OP_CHANGE_ALL)
  891. return -EINVAL;
  892. if (ev.type >= NUM_RFKILL_TYPES)
  893. return -EINVAL;
  894. mutex_lock(&rfkill_global_mutex);
  895. if (ev.op == RFKILL_OP_CHANGE_ALL) {
  896. if (ev.type == RFKILL_TYPE_ALL) {
  897. enum rfkill_type i;
  898. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  899. rfkill_global_states[i].cur = ev.soft;
  900. } else {
  901. rfkill_global_states[ev.type].cur = ev.soft;
  902. }
  903. }
  904. list_for_each_entry(rfkill, &rfkill_list, node) {
  905. if (rfkill->idx != ev.idx && ev.op != RFKILL_OP_CHANGE_ALL)
  906. continue;
  907. if (rfkill->type != ev.type && ev.type != RFKILL_TYPE_ALL)
  908. continue;
  909. rfkill_set_block(rfkill, ev.soft);
  910. }
  911. mutex_unlock(&rfkill_global_mutex);
  912. return count;
  913. }
  914. static int rfkill_fop_release(struct inode *inode, struct file *file)
  915. {
  916. struct rfkill_data *data = file->private_data;
  917. struct rfkill_int_event *ev, *tmp;
  918. mutex_lock(&rfkill_global_mutex);
  919. list_del(&data->list);
  920. mutex_unlock(&rfkill_global_mutex);
  921. mutex_destroy(&data->mtx);
  922. list_for_each_entry_safe(ev, tmp, &data->events, list)
  923. kfree(ev);
  924. #ifdef CONFIG_RFKILL_INPUT
  925. if (data->input_handler)
  926. if (atomic_dec_return(&rfkill_input_disabled) == 0)
  927. printk(KERN_DEBUG "rfkill: input handler enabled\n");
  928. #endif
  929. kfree(data);
  930. return 0;
  931. }
  932. #ifdef CONFIG_RFKILL_INPUT
  933. static long rfkill_fop_ioctl(struct file *file, unsigned int cmd,
  934. unsigned long arg)
  935. {
  936. struct rfkill_data *data = file->private_data;
  937. if (_IOC_TYPE(cmd) != RFKILL_IOC_MAGIC)
  938. return -ENOSYS;
  939. if (_IOC_NR(cmd) != RFKILL_IOC_NOINPUT)
  940. return -ENOSYS;
  941. mutex_lock(&data->mtx);
  942. if (!data->input_handler) {
  943. if (atomic_inc_return(&rfkill_input_disabled) == 1)
  944. printk(KERN_DEBUG "rfkill: input handler disabled\n");
  945. data->input_handler = true;
  946. }
  947. mutex_unlock(&data->mtx);
  948. return 0;
  949. }
  950. #endif
  951. static const struct file_operations rfkill_fops = {
  952. .owner = THIS_MODULE,
  953. .open = rfkill_fop_open,
  954. .read = rfkill_fop_read,
  955. .write = rfkill_fop_write,
  956. .poll = rfkill_fop_poll,
  957. .release = rfkill_fop_release,
  958. #ifdef CONFIG_RFKILL_INPUT
  959. .unlocked_ioctl = rfkill_fop_ioctl,
  960. .compat_ioctl = rfkill_fop_ioctl,
  961. #endif
  962. };
  963. static struct miscdevice rfkill_miscdev = {
  964. .name = "rfkill",
  965. .fops = &rfkill_fops,
  966. .minor = MISC_DYNAMIC_MINOR,
  967. };
  968. static int __init rfkill_init(void)
  969. {
  970. int error;
  971. int i;
  972. for (i = 0; i < NUM_RFKILL_TYPES; i++)
  973. rfkill_global_states[i].cur = !rfkill_default_state;
  974. error = class_register(&rfkill_class);
  975. if (error)
  976. goto out;
  977. error = misc_register(&rfkill_miscdev);
  978. if (error) {
  979. class_unregister(&rfkill_class);
  980. goto out;
  981. }
  982. #ifdef CONFIG_RFKILL_INPUT
  983. error = rfkill_handler_init();
  984. if (error) {
  985. misc_deregister(&rfkill_miscdev);
  986. class_unregister(&rfkill_class);
  987. goto out;
  988. }
  989. #endif
  990. out:
  991. return error;
  992. }
  993. subsys_initcall(rfkill_init);
  994. static void __exit rfkill_exit(void)
  995. {
  996. #ifdef CONFIG_RFKILL_INPUT
  997. rfkill_handler_exit();
  998. #endif
  999. misc_deregister(&rfkill_miscdev);
  1000. class_unregister(&rfkill_class);
  1001. }
  1002. module_exit(rfkill_exit);