4 #include <net/genetlink.h>
5 #include <linux/wimax.h>
6 #include <linux/security.h>
7 #include <linux/rfkill.h>
8 #include "wimax-internal.h"
10 #define D_SUBMODULE op_rfkill
11 #include "debug-levels.h"
14 * wimax_report_rfkill_hw - Reports changes in the hardware RF switch
16 * @wimax_dev: WiMAX device descriptor
18 * @state: New state of the RF Kill switch. %WIMAX_RF_ON radio on,
19 * %WIMAX_RF_OFF radio off.
21 * When the device detects a change in the state of thehardware RF
22 * switch, it must call this function to let the WiMAX kernel stack
23 * know that the state has changed so it can be properly propagated.
25 * The WiMAX stack caches the state (the driver doesn't need to). As
26 * well, as the change is propagated it will come back as a request to
27 * change the software state to mirror the hardware state.
29 * If the device doesn't have a hardware kill switch, just report
30 * it on initialization as always on (%WIMAX_RF_ON, radio on).
32 void wimax_report_rfkill_hw(struct wimax_dev
*wimax_dev
,
33 enum wimax_rf_state state
)
36 struct device
*dev
= wimax_dev_to_dev(wimax_dev
);
37 enum wimax_st wimax_state
;
39 d_fnstart(3, dev
, "(wimax_dev %p state %u)\n", wimax_dev
, state
);
40 BUG_ON(state
== WIMAX_RF_QUERY
);
41 BUG_ON(state
!= WIMAX_RF_ON
&& state
!= WIMAX_RF_OFF
);
43 mutex_lock(&wimax_dev
->mutex
);
44 result
= wimax_dev_is_ready(wimax_dev
);
48 if (state
!= wimax_dev
->rf_hw
) {
49 wimax_dev
->rf_hw
= state
;
50 if (wimax_dev
->rf_hw
== WIMAX_RF_ON
&&
51 wimax_dev
->rf_sw
== WIMAX_RF_ON
)
52 wimax_state
= WIMAX_ST_READY
;
54 wimax_state
= WIMAX_ST_RADIO_OFF
;
56 result
= rfkill_set_hw_state(wimax_dev
->rfkill
,
57 state
== WIMAX_RF_OFF
);
59 __wimax_state_change(wimax_dev
, wimax_state
);
62 mutex_unlock(&wimax_dev
->mutex
);
63 d_fnend(3, dev
, "(wimax_dev %p state %u) = void [%d]\n",
64 wimax_dev
, state
, result
);
66 EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw
);
70 * wimax_report_rfkill_sw - Reports changes in the software RF switch
72 * @wimax_dev: WiMAX device descriptor
74 * @state: New state of the RF kill switch. %WIMAX_RF_ON radio on,
75 * %WIMAX_RF_OFF radio off.
77 * Reports changes in the software RF switch state to the the WiMAX
80 * The main use is during initialization, so the driver can query the
81 * device for its current software radio kill switch state and feed it
84 * On the side, the device does not change the software state by
85 * itself. In practice, this can happen, as the device might decide to
86 * switch (in software) the radio off for different reasons.
88 void wimax_report_rfkill_sw(struct wimax_dev
*wimax_dev
,
89 enum wimax_rf_state state
)
92 struct device
*dev
= wimax_dev_to_dev(wimax_dev
);
93 enum wimax_st wimax_state
;
95 d_fnstart(3, dev
, "(wimax_dev %p state %u)\n", wimax_dev
, state
);
96 BUG_ON(state
== WIMAX_RF_QUERY
);
97 BUG_ON(state
!= WIMAX_RF_ON
&& state
!= WIMAX_RF_OFF
);
99 mutex_lock(&wimax_dev
->mutex
);
100 result
= wimax_dev_is_ready(wimax_dev
);
102 goto error_not_ready
;
104 if (state
!= wimax_dev
->rf_sw
) {
105 wimax_dev
->rf_sw
= state
;
106 if (wimax_dev
->rf_hw
== WIMAX_RF_ON
&&
107 wimax_dev
->rf_sw
== WIMAX_RF_ON
)
108 wimax_state
= WIMAX_ST_READY
;
110 wimax_state
= WIMAX_ST_RADIO_OFF
;
111 __wimax_state_change(wimax_dev
, wimax_state
);
112 rfkill_set_sw_state(wimax_dev
->rfkill
, state
== WIMAX_RF_OFF
);
115 mutex_unlock(&wimax_dev
->mutex
);
116 d_fnend(3, dev
, "(wimax_dev %p state %u) = void [%d]\n",
117 wimax_dev
, state
, result
);
119 EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw
);
123 * Callback for the RF Kill toggle operation
125 * This function is called by:
127 * - The rfkill subsystem when the RF-Kill key is pressed in the
128 * hardware and the driver notifies through
129 * wimax_report_rfkill_hw(). The rfkill subsystem ends up calling back
130 * here so the software RF Kill switch state is changed to reflect
131 * the hardware switch state.
133 * - When the user sets the state through sysfs' rfkill/state file
135 * - When the user calls wimax_rfkill().
139 * WARNING! When we call rfkill_unregister(), this will be called with
142 * WARNING: wimax_dev must be locked
145 int __wimax_rf_toggle_radio(struct wimax_dev
*wimax_dev
,
146 enum wimax_rf_state state
)
149 struct device
*dev
= wimax_dev_to_dev(wimax_dev
);
150 enum wimax_st wimax_state
;
153 d_fnstart(3, dev
, "(wimax_dev %p state %u)\n", wimax_dev
, state
);
154 if (wimax_dev
->rf_sw
== state
)
156 if (wimax_dev
->op_rfkill_sw_toggle
!= NULL
)
157 result
= wimax_dev
->op_rfkill_sw_toggle(wimax_dev
, state
);
158 else if (state
== WIMAX_RF_OFF
) /* No op? can't turn off */
160 else /* No op? can turn on */
161 result
= 0; /* should never happen tho */
164 wimax_dev
->rf_sw
= state
;
165 wimax_state
= state
== WIMAX_RF_ON
?
166 WIMAX_ST_READY
: WIMAX_ST_RADIO_OFF
;
167 __wimax_state_change(wimax_dev
, wimax_state
);
170 d_fnend(3, dev
, "(wimax_dev %p state %u) = %d\n",
171 wimax_dev
, state
, result
);
177 * Translate from rfkill state to wimax state
179 * NOTE: Special state handling rules here
181 * Just pretend the call didn't happen if we are in a state where
182 * we know for sure it cannot be handled (WIMAX_ST_DOWN or
183 * __WIMAX_ST_QUIESCING). rfkill() needs it to register and
184 * unregister, as it will run this path.
186 * NOTE: This call will block until the operation is completed.
188 static int wimax_rfkill_set_radio_block(void *data
, bool blocked
)
191 struct wimax_dev
*wimax_dev
= data
;
192 struct device
*dev
= wimax_dev_to_dev(wimax_dev
);
193 enum wimax_rf_state rf_state
;
195 d_fnstart(3, dev
, "(wimax_dev %p blocked %u)\n", wimax_dev
, blocked
);
196 rf_state
= WIMAX_RF_ON
;
198 rf_state
= WIMAX_RF_OFF
;
199 mutex_lock(&wimax_dev
->mutex
);
200 if (wimax_dev
->state
<= __WIMAX_ST_QUIESCING
)
203 result
= __wimax_rf_toggle_radio(wimax_dev
, rf_state
);
204 mutex_unlock(&wimax_dev
->mutex
);
205 d_fnend(3, dev
, "(wimax_dev %p blocked %u) = %d\n",
206 wimax_dev
, blocked
, result
);
210 static const struct rfkill_ops wimax_rfkill_ops
= {
211 .set_block
= wimax_rfkill_set_radio_block
,
215 * wimax_rfkill - Set the software RF switch state for a WiMAX device
217 * @wimax_dev: WiMAX device descriptor
219 * @state: New RF state.
223 * >= 0 toggle state if ok, < 0 errno code on error. The toggle state
224 * is returned as a bitmap, bit 0 being the hardware RF state, bit 1
225 * the software RF state.
227 * 0 means disabled (%WIMAX_RF_ON, radio on), 1 means enabled radio
228 * off (%WIMAX_RF_OFF).
232 * Called by the user when he wants to request the WiMAX radio to be
233 * switched on (%WIMAX_RF_ON) or off (%WIMAX_RF_OFF). With
234 * %WIMAX_RF_QUERY, just the current state is returned.
238 * This call will block until the operation is complete.
240 int wimax_rfkill(struct wimax_dev
*wimax_dev
, enum wimax_rf_state state
)
243 struct device
*dev
= wimax_dev_to_dev(wimax_dev
);
245 d_fnstart(3, dev
, "(wimax_dev %p state %u)\n", wimax_dev
, state
);
246 mutex_lock(&wimax_dev
->mutex
);
247 result
= wimax_dev_is_ready(wimax_dev
);
249 /* While initializing, < 1.4.3 wimax-tools versions use
250 * this call to check if the device is a valid WiMAX
251 * device; so we allow it to proceed always,
252 * considering the radios are all off. */
253 if (result
== -ENOMEDIUM
&& state
== WIMAX_RF_QUERY
)
254 result
= WIMAX_RF_OFF
<< 1 | WIMAX_RF_OFF
;
255 goto error_not_ready
;
260 result
= __wimax_rf_toggle_radio(wimax_dev
, state
);
263 rfkill_set_sw_state(wimax_dev
->rfkill
, state
== WIMAX_RF_OFF
);
271 result
= wimax_dev
->rf_sw
<< 1 | wimax_dev
->rf_hw
;
274 mutex_unlock(&wimax_dev
->mutex
);
275 d_fnend(3, dev
, "(wimax_dev %p state %u) = %d\n",
276 wimax_dev
, state
, result
);
279 EXPORT_SYMBOL(wimax_rfkill
);
283 * Register a new WiMAX device's RF Kill support
285 * WARNING: wimax_dev->mutex must be unlocked
287 int wimax_rfkill_add(struct wimax_dev
*wimax_dev
)
290 struct rfkill
*rfkill
;
291 struct device
*dev
= wimax_dev_to_dev(wimax_dev
);
293 d_fnstart(3, dev
, "(wimax_dev %p)\n", wimax_dev
);
294 /* Initialize RF Kill */
296 rfkill
= rfkill_alloc(wimax_dev
->name
, dev
, RFKILL_TYPE_WIMAX
,
297 &wimax_rfkill_ops
, wimax_dev
);
299 goto error_rfkill_allocate
;
301 d_printf(1, dev
, "rfkill %p\n", rfkill
);
303 wimax_dev
->rfkill
= rfkill
;
305 rfkill_init_sw_state(rfkill
, 1);
306 result
= rfkill_register(wimax_dev
->rfkill
);
308 goto error_rfkill_register
;
310 /* If there is no SW toggle op, SW RFKill is always on */
311 if (wimax_dev
->op_rfkill_sw_toggle
== NULL
)
312 wimax_dev
->rf_sw
= WIMAX_RF_ON
;
314 d_fnend(3, dev
, "(wimax_dev %p) = 0\n", wimax_dev
);
317 error_rfkill_register
:
318 rfkill_destroy(wimax_dev
->rfkill
);
319 error_rfkill_allocate
:
320 d_fnend(3, dev
, "(wimax_dev %p) = %d\n", wimax_dev
, result
);
326 * Deregister a WiMAX device's RF Kill support
328 * Ick, we can't call rfkill_free() after rfkill_unregister()...oh
331 * WARNING: wimax_dev->mutex must be unlocked
333 void wimax_rfkill_rm(struct wimax_dev
*wimax_dev
)
335 struct device
*dev
= wimax_dev_to_dev(wimax_dev
);
336 d_fnstart(3, dev
, "(wimax_dev %p)\n", wimax_dev
);
337 rfkill_unregister(wimax_dev
->rfkill
);
338 rfkill_destroy(wimax_dev
->rfkill
);
339 d_fnend(3, dev
, "(wimax_dev %p)\n", wimax_dev
);
344 * Exporting to user space over generic netlink
346 * Parse the rfkill command from user space, return a combination
347 * value that describe the states of the different toggles.
349 * Only one attribute: the new state requested (on, off or no change,
353 static const struct nla_policy wimax_gnl_rfkill_policy
[WIMAX_GNL_ATTR_MAX
+ 1] = {
354 [WIMAX_GNL_RFKILL_IFIDX
] = {
357 [WIMAX_GNL_RFKILL_STATE
] = {
358 .type
= NLA_U32
/* enum wimax_rf_state */
364 int wimax_gnl_doit_rfkill(struct sk_buff
*skb
, struct genl_info
*info
)
367 struct wimax_dev
*wimax_dev
;
369 enum wimax_rf_state new_state
;
371 d_fnstart(3, NULL
, "(skb %p info %p)\n", skb
, info
);
373 if (info
->attrs
[WIMAX_GNL_RFKILL_IFIDX
] == NULL
) {
374 printk(KERN_ERR
"WIMAX_GNL_OP_RFKILL: can't find IFIDX "
376 goto error_no_wimax_dev
;
378 ifindex
= nla_get_u32(info
->attrs
[WIMAX_GNL_RFKILL_IFIDX
]);
379 wimax_dev
= wimax_dev_get_by_genl_info(info
, ifindex
);
380 if (wimax_dev
== NULL
)
381 goto error_no_wimax_dev
;
382 dev
= wimax_dev_to_dev(wimax_dev
);
384 if (info
->attrs
[WIMAX_GNL_RFKILL_STATE
] == NULL
) {
385 dev_err(dev
, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE "
389 new_state
= nla_get_u32(info
->attrs
[WIMAX_GNL_RFKILL_STATE
]);
391 /* Execute the operation and send the result back to user space */
392 result
= wimax_rfkill(wimax_dev
, new_state
);
394 dev_put(wimax_dev
->net_dev
);
396 d_fnend(3, NULL
, "(skb %p info %p) = %d\n", skb
, info
, result
);
401 struct genl_ops wimax_gnl_rfkill
= {
402 .cmd
= WIMAX_GNL_OP_RFKILL
,
403 .flags
= GENL_ADMIN_PERM
,
404 .policy
= wimax_gnl_rfkill_policy
,
405 .doit
= wimax_gnl_doit_rfkill
,