rfkill.c 22 KB

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