1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel Wireless WiMAX Connection 2400m
4 * Implement backend for the WiMAX stack rfkill support
6 * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * The WiMAX kernel stack integrates into RF-Kill and keeps the
10 * switches's status. We just need to:
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.
17 * - implement indications from the stack to change the SW RF Kill
18 * switch (coming from sysfs, the wimax stack or user space).
21 #include <linux/wimax/i2400m.h>
22 #include <linux/slab.h>
26 #define D_SUBMODULE rfkill
27 #include "debug-levels.h"
30 * Return true if the i2400m radio is in the requested wimax_rf_state state
34 int i2400m_radio_is(struct i2400m
*i2400m
, enum wimax_rf_state state
)
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
;
45 return -EINVAL
; /* shut gcc warnings on certain arches */
51 * WiMAX stack operation: implement SW RFKill toggling
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
58 * Generic Netlink will call this function when a message is sent from
59 * userspace to change the software RF-Kill switch status.
61 * This function will set the device's software RF-Kill switch state to
62 * match what is requested.
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).
69 int i2400m_op_rfkill_sw_toggle(struct wimax_dev
*wimax_dev
,
70 enum wimax_rf_state state
)
73 struct i2400m
*i2400m
= wimax_dev_to_i2400m(wimax_dev
);
74 struct device
*dev
= i2400m_dev(i2400m
);
75 struct sk_buff
*ack_skb
;
77 struct i2400m_l3l4_hdr hdr
;
78 struct i2400m_tlv_rf_operation sw_rf
;
82 d_fnstart(4, dev
, "(wimax_dev %p state %d)\n", wimax_dev
, state
);
85 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
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
));
94 case WIMAX_RF_OFF
: /* RFKILL ON, radio OFF */
95 cmd
->sw_rf
.status
= cpu_to_le32(2);
97 case WIMAX_RF_ON
: /* RFKILL OFF, radio ON */
98 cmd
->sw_rf
.status
= cpu_to_le32(1);
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",
109 goto error_msg_to_dev
;
111 result
= i2400m_msg_check_status(wimax_msg_data(ack_skb
),
112 strerr
, sizeof(strerr
));
114 dev_err(dev
, "'RF Control' (0x%04x) command failed: %d - %s\n",
115 I2400M_MT_CMD_RF_CONTROL
, result
, strerr
);
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
),
126 dev_err(dev
, "Error waiting for device to toggle RF state: "
133 d_fnend(4, dev
, "(wimax_dev %p state %d) = %d\n",
134 wimax_dev
, state
, result
);
141 * Inform the WiMAX stack of changes in the RF Kill switches reported
144 * @i2400m: device descriptor
145 * @rfss: TLV for RF Switches status; already validated
147 * NOTE: the reports on RF switch status cannot be trusted
148 * or used until the device is in a state of RADIO_OFF
151 void i2400m_report_tlv_rf_switches_status(
152 struct i2400m
*i2400m
,
153 const struct i2400m_tlv_rf_switches_status
*rfss
)
155 struct device
*dev
= i2400m_dev(i2400m
);
156 enum i2400m_rf_switch_status hw
, sw
;
157 enum wimax_st wimax_state
;
159 sw
= le32_to_cpu(rfss
->sw_rf_switch
);
160 hw
= le32_to_cpu(rfss
->hw_rf_switch
);
162 d_fnstart(3, dev
, "(i2400m %p rfss %p [hw %u sw %u])\n",
163 i2400m
, rfss
, hw
, sw
);
164 /* We only process rw switch evens when the device has been
165 * fully initialized */
166 wimax_state
= wimax_state_get(&i2400m
->wimax_dev
);
167 if (wimax_state
< WIMAX_ST_RADIO_OFF
) {
168 d_printf(3, dev
, "ignoring RF switches report, state %u\n",
173 case I2400M_RF_SWITCH_ON
: /* RF Kill disabled (radio on) */
174 wimax_report_rfkill_sw(&i2400m
->wimax_dev
, WIMAX_RF_ON
);
176 case I2400M_RF_SWITCH_OFF
: /* RF Kill enabled (radio off) */
177 wimax_report_rfkill_sw(&i2400m
->wimax_dev
, WIMAX_RF_OFF
);
180 dev_err(dev
, "HW BUG? Unknown RF SW state 0x%x\n", sw
);
184 case I2400M_RF_SWITCH_ON
: /* RF Kill disabled (radio on) */
185 wimax_report_rfkill_hw(&i2400m
->wimax_dev
, WIMAX_RF_ON
);
187 case I2400M_RF_SWITCH_OFF
: /* RF Kill enabled (radio off) */
188 wimax_report_rfkill_hw(&i2400m
->wimax_dev
, WIMAX_RF_OFF
);
191 dev_err(dev
, "HW BUG? Unknown RF HW state 0x%x\n", hw
);
194 d_fnend(3, dev
, "(i2400m %p rfss %p [hw %u sw %u]) = void\n",
195 i2400m
, rfss
, hw
, sw
);