• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/net/wimax/
1
2
3#include <net/wimax.h>
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"
9
10#define D_SUBMODULE op_rfkill
11#include "debug-levels.h"
12
13/**
14 * wimax_report_rfkill_hw - Reports changes in the hardware RF switch
15 *
16 * @wimax_dev: WiMAX device descriptor
17 *
18 * @state: New state of the RF Kill switch. %WIMAX_RF_ON radio on,
19 *     %WIMAX_RF_OFF radio off.
20 *
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.
24 *
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.
28 *
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).
31 */
32void wimax_report_rfkill_hw(struct wimax_dev *wimax_dev,
33			    enum wimax_rf_state state)
34{
35	int result;
36	struct device *dev = wimax_dev_to_dev(wimax_dev);
37	enum wimax_st wimax_state;
38
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);
42
43	mutex_lock(&wimax_dev->mutex);
44	result = wimax_dev_is_ready(wimax_dev);
45	if (result < 0)
46		goto error_not_ready;
47
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;
53		else
54			wimax_state = WIMAX_ST_RADIO_OFF;
55
56		result = rfkill_set_hw_state(wimax_dev->rfkill,
57					     state == WIMAX_RF_OFF);
58
59		__wimax_state_change(wimax_dev, wimax_state);
60	}
61error_not_ready:
62	mutex_unlock(&wimax_dev->mutex);
63	d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
64		wimax_dev, state, result);
65}
66EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw);
67
68
69/**
70 * wimax_report_rfkill_sw - Reports changes in the software RF switch
71 *
72 * @wimax_dev: WiMAX device descriptor
73 *
74 * @state: New state of the RF kill switch. %WIMAX_RF_ON radio on,
75 *     %WIMAX_RF_OFF radio off.
76 *
77 * Reports changes in the software RF switch state to the the WiMAX
78 * stack.
79 *
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
82 * to the system.
83 *
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.
87 */
88void wimax_report_rfkill_sw(struct wimax_dev *wimax_dev,
89			    enum wimax_rf_state state)
90{
91	int result;
92	struct device *dev = wimax_dev_to_dev(wimax_dev);
93	enum wimax_st wimax_state;
94
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);
98
99	mutex_lock(&wimax_dev->mutex);
100	result = wimax_dev_is_ready(wimax_dev);
101	if (result < 0)
102		goto error_not_ready;
103
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;
109		else
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);
113	}
114error_not_ready:
115	mutex_unlock(&wimax_dev->mutex);
116	d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
117		wimax_dev, state, result);
118}
119EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw);
120
121
122/*
123 * Callback for the RF Kill toggle operation
124 *
125 * This function is called by:
126 *
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.
132 *
133 * - When the user sets the state through sysfs' rfkill/state file
134 *
135 * - When the user calls wimax_rfkill().
136 *
137 * This call blocks!
138 *
139 * WARNING! When we call rfkill_unregister(), this will be called with
140 * state 0!
141 *
142 * WARNING: wimax_dev must be locked
143 */
144static
145int __wimax_rf_toggle_radio(struct wimax_dev *wimax_dev,
146			    enum wimax_rf_state state)
147{
148	int result = 0;
149	struct device *dev = wimax_dev_to_dev(wimax_dev);
150	enum wimax_st wimax_state;
151
152	might_sleep();
153	d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
154	if (wimax_dev->rf_sw == state)
155		goto out_no_change;
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 */
159		result = -ENXIO;
160	else				/* No op? can turn on */
161		result = 0;		/* should never happen tho */
162	if (result >= 0) {
163		result = 0;
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);
168	}
169out_no_change:
170	d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
171		wimax_dev, state, result);
172	return result;
173}
174
175
176/*
177 * Translate from rfkill state to wimax state
178 *
179 * NOTE: Special state handling rules here
180 *
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.
185 *
186 * NOTE: This call will block until the operation is completed.
187 */
188static int wimax_rfkill_set_radio_block(void *data, bool blocked)
189{
190	int result;
191	struct wimax_dev *wimax_dev = data;
192	struct device *dev = wimax_dev_to_dev(wimax_dev);
193	enum wimax_rf_state rf_state;
194
195	d_fnstart(3, dev, "(wimax_dev %p blocked %u)\n", wimax_dev, blocked);
196	rf_state = WIMAX_RF_ON;
197	if (blocked)
198		rf_state = WIMAX_RF_OFF;
199	mutex_lock(&wimax_dev->mutex);
200	if (wimax_dev->state <= __WIMAX_ST_QUIESCING)
201		result = 0;
202	else
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);
207	return result;
208}
209
210static const struct rfkill_ops wimax_rfkill_ops = {
211	.set_block = wimax_rfkill_set_radio_block,
212};
213
214/**
215 * wimax_rfkill - Set the software RF switch state for a WiMAX device
216 *
217 * @wimax_dev: WiMAX device descriptor
218 *
219 * @state: New RF state.
220 *
221 * Returns:
222 *
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.
226 *
227 * 0 means disabled (%WIMAX_RF_ON, radio on), 1 means enabled radio
228 * off (%WIMAX_RF_OFF).
229 *
230 * Description:
231 *
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.
235 *
236 * NOTE:
237 *
238 * This call will block until the operation is complete.
239 */
240int wimax_rfkill(struct wimax_dev *wimax_dev, enum wimax_rf_state state)
241{
242	int result;
243	struct device *dev = wimax_dev_to_dev(wimax_dev);
244
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);
248	if (result < 0) {
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;
256	}
257	switch (state) {
258	case WIMAX_RF_ON:
259	case WIMAX_RF_OFF:
260		result = __wimax_rf_toggle_radio(wimax_dev, state);
261		if (result < 0)
262			goto error;
263		rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
264		break;
265	case WIMAX_RF_QUERY:
266		break;
267	default:
268		result = -EINVAL;
269		goto error;
270	}
271	result = wimax_dev->rf_sw << 1 | wimax_dev->rf_hw;
272error:
273error_not_ready:
274	mutex_unlock(&wimax_dev->mutex);
275	d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
276		wimax_dev, state, result);
277	return result;
278}
279EXPORT_SYMBOL(wimax_rfkill);
280
281
282/*
283 * Register a new WiMAX device's RF Kill support
284 *
285 * WARNING: wimax_dev->mutex must be unlocked
286 */
287int wimax_rfkill_add(struct wimax_dev *wimax_dev)
288{
289	int result;
290	struct rfkill *rfkill;
291	struct device *dev = wimax_dev_to_dev(wimax_dev);
292
293	d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
294	/* Initialize RF Kill */
295	result = -ENOMEM;
296	rfkill = rfkill_alloc(wimax_dev->name, dev, RFKILL_TYPE_WIMAX,
297			      &wimax_rfkill_ops, wimax_dev);
298	if (rfkill == NULL)
299		goto error_rfkill_allocate;
300
301	d_printf(1, dev, "rfkill %p\n", rfkill);
302
303	wimax_dev->rfkill = rfkill;
304
305	rfkill_init_sw_state(rfkill, 1);
306	result = rfkill_register(wimax_dev->rfkill);
307	if (result < 0)
308		goto error_rfkill_register;
309
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;
313
314	d_fnend(3, dev, "(wimax_dev %p) = 0\n", wimax_dev);
315	return 0;
316
317error_rfkill_register:
318	rfkill_destroy(wimax_dev->rfkill);
319error_rfkill_allocate:
320	d_fnend(3, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
321	return result;
322}
323
324
325/*
326 * Deregister a WiMAX device's RF Kill support
327 *
328 * Ick, we can't call rfkill_free() after rfkill_unregister()...oh
329 * well.
330 *
331 * WARNING: wimax_dev->mutex must be unlocked
332 */
333void wimax_rfkill_rm(struct wimax_dev *wimax_dev)
334{
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);
340}
341
342
343/*
344 * Exporting to user space over generic netlink
345 *
346 * Parse the rfkill command from user space, return a combination
347 * value that describe the states of the different toggles.
348 *
349 * Only one attribute: the new state requested (on, off or no change,
350 * just query).
351 */
352
353static const struct nla_policy wimax_gnl_rfkill_policy[WIMAX_GNL_ATTR_MAX + 1] = {
354	[WIMAX_GNL_RFKILL_IFIDX] = {
355		.type = NLA_U32,
356	},
357	[WIMAX_GNL_RFKILL_STATE] = {
358		.type = NLA_U32		/* enum wimax_rf_state */
359	},
360};
361
362
363static
364int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info)
365{
366	int result, ifindex;
367	struct wimax_dev *wimax_dev;
368	struct device *dev;
369	enum wimax_rf_state new_state;
370
371	d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
372	result = -ENODEV;
373	if (info->attrs[WIMAX_GNL_RFKILL_IFIDX] == NULL) {
374		printk(KERN_ERR "WIMAX_GNL_OP_RFKILL: can't find IFIDX "
375			"attribute\n");
376		goto error_no_wimax_dev;
377	}
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);
383	result = -EINVAL;
384	if (info->attrs[WIMAX_GNL_RFKILL_STATE] == NULL) {
385		dev_err(dev, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE "
386			"attribute\n");
387		goto error_no_pid;
388	}
389	new_state = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_STATE]);
390
391	/* Execute the operation and send the result back to user space */
392	result = wimax_rfkill(wimax_dev, new_state);
393error_no_pid:
394	dev_put(wimax_dev->net_dev);
395error_no_wimax_dev:
396	d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
397	return result;
398}
399
400
401struct 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,
406	.dumpit = NULL,
407};
408