1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include "net-wimax.h"
49#include <net/genetlink.h>
50#include "linux-wimax.h"
51#include <linux/security.h>
52#include <linux/rfkill.h>
53#include <linux/export.h>
54#include "wimax-internal.h"
55
56#define D_SUBMODULE op_rfkill
57#include "debug-levels.h"
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78void wimax_report_rfkill_hw(struct wimax_dev *wimax_dev,
79 enum wimax_rf_state state)
80{
81 int result;
82 struct device *dev = wimax_dev_to_dev(wimax_dev);
83 enum wimax_st wimax_state;
84
85 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
86 BUG_ON(state == WIMAX_RF_QUERY);
87 BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF);
88
89 mutex_lock(&wimax_dev->mutex);
90 result = wimax_dev_is_ready(wimax_dev);
91 if (result < 0)
92 goto error_not_ready;
93
94 if (state != wimax_dev->rf_hw) {
95 wimax_dev->rf_hw = state;
96 if (wimax_dev->rf_hw == WIMAX_RF_ON &&
97 wimax_dev->rf_sw == WIMAX_RF_ON)
98 wimax_state = WIMAX_ST_READY;
99 else
100 wimax_state = WIMAX_ST_RADIO_OFF;
101
102 result = rfkill_set_hw_state(wimax_dev->rfkill,
103 state == WIMAX_RF_OFF);
104
105 __wimax_state_change(wimax_dev, wimax_state);
106 }
107error_not_ready:
108 mutex_unlock(&wimax_dev->mutex);
109 d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
110 wimax_dev, state, result);
111}
112EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133void wimax_report_rfkill_sw(struct wimax_dev *wimax_dev,
134 enum wimax_rf_state state)
135{
136 int result;
137 struct device *dev = wimax_dev_to_dev(wimax_dev);
138 enum wimax_st wimax_state;
139
140 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
141 BUG_ON(state == WIMAX_RF_QUERY);
142 BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF);
143
144 mutex_lock(&wimax_dev->mutex);
145 result = wimax_dev_is_ready(wimax_dev);
146 if (result < 0)
147 goto error_not_ready;
148
149 if (state != wimax_dev->rf_sw) {
150 wimax_dev->rf_sw = state;
151 if (wimax_dev->rf_hw == WIMAX_RF_ON &&
152 wimax_dev->rf_sw == WIMAX_RF_ON)
153 wimax_state = WIMAX_ST_READY;
154 else
155 wimax_state = WIMAX_ST_RADIO_OFF;
156 __wimax_state_change(wimax_dev, wimax_state);
157 rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
158 }
159error_not_ready:
160 mutex_unlock(&wimax_dev->mutex);
161 d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
162 wimax_dev, state, result);
163}
164EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw);
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189static
190int __wimax_rf_toggle_radio(struct wimax_dev *wimax_dev,
191 enum wimax_rf_state state)
192{
193 int result = 0;
194 struct device *dev = wimax_dev_to_dev(wimax_dev);
195 enum wimax_st wimax_state;
196
197 might_sleep();
198 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
199 if (wimax_dev->rf_sw == state)
200 goto out_no_change;
201 if (wimax_dev->op_rfkill_sw_toggle != NULL)
202 result = wimax_dev->op_rfkill_sw_toggle(wimax_dev, state);
203 else if (state == WIMAX_RF_OFF)
204 result = -ENXIO;
205 else
206 result = 0;
207 if (result >= 0) {
208 result = 0;
209 wimax_dev->rf_sw = state;
210 wimax_state = state == WIMAX_RF_ON ?
211 WIMAX_ST_READY : WIMAX_ST_RADIO_OFF;
212 __wimax_state_change(wimax_dev, wimax_state);
213 }
214out_no_change:
215 d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
216 wimax_dev, state, result);
217 return result;
218}
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233static int wimax_rfkill_set_radio_block(void *data, bool blocked)
234{
235 int result;
236 struct wimax_dev *wimax_dev = data;
237 struct device *dev = wimax_dev_to_dev(wimax_dev);
238 enum wimax_rf_state rf_state;
239
240 d_fnstart(3, dev, "(wimax_dev %p blocked %u)\n", wimax_dev, blocked);
241 rf_state = WIMAX_RF_ON;
242 if (blocked)
243 rf_state = WIMAX_RF_OFF;
244 mutex_lock(&wimax_dev->mutex);
245 if (wimax_dev->state <= __WIMAX_ST_QUIESCING)
246 result = 0;
247 else
248 result = __wimax_rf_toggle_radio(wimax_dev, rf_state);
249 mutex_unlock(&wimax_dev->mutex);
250 d_fnend(3, dev, "(wimax_dev %p blocked %u) = %d\n",
251 wimax_dev, blocked, result);
252 return result;
253}
254
255static const struct rfkill_ops wimax_rfkill_ops = {
256 .set_block = wimax_rfkill_set_radio_block,
257};
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285int wimax_rfkill(struct wimax_dev *wimax_dev, enum wimax_rf_state state)
286{
287 int result;
288 struct device *dev = wimax_dev_to_dev(wimax_dev);
289
290 d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
291 mutex_lock(&wimax_dev->mutex);
292 result = wimax_dev_is_ready(wimax_dev);
293 if (result < 0) {
294
295
296
297
298 if (result == -ENOMEDIUM && state == WIMAX_RF_QUERY)
299 result = WIMAX_RF_OFF << 1 | WIMAX_RF_OFF;
300 goto error_not_ready;
301 }
302 switch (state) {
303 case WIMAX_RF_ON:
304 case WIMAX_RF_OFF:
305 result = __wimax_rf_toggle_radio(wimax_dev, state);
306 if (result < 0)
307 goto error;
308 rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
309 break;
310 case WIMAX_RF_QUERY:
311 break;
312 default:
313 result = -EINVAL;
314 goto error;
315 }
316 result = wimax_dev->rf_sw << 1 | wimax_dev->rf_hw;
317error:
318error_not_ready:
319 mutex_unlock(&wimax_dev->mutex);
320 d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
321 wimax_dev, state, result);
322 return result;
323}
324EXPORT_SYMBOL(wimax_rfkill);
325
326
327
328
329
330
331
332int wimax_rfkill_add(struct wimax_dev *wimax_dev)
333{
334 int result;
335 struct rfkill *rfkill;
336 struct device *dev = wimax_dev_to_dev(wimax_dev);
337
338 d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
339
340 result = -ENOMEM;
341 rfkill = rfkill_alloc(wimax_dev->name, dev, RFKILL_TYPE_WIMAX,
342 &wimax_rfkill_ops, wimax_dev);
343 if (rfkill == NULL)
344 goto error_rfkill_allocate;
345
346 d_printf(1, dev, "rfkill %p\n", rfkill);
347
348 wimax_dev->rfkill = rfkill;
349
350 rfkill_init_sw_state(rfkill, 1);
351 result = rfkill_register(wimax_dev->rfkill);
352 if (result < 0)
353 goto error_rfkill_register;
354
355
356 if (wimax_dev->op_rfkill_sw_toggle == NULL)
357 wimax_dev->rf_sw = WIMAX_RF_ON;
358
359 d_fnend(3, dev, "(wimax_dev %p) = 0\n", wimax_dev);
360 return 0;
361
362error_rfkill_register:
363 rfkill_destroy(wimax_dev->rfkill);
364error_rfkill_allocate:
365 d_fnend(3, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
366 return result;
367}
368
369
370
371
372
373
374
375
376
377
378void wimax_rfkill_rm(struct wimax_dev *wimax_dev)
379{
380 struct device *dev = wimax_dev_to_dev(wimax_dev);
381 d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
382 rfkill_unregister(wimax_dev->rfkill);
383 rfkill_destroy(wimax_dev->rfkill);
384 d_fnend(3, dev, "(wimax_dev %p)\n", wimax_dev);
385}
386
387
388
389
390
391
392
393
394
395
396
397
398int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info)
399{
400 int result, ifindex;
401 struct wimax_dev *wimax_dev;
402 struct device *dev;
403 enum wimax_rf_state new_state;
404
405 d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
406 result = -ENODEV;
407 if (info->attrs[WIMAX_GNL_RFKILL_IFIDX] == NULL) {
408 pr_err("WIMAX_GNL_OP_RFKILL: can't find IFIDX attribute\n");
409 goto error_no_wimax_dev;
410 }
411 ifindex = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_IFIDX]);
412 wimax_dev = wimax_dev_get_by_genl_info(info, ifindex);
413 if (wimax_dev == NULL)
414 goto error_no_wimax_dev;
415 dev = wimax_dev_to_dev(wimax_dev);
416 result = -EINVAL;
417 if (info->attrs[WIMAX_GNL_RFKILL_STATE] == NULL) {
418 dev_err(dev, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE "
419 "attribute\n");
420 goto error_no_pid;
421 }
422 new_state = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_STATE]);
423
424
425 result = wimax_rfkill(wimax_dev, new_state);
426error_no_pid:
427 dev_put(wimax_dev->net_dev);
428error_no_wimax_dev:
429 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
430 return result;
431}
432