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