rfkill.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Broadcom B43 wireless driver
  3. RFKILL support
  4. Copyright (c) 2007 Michael Buesch <mb@bu3sch.de>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; see the file COPYING. If not, write to
  15. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  16. Boston, MA 02110-1301, USA.
  17. */
  18. #include "rfkill.h"
  19. #include "radio.h"
  20. #include "b43legacy.h"
  21. #include <linux/kmod.h>
  22. /* Returns TRUE, if the radio is enabled in hardware. */
  23. static bool b43legacy_is_hw_radio_enabled(struct b43legacy_wldev *dev)
  24. {
  25. if (dev->phy.rev >= 3) {
  26. if (!(b43legacy_read32(dev, B43legacy_MMIO_RADIO_HWENABLED_HI)
  27. & B43legacy_MMIO_RADIO_HWENABLED_HI_MASK))
  28. return 1;
  29. } else {
  30. if (b43legacy_read16(dev, B43legacy_MMIO_RADIO_HWENABLED_LO)
  31. & B43legacy_MMIO_RADIO_HWENABLED_LO_MASK)
  32. return 1;
  33. }
  34. return 0;
  35. }
  36. /* The poll callback for the hardware button. */
  37. static void b43legacy_rfkill_poll(struct rfkill *rfkill, void *data)
  38. {
  39. struct b43legacy_wldev *dev = data;
  40. struct b43legacy_wl *wl = dev->wl;
  41. bool enabled;
  42. mutex_lock(&wl->mutex);
  43. if (unlikely(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)) {
  44. mutex_unlock(&wl->mutex);
  45. return;
  46. }
  47. enabled = b43legacy_is_hw_radio_enabled(dev);
  48. if (unlikely(enabled != dev->radio_hw_enable)) {
  49. dev->radio_hw_enable = enabled;
  50. b43legacyinfo(wl, "Radio hardware status changed to %s\n",
  51. enabled ? "ENABLED" : "DISABLED");
  52. enabled = !rfkill_set_hw_state(rfkill, !enabled);
  53. if (enabled != dev->phy.radio_on) {
  54. if (enabled)
  55. b43legacy_radio_turn_on(dev);
  56. else
  57. b43legacy_radio_turn_off(dev, 0);
  58. }
  59. }
  60. mutex_unlock(&wl->mutex);
  61. }
  62. /* Called when the RFKILL toggled in software.
  63. * This is called without locking. */
  64. static int b43legacy_rfkill_soft_set(void *data, bool blocked)
  65. {
  66. struct b43legacy_wldev *dev = data;
  67. struct b43legacy_wl *wl = dev->wl;
  68. int ret = -EINVAL;
  69. if (!wl->rfkill.registered)
  70. return -EINVAL;
  71. mutex_lock(&wl->mutex);
  72. if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
  73. goto out_unlock;
  74. if (!dev->radio_hw_enable)
  75. goto out_unlock;
  76. if (!blocked != dev->phy.radio_on) {
  77. if (!blocked)
  78. b43legacy_radio_turn_on(dev);
  79. else
  80. b43legacy_radio_turn_off(dev, 0);
  81. }
  82. ret = 0;
  83. out_unlock:
  84. mutex_unlock(&wl->mutex);
  85. return ret;
  86. }
  87. const char *b43legacy_rfkill_led_name(struct b43legacy_wldev *dev)
  88. {
  89. struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
  90. if (!rfk->registered)
  91. return NULL;
  92. return rfkill_get_led_trigger_name(rfk->rfkill);
  93. }
  94. static const struct rfkill_ops b43legacy_rfkill_ops = {
  95. .set_block = b43legacy_rfkill_soft_set,
  96. .poll = b43legacy_rfkill_poll,
  97. };
  98. void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
  99. {
  100. struct b43legacy_wl *wl = dev->wl;
  101. struct b43legacy_rfkill *rfk = &(wl->rfkill);
  102. int err;
  103. rfk->registered = 0;
  104. snprintf(rfk->name, sizeof(rfk->name),
  105. "b43legacy-%s", wiphy_name(wl->hw->wiphy));
  106. rfk->rfkill = rfkill_alloc(rfk->name,
  107. dev->dev->dev,
  108. RFKILL_TYPE_WLAN,
  109. &b43legacy_rfkill_ops, dev);
  110. if (!rfk->rfkill)
  111. goto out_error;
  112. err = rfkill_register(rfk->rfkill);
  113. if (err)
  114. goto err_free;
  115. rfk->registered = 1;
  116. return;
  117. err_free:
  118. rfkill_destroy(rfk->rfkill);
  119. out_error:
  120. rfk->registered = 0;
  121. b43legacywarn(wl, "RF-kill button init failed\n");
  122. }
  123. void b43legacy_rfkill_exit(struct b43legacy_wldev *dev)
  124. {
  125. struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
  126. if (!rfk->registered)
  127. return;
  128. rfk->registered = 0;
  129. rfkill_unregister(rfk->rfkill);
  130. rfkill_destroy(rfk->rfkill);
  131. rfk->rfkill = NULL;
  132. }