18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Linux RFKILL support for RTL8187
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2009 Herton Ronaldo Krzesinski <herton@mandriva.com.br>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on the RFKILL handling in the r8187 driver, which is:
88c2ecf20Sopenharmony_ci * Copyright (c) Realtek Semiconductor Corp. All rights reserved.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Thanks to Realtek for their support!
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <linux/usb.h>
158c2ecf20Sopenharmony_ci#include <net/mac80211.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "rtl8187.h"
188c2ecf20Sopenharmony_ci#include "rfkill.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic bool rtl8187_is_radio_enabled(struct rtl8187_priv *priv)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	u8 gpio;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	gpio = rtl818x_ioread8(priv, &priv->map->GPIO0);
258c2ecf20Sopenharmony_ci	rtl818x_iowrite8(priv, &priv->map->GPIO0, gpio & ~priv->rfkill_mask);
268c2ecf20Sopenharmony_ci	gpio = rtl818x_ioread8(priv, &priv->map->GPIO1);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	return gpio & priv->rfkill_mask;
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_civoid rtl8187_rfkill_init(struct ieee80211_hw *hw)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	struct rtl8187_priv *priv = hw->priv;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	priv->rfkill_off = rtl8187_is_radio_enabled(priv);
368c2ecf20Sopenharmony_ci	printk(KERN_INFO "rtl8187: wireless switch is %s\n",
378c2ecf20Sopenharmony_ci	       priv->rfkill_off ? "on" : "off");
388c2ecf20Sopenharmony_ci	wiphy_rfkill_set_hw_state(hw->wiphy, !priv->rfkill_off);
398c2ecf20Sopenharmony_ci	wiphy_rfkill_start_polling(hw->wiphy);
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_civoid rtl8187_rfkill_poll(struct ieee80211_hw *hw)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	bool enabled;
458c2ecf20Sopenharmony_ci	struct rtl8187_priv *priv = hw->priv;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	mutex_lock(&priv->conf_mutex);
488c2ecf20Sopenharmony_ci	enabled = rtl8187_is_radio_enabled(priv);
498c2ecf20Sopenharmony_ci	if (unlikely(enabled != priv->rfkill_off)) {
508c2ecf20Sopenharmony_ci		priv->rfkill_off = enabled;
518c2ecf20Sopenharmony_ci		printk(KERN_INFO "rtl8187: wireless radio switch turned %s\n",
528c2ecf20Sopenharmony_ci		       enabled ? "on" : "off");
538c2ecf20Sopenharmony_ci		wiphy_rfkill_set_hw_state(hw->wiphy, !enabled);
548c2ecf20Sopenharmony_ci	}
558c2ecf20Sopenharmony_ci	mutex_unlock(&priv->conf_mutex);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_civoid rtl8187_rfkill_exit(struct ieee80211_hw *hw)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	wiphy_rfkill_stop_polling(hw->wiphy);
618c2ecf20Sopenharmony_ci}
62