aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/b43legacy/rfkill.c
blob: db6292642057939db082e867c6a9a26dedd40f4f (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
157
158
/*

  Broadcom B43legacy 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 "radio.h"
#include "b43legacy.h"


static void b43legacy_notify_rfkill_press(struct work_struct *work)
{
	struct b43legacy_rfkill *rfk = container_of(work,
						    struct b43legacy_rfkill,
						    notify_work);
	struct b43legacy_wl *wl = container_of(rfk, struct b43legacy_wl,
				  rfkill);
	struct b43legacy_wldev *dev;
	enum rfkill_state state;

	mutex_lock(&wl->mutex);
	dev = wl->current_dev;
	if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
		mutex_unlock(&wl->mutex);
		return;
	}
	if (dev->radio_hw_enable)
		state = RFKILL_STATE_ON;
	else
		state = RFKILL_STATE_OFF;
	b43legacyinfo(wl, "Radio hardware status changed to %s\n",
		dev->radio_hw_enable ? "ENABLED" : "DISABLED");
	mutex_unlock(&wl->mutex);

	if (rfk->rfkill) {
		/* Be careful. This calls back into the software toggle
		 * routines. So we must unlock before calling. */
		rfkill_switch_all(rfk->rfkill->type, state);
	}
}

/* Called when the RFKILL toggled in hardware.
 * This is called with the mutex locked. */
void b43legacy_rfkill_toggled(struct b43legacy_wldev *dev, bool on)
{
	struct b43legacy_wl *wl = dev->wl;

	B43legacy_WARN_ON(b43legacy_status(dev) < B43legacy_STAT_INITIALIZED);
	/* Update the RF status asynchronously, as rfkill will
	 * call back into the software toggle handler.
	 * This would deadlock if done synchronously. */
	queue_work(wl->hw->workqueue, &wl->rfkill.notify_work);
}

/* Called when the RFKILL toggled in software.
 * This is called without locking. */
static int b43legacy_rfkill_soft_toggle(void *data, enum rfkill_state state)
{
	struct b43legacy_wldev *dev = data;
	struct b43legacy_wl *wl = dev->wl;
	int err = 0;

	mutex_lock(&wl->mutex);
	if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
		goto out_unlock;

	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)
			b43legacy_radio_turn_on(dev);
		break;
	case RFKILL_STATE_OFF:
		if (dev->phy.radio_on)
			b43legacy_radio_turn_off(dev, 0);
		break;
	}

out_unlock:
	mutex_unlock(&wl->mutex);

	return err;
}

char *b43legacy_rfkill_led_name(struct b43legacy_wldev *dev)
{
	struct b43legacy_wl *wl = dev->wl;

	if (!wl->rfkill.rfkill)
		return NULL;
	return rfkill_get_led_name(wl->rfkill.rfkill);
}

void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
{
	struct b43legacy_wl *wl = dev->wl;
	struct b43legacy_rfkill *rfk = &(wl->rfkill);
	int err;

	snprintf(rfk->name, sizeof(rfk->name),
		 "b43legacy-%s", wiphy_name(wl->hw->wiphy));
	rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
	if (!rfk->rfkill)
		goto error;
	rfk->rfkill->name = rfk->name;
	rfk->rfkill->state = RFKILL_STATE_ON;
	rfk->rfkill->data = dev;
	rfk->rfkill->toggle_radio = b43legacy_rfkill_soft_toggle;
	rfk->rfkill->user_claim_unsupported = 1;

	INIT_WORK(&rfk->notify_work, b43legacy_notify_rfkill_press);

	err = rfkill_register(rfk->rfkill);
	if (err)
		goto error;

	return;
error:
	b43legacywarn(dev->wl, "Failed to initialize the RF-kill button\n");
	rfkill_free(rfk->rfkill);
	rfk->rfkill = NULL;
}

void b43legacy_rfkill_exit(struct b43legacy_wldev *dev)
{
	struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);

	if (!rfk->rfkill)
		return;
	cancel_work_sync(&rfk->notify_work);
	rfkill_unregister(rfk->rfkill);
	rfkill_free(rfk->rfkill);
	rfk->rfkill = NULL;
}