rfkill.c 5.5 KB

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