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