core.c 32 KB

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