Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/linville/wireles...
[linux-2.6] / drivers / net / wireless / iwlwifi / iwl-rfkill.c
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
33 #include <net/mac80211.h>
34
35 #include "iwl-eeprom.h"
36 #include "iwl-dev.h"
37 #include "iwl-core.h"
38 #include "iwl-helpers.h"
39
40
41 /* software rf-kill from user */
42 static int iwl_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
43 {
44         struct iwl_priv *priv = data;
45         int err = 0;
46
47         if (!priv->rfkill_mngr.rfkill)
48                 return 0;
49
50         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
51                 return 0;
52
53         IWL_DEBUG_RF_KILL("we recieved soft RFKILL set to state %d\n", state);
54         mutex_lock(&priv->mutex);
55
56         switch (state) {
57         case RFKILL_STATE_UNBLOCKED:
58                 iwl_radio_kill_sw_enable_radio(priv);
59                 /* if HW rf-kill is set dont allow ON state */
60                 if (iwl_is_rfkill(priv))
61                         err = -EBUSY;
62                 break;
63         case RFKILL_STATE_SOFT_BLOCKED:
64                 iwl_radio_kill_sw_disable_radio(priv);
65                 if (!iwl_is_rfkill(priv))
66                         err = -EBUSY;
67                 break;
68         default:
69                 IWL_WARNING("we recieved unexpected RFKILL state %d\n", state);
70                 break;
71         }
72         mutex_unlock(&priv->mutex);
73
74         return err;
75 }
76
77 int iwl_rfkill_init(struct iwl_priv *priv)
78 {
79         struct device *device = wiphy_dev(priv->hw->wiphy);
80         int ret = 0;
81
82         BUG_ON(device == NULL);
83
84         IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
85         priv->rfkill_mngr.rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
86         if (!priv->rfkill_mngr.rfkill) {
87                 IWL_ERROR("Unable to allocate rfkill device.\n");
88                 ret = -ENOMEM;
89                 goto error;
90         }
91
92         priv->rfkill_mngr.rfkill->name = priv->cfg->name;
93         priv->rfkill_mngr.rfkill->data = priv;
94         priv->rfkill_mngr.rfkill->state = RFKILL_STATE_ON;
95         priv->rfkill_mngr.rfkill->toggle_radio = iwl_rfkill_soft_rf_kill;
96         priv->rfkill_mngr.rfkill->user_claim_unsupported = 1;
97
98         priv->rfkill_mngr.rfkill->dev.class->suspend = NULL;
99         priv->rfkill_mngr.rfkill->dev.class->resume = NULL;
100
101 #if defined(CONFIG_RFKILL_INPUT) || defined(CONFIG_RFKILL_INPUT_MODULE)
102         priv->rfkill_mngr.input_dev = input_allocate_device();
103         if (!priv->rfkill_mngr.input_dev) {
104                 IWL_ERROR("Unable to allocate rfkill input device.\n");
105                 ret = -ENOMEM;
106                 goto freed_rfkill;
107         }
108
109         priv->rfkill_mngr.input_dev->name = priv->cfg->name;
110         priv->rfkill_mngr.input_dev->phys = wiphy_name(priv->hw->wiphy);
111         priv->rfkill_mngr.input_dev->id.bustype = BUS_HOST;
112         priv->rfkill_mngr.input_dev->id.vendor = priv->pci_dev->vendor;
113         priv->rfkill_mngr.input_dev->dev.parent = device;
114         priv->rfkill_mngr.input_dev->evbit[0] = BIT(EV_KEY);
115         set_bit(KEY_WLAN, priv->rfkill_mngr.input_dev->keybit);
116 #endif
117
118         ret = rfkill_register(priv->rfkill_mngr.rfkill);
119         if (ret) {
120                 IWL_ERROR("Unable to register rfkill: %d\n", ret);
121                 goto free_input_dev;
122         }
123
124 #if defined(CONFIG_RFKILL_INPUT) || defined(CONFIG_RFKILL_INPUT_MODULE)
125         ret = input_register_device(priv->rfkill_mngr.input_dev);
126         if (ret) {
127                 IWL_ERROR("Unable to register rfkill input device: %d\n", ret);
128                 goto unregister_rfkill;
129         }
130 #endif
131
132         IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
133         return ret;
134
135 unregister_rfkill:
136         rfkill_unregister(priv->rfkill_mngr.rfkill);
137         priv->rfkill_mngr.rfkill = NULL;
138
139 free_input_dev:
140 #if defined(CONFIG_RFKILL_INPUT) || defined(CONFIG_RFKILL_INPUT_MODULE)
141         input_free_device(priv->rfkill_mngr.input_dev);
142         priv->rfkill_mngr.input_dev = NULL;
143 #endif
144
145 freed_rfkill:
146         if (priv->rfkill_mngr.rfkill != NULL)
147                 rfkill_free(priv->rfkill_mngr.rfkill);
148         priv->rfkill_mngr.rfkill = NULL;
149
150 error:
151         IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
152         return ret;
153 }
154 EXPORT_SYMBOL(iwl_rfkill_init);
155
156 void iwl_rfkill_unregister(struct iwl_priv *priv)
157 {
158
159 #if defined(CONFIG_RFKILL_INPUT) || defined(CONFIG_RFKILL_INPUT_MODULE)
160         if (priv->rfkill_mngr.input_dev)
161                 input_unregister_device(priv->rfkill_mngr.input_dev);
162         input_free_device(priv->rfkill_mngr.input_dev);
163         priv->rfkill_mngr.input_dev = NULL;
164 #endif
165
166         if (priv->rfkill_mngr.rfkill)
167                 rfkill_unregister(priv->rfkill_mngr.rfkill);
168
169         priv->rfkill_mngr.rfkill = NULL;
170 }
171 EXPORT_SYMBOL(iwl_rfkill_unregister);
172
173 /* set rf-kill to the right state. */
174 void iwl_rfkill_set_hw_state(struct iwl_priv *priv)
175 {
176
177         if (!priv->rfkill_mngr.rfkill)
178                 return;
179
180         if (!iwl_is_rfkill(priv))
181                 priv->rfkill_mngr.rfkill->state = RFKILL_STATE_ON;
182         else
183                 priv->rfkill_mngr.rfkill->state = RFKILL_STATE_OFF;
184 }
185 EXPORT_SYMBOL(iwl_rfkill_set_hw_state);