rfkill.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * Copyright (C) 2006 - 2007 Ivo van Doorn
  3. * Copyright (C) 2007 Dmitry Torokhov
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the
  17. * Free Software Foundation, Inc.,
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/capability.h>
  25. #include <linux/list.h>
  26. #include <linux/mutex.h>
  27. #include <linux/rfkill.h>
  28. /* Get declaration of rfkill_switch_all() to shut up sparse. */
  29. #include "rfkill-input.h"
  30. MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>");
  31. MODULE_VERSION("1.0");
  32. MODULE_DESCRIPTION("RF switch support");
  33. MODULE_LICENSE("GPL");
  34. static LIST_HEAD(rfkill_list); /* list of registered rf switches */
  35. static DEFINE_MUTEX(rfkill_global_mutex);
  36. static unsigned int rfkill_default_state = RFKILL_STATE_UNBLOCKED;
  37. module_param_named(default_state, rfkill_default_state, uint, 0444);
  38. MODULE_PARM_DESC(default_state,
  39. "Default initial state for all radio types, 0 = radio off");
  40. struct rfkill_gsw_state {
  41. enum rfkill_state current_state;
  42. enum rfkill_state default_state;
  43. };
  44. static struct rfkill_gsw_state rfkill_global_states[RFKILL_TYPE_MAX];
  45. static unsigned long rfkill_states_lockdflt[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
  46. static bool rfkill_epo_lock_active;
  47. static BLOCKING_NOTIFIER_HEAD(rfkill_notifier_list);
  48. /**
  49. * register_rfkill_notifier - Add notifier to rfkill notifier chain
  50. * @nb: pointer to the new entry to add to the chain
  51. *
  52. * See blocking_notifier_chain_register() for return value and further
  53. * observations.
  54. *
  55. * Adds a notifier to the rfkill notifier chain. The chain will be
  56. * called with a pointer to the relevant rfkill structure as a parameter,
  57. * refer to include/linux/rfkill.h for the possible events.
  58. *
  59. * Notifiers added to this chain are to always return NOTIFY_DONE. This
  60. * chain is a blocking notifier chain: notifiers can sleep.
  61. *
  62. * Calls to this chain may have been done through a workqueue. One must
  63. * assume unordered asynchronous behaviour, there is no way to know if
  64. * actions related to the event that generated the notification have been
  65. * carried out already.
  66. */
  67. int register_rfkill_notifier(struct notifier_block *nb)
  68. {
  69. BUG_ON(!nb);
  70. return blocking_notifier_chain_register(&rfkill_notifier_list, nb);
  71. }
  72. EXPORT_SYMBOL_GPL(register_rfkill_notifier);
  73. /**
  74. * unregister_rfkill_notifier - remove notifier from rfkill notifier chain
  75. * @nb: pointer to the entry to remove from the chain
  76. *
  77. * See blocking_notifier_chain_unregister() for return value and further
  78. * observations.
  79. *
  80. * Removes a notifier from the rfkill notifier chain.
  81. */
  82. int unregister_rfkill_notifier(struct notifier_block *nb)
  83. {
  84. BUG_ON(!nb);
  85. return blocking_notifier_chain_unregister(&rfkill_notifier_list, nb);
  86. }
  87. EXPORT_SYMBOL_GPL(unregister_rfkill_notifier);
  88. static void rfkill_led_trigger(struct rfkill *rfkill,
  89. enum rfkill_state state)
  90. {
  91. #ifdef CONFIG_RFKILL_LEDS
  92. struct led_trigger *led = &rfkill->led_trigger;
  93. if (!led->name)
  94. return;
  95. if (state != RFKILL_STATE_UNBLOCKED)
  96. led_trigger_event(led, LED_OFF);
  97. else
  98. led_trigger_event(led, LED_FULL);
  99. #endif /* CONFIG_RFKILL_LEDS */
  100. }
  101. #ifdef CONFIG_RFKILL_LEDS
  102. static void rfkill_led_trigger_activate(struct led_classdev *led)
  103. {
  104. struct rfkill *rfkill = container_of(led->trigger,
  105. struct rfkill, led_trigger);
  106. rfkill_led_trigger(rfkill, rfkill->state);
  107. }
  108. #endif /* CONFIG_RFKILL_LEDS */
  109. static void notify_rfkill_state_change(struct rfkill *rfkill)
  110. {
  111. rfkill_led_trigger(rfkill, rfkill->state);
  112. blocking_notifier_call_chain(&rfkill_notifier_list,
  113. RFKILL_STATE_CHANGED,
  114. rfkill);
  115. }
  116. static void update_rfkill_state(struct rfkill *rfkill)
  117. {
  118. enum rfkill_state newstate, oldstate;
  119. if (rfkill->get_state) {
  120. mutex_lock(&rfkill->mutex);
  121. if (!rfkill->get_state(rfkill->data, &newstate)) {
  122. oldstate = rfkill->state;
  123. rfkill->state = newstate;
  124. if (oldstate != newstate)
  125. notify_rfkill_state_change(rfkill);
  126. }
  127. mutex_unlock(&rfkill->mutex);
  128. }
  129. }
  130. /**
  131. * rfkill_toggle_radio - wrapper for toggle_radio hook
  132. * @rfkill: the rfkill struct to use
  133. * @force: calls toggle_radio even if cache says it is not needed,
  134. * and also makes sure notifications of the state will be
  135. * sent even if it didn't change
  136. * @state: the new state to call toggle_radio() with
  137. *
  138. * Calls rfkill->toggle_radio, enforcing the API for toggle_radio
  139. * calls and handling all the red tape such as issuing notifications
  140. * if the call is successful.
  141. *
  142. * Suspended devices are not touched at all, and -EAGAIN is returned.
  143. *
  144. * Note that the @force parameter cannot override a (possibly cached)
  145. * state of RFKILL_STATE_HARD_BLOCKED. Any device making use of
  146. * RFKILL_STATE_HARD_BLOCKED implements either get_state() or
  147. * rfkill_force_state(), so the cache either is bypassed or valid.
  148. *
  149. * Note that we do call toggle_radio for RFKILL_STATE_SOFT_BLOCKED
  150. * even if the radio is in RFKILL_STATE_HARD_BLOCKED state, so as to
  151. * give the driver a hint that it should double-BLOCK the transmitter.
  152. *
  153. * Caller must have acquired rfkill->mutex.
  154. */
  155. static int rfkill_toggle_radio(struct rfkill *rfkill,
  156. enum rfkill_state state,
  157. int force)
  158. {
  159. int retval = 0;
  160. enum rfkill_state oldstate, newstate;
  161. if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP))
  162. return -EBUSY;
  163. oldstate = rfkill->state;
  164. if (rfkill->get_state && !force &&
  165. !rfkill->get_state(rfkill->data, &newstate))
  166. rfkill->state = newstate;
  167. switch (state) {
  168. case RFKILL_STATE_HARD_BLOCKED:
  169. /* typically happens when refreshing hardware state,
  170. * such as on resume */
  171. state = RFKILL_STATE_SOFT_BLOCKED;
  172. break;
  173. case RFKILL_STATE_UNBLOCKED:
  174. /* force can't override this, only rfkill_force_state() can */
  175. if (rfkill->state == RFKILL_STATE_HARD_BLOCKED)
  176. return -EPERM;
  177. break;
  178. case RFKILL_STATE_SOFT_BLOCKED:
  179. /* nothing to do, we want to give drivers the hint to double
  180. * BLOCK even a transmitter that is already in state
  181. * RFKILL_STATE_HARD_BLOCKED */
  182. break;
  183. default:
  184. WARN(1, KERN_WARNING
  185. "rfkill: illegal state %d passed as parameter "
  186. "to rfkill_toggle_radio\n", state);
  187. return -EINVAL;
  188. }
  189. if (force || state != rfkill->state) {
  190. retval = rfkill->toggle_radio(rfkill->data, state);
  191. /* never allow a HARD->SOFT downgrade! */
  192. if (!retval && rfkill->state != RFKILL_STATE_HARD_BLOCKED)
  193. rfkill->state = state;
  194. }
  195. if (force || rfkill->state != oldstate)
  196. notify_rfkill_state_change(rfkill);
  197. return retval;
  198. }
  199. /**
  200. * __rfkill_switch_all - Toggle state of all switches of given type
  201. * @type: type of interfaces to be affected
  202. * @state: the new state
  203. *
  204. * This function toggles the state of all switches of given type,
  205. * unless a specific switch is claimed by userspace (in which case,
  206. * that switch is left alone) or suspended.
  207. *
  208. * Caller must have acquired rfkill_global_mutex.
  209. */
  210. static void __rfkill_switch_all(const enum rfkill_type type,
  211. const enum rfkill_state state)
  212. {
  213. struct rfkill *rfkill;
  214. if (WARN((state >= RFKILL_STATE_MAX || type >= RFKILL_TYPE_MAX),
  215. KERN_WARNING
  216. "rfkill: illegal state %d or type %d "
  217. "passed as parameter to __rfkill_switch_all\n",
  218. state, type))
  219. return;
  220. rfkill_global_states[type].current_state = state;
  221. list_for_each_entry(rfkill, &rfkill_list, node) {
  222. if ((!rfkill->user_claim) && (rfkill->type == type)) {
  223. mutex_lock(&rfkill->mutex);
  224. rfkill_toggle_radio(rfkill, state, 0);
  225. mutex_unlock(&rfkill->mutex);
  226. }
  227. }
  228. }
  229. /**
  230. * rfkill_switch_all - Toggle state of all switches of given type
  231. * @type: type of interfaces to be affected
  232. * @state: the new state
  233. *
  234. * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state).
  235. * Please refer to __rfkill_switch_all() for details.
  236. *
  237. * Does nothing if the EPO lock is active.
  238. */
  239. void rfkill_switch_all(enum rfkill_type type, enum rfkill_state state)
  240. {
  241. mutex_lock(&rfkill_global_mutex);
  242. if (!rfkill_epo_lock_active)
  243. __rfkill_switch_all(type, state);
  244. mutex_unlock(&rfkill_global_mutex);
  245. }
  246. EXPORT_SYMBOL(rfkill_switch_all);
  247. /**
  248. * rfkill_epo - emergency power off all transmitters
  249. *
  250. * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED,
  251. * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex.
  252. *
  253. * The global state before the EPO is saved and can be restored later
  254. * using rfkill_restore_states().
  255. */
  256. void rfkill_epo(void)
  257. {
  258. struct rfkill *rfkill;
  259. int i;
  260. mutex_lock(&rfkill_global_mutex);
  261. rfkill_epo_lock_active = true;
  262. list_for_each_entry(rfkill, &rfkill_list, node) {
  263. mutex_lock(&rfkill->mutex);
  264. rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
  265. mutex_unlock(&rfkill->mutex);
  266. }
  267. for (i = 0; i < RFKILL_TYPE_MAX; i++) {
  268. rfkill_global_states[i].default_state =
  269. rfkill_global_states[i].current_state;
  270. rfkill_global_states[i].current_state =
  271. RFKILL_STATE_SOFT_BLOCKED;
  272. }
  273. mutex_unlock(&rfkill_global_mutex);
  274. }
  275. EXPORT_SYMBOL_GPL(rfkill_epo);
  276. /**
  277. * rfkill_restore_states - restore global states
  278. *
  279. * Restore (and sync switches to) the global state from the
  280. * states in rfkill_default_states. This can undo the effects of
  281. * a call to rfkill_epo().
  282. */
  283. void rfkill_restore_states(void)
  284. {
  285. int i;
  286. mutex_lock(&rfkill_global_mutex);
  287. rfkill_epo_lock_active = false;
  288. for (i = 0; i < RFKILL_TYPE_MAX; i++)
  289. __rfkill_switch_all(i, rfkill_global_states[i].default_state);
  290. mutex_unlock(&rfkill_global_mutex);
  291. }
  292. EXPORT_SYMBOL_GPL(rfkill_restore_states);
  293. /**
  294. * rfkill_remove_epo_lock - unlock state changes
  295. *
  296. * Used by rfkill-input manually unlock state changes, when
  297. * the EPO switch is deactivated.
  298. */
  299. void rfkill_remove_epo_lock(void)
  300. {
  301. mutex_lock(&rfkill_global_mutex);
  302. rfkill_epo_lock_active = false;
  303. mutex_unlock(&rfkill_global_mutex);
  304. }
  305. EXPORT_SYMBOL_GPL(rfkill_remove_epo_lock);
  306. /**
  307. * rfkill_is_epo_lock_active - returns true EPO is active
  308. *
  309. * Returns 0 (false) if there is NOT an active EPO contidion,
  310. * and 1 (true) if there is an active EPO contition, which
  311. * locks all radios in one of the BLOCKED states.
  312. *
  313. * Can be called in atomic context.
  314. */
  315. bool rfkill_is_epo_lock_active(void)
  316. {
  317. return rfkill_epo_lock_active;
  318. }
  319. EXPORT_SYMBOL_GPL(rfkill_is_epo_lock_active);
  320. /**
  321. * rfkill_get_global_state - returns global state for a type
  322. * @type: the type to get the global state of
  323. *
  324. * Returns the current global state for a given wireless
  325. * device type.
  326. */
  327. enum rfkill_state rfkill_get_global_state(const enum rfkill_type type)
  328. {
  329. return rfkill_global_states[type].current_state;
  330. }
  331. EXPORT_SYMBOL_GPL(rfkill_get_global_state);
  332. /**
  333. * rfkill_force_state - Force the internal rfkill radio state
  334. * @rfkill: pointer to the rfkill class to modify.
  335. * @state: the current radio state the class should be forced to.
  336. *
  337. * This function updates the internal state of the radio cached
  338. * by the rfkill class. It should be used when the driver gets
  339. * a notification by the firmware/hardware of the current *real*
  340. * state of the radio rfkill switch.
  341. *
  342. * Devices which are subject to external changes on their rfkill
  343. * state (such as those caused by a hardware rfkill line) MUST
  344. * have their driver arrange to call rfkill_force_state() as soon
  345. * as possible after such a change.
  346. *
  347. * This function may not be called from an atomic context.
  348. */
  349. int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state)
  350. {
  351. enum rfkill_state oldstate;
  352. BUG_ON(!rfkill);
  353. if (WARN((state >= RFKILL_STATE_MAX),
  354. KERN_WARNING
  355. "rfkill: illegal state %d passed as parameter "
  356. "to rfkill_force_state\n", state))
  357. return -EINVAL;
  358. mutex_lock(&rfkill->mutex);
  359. oldstate = rfkill->state;
  360. rfkill->state = state;
  361. if (state != oldstate)
  362. notify_rfkill_state_change(rfkill);
  363. mutex_unlock(&rfkill->mutex);
  364. return 0;
  365. }
  366. EXPORT_SYMBOL(rfkill_force_state);
  367. static ssize_t rfkill_name_show(struct device *dev,
  368. struct device_attribute *attr,
  369. char *buf)
  370. {
  371. struct rfkill *rfkill = to_rfkill(dev);
  372. return sprintf(buf, "%s\n", rfkill->name);
  373. }
  374. static const char *rfkill_get_type_str(enum rfkill_type type)
  375. {
  376. switch (type) {
  377. case RFKILL_TYPE_WLAN:
  378. return "wlan";
  379. case RFKILL_TYPE_BLUETOOTH:
  380. return "bluetooth";
  381. case RFKILL_TYPE_UWB:
  382. return "ultrawideband";
  383. case RFKILL_TYPE_WIMAX:
  384. return "wimax";
  385. case RFKILL_TYPE_WWAN:
  386. return "wwan";
  387. default:
  388. BUG();
  389. }
  390. }
  391. static ssize_t rfkill_type_show(struct device *dev,
  392. struct device_attribute *attr,
  393. char *buf)
  394. {
  395. struct rfkill *rfkill = to_rfkill(dev);
  396. return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
  397. }
  398. static ssize_t rfkill_state_show(struct device *dev,
  399. struct device_attribute *attr,
  400. char *buf)
  401. {
  402. struct rfkill *rfkill = to_rfkill(dev);
  403. update_rfkill_state(rfkill);
  404. return sprintf(buf, "%d\n", rfkill->state);
  405. }
  406. static ssize_t rfkill_state_store(struct device *dev,
  407. struct device_attribute *attr,
  408. const char *buf, size_t count)
  409. {
  410. struct rfkill *rfkill = to_rfkill(dev);
  411. unsigned long state;
  412. int error;
  413. if (!capable(CAP_NET_ADMIN))
  414. return -EPERM;
  415. error = strict_strtoul(buf, 0, &state);
  416. if (error)
  417. return error;
  418. /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
  419. if (state != RFKILL_STATE_UNBLOCKED &&
  420. state != RFKILL_STATE_SOFT_BLOCKED)
  421. return -EINVAL;
  422. error = mutex_lock_killable(&rfkill->mutex);
  423. if (error)
  424. return error;
  425. if (!rfkill_epo_lock_active)
  426. error = rfkill_toggle_radio(rfkill, state, 0);
  427. else
  428. error = -EPERM;
  429. mutex_unlock(&rfkill->mutex);
  430. return error ? error : count;
  431. }
  432. static ssize_t rfkill_claim_show(struct device *dev,
  433. struct device_attribute *attr,
  434. char *buf)
  435. {
  436. struct rfkill *rfkill = to_rfkill(dev);
  437. return sprintf(buf, "%d\n", rfkill->user_claim);
  438. }
  439. static ssize_t rfkill_claim_store(struct device *dev,
  440. struct device_attribute *attr,
  441. const char *buf, size_t count)
  442. {
  443. struct rfkill *rfkill = to_rfkill(dev);
  444. unsigned long claim_tmp;
  445. bool claim;
  446. int error;
  447. if (!capable(CAP_NET_ADMIN))
  448. return -EPERM;
  449. if (rfkill->user_claim_unsupported)
  450. return -EOPNOTSUPP;
  451. error = strict_strtoul(buf, 0, &claim_tmp);
  452. if (error)
  453. return error;
  454. claim = !!claim_tmp;
  455. /*
  456. * Take the global lock to make sure the kernel is not in
  457. * the middle of rfkill_switch_all
  458. */
  459. error = mutex_lock_killable(&rfkill_global_mutex);
  460. if (error)
  461. return error;
  462. if (rfkill->user_claim != claim) {
  463. if (!claim && !rfkill_epo_lock_active) {
  464. mutex_lock(&rfkill->mutex);
  465. rfkill_toggle_radio(rfkill,
  466. rfkill_global_states[rfkill->type].current_state,
  467. 0);
  468. mutex_unlock(&rfkill->mutex);
  469. }
  470. rfkill->user_claim = claim;
  471. }
  472. mutex_unlock(&rfkill_global_mutex);
  473. return error ? error : count;
  474. }
  475. static struct device_attribute rfkill_dev_attrs[] = {
  476. __ATTR(name, S_IRUGO, rfkill_name_show, NULL),
  477. __ATTR(type, S_IRUGO, rfkill_type_show, NULL),
  478. __ATTR(state, S_IRUGO|S_IWUSR, rfkill_state_show, rfkill_state_store),
  479. __ATTR(claim, S_IRUGO|S_IWUSR, rfkill_claim_show, rfkill_claim_store),
  480. __ATTR_NULL
  481. };
  482. static void rfkill_release(struct device *dev)
  483. {
  484. struct rfkill *rfkill = to_rfkill(dev);
  485. kfree(rfkill);
  486. module_put(THIS_MODULE);
  487. }
  488. #ifdef CONFIG_PM
  489. static int rfkill_suspend(struct device *dev, pm_message_t state)
  490. {
  491. struct rfkill *rfkill = to_rfkill(dev);
  492. /* mark class device as suspended */
  493. if (dev->power.power_state.event != state.event)
  494. dev->power.power_state = state;
  495. /* store state for the resume handler */
  496. rfkill->state_for_resume = rfkill->state;
  497. return 0;
  498. }
  499. static int rfkill_resume(struct device *dev)
  500. {
  501. struct rfkill *rfkill = to_rfkill(dev);
  502. if (dev->power.power_state.event != PM_EVENT_ON) {
  503. mutex_lock(&rfkill->mutex);
  504. dev->power.power_state.event = PM_EVENT_ON;
  505. /*
  506. * If we are under EPO, kick transmitter offline,
  507. * otherwise restore to pre-suspend state.
  508. *
  509. * Issue a notification in any case
  510. */
  511. rfkill_toggle_radio(rfkill,
  512. rfkill_epo_lock_active ?
  513. RFKILL_STATE_SOFT_BLOCKED :
  514. rfkill->state_for_resume,
  515. 1);
  516. mutex_unlock(&rfkill->mutex);
  517. }
  518. return 0;
  519. }
  520. #else
  521. #define rfkill_suspend NULL
  522. #define rfkill_resume NULL
  523. #endif
  524. static int rfkill_blocking_uevent_notifier(struct notifier_block *nb,
  525. unsigned long eventid,
  526. void *data)
  527. {
  528. struct rfkill *rfkill = (struct rfkill *)data;
  529. switch (eventid) {
  530. case RFKILL_STATE_CHANGED:
  531. kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE);
  532. break;
  533. default:
  534. break;
  535. }
  536. return NOTIFY_DONE;
  537. }
  538. static struct notifier_block rfkill_blocking_uevent_nb = {
  539. .notifier_call = rfkill_blocking_uevent_notifier,
  540. .priority = 0,
  541. };
  542. static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  543. {
  544. struct rfkill *rfkill = to_rfkill(dev);
  545. int error;
  546. error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name);
  547. if (error)
  548. return error;
  549. error = add_uevent_var(env, "RFKILL_TYPE=%s",
  550. rfkill_get_type_str(rfkill->type));
  551. if (error)
  552. return error;
  553. error = add_uevent_var(env, "RFKILL_STATE=%d", rfkill->state);
  554. return error;
  555. }
  556. static struct class rfkill_class = {
  557. .name = "rfkill",
  558. .dev_release = rfkill_release,
  559. .dev_attrs = rfkill_dev_attrs,
  560. .suspend = rfkill_suspend,
  561. .resume = rfkill_resume,
  562. .dev_uevent = rfkill_dev_uevent,
  563. };
  564. static int rfkill_check_duplicity(const struct rfkill *rfkill)
  565. {
  566. struct rfkill *p;
  567. unsigned long seen[BITS_TO_LONGS(RFKILL_TYPE_MAX)];
  568. memset(seen, 0, sizeof(seen));
  569. list_for_each_entry(p, &rfkill_list, node) {
  570. if (WARN((p == rfkill), KERN_WARNING
  571. "rfkill: illegal attempt to register "
  572. "an already registered rfkill struct\n"))
  573. return -EEXIST;
  574. set_bit(p->type, seen);
  575. }
  576. /* 0: first switch of its kind */
  577. return (test_bit(rfkill->type, seen)) ? 1 : 0;
  578. }
  579. static int rfkill_add_switch(struct rfkill *rfkill)
  580. {
  581. int error;
  582. mutex_lock(&rfkill_global_mutex);
  583. error = rfkill_check_duplicity(rfkill);
  584. if (error < 0)
  585. goto unlock_out;
  586. if (!error) {
  587. /* lock default after first use */
  588. set_bit(rfkill->type, rfkill_states_lockdflt);
  589. rfkill_global_states[rfkill->type].current_state =
  590. rfkill_global_states[rfkill->type].default_state;
  591. }
  592. rfkill_toggle_radio(rfkill,
  593. rfkill_global_states[rfkill->type].current_state,
  594. 0);
  595. list_add_tail(&rfkill->node, &rfkill_list);
  596. error = 0;
  597. unlock_out:
  598. mutex_unlock(&rfkill_global_mutex);
  599. return error;
  600. }
  601. static void rfkill_remove_switch(struct rfkill *rfkill)
  602. {
  603. mutex_lock(&rfkill_global_mutex);
  604. list_del_init(&rfkill->node);
  605. mutex_unlock(&rfkill_global_mutex);
  606. mutex_lock(&rfkill->mutex);
  607. rfkill_toggle_radio(rfkill, RFKILL_STATE_SOFT_BLOCKED, 1);
  608. mutex_unlock(&rfkill->mutex);
  609. }
  610. /**
  611. * rfkill_allocate - allocate memory for rfkill structure.
  612. * @parent: device that has rf switch on it
  613. * @type: type of the switch (RFKILL_TYPE_*)
  614. *
  615. * This function should be called by the network driver when it needs
  616. * rfkill structure. Once the structure is allocated the driver should
  617. * finish its initialization by setting the name, private data, enable_radio
  618. * and disable_radio methods and then register it with rfkill_register().
  619. *
  620. * NOTE: If registration fails the structure shoudl be freed by calling
  621. * rfkill_free() otherwise rfkill_unregister() should be used.
  622. */
  623. struct rfkill * __must_check rfkill_allocate(struct device *parent,
  624. enum rfkill_type type)
  625. {
  626. struct rfkill *rfkill;
  627. struct device *dev;
  628. if (WARN((type >= RFKILL_TYPE_MAX),
  629. KERN_WARNING
  630. "rfkill: illegal type %d passed as parameter "
  631. "to rfkill_allocate\n", type))
  632. return NULL;
  633. rfkill = kzalloc(sizeof(struct rfkill), GFP_KERNEL);
  634. if (!rfkill)
  635. return NULL;
  636. mutex_init(&rfkill->mutex);
  637. INIT_LIST_HEAD(&rfkill->node);
  638. rfkill->type = type;
  639. dev = &rfkill->dev;
  640. dev->class = &rfkill_class;
  641. dev->parent = parent;
  642. device_initialize(dev);
  643. __module_get(THIS_MODULE);
  644. return rfkill;
  645. }
  646. EXPORT_SYMBOL(rfkill_allocate);
  647. /**
  648. * rfkill_free - Mark rfkill structure for deletion
  649. * @rfkill: rfkill structure to be destroyed
  650. *
  651. * Decrements reference count of the rfkill structure so it is destroyed.
  652. * Note that rfkill_free() should _not_ be called after rfkill_unregister().
  653. */
  654. void rfkill_free(struct rfkill *rfkill)
  655. {
  656. if (rfkill)
  657. put_device(&rfkill->dev);
  658. }
  659. EXPORT_SYMBOL(rfkill_free);
  660. static void rfkill_led_trigger_register(struct rfkill *rfkill)
  661. {
  662. #ifdef CONFIG_RFKILL_LEDS
  663. int error;
  664. if (!rfkill->led_trigger.name)
  665. rfkill->led_trigger.name = dev_name(&rfkill->dev);
  666. if (!rfkill->led_trigger.activate)
  667. rfkill->led_trigger.activate = rfkill_led_trigger_activate;
  668. error = led_trigger_register(&rfkill->led_trigger);
  669. if (error)
  670. rfkill->led_trigger.name = NULL;
  671. #endif /* CONFIG_RFKILL_LEDS */
  672. }
  673. static void rfkill_led_trigger_unregister(struct rfkill *rfkill)
  674. {
  675. #ifdef CONFIG_RFKILL_LEDS
  676. if (rfkill->led_trigger.name) {
  677. led_trigger_unregister(&rfkill->led_trigger);
  678. rfkill->led_trigger.name = NULL;
  679. }
  680. #endif
  681. }
  682. /**
  683. * rfkill_register - Register a rfkill structure.
  684. * @rfkill: rfkill structure to be registered
  685. *
  686. * This function should be called by the network driver when the rfkill
  687. * structure needs to be registered. Immediately from registration the
  688. * switch driver should be able to service calls to toggle_radio.
  689. */
  690. int __must_check rfkill_register(struct rfkill *rfkill)
  691. {
  692. static atomic_t rfkill_no = ATOMIC_INIT(0);
  693. struct device *dev = &rfkill->dev;
  694. int error;
  695. if (WARN((!rfkill || !rfkill->toggle_radio ||
  696. rfkill->type >= RFKILL_TYPE_MAX ||
  697. rfkill->state >= RFKILL_STATE_MAX),
  698. KERN_WARNING
  699. "rfkill: attempt to register a "
  700. "badly initialized rfkill struct\n"))
  701. return -EINVAL;
  702. dev_set_name(dev, "rfkill%ld", (long)atomic_inc_return(&rfkill_no) - 1);
  703. rfkill_led_trigger_register(rfkill);
  704. error = rfkill_add_switch(rfkill);
  705. if (error) {
  706. rfkill_led_trigger_unregister(rfkill);
  707. return error;
  708. }
  709. error = device_add(dev);
  710. if (error) {
  711. rfkill_remove_switch(rfkill);
  712. rfkill_led_trigger_unregister(rfkill);
  713. return error;
  714. }
  715. return 0;
  716. }
  717. EXPORT_SYMBOL(rfkill_register);
  718. /**
  719. * rfkill_unregister - Unregister a rfkill structure.
  720. * @rfkill: rfkill structure to be unregistered
  721. *
  722. * This function should be called by the network driver during device
  723. * teardown to destroy rfkill structure. Note that rfkill_free() should
  724. * _not_ be called after rfkill_unregister().
  725. */
  726. void rfkill_unregister(struct rfkill *rfkill)
  727. {
  728. BUG_ON(!rfkill);
  729. device_del(&rfkill->dev);
  730. rfkill_remove_switch(rfkill);
  731. rfkill_led_trigger_unregister(rfkill);
  732. put_device(&rfkill->dev);
  733. }
  734. EXPORT_SYMBOL(rfkill_unregister);
  735. /**
  736. * rfkill_set_default - set initial value for a switch type
  737. * @type - the type of switch to set the default state of
  738. * @state - the new default state for that group of switches
  739. *
  740. * Sets the initial state rfkill should use for a given type.
  741. * The following initial states are allowed: RFKILL_STATE_SOFT_BLOCKED
  742. * and RFKILL_STATE_UNBLOCKED.
  743. *
  744. * This function is meant to be used by platform drivers for platforms
  745. * that can save switch state across power down/reboot.
  746. *
  747. * The default state for each switch type can be changed exactly once.
  748. * After a switch of that type is registered, the default state cannot
  749. * be changed anymore. This guards against multiple drivers it the
  750. * same platform trying to set the initial switch default state, which
  751. * is not allowed.
  752. *
  753. * Returns -EPERM if the state has already been set once or is in use,
  754. * so drivers likely want to either ignore or at most printk(KERN_NOTICE)
  755. * if this function returns -EPERM.
  756. *
  757. * Returns 0 if the new default state was set, or an error if it
  758. * could not be set.
  759. */
  760. int rfkill_set_default(enum rfkill_type type, enum rfkill_state state)
  761. {
  762. int error;
  763. if (WARN((type >= RFKILL_TYPE_MAX ||
  764. (state != RFKILL_STATE_SOFT_BLOCKED &&
  765. state != RFKILL_STATE_UNBLOCKED)),
  766. KERN_WARNING
  767. "rfkill: illegal state %d or type %d passed as "
  768. "parameter to rfkill_set_default\n", state, type))
  769. return -EINVAL;
  770. mutex_lock(&rfkill_global_mutex);
  771. if (!test_and_set_bit(type, rfkill_states_lockdflt)) {
  772. rfkill_global_states[type].default_state = state;
  773. rfkill_global_states[type].current_state = state;
  774. error = 0;
  775. } else
  776. error = -EPERM;
  777. mutex_unlock(&rfkill_global_mutex);
  778. return error;
  779. }
  780. EXPORT_SYMBOL_GPL(rfkill_set_default);
  781. /*
  782. * Rfkill module initialization/deinitialization.
  783. */
  784. static int __init rfkill_init(void)
  785. {
  786. int error;
  787. int i;
  788. /* RFKILL_STATE_HARD_BLOCKED is illegal here... */
  789. if (rfkill_default_state != RFKILL_STATE_SOFT_BLOCKED &&
  790. rfkill_default_state != RFKILL_STATE_UNBLOCKED)
  791. return -EINVAL;
  792. for (i = 0; i < RFKILL_TYPE_MAX; i++)
  793. rfkill_global_states[i].default_state = rfkill_default_state;
  794. error = class_register(&rfkill_class);
  795. if (error) {
  796. printk(KERN_ERR "rfkill: unable to register rfkill class\n");
  797. return error;
  798. }
  799. register_rfkill_notifier(&rfkill_blocking_uevent_nb);
  800. return 0;
  801. }
  802. static void __exit rfkill_exit(void)
  803. {
  804. unregister_rfkill_notifier(&rfkill_blocking_uevent_nb);
  805. class_unregister(&rfkill_class);
  806. }
  807. subsys_initcall(rfkill_init);
  808. module_exit(rfkill_exit);