Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[sfrench/cifs-2.6.git] / drivers / net / wireless / rt2x00 / rt2x00rfkill.c
1 /*
2         Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3         <http://rt2x00.serialmonkey.com>
4
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
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the
17         Free Software Foundation, Inc.,
18         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 /*
22         Module: rt2x00rfkill
23         Abstract: rt2x00 rfkill routines.
24  */
25
26 #include <linux/input-polldev.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/rfkill.h>
30
31 #include "rt2x00.h"
32 #include "rt2x00lib.h"
33
34 static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
35 {
36         struct rt2x00_dev *rt2x00dev = data;
37         int retval = 0;
38
39         if (unlikely(!rt2x00dev))
40                 return 0;
41
42         /*
43          * Only continue if there are enabled interfaces.
44          */
45         if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
46                 return 0;
47
48         if (state == RFKILL_STATE_ON) {
49                 INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
50                 __clear_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
51                 retval = rt2x00lib_enable_radio(rt2x00dev);
52         } else if (state == RFKILL_STATE_OFF) {
53                 INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
54                 __set_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
55                 rt2x00lib_disable_radio(rt2x00dev);
56         }
57
58         return retval;
59 }
60
61 static void rt2x00rfkill_poll(struct input_polled_dev *poll_dev)
62 {
63         struct rt2x00_dev *rt2x00dev = poll_dev->private;
64         int state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
65
66         if (rt2x00dev->rfkill->state != state) {
67                 input_report_key(poll_dev->input, KEY_WLAN, 1);
68                 input_report_key(poll_dev->input, KEY_WLAN, 0);
69         }
70 }
71
72 void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
73 {
74         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
75             !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
76                 return;
77
78         if (rfkill_register(rt2x00dev->rfkill)) {
79                 ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
80                 return;
81         }
82
83         if (input_register_polled_device(rt2x00dev->poll_dev)) {
84                 ERROR(rt2x00dev, "Failed to register polled device.\n");
85                 rfkill_unregister(rt2x00dev->rfkill);
86                 return;
87         }
88
89         __set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
90
91         /*
92          * Force initial poll which will detect the initial device state,
93          * and correctly sends the signal to the rfkill layer about this
94          * state.
95          */
96         rt2x00rfkill_poll(rt2x00dev->poll_dev);
97 }
98
99 void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
100 {
101         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
102             !test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
103                 return;
104
105         input_unregister_polled_device(rt2x00dev->poll_dev);
106         rfkill_unregister(rt2x00dev->rfkill);
107
108         __clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
109 }
110
111 static struct input_polled_dev *
112 rt2x00rfkill_allocate_polldev(struct rt2x00_dev *rt2x00dev)
113 {
114         struct input_polled_dev *poll_dev;
115
116         poll_dev = input_allocate_polled_device();
117         if (!poll_dev)
118                 return NULL;
119
120         poll_dev->private = rt2x00dev;
121         poll_dev->poll = rt2x00rfkill_poll;
122         poll_dev->poll_interval = RFKILL_POLL_INTERVAL;
123
124         poll_dev->input->name = rt2x00dev->ops->name;
125         poll_dev->input->phys = wiphy_name(rt2x00dev->hw->wiphy);
126         poll_dev->input->id.bustype = BUS_HOST;
127         poll_dev->input->id.vendor = 0x1814;
128         poll_dev->input->id.product = rt2x00dev->chip.rt;
129         poll_dev->input->id.version = rt2x00dev->chip.rev;
130         poll_dev->input->dev.parent = wiphy_dev(rt2x00dev->hw->wiphy);
131         poll_dev->input->evbit[0] = BIT(EV_KEY);
132         set_bit(KEY_WLAN, poll_dev->input->keybit);
133
134         return poll_dev;
135 }
136
137 void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
138 {
139         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
140                 return;
141
142         rt2x00dev->rfkill =
143             rfkill_allocate(wiphy_dev(rt2x00dev->hw->wiphy), RFKILL_TYPE_WLAN);
144         if (!rt2x00dev->rfkill) {
145                 ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
146                 return;
147         }
148
149         rt2x00dev->rfkill->name = rt2x00dev->ops->name;
150         rt2x00dev->rfkill->data = rt2x00dev;
151         rt2x00dev->rfkill->state = -1;
152         rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
153
154         rt2x00dev->poll_dev = rt2x00rfkill_allocate_polldev(rt2x00dev);
155         if (!rt2x00dev->poll_dev) {
156                 ERROR(rt2x00dev, "Failed to allocate polled device.\n");
157                 rfkill_free(rt2x00dev->rfkill);
158                 rt2x00dev->rfkill = NULL;
159                 return;
160         }
161
162         return;
163 }
164
165 void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
166 {
167         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
168             !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
169                 return;
170
171         input_free_polled_device(rt2x00dev->poll_dev);
172         rt2x00dev->poll_dev = NULL;
173
174         rfkill_free(rt2x00dev->rfkill);
175         rt2x00dev->rfkill = NULL;
176 }
177
178 void rt2x00rfkill_suspend(struct rt2x00_dev *rt2x00dev)
179 {
180         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
181             !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
182                 return;
183
184         input_free_polled_device(rt2x00dev->poll_dev);
185         rt2x00dev->poll_dev = NULL;
186 }
187
188 void rt2x00rfkill_resume(struct rt2x00_dev *rt2x00dev)
189 {
190         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
191             !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
192                 return;
193
194         rt2x00dev->poll_dev = rt2x00rfkill_allocate_polldev(rt2x00dev);
195         if (!rt2x00dev->poll_dev) {
196                 ERROR(rt2x00dev, "Failed to allocate polled device.\n");
197                 return;
198         }
199 }