iwl-rfkill.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * James P. Ketrenos <ipw2100-admin@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *****************************************************************************/
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/version.h>
  31. #include <linux/init.h>
  32. #include <net/mac80211.h>
  33. #include "iwl-eeprom.h"
  34. #include "iwl-dev.h"
  35. #include "iwl-core.h"
  36. #include "iwl-helpers.h"
  37. /* software rf-kill from user */
  38. static int iwl_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
  39. {
  40. struct iwl_priv *priv = data;
  41. int err = 0;
  42. if (!priv->rfkill_mngr.rfkill)
  43. return 0;
  44. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  45. return 0;
  46. IWL_DEBUG_RF_KILL("we recieved soft RFKILL set to state %d\n", state);
  47. mutex_lock(&priv->mutex);
  48. switch (state) {
  49. case RFKILL_STATE_ON:
  50. priv->cfg->ops->lib->radio_kill_sw(priv, 0);
  51. /* if HW rf-kill is set dont allow ON state */
  52. if (iwl_is_rfkill(priv))
  53. err = -EBUSY;
  54. break;
  55. case RFKILL_STATE_OFF:
  56. priv->cfg->ops->lib->radio_kill_sw(priv, 1);
  57. if (!iwl_is_rfkill(priv))
  58. err = -EBUSY;
  59. break;
  60. }
  61. mutex_unlock(&priv->mutex);
  62. return err;
  63. }
  64. int iwl_rfkill_init(struct iwl_priv *priv)
  65. {
  66. struct device *device = wiphy_dev(priv->hw->wiphy);
  67. int ret = 0;
  68. BUG_ON(device == NULL);
  69. IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
  70. priv->rfkill_mngr.rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
  71. if (!priv->rfkill_mngr.rfkill) {
  72. IWL_ERROR("Unable to allocate rfkill device.\n");
  73. ret = -ENOMEM;
  74. goto error;
  75. }
  76. priv->rfkill_mngr.rfkill->name = priv->cfg->name;
  77. priv->rfkill_mngr.rfkill->data = priv;
  78. priv->rfkill_mngr.rfkill->state = RFKILL_STATE_ON;
  79. priv->rfkill_mngr.rfkill->toggle_radio = iwl_rfkill_soft_rf_kill;
  80. priv->rfkill_mngr.rfkill->user_claim_unsupported = 1;
  81. priv->rfkill_mngr.rfkill->dev.class->suspend = NULL;
  82. priv->rfkill_mngr.rfkill->dev.class->resume = NULL;
  83. priv->rfkill_mngr.input_dev = input_allocate_device();
  84. if (!priv->rfkill_mngr.input_dev) {
  85. IWL_ERROR("Unable to allocate rfkill input device.\n");
  86. ret = -ENOMEM;
  87. goto freed_rfkill;
  88. }
  89. priv->rfkill_mngr.input_dev->name = priv->cfg->name;
  90. priv->rfkill_mngr.input_dev->phys = wiphy_name(priv->hw->wiphy);
  91. priv->rfkill_mngr.input_dev->id.bustype = BUS_HOST;
  92. priv->rfkill_mngr.input_dev->id.vendor = priv->pci_dev->vendor;
  93. priv->rfkill_mngr.input_dev->dev.parent = device;
  94. priv->rfkill_mngr.input_dev->evbit[0] = BIT(EV_KEY);
  95. set_bit(KEY_WLAN, priv->rfkill_mngr.input_dev->keybit);
  96. ret = rfkill_register(priv->rfkill_mngr.rfkill);
  97. if (ret) {
  98. IWL_ERROR("Unable to register rfkill: %d\n", ret);
  99. goto free_input_dev;
  100. }
  101. ret = input_register_device(priv->rfkill_mngr.input_dev);
  102. if (ret) {
  103. IWL_ERROR("Unable to register rfkill input device: %d\n", ret);
  104. goto unregister_rfkill;
  105. }
  106. IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
  107. return ret;
  108. unregister_rfkill:
  109. rfkill_unregister(priv->rfkill_mngr.rfkill);
  110. priv->rfkill_mngr.rfkill = NULL;
  111. free_input_dev:
  112. input_free_device(priv->rfkill_mngr.input_dev);
  113. priv->rfkill_mngr.input_dev = NULL;
  114. freed_rfkill:
  115. if (priv->rfkill_mngr.rfkill != NULL)
  116. rfkill_free(priv->rfkill_mngr.rfkill);
  117. priv->rfkill_mngr.rfkill = NULL;
  118. error:
  119. IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
  120. return ret;
  121. }
  122. EXPORT_SYMBOL(iwl_rfkill_init);
  123. void iwl_rfkill_unregister(struct iwl_priv *priv)
  124. {
  125. if (priv->rfkill_mngr.input_dev)
  126. input_unregister_device(priv->rfkill_mngr.input_dev);
  127. if (priv->rfkill_mngr.rfkill)
  128. rfkill_unregister(priv->rfkill_mngr.rfkill);
  129. priv->rfkill_mngr.input_dev = NULL;
  130. priv->rfkill_mngr.rfkill = NULL;
  131. }
  132. EXPORT_SYMBOL(iwl_rfkill_unregister);
  133. /* set rf-kill to the right state. */
  134. void iwl_rfkill_set_hw_state(struct iwl_priv *priv)
  135. {
  136. if (!priv->rfkill_mngr.rfkill)
  137. return;
  138. if (!iwl_is_rfkill(priv))
  139. priv->rfkill_mngr.rfkill->state = RFKILL_STATE_ON;
  140. else
  141. priv->rfkill_mngr.rfkill->state = RFKILL_STATE_OFF;
  142. }
  143. EXPORT_SYMBOL(iwl_rfkill_set_hw_state);