rfkill.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /* Update the rfkill state */
  37. static void b43legacy_rfkill_update_state(struct b43legacy_wldev *dev)
  38. {
  39. struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
  40. if (!dev->radio_hw_enable) {
  41. rfk->rfkill->state = RFKILL_STATE_HARD_BLOCKED;
  42. return;
  43. }
  44. if (!dev->phy.radio_on)
  45. rfk->rfkill->state = RFKILL_STATE_SOFT_BLOCKED;
  46. else
  47. rfk->rfkill->state = RFKILL_STATE_UNBLOCKED;
  48. }
  49. /* The poll callback for the hardware button. */
  50. static void b43legacy_rfkill_poll(struct input_polled_dev *poll_dev)
  51. {
  52. struct b43legacy_wldev *dev = poll_dev->private;
  53. struct b43legacy_wl *wl = dev->wl;
  54. bool enabled;
  55. bool report_change = 0;
  56. mutex_lock(&wl->mutex);
  57. if (unlikely(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)) {
  58. mutex_unlock(&wl->mutex);
  59. return;
  60. }
  61. enabled = b43legacy_is_hw_radio_enabled(dev);
  62. if (unlikely(enabled != dev->radio_hw_enable)) {
  63. dev->radio_hw_enable = enabled;
  64. report_change = 1;
  65. b43legacy_rfkill_update_state(dev);
  66. b43legacyinfo(wl, "Radio hardware status changed to %s\n",
  67. enabled ? "ENABLED" : "DISABLED");
  68. }
  69. mutex_unlock(&wl->mutex);
  70. /* send the radio switch event to the system - note both a key press
  71. * and a release are required */
  72. if (unlikely(report_change)) {
  73. input_report_key(poll_dev->input, KEY_WLAN, 1);
  74. input_report_key(poll_dev->input, KEY_WLAN, 0);
  75. }
  76. }
  77. /* Called when the RFKILL toggled in software.
  78. * This is called without locking. */
  79. static int b43legacy_rfkill_soft_toggle(void *data, enum rfkill_state state)
  80. {
  81. struct b43legacy_wldev *dev = data;
  82. struct b43legacy_wl *wl = dev->wl;
  83. int err = -EBUSY;
  84. if (!wl->rfkill.registered)
  85. return 0;
  86. mutex_lock(&wl->mutex);
  87. if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
  88. goto out_unlock;
  89. err = 0;
  90. switch (state) {
  91. case RFKILL_STATE_UNBLOCKED:
  92. if (!dev->radio_hw_enable) {
  93. /* No luck. We can't toggle the hardware RF-kill
  94. * button from software. */
  95. err = -EBUSY;
  96. goto out_unlock;
  97. }
  98. if (!dev->phy.radio_on)
  99. b43legacy_radio_turn_on(dev);
  100. break;
  101. case RFKILL_STATE_SOFT_BLOCKED:
  102. if (dev->phy.radio_on)
  103. b43legacy_radio_turn_off(dev, 0);
  104. break;
  105. default:
  106. b43legacywarn(wl, "Received unexpected rfkill state %d.\n",
  107. state);
  108. break;
  109. }
  110. out_unlock:
  111. mutex_unlock(&wl->mutex);
  112. return err;
  113. }
  114. char *b43legacy_rfkill_led_name(struct b43legacy_wldev *dev)
  115. {
  116. struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
  117. if (!rfk->registered)
  118. return NULL;
  119. return rfkill_get_led_name(rfk->rfkill);
  120. }
  121. void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
  122. {
  123. struct b43legacy_wl *wl = dev->wl;
  124. struct b43legacy_rfkill *rfk = &(wl->rfkill);
  125. int err;
  126. rfk->registered = 0;
  127. rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
  128. if (!rfk->rfkill)
  129. goto out_error;
  130. snprintf(rfk->name, sizeof(rfk->name),
  131. "b43legacy-%s", wiphy_name(wl->hw->wiphy));
  132. rfk->rfkill->name = rfk->name;
  133. rfk->rfkill->state = RFKILL_STATE_UNBLOCKED;
  134. rfk->rfkill->data = dev;
  135. rfk->rfkill->toggle_radio = b43legacy_rfkill_soft_toggle;
  136. rfk->rfkill->user_claim_unsupported = 1;
  137. rfk->poll_dev = input_allocate_polled_device();
  138. if (!rfk->poll_dev) {
  139. rfkill_free(rfk->rfkill);
  140. goto err_freed_rfk;
  141. }
  142. rfk->poll_dev->private = dev;
  143. rfk->poll_dev->poll = b43legacy_rfkill_poll;
  144. rfk->poll_dev->poll_interval = 1000; /* msecs */
  145. rfk->poll_dev->input->name = rfk->name;
  146. rfk->poll_dev->input->id.bustype = BUS_HOST;
  147. rfk->poll_dev->input->id.vendor = dev->dev->bus->boardinfo.vendor;
  148. rfk->poll_dev->input->evbit[0] = BIT(EV_KEY);
  149. set_bit(KEY_WLAN, rfk->poll_dev->input->keybit);
  150. err = rfkill_register(rfk->rfkill);
  151. if (err)
  152. goto err_free_polldev;
  153. #ifdef CONFIG_RFKILL_INPUT_MODULE
  154. /* B43legacy RF-kill isn't useful without the rfkill-input subsystem.
  155. * Try to load the module. */
  156. err = request_module("rfkill-input");
  157. if (err)
  158. b43legacywarn(wl, "Failed to load the rfkill-input module."
  159. "The built-in radio LED will not work.\n");
  160. #endif /* CONFIG_RFKILL_INPUT */
  161. err = input_register_polled_device(rfk->poll_dev);
  162. if (err)
  163. goto err_unreg_rfk;
  164. rfk->registered = 1;
  165. return;
  166. err_unreg_rfk:
  167. rfkill_unregister(rfk->rfkill);
  168. err_free_polldev:
  169. input_free_polled_device(rfk->poll_dev);
  170. rfk->poll_dev = NULL;
  171. err_freed_rfk:
  172. rfk->rfkill = NULL;
  173. out_error:
  174. rfk->registered = 0;
  175. b43legacywarn(wl, "RF-kill button init failed\n");
  176. }
  177. void b43legacy_rfkill_exit(struct b43legacy_wldev *dev)
  178. {
  179. struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
  180. if (!rfk->registered)
  181. return;
  182. rfk->registered = 0;
  183. input_unregister_polled_device(rfk->poll_dev);
  184. rfkill_unregister(rfk->rfkill);
  185. input_free_polled_device(rfk->poll_dev);
  186. rfk->poll_dev = NULL;
  187. rfk->rfkill = NULL;
  188. }