OMAP3: SR: Fix init voltage on OPP change
[linux-ginger.git] / net / wimax / op-rfkill.c
blob70ef4df863b974ef4ad0e199b4e4fb424b96a892
1 /*
2 * Linux WiMAX
3 * RF-kill framework integration
6 * Copyright (C) 2008 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 * This integrates into the Linux Kernel rfkill susbystem so that the
25 * drivers just have to do the bare minimal work, which is providing a
26 * method to set the software RF-Kill switch and to report changes in
27 * the software and hardware switch status.
29 * A non-polled generic rfkill device is embedded into the WiMAX
30 * subsystem's representation of a device.
32 * FIXME: Need polled support? Let drivers provide a poll routine
33 * and hand it to rfkill ops then?
35 * All device drivers have to do is after wimax_dev_init(), call
36 * wimax_report_rfkill_hw() and wimax_report_rfkill_sw() to update
37 * initial state and then every time it changes. See wimax.h:struct
38 * wimax_dev for more information.
40 * ROADMAP
42 * wimax_gnl_doit_rfkill() User space calling wimax_rfkill()
43 * wimax_rfkill() Kernel calling wimax_rfkill()
44 * __wimax_rf_toggle_radio()
46 * wimax_rfkill_set_radio_block() RF-Kill subsytem calling
47 * __wimax_rf_toggle_radio()
49 * __wimax_rf_toggle_radio()
50 * wimax_dev->op_rfkill_sw_toggle() Driver backend
51 * __wimax_state_change()
53 * wimax_report_rfkill_sw() Driver reports state change
54 * __wimax_state_change()
56 * wimax_report_rfkill_hw() Driver reports state change
57 * __wimax_state_change()
59 * wimax_rfkill_add() Initialize/shutdown rfkill support
60 * wimax_rfkill_rm() [called by wimax_dev_add/rm()]
63 #include <net/wimax.h>
64 #include <net/genetlink.h>
65 #include <linux/wimax.h>
66 #include <linux/security.h>
67 #include <linux/rfkill.h>
68 #include "wimax-internal.h"
70 #define D_SUBMODULE op_rfkill
71 #include "debug-levels.h"
73 /**
74 * wimax_report_rfkill_hw - Reports changes in the hardware RF switch
76 * @wimax_dev: WiMAX device descriptor
78 * @state: New state of the RF Kill switch. %WIMAX_RF_ON radio on,
79 * %WIMAX_RF_OFF radio off.
81 * When the device detects a change in the state of thehardware RF
82 * switch, it must call this function to let the WiMAX kernel stack
83 * know that the state has changed so it can be properly propagated.
85 * The WiMAX stack caches the state (the driver doesn't need to). As
86 * well, as the change is propagated it will come back as a request to
87 * change the software state to mirror the hardware state.
89 * If the device doesn't have a hardware kill switch, just report
90 * it on initialization as always on (%WIMAX_RF_ON, radio on).
92 void wimax_report_rfkill_hw(struct wimax_dev *wimax_dev,
93 enum wimax_rf_state state)
95 int result;
96 struct device *dev = wimax_dev_to_dev(wimax_dev);
97 enum wimax_st wimax_state;
99 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
100 BUG_ON(state == WIMAX_RF_QUERY);
101 BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF);
103 mutex_lock(&wimax_dev->mutex);
104 result = wimax_dev_is_ready(wimax_dev);
105 if (result < 0)
106 goto error_not_ready;
108 if (state != wimax_dev->rf_hw) {
109 wimax_dev->rf_hw = state;
110 if (wimax_dev->rf_hw == WIMAX_RF_ON
111 && wimax_dev->rf_sw == WIMAX_RF_ON)
112 wimax_state = WIMAX_ST_READY;
113 else
114 wimax_state = WIMAX_ST_RADIO_OFF;
116 result = rfkill_set_hw_state(wimax_dev->rfkill,
117 state == WIMAX_RF_OFF);
119 __wimax_state_change(wimax_dev, wimax_state);
121 error_not_ready:
122 mutex_unlock(&wimax_dev->mutex);
123 d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
124 wimax_dev, state, result);
126 EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw);
130 * wimax_report_rfkill_sw - Reports changes in the software RF switch
132 * @wimax_dev: WiMAX device descriptor
134 * @state: New state of the RF kill switch. %WIMAX_RF_ON radio on,
135 * %WIMAX_RF_OFF radio off.
137 * Reports changes in the software RF switch state to the the WiMAX
138 * stack.
140 * The main use is during initialization, so the driver can query the
141 * device for its current software radio kill switch state and feed it
142 * to the system.
144 * On the side, the device does not change the software state by
145 * itself. In practice, this can happen, as the device might decide to
146 * switch (in software) the radio off for different reasons.
148 void wimax_report_rfkill_sw(struct wimax_dev *wimax_dev,
149 enum wimax_rf_state state)
151 int result;
152 struct device *dev = wimax_dev_to_dev(wimax_dev);
153 enum wimax_st wimax_state;
155 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
156 BUG_ON(state == WIMAX_RF_QUERY);
157 BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF);
159 mutex_lock(&wimax_dev->mutex);
160 result = wimax_dev_is_ready(wimax_dev);
161 if (result < 0)
162 goto error_not_ready;
164 if (state != wimax_dev->rf_sw) {
165 wimax_dev->rf_sw = state;
166 if (wimax_dev->rf_hw == WIMAX_RF_ON
167 && wimax_dev->rf_sw == WIMAX_RF_ON)
168 wimax_state = WIMAX_ST_READY;
169 else
170 wimax_state = WIMAX_ST_RADIO_OFF;
171 __wimax_state_change(wimax_dev, wimax_state);
172 rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
174 error_not_ready:
175 mutex_unlock(&wimax_dev->mutex);
176 d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
177 wimax_dev, state, result);
179 EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw);
183 * Callback for the RF Kill toggle operation
185 * This function is called by:
187 * - The rfkill subsystem when the RF-Kill key is pressed in the
188 * hardware and the driver notifies through
189 * wimax_report_rfkill_hw(). The rfkill subsystem ends up calling back
190 * here so the software RF Kill switch state is changed to reflect
191 * the hardware switch state.
193 * - When the user sets the state through sysfs' rfkill/state file
195 * - When the user calls wimax_rfkill().
197 * This call blocks!
199 * WARNING! When we call rfkill_unregister(), this will be called with
200 * state 0!
202 * WARNING: wimax_dev must be locked
204 static
205 int __wimax_rf_toggle_radio(struct wimax_dev *wimax_dev,
206 enum wimax_rf_state state)
208 int result = 0;
209 struct device *dev = wimax_dev_to_dev(wimax_dev);
210 enum wimax_st wimax_state;
212 might_sleep();
213 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
214 if (wimax_dev->rf_sw == state)
215 goto out_no_change;
216 if (wimax_dev->op_rfkill_sw_toggle != NULL)
217 result = wimax_dev->op_rfkill_sw_toggle(wimax_dev, state);
218 else if (state == WIMAX_RF_OFF) /* No op? can't turn off */
219 result = -ENXIO;
220 else /* No op? can turn on */
221 result = 0; /* should never happen tho */
222 if (result >= 0) {
223 result = 0;
224 wimax_dev->rf_sw = state;
225 wimax_state = state == WIMAX_RF_ON ?
226 WIMAX_ST_READY : WIMAX_ST_RADIO_OFF;
227 __wimax_state_change(wimax_dev, wimax_state);
229 out_no_change:
230 d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
231 wimax_dev, state, result);
232 return result;
237 * Translate from rfkill state to wimax state
239 * NOTE: Special state handling rules here
241 * Just pretend the call didn't happen if we are in a state where
242 * we know for sure it cannot be handled (WIMAX_ST_DOWN or
243 * __WIMAX_ST_QUIESCING). rfkill() needs it to register and
244 * unregister, as it will run this path.
246 * NOTE: This call will block until the operation is completed.
248 static int wimax_rfkill_set_radio_block(void *data, bool blocked)
250 int result;
251 struct wimax_dev *wimax_dev = data;
252 struct device *dev = wimax_dev_to_dev(wimax_dev);
253 enum wimax_rf_state rf_state;
255 d_fnstart(3, dev, "(wimax_dev %p blocked %u)\n", wimax_dev, blocked);
256 rf_state = WIMAX_RF_ON;
257 if (blocked)
258 rf_state = WIMAX_RF_OFF;
259 mutex_lock(&wimax_dev->mutex);
260 if (wimax_dev->state <= __WIMAX_ST_QUIESCING)
261 result = 0;
262 else
263 result = __wimax_rf_toggle_radio(wimax_dev, rf_state);
264 mutex_unlock(&wimax_dev->mutex);
265 d_fnend(3, dev, "(wimax_dev %p blocked %u) = %d\n",
266 wimax_dev, blocked, result);
267 return result;
270 static const struct rfkill_ops wimax_rfkill_ops = {
271 .set_block = wimax_rfkill_set_radio_block,
275 * wimax_rfkill - Set the software RF switch state for a WiMAX device
277 * @wimax_dev: WiMAX device descriptor
279 * @state: New RF state.
281 * Returns:
283 * >= 0 toggle state if ok, < 0 errno code on error. The toggle state
284 * is returned as a bitmap, bit 0 being the hardware RF state, bit 1
285 * the software RF state.
287 * 0 means disabled (%WIMAX_RF_ON, radio on), 1 means enabled radio
288 * off (%WIMAX_RF_OFF).
290 * Description:
292 * Called by the user when he wants to request the WiMAX radio to be
293 * switched on (%WIMAX_RF_ON) or off (%WIMAX_RF_OFF). With
294 * %WIMAX_RF_QUERY, just the current state is returned.
296 * NOTE:
298 * This call will block until the operation is complete.
300 int wimax_rfkill(struct wimax_dev *wimax_dev, enum wimax_rf_state state)
302 int result;
303 struct device *dev = wimax_dev_to_dev(wimax_dev);
305 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
306 mutex_lock(&wimax_dev->mutex);
307 result = wimax_dev_is_ready(wimax_dev);
308 if (result < 0)
309 goto error_not_ready;
310 switch (state) {
311 case WIMAX_RF_ON:
312 case WIMAX_RF_OFF:
313 result = __wimax_rf_toggle_radio(wimax_dev, state);
314 if (result < 0)
315 goto error;
316 rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
317 break;
318 case WIMAX_RF_QUERY:
319 break;
320 default:
321 result = -EINVAL;
322 goto error;
324 result = wimax_dev->rf_sw << 1 | wimax_dev->rf_hw;
325 error:
326 error_not_ready:
327 mutex_unlock(&wimax_dev->mutex);
328 d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
329 wimax_dev, state, result);
330 return result;
332 EXPORT_SYMBOL(wimax_rfkill);
336 * Register a new WiMAX device's RF Kill support
338 * WARNING: wimax_dev->mutex must be unlocked
340 int wimax_rfkill_add(struct wimax_dev *wimax_dev)
342 int result;
343 struct rfkill *rfkill;
344 struct device *dev = wimax_dev_to_dev(wimax_dev);
346 d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
347 /* Initialize RF Kill */
348 result = -ENOMEM;
349 rfkill = rfkill_alloc(wimax_dev->name, dev, RFKILL_TYPE_WIMAX,
350 &wimax_rfkill_ops, wimax_dev);
351 if (rfkill == NULL)
352 goto error_rfkill_allocate;
354 d_printf(1, dev, "rfkill %p\n", rfkill);
356 wimax_dev->rfkill = rfkill;
358 result = rfkill_register(wimax_dev->rfkill);
359 if (result < 0)
360 goto error_rfkill_register;
362 /* If there is no SW toggle op, SW RFKill is always on */
363 if (wimax_dev->op_rfkill_sw_toggle == NULL)
364 wimax_dev->rf_sw = WIMAX_RF_ON;
366 d_fnend(3, dev, "(wimax_dev %p) = 0\n", wimax_dev);
367 return 0;
369 error_rfkill_register:
370 rfkill_destroy(wimax_dev->rfkill);
371 error_rfkill_allocate:
372 d_fnend(3, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
373 return result;
378 * Deregister a WiMAX device's RF Kill support
380 * Ick, we can't call rfkill_free() after rfkill_unregister()...oh
381 * well.
383 * WARNING: wimax_dev->mutex must be unlocked
385 void wimax_rfkill_rm(struct wimax_dev *wimax_dev)
387 struct device *dev = wimax_dev_to_dev(wimax_dev);
388 d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
389 rfkill_unregister(wimax_dev->rfkill);
390 rfkill_destroy(wimax_dev->rfkill);
391 d_fnend(3, dev, "(wimax_dev %p)\n", wimax_dev);
396 * Exporting to user space over generic netlink
398 * Parse the rfkill command from user space, return a combination
399 * value that describe the states of the different toggles.
401 * Only one attribute: the new state requested (on, off or no change,
402 * just query).
405 static const
406 struct nla_policy wimax_gnl_rfkill_policy[WIMAX_GNL_ATTR_MAX + 1] = {
407 [WIMAX_GNL_RFKILL_IFIDX] = {
408 .type = NLA_U32,
410 [WIMAX_GNL_RFKILL_STATE] = {
411 .type = NLA_U32 /* enum wimax_rf_state */
416 static
417 int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info)
419 int result, ifindex;
420 struct wimax_dev *wimax_dev;
421 struct device *dev;
422 enum wimax_rf_state new_state;
424 d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
425 result = -ENODEV;
426 if (info->attrs[WIMAX_GNL_RFKILL_IFIDX] == NULL) {
427 printk(KERN_ERR "WIMAX_GNL_OP_RFKILL: can't find IFIDX "
428 "attribute\n");
429 goto error_no_wimax_dev;
431 ifindex = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_IFIDX]);
432 wimax_dev = wimax_dev_get_by_genl_info(info, ifindex);
433 if (wimax_dev == NULL)
434 goto error_no_wimax_dev;
435 dev = wimax_dev_to_dev(wimax_dev);
436 result = -EINVAL;
437 if (info->attrs[WIMAX_GNL_RFKILL_STATE] == NULL) {
438 dev_err(dev, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE "
439 "attribute\n");
440 goto error_no_pid;
442 new_state = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_STATE]);
444 /* Execute the operation and send the result back to user space */
445 result = wimax_rfkill(wimax_dev, new_state);
446 error_no_pid:
447 dev_put(wimax_dev->net_dev);
448 error_no_wimax_dev:
449 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
450 return result;
454 struct genl_ops wimax_gnl_rfkill = {
455 .cmd = WIMAX_GNL_OP_RFKILL,
456 .flags = GENL_ADMIN_PERM,
457 .policy = wimax_gnl_rfkill_policy,
458 .doit = wimax_gnl_doit_rfkill,
459 .dumpit = NULL,