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
49
50
51
52
53
54
55
56
57
58
59
60
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>
68#include <linux/export.h>
69#include "wimax-internal.h"
70
71#define D_SUBMODULE op_rfkill
72#include "debug-levels.h"
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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;
99
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;
111 if (wimax_dev->rf_hw == WIMAX_RF_ON &&
112 wimax_dev->rf_sw == WIMAX_RF_ON)
113 wimax_state = WIMAX_ST_READY;
114 else
115 wimax_state = WIMAX_ST_RADIO_OFF;
116
117 result = rfkill_set_hw_state(wimax_dev->rfkill,
118 state == WIMAX_RF_OFF);
119
120 __wimax_state_change(wimax_dev, wimax_state);
121 }
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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;
167 if (wimax_dev->rf_hw == WIMAX_RF_ON &&
168 wimax_dev->rf_sw == WIMAX_RF_ON)
169 wimax_state = WIMAX_ST_READY;
170 else
171 wimax_state = WIMAX_ST_RADIO_OFF;
172 __wimax_state_change(wimax_dev, wimax_state);
173 rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
174 }
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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)
220 result = -ENXIO;
221 else
222 result = 0;
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
239
240
241
242
243
244
245
246
247
248
249static int wimax_rfkill_set_radio_block(void *data, bool blocked)
250{
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
256 d_fnstart(3, dev, "(wimax_dev %p blocked %u)\n", wimax_dev, blocked);
257 rf_state = WIMAX_RF_ON;
258 if (blocked)
259 rf_state = WIMAX_RF_OFF;
260 mutex_lock(&wimax_dev->mutex);
261 if (wimax_dev->state <= __WIMAX_ST_QUIESCING)
262 result = 0;
263 else
264 result = __wimax_rf_toggle_radio(wimax_dev, rf_state);
265 mutex_unlock(&wimax_dev->mutex);
266 d_fnend(3, dev, "(wimax_dev %p blocked %u) = %d\n",
267 wimax_dev, blocked, result);
268 return result;
269}
270
271static const struct rfkill_ops wimax_rfkill_ops = {
272 .set_block = wimax_rfkill_set_radio_block,
273};
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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);
309 if (result < 0) {
310
311
312
313
314 if (result == -ENOMEDIUM && state == WIMAX_RF_QUERY)
315 result = WIMAX_RF_OFF << 1 | WIMAX_RF_OFF;
316 goto error_not_ready;
317 }
318 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;
324 rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
325 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
345
346
347
348int wimax_rfkill_add(struct wimax_dev *wimax_dev)
349{
350 int result;
351 struct rfkill *rfkill;
352 struct device *dev = wimax_dev_to_dev(wimax_dev);
353
354 d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
355
356 result = -ENOMEM;
357 rfkill = rfkill_alloc(wimax_dev->name, dev, RFKILL_TYPE_WIMAX,
358 &wimax_rfkill_ops, wimax_dev);
359 if (rfkill == NULL)
360 goto error_rfkill_allocate;
361
362 d_printf(1, dev, "rfkill %p\n", rfkill);
363
364 wimax_dev->rfkill = rfkill;
365
366 rfkill_init_sw_state(rfkill, 1);
367 result = rfkill_register(wimax_dev->rfkill);
368 if (result < 0)
369 goto error_rfkill_register;
370
371
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
378error_rfkill_register:
379 rfkill_destroy(wimax_dev->rfkill);
380error_rfkill_allocate:
381 d_fnend(3, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
382 return result;
383}
384
385
386
387
388
389
390
391
392
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);
398 rfkill_unregister(wimax_dev->rfkill);
399 rfkill_destroy(wimax_dev->rfkill);
400 d_fnend(3, dev, "(wimax_dev %p)\n", wimax_dev);
401}
402
403
404
405
406
407
408
409
410
411
412
413
414static const struct nla_policy wimax_gnl_rfkill_policy[WIMAX_GNL_ATTR_MAX + 1] = {
415 [WIMAX_GNL_RFKILL_IFIDX] = {
416 .type = NLA_U32,
417 },
418 [WIMAX_GNL_RFKILL_STATE] = {
419 .type = NLA_U32
420 },
421};
422
423
424static
425int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info)
426{
427 int result, ifindex;
428 struct wimax_dev *wimax_dev;
429 struct device *dev;
430 enum wimax_rf_state new_state;
431
432 d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
433 result = -ENODEV;
434 if (info->attrs[WIMAX_GNL_RFKILL_IFIDX] == NULL) {
435 printk(KERN_ERR "WIMAX_GNL_OP_RFKILL: can't find IFIDX "
436 "attribute\n");
437 goto error_no_wimax_dev;
438 }
439 ifindex = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_IFIDX]);
440 wimax_dev = wimax_dev_get_by_genl_info(info, ifindex);
441 if (wimax_dev == NULL)
442 goto error_no_wimax_dev;
443 dev = wimax_dev_to_dev(wimax_dev);
444 result = -EINVAL;
445 if (info->attrs[WIMAX_GNL_RFKILL_STATE] == NULL) {
446 dev_err(dev, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE "
447 "attribute\n");
448 goto error_no_pid;
449 }
450 new_state = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_STATE]);
451
452
453 result = wimax_rfkill(wimax_dev, new_state);
454error_no_pid:
455 dev_put(wimax_dev->net_dev);
456error_no_wimax_dev:
457 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
458 return result;
459}
460
461
462struct genl_ops wimax_gnl_rfkill = {
463 .cmd = WIMAX_GNL_OP_RFKILL,
464 .flags = GENL_ADMIN_PERM,
465 .policy = wimax_gnl_rfkill_policy,
466 .doit = wimax_gnl_doit_rfkill,
467 .dumpit = NULL,
468};
469
470