blob: 403078d670a909cb20b4660e1a33965fd996bb52 [file] [log] [blame]
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -08001/*
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 *
Johannes Berg19d337d2009-06-02 13:01:37 +020032 * FIXME: Need polled support? Let drivers provide a poll routine
33 * and hand it to rfkill ops then?
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -080034 *
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 *
Thomas Weber88393162010-03-16 11:47:56 +010046 * wimax_rfkill_set_radio_block() RF-Kill subsystem calling
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -080047 * __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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040068#include <linux/export.h>
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -080069#include "wimax-internal.h"
70
71#define D_SUBMODULE op_rfkill
72#include "debug-levels.h"
73
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -080074/**
75 * wimax_report_rfkill_hw - Reports changes in the hardware RF switch
76 *
77 * @wimax_dev: WiMAX device descriptor
78 *
79 * @state: New state of the RF Kill switch. %WIMAX_RF_ON radio on,
80 * %WIMAX_RF_OFF radio off.
81 *
82 * When the device detects a change in the state of thehardware RF
83 * switch, it must call this function to let the WiMAX kernel stack
84 * know that the state has changed so it can be properly propagated.
85 *
86 * The WiMAX stack caches the state (the driver doesn't need to). As
87 * well, as the change is propagated it will come back as a request to
88 * change the software state to mirror the hardware state.
89 *
90 * If the device doesn't have a hardware kill switch, just report
91 * it on initialization as always on (%WIMAX_RF_ON, radio on).
92 */
93void wimax_report_rfkill_hw(struct wimax_dev *wimax_dev,
94 enum wimax_rf_state state)
95{
96 int result;
97 struct device *dev = wimax_dev_to_dev(wimax_dev);
98 enum wimax_st wimax_state;
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -080099
100 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
101 BUG_ON(state == WIMAX_RF_QUERY);
102 BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF);
103
104 mutex_lock(&wimax_dev->mutex);
105 result = wimax_dev_is_ready(wimax_dev);
106 if (result < 0)
107 goto error_not_ready;
108
109 if (state != wimax_dev->rf_hw) {
110 wimax_dev->rf_hw = state;
Joe Perchesf64f9e72009-11-29 16:55:45 -0800111 if (wimax_dev->rf_hw == WIMAX_RF_ON &&
112 wimax_dev->rf_sw == WIMAX_RF_ON)
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800113 wimax_state = WIMAX_ST_READY;
114 else
115 wimax_state = WIMAX_ST_RADIO_OFF;
Johannes Berg19d337d2009-06-02 13:01:37 +0200116
Inaky Perez-Gonzalezd2f4c102009-06-11 11:12:48 -0700117 result = rfkill_set_hw_state(wimax_dev->rfkill,
118 state == WIMAX_RF_OFF);
Johannes Berg19d337d2009-06-02 13:01:37 +0200119
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800120 __wimax_state_change(wimax_dev, wimax_state);
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800121 }
122error_not_ready:
123 mutex_unlock(&wimax_dev->mutex);
124 d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
125 wimax_dev, state, result);
126}
127EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw);
128
129
130/**
131 * wimax_report_rfkill_sw - Reports changes in the software RF switch
132 *
133 * @wimax_dev: WiMAX device descriptor
134 *
135 * @state: New state of the RF kill switch. %WIMAX_RF_ON radio on,
136 * %WIMAX_RF_OFF radio off.
137 *
138 * Reports changes in the software RF switch state to the the WiMAX
139 * stack.
140 *
141 * The main use is during initialization, so the driver can query the
142 * device for its current software radio kill switch state and feed it
143 * to the system.
144 *
145 * On the side, the device does not change the software state by
146 * itself. In practice, this can happen, as the device might decide to
147 * switch (in software) the radio off for different reasons.
148 */
149void wimax_report_rfkill_sw(struct wimax_dev *wimax_dev,
150 enum wimax_rf_state state)
151{
152 int result;
153 struct device *dev = wimax_dev_to_dev(wimax_dev);
154 enum wimax_st wimax_state;
155
156 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
157 BUG_ON(state == WIMAX_RF_QUERY);
158 BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF);
159
160 mutex_lock(&wimax_dev->mutex);
161 result = wimax_dev_is_ready(wimax_dev);
162 if (result < 0)
163 goto error_not_ready;
164
165 if (state != wimax_dev->rf_sw) {
166 wimax_dev->rf_sw = state;
Joe Perchesf64f9e72009-11-29 16:55:45 -0800167 if (wimax_dev->rf_hw == WIMAX_RF_ON &&
168 wimax_dev->rf_sw == WIMAX_RF_ON)
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800169 wimax_state = WIMAX_ST_READY;
170 else
171 wimax_state = WIMAX_ST_RADIO_OFF;
172 __wimax_state_change(wimax_dev, wimax_state);
Johannes Berg19d337d2009-06-02 13:01:37 +0200173 rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800174 }
175error_not_ready:
176 mutex_unlock(&wimax_dev->mutex);
177 d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
178 wimax_dev, state, result);
179}
180EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw);
181
182
183/*
184 * Callback for the RF Kill toggle operation
185 *
186 * This function is called by:
187 *
188 * - The rfkill subsystem when the RF-Kill key is pressed in the
189 * hardware and the driver notifies through
190 * wimax_report_rfkill_hw(). The rfkill subsystem ends up calling back
191 * here so the software RF Kill switch state is changed to reflect
192 * the hardware switch state.
193 *
194 * - When the user sets the state through sysfs' rfkill/state file
195 *
196 * - When the user calls wimax_rfkill().
197 *
198 * This call blocks!
199 *
200 * WARNING! When we call rfkill_unregister(), this will be called with
201 * state 0!
202 *
203 * WARNING: wimax_dev must be locked
204 */
205static
206int __wimax_rf_toggle_radio(struct wimax_dev *wimax_dev,
207 enum wimax_rf_state state)
208{
209 int result = 0;
210 struct device *dev = wimax_dev_to_dev(wimax_dev);
211 enum wimax_st wimax_state;
212
213 might_sleep();
214 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
215 if (wimax_dev->rf_sw == state)
216 goto out_no_change;
217 if (wimax_dev->op_rfkill_sw_toggle != NULL)
218 result = wimax_dev->op_rfkill_sw_toggle(wimax_dev, state);
219 else if (state == WIMAX_RF_OFF) /* No op? can't turn off */
220 result = -ENXIO;
221 else /* No op? can turn on */
222 result = 0; /* should never happen tho */
223 if (result >= 0) {
224 result = 0;
225 wimax_dev->rf_sw = state;
226 wimax_state = state == WIMAX_RF_ON ?
227 WIMAX_ST_READY : WIMAX_ST_RADIO_OFF;
228 __wimax_state_change(wimax_dev, wimax_state);
229 }
230out_no_change:
231 d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
232 wimax_dev, state, result);
233 return result;
234}
235
236
237/*
238 * Translate from rfkill state to wimax state
239 *
240 * NOTE: Special state handling rules here
241 *
242 * Just pretend the call didn't happen if we are in a state where
243 * we know for sure it cannot be handled (WIMAX_ST_DOWN or
244 * __WIMAX_ST_QUIESCING). rfkill() needs it to register and
245 * unregister, as it will run this path.
246 *
247 * NOTE: This call will block until the operation is completed.
248 */
Johannes Berg19d337d2009-06-02 13:01:37 +0200249static int wimax_rfkill_set_radio_block(void *data, bool blocked)
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800250{
251 int result;
252 struct wimax_dev *wimax_dev = data;
253 struct device *dev = wimax_dev_to_dev(wimax_dev);
254 enum wimax_rf_state rf_state;
255
Johannes Berg19d337d2009-06-02 13:01:37 +0200256 d_fnstart(3, dev, "(wimax_dev %p blocked %u)\n", wimax_dev, blocked);
257 rf_state = WIMAX_RF_ON;
258 if (blocked)
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800259 rf_state = WIMAX_RF_OFF;
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800260 mutex_lock(&wimax_dev->mutex);
261 if (wimax_dev->state <= __WIMAX_ST_QUIESCING)
Johannes Berg19d337d2009-06-02 13:01:37 +0200262 result = 0;
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800263 else
264 result = __wimax_rf_toggle_radio(wimax_dev, rf_state);
265 mutex_unlock(&wimax_dev->mutex);
Johannes Berg19d337d2009-06-02 13:01:37 +0200266 d_fnend(3, dev, "(wimax_dev %p blocked %u) = %d\n",
267 wimax_dev, blocked, result);
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800268 return result;
269}
270
Johannes Berg19d337d2009-06-02 13:01:37 +0200271static const struct rfkill_ops wimax_rfkill_ops = {
272 .set_block = wimax_rfkill_set_radio_block,
273};
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800274
275/**
276 * wimax_rfkill - Set the software RF switch state for a WiMAX device
277 *
278 * @wimax_dev: WiMAX device descriptor
279 *
280 * @state: New RF state.
281 *
282 * Returns:
283 *
284 * >= 0 toggle state if ok, < 0 errno code on error. The toggle state
285 * is returned as a bitmap, bit 0 being the hardware RF state, bit 1
286 * the software RF state.
287 *
288 * 0 means disabled (%WIMAX_RF_ON, radio on), 1 means enabled radio
289 * off (%WIMAX_RF_OFF).
290 *
291 * Description:
292 *
293 * Called by the user when he wants to request the WiMAX radio to be
294 * switched on (%WIMAX_RF_ON) or off (%WIMAX_RF_OFF). With
295 * %WIMAX_RF_QUERY, just the current state is returned.
296 *
297 * NOTE:
298 *
299 * This call will block until the operation is complete.
300 */
301int wimax_rfkill(struct wimax_dev *wimax_dev, enum wimax_rf_state state)
302{
303 int result;
304 struct device *dev = wimax_dev_to_dev(wimax_dev);
305
306 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
307 mutex_lock(&wimax_dev->mutex);
308 result = wimax_dev_is_ready(wimax_dev);
Inaky Perez-Gonzalez81d3f902009-10-03 16:51:02 +0900309 if (result < 0) {
310 /* While initializing, < 1.4.3 wimax-tools versions use
311 * this call to check if the device is a valid WiMAX
312 * device; so we allow it to proceed always,
313 * considering the radios are all off. */
314 if (result == -ENOMEDIUM && state == WIMAX_RF_QUERY)
315 result = WIMAX_RF_OFF << 1 | WIMAX_RF_OFF;
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800316 goto error_not_ready;
Inaky Perez-Gonzalez81d3f902009-10-03 16:51:02 +0900317 }
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800318 switch (state) {
319 case WIMAX_RF_ON:
320 case WIMAX_RF_OFF:
321 result = __wimax_rf_toggle_radio(wimax_dev, state);
322 if (result < 0)
323 goto error;
Johannes Berg19d337d2009-06-02 13:01:37 +0200324 rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800325 break;
326 case WIMAX_RF_QUERY:
327 break;
328 default:
329 result = -EINVAL;
330 goto error;
331 }
332 result = wimax_dev->rf_sw << 1 | wimax_dev->rf_hw;
333error:
334error_not_ready:
335 mutex_unlock(&wimax_dev->mutex);
336 d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
337 wimax_dev, state, result);
338 return result;
339}
340EXPORT_SYMBOL(wimax_rfkill);
341
342
343/*
344 * Register a new WiMAX device's RF Kill support
345 *
346 * WARNING: wimax_dev->mutex must be unlocked
347 */
348int wimax_rfkill_add(struct wimax_dev *wimax_dev)
349{
350 int result;
351 struct rfkill *rfkill;
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800352 struct device *dev = wimax_dev_to_dev(wimax_dev);
353
354 d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
355 /* Initialize RF Kill */
356 result = -ENOMEM;
Johannes Berg19d337d2009-06-02 13:01:37 +0200357 rfkill = rfkill_alloc(wimax_dev->name, dev, RFKILL_TYPE_WIMAX,
358 &wimax_rfkill_ops, wimax_dev);
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800359 if (rfkill == NULL)
360 goto error_rfkill_allocate;
Johannes Berg19d337d2009-06-02 13:01:37 +0200361
362 d_printf(1, dev, "rfkill %p\n", rfkill);
363
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800364 wimax_dev->rfkill = rfkill;
365
Inaky Perez-Gonzalezc29eaf32009-08-27 17:54:53 -0700366 rfkill_init_sw_state(rfkill, 1);
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800367 result = rfkill_register(wimax_dev->rfkill);
368 if (result < 0)
369 goto error_rfkill_register;
370
371 /* If there is no SW toggle op, SW RFKill is always on */
372 if (wimax_dev->op_rfkill_sw_toggle == NULL)
373 wimax_dev->rf_sw = WIMAX_RF_ON;
374
375 d_fnend(3, dev, "(wimax_dev %p) = 0\n", wimax_dev);
376 return 0;
377
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800378error_rfkill_register:
Johannes Berg19d337d2009-06-02 13:01:37 +0200379 rfkill_destroy(wimax_dev->rfkill);
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800380error_rfkill_allocate:
381 d_fnend(3, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
382 return result;
383}
384
385
386/*
387 * Deregister a WiMAX device's RF Kill support
388 *
389 * Ick, we can't call rfkill_free() after rfkill_unregister()...oh
390 * well.
391 *
392 * WARNING: wimax_dev->mutex must be unlocked
393 */
394void wimax_rfkill_rm(struct wimax_dev *wimax_dev)
395{
396 struct device *dev = wimax_dev_to_dev(wimax_dev);
397 d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
Johannes Berg19d337d2009-06-02 13:01:37 +0200398 rfkill_unregister(wimax_dev->rfkill);
399 rfkill_destroy(wimax_dev->rfkill);
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800400 d_fnend(3, dev, "(wimax_dev %p)\n", wimax_dev);
401}
402
403
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800404/*
405 * Exporting to user space over generic netlink
406 *
407 * Parse the rfkill command from user space, return a combination
408 * value that describe the states of the different toggles.
409 *
410 * Only one attribute: the new state requested (on, off or no change,
411 * just query).
412 */
413
Inaky Perez-Gonzalez3e656462008-12-20 16:57:38 -0800414int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info)
415{
416 int result, ifindex;
417 struct wimax_dev *wimax_dev;
418 struct device *dev;
419 enum wimax_rf_state new_state;
420
421 d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
422 result = -ENODEV;
423 if (info->attrs[WIMAX_GNL_RFKILL_IFIDX] == NULL) {
424 printk(KERN_ERR "WIMAX_GNL_OP_RFKILL: can't find IFIDX "
425 "attribute\n");
426 goto error_no_wimax_dev;
427 }
428 ifindex = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_IFIDX]);
429 wimax_dev = wimax_dev_get_by_genl_info(info, ifindex);
430 if (wimax_dev == NULL)
431 goto error_no_wimax_dev;
432 dev = wimax_dev_to_dev(wimax_dev);
433 result = -EINVAL;
434 if (info->attrs[WIMAX_GNL_RFKILL_STATE] == NULL) {
435 dev_err(dev, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE "
436 "attribute\n");
437 goto error_no_pid;
438 }
439 new_state = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_STATE]);
440
441 /* Execute the operation and send the result back to user space */
442 result = wimax_rfkill(wimax_dev, new_state);
443error_no_pid:
444 dev_put(wimax_dev->net_dev);
445error_no_wimax_dev:
446 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
447 return result;
448}