Merge branch 'linux-next' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes...
[safe/jmp/linux-2.6] / drivers / acpi / glue.c
index fcb881d..0f2dd81 100644 (file)
@@ -16,7 +16,7 @@
 #if ACPI_GLUE_DEBUG
 #define DBG(x...) printk(PREFIX x)
 #else
-#define DBG(x...)
+#define DBG(x...) do { } while(0)
 #endif
 static LIST_HEAD(bus_type_list);
 static DECLARE_RWSEM(bus_type_sem);
@@ -36,8 +36,6 @@ int register_acpi_bus_type(struct acpi_bus_type *type)
        return -ENODEV;
 }
 
-EXPORT_SYMBOL(register_acpi_bus_type);
-
 int unregister_acpi_bus_type(struct acpi_bus_type *type)
 {
        if (acpi_disabled)
@@ -53,8 +51,6 @@ int unregister_acpi_bus_type(struct acpi_bus_type *type)
        return -ENODEV;
 }
 
-EXPORT_SYMBOL(unregister_acpi_bus_type);
-
 static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
 {
        struct acpi_bus_type *tmp, *ret = NULL;
@@ -86,125 +82,6 @@ static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
        return ret;
 }
 
-/* Get PCI root bridge's handle from its segment and bus number */
-struct acpi_find_pci_root {
-       unsigned int seg;
-       unsigned int bus;
-       acpi_handle handle;
-};
-
-static acpi_status
-do_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
-{
-       unsigned long *busnr = (unsigned long *)data;
-       struct acpi_resource_address64 address;
-
-       if (resource->type != ACPI_RSTYPE_ADDRESS16 &&
-           resource->type != ACPI_RSTYPE_ADDRESS32 &&
-           resource->type != ACPI_RSTYPE_ADDRESS64)
-               return AE_OK;
-
-       acpi_resource_to_address64(resource, &address);
-       if ((address.address_length > 0) &&
-           (address.resource_type == ACPI_BUS_NUMBER_RANGE))
-               *busnr = address.min_address_range;
-
-       return AE_OK;
-}
-
-static int get_root_bridge_busnr(acpi_handle handle)
-{
-       acpi_status status;
-       unsigned long bus, bbn;
-       struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
-
-       acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
-
-       status = acpi_evaluate_integer(handle, METHOD_NAME__BBN, NULL,
-                                      &bbn);
-       if (status == AE_NOT_FOUND) {
-               /* Assume bus = 0 */
-               printk(KERN_INFO PREFIX
-                      "Assume root bridge [%s] bus is 0\n",
-                      (char *)buffer.pointer);
-               status = AE_OK;
-               bbn = 0;
-       }
-       if (ACPI_FAILURE(status)) {
-               bbn = -ENODEV;
-               goto exit;
-       }
-       if (bbn > 0)
-               goto exit;
-
-       /* _BBN in some systems return 0 for all root bridges */
-       bus = -1;
-       status = acpi_walk_resources(handle, METHOD_NAME__CRS,
-                                    do_root_bridge_busnr_callback, &bus);
-       /* If _CRS failed, we just use _BBN */
-       if (ACPI_FAILURE(status) || (bus == -1))
-               goto exit;
-       /* We select _CRS */
-       if (bbn != bus) {
-               printk(KERN_INFO PREFIX
-                      "_BBN and _CRS returns different value for %s. Select _CRS\n",
-                      (char *)buffer.pointer);
-               bbn = bus;
-       }
-      exit:
-       acpi_os_free(buffer.pointer);
-       return (int)bbn;
-}
-
-static acpi_status
-find_pci_rootbridge(acpi_handle handle, u32 lvl, void *context, void **rv)
-{
-       struct acpi_find_pci_root *find = (struct acpi_find_pci_root *)context;
-       unsigned long seg, bus;
-       acpi_status status;
-       int tmp;
-       struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
-
-       acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
-
-       status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL, &seg);
-       if (status == AE_NOT_FOUND) {
-               /* Assume seg = 0 */
-               status = AE_OK;
-               seg = 0;
-       }
-       if (ACPI_FAILURE(status)) {
-               status = AE_CTRL_DEPTH;
-               goto exit;
-       }
-
-       tmp = get_root_bridge_busnr(handle);
-       if (tmp < 0) {
-               printk(KERN_ERR PREFIX
-                      "Find root bridge failed for %s\n",
-                      (char *)buffer.pointer);
-               status = AE_CTRL_DEPTH;
-               goto exit;
-       }
-       bus = tmp;
-
-       if (seg == find->seg && bus == find->bus)
-               find->handle = handle;
-       status = AE_OK;
-      exit:
-       acpi_os_free(buffer.pointer);
-       return status;
-}
-
-acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
-{
-       struct acpi_find_pci_root find = { seg, bus, NULL };
-
-       acpi_get_devices(PCI_ROOT_HID_STRING, find_pci_rootbridge, &find, NULL);
-       return find.handle;
-}
-EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
-
 /* Get device's handler per its address under its parent */
 struct acpi_find_child {
        acpi_handle handle;
@@ -217,14 +94,14 @@ do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
        acpi_status status;
        struct acpi_device_info *info;
        struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
-       struct acpi_find_child *find = (struct acpi_find_child *)context;
+       struct acpi_find_child *find = context;
 
        status = acpi_get_object_info(handle, &buffer);
        if (ACPI_SUCCESS(status)) {
                info = buffer.pointer;
                if (info->address == find->address)
                        find->handle = handle;
-               acpi_os_free(buffer.pointer);
+               kfree(buffer.pointer);
        }
        return AE_OK;
 }
@@ -265,11 +142,12 @@ EXPORT_SYMBOL(acpi_get_physical_device);
 
 static int acpi_bind_one(struct device *dev, acpi_handle handle)
 {
+       struct acpi_device *acpi_dev;
        acpi_status status;
 
-       if (dev->firmware_data) {
+       if (dev->archdata.acpi_handle) {
                printk(KERN_WARNING PREFIX
-                      "Drivers changed 'firmware_data' for %s\n", dev->bus_id);
+                      "Drivers changed 'acpi_handle' for %s\n", dev->bus_id);
                return -EINVAL;
        }
        get_device(dev);
@@ -278,25 +156,47 @@ static int acpi_bind_one(struct device *dev, acpi_handle handle)
                put_device(dev);
                return -EINVAL;
        }
-       dev->firmware_data = handle;
+       dev->archdata.acpi_handle = handle;
+
+       status = acpi_bus_get_device(handle, &acpi_dev);
+       if (!ACPI_FAILURE(status)) {
+               int ret;
+
+               ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
+                               "firmware_node");
+               ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
+                               "physical_node");
+               if (acpi_dev->wakeup.flags.valid)
+                       device_set_wakeup_capable(dev, true);
+       }
 
        return 0;
 }
 
 static int acpi_unbind_one(struct device *dev)
 {
-       if (!dev->firmware_data)
+       if (!dev->archdata.acpi_handle)
                return 0;
-       if (dev == acpi_get_physical_device(dev->firmware_data)) {
+       if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
+               struct acpi_device *acpi_dev;
+
                /* acpi_get_physical_device increase refcnt by one */
                put_device(dev);
-               acpi_detach_data(dev->firmware_data, acpi_glue_data_handler);
-               dev->firmware_data = NULL;
+
+               if (!acpi_bus_get_device(dev->archdata.acpi_handle,
+                                       &acpi_dev)) {
+                       sysfs_remove_link(&dev->kobj, "firmware_node");
+                       sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node");
+               }
+
+               acpi_detach_data(dev->archdata.acpi_handle,
+                                acpi_glue_data_handler);
+               dev->archdata.acpi_handle = NULL;
                /* acpi_bind_one increase refcnt by one */
                put_device(dev);
        } else {
                printk(KERN_ERR PREFIX
-                      "Oops, 'firmware_data' corrupt for %s\n", dev->bus_id);
+                      "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id);
        }
        return 0;
 }
@@ -328,9 +228,10 @@ static int acpi_platform_notify(struct device *dev)
        if (!ret) {
                struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 
-               acpi_get_name(dev->firmware_data, ACPI_FULL_PATHNAME, &buffer);
+               acpi_get_name(dev->archdata.acpi_handle,
+                             ACPI_FULL_PATHNAME, &buffer);
                DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
-               acpi_os_free(buffer.pointer);
+               kfree(buffer.pointer);
        } else
                DBG("Device %s -> No ACPI support\n", dev->bus_id);
 #endif
@@ -358,3 +259,119 @@ static int __init init_acpi_device_notify(void)
 }
 
 arch_initcall(init_acpi_device_notify);
+
+
+#if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
+
+#ifdef CONFIG_PM
+static u32 rtc_handler(void *context)
+{
+       acpi_clear_event(ACPI_EVENT_RTC);
+       acpi_disable_event(ACPI_EVENT_RTC, 0);
+       return ACPI_INTERRUPT_HANDLED;
+}
+
+static inline void rtc_wake_setup(void)
+{
+       acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
+       /*
+        * After the RTC handler is installed, the Fixed_RTC event should
+        * be disabled. Only when the RTC alarm is set will it be enabled.
+        */
+       acpi_clear_event(ACPI_EVENT_RTC);
+       acpi_disable_event(ACPI_EVENT_RTC, 0);
+}
+
+static void rtc_wake_on(struct device *dev)
+{
+       acpi_clear_event(ACPI_EVENT_RTC);
+       acpi_enable_event(ACPI_EVENT_RTC, 0);
+}
+
+static void rtc_wake_off(struct device *dev)
+{
+       acpi_disable_event(ACPI_EVENT_RTC, 0);
+}
+#else
+#define rtc_wake_setup()       do{}while(0)
+#define rtc_wake_on            NULL
+#define rtc_wake_off           NULL
+#endif
+
+/* Every ACPI platform has a mc146818 compatible "cmos rtc".  Here we find
+ * its device node and pass extra config data.  This helps its driver use
+ * capabilities that the now-obsolete mc146818 didn't have, and informs it
+ * that this board's RTC is wakeup-capable (per ACPI spec).
+ */
+#include <linux/mc146818rtc.h>
+
+static struct cmos_rtc_board_info rtc_info;
+
+
+/* PNP devices are registered in a subsys_initcall();
+ * ACPI specifies the PNP IDs to use.
+ */
+#include <linux/pnp.h>
+
+static int __init pnp_match(struct device *dev, void *data)
+{
+       static const char *ids[] = { "PNP0b00", "PNP0b01", "PNP0b02", };
+       struct pnp_dev *pnp = to_pnp_dev(dev);
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(ids); i++) {
+               if (compare_pnp_id(pnp->id, ids[i]) != 0)
+                       return 1;
+       }
+       return 0;
+}
+
+static struct device *__init get_rtc_dev(void)
+{
+       return bus_find_device(&pnp_bus_type, NULL, NULL, pnp_match);
+}
+
+static int __init acpi_rtc_init(void)
+{
+       struct device *dev = get_rtc_dev();
+
+       if (acpi_disabled)
+               return 0;
+
+       if (acpi_disabled)
+               return 0;
+
+       if (dev) {
+               rtc_wake_setup();
+               rtc_info.wake_on = rtc_wake_on;
+               rtc_info.wake_off = rtc_wake_off;
+
+               /* workaround bug in some ACPI tables */
+               if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
+                       DBG("bogus FADT month_alarm\n");
+                       acpi_gbl_FADT.month_alarm = 0;
+               }
+
+               rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
+               rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
+               rtc_info.rtc_century = acpi_gbl_FADT.century;
+
+               /* NOTE:  S4_RTC_WAKE is NOT currently useful to Linux */
+               if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
+                       printk(PREFIX "RTC can wake from S4\n");
+
+
+               dev->platform_data = &rtc_info;
+
+               /* RTC always wakes from S1/S2/S3, and often S4/STD */
+               device_init_wakeup(dev, 1);
+
+               put_device(dev);
+       } else
+               DBG("RTC unavailable?\n");
+       return 0;
+}
+/* do this between RTC subsys_initcall() and rtc_cmos driver_initcall() */
+fs_initcall(acpi_rtc_init);
+
+#endif