rt2x00: Input-polldev requires input device
[linux-2.6] / 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 /*
27  * Set enviroment defines for rt2x00.h
28  */
29 #define DRV_NAME "rt2x00lib"
30
31 #include <linux/input-polldev.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/rfkill.h>
35
36 #include "rt2x00.h"
37 #include "rt2x00lib.h"
38
39 static int rt2x00rfkill_toggle_radio(void *data, enum rfkill_state state)
40 {
41         struct rt2x00_dev *rt2x00dev = data;
42         int retval = 0;
43
44         if (unlikely(!rt2x00dev))
45                 return 0;
46
47         /*
48          * Only continue if there are enabled interfaces.
49          */
50         if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
51                 return 0;
52
53         if (state == RFKILL_STATE_ON) {
54                 INFO(rt2x00dev, "Hardware button pressed, enabling radio.\n");
55                 __clear_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
56                 retval = rt2x00lib_enable_radio(rt2x00dev);
57         } else if (state == RFKILL_STATE_OFF) {
58                 INFO(rt2x00dev, "Hardware button pressed, disabling radio.\n");
59                 __set_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags);
60                 rt2x00lib_disable_radio(rt2x00dev);
61         }
62
63         return retval;
64 }
65
66 static void rt2x00rfkill_poll(struct input_polled_dev *poll_dev)
67 {
68         struct rt2x00_dev *rt2x00dev = poll_dev->private;
69         int state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
70
71         if (rt2x00dev->rfkill->state != state) {
72                 input_report_key(poll_dev->input, KEY_WLAN, 1);
73                 input_report_key(poll_dev->input, KEY_WLAN, 0);
74         }
75 }
76
77 int rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
78 {
79         int retval;
80
81         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
82                 return 0;
83
84         retval = rfkill_register(rt2x00dev->rfkill);
85         if (retval) {
86                 ERROR(rt2x00dev, "Failed to register rfkill handler.\n");
87                 return retval;
88         }
89
90         retval = input_register_polled_device(rt2x00dev->poll_dev);
91         if (retval) {
92                 ERROR(rt2x00dev, "Failed to register polled device.\n");
93                 rfkill_unregister(rt2x00dev->rfkill);
94                 return retval;
95         }
96
97         return 0;
98 }
99
100 void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
101 {
102         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
103                 return;
104
105         input_unregister_polled_device(rt2x00dev->poll_dev);
106         rfkill_unregister(rt2x00dev->rfkill);
107 }
108
109 int rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
110 {
111         struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
112
113         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
114                 return 0;
115
116         rt2x00dev->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
117         if (!rt2x00dev->rfkill) {
118                 ERROR(rt2x00dev, "Failed to allocate rfkill handler.\n");
119                 goto exit;
120         }
121
122         rt2x00dev->rfkill->name = rt2x00dev->ops->name;
123         rt2x00dev->rfkill->data = rt2x00dev;
124         rt2x00dev->rfkill->state = rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
125         rt2x00dev->rfkill->toggle_radio = rt2x00rfkill_toggle_radio;
126
127         rt2x00dev->poll_dev = input_allocate_polled_device();
128         if (!rt2x00dev->poll_dev) {
129                 ERROR(rt2x00dev, "Failed to allocate polled device.\n");
130                 goto exit_free_rfkill;
131         }
132
133         rt2x00dev->poll_dev->private = rt2x00dev;
134         rt2x00dev->poll_dev->poll = rt2x00rfkill_poll;
135         rt2x00dev->poll_dev->poll_interval = RFKILL_POLL_INTERVAL;
136
137         rt2x00dev->poll_dev->input = input_allocate_device();
138         if (!rt2x00dev->poll_dev->input) {
139                 ERROR(rt2x00dev, "Failed to allocate input device.\n");
140                 goto exit_free_polldev;
141         }
142
143         rt2x00dev->poll_dev->input->name = rt2x00dev->ops->name;
144         rt2x00dev->poll_dev->input->phys = wiphy_name(rt2x00dev->hw->wiphy);
145         rt2x00dev->poll_dev->input->id.bustype = BUS_HOST;
146         rt2x00dev->poll_dev->input->id.vendor = 0x1814;
147         rt2x00dev->poll_dev->input->id.product = rt2x00dev->chip.rt;
148         rt2x00dev->poll_dev->input->id.version = rt2x00dev->chip.rev;
149         rt2x00dev->poll_dev->input->dev.parent = device;
150         rt2x00dev->poll_dev->input->evbit[0] = BIT(EV_KEY);
151         set_bit(KEY_WLAN, rt2x00dev->poll_dev->input->keybit);
152
153         return 0;
154
155 exit_free_polldev:
156         input_free_polled_device(rt2x00dev->poll_dev);
157
158 exit_free_rfkill:
159         rfkill_free(rt2x00dev->rfkill);
160
161 exit:
162         return -ENOMEM;
163 }
164
165 void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
166 {
167         if (!test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
168                 return;
169
170         input_free_device(rt2x00dev->poll_dev->input);
171         input_free_polled_device(rt2x00dev->poll_dev);
172         rfkill_free(rt2x00dev->rfkill);
173 }