Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00rfkill.c
1 /*
2         Copyright (C) 2004 - 2008 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/kernel.h>
27 #include <linux/module.h>
28 #include <linux/rfkill.h>
29
30 #include "rt2x00.h"
31 #include "rt2x00lib.h"
32
33 static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
34 {
35         struct rt2x00_dev *rt2x00dev = data;
36         int retval = 0;
37
38         if (unlikely(!rt2x00dev))
39                 return 0;
40
41         /*
42          * Only continue if there are enabled interfaces.
43          */
44         if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
45                 return 0;
46
47         if (state == RFKILL_STATE_UNBLOCKED) {
48                 INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
49                 __clear_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
50                 retval = rt2x00lib_enable_radio(rt2x00dev);
51         } else if (state == RFKILL_STATE_SOFT_BLOCKED) {
52                 INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
53                 __set_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
54                 rt2x00lib_disable_radio(rt2x00dev);
55         } else {
56                 WARNING(rt2x00dev, "Received unexpected rfkill state %d.\n",
57                         state);
58         }
59
60         return retval;
61 }
62
63 static int rt2x00rfkill_get_state(void *data, enum rfkill_state *state)
64 {
65         struct rt2x00_dev *rt2x00dev = data;
66
67         *state = rt2x00dev->rfkill->state;
68
69         return 0;
70 }
71
72 static void rt2x00rfkill_poll(struct work_struct *work)
73 {
74         struct rt2x00_dev *rt2x00dev =
75             container_of(work, struct rt2x00_dev, rfkill_work.work);
76         int state;
77
78         if (!test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
79                 return;
80
81         /*
82          * rfkill_poll reports 1 when the key has been pressed and the
83          * radio should be blocked.
84          */
85         state = !rt2x00dev->ops->lib->rfkill_poll(rt2x00dev) ?
86             RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
87
88         rfkill_force_state(rt2x00dev->rfkill, state);
89
90         queue_delayed_work(rt2x00dev->hw->workqueue,
91                            &rt2x00dev->rfkill_work, RFKILL_POLL_INTERVAL);
92 }
93
94 void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
95 {
96         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
97             !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
98                 return;
99
100         if (rfkill_register(rt2x00dev->rfkill)) {
101                 ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
102                 return;
103         }
104
105         __set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
106
107         /*
108          * Force initial poll which will detect the initial device state,
109          * and correctly sends the signal to the rfkill layer about this
110          * state.
111          */
112         rt2x00rfkill_poll(&rt2x00dev->rfkill_work.work);
113 }
114
115 void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
116 {
117         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
118             !test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
119                 return;
120
121         cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
122
123         rfkill_unregister(rt2x00dev->rfkill);
124
125         __clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
126 }
127
128 void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
129 {
130         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
131                 return;
132
133         rt2x00dev->rfkill =
134             rfkill_allocate(wiphy_dev(rt2x00dev->hw->wiphy), RFKILL_TYPE_WLAN);
135         if (!rt2x00dev->rfkill) {
136                 ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
137                 return;
138         }
139
140         rt2x00dev->rfkill->name = rt2x00dev->ops->name;
141         rt2x00dev->rfkill->data = rt2x00dev;
142         rt2x00dev->rfkill->state = -1;
143         rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
144         rt2x00dev->rfkill->get_state = rt2x00rfkill_get_state;
145
146         INIT_DELAYED_WORK(&rt2x00dev->rfkill_work, rt2x00rfkill_poll);
147
148         return;
149 }
150
151 void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
152 {
153         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) ||
154             !test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state))
155                 return;
156
157         cancel_delayed_work_sync(&rt2x00dev->rfkill_work);
158
159         rfkill_free(rt2x00dev->rfkill);
160         rt2x00dev->rfkill = NULL;
161 }