core.c 33 KB

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