Merge branch 'core/debugobjects' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00rfkill.c
1 /*
2         Copyright (C) 2004 - 2009 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
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
31
32 static void rt2x00rfkill_poll(struct input_polled_dev *poll_dev)
33 {
34         struct rt2x00_dev *rt2x00dev = poll_dev->private;
35         int state, old_state;
36
37         if (!test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state) ||
38             !test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
39                 return;
40
41         /*
42          * Poll latest state, if the state is different then the previous state,
43          * we should generate an input event.
44          */
45         state = !!rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
46         old_state = !!test_bit(RFKILL_STATE_BLOCKED, &rt2x00dev->rfkill_state);
47
48         if (old_state != state) {
49                 input_report_switch(poll_dev->input, SW_RFKILL_ALL, state);
50                 change_bit(RFKILL_STATE_BLOCKED, &rt2x00dev->rfkill_state);
51         }
52 }
53
54 void rt2x00rfkill_register(struct rt2x00_dev *rt2x00dev)
55 {
56         if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state) ||
57             test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
58                 return;
59
60         if (input_register_polled_device(rt2x00dev->rfkill_poll_dev)) {
61                 ERROR(rt2x00dev, "Failed to register polled device.\n");
62                 return;
63         }
64
65         __set_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
66
67         /*
68          * Force initial poll which will detect the initial device state,
69          * and correctly sends the signal to the input layer about this
70          * state.
71          */
72         rt2x00rfkill_poll(rt2x00dev->rfkill_poll_dev);
73 }
74
75 void rt2x00rfkill_unregister(struct rt2x00_dev *rt2x00dev)
76 {
77         if (!test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state) ||
78             !test_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state))
79                 return;
80
81         input_unregister_polled_device(rt2x00dev->rfkill_poll_dev);
82
83         __clear_bit(RFKILL_STATE_REGISTERED, &rt2x00dev->rfkill_state);
84 }
85
86 void rt2x00rfkill_allocate(struct rt2x00_dev *rt2x00dev)
87 {
88         struct input_polled_dev *poll_dev;
89
90         if (test_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state) ||
91             !test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags))
92                 return;
93
94         poll_dev = input_allocate_polled_device();
95         if (!poll_dev) {
96                 ERROR(rt2x00dev, "Failed to allocate polled device.\n");
97                 return;
98         }
99
100         poll_dev->private = rt2x00dev;
101         poll_dev->poll = rt2x00rfkill_poll;
102         poll_dev->poll_interval = RFKILL_POLL_INTERVAL;
103
104         poll_dev->input->name = rt2x00dev->ops->name;
105         poll_dev->input->phys = wiphy_name(rt2x00dev->hw->wiphy);
106         poll_dev->input->id.bustype = BUS_HOST;
107         poll_dev->input->id.vendor = 0x1814;
108         poll_dev->input->id.product = rt2x00dev->chip.rt;
109         poll_dev->input->id.version = rt2x00dev->chip.rev;
110         poll_dev->input->dev.parent = wiphy_dev(rt2x00dev->hw->wiphy);
111         poll_dev->input->evbit[0] = BIT(EV_SW);
112         poll_dev->input->swbit[0] = BIT(SW_RFKILL_ALL);
113
114         rt2x00dev->rfkill_poll_dev = poll_dev;
115
116         __set_bit(RFKILL_STATE_ALLOCATED, &rt2x00dev->rfkill_state);
117 }
118
119 void rt2x00rfkill_free(struct rt2x00_dev *rt2x00dev)
120 {
121         if (!__test_and_clear_bit(RFKILL_STATE_ALLOCATED,
122                                   &rt2x00dev->rfkill_state))
123                 return;
124
125         input_free_polled_device(rt2x00dev->rfkill_poll_dev);
126         rt2x00dev->rfkill_poll_dev = NULL;
127 }