rfkill.c 4.8 KB

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