rt2x00rfkill.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/input-polldev.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/rfkill.h>
  25. #include "rt2x00.h"
  26. #include "rt2x00lib.h"
  27. static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
  28. {
  29. struct rt2x00_dev *rt2x00dev = data;
  30. int retval = 0;
  31. if (unlikely(!rt2x00dev))
  32. return 0;
  33. /*
  34. * Only continue if there are enabled interfaces.
  35. */
  36. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  37. return 0;
  38. if (state == RFKILL_STATE_UNBLOCKED) {
  39. INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
  40. __clear_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
  41. retval = rt2x00lib_enable_radio(rt2x00dev);
  42. } else if (state == RFKILL_STATE_SOFT_BLOCKED) {
  43. INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
  44. __set_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
  45. rt2x00lib_disable_radio(rt2x00dev);
  46. } else {
  47. WARNING(rt2x00dev, "Received unexpected rfkill state %d.\n",
  48. state);
  49. }
  50. return retval;
  51. }
  52. static void rt2x00rfkill_poll(struct input_polled_dev *poll_dev)
  53. {
  54. struct rt2x00_dev *rt2x00dev = poll_dev->private;
  55. int state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
  56. if (rt2x00dev->rfkill->state != state) {
  57. input_report_key(poll_dev->input, KEY_WLAN, 1);
  58. input_report_key(poll_dev->input, KEY_WLAN, 0);
  59. }
  60. }
  61. void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
  62. {
  63. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
  64. !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
  65. return;
  66. if (rfkill_register(rt2x00dev->rfkill)) {
  67. ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
  68. return;
  69. }
  70. if (input_register_polled_device(rt2x00dev->poll_dev)) {
  71. ERROR(rt2x00dev, "Failed to register polled device.\n");
  72. rfkill_unregister(rt2x00dev->rfkill);
  73. return;
  74. }
  75. __set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
  76. /*
  77. * Force initial poll which will detect the initial device state,
  78. * and correctly sends the signal to the rfkill layer about this
  79. * state.
  80. */
  81. rt2x00rfkill_poll(rt2x00dev->poll_dev);
  82. }
  83. void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
  84. {
  85. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
  86. !test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
  87. return;
  88. input_unregister_polled_device(rt2x00dev->poll_dev);
  89. rfkill_unregister(rt2x00dev->rfkill);
  90. __clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
  91. }
  92. static struct input_polled_dev *
  93. rt2x00rfkill_allocate_polldev(struct rt2x00_dev *rt2x00dev)
  94. {
  95. struct input_polled_dev *poll_dev;
  96. poll_dev = input_allocate_polled_device();
  97. if (!poll_dev)
  98. return NULL;
  99. poll_dev->private = rt2x00dev;
  100. poll_dev->poll = rt2x00rfkill_poll;
  101. poll_dev->poll_interval = RFKILL_POLL_INTERVAL;
  102. poll_dev->input->name = rt2x00dev->ops->name;
  103. poll_dev->input->phys = wiphy_name(rt2x00dev->hw->wiphy);
  104. poll_dev->input->id.bustype = BUS_HOST;
  105. poll_dev->input->id.vendor = 0x1814;
  106. poll_dev->input->id.product = rt2x00dev->chip.rt;
  107. poll_dev->input->id.version = rt2x00dev->chip.rev;
  108. poll_dev->input->dev.parent = wiphy_dev(rt2x00dev->hw->wiphy);
  109. poll_dev->input->evbit[0] = BIT(EV_KEY);
  110. set_bit(KEY_WLAN, poll_dev->input->keybit);
  111. return poll_dev;
  112. }
  113. void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
  114. {
  115. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  116. return;
  117. rt2x00dev->rfkill =
  118. rfkill_allocate(wiphy_dev(rt2x00dev->hw->wiphy), RFKILL_TYPE_WLAN);
  119. if (!rt2x00dev->rfkill) {
  120. ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
  121. return;
  122. }
  123. rt2x00dev->rfkill->name = rt2x00dev->ops->name;
  124. rt2x00dev->rfkill->data = rt2x00dev;
  125. rt2x00dev->rfkill->state = -1;
  126. rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
  127. rt2x00dev->poll_dev = rt2x00rfkill_allocate_polldev(rt2x00dev);
  128. if (!rt2x00dev->poll_dev) {
  129. ERROR(rt2x00dev, "Failed to allocate polled device.\n");
  130. rfkill_free(rt2x00dev->rfkill);
  131. rt2x00dev->rfkill = NULL;
  132. return;
  133. }
  134. return;
  135. }
  136. void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
  137. {
  138. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
  139. !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
  140. return;
  141. input_free_polled_device(rt2x00dev->poll_dev);
  142. rt2x00dev->poll_dev = NULL;
  143. rfkill_free(rt2x00dev->rfkill);
  144. rt2x00dev->rfkill = NULL;
  145. }
  146. void rt2x00rfkill_suspend(struct rt2x00_dev *rt2x00dev)
  147. {
  148. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
  149. !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
  150. return;
  151. input_free_polled_device(rt2x00dev->poll_dev);
  152. rt2x00dev->poll_dev = NULL;
  153. }
  154. void rt2x00rfkill_resume(struct rt2x00_dev *rt2x00dev)
  155. {
  156. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
  157. !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
  158. return;
  159. rt2x00dev->poll_dev = rt2x00rfkill_allocate_polldev(rt2x00dev);
  160. if (!rt2x00dev->poll_dev) {
  161. ERROR(rt2x00dev, "Failed to allocate polled device.\n");
  162. return;
  163. }
  164. }