core.c 33 KB

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