rt2x00rfkill.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright (C) 2004 - 2007 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. int rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
  59. {
  60. int retval;
  61. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  62. return 0;
  63. retval = rfkill_register(rt2x00dev->rfkill);
  64. if (retval) {
  65. ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
  66. return retval;
  67. }
  68. retval = input_register_polled_device(rt2x00dev->poll_dev);
  69. if (retval) {
  70. ERROR(rt2x00dev, "Failed to register polled device.\n");
  71. rfkill_unregister(rt2x00dev->rfkill);
  72. return retval;
  73. }
  74. /*
  75. * Force initial poll which will detect the initial device state,
  76. * and correctly sends the signal to the rfkill layer about this
  77. * state.
  78. */
  79. rt2x00rfkill_poll(rt2x00dev->poll_dev);
  80. return 0;
  81. }
  82. void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
  83. {
  84. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  85. return;
  86. input_unregister_polled_device(rt2x00dev->poll_dev);
  87. rfkill_unregister(rt2x00dev->rfkill);
  88. }
  89. int rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
  90. {
  91. struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
  92. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  93. return 0;
  94. rt2x00dev->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
  95. if (!rt2x00dev->rfkill) {
  96. ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
  97. goto exit;
  98. }
  99. rt2x00dev->rfkill->name = rt2x00dev->ops->name;
  100. rt2x00dev->rfkill->data = rt2x00dev;
  101. rt2x00dev->rfkill->state = -1;
  102. rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
  103. rt2x00dev->poll_dev = input_allocate_polled_device();
  104. if (!rt2x00dev->poll_dev) {
  105. ERROR(rt2x00dev, "Failed to allocate polled device.\n");
  106. goto exit_free_rfkill;
  107. }
  108. rt2x00dev->poll_dev->private = rt2x00dev;
  109. rt2x00dev->poll_dev->poll = rt2x00rfkill_poll;
  110. rt2x00dev->poll_dev->poll_interval = RFKILL_POLL_INTERVAL;
  111. rt2x00dev->poll_dev->input->name = rt2x00dev->ops->name;
  112. rt2x00dev->poll_dev->input->phys = wiphy_name(rt2x00dev->hw->wiphy);
  113. rt2x00dev->poll_dev->input->id.bustype = BUS_HOST;
  114. rt2x00dev->poll_dev->input->id.vendor = 0x1814;
  115. rt2x00dev->poll_dev->input->id.product = rt2x00dev->chip.rt;
  116. rt2x00dev->poll_dev->input->id.version = rt2x00dev->chip.rev;
  117. rt2x00dev->poll_dev->input->dev.parent = device;
  118. rt2x00dev->poll_dev->input->evbit[0] = BIT(EV_KEY);
  119. set_bit(KEY_WLAN, rt2x00dev->poll_dev->input->keybit);
  120. return 0;
  121. exit_free_rfkill:
  122. rfkill_free(rt2x00dev->rfkill);
  123. exit:
  124. return -ENOMEM;
  125. }
  126. void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
  127. {
  128. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  129. return;
  130. input_free_polled_device(rt2x00dev->poll_dev);
  131. rfkill_free(rt2x00dev->rfkill);
  132. }