rt2x00rfkill.c 5.6 KB

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