18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Intel Wireless WiMAX Connection 2400m 48c2ecf20Sopenharmony_ci * Implement backend for the WiMAX stack rfkill support 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com> 78c2ecf20Sopenharmony_ci * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * The WiMAX kernel stack integrates into RF-Kill and keeps the 108c2ecf20Sopenharmony_ci * switches's status. We just need to: 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * - report changes in the HW RF Kill switch [with 138c2ecf20Sopenharmony_ci * wimax_rfkill_{sw,hw}_report(), which happens when we detect those 148c2ecf20Sopenharmony_ci * indications coming through hardware reports]. We also do it on 158c2ecf20Sopenharmony_ci * initialization to let the stack know the initial HW state. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * - implement indications from the stack to change the SW RF Kill 188c2ecf20Sopenharmony_ci * switch (coming from sysfs, the wimax stack or user space). 198c2ecf20Sopenharmony_ci */ 208c2ecf20Sopenharmony_ci#include "i2400m.h" 218c2ecf20Sopenharmony_ci#include <linux/wimax/i2400m.h> 228c2ecf20Sopenharmony_ci#include <linux/slab.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define D_SUBMODULE rfkill 278c2ecf20Sopenharmony_ci#include "debug-levels.h" 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* 308c2ecf20Sopenharmony_ci * Return true if the i2400m radio is in the requested wimax_rf_state state 318c2ecf20Sopenharmony_ci * 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_cistatic 348c2ecf20Sopenharmony_ciint i2400m_radio_is(struct i2400m *i2400m, enum wimax_rf_state state) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci if (state == WIMAX_RF_OFF) 378c2ecf20Sopenharmony_ci return i2400m->state == I2400M_SS_RF_OFF 388c2ecf20Sopenharmony_ci || i2400m->state == I2400M_SS_RF_SHUTDOWN; 398c2ecf20Sopenharmony_ci else if (state == WIMAX_RF_ON) 408c2ecf20Sopenharmony_ci /* state == WIMAX_RF_ON */ 418c2ecf20Sopenharmony_ci return i2400m->state != I2400M_SS_RF_OFF 428c2ecf20Sopenharmony_ci && i2400m->state != I2400M_SS_RF_SHUTDOWN; 438c2ecf20Sopenharmony_ci else { 448c2ecf20Sopenharmony_ci BUG(); 458c2ecf20Sopenharmony_ci return -EINVAL; /* shut gcc warnings on certain arches */ 468c2ecf20Sopenharmony_ci } 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* 518c2ecf20Sopenharmony_ci * WiMAX stack operation: implement SW RFKill toggling 528c2ecf20Sopenharmony_ci * 538c2ecf20Sopenharmony_ci * @wimax_dev: device descriptor 548c2ecf20Sopenharmony_ci * @skb: skb where the message has been received; skb->data is 558c2ecf20Sopenharmony_ci * expected to point to the message payload. 568c2ecf20Sopenharmony_ci * @genl_info: passed by the generic netlink layer 578c2ecf20Sopenharmony_ci * 588c2ecf20Sopenharmony_ci * Generic Netlink will call this function when a message is sent from 598c2ecf20Sopenharmony_ci * userspace to change the software RF-Kill switch status. 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * This function will set the device's software RF-Kill switch state to 628c2ecf20Sopenharmony_ci * match what is requested. 638c2ecf20Sopenharmony_ci * 648c2ecf20Sopenharmony_ci * NOTE: the i2400m has a strict state machine; we can only set the 658c2ecf20Sopenharmony_ci * RF-Kill switch when it is on, the HW RF-Kill is on and the 668c2ecf20Sopenharmony_ci * device is initialized. So we ignore errors steaming from not 678c2ecf20Sopenharmony_ci * being in the right state (-EILSEQ). 688c2ecf20Sopenharmony_ci */ 698c2ecf20Sopenharmony_ciint i2400m_op_rfkill_sw_toggle(struct wimax_dev *wimax_dev, 708c2ecf20Sopenharmony_ci enum wimax_rf_state state) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci int result; 738c2ecf20Sopenharmony_ci struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev); 748c2ecf20Sopenharmony_ci struct device *dev = i2400m_dev(i2400m); 758c2ecf20Sopenharmony_ci struct sk_buff *ack_skb; 768c2ecf20Sopenharmony_ci struct { 778c2ecf20Sopenharmony_ci struct i2400m_l3l4_hdr hdr; 788c2ecf20Sopenharmony_ci struct i2400m_tlv_rf_operation sw_rf; 798c2ecf20Sopenharmony_ci } __packed *cmd; 808c2ecf20Sopenharmony_ci char strerr[32]; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci d_fnstart(4, dev, "(wimax_dev %p state %d)\n", wimax_dev, state); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci result = -ENOMEM; 858c2ecf20Sopenharmony_ci cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 868c2ecf20Sopenharmony_ci if (cmd == NULL) 878c2ecf20Sopenharmony_ci goto error_alloc; 888c2ecf20Sopenharmony_ci cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_RF_CONTROL); 898c2ecf20Sopenharmony_ci cmd->hdr.length = cpu_to_le16(sizeof(cmd->sw_rf)); 908c2ecf20Sopenharmony_ci cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION); 918c2ecf20Sopenharmony_ci cmd->sw_rf.hdr.type = cpu_to_le16(I2400M_TLV_RF_OPERATION); 928c2ecf20Sopenharmony_ci cmd->sw_rf.hdr.length = cpu_to_le16(sizeof(cmd->sw_rf.status)); 938c2ecf20Sopenharmony_ci switch (state) { 948c2ecf20Sopenharmony_ci case WIMAX_RF_OFF: /* RFKILL ON, radio OFF */ 958c2ecf20Sopenharmony_ci cmd->sw_rf.status = cpu_to_le32(2); 968c2ecf20Sopenharmony_ci break; 978c2ecf20Sopenharmony_ci case WIMAX_RF_ON: /* RFKILL OFF, radio ON */ 988c2ecf20Sopenharmony_ci cmd->sw_rf.status = cpu_to_le32(1); 998c2ecf20Sopenharmony_ci break; 1008c2ecf20Sopenharmony_ci default: 1018c2ecf20Sopenharmony_ci BUG(); 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd)); 1058c2ecf20Sopenharmony_ci result = PTR_ERR(ack_skb); 1068c2ecf20Sopenharmony_ci if (IS_ERR(ack_skb)) { 1078c2ecf20Sopenharmony_ci dev_err(dev, "Failed to issue 'RF Control' command: %d\n", 1088c2ecf20Sopenharmony_ci result); 1098c2ecf20Sopenharmony_ci goto error_msg_to_dev; 1108c2ecf20Sopenharmony_ci } 1118c2ecf20Sopenharmony_ci result = i2400m_msg_check_status(wimax_msg_data(ack_skb), 1128c2ecf20Sopenharmony_ci strerr, sizeof(strerr)); 1138c2ecf20Sopenharmony_ci if (result < 0) { 1148c2ecf20Sopenharmony_ci dev_err(dev, "'RF Control' (0x%04x) command failed: %d - %s\n", 1158c2ecf20Sopenharmony_ci I2400M_MT_CMD_RF_CONTROL, result, strerr); 1168c2ecf20Sopenharmony_ci goto error_cmd; 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci /* Now we wait for the state to change to RADIO_OFF or RADIO_ON */ 1208c2ecf20Sopenharmony_ci result = wait_event_timeout( 1218c2ecf20Sopenharmony_ci i2400m->state_wq, i2400m_radio_is(i2400m, state), 1228c2ecf20Sopenharmony_ci 5 * HZ); 1238c2ecf20Sopenharmony_ci if (result == 0) 1248c2ecf20Sopenharmony_ci result = -ETIMEDOUT; 1258c2ecf20Sopenharmony_ci if (result < 0) 1268c2ecf20Sopenharmony_ci dev_err(dev, "Error waiting for device to toggle RF state: " 1278c2ecf20Sopenharmony_ci "%d\n", result); 1288c2ecf20Sopenharmony_ci result = 0; 1298c2ecf20Sopenharmony_cierror_cmd: 1308c2ecf20Sopenharmony_ci kfree_skb(ack_skb); 1318c2ecf20Sopenharmony_cierror_msg_to_dev: 1328c2ecf20Sopenharmony_cierror_alloc: 1338c2ecf20Sopenharmony_ci d_fnend(4, dev, "(wimax_dev %p state %d) = %d\n", 1348c2ecf20Sopenharmony_ci wimax_dev, state, result); 1358c2ecf20Sopenharmony_ci kfree(cmd); 1368c2ecf20Sopenharmony_ci return result; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci/* 1418c2ecf20Sopenharmony_ci * Inform the WiMAX stack of changes in the RF Kill switches reported 1428c2ecf20Sopenharmony_ci * by the device 1438c2ecf20Sopenharmony_ci * 1448c2ecf20Sopenharmony_ci * @i2400m: device descriptor 1458c2ecf20Sopenharmony_ci * @rfss: TLV for RF Switches status; already validated 1468c2ecf20Sopenharmony_ci * 1478c2ecf20Sopenharmony_ci * NOTE: the reports on RF switch status cannot be trusted 1488c2ecf20Sopenharmony_ci * or used until the device is in a state of RADIO_OFF 1498c2ecf20Sopenharmony_ci * or greater. 1508c2ecf20Sopenharmony_ci */ 1518c2ecf20Sopenharmony_civoid i2400m_report_tlv_rf_switches_status( 1528c2ecf20Sopenharmony_ci struct i2400m *i2400m, 1538c2ecf20Sopenharmony_ci const struct i2400m_tlv_rf_switches_status *rfss) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci struct device *dev = i2400m_dev(i2400m); 1568c2ecf20Sopenharmony_ci enum i2400m_rf_switch_status hw, sw; 1578c2ecf20Sopenharmony_ci enum wimax_st wimax_state; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci sw = le32_to_cpu(rfss->sw_rf_switch); 1608c2ecf20Sopenharmony_ci hw = le32_to_cpu(rfss->hw_rf_switch); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci d_fnstart(3, dev, "(i2400m %p rfss %p [hw %u sw %u])\n", 1638c2ecf20Sopenharmony_ci i2400m, rfss, hw, sw); 1648c2ecf20Sopenharmony_ci /* We only process rw switch evens when the device has been 1658c2ecf20Sopenharmony_ci * fully initialized */ 1668c2ecf20Sopenharmony_ci wimax_state = wimax_state_get(&i2400m->wimax_dev); 1678c2ecf20Sopenharmony_ci if (wimax_state < WIMAX_ST_RADIO_OFF) { 1688c2ecf20Sopenharmony_ci d_printf(3, dev, "ignoring RF switches report, state %u\n", 1698c2ecf20Sopenharmony_ci wimax_state); 1708c2ecf20Sopenharmony_ci goto out; 1718c2ecf20Sopenharmony_ci } 1728c2ecf20Sopenharmony_ci switch (sw) { 1738c2ecf20Sopenharmony_ci case I2400M_RF_SWITCH_ON: /* RF Kill disabled (radio on) */ 1748c2ecf20Sopenharmony_ci wimax_report_rfkill_sw(&i2400m->wimax_dev, WIMAX_RF_ON); 1758c2ecf20Sopenharmony_ci break; 1768c2ecf20Sopenharmony_ci case I2400M_RF_SWITCH_OFF: /* RF Kill enabled (radio off) */ 1778c2ecf20Sopenharmony_ci wimax_report_rfkill_sw(&i2400m->wimax_dev, WIMAX_RF_OFF); 1788c2ecf20Sopenharmony_ci break; 1798c2ecf20Sopenharmony_ci default: 1808c2ecf20Sopenharmony_ci dev_err(dev, "HW BUG? Unknown RF SW state 0x%x\n", sw); 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci switch (hw) { 1848c2ecf20Sopenharmony_ci case I2400M_RF_SWITCH_ON: /* RF Kill disabled (radio on) */ 1858c2ecf20Sopenharmony_ci wimax_report_rfkill_hw(&i2400m->wimax_dev, WIMAX_RF_ON); 1868c2ecf20Sopenharmony_ci break; 1878c2ecf20Sopenharmony_ci case I2400M_RF_SWITCH_OFF: /* RF Kill enabled (radio off) */ 1888c2ecf20Sopenharmony_ci wimax_report_rfkill_hw(&i2400m->wimax_dev, WIMAX_RF_OFF); 1898c2ecf20Sopenharmony_ci break; 1908c2ecf20Sopenharmony_ci default: 1918c2ecf20Sopenharmony_ci dev_err(dev, "HW BUG? Unknown RF HW state 0x%x\n", hw); 1928c2ecf20Sopenharmony_ci } 1938c2ecf20Sopenharmony_ciout: 1948c2ecf20Sopenharmony_ci d_fnend(3, dev, "(i2400m %p rfss %p [hw %u sw %u]) = void\n", 1958c2ecf20Sopenharmony_ci i2400m, rfss, hw, sw); 1968c2ecf20Sopenharmony_ci} 197