blob: 27a7072347ea78b8a707ad4312f245dbabd1a685 [file] [log] [blame]
David Shaohua Li4e10d122005-03-18 18:45:35 -05001/*
2 * Link physical devices with ACPI devices support
3 *
4 * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
5 * Copyright (c) 2005 Intel Corp.
6 *
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>
14
15#define ACPI_GLUE_DEBUG 0
16#if ACPI_GLUE_DEBUG
17#define DBG(x...) printk(PREFIX x)
18#else
Dave Jones4ebf83c2007-07-09 11:33:14 -070019#define DBG(x...) do { } while(0)
David Shaohua Li4e10d122005-03-18 18:45:35 -050020#endif
21static LIST_HEAD(bus_type_list);
22static DECLARE_RWSEM(bus_type_sem);
23
24int register_acpi_bus_type(struct acpi_bus_type *type)
25{
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);
Len Brown4be44fc2005-08-05 00:44:28 -040032 printk(KERN_INFO PREFIX "bus type %s registered\n",
33 type->bus->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050034 return 0;
35 }
36 return -ENODEV;
37}
38
David Shaohua Li4e10d122005-03-18 18:45:35 -050039int unregister_acpi_bus_type(struct acpi_bus_type *type)
40{
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);
Len Brown4be44fc2005-08-05 00:44:28 -040047 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
48 type->bus->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050049 return 0;
50 }
51 return -ENODEV;
52}
53
David Shaohua Li4e10d122005-03-18 18:45:35 -050054static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
55{
56 struct acpi_bus_type *tmp, *ret = NULL;
57
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;
63 }
64 }
65 up_read(&bus_type_sem);
66 return ret;
67}
68
69static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
70{
71 struct acpi_bus_type *tmp;
72 int ret = -ENODEV;
73
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;
79 }
80 }
81 up_read(&bus_type_sem);
82 return ret;
83}
84
David Shaohua Li4e10d122005-03-18 18:45:35 -050085/* Get device's handler per its address under its parent */
86struct acpi_find_child {
87 acpi_handle handle;
88 acpi_integer address;
89};
90
91static acpi_status
92do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
93{
94 acpi_status status;
95 struct acpi_device_info *info;
Jan Engelhardt50dd0962006-10-01 00:28:50 +020096 struct acpi_find_child *find = context;
David Shaohua Li4e10d122005-03-18 18:45:35 -050097
Bob Moore15b8dd52009-06-29 13:39:29 +080098 status = acpi_get_object_info(handle, &info);
David Shaohua Li4e10d122005-03-18 18:45:35 -050099 if (ACPI_SUCCESS(status)) {
David Shaohua Li4e10d122005-03-18 18:45:35 -0500100 if (info->address == find->address)
101 find->handle = handle;
Bob Moore15b8dd52009-06-29 13:39:29 +0800102 kfree(info);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500103 }
104 return AE_OK;
105}
106
107acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
108{
109 struct acpi_find_child find = { NULL, address };
110
111 if (!parent)
112 return NULL;
113 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
114 1, do_acpi_find_child, &find, NULL);
115 return find.handle;
116}
117
118EXPORT_SYMBOL(acpi_get_child);
119
120/* Link ACPI devices with physical devices */
121static void acpi_glue_data_handler(acpi_handle handle,
122 u32 function, void *context)
123{
124 /* we provide an empty handler */
125}
126
127/* Note: a success call will increase reference count by one */
128struct device *acpi_get_physical_device(acpi_handle handle)
129{
130 acpi_status status;
131 struct device *dev;
132
133 status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
134 if (ACPI_SUCCESS(status))
135 return get_device(dev);
136 return NULL;
137}
138
139EXPORT_SYMBOL(acpi_get_physical_device);
140
141static int acpi_bind_one(struct device *dev, acpi_handle handle)
142{
David Brownell10716952008-02-22 21:54:24 -0800143 struct acpi_device *acpi_dev;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500144 acpi_status status;
145
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100146 if (dev->archdata.acpi_handle) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200147 dev_warn(dev, "Drivers changed 'acpi_handle'\n");
David Shaohua Li4e10d122005-03-18 18:45:35 -0500148 return -EINVAL;
149 }
150 get_device(dev);
151 status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
152 if (ACPI_FAILURE(status)) {
153 put_device(dev);
154 return -EINVAL;
155 }
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100156 dev->archdata.acpi_handle = handle;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500157
David Brownell10716952008-02-22 21:54:24 -0800158 status = acpi_bus_get_device(handle, &acpi_dev);
159 if (!ACPI_FAILURE(status)) {
160 int ret;
161
162 ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
163 "firmware_node");
164 ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
165 "physical_node");
Rafael J. Wysocki76acae02008-10-03 15:23:49 -0700166 if (acpi_dev->wakeup.flags.valid) {
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200167 device_set_wakeup_capable(dev, true);
Rafael J. Wysocki76acae02008-10-03 15:23:49 -0700168 device_set_wakeup_enable(dev,
169 acpi_dev->wakeup.state.enabled);
170 }
David Brownell10716952008-02-22 21:54:24 -0800171 }
172
David Shaohua Li4e10d122005-03-18 18:45:35 -0500173 return 0;
174}
175
176static int acpi_unbind_one(struct device *dev)
177{
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100178 if (!dev->archdata.acpi_handle)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500179 return 0;
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100180 if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
David Brownell10716952008-02-22 21:54:24 -0800181 struct acpi_device *acpi_dev;
182
David Shaohua Li4e10d122005-03-18 18:45:35 -0500183 /* acpi_get_physical_device increase refcnt by one */
184 put_device(dev);
David Brownell10716952008-02-22 21:54:24 -0800185
186 if (!acpi_bus_get_device(dev->archdata.acpi_handle,
187 &acpi_dev)) {
188 sysfs_remove_link(&dev->kobj, "firmware_node");
189 sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node");
190 }
191
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100192 acpi_detach_data(dev->archdata.acpi_handle,
193 acpi_glue_data_handler);
194 dev->archdata.acpi_handle = NULL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500195 /* acpi_bind_one increase refcnt by one */
196 put_device(dev);
197 } else {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200198 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
David Shaohua Li4e10d122005-03-18 18:45:35 -0500199 }
200 return 0;
201}
202
203static int acpi_platform_notify(struct device *dev)
204{
205 struct acpi_bus_type *type;
206 acpi_handle handle;
207 int ret = -EINVAL;
208
209 if (!dev->bus || !dev->parent) {
210 /* bridge devices genernally haven't bus or parent */
211 ret = acpi_find_bridge_device(dev, &handle);
212 goto end;
213 }
214 type = acpi_get_bus_type(dev->bus);
215 if (!type) {
Kay Sieversdb1461a2009-01-25 23:40:56 +0100216 DBG("No ACPI bus support for %s\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500217 ret = -EINVAL;
218 goto end;
219 }
220 if ((ret = type->find_device(dev, &handle)) != 0)
Kay Sieversdb1461a2009-01-25 23:40:56 +0100221 DBG("Can't get handler for %s\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500222 end:
223 if (!ret)
224 acpi_bind_one(dev, handle);
225
226#if ACPI_GLUE_DEBUG
227 if (!ret) {
228 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
229
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100230 acpi_get_name(dev->archdata.acpi_handle,
231 ACPI_FULL_PATHNAME, &buffer);
Kay Sieversdb1461a2009-01-25 23:40:56 +0100232 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
Len Brown02438d82006-06-30 03:19:10 -0400233 kfree(buffer.pointer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500234 } else
Kay Sieversdb1461a2009-01-25 23:40:56 +0100235 DBG("Device %s -> No ACPI support\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500236#endif
237
238 return ret;
239}
240
241static int acpi_platform_notify_remove(struct device *dev)
242{
243 acpi_unbind_one(dev);
244 return 0;
245}
246
Bjorn Helgaas0e465172009-03-24 16:50:09 -0600247int __init init_acpi_device_notify(void)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500248{
David Shaohua Li4e10d122005-03-18 18:45:35 -0500249 if (platform_notify || platform_notify_remove) {
250 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
251 return 0;
252 }
253 platform_notify = acpi_platform_notify;
254 platform_notify_remove = acpi_platform_notify_remove;
255 return 0;
256}