Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes...
[pandora-kernel.git] / drivers / net / wireless / b43legacy / rfkill.c
index db62926..b32bf6a 100644 (file)
@@ -1,6 +1,6 @@
 /*
 
-  Broadcom B43legacy wireless driver
+  Broadcom B43 wireless driver
   RFKILL support
 
   Copyright (c) 2007 Michael Buesch <mb@bu3sch.de>
 #include "radio.h"
 #include "b43legacy.h"
 
+#include <linux/kmod.h>
 
-static void b43legacy_notify_rfkill_press(struct work_struct *work)
+
+/* Returns TRUE, if the radio is enabled in hardware. */
+static bool b43legacy_is_hw_radio_enabled(struct b43legacy_wldev *dev)
 {
-       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;
+       if (dev->phy.rev >= 3) {
+               if (!(b43legacy_read32(dev, B43legacy_MMIO_RADIO_HWENABLED_HI)
+                     & B43legacy_MMIO_RADIO_HWENABLED_HI_MASK))
+                       return 1;
+       } else {
+               if (b43legacy_read16(dev, B43legacy_MMIO_RADIO_HWENABLED_LO)
+                   & B43legacy_MMIO_RADIO_HWENABLED_LO_MASK)
+                       return 1;
+       }
+       return 0;
+}
+
+/* The poll callback for the hardware button. */
+static void b43legacy_rfkill_poll(struct input_polled_dev *poll_dev)
+{
+       struct b43legacy_wldev *dev = poll_dev->private;
+       struct b43legacy_wl *wl = dev->wl;
+       bool enabled;
+       bool report_change = 0;
 
        mutex_lock(&wl->mutex);
-       dev = wl->current_dev;
-       if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
+       if (unlikely(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");
+       enabled = b43legacy_is_hw_radio_enabled(dev);
+       if (unlikely(enabled != dev->radio_hw_enable)) {
+               dev->radio_hw_enable = enabled;
+               report_change = 1;
+               b43legacyinfo(wl, "Radio hardware status changed to %s\n",
+                       enabled ? "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);
+       /* send the radio switch event to the system - note both a key press
+        * and a release are required */
+       if (unlikely(report_change)) {
+               input_report_key(poll_dev->input, KEY_WLAN, 1);
+               input_report_key(poll_dev->input, KEY_WLAN, 0);
        }
 }
 
-/* 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;
+       int err = -EBUSY;
+
+       if (!wl->rfkill.registered)
+               return 0;
 
        mutex_lock(&wl->mutex);
        if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
                goto out_unlock;
-
+       err = 0;
        switch (state) {
-       case RFKILL_STATE_ON:
+       case RFKILL_STATE_UNBLOCKED:
                if (!dev->radio_hw_enable) {
                        /* No luck. We can't toggle the hardware RF-kill
                         * button from software. */
@@ -94,10 +100,14 @@ static int b43legacy_rfkill_soft_toggle(void *data, enum rfkill_state state)
                if (!dev->phy.radio_on)
                        b43legacy_radio_turn_on(dev);
                break;
-       case RFKILL_STATE_OFF:
+       case RFKILL_STATE_SOFT_BLOCKED:
                if (dev->phy.radio_on)
                        b43legacy_radio_turn_off(dev, 0);
                break;
+       default:
+               b43legacywarn(wl, "Received unexpected rfkill state %d.\n",
+                             state);
+               break;
        }
 
 out_unlock:
@@ -108,11 +118,11 @@ out_unlock:
 
 char *b43legacy_rfkill_led_name(struct b43legacy_wldev *dev)
 {
-       struct b43legacy_wl *wl = dev->wl;
+       struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
 
-       if (!wl->rfkill.rfkill)
+       if (!rfk->registered)
                return NULL;
-       return rfkill_get_led_name(wl->rfkill.rfkill);
+       return rfkill_get_led_name(rfk->rfkill);
 }
 
 void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
@@ -121,38 +131,79 @@ void b43legacy_rfkill_init(struct b43legacy_wldev *dev)
        struct b43legacy_rfkill *rfk = &(wl->rfkill);
        int err;
 
-       snprintf(rfk->name, sizeof(rfk->name),
-                "b43legacy-%s", wiphy_name(wl->hw->wiphy));
+       rfk->registered = 0;
+
        rfk->rfkill = rfkill_allocate(dev->dev->dev, RFKILL_TYPE_WLAN);
        if (!rfk->rfkill)
-               goto error;
+               goto out_error;
+       snprintf(rfk->name, sizeof(rfk->name),
+                "b43legacy-%s", wiphy_name(wl->hw->wiphy));
        rfk->rfkill->name = rfk->name;
-       rfk->rfkill->state = RFKILL_STATE_ON;
+       rfk->rfkill->state = RFKILL_STATE_UNBLOCKED;
        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);
+       rfk->poll_dev = input_allocate_polled_device();
+       if (!rfk->poll_dev) {
+               rfkill_free(rfk->rfkill);
+               goto err_freed_rfk;
+       }
+
+       rfk->poll_dev->private = dev;
+       rfk->poll_dev->poll = b43legacy_rfkill_poll;
+       rfk->poll_dev->poll_interval = 1000; /* msecs */
+
+       rfk->poll_dev->input->name = rfk->name;
+       rfk->poll_dev->input->id.bustype = BUS_HOST;
+       rfk->poll_dev->input->id.vendor = dev->dev->bus->boardinfo.vendor;
+       rfk->poll_dev->input->evbit[0] = BIT(EV_KEY);
+       set_bit(KEY_WLAN, rfk->poll_dev->input->keybit);
 
        err = rfkill_register(rfk->rfkill);
        if (err)
-               goto error;
+               goto err_free_polldev;
+
+#ifdef CONFIG_RFKILL_INPUT_MODULE
+       /* B43legacy RF-kill isn't useful without the rfkill-input subsystem.
+        * Try to load the module. */
+       err = request_module("rfkill-input");
+       if (err)
+               b43legacywarn(wl, "Failed to load the rfkill-input module."
+                       "The built-in radio LED will not work.\n");
+#endif /* CONFIG_RFKILL_INPUT */
+
+       err = input_register_polled_device(rfk->poll_dev);
+       if (err)
+               goto err_unreg_rfk;
+
+       rfk->registered = 1;
 
        return;
-error:
-       b43legacywarn(dev->wl, "Failed to initialize the RF-kill button\n");
-       rfkill_free(rfk->rfkill);
+err_unreg_rfk:
+       rfkill_unregister(rfk->rfkill);
+err_free_polldev:
+       input_free_polled_device(rfk->poll_dev);
+       rfk->poll_dev = NULL;
+err_freed_rfk:
        rfk->rfkill = NULL;
+out_error:
+       rfk->registered = 0;
+       b43legacywarn(wl, "RF-kill button init failed\n");
 }
 
 void b43legacy_rfkill_exit(struct b43legacy_wldev *dev)
 {
        struct b43legacy_rfkill *rfk = &(dev->wl->rfkill);
 
-       if (!rfk->rfkill)
+       if (!rfk->registered)
                return;
-       cancel_work_sync(&rfk->notify_work);
+       rfk->registered = 0;
+
+       input_unregister_polled_device(rfk->poll_dev);
        rfkill_unregister(rfk->rfkill);
-       rfkill_free(rfk->rfkill);
+       input_free_polled_device(rfk->poll_dev);
+       rfk->poll_dev = NULL;
        rfk->rfkill = NULL;
 }
+