wrong comment in connect_port (thank you Sohail Qayum Malik)
[cor_2_6_31.git] / drivers / acpi / glue.c
bloba8a5c29958c81faaea6b007626e40ee225ec0275
1 /*
2 * Link physical devices with ACPI devices support
4 * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
5 * Copyright (c) 2005 Intel Corp.
7 * This file is released under the GPLv2.
8 */
9 #include <linux/init.h>
10 #include <linux/list.h>
11 #include <linux/device.h>
12 #include <linux/rwsem.h>
13 #include <linux/acpi.h>
15 #define ACPI_GLUE_DEBUG 0
16 #if ACPI_GLUE_DEBUG
17 #define DBG(x...) printk(PREFIX x)
18 #else
19 #define DBG(x...) do { } while(0)
20 #endif
21 static LIST_HEAD(bus_type_list);
22 static DECLARE_RWSEM(bus_type_sem);
24 int register_acpi_bus_type(struct acpi_bus_type *type)
26 if (acpi_disabled)
27 return -ENODEV;
28 if (type && type->bus && type->find_device) {
29 down_write(&bus_type_sem);
30 list_add_tail(&type->list, &bus_type_list);
31 up_write(&bus_type_sem);
32 printk(KERN_INFO PREFIX "bus type %s registered\n",
33 type->bus->name);
34 return 0;
36 return -ENODEV;
39 int unregister_acpi_bus_type(struct acpi_bus_type *type)
41 if (acpi_disabled)
42 return 0;
43 if (type) {
44 down_write(&bus_type_sem);
45 list_del_init(&type->list);
46 up_write(&bus_type_sem);
47 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
48 type->bus->name);
49 return 0;
51 return -ENODEV;
54 static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
56 struct acpi_bus_type *tmp, *ret = NULL;
58 down_read(&bus_type_sem);
59 list_for_each_entry(tmp, &bus_type_list, list) {
60 if (tmp->bus == type) {
61 ret = tmp;
62 break;
65 up_read(&bus_type_sem);
66 return ret;
69 static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
71 struct acpi_bus_type *tmp;
72 int ret = -ENODEV;
74 down_read(&bus_type_sem);
75 list_for_each_entry(tmp, &bus_type_list, list) {
76 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
77 ret = 0;
78 break;
81 up_read(&bus_type_sem);
82 return ret;
85 /* Get device's handler per its address under its parent */
86 struct acpi_find_child {
87 acpi_handle handle;
88 acpi_integer address;
91 static acpi_status
92 do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
94 acpi_status status;
95 struct acpi_device_info *info;
96 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
97 struct acpi_find_child *find = context;
99 status = acpi_get_object_info(handle, &buffer);
100 if (ACPI_SUCCESS(status)) {
101 info = buffer.pointer;
102 if (info->address == find->address)
103 find->handle = handle;
104 kfree(buffer.pointer);
106 return AE_OK;
109 acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
111 struct acpi_find_child find = { NULL, address };
113 if (!parent)
114 return NULL;
115 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
116 1, do_acpi_find_child, &find, NULL);
117 return find.handle;
120 EXPORT_SYMBOL(acpi_get_child);
122 /* Link ACPI devices with physical devices */
123 static void acpi_glue_data_handler(acpi_handle handle,
124 u32 function, void *context)
126 /* we provide an empty handler */
129 /* Note: a success call will increase reference count by one */
130 struct device *acpi_get_physical_device(acpi_handle handle)
132 acpi_status status;
133 struct device *dev;
135 status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
136 if (ACPI_SUCCESS(status))
137 return get_device(dev);
138 return NULL;
141 EXPORT_SYMBOL(acpi_get_physical_device);
143 static int acpi_bind_one(struct device *dev, acpi_handle handle)
145 struct acpi_device *acpi_dev;
146 acpi_status status;
148 if (dev->archdata.acpi_handle) {
149 dev_warn(dev, "Drivers changed 'acpi_handle'\n");
150 return -EINVAL;
152 get_device(dev);
153 status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
154 if (ACPI_FAILURE(status)) {
155 put_device(dev);
156 return -EINVAL;
158 dev->archdata.acpi_handle = handle;
160 status = acpi_bus_get_device(handle, &acpi_dev);
161 if (!ACPI_FAILURE(status)) {
162 int ret;
164 ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
165 "firmware_node");
166 ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
167 "physical_node");
168 if (acpi_dev->wakeup.flags.valid) {
169 device_set_wakeup_capable(dev, true);
170 device_set_wakeup_enable(dev,
171 acpi_dev->wakeup.state.enabled);
175 return 0;
178 static int acpi_unbind_one(struct device *dev)
180 if (!dev->archdata.acpi_handle)
181 return 0;
182 if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
183 struct acpi_device *acpi_dev;
185 /* acpi_get_physical_device increase refcnt by one */
186 put_device(dev);
188 if (!acpi_bus_get_device(dev->archdata.acpi_handle,
189 &acpi_dev)) {
190 sysfs_remove_link(&dev->kobj, "firmware_node");
191 sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node");
194 acpi_detach_data(dev->archdata.acpi_handle,
195 acpi_glue_data_handler);
196 dev->archdata.acpi_handle = NULL;
197 /* acpi_bind_one increase refcnt by one */
198 put_device(dev);
199 } else {
200 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
202 return 0;
205 static int acpi_platform_notify(struct device *dev)
207 struct acpi_bus_type *type;
208 acpi_handle handle;
209 int ret = -EINVAL;
211 if (!dev->bus || !dev->parent) {
212 /* bridge devices genernally haven't bus or parent */
213 ret = acpi_find_bridge_device(dev, &handle);
214 goto end;
216 type = acpi_get_bus_type(dev->bus);
217 if (!type) {
218 DBG("No ACPI bus support for %s\n", dev_name(dev));
219 ret = -EINVAL;
220 goto end;
222 if ((ret = type->find_device(dev, &handle)) != 0)
223 DBG("Can't get handler for %s\n", dev_name(dev));
224 end:
225 if (!ret)
226 acpi_bind_one(dev, handle);
228 #if ACPI_GLUE_DEBUG
229 if (!ret) {
230 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
232 acpi_get_name(dev->archdata.acpi_handle,
233 ACPI_FULL_PATHNAME, &buffer);
234 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
235 kfree(buffer.pointer);
236 } else
237 DBG("Device %s -> No ACPI support\n", dev_name(dev));
238 #endif
240 return ret;
243 static int acpi_platform_notify_remove(struct device *dev)
245 acpi_unbind_one(dev);
246 return 0;
249 int __init init_acpi_device_notify(void)
251 if (platform_notify || platform_notify_remove) {
252 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
253 return 0;
255 platform_notify = acpi_platform_notify;
256 platform_notify_remove = acpi_platform_notify_remove;
257 return 0;