Skip to content

Commit 19d337d

Browse files
jmberglinvjw
authored andcommitted
rfkill: rewrite
This patch completely rewrites the rfkill core to address the following deficiencies: * all rfkill drivers need to implement polling where necessary rather than having one central implementation * updating the rfkill state cannot be done from arbitrary contexts, forcing drivers to use schedule_work and requiring lots of code * rfkill drivers need to keep track of soft/hard blocked internally -- the core should do this * the rfkill API has many unexpected quirks, for example being asymmetric wrt. alloc/free and register/unregister * rfkill can call back into a driver from within a function the driver called -- this is prone to deadlocks and generally should be avoided * rfkill-input pointlessly is a separate module * drivers need to #ifdef rfkill functions (unless they want to depend on or select RFKILL) -- rfkill should provide inlines that do nothing if it isn't compiled in * the rfkill structure is not opaque -- drivers need to initialise it correctly (lots of sanity checking code required) -- instead force drivers to pass the right variables to rfkill_alloc() * the documentation is hard to read because it always assumes the reader is completely clueless and contains way TOO MANY CAPS * the rfkill code needlessly uses a lot of locks and atomic operations in locked sections * fix LED trigger to actually change the LED when the radio state changes -- this wasn't done before Tested-by: Alan Jenkins <alan-jenkins@tuffmail.co.uk> Signed-off-by: Henrique de Moraes Holschuh <hmh@hmh.eng.br> [thinkpad] Signed-off-by: Johannes Berg <johannes@sipsolutions.net> Signed-off-by: John W. Linville <linville@tuxdriver.com>
1 parent 0f6399c commit 19d337d

46 files changed

Lines changed: 2536 additions & 3273 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Documentation/rfkill.txt‎

Lines changed: 78 additions & 519 deletions
Large diffs are not rendered by default.

‎MAINTAINERS‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4753,9 +4753,9 @@ S: Supported
47534753
F: fs/reiserfs/
47544754

47554755
RFKILL
4756-
P: Ivo van Doorn
4757-
M: IvDoorn@gmail.com
4758-
L: netdev@vger.kernel.org
4756+
P: Johannes Berg
4757+
M: johannes@sipsolutions.net
4758+
L: linux-wireless@vger.kernel.org
47594759
S: Maintained
47604760
F Documentation/rfkill.txt
47614761
F: net/rfkill/

‎arch/arm/mach-pxa/tosa-bt.c‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,25 @@ static void tosa_bt_off(struct tosa_bt_data *data)
3535
gpio_set_value(data->gpio_reset, 0);
3636
}
3737

38-
static int tosa_bt_toggle_radio(void *data, enum rfkill_state state)
38+
static int tosa_bt_set_block(void *data, bool blocked)
3939
{
40-
pr_info("BT_RADIO going: %s\n",
41-
state == RFKILL_STATE_UNBLOCKED ? "on" : "off");
40+
pr_info("BT_RADIO going: %s\n", blocked ? "off" : "on");
4241

43-
if (state == RFKILL_STATE_UNBLOCKED) {
42+
if (!blocked) {
4443
pr_info("TOSA_BT: going ON\n");
4544
tosa_bt_on(data);
4645
} else {
4746
pr_info("TOSA_BT: going OFF\n");
4847
tosa_bt_off(data);
4948
}
49+
5050
return 0;
5151
}
5252

53+
static const struct rfkill_ops tosa_bt_rfkill_ops = {
54+
.set_block = tosa_bt_set_block,
55+
};
56+
5357
static int tosa_bt_probe(struct platform_device *dev)
5458
{
5559
int rc;
@@ -70,18 +74,14 @@ static int tosa_bt_probe(struct platform_device *dev)
7074
if (rc)
7175
goto err_pwr_dir;
7276

73-
rfk = rfkill_allocate(&dev->dev, RFKILL_TYPE_BLUETOOTH);
77+
rfk = rfkill_alloc("tosa-bt", &dev->dev, RFKILL_TYPE_BLUETOOTH,
78+
&tosa_bt_rfkill_ops, data);
7479
if (!rfk) {
7580
rc = -ENOMEM;
7681
goto err_rfk_alloc;
7782
}
7883

79-
rfk->name = "tosa-bt";
80-
rfk->toggle_radio = tosa_bt_toggle_radio;
81-
rfk->data = data;
82-
#ifdef CONFIG_RFKILL_LEDS
83-
rfk->led_trigger.name = "tosa-bt";
84-
#endif
84+
rfkill_set_led_trigger_name(rfk, "tosa-bt");
8585

8686
rc = rfkill_register(rfk);
8787
if (rc)
@@ -92,9 +92,7 @@ static int tosa_bt_probe(struct platform_device *dev)
9292
return 0;
9393

9494
err_rfkill:
95-
if (rfk)
96-
rfkill_free(rfk);
97-
rfk = NULL;
95+
rfkill_destroy(rfk);
9896
err_rfk_alloc:
9997
tosa_bt_off(data);
10098
err_pwr_dir:
@@ -113,8 +111,10 @@ static int __devexit tosa_bt_remove(struct platform_device *dev)
113111

114112
platform_set_drvdata(dev, NULL);
115113

116-
if (rfk)
114+
if (rfk) {
117115
rfkill_unregister(rfk);
116+
rfkill_destroy(rfk);
117+
}
118118
rfk = NULL;
119119

120120
tosa_bt_off(data);

‎arch/arm/mach-pxa/tosa.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <linux/input.h>
3232
#include <linux/gpio.h>
3333
#include <linux/pda_power.h>
34-
#include <linux/rfkill.h>
3534
#include <linux/spi/spi.h>
3635

3736
#include <asm/setup.h>

‎drivers/net/usb/hso.c‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,10 +2481,10 @@ static int add_net_device(struct hso_device *hso_dev)
24812481
return 0;
24822482
}
24832483

2484-
static int hso_radio_toggle(void *data, enum rfkill_state state)
2484+
static int hso_rfkill_set_block(void *data, bool blocked)
24852485
{
24862486
struct hso_device *hso_dev = data;
2487-
int enabled = (state == RFKILL_STATE_UNBLOCKED);
2487+
int enabled = !blocked;
24882488
int rv;
24892489

24902490
mutex_lock(&hso_dev->mutex);
@@ -2498,6 +2498,10 @@ static int hso_radio_toggle(void *data, enum rfkill_state state)
24982498
return rv;
24992499
}
25002500

2501+
static const struct rfkill_ops hso_rfkill_ops = {
2502+
.set_block = hso_rfkill_set_block,
2503+
};
2504+
25012505
/* Creates and sets up everything for rfkill */
25022506
static void hso_create_rfkill(struct hso_device *hso_dev,
25032507
struct usb_interface *interface)
@@ -2506,29 +2510,25 @@ static void hso_create_rfkill(struct hso_device *hso_dev,
25062510
struct device *dev = &hso_net->net->dev;
25072511
char *rfkn;
25082512

2509-
hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
2510-
RFKILL_TYPE_WWAN);
2511-
if (!hso_net->rfkill) {
2512-
dev_err(dev, "%s - Out of memory\n", __func__);
2513-
return;
2514-
}
25152513
rfkn = kzalloc(20, GFP_KERNEL);
2516-
if (!rfkn) {
2517-
rfkill_free(hso_net->rfkill);
2518-
hso_net->rfkill = NULL;
2514+
if (!rfkn)
25192515
dev_err(dev, "%s - Out of memory\n", __func__);
2520-
return;
2521-
}
2516+
25222517
snprintf(rfkn, 20, "hso-%d",
25232518
interface->altsetting->desc.bInterfaceNumber);
2524-
hso_net->rfkill->name = rfkn;
2525-
hso_net->rfkill->state = RFKILL_STATE_UNBLOCKED;
2526-
hso_net->rfkill->data = hso_dev;
2527-
hso_net->rfkill->toggle_radio = hso_radio_toggle;
2519+
2520+
hso_net->rfkill = rfkill_alloc(rfkn,
2521+
&interface_to_usbdev(interface)->dev,
2522+
RFKILL_TYPE_WWAN,
2523+
&hso_rfkill_ops, hso_dev);
2524+
if (!hso_net->rfkill) {
2525+
dev_err(dev, "%s - Out of memory\n", __func__);
2526+
kfree(rfkn);
2527+
return;
2528+
}
25282529
if (rfkill_register(hso_net->rfkill) < 0) {
2530+
rfkill_destroy(hso_net->rfkill);
25292531
kfree(rfkn);
2530-
hso_net->rfkill->name = NULL;
2531-
rfkill_free(hso_net->rfkill);
25322532
hso_net->rfkill = NULL;
25332533
dev_err(dev, "%s - Failed to register rfkill\n", __func__);
25342534
return;
@@ -3165,8 +3165,10 @@ static void hso_free_interface(struct usb_interface *interface)
31653165
hso_stop_net_device(network_table[i]);
31663166
cancel_work_sync(&network_table[i]->async_put_intf);
31673167
cancel_work_sync(&network_table[i]->async_get_intf);
3168-
if (rfk)
3168+
if (rfk) {
31693169
rfkill_unregister(rfk);
3170+
rfkill_destroy(rfk);
3171+
}
31703172
hso_free_net_device(network_table[i]);
31713173
}
31723174
}

‎drivers/net/wireless/ath/ath9k/ath9k.h‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,12 +460,9 @@ struct ath_led {
460460
bool registered;
461461
};
462462

463-
/* Rfkill */
464-
#define ATH_RFKILL_POLL_INTERVAL 2000 /* msecs */
465-
466463
struct ath_rfkill {
467464
struct rfkill *rfkill;
468-
struct delayed_work rfkill_poll;
465+
struct rfkill_ops ops;
469466
char rfkill_name[32];
470467
};
471468

@@ -509,8 +506,6 @@ struct ath_rfkill {
509506
#define SC_OP_RXFLUSH BIT(7)
510507
#define SC_OP_LED_ASSOCIATED BIT(8)
511508
#define SC_OP_RFKILL_REGISTERED BIT(9)
512-
#define SC_OP_RFKILL_SW_BLOCKED BIT(10)
513-
#define SC_OP_RFKILL_HW_BLOCKED BIT(11)
514509
#define SC_OP_WAIT_FOR_BEACON BIT(12)
515510
#define SC_OP_LED_ON BIT(13)
516511
#define SC_OP_SCANNING BIT(14)

‎drivers/net/wireless/ath/ath9k/main.c‎

Lines changed: 29 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,120 +1192,69 @@ static bool ath_is_rfkill_set(struct ath_softc *sc)
11921192
ah->rfkill_polarity;
11931193
}
11941194

1195-
/* h/w rfkill poll function */
1196-
static void ath_rfkill_poll(struct work_struct *work)
1195+
/* s/w rfkill handlers */
1196+
static int ath_rfkill_set_block(void *data, bool blocked)
11971197
{
1198-
struct ath_softc *sc = container_of(work, struct ath_softc,
1199-
rf_kill.rfkill_poll.work);
1200-
bool radio_on;
1201-
1202-
if (sc->sc_flags & SC_OP_INVALID)
1203-
return;
1204-
1205-
radio_on = !ath_is_rfkill_set(sc);
1206-
1207-
/*
1208-
* enable/disable radio only when there is a
1209-
* state change in RF switch
1210-
*/
1211-
if (radio_on == !!(sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED)) {
1212-
enum rfkill_state state;
1213-
1214-
if (sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED) {
1215-
state = radio_on ? RFKILL_STATE_SOFT_BLOCKED
1216-
: RFKILL_STATE_HARD_BLOCKED;
1217-
} else if (radio_on) {
1218-
ath_radio_enable(sc);
1219-
state = RFKILL_STATE_UNBLOCKED;
1220-
} else {
1221-
ath_radio_disable(sc);
1222-
state = RFKILL_STATE_HARD_BLOCKED;
1223-
}
1224-
1225-
if (state == RFKILL_STATE_HARD_BLOCKED)
1226-
sc->sc_flags |= SC_OP_RFKILL_HW_BLOCKED;
1227-
else
1228-
sc->sc_flags &= ~SC_OP_RFKILL_HW_BLOCKED;
1198+
struct ath_softc *sc = data;
12291199

1230-
rfkill_force_state(sc->rf_kill.rfkill, state);
1231-
}
1200+
if (blocked)
1201+
ath_radio_disable(sc);
1202+
else
1203+
ath_radio_enable(sc);
12321204

1233-
queue_delayed_work(sc->hw->workqueue, &sc->rf_kill.rfkill_poll,
1234-
msecs_to_jiffies(ATH_RFKILL_POLL_INTERVAL));
1205+
return 0;
12351206
}
12361207

1237-
/* s/w rfkill handler */
1238-
static int ath_sw_toggle_radio(void *data, enum rfkill_state state)
1208+
static void ath_rfkill_poll_state(struct rfkill *rfkill, void *data)
12391209
{
12401210
struct ath_softc *sc = data;
1211+
bool blocked = !!ath_is_rfkill_set(sc);
12411212

1242-
switch (state) {
1243-
case RFKILL_STATE_SOFT_BLOCKED:
1244-
if (!(sc->sc_flags & (SC_OP_RFKILL_HW_BLOCKED |
1245-
SC_OP_RFKILL_SW_BLOCKED)))
1246-
ath_radio_disable(sc);
1247-
sc->sc_flags |= SC_OP_RFKILL_SW_BLOCKED;
1248-
return 0;
1249-
case RFKILL_STATE_UNBLOCKED:
1250-
if ((sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED)) {
1251-
sc->sc_flags &= ~SC_OP_RFKILL_SW_BLOCKED;
1252-
if (sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED) {
1253-
DPRINTF(sc, ATH_DBG_FATAL, "Can't turn on the"
1254-
"radio as it is disabled by h/w\n");
1255-
return -EPERM;
1256-
}
1257-
ath_radio_enable(sc);
1258-
}
1259-
return 0;
1260-
default:
1261-
return -EINVAL;
1262-
}
1213+
if (rfkill_set_hw_state(rfkill, blocked))
1214+
ath_radio_disable(sc);
1215+
else
1216+
ath_radio_enable(sc);
12631217
}
12641218

12651219
/* Init s/w rfkill */
12661220
static int ath_init_sw_rfkill(struct ath_softc *sc)
12671221
{
1268-
sc->rf_kill.rfkill = rfkill_allocate(wiphy_dev(sc->hw->wiphy),
1269-
RFKILL_TYPE_WLAN);
1222+
sc->rf_kill.ops.set_block = ath_rfkill_set_block;
1223+
if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1224+
sc->rf_kill.ops.poll = ath_rfkill_poll_state;
1225+
1226+
snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
1227+
"ath9k-%s::rfkill", wiphy_name(sc->hw->wiphy));
1228+
1229+
sc->rf_kill.rfkill = rfkill_alloc(sc->rf_kill.rfkill_name,
1230+
wiphy_dev(sc->hw->wiphy),
1231+
RFKILL_TYPE_WLAN,
1232+
&sc->rf_kill.ops, sc);
12701233
if (!sc->rf_kill.rfkill) {
12711234
DPRINTF(sc, ATH_DBG_FATAL, "Failed to allocate rfkill\n");
12721235
return -ENOMEM;
12731236
}
12741237

1275-
snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
1276-
"ath9k-%s::rfkill", wiphy_name(sc->hw->wiphy));
1277-
sc->rf_kill.rfkill->name = sc->rf_kill.rfkill_name;
1278-
sc->rf_kill.rfkill->data = sc;
1279-
sc->rf_kill.rfkill->toggle_radio = ath_sw_toggle_radio;
1280-
sc->rf_kill.rfkill->state = RFKILL_STATE_UNBLOCKED;
1281-
12821238
return 0;
12831239
}
12841240

12851241
/* Deinitialize rfkill */
12861242
static void ath_deinit_rfkill(struct ath_softc *sc)
12871243
{
1288-
if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1289-
cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
1290-
12911244
if (sc->sc_flags & SC_OP_RFKILL_REGISTERED) {
12921245
rfkill_unregister(sc->rf_kill.rfkill);
1246+
rfkill_destroy(sc->rf_kill.rfkill);
12931247
sc->sc_flags &= ~SC_OP_RFKILL_REGISTERED;
1294-
sc->rf_kill.rfkill = NULL;
12951248
}
12961249
}
12971250

12981251
static int ath_start_rfkill_poll(struct ath_softc *sc)
12991252
{
1300-
if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1301-
queue_delayed_work(sc->hw->workqueue,
1302-
&sc->rf_kill.rfkill_poll, 0);
1303-
13041253
if (!(sc->sc_flags & SC_OP_RFKILL_REGISTERED)) {
13051254
if (rfkill_register(sc->rf_kill.rfkill)) {
13061255
DPRINTF(sc, ATH_DBG_FATAL,
13071256
"Unable to register rfkill\n");
1308-
rfkill_free(sc->rf_kill.rfkill);
1257+
rfkill_destroy(sc->rf_kill.rfkill);
13091258

13101259
/* Deinitialize the device */
13111260
ath_cleanup(sc);
@@ -1678,10 +1627,6 @@ int ath_attach(u16 devid, struct ath_softc *sc)
16781627
goto error_attach;
16791628

16801629
#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
1681-
/* Initialze h/w Rfkill */
1682-
if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1683-
INIT_DELAYED_WORK(&sc->rf_kill.rfkill_poll, ath_rfkill_poll);
1684-
16851630
/* Initialize s/w rfkill */
16861631
error = ath_init_sw_rfkill(sc);
16871632
if (error)
@@ -2214,10 +2159,8 @@ static void ath9k_stop(struct ieee80211_hw *hw)
22142159
} else
22152160
sc->rx.rxlink = NULL;
22162161

2217-
#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
2218-
if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
2219-
cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
2220-
#endif
2162+
rfkill_pause_polling(sc->rf_kill.rfkill);
2163+
22212164
/* disable HAL and put h/w to sleep */
22222165
ath9k_hw_disable(sc->sc_ah);
22232166
ath9k_hw_configpcipowersave(sc->sc_ah, 1);

‎drivers/net/wireless/ath/ath9k/pci.c‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,6 @@ static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
227227

228228
ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
229229

230-
#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
231-
if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
232-
cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
233-
#endif
234-
235230
pci_save_state(pdev);
236231
pci_disable_device(pdev);
237232
pci_set_power_state(pdev, PCI_D3hot);
@@ -256,16 +251,6 @@ static int ath_pci_resume(struct pci_dev *pdev)
256251
AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
257252
ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
258253

259-
#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
260-
/*
261-
* check the h/w rfkill state on resume
262-
* and start the rfkill poll timer
263-
*/
264-
if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
265-
queue_delayed_work(sc->hw->workqueue,
266-
&sc->rf_kill.rfkill_poll, 0);
267-
#endif
268-
269254
return 0;
270255
}
271256

0 commit comments

Comments
 (0)