diff options
author | Johannes Berg <johannes@sipsolutions.net> | 2009-06-02 07:01:37 -0400 |
---|---|---|
committer | John W. Linville <linville@tuxdriver.com> | 2009-06-03 14:06:13 -0400 |
commit | 19d337dff95cbf76edd3ad95c0cee2732c3e1ec5 (patch) | |
tree | 33326eeb09cb9664cc8427a5dc7cd2b08b5a57c3 /include/linux/rfkill.h | |
parent | 0f6399c4c525b518644a9b09f8d6fb125a418c4d (diff) |
rfkill: rewrite
This patch completely rewrites the rfkill core to address
the following deficiencies:
* all rfkill drivers need to implement polling where necessary
rather than having one central implementation
* updating the rfkill state cannot be done from arbitrary
contexts, forcing drivers to use schedule_work and requiring
lots of code
* rfkill drivers need to keep track of soft/hard blocked
internally -- the core should do this
* the rfkill API has many unexpected quirks, for example being
asymmetric wrt. alloc/free and register/unregister
* rfkill can call back into a driver from within a function the
driver called -- this is prone to deadlocks and generally
should be avoided
* rfkill-input pointlessly is a separate module
* drivers need to #ifdef rfkill functions (unless they want to
depend on or select RFKILL) -- rfkill should provide inlines
that do nothing if it isn't compiled in
* the rfkill structure is not opaque -- drivers need to initialise
it correctly (lots of sanity checking code required) -- instead
force drivers to pass the right variables to rfkill_alloc()
* the documentation is hard to read because it always assumes the
reader is completely clueless and contains way TOO MANY CAPS
* the rfkill code needlessly uses a lot of locks and atomic
operations in locked sections
* fix LED trigger to actually change the LED when the radio state
changes -- this wasn't done before
Tested-by: Alan Jenkins <alan-jenkins@tuffmail.co.uk>
Signed-off-by: Henrique de Moraes Holschuh <hmh@hmh.eng.br> [thinkpad]
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: John W. Linville <linville@tuxdriver.com>
Diffstat (limited to 'include/linux/rfkill.h')
-rw-r--r-- | include/linux/rfkill.h | 325 |
1 files changed, 251 insertions, 74 deletions
diff --git a/include/linux/rfkill.h b/include/linux/rfkill.h index de18ef227e00..090852c8de7a 100644 --- a/include/linux/rfkill.h +++ b/include/linux/rfkill.h | |||
@@ -4,6 +4,7 @@ | |||
4 | /* | 4 | /* |
5 | * Copyright (C) 2006 - 2007 Ivo van Doorn | 5 | * Copyright (C) 2006 - 2007 Ivo van Doorn |
6 | * Copyright (C) 2007 Dmitry Torokhov | 6 | * Copyright (C) 2007 Dmitry Torokhov |
7 | * Copyright 2009 Johannes Berg <johannes@sipsolutions.net> | ||
7 | * | 8 | * |
8 | * This program is free software; you can redistribute it and/or modify | 9 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | 10 | * it under the terms of the GNU General Public License as published by |
@@ -21,6 +22,24 @@ | |||
21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 22 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22 | */ | 23 | */ |
23 | 24 | ||
25 | |||
26 | /* define userspace visible states */ | ||
27 | #define RFKILL_STATE_SOFT_BLOCKED 0 | ||
28 | #define RFKILL_STATE_UNBLOCKED 1 | ||
29 | #define RFKILL_STATE_HARD_BLOCKED 2 | ||
30 | |||
31 | /* and that's all userspace gets */ | ||
32 | #ifdef __KERNEL__ | ||
33 | /* don't allow anyone to use these in the kernel */ | ||
34 | enum rfkill_user_states { | ||
35 | RFKILL_USER_STATE_SOFT_BLOCKED = RFKILL_STATE_SOFT_BLOCKED, | ||
36 | RFKILL_USER_STATE_UNBLOCKED = RFKILL_STATE_UNBLOCKED, | ||
37 | RFKILL_USER_STATE_HARD_BLOCKED = RFKILL_STATE_HARD_BLOCKED, | ||
38 | }; | ||
39 | #undef RFKILL_STATE_SOFT_BLOCKED | ||
40 | #undef RFKILL_STATE_UNBLOCKED | ||
41 | #undef RFKILL_STATE_HARD_BLOCKED | ||
42 | |||
24 | #include <linux/types.h> | 43 | #include <linux/types.h> |
25 | #include <linux/kernel.h> | 44 | #include <linux/kernel.h> |
26 | #include <linux/list.h> | 45 | #include <linux/list.h> |
@@ -30,109 +49,267 @@ | |||
30 | 49 | ||
31 | /** | 50 | /** |
32 | * enum rfkill_type - type of rfkill switch. | 51 | * enum rfkill_type - type of rfkill switch. |
33 | * RFKILL_TYPE_WLAN: switch is on a 802.11 wireless network device. | 52 | * |
34 | * RFKILL_TYPE_BLUETOOTH: switch is on a bluetooth device. | 53 | * @RFKILL_TYPE_WLAN: switch is on a 802.11 wireless network device. |
35 | * RFKILL_TYPE_UWB: switch is on a ultra wideband device. | 54 | * @RFKILL_TYPE_BLUETOOTH: switch is on a bluetooth device. |
36 | * RFKILL_TYPE_WIMAX: switch is on a WiMAX device. | 55 | * @RFKILL_TYPE_UWB: switch is on a ultra wideband device. |
37 | * RFKILL_TYPE_WWAN: switch is on a wireless WAN device. | 56 | * @RFKILL_TYPE_WIMAX: switch is on a WiMAX device. |
57 | * @RFKILL_TYPE_WWAN: switch is on a wireless WAN device. | ||
58 | * @NUM_RFKILL_TYPES: number of defined rfkill types | ||
38 | */ | 59 | */ |
39 | enum rfkill_type { | 60 | enum rfkill_type { |
40 | RFKILL_TYPE_WLAN , | 61 | RFKILL_TYPE_WLAN, |
41 | RFKILL_TYPE_BLUETOOTH, | 62 | RFKILL_TYPE_BLUETOOTH, |
42 | RFKILL_TYPE_UWB, | 63 | RFKILL_TYPE_UWB, |
43 | RFKILL_TYPE_WIMAX, | 64 | RFKILL_TYPE_WIMAX, |
44 | RFKILL_TYPE_WWAN, | 65 | RFKILL_TYPE_WWAN, |
45 | RFKILL_TYPE_MAX, | 66 | NUM_RFKILL_TYPES, |
46 | }; | 67 | }; |
47 | 68 | ||
48 | enum rfkill_state { | 69 | /* this is opaque */ |
49 | RFKILL_STATE_SOFT_BLOCKED = 0, /* Radio output blocked */ | 70 | struct rfkill; |
50 | RFKILL_STATE_UNBLOCKED = 1, /* Radio output allowed */ | 71 | |
51 | RFKILL_STATE_HARD_BLOCKED = 2, /* Output blocked, non-overrideable */ | 72 | /** |
52 | RFKILL_STATE_MAX, /* marker for last valid state */ | 73 | * struct rfkill_ops - rfkill driver methods |
74 | * | ||
75 | * @poll: poll the rfkill block state(s) -- only assign this method | ||
76 | * when you need polling. When called, simply call one of the | ||
77 | * rfkill_set{,_hw,_sw}_state family of functions. If the hw | ||
78 | * is getting unblocked you need to take into account the return | ||
79 | * value of those functions to make sure the software block is | ||
80 | * properly used. | ||
81 | * @query: query the rfkill block state(s) and call exactly one of the | ||
82 | * rfkill_set{,_hw,_sw}_state family of functions. Assign this | ||
83 | * method if input events can cause hardware state changes to make | ||
84 | * the rfkill core query your driver before setting a requested | ||
85 | * block. | ||
86 | * @set_block: turn the transmitter on (blocked == false) or off | ||
87 | * (blocked == true) -- this is called only while the transmitter | ||
88 | * is not hard-blocked, but note that the core's view of whether | ||
89 | * the transmitter is hard-blocked might differ from your driver's | ||
90 | * view due to race conditions, so it is possible that it is still | ||
91 | * called at the same time as you are calling rfkill_set_hw_state(). | ||
92 | * This callback must be assigned. | ||
93 | */ | ||
94 | struct rfkill_ops { | ||
95 | void (*poll)(struct rfkill *rfkill, void *data); | ||
96 | void (*query)(struct rfkill *rfkill, void *data); | ||
97 | int (*set_block)(void *data, bool blocked); | ||
53 | }; | 98 | }; |
54 | 99 | ||
100 | #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE) | ||
55 | /** | 101 | /** |
56 | * struct rfkill - rfkill control structure. | 102 | * rfkill_alloc - allocate rfkill structure |
57 | * @name: Name of the switch. | 103 | * @name: name of the struct -- the string is not copied internally |
58 | * @type: Radio type which the button controls, the value stored | 104 | * @parent: device that has rf switch on it |
59 | * here should be a value from enum rfkill_type. | 105 | * @type: type of the switch (RFKILL_TYPE_*) |
60 | * @state: State of the switch, "UNBLOCKED" means radio can operate. | 106 | * @ops: rfkill methods |
61 | * @mutex: Guards switch state transitions. It serializes callbacks | 107 | * @ops_data: data passed to each method |
62 | * and also protects the state. | 108 | * |
63 | * @data: Pointer to the RF button drivers private data which will be | 109 | * This function should be called by the transmitter driver to allocate an |
64 | * passed along when toggling radio state. | 110 | * rfkill structure. Returns %NULL on failure. |
65 | * @toggle_radio(): Mandatory handler to control state of the radio. | ||
66 | * only RFKILL_STATE_SOFT_BLOCKED and RFKILL_STATE_UNBLOCKED are | ||
67 | * valid parameters. | ||
68 | * @get_state(): handler to read current radio state from hardware, | ||
69 | * may be called from atomic context, should return 0 on success. | ||
70 | * Either this handler OR judicious use of rfkill_force_state() is | ||
71 | * MANDATORY for any driver capable of RFKILL_STATE_HARD_BLOCKED. | ||
72 | * @led_trigger: A LED trigger for this button's LED. | ||
73 | * @dev: Device structure integrating the switch into device tree. | ||
74 | * @node: Used to place switch into list of all switches known to the | ||
75 | * the system. | ||
76 | * | ||
77 | * This structure represents a RF switch located on a network device. | ||
78 | */ | 111 | */ |
79 | struct rfkill { | 112 | struct rfkill * __must_check rfkill_alloc(const char *name, |
80 | const char *name; | 113 | struct device *parent, |
81 | enum rfkill_type type; | 114 | const enum rfkill_type type, |
82 | 115 | const struct rfkill_ops *ops, | |
83 | /* the mutex serializes callbacks and also protects | 116 | void *ops_data); |
84 | * the state */ | ||
85 | struct mutex mutex; | ||
86 | enum rfkill_state state; | ||
87 | void *data; | ||
88 | int (*toggle_radio)(void *data, enum rfkill_state state); | ||
89 | int (*get_state)(void *data, enum rfkill_state *state); | ||
90 | 117 | ||
91 | #ifdef CONFIG_RFKILL_LEDS | 118 | /** |
92 | struct led_trigger led_trigger; | 119 | * rfkill_register - Register a rfkill structure. |
93 | #endif | 120 | * @rfkill: rfkill structure to be registered |
121 | * | ||
122 | * This function should be called by the transmitter driver to register | ||
123 | * the rfkill structure needs to be registered. Before calling this function | ||
124 | * the driver needs to be ready to service method calls from rfkill. | ||
125 | */ | ||
126 | int __must_check rfkill_register(struct rfkill *rfkill); | ||
94 | 127 | ||
95 | struct device dev; | 128 | /** |
96 | struct list_head node; | 129 | * rfkill_pause_polling(struct rfkill *rfkill) |
97 | enum rfkill_state state_for_resume; | 130 | * |
98 | }; | 131 | * Pause polling -- say transmitter is off for other reasons. |
99 | #define to_rfkill(d) container_of(d, struct rfkill, dev) | 132 | * NOTE: not necessary for suspend/resume -- in that case the |
133 | * core stops polling anyway | ||
134 | */ | ||
135 | void rfkill_pause_polling(struct rfkill *rfkill); | ||
100 | 136 | ||
101 | struct rfkill * __must_check rfkill_allocate(struct device *parent, | 137 | /** |
102 | enum rfkill_type type); | 138 | * rfkill_resume_polling(struct rfkill *rfkill) |
103 | void rfkill_free(struct rfkill *rfkill); | 139 | * |
104 | int __must_check rfkill_register(struct rfkill *rfkill); | 140 | * Pause polling -- say transmitter is off for other reasons. |
141 | * NOTE: not necessary for suspend/resume -- in that case the | ||
142 | * core stops polling anyway | ||
143 | */ | ||
144 | void rfkill_resume_polling(struct rfkill *rfkill); | ||
145 | |||
146 | |||
147 | /** | ||
148 | * rfkill_unregister - Unregister a rfkill structure. | ||
149 | * @rfkill: rfkill structure to be unregistered | ||
150 | * | ||
151 | * This function should be called by the network driver during device | ||
152 | * teardown to destroy rfkill structure. Until it returns, the driver | ||
153 | * needs to be able to service method calls. | ||
154 | */ | ||
105 | void rfkill_unregister(struct rfkill *rfkill); | 155 | void rfkill_unregister(struct rfkill *rfkill); |
106 | 156 | ||
107 | int rfkill_force_state(struct rfkill *rfkill, enum rfkill_state state); | 157 | /** |
108 | int rfkill_set_default(enum rfkill_type type, enum rfkill_state state); | 158 | * rfkill_destroy - free rfkill structure |
159 | * @rfkill: rfkill structure to be destroyed | ||
160 | * | ||
161 | * Destroys the rfkill structure. | ||
162 | */ | ||
163 | void rfkill_destroy(struct rfkill *rfkill); | ||
164 | |||
165 | /** | ||
166 | * rfkill_set_hw_state - Set the internal rfkill hardware block state | ||
167 | * @rfkill: pointer to the rfkill class to modify. | ||
168 | * @state: the current hardware block state to set | ||
169 | * | ||
170 | * rfkill drivers that get events when the hard-blocked state changes | ||
171 | * use this function to notify the rfkill core (and through that also | ||
172 | * userspace) of the current state -- they should also use this after | ||
173 | * resume if the state could have changed. | ||
174 | * | ||
175 | * You need not (but may) call this function if poll_state is assigned. | ||
176 | * | ||
177 | * This function can be called in any context, even from within rfkill | ||
178 | * callbacks. | ||
179 | * | ||
180 | * The function returns the combined block state (true if transmitter | ||
181 | * should be blocked) so that drivers need not keep track of the soft | ||
182 | * block state -- which they might not be able to. | ||
183 | */ | ||
184 | bool __must_check rfkill_set_hw_state(struct rfkill *rfkill, bool blocked); | ||
185 | |||
186 | /** | ||
187 | * rfkill_set_sw_state - Set the internal rfkill software block state | ||
188 | * @rfkill: pointer to the rfkill class to modify. | ||
189 | * @state: the current software block state to set | ||
190 | * | ||
191 | * rfkill drivers that get events when the soft-blocked state changes | ||
192 | * (yes, some platforms directly act on input but allow changing again) | ||
193 | * use this function to notify the rfkill core (and through that also | ||
194 | * userspace) of the current state -- they should also use this after | ||
195 | * resume if the state could have changed. | ||
196 | * | ||
197 | * This function can be called in any context, even from within rfkill | ||
198 | * callbacks. | ||
199 | * | ||
200 | * The function returns the combined block state (true if transmitter | ||
201 | * should be blocked). | ||
202 | */ | ||
203 | bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked); | ||
204 | |||
205 | /** | ||
206 | * rfkill_set_states - Set the internal rfkill block states | ||
207 | * @rfkill: pointer to the rfkill class to modify. | ||
208 | * @sw: the current software block state to set | ||
209 | * @hw: the current hardware block state to set | ||
210 | * | ||
211 | * This function can be called in any context, even from within rfkill | ||
212 | * callbacks. | ||
213 | */ | ||
214 | void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw); | ||
109 | 215 | ||
110 | /** | 216 | /** |
111 | * rfkill_state_complement - return complementar state | 217 | * rfkill_set_global_sw_state - set global sw block default |
112 | * @state: state to return the complement of | 218 | * @type: rfkill type to set default for |
219 | * @blocked: default to set | ||
113 | * | 220 | * |
114 | * Returns RFKILL_STATE_SOFT_BLOCKED if @state is RFKILL_STATE_UNBLOCKED, | 221 | * This function sets the global default -- use at boot if your platform has |
115 | * returns RFKILL_STATE_UNBLOCKED otherwise. | 222 | * an rfkill switch. If not early enough this call may be ignored. |
223 | * | ||
224 | * XXX: instead of ignoring -- how about just updating all currently | ||
225 | * registered drivers? | ||
116 | */ | 226 | */ |
117 | static inline enum rfkill_state rfkill_state_complement(enum rfkill_state state) | 227 | void rfkill_set_global_sw_state(const enum rfkill_type type, bool blocked); |
228 | #else /* !RFKILL */ | ||
229 | static inline struct rfkill * __must_check | ||
230 | rfkill_alloc(const char *name, | ||
231 | struct device *parent, | ||
232 | const enum rfkill_type type, | ||
233 | const struct rfkill_ops *ops, | ||
234 | void *ops_data) | ||
235 | { | ||
236 | return ERR_PTR(-ENODEV); | ||
237 | } | ||
238 | |||
239 | static inline int __must_check rfkill_register(struct rfkill *rfkill) | ||
240 | { | ||
241 | if (rfkill == ERR_PTR(-ENODEV)) | ||
242 | return 0; | ||
243 | return -EINVAL; | ||
244 | } | ||
245 | |||
246 | static inline void rfkill_pause_polling(struct rfkill *rfkill) | ||
247 | { | ||
248 | } | ||
249 | |||
250 | static inline void rfkill_resume_polling(struct rfkill *rfkill) | ||
251 | { | ||
252 | } | ||
253 | |||
254 | static inline void rfkill_unregister(struct rfkill *rfkill) | ||
255 | { | ||
256 | } | ||
257 | |||
258 | static inline void rfkill_destroy(struct rfkill *rfkill) | ||
259 | { | ||
260 | } | ||
261 | |||
262 | static inline bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked) | ||
263 | { | ||
264 | return blocked; | ||
265 | } | ||
266 | |||
267 | static inline bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked) | ||
268 | { | ||
269 | return blocked; | ||
270 | } | ||
271 | |||
272 | static inline void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw) | ||
273 | { | ||
274 | } | ||
275 | |||
276 | static inline void rfkill_set_global_sw_state(const enum rfkill_type type, | ||
277 | bool blocked) | ||
118 | { | 278 | { |
119 | return (state == RFKILL_STATE_UNBLOCKED) ? | ||
120 | RFKILL_STATE_SOFT_BLOCKED : RFKILL_STATE_UNBLOCKED; | ||
121 | } | 279 | } |
280 | #endif /* RFKILL || RFKILL_MODULE */ | ||
281 | |||
122 | 282 | ||
283 | #ifdef CONFIG_RFKILL_LEDS | ||
123 | /** | 284 | /** |
124 | * rfkill_get_led_name - Get the LED trigger name for the button's LED. | 285 | * rfkill_get_led_trigger_name - Get the LED trigger name for the button's LED. |
125 | * This function might return a NULL pointer if registering of the | 286 | * This function might return a NULL pointer if registering of the |
126 | * LED trigger failed. | 287 | * LED trigger failed. Use this as "default_trigger" for the LED. |
127 | * Use this as "default_trigger" for the LED. | ||
128 | */ | 288 | */ |
129 | static inline char *rfkill_get_led_name(struct rfkill *rfkill) | 289 | const char *rfkill_get_led_trigger_name(struct rfkill *rfkill); |
130 | { | 290 | |
131 | #ifdef CONFIG_RFKILL_LEDS | 291 | /** |
132 | return (char *)(rfkill->led_trigger.name); | 292 | * rfkill_set_led_trigger_name -- set the LED trigger name |
293 | * @rfkill: rfkill struct | ||
294 | * @name: LED trigger name | ||
295 | * | ||
296 | * This function sets the LED trigger name of the radio LED | ||
297 | * trigger that rfkill creates. It is optional, but if called | ||
298 | * must be called before rfkill_register() to be effective. | ||
299 | */ | ||
300 | void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name); | ||
133 | #else | 301 | #else |
302 | static inline const char *rfkill_get_led_trigger_name(struct rfkill *rfkill) | ||
303 | { | ||
134 | return NULL; | 304 | return NULL; |
135 | #endif | ||
136 | } | 305 | } |
137 | 306 | ||
307 | static inline void | ||
308 | rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name) | ||
309 | { | ||
310 | } | ||
311 | #endif | ||
312 | |||
313 | #endif /* __KERNEL__ */ | ||
314 | |||
138 | #endif /* RFKILL_H */ | 315 | #endif /* RFKILL_H */ |