1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Linux RFKILL support for RTL8187
4 *
5 * Copyright (c) 2009 Herton Ronaldo Krzesinski <herton@mandriva.com.br>
6 *
7 * Based on the RFKILL handling in the r8187 driver, which is:
8 * Copyright (c) Realtek Semiconductor Corp. All rights reserved.
9 *
10 * Thanks to Realtek for their support!
11 */
12
13#include <linux/types.h>
14#include <linux/usb.h>
15#include <net/mac80211.h>
16
17#include "rtl8187.h"
18#include "rfkill.h"
19
20static bool rtl8187_is_radio_enabled(struct rtl8187_priv *priv)
21{
22	u8 gpio;
23
24	gpio = rtl818x_ioread8(priv, &priv->map->GPIO0);
25	rtl818x_iowrite8(priv, &priv->map->GPIO0, gpio & ~priv->rfkill_mask);
26	gpio = rtl818x_ioread8(priv, &priv->map->GPIO1);
27
28	return gpio & priv->rfkill_mask;
29}
30
31void rtl8187_rfkill_init(struct ieee80211_hw *hw)
32{
33	struct rtl8187_priv *priv = hw->priv;
34
35	priv->rfkill_off = rtl8187_is_radio_enabled(priv);
36	printk(KERN_INFO "rtl8187: wireless switch is %s\n",
37	       priv->rfkill_off ? "on" : "off");
38	wiphy_rfkill_set_hw_state(hw->wiphy, !priv->rfkill_off);
39	wiphy_rfkill_start_polling(hw->wiphy);
40}
41
42void rtl8187_rfkill_poll(struct ieee80211_hw *hw)
43{
44	bool enabled;
45	struct rtl8187_priv *priv = hw->priv;
46
47	mutex_lock(&priv->conf_mutex);
48	enabled = rtl8187_is_radio_enabled(priv);
49	if (unlikely(enabled != priv->rfkill_off)) {
50		priv->rfkill_off = enabled;
51		printk(KERN_INFO "rtl8187: wireless radio switch turned %s\n",
52		       enabled ? "on" : "off");
53		wiphy_rfkill_set_hw_state(hw->wiphy, !enabled);
54	}
55	mutex_unlock(&priv->conf_mutex);
56}
57
58void rtl8187_rfkill_exit(struct ieee80211_hw *hw)
59{
60	wiphy_rfkill_stop_polling(hw->wiphy);
61}
62