lib: test_sysctl: delete duplicated words
[linux-2.6-microblaze.git] / net / wimax / op-rfkill.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Linux WiMAX
4  * RF-kill framework integration
5  *
6  * Copyright (C) 2008 Intel Corporation <linux-wimax@intel.com>
7  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8  *
9  * This integrates into the Linux Kernel rfkill susbystem so that the
10  * drivers just have to do the bare minimal work, which is providing a
11  * method to set the software RF-Kill switch and to report changes in
12  * the software and hardware switch status.
13  *
14  * A non-polled generic rfkill device is embedded into the WiMAX
15  * subsystem's representation of a device.
16  *
17  * FIXME: Need polled support? Let drivers provide a poll routine
18  *        and hand it to rfkill ops then?
19  *
20  * All device drivers have to do is after wimax_dev_init(), call
21  * wimax_report_rfkill_hw() and wimax_report_rfkill_sw() to update
22  * initial state and then every time it changes. See wimax.h:struct
23  * wimax_dev for more information.
24  *
25  * ROADMAP
26  *
27  * wimax_gnl_doit_rfkill()      User space calling wimax_rfkill()
28  *   wimax_rfkill()             Kernel calling wimax_rfkill()
29  *     __wimax_rf_toggle_radio()
30  *
31  * wimax_rfkill_set_radio_block()  RF-Kill subsystem calling
32  *   __wimax_rf_toggle_radio()
33  *
34  * __wimax_rf_toggle_radio()
35  *   wimax_dev->op_rfkill_sw_toggle() Driver backend
36  *   __wimax_state_change()
37  *
38  * wimax_report_rfkill_sw()     Driver reports state change
39  *   __wimax_state_change()
40  *
41  * wimax_report_rfkill_hw()     Driver reports state change
42  *   __wimax_state_change()
43  *
44  * wimax_rfkill_add()           Initialize/shutdown rfkill support
45  * wimax_rfkill_rm()            [called by wimax_dev_add/rm()]
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  * wimax_report_rfkill_hw - Reports changes in the hardware RF switch
61  *
62  * @wimax_dev: WiMAX device descriptor
63  *
64  * @state: New state of the RF Kill switch. %WIMAX_RF_ON radio on,
65  *     %WIMAX_RF_OFF radio off.
66  *
67  * When the device detects a change in the state of thehardware RF
68  * switch, it must call this function to let the WiMAX kernel stack
69  * know that the state has changed so it can be properly propagated.
70  *
71  * The WiMAX stack caches the state (the driver doesn't need to). As
72  * well, as the change is propagated it will come back as a request to
73  * change the software state to mirror the hardware state.
74  *
75  * If the device doesn't have a hardware kill switch, just report
76  * it on initialization as always on (%WIMAX_RF_ON, radio on).
77  */
78 void 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         }
107 error_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 }
112 EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw);
113
114
115 /**
116  * wimax_report_rfkill_sw - Reports changes in the software RF switch
117  *
118  * @wimax_dev: WiMAX device descriptor
119  *
120  * @state: New state of the RF kill switch. %WIMAX_RF_ON radio on,
121  *     %WIMAX_RF_OFF radio off.
122  *
123  * Reports changes in the software RF switch state to the WiMAX stack.
124  *
125  * The main use is during initialization, so the driver can query the
126  * device for its current software radio kill switch state and feed it
127  * to the system.
128  *
129  * On the side, the device does not change the software state by
130  * itself. In practice, this can happen, as the device might decide to
131  * switch (in software) the radio off for different reasons.
132  */
133 void 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         }
159 error_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 }
164 EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw);
165
166
167 /*
168  * Callback for the RF Kill toggle operation
169  *
170  * This function is called by:
171  *
172  * - The rfkill subsystem when the RF-Kill key is pressed in the
173  *   hardware and the driver notifies through
174  *   wimax_report_rfkill_hw(). The rfkill subsystem ends up calling back
175  *   here so the software RF Kill switch state is changed to reflect
176  *   the hardware switch state.
177  *
178  * - When the user sets the state through sysfs' rfkill/state file
179  *
180  * - When the user calls wimax_rfkill().
181  *
182  * This call blocks!
183  *
184  * WARNING! When we call rfkill_unregister(), this will be called with
185  * state 0!
186  *
187  * WARNING: wimax_dev must be locked
188  */
189 static
190 int __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) /* No op? can't turn off */
204                 result = -ENXIO;
205         else                            /* No op? can turn on */
206                 result = 0;             /* should never happen tho */
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         }
214 out_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  * Translate from rfkill state to wimax state
223  *
224  * NOTE: Special state handling rules here
225  *
226  *     Just pretend the call didn't happen if we are in a state where
227  *     we know for sure it cannot be handled (WIMAX_ST_DOWN or
228  *     __WIMAX_ST_QUIESCING). rfkill() needs it to register and
229  *     unregister, as it will run this path.
230  *
231  * NOTE: This call will block until the operation is completed.
232  */
233 static 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
255 static const struct rfkill_ops wimax_rfkill_ops = {
256         .set_block = wimax_rfkill_set_radio_block,
257 };
258
259 /**
260  * wimax_rfkill - Set the software RF switch state for a WiMAX device
261  *
262  * @wimax_dev: WiMAX device descriptor
263  *
264  * @state: New RF state.
265  *
266  * Returns:
267  *
268  * >= 0 toggle state if ok, < 0 errno code on error. The toggle state
269  * is returned as a bitmap, bit 0 being the hardware RF state, bit 1
270  * the software RF state.
271  *
272  * 0 means disabled (%WIMAX_RF_ON, radio on), 1 means enabled radio
273  * off (%WIMAX_RF_OFF).
274  *
275  * Description:
276  *
277  * Called by the user when he wants to request the WiMAX radio to be
278  * switched on (%WIMAX_RF_ON) or off (%WIMAX_RF_OFF). With
279  * %WIMAX_RF_QUERY, just the current state is returned.
280  *
281  * NOTE:
282  *
283  * This call will block until the operation is complete.
284  */
285 int 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                 /* While initializing, < 1.4.3 wimax-tools versions use
295                  * this call to check if the device is a valid WiMAX
296                  * device; so we allow it to proceed always,
297                  * considering the radios are all off. */
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;
317 error:
318 error_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 }
324 EXPORT_SYMBOL(wimax_rfkill);
325
326
327 /*
328  * Register a new WiMAX device's RF Kill support
329  *
330  * WARNING: wimax_dev->mutex must be unlocked
331  */
332 int 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         /* Initialize RF Kill */
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         /* If there is no SW toggle op, SW RFKill is always on */
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
362 error_rfkill_register:
363         rfkill_destroy(wimax_dev->rfkill);
364 error_rfkill_allocate:
365         d_fnend(3, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
366         return result;
367 }
368
369
370 /*
371  * Deregister a WiMAX device's RF Kill support
372  *
373  * Ick, we can't call rfkill_free() after rfkill_unregister()...oh
374  * well.
375  *
376  * WARNING: wimax_dev->mutex must be unlocked
377  */
378 void 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  * Exporting to user space over generic netlink
390  *
391  * Parse the rfkill command from user space, return a combination
392  * value that describe the states of the different toggles.
393  *
394  * Only one attribute: the new state requested (on, off or no change,
395  * just query).
396  */
397
398 int 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         /* Execute the operation and send the result back to user space */
425         result = wimax_rfkill(wimax_dev, new_state);
426 error_no_pid:
427         dev_put(wimax_dev->net_dev);
428 error_no_wimax_dev:
429         d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
430         return result;
431 }