rt2x00rfkill.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_STARTED, &rt2x00dev->flags))
  36. return 0;
  37. if (state == RFKILL_STATE_UNBLOCKED) {
  38. INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
  39. __clear_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
  40. retval = rt2x00lib_enable_radio(rt2x00dev);
  41. } else if (state == RFKILL_STATE_SOFT_BLOCKED) {
  42. INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
  43. __set_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
  44. rt2x00lib_disable_radio(rt2x00dev);
  45. } else {
  46. WARNING(rt2x00dev, "Received unexpected rfkill state %d.\n",
  47. state);
  48. }
  49. return retval;
  50. }
  51. static int rt2x00rfkill_get_state(void *data, enum rfkill_state *state)
  52. {
  53. struct rt2x00_dev *rt2x00dev = data;
  54. *state = rt2x00dev->rfkill->state;
  55. return 0;
  56. }
  57. static void rt2x00rfkill_poll(struct work_struct *work)
  58. {
  59. struct rt2x00_dev *rt2x00dev =
  60. container_of(work, struct rt2x00_dev, rfkill_work.work);
  61. int state;
  62. if (!test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
  63. return;
  64. /*
  65. * rfkill_poll reports 1 when the key has been pressed and the
  66. * radio should be blocked.
  67. */
  68. state = !rt2x00dev->ops->lib->rfkill_poll(rt2x00dev) ?
  69. RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
  70. rfkill_force_state(rt2x00dev->rfkill, state);
  71. queue_delayed_work(rt2x00dev->hw->workqueue,
  72. &rt2x00dev->rfkill_work, RFKILL_POLL_INTERVAL);
  73. }
  74. void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
  75. {
  76. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
  77. !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
  78. return;
  79. if (rfkill_register(rt2x00dev->rfkill)) {
  80. ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
  81. return;
  82. }
  83. __set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
  84. /*
  85. * Force initial poll which will detect the initial device state,
  86. * and correctly sends the signal to the rfkill layer about this
  87. * state.
  88. */
  89. rt2x00rfkill_poll(&rt2x00dev->rfkill_work.work);
  90. }
  91. void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
  92. {
  93. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
  94. !test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
  95. return;
  96. cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
  97. rfkill_unregister(rt2x00dev->rfkill);
  98. __clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
  99. }
  100. void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
  101. {
  102. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  103. return;
  104. rt2x00dev->rfkill =
  105. rfkill_allocate(wiphy_dev(rt2x00dev->hw->wiphy), RFKILL_TYPE_WLAN);
  106. if (!rt2x00dev->rfkill) {
  107. ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
  108. return;
  109. }
  110. rt2x00dev->rfkill->name = rt2x00dev->ops->name;
  111. rt2x00dev->rfkill->data = rt2x00dev;
  112. rt2x00dev->rfkill->state = -1;
  113. rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
  114. rt2x00dev->rfkill->get_state = rt2x00rfkill_get_state;
  115. INIT_DELAYED_WORK(&rt2x00dev->rfkill_work, rt2x00rfkill_poll);
  116. return;
  117. }
  118. void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
  119. {
  120. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
  121. !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
  122. return;
  123. cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
  124. rfkill_free(rt2x00dev->rfkill);
  125. rt2x00dev->rfkill = NULL;
  126. }