rfkill.c 4.3 KB
Newer Older
M
Michael Buesch 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*

  Broadcom B43 wireless driver
  RFKILL support

  Copyright (c) 2007 Michael Buesch <mb@bu3sch.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  Boston, MA 02110-1301, USA.

*/

#include "rfkill.h"
#include "b43.h"


29 30
/* Returns TRUE, if the radio is enabled in hardware. */
static bool b43_is_hw_radio_enabled(struct b43_wldev *dev)
M
Michael Buesch 已提交
31
{
32 33 34 35 36 37 38 39
	if (dev->phy.rev >= 3) {
		if (!(b43_read32(dev, B43_MMIO_RADIO_HWENABLED_HI)
		      & B43_MMIO_RADIO_HWENABLED_HI_MASK))
			return 1;
	} else {
		if (b43_read16(dev, B43_MMIO_RADIO_HWENABLED_LO)
		    & B43_MMIO_RADIO_HWENABLED_LO_MASK)
			return 1;
M
Michael Buesch 已提交
40
	}
41
	return 0;
M
Michael Buesch 已提交
42 43
}

44 45
/* The poll callback for the hardware button. */
static void b43_rfkill_poll(struct input_polled_dev *poll_dev)
M
Michael Buesch 已提交
46
{
47
	struct b43_wldev *dev = poll_dev->private;
M
Michael Buesch 已提交
48
	struct b43_wl *wl = dev->wl;
49
	bool enabled;
M
Michael Buesch 已提交
50
	bool report_change = 0;
M
Michael Buesch 已提交
51

52
	mutex_lock(&wl->mutex);
M
Michael Buesch 已提交
53
	B43_WARN_ON(b43_status(dev) < B43_STAT_INITIALIZED);
54 55 56
	enabled = b43_is_hw_radio_enabled(dev);
	if (unlikely(enabled != dev->radio_hw_enable)) {
		dev->radio_hw_enable = enabled;
M
Michael Buesch 已提交
57
		report_change = 1;
58 59
		b43info(wl, "Radio hardware status changed to %s\n",
			enabled ? "ENABLED" : "DISABLED");
M
Michael Buesch 已提交
60 61 62 63
	}
	mutex_unlock(&wl->mutex);

	if (unlikely(report_change))
64
		input_report_key(poll_dev->input, KEY_WLAN, enabled);
M
Michael Buesch 已提交
65 66
}

M
Michael Buesch 已提交
67
/* Called when the RFKILL toggled in software. */
M
Michael Buesch 已提交
68 69 70 71 72 73
static int b43_rfkill_soft_toggle(void *data, enum rfkill_state state)
{
	struct b43_wldev *dev = data;
	struct b43_wl *wl = dev->wl;
	int err = 0;

M
Michael Buesch 已提交
74 75
	if (!wl->rfkill.registered)
		return 0;
M
Michael Buesch 已提交
76

M
Michael Buesch 已提交
77 78
	mutex_lock(&wl->mutex);
	B43_WARN_ON(b43_status(dev) < B43_STAT_INITIALIZED);
M
Michael Buesch 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	switch (state) {
	case RFKILL_STATE_ON:
		if (!dev->radio_hw_enable) {
			/* No luck. We can't toggle the hardware RF-kill
			 * button from software. */
			err = -EBUSY;
			goto out_unlock;
		}
		if (!dev->phy.radio_on)
			b43_radio_turn_on(dev);
		break;
	case RFKILL_STATE_OFF:
		if (dev->phy.radio_on)
			b43_radio_turn_off(dev, 0);
		break;
	}
out_unlock:
	mutex_unlock(&wl->mutex);

	return err;
}

char * b43_rfkill_led_name(struct b43_wldev *dev)
{
M
Michael Buesch 已提交
103
	struct b43_rfkill *rfk = &(dev->wl->rfkill);
M
Michael Buesch 已提交
104

M
Michael Buesch 已提交
105
	if (!rfk->registered)
M
Michael Buesch 已提交
106
		return NULL;
M
Michael Buesch 已提交
107
	return rfkill_get_led_name(rfk->rfkill);
M
Michael Buesch 已提交
108 109 110 111 112 113 114 115
}

void b43_rfkill_init(struct b43_wldev *dev)
{
	struct b43_wl *wl = dev->wl;
	struct b43_rfkill *rfk = &(wl->rfkill);
	int err;

M
Michael Buesch 已提交
116
	rfk->registered = 0;
117

M
Michael Buesch 已提交
118 119 120
	rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
	if (!rfk->rfkill)
		goto out_error;
M
Michael Buesch 已提交
121 122 123 124 125 126 127 128
	snprintf(rfk->name, sizeof(rfk->name),
		 "b43-%s", wiphy_name(wl->hw->wiphy));
	rfk->rfkill->name = rfk->name;
	rfk->rfkill->state = RFKILL_STATE_ON;
	rfk->rfkill->data = dev;
	rfk->rfkill->toggle_radio = b43_rfkill_soft_toggle;
	rfk->rfkill->user_claim_unsupported = 1;

129
	rfk->poll_dev = input_allocate_polled_device();
M
Michael Buesch 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	if (!rfk->poll_dev)
		goto err_free_rfk;
	rfk->poll_dev->private = dev;
	rfk->poll_dev->poll = b43_rfkill_poll;
	rfk->poll_dev->poll_interval = 1000; /* msecs */

	err = rfkill_register(rfk->rfkill);
	if (err)
		goto err_free_polldev;
	err = input_register_polled_device(rfk->poll_dev);
	if (err)
		goto err_unreg_rfk;

	rfk->registered = 1;

	return;
err_unreg_rfk:
	rfkill_unregister(rfk->rfkill);
err_free_polldev:
	input_free_polled_device(rfk->poll_dev);
	rfk->poll_dev = NULL;
err_free_rfk:
	rfkill_free(rfk->rfkill);
	rfk->rfkill = NULL;
out_error:
	rfk->registered = 0;
	b43warn(wl, "RF-kill button init failed\n");
M
Michael Buesch 已提交
157 158
}

M
Michael Buesch 已提交
159
void b43_rfkill_exit(struct b43_wldev *dev)
M
Michael Buesch 已提交
160 161 162
{
	struct b43_rfkill *rfk = &(dev->wl->rfkill);

M
Michael Buesch 已提交
163 164 165 166 167 168
	if (!rfk->registered)
		return;
	rfk->registered = 0;

	input_unregister_polled_device(rfk->poll_dev);
	rfkill_unregister(rfk->rfkill);
169 170
	input_free_polled_device(rfk->poll_dev);
	rfk->poll_dev = NULL;
M
Michael Buesch 已提交
171 172 173
	rfkill_free(rfk->rfkill);
	rfk->rfkill = NULL;
}