]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - drivers/net/wimax/i2400m/op-rfkill.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 268
[thirdparty/kernel/stable.git] / drivers / net / wimax / i2400m / op-rfkill.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Intel Wireless WiMAX Connection 2400m
4 * Implement backend for the WiMAX stack rfkill support
5 *
6 * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8 *
9 * The WiMAX kernel stack integrates into RF-Kill and keeps the
10 * switches's status. We just need to:
11 *
12 * - report changes in the HW RF Kill switch [with
13 * wimax_rfkill_{sw,hw}_report(), which happens when we detect those
14 * indications coming through hardware reports]. We also do it on
15 * initialization to let the stack know the initial HW state.
16 *
17 * - implement indications from the stack to change the SW RF Kill
18 * switch (coming from sysfs, the wimax stack or user space).
19 */
20 #include "i2400m.h"
21 #include <linux/wimax/i2400m.h>
22 #include <linux/slab.h>
23
24
25
26 #define D_SUBMODULE rfkill
27 #include "debug-levels.h"
28
29 /*
30 * Return true if the i2400m radio is in the requested wimax_rf_state state
31 *
32 */
33 static
34 int i2400m_radio_is(struct i2400m *i2400m, enum wimax_rf_state state)
35 {
36 if (state == WIMAX_RF_OFF)
37 return i2400m->state == I2400M_SS_RF_OFF
38 || i2400m->state == I2400M_SS_RF_SHUTDOWN;
39 else if (state == WIMAX_RF_ON)
40 /* state == WIMAX_RF_ON */
41 return i2400m->state != I2400M_SS_RF_OFF
42 && i2400m->state != I2400M_SS_RF_SHUTDOWN;
43 else {
44 BUG();
45 return -EINVAL; /* shut gcc warnings on certain arches */
46 }
47 }
48
49
50 /*
51 * WiMAX stack operation: implement SW RFKill toggling
52 *
53 * @wimax_dev: device descriptor
54 * @skb: skb where the message has been received; skb->data is
55 * expected to point to the message payload.
56 * @genl_info: passed by the generic netlink layer
57 *
58 * Generic Netlink will call this function when a message is sent from
59 * userspace to change the software RF-Kill switch status.
60 *
61 * This function will set the device's software RF-Kill switch state to
62 * match what is requested.
63 *
64 * NOTE: the i2400m has a strict state machine; we can only set the
65 * RF-Kill switch when it is on, the HW RF-Kill is on and the
66 * device is initialized. So we ignore errors steaming from not
67 * being in the right state (-EILSEQ).
68 */
69 int i2400m_op_rfkill_sw_toggle(struct wimax_dev *wimax_dev,
70 enum wimax_rf_state state)
71 {
72 int result;
73 struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
74 struct device *dev = i2400m_dev(i2400m);
75 struct sk_buff *ack_skb;
76 struct {
77 struct i2400m_l3l4_hdr hdr;
78 struct i2400m_tlv_rf_operation sw_rf;
79 } __packed *cmd;
80 char strerr[32];
81
82 d_fnstart(4, dev, "(wimax_dev %p state %d)\n", wimax_dev, state);
83
84 result = -ENOMEM;
85 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
86 if (cmd == NULL)
87 goto error_alloc;
88 cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_RF_CONTROL);
89 cmd->hdr.length = sizeof(cmd->sw_rf);
90 cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
91 cmd->sw_rf.hdr.type = cpu_to_le16(I2400M_TLV_RF_OPERATION);
92 cmd->sw_rf.hdr.length = cpu_to_le16(sizeof(cmd->sw_rf.status));
93 switch (state) {
94 case WIMAX_RF_OFF: /* RFKILL ON, radio OFF */
95 cmd->sw_rf.status = cpu_to_le32(2);
96 break;
97 case WIMAX_RF_ON: /* RFKILL OFF, radio ON */
98 cmd->sw_rf.status = cpu_to_le32(1);
99 break;
100 default:
101 BUG();
102 }
103
104 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
105 result = PTR_ERR(ack_skb);
106 if (IS_ERR(ack_skb)) {
107 dev_err(dev, "Failed to issue 'RF Control' command: %d\n",
108 result);
109 goto error_msg_to_dev;
110 }
111 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
112 strerr, sizeof(strerr));
113 if (result < 0) {
114 dev_err(dev, "'RF Control' (0x%04x) command failed: %d - %s\n",
115 I2400M_MT_CMD_RF_CONTROL, result, strerr);
116 goto error_cmd;
117 }
118
119 /* Now we wait for the state to change to RADIO_OFF or RADIO_ON */
120 result = wait_event_timeout(
121 i2400m->state_wq, i2400m_radio_is(i2400m, state),
122 5 * HZ);
123 if (result == 0)
124 result = -ETIMEDOUT;
125 if (result < 0)
126 dev_err(dev, "Error waiting for device to toggle RF state: "
127 "%d\n", result);
128 result = 0;
129 error_cmd:
130 kfree_skb(ack_skb);
131 error_msg_to_dev:
132 error_alloc:
133 d_fnend(4, dev, "(wimax_dev %p state %d) = %d\n",
134 wimax_dev, state, result);
135 return result;
136 }
137
138
139 /*
140 * Inform the WiMAX stack of changes in the RF Kill switches reported
141 * by the device
142 *
143 * @i2400m: device descriptor
144 * @rfss: TLV for RF Switches status; already validated
145 *
146 * NOTE: the reports on RF switch status cannot be trusted
147 * or used until the device is in a state of RADIO_OFF
148 * or greater.
149 */
150 void i2400m_report_tlv_rf_switches_status(
151 struct i2400m *i2400m,
152 const struct i2400m_tlv_rf_switches_status *rfss)
153 {
154 struct device *dev = i2400m_dev(i2400m);
155 enum i2400m_rf_switch_status hw, sw;
156 enum wimax_st wimax_state;
157
158 sw = le32_to_cpu(rfss->sw_rf_switch);
159 hw = le32_to_cpu(rfss->hw_rf_switch);
160
161 d_fnstart(3, dev, "(i2400m %p rfss %p [hw %u sw %u])\n",
162 i2400m, rfss, hw, sw);
163 /* We only process rw switch evens when the device has been
164 * fully initialized */
165 wimax_state = wimax_state_get(&i2400m->wimax_dev);
166 if (wimax_state < WIMAX_ST_RADIO_OFF) {
167 d_printf(3, dev, "ignoring RF switches report, state %u\n",
168 wimax_state);
169 goto out;
170 }
171 switch (sw) {
172 case I2400M_RF_SWITCH_ON: /* RF Kill disabled (radio on) */
173 wimax_report_rfkill_sw(&i2400m->wimax_dev, WIMAX_RF_ON);
174 break;
175 case I2400M_RF_SWITCH_OFF: /* RF Kill enabled (radio off) */
176 wimax_report_rfkill_sw(&i2400m->wimax_dev, WIMAX_RF_OFF);
177 break;
178 default:
179 dev_err(dev, "HW BUG? Unknown RF SW state 0x%x\n", sw);
180 }
181
182 switch (hw) {
183 case I2400M_RF_SWITCH_ON: /* RF Kill disabled (radio on) */
184 wimax_report_rfkill_hw(&i2400m->wimax_dev, WIMAX_RF_ON);
185 break;
186 case I2400M_RF_SWITCH_OFF: /* RF Kill enabled (radio off) */
187 wimax_report_rfkill_hw(&i2400m->wimax_dev, WIMAX_RF_OFF);
188 break;
189 default:
190 dev_err(dev, "HW BUG? Unknown RF HW state 0x%x\n", hw);
191 }
192 out:
193 d_fnend(3, dev, "(i2400m %p rfss %p [hw %u sw %u]) = void\n",
194 i2400m, rfss, hw, sw);
195 }