core.c 29 KB

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