rfkill.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_ON:
  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_OFF:
  88. if (dev->phy.radio_on)
  89. b43legacy_radio_turn_off(dev, 0);
  90. break;
  91. }
  92. out_unlock:
  93. mutex_unlock(&wl->mutex);
  94. return err;
  95. }
  96. char *b43legacy_rfkill_led_name(struct b43legacy_wldev *dev)
  97. {
  98. struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
  99. if (!rfk->registered)
  100. return NULL;
  101. return rfkill_get_led_name(rfk->rfkill);
  102. }
  103. void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
  104. {
  105. struct b43legacy_wl *wl = dev->wl;
  106. struct b43legacy_rfkill *rfk = &(wl->rfkill);
  107. int err;
  108. rfk->registered = 0;
  109. rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
  110. if (!rfk->rfkill)
  111. goto out_error;
  112. snprintf(rfk->name, sizeof(rfk->name),
  113. "b43legacy-%s", wiphy_name(wl->hw->wiphy));
  114. rfk->rfkill->name = rfk->name;
  115. rfk->rfkill->state = RFKILL_STATE_ON;
  116. rfk->rfkill->data = dev;
  117. rfk->rfkill->toggle_radio = b43legacy_rfkill_soft_toggle;
  118. rfk->rfkill->user_claim_unsupported = 1;
  119. rfk->poll_dev = input_allocate_polled_device();
  120. if (!rfk->poll_dev) {
  121. rfkill_free(rfk->rfkill);
  122. goto err_freed_rfk;
  123. }
  124. rfk->poll_dev->private = dev;
  125. rfk->poll_dev->poll = b43legacy_rfkill_poll;
  126. rfk->poll_dev->poll_interval = 1000; /* msecs */
  127. rfk->poll_dev->input->name = rfk->name;
  128. rfk->poll_dev->input->id.bustype = BUS_HOST;
  129. rfk->poll_dev->input->id.vendor = dev->dev->bus->boardinfo.vendor;
  130. rfk->poll_dev->input->evbit[0] = BIT(EV_KEY);
  131. set_bit(KEY_WLAN, rfk->poll_dev->input->keybit);
  132. err = rfkill_register(rfk->rfkill);
  133. if (err)
  134. goto err_free_polldev;
  135. #ifdef CONFIG_RFKILL_INPUT_MODULE
  136. /* B43legacy RF-kill isn't useful without the rfkill-input subsystem.
  137. * Try to load the module. */
  138. err = request_module("rfkill-input");
  139. if (err)
  140. b43legacywarn(wl, "Failed to load the rfkill-input module."
  141. "The built-in radio LED will not work.\n");
  142. #endif /* CONFIG_RFKILL_INPUT */
  143. err = input_register_polled_device(rfk->poll_dev);
  144. if (err)
  145. goto err_unreg_rfk;
  146. rfk->registered = 1;
  147. return;
  148. err_unreg_rfk:
  149. rfkill_unregister(rfk->rfkill);
  150. err_free_polldev:
  151. input_free_polled_device(rfk->poll_dev);
  152. rfk->poll_dev = NULL;
  153. err_freed_rfk:
  154. rfk->rfkill = NULL;
  155. out_error:
  156. rfk->registered = 0;
  157. b43legacywarn(wl, "RF-kill button init failed\n");
  158. }
  159. void b43legacy_rfkill_exit(struct b43legacy_wldev *dev)
  160. {
  161. struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
  162. if (!rfk->registered)
  163. return;
  164. rfk->registered = 0;
  165. input_unregister_polled_device(rfk->poll_dev);
  166. rfkill_unregister(rfk->rfkill);
  167. input_free_polled_device(rfk->poll_dev);
  168. rfk->poll_dev = NULL;
  169. rfk->rfkill = NULL;
  170. }