core.c 31 KB

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