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