rfkill.c 23 KB

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