blob: 40a84cc6740c6c6522afc6bac6d5d5d2178a86bd [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
Joe Perches23415eb2012-12-18 06:31:30 +000021#define DBG(fmt, ...) \
22 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
David Shaohua Li4e10d122005-03-18 18:45:35 -050023#else
Joe Perches23415eb2012-12-18 06:31:30 +000024#define DBG(fmt, ...) \
25do { \
26 if (0) \
27 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \
28} while (0)
David Shaohua Li4e10d122005-03-18 18:45:35 -050029#endif
30static LIST_HEAD(bus_type_list);
31static DECLARE_RWSEM(bus_type_sem);
32
Lan Tianyu1033f902012-08-17 14:44:09 +080033#define PHYSICAL_NODE_STRING "physical_node"
34
David Shaohua Li4e10d122005-03-18 18:45:35 -050035int register_acpi_bus_type(struct acpi_bus_type *type)
36{
37 if (acpi_disabled)
38 return -ENODEV;
Rafael J. Wysocki53540092013-03-03 22:35:20 +010039 if (type && type->match && type->find_device) {
David Shaohua Li4e10d122005-03-18 18:45:35 -050040 down_write(&bus_type_sem);
41 list_add_tail(&type->list, &bus_type_list);
42 up_write(&bus_type_sem);
Rafael J. Wysocki53540092013-03-03 22:35:20 +010043 printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050044 return 0;
45 }
46 return -ENODEV;
47}
Jeff Garzik91e4d5a2012-07-25 14:24:13 -040048EXPORT_SYMBOL_GPL(register_acpi_bus_type);
David Shaohua Li4e10d122005-03-18 18:45:35 -050049
David Shaohua Li4e10d122005-03-18 18:45:35 -050050int unregister_acpi_bus_type(struct acpi_bus_type *type)
51{
52 if (acpi_disabled)
53 return 0;
54 if (type) {
55 down_write(&bus_type_sem);
56 list_del_init(&type->list);
57 up_write(&bus_type_sem);
Rafael J. Wysocki53540092013-03-03 22:35:20 +010058 printk(KERN_INFO PREFIX "bus type %s unregistered\n",
59 type->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050060 return 0;
61 }
62 return -ENODEV;
63}
Jeff Garzik91e4d5a2012-07-25 14:24:13 -040064EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
David Shaohua Li4e10d122005-03-18 18:45:35 -050065
Rafael J. Wysocki53540092013-03-03 22:35:20 +010066static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
David Shaohua Li4e10d122005-03-18 18:45:35 -050067{
68 struct acpi_bus_type *tmp, *ret = NULL;
69
70 down_read(&bus_type_sem);
71 list_for_each_entry(tmp, &bus_type_list, list) {
Rafael J. Wysocki53540092013-03-03 22:35:20 +010072 if (tmp->match(dev)) {
David Shaohua Li4e10d122005-03-18 18:45:35 -050073 ret = tmp;
74 break;
75 }
76 }
77 up_read(&bus_type_sem);
78 return ret;
79}
80
Rafael J. Wysocki33f767d2013-01-10 13:13:49 +010081static acpi_status do_acpi_find_child(acpi_handle handle, u32 lvl_not_used,
82 void *addr_p, void **ret_p)
David Shaohua Li4e10d122005-03-18 18:45:35 -050083{
Rafael J. Wysocki33f767d2013-01-10 13:13:49 +010084 unsigned long long addr;
David Shaohua Li4e10d122005-03-18 18:45:35 -050085 acpi_status status;
David Shaohua Li4e10d122005-03-18 18:45:35 -050086
Rafael J. Wysocki33f767d2013-01-10 13:13:49 +010087 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
88 if (ACPI_SUCCESS(status) && addr == *((u64 *)addr_p)) {
89 *ret_p = handle;
90 return AE_CTRL_TERMINATE;
David Shaohua Li4e10d122005-03-18 18:45:35 -050091 }
92 return AE_OK;
93}
94
Lin Ming439913f2010-01-28 10:53:19 +080095acpi_handle acpi_get_child(acpi_handle parent, u64 address)
David Shaohua Li4e10d122005-03-18 18:45:35 -050096{
Rafael J. Wysocki33f767d2013-01-10 13:13:49 +010097 void *ret = NULL;
David Shaohua Li4e10d122005-03-18 18:45:35 -050098
99 if (!parent)
100 return NULL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500101
Rafael J. Wysocki33f767d2013-01-10 13:13:49 +0100102 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, NULL,
103 do_acpi_find_child, &address, &ret);
104 return (acpi_handle)ret;
105}
David Shaohua Li4e10d122005-03-18 18:45:35 -0500106EXPORT_SYMBOL(acpi_get_child);
107
David Shaohua Li4e10d122005-03-18 18:45:35 -0500108static int acpi_bind_one(struct device *dev, acpi_handle handle)
109{
David Brownell10716952008-02-22 21:54:24 -0800110 struct acpi_device *acpi_dev;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500111 acpi_status status;
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100112 struct acpi_device_physical_node *physical_node, *pn;
Lan Tianyu1033f902012-08-17 14:44:09 +0800113 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
114 int retval = -EINVAL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500115
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100116 if (ACPI_HANDLE(dev)) {
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100117 if (handle) {
118 dev_warn(dev, "ACPI handle is already set\n");
119 return -EINVAL;
120 } else {
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100121 handle = ACPI_HANDLE(dev);
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100122 }
David Shaohua Li4e10d122005-03-18 18:45:35 -0500123 }
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100124 if (!handle)
125 return -EINVAL;
Lan Tianyu1033f902012-08-17 14:44:09 +0800126
David Shaohua Li4e10d122005-03-18 18:45:35 -0500127 get_device(dev);
Lan Tianyu1033f902012-08-17 14:44:09 +0800128 status = acpi_bus_get_device(handle, &acpi_dev);
129 if (ACPI_FAILURE(status))
130 goto err;
131
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100132 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
Lan Tianyu1033f902012-08-17 14:44:09 +0800133 if (!physical_node) {
134 retval = -ENOMEM;
135 goto err;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500136 }
Lan Tianyu1033f902012-08-17 14:44:09 +0800137
138 mutex_lock(&acpi_dev->physical_node_lock);
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100139
140 /* Sanity check. */
141 list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
142 if (pn->dev == dev) {
143 dev_warn(dev, "Already associated with ACPI node\n");
144 goto err_free;
145 }
146
Lan Tianyu1033f902012-08-17 14:44:09 +0800147 /* allocate physical node id according to physical_node_id_bitmap */
148 physical_node->node_id =
149 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
150 ACPI_MAX_PHYSICAL_NODE);
151 if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
152 retval = -ENOSPC;
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100153 goto err_free;
Lan Tianyu1033f902012-08-17 14:44:09 +0800154 }
155
156 set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
157 physical_node->dev = dev;
158 list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
159 acpi_dev->physical_node_count++;
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100160
Lan Tianyu1033f902012-08-17 14:44:09 +0800161 mutex_unlock(&acpi_dev->physical_node_lock);
162
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100163 if (!ACPI_HANDLE(dev))
164 ACPI_HANDLE_SET(dev, acpi_dev->handle);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500165
Lan Tianyu1033f902012-08-17 14:44:09 +0800166 if (!physical_node->node_id)
167 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
168 else
169 sprintf(physical_node_name,
170 "physical_node%d", physical_node->node_id);
171 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
172 physical_node_name);
173 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
174 "firmware_node");
David Brownell10716952008-02-22 21:54:24 -0800175
Lan Tianyu1033f902012-08-17 14:44:09 +0800176 if (acpi_dev->wakeup.flags.valid)
177 device_set_wakeup_capable(dev, true);
David Brownell10716952008-02-22 21:54:24 -0800178
David Shaohua Li4e10d122005-03-18 18:45:35 -0500179 return 0;
Lan Tianyu1033f902012-08-17 14:44:09 +0800180
181 err:
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100182 ACPI_HANDLE_SET(dev, NULL);
Lan Tianyu1033f902012-08-17 14:44:09 +0800183 put_device(dev);
184 return retval;
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100185
186 err_free:
187 mutex_unlock(&acpi_dev->physical_node_lock);
188 kfree(physical_node);
189 goto err;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500190}
191
192static int acpi_unbind_one(struct device *dev)
193{
Lan Tianyu1033f902012-08-17 14:44:09 +0800194 struct acpi_device_physical_node *entry;
195 struct acpi_device *acpi_dev;
196 acpi_status status;
197 struct list_head *node, *next;
198
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100199 if (!ACPI_HANDLE(dev))
David Shaohua Li4e10d122005-03-18 18:45:35 -0500200 return 0;
David Brownell10716952008-02-22 21:54:24 -0800201
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100202 status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
Lan Tianyu1033f902012-08-17 14:44:09 +0800203 if (ACPI_FAILURE(status))
204 goto err;
David Brownell10716952008-02-22 21:54:24 -0800205
Lan Tianyu1033f902012-08-17 14:44:09 +0800206 mutex_lock(&acpi_dev->physical_node_lock);
207 list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
208 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
David Brownell10716952008-02-22 21:54:24 -0800209
Lan Tianyu1033f902012-08-17 14:44:09 +0800210 entry = list_entry(node, struct acpi_device_physical_node,
211 node);
212 if (entry->dev != dev)
213 continue;
214
215 list_del(node);
216 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
217
218 acpi_dev->physical_node_count--;
219
220 if (!entry->node_id)
221 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
222 else
223 sprintf(physical_node_name,
224 "physical_node%d", entry->node_id);
225
226 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
227 sysfs_remove_link(&dev->kobj, "firmware_node");
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100228 ACPI_HANDLE_SET(dev, NULL);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500229 /* acpi_bind_one increase refcnt by one */
230 put_device(dev);
Lan Tianyu1033f902012-08-17 14:44:09 +0800231 kfree(entry);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500232 }
Lan Tianyu1033f902012-08-17 14:44:09 +0800233 mutex_unlock(&acpi_dev->physical_node_lock);
234
David Shaohua Li4e10d122005-03-18 18:45:35 -0500235 return 0;
Lan Tianyu1033f902012-08-17 14:44:09 +0800236
237err:
238 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
239 return -EINVAL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500240}
241
242static int acpi_platform_notify(struct device *dev)
243{
Rafael J. Wysocki53540092013-03-03 22:35:20 +0100244 struct acpi_bus_type *type = acpi_get_bus_type(dev);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500245 acpi_handle handle;
Rafael J. Wysocki11909ca2012-12-23 00:02:13 +0100246 int ret;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500247
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100248 ret = acpi_bind_one(dev, NULL);
Rafael J. Wysocki92414482013-03-03 22:35:44 +0100249 if (ret && type) {
Rafael J. Wysocki11909ca2012-12-23 00:02:13 +0100250 ret = type->find_device(dev, &handle);
251 if (ret) {
252 DBG("Unable to get handle for %s\n", dev_name(dev));
253 goto out;
254 }
255 ret = acpi_bind_one(dev, handle);
256 if (ret)
257 goto out;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500258 }
Rafael J. Wysocki11909ca2012-12-23 00:02:13 +0100259
260 if (type && type->setup)
261 type->setup(dev);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500262
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100263 out:
David Shaohua Li4e10d122005-03-18 18:45:35 -0500264#if ACPI_GLUE_DEBUG
265 if (!ret) {
266 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
267
Yinghai Lua412a112013-01-12 14:00:06 +0100268 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
Kay Sieversdb1461a2009-01-25 23:40:56 +0100269 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
Len Brown02438d82006-06-30 03:19:10 -0400270 kfree(buffer.pointer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500271 } else
Kay Sieversdb1461a2009-01-25 23:40:56 +0100272 DBG("Device %s -> No ACPI support\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500273#endif
274
275 return ret;
276}
277
278static int acpi_platform_notify_remove(struct device *dev)
279{
Rafael J. Wysocki11909ca2012-12-23 00:02:13 +0100280 struct acpi_bus_type *type;
281
Rafael J. Wysocki53540092013-03-03 22:35:20 +0100282 type = acpi_get_bus_type(dev);
Rafael J. Wysocki11909ca2012-12-23 00:02:13 +0100283 if (type && type->cleanup)
284 type->cleanup(dev);
285
David Shaohua Li4e10d122005-03-18 18:45:35 -0500286 acpi_unbind_one(dev);
287 return 0;
288}
289
Bjorn Helgaas0e465172009-03-24 16:50:09 -0600290int __init init_acpi_device_notify(void)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500291{
David Shaohua Li4e10d122005-03-18 18:45:35 -0500292 if (platform_notify || platform_notify_remove) {
293 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
294 return 0;
295 }
296 platform_notify = acpi_platform_notify;
297 platform_notify_remove = acpi_platform_notify_remove;
298 return 0;
299}