rt2x00rfkill.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00rfkill
  19. Abstract: rt2x00 rfkill routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/rfkill.h>
  24. #include "rt2x00.h"
  25. #include "rt2x00lib.h"
  26. static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
  27. {
  28. struct rt2x00_dev *rt2x00dev = data;
  29. int retval = 0;
  30. if (unlikely(!rt2x00dev))
  31. return 0;
  32. /*
  33. * Only continue if there are enabled interfaces.
  34. */
  35. if (!test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  36. return 0;
  37. if (state == RFKILL_STATE_UNBLOCKED) {
  38. INFO(rt2x00dev, "RFKILL event: enabling radio.\n");
  39. clear_bit(DEVICE_STATE_DISABLED_RADIO_HW, &rt2x00dev->flags);
  40. retval = rt2x00lib_enable_radio(rt2x00dev);
  41. } else if (state == RFKILL_STATE_SOFT_BLOCKED) {
  42. INFO(rt2x00dev, "RFKILL event: disabling radio.\n");
  43. set_bit(DEVICE_STATE_DISABLED_RADIO_HW, &rt2x00dev->flags);
  44. rt2x00lib_disable_radio(rt2x00dev);
  45. } else {
  46. WARNING(rt2x00dev, "RFKILL event: unknown state %d.\n", state);
  47. }
  48. return retval;
  49. }
  50. static int rt2x00rfkill_get_state(void *data, enum rfkill_state *state)
  51. {
  52. struct rt2x00_dev *rt2x00dev = data;
  53. /*
  54. * rfkill_poll reports 1 when the key has been pressed and the
  55. * radio should be blocked.
  56. */
  57. *state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev) ?
  58. RFKILL_STATE_SOFT_BLOCKED : RFKILL_STATE_UNBLOCKED;
  59. return 0;
  60. }
  61. static void rt2x00rfkill_poll(struct work_struct *work)
  62. {
  63. struct rt2x00_dev *rt2x00dev =
  64. container_of(work, struct rt2x00_dev, rfkill_work.work);
  65. enum rfkill_state state;
  66. if (!test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state) ||
  67. !test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  68. return;
  69. /*
  70. * Poll latest state and report it to rfkill who should sort
  71. * out if the state should be toggled or not.
  72. */
  73. if (!rt2x00rfkill_get_state(rt2x00dev, &state))
  74. rfkill_force_state(rt2x00dev->rfkill, state);
  75. queue_delayed_work(rt2x00dev->hw->workqueue,
  76. &rt2x00dev->rfkill_work, RFKILL_POLL_INTERVAL);
  77. }
  78. void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
  79. {
  80. if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state) ||
  81. test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
  82. return;
  83. if (rfkill_register(rt2x00dev->rfkill)) {
  84. ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
  85. return;
  86. }
  87. __set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
  88. /*
  89. * Force initial poll which will detect the initial device state,
  90. * and correctly sends the signal to the rfkill layer about this
  91. * state.
  92. */
  93. rt2x00rfkill_poll(&rt2x00dev->rfkill_work.work);
  94. }
  95. void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
  96. {
  97. if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state) ||
  98. !test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
  99. return;
  100. cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
  101. rfkill_unregister(rt2x00dev->rfkill);
  102. __clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
  103. }
  104. void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
  105. {
  106. struct device *dev = wiphy_dev(rt2x00dev->hw->wiphy);
  107. if (test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
  108. return;
  109. rt2x00dev->rfkill = rfkill_allocate(dev, RFKILL_TYPE_WLAN);
  110. if (!rt2x00dev->rfkill) {
  111. ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
  112. return;
  113. }
  114. __set_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state);
  115. rt2x00dev->rfkill->name = rt2x00dev->ops->name;
  116. rt2x00dev->rfkill->data = rt2x00dev;
  117. rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
  118. if (test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags)) {
  119. rt2x00dev->rfkill->get_state = rt2x00rfkill_get_state;
  120. rt2x00dev->rfkill->state =
  121. rt2x00dev->ops->lib->rfkill_poll(rt2x00dev) ?
  122. RFKILL_STATE_SOFT_BLOCKED : RFKILL_STATE_UNBLOCKED;
  123. } else {
  124. rt2x00dev->rfkill->state = RFKILL_STATE_UNBLOCKED;
  125. }
  126. INIT_DELAYED_WORK(&rt2x00dev->rfkill_work, rt2x00rfkill_poll);
  127. return;
  128. }
  129. void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
  130. {
  131. if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->flags))
  132. return;
  133. cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
  134. rfkill_free(rt2x00dev->rfkill);
  135. rt2x00dev->rfkill = NULL;
  136. }