core.c 33 KB

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