rfkill.c 5.6 KB

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