blob: c6645f26224b2c820375a1a50d53312e1d541048 [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
Len Browna192a952009-07-28 16:45:54 -040015#include "internal.h"
16
David Shaohua Li4e10d122005-03-18 18:45:35 -050017#define ACPI_GLUE_DEBUG 0
18#if ACPI_GLUE_DEBUG
19#define DBG(x...) printk(PREFIX x)
20#else
Dave Jones4ebf83c2007-07-09 11:33:14 -070021#define DBG(x...) do { } while(0)
David Shaohua Li4e10d122005-03-18 18:45:35 -050022#endif
23static LIST_HEAD(bus_type_list);
24static DECLARE_RWSEM(bus_type_sem);
25
26int register_acpi_bus_type(struct acpi_bus_type *type)
27{
28 if (acpi_disabled)
29 return -ENODEV;
30 if (type && type->bus && type->find_device) {
31 down_write(&bus_type_sem);
32 list_add_tail(&type->list, &bus_type_list);
33 up_write(&bus_type_sem);
Len Brown4be44fc2005-08-05 00:44:28 -040034 printk(KERN_INFO PREFIX "bus type %s registered\n",
35 type->bus->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050036 return 0;
37 }
38 return -ENODEV;
39}
40
David Shaohua Li4e10d122005-03-18 18:45:35 -050041int unregister_acpi_bus_type(struct acpi_bus_type *type)
42{
43 if (acpi_disabled)
44 return 0;
45 if (type) {
46 down_write(&bus_type_sem);
47 list_del_init(&type->list);
48 up_write(&bus_type_sem);
Len Brown4be44fc2005-08-05 00:44:28 -040049 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
50 type->bus->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050051 return 0;
52 }
53 return -ENODEV;
54}
55
David Shaohua Li4e10d122005-03-18 18:45:35 -050056static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
57{
58 struct acpi_bus_type *tmp, *ret = NULL;
59
60 down_read(&bus_type_sem);
61 list_for_each_entry(tmp, &bus_type_list, list) {
62 if (tmp->bus == type) {
63 ret = tmp;
64 break;
65 }
66 }
67 up_read(&bus_type_sem);
68 return ret;
69}
70
71static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
72{
73 struct acpi_bus_type *tmp;
74 int ret = -ENODEV;
75
76 down_read(&bus_type_sem);
77 list_for_each_entry(tmp, &bus_type_list, list) {
78 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
79 ret = 0;
80 break;
81 }
82 }
83 up_read(&bus_type_sem);
84 return ret;
85}
86
David Shaohua Li4e10d122005-03-18 18:45:35 -050087/* Get device's handler per its address under its parent */
88struct acpi_find_child {
89 acpi_handle handle;
90 acpi_integer address;
91};
92
93static acpi_status
94do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
95{
96 acpi_status status;
97 struct acpi_device_info *info;
Jan Engelhardt50dd0962006-10-01 00:28:50 +020098 struct acpi_find_child *find = context;
David Shaohua Li4e10d122005-03-18 18:45:35 -050099
Bob Moore15b8dd52009-06-29 13:39:29 +0800100 status = acpi_get_object_info(handle, &info);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500101 if (ACPI_SUCCESS(status)) {
David Shaohua Li4e10d122005-03-18 18:45:35 -0500102 if (info->address == find->address)
103 find->handle = handle;
Bob Moore15b8dd52009-06-29 13:39:29 +0800104 kfree(info);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500105 }
106 return AE_OK;
107}
108
109acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
110{
111 struct acpi_find_child find = { NULL, address };
112
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;
118}
119
120EXPORT_SYMBOL(acpi_get_child);
121
122/* Link ACPI devices with physical devices */
123static void acpi_glue_data_handler(acpi_handle handle,
Bob Moore8e4319c2009-06-29 13:43:27 +0800124 void *context)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500125{
126 /* we provide an empty handler */
127}
128
129/* Note: a success call will increase reference count by one */
130struct device *acpi_get_physical_device(acpi_handle handle)
131{
132 acpi_status status;
133 struct device *dev;
134
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;
139}
140
141EXPORT_SYMBOL(acpi_get_physical_device);
142
143static int acpi_bind_one(struct device *dev, acpi_handle handle)
144{
David Brownell10716952008-02-22 21:54:24 -0800145 struct acpi_device *acpi_dev;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500146 acpi_status status;
147
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100148 if (dev->archdata.acpi_handle) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200149 dev_warn(dev, "Drivers changed 'acpi_handle'\n");
David Shaohua Li4e10d122005-03-18 18:45:35 -0500150 return -EINVAL;
151 }
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;
157 }
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100158 dev->archdata.acpi_handle = handle;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500159
David Brownell10716952008-02-22 21:54:24 -0800160 status = acpi_bus_get_device(handle, &acpi_dev);
161 if (!ACPI_FAILURE(status)) {
162 int ret;
163
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");
Rafael J. Wysocki76acae02008-10-03 15:23:49 -0700168 if (acpi_dev->wakeup.flags.valid) {
Rafael J. Wysockieb9d0fe2008-07-07 03:34:48 +0200169 device_set_wakeup_capable(dev, true);
Rafael J. Wysocki76acae02008-10-03 15:23:49 -0700170 device_set_wakeup_enable(dev,
171 acpi_dev->wakeup.state.enabled);
172 }
David Brownell10716952008-02-22 21:54:24 -0800173 }
174
David Shaohua Li4e10d122005-03-18 18:45:35 -0500175 return 0;
176}
177
178static int acpi_unbind_one(struct device *dev)
179{
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100180 if (!dev->archdata.acpi_handle)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500181 return 0;
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100182 if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
David Brownell10716952008-02-22 21:54:24 -0800183 struct acpi_device *acpi_dev;
184
David Shaohua Li4e10d122005-03-18 18:45:35 -0500185 /* acpi_get_physical_device increase refcnt by one */
186 put_device(dev);
David Brownell10716952008-02-22 21:54:24 -0800187
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");
192 }
193
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100194 acpi_detach_data(dev->archdata.acpi_handle,
195 acpi_glue_data_handler);
196 dev->archdata.acpi_handle = NULL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500197 /* acpi_bind_one increase refcnt by one */
198 put_device(dev);
199 } else {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200200 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
David Shaohua Li4e10d122005-03-18 18:45:35 -0500201 }
202 return 0;
203}
204
205static int acpi_platform_notify(struct device *dev)
206{
207 struct acpi_bus_type *type;
208 acpi_handle handle;
209 int ret = -EINVAL;
210
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;
215 }
216 type = acpi_get_bus_type(dev->bus);
217 if (!type) {
Kay Sieversdb1461a2009-01-25 23:40:56 +0100218 DBG("No ACPI bus support for %s\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500219 ret = -EINVAL;
220 goto end;
221 }
222 if ((ret = type->find_device(dev, &handle)) != 0)
Kay Sieversdb1461a2009-01-25 23:40:56 +0100223 DBG("Can't get handler for %s\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500224 end:
225 if (!ret)
226 acpi_bind_one(dev, handle);
227
228#if ACPI_GLUE_DEBUG
229 if (!ret) {
230 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
231
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100232 acpi_get_name(dev->archdata.acpi_handle,
233 ACPI_FULL_PATHNAME, &buffer);
Kay Sieversdb1461a2009-01-25 23:40:56 +0100234 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
Len Brown02438d82006-06-30 03:19:10 -0400235 kfree(buffer.pointer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500236 } else
Kay Sieversdb1461a2009-01-25 23:40:56 +0100237 DBG("Device %s -> No ACPI support\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500238#endif
239
240 return ret;
241}
242
243static int acpi_platform_notify_remove(struct device *dev)
244{
245 acpi_unbind_one(dev);
246 return 0;
247}
248
Bjorn Helgaas0e465172009-03-24 16:50:09 -0600249int __init init_acpi_device_notify(void)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500250{
David Shaohua Li4e10d122005-03-18 18:45:35 -0500251 if (platform_notify || platform_notify_remove) {
252 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
253 return 0;
254 }
255 platform_notify = acpi_platform_notify;
256 platform_notify_remove = acpi_platform_notify_remove;
257 return 0;
258}