Bind physical devices with ACPI devices. The PCI and PNP bind code are ok in the patch, but IDE bus type is just an experiment. --- 2.6-root/drivers/acpi/Makefile | 2 2.6-root/drivers/acpi/acpi_platform_notify.c | 214 +++++++++++++++++++++++++++ 2.6-root/drivers/acpi/bus.c | 3 2.6-root/drivers/acpi/pci_root.c | 13 + 2.6-root/include/acpi/acpi_bus.h | 1 2.6-root/include/acpi/acpi_drivers.h | 1 6 files changed, 233 insertions(+), 1 deletion(-) diff -puN drivers/acpi/bus.c~acpi_platform_notify drivers/acpi/bus.c --- 2.6/drivers/acpi/bus.c~acpi_platform_notify 2004-11-09 16:27:12.041700736 +0800 +++ 2.6-root/drivers/acpi/bus.c 2004-11-09 16:27:12.052699064 +0800 @@ -763,6 +763,9 @@ static int __init acpi_init (void) } else disable_acpi(); + if (!result) + init_acpi_device_notify(); + return_VALUE(result); } diff -puN /dev/null drivers/acpi/acpi_platform_notify.c --- /dev/null 2004-02-24 05:02:56.000000000 +0800 +++ 2.6-root/drivers/acpi/acpi_platform_notify.c 2004-11-09 16:42:10.898053800 +0800 @@ -0,0 +1,214 @@ +/* + * Bind physical devices with ACPI devices + */ +#include +#include +#include +#include + +struct acpi_find_child { + acpi_handle handle; + acpi_integer address; +}; + +static acpi_status __devinit +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; + + status = acpi_get_object_info(handle, &buffer); + if (ACPI_SUCCESS(status)) { + info = buffer.pointer; + if (info->address == find->address) { + find->handle = handle; + return AE_CTRL_TERMINATE; + } + } + return AE_OK; +} + +/* Move it to ACPICA? */ +static acpi_handle __devinit +acpi_get_child(acpi_handle parent, acpi_integer address) +{ + struct acpi_find_child find = {NULL, address}; + + if (!parent) + return NULL; + acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, + 1, find_child, + &find, NULL); + return find.handle; +} + +/*---------------- PCI BUS ------------------------*/ +static int __devinit is_pci_root_bridge(struct device *dev, + unsigned int *segment, unsigned int *bus) +{ + int num; + num = sscanf(dev->bus_id, "pci%04x:%02x", segment, bus); + if (num == 2) + return 1; + return 0; +} + +static int __devinit acpi_bind_pci_root_bridge(struct device *dev, + unsigned int seg, unsigned int bus) +{ + dev->platform_data = acpi_get_rootbridge_handle(seg, bus); + return 0; +} + +static int __devinit acpi_bind_pci_bus(struct device *dev) +{ + struct pci_dev * pci_dev; + acpi_handle parent_handle = NULL; + acpi_integer address; + + pci_dev = to_pci_dev(dev); + if (dev->parent) + parent_handle = dev->parent->platform_data; + + if (!parent_handle) { + printk("Can't find parent handle \n"); + return -EINVAL; + } + + /* Please ref to ACPI spec for syntax of _ADR */ + address = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn); + dev->platform_data = acpi_get_child(parent_handle, address); + return 0; +} + +/*----------------- IDE BUS --------------------------*/ +/* This possibly is completly wrong, IDE guru please correct it */ +static int __devinit is_ide_channel(struct device *dev, + unsigned int *channel) +{ + int num; + num = sscanf(dev->bus_id, "ide%d", channel); + if (num == 1) + return 1; + return 0; +} + +static int __devinit acpi_bind_ide_channel(struct device *dev, unsigned int channel) +{ + acpi_handle parent_handle = NULL; + acpi_integer address; + + /* Seems dev->parent is the PCI IDE controller */ + if (dev->parent) + parent_handle = dev->parent->platform_data; + + if (!parent_handle) { + printk("Can't find parent handle \n"); + return -EINVAL; + } + + /* Please ref to ACPI spec for syntax of _ADR */ + address = channel; + dev->platform_data = acpi_get_child(parent_handle, address); + return 0; +} + +static int __devinit acpi_bind_ide_bus(struct device *dev) +{ + acpi_handle parent_handle = NULL; + acpi_integer address; + int i; + + /* Seems dev->parent is the IDE channel device */ + if (dev->parent) + parent_handle = dev->parent->platform_data; + + if (!parent_handle) { + printk("Can't find parent handle \n"); + return -1; + } + + /* Please ref to ACPI spec for syntax of _ADR */ + sscanf(dev->bus_id, "%d", &i); + address = i; + dev->platform_data = acpi_get_child(parent_handle, address); + return 0; +} + +/*--------------------- PNP Bus -----------------------*/ +static int __devinit acpi_bind_pnp_bus(struct device *dev) +{ + struct pnp_dev *pnp_dev; + + pnp_dev = to_pnp_dev(dev); + /* PNPACPI adds ACPI handle in pnp_dev->data */ + /*dev->platform_data = pnp_dev->data;*/ + return 0; +} + +/*----------------------------------------------------*/ +static int __devinit acpi_platform_notify (struct device *dev) +{ + int ret = -EINVAL; + + if (!dev->bus) { + unsigned int tmp1, tmp2; + + /* PCI root bridge device hasn't a bus type */ + if (is_pci_root_bridge(dev, &tmp1, &tmp2)) { + ret = acpi_bind_pci_root_bridge(dev, tmp1, tmp2); + goto end; + } + /* IDE channel device hasn't a bus type */ + if (is_ide_channel(dev, &tmp1)) { + ret = acpi_bind_ide_channel(dev, tmp1); + goto end; + } + goto end; + } + switch (dev->bus->type) { + case BUS_TYPE_PCI: + ret = acpi_bind_pci_bus(dev); + break; + case BUS_TYPE_IDE: + ret = acpi_bind_ide_bus(dev); + break; + case BUS_TYPE_PNP: + ret = acpi_bind_pnp_bus(dev); + break; + /* TBD: add more bus types */ + default: + printk("Please add an implementation for bus %s\n", dev->bus->name); + break; + } +end: +#if 1 + {/* For debug */ + char name[80] = {'?','\0'}; + struct acpi_buffer buffer = {sizeof(name), name}; + + printk("Device %s -> ", dev->bus_id); + if (dev->platform_data) { + acpi_get_name(dev->platform_data, ACPI_FULL_PATHNAME, &buffer); + printk("%s", name); + } + printk("\n"); + } +#endif + return ret; +} + +static int __devinitdata (*saved_platform_notify)(struct device *dev); + +void __init init_acpi_device_notify(void) +{ + saved_platform_notify = platform_notify; + platform_notify = acpi_platform_notify; +} + +void __exit exit_acpi_device_notify(void) +{ + platform_notify = saved_platform_notify; +} diff -puN include/acpi/acpi_drivers.h~acpi_platform_notify include/acpi/acpi_drivers.h --- 2.6/include/acpi/acpi_drivers.h~acpi_platform_notify 2004-11-09 16:27:12.043700432 +0800 +++ 2.6-root/include/acpi/acpi_drivers.h 2004-11-09 16:27:12.053698912 +0800 @@ -53,6 +53,7 @@ #define ACPI_PCI_COMPONENT 0x00400000 +acpi_handle acpi_get_rootbridge_handle(u16 segment, u16 bus); /* ACPI PCI Interrupt Link (pci_link.c) */ int acpi_irq_penalty_init (void); diff -puN drivers/acpi/pci_root.c~acpi_platform_notify drivers/acpi/pci_root.c --- 2.6/drivers/acpi/pci_root.c~acpi_platform_notify 2004-11-09 16:27:12.044700280 +0800 +++ 2.6-root/drivers/acpi/pci_root.c 2004-11-09 16:27:12.053698912 +0800 @@ -343,3 +343,16 @@ static int __init acpi_pci_root_init (vo subsys_initcall(acpi_pci_root_init); +acpi_handle acpi_get_rootbridge_handle(u16 segment, u16 bus) +{ + struct list_head *entry; + + list_for_each(entry, &acpi_pci_roots) { + struct acpi_pci_root *root; + + root = list_entry(entry, struct acpi_pci_root, node); + if ((root->id.segment == segment) && (root->id.bus == bus)) + return root->handle; + } + return NULL; +} diff -puN include/acpi/acpi_bus.h~acpi_platform_notify include/acpi/acpi_bus.h --- 2.6/include/acpi/acpi_bus.h~acpi_platform_notify 2004-11-09 16:27:12.046699976 +0800 +++ 2.6-root/include/acpi/acpi_bus.h 2004-11-09 16:27:12.053698912 +0800 @@ -325,6 +325,7 @@ int acpi_bus_unregister_driver (struct a int acpi_create_dir(struct acpi_device *); void acpi_remove_dir(struct acpi_device *); +void init_acpi_device_notify(void); #endif /*CONFIG_ACPI_BUS*/ #endif /*__ACPI_BUS_H__*/ diff -puN drivers/acpi/Makefile~acpi_platform_notify drivers/acpi/Makefile --- 2.6/drivers/acpi/Makefile~acpi_platform_notify 2004-11-09 16:27:12.047699824 +0800 +++ 2.6-root/drivers/acpi/Makefile 2004-11-09 16:27:12.053698912 +0800 @@ -32,7 +32,7 @@ obj-$(CONFIG_ACPI_INTERPRETER) += osl.o # ACPI Bus and Device Drivers # obj-$(CONFIG_ACPI_BUS) += sleep/ -obj-$(CONFIG_ACPI_BUS) += bus.o +obj-$(CONFIG_ACPI_BUS) += bus.o acpi_platform_notify.o obj-$(CONFIG_ACPI_AC) += ac.o obj-$(CONFIG_ACPI_BATTERY) += battery.o obj-$(CONFIG_ACPI_BUTTON) += button.o _