rt2x00rfkill.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /*
  22. * Set enviroment defines for rt2x00.h
  23. */
  24. #define DRV_NAME "rt2x00lib"
  25. #include <linux/input-polldev.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/rfkill.h>
  29. #include "rt2x00.h"
  30. #include "rt2x00lib.h"
  31. static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
  32. {
  33. struct rt2x00_dev *rt2x00dev = data;
  34. int retval = 0;
  35. if (unlikely(!rt2x00dev))
  36. return 0;
  37. /*
  38. * Only continue if there are enabled interfaces.
  39. */
  40. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  41. return 0;
  42. if (state == RFKILL_STATE_ON) {
  43. INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
  44. __clear_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
  45. retval = rt2x00lib_enable_radio(rt2x00dev);
  46. } else if (state == RFKILL_STATE_OFF) {
  47. INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
  48. __set_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
  49. rt2x00lib_disable_radio(rt2x00dev);
  50. }
  51. return retval;
  52. }
  53. static void rt2x00rfkill_poll(struct input_polled_dev *poll_dev)
  54. {
  55. struct rt2x00_dev *rt2x00dev = poll_dev->private;
  56. int state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
  57. if (rt2x00dev->rfkill->state != state)
  58. input_report_key(poll_dev->input, KEY_WLAN, 1);
  59. }
  60. int rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
  61. {
  62. int retval;
  63. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  64. return 0;
  65. retval = rfkill_register(rt2x00dev->rfkill);
  66. if (retval) {
  67. ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
  68. return retval;
  69. }
  70. retval = input_register_polled_device(rt2x00dev->poll_dev);
  71. if (retval) {
  72. ERROR(rt2x00dev, "Failed to register polled device.\n");
  73. rfkill_unregister(rt2x00dev->rfkill);
  74. return retval;
  75. }
  76. return 0;
  77. }
  78. void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
  79. {
  80. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  81. return;
  82. input_unregister_polled_device(rt2x00dev->poll_dev);
  83. rfkill_unregister(rt2x00dev->rfkill);
  84. }
  85. int rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
  86. {
  87. struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
  88. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  89. return 0;
  90. rt2x00dev->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
  91. if (!rt2x00dev->rfkill) {
  92. ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
  93. return -ENOMEM;
  94. }
  95. rt2x00dev->rfkill->name = rt2x00dev->ops->name;
  96. rt2x00dev->rfkill->data = rt2x00dev;
  97. rt2x00dev->rfkill->state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
  98. rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
  99. rt2x00dev->poll_dev = input_allocate_polled_device();
  100. if (!rt2x00dev->poll_dev) {
  101. ERROR(rt2x00dev, "Failed to allocate polled device.\n");
  102. rfkill_free(rt2x00dev->rfkill);
  103. return -ENOMEM;
  104. }
  105. rt2x00dev->poll_dev->private = rt2x00dev;
  106. rt2x00dev->poll_dev->poll = rt2x00rfkill_poll;
  107. rt2x00dev->poll_dev->poll_interval = RFKILL_POLL_INTERVAL;
  108. return 0;
  109. }
  110. void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
  111. {
  112. if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
  113. return;
  114. input_free_polled_device(rt2x00dev->poll_dev);
  115. rfkill_free(rt2x00dev->rfkill);
  116. }