OSDN Git Service

eeepc-laptop: rfkill refactoring
authorCorentin Chary <corentincj@iksaif.net>
Sun, 27 Sep 2009 19:02:09 +0000 (21:02 +0200)
committerCorentin Chary <corentincj@iksaif.net>
Wed, 30 Sep 2009 20:17:44 +0000 (22:17 +0200)
Refactor rfkill code, because we'll add another
rfkill for wwan3g later.

Signed-off-by: Corentin Chary <corentincj@iksaif.net>
Signed-off-by: Len Brown <len.brown@intel.com>
drivers/platform/x86/eeepc-laptop.c

index fc79127..653c4bd 100644 (file)
@@ -139,8 +139,8 @@ struct eeepc_hotk {
        u16 event_count[128];           /* count for each event */
        struct input_dev *inputdev;
        u16 *keycode_map;
-       struct rfkill *eeepc_wlan_rfkill;
-       struct rfkill *eeepc_bluetooth_rfkill;
+       struct rfkill *wlan_rfkill;
+       struct rfkill *bluetooth_rfkill;
        struct hotplug_slot *hotplug_slot;
 };
 
@@ -673,7 +673,7 @@ static void eeepc_rfkill_hotplug(void)
                }
        }
 
-       rfkill_force_state(ehotk->eeepc_wlan_rfkill, !blocked);
+       rfkill_force_state(ehotk->wlan_rfkill, !blocked);
 }
 
 static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
@@ -768,29 +768,6 @@ static void eeepc_unregister_rfkill_notifier(char *node)
        }
 }
 
-static struct rfkill *eeepc_rfkill_alloc(const char *name,
-                                        struct device *dev,
-                                        enum rfkill_type type, int asl)
-{
-       struct rfkill *rfkill;
-
-       rfkill = rfkill_allocate(dev, type);
-       if (!rfkill)
-               return NULL;
-
-       rfkill->name = name;
-       rfkill->toggle_radio = eeepc_rfkill_set;
-       rfkill->get_state = eeepc_rfkill_state;
-       if (get_acpi(asl) == 1) {
-               rfkill->state = RFKILL_STATE_UNBLOCKED;
-               rfkill_set_default(type, RFKILL_STATE_UNBLOCKED);
-       } else {
-               rfkill->state = RFKILL_STATE_SOFT_BLOCKED;
-               rfkill_set_default(type, RFKILL_STATE_SOFT_BLOCKED);
-       }
-       return rfkill;
-}
-
 static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
 {
        kfree(hotplug_slot->info);
@@ -864,65 +841,8 @@ static int eeepc_hotk_add(struct acpi_device *device)
        if (ACPI_FAILURE(status))
                pr_err("Error installing notify handler\n");
 
-       eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
-       eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
-
-       if (get_acpi(CM_ASL_WLAN) != -1) {
-               ehotk->eeepc_wlan_rfkill =
-                       eeepc_rfkill_alloc("eeepc-wlan",
-                                          &device->dev,
-                                          RFKILL_TYPE_WLAN,
-                                          CM_ASL_WLAN);
-
-               if (!ehotk->eeepc_wlan_rfkill)
-                       goto wlan_fail;
-
-               result = rfkill_register(ehotk->eeepc_wlan_rfkill);
-               if (result)
-                       goto wlan_fail;
-       }
-
-       if (get_acpi(CM_ASL_BLUETOOTH) != -1) {
-               ehotk->eeepc_wlan_rfkill =
-                       eeepc_rfkill_alloc("eeepc-bluetooth",
-                                          &device->dev,
-                                          RFKILL_TYPE_BLUETOOTH,
-                                          CM_ASL_BLUETOOTH);
-
-               if (!ehotk->eeepc_bluetooth_rfkill)
-                       goto bluetooth_fail;
-
-               result = rfkill_register(ehotk->eeepc_bluetooth_rfkill);
-               if (result)
-                       goto bluetooth_fail;
-       }
-
-       result = eeepc_setup_pci_hotplug();
-       /*
-        * If we get -EBUSY then something else is handling the PCI hotplug -
-        * don't fail in this case
-        */
-       if (result == -EBUSY)
-               return 0;
-       else if (result)
-               goto pci_fail;
-
        return 0;
 
- pci_fail:
-       if (ehotk->eeepc_bluetooth_rfkill)
-               rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
-       ehotk->eeepc_bluetooth_rfkill = NULL;
- bluetooth_fail:
-       if (ehotk->eeepc_bluetooth_rfkill)
-               rfkill_free(ehotk->eeepc_bluetooth_rfkill);
-       rfkill_unregister(ehotk->eeepc_wlan_rfkill);
-       ehotk->eeepc_wlan_rfkill = NULL;
- wlan_fail:
-       if (ehotk->eeepc_wlan_rfkill)
-               rfkill_free(ehotk->eeepc_wlan_rfkill);
-       eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
-       eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
  ehotk_fail:
        kfree(ehotk);
        ehotk = NULL;
@@ -941,18 +861,13 @@ static int eeepc_hotk_remove(struct acpi_device *device, int type)
        if (ACPI_FAILURE(status))
                pr_err("Error removing notify handler\n");
 
-       eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
-       eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
-       if (ehotk->hotplug_slot)
-               pci_hp_deregister(ehotk->hotplug_slot);
-
        kfree(ehotk);
        return 0;
 }
 
 static int eeepc_hotk_resume(struct acpi_device *device)
 {
-       if (ehotk->eeepc_wlan_rfkill) {
+       if (ehotk->wlan_rfkill) {
                bool wlan;
 
                /* Workaround - it seems that _PTS disables the wireless
@@ -964,14 +879,14 @@ static int eeepc_hotk_resume(struct acpi_device *device)
                wlan = get_acpi(CM_ASL_WLAN);
                set_acpi(CM_ASL_WLAN, wlan);
 
-               rfkill_force_state(ehotk->eeepc_wlan_rfkill,
+               rfkill_force_state(ehotk->wlan_rfkill,
                                   wlan != 1);
 
                eeepc_rfkill_hotplug();
        }
 
-       if (ehotk->eeepc_bluetooth_rfkill)
-               rfkill_force_state(ehotk->eeepc_bluetooth_rfkill,
+       if (ehotk->bluetooth_rfkill)
+               rfkill_force_state(ehotk->bluetooth_rfkill,
                                   get_acpi(CM_ASL_BLUETOOTH) != 1);
 
        return 0;
@@ -1093,10 +1008,14 @@ static void eeepc_backlight_exit(void)
 
 static void eeepc_rfkill_exit(void)
 {
-       if (ehotk->eeepc_wlan_rfkill)
-               rfkill_unregister(ehotk->eeepc_wlan_rfkill);
-       if (ehotk->eeepc_bluetooth_rfkill)
-               rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
+       eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
+       eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
+       if (ehotk->wlan_rfkill)
+               rfkill_unregister(ehotk->wlan_rfkill);
+       if (ehotk->bluetooth_rfkill)
+               rfkill_unregister(ehotk->bluetooth_rfkill);
+       if (ehotk->hotplug_slot)
+               pci_hp_deregister(ehotk->hotplug_slot);
 }
 
 static void eeepc_input_exit(void)
@@ -1131,6 +1050,88 @@ static void __exit eeepc_laptop_exit(void)
        platform_driver_unregister(&platform_driver);
 }
 
+static struct rfkill *eeepc_rfkill_alloc(const char *name,
+                                        struct device *dev,
+                                        enum rfkill_type type, int asl)
+{
+       struct rfkill *rfkill;
+
+       rfkill = rfkill_allocate(dev, type);
+       if (!rfkill)
+               return NULL;
+
+       rfkill->name = name;
+       rfkill->toggle_radio = eeepc_rfkill_set;
+       rfkill->get_state = eeepc_rfkill_state;
+       if (get_acpi(asl) == 1) {
+               rfkill->state = RFKILL_STATE_UNBLOCKED;
+               rfkill_set_default(type, RFKILL_STATE_UNBLOCKED);
+       } else {
+               rfkill->state = RFKILL_STATE_SOFT_BLOCKED;
+               rfkill_set_default(type, RFKILL_STATE_SOFT_BLOCKED);
+       }
+       return rfkill;
+}
+
+static int eeepc_new_rfkill(struct rfkill **rfkill,
+                           const char *name, struct device *dev,
+                           enum rfkill_type type, int cm)
+{
+       int result;
+
+       if (get_acpi(cm) == -1)
+               return -ENODEV;
+
+       *rfkill = eeepc_rfkill_alloc(name, dev, type, cm);
+
+       if (!*rfkill)
+               return -EINVAL;
+
+       result = rfkill_register(*rfkill);
+       if (result) {
+               rfkill_free(*rfkill);
+               *rfkill = NULL;
+               return result;
+       }
+       return 0;
+}
+
+
+static int eeepc_rfkill_init(struct device *dev)
+{
+       int result = 0;
+
+       eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
+       eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
+
+       result = eeepc_new_rfkill(&ehotk->wlan_rfkill,
+                                 "eeepc-wlan", dev,
+                                 RFKILL_TYPE_WLAN, CM_ASL_WLAN);
+
+       if (result && result != -ENODEV)
+               goto exit;
+
+       result = eeepc_new_rfkill(&ehotk->bluetooth_rfkill,
+                                 "eeepc-bluetooth", dev,
+                                 RFKILL_TYPE_BLUETOOTH, CM_ASL_BLUETOOTH);
+
+       if (result && result != -ENODEV)
+               goto exit;
+
+       result = eeepc_setup_pci_hotplug();
+       /*
+        * If we get -EBUSY then something else is handling the PCI hotplug -
+        * don't fail in this case
+        */
+       if (result == -EBUSY)
+               result = 0;
+
+exit:
+       if (result && result != -ENODEV)
+               eeepc_rfkill_exit();
+       return result;
+}
+
 static int eeepc_backlight_init(struct device *dev)
 {
        struct backlight_device *bd;
@@ -1214,7 +1215,15 @@ static int __init eeepc_laptop_init(void)
                                    &platform_attribute_group);
        if (result)
                goto fail_sysfs;
+
+       result = eeepc_rfkill_init(dev);
+       if (result)
+               goto fail_rfkill;
+
        return 0;
+fail_rfkill:
+       sysfs_remove_group(&platform_device->dev.kobj,
+                          &platform_attribute_group);
 fail_sysfs:
        platform_device_del(platform_device);
 fail_platform_device2:
@@ -1227,7 +1236,6 @@ fail_hwmon:
        eeepc_backlight_exit();
 fail_backlight:
        eeepc_input_exit();
-       eeepc_rfkill_exit();
        return result;
 }