rfkill.c 5.6 KB

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