blob: d1a2d74033e945237afdef3fac02aea87cdebbdd [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 */
Paul Gortmaker214f2c92011-10-26 16:22:14 -04009#include <linux/export.h>
David Shaohua Li4e10d122005-03-18 18:45:35 -050010#include <linux/init.h>
11#include <linux/list.h>
12#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Shaohua Li4e10d122005-03-18 18:45:35 -050014#include <linux/rwsem.h>
15#include <linux/acpi.h>
16
Len Browna192a952009-07-28 16:45:54 -040017#include "internal.h"
18
David Shaohua Li4e10d122005-03-18 18:45:35 -050019#define ACPI_GLUE_DEBUG 0
20#if ACPI_GLUE_DEBUG
21#define DBG(x...) printk(PREFIX x)
22#else
Dave Jones4ebf83c2007-07-09 11:33:14 -070023#define DBG(x...) do { } while(0)
David Shaohua Li4e10d122005-03-18 18:45:35 -050024#endif
25static LIST_HEAD(bus_type_list);
26static DECLARE_RWSEM(bus_type_sem);
27
Lan Tianyu1033f902012-08-17 14:44:09 +080028#define PHYSICAL_NODE_STRING "physical_node"
29
David Shaohua Li4e10d122005-03-18 18:45:35 -050030int register_acpi_bus_type(struct acpi_bus_type *type)
31{
32 if (acpi_disabled)
33 return -ENODEV;
34 if (type && type->bus && type->find_device) {
35 down_write(&bus_type_sem);
36 list_add_tail(&type->list, &bus_type_list);
37 up_write(&bus_type_sem);
Len Brown4be44fc2005-08-05 00:44:28 -040038 printk(KERN_INFO PREFIX "bus type %s registered\n",
39 type->bus->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050040 return 0;
41 }
42 return -ENODEV;
43}
Jeff Garzik91e4d5a2012-07-25 14:24:13 -040044EXPORT_SYMBOL_GPL(register_acpi_bus_type);
David Shaohua Li4e10d122005-03-18 18:45:35 -050045
David Shaohua Li4e10d122005-03-18 18:45:35 -050046int unregister_acpi_bus_type(struct acpi_bus_type *type)
47{
48 if (acpi_disabled)
49 return 0;
50 if (type) {
51 down_write(&bus_type_sem);
52 list_del_init(&type->list);
53 up_write(&bus_type_sem);
Len Brown4be44fc2005-08-05 00:44:28 -040054 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
55 type->bus->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050056 return 0;
57 }
58 return -ENODEV;
59}
Jeff Garzik91e4d5a2012-07-25 14:24:13 -040060EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
David Shaohua Li4e10d122005-03-18 18:45:35 -050061
David Shaohua Li4e10d122005-03-18 18:45:35 -050062static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
63{
64 struct acpi_bus_type *tmp, *ret = NULL;
65
66 down_read(&bus_type_sem);
67 list_for_each_entry(tmp, &bus_type_list, list) {
68 if (tmp->bus == type) {
69 ret = tmp;
70 break;
71 }
72 }
73 up_read(&bus_type_sem);
74 return ret;
75}
76
77static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
78{
79 struct acpi_bus_type *tmp;
80 int ret = -ENODEV;
81
82 down_read(&bus_type_sem);
83 list_for_each_entry(tmp, &bus_type_list, list) {
84 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
85 ret = 0;
86 break;
87 }
88 }
89 up_read(&bus_type_sem);
90 return ret;
91}
92
David Shaohua Li4e10d122005-03-18 18:45:35 -050093/* Get device's handler per its address under its parent */
94struct acpi_find_child {
95 acpi_handle handle;
Lin Ming439913f2010-01-28 10:53:19 +080096 u64 address;
David Shaohua Li4e10d122005-03-18 18:45:35 -050097};
98
99static acpi_status
100do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
101{
102 acpi_status status;
103 struct acpi_device_info *info;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200104 struct acpi_find_child *find = context;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500105
Bob Moore15b8dd52009-06-29 13:39:29 +0800106 status = acpi_get_object_info(handle, &info);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500107 if (ACPI_SUCCESS(status)) {
Zhao Yakui108029f2010-07-13 03:36:08 +0000108 if ((info->address == find->address)
109 && (info->valid & ACPI_VALID_ADR))
David Shaohua Li4e10d122005-03-18 18:45:35 -0500110 find->handle = handle;
Bob Moore15b8dd52009-06-29 13:39:29 +0800111 kfree(info);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500112 }
113 return AE_OK;
114}
115
Lin Ming439913f2010-01-28 10:53:19 +0800116acpi_handle acpi_get_child(acpi_handle parent, u64 address)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500117{
118 struct acpi_find_child find = { NULL, address };
119
120 if (!parent)
121 return NULL;
122 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
Lin Ming22635762009-11-13 10:06:08 +0800123 1, do_acpi_find_child, NULL, &find, NULL);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500124 return find.handle;
125}
126
127EXPORT_SYMBOL(acpi_get_child);
128
David Shaohua Li4e10d122005-03-18 18:45:35 -0500129static int acpi_bind_one(struct device *dev, acpi_handle handle)
130{
David Brownell10716952008-02-22 21:54:24 -0800131 struct acpi_device *acpi_dev;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500132 acpi_status status;
Lan Tianyu1033f902012-08-17 14:44:09 +0800133 struct acpi_device_physical_node *physical_node;
134 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
135 int retval = -EINVAL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500136
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100137 if (dev->archdata.acpi_handle) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200138 dev_warn(dev, "Drivers changed 'acpi_handle'\n");
David Shaohua Li4e10d122005-03-18 18:45:35 -0500139 return -EINVAL;
140 }
Lan Tianyu1033f902012-08-17 14:44:09 +0800141
David Shaohua Li4e10d122005-03-18 18:45:35 -0500142 get_device(dev);
Lan Tianyu1033f902012-08-17 14:44:09 +0800143 status = acpi_bus_get_device(handle, &acpi_dev);
144 if (ACPI_FAILURE(status))
145 goto err;
146
147 physical_node = kzalloc(sizeof(struct acpi_device_physical_node),
148 GFP_KERNEL);
149 if (!physical_node) {
150 retval = -ENOMEM;
151 goto err;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500152 }
Lan Tianyu1033f902012-08-17 14:44:09 +0800153
154 mutex_lock(&acpi_dev->physical_node_lock);
155 /* allocate physical node id according to physical_node_id_bitmap */
156 physical_node->node_id =
157 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
158 ACPI_MAX_PHYSICAL_NODE);
159 if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
160 retval = -ENOSPC;
161 mutex_unlock(&acpi_dev->physical_node_lock);
162 goto err;
163 }
164
165 set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
166 physical_node->dev = dev;
167 list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
168 acpi_dev->physical_node_count++;
169 mutex_unlock(&acpi_dev->physical_node_lock);
170
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100171 dev->archdata.acpi_handle = handle;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500172
Lan Tianyu1033f902012-08-17 14:44:09 +0800173 if (!physical_node->node_id)
174 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
175 else
176 sprintf(physical_node_name,
177 "physical_node%d", physical_node->node_id);
178 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
179 physical_node_name);
180 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
181 "firmware_node");
David Brownell10716952008-02-22 21:54:24 -0800182
Lan Tianyu1033f902012-08-17 14:44:09 +0800183 if (acpi_dev->wakeup.flags.valid)
184 device_set_wakeup_capable(dev, true);
David Brownell10716952008-02-22 21:54:24 -0800185
David Shaohua Li4e10d122005-03-18 18:45:35 -0500186 return 0;
Lan Tianyu1033f902012-08-17 14:44:09 +0800187
188 err:
189 put_device(dev);
190 return retval;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500191}
192
193static int acpi_unbind_one(struct device *dev)
194{
Lan Tianyu1033f902012-08-17 14:44:09 +0800195 struct acpi_device_physical_node *entry;
196 struct acpi_device *acpi_dev;
197 acpi_status status;
198 struct list_head *node, *next;
199
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100200 if (!dev->archdata.acpi_handle)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500201 return 0;
David Brownell10716952008-02-22 21:54:24 -0800202
Lan Tianyu1033f902012-08-17 14:44:09 +0800203 status = acpi_bus_get_device(dev->archdata.acpi_handle,
204 &acpi_dev);
205 if (ACPI_FAILURE(status))
206 goto err;
David Brownell10716952008-02-22 21:54:24 -0800207
Lan Tianyu1033f902012-08-17 14:44:09 +0800208 mutex_lock(&acpi_dev->physical_node_lock);
209 list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
210 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
David Brownell10716952008-02-22 21:54:24 -0800211
Lan Tianyu1033f902012-08-17 14:44:09 +0800212 entry = list_entry(node, struct acpi_device_physical_node,
213 node);
214 if (entry->dev != dev)
215 continue;
216
217 list_del(node);
218 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
219
220 acpi_dev->physical_node_count--;
221
222 if (!entry->node_id)
223 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
224 else
225 sprintf(physical_node_name,
226 "physical_node%d", entry->node_id);
227
228 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
229 sysfs_remove_link(&dev->kobj, "firmware_node");
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100230 dev->archdata.acpi_handle = NULL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500231 /* acpi_bind_one increase refcnt by one */
232 put_device(dev);
Lan Tianyu1033f902012-08-17 14:44:09 +0800233 kfree(entry);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500234 }
Lan Tianyu1033f902012-08-17 14:44:09 +0800235 mutex_unlock(&acpi_dev->physical_node_lock);
236
David Shaohua Li4e10d122005-03-18 18:45:35 -0500237 return 0;
Lan Tianyu1033f902012-08-17 14:44:09 +0800238
239err:
240 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
241 return -EINVAL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500242}
243
244static int acpi_platform_notify(struct device *dev)
245{
246 struct acpi_bus_type *type;
247 acpi_handle handle;
248 int ret = -EINVAL;
249
250 if (!dev->bus || !dev->parent) {
251 /* bridge devices genernally haven't bus or parent */
252 ret = acpi_find_bridge_device(dev, &handle);
253 goto end;
254 }
255 type = acpi_get_bus_type(dev->bus);
256 if (!type) {
Kay Sieversdb1461a2009-01-25 23:40:56 +0100257 DBG("No ACPI bus support for %s\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500258 ret = -EINVAL;
259 goto end;
260 }
261 if ((ret = type->find_device(dev, &handle)) != 0)
Kay Sieversdb1461a2009-01-25 23:40:56 +0100262 DBG("Can't get handler for %s\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500263 end:
264 if (!ret)
265 acpi_bind_one(dev, handle);
266
267#if ACPI_GLUE_DEBUG
268 if (!ret) {
269 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
270
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100271 acpi_get_name(dev->archdata.acpi_handle,
272 ACPI_FULL_PATHNAME, &buffer);
Kay Sieversdb1461a2009-01-25 23:40:56 +0100273 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
Len Brown02438d82006-06-30 03:19:10 -0400274 kfree(buffer.pointer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500275 } else
Kay Sieversdb1461a2009-01-25 23:40:56 +0100276 DBG("Device %s -> No ACPI support\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500277#endif
278
279 return ret;
280}
281
282static int acpi_platform_notify_remove(struct device *dev)
283{
284 acpi_unbind_one(dev);
285 return 0;
286}
287
Bjorn Helgaas0e465172009-03-24 16:50:09 -0600288int __init init_acpi_device_notify(void)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500289{
David Shaohua Li4e10d122005-03-18 18:45:35 -0500290 if (platform_notify || platform_notify_remove) {
291 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
292 return 0;
293 }
294 platform_notify = acpi_platform_notify;
295 platform_notify_remove = acpi_platform_notify_remove;
296 return 0;
297}