rfkill.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /* Returns TRUE, if the radio is enabled in hardware. */
  22. static bool b43legacy_is_hw_radio_enabled(struct b43legacy_wldev *dev)
  23. {
  24. if (dev->phy.rev >= 3) {
  25. if (!(b43legacy_read32(dev, B43legacy_MMIO_RADIO_HWENABLED_HI)
  26. & B43legacy_MMIO_RADIO_HWENABLED_HI_MASK))
  27. return 1;
  28. } else {
  29. if (b43legacy_read16(dev, B43legacy_MMIO_RADIO_HWENABLED_LO)
  30. & B43legacy_MMIO_RADIO_HWENABLED_LO_MASK)
  31. return 1;
  32. }
  33. return 0;
  34. }
  35. /* The poll callback for the hardware button. */
  36. static void b43legacy_rfkill_poll(struct input_polled_dev *poll_dev)
  37. {
  38. struct b43legacy_wldev *dev = poll_dev->private;
  39. struct b43legacy_wl *wl = dev->wl;
  40. bool enabled;
  41. bool report_change = 0;
  42. mutex_lock(&wl->mutex);
  43. B43legacy_WARN_ON(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED);
  44. enabled = b43legacy_is_hw_radio_enabled(dev);
  45. if (unlikely(enabled != dev->radio_hw_enable)) {
  46. dev->radio_hw_enable = enabled;
  47. report_change = 1;
  48. b43legacyinfo(wl, "Radio hardware status changed to %s\n",
  49. enabled ? "ENABLED" : "DISABLED");
  50. }
  51. mutex_unlock(&wl->mutex);
  52. if (unlikely(report_change))
  53. input_report_key(poll_dev->input, KEY_WLAN, enabled);
  54. }
  55. /* Called when the RFKILL toggled in software.
  56. * This is called without locking. */
  57. static int b43legacy_rfkill_soft_toggle(void *data, enum rfkill_state state)
  58. {
  59. struct b43legacy_wldev *dev = data;
  60. struct b43legacy_wl *wl = dev->wl;
  61. int err = 0;
  62. if (!wl->rfkill.registered)
  63. return 0;
  64. mutex_lock(&wl->mutex);
  65. B43legacy_WARN_ON(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED);
  66. switch (state) {
  67. case RFKILL_STATE_ON:
  68. if (!dev->radio_hw_enable) {
  69. /* No luck. We can't toggle the hardware RF-kill
  70. * button from software. */
  71. err = -EBUSY;
  72. goto out_unlock;
  73. }
  74. if (!dev->phy.radio_on)
  75. b43legacy_radio_turn_on(dev);
  76. break;
  77. case RFKILL_STATE_OFF:
  78. if (dev->phy.radio_on)
  79. b43legacy_radio_turn_off(dev, 0);
  80. break;
  81. }
  82. out_unlock:
  83. mutex_unlock(&wl->mutex);
  84. return err;
  85. }
  86. char *b43legacy_rfkill_led_name(struct b43legacy_wldev *dev)
  87. {
  88. struct b43legacy_wl *wl = dev->wl;
  89. if (!wl->rfkill.rfkill)
  90. return NULL;
  91. return rfkill_get_led_name(wl->rfkill.rfkill);
  92. }
  93. void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
  94. {
  95. struct b43legacy_wl *wl = dev->wl;
  96. struct b43legacy_rfkill *rfk = &(wl->rfkill);
  97. int err;
  98. if (rfk->rfkill) {
  99. err = rfkill_register(rfk->rfkill);
  100. if (err) {
  101. b43legacywarn(wl, "Failed to register RF-kill button\n");
  102. goto err_free_rfk;
  103. }
  104. }
  105. if (rfk->poll_dev) {
  106. err = input_register_polled_device(rfk->poll_dev);
  107. if (err) {
  108. b43legacywarn(wl, "Failed to register RF-kill polldev\n");
  109. goto err_free_polldev;
  110. }
  111. }
  112. return;
  113. err_free_rfk:
  114. rfkill_free(rfk->rfkill);
  115. rfk->rfkill = NULL;
  116. err_free_polldev:
  117. input_free_polled_device(rfk->poll_dev);
  118. rfk->poll_dev = NULL;
  119. }
  120. void b43legacy_rfkill_exit(struct b43legacy_wldev *dev)
  121. {
  122. struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
  123. if (rfk->poll_dev)
  124. input_unregister_polled_device(rfk->poll_dev);
  125. if (rfk->rfkill)
  126. rfkill_unregister(rfk->rfkill);
  127. }
  128. void b43legacy_rfkill_alloc(struct b43legacy_wldev *dev)
  129. {
  130. struct b43legacy_wl *wl = dev->wl;
  131. struct b43legacy_rfkill *rfk = &(wl->rfkill);
  132. snprintf(rfk->name, sizeof(rfk->name),
  133. "b43legacy-%s", wiphy_name(wl->hw->wiphy));
  134. rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
  135. if (!rfk->rfkill) {
  136. b43legacywarn(wl, "Failed to allocate RF-kill button\n");
  137. return;
  138. }
  139. rfk->rfkill->name = rfk->name;
  140. rfk->rfkill->state = RFKILL_STATE_ON;
  141. rfk->rfkill->data = dev;
  142. rfk->rfkill->toggle_radio = b43legacy_rfkill_soft_toggle;
  143. rfk->rfkill->user_claim_unsupported = 1;
  144. rfk->poll_dev = input_allocate_polled_device();
  145. if (rfk->poll_dev) {
  146. rfk->poll_dev->private = dev;
  147. rfk->poll_dev->poll = b43legacy_rfkill_poll;
  148. rfk->poll_dev->poll_interval = 1000; /* msecs */
  149. } else
  150. b43legacywarn(wl, "Failed to allocate RF-kill polldev\n");
  151. }
  152. void b43legacy_rfkill_free(struct b43legacy_wldev *dev)
  153. {
  154. struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
  155. input_free_polled_device(rfk->poll_dev);
  156. rfk->poll_dev = NULL;
  157. rfkill_free(rfk->rfkill);
  158. rfk->rfkill = NULL;
  159. }