rfkill.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "b43.h"
  20. /* Returns TRUE, if the radio is enabled in hardware. */
  21. static bool b43_is_hw_radio_enabled(struct b43_wldev *dev)
  22. {
  23. if (dev->phy.rev >= 3) {
  24. if (!(b43_read32(dev, B43_MMIO_RADIO_HWENABLED_HI)
  25. & B43_MMIO_RADIO_HWENABLED_HI_MASK))
  26. return 1;
  27. } else {
  28. if (b43_read16(dev, B43_MMIO_RADIO_HWENABLED_LO)
  29. & B43_MMIO_RADIO_HWENABLED_LO_MASK)
  30. return 1;
  31. }
  32. return 0;
  33. }
  34. /* The poll callback for the hardware button. */
  35. static void b43_rfkill_poll(struct input_polled_dev *poll_dev)
  36. {
  37. struct b43_wldev *dev = poll_dev->private;
  38. struct b43_wl *wl = dev->wl;
  39. bool enabled;
  40. mutex_lock(&wl->mutex);
  41. B43_WARN_ON(b43_status(dev) < B43_STAT_INITIALIZED);
  42. enabled = b43_is_hw_radio_enabled(dev);
  43. if (unlikely(enabled != dev->radio_hw_enable)) {
  44. dev->radio_hw_enable = enabled;
  45. b43info(wl, "Radio hardware status changed to %s\n",
  46. enabled ? "ENABLED" : "DISABLED");
  47. mutex_unlock(&wl->mutex);
  48. input_report_key(poll_dev->input, KEY_WLAN, enabled);
  49. } else
  50. mutex_unlock(&wl->mutex);
  51. }
  52. /* Called when the RFKILL toggled in software. */
  53. static int b43_rfkill_soft_toggle(void *data, enum rfkill_state state)
  54. {
  55. struct b43_wldev *dev = data;
  56. struct b43_wl *wl = dev->wl;
  57. int err = 0;
  58. /* When RFKILL is registered, it will call back into this callback.
  59. * wl->mutex will already be locked when this happens.
  60. * So first trylock. On contention check if we are in initialization.
  61. * Silently return if that happens to avoid a deadlock. */
  62. if (mutex_trylock(&wl->mutex) == 0) {
  63. if (b43_status(dev) < B43_STAT_INITIALIZED)
  64. return 0;
  65. mutex_lock(&wl->mutex);
  66. }
  67. if (b43_status(dev) < B43_STAT_INITIALIZED)
  68. goto out_unlock;
  69. switch (state) {
  70. case RFKILL_STATE_ON:
  71. if (!dev->radio_hw_enable) {
  72. /* No luck. We can't toggle the hardware RF-kill
  73. * button from software. */
  74. err = -EBUSY;
  75. goto out_unlock;
  76. }
  77. if (!dev->phy.radio_on)
  78. b43_radio_turn_on(dev);
  79. break;
  80. case RFKILL_STATE_OFF:
  81. if (dev->phy.radio_on)
  82. b43_radio_turn_off(dev, 0);
  83. break;
  84. }
  85. out_unlock:
  86. mutex_unlock(&wl->mutex);
  87. return err;
  88. }
  89. char * b43_rfkill_led_name(struct b43_wldev *dev)
  90. {
  91. struct b43_wl *wl = dev->wl;
  92. if (!wl->rfkill.rfkill)
  93. return NULL;
  94. return rfkill_get_led_name(wl->rfkill.rfkill);
  95. }
  96. void b43_rfkill_init(struct b43_wldev *dev)
  97. {
  98. struct b43_wl *wl = dev->wl;
  99. struct b43_rfkill *rfk = &(wl->rfkill);
  100. int err;
  101. if (rfk->rfkill) {
  102. err = rfkill_register(rfk->rfkill);
  103. if (err) {
  104. b43warn(wl, "Failed to register RF-kill button\n");
  105. goto err_free_rfk;
  106. }
  107. }
  108. if (rfk->poll_dev) {
  109. err = input_register_polled_device(rfk->poll_dev);
  110. if (err) {
  111. b43warn(wl, "Failed to register RF-kill polldev\n");
  112. goto err_free_polldev;
  113. }
  114. }
  115. return;
  116. err_free_rfk:
  117. rfkill_free(rfk->rfkill);
  118. rfk->rfkill = NULL;
  119. err_free_polldev:
  120. input_free_polled_device(rfk->poll_dev);
  121. rfk->poll_dev = NULL;
  122. }
  123. void b43_rfkill_exit(struct b43_wldev *dev)
  124. {
  125. struct b43_rfkill *rfk = &(dev->wl->rfkill);
  126. if (rfk->poll_dev)
  127. input_unregister_polled_device(rfk->poll_dev);
  128. if (rfk->rfkill)
  129. rfkill_unregister(rfk->rfkill);
  130. }
  131. void b43_rfkill_alloc(struct b43_wldev *dev)
  132. {
  133. struct b43_wl *wl = dev->wl;
  134. struct b43_rfkill *rfk = &(wl->rfkill);
  135. snprintf(rfk->name, sizeof(rfk->name),
  136. "b43-%s", wiphy_name(wl->hw->wiphy));
  137. rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
  138. if (!rfk->rfkill) {
  139. b43warn(wl, "Failed to allocate RF-kill button\n");
  140. return;
  141. }
  142. rfk->rfkill->name = rfk->name;
  143. rfk->rfkill->state = RFKILL_STATE_ON;
  144. rfk->rfkill->data = dev;
  145. rfk->rfkill->toggle_radio = b43_rfkill_soft_toggle;
  146. rfk->rfkill->user_claim_unsupported = 1;
  147. rfk->poll_dev = input_allocate_polled_device();
  148. if (rfk->poll_dev) {
  149. rfk->poll_dev->private = dev;
  150. rfk->poll_dev->poll = b43_rfkill_poll;
  151. rfk->poll_dev->poll_interval = 1000; /* msecs */
  152. } else
  153. b43warn(wl, "Failed to allocate RF-kill polldev\n");
  154. }
  155. void b43_rfkill_free(struct b43_wldev *dev)
  156. {
  157. struct b43_rfkill *rfk = &(dev->wl->rfkill);
  158. input_free_polled_device(rfk->poll_dev);
  159. rfk->poll_dev = NULL;
  160. rfkill_free(rfk->rfkill);
  161. rfk->rfkill = NULL;
  162. }