]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/wimax/op-rfkill.c
rfkill: rewrite
[mirror_ubuntu-zesty-kernel.git] / net / wimax / op-rfkill.c
CommitLineData
3e65646b
IPG
1/*
2 * Linux WiMAX
3 * RF-kill framework integration
4 *
5 *
6 * Copyright (C) 2008 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8 *
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.
12 *
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.
17 *
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.
22 *
23 *
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.
28 *
29 * A non-polled generic rfkill device is embedded into the WiMAX
30 * subsystem's representation of a device.
31 *
19d337df
JB
32 * FIXME: Need polled support? Let drivers provide a poll routine
33 * and hand it to rfkill ops then?
3e65646b
IPG
34 *
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.
39 *
40 * ROADMAP
41 *
42 * wimax_gnl_doit_rfkill() User space calling wimax_rfkill()
43 * wimax_rfkill() Kernel calling wimax_rfkill()
44 * __wimax_rf_toggle_radio()
45 *
19d337df 46 * wimax_rfkill_set_radio_block() RF-Kill subsytem calling
3e65646b
IPG
47 * __wimax_rf_toggle_radio()
48 *
49 * __wimax_rf_toggle_radio()
50 * wimax_dev->op_rfkill_sw_toggle() Driver backend
51 * __wimax_state_change()
52 *
53 * wimax_report_rfkill_sw() Driver reports state change
54 * __wimax_state_change()
55 *
56 * wimax_report_rfkill_hw() Driver reports state change
57 * __wimax_state_change()
58 *
59 * wimax_rfkill_add() Initialize/shutdown rfkill support
60 * wimax_rfkill_rm() [called by wimax_dev_add/rm()]
61 */
62
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>
3e65646b
IPG
68#include "wimax-internal.h"
69
70#define D_SUBMODULE op_rfkill
71#include "debug-levels.h"
72
3e65646b
IPG
73/**
74 * wimax_report_rfkill_hw - Reports changes in the hardware RF switch
75 *
76 * @wimax_dev: WiMAX device descriptor
77 *
78 * @state: New state of the RF Kill switch. %WIMAX_RF_ON radio on,
79 * %WIMAX_RF_OFF radio off.
80 *
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.
84 *
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.
88 *
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).
91 */
92void wimax_report_rfkill_hw(struct wimax_dev *wimax_dev,
93 enum wimax_rf_state state)
94{
95 int result;
96 struct device *dev = wimax_dev_to_dev(wimax_dev);
97 enum wimax_st wimax_state;
3e65646b
IPG
98
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);
102
103 mutex_lock(&wimax_dev->mutex);
104 result = wimax_dev_is_ready(wimax_dev);
105 if (result < 0)
106 goto error_not_ready;
107
108 if (state != wimax_dev->rf_hw) {
109 wimax_dev->rf_hw = state;
3e65646b
IPG
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;
19d337df
JB
115
116 rfkill_set_hw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
117
3e65646b 118 __wimax_state_change(wimax_dev, wimax_state);
3e65646b
IPG
119 }
120error_not_ready:
121 mutex_unlock(&wimax_dev->mutex);
122 d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
123 wimax_dev, state, result);
124}
125EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw);
126
127
128/**
129 * wimax_report_rfkill_sw - Reports changes in the software RF switch
130 *
131 * @wimax_dev: WiMAX device descriptor
132 *
133 * @state: New state of the RF kill switch. %WIMAX_RF_ON radio on,
134 * %WIMAX_RF_OFF radio off.
135 *
136 * Reports changes in the software RF switch state to the the WiMAX
137 * stack.
138 *
139 * The main use is during initialization, so the driver can query the
140 * device for its current software radio kill switch state and feed it
141 * to the system.
142 *
143 * On the side, the device does not change the software state by
144 * itself. In practice, this can happen, as the device might decide to
145 * switch (in software) the radio off for different reasons.
146 */
147void wimax_report_rfkill_sw(struct wimax_dev *wimax_dev,
148 enum wimax_rf_state state)
149{
150 int result;
151 struct device *dev = wimax_dev_to_dev(wimax_dev);
152 enum wimax_st wimax_state;
153
154 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
155 BUG_ON(state == WIMAX_RF_QUERY);
156 BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF);
157
158 mutex_lock(&wimax_dev->mutex);
159 result = wimax_dev_is_ready(wimax_dev);
160 if (result < 0)
161 goto error_not_ready;
162
163 if (state != wimax_dev->rf_sw) {
164 wimax_dev->rf_sw = state;
165 if (wimax_dev->rf_hw == WIMAX_RF_ON
166 && wimax_dev->rf_sw == WIMAX_RF_ON)
167 wimax_state = WIMAX_ST_READY;
168 else
169 wimax_state = WIMAX_ST_RADIO_OFF;
170 __wimax_state_change(wimax_dev, wimax_state);
19d337df 171 rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
3e65646b
IPG
172 }
173error_not_ready:
174 mutex_unlock(&wimax_dev->mutex);
175 d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
176 wimax_dev, state, result);
177}
178EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw);
179
180
181/*
182 * Callback for the RF Kill toggle operation
183 *
184 * This function is called by:
185 *
186 * - The rfkill subsystem when the RF-Kill key is pressed in the
187 * hardware and the driver notifies through
188 * wimax_report_rfkill_hw(). The rfkill subsystem ends up calling back
189 * here so the software RF Kill switch state is changed to reflect
190 * the hardware switch state.
191 *
192 * - When the user sets the state through sysfs' rfkill/state file
193 *
194 * - When the user calls wimax_rfkill().
195 *
196 * This call blocks!
197 *
198 * WARNING! When we call rfkill_unregister(), this will be called with
199 * state 0!
200 *
201 * WARNING: wimax_dev must be locked
202 */
203static
204int __wimax_rf_toggle_radio(struct wimax_dev *wimax_dev,
205 enum wimax_rf_state state)
206{
207 int result = 0;
208 struct device *dev = wimax_dev_to_dev(wimax_dev);
209 enum wimax_st wimax_state;
210
211 might_sleep();
212 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
213 if (wimax_dev->rf_sw == state)
214 goto out_no_change;
215 if (wimax_dev->op_rfkill_sw_toggle != NULL)
216 result = wimax_dev->op_rfkill_sw_toggle(wimax_dev, state);
217 else if (state == WIMAX_RF_OFF) /* No op? can't turn off */
218 result = -ENXIO;
219 else /* No op? can turn on */
220 result = 0; /* should never happen tho */
221 if (result >= 0) {
222 result = 0;
223 wimax_dev->rf_sw = state;
224 wimax_state = state == WIMAX_RF_ON ?
225 WIMAX_ST_READY : WIMAX_ST_RADIO_OFF;
226 __wimax_state_change(wimax_dev, wimax_state);
227 }
228out_no_change:
229 d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
230 wimax_dev, state, result);
231 return result;
232}
233
234
235/*
236 * Translate from rfkill state to wimax state
237 *
238 * NOTE: Special state handling rules here
239 *
240 * Just pretend the call didn't happen if we are in a state where
241 * we know for sure it cannot be handled (WIMAX_ST_DOWN or
242 * __WIMAX_ST_QUIESCING). rfkill() needs it to register and
243 * unregister, as it will run this path.
244 *
245 * NOTE: This call will block until the operation is completed.
246 */
19d337df 247static int wimax_rfkill_set_radio_block(void *data, bool blocked)
3e65646b
IPG
248{
249 int result;
250 struct wimax_dev *wimax_dev = data;
251 struct device *dev = wimax_dev_to_dev(wimax_dev);
252 enum wimax_rf_state rf_state;
253
19d337df
JB
254 d_fnstart(3, dev, "(wimax_dev %p blocked %u)\n", wimax_dev, blocked);
255 rf_state = WIMAX_RF_ON;
256 if (blocked)
3e65646b 257 rf_state = WIMAX_RF_OFF;
3e65646b
IPG
258 mutex_lock(&wimax_dev->mutex);
259 if (wimax_dev->state <= __WIMAX_ST_QUIESCING)
19d337df 260 result = 0;
3e65646b
IPG
261 else
262 result = __wimax_rf_toggle_radio(wimax_dev, rf_state);
263 mutex_unlock(&wimax_dev->mutex);
19d337df
JB
264 d_fnend(3, dev, "(wimax_dev %p blocked %u) = %d\n",
265 wimax_dev, blocked, result);
3e65646b
IPG
266 return result;
267}
268
19d337df
JB
269static const struct rfkill_ops wimax_rfkill_ops = {
270 .set_block = wimax_rfkill_set_radio_block,
271};
3e65646b
IPG
272
273/**
274 * wimax_rfkill - Set the software RF switch state for a WiMAX device
275 *
276 * @wimax_dev: WiMAX device descriptor
277 *
278 * @state: New RF state.
279 *
280 * Returns:
281 *
282 * >= 0 toggle state if ok, < 0 errno code on error. The toggle state
283 * is returned as a bitmap, bit 0 being the hardware RF state, bit 1
284 * the software RF state.
285 *
286 * 0 means disabled (%WIMAX_RF_ON, radio on), 1 means enabled radio
287 * off (%WIMAX_RF_OFF).
288 *
289 * Description:
290 *
291 * Called by the user when he wants to request the WiMAX radio to be
292 * switched on (%WIMAX_RF_ON) or off (%WIMAX_RF_OFF). With
293 * %WIMAX_RF_QUERY, just the current state is returned.
294 *
295 * NOTE:
296 *
297 * This call will block until the operation is complete.
298 */
299int wimax_rfkill(struct wimax_dev *wimax_dev, enum wimax_rf_state state)
300{
301 int result;
302 struct device *dev = wimax_dev_to_dev(wimax_dev);
303
304 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
305 mutex_lock(&wimax_dev->mutex);
306 result = wimax_dev_is_ready(wimax_dev);
307 if (result < 0)
308 goto error_not_ready;
309 switch (state) {
310 case WIMAX_RF_ON:
311 case WIMAX_RF_OFF:
312 result = __wimax_rf_toggle_radio(wimax_dev, state);
313 if (result < 0)
314 goto error;
19d337df 315 rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
3e65646b
IPG
316 break;
317 case WIMAX_RF_QUERY:
318 break;
319 default:
320 result = -EINVAL;
321 goto error;
322 }
323 result = wimax_dev->rf_sw << 1 | wimax_dev->rf_hw;
324error:
325error_not_ready:
326 mutex_unlock(&wimax_dev->mutex);
327 d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
328 wimax_dev, state, result);
329 return result;
330}
331EXPORT_SYMBOL(wimax_rfkill);
332
333
334/*
335 * Register a new WiMAX device's RF Kill support
336 *
337 * WARNING: wimax_dev->mutex must be unlocked
338 */
339int wimax_rfkill_add(struct wimax_dev *wimax_dev)
340{
341 int result;
342 struct rfkill *rfkill;
3e65646b
IPG
343 struct device *dev = wimax_dev_to_dev(wimax_dev);
344
345 d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
346 /* Initialize RF Kill */
347 result = -ENOMEM;
19d337df
JB
348 rfkill = rfkill_alloc(wimax_dev->name, dev, RFKILL_TYPE_WIMAX,
349 &wimax_rfkill_ops, wimax_dev);
3e65646b
IPG
350 if (rfkill == NULL)
351 goto error_rfkill_allocate;
19d337df
JB
352
353 d_printf(1, dev, "rfkill %p\n", rfkill);
354
3e65646b
IPG
355 wimax_dev->rfkill = rfkill;
356
3e65646b
IPG
357 result = rfkill_register(wimax_dev->rfkill);
358 if (result < 0)
359 goto error_rfkill_register;
360
361 /* If there is no SW toggle op, SW RFKill is always on */
362 if (wimax_dev->op_rfkill_sw_toggle == NULL)
363 wimax_dev->rf_sw = WIMAX_RF_ON;
364
365 d_fnend(3, dev, "(wimax_dev %p) = 0\n", wimax_dev);
366 return 0;
367
3e65646b 368error_rfkill_register:
19d337df 369 rfkill_destroy(wimax_dev->rfkill);
3e65646b
IPG
370error_rfkill_allocate:
371 d_fnend(3, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
372 return result;
373}
374
375
376/*
377 * Deregister a WiMAX device's RF Kill support
378 *
379 * Ick, we can't call rfkill_free() after rfkill_unregister()...oh
380 * well.
381 *
382 * WARNING: wimax_dev->mutex must be unlocked
383 */
384void wimax_rfkill_rm(struct wimax_dev *wimax_dev)
385{
386 struct device *dev = wimax_dev_to_dev(wimax_dev);
387 d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
19d337df
JB
388 rfkill_unregister(wimax_dev->rfkill);
389 rfkill_destroy(wimax_dev->rfkill);
3e65646b
IPG
390 d_fnend(3, dev, "(wimax_dev %p)\n", wimax_dev);
391}
392
393
3e65646b
IPG
394/*
395 * Exporting to user space over generic netlink
396 *
397 * Parse the rfkill command from user space, return a combination
398 * value that describe the states of the different toggles.
399 *
400 * Only one attribute: the new state requested (on, off or no change,
401 * just query).
402 */
403
404static const
405struct nla_policy wimax_gnl_rfkill_policy[WIMAX_GNL_ATTR_MAX + 1] = {
406 [WIMAX_GNL_RFKILL_IFIDX] = {
407 .type = NLA_U32,
408 },
409 [WIMAX_GNL_RFKILL_STATE] = {
410 .type = NLA_U32 /* enum wimax_rf_state */
411 },
412};
413
414
415static
416int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info)
417{
418 int result, ifindex;
419 struct wimax_dev *wimax_dev;
420 struct device *dev;
421 enum wimax_rf_state new_state;
422
423 d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
424 result = -ENODEV;
425 if (info->attrs[WIMAX_GNL_RFKILL_IFIDX] == NULL) {
426 printk(KERN_ERR "WIMAX_GNL_OP_RFKILL: can't find IFIDX "
427 "attribute\n");
428 goto error_no_wimax_dev;
429 }
430 ifindex = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_IFIDX]);
431 wimax_dev = wimax_dev_get_by_genl_info(info, ifindex);
432 if (wimax_dev == NULL)
433 goto error_no_wimax_dev;
434 dev = wimax_dev_to_dev(wimax_dev);
435 result = -EINVAL;
436 if (info->attrs[WIMAX_GNL_RFKILL_STATE] == NULL) {
437 dev_err(dev, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE "
438 "attribute\n");
439 goto error_no_pid;
440 }
441 new_state = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_STATE]);
442
443 /* Execute the operation and send the result back to user space */
444 result = wimax_rfkill(wimax_dev, new_state);
445error_no_pid:
446 dev_put(wimax_dev->net_dev);
447error_no_wimax_dev:
448 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
449 return result;
450}
451
452
453struct genl_ops wimax_gnl_rfkill = {
454 .cmd = WIMAX_GNL_OP_RFKILL,
455 .flags = GENL_ADMIN_PERM,
456 .policy = wimax_gnl_rfkill_policy,
457 .doit = wimax_gnl_doit_rfkill,
458 .dumpit = NULL,
459};
460