core.c 27 KB

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