rfkill.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * This is called without locking. */
  54. static int b43_rfkill_soft_toggle(void *data, enum rfkill_state state)
  55. {
  56. struct b43_wldev *dev = data;
  57. struct b43_wl *wl = dev->wl;
  58. int err = 0;
  59. mutex_lock(&wl->mutex);
  60. if (b43_status(dev) < B43_STAT_INITIALIZED)
  61. goto out_unlock;
  62. switch (state) {
  63. case RFKILL_STATE_ON:
  64. if (!dev->radio_hw_enable) {
  65. /* No luck. We can't toggle the hardware RF-kill
  66. * button from software. */
  67. err = -EBUSY;
  68. goto out_unlock;
  69. }
  70. if (!dev->phy.radio_on)
  71. b43_radio_turn_on(dev);
  72. break;
  73. case RFKILL_STATE_OFF:
  74. if (dev->phy.radio_on)
  75. b43_radio_turn_off(dev, 0);
  76. break;
  77. }
  78. out_unlock:
  79. mutex_unlock(&wl->mutex);
  80. return err;
  81. }
  82. char * b43_rfkill_led_name(struct b43_wldev *dev)
  83. {
  84. struct b43_wl *wl = dev->wl;
  85. if (!wl->rfkill.rfkill)
  86. return NULL;
  87. return rfkill_get_led_name(wl->rfkill.rfkill);
  88. }
  89. void b43_rfkill_init(struct b43_wldev *dev)
  90. {
  91. struct b43_wl *wl = dev->wl;
  92. struct b43_rfkill *rfk = &(wl->rfkill);
  93. int err;
  94. if (rfk->rfkill) {
  95. err = rfkill_register(rfk->rfkill);
  96. if (err) {
  97. b43warn(wl, "Failed to register RF-kill button\n");
  98. goto err_free_rfk;
  99. }
  100. }
  101. if (rfk->poll_dev) {
  102. err = input_register_polled_device(rfk->poll_dev);
  103. if (err) {
  104. b43warn(wl, "Failed to register RF-kill polldev\n");
  105. goto err_free_polldev;
  106. }
  107. }
  108. return;
  109. err_free_rfk:
  110. rfkill_free(rfk->rfkill);
  111. rfk->rfkill = NULL;
  112. err_free_polldev:
  113. input_free_polled_device(rfk->poll_dev);
  114. rfk->poll_dev = NULL;
  115. }
  116. void b43_rfkill_exit(struct b43_wldev *dev)
  117. {
  118. struct b43_rfkill *rfk = &(dev->wl->rfkill);
  119. if (rfk->poll_dev)
  120. input_unregister_polled_device(rfk->poll_dev);
  121. if (rfk->rfkill)
  122. rfkill_unregister(rfk->rfkill);
  123. }
  124. void b43_rfkill_alloc(struct b43_wldev *dev)
  125. {
  126. struct b43_wl *wl = dev->wl;
  127. struct b43_rfkill *rfk = &(wl->rfkill);
  128. snprintf(rfk->name, sizeof(rfk->name),
  129. "b43-%s", wiphy_name(wl->hw->wiphy));
  130. rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
  131. if (!rfk->rfkill) {
  132. b43warn(wl, "Failed to allocate RF-kill button\n");
  133. return;
  134. }
  135. rfk->rfkill->name = rfk->name;
  136. rfk->rfkill->state = RFKILL_STATE_ON;
  137. rfk->rfkill->data = dev;
  138. rfk->rfkill->toggle_radio = b43_rfkill_soft_toggle;
  139. rfk->rfkill->user_claim_unsupported = 1;
  140. rfk->poll_dev = input_allocate_polled_device();
  141. if (rfk->poll_dev) {
  142. rfk->poll_dev->private = dev;
  143. rfk->poll_dev->poll = b43_rfkill_poll;
  144. rfk->poll_dev->poll_interval = 1000; /* msecs */
  145. } else
  146. b43warn(wl, "Failed to allocate RF-kill polldev\n");
  147. }
  148. void b43_rfkill_free(struct b43_wldev *dev)
  149. {
  150. struct b43_rfkill *rfk = &(dev->wl->rfkill);
  151. input_free_polled_device(rfk->poll_dev);
  152. rfk->poll_dev = NULL;
  153. rfkill_free(rfk->rfkill);
  154. rfk->rfkill = NULL;
  155. }